Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(672)

Side by Side Diff: webrtc/media/engine/webrtcvoiceengine.cc

Issue 1765873002: On WVoMC::SetSendParameters(), figure out send codec settings ONCE, not for each send stream. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: build warning+rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/media/engine/webrtcvoiceengine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2004 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
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms; 373 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
374 return true; 374 return true;
375 } 375 }
376 } 376 }
377 } 377 }
378 return false; 378 return false;
379 } 379 }
380 380
381 static const AudioCodec* GetPreferredCodec( 381 static const AudioCodec* GetPreferredCodec(
382 const std::vector<AudioCodec>& codecs, 382 const std::vector<AudioCodec>& codecs,
383 webrtc::CodecInst* voe_codec, 383 webrtc::CodecInst* out,
384 int* red_payload_type) { 384 int* red_payload_type) {
385 RTC_DCHECK(voe_codec); 385 RTC_DCHECK(out);
386 RTC_DCHECK(red_payload_type); 386 RTC_DCHECK(red_payload_type);
387 // Select the preferred send codec (the first non-telephone-event/CN codec). 387 // Select the preferred send codec (the first non-telephone-event/CN codec).
388 for (const AudioCodec& codec : codecs) { 388 for (const AudioCodec& codec : codecs) {
389 *red_payload_type = -1; 389 *red_payload_type = -1;
390 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) { 390 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
391 // Skip telephone-event/CN codec, which will be handled later. 391 // Skip telephone-event/CN codec, which will be handled later.
392 continue; 392 continue;
393 } 393 }
394 394
395 // We'll use the first codec in the list to actually send audio data. 395 // We'll use the first codec in the list to actually send audio data.
396 // Be sure to use the payload type requested by the remote side. 396 // Be sure to use the payload type requested by the remote side.
397 // "red", for RED audio, is a special case where the actual codec to be 397 // "red", for RED audio, is a special case where the actual codec to be
398 // used is specified in params. 398 // used is specified in params.
399 const AudioCodec* found_codec = &codec; 399 const AudioCodec* found_codec = &codec;
400 if (IsCodec(*found_codec, kRedCodecName)) { 400 if (IsCodec(*found_codec, kRedCodecName)) {
401 // Parse out the RED parameters. If we fail, just ignore RED; 401 // Parse out the RED parameters. If we fail, just ignore RED;
402 // we don't support all possible params/usage scenarios. 402 // we don't support all possible params/usage scenarios.
403 *red_payload_type = codec.id; 403 *red_payload_type = codec.id;
404 found_codec = GetRedSendCodec(*found_codec, codecs); 404 found_codec = GetRedSendCodec(*found_codec, codecs);
405 if (!found_codec) { 405 if (!found_codec) {
406 continue; 406 continue;
407 } 407 }
408 } 408 }
409 // Ignore codecs we don't know about. The negotiation step should prevent 409 // Ignore codecs we don't know about. The negotiation step should prevent
410 // this, but double-check to be sure. 410 // this, but double-check to be sure.
411 if (!ToCodecInst(*found_codec, voe_codec)) { 411 webrtc::CodecInst voe_codec = {0};
412 if (!ToCodecInst(*found_codec, &voe_codec)) {
kwiberg-webrtc 2016/03/05 02:29:45 Zeroing the output argument before calling ToCodec
the sun 2016/03/05 08:55:33 Well, I actually changed that in response to one o
412 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec); 413 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
413 continue; 414 continue;
414 } 415 }
416 *out = voe_codec;
415 return found_codec; 417 return found_codec;
416 } 418 }
417 return nullptr; 419 return nullptr;
418 } 420 }
419 421
420 private: 422 private:
421 static const int kMaxNumPacketSize = 6; 423 static const int kMaxNumPacketSize = 6;
422 struct CodecPref { 424 struct CodecPref {
423 const char* name; 425 const char* name;
424 int clockrate; 426 int clockrate;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!"; 561 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
560 } else { 562 } else {
561 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed"; 563 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
562 Terminate(); 564 Terminate();
563 } 565 }
564 return res; 566 return res;
565 } 567 }
566 568
567 bool WebRtcVoiceEngine::InitInternal() { 569 bool WebRtcVoiceEngine::InitInternal() {
568 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 570 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
569 // Temporarily turn logging level up for the Init call 571 // Temporarily turn logging level up for the Init call.
kwiberg-webrtc 2016/03/05 02:29:45 Fixing spelling errors in lines that your CL is no
the sun 2016/03/05 08:55:33 Acknowledged. Should have kept my fingers in check
570 webrtc::Trace::set_level_filter(kElevatedTraceFilter); 572 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
571 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString(); 573 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
572 if (voe_wrapper_->base()->Init(adm_) == -1) { 574 if (voe_wrapper_->base()->Init(adm_) == -1) {
573 LOG_RTCERR0_EX(Init, voe_wrapper_->error()); 575 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
574 return false; 576 return false;
575 } 577 }
576 webrtc::Trace::set_level_filter(kDefaultTraceFilter); 578 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
577 579
578 // Save the default AGC configuration settings. This must happen before 580 // Save the default AGC configuration settings. This must happen before
579 // calling ApplyOptions or the default will be overwritten. 581 // calling ApplyOptions or the default will be overwritten.
(...skipping 16 matching lines...) Expand all
596 options.adjust_agc_delta = rtc::Optional<int>(0); 598 options.adjust_agc_delta = rtc::Optional<int>(0);
597 options.experimental_agc = rtc::Optional<bool>(false); 599 options.experimental_agc = rtc::Optional<bool>(false);
598 options.extended_filter_aec = rtc::Optional<bool>(false); 600 options.extended_filter_aec = rtc::Optional<bool>(false);
599 options.delay_agnostic_aec = rtc::Optional<bool>(false); 601 options.delay_agnostic_aec = rtc::Optional<bool>(false);
600 options.experimental_ns = rtc::Optional<bool>(false); 602 options.experimental_ns = rtc::Optional<bool>(false);
601 if (!ApplyOptions(options)) { 603 if (!ApplyOptions(options)) {
602 return false; 604 return false;
603 } 605 }
604 } 606 }
605 607
606 // Print our codec list again for the call diagnostic log 608 // Print our codec list again for the call diagnostic log.
607 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:"; 609 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
608 for (const AudioCodec& codec : codecs_) { 610 for (const AudioCodec& codec : codecs_) {
609 LOG(LS_INFO) << ToString(codec); 611 LOG(LS_INFO) << ToString(codec);
610 } 612 }
611 613
612 SetDefaultDevices(); 614 SetDefaultDevices();
613 615
614 initialized_ = true; 616 initialized_ = true;
615 return true; 617 return true;
616 } 618 }
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 rtc::LoggingSeverity sev = rtc::LS_VERBOSE; 998 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
997 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical) 999 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
998 sev = rtc::LS_ERROR; 1000 sev = rtc::LS_ERROR;
999 else if (level == webrtc::kTraceWarning) 1001 else if (level == webrtc::kTraceWarning)
1000 sev = rtc::LS_WARNING; 1002 sev = rtc::LS_WARNING;
1001 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo) 1003 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
1002 sev = rtc::LS_INFO; 1004 sev = rtc::LS_INFO;
1003 else if (level == webrtc::kTraceTerseInfo) 1005 else if (level == webrtc::kTraceTerseInfo)
1004 sev = rtc::LS_INFO; 1006 sev = rtc::LS_INFO;
1005 1007
1006 // Skip past boilerplate prefix text 1008 // Skip past boilerplate prefix text.
1007 if (length < 72) { 1009 if (length < 72) {
1008 std::string msg(trace, length); 1010 std::string msg(trace, length);
1009 LOG(LS_ERROR) << "Malformed webrtc log message: "; 1011 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1010 LOG_V(sev) << msg; 1012 LOG_V(sev) << msg;
1011 } else { 1013 } else {
1012 std::string msg(trace + 71, length - 72); 1014 std::string msg(trace + 71, length - 72);
1013 LOG_V(sev) << "webrtc: " << msg; 1015 LOG_V(sev) << "webrtc: " << msg;
1014 } 1016 }
1015 } 1017 }
1016 1018
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
1482 } 1484 }
1483 1485
1484 if (playout_) { 1486 if (playout_) {
1485 // Receive codecs can not be changed while playing. So we temporarily 1487 // Receive codecs can not be changed while playing. So we temporarily
1486 // pause playout. 1488 // pause playout.
1487 PausePlayout(); 1489 PausePlayout();
1488 } 1490 }
1489 1491
1490 bool result = true; 1492 bool result = true;
1491 for (const AudioCodec& codec : new_codecs) { 1493 for (const AudioCodec& codec : new_codecs) {
1492 webrtc::CodecInst voe_codec; 1494 webrtc::CodecInst voe_codec = {0};
1493 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) { 1495 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1494 LOG(LS_INFO) << ToString(codec); 1496 LOG(LS_INFO) << ToString(codec);
1495 voe_codec.pltype = codec.id; 1497 voe_codec.pltype = codec.id;
1496 for (const auto& ch : recv_streams_) { 1498 for (const auto& ch : recv_streams_) {
1497 if (engine()->voe()->codec()->SetRecPayloadType( 1499 if (engine()->voe()->codec()->SetRecPayloadType(
1498 ch.second->channel(), voe_codec) == -1) { 1500 ch.second->channel(), voe_codec) == -1) {
1499 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(), 1501 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1500 ToString(voe_codec)); 1502 ToString(voe_codec));
1501 result = false; 1503 result = false;
1502 } 1504 }
1503 } 1505 }
1504 } else { 1506 } else {
1505 LOG(LS_WARNING) << "Unknown codec " << ToString(codec); 1507 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1506 result = false; 1508 result = false;
1507 break; 1509 break;
1508 } 1510 }
1509 } 1511 }
1510 if (result) { 1512 if (result) {
1511 recv_codecs_ = codecs; 1513 recv_codecs_ = codecs;
1512 } 1514 }
1513 1515
1514 if (desired_playout_ && !playout_) { 1516 if (desired_playout_ && !playout_) {
1515 ResumePlayout(); 1517 ResumePlayout();
1516 } 1518 }
1517 return result; 1519 return result;
1518 } 1520 }
1519 1521
kwiberg-webrtc 2016/03/05 02:29:45 These methods don't have any docs that explain wha
the sun 2016/03/07 09:02:34 Done.
1520 bool WebRtcVoiceMediaChannel::SetSendCodecs( 1522 bool WebRtcVoiceMediaChannel::SetSendCodecs(
1521 int channel, const std::vector<AudioCodec>& codecs) { 1523 const std::vector<AudioCodec>& codecs) {
1524 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1525 // TODO(solenberg): Validate input - that payload types don't overlap, are
1526 // within range, filter out codecs we don't support,
1527 // redundant codecs etc.
1528
1529 // Find the DTMF telephone event "codec" payload type.
1530 dtmf_payload_type_ = rtc::Optional<int>();
1531 for (const AudioCodec& codec : codecs) {
1532 if (IsCodec(codec, kDtmfCodecName)) {
1533 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1534 break;
1535 }
1536 }
1537
1538 // Scan through the list to figure out the codec to use for sending, along
1539 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1540 // parameters.
1541 {
1542 SendCodecSpec send_codec_spec;
1543 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1544
1545 // Find send codec (the first non-telephone-event/CN codec).
1546 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1547 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1548 if (!codec) {
1549 LOG(LS_WARNING) << "Received empty list of codecs.";
1550 return false;
1551 }
1552
1553 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1554
1555 // TODO(solenberg): This is a curious condition - kept for legacy reasons -
1556 // why would we only want to figure out these parameters
1557 // when RED is disabled?
1558 if (send_codec_spec.red_payload_type == -1) {
1559 send_codec_spec.nack_enabled = HasNack(*codec);
1560 // For Opus as the send codec, we are to determine inband FEC, maximum
1561 // playback rate, and opus internal dtx.
1562 if (IsCodec(*codec, kOpusCodecName)) {
1563 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1564 &send_codec_spec.enable_codec_fec,
1565 &send_codec_spec.opus_max_playback_rate,
1566 &send_codec_spec.enable_opus_dtx);
1567 }
1568
1569 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1570 int ptime_ms = 0;
1571 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1572 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1573 &send_codec_spec.codec_inst, ptime_ms)) {
1574 LOG(LS_WARNING) << "Failed to set packet size for codec "
1575 << send_codec_spec.codec_inst.plname;
1576 return false;
1577 }
1578 }
1579 }
1580
1581 // Loop through the codecs list again to find the CN codec.
1582 for (const AudioCodec& codec : codecs) {
1583 // Ignore codecs we don't know about. The negotiation step should prevent
1584 // this, but double-check to be sure.
1585 webrtc::CodecInst voe_codec = {0};
1586 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1587 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1588 continue;
1589 }
1590
1591 if (IsCodec(codec, kCnCodecName)) {
1592 // Turn voice activity detection/comfort noise on if supported.
1593 // Set the wideband CN payload type appropriately.
1594 // (narrowband always uses the static payload type 13).
1595 int cng_plfreq = -1;
1596 switch (codec.clockrate) {
1597 case 8000:
1598 case 16000:
1599 case 32000:
1600 cng_plfreq = codec.clockrate;
1601 break;
1602 default:
1603 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1604 << " not supported.";
1605 continue;
kwiberg-webrtc 2016/03/05 02:29:45 Hmm. Not changed by this CL, but don't we support
the sun 2016/03/05 08:55:33 It's not in the list of preferred codecs. Do we su
1606 }
1607 send_codec_spec.cng_payload_type = codec.id;
1608 send_codec_spec.cng_plfreq = cng_plfreq;
1609 break;
1610 }
1611 }
1612
1613 send_codec_spec_ = std::move(send_codec_spec);
1614 }
1615
1616 // Cache the codecs in order to configure the channel created later.
1617 for (const auto& ch : send_streams_) {
1618 if (!SetSendCodecs(ch.second->channel())) {
1619 return false;
1620 }
1621 }
1622
1623 // Set nack status on receive channels and update |nack_enabled_|.
1624 if (!send_streams_.empty()) {
1625 for (const auto& kv : recv_streams_) {
1626 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
1627 }
1628 }
1629
1630 // Check if the transport cc feedback has changed on the preferred send codec,
1631 // and in that case reconfigure all receive streams.
1632 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1633 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1634 "codec has changed.";
1635 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1636 for (auto& kv : recv_streams_) {
1637 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1638 }
1639 }
1640
1641 return true;
1642 }
1643
1644 bool WebRtcVoiceMediaChannel::SetSendCodecs(int channel) {
1522 // Disable VAD, FEC, and RED unless we know the other side wants them. 1645 // Disable VAD, FEC, and RED unless we know the other side wants them.
1523 engine()->voe()->codec()->SetVADStatus(channel, false); 1646 engine()->voe()->codec()->SetVADStatus(channel, false);
1524 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0); 1647 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1525 engine()->voe()->rtp()->SetREDStatus(channel, false); 1648 engine()->voe()->rtp()->SetREDStatus(channel, false);
1526 engine()->voe()->codec()->SetFECStatus(channel, false); 1649 engine()->voe()->codec()->SetFECStatus(channel, false);
1527 1650
1528 // Scan through the list to figure out the codec to use for sending, along 1651 if (send_codec_spec_.red_payload_type != -1) {
1529 // with the proper configuration for VAD. 1652 // Enable redundant encoding of the specified codec. Treat any
1530 webrtc::CodecInst send_codec; 1653 // failure as a fatal internal error.
1531 memset(&send_codec, 0, sizeof(send_codec)); 1654 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1532 1655 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1533 bool nack_enabled = nack_enabled_; 1656 send_codec_spec_.red_payload_type) == -1) {
1534 bool enable_codec_fec = false; 1657 LOG_RTCERR3(SetREDStatus, channel, true,
1535 bool enable_opus_dtx = false; 1658 send_codec_spec_.red_payload_type);
1536 int opus_max_playback_rate = 0; 1659 return false;
1537 int red_payload_type = -1;
1538
1539 // Set send codec (the first non-telephone-event/CN codec)
1540 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1541 codecs, &send_codec, &red_payload_type);
1542 if (codec) {
1543 if (red_payload_type != -1) {
1544 // Enable redundant encoding of the specified codec. Treat any
1545 // failure as a fatal internal error.
1546 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1547 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1548 red_payload_type) == -1) {
1549 LOG_RTCERR3(SetREDStatus, channel, true, red_payload_type);
1550 return false;
1551 }
1552 } else {
1553 nack_enabled = HasNack(*codec);
1554 // For Opus as the send codec, we are to determine inband FEC, maximum
1555 // playback rate, and opus internal dtx.
1556 if (IsCodec(*codec, kOpusCodecName)) {
1557 GetOpusConfig(*codec, &send_codec, &enable_codec_fec,
1558 &opus_max_playback_rate, &enable_opus_dtx);
1559 }
1560
1561 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1562 int ptime_ms = 0;
1563 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1564 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
1565 LOG(LS_WARNING) << "Failed to set packet size for codec "
1566 << send_codec.plname;
1567 return false;
1568 }
1569 }
1570 } 1660 }
1571 } 1661 }
1572 1662
1573 if (nack_enabled_ != nack_enabled) { 1663 SetNack(channel, send_codec_spec_.nack_enabled);
1574 SetNack(channel, nack_enabled); 1664
1575 nack_enabled_ = nack_enabled; 1665 // Set the codec immediately, since SetVADStatus() depends on whether
1576 } 1666 // the current codec is mono or stereo.
1577 if (!codec) { 1667 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1578 LOG(LS_WARNING) << "Received empty list of codecs.";
1579 return false; 1668 return false;
1580 } 1669 }
1581 1670
1582 // Set the codec immediately, since SetVADStatus() depends on whether
1583 // the current codec is mono or stereo.
1584 if (!SetSendCodec(channel, send_codec))
1585 return false;
1586
1587 // FEC should be enabled after SetSendCodec. 1671 // FEC should be enabled after SetSendCodec.
1588 if (enable_codec_fec) { 1672 if (send_codec_spec_.enable_codec_fec) {
1589 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel " 1673 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1590 << channel; 1674 << channel;
1591 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) { 1675 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1592 // Enable codec internal FEC. Treat any failure as fatal internal error. 1676 // Enable codec internal FEC. Treat any failure as fatal internal error.
1593 LOG_RTCERR2(SetFECStatus, channel, true); 1677 LOG_RTCERR2(SetFECStatus, channel, true);
1594 return false; 1678 return false;
1595 } 1679 }
1596 } 1680 }
1597 1681
1598 if (IsCodec(send_codec, kOpusCodecName)) { 1682 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1599 // DTX and maxplaybackrate should be set after SetSendCodec. Because current 1683 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1600 // send codec has to be Opus. 1684 // send codec has to be Opus.
1601 1685
1602 // Set Opus internal DTX. 1686 // Set Opus internal DTX.
1603 LOG(LS_INFO) << "Attempt to " 1687 LOG(LS_INFO) << "Attempt to "
1604 << (enable_opus_dtx ? "enable" : "disable") 1688 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1605 << " Opus DTX on channel " 1689 << " Opus DTX on channel "
1606 << channel; 1690 << channel;
1607 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) { 1691 if (engine()->voe()->codec()->SetOpusDtx(channel,
1608 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx); 1692 send_codec_spec_.enable_opus_dtx)) {
1693 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1609 return false; 1694 return false;
1610 } 1695 }
1611 1696
1612 // If opus_max_playback_rate <= 0, the default maximum playback rate 1697 // If opus_max_playback_rate <= 0, the default maximum playback rate
1613 // (48 kHz) will be used. 1698 // (48 kHz) will be used.
1614 if (opus_max_playback_rate > 0) { 1699 if (send_codec_spec_.opus_max_playback_rate > 0) {
1615 LOG(LS_INFO) << "Attempt to set maximum playback rate to " 1700 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1616 << opus_max_playback_rate 1701 << send_codec_spec_.opus_max_playback_rate
1617 << " Hz on channel " 1702 << " Hz on channel "
1618 << channel; 1703 << channel;
1619 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate( 1704 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1620 channel, opus_max_playback_rate) == -1) { 1705 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1621 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate); 1706 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1707 send_codec_spec_.opus_max_playback_rate);
1622 return false; 1708 return false;
1623 } 1709 }
1624 } 1710 }
1625 } 1711 }
1626 1712
1627 // Always update the |send_codec_| to the currently set send codec.
1628 send_codec_.reset(new webrtc::CodecInst(send_codec));
1629
1630 if (send_bitrate_setting_) { 1713 if (send_bitrate_setting_) {
1631 SetSendBitrateInternal(send_bitrate_bps_); 1714 SetSendBitrateInternal(send_bitrate_bps_);
1632 } 1715 }
1633 1716
1634 // Loop through the codecs list again to config the CN codec. 1717 // Set the CN payloadtype and the VAD status.
1635 for (const AudioCodec& codec : codecs) { 1718 if (send_codec_spec_.cng_payload_type != -1) {
1636 // Ignore codecs we don't know about. The negotiation step should prevent 1719 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1637 // this, but double-check to be sure. 1720 if (send_codec_spec_.cng_plfreq != 8000) {
1638 webrtc::CodecInst voe_codec;
1639 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1640 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1641 continue;
1642 }
1643
1644 if (IsCodec(codec, kCnCodecName)) {
1645 // Turn voice activity detection/comfort noise on if supported.
1646 // Set the wideband CN payload type appropriately.
1647 // (narrowband always uses the static payload type 13).
1648 webrtc::PayloadFrequencies cn_freq; 1721 webrtc::PayloadFrequencies cn_freq;
1649 switch (codec.clockrate) { 1722 switch (send_codec_spec_.cng_plfreq) {
1650 case 8000:
1651 cn_freq = webrtc::kFreq8000Hz;
1652 break;
1653 case 16000: 1723 case 16000:
1654 cn_freq = webrtc::kFreq16000Hz; 1724 cn_freq = webrtc::kFreq16000Hz;
1655 break; 1725 break;
1656 case 32000: 1726 case 32000:
1657 cn_freq = webrtc::kFreq32000Hz; 1727 cn_freq = webrtc::kFreq32000Hz;
1658 break; 1728 break;
1659 default: 1729 default:
1660 LOG(LS_WARNING) << "CN frequency " << codec.clockrate 1730 RTC_NOTREACHED();
1661 << " not supported."; 1731 return false;
1662 continue;
1663 } 1732 }
1664 // Set the CN payloadtype and the VAD status. 1733 if (engine()->voe()->codec()->SetSendCNPayloadType(
1665 // The CN payload type for 8000 Hz clockrate is fixed at 13. 1734 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1666 if (cn_freq != webrtc::kFreq8000Hz) { 1735 LOG_RTCERR3(SetSendCNPayloadType, channel,
1667 if (engine()->voe()->codec()->SetSendCNPayloadType( 1736 send_codec_spec_.cng_payload_type, cn_freq);
1668 channel, codec.id, cn_freq) == -1) { 1737 // TODO(ajm): This failure condition will be removed from VoE.
1669 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq); 1738 // Restore the return here when we update to a new enough webrtc.
1670 // TODO(ajm): This failure condition will be removed from VoE. 1739 //
1671 // Restore the return here when we update to a new enough webrtc. 1740 // Not returning false because the SetSendCNPayloadType will fail if
1672 // 1741 // the channel is already sending.
1673 // Not returning false because the SetSendCNPayloadType will fail if 1742 // This can happen if the remote description is applied twice, for
1674 // the channel is already sending. 1743 // example in the case of ROAP on top of JSEP, where both side will
1675 // This can happen if the remote description is applied twice, for 1744 // send the offer.
1676 // example in the case of ROAP on top of JSEP, where both side will
1677 // send the offer.
1678 }
1679 } 1745 }
1680 // Only turn on VAD if we have a CN payload type that matches the 1746 }
1681 // clockrate for the codec we are going to use. 1747
1682 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) { 1748 // Only turn on VAD if we have a CN payload type that matches the
1683 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the 1749 // clockrate for the codec we are going to use.
1684 // interaction between VAD and Opus FEC. 1750 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1685 LOG(LS_INFO) << "Enabling VAD"; 1751 send_codec_spec_.codec_inst.channels == 1) {
1686 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) { 1752 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1687 LOG_RTCERR2(SetVADStatus, channel, true); 1753 // interaction between VAD and Opus FEC.
1688 return false; 1754 LOG(LS_INFO) << "Enabling VAD";
1689 } 1755 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1756 LOG_RTCERR2(SetVADStatus, channel, true);
1757 return false;
1690 } 1758 }
1691 } 1759 }
1692 } 1760 }
1693 return true; 1761 return true;
1694 } 1762 }
1695 1763
1696 bool WebRtcVoiceMediaChannel::SetSendCodecs(
1697 const std::vector<AudioCodec>& codecs) {
1698 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1699 // TODO(solenberg): Validate input - that payload types don't overlap, are
1700 // within range, filter out codecs we don't support,
1701 // redundant codecs etc.
1702
1703 // Find the DTMF telephone event "codec" payload type.
1704 dtmf_payload_type_ = rtc::Optional<int>();
1705 for (const AudioCodec& codec : codecs) {
1706 if (IsCodec(codec, kDtmfCodecName)) {
1707 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1708 break;
1709 }
1710 }
1711
1712 // Cache the codecs in order to configure the channel created later.
1713 send_codecs_ = codecs;
1714 for (const auto& ch : send_streams_) {
1715 if (!SetSendCodecs(ch.second->channel(), codecs)) {
1716 return false;
1717 }
1718 }
1719
1720 // Set nack status on receive channels and update |nack_enabled_|.
1721 for (const auto& ch : recv_streams_) {
1722 SetNack(ch.second->channel(), nack_enabled_);
1723 }
1724
1725 // Check if the transport cc feedback has changed on the preferred send codec,
1726 // and in that case reconfigure all receive streams.
1727 webrtc::CodecInst voe_codec;
1728 int red_payload_type;
1729 const AudioCodec* send_codec = WebRtcVoiceCodecs::GetPreferredCodec(
1730 send_codecs_, &voe_codec, &red_payload_type);
1731 if (send_codec) {
1732 bool transport_cc = HasTransportCc(*send_codec);
1733 if (transport_cc_enabled_ != transport_cc) {
1734 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1735 "codec has changed.";
1736 transport_cc_enabled_ = transport_cc;
1737 for (auto& kv : recv_streams_) {
1738 RTC_DCHECK(kv.second != nullptr);
1739 kv.second->RecreateAudioReceiveStream(transport_cc_enabled_);
1740 }
1741 }
1742 }
1743
1744 return true;
1745 }
1746
1747 void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) { 1764 void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
1748 if (nack_enabled) { 1765 if (nack_enabled) {
1749 LOG(LS_INFO) << "Enabling NACK for channel " << channel; 1766 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
1750 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets); 1767 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1751 } else { 1768 } else {
1752 LOG(LS_INFO) << "Disabling NACK for channel " << channel; 1769 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
1753 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0); 1770 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1754 } 1771 }
1755 } 1772 }
1756 1773
1757 bool WebRtcVoiceMediaChannel::SetSendCodec( 1774 bool WebRtcVoiceMediaChannel::SetSendCodec(
1758 int channel, const webrtc::CodecInst& send_codec) { 1775 int channel, const webrtc::CodecInst& send_codec) {
1759 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec " 1776 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1760 << ToString(send_codec) << ", bitrate=" << send_codec.rate; 1777 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1761 1778
1762 webrtc::CodecInst current_codec; 1779 webrtc::CodecInst current_codec = {0};
1763 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 && 1780 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1764 (send_codec == current_codec)) { 1781 (send_codec == current_codec)) {
1765 // Codec is already configured, we can return without setting it again. 1782 // Codec is already configured, we can return without setting it again.
1766 return true; 1783 return true;
1767 } 1784 }
1768 1785
1769 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) { 1786 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1770 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec)); 1787 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
1771 return false; 1788 return false;
1772 } 1789 }
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
1922 // Save the channel to send_streams_, so that RemoveSendStream() can still 1939 // Save the channel to send_streams_, so that RemoveSendStream() can still
1923 // delete the channel in case failure happens below. 1940 // delete the channel in case failure happens below.
1924 webrtc::AudioTransport* audio_transport = 1941 webrtc::AudioTransport* audio_transport =
1925 engine()->voe()->base()->audio_transport(); 1942 engine()->voe()->base()->audio_transport();
1926 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream( 1943 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1927 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_))); 1944 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
1928 1945
1929 // Set the current codecs to be used for the new channel. We need to do this 1946 // Set the current codecs to be used for the new channel. We need to do this
1930 // after adding the channel to send_channels_, because of how max bitrate is 1947 // after adding the channel to send_channels_, because of how max bitrate is
1931 // currently being configured by SetSendCodec(). 1948 // currently being configured by SetSendCodec().
1932 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) { 1949 if (HasSendCodec() && !SetSendCodecs(channel)) {
1933 RemoveSendStream(ssrc); 1950 RemoveSendStream(ssrc);
1934 return false; 1951 return false;
1935 } 1952 }
1936 1953
1937 // At this point the channel's local SSRC has been updated. If the channel is 1954 // At this point the channel's local SSRC has been updated. If the channel is
1938 // the first send channel make sure that all the receive channels are updated 1955 // the first send channel make sure that all the receive channels are updated
1939 // with the same SSRC in order to send receiver reports. 1956 // with the same SSRC in order to send receiver reports.
1940 if (send_streams_.size() == 1) { 1957 if (send_streams_.size() == 1) {
1941 receiver_reports_ssrc_ = ssrc; 1958 receiver_reports_ssrc_ = ssrc;
1942 for (const auto& stream : recv_streams_) { 1959 for (const auto& stream : recv_streams_) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
2019 voe_codec.pltype = -1; 2036 voe_codec.pltype = -1;
2020 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) { 2037 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2021 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec)); 2038 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2022 DeleteVoEChannel(channel); 2039 DeleteVoEChannel(channel);
2023 return false; 2040 return false;
2024 } 2041 }
2025 } 2042 }
2026 2043
2027 // Only enable those configured for this channel. 2044 // Only enable those configured for this channel.
2028 for (const auto& codec : recv_codecs_) { 2045 for (const auto& codec : recv_codecs_) {
2029 webrtc::CodecInst voe_codec; 2046 webrtc::CodecInst voe_codec = {0};
2030 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) { 2047 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
2031 voe_codec.pltype = codec.id; 2048 voe_codec.pltype = codec.id;
2032 if (engine()->voe()->codec()->SetRecPayloadType( 2049 if (engine()->voe()->codec()->SetRecPayloadType(
2033 channel, voe_codec) == -1) { 2050 channel, voe_codec) == -1) {
2034 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec)); 2051 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2035 DeleteVoEChannel(channel); 2052 DeleteVoEChannel(channel);
2036 return false; 2053 return false;
2037 } 2054 }
2038 } 2055 }
2039 } 2056 }
2040 2057
2041 const int send_channel = GetSendChannelId(receiver_reports_ssrc_); 2058 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2042 if (send_channel != -1) { 2059 if (send_channel != -1) {
2043 // Associate receive channel with first send channel (so the receive channel 2060 // Associate receive channel with first send channel (so the receive channel
2044 // can obtain RTT from the send channel) 2061 // can obtain RTT from the send channel)
2045 engine()->voe()->base()->AssociateSendChannel(channel, send_channel); 2062 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2046 LOG(LS_INFO) << "VoiceEngine channel #" << channel 2063 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2047 << " is associated with channel #" << send_channel << "."; 2064 << " is associated with channel #" << send_channel << ".";
2048 } 2065 }
2049 2066
2050 transport_cc_enabled_ =
2051 !send_codecs_.empty() ? HasTransportCc(send_codecs_[0]) : false;
2052
2053 recv_streams_.insert(std::make_pair( 2067 recv_streams_.insert(std::make_pair(
2054 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_, 2068 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
2055 transport_cc_enabled_, sp.sync_label, 2069 recv_transport_cc_enabled_,
2056 recv_rtp_extensions_, call_))); 2070 sp.sync_label, recv_rtp_extensions_,
2071 call_)));
2057 2072
2058 SetNack(channel, nack_enabled_); 2073 SetNack(channel, send_codec_spec_.nack_enabled);
2059 SetPlayout(channel, playout_); 2074 SetPlayout(channel, playout_);
2060 2075
2061 return true; 2076 return true;
2062 } 2077 }
2063 2078
2064 bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) { 2079 bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
2065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 2080 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2066 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; 2081 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2067 2082
2068 const auto it = recv_streams_.find(ssrc); 2083 const auto it = recv_streams_.find(ssrc);
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
2352 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth."; 2367 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
2353 return SetSendBitrateInternal(bps); 2368 return SetSendBitrateInternal(bps);
2354 } 2369 }
2355 2370
2356 bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) { 2371 bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2357 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal."; 2372 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
2358 2373
2359 send_bitrate_setting_ = true; 2374 send_bitrate_setting_ = true;
2360 send_bitrate_bps_ = bps; 2375 send_bitrate_bps_ = bps;
2361 2376
2362 if (!send_codec_) { 2377 if (!HasSendCodec()) {
2363 LOG(LS_INFO) << "The send codec has not been set up yet. " 2378 LOG(LS_INFO) << "The send codec has not been set up yet. "
2364 << "The send bitrate setting will be applied later."; 2379 << "The send bitrate setting will be applied later.";
2365 return true; 2380 return true;
2366 } 2381 }
2367 2382
2368 // Bitrate is auto by default. 2383 // Bitrate is auto by default.
2369 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by 2384 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2370 // SetMaxSendBandwith(0), the second call removes the previous limit. 2385 // SetMaxSendBandwith(0), the second call removes the previous limit.
2371 if (bps <= 0) 2386 if (bps <= 0)
2372 return true; 2387 return true;
2373 2388
2374 webrtc::CodecInst codec = *send_codec_; 2389 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
2375 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec); 2390 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
2376 2391
2377 if (is_multi_rate) { 2392 if (is_multi_rate) {
2378 // If codec is multi-rate then just set the bitrate. 2393 // If codec is multi-rate then just set the bitrate.
2379 codec.rate = bps; 2394 codec.rate = bps;
2380 for (const auto& ch : send_streams_) { 2395 for (const auto& ch : send_streams_) {
2381 if (!SetSendCodec(ch.second->channel(), codec)) { 2396 if (!SetSendCodec(ch.second->channel(), codec)) {
2382 LOG(LS_INFO) << "Failed to set codec " << codec.plname 2397 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2383 << " to bitrate " << bps << " bps."; 2398 << " to bitrate " << bps << " bps.";
2384 return false; 2399 return false;
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
2520 } 2535 }
2521 } else { 2536 } else {
2522 LOG(LS_INFO) << "Stopping playout for channel #" << channel; 2537 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2523 engine()->voe()->base()->StopPlayout(channel); 2538 engine()->voe()->base()->StopPlayout(channel);
2524 } 2539 }
2525 return true; 2540 return true;
2526 } 2541 }
2527 } // namespace cricket 2542 } // namespace cricket
2528 2543
2529 #endif // HAVE_WEBRTC_VOICE 2544 #endif // HAVE_WEBRTC_VOICE
OLDNEW
« no previous file with comments | « webrtc/media/engine/webrtcvoiceengine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698