OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 #include "webrtc/audio/audio_send_stream.h" | 11 #include "webrtc/audio/audio_send_stream.h" |
12 | 12 |
13 #include <string> | 13 #include <string> |
| 14 #include <utility> |
14 | 15 |
15 #include "webrtc/audio/audio_state.h" | 16 #include "webrtc/audio/audio_state.h" |
16 #include "webrtc/audio/conversion.h" | 17 #include "webrtc/audio/conversion.h" |
17 #include "webrtc/audio/scoped_voe_interface.h" | 18 #include "webrtc/audio/scoped_voe_interface.h" |
18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/event.h" | 20 #include "webrtc/base/event.h" |
20 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/task_queue.h" | 22 #include "webrtc/base/task_queue.h" |
22 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 23 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
23 #include "webrtc/modules/pacing/paced_sender.h" | 24 #include "webrtc/modules/pacing/paced_sender.h" |
(...skipping 10 matching lines...) Expand all Loading... |
34 namespace { | 35 namespace { |
35 | 36 |
36 constexpr char kOpusCodecName[] = "opus"; | 37 constexpr char kOpusCodecName[] = "opus"; |
37 | 38 |
38 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { | 39 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { |
39 return (_stricmp(codec.plname, ref_name) == 0); | 40 return (_stricmp(codec.plname, ref_name) == 0); |
40 } | 41 } |
41 } // namespace | 42 } // namespace |
42 | 43 |
43 namespace internal { | 44 namespace internal { |
| 45 |
| 46 class AudioSendStream::AdaptCodecTask : public rtc::QueuedTask { |
| 47 public: |
| 48 explicit AdaptCodecTask(const rtc::WeakPtr<AudioSendStream>& send_stream) |
| 49 : send_stream_(std::move(send_stream)) {} |
| 50 |
| 51 private: |
| 52 bool Run() override { |
| 53 if (send_stream_) { |
| 54 send_stream_->AdaptCodec(); |
| 55 } |
| 56 return true; |
| 57 } |
| 58 |
| 59 rtc::WeakPtr<AudioSendStream> send_stream_; |
| 60 }; |
| 61 |
44 AudioSendStream::AudioSendStream( | 62 AudioSendStream::AudioSendStream( |
45 const webrtc::AudioSendStream::Config& config, | 63 const webrtc::AudioSendStream::Config& config, |
46 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, | 64 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
47 rtc::TaskQueue* worker_queue, | 65 rtc::TaskQueue* worker_queue, |
48 CongestionController* congestion_controller, | 66 CongestionController* congestion_controller, |
49 BitrateAllocator* bitrate_allocator, | 67 BitrateAllocator* bitrate_allocator, |
50 RtcEventLog* event_log) | 68 RtcEventLog* event_log) |
51 : worker_queue_(worker_queue), | 69 : worker_queue_(worker_queue), |
52 config_(config), | 70 config_(config), |
53 audio_state_(audio_state), | 71 audio_state_(audio_state), |
54 bitrate_allocator_(bitrate_allocator) { | 72 bitrate_allocator_(bitrate_allocator) { |
55 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); | 73 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
56 RTC_DCHECK_NE(config_.voe_channel_id, -1); | 74 RTC_DCHECK_NE(config_.voe_channel_id, -1); |
57 RTC_DCHECK(audio_state_.get()); | 75 RTC_DCHECK(audio_state_.get()); |
58 RTC_DCHECK(congestion_controller); | 76 RTC_DCHECK(congestion_controller); |
59 | |
60 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); | 77 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); |
61 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); | 78 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); |
62 channel_proxy_->SetRtcEventLog(event_log); | 79 channel_proxy_->SetRtcEventLog(event_log); |
63 channel_proxy_->RegisterSenderCongestionControlObjects( | 80 channel_proxy_->RegisterSenderCongestionControlObjects( |
64 congestion_controller->pacer(), | 81 congestion_controller->pacer(), |
65 congestion_controller->GetTransportFeedbackObserver(), | 82 congestion_controller->GetTransportFeedbackObserver(), |
66 congestion_controller->packet_router()); | 83 congestion_controller->packet_router()); |
67 channel_proxy_->SetRTCPStatus(true); | 84 channel_proxy_->SetRTCPStatus(true); |
68 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); | 85 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); |
69 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); | 86 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); |
(...skipping 25 matching lines...) Expand all Loading... |
95 channel_proxy_->ResetCongestionControlObjects(); | 112 channel_proxy_->ResetCongestionControlObjects(); |
96 channel_proxy_->SetRtcEventLog(nullptr); | 113 channel_proxy_->SetRtcEventLog(nullptr); |
97 } | 114 } |
98 | 115 |
99 void AudioSendStream::Start() { | 116 void AudioSendStream::Start() { |
100 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 117 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
101 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { | 118 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { |
102 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); | 119 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); |
103 rtc::Event thread_sync_event(false /* manual_reset */, false); | 120 rtc::Event thread_sync_event(false /* manual_reset */, false); |
104 worker_queue_->PostTask([this, &thread_sync_event] { | 121 worker_queue_->PostTask([this, &thread_sync_event] { |
| 122 weak_ptr_factory_.reset(new rtc::WeakPtrFactory<AudioSendStream>(this)); |
| 123 weak_ptr_ = weak_ptr_factory_->GetWeakPtr(); |
105 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, | 124 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, |
106 config_.max_bitrate_bps, 0, true); | 125 config_.max_bitrate_bps, 0, true); |
107 thread_sync_event.Set(); | 126 thread_sync_event.Set(); |
108 }); | 127 }); |
109 thread_sync_event.Wait(rtc::Event::kForever); | 128 thread_sync_event.Wait(rtc::Event::kForever); |
110 } | 129 } |
111 | 130 |
112 ScopedVoEInterface<VoEBase> base(voice_engine()); | 131 ScopedVoEInterface<VoEBase> base(voice_engine()); |
113 int error = base->StartSend(config_.voe_channel_id); | 132 int error = base->StartSend(config_.voe_channel_id); |
114 if (error != 0) { | 133 if (error != 0) { |
115 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; | 134 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; |
116 } | 135 } |
117 } | 136 } |
118 | 137 |
119 void AudioSendStream::Stop() { | 138 void AudioSendStream::Stop() { |
120 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 139 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
121 rtc::Event thread_sync_event(false /* manual_reset */, false); | 140 rtc::Event thread_sync_event(false /* manual_reset */, false); |
122 worker_queue_->PostTask([this, &thread_sync_event] { | 141 worker_queue_->PostTask([this, &thread_sync_event] { |
123 bitrate_allocator_->RemoveObserver(this); | 142 bitrate_allocator_->RemoveObserver(this); |
| 143 weak_ptr_factory_.reset(nullptr); |
124 thread_sync_event.Set(); | 144 thread_sync_event.Set(); |
125 }); | 145 }); |
126 thread_sync_event.Wait(rtc::Event::kForever); | 146 thread_sync_event.Wait(rtc::Event::kForever); |
127 | 147 |
128 ScopedVoEInterface<VoEBase> base(voice_engine()); | 148 ScopedVoEInterface<VoEBase> base(voice_engine()); |
129 int error = base->StopSend(config_.voe_channel_id); | 149 int error = base->StopSend(config_.voe_channel_id); |
130 if (error != 0) { | 150 if (error != 0) { |
131 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; | 151 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; |
132 } | 152 } |
133 } | 153 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 RTC_DCHECK_GE(bitrate_bps, | 253 RTC_DCHECK_GE(bitrate_bps, |
234 static_cast<uint32_t>(config_.min_bitrate_bps)); | 254 static_cast<uint32_t>(config_.min_bitrate_bps)); |
235 // The bitrate allocator might allocate an higher than max configured bitrate | 255 // The bitrate allocator might allocate an higher than max configured bitrate |
236 // if there is room, to allow for, as example, extra FEC. Ignore that for now. | 256 // if there is room, to allow for, as example, extra FEC. Ignore that for now. |
237 const uint32_t max_bitrate_bps = config_.max_bitrate_bps; | 257 const uint32_t max_bitrate_bps = config_.max_bitrate_bps; |
238 if (bitrate_bps > max_bitrate_bps) | 258 if (bitrate_bps > max_bitrate_bps) |
239 bitrate_bps = max_bitrate_bps; | 259 bitrate_bps = max_bitrate_bps; |
240 | 260 |
241 channel_proxy_->SetBitrate(bitrate_bps); | 261 channel_proxy_->SetBitrate(bitrate_bps); |
242 | 262 |
| 263 if (first_update_bitrate_()) { |
| 264 AdaptCodec(); |
| 265 } |
| 266 |
243 // The amount of audio protection is not exposed by the encoder, hence | 267 // The amount of audio protection is not exposed by the encoder, hence |
244 // always returning 0. | 268 // always returning 0. |
245 return 0; | 269 return 0; |
246 } | 270 } |
247 | 271 |
248 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { | 272 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { |
249 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 273 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
250 return config_; | 274 return config_; |
251 } | 275 } |
252 | 276 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 // interaction between VAD and Opus FEC. | 399 // interaction between VAD and Opus FEC. |
376 if (codec->SetVADStatus(channel, true) != 0) { | 400 if (codec->SetVADStatus(channel, true) != 0) { |
377 LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); | 401 LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); |
378 return false; | 402 return false; |
379 } | 403 } |
380 } | 404 } |
381 } | 405 } |
382 return true; | 406 return true; |
383 } | 407 } |
384 | 408 |
| 409 void AudioSendStream::AdaptCodec() { |
| 410 RTC_DCHECK_RUN_ON(worker_queue_); |
| 411 channel_proxy_->AdaptCodec(); |
| 412 constexpr uint32_t kAdaptCodecIntervalMs = 200; |
| 413 worker_queue_->PostDelayedTask( |
| 414 std::unique_ptr<rtc::QueuedTask>(new AdaptCodecTask(weak_ptr_)), |
| 415 kAdaptCodecIntervalMs); |
| 416 } |
| 417 |
385 } // namespace internal | 418 } // namespace internal |
386 } // namespace webrtc | 419 } // namespace webrtc |
OLD | NEW |