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> | |
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 // TODO(minyue): these are all copied from corresponding cricket. Find good | |
37 // place for them. | |
38 | |
39 const char kOpusCodecName[] = "opus"; | |
40 const char kIsacCodecName[] = "isac"; | |
41 const char kG722CodecName[] = "g722"; | |
42 const char kIlbcCodecName[] = "ilbc"; | |
43 const char kPcmuCodecName[] = "pcmu"; | |
44 const char kPcmaCodecName[] = "pcma"; | |
45 const char kCnCodecName[] = "cn"; | |
46 const char kDtmfCodecName[] = "telephone-event"; | |
47 const int kOpusMaxBitrate = 510000; | |
48 const int kIsacMaxBitrate = 56000; | |
49 | |
50 static const int kMaxNumPacketSize = 6; | |
51 struct CodecPref { | |
52 const char* name; | |
53 int clockrate; | |
54 size_t channels; | |
55 int payload_type; | |
56 bool is_multi_rate; | |
57 int packet_sizes_ms[kMaxNumPacketSize]; | |
58 int max_bitrate_bps; | |
59 }; | |
60 | |
61 const CodecPref kCodecPrefs[11] = { | |
62 {kOpusCodecName, 48000, 2, 111, true, {10, 20, 40, 60}, kOpusMaxBitrate}, | |
63 {kIsacCodecName, 16000, 1, 103, true, {30, 60}, kIsacMaxBitrate}, | |
64 {kIsacCodecName, 32000, 1, 104, true, {30}, kIsacMaxBitrate}, | |
65 // G722 should be advertised as 8000 Hz because of the RFC "bug". | |
66 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}}, | |
67 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}}, | |
68 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}}, | |
69 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}}, | |
70 {kCnCodecName, 32000, 1, 106, false, {}}, | |
71 {kCnCodecName, 16000, 1, 105, false, {}}, | |
72 {kCnCodecName, 8000, 1, 13, false, {}}, | |
73 {kDtmfCodecName, 8000, 1, 126, false, {}}}; | |
74 | |
75 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { | |
76 return (_stricmp(codec.plname, ref_name) == 0); | |
77 } | |
78 | |
79 bool IsCodecMultiRate(const webrtc::CodecInst& codec) { | |
80 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) { | |
81 if (IsCodec(codec, kCodecPrefs[i].name) && | |
82 kCodecPrefs[i].clockrate == codec.plfreq) { | |
83 return kCodecPrefs[i].is_multi_rate; | |
84 } | |
85 } | |
86 return false; | |
87 } | |
88 | |
89 int MaxBitrateBps(const webrtc::CodecInst& codec) { | |
90 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) { | |
91 if (IsCodec(codec, kCodecPrefs[i].name) && | |
92 kCodecPrefs[i].clockrate == codec.plfreq) { | |
93 return kCodecPrefs[i].max_bitrate_bps; | |
94 } | |
95 } | |
96 return 0; | |
97 } | |
98 | |
99 template <typename T> | |
100 static T MinPositive(T a, T b) { | |
101 if (a <= 0) { | |
102 return b; | |
103 } | |
104 if (b <= 0) { | |
105 return a; | |
106 } | |
107 return std::min(a, b); | |
108 } | |
109 | |
110 } // namespace | |
111 | |
33 std::string AudioSendStream::Config::Rtp::ToString() const { | 112 std::string AudioSendStream::Config::Rtp::ToString() const { |
34 std::stringstream ss; | 113 std::stringstream ss; |
35 ss << "{ssrc: " << ssrc; | 114 ss << "{ssrc: " << ssrc; |
36 ss << ", extensions: ["; | 115 ss << ", extensions: ["; |
37 for (size_t i = 0; i < extensions.size(); ++i) { | 116 for (size_t i = 0; i < extensions.size(); ++i) { |
38 ss << extensions[i].ToString(); | 117 ss << extensions[i].ToString(); |
39 if (i != extensions.size() - 1) { | 118 if (i != extensions.size() - 1) { |
40 ss << ", "; | 119 ss << ", "; |
41 } | 120 } |
42 } | 121 } |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 if (extension.uri == RtpExtension::kAbsSendTimeUri) { | 174 if (extension.uri == RtpExtension::kAbsSendTimeUri) { |
96 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); | 175 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); |
97 } else if (extension.uri == RtpExtension::kAudioLevelUri) { | 176 } else if (extension.uri == RtpExtension::kAudioLevelUri) { |
98 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); | 177 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); |
99 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { | 178 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
100 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); | 179 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); |
101 } else { | 180 } else { |
102 RTC_NOTREACHED() << "Registering unsupported RTP extension."; | 181 RTC_NOTREACHED() << "Registering unsupported RTP extension."; |
103 } | 182 } |
104 } | 183 } |
184 SetSendCodecs(); | |
105 } | 185 } |
106 | 186 |
107 AudioSendStream::~AudioSendStream() { | 187 AudioSendStream::~AudioSendStream() { |
108 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 188 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
109 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); | 189 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
110 channel_proxy_->DeRegisterExternalTransport(); | 190 channel_proxy_->DeRegisterExternalTransport(); |
111 channel_proxy_->ResetCongestionControlObjects(); | 191 channel_proxy_->ResetCongestionControlObjects(); |
112 channel_proxy_->SetRtcEventLog(nullptr); | 192 channel_proxy_->SetRtcEventLog(nullptr); |
113 } | 193 } |
114 | 194 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
278 return config_; | 358 return config_; |
279 } | 359 } |
280 | 360 |
281 VoiceEngine* AudioSendStream::voice_engine() const { | 361 VoiceEngine* AudioSendStream::voice_engine() const { |
282 internal::AudioState* audio_state = | 362 internal::AudioState* audio_state = |
283 static_cast<internal::AudioState*>(audio_state_.get()); | 363 static_cast<internal::AudioState*>(audio_state_.get()); |
284 VoiceEngine* voice_engine = audio_state->voice_engine(); | 364 VoiceEngine* voice_engine = audio_state->voice_engine(); |
285 RTC_DCHECK(voice_engine); | 365 RTC_DCHECK(voice_engine); |
286 return voice_engine; | 366 return voice_engine; |
287 } | 367 } |
368 | |
369 // Apply current codec settings to a single voe::Channel used for sending. | |
370 bool AudioSendStream::SetSendCodecs() { | |
371 ScopedVoEInterface<VoECodec> codec(voice_engine()); | |
372 const int channel = config_.voe_channel_id; | |
373 | |
374 // Disable VAD and FEC unless we know the other side wants them. | |
375 codec->SetVADStatus(channel, false); | |
376 codec->SetFECStatus(channel, false); | |
377 | |
378 // Set the codec immediately, since SetVADStatus() depends on whether | |
379 // the current codec is mono or stereo. | |
380 if (!SetSendCodec(config_.send_codec_spec.codec_inst)) { | |
381 return false; | |
382 } | |
383 | |
384 // FEC should be enabled after SetSendCodec. | |
385 if (config_.send_codec_spec.enable_codec_fec) { | |
386 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel " | |
387 << channel; | |
388 if (codec->SetFECStatus(channel, true) == -1) { | |
389 // Enable codec internal FEC. Treat any failure as fatal internal error. | |
390 // TODO(minyue): use normal logging. | |
391 // LOG_RTCERR2(SetFECStatus, channel, true); | |
392 return false; | |
393 } | |
394 } | |
395 | |
396 if (IsCodec(config_.send_codec_spec.codec_inst, kOpusCodecName)) { | |
397 // DTX and maxplaybackrate should be set after SetSendCodec. Because current | |
398 // send codec has to be Opus. | |
399 | |
400 // Set Opus internal DTX. | |
401 LOG(LS_INFO) << "Attempt to " | |
402 << (config_.send_codec_spec.enable_opus_dtx ? "enable" | |
403 : "disable") | |
404 << " Opus DTX on channel " << channel; | |
405 if (codec->SetOpusDtx(channel, config_.send_codec_spec.enable_opus_dtx)) { | |
406 // TODO(minyue): use normal logging. | |
407 // LOG_RTCERR2(SetOpusDtx, channel, | |
408 // config_.send_codec_spec.enable_opus_dtx); | |
409 return false; | |
410 } | |
411 | |
412 // If opus_max_playback_rate <= 0, the default maximum playback rate | |
413 // (48 kHz) will be used. | |
414 if (config_.send_codec_spec.opus_max_playback_rate > 0) { | |
415 LOG(LS_INFO) << "Attempt to set maximum playback rate to " | |
416 << config_.send_codec_spec.opus_max_playback_rate | |
417 << " Hz on channel " << channel; | |
418 if (codec->SetOpusMaxPlaybackRate( | |
419 channel, config_.send_codec_spec.opus_max_playback_rate) == -1) { | |
420 // TODO(minyue): use normal logging. | |
421 // LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, | |
422 // config_.send_codec_spec.opus_max_playback_rate); | |
423 return false; | |
424 } | |
425 } | |
426 } | |
427 // TODO(solenberg): ApplyMaxSendBitrate() yields another call to | |
428 // SetSendCodec(). Check if it is possible to fuse with the previous call | |
429 // in this function. | |
430 ApplyMaxSendBitrate(); | |
431 | |
432 // Set the CN payloadtype and the VAD status. | |
433 if (config_.send_codec_spec.cng_payload_type != -1) { | |
434 // The CN payload type for 8000 Hz clockrate is fixed at 13. | |
435 if (config_.send_codec_spec.cng_plfreq != 8000) { | |
436 webrtc::PayloadFrequencies cn_freq; | |
437 switch (config_.send_codec_spec.cng_plfreq) { | |
438 case 16000: | |
439 cn_freq = webrtc::kFreq16000Hz; | |
440 break; | |
441 case 32000: | |
442 cn_freq = webrtc::kFreq32000Hz; | |
443 break; | |
444 default: | |
445 RTC_NOTREACHED(); | |
446 return false; | |
447 } | |
448 if (codec->SetSendCNPayloadType(channel, | |
449 config_.send_codec_spec.cng_payload_type, | |
450 cn_freq) == -1) { | |
451 // TODO(minyue): use normal logging. | |
452 // LOG_RTCERR3(SetSendCNPayloadType, channel, | |
453 // config_.send_codec_spec.cng_payload_type, cn_freq); | |
454 | |
455 // TODO(ajm): This failure condition will be removed from VoE. | |
456 // Restore the return here when we update to a new enough webrtc. | |
457 // | |
458 // Not returning false because the SetSendCNPayloadType will fail if | |
459 // the channel is already sending. | |
460 // This can happen if the remote description is applied twice, for | |
461 // example in the case of ROAP on top of JSEP, where both side will | |
462 // send the offer. | |
463 } | |
464 } | |
465 | |
466 // Only turn on VAD if we have a CN payload type that matches the | |
467 // clockrate for the codec we are going to use. | |
468 if (config_.send_codec_spec.cng_plfreq == | |
469 config_.send_codec_spec.codec_inst.plfreq && | |
470 config_.send_codec_spec.codec_inst.channels == 1) { | |
471 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the | |
472 // interaction between VAD and Opus FEC. | |
473 LOG(LS_INFO) << "Enabling VAD"; | |
474 if (codec->SetVADStatus(channel, true) == -1) { | |
475 // TODO(minyue): use normal logging. | |
476 // LOG_RTCERR2(SetVADStatus, channel, true); | |
477 return false; | |
478 } | |
479 } | |
480 } | |
481 return true; | |
482 } | |
483 | |
484 bool AudioSendStream::SetSendCodec(const webrtc::CodecInst& send_codec) { | |
485 // TODO(minyue): avoid ToString | |
486 // LOG(LS_INFO) << "Send channel " << channel << " selected voice codec " | |
487 // << ToString(send_codec) << ", bitrate=" << send_codec.rate; | |
488 | |
489 ScopedVoEInterface<VoECodec> codec(voice_engine()); | |
490 int channel = config_.voe_channel_id; | |
491 | |
492 webrtc::CodecInst current_codec = {0}; | |
493 if (codec->GetSendCodec(channel, current_codec) == 0 && | |
494 (send_codec == current_codec)) { | |
495 // Codec is already configured, we can return without setting it again. | |
496 return true; | |
497 } | |
498 | |
499 if (codec->SetSendCodec(channel, send_codec) == -1) { | |
500 // TODO(minyue): use normal logging. | |
501 // LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec)); | |
502 return false; | |
503 } | |
504 return true; | |
505 } | |
506 | |
507 bool AudioSendStream::ApplyMaxSendBitrate() { | |
508 int bps = config_.max_send_bitrate_bps; | |
509 | |
510 // Bitrate is auto by default. | |
511 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by | |
512 // SetMaxSendBandwith(0), the second call removes the previous limit. | |
513 if (bps <= 0) { | |
514 return true; | |
515 } | |
516 | |
517 if (!HasSendCodec()) { | |
518 LOG(LS_INFO) << "The send codec has not been set up yet. " | |
519 << "The send bitrate setting will be applied later."; | |
520 return true; | |
521 } | |
522 | |
523 webrtc::CodecInst codec = config_.send_codec_spec.codec_inst; | |
524 bool is_multi_rate = IsCodecMultiRate(codec); | |
525 | |
526 if (is_multi_rate) { | |
527 // If codec is multi-rate then just set the bitrate. | |
528 int max_bitrate_bps = MaxBitrateBps(codec); | |
529 codec.rate = std::min(bps, max_bitrate_bps); | |
530 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps | |
531 << " bps."; | |
532 if (!SetSendCodec(codec)) { | |
533 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate " | |
534 << bps << " bps."; | |
535 return false; | |
536 } | |
537 return true; | |
538 } else { | |
539 // If codec is not multi-rate and |bps| is less than the fixed bitrate | |
540 // then fail. If codec is not multi-rate and |bps| exceeds or equal the | |
541 // fixed bitrate then ignore. | |
542 if (bps < codec.rate) { | |
543 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate " | |
544 << bps << " bps" | |
545 << ", requires at least " << codec.rate << " bps."; | |
546 return false; | |
547 } | |
548 return true; | |
549 } | |
550 } | |
551 | |
552 bool AudioSendStream::HasSendCodec() const { | |
553 return config_.send_codec_spec.codec_inst.pltype != -1; | |
the sun
2016/10/12 15:15:17
Think we can do without this function.
minyue-webrtc
2016/10/12 18:59:21
sure.
| |
554 } | |
555 | |
288 } // namespace internal | 556 } // namespace internal |
289 } // namespace webrtc | 557 } // namespace webrtc |
OLD | NEW |