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" |
23 #include "webrtc/modules/audio_coding/codecs/cng/audio_encoder_cng.h" | |
22 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 24 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
23 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 25 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
24 #include "webrtc/modules/pacing/paced_sender.h" | 26 #include "webrtc/modules/pacing/paced_sender.h" |
25 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
26 #include "webrtc/voice_engine/channel_proxy.h" | 28 #include "webrtc/voice_engine/channel_proxy.h" |
27 #include "webrtc/voice_engine/include/voe_base.h" | 29 #include "webrtc/voice_engine/include/voe_base.h" |
28 #include "webrtc/voice_engine/transmit_mixer.h" | 30 #include "webrtc/voice_engine/transmit_mixer.h" |
29 #include "webrtc/voice_engine/voice_engine_impl.h" | 31 #include "webrtc/voice_engine/voice_engine_impl.h" |
30 | 32 |
31 namespace webrtc { | 33 namespace webrtc { |
32 | 34 |
33 namespace { | |
34 | |
35 constexpr char kOpusCodecName[] = "opus"; | |
36 | |
37 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { | |
38 return (STR_CASE_CMP(codec.plname, ref_name) == 0); | |
39 } | |
40 } // namespace | |
41 | |
42 namespace internal { | 35 namespace internal { |
43 AudioSendStream::AudioSendStream( | 36 AudioSendStream::AudioSendStream( |
44 const webrtc::AudioSendStream::Config& config, | 37 const webrtc::AudioSendStream::Config& config, |
45 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, | 38 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
46 rtc::TaskQueue* worker_queue, | 39 rtc::TaskQueue* worker_queue, |
47 PacketRouter* packet_router, | 40 PacketRouter* packet_router, |
48 CongestionController* congestion_controller, | 41 CongestionController* congestion_controller, |
49 BitrateAllocator* bitrate_allocator, | 42 BitrateAllocator* bitrate_allocator, |
50 RtcEventLog* event_log, | 43 RtcEventLog* event_log, |
51 RtcpRttStats* rtcp_rtt_stats) | 44 RtcpRttStats* rtcp_rtt_stats) |
52 : worker_queue_(worker_queue), | 45 : worker_queue_(worker_queue), |
53 config_(config), | 46 config_(config), |
54 audio_state_(audio_state), | 47 audio_state_(audio_state), |
48 event_log_(event_log), | |
55 bitrate_allocator_(bitrate_allocator), | 49 bitrate_allocator_(bitrate_allocator), |
56 congestion_controller_(congestion_controller) { | 50 congestion_controller_(congestion_controller) { |
57 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); | 51 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
58 RTC_DCHECK_NE(config_.voe_channel_id, -1); | 52 RTC_DCHECK_NE(config_.voe_channel_id, -1); |
59 RTC_DCHECK(audio_state_.get()); | 53 RTC_DCHECK(audio_state_.get()); |
60 RTC_DCHECK(congestion_controller); | 54 RTC_DCHECK(congestion_controller); |
61 | 55 |
62 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); | 56 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); |
63 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); | 57 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); |
64 channel_proxy_->SetRtcEventLog(event_log); | 58 channel_proxy_->SetRtcEventLog(event_log_); |
65 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); | 59 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); |
66 channel_proxy_->SetRTCPStatus(true); | 60 channel_proxy_->SetRTCPStatus(true); |
67 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); | 61 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); |
68 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); | 62 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); |
69 // TODO(solenberg): Config NACK history window (which is a packet count), | 63 // TODO(solenberg): Config NACK history window (which is a packet count), |
70 // using the actual packet size for the configured codec. | 64 // using the actual packet size for the configured codec. |
71 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, | 65 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, |
72 config_.rtp.nack.rtp_history_ms / 20); | 66 config_.rtp.nack.rtp_history_ms / 20); |
73 | 67 |
74 channel_proxy_->RegisterExternalTransport(config.send_transport); | 68 channel_proxy_->RegisterExternalTransport(config.send_transport); |
75 | 69 |
76 for (const auto& extension : config.rtp.extensions) { | 70 for (const auto& extension : config.rtp.extensions) { |
77 if (extension.uri == RtpExtension::kAudioLevelUri) { | 71 if (extension.uri == RtpExtension::kAudioLevelUri) { |
78 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); | 72 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); |
79 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { | 73 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
80 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); | 74 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); |
81 congestion_controller->EnablePeriodicAlrProbing(true); | 75 congestion_controller->EnablePeriodicAlrProbing(true); |
82 bandwidth_observer_.reset(congestion_controller->GetBitrateController() | 76 bandwidth_observer_.reset(congestion_controller->GetBitrateController() |
83 ->CreateRtcpBandwidthObserver()); | 77 ->CreateRtcpBandwidthObserver()); |
84 } else { | 78 } else { |
85 RTC_NOTREACHED() << "Registering unsupported RTP extension."; | 79 RTC_NOTREACHED() << "Registering unsupported RTP extension."; |
86 } | 80 } |
87 } | 81 } |
88 channel_proxy_->RegisterSenderCongestionControlObjects( | 82 channel_proxy_->RegisterSenderCongestionControlObjects( |
89 congestion_controller->pacer(), congestion_controller, packet_router, | 83 congestion_controller->pacer(), congestion_controller, packet_router, |
90 bandwidth_observer_.get()); | 84 bandwidth_observer_.get()); |
91 if (!SetupSendCodec()) { | 85 if (config_.send_codec_spec && !SetupSendCodec()) { |
92 LOG(LS_ERROR) << "Failed to set up send codec state."; | 86 LOG(LS_ERROR) << "Failed to set up send codec state."; |
the sun
2017/03/20 20:17:25
nit: indent off
ossu
2017/03/21 14:53:25
Acknowledged.
| |
93 } | 87 } |
94 } | 88 } |
95 | 89 |
96 AudioSendStream::~AudioSendStream() { | 90 AudioSendStream::~AudioSendStream() { |
97 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 91 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
98 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); | 92 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
99 channel_proxy_->DeRegisterExternalTransport(); | 93 channel_proxy_->DeRegisterExternalTransport(); |
100 channel_proxy_->ResetCongestionControlObjects(); | 94 channel_proxy_->ResetCongestionControlObjects(); |
101 channel_proxy_->SetRtcEventLog(nullptr); | 95 channel_proxy_->SetRtcEventLog(nullptr); |
102 channel_proxy_->SetRtcpRttStats(nullptr); | 96 channel_proxy_->SetRtcpRttStats(nullptr); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 stats.packets_sent = call_stats.packetsSent; | 156 stats.packets_sent = call_stats.packetsSent; |
163 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine | 157 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine |
164 // returns 0 to indicate an error value. | 158 // returns 0 to indicate an error value. |
165 if (call_stats.rttMs > 0) { | 159 if (call_stats.rttMs > 0) { |
166 stats.rtt_ms = call_stats.rttMs; | 160 stats.rtt_ms = call_stats.rttMs; |
167 } | 161 } |
168 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable | 162 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable |
169 // implementation. | 163 // implementation. |
170 stats.aec_quality_min = -1; | 164 stats.aec_quality_min = -1; |
171 | 165 |
172 webrtc::CodecInst codec_inst = {0}; | 166 if (config_.send_codec_spec) { |
173 if (channel_proxy_->GetSendCodec(&codec_inst)) { | 167 const auto& spec = *config_.send_codec_spec; |
174 RTC_DCHECK_NE(codec_inst.pltype, -1); | 168 stats.codec_name = spec.format.name; |
175 stats.codec_name = codec_inst.plname; | 169 stats.codec_payload_type = rtc::Optional<int>(spec.payload_type); |
176 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype); | |
177 | 170 |
178 // Get data from the last remote RTCP report. | 171 // Get data from the last remote RTCP report. |
179 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) { | 172 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) { |
180 // Lookup report for send ssrc only. | 173 // Lookup report for send ssrc only. |
181 if (block.source_SSRC == stats.local_ssrc) { | 174 if (block.source_SSRC == stats.local_ssrc) { |
182 stats.packets_lost = block.cumulative_num_packets_lost; | 175 stats.packets_lost = block.cumulative_num_packets_lost; |
183 stats.fraction_lost = Q8ToFloat(block.fraction_lost); | 176 stats.fraction_lost = Q8ToFloat(block.fraction_lost); |
184 stats.ext_seqnum = block.extended_highest_sequence_number; | 177 stats.ext_seqnum = block.extended_highest_sequence_number; |
185 // Convert samples to milliseconds. | 178 // Convert timestamps to milliseconds. |
186 if (codec_inst.plfreq / 1000 > 0) { | 179 if (spec.format.clockrate_hz / 1000 > 0) { |
187 stats.jitter_ms = | 180 stats.jitter_ms = |
188 block.interarrival_jitter / (codec_inst.plfreq / 1000); | 181 block.interarrival_jitter / (spec.format.clockrate_hz / 1000); |
189 } | 182 } |
190 break; | 183 break; |
191 } | 184 } |
192 } | 185 } |
193 } | 186 } |
194 | 187 |
195 ScopedVoEInterface<VoEBase> base(voice_engine()); | 188 ScopedVoEInterface<VoEBase> base(voice_engine()); |
196 RTC_DCHECK(base->transmit_mixer()); | 189 RTC_DCHECK(base->transmit_mixer()); |
197 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange(); | 190 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange(); |
198 RTC_DCHECK_LE(0, stats.audio_level); | 191 RTC_DCHECK_LE(0, stats.audio_level); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 VoiceEngine* AudioSendStream::voice_engine() const { | 254 VoiceEngine* AudioSendStream::voice_engine() const { |
262 internal::AudioState* audio_state = | 255 internal::AudioState* audio_state = |
263 static_cast<internal::AudioState*>(audio_state_.get()); | 256 static_cast<internal::AudioState*>(audio_state_.get()); |
264 VoiceEngine* voice_engine = audio_state->voice_engine(); | 257 VoiceEngine* voice_engine = audio_state->voice_engine(); |
265 RTC_DCHECK(voice_engine); | 258 RTC_DCHECK(voice_engine); |
266 return voice_engine; | 259 return voice_engine; |
267 } | 260 } |
268 | 261 |
269 // Apply current codec settings to a single voe::Channel used for sending. | 262 // Apply current codec settings to a single voe::Channel used for sending. |
270 bool AudioSendStream::SetupSendCodec() { | 263 bool AudioSendStream::SetupSendCodec() { |
271 // Disable VAD and FEC unless we know the other side wants them. | 264 RTC_DCHECK(config_.send_codec_spec); |
272 channel_proxy_->SetVADStatus(false); | 265 const auto& spec = *config_.send_codec_spec; |
273 channel_proxy_->SetCodecFECStatus(false); | 266 std::unique_ptr<AudioEncoder> encoder = |
267 config_.encoder_factory->MakeAudioEncoder(spec.payload_type, spec.format); | |
274 | 268 |
275 // We disable audio network adaptor here. This will on one hand make sure that | 269 if (!encoder) { |
276 // audio network adaptor is disabled by default, and on the other allow audio | 270 LOG(LS_ERROR) << "Unable to create encoder for " << spec.format; |
277 // network adaptor to be reconfigured, since SetReceiverFrameLengthRange can | 271 return false; |
278 // be only called when audio network adaptor is disabled. | 272 } |
279 channel_proxy_->DisableAudioNetworkAdaptor(); | |
280 | 273 |
281 const auto& send_codec_spec = config_.send_codec_spec; | 274 // If a bitrate has been specified for the codec, use it over the |
275 // codec's default. | |
276 if (spec.target_bitrate_bps) { | |
277 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps); | |
278 } | |
282 | 279 |
283 // We set the codec first, since the below extra configuration is only applied | 280 // Enable ANA if configured (currently only used by Opus). |
284 // to the "current" codec. | 281 if (config_.audio_network_adaptor_config) { |
285 | 282 if (encoder->EnableAudioNetworkAdaptor( |
286 // If codec is already configured, we do not it again. | 283 *config_.audio_network_adaptor_config, event_log_, |
287 // TODO(minyue): check if this check is really needed, or can we move it into | 284 Clock::GetRealTimeClock())) { |
288 // |codec->SetSendCodec|. | 285 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
289 webrtc::CodecInst current_codec = {0}; | 286 << config_.rtp.ssrc; |
290 if (!channel_proxy_->GetSendCodec(¤t_codec) || | 287 } else { |
291 (send_codec_spec.codec_inst != current_codec)) { | 288 RTC_NOTREACHED(); |
292 if (!channel_proxy_->SetSendCodec(send_codec_spec.codec_inst)) { | |
293 LOG(LS_WARNING) << "SetSendCodec() failed."; | |
294 return false; | |
295 } | 289 } |
296 } | 290 } |
297 | 291 |
298 // Codec internal FEC. Treat any failure as fatal internal error. | 292 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled. |
299 if (send_codec_spec.enable_codec_fec) { | 293 if (spec.cng_payload_type != -1) { |
the sun
2017/03/20 20:17:25
Is it time to make cng_payload_type an Optional?
ossu
2017/03/21 14:53:25
Yeah, why not? It's clearer imo. -1 is just HowItU
kwiberg-webrtc
2017/03/22 00:00:44
+1
| |
300 if (!channel_proxy_->SetCodecFECStatus(true)) { | 294 AudioEncoderCng::Config config; |
301 LOG(LS_WARNING) << "SetCodecFECStatus() failed."; | 295 config.num_channels = encoder->NumChannels(); |
302 return false; | 296 config.payload_type = spec.cng_payload_type; |
303 } | 297 config.speech_encoder = std::move(encoder); |
298 config.vad_mode = Vad::kVadNormal; | |
299 encoder.reset(new AudioEncoderCng(std::move(config))); | |
304 } | 300 } |
305 | 301 |
306 // DTX and maxplaybackrate are only set if current codec is Opus. | 302 channel_proxy_->SetEncoder(spec.payload_type, std::move(encoder)); |
the sun
2017/03/20 20:17:25
This is so great - we won't have the half-setup st
ossu
2017/03/21 14:53:25
Hmm, I see what you mean. I'll have a look at movi
ossu
2017/03/22 13:28:32
I have now looked at it and I cannot move the cons
| |
307 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) { | |
308 if (!channel_proxy_->SetOpusDtx(send_codec_spec.enable_opus_dtx)) { | |
309 LOG(LS_WARNING) << "SetOpusDtx() failed."; | |
310 return false; | |
311 } | |
312 | 303 |
313 // If opus_max_playback_rate <= 0, the default maximum playback rate | |
314 // (48 kHz) will be used. | |
315 if (send_codec_spec.opus_max_playback_rate > 0) { | |
316 if (!channel_proxy_->SetOpusMaxPlaybackRate( | |
317 send_codec_spec.opus_max_playback_rate)) { | |
318 LOG(LS_WARNING) << "SetOpusMaxPlaybackRate() failed."; | |
319 return false; | |
320 } | |
321 } | |
322 | |
323 if (config_.audio_network_adaptor_config) { | |
324 // Audio network adaptor is only allowed for Opus currently. | |
325 // |SetReceiverFrameLengthRange| needs to be called before | |
326 // |EnableAudioNetworkAdaptor|. | |
327 channel_proxy_->SetReceiverFrameLengthRange(send_codec_spec.min_ptime_ms, | |
328 send_codec_spec.max_ptime_ms); | |
329 channel_proxy_->EnableAudioNetworkAdaptor( | |
330 *config_.audio_network_adaptor_config); | |
331 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " | |
332 << config_.rtp.ssrc; | |
333 } | |
334 } | |
335 | |
336 // Set the CN payloadtype and the VAD status. | |
337 if (send_codec_spec.cng_payload_type != -1) { | |
338 // The CN payload type for 8000 Hz clockrate is fixed at 13. | |
339 if (send_codec_spec.cng_plfreq != 8000) { | |
340 webrtc::PayloadFrequencies cn_freq; | |
341 switch (send_codec_spec.cng_plfreq) { | |
342 case 16000: | |
343 cn_freq = webrtc::kFreq16000Hz; | |
344 break; | |
345 case 32000: | |
346 cn_freq = webrtc::kFreq32000Hz; | |
347 break; | |
348 default: | |
349 RTC_NOTREACHED(); | |
350 return false; | |
351 } | |
352 if (!channel_proxy_->SetSendCNPayloadType( | |
353 send_codec_spec.cng_payload_type, cn_freq)) { | |
354 LOG(LS_WARNING) << "SetSendCNPayloadType() failed."; | |
355 // TODO(ajm): This failure condition will be removed from VoE. | |
356 // Restore the return here when we update to a new enough webrtc. | |
357 // | |
358 // Not returning false because the SetSendCNPayloadType will fail if | |
359 // the channel is already sending. | |
360 // This can happen if the remote description is applied twice, for | |
361 // example in the case of ROAP on top of JSEP, where both side will | |
362 // send the offer. | |
363 } | |
364 } | |
365 | |
366 // Only turn on VAD if we have a CN payload type that matches the | |
367 // clockrate for the codec we are going to use. | |
368 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq && | |
369 send_codec_spec.codec_inst.channels == 1) { | |
370 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the | |
371 // interaction between VAD and Opus FEC. | |
372 if (!channel_proxy_->SetVADStatus(true)) { | |
373 LOG(LS_WARNING) << "SetVADStatus() failed."; | |
374 return false; | |
375 } | |
376 } | |
377 } | |
378 return true; | 304 return true; |
379 } | 305 } |
380 | 306 |
381 } // namespace internal | 307 } // namespace internal |
382 } // namespace webrtc | 308 } // namespace webrtc |
OLD | NEW |