| 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 AudioEncoderOpusImpl final : public AudioEncoder { |
| 35 public: | 36 public: |
| 36 enum ApplicationMode { | 37 static AudioEncoderOpusConfig CreateConfig(const CodecInst& codec_inst); |
| 37 kVoip = 0, | 38 static rtc::Optional<AudioEncoderOpusConfig> SdpToConfig( |
| 38 kAudio = 1, | 39 const SdpAudioFormat& format); |
| 39 }; | |
| 40 | 40 |
| 41 struct Config { | 41 // Returns empty if the current bitrate falls within the hysteresis window, |
| 42 Config(); | 42 // defined by complexity_threshold_bps +/- complexity_threshold_window_bps. |
| 43 Config(const Config&); | 43 // Otherwise, returns the current complexity depending on whether the current |
| 44 ~Config(); | 44 // bitrate is above or below complexity_threshold_bps. |
| 45 Config& operator=(const Config&); | 45 static rtc::Optional<int> GetNewComplexity( |
| 46 | 46 const AudioEncoderOpusConfig& config); |
| 47 bool IsOk() const; | |
| 48 int GetBitrateBps() const; | |
| 49 // Returns empty if the current bitrate falls within the hysteresis window, | |
| 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 | |
| 55 static constexpr int kDefaultFrameSizeMs = 20; | |
| 56 int frame_size_ms = kDefaultFrameSizeMs; | |
| 57 size_t num_channels = 1; | |
| 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 | |
| 74 private: | |
| 75 #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS) || defined(WEBRTC_ARCH_ARM) | |
| 76 // If we are on Android, iOS and/or ARM, use a lower complexity setting as | |
| 77 // default, to save encoder complexity. | |
| 78 static const int kDefaultComplexity = 5; | |
| 79 #else | |
| 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 | 47 |
| 87 using AudioNetworkAdaptorCreator = | 48 using AudioNetworkAdaptorCreator = |
| 88 std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&, | 49 std::function<std::unique_ptr<AudioNetworkAdaptor>(const std::string&, |
| 89 RtcEventLog*)>; | 50 RtcEventLog*)>; |
| 90 AudioEncoderOpus( | 51 AudioEncoderOpusImpl( |
| 91 const Config& config, | 52 const AudioEncoderOpusConfig& config, |
| 53 int payload_type, |
| 92 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr, | 54 AudioNetworkAdaptorCreator&& audio_network_adaptor_creator = nullptr, |
| 93 std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr); | 55 std::unique_ptr<SmoothingFilter> bitrate_smoother = nullptr); |
| 94 | 56 |
| 95 explicit AudioEncoderOpus(const CodecInst& codec_inst); | 57 explicit AudioEncoderOpusImpl(const CodecInst& codec_inst); |
| 96 AudioEncoderOpus(int payload_type, const SdpAudioFormat& format); | 58 AudioEncoderOpusImpl(int payload_type, const SdpAudioFormat& format); |
| 97 ~AudioEncoderOpus() override; | 59 ~AudioEncoderOpusImpl() override; |
| 98 | 60 |
| 99 // Static interface for use by BuiltinAudioEncoderFactory. | 61 // Static interface for use by BuiltinAudioEncoderFactory. |
| 100 static constexpr const char* GetPayloadName() { return "opus"; } | 62 static constexpr const char* GetPayloadName() { return "opus"; } |
| 101 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( | 63 static rtc::Optional<AudioCodecInfo> QueryAudioEncoder( |
| 102 const SdpAudioFormat& format); | 64 const SdpAudioFormat& format); |
| 103 | 65 |
| 104 int SampleRateHz() const override; | 66 int SampleRateHz() const override; |
| 105 size_t NumChannels() const override; | 67 size_t NumChannels() const override; |
| 106 size_t Num10MsFramesInNextPacket() const override; | 68 size_t Num10MsFramesInNextPacket() const override; |
| 107 size_t Max10MsFramesInAPacket() const override; | 69 size_t Max10MsFramesInAPacket() const override; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 131 void OnReceivedRtt(int rtt_ms) override; | 93 void OnReceivedRtt(int rtt_ms) override; |
| 132 void OnReceivedOverhead(size_t overhead_bytes_per_packet) override; | 94 void OnReceivedOverhead(size_t overhead_bytes_per_packet) override; |
| 133 void SetReceiverFrameLengthRange(int min_frame_length_ms, | 95 void SetReceiverFrameLengthRange(int min_frame_length_ms, |
| 134 int max_frame_length_ms) override; | 96 int max_frame_length_ms) override; |
| 135 rtc::ArrayView<const int> supported_frame_lengths_ms() const { | 97 rtc::ArrayView<const int> supported_frame_lengths_ms() const { |
| 136 return config_.supported_frame_lengths_ms; | 98 return config_.supported_frame_lengths_ms; |
| 137 } | 99 } |
| 138 | 100 |
| 139 // Getters for testing. | 101 // Getters for testing. |
| 140 float packet_loss_rate() const { return packet_loss_rate_; } | 102 float packet_loss_rate() const { return packet_loss_rate_; } |
| 141 ApplicationMode application() const { return config_.application; } | 103 AudioEncoderOpusConfig::ApplicationMode application() const { |
| 104 return config_.application; |
| 105 } |
| 142 bool fec_enabled() const { return config_.fec_enabled; } | 106 bool fec_enabled() const { return config_.fec_enabled; } |
| 143 size_t num_channels_to_encode() const { return num_channels_to_encode_; } | 107 size_t num_channels_to_encode() const { return num_channels_to_encode_; } |
| 144 int next_frame_length_ms() const { return next_frame_length_ms_; } | 108 int next_frame_length_ms() const { return next_frame_length_ms_; } |
| 145 | 109 |
| 146 protected: | 110 protected: |
| 147 EncodedInfo EncodeImpl(uint32_t rtp_timestamp, | 111 EncodedInfo EncodeImpl(uint32_t rtp_timestamp, |
| 148 rtc::ArrayView<const int16_t> audio, | 112 rtc::ArrayView<const int16_t> audio, |
| 149 rtc::Buffer* encoded) override; | 113 rtc::Buffer* encoded) override; |
| 150 | 114 |
| 151 private: | 115 private: |
| 152 class PacketLossFractionSmoother; | 116 class PacketLossFractionSmoother; |
| 153 | 117 |
| 154 size_t Num10msFramesPerPacket() const; | 118 size_t Num10msFramesPerPacket() const; |
| 155 size_t SamplesPer10msFrame() const; | 119 size_t SamplesPer10msFrame() const; |
| 156 size_t SufficientOutputBufferSize() const; | 120 size_t SufficientOutputBufferSize() const; |
| 157 bool RecreateEncoderInstance(const Config& config); | 121 bool RecreateEncoderInstance(const AudioEncoderOpusConfig& config); |
| 158 void SetFrameLength(int frame_length_ms); | 122 void SetFrameLength(int frame_length_ms); |
| 159 void SetNumChannelsToEncode(size_t num_channels_to_encode); | 123 void SetNumChannelsToEncode(size_t num_channels_to_encode); |
| 160 void SetProjectedPacketLossRate(float fraction); | 124 void SetProjectedPacketLossRate(float fraction); |
| 161 | 125 |
| 162 // TODO(minyue): remove "override" when we can deprecate | 126 // TODO(minyue): remove "override" when we can deprecate |
| 163 // |AudioEncoder::SetTargetBitrate|. | 127 // |AudioEncoder::SetTargetBitrate|. |
| 164 void SetTargetBitrate(int target_bps) override; | 128 void SetTargetBitrate(int target_bps) override; |
| 165 | 129 |
| 166 void ApplyAudioNetworkAdaptor(); | 130 void ApplyAudioNetworkAdaptor(); |
| 167 std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator( | 131 std::unique_ptr<AudioNetworkAdaptor> DefaultAudioNetworkAdaptorCreator( |
| 168 const ProtoString& config_string, | 132 const ProtoString& config_string, |
| 169 RtcEventLog* event_log) const; | 133 RtcEventLog* event_log) const; |
| 170 | 134 |
| 171 void MaybeUpdateUplinkBandwidth(); | 135 void MaybeUpdateUplinkBandwidth(); |
| 172 | 136 |
| 173 Config config_; | 137 AudioEncoderOpusConfig config_; |
| 138 const int payload_type_; |
| 174 const bool send_side_bwe_with_overhead_; | 139 const bool send_side_bwe_with_overhead_; |
| 175 float packet_loss_rate_; | 140 float packet_loss_rate_; |
| 176 std::vector<int16_t> input_buffer_; | 141 std::vector<int16_t> input_buffer_; |
| 177 OpusEncInst* inst_; | 142 OpusEncInst* inst_; |
| 178 uint32_t first_timestamp_in_buffer_; | 143 uint32_t first_timestamp_in_buffer_; |
| 179 size_t num_channels_to_encode_; | 144 size_t num_channels_to_encode_; |
| 180 int next_frame_length_ms_; | 145 int next_frame_length_ms_; |
| 181 int complexity_; | 146 int complexity_; |
| 182 std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_; | 147 std::unique_ptr<PacketLossFractionSmoother> packet_loss_fraction_smoother_; |
| 183 AudioNetworkAdaptorCreator audio_network_adaptor_creator_; | 148 AudioNetworkAdaptorCreator audio_network_adaptor_creator_; |
| 184 std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_; | 149 std::unique_ptr<AudioNetworkAdaptor> audio_network_adaptor_; |
| 185 rtc::Optional<size_t> overhead_bytes_per_packet_; | 150 rtc::Optional<size_t> overhead_bytes_per_packet_; |
| 186 const std::unique_ptr<SmoothingFilter> bitrate_smoother_; | 151 const std::unique_ptr<SmoothingFilter> bitrate_smoother_; |
| 187 rtc::Optional<int64_t> bitrate_smoother_last_update_time_; | 152 rtc::Optional<int64_t> bitrate_smoother_last_update_time_; |
| 188 | 153 |
| 189 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpus); | 154 RTC_DISALLOW_COPY_AND_ASSIGN(AudioEncoderOpusImpl); |
| 190 }; | 155 }; |
| 191 | 156 |
| 192 } // namespace webrtc | 157 } // namespace webrtc |
| 193 | 158 |
| 194 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ | 159 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_OPUS_AUDIO_ENCODER_OPUS_H_ |
| OLD | NEW |