blob: 053c067302a84c9bbeb62926f15be9ac38fb07f2 [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
kwibergc01c6a42016-04-28 14:23:32 -070015#include <map>
kwibergc01c6a42016-04-28 14:23:32 -070016#include <string>
kwibergc01c6a42016-04-28 14:23:32 -070017
Karl Wiberg24744a92018-08-15 15:23:08 +020018#include "absl/strings/string_view.h"
Danil Chapovalov6e9d8952018-04-09 20:30:51 +020019#include "rtc_base/checks.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020020#include "rtc_base/system/rtc_export.h"
ossua1a040a2017-04-06 10:03:21 -070021
kwibergc01c6a42016-04-28 14:23:32 -070022namespace webrtc {
23
24// SDP specification for a single audio codec.
Mirko Bonadeiac194142018-10-22 17:08:37 +020025struct RTC_EXPORT 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&&);
Karl Wiberg24744a92018-08-15 15:23:08 +020030 SdpAudioFormat(absl::string_view name, int clockrate_hz, size_t num_channels);
31 SdpAudioFormat(absl::string_view name,
kwibergd32bf752017-01-19 07:03:59 -080032 int clockrate_hz,
ossua1a040a2017-04-06 10:03:21 -070033 size_t num_channels,
kwibergd32bf752017-01-19 07:03:59 -080034 const Parameters& param);
Mirko Bonadei1c546052019-02-04 14:50:38 +010035 SdpAudioFormat(absl::string_view name,
36 int clockrate_hz,
37 size_t num_channels,
38 Parameters&& param);
kwibergc01c6a42016-04-28 14:23:32 -070039 ~SdpAudioFormat();
40
deadbeefcb383672017-04-26 16:28:42 -070041 // Returns true if this format is compatible with |o|. In SDP terminology:
42 // would it represent the same codec between an offer and an answer? As
43 // opposed to operator==, this method disregards codec parameters.
44 bool Matches(const SdpAudioFormat& o) const;
45
kwibergc01c6a42016-04-28 14:23:32 -070046 SdpAudioFormat& operator=(const SdpAudioFormat&);
47 SdpAudioFormat& operator=(SdpAudioFormat&&);
48
kwibergc4ccd4d2016-09-21 10:55:15 -070049 friend bool operator==(const SdpAudioFormat& a, const SdpAudioFormat& b);
50 friend bool operator!=(const SdpAudioFormat& a, const SdpAudioFormat& b) {
51 return !(a == b);
52 }
53
kwibergc01c6a42016-04-28 14:23:32 -070054 std::string name;
55 int clockrate_hz;
ossua1a040a2017-04-06 10:03:21 -070056 size_t num_channels;
kwibergc01c6a42016-04-28 14:23:32 -070057 Parameters parameters;
kwibergc01c6a42016-04-28 14:23:32 -070058};
59
ossua1a040a2017-04-06 10:03:21 -070060// Information about how an audio format is treated by the codec implementation.
61// Contains basic information, such as sample rate and number of channels, which
62// isn't uniformly presented by SDP. Also contains flags indicating support for
63// integrating with other parts of WebRTC, like external VAD and comfort noise
64// level calculation.
65//
66// To avoid API breakage, and make the code clearer, AudioCodecInfo should not
ossu9def8002017-02-09 05:14:32 -080067// be directly initializable with any flags indicating optional support. If it
68// were, these initializers would break any time a new flag was added. It's also
69// more difficult to understand:
ossua1a040a2017-04-06 10:03:21 -070070// AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true};
ossu9def8002017-02-09 05:14:32 -080071// than
ossua1a040a2017-04-06 10:03:21 -070072// AudioCodecInfo info(16000, 1, 32000);
73// info.allow_comfort_noise = true;
74// info.future_flag_b = true;
75// info.future_flag_c = true;
76struct AudioCodecInfo {
77 AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps);
78 AudioCodecInfo(int sample_rate_hz,
79 size_t num_channels,
80 int default_bitrate_bps,
81 int min_bitrate_bps,
82 int max_bitrate_bps);
83 AudioCodecInfo(const AudioCodecInfo& b) = default;
84 ~AudioCodecInfo() = default;
ossu9def8002017-02-09 05:14:32 -080085
ossua1a040a2017-04-06 10:03:21 -070086 bool operator==(const AudioCodecInfo& b) const {
87 return sample_rate_hz == b.sample_rate_hz &&
88 num_channels == b.num_channels &&
89 default_bitrate_bps == b.default_bitrate_bps &&
90 min_bitrate_bps == b.min_bitrate_bps &&
91 max_bitrate_bps == b.max_bitrate_bps &&
92 allow_comfort_noise == b.allow_comfort_noise &&
93 supports_network_adaption == b.supports_network_adaption;
94 }
95
96 bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); }
97
98 bool HasFixedBitrate() const {
99 RTC_DCHECK_GE(min_bitrate_bps, 0);
100 RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps);
101 RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps);
102 return min_bitrate_bps == max_bitrate_bps;
103 }
104
105 int sample_rate_hz;
106 size_t num_channels;
107 int default_bitrate_bps;
108 int min_bitrate_bps;
109 int max_bitrate_bps;
110
kwiberg087bd342017-02-10 08:15:44 -0800111 bool allow_comfort_noise = true; // This codec can be used with an external
112 // comfort noise generator.
ossu9def8002017-02-09 05:14:32 -0800113 bool supports_network_adaption = false; // This codec can adapt to varying
114 // network conditions.
ossud4e9f622016-08-18 02:01:17 -0700115};
116
ossua1a040a2017-04-06 10:03:21 -0700117// AudioCodecSpec ties an audio format to specific information about the codec
118// and its implementation.
119struct AudioCodecSpec {
120 bool operator==(const AudioCodecSpec& b) const {
121 return format == b.format && info == b.info;
122 }
123
124 bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); }
125
126 SdpAudioFormat format;
127 AudioCodecInfo info;
128};
129
kwibergc01c6a42016-04-28 14:23:32 -0700130} // namespace webrtc
131
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200132#endif // API_AUDIO_CODECS_AUDIO_FORMAT_H_