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 <algorithm> | |
the sun
2016/10/17 14:58:22
Why?
minyue-webrtc
2016/10/18 09:39:00
Oh, should be removed, I used std::min in one of t
| |
13 #include <string> | 14 #include <string> |
14 | 15 |
15 #include "webrtc/audio/audio_state.h" | 16 #include "webrtc/audio/audio_state.h" |
16 #include "webrtc/audio/conversion.h" | 17 #include "webrtc/audio/conversion.h" |
17 #include "webrtc/audio/scoped_voe_interface.h" | 18 #include "webrtc/audio/scoped_voe_interface.h" |
18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" |
19 #include "webrtc/base/event.h" | 20 #include "webrtc/base/event.h" |
20 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
21 #include "webrtc/base/task_queue.h" | 22 #include "webrtc/base/task_queue.h" |
22 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" | 23 #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
23 #include "webrtc/modules/pacing/paced_sender.h" | 24 #include "webrtc/modules/pacing/paced_sender.h" |
24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 25 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
25 #include "webrtc/voice_engine/channel_proxy.h" | 26 #include "webrtc/voice_engine/channel_proxy.h" |
26 #include "webrtc/voice_engine/include/voe_audio_processing.h" | 27 #include "webrtc/voice_engine/include/voe_audio_processing.h" |
27 #include "webrtc/voice_engine/include/voe_codec.h" | 28 #include "webrtc/voice_engine/include/voe_codec.h" |
28 #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" | 29 #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" |
29 #include "webrtc/voice_engine/include/voe_volume_control.h" | 30 #include "webrtc/voice_engine/include/voe_volume_control.h" |
30 #include "webrtc/voice_engine/voice_engine_impl.h" | 31 #include "webrtc/voice_engine/voice_engine_impl.h" |
31 | 32 |
32 namespace webrtc { | 33 namespace webrtc { |
34 | |
35 namespace { | |
36 | |
37 constexpr char kOpusCodecName[] = "opus"; | |
38 | |
39 #define LOG_RTCERR2(func, a1, a2, err) \ | |
the sun
2016/10/17 14:58:22
Add TODOs that these macros should be removed.
minyue-webrtc
2016/10/18 09:39:00
Done.
| |
40 LOG(LS_WARNING) << "" << #func << "(" << a1 << ", " << a2 \ | |
41 << ") failed, err=" << err | |
42 | |
43 #define LOG_RTCERR3(func, a1, a2, a3, err) \ | |
44 LOG(LS_WARNING) << "" << #func << "(" << a1 << ", " << a2 << ", " << a3 \ | |
45 << ") failed, err=" << err | |
46 | |
47 std::string ToString(const webrtc::CodecInst& codec) { | |
48 std::stringstream ss; | |
49 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels << " (" | |
50 << codec.pltype << ")"; | |
51 return ss.str(); | |
52 } | |
53 | |
54 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { | |
55 return (_stricmp(codec.plname, ref_name) == 0); | |
56 } | |
57 | |
58 } // namespace | |
59 | |
33 std::string AudioSendStream::Config::Rtp::ToString() const { | 60 std::string AudioSendStream::Config::Rtp::ToString() const { |
34 std::stringstream ss; | 61 std::stringstream ss; |
35 ss << "{ssrc: " << ssrc; | 62 ss << "{ssrc: " << ssrc; |
36 ss << ", extensions: ["; | 63 ss << ", extensions: ["; |
37 for (size_t i = 0; i < extensions.size(); ++i) { | 64 for (size_t i = 0; i < extensions.size(); ++i) { |
38 ss << extensions[i].ToString(); | 65 ss << extensions[i].ToString(); |
39 if (i != extensions.size() - 1) { | 66 if (i != extensions.size() - 1) { |
40 ss << ", "; | 67 ss << ", "; |
41 } | 68 } |
42 } | 69 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 if (extension.uri == RtpExtension::kAbsSendTimeUri) { | 122 if (extension.uri == RtpExtension::kAbsSendTimeUri) { |
96 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); | 123 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); |
97 } else if (extension.uri == RtpExtension::kAudioLevelUri) { | 124 } else if (extension.uri == RtpExtension::kAudioLevelUri) { |
98 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); | 125 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); |
99 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { | 126 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
100 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); | 127 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); |
101 } else { | 128 } else { |
102 RTC_NOTREACHED() << "Registering unsupported RTP extension."; | 129 RTC_NOTREACHED() << "Registering unsupported RTP extension."; |
103 } | 130 } |
104 } | 131 } |
132 if (!SetupSendCodec()) { | |
133 LOG(LS_ERROR) << "Failed to set up send codec state."; | |
134 } | |
105 } | 135 } |
106 | 136 |
107 AudioSendStream::~AudioSendStream() { | 137 AudioSendStream::~AudioSendStream() { |
108 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 138 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
109 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); | 139 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
110 channel_proxy_->DeRegisterExternalTransport(); | 140 channel_proxy_->DeRegisterExternalTransport(); |
111 channel_proxy_->ResetCongestionControlObjects(); | 141 channel_proxy_->ResetCongestionControlObjects(); |
112 channel_proxy_->SetRtcEventLog(nullptr); | 142 channel_proxy_->SetRtcEventLog(nullptr); |
113 } | 143 } |
114 | 144 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 return config_; | 308 return config_; |
279 } | 309 } |
280 | 310 |
281 VoiceEngine* AudioSendStream::voice_engine() const { | 311 VoiceEngine* AudioSendStream::voice_engine() const { |
282 internal::AudioState* audio_state = | 312 internal::AudioState* audio_state = |
283 static_cast<internal::AudioState*>(audio_state_.get()); | 313 static_cast<internal::AudioState*>(audio_state_.get()); |
284 VoiceEngine* voice_engine = audio_state->voice_engine(); | 314 VoiceEngine* voice_engine = audio_state->voice_engine(); |
285 RTC_DCHECK(voice_engine); | 315 RTC_DCHECK(voice_engine); |
286 return voice_engine; | 316 return voice_engine; |
287 } | 317 } |
318 | |
319 // Apply current codec settings to a single voe::Channel used for sending. | |
320 bool AudioSendStream::SetupSendCodec() { | |
321 ScopedVoEInterface<VoEBase> base(voice_engine()); | |
322 ScopedVoEInterface<VoECodec> codec(voice_engine()); | |
323 | |
324 const int channel = config_.voe_channel_id; | |
325 | |
326 // Disable VAD and FEC unless we know the other side wants them. | |
327 codec->SetVADStatus(channel, false); | |
328 codec->SetFECStatus(channel, false); | |
329 | |
330 auto send_codec_spec = config_.send_codec_spec; | |
331 | |
332 // Set the codec immediately, since SetVADStatus() depends on whether | |
333 // the current codec is mono or stereo. | |
334 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec " | |
335 << ToString(send_codec_spec.codec_inst) | |
336 << ", bitrate=" << send_codec_spec.codec_inst.rate; | |
337 | |
338 // If codec is already configured, we do not it again. | |
339 // TODO(minyue): check if this check is really needed, or can we move it into | |
340 // |codec->SetSendCodec|. | |
341 webrtc::CodecInst current_codec = {0}; | |
342 if (codec->GetSendCodec(channel, current_codec) != 0 || | |
343 (send_codec_spec.codec_inst != current_codec)) { | |
344 if (codec->SetSendCodec(channel, send_codec_spec.codec_inst) == -1) { | |
345 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec_spec.codec_inst), | |
346 base->LastError()); | |
347 return false; | |
348 } | |
349 } | |
350 | |
351 // FEC should be enabled after SetSendCodec. | |
352 if (send_codec_spec.enable_codec_fec) { | |
353 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel " | |
354 << channel; | |
355 if (codec->SetFECStatus(channel, true) == -1) { | |
356 // Enable codec internal FEC. Treat any failure as fatal internal error. | |
357 LOG_RTCERR2(SetFECStatus, channel, true, base->LastError()); | |
358 return false; | |
359 } | |
360 } | |
361 | |
362 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) { | |
363 // DTX and maxplaybackrate should be set after SetSendCodec. Because current | |
364 // send codec has to be Opus. | |
365 | |
366 // Set Opus internal DTX. | |
367 LOG(LS_INFO) << "Attempt to " | |
368 << (send_codec_spec.enable_opus_dtx ? "enable" : "disable") | |
369 << " Opus DTX on channel " << channel; | |
370 if (codec->SetOpusDtx(channel, send_codec_spec.enable_opus_dtx)) { | |
371 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec.enable_opus_dtx, | |
372 base->LastError()); | |
373 return false; | |
374 } | |
375 | |
376 // If opus_max_playback_rate <= 0, the default maximum playback rate | |
377 // (48 kHz) will be used. | |
378 if (send_codec_spec.opus_max_playback_rate > 0) { | |
379 LOG(LS_INFO) << "Attempt to set maximum playback rate to " | |
380 << send_codec_spec.opus_max_playback_rate | |
381 << " Hz on channel " << channel; | |
382 if (codec->SetOpusMaxPlaybackRate( | |
383 channel, send_codec_spec.opus_max_playback_rate) == -1) { | |
384 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, | |
385 send_codec_spec.opus_max_playback_rate, base->LastError()); | |
386 return false; | |
387 } | |
388 } | |
389 } | |
390 | |
391 // Set the CN payloadtype and the VAD status. | |
392 if (send_codec_spec.cng_payload_type != -1) { | |
393 // The CN payload type for 8000 Hz clockrate is fixed at 13. | |
394 if (send_codec_spec.cng_plfreq != 8000) { | |
395 webrtc::PayloadFrequencies cn_freq; | |
396 switch (send_codec_spec.cng_plfreq) { | |
397 case 16000: | |
398 cn_freq = webrtc::kFreq16000Hz; | |
399 break; | |
400 case 32000: | |
401 cn_freq = webrtc::kFreq32000Hz; | |
402 break; | |
403 default: | |
404 RTC_NOTREACHED(); | |
405 return false; | |
406 } | |
407 if (codec->SetSendCNPayloadType(channel, send_codec_spec.cng_payload_type, | |
408 cn_freq) == -1) { | |
409 LOG_RTCERR3(SetSendCNPayloadType, channel, | |
410 send_codec_spec.cng_payload_type, cn_freq, | |
411 base->LastError()); | |
412 | |
413 // TODO(ajm): This failure condition will be removed from VoE. | |
414 // Restore the return here when we update to a new enough webrtc. | |
415 // | |
416 // Not returning false because the SetSendCNPayloadType will fail if | |
417 // the channel is already sending. | |
418 // This can happen if the remote description is applied twice, for | |
419 // example in the case of ROAP on top of JSEP, where both side will | |
420 // send the offer. | |
421 } | |
422 } | |
423 | |
424 // Only turn on VAD if we have a CN payload type that matches the | |
425 // clockrate for the codec we are going to use. | |
426 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq && | |
427 send_codec_spec.codec_inst.channels == 1) { | |
428 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the | |
429 // interaction between VAD and Opus FEC. | |
430 LOG(LS_INFO) << "Enabling VAD"; | |
431 if (codec->SetVADStatus(channel, true) == -1) { | |
432 LOG_RTCERR2(SetVADStatus, channel, true, base->LastError()); | |
433 return false; | |
434 } | |
435 } | |
436 } | |
437 return true; | |
438 } | |
439 | |
288 } // namespace internal | 440 } // namespace internal |
289 } // namespace webrtc | 441 } // namespace webrtc |
OLD | NEW |