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

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

Issue 2705093002: Injectable audio encoders: WebRtcVoiceEngine and company (Closed)
Patch Set: Channel::GetSendCodec asks both its acm and its codec manager. Created 3 years, 7 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 #include <utility>
15 #include <vector>
14 16
15 #include "webrtc/audio/audio_state.h" 17 #include "webrtc/audio/audio_state.h"
16 #include "webrtc/audio/conversion.h" 18 #include "webrtc/audio/conversion.h"
17 #include "webrtc/audio/scoped_voe_interface.h" 19 #include "webrtc/audio/scoped_voe_interface.h"
18 #include "webrtc/base/checks.h" 20 #include "webrtc/base/checks.h"
19 #include "webrtc/base/event.h" 21 #include "webrtc/base/event.h"
22 #include "webrtc/base/function_view.h"
20 #include "webrtc/base/logging.h" 23 #include "webrtc/base/logging.h"
21 #include "webrtc/base/task_queue.h" 24 #include "webrtc/base/task_queue.h"
22 #include "webrtc/base/timeutils.h" 25 #include "webrtc/base/timeutils.h"
23 #include "webrtc/call/rtp_transport_controller_send_interface.h" 26 #include "webrtc/call/rtp_transport_controller_send_interface.h"
27 #include "webrtc/modules/audio_coding/codecs/cng/audio_encoder_cng.h"
24 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" 28 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
25 #include "webrtc/modules/congestion_controller/include/send_side_congestion_cont roller.h" 29 #include "webrtc/modules/congestion_controller/include/send_side_congestion_cont roller.h"
26 #include "webrtc/modules/pacing/paced_sender.h" 30 #include "webrtc/modules/pacing/paced_sender.h"
27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" 31 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
28 #include "webrtc/voice_engine/channel_proxy.h" 32 #include "webrtc/voice_engine/channel_proxy.h"
29 #include "webrtc/voice_engine/include/voe_base.h" 33 #include "webrtc/voice_engine/include/voe_base.h"
30 #include "webrtc/voice_engine/transmit_mixer.h" 34 #include "webrtc/voice_engine/transmit_mixer.h"
31 #include "webrtc/voice_engine/voice_engine_impl.h" 35 #include "webrtc/voice_engine/voice_engine_impl.h"
32 36
33 namespace webrtc { 37 namespace webrtc {
34 38
35 namespace {
36
37 constexpr char kOpusCodecName[] = "opus";
38
39 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
40 return (STR_CASE_CMP(codec.plname, ref_name) == 0);
41 }
42 } // namespace
43
44 namespace internal { 39 namespace internal {
45 // TODO(elad.alon): Subsequent CL will make these values experiment-dependent. 40 // TODO(elad.alon): Subsequent CL will make these values experiment-dependent.
46 constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000; 41 constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000;
47 constexpr size_t kPacketLossRateMinNumAckedPackets = 50; 42 constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
48 constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40; 43 constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
49 44
45 namespace {
46 void CallEncoder(const std::unique_ptr<voe::ChannelProxy>& channel_proxy,
47 rtc::FunctionView<void(AudioEncoder*)> lambda) {
48 channel_proxy->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
49 RTC_DCHECK(encoder_ptr);
50 lambda(encoder_ptr->get());
51 });
52 }
53 } // namespace
54
50 AudioSendStream::AudioSendStream( 55 AudioSendStream::AudioSendStream(
51 const webrtc::AudioSendStream::Config& config, 56 const webrtc::AudioSendStream::Config& config,
52 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 57 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
53 rtc::TaskQueue* worker_queue, 58 rtc::TaskQueue* worker_queue,
54 RtpTransportControllerSendInterface* transport, 59 RtpTransportControllerSendInterface* transport,
55 BitrateAllocator* bitrate_allocator, 60 BitrateAllocator* bitrate_allocator,
56 RtcEventLog* event_log, 61 RtcEventLog* event_log,
57 RtcpRttStats* rtcp_rtt_stats) 62 RtcpRttStats* rtcp_rtt_stats)
58 : worker_queue_(worker_queue), 63 : worker_queue_(worker_queue),
59 config_(config), 64 config_(Config(nullptr)),
60 audio_state_(audio_state), 65 audio_state_(audio_state),
66 event_log_(event_log),
61 bitrate_allocator_(bitrate_allocator), 67 bitrate_allocator_(bitrate_allocator),
62 transport_(transport), 68 transport_(transport),
63 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs, 69 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs,
64 kPacketLossRateMinNumAckedPackets, 70 kPacketLossRateMinNumAckedPackets,
65 kRecoverablePacketLossRateMinNumAckedPairs) { 71 kRecoverablePacketLossRateMinNumAckedPairs) {
66 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); 72 LOG(LS_INFO) << "AudioSendStream: " << config.ToString();
67 RTC_DCHECK_NE(config_.voe_channel_id, -1); 73 RTC_DCHECK_NE(config.voe_channel_id, -1);
68 RTC_DCHECK(audio_state_.get()); 74 RTC_DCHECK(audio_state_.get());
69 RTC_DCHECK(transport); 75 RTC_DCHECK(transport);
70 RTC_DCHECK(transport->send_side_cc()); 76 RTC_DCHECK(transport->send_side_cc());
71 77
72 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); 78 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
73 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); 79 channel_proxy_ = voe_impl->GetChannelProxy(config.voe_channel_id);
74 channel_proxy_->SetRtcEventLog(event_log); 80 channel_proxy_->SetRtcEventLog(event_log_);
75 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); 81 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
76 channel_proxy_->SetRTCPStatus(true); 82 channel_proxy_->SetRTCPStatus(true);
77 channel_proxy_->SetLocalSSRC(config.rtp.ssrc);
78 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name);
79 // TODO(solenberg): Config NACK history window (which is a packet count),
80 // using the actual packet size for the configured codec.
81 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
82 config_.rtp.nack.rtp_history_ms / 20);
83
84 channel_proxy_->RegisterExternalTransport(config.send_transport);
85 transport_->send_side_cc()->RegisterPacketFeedbackObserver(this); 83 transport_->send_side_cc()->RegisterPacketFeedbackObserver(this);
86 84
87 for (const auto& extension : config.rtp.extensions) { 85 ConfigureStream(this, config, true);
88 if (extension.uri == RtpExtension::kAudioLevelUri) {
89 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
90 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
91 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
92 transport->send_side_cc()->EnablePeriodicAlrProbing(true);
93 bandwidth_observer_.reset(transport->send_side_cc()
94 ->GetBitrateController()
95 ->CreateRtcpBandwidthObserver());
96 } else {
97 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
98 }
99 }
100 channel_proxy_->RegisterSenderCongestionControlObjects(
101 transport, bandwidth_observer_.get());
102 if (!SetupSendCodec()) {
103 LOG(LS_ERROR) << "Failed to set up send codec state.";
104 }
105 86
106 pacer_thread_checker_.DetachFromThread(); 87 pacer_thread_checker_.DetachFromThread();
107 } 88 }
108 89
109 AudioSendStream::~AudioSendStream() { 90 AudioSendStream::~AudioSendStream() {
110 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 91 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
111 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); 92 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
112 transport_->send_side_cc()->DeRegisterPacketFeedbackObserver(this); 93 transport_->send_side_cc()->DeRegisterPacketFeedbackObserver(this);
113 channel_proxy_->DeRegisterExternalTransport(); 94 channel_proxy_->DeRegisterExternalTransport();
114 channel_proxy_->ResetSenderCongestionControlObjects(); 95 channel_proxy_->ResetSenderCongestionControlObjects();
115 channel_proxy_->SetRtcEventLog(nullptr); 96 channel_proxy_->SetRtcEventLog(nullptr);
116 channel_proxy_->SetRtcpRttStats(nullptr); 97 channel_proxy_->SetRtcpRttStats(nullptr);
117 } 98 }
118 99
100 void AudioSendStream::Reconfigure(
101 const webrtc::AudioSendStream::Config& new_config) {
102 ConfigureStream(this, new_config, false);
103 }
104
105 void AudioSendStream::ConfigureStream(
106 webrtc::internal::AudioSendStream* stream,
107 const webrtc::AudioSendStream::Config& new_config,
108 bool first_time) {
109 LOG(LS_INFO) << "AudioSendStream::Configuring: " << new_config.ToString();
110 const auto& channel_proxy = stream->channel_proxy_;
111 const auto& old_config = stream->config_;
112
113 if (first_time || old_config.rtp.ssrc != new_config.rtp.ssrc) {
114 channel_proxy->SetLocalSSRC(new_config.rtp.ssrc);
115 }
116 if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) {
117 channel_proxy->SetRTCP_CNAME(new_config.rtp.c_name);
118 }
119 // TODO(solenberg): Config NACK history window (which is a packet count),
120 // using the actual packet size for the configured codec.
121 if (first_time || old_config.rtp.nack.rtp_history_ms !=
122 new_config.rtp.nack.rtp_history_ms) {
123 channel_proxy->SetNACKStatus(new_config.rtp.nack.rtp_history_ms != 0,
124 new_config.rtp.nack.rtp_history_ms / 20);
125 }
126
127 if (first_time ||
128 new_config.send_transport != old_config.send_transport) {
129 if (old_config.send_transport) {
130 channel_proxy->DeRegisterExternalTransport();
131 }
132
133 channel_proxy->RegisterExternalTransport(new_config.send_transport);
134 }
135
136 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
137 // reserved for padding and MUST NOT be used as a local identifier.
138 // So it should be safe to use 0 here to indicate "not configured".
139 struct ExtensionIds {
140 int audio_level = 0;
141 int transport_sequence_number = 0;
142 };
143
144 auto find_extension_ids = [](const std::vector<RtpExtension>& extensions) {
145 ExtensionIds ids;
146 for (const auto& extension : extensions) {
147 if (extension.uri == RtpExtension::kAudioLevelUri) {
148 ids.audio_level = extension.id;
149 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
150 ids.transport_sequence_number = extension.id;
151 }
152 }
153 return ids;
154 };
155
156 const ExtensionIds old_ids = find_extension_ids(old_config.rtp.extensions);
157 const ExtensionIds new_ids = find_extension_ids(new_config.rtp.extensions);
158 // Audio level indication
159 if (first_time || new_ids.audio_level != old_ids.audio_level) {
160 channel_proxy->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
161 new_ids.audio_level);
162 }
163 // Transport sequence number
164 if (first_time ||
165 new_ids.transport_sequence_number != old_ids.transport_sequence_number) {
166 if (old_ids.transport_sequence_number) {
167 channel_proxy->ResetSenderCongestionControlObjects();
168 stream->bandwidth_observer_.reset();
169 }
170
171 if (new_ids.transport_sequence_number != 0) {
172 channel_proxy->EnableSendTransportSequenceNumber(
173 new_ids.transport_sequence_number);
174 stream->transport_->send_side_cc()->EnablePeriodicAlrProbing(true);
175 stream->bandwidth_observer_.reset(stream->transport_->send_side_cc()
176 ->GetBitrateController()
177 ->CreateRtcpBandwidthObserver());
178 }
179
180 channel_proxy->RegisterSenderCongestionControlObjects(
181 stream->transport_, stream->bandwidth_observer_.get());
182 }
183
184 if (!ReconfigureSendCodec(stream, new_config)) {
185 LOG(LS_ERROR) << "Failed to set up send codec state.";
186 }
187
188 ReconfigureBitrateObserver(stream, new_config);
189 stream->config_ = new_config;
190 }
191
119 void AudioSendStream::Start() { 192 void AudioSendStream::Start() {
120 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
121 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { 194 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) {
122 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); 195 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps);
123 rtc::Event thread_sync_event(false /* manual_reset */, false);
124 worker_queue_->PostTask([this, &thread_sync_event] {
125 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps,
126 config_.max_bitrate_bps, 0, true);
127 thread_sync_event.Set();
128 });
129 thread_sync_event.Wait(rtc::Event::kForever);
130 } 196 }
131 197
132 ScopedVoEInterface<VoEBase> base(voice_engine()); 198 ScopedVoEInterface<VoEBase> base(voice_engine());
133 int error = base->StartSend(config_.voe_channel_id); 199 int error = base->StartSend(config_.voe_channel_id);
134 if (error != 0) { 200 if (error != 0) {
135 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; 201 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error;
136 } 202 }
137 } 203 }
138 204
139 void AudioSendStream::Stop() { 205 void AudioSendStream::Stop() {
140 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 206 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
141 rtc::Event thread_sync_event(false /* manual_reset */, false); 207 RemoveBitrateObserver();
142 worker_queue_->PostTask([this, &thread_sync_event] {
143 bitrate_allocator_->RemoveObserver(this);
144 thread_sync_event.Set();
145 });
146 thread_sync_event.Wait(rtc::Event::kForever);
147 208
148 ScopedVoEInterface<VoEBase> base(voice_engine()); 209 ScopedVoEInterface<VoEBase> base(voice_engine());
149 int error = base->StopSend(config_.voe_channel_id); 210 int error = base->StopSend(config_.voe_channel_id);
150 if (error != 0) { 211 if (error != 0) {
151 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; 212 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error;
152 } 213 }
153 } 214 }
154 215
155 bool AudioSendStream::SendTelephoneEvent(int payload_type, 216 bool AudioSendStream::SendTelephoneEvent(int payload_type,
156 int payload_frequency, int event, 217 int payload_frequency, int event,
(...skipping 19 matching lines...) Expand all
176 stats.packets_sent = call_stats.packetsSent; 237 stats.packets_sent = call_stats.packetsSent;
177 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine 238 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
178 // returns 0 to indicate an error value. 239 // returns 0 to indicate an error value.
179 if (call_stats.rttMs > 0) { 240 if (call_stats.rttMs > 0) {
180 stats.rtt_ms = call_stats.rttMs; 241 stats.rtt_ms = call_stats.rttMs;
181 } 242 }
182 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable 243 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
183 // implementation. 244 // implementation.
184 stats.aec_quality_min = -1; 245 stats.aec_quality_min = -1;
185 246
186 webrtc::CodecInst codec_inst = {0}; 247 if (config_.send_codec_spec) {
187 if (channel_proxy_->GetSendCodec(&codec_inst)) { 248 const auto& spec = *config_.send_codec_spec;
188 RTC_DCHECK_NE(codec_inst.pltype, -1); 249 stats.codec_name = spec.format.name;
189 stats.codec_name = codec_inst.plname; 250 stats.codec_payload_type = rtc::Optional<int>(spec.payload_type);
190 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
191 251
192 // Get data from the last remote RTCP report. 252 // Get data from the last remote RTCP report.
193 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) { 253 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
194 // Lookup report for send ssrc only. 254 // Lookup report for send ssrc only.
195 if (block.source_SSRC == stats.local_ssrc) { 255 if (block.source_SSRC == stats.local_ssrc) {
196 stats.packets_lost = block.cumulative_num_packets_lost; 256 stats.packets_lost = block.cumulative_num_packets_lost;
197 stats.fraction_lost = Q8ToFloat(block.fraction_lost); 257 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
198 stats.ext_seqnum = block.extended_highest_sequence_number; 258 stats.ext_seqnum = block.extended_highest_sequence_number;
199 // Convert samples to milliseconds. 259 // Convert timestamps to milliseconds.
200 if (codec_inst.plfreq / 1000 > 0) { 260 if (spec.format.clockrate_hz / 1000 > 0) {
201 stats.jitter_ms = 261 stats.jitter_ms =
202 block.interarrival_jitter / (codec_inst.plfreq / 1000); 262 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
203 } 263 }
204 break; 264 break;
205 } 265 }
206 } 266 }
207 } 267 }
208 268
209 ScopedVoEInterface<VoEBase> base(voice_engine()); 269 ScopedVoEInterface<VoEBase> base(voice_engine());
210 RTC_DCHECK(base->transmit_mixer()); 270 RTC_DCHECK(base->transmit_mixer());
211 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange(); 271 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange();
212 RTC_DCHECK_LE(0, stats.audio_level); 272 RTC_DCHECK_LE(0, stats.audio_level);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 377
318 VoiceEngine* AudioSendStream::voice_engine() const { 378 VoiceEngine* AudioSendStream::voice_engine() const {
319 internal::AudioState* audio_state = 379 internal::AudioState* audio_state =
320 static_cast<internal::AudioState*>(audio_state_.get()); 380 static_cast<internal::AudioState*>(audio_state_.get());
321 VoiceEngine* voice_engine = audio_state->voice_engine(); 381 VoiceEngine* voice_engine = audio_state->voice_engine();
322 RTC_DCHECK(voice_engine); 382 RTC_DCHECK(voice_engine);
323 return voice_engine; 383 return voice_engine;
324 } 384 }
325 385
326 // Apply current codec settings to a single voe::Channel used for sending. 386 // Apply current codec settings to a single voe::Channel used for sending.
327 bool AudioSendStream::SetupSendCodec() { 387 bool AudioSendStream::SetupSendCodec(AudioSendStream* stream,
328 // Disable VAD and FEC unless we know the other side wants them. 388 const Config& new_config) {
329 channel_proxy_->SetVADStatus(false); 389 RTC_DCHECK(new_config.send_codec_spec);
330 channel_proxy_->SetCodecFECStatus(false); 390 const auto& spec = *new_config.send_codec_spec;
391 std::unique_ptr<AudioEncoder> encoder =
392 new_config.encoder_factory->MakeAudioEncoder(spec.payload_type,
393 spec.format);
331 394
332 // We disable audio network adaptor here. This will on one hand make sure that 395 if (!encoder) {
333 // audio network adaptor is disabled by default, and on the other allow audio 396 LOG(LS_ERROR) << "Unable to create encoder for " << spec.format;
334 // network adaptor to be reconfigured, since SetReceiverFrameLengthRange can 397 return false;
335 // be only called when audio network adaptor is disabled. 398 }
336 channel_proxy_->DisableAudioNetworkAdaptor(); 399 // If a bitrate has been specified for the codec, use it over the
400 // codec's default.
401 if (spec.target_bitrate_bps) {
402 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
403 }
337 404
338 const auto& send_codec_spec = config_.send_codec_spec; 405 // Enable ANA if configured (currently only used by Opus).
339 406 if (new_config.audio_network_adaptor_config) {
340 // We set the codec first, since the below extra configuration is only applied 407 if (encoder->EnableAudioNetworkAdaptor(
341 // to the "current" codec. 408 *new_config.audio_network_adaptor_config, stream->event_log_)) {
342 409 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
343 // If codec is already configured, we do not it again. 410 << new_config.rtp.ssrc;
344 // TODO(minyue): check if this check is really needed, or can we move it into 411 } else {
345 // |codec->SetSendCodec|. 412 RTC_NOTREACHED();
346 webrtc::CodecInst current_codec = {0};
347 if (!channel_proxy_->GetSendCodec(&current_codec) ||
348 (send_codec_spec.codec_inst != current_codec)) {
349 if (!channel_proxy_->SetSendCodec(send_codec_spec.codec_inst)) {
350 LOG(LS_WARNING) << "SetSendCodec() failed.";
351 return false;
352 } 413 }
353 } 414 }
354 415
355 // Codec internal FEC. Treat any failure as fatal internal error. 416 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
356 if (send_codec_spec.enable_codec_fec) { 417 if (spec.cng_payload_type) {
357 if (!channel_proxy_->SetCodecFECStatus(true)) { 418 AudioEncoderCng::Config cng_config;
358 LOG(LS_WARNING) << "SetCodecFECStatus() failed."; 419 cng_config.num_channels = encoder->NumChannels();
359 return false; 420 cng_config.payload_type = *spec.cng_payload_type;
360 } 421 cng_config.speech_encoder = std::move(encoder);
422 cng_config.vad_mode = Vad::kVadNormal;
423 encoder.reset(new AudioEncoderCng(std::move(cng_config)));
361 } 424 }
362 425
363 // DTX and maxplaybackrate are only set if current codec is Opus. 426 stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type,
364 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) { 427 std::move(encoder));
365 if (!channel_proxy_->SetOpusDtx(send_codec_spec.enable_opus_dtx)) { 428 return true;
366 LOG(LS_WARNING) << "SetOpusDtx() failed."; 429 }
367 return false;
368 }
369 430
370 // If opus_max_playback_rate <= 0, the default maximum playback rate 431 bool AudioSendStream::ReconfigureSendCodec(AudioSendStream* stream,
371 // (48 kHz) will be used. 432 const Config& new_config) {
372 if (send_codec_spec.opus_max_playback_rate > 0) { 433 const auto& old_config = stream->config_;
373 if (!channel_proxy_->SetOpusMaxPlaybackRate( 434 if (new_config.send_codec_spec == old_config.send_codec_spec) {
374 send_codec_spec.opus_max_playback_rate)) { 435 return true;
375 LOG(LS_WARNING) << "SetOpusMaxPlaybackRate() failed.";
376 return false;
377 }
378 }
379
380 if (config_.audio_network_adaptor_config) {
381 // Audio network adaptor is only allowed for Opus currently.
382 // |SetReceiverFrameLengthRange| needs to be called before
383 // |EnableAudioNetworkAdaptor|.
384 channel_proxy_->SetReceiverFrameLengthRange(send_codec_spec.min_ptime_ms,
385 send_codec_spec.max_ptime_ms);
386 channel_proxy_->EnableAudioNetworkAdaptor(
387 *config_.audio_network_adaptor_config);
388 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
389 << config_.rtp.ssrc;
390 }
391 } 436 }
392 437
393 // Set the CN payloadtype and the VAD status. 438 // If we have no encoder, or the format or payload type's changed, create a
394 if (send_codec_spec.cng_payload_type != -1) { 439 // new encoder.
395 // The CN payload type for 8000 Hz clockrate is fixed at 13. 440 if (!old_config.send_codec_spec ||
396 if (send_codec_spec.cng_plfreq != 8000) { 441 new_config.send_codec_spec->format !=
397 webrtc::PayloadFrequencies cn_freq; 442 old_config.send_codec_spec->format ||
398 switch (send_codec_spec.cng_plfreq) { 443 new_config.send_codec_spec->payload_type !=
399 case 16000: 444 old_config.send_codec_spec->payload_type) {
400 cn_freq = webrtc::kFreq16000Hz; 445 return SetupSendCodec(stream, new_config);
401 break; 446 }
402 case 32000: 447
403 cn_freq = webrtc::kFreq32000Hz; 448 // Should never move a stream from fully configured to unconfigured.
404 break; 449 RTC_CHECK(new_config.send_codec_spec);
405 default: 450
406 RTC_NOTREACHED(); 451 const rtc::Optional<int>& new_target_bitrate_bps =
407 return false; 452 new_config.send_codec_spec->target_bitrate_bps;
453 // If a bitrate has been specified for the codec, use it over the
454 // codec's default.
455 if (new_target_bitrate_bps &&
456 new_target_bitrate_bps !=
457 old_config.send_codec_spec->target_bitrate_bps) {
458 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
459 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
460 });
461 }
462
463 ReconfigureANA(stream, new_config);
464 ReconfigureCNG(stream, new_config);
465
466 return true;
467 }
468
469 void AudioSendStream::ReconfigureANA(AudioSendStream* stream,
470 const Config& new_config) {
471 if (new_config.audio_network_adaptor_config ==
472 stream->config_.audio_network_adaptor_config) {
473 return;
474 }
475 if (new_config.audio_network_adaptor_config) {
476 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
477 if (encoder->EnableAudioNetworkAdaptor(
478 *new_config.audio_network_adaptor_config, stream->event_log_)) {
479 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
480 << new_config.rtp.ssrc;
481 } else {
482 RTC_NOTREACHED();
408 } 483 }
409 if (!channel_proxy_->SetSendCNPayloadType( 484 });
410 send_codec_spec.cng_payload_type, cn_freq)) { 485 } else {
411 LOG(LS_WARNING) << "SetSendCNPayloadType() failed."; 486 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
412 // TODO(ajm): This failure condition will be removed from VoE. 487 encoder->DisableAudioNetworkAdaptor();
413 // Restore the return here when we update to a new enough webrtc. 488 });
414 // 489 LOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
415 // Not returning false because the SetSendCNPayloadType will fail if 490 << new_config.rtp.ssrc;
416 // the channel is already sending. 491 }
417 // This can happen if the remote description is applied twice, for 492 }
418 // example in the case of ROAP on top of JSEP, where both side will
419 // send the offer.
420 }
421 }
422 493
423 // Only turn on VAD if we have a CN payload type that matches the 494 void AudioSendStream::ReconfigureCNG(AudioSendStream* stream,
424 // clockrate for the codec we are going to use. 495 const Config& new_config) {
425 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq && 496 if (new_config.send_codec_spec->cng_payload_type ==
426 send_codec_spec.codec_inst.channels == 1) { 497 stream->config_.send_codec_spec->cng_payload_type) {
427 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the 498 return;
428 // interaction between VAD and Opus FEC.
429 if (!channel_proxy_->SetVADStatus(true)) {
430 LOG(LS_WARNING) << "SetVADStatus() failed.";
431 return false;
432 }
433 }
434 } 499 }
435 return true; 500
501 // Wrap or unwrap the encoder in an AudioEncoderCNG.
502 stream->channel_proxy_->ModifyEncoder(
503 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
504 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
505 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
506 if (!sub_encoders.empty()) {
507 // Replace enc with its sub encoder. We need to put the sub
508 // encoder in a temporary first, since otherwise the old value
509 // of enc would be destroyed before the new value got assigned,
510 // which would be bad since the new value is a part of the old
511 // value.
512 auto tmp = std::move(sub_encoders[0]);
513 old_encoder = std::move(tmp);
514 }
515 if (new_config.send_codec_spec->cng_payload_type) {
516 AudioEncoderCng::Config config;
517 config.speech_encoder = std::move(old_encoder);
518 config.num_channels = config.speech_encoder->NumChannels();
519 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
520 config.vad_mode = Vad::kVadNormal;
521 encoder_ptr->reset(new AudioEncoderCng(std::move(config)));
522 } else {
523 *encoder_ptr = std::move(old_encoder);
524 }
525 });
526 }
527
528 void AudioSendStream::ReconfigureBitrateObserver(
529 AudioSendStream* stream,
530 const webrtc::AudioSendStream::Config& new_config) {
531 // Since the Config's default is for both of these to be -1, this test will
532 // allow us to configure the bitrate observer if the new config has bitrate
533 // limits set, but would only have us call RemoveBitrateObserver if we were
534 // previously configured with bitrate limits.
535 if (stream->config_.min_bitrate_bps == new_config.min_bitrate_bps &&
536 stream->config_.max_bitrate_bps == new_config.max_bitrate_bps) {
537 return;
538 }
539
540 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1) {
541 stream->ConfigureBitrateObserver(new_config.min_bitrate_bps,
542 new_config.max_bitrate_bps);
543 } else {
544 stream->RemoveBitrateObserver();
545 }
546 }
547
548 void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps,
549 int max_bitrate_bps) {
550 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
551 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps);
552 rtc::Event thread_sync_event(false /* manual_reset */, false);
553 worker_queue_->PostTask([&] {
554 // We may get a callback immediately as the observer is registered, so make
555 // sure the bitrate limits in config_ are up-to-date.
556 config_.min_bitrate_bps = min_bitrate_bps;
557 config_.max_bitrate_bps = max_bitrate_bps;
558 bitrate_allocator_->AddObserver(this, min_bitrate_bps, max_bitrate_bps, 0,
559 true);
560 thread_sync_event.Set();
561 });
562 thread_sync_event.Wait(rtc::Event::kForever);
563 }
564
565 void AudioSendStream::RemoveBitrateObserver() {
566 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
567 rtc::Event thread_sync_event(false /* manual_reset */, false);
568 worker_queue_->PostTask([this, &thread_sync_event] {
569 bitrate_allocator_->RemoveObserver(this);
570 thread_sync_event.Set();
571 });
572 thread_sync_event.Wait(rtc::Event::kForever);
436 } 573 }
437 574
438 } // namespace internal 575 } // namespace internal
439 } // namespace webrtc 576 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698