blob: 77488120514af7fa38263c13b34baf49db7222c7 [file] [log] [blame]
kwibergc01c6a42016-04-28 14:23:32 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_AUDIO_CODECS_AUDIO_FORMAT_H_
12#define API_AUDIO_CODECS_AUDIO_FORMAT_H_
kwibergc01c6a42016-04-28 14:23:32 -070013
14#include <map>
kwibergc01c6a42016-04-28 14:23:32 -070015#include <string>
kwibergc01c6a42016-04-28 14:23:32 -070016
Karl Wiberg24744a92018-08-15 15:23:08 +020017#include "absl/strings/string_view.h"
Danil Chapovalov6e9d8952018-04-09 20:30:51 +020018#include "rtc_base/checks.h"
ossua1a040a2017-04-06 10:03:21 -070019
kwibergc01c6a42016-04-28 14:23:32 -070020namespace webrtc {
21
22// SDP specification for a single audio codec.
kwibergc01c6a42016-04-28 14:23:32 -070023struct SdpAudioFormat {
kwiberg5178ee82016-05-03 01:39:01 -070024 using Parameters = std::map<std::string, std::string>;
25
kwibergc01c6a42016-04-28 14:23:32 -070026 SdpAudioFormat(const SdpAudioFormat&);
27 SdpAudioFormat(SdpAudioFormat&&);
Karl Wiberg24744a92018-08-15 15:23:08 +020028 SdpAudioFormat(absl::string_view name, int clockrate_hz, size_t num_channels);
29 SdpAudioFormat(absl::string_view name,
kwibergd32bf752017-01-19 07:03:59 -080030 int clockrate_hz,
ossua1a040a2017-04-06 10:03:21 -070031 size_t num_channels,
kwibergd32bf752017-01-19 07:03:59 -080032 const Parameters& param);
kwibergc01c6a42016-04-28 14:23:32 -070033 ~SdpAudioFormat();
34
deadbeefcb383672017-04-26 16:28:42 -070035 // Returns true if this format is compatible with |o|. In SDP terminology:
36 // would it represent the same codec between an offer and an answer? As
37 // opposed to operator==, this method disregards codec parameters.
38 bool Matches(const SdpAudioFormat& o) const;
39
kwibergc01c6a42016-04-28 14:23:32 -070040 SdpAudioFormat& operator=(const SdpAudioFormat&);
41 SdpAudioFormat& operator=(SdpAudioFormat&&);
42
kwibergc4ccd4d2016-09-21 10:55:15 -070043 friend bool operator==(const SdpAudioFormat& a, const SdpAudioFormat& b);
44 friend bool operator!=(const SdpAudioFormat& a, const SdpAudioFormat& b) {
45 return !(a == b);
46 }
47
kwibergc01c6a42016-04-28 14:23:32 -070048 std::string name;
49 int clockrate_hz;
ossua1a040a2017-04-06 10:03:21 -070050 size_t num_channels;
kwibergc01c6a42016-04-28 14:23:32 -070051 Parameters parameters;
kwibergc01c6a42016-04-28 14:23:32 -070052};
53
ossua1a040a2017-04-06 10:03:21 -070054// Information about how an audio format is treated by the codec implementation.
55// Contains basic information, such as sample rate and number of channels, which
56// isn't uniformly presented by SDP. Also contains flags indicating support for
57// integrating with other parts of WebRTC, like external VAD and comfort noise
58// level calculation.
59//
60// To avoid API breakage, and make the code clearer, AudioCodecInfo should not
ossu9def8002017-02-09 05:14:32 -080061// be directly initializable with any flags indicating optional support. If it
62// were, these initializers would break any time a new flag was added. It's also
63// more difficult to understand:
ossua1a040a2017-04-06 10:03:21 -070064// AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true};
ossu9def8002017-02-09 05:14:32 -080065// than
ossua1a040a2017-04-06 10:03:21 -070066// AudioCodecInfo info(16000, 1, 32000);
67// info.allow_comfort_noise = true;
68// info.future_flag_b = true;
69// info.future_flag_c = true;
70struct AudioCodecInfo {
71 AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps);
72 AudioCodecInfo(int sample_rate_hz,
73 size_t num_channels,
74 int default_bitrate_bps,
75 int min_bitrate_bps,
76 int max_bitrate_bps);
77 AudioCodecInfo(const AudioCodecInfo& b) = default;
78 ~AudioCodecInfo() = default;
ossu9def8002017-02-09 05:14:32 -080079
ossua1a040a2017-04-06 10:03:21 -070080 bool operator==(const AudioCodecInfo& b) const {
81 return sample_rate_hz == b.sample_rate_hz &&
82 num_channels == b.num_channels &&
83 default_bitrate_bps == b.default_bitrate_bps &&
84 min_bitrate_bps == b.min_bitrate_bps &&
85 max_bitrate_bps == b.max_bitrate_bps &&
86 allow_comfort_noise == b.allow_comfort_noise &&
87 supports_network_adaption == b.supports_network_adaption;
88 }
89
90 bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); }
91
92 bool HasFixedBitrate() const {
93 RTC_DCHECK_GE(min_bitrate_bps, 0);
94 RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps);
95 RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps);
96 return min_bitrate_bps == max_bitrate_bps;
97 }
98
99 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
kwiberg087bd342017-02-10 08:15:44 -0800105 bool allow_comfort_noise = true; // This codec can be used with an external
106 // comfort noise generator.
ossu9def8002017-02-09 05:14:32 -0800107 bool supports_network_adaption = false; // This codec can adapt to varying
108 // network conditions.
ossud4e9f622016-08-18 02:01:17 -0700109};
110
ossua1a040a2017-04-06 10:03:21 -0700111// AudioCodecSpec ties an audio format to specific information about the codec
112// and its implementation.
113struct AudioCodecSpec {
114 bool operator==(const AudioCodecSpec& b) const {
115 return format == b.format && info == b.info;
116 }
117
118 bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); }
119
120 SdpAudioFormat format;
121 AudioCodecInfo info;
122};
123
kwibergc01c6a42016-04-28 14:23:32 -0700124} // namespace webrtc
125
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200126#endif // API_AUDIO_CODECS_AUDIO_FORMAT_H_