Chromium Code Reviews| Index: webrtc/audio/audio_send_stream.cc |
| diff --git a/webrtc/audio/audio_send_stream.cc b/webrtc/audio/audio_send_stream.cc |
| index aff605fe6a6845b995fd2082d458d8e5b9f45f4c..dad0fa0bf8b6172ad158187d6695b7a3103ebf92 100644 |
| --- a/webrtc/audio/audio_send_stream.cc |
| +++ b/webrtc/audio/audio_send_stream.cc |
| @@ -11,6 +11,7 @@ |
| #include "webrtc/audio/audio_send_stream.h" |
| #include <string> |
| +#include <utility> |
| #include "webrtc/audio/audio_state.h" |
| #include "webrtc/audio/conversion.h" |
| @@ -41,6 +42,23 @@ bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { |
| } // namespace |
| namespace internal { |
| + |
| +class AudioSendStream::AdaptCodecTask : public rtc::QueuedTask { |
| + public: |
| + explicit AdaptCodecTask(const rtc::WeakPtr<AudioSendStream>& send_stream) |
| + : send_stream_(std::move(send_stream)) {} |
| + |
| + private: |
| + bool Run() override { |
| + if (send_stream_) { |
| + send_stream_->AdaptCodec(); |
| + } |
| + return true; |
| + } |
| + |
| + rtc::WeakPtr<AudioSendStream> send_stream_; |
| +}; |
| + |
| AudioSendStream::AudioSendStream( |
| const webrtc::AudioSendStream::Config& config, |
| const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
| @@ -53,7 +71,8 @@ AudioSendStream::AudioSendStream( |
| : worker_queue_(worker_queue), |
| config_(config), |
| audio_state_(audio_state), |
| - bitrate_allocator_(bitrate_allocator) { |
| + bitrate_allocator_(bitrate_allocator), |
| + adapt_codec_task_started_(false) { |
| LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
| RTC_DCHECK_NE(config_.voe_channel_id, -1); |
| RTC_DCHECK(audio_state_.get()); |
| @@ -105,6 +124,8 @@ void AudioSendStream::Start() { |
| RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); |
| rtc::Event thread_sync_event(false /* manual_reset */, false); |
| worker_queue_->PostTask([this, &thread_sync_event] { |
| + weak_ptr_factory_.reset(new rtc::WeakPtrFactory<AudioSendStream>(this)); |
| + weak_ptr_ = weak_ptr_factory_->GetWeakPtr(); |
|
stefan-webrtc
2016/12/14 11:58:33
We should give weak_ptr_ a more descriptive name.
michaelt
2016/12/14 13:45:51
Done.
|
| bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, |
| config_.max_bitrate_bps, 0, true); |
| thread_sync_event.Set(); |
| @@ -124,6 +145,8 @@ void AudioSendStream::Stop() { |
| rtc::Event thread_sync_event(false /* manual_reset */, false); |
| worker_queue_->PostTask([this, &thread_sync_event] { |
| bitrate_allocator_->RemoveObserver(this); |
| + weak_ptr_factory_.reset(nullptr); |
| + adapt_codec_task_started_ = false; |
| thread_sync_event.Set(); |
| }); |
| thread_sync_event.Wait(rtc::Event::kForever); |
| @@ -234,6 +257,7 @@ uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
| uint8_t fraction_loss, |
| int64_t rtt, |
| int64_t probing_interval_ms) { |
| + RTC_DCHECK_RUN_ON(worker_queue_); |
| RTC_DCHECK_GE(bitrate_bps, |
| static_cast<uint32_t>(config_.min_bitrate_bps)); |
| // The bitrate allocator might allocate an higher than max configured bitrate |
| @@ -244,6 +268,12 @@ uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
| channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms); |
| + if (!adapt_codec_task_started_) { |
| + // Starts adapt codec task, which calls AdaptCodec on a timely base. |
| + worker_queue_->PostTask([this]() { AdaptCodec(); }); |
|
stefan-webrtc
2016/12/14 11:58:33
It's still not clear to me why we can't call Adapt
michaelt
2016/12/14 13:45:51
I removed "RTC_DCHECK_RUN_ON(worker_queue_);" sinc
stefan-webrtc
2016/12/14 16:08:02
So we want to allow calling OnBitrateUpdated on a
|
| + adapt_codec_task_started_ = true; |
| + } |
| + |
| // The amount of audio protection is not exposed by the encoder, hence |
| // always returning 0. |
| return 0; |
| @@ -386,5 +416,13 @@ bool AudioSendStream::SetupSendCodec() { |
| return true; |
| } |
| +void AudioSendStream::AdaptCodec() { |
| + RTC_DCHECK_RUN_ON(worker_queue_); |
| + channel_proxy_->AdaptCodec(); |
| + worker_queue_->PostDelayedTask( |
| + std::unique_ptr<rtc::QueuedTask>(new AdaptCodecTask(weak_ptr_)), |
| + config_.adapt_codec_interval_ms); |
| +} |
| + |
| } // namespace internal |
| } // namespace webrtc |