Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: webrtc/audio/audio_send_stream.cc

Issue 2638083002: Attach TransportFeedbackPacketLossTracker to ANA (PLR only) (Closed)
Patch Set: Fix UT Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 14
15 #include "webrtc/audio/audio_state.h" 15 #include "webrtc/audio/audio_state.h"
16 #include "webrtc/audio/conversion.h" 16 #include "webrtc/audio/conversion.h"
17 #include "webrtc/audio/scoped_voe_interface.h" 17 #include "webrtc/audio/scoped_voe_interface.h"
18 #include "webrtc/base/checks.h" 18 #include "webrtc/base/checks.h"
19 #include "webrtc/base/event.h" 19 #include "webrtc/base/event.h"
20 #include "webrtc/base/logging.h" 20 #include "webrtc/base/logging.h"
21 #include "webrtc/base/task_queue.h" 21 #include "webrtc/base/task_queue.h"
22 #include "webrtc/base/timeutils.h"
22 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" 23 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
23 #include "webrtc/modules/congestion_controller/include/send_side_congestion_cont roller.h" 24 #include "webrtc/modules/congestion_controller/include/send_side_congestion_cont roller.h"
24 #include "webrtc/modules/pacing/paced_sender.h" 25 #include "webrtc/modules/pacing/paced_sender.h"
25 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 26 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
26 #include "webrtc/voice_engine/channel_proxy.h" 27 #include "webrtc/voice_engine/channel_proxy.h"
27 #include "webrtc/voice_engine/include/voe_base.h" 28 #include "webrtc/voice_engine/include/voe_base.h"
28 #include "webrtc/voice_engine/transmit_mixer.h" 29 #include "webrtc/voice_engine/transmit_mixer.h"
29 #include "webrtc/voice_engine/voice_engine_impl.h" 30 #include "webrtc/voice_engine/voice_engine_impl.h"
30 31
31 namespace webrtc { 32 namespace webrtc {
32 33
33 namespace { 34 namespace {
34 35
35 constexpr char kOpusCodecName[] = "opus"; 36 constexpr char kOpusCodecName[] = "opus";
36 37
37 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { 38 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
38 return (STR_CASE_CMP(codec.plname, ref_name) == 0); 39 return (STR_CASE_CMP(codec.plname, ref_name) == 0);
39 } 40 }
40 } // namespace 41 } // namespace
41 42
42 namespace internal { 43 namespace internal {
44 // TODO(elad.alon): Subsequent CL will make these values experiment-dependent.
45 constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000;
46 constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
47 constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
48
43 AudioSendStream::AudioSendStream( 49 AudioSendStream::AudioSendStream(
44 const webrtc::AudioSendStream::Config& config, 50 const webrtc::AudioSendStream::Config& config,
45 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 51 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
46 rtc::TaskQueue* worker_queue, 52 rtc::TaskQueue* worker_queue,
47 PacketRouter* packet_router, 53 PacketRouter* packet_router,
48 SendSideCongestionController* send_side_cc, 54 SendSideCongestionController* send_side_cc,
49 BitrateAllocator* bitrate_allocator, 55 BitrateAllocator* bitrate_allocator,
50 RtcEventLog* event_log, 56 RtcEventLog* event_log,
51 RtcpRttStats* rtcp_rtt_stats) 57 RtcpRttStats* rtcp_rtt_stats)
52 : worker_queue_(worker_queue), 58 : worker_queue_(worker_queue),
53 config_(config), 59 config_(config),
54 audio_state_(audio_state), 60 audio_state_(audio_state),
55 bitrate_allocator_(bitrate_allocator), 61 bitrate_allocator_(bitrate_allocator),
56 send_side_cc_(send_side_cc) { 62 send_side_cc_(send_side_cc),
63 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs,
64 kPacketLossRateMinNumAckedPackets,
65 kRecoverablePacketLossRateMinNumAckedPairs) {
57 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); 66 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
58 RTC_DCHECK_NE(config_.voe_channel_id, -1); 67 RTC_DCHECK_NE(config_.voe_channel_id, -1);
59 RTC_DCHECK(audio_state_.get()); 68 RTC_DCHECK(audio_state_.get());
60 RTC_DCHECK(send_side_cc); 69 RTC_DCHECK(send_side_cc);
61 70
62 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); 71 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
63 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); 72 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
64 channel_proxy_->SetRtcEventLog(event_log); 73 channel_proxy_->SetRtcEventLog(event_log);
65 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); 74 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
66 channel_proxy_->SetRTCPStatus(true); 75 channel_proxy_->SetRTCPStatus(true);
67 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); 76 channel_proxy_->SetLocalSSRC(config.rtp.ssrc);
68 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); 77 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name);
69 // TODO(solenberg): Config NACK history window (which is a packet count), 78 // TODO(solenberg): Config NACK history window (which is a packet count),
70 // using the actual packet size for the configured codec. 79 // using the actual packet size for the configured codec.
71 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, 80 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
72 config_.rtp.nack.rtp_history_ms / 20); 81 config_.rtp.nack.rtp_history_ms / 20);
73 82
74 channel_proxy_->RegisterExternalTransport(config.send_transport); 83 channel_proxy_->RegisterExternalTransport(config.send_transport);
84 send_side_cc_->RegisterPacketFeedbackObserver(this);
75 85
76 for (const auto& extension : config.rtp.extensions) { 86 for (const auto& extension : config.rtp.extensions) {
77 if (extension.uri == RtpExtension::kAudioLevelUri) { 87 if (extension.uri == RtpExtension::kAudioLevelUri) {
78 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); 88 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
79 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { 89 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
80 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); 90 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
81 send_side_cc->EnablePeriodicAlrProbing(true); 91 send_side_cc->EnablePeriodicAlrProbing(true);
82 bandwidth_observer_.reset( 92 bandwidth_observer_.reset(
83 send_side_cc->GetBitrateController()->CreateRtcpBandwidthObserver()); 93 send_side_cc->GetBitrateController()->CreateRtcpBandwidthObserver());
84 } else { 94 } else {
85 RTC_NOTREACHED() << "Registering unsupported RTP extension."; 95 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
86 } 96 }
87 } 97 }
88 channel_proxy_->RegisterSenderCongestionControlObjects( 98 channel_proxy_->RegisterSenderCongestionControlObjects(
89 send_side_cc->pacer(), send_side_cc, packet_router, 99 send_side_cc->pacer(), send_side_cc, packet_router,
90 bandwidth_observer_.get()); 100 bandwidth_observer_.get());
91 if (!SetupSendCodec()) { 101 if (!SetupSendCodec()) {
92 LOG(LS_ERROR) << "Failed to set up send codec state."; 102 LOG(LS_ERROR) << "Failed to set up send codec state.";
93 } 103 }
104
105 pacer_thread_checker_.DetachFromThread();
94 } 106 }
95 107
96 AudioSendStream::~AudioSendStream() { 108 AudioSendStream::~AudioSendStream() {
97 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 109 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
98 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); 110 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
111 send_side_cc_->DeRegisterPacketFeedbackObserver(this);
99 channel_proxy_->DeRegisterExternalTransport(); 112 channel_proxy_->DeRegisterExternalTransport();
100 channel_proxy_->ResetCongestionControlObjects(); 113 channel_proxy_->ResetCongestionControlObjects();
101 channel_proxy_->SetRtcEventLog(nullptr); 114 channel_proxy_->SetRtcEventLog(nullptr);
102 channel_proxy_->SetRtcpRttStats(nullptr); 115 channel_proxy_->SetRtcpRttStats(nullptr);
103 } 116 }
104 117
105 void AudioSendStream::Start() { 118 void AudioSendStream::Start() {
106 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 119 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
107 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { 120 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) {
108 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); 121 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps);
109 rtc::Event thread_sync_event(false /* manual_reset */, false); 122 rtc::Event thread_sync_event(false /* manual_reset */, false);
110 worker_queue_->PostTask([this, &thread_sync_event] { 123 worker_queue_->PostTask([this, &thread_sync_event] {
111 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, 124 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps,
112 config_.max_bitrate_bps, 0, true); 125 config_.max_bitrate_bps, 0, true);
113 thread_sync_event.Set(); 126 thread_sync_event.Set();
114 }); 127 });
115 thread_sync_event.Wait(rtc::Event::kForever); 128 thread_sync_event.Wait(rtc::Event::kForever);
116 } 129 }
117 130
118 ScopedVoEInterface<VoEBase> base(voice_engine()); 131 ScopedVoEInterface<VoEBase> base(voice_engine());
119 int error = base->StartSend(config_.voe_channel_id); 132 int error = base->StartSend(config_.voe_channel_id);
120 if (error != 0) { 133 if (error != 0) {
121 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; 134 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error;
122 } 135 }
123 } 136 }
124 137
125 void AudioSendStream::Stop() { 138 void AudioSendStream::Stop() {
126 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 139 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
127 rtc::Event thread_sync_event(false /* manual_reset */, false); 140 rtc::Event thread_sync_event(false /* manual_reset */, false);
128 worker_queue_->PostTask([this, &thread_sync_event] { 141 worker_queue_->PostTask([this, &thread_sync_event] {
129 bitrate_allocator_->RemoveObserver(this); 142 bitrate_allocator_->RemoveObserver(this);
130 thread_sync_event.Set(); 143 thread_sync_event.Set();
131 }); 144 });
132 thread_sync_event.Wait(rtc::Event::kForever); 145 thread_sync_event.Wait(rtc::Event::kForever);
133 146
134 ScopedVoEInterface<VoEBase> base(voice_engine()); 147 ScopedVoEInterface<VoEBase> base(voice_engine());
135 int error = base->StopSend(config_.voe_channel_id); 148 int error = base->StopSend(config_.voe_channel_id);
136 if (error != 0) { 149 if (error != 0) {
137 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; 150 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error;
138 } 151 }
139 } 152 }
140 153
141 bool AudioSendStream::SendTelephoneEvent(int payload_type, 154 bool AudioSendStream::SendTelephoneEvent(int payload_type,
142 int payload_frequency, int event, 155 int payload_frequency, int event,
143 int duration_ms) { 156 int duration_ms) {
144 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 157 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
145 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type, 158 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type,
146 payload_frequency) && 159 payload_frequency) &&
147 channel_proxy_->SendTelephoneEventOutband(event, duration_ms); 160 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
148 } 161 }
149 162
150 void AudioSendStream::SetMuted(bool muted) { 163 void AudioSendStream::SetMuted(bool muted) {
151 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 164 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
152 channel_proxy_->SetInputMute(muted); 165 channel_proxy_->SetInputMute(muted);
153 } 166 }
154 167
155 webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const { 168 webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
156 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 169 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
157 webrtc::AudioSendStream::Stats stats; 170 webrtc::AudioSendStream::Stats stats;
158 stats.local_ssrc = config_.rtp.ssrc; 171 stats.local_ssrc = config_.rtp.ssrc;
159 172
160 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics(); 173 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
161 stats.bytes_sent = call_stats.bytesSent; 174 stats.bytes_sent = call_stats.bytesSent;
162 stats.packets_sent = call_stats.packetsSent; 175 stats.packets_sent = call_stats.packetsSent;
163 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine 176 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
164 // returns 0 to indicate an error value. 177 // returns 0 to indicate an error value.
165 if (call_stats.rttMs > 0) { 178 if (call_stats.rttMs > 0) {
166 stats.rtt_ms = call_stats.rttMs; 179 stats.rtt_ms = call_stats.rttMs;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 audio_processing_stats.residual_echo_likelihood_recent_max; 223 audio_processing_stats.residual_echo_likelihood_recent_max;
211 224
212 internal::AudioState* audio_state = 225 internal::AudioState* audio_state =
213 static_cast<internal::AudioState*>(audio_state_.get()); 226 static_cast<internal::AudioState*>(audio_state_.get());
214 stats.typing_noise_detected = audio_state->typing_noise_detected(); 227 stats.typing_noise_detected = audio_state->typing_noise_detected();
215 228
216 return stats; 229 return stats;
217 } 230 }
218 231
219 void AudioSendStream::SignalNetworkState(NetworkState state) { 232 void AudioSendStream::SignalNetworkState(NetworkState state) {
220 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 233 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
221 } 234 }
222 235
223 bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { 236 bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
224 // TODO(solenberg): Tests call this function on a network thread, libjingle 237 // TODO(solenberg): Tests call this function on a network thread, libjingle
225 // calls on the worker thread. We should move towards always using a network 238 // calls on the worker thread. We should move towards always using a network
226 // thread. Then this check can be enabled. 239 // thread. Then this check can be enabled.
227 // RTC_DCHECK(!thread_checker_.CalledOnValidThread()); 240 // RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
228 return channel_proxy_->ReceivedRTCPPacket(packet, length); 241 return channel_proxy_->ReceivedRTCPPacket(packet, length);
229 } 242 }
230 243
231 uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, 244 uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
232 uint8_t fraction_loss, 245 uint8_t fraction_loss,
233 int64_t rtt, 246 int64_t rtt,
234 int64_t probing_interval_ms) { 247 int64_t probing_interval_ms) {
235 RTC_DCHECK_GE(bitrate_bps, 248 RTC_DCHECK_GE(bitrate_bps,
236 static_cast<uint32_t>(config_.min_bitrate_bps)); 249 static_cast<uint32_t>(config_.min_bitrate_bps));
237 // The bitrate allocator might allocate an higher than max configured bitrate 250 // The bitrate allocator might allocate an higher than max configured bitrate
238 // if there is room, to allow for, as example, extra FEC. Ignore that for now. 251 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
239 const uint32_t max_bitrate_bps = config_.max_bitrate_bps; 252 const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
240 if (bitrate_bps > max_bitrate_bps) 253 if (bitrate_bps > max_bitrate_bps)
241 bitrate_bps = max_bitrate_bps; 254 bitrate_bps = max_bitrate_bps;
242 255
243 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms); 256 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms);
244 257
245 // The amount of audio protection is not exposed by the encoder, hence 258 // The amount of audio protection is not exposed by the encoder, hence
246 // always returning 0. 259 // always returning 0.
247 return 0; 260 return 0;
248 } 261 }
249 262
263 void AudioSendStream::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) {
264 RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
265 // Only packets that belong to this stream are of interest.
266 if (ssrc == config_.rtp.ssrc) {
267 rtc::CritScope lock(&packet_loss_tracker_cs_);
268 // TODO(elad.alon): This function call could potentially reset the window,
269 // setting both PLR and RPLR to unknown. Consider (during upcoming
270 // refactoring) passing an indication of such an event.
271 packet_loss_tracker_.OnPacketAdded(seq_num, rtc::TimeMillis());
272 }
273 }
274
275 void AudioSendStream::OnPacketFeedbackVector(
276 const std::vector<PacketFeedback>& packet_feedback_vector) {
277 // TODO(elad.alon): This fails in UT; fix and uncomment.
278 // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
279 rtc::Optional<float> plr;
280 {
281 rtc::CritScope lock(&packet_loss_tracker_cs_);
282 packet_loss_tracker_.OnPacketFeedbackVector(packet_feedback_vector);
283 plr = packet_loss_tracker_.GetPacketLossRate();
284 }
285 // TODO(elad.alon): If PLR goes back to unknown, no indication is given that
286 // the previously sent value is no longer relevant. This will be taken care
287 // of with some refactoring which is now being done.
288 if (plr) {
289 channel_proxy_->OnTwccBasedUplinkPacketLossRate(*plr);
290 }
291 }
292
250 const webrtc::AudioSendStream::Config& AudioSendStream::config() const { 293 const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
251 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 294 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
252 return config_; 295 return config_;
253 } 296 }
254 297
255 void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) { 298 void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) {
256 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 299 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
257 send_side_cc_->SetTransportOverhead(transport_overhead_per_packet); 300 send_side_cc_->SetTransportOverhead(transport_overhead_per_packet);
258 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet); 301 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet);
259 } 302 }
260 303
261 VoiceEngine* AudioSendStream::voice_engine() const { 304 VoiceEngine* AudioSendStream::voice_engine() const {
262 internal::AudioState* audio_state = 305 internal::AudioState* audio_state =
263 static_cast<internal::AudioState*>(audio_state_.get()); 306 static_cast<internal::AudioState*>(audio_state_.get());
264 VoiceEngine* voice_engine = audio_state->voice_engine(); 307 VoiceEngine* voice_engine = audio_state->voice_engine();
265 RTC_DCHECK(voice_engine); 308 RTC_DCHECK(voice_engine);
266 return voice_engine; 309 return voice_engine;
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 LOG(LS_WARNING) << "SetVADStatus() failed."; 416 LOG(LS_WARNING) << "SetVADStatus() failed.";
374 return false; 417 return false;
375 } 418 }
376 } 419 }
377 } 420 }
378 return true; 421 return true;
379 } 422 }
380 423
381 } // namespace internal 424 } // namespace internal
382 } // namespace webrtc 425 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698