|
| 1 | +// Copyright 2024 LiveKit, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import 'package:flutter_webrtc/flutter_webrtc.dart' as rtc; |
| 16 | +import 'package:meta/meta.dart'; |
| 17 | + |
| 18 | +import 'priority.dart'; |
| 19 | + |
| 20 | +/// A type that represents audio encoding information. |
| 21 | +@immutable |
| 22 | +class AudioEncoding { |
| 23 | + /// Maximum bitrate for the audio track. |
| 24 | + final int maxBitrate; |
| 25 | + |
| 26 | + /// Priority for bandwidth allocation. |
| 27 | + final Priority? bitratePriority; |
| 28 | + |
| 29 | + /// Priority for DSCP marking. Requires `RTCConfiguration.isDscpEnabled` to be true. |
| 30 | + final Priority? networkPriority; |
| 31 | + |
| 32 | + const AudioEncoding({ |
| 33 | + required this.maxBitrate, |
| 34 | + this.bitratePriority, |
| 35 | + this.networkPriority, |
| 36 | + }); |
| 37 | + |
| 38 | + AudioEncoding copyWith({ |
| 39 | + int? maxBitrate, |
| 40 | + Priority? bitratePriority, |
| 41 | + Priority? networkPriority, |
| 42 | + }) => |
| 43 | + AudioEncoding( |
| 44 | + maxBitrate: maxBitrate ?? this.maxBitrate, |
| 45 | + bitratePriority: bitratePriority ?? this.bitratePriority, |
| 46 | + networkPriority: networkPriority ?? this.networkPriority, |
| 47 | + ); |
| 48 | + |
| 49 | + @override |
| 50 | + String toString() => |
| 51 | + '${runtimeType}(maxBitrate: ${maxBitrate}, bitratePriority: ${bitratePriority}, networkPriority: ${networkPriority})'; |
| 52 | + |
| 53 | + @override |
| 54 | + bool operator ==(Object other) => |
| 55 | + identical(this, other) || |
| 56 | + other is AudioEncoding && |
| 57 | + maxBitrate == other.maxBitrate && |
| 58 | + bitratePriority == other.bitratePriority && |
| 59 | + networkPriority == other.networkPriority; |
| 60 | + |
| 61 | + @override |
| 62 | + int get hashCode => Object.hash(maxBitrate, bitratePriority, networkPriority); |
| 63 | + |
| 64 | + static const presetTelephone = AudioEncoding(maxBitrate: 12000); |
| 65 | + static const presetSpeech = AudioEncoding(maxBitrate: 24000); |
| 66 | + static const presetMusic = AudioEncoding(maxBitrate: 48000); |
| 67 | + static const presetMusicStereo = AudioEncoding(maxBitrate: 64000); |
| 68 | + static const presetMusicHighQuality = AudioEncoding(maxBitrate: 96000); |
| 69 | + static const presetMusicHighQualityStereo = AudioEncoding(maxBitrate: 128000); |
| 70 | + |
| 71 | + static const presets = [ |
| 72 | + presetTelephone, |
| 73 | + presetSpeech, |
| 74 | + presetMusic, |
| 75 | + presetMusicStereo, |
| 76 | + presetMusicHighQuality, |
| 77 | + presetMusicHighQualityStereo, |
| 78 | + ]; |
| 79 | +} |
| 80 | + |
| 81 | +/// Convenience extension for [AudioEncoding]. |
| 82 | +extension AudioEncodingExt on AudioEncoding { |
| 83 | + rtc.RTCRtpEncoding toRTCRtpEncoding() => rtc.RTCRtpEncoding( |
| 84 | + maxBitrate: maxBitrate, |
| 85 | + priority: bitratePriority?.toRtcpPriorityType() ?? rtc.RTCPriorityType.low, |
| 86 | + networkPriority: networkPriority?.toRtcpPriorityType(), |
| 87 | + ); |
| 88 | +} |
0 commit comments