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 | |
| 14 | #include <map> |
Taylor Brandstetter | bd73928 | 2018-04-20 15:58:11 +0000 | [diff] [blame^] | 15 | #include <ostream> |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <utility> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/optional.h" |
Danil Chapovalov | 6e9d895 | 2018-04-09 20:30:51 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.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. |
| 25 | // NOTE: This class is still under development and may change without notice. |
| 26 | struct SdpAudioFormat { |
kwiberg | 5178ee8 | 2016-05-03 01:39:01 -0700 | [diff] [blame] | 27 | using Parameters = std::map<std::string, std::string>; |
| 28 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 29 | SdpAudioFormat(const SdpAudioFormat&); |
| 30 | SdpAudioFormat(SdpAudioFormat&&); |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 31 | SdpAudioFormat(const char* name, int clockrate_hz, size_t num_channels); |
| 32 | SdpAudioFormat(const std::string& name, |
| 33 | int clockrate_hz, |
| 34 | size_t num_channels); |
kwiberg | 5178ee8 | 2016-05-03 01:39:01 -0700 | [diff] [blame] | 35 | SdpAudioFormat(const char* name, |
| 36 | int clockrate_hz, |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 37 | size_t num_channels, |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 38 | const Parameters& param); |
| 39 | SdpAudioFormat(const std::string& name, |
| 40 | int clockrate_hz, |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 41 | size_t num_channels, |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 42 | const Parameters& param); |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 43 | ~SdpAudioFormat(); |
| 44 | |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 45 | // Returns true if this format is compatible with |o|. In SDP terminology: |
| 46 | // would it represent the same codec between an offer and an answer? As |
| 47 | // opposed to operator==, this method disregards codec parameters. |
| 48 | bool Matches(const SdpAudioFormat& o) const; |
| 49 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 50 | SdpAudioFormat& operator=(const SdpAudioFormat&); |
| 51 | SdpAudioFormat& operator=(SdpAudioFormat&&); |
| 52 | |
kwiberg | c4ccd4d | 2016-09-21 10:55:15 -0700 | [diff] [blame] | 53 | friend bool operator==(const SdpAudioFormat& a, const SdpAudioFormat& b); |
| 54 | friend bool operator!=(const SdpAudioFormat& a, const SdpAudioFormat& b) { |
| 55 | return !(a == b); |
| 56 | } |
| 57 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 58 | std::string name; |
| 59 | int clockrate_hz; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 60 | size_t num_channels; |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 61 | Parameters parameters; |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | void swap(SdpAudioFormat& a, SdpAudioFormat& b); |
Taylor Brandstetter | bd73928 | 2018-04-20 15:58:11 +0000 | [diff] [blame^] | 65 | std::ostream& operator<<(std::ostream& os, const SdpAudioFormat& saf); |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 66 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 67 | // Information about how an audio format is treated by the codec implementation. |
| 68 | // Contains basic information, such as sample rate and number of channels, which |
| 69 | // isn't uniformly presented by SDP. Also contains flags indicating support for |
| 70 | // integrating with other parts of WebRTC, like external VAD and comfort noise |
| 71 | // level calculation. |
| 72 | // |
| 73 | // To avoid API breakage, and make the code clearer, AudioCodecInfo should not |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 74 | // be directly initializable with any flags indicating optional support. If it |
| 75 | // were, these initializers would break any time a new flag was added. It's also |
| 76 | // more difficult to understand: |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 77 | // AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true}; |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 78 | // than |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 79 | // AudioCodecInfo info(16000, 1, 32000); |
| 80 | // info.allow_comfort_noise = true; |
| 81 | // info.future_flag_b = true; |
| 82 | // info.future_flag_c = true; |
| 83 | struct AudioCodecInfo { |
| 84 | AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps); |
| 85 | AudioCodecInfo(int sample_rate_hz, |
| 86 | size_t num_channels, |
| 87 | int default_bitrate_bps, |
| 88 | int min_bitrate_bps, |
| 89 | int max_bitrate_bps); |
| 90 | AudioCodecInfo(const AudioCodecInfo& b) = default; |
| 91 | ~AudioCodecInfo() = default; |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 92 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 93 | bool operator==(const AudioCodecInfo& b) const { |
| 94 | return sample_rate_hz == b.sample_rate_hz && |
| 95 | num_channels == b.num_channels && |
| 96 | default_bitrate_bps == b.default_bitrate_bps && |
| 97 | min_bitrate_bps == b.min_bitrate_bps && |
| 98 | max_bitrate_bps == b.max_bitrate_bps && |
| 99 | allow_comfort_noise == b.allow_comfort_noise && |
| 100 | supports_network_adaption == b.supports_network_adaption; |
| 101 | } |
| 102 | |
| 103 | bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); } |
| 104 | |
| 105 | bool HasFixedBitrate() const { |
| 106 | RTC_DCHECK_GE(min_bitrate_bps, 0); |
| 107 | RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps); |
| 108 | RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps); |
| 109 | return min_bitrate_bps == max_bitrate_bps; |
| 110 | } |
| 111 | |
| 112 | int sample_rate_hz; |
| 113 | size_t num_channels; |
| 114 | int default_bitrate_bps; |
| 115 | int min_bitrate_bps; |
| 116 | int max_bitrate_bps; |
| 117 | |
kwiberg | 087bd34 | 2017-02-10 08:15:44 -0800 | [diff] [blame] | 118 | bool allow_comfort_noise = true; // This codec can be used with an external |
| 119 | // comfort noise generator. |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 120 | bool supports_network_adaption = false; // This codec can adapt to varying |
| 121 | // network conditions. |
ossu | d4e9f62 | 2016-08-18 02:01:17 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Taylor Brandstetter | bd73928 | 2018-04-20 15:58:11 +0000 | [diff] [blame^] | 124 | std::ostream& operator<<(std::ostream& os, const AudioCodecInfo& aci); |
| 125 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 126 | // AudioCodecSpec ties an audio format to specific information about the codec |
| 127 | // and its implementation. |
| 128 | struct AudioCodecSpec { |
| 129 | bool operator==(const AudioCodecSpec& b) const { |
| 130 | return format == b.format && info == b.info; |
| 131 | } |
| 132 | |
| 133 | bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); } |
| 134 | |
| 135 | SdpAudioFormat format; |
| 136 | AudioCodecInfo info; |
| 137 | }; |
| 138 | |
Taylor Brandstetter | bd73928 | 2018-04-20 15:58:11 +0000 | [diff] [blame^] | 139 | std::ostream& operator<<(std::ostream& os, const AudioCodecSpec& acs); |
| 140 | |
kwiberg | c01c6a4 | 2016-04-28 14:23:32 -0700 | [diff] [blame] | 141 | } // namespace webrtc |
| 142 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 143 | #endif // API_AUDIO_CODECS_AUDIO_FORMAT_H_ |