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 PacketRouter* packet_router, | 66 PacketRouter* packet_router, |
49 CongestionController* congestion_controller, | 67 CongestionController* congestion_controller, |
50 BitrateAllocator* bitrate_allocator, | 68 BitrateAllocator* bitrate_allocator, |
51 RtcEventLog* event_log, | 69 RtcEventLog* event_log, |
52 RtcpRttStats* rtcp_rtt_stats) | 70 RtcpRttStats* rtcp_rtt_stats) |
53 : worker_queue_(worker_queue), | 71 : worker_queue_(worker_queue), |
54 config_(config), | 72 config_(config), |
55 audio_state_(audio_state), | 73 audio_state_(audio_state), |
56 bitrate_allocator_(bitrate_allocator) { | 74 bitrate_allocator_(bitrate_allocator), |
75 adapt_codec_task_started_(false) { | |
57 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); | 76 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
58 RTC_DCHECK_NE(config_.voe_channel_id, -1); | 77 RTC_DCHECK_NE(config_.voe_channel_id, -1); |
59 RTC_DCHECK(audio_state_.get()); | 78 RTC_DCHECK(audio_state_.get()); |
60 RTC_DCHECK(congestion_controller); | 79 RTC_DCHECK(congestion_controller); |
61 | 80 |
62 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); | 81 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); |
63 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); | 82 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); |
64 channel_proxy_->SetRtcEventLog(event_log); | 83 channel_proxy_->SetRtcEventLog(event_log); |
65 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); | 84 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); |
66 channel_proxy_->RegisterSenderCongestionControlObjects( | 85 channel_proxy_->RegisterSenderCongestionControlObjects( |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 channel_proxy_->SetRtcEventLog(nullptr); | 117 channel_proxy_->SetRtcEventLog(nullptr); |
99 channel_proxy_->SetRtcpRttStats(nullptr); | 118 channel_proxy_->SetRtcpRttStats(nullptr); |
100 } | 119 } |
101 | 120 |
102 void AudioSendStream::Start() { | 121 void AudioSendStream::Start() { |
103 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 122 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
104 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { | 123 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { |
105 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); | 124 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); |
106 rtc::Event thread_sync_event(false /* manual_reset */, false); | 125 rtc::Event thread_sync_event(false /* manual_reset */, false); |
107 worker_queue_->PostTask([this, &thread_sync_event] { | 126 worker_queue_->PostTask([this, &thread_sync_event] { |
127 weak_ptr_factory_.reset(new rtc::WeakPtrFactory<AudioSendStream>(this)); | |
128 weak_ptr_ = weak_ptr_factory_->GetWeakPtr(); | |
108 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, | 129 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, |
109 config_.max_bitrate_bps, 0, true); | 130 config_.max_bitrate_bps, 0, true); |
110 thread_sync_event.Set(); | 131 thread_sync_event.Set(); |
111 }); | 132 }); |
112 thread_sync_event.Wait(rtc::Event::kForever); | 133 thread_sync_event.Wait(rtc::Event::kForever); |
113 } | 134 } |
114 | 135 |
115 ScopedVoEInterface<VoEBase> base(voice_engine()); | 136 ScopedVoEInterface<VoEBase> base(voice_engine()); |
116 int error = base->StartSend(config_.voe_channel_id); | 137 int error = base->StartSend(config_.voe_channel_id); |
117 if (error != 0) { | 138 if (error != 0) { |
118 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; | 139 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; |
119 } | 140 } |
120 } | 141 } |
121 | 142 |
122 void AudioSendStream::Stop() { | 143 void AudioSendStream::Stop() { |
123 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 144 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
124 rtc::Event thread_sync_event(false /* manual_reset */, false); | 145 rtc::Event thread_sync_event(false /* manual_reset */, false); |
125 worker_queue_->PostTask([this, &thread_sync_event] { | 146 worker_queue_->PostTask([this, &thread_sync_event] { |
126 bitrate_allocator_->RemoveObserver(this); | 147 bitrate_allocator_->RemoveObserver(this); |
148 weak_ptr_factory_.reset(nullptr); | |
149 adapt_codec_task_started_ = false; | |
127 thread_sync_event.Set(); | 150 thread_sync_event.Set(); |
128 }); | 151 }); |
129 thread_sync_event.Wait(rtc::Event::kForever); | 152 thread_sync_event.Wait(rtc::Event::kForever); |
130 | 153 |
131 ScopedVoEInterface<VoEBase> base(voice_engine()); | 154 ScopedVoEInterface<VoEBase> base(voice_engine()); |
132 int error = base->StopSend(config_.voe_channel_id); | 155 int error = base->StopSend(config_.voe_channel_id); |
133 if (error != 0) { | 156 if (error != 0) { |
134 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; | 157 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; |
135 } | 158 } |
136 } | 159 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
227 // calls on the worker thread. We should move towards always using a network | 250 // calls on the worker thread. We should move towards always using a network |
228 // thread. Then this check can be enabled. | 251 // thread. Then this check can be enabled. |
229 // RTC_DCHECK(!thread_checker_.CalledOnValidThread()); | 252 // RTC_DCHECK(!thread_checker_.CalledOnValidThread()); |
230 return channel_proxy_->ReceivedRTCPPacket(packet, length); | 253 return channel_proxy_->ReceivedRTCPPacket(packet, length); |
231 } | 254 } |
232 | 255 |
233 uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, | 256 uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
234 uint8_t fraction_loss, | 257 uint8_t fraction_loss, |
235 int64_t rtt, | 258 int64_t rtt, |
236 int64_t probing_interval_ms) { | 259 int64_t probing_interval_ms) { |
260 RTC_DCHECK_RUN_ON(worker_queue_); | |
237 RTC_DCHECK_GE(bitrate_bps, | 261 RTC_DCHECK_GE(bitrate_bps, |
238 static_cast<uint32_t>(config_.min_bitrate_bps)); | 262 static_cast<uint32_t>(config_.min_bitrate_bps)); |
239 // The bitrate allocator might allocate an higher than max configured bitrate | 263 // The bitrate allocator might allocate an higher than max configured bitrate |
240 // if there is room, to allow for, as example, extra FEC. Ignore that for now. | 264 // if there is room, to allow for, as example, extra FEC. Ignore that for now. |
241 const uint32_t max_bitrate_bps = config_.max_bitrate_bps; | 265 const uint32_t max_bitrate_bps = config_.max_bitrate_bps; |
242 if (bitrate_bps > max_bitrate_bps) | 266 if (bitrate_bps > max_bitrate_bps) |
243 bitrate_bps = max_bitrate_bps; | 267 bitrate_bps = max_bitrate_bps; |
244 | 268 |
245 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms); | 269 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms); |
246 | 270 |
271 if (!adapt_codec_task_started_) { | |
272 // Starts adapt codec task, which calls AdaptCodec on a timely base. | |
273 worker_queue_->PostTask([this]() { AdaptCodec(); }); | |
274 adapt_codec_task_started_ = true; | |
275 } | |
276 | |
247 // The amount of audio protection is not exposed by the encoder, hence | 277 // The amount of audio protection is not exposed by the encoder, hence |
248 // always returning 0. | 278 // always returning 0. |
249 return 0; | 279 return 0; |
250 } | 280 } |
251 | 281 |
252 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { | 282 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { |
253 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 283 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
254 return config_; | 284 return config_; |
255 } | 285 } |
256 | 286 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 // interaction between VAD and Opus FEC. | 409 // interaction between VAD and Opus FEC. |
380 if (codec->SetVADStatus(channel, true) != 0) { | 410 if (codec->SetVADStatus(channel, true) != 0) { |
381 LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); | 411 LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); |
382 return false; | 412 return false; |
383 } | 413 } |
384 } | 414 } |
385 } | 415 } |
386 return true; | 416 return true; |
387 } | 417 } |
388 | 418 |
419 void AudioSendStream::AdaptCodec() { | |
420 RTC_DCHECK_RUN_ON(worker_queue_); | |
421 channel_proxy_->AdaptCodec(); | |
422 constexpr uint32_t kAdaptCodecIntervalMs = 200; | |
minyue-webrtc
2016/12/13 17:21:27
oh, this, how about making it a config_ field?
michaelt
2016/12/14 11:31:21
Done.
| |
423 worker_queue_->PostDelayedTask( | |
424 std::unique_ptr<rtc::QueuedTask>(new AdaptCodecTask(weak_ptr_)), | |
425 kAdaptCodecIntervalMs); | |
426 } | |
427 | |
389 } // namespace internal | 428 } // namespace internal |
390 } // namespace webrtc | 429 } // namespace webrtc |
OLD | NEW |