audio_format.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #ifndef API_AUDIO_CODECS_AUDIO_FORMAT_H_
  11. #define API_AUDIO_CODECS_AUDIO_FORMAT_H_
  12. #include <stddef.h>
  13. #include <map>
  14. #include <string>
  15. #include "absl/strings/string_view.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/system/rtc_export.h"
  18. namespace webrtc {
  19. // SDP specification for a single audio codec.
  20. struct RTC_EXPORT SdpAudioFormat {
  21. using Parameters = std::map<std::string, std::string>;
  22. SdpAudioFormat(const SdpAudioFormat&);
  23. SdpAudioFormat(SdpAudioFormat&&);
  24. SdpAudioFormat(absl::string_view name, int clockrate_hz, size_t num_channels);
  25. SdpAudioFormat(absl::string_view name,
  26. int clockrate_hz,
  27. size_t num_channels,
  28. const Parameters& param);
  29. SdpAudioFormat(absl::string_view name,
  30. int clockrate_hz,
  31. size_t num_channels,
  32. Parameters&& param);
  33. ~SdpAudioFormat();
  34. // Returns true if this format is compatible with |o|. In SDP terminology:
  35. // would it represent the same codec between an offer and an answer? As
  36. // opposed to operator==, this method disregards codec parameters.
  37. bool Matches(const SdpAudioFormat& o) const;
  38. SdpAudioFormat& operator=(const SdpAudioFormat&);
  39. SdpAudioFormat& operator=(SdpAudioFormat&&);
  40. friend bool operator==(const SdpAudioFormat& a, const SdpAudioFormat& b);
  41. friend bool operator!=(const SdpAudioFormat& a, const SdpAudioFormat& b) {
  42. return !(a == b);
  43. }
  44. std::string name;
  45. int clockrate_hz;
  46. size_t num_channels;
  47. Parameters parameters;
  48. };
  49. // Information about how an audio format is treated by the codec implementation.
  50. // Contains basic information, such as sample rate and number of channels, which
  51. // isn't uniformly presented by SDP. Also contains flags indicating support for
  52. // integrating with other parts of WebRTC, like external VAD and comfort noise
  53. // level calculation.
  54. //
  55. // To avoid API breakage, and make the code clearer, AudioCodecInfo should not
  56. // be directly initializable with any flags indicating optional support. If it
  57. // were, these initializers would break any time a new flag was added. It's also
  58. // more difficult to understand:
  59. // AudioCodecInfo info{16000, 1, 32000, true, false, false, true, true};
  60. // than
  61. // AudioCodecInfo info(16000, 1, 32000);
  62. // info.allow_comfort_noise = true;
  63. // info.future_flag_b = true;
  64. // info.future_flag_c = true;
  65. struct AudioCodecInfo {
  66. AudioCodecInfo(int sample_rate_hz, size_t num_channels, int bitrate_bps);
  67. AudioCodecInfo(int sample_rate_hz,
  68. size_t num_channels,
  69. int default_bitrate_bps,
  70. int min_bitrate_bps,
  71. int max_bitrate_bps);
  72. AudioCodecInfo(const AudioCodecInfo& b) = default;
  73. ~AudioCodecInfo() = default;
  74. bool operator==(const AudioCodecInfo& b) const {
  75. return sample_rate_hz == b.sample_rate_hz &&
  76. num_channels == b.num_channels &&
  77. default_bitrate_bps == b.default_bitrate_bps &&
  78. min_bitrate_bps == b.min_bitrate_bps &&
  79. max_bitrate_bps == b.max_bitrate_bps &&
  80. allow_comfort_noise == b.allow_comfort_noise &&
  81. supports_network_adaption == b.supports_network_adaption;
  82. }
  83. bool operator!=(const AudioCodecInfo& b) const { return !(*this == b); }
  84. bool HasFixedBitrate() const {
  85. RTC_DCHECK_GE(min_bitrate_bps, 0);
  86. RTC_DCHECK_LE(min_bitrate_bps, default_bitrate_bps);
  87. RTC_DCHECK_GE(max_bitrate_bps, default_bitrate_bps);
  88. return min_bitrate_bps == max_bitrate_bps;
  89. }
  90. int sample_rate_hz;
  91. size_t num_channels;
  92. int default_bitrate_bps;
  93. int min_bitrate_bps;
  94. int max_bitrate_bps;
  95. bool allow_comfort_noise = true; // This codec can be used with an external
  96. // comfort noise generator.
  97. bool supports_network_adaption = false; // This codec can adapt to varying
  98. // network conditions.
  99. };
  100. // AudioCodecSpec ties an audio format to specific information about the codec
  101. // and its implementation.
  102. struct AudioCodecSpec {
  103. bool operator==(const AudioCodecSpec& b) const {
  104. return format == b.format && info == b.info;
  105. }
  106. bool operator!=(const AudioCodecSpec& b) const { return !(*this == b); }
  107. SdpAudioFormat format;
  108. AudioCodecInfo info;
  109. };
  110. } // namespace webrtc
  111. #endif // API_AUDIO_CODECS_AUDIO_FORMAT_H_