kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_AUDIO_CODECS_AUDIO_FORMAT_H_ |
| 12 | #define API_AUDIO_CODECS_AUDIO_FORMAT_H_ |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 15 | #include <map> |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 16 | #include <string> |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 17 | |
Karl Wiberg | 24744a9 | 2018-08-15 15:23:08 +0200 | [diff] [blame] | 18 | #include "absl/strings/string_view.h" |
Danil Chapovalov | 6e9d895 | 2018-04-09 20:30:51 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 20 | #include "rtc_base/system/rtc_export.h" |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 21 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | // SDP specification for a single audio codec. |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 25 | struct RTC_EXPORT SdpAudioFormat { |
kwiberg | 5178ee8 | 2016-05-03 01:39:01 -0700 | [diff] [blame] | 26 | using Parameters = std::map<std::string, std::string>; |
| 27 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 28 | SdpAudioFormat(const SdpAudioFormat&); |
| 29 | SdpAudioFormat(SdpAudioFormat&&); |
Karl Wiberg | 24744a9 | 2018-08-15 15:23:08 +0200 | [diff] [blame] | 30 | SdpAudioFormat(absl::string_view name, int clockrate_hz, size_t num_channels); |
| 31 | SdpAudioFormat(absl::string_view name, |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 32 | int clockrate_hz, |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 33 | size_t num_channels, |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 34 | const Parameters& param); |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 35 | ~SdpAudioFormat(); |
| 36 | |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 37 | // Returns true if this format is compatible with |o|. In SDP terminology: |
| 38 | // would it represent the same codec between an offer and an answer? As |
| 39 | // opposed to operator==, this method disregards codec parameters. |
| 40 | bool Matches(const SdpAudioFormat& o) const; |
| 41 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 42 | SdpAudioFormat& operator=(const SdpAudioFormat&); |
| 43 | SdpAudioFormat& operator=(SdpAudioFormat&&); |
| 44 | |
kwiberg | c4ccd4d | 2016-09-21 10:55:15 -0700 | [diff] [blame] | 45 | friend bool operator==(const SdpAudioFormat& a, const SdpAudioFormat& b); |
| 46 | friend bool operator!=(const SdpAudioFormat& a, const SdpAudioFormat& b) { |
| 47 | return !(a == b); |
| 48 | } |
| 49 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 50 | std::string name; |
| 51 | int clockrate_hz; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 52 | size_t num_channels; |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 53 | Parameters parameters; |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 56 | // Information about how an audio format is treated by the codec implementation. |
| 57 | // Contains basic information, such as sample rate and number of channels, which |
| 58 | // isn't uniformly presented by SDP. Also contains flags indicating support for |
| 59 | // integrating with other parts of WebRTC, like external VAD and comfort noise |
| 60 | // level calculation. |
| 61 | // |
| 62 | // To avoid API breakage, and make the code clearer, AudioCodecInfo should not |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 63 | // be directly initializable with any flags indicating optional support. If it |
| 64 | // were, these initializers would break any time a new flag was added. It's also |
| 65 | // more difficult to understand: |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 66 | // AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true}; |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 67 | // than |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 68 | // AudioCodecInfo info(16000, 1, 32000); |
| 69 | // info.allow_comfort_noise = true; |
| 70 | // info.future_flag_b = true; |
| 71 | // info.future_flag_c = true; |
| 72 | struct AudioCodecInfo { |
| 73 | AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps); |
| 74 | AudioCodecInfo(int sample_rate_hz, |
| 75 | size_t num_channels, |
| 76 | int default_bitrate_bps, |
| 77 | int min_bitrate_bps, |
| 78 | int max_bitrate_bps); |
| 79 | AudioCodecInfo(const AudioCodecInfo& b) = default; |
| 80 | ~AudioCodecInfo() = default; |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 81 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 82 | bool operator==(const AudioCodecInfo& b) const { |
| 83 | return sample_rate_hz == b.sample_rate_hz && |
| 84 | num_channels == b.num_channels && |
| 85 | default_bitrate_bps == b.default_bitrate_bps && |
| 86 | min_bitrate_bps == b.min_bitrate_bps && |
| 87 | max_bitrate_bps == b.max_bitrate_bps && |
| 88 | allow_comfort_noise == b.allow_comfort_noise && |
| 89 | supports_network_adaption == b.supports_network_adaption; |
| 90 | } |
| 91 | |
| 92 | bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); } |
| 93 | |
| 94 | bool HasFixedBitrate() const { |
| 95 | RTC_DCHECK_GE(min_bitrate_bps, 0); |
| 96 | RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps); |
| 97 | RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps); |
| 98 | return min_bitrate_bps == max_bitrate_bps; |
| 99 | } |
| 100 | |
| 101 | int sample_rate_hz; |
| 102 | size_t num_channels; |
| 103 | int default_bitrate_bps; |
| 104 | int min_bitrate_bps; |
| 105 | int max_bitrate_bps; |
| 106 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 107 | bool allow_comfort_noise = true; // This codec can be used with an external |
| 108 | // comfort noise generator. |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 109 | bool supports_network_adaption = false; // This codec can adapt to varying |
| 110 | // network conditions. |
ossu | d4e9f62 | 2016-08-18 02:01:17 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 113 | // AudioCodecSpec ties an audio format to specific information about the codec |
| 114 | // and its implementation. |
| 115 | struct AudioCodecSpec { |
| 116 | bool operator==(const AudioCodecSpec& b) const { |
| 117 | return format == b.format && info == b.info; |
| 118 | } |
| 119 | |
| 120 | bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); } |
| 121 | |
| 122 | SdpAudioFormat format; |
| 123 | AudioCodecInfo info; |
| 124 | }; |
| 125 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 126 | } // namespace webrtc |
| 127 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 128 | #endif // API_AUDIO_CODECS_AUDIO_FORMAT_H_ |