audio_format.h (6168B)
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 11 #ifndef API_AUDIO_CODECS_AUDIO_FORMAT_H_ 12 #define API_AUDIO_CODECS_AUDIO_FORMAT_H_ 13 14 #include <stddef.h> 15 16 #include <map> 17 #include <string> 18 19 #include "absl/strings/string_view.h" 20 #include "api/rtp_parameters.h" 21 #include "rtc_base/checks.h" 22 #include "rtc_base/strings/string_builder.h" 23 #include "rtc_base/system/rtc_export.h" // IWYU pragma: private 24 25 namespace webrtc { 26 27 // SDP specification for a single audio codec. 28 struct RTC_EXPORT SdpAudioFormat { 29 using Parameters [[deprecated("Use CodecParameterMap")]] = 30 std::map<std::string, std::string>; 31 32 SdpAudioFormat(const SdpAudioFormat&); 33 SdpAudioFormat(SdpAudioFormat&&); 34 SdpAudioFormat(absl::string_view name, int clockrate_hz, size_t num_channels); 35 SdpAudioFormat(absl::string_view name, 36 int clockrate_hz, 37 size_t num_channels, 38 const CodecParameterMap& param); 39 SdpAudioFormat(absl::string_view name, 40 int clockrate_hz, 41 size_t num_channels, 42 CodecParameterMap&& param); 43 ~SdpAudioFormat(); 44 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 50 SdpAudioFormat& operator=(const SdpAudioFormat&); 51 SdpAudioFormat& operator=(SdpAudioFormat&&); 52 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 58 template <typename Sink> 59 friend void AbslStringify(Sink& sink, const SdpAudioFormat& saf) { 60 StringBuilder sb("{"); 61 bool first = true; 62 for (const auto& [key, value] : saf.parameters) { 63 if (!first) { 64 sb << ", "; 65 } 66 first = false; 67 sb << key << ": " << value; 68 } 69 sb << "}"; 70 absl::Format( 71 &sink, "{name: %s, clockrate_hz: %d, num_channels: %d, parameters: %v}", 72 saf.name, saf.clockrate_hz, saf.num_channels, sb.Release()); 73 } 74 75 std::string name; 76 int clockrate_hz; 77 size_t num_channels; 78 CodecParameterMap parameters; 79 }; 80 81 // Information about how an audio format is treated by the codec implementation. 82 // Contains basic information, such as sample rate and number of channels, which 83 // isn't uniformly presented by SDP. Also contains flags indicating support for 84 // integrating with other parts of WebRTC, like external VAD and comfort noise 85 // level calculation. 86 // 87 // To avoid API breakage, and make the code clearer, AudioCodecInfo should not 88 // be directly initializable with any flags indicating optional support. If it 89 // were, these initializers would break any time a new flag was added. It's also 90 // more difficult to understand: 91 // AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true}; 92 // than 93 // AudioCodecInfo info(16000, 1, 32000); 94 // info.allow_comfort_noise = true; 95 // info.future_flag_b = true; 96 // info.future_flag_c = true; 97 struct AudioCodecInfo { 98 AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps); 99 AudioCodecInfo(int sample_rate_hz, 100 size_t num_channels, 101 int default_bitrate_bps, 102 int min_bitrate_bps, 103 int max_bitrate_bps); 104 AudioCodecInfo(const AudioCodecInfo& b) = default; 105 ~AudioCodecInfo() = default; 106 107 bool operator==(const AudioCodecInfo& b) const { 108 return sample_rate_hz == b.sample_rate_hz && 109 num_channels == b.num_channels && 110 default_bitrate_bps == b.default_bitrate_bps && 111 min_bitrate_bps == b.min_bitrate_bps && 112 max_bitrate_bps == b.max_bitrate_bps && 113 allow_comfort_noise == b.allow_comfort_noise && 114 supports_network_adaption == b.supports_network_adaption; 115 } 116 117 bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); } 118 119 bool HasFixedBitrate() const { 120 RTC_DCHECK_GE(min_bitrate_bps, 0); 121 RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps); 122 RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps); 123 return min_bitrate_bps == max_bitrate_bps; 124 } 125 126 template <typename Sink> 127 friend void AbslStringify(Sink& sink, const AudioCodecInfo& aci) { 128 absl::Format(&sink, 129 "{sample_rate_hz: %d, num_channels: %d, default_bitrate_bps: " 130 "%d, min_bitrate_bps: %d, max_bitrate_bps: %d, " 131 "allow_comfort_noise: %v, supports_network_adaption: %v}", 132 aci.sample_rate_hz, aci.num_channels, aci.default_bitrate_bps, 133 aci.min_bitrate_bps, aci.max_bitrate_bps, 134 aci.allow_comfort_noise, aci.supports_network_adaption); 135 } 136 137 int sample_rate_hz; 138 size_t num_channels; 139 int default_bitrate_bps; 140 int min_bitrate_bps; 141 int max_bitrate_bps; 142 143 bool allow_comfort_noise = true; // This codec can be used with an external 144 // comfort noise generator. 145 bool supports_network_adaption = false; // This codec can adapt to varying 146 // network conditions. 147 }; 148 149 // AudioCodecSpec ties an audio format to specific information about the codec 150 // and its implementation. 151 struct AudioCodecSpec { 152 bool operator==(const AudioCodecSpec& b) const { 153 return format == b.format && info == b.info; 154 } 155 156 bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); } 157 158 template <typename Sink> 159 friend void AbslStringify(Sink& sink, const AudioCodecSpec& acs) { 160 absl::Format(&sink, "{format: %v, info: %v}", acs.format, acs.info); 161 } 162 163 SdpAudioFormat format; 164 AudioCodecInfo info; 165 }; 166 167 } // namespace webrtc 168 169 #endif // API_AUDIO_CODECS_AUDIO_FORMAT_H_