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_) { | |
stefan-webrtc
2016/12/13 12:29:55
Are we not able to DCHECK this because the worker
michaelt
2016/12/13 17:01:26
Yes and that is as well the reason i used weak poi
| |
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), |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 channel_proxy_->SetRtcEventLog(nullptr); | 116 channel_proxy_->SetRtcEventLog(nullptr); |
99 channel_proxy_->SetRtcpRttStats(nullptr); | 117 channel_proxy_->SetRtcpRttStats(nullptr); |
100 } | 118 } |
101 | 119 |
102 void AudioSendStream::Start() { | 120 void AudioSendStream::Start() { |
103 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 121 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
104 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { | 122 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { |
105 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); | 123 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); |
106 rtc::Event thread_sync_event(false /* manual_reset */, false); | 124 rtc::Event thread_sync_event(false /* manual_reset */, false); |
107 worker_queue_->PostTask([this, &thread_sync_event] { | 125 worker_queue_->PostTask([this, &thread_sync_event] { |
126 weak_ptr_factory_.reset(new rtc::WeakPtrFactory<AudioSendStream>(this)); | |
127 weak_ptr_ = weak_ptr_factory_->GetWeakPtr(); | |
108 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, | 128 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, |
109 config_.max_bitrate_bps, 0, true); | 129 config_.max_bitrate_bps, 0, true); |
110 thread_sync_event.Set(); | 130 thread_sync_event.Set(); |
111 }); | 131 }); |
112 thread_sync_event.Wait(rtc::Event::kForever); | 132 thread_sync_event.Wait(rtc::Event::kForever); |
113 } | 133 } |
114 | 134 |
115 ScopedVoEInterface<VoEBase> base(voice_engine()); | 135 ScopedVoEInterface<VoEBase> base(voice_engine()); |
116 int error = base->StartSend(config_.voe_channel_id); | 136 int error = base->StartSend(config_.voe_channel_id); |
117 if (error != 0) { | 137 if (error != 0) { |
118 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; | 138 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; |
119 } | 139 } |
120 } | 140 } |
121 | 141 |
122 void AudioSendStream::Stop() { | 142 void AudioSendStream::Stop() { |
123 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 143 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
124 rtc::Event thread_sync_event(false /* manual_reset */, false); | 144 rtc::Event thread_sync_event(false /* manual_reset */, false); |
125 worker_queue_->PostTask([this, &thread_sync_event] { | 145 worker_queue_->PostTask([this, &thread_sync_event] { |
126 bitrate_allocator_->RemoveObserver(this); | 146 bitrate_allocator_->RemoveObserver(this); |
147 weak_ptr_factory_.reset(nullptr); | |
127 thread_sync_event.Set(); | 148 thread_sync_event.Set(); |
128 }); | 149 }); |
129 thread_sync_event.Wait(rtc::Event::kForever); | 150 thread_sync_event.Wait(rtc::Event::kForever); |
130 | 151 |
131 ScopedVoEInterface<VoEBase> base(voice_engine()); | 152 ScopedVoEInterface<VoEBase> base(voice_engine()); |
132 int error = base->StopSend(config_.voe_channel_id); | 153 int error = base->StopSend(config_.voe_channel_id); |
133 if (error != 0) { | 154 if (error != 0) { |
134 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; | 155 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; |
135 } | 156 } |
136 } | 157 } |
(...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 | 248 // calls on the worker thread. We should move towards always using a network |
228 // thread. Then this check can be enabled. | 249 // thread. Then this check can be enabled. |
229 // RTC_DCHECK(!thread_checker_.CalledOnValidThread()); | 250 // RTC_DCHECK(!thread_checker_.CalledOnValidThread()); |
230 return channel_proxy_->ReceivedRTCPPacket(packet, length); | 251 return channel_proxy_->ReceivedRTCPPacket(packet, length); |
231 } | 252 } |
232 | 253 |
233 uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, | 254 uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
234 uint8_t fraction_loss, | 255 uint8_t fraction_loss, |
235 int64_t rtt, | 256 int64_t rtt, |
236 int64_t probing_interval_ms) { | 257 int64_t probing_interval_ms) { |
258 RTC_DCHECK_RUN_ON(worker_queue_); | |
237 RTC_DCHECK_GE(bitrate_bps, | 259 RTC_DCHECK_GE(bitrate_bps, |
238 static_cast<uint32_t>(config_.min_bitrate_bps)); | 260 static_cast<uint32_t>(config_.min_bitrate_bps)); |
239 // The bitrate allocator might allocate an higher than max configured bitrate | 261 // 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. | 262 // 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; | 263 const uint32_t max_bitrate_bps = config_.max_bitrate_bps; |
242 if (bitrate_bps > max_bitrate_bps) | 264 if (bitrate_bps > max_bitrate_bps) |
243 bitrate_bps = max_bitrate_bps; | 265 bitrate_bps = max_bitrate_bps; |
244 | 266 |
245 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms); | 267 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms); |
246 | 268 |
269 if (first_update_bitrate_()) { | |
stefan-webrtc
2016/12/13 12:29:55
I think it would make sense to comment on why this
michaelt
2016/12/13 17:01:26
Done.
| |
270 worker_queue_->PostTask([this]() { AdaptCodec(); }); | |
the sun
2016/12/15 14:58:37
When is OnBitrateUpdated() called?
IIUC you're st
michaelt
2016/12/19 14:43:28
I removed the conditions here a start the task in
| |
271 } | |
272 | |
247 // The amount of audio protection is not exposed by the encoder, hence | 273 // The amount of audio protection is not exposed by the encoder, hence |
248 // always returning 0. | 274 // always returning 0. |
249 return 0; | 275 return 0; |
250 } | 276 } |
251 | 277 |
252 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { | 278 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { |
253 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 279 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
254 return config_; | 280 return config_; |
255 } | 281 } |
256 | 282 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 // interaction between VAD and Opus FEC. | 405 // interaction between VAD and Opus FEC. |
380 if (codec->SetVADStatus(channel, true) != 0) { | 406 if (codec->SetVADStatus(channel, true) != 0) { |
381 LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); | 407 LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); |
382 return false; | 408 return false; |
383 } | 409 } |
384 } | 410 } |
385 } | 411 } |
386 return true; | 412 return true; |
387 } | 413 } |
388 | 414 |
415 void AudioSendStream::AdaptCodec() { | |
416 RTC_DCHECK_RUN_ON(worker_queue_); | |
417 channel_proxy_->AdaptCodec(); | |
418 constexpr uint32_t kAdaptCodecIntervalMs = 200; | |
419 worker_queue_->PostDelayedTask( | |
stefan-webrtc
2016/12/13 12:29:55
Is there a point in posting another task here? Can
michaelt
2016/12/13 17:01:26
This post is the trick of this CL.
We have to call
| |
420 std::unique_ptr<rtc::QueuedTask>(new AdaptCodecTask(weak_ptr_)), | |
the sun
2016/12/13 16:43:24
Can you just post a lambda here, calling AdaptCode
michaelt
2016/12/13 17:01:26
The QueuedTask and the weak pointer implemented to
the sun
2016/12/15 14:58:37
I see. A subtle effect here is that you won't be r
michaelt
2016/12/19 14:43:28
Done.
| |
421 kAdaptCodecIntervalMs); | |
422 } | |
423 | |
389 } // namespace internal | 424 } // namespace internal |
390 } // namespace webrtc | 425 } // namespace webrtc |
OLD | NEW |