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> | |
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.h" | 26 #include "webrtc/call/rtp_transport_controller_send.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 Loading... | |
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 Loading... | |
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 // Explicitly hide config_ here, so we don't accidentally setup a send codec |
the sun
2017/04/24 14:48:49
Don't understand this comment
ossu
2017/04/24 16:20:30
No, it's not applicable anymore as SetupSendCodec
| |
391 // with old parameters. | |
392 const auto& spec = *new_config.send_codec_spec; | |
393 std::unique_ptr<AudioEncoder> encoder = | |
394 new_config.encoder_factory->MakeAudioEncoder(spec.payload_type, | |
395 spec.format); | |
331 | 396 |
332 // We disable audio network adaptor here. This will on one hand make sure that | 397 if (!encoder) { |
333 // audio network adaptor is disabled by default, and on the other allow audio | 398 LOG(LS_ERROR) << "Unable to create encoder for " << spec.format; |
334 // network adaptor to be reconfigured, since SetReceiverFrameLengthRange can | 399 return false; |
335 // be only called when audio network adaptor is disabled. | 400 } |
336 channel_proxy_->DisableAudioNetworkAdaptor(); | 401 // If a bitrate has been specified for the codec, use it over the |
402 // codec's default. | |
403 if (spec.target_bitrate_bps) { | |
404 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps); | |
405 } | |
337 | 406 |
338 const auto& send_codec_spec = config_.send_codec_spec; | 407 // Enable ANA if configured (currently only used by Opus). |
339 | 408 if (new_config.audio_network_adaptor_config) { |
340 // We set the codec first, since the below extra configuration is only applied | 409 if (encoder->EnableAudioNetworkAdaptor( |
341 // to the "current" codec. | 410 *new_config.audio_network_adaptor_config, stream->event_log_)) { |
342 | 411 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
343 // If codec is already configured, we do not it again. | 412 << new_config.rtp.ssrc; |
344 // TODO(minyue): check if this check is really needed, or can we move it into | 413 } else { |
345 // |codec->SetSendCodec|. | 414 RTC_NOTREACHED(); |
346 webrtc::CodecInst current_codec = {0}; | |
347 if (!channel_proxy_->GetSendCodec(¤t_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 } | 415 } |
353 } | 416 } |
354 | 417 |
355 // Codec internal FEC. Treat any failure as fatal internal error. | 418 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled. |
356 if (send_codec_spec.enable_codec_fec) { | 419 if (spec.cng_payload_type) { |
357 if (!channel_proxy_->SetCodecFECStatus(true)) { | 420 AudioEncoderCng::Config cng_config; |
358 LOG(LS_WARNING) << "SetCodecFECStatus() failed."; | 421 cng_config.num_channels = encoder->NumChannels(); |
359 return false; | 422 cng_config.payload_type = *spec.cng_payload_type; |
360 } | 423 cng_config.speech_encoder = std::move(encoder); |
424 cng_config.vad_mode = Vad::kVadNormal; | |
425 encoder.reset(new AudioEncoderCng(std::move(cng_config))); | |
361 } | 426 } |
362 | 427 |
363 // DTX and maxplaybackrate are only set if current codec is Opus. | 428 stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type, |
364 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) { | 429 std::move(encoder)); |
365 if (!channel_proxy_->SetOpusDtx(send_codec_spec.enable_opus_dtx)) { | 430 return true; |
366 LOG(LS_WARNING) << "SetOpusDtx() failed."; | 431 } |
367 return false; | |
368 } | |
369 | 432 |
370 // If opus_max_playback_rate <= 0, the default maximum playback rate | 433 bool AudioSendStream::ReconfigureSendCodec(AudioSendStream* stream, |
371 // (48 kHz) will be used. | 434 const Config& new_config) { |
372 if (send_codec_spec.opus_max_playback_rate > 0) { | 435 const auto& old_config = stream->config_; |
373 if (!channel_proxy_->SetOpusMaxPlaybackRate( | 436 if (new_config.send_codec_spec == old_config.send_codec_spec) { |
374 send_codec_spec.opus_max_playback_rate)) { | 437 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 } | 438 } |
392 | 439 |
393 // Set the CN payloadtype and the VAD status. | 440 // If we have no encoder, or the format or payload type's changed, create a |
394 if (send_codec_spec.cng_payload_type != -1) { | 441 // new encoder. |
395 // The CN payload type for 8000 Hz clockrate is fixed at 13. | 442 if (!old_config.send_codec_spec || |
396 if (send_codec_spec.cng_plfreq != 8000) { | 443 new_config.send_codec_spec->format != |
397 webrtc::PayloadFrequencies cn_freq; | 444 old_config.send_codec_spec->format || |
398 switch (send_codec_spec.cng_plfreq) { | 445 new_config.send_codec_spec->payload_type != |
399 case 16000: | 446 old_config.send_codec_spec->payload_type) { |
400 cn_freq = webrtc::kFreq16000Hz; | 447 return SetupSendCodec(stream, new_config); |
401 break; | 448 } |
402 case 32000: | 449 |
403 cn_freq = webrtc::kFreq32000Hz; | 450 if (!new_config.send_codec_spec) { |
the sun
2017/04/24 14:48:48
RTC_CHECK(new_config.send_codec_spec) << "Cannot r
ossu
2017/04/24 16:20:30
My guess is as good as yours. This really shouldn'
ossu
2017/04/24 16:22:09
... or is that "Your guess is as good as mine?" Hm
| |
404 break; | 451 // I'd expect that a renegotiation that removes all available send codecs |
405 default: | 452 // would either fail or force the stream to recvonly. |
406 RTC_NOTREACHED(); | 453 LOG(LS_ERROR) << "Cannot replace the current encoder with no encoder"; |
407 return false; | 454 RTC_NOTREACHED(); |
455 return false; | |
456 } | |
457 | |
458 const rtc::Optional<int>& new_target_bitrate_bps = | |
459 new_config.send_codec_spec->target_bitrate_bps; | |
460 // If a bitrate has been specified for the codec, use it over the | |
461 // codec's default. | |
462 if (new_target_bitrate_bps && | |
the sun
2017/04/24 14:48:49
Should also execute if "first_time" is true?
ossu
2017/04/24 16:20:30
No. Once we have a SendCodecSpec to use, this gets
the sun
2017/04/25 11:38:53
Ah, sorry!
| |
463 new_target_bitrate_bps != | |
464 old_config.send_codec_spec->target_bitrate_bps) { | |
465 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) { | |
466 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps); | |
467 }); | |
468 } | |
469 | |
470 ReconfigureANA(stream, new_config); | |
471 ReconfigureCNG(stream, new_config); | |
472 | |
473 return true; | |
474 } | |
475 | |
476 void AudioSendStream::ReconfigureANA(AudioSendStream* stream, | |
477 const Config& new_config) { | |
478 if (new_config.audio_network_adaptor_config == | |
the sun
2017/04/24 14:48:49
Shouldn't you push down the "first_time" flag all
ossu
2017/04/24 16:20:30
No. See previous comment.
| |
479 stream->config_.audio_network_adaptor_config) { | |
480 return; | |
481 } | |
482 if (new_config.audio_network_adaptor_config) { | |
483 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) { | |
484 if (encoder->EnableAudioNetworkAdaptor( | |
485 *new_config.audio_network_adaptor_config, stream->event_log_)) { | |
486 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " | |
487 << new_config.rtp.ssrc; | |
488 } else { | |
489 RTC_NOTREACHED(); | |
408 } | 490 } |
409 if (!channel_proxy_->SetSendCNPayloadType( | 491 }); |
410 send_codec_spec.cng_payload_type, cn_freq)) { | 492 } else { |
411 LOG(LS_WARNING) << "SetSendCNPayloadType() failed."; | 493 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) { |
412 // TODO(ajm): This failure condition will be removed from VoE. | 494 encoder->DisableAudioNetworkAdaptor(); |
413 // Restore the return here when we update to a new enough webrtc. | 495 }); |
414 // | 496 LOG(LS_INFO) << "Audio network adaptor disabled on SSRC " |
415 // Not returning false because the SetSendCNPayloadType will fail if | 497 << new_config.rtp.ssrc; |
416 // the channel is already sending. | 498 } |
417 // This can happen if the remote description is applied twice, for | 499 } |
418 // example in the case of ROAP on top of JSEP, where both side will | |
419 // send the offer. | |
420 } | |
421 } | |
422 | 500 |
423 // Only turn on VAD if we have a CN payload type that matches the | 501 void AudioSendStream::ReconfigureCNG(AudioSendStream* stream, |
424 // clockrate for the codec we are going to use. | 502 const Config& new_config) { |
425 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq && | 503 if (new_config.send_codec_spec->cng_payload_type == |
426 send_codec_spec.codec_inst.channels == 1) { | 504 stream->config_.send_codec_spec->cng_payload_type) { |
427 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the | 505 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 } | 506 } |
435 return true; | 507 |
508 // Wrap or unwrap the encoder in an AudioEncoderCNG. | |
509 stream->channel_proxy_->ModifyEncoder( | |
510 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) { | |
511 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr)); | |
512 auto sub_encoders = old_encoder->ReclaimContainedEncoders(); | |
513 if (!sub_encoders.empty()) { | |
514 // Replace enc with its sub encoder. We need to put the sub | |
515 // encoder in a temporary first, since otherwise the old value | |
516 // of enc would be destroyed before the new value got assigned, | |
517 // which would be bad since the new value is a part of the old | |
518 // value. | |
519 auto tmp = std::move(sub_encoders[0]); | |
520 old_encoder = std::move(tmp); | |
521 } | |
522 if (new_config.send_codec_spec->cng_payload_type) { | |
523 AudioEncoderCng::Config config; | |
524 config.speech_encoder = std::move(old_encoder); | |
525 config.num_channels = config.speech_encoder->NumChannels(); | |
526 config.payload_type = *new_config.send_codec_spec->cng_payload_type; | |
527 config.vad_mode = Vad::kVadNormal; | |
528 encoder_ptr->reset(new AudioEncoderCng(std::move(config))); | |
529 } else { | |
530 *encoder_ptr = std::move(old_encoder); | |
531 } | |
532 }); | |
533 } | |
534 | |
535 void AudioSendStream::ReconfigureBitrateObserver( | |
536 AudioSendStream* stream, | |
537 const webrtc::AudioSendStream::Config& new_config) { | |
538 if (stream->config_.min_bitrate_bps == new_config.min_bitrate_bps && | |
the sun
2017/04/24 14:48:49
first_time?
ossu
2017/04/24 16:20:30
That should not be necessary, though I should prob
| |
539 stream->config_.max_bitrate_bps == new_config.max_bitrate_bps) { | |
540 return; | |
541 } | |
542 | |
543 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1) { | |
544 stream->ConfigureBitrateObserver(new_config.min_bitrate_bps, | |
545 new_config.max_bitrate_bps); | |
546 } else { | |
547 stream->RemoveBitrateObserver(); | |
548 } | |
549 } | |
550 | |
551 void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps, | |
552 int max_bitrate_bps) { | |
553 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | |
554 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps); | |
555 rtc::Event thread_sync_event(false /* manual_reset */, false); | |
556 worker_queue_->PostTask([&] { | |
557 // We may get a callback immediately as the observer is registered, so make | |
558 // sure the bitrate limits in config_ are up-to-date. | |
559 config_.min_bitrate_bps = min_bitrate_bps; | |
560 config_.max_bitrate_bps = max_bitrate_bps; | |
561 bitrate_allocator_->AddObserver(this, min_bitrate_bps, max_bitrate_bps, 0, | |
562 true); | |
563 thread_sync_event.Set(); | |
564 }); | |
565 thread_sync_event.Wait(rtc::Event::kForever); | |
566 } | |
567 | |
568 void AudioSendStream::RemoveBitrateObserver() { | |
569 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | |
570 rtc::Event thread_sync_event(false /* manual_reset */, false); | |
571 worker_queue_->PostTask([this, &thread_sync_event] { | |
572 bitrate_allocator_->RemoveObserver(this); | |
573 thread_sync_event.Set(); | |
574 }); | |
575 thread_sync_event.Wait(rtc::Event::kForever); | |
436 } | 576 } |
437 | 577 |
438 } // namespace internal | 578 } // namespace internal |
439 } // namespace webrtc | 579 } // namespace webrtc |
OLD | NEW |