| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
| 13 | 13 |
| 14 #include <functional> | 14 #include <functional> |
| 15 #include <memory> | 15 #include <memory> |
| 16 #include <string> | 16 #include <string> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "webrtc/api/audio_codecs/audio_encoder.h" | 19 #include "webrtc/api/audio_codecs/audio_encoder.h" |
| 20 #include "webrtc/api/audio_codecs/audio_format.h" | 20 #include "webrtc/api/audio_codecs/audio_format.h" |
| 21 #include "webrtc/api/audio_codecs/opus/audio_encoder_opus_config.h" |
| 21 #include "webrtc/base/constructormagic.h" | 22 #include "webrtc/base/constructormagic.h" |
| 22 #include "webrtc/base/optional.h" | 23 #include "webrtc/base/optional.h" |
| 23 #include "webrtc/base/protobuf_utils.h" | 24 #include "webrtc/base/protobuf_utils.h" |
| 24 #include "webrtc/common_audio/smoothing_filter.h" | 25 #include "webrtc/common_audio/smoothing_filter.h" |
| 25 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ
k_adaptor.h" | 26 #include "webrtc/modules/audio_coding/audio_network_adaptor/include/audio_networ
k_adaptor.h" |
| 26 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" | 27 #include "webrtc/modules/audio_coding/codecs/opus/opus_interface.h" |
| 27 | 28 |
| 28 namespace webrtc { | 29 namespace webrtc { |
| 29 | 30 |
| 30 class RtcEventLog; | 31 class RtcEventLog; |
| 31 | 32 |
| 32 struct CodecInst; | 33 struct CodecInst; |
| 33 | 34 |
| 34 class AudioEncoderOpus final : public AudioEncoder { | 35 class AudioEncoderOpus final : public AudioEncoder { |
| 35 public: | 36 public: |
| 36 enum ApplicationMode { | 37 static void AppendSupportedEncoders(std::vector<AudioCodecSpec>* specs); |
| 37 kVoip = 0, | 38 static AudioCodecInfo QueryAudioEncoder(const AudioEncoderOpusConfig& config); |
| 38 kAudio = 1, | 39 static std::unique_ptr<AudioEncoder> MakeAudioEncoder( |
| 39 }; | 40 const AudioEncoderOpusConfig&, |
| 41 int payload_type); |
| 40 | 42 |
| 41 struct Config { | 43 // NOTE: This alias will soon go away. See |
| 42 Config(); | 44 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7847 |
| 43 Config(const Config&); | 45 using Config = AudioEncoderOpusConfig; |
| 44 ~Config(); | |
| 45 Config& operator=(const Config&); | |
| 46 | 46 |
| 47 bool IsOk() const; | 47 // NOTE: This function will soon go away. See |
| 48 int GetBitrateBps() const; | 48 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7847 |
| 49 // Returns empty if the current bitrate falls within the hysteresis window, | 49 static Config CreateConfig(int payload_type, const SdpAudioFormat& format); |
| 50 // defined by complexity_threshold_bps +/- complexity_threshold_window_bps. | |
| 51 // Otherwise, returns the current complexity depending on whether the | |
| 52 // current bitrate is above or below complexity_threshold_bps. | |
| 53 rtc::Optional<int> GetNewComplexity() const; | |
| 54 | 50 |
| 55 static constexpr int kDefaultFrameSizeMs = 20; | 51 static AudioEncoderOpusConfig CreateConfig(const CodecInst& codec_inst); |
| 56 int frame_size_ms = kDefaultFrameSizeMs; | 52 static rtc::Optional<AudioEncoderOpusConfig> SdpToConfig( |
| 57 size_t num_channels = 1; | 53 const SdpAudioFormat& format); |
| 58 int payload_type = 120; | |
| 59 ApplicationMode application = kVoip; | |
| 60 rtc::Optional<int> bitrate_bps; // Unset means to use default value. | |
| 61 bool fec_enabled = false; | |
| 62 bool cbr_enabled = false; | |
| 63 int max_playback_rate_hz = 48000; | |
| 64 int complexity = kDefaultComplexity; | |
| 65 // This value may change in the struct's constructor. | |
| 66 int low_rate_complexity = kDefaultComplexity; | |
| 67 // low_rate_complexity is used when the bitrate is below this threshold. | |
| 68 int complexity_threshold_bps = 12500; | |
| 69 int complexity_threshold_window_bps = 1500; | |
| 70 bool dtx_enabled = false; | |
| 71 std::vector<int> supported_frame_lengths_ms; | |
| 72 int uplink_bandwidth_update_interval_ms = 200; | |
| 73 | 54 |
| 74 private: | 55 // Returns empty if the current bitrate falls within the hysteresis window, |
| 75 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM) | 56 // defined by complexity_threshold_bps +/- complexity_threshold_window_bps. |
| 76 // If we are on Android, iOS and/or ARM, use a lower complexity setting as | 57 // Otherwise, returns the current complexity depending on whether the |
| 77 // default, to save encoder complexity. | 58 // current bitrate is above or below complexity_threshold_bps. |
| 78 static const int kDefaultComplexity = 5; | 59 static rtc::Optional<int> GetNewComplexity( |
| 79 #else | 60 const AudioEncoderOpusConfig& config); |
| 80 static const int kDefaultComplexity = 9; | |
| 81 #endif | |
| 82 }; | |
| 83 | |
| 84 static Config CreateConfig(int payload_type, const SdpAudioFormat& format); | |
| 85 static Config CreateConfig(const CodecInst& codec_inst); | |
| 86 | 61 |
| 87 using AudioNetworkAdaptorCreator = | 62 using AudioNetworkAdaptorCreator = |
| 88 std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&, | 63 std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&, |
| 89 RtcEventLog*)>; | 64 RtcEventLog*)>; |
| 65 |
| 66 // NOTE: This constructor will soon go away. See |
| 67 // https://bugs.chromium.org/p/webrtc/issues/detail?id=7847 |
| 68 AudioEncoderOpus(const AudioEncoderOpusConfig& config); |
| 69 |
| 90 AudioEncoderOpus( | 70 AudioEncoderOpus( |
| 91 const Config& config, | 71 const AudioEncoderOpusConfig& config, |
| 72 int payload_type, |
| 92 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr, | 73 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr, |
| 93 std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr); | 74 std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr); |
| 94 | 75 |
| 95 explicit AudioEncoderOpus(const CodecInst& codec_inst); | 76 explicit AudioEncoderOpus(const CodecInst& codec_inst); |
| 96 AudioEncoderOpus(int payload_type, const SdpAudioFormat& format); | 77 AudioEncoderOpus(int payload_type, const SdpAudioFormat& format); |
| 97 ~AudioEncoderOpus() override; | 78 ~AudioEncoderOpus() override; |
| 98 | 79 |
| 99 // Static interface for use by BuiltinAudioEncoderFactory. | 80 // Static interface for use by BuiltinAudioEncoderFactory. |
| 100 static constexpr const char* GetPayloadName() { return "opus"; } | 81 static constexpr const char* GetPayloadName() { return "opus"; } |
| 101 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | 82 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( |
| 102 const SdpAudioFormat& format); | 83 const SdpAudioFormat& format); |
| 103 | 84 |
| 104 int SampleRateHz() const override; | 85 int SampleRateHz() const override; |
| 105 size_t NumChannels() const override; | 86 size_t NumChannels() const override; |
| 106 size_t Num10MsFramesInNextPacket() const override; | 87 size_t Num10MsFramesInNextPacket() const override; |
| 107 size_t Max10MsFramesInAPacket() const override; | 88 size_t Max10MsFramesInAPacket() const override; |
| 108 int GetTargetBitrate() const override; | 89 int GetTargetBitrate() const override; |
| 109 | 90 |
| 110 void Reset() override; | 91 void Reset() override; |
| 111 bool SetFec(bool enable) override; | 92 bool SetFec(bool enable) override; |
| 112 | 93 |
| 113 // Set Opus DTX. Once enabled, Opus stops transmission, when it detects voice | 94 // Set Opus DTX. Once enabled, Opus stops transmission, when it detects |
| 114 // being inactive. During that, it still sends 2 packets (one for content, one | 95 // voice being inactive. During that, it still sends 2 packets (one for |
| 115 // for signaling) about every 400 ms. | 96 // content, one for signaling) about every 400 ms. |
| 116 bool SetDtx(bool enable) override; | 97 bool SetDtx(bool enable) override; |
| 117 bool GetDtx() const override; | 98 bool GetDtx() const override; |
| 118 | 99 |
| 119 bool SetApplication(Application application) override; | 100 bool SetApplication(Application application) override; |
| 120 void SetMaxPlaybackRate(int frequency_hz) override; | 101 void SetMaxPlaybackRate(int frequency_hz) override; |
| 121 bool EnableAudioNetworkAdaptor(const std::string& config_string, | 102 bool EnableAudioNetworkAdaptor(const std::string& config_string, |
| 122 RtcEventLog* event_log) override; | 103 RtcEventLog* event_log) override; |
| 123 void DisableAudioNetworkAdaptor() override; | 104 void DisableAudioNetworkAdaptor() override; |
| 124 void OnReceivedUplinkPacketLossFraction( | 105 void OnReceivedUplinkPacketLossFraction( |
| 125 float uplink_packet_loss_fraction) override; | 106 float uplink_packet_loss_fraction) override; |
| 126 void OnReceivedUplinkRecoverablePacketLossFraction( | 107 void OnReceivedUplinkRecoverablePacketLossFraction( |
| 127 float uplink_recoverable_packet_loss_fraction) override; | 108 float uplink_recoverable_packet_loss_fraction) override; |
| 128 void OnReceivedUplinkBandwidth( | 109 void OnReceivedUplinkBandwidth( |
| 129 int target_audio_bitrate_bps, | 110 int target_audio_bitrate_bps, |
| 130 rtc::Optional<int64_t> probing_interval_ms) override; | 111 rtc::Optional<int64_t> probing_interval_ms) override; |
| 131 void OnReceivedRtt(int rtt_ms) override; | 112 void OnReceivedRtt(int rtt_ms) override; |
| 132 void OnReceivedOverhead(size_t overhead_bytes_per_packet) override; | 113 void OnReceivedOverhead(size_t overhead_bytes_per_packet) override; |
| 133 void SetReceiverFrameLengthRange(int min_frame_length_ms, | 114 void SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 134 int max_frame_length_ms) override; | 115 int max_frame_length_ms) override; |
| 135 rtc::ArrayView<const int> supported_frame_lengths_ms() const { | 116 rtc::ArrayView<const int> supported_frame_lengths_ms() const { |
| 136 return config_.supported_frame_lengths_ms; | 117 return config_.supported_frame_lengths_ms; |
| 137 } | 118 } |
| 138 | 119 |
| 139 // Getters for testing. | 120 // Getters for testing. |
| 140 float packet_loss_rate() const { return packet_loss_rate_; } | 121 float packet_loss_rate() const { return packet_loss_rate_; } |
| 141 ApplicationMode application() const { return config_.application; } | 122 AudioEncoderOpusConfig::ApplicationMode application() const { |
| 123 return config_.application; |
| 124 } |
| 142 bool fec_enabled() const { return config_.fec_enabled; } | 125 bool fec_enabled() const { return config_.fec_enabled; } |
| 143 size_t num_channels_to_encode() const { return num_channels_to_encode_; } | 126 size_t num_channels_to_encode() const { return num_channels_to_encode_; } |
| 144 int next_frame_length_ms() const { return next_frame_length_ms_; } | 127 int next_frame_length_ms() const { return next_frame_length_ms_; } |
| 145 | 128 |
| 146 protected: | 129 protected: |
| 147 EncodedInfo EncodeImpl(uint32_t rtp_timestamp, | 130 EncodedInfo EncodeImpl(uint32_t rtp_timestamp, |
| 148 rtc::ArrayView<const int16_t> audio, | 131 rtc::ArrayView<const int16_t> audio, |
| 149 rtc::Buffer* encoded) override; | 132 rtc::Buffer* encoded) override; |
| 150 | 133 |
| 151 private: | 134 private: |
| 152 class PacketLossFractionSmoother; | 135 class PacketLossFractionSmoother; |
| 153 | 136 |
| 154 size_t Num10msFramesPerPacket() const; | 137 size_t Num10msFramesPerPacket() const; |
| 155 size_t SamplesPer10msFrame() const; | 138 size_t SamplesPer10msFrame() const; |
| 156 size_t SufficientOutputBufferSize() const; | 139 size_t SufficientOutputBufferSize() const; |
| 157 bool RecreateEncoderInstance(const Config& config); | 140 bool RecreateEncoderInstance(const AudioEncoderOpusConfig& config); |
| 158 void SetFrameLength(int frame_length_ms); | 141 void SetFrameLength(int frame_length_ms); |
| 159 void SetNumChannelsToEncode(size_t num_channels_to_encode); | 142 void SetNumChannelsToEncode(size_t num_channels_to_encode); |
| 160 void SetProjectedPacketLossRate(float fraction); | 143 void SetProjectedPacketLossRate(float fraction); |
| 161 | 144 |
| 162 // TODO(minyue): remove "override" when we can deprecate | 145 // TODO(minyue): remove "override" when we can deprecate |
| 163 // |AudioEncoder::SetTargetBitrate|. | 146 // |AudioEncoder::SetTargetBitrate|. |
| 164 void SetTargetBitrate(int target_bps) override; | 147 void SetTargetBitrate(int target_bps) override; |
| 165 | 148 |
| 166 void ApplyAudioNetworkAdaptor(); | 149 void ApplyAudioNetworkAdaptor(); |
| 167 std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator( | 150 std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator( |
| 168 const ProtoString& config_string, | 151 const ProtoString& config_string, |
| 169 RtcEventLog* event_log) const; | 152 RtcEventLog* event_log) const; |
| 170 | 153 |
| 171 void MaybeUpdateUplinkBandwidth(); | 154 void MaybeUpdateUplinkBandwidth(); |
| 172 | 155 |
| 173 Config config_; | 156 AudioEncoderOpusConfig config_; |
| 157 const int payload_type_; |
| 174 const bool send_side_bwe_with_overhead_; | 158 const bool send_side_bwe_with_overhead_; |
| 175 float packet_loss_rate_; | 159 float packet_loss_rate_; |
| 176 std::vector<int16_t> input_buffer_; | 160 std::vector<int16_t> input_buffer_; |
| 177 OpusEncInst* inst_; | 161 OpusEncInst* inst_; |
| 178 uint32_t first_timestamp_in_buffer_; | 162 uint32_t first_timestamp_in_buffer_; |
| 179 size_t num_channels_to_encode_; | 163 size_t num_channels_to_encode_; |
| 180 int next_frame_length_ms_; | 164 int next_frame_length_ms_; |
| 181 int complexity_; | 165 int complexity_; |
| 182 std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_; | 166 std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_; |
| 183 AudioNetworkAdaptorCreator audio_network_adaptor_creator_; | 167 AudioNetworkAdaptorCreator audio_network_adaptor_creator_; |
| 184 std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_; | 168 std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_; |
| 185 rtc::Optional<size_t> overhead_bytes_per_packet_; | 169 rtc::Optional<size_t> overhead_bytes_per_packet_; |
| 186 const std::unique_ptr<SmoothingFilter> bitrate_smoother_; | 170 const std::unique_ptr<SmoothingFilter> bitrate_smoother_; |
| 187 rtc::Optional<int64_t> bitrate_smoother_last_update_time_; | 171 rtc::Optional<int64_t> bitrate_smoother_last_update_time_; |
| 188 | 172 |
| 189 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpus); | 173 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpus); |
| 190 }; | 174 }; |
| 191 | 175 |
| 192 } // namespace webrtc | 176 } // namespace webrtc |
| 193 | 177 |
| 194 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 178 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
| OLD | NEW |