blob: c4429727889b19437e7abe52a00c9b9bb343550d [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
kwiberg087bd342017-02-10 08:15:44 -080011#ifndef WEBRTC_API_AUDIO_CODECS_AUDIO_FORMAT_H_
12#define WEBRTC_API_AUDIO_CODECS_AUDIO_FORMAT_H_
kwibergc01c6a42016-04-28 14:23:32 -070013
14#include <map>
15#include <ostream>
16#include <string>
17#include <utility>
18
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/optional.h"
ossua1a040a2017-04-06 10:03:21 -070020
kwibergc01c6a42016-04-28 14:23:32 -070021namespace webrtc {
22
23// SDP specification for a single audio codec.
24// NOTE: This class is still under development and may change without notice.
25struct SdpAudioFormat {
kwiberg5178ee82016-05-03 01:39:01 -070026 using Parameters = std::map<std::string, std::string>;
27
kwibergc01c6a42016-04-28 14:23:32 -070028 SdpAudioFormat(const SdpAudioFormat&);
29 SdpAudioFormat(SdpAudioFormat&&);
ossua1a040a2017-04-06 10:03:21 -070030 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);
kwiberg5178ee82016-05-03 01:39:01 -070034 SdpAudioFormat(const char* name,
35 int clockrate_hz,
ossua1a040a2017-04-06 10:03:21 -070036 size_t num_channels,
kwibergd32bf752017-01-19 07:03:59 -080037 const Parameters& param);
38 SdpAudioFormat(const std::string& name,
39 int clockrate_hz,
ossua1a040a2017-04-06 10:03:21 -070040 size_t num_channels,
kwibergd32bf752017-01-19 07:03:59 -080041 const Parameters& param);
kwibergc01c6a42016-04-28 14:23:32 -070042 ~SdpAudioFormat();
43
deadbeefcb383672017-04-26 16:28:42 -070044 // Returns true if this format is compatible with |o|. In SDP terminology:
45 // would it represent the same codec between an offer and an answer? As
46 // opposed to operator==, this method disregards codec parameters.
47 bool Matches(const SdpAudioFormat& o) const;
48
kwibergc01c6a42016-04-28 14:23:32 -070049 SdpAudioFormat& operator=(const SdpAudioFormat&);
50 SdpAudioFormat& operator=(SdpAudioFormat&&);
51
kwibergc4ccd4d2016-09-21 10:55:15 -070052 friend bool operator==(const SdpAudioFormat& a, const SdpAudioFormat& b);
53 friend bool operator!=(const SdpAudioFormat& a, const SdpAudioFormat& b) {
54 return !(a == b);
55 }
56
kwibergc01c6a42016-04-28 14:23:32 -070057 std::string name;
58 int clockrate_hz;
ossua1a040a2017-04-06 10:03:21 -070059 size_t num_channels;
kwibergc01c6a42016-04-28 14:23:32 -070060 Parameters parameters;
kwibergc01c6a42016-04-28 14:23:32 -070061};
62
63void swap(SdpAudioFormat& a, SdpAudioFormat& b);
64std::ostream& operator<<(std::ostream& os, const SdpAudioFormat& saf);
65
ossua1a040a2017-04-06 10:03:21 -070066// Information about how an audio format is treated by the codec implementation.
67// Contains basic information, such as sample rate and number of channels, which
68// isn't uniformly presented by SDP. Also contains flags indicating support for
69// integrating with other parts of WebRTC, like external VAD and comfort noise
70// level calculation.
71//
72// To avoid API breakage, and make the code clearer, AudioCodecInfo should not
ossu9def8002017-02-09 05:14:32 -080073// be directly initializable with any flags indicating optional support. If it
74// were, these initializers would break any time a new flag was added. It's also
75// more difficult to understand:
ossua1a040a2017-04-06 10:03:21 -070076// AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true};
ossu9def8002017-02-09 05:14:32 -080077// than
ossua1a040a2017-04-06 10:03:21 -070078// AudioCodecInfo info(16000, 1, 32000);
79// info.allow_comfort_noise = true;
80// info.future_flag_b = true;
81// info.future_flag_c = true;
82struct AudioCodecInfo {
83 AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps);
84 AudioCodecInfo(int sample_rate_hz,
85 size_t num_channels,
86 int default_bitrate_bps,
87 int min_bitrate_bps,
88 int max_bitrate_bps);
89 AudioCodecInfo(const AudioCodecInfo& b) = default;
90 ~AudioCodecInfo() = default;
ossu9def8002017-02-09 05:14:32 -080091
ossua1a040a2017-04-06 10:03:21 -070092 bool operator==(const AudioCodecInfo& b) const {
93 return sample_rate_hz == b.sample_rate_hz &&
94 num_channels == b.num_channels &&
95 default_bitrate_bps == b.default_bitrate_bps &&
96 min_bitrate_bps == b.min_bitrate_bps &&
97 max_bitrate_bps == b.max_bitrate_bps &&
98 allow_comfort_noise == b.allow_comfort_noise &&
99 supports_network_adaption == b.supports_network_adaption;
100 }
101
102 bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); }
103
104 bool HasFixedBitrate() const {
105 RTC_DCHECK_GE(min_bitrate_bps, 0);
106 RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps);
107 RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps);
108 return min_bitrate_bps == max_bitrate_bps;
109 }
110
111 int sample_rate_hz;
112 size_t num_channels;
113 int default_bitrate_bps;
114 int min_bitrate_bps;
115 int max_bitrate_bps;
116
kwiberg087bd342017-02-10 08:15:44 -0800117 bool allow_comfort_noise = true; // This codec can be used with an external
118 // comfort noise generator.
ossu9def8002017-02-09 05:14:32 -0800119 bool supports_network_adaption = false; // This codec can adapt to varying
120 // network conditions.
ossud4e9f622016-08-18 02:01:17 -0700121};
122
kwiberg96444ae2017-06-14 03:27:40 -0700123std::ostream& operator<<(std::ostream& os, const AudioCodecInfo& aci);
124
ossua1a040a2017-04-06 10:03:21 -0700125// AudioCodecSpec ties an audio format to specific information about the codec
126// and its implementation.
127struct AudioCodecSpec {
128 bool operator==(const AudioCodecSpec& b) const {
129 return format == b.format && info == b.info;
130 }
131
132 bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); }
133
134 SdpAudioFormat format;
135 AudioCodecInfo info;
136};
137
kwiberg96444ae2017-06-14 03:27:40 -0700138std::ostream& operator<<(std::ostream& os, const AudioCodecSpec& acs);
139
kwibergc01c6a42016-04-28 14:23:32 -0700140} // namespace webrtc
141
kwiberg087bd342017-02-10 08:15:44 -0800142#endif // WEBRTC_API_AUDIO_CODECS_AUDIO_FORMAT_H_