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