Chromium Code Reviews| 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 | |
| 37 constexpr char kOpusCodecName[] = "opus"; | |
| 38 | |
| 39 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { | |
| 40 return (_stricmp(codec.plname, ref_name) == 0); | |
| 41 } | |
| 42 | |
| 43 template <typename T> | |
| 44 static T MinPositive(T a, T b) { | |
| 45 if (a <= 0) { | |
| 46 return b; | |
| 47 } | |
| 48 if (b <= 0) { | |
| 49 return a; | |
| 50 } | |
| 51 return std::min(a, b); | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 33 std::string AudioSendStream::Config::Rtp::ToString() const { | 56 std::string AudioSendStream::Config::Rtp::ToString() const { |
| 34 std::stringstream ss; | 57 std::stringstream ss; |
| 35 ss << "{ssrc: " << ssrc; | 58 ss << "{ssrc: " << ssrc; |
| 36 ss << ", extensions: ["; | 59 ss << ", extensions: ["; |
| 37 for (size_t i = 0; i < extensions.size(); ++i) { | 60 for (size_t i = 0; i < extensions.size(); ++i) { |
| 38 ss << extensions[i].ToString(); | 61 ss << extensions[i].ToString(); |
| 39 if (i != extensions.size() - 1) { | 62 if (i != extensions.size() - 1) { |
| 40 ss << ", "; | 63 ss << ", "; |
| 41 } | 64 } |
| 42 } | 65 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 if (extension.uri == RtpExtension::kAbsSendTimeUri) { | 118 if (extension.uri == RtpExtension::kAbsSendTimeUri) { |
| 96 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); | 119 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id); |
| 97 } else if (extension.uri == RtpExtension::kAudioLevelUri) { | 120 } else if (extension.uri == RtpExtension::kAudioLevelUri) { |
| 98 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); | 121 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); |
| 99 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { | 122 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
| 100 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); | 123 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); |
| 101 } else { | 124 } else { |
| 102 RTC_NOTREACHED() << "Registering unsupported RTP extension."; | 125 RTC_NOTREACHED() << "Registering unsupported RTP extension."; |
| 103 } | 126 } |
| 104 } | 127 } |
| 128 SetSendCodecs(); | |
| 105 } | 129 } |
| 106 | 130 |
| 107 AudioSendStream::~AudioSendStream() { | 131 AudioSendStream::~AudioSendStream() { |
| 108 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 132 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 109 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); | 133 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
| 110 channel_proxy_->DeRegisterExternalTransport(); | 134 channel_proxy_->DeRegisterExternalTransport(); |
| 111 channel_proxy_->ResetCongestionControlObjects(); | 135 channel_proxy_->ResetCongestionControlObjects(); |
| 112 channel_proxy_->SetRtcEventLog(nullptr); | 136 channel_proxy_->SetRtcEventLog(nullptr); |
| 113 } | 137 } |
| 114 | 138 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 return config_; | 302 return config_; |
| 279 } | 303 } |
| 280 | 304 |
| 281 VoiceEngine* AudioSendStream::voice_engine() const { | 305 VoiceEngine* AudioSendStream::voice_engine() const { |
| 282 internal::AudioState* audio_state = | 306 internal::AudioState* audio_state = |
| 283 static_cast<internal::AudioState*>(audio_state_.get()); | 307 static_cast<internal::AudioState*>(audio_state_.get()); |
| 284 VoiceEngine* voice_engine = audio_state->voice_engine(); | 308 VoiceEngine* voice_engine = audio_state->voice_engine(); |
| 285 RTC_DCHECK(voice_engine); | 309 RTC_DCHECK(voice_engine); |
| 286 return voice_engine; | 310 return voice_engine; |
| 287 } | 311 } |
| 312 | |
| 313 // Apply current codec settings to a single voe::Channel used for sending. | |
| 314 bool AudioSendStream::SetSendCodecs() { | |
| 315 ScopedVoEInterface<VoECodec> codec(voice_engine()); | |
| 316 const int channel = config_.voe_channel_id; | |
| 317 | |
| 318 // Disable VAD and FEC unless we know the other side wants them. | |
| 319 codec->SetVADStatus(channel, false); | |
| 320 codec->SetFECStatus(channel, false); | |
| 321 | |
| 322 // Set the codec immediately, since SetVADStatus() depends on whether | |
| 323 // the current codec is mono or stereo. | |
| 324 if (!SetSendCodec(config_.send_codec_spec.codec_inst)) { | |
| 325 return false; | |
| 326 } | |
| 327 | |
| 328 // FEC should be enabled after SetSendCodec. | |
| 329 if (config_.send_codec_spec.enable_codec_fec) { | |
| 330 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel " | |
| 331 << channel; | |
| 332 if (codec->SetFECStatus(channel, true) == -1) { | |
| 333 // Enable codec internal FEC. Treat any failure as fatal internal error. | |
| 334 // TODO(minyue): use normal logging. | |
| 335 // LOG_RTCERR2(SetFECStatus, channel, true); | |
| 336 return false; | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 if (IsCodec(config_.send_codec_spec.codec_inst, kOpusCodecName)) { | |
| 341 // DTX and maxplaybackrate should be set after SetSendCodec. Because current | |
| 342 // send codec has to be Opus. | |
| 343 | |
| 344 // Set Opus internal DTX. | |
| 345 LOG(LS_INFO) << "Attempt to " | |
| 346 << (config_.send_codec_spec.enable_opus_dtx ? "enable" | |
| 347 : "disable") | |
| 348 << " Opus DTX on channel " << channel; | |
| 349 if (codec->SetOpusDtx(channel, config_.send_codec_spec.enable_opus_dtx)) { | |
| 350 // TODO(minyue): use normal logging. | |
| 351 // LOG_RTCERR2(SetOpusDtx, channel, | |
| 352 // config_.send_codec_spec.enable_opus_dtx); | |
| 353 return false; | |
| 354 } | |
| 355 | |
| 356 // If opus_max_playback_rate <= 0, the default maximum playback rate | |
| 357 // (48 kHz) will be used. | |
| 358 if (config_.send_codec_spec.opus_max_playback_rate > 0) { | |
| 359 LOG(LS_INFO) << "Attempt to set maximum playback rate to " | |
| 360 << config_.send_codec_spec.opus_max_playback_rate | |
| 361 << " Hz on channel " << channel; | |
| 362 if (codec->SetOpusMaxPlaybackRate( | |
| 363 channel, config_.send_codec_spec.opus_max_playback_rate) == -1) { | |
| 364 // TODO(minyue): use normal logging. | |
| 365 // LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, | |
| 366 // config_.send_codec_spec.opus_max_playback_rate); | |
| 367 return false; | |
| 368 } | |
| 369 } | |
| 370 } | |
| 371 // TODO(solenberg): ApplyMaxSendBitrate() yields another call to | |
| 372 // SetSendCodec(). Check if it is possible to fuse with the previous call | |
| 373 // in this function. | |
| 374 ApplyMaxSendBitrate(); | |
|
minyue-webrtc
2016/10/13 12:48:08
The only reason to place this here is, as far as I
| |
| 375 | |
| 376 // Set the CN payloadtype and the VAD status. | |
| 377 if (config_.send_codec_spec.cng_payload_type != -1) { | |
| 378 // The CN payload type for 8000 Hz clockrate is fixed at 13. | |
| 379 if (config_.send_codec_spec.cng_plfreq != 8000) { | |
| 380 webrtc::PayloadFrequencies cn_freq; | |
| 381 switch (config_.send_codec_spec.cng_plfreq) { | |
| 382 case 16000: | |
| 383 cn_freq = webrtc::kFreq16000Hz; | |
| 384 break; | |
| 385 case 32000: | |
| 386 cn_freq = webrtc::kFreq32000Hz; | |
| 387 break; | |
| 388 default: | |
| 389 RTC_NOTREACHED(); | |
| 390 return false; | |
| 391 } | |
| 392 if (codec->SetSendCNPayloadType(channel, | |
| 393 config_.send_codec_spec.cng_payload_type, | |
| 394 cn_freq) == -1) { | |
| 395 // TODO(minyue): use normal logging. | |
| 396 // LOG_RTCERR3(SetSendCNPayloadType, channel, | |
| 397 // config_.send_codec_spec.cng_payload_type, cn_freq); | |
| 398 | |
| 399 // TODO(ajm): This failure condition will be removed from VoE. | |
| 400 // Restore the return here when we update to a new enough webrtc. | |
| 401 // | |
| 402 // Not returning false because the SetSendCNPayloadType will fail if | |
| 403 // the channel is already sending. | |
| 404 // This can happen if the remote description is applied twice, for | |
| 405 // example in the case of ROAP on top of JSEP, where both side will | |
| 406 // send the offer. | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 // Only turn on VAD if we have a CN payload type that matches the | |
| 411 // clockrate for the codec we are going to use. | |
| 412 if (config_.send_codec_spec.cng_plfreq == | |
| 413 config_.send_codec_spec.codec_inst.plfreq && | |
| 414 config_.send_codec_spec.codec_inst.channels == 1) { | |
| 415 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the | |
| 416 // interaction between VAD and Opus FEC. | |
| 417 LOG(LS_INFO) << "Enabling VAD"; | |
| 418 if (codec->SetVADStatus(channel, true) == -1) { | |
| 419 // TODO(minyue): use normal logging. | |
| 420 // LOG_RTCERR2(SetVADStatus, channel, true); | |
| 421 return false; | |
| 422 } | |
| 423 } | |
| 424 } | |
| 425 return true; | |
| 426 } | |
| 427 | |
| 428 bool AudioSendStream::SetSendCodec(const webrtc::CodecInst& send_codec) { | |
| 429 // TODO(minyue): avoid ToString | |
| 430 // LOG(LS_INFO) << "Send channel " << channel << " selected voice codec " | |
| 431 // << ToString(send_codec) << ", bitrate=" << send_codec.rate; | |
| 432 | |
| 433 ScopedVoEInterface<VoECodec> codec(voice_engine()); | |
| 434 int channel = config_.voe_channel_id; | |
| 435 | |
| 436 webrtc::CodecInst current_codec = {0}; | |
| 437 if (codec->GetSendCodec(channel, current_codec) == 0 && | |
| 438 (send_codec == current_codec)) { | |
| 439 // Codec is already configured, we can return without setting it again. | |
| 440 return true; | |
| 441 } | |
| 442 | |
| 443 if (codec->SetSendCodec(channel, send_codec) == -1) { | |
| 444 // TODO(minyue): use normal logging. | |
| 445 // LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec)); | |
| 446 return false; | |
| 447 } | |
| 448 return true; | |
| 449 } | |
| 450 | |
| 451 bool AudioSendStream::ApplyMaxSendBitrate() { | |
| 452 int bps = config_.max_send_bitrate_bps; | |
| 453 | |
| 454 // Bitrate is auto by default. | |
| 455 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by | |
| 456 // SetMaxSendBandwith(0), the second call removes the previous limit. | |
| 457 if (bps <= 0) { | |
| 458 return true; | |
| 459 } | |
| 460 | |
| 461 if (config_.send_codec_spec.codec_inst.pltype == -1) { | |
| 462 LOG(LS_INFO) << "The send codec has not been set up yet. " | |
| 463 << "The send bitrate setting will be applied later."; | |
| 464 return true; | |
| 465 } | |
| 466 | |
| 467 webrtc::CodecInst codec = config_.send_codec_spec.codec_inst; | |
| 468 bool is_multi_rate = IsCodecMultiRate(codec); | |
| 469 | |
| 470 if (is_multi_rate) { | |
| 471 // If codec is multi-rate then just set the bitrate. | |
| 472 int max_bitrate_bps = MaxBitrateBps(codec); | |
| 473 codec.rate = std::min(bps, max_bitrate_bps); | |
| 474 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps | |
| 475 << " bps."; | |
| 476 if (!SetSendCodec(codec)) { | |
| 477 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate " | |
| 478 << bps << " bps."; | |
| 479 return false; | |
| 480 } | |
| 481 return true; | |
| 482 } else { | |
| 483 // If codec is not multi-rate and |bps| is less than the fixed bitrate | |
| 484 // then fail. If codec is not multi-rate and |bps| exceeds or equal the | |
| 485 // fixed bitrate then ignore. | |
| 486 if (bps < codec.rate) { | |
| 487 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate " | |
| 488 << bps << " bps" | |
| 489 << ", requires at least " << codec.rate << " bps."; | |
| 490 return false; | |
| 491 } | |
| 492 return true; | |
| 493 } | |
| 494 } | |
| 495 | |
| 496 bool AudioSendStream::IsCodecMultiRate(const webrtc::CodecInst& codec) const { | |
| 497 for (size_t i = 0; i < arraysize(config_.codec_prefs); ++i) { | |
| 498 if (IsCodec(codec, config_.codec_prefs[i].name) && | |
| 499 config_.codec_prefs[i].clockrate == codec.plfreq) { | |
| 500 return config_.codec_prefs[i].is_multi_rate; | |
| 501 } | |
| 502 } | |
| 503 return false; | |
| 504 } | |
| 505 | |
| 506 int AudioSendStream::MaxBitrateBps(const webrtc::CodecInst& codec) const { | |
| 507 for (size_t i = 0; i < arraysize(config_.codec_prefs); ++i) { | |
| 508 if (IsCodec(codec, config_.codec_prefs[i].name) && | |
| 509 config_.codec_prefs[i].clockrate == codec.plfreq) { | |
| 510 return config_.codec_prefs[i].max_bitrate_bps; | |
| 511 } | |
| 512 } | |
| 513 return 0; | |
| 514 } | |
| 515 | |
| 288 } // namespace internal | 516 } // namespace internal |
| 289 } // namespace webrtc | 517 } // namespace webrtc |
| OLD | NEW |