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

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

Issue 2405183002: Moving WebRtcVoiceMediaChannel::SendSetCodec to AudioSendStream. (Closed)
Patch Set: final change Created 4 years, 2 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
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 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 // G722 should be advertised as 8000 Hz because of the RFC "bug". 457 // G722 should be advertised as 8000 Hz because of the RFC "bug".
458 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}}, 458 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}},
459 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}}, 459 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}},
460 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}}, 460 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}},
461 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}}, 461 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}},
462 {kCnCodecName, 32000, 1, 106, false, {}}, 462 {kCnCodecName, 32000, 1, 106, false, {}},
463 {kCnCodecName, 16000, 1, 105, false, {}}, 463 {kCnCodecName, 16000, 1, 105, false, {}},
464 {kCnCodecName, 8000, 1, 13, false, {}}, 464 {kCnCodecName, 8000, 1, 13, false, {}},
465 {kDtmfCodecName, 8000, 1, 126, false, {}} 465 {kDtmfCodecName, 8000, 1, 126, false, {}}
466 }; 466 };
467 } // namespace {
468 467
469 bool SendCodecSpec::operator==(const SendCodecSpec& rhs) const { 468 rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
470 if (nack_enabled != rhs.nack_enabled) { 469 int rtp_max_bitrate_bps,
471 return false; 470 const webrtc::CodecInst& codec_inst) {
471 const int bps = MinPositive(max_send_bitrate_bps, rtp_max_bitrate_bps);
472 const int codec_rate = codec_inst.rate;
473
474 if (bps <= 0) {
475 return rtc::Optional<int>(codec_rate);
472 } 476 }
473 if (transport_cc_enabled != rhs.transport_cc_enabled) { 477
474 return false; 478 if (codec_inst.pltype == -1) {
479 return rtc::Optional<int>(codec_rate);
480 ;
475 } 481 }
476 if (enable_codec_fec != rhs.enable_codec_fec) { 482
477 return false; 483 if (WebRtcVoiceCodecs::IsCodecMultiRate(codec_inst)) {
484 // If codec is multi-rate then just set the bitrate.
485 return rtc::Optional<int>(
486 std::min(bps, WebRtcVoiceCodecs::MaxBitrateBps(codec_inst)));
478 } 487 }
479 if (enable_opus_dtx != rhs.enable_opus_dtx) { 488
480 return false; 489 if (bps < codec_inst.rate) {
490 // If codec is not multi-rate and |bps| is less than the fixed bitrate then
491 // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
492 // bitrate then ignore.
493 LOG(LS_ERROR) << "Failed to set codec " << codec_inst.plname
494 << " to bitrate " << bps << " bps"
495 << ", requires at least " << codec_inst.rate << " bps.";
496 return rtc::Optional<int>();
481 } 497 }
482 if (opus_max_playback_rate != rhs.opus_max_playback_rate) { 498 return rtc::Optional<int>(codec_rate);
483 return false;
484 }
485 if (red_payload_type != rhs.red_payload_type) {
486 return false;
487 }
488 if (cng_payload_type != rhs.cng_payload_type) {
489 return false;
490 }
491 if (cng_plfreq != rhs.cng_plfreq) {
492 return false;
493 }
494 if (codec_inst != rhs.codec_inst) {
495 return false;
496 }
497 return true;
498 } 499 }
499 500
500 bool SendCodecSpec::operator!=(const SendCodecSpec& rhs) const { 501 } // namespace {
501 return !(*this == rhs);
502 }
503 502
504 bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in, 503 bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
505 webrtc::CodecInst* out) { 504 webrtc::CodecInst* out) {
506 return WebRtcVoiceCodecs::ToCodecInst(in, out); 505 return WebRtcVoiceCodecs::ToCodecInst(in, out);
507 } 506 }
508 507
509 WebRtcVoiceEngine::WebRtcVoiceEngine( 508 WebRtcVoiceEngine::WebRtcVoiceEngine(
510 webrtc::AudioDeviceModule* adm, 509 webrtc::AudioDeviceModule* adm,
511 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory) 510 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
512 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) { 511 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 1132
1134 // Add telephone-event codec last 1133 // Add telephone-event codec last
1135 map_format({kDtmfCodecName, 8000, 1}); 1134 map_format({kDtmfCodecName, 8000, 1});
1136 1135
1137 return out; 1136 return out;
1138 } 1137 }
1139 1138
1140 class WebRtcVoiceMediaChannel::WebRtcAudioSendStream 1139 class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
1141 : public AudioSource::Sink { 1140 : public AudioSource::Sink {
1142 public: 1141 public:
1143 WebRtcAudioSendStream(int ch, 1142 WebRtcAudioSendStream(
1144 webrtc::AudioTransport* voe_audio_transport, 1143 int ch,
1145 uint32_t ssrc, 1144 webrtc::AudioTransport* voe_audio_transport,
1146 const std::string& c_name, 1145 uint32_t ssrc,
1147 const SendCodecSpec& send_codec_spec, 1146 const std::string& c_name,
1148 const std::vector<webrtc::RtpExtension>& extensions, 1147 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec,
1149 webrtc::Call* call, 1148 const std::vector<webrtc::RtpExtension>& extensions,
1150 webrtc::Transport* send_transport) 1149 int max_send_bitrate_bps,
1150 webrtc::Call* call,
1151 webrtc::Transport* send_transport)
1151 : voe_audio_transport_(voe_audio_transport), 1152 : voe_audio_transport_(voe_audio_transport),
1152 call_(call), 1153 call_(call),
1153 config_(send_transport), 1154 config_(send_transport),
1155 max_send_bitrate_bps_(max_send_bitrate_bps),
1154 rtp_parameters_(CreateRtpParametersWithOneEncoding()) { 1156 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
1155 RTC_DCHECK_GE(ch, 0); 1157 RTC_DCHECK_GE(ch, 0);
1156 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore: 1158 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1157 // RTC_DCHECK(voe_audio_transport); 1159 // RTC_DCHECK(voe_audio_transport);
1158 RTC_DCHECK(call); 1160 RTC_DCHECK(call);
1159 config_.rtp.ssrc = ssrc; 1161 config_.rtp.ssrc = ssrc;
1160 config_.rtp.c_name = c_name; 1162 config_.rtp.c_name = c_name;
1161 config_.voe_channel_id = ch; 1163 config_.voe_channel_id = ch;
1162 config_.rtp.extensions = extensions; 1164 config_.rtp.extensions = extensions;
1163 RecreateAudioSendStream(send_codec_spec); 1165 RecreateAudioSendStream(send_codec_spec);
1164 } 1166 }
1165 1167
1166 ~WebRtcAudioSendStream() override { 1168 ~WebRtcAudioSendStream() override {
1167 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1169 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1168 ClearSource(); 1170 ClearSource();
1169 call_->DestroyAudioSendStream(stream_); 1171 call_->DestroyAudioSendStream(stream_);
1170 } 1172 }
1171 1173
1172 void RecreateAudioSendStream(const SendCodecSpec& send_codec_spec) { 1174 void RecreateAudioSendStream(
1175 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1173 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1177 send_codec_spec_ = send_codec_spec;
1174 config_.rtp.nack.rtp_history_ms = 1178 config_.rtp.nack.rtp_history_ms =
1175 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0; 1179 send_codec_spec_.nack_enabled ? kNackRtpHistoryMs : 0;
1180 config_.send_codec_spec = send_codec_spec_;
1181
1182 auto send_rate = ComputeSendBitrate(
1183 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1184 send_codec_spec.codec_inst);
1185 if (send_rate) {
1186 // Apply a send rate that abides by |max_send_bitrate_bps_| and
1187 // |rtp_parameters_| when possible. Otherwise use the codec rate.
1188 config_.send_codec_spec.codec_inst.rate = *send_rate;
1189 }
1176 RecreateAudioSendStream(); 1190 RecreateAudioSendStream();
1177 } 1191 }
1178 1192
1179 void RecreateAudioSendStream( 1193 void RecreateAudioSendStream(
1180 const std::vector<webrtc::RtpExtension>& extensions) { 1194 const std::vector<webrtc::RtpExtension>& extensions) {
1181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1195 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1182 config_.rtp.extensions = extensions; 1196 config_.rtp.extensions = extensions;
1183 RecreateAudioSendStream(); 1197 RecreateAudioSendStream();
1184 } 1198 }
1185 1199
1200 bool SetMaxSendBitrate(int bps) {
1201 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1202 auto send_rate =
1203 ComputeSendBitrate(bps, rtp_parameters_.encodings[0].max_bitrate_bps,
1204 send_codec_spec_.codec_inst);
1205 if (!send_rate) {
1206 return false;
1207 }
1208
1209 max_send_bitrate_bps_ = bps;
1210
1211 if (config_.send_codec_spec.codec_inst.rate != *send_rate) {
1212 // Recreate AudioSendStream with new bit rate.
1213 config_.send_codec_spec.codec_inst.rate = *send_rate;
1214 RecreateAudioSendStream();
1215 }
1216 return true;
1217 }
1218
1186 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) { 1219 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
1187 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1220 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1188 RTC_DCHECK(stream_); 1221 RTC_DCHECK(stream_);
1189 return stream_->SendTelephoneEvent(payload_type, event, duration_ms); 1222 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1190 } 1223 }
1191 1224
1192 void SetSend(bool send) { 1225 void SetSend(bool send) {
1193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1226 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1194 send_ = send; 1227 send_ = send;
1195 UpdateSendState(); 1228 UpdateSendState();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1268 // Accessor to the VoE channel ID. 1301 // Accessor to the VoE channel ID.
1269 int channel() const { 1302 int channel() const {
1270 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1271 return config_.voe_channel_id; 1304 return config_.voe_channel_id;
1272 } 1305 }
1273 1306
1274 const webrtc::RtpParameters& rtp_parameters() const { 1307 const webrtc::RtpParameters& rtp_parameters() const {
1275 return rtp_parameters_; 1308 return rtp_parameters_;
1276 } 1309 }
1277 1310
1278 void SetRtpParameters(const webrtc::RtpParameters& parameters) { 1311 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
1279 RTC_CHECK_EQ(1UL, parameters.encodings.size()); 1312 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1313 auto send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
1314 parameters.encodings[0].max_bitrate_bps,
1315 send_codec_spec_.codec_inst);
1316 if (!send_rate) {
1317 return false;
1318 }
1319
1280 rtp_parameters_ = parameters; 1320 rtp_parameters_ = parameters;
1281 // parameters.encodings[0].active could have changed. 1321
1282 UpdateSendState(); 1322 // parameters.encodings[0].encodings[0].max_bitrate_bps could have changed.
1323 if (config_.send_codec_spec.codec_inst.rate != *send_rate) {
1324 // Recreate AudioSendStream with new bit rate.
1325 config_.send_codec_spec.codec_inst.rate = *send_rate;
1326 RecreateAudioSendStream();
1327 } else {
1328 // parameters.encodings[0].active could have changed.
1329 UpdateSendState();
1330 }
1331 return true;
1283 } 1332 }
1284 1333
1285 private: 1334 private:
1286 void UpdateSendState() { 1335 void UpdateSendState() {
1287 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1336 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1288 RTC_DCHECK(stream_); 1337 RTC_DCHECK(stream_);
1289 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size()); 1338 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1290 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) { 1339 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
1291 stream_->Start(); 1340 stream_->Start();
1292 } else { // !send || source_ = nullptr 1341 } else { // !send || source_ = nullptr
(...skipping 28 matching lines...) Expand all
1321 // The stream is owned by WebRtcAudioSendStream and may be reallocated if 1370 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1322 // configuration changes. 1371 // configuration changes.
1323 webrtc::AudioSendStream* stream_ = nullptr; 1372 webrtc::AudioSendStream* stream_ = nullptr;
1324 1373
1325 // Raw pointer to AudioSource owned by LocalAudioTrackHandler. 1374 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
1326 // PeerConnection will make sure invalidating the pointer before the object 1375 // PeerConnection will make sure invalidating the pointer before the object
1327 // goes away. 1376 // goes away.
1328 AudioSource* source_ = nullptr; 1377 AudioSource* source_ = nullptr;
1329 bool send_ = false; 1378 bool send_ = false;
1330 bool muted_ = false; 1379 bool muted_ = false;
1380 int max_send_bitrate_bps_;
1331 webrtc::RtpParameters rtp_parameters_; 1381 webrtc::RtpParameters rtp_parameters_;
1382 webrtc::AudioSendStream::Config::SendCodecSpec send_codec_spec_;
1332 1383
1333 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream); 1384 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1334 }; 1385 };
1335 1386
1336 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream { 1387 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1337 public: 1388 public:
1338 WebRtcAudioReceiveStream( 1389 WebRtcAudioReceiveStream(
1339 int ch, 1390 int ch,
1340 uint32_t remote_ssrc, 1391 uint32_t remote_ssrc,
1341 uint32_t local_ssrc, 1392 uint32_t local_ssrc,
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1581 1632
1582 // TODO(deadbeef): Handle setting parameters with a list of codecs in a 1633 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1583 // different order (which should change the send codec). 1634 // different order (which should change the send codec).
1584 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); 1635 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1585 if (current_parameters.codecs != parameters.codecs) { 1636 if (current_parameters.codecs != parameters.codecs) {
1586 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs " 1637 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1587 << "is not currently supported."; 1638 << "is not currently supported.";
1588 return false; 1639 return false;
1589 } 1640 }
1590 1641
1591 if (!SetChannelSendParameters(it->second->channel(), parameters)) { 1642 // TODO(minyue): The following legacy actions go into
1592 LOG(LS_WARNING) << "Failed to set send RtpParameters."; 1643 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1593 return false; 1644 // though there are two difference:
1594 } 1645 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1646 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1647 // |SetSendCodecs|. The outcome should be the same.
1648 // 2. AudioSendStream can be recreated.
1649
1595 // Codecs are handled at the WebRtcVoiceMediaChannel level. 1650 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1596 webrtc::RtpParameters reduced_params = parameters; 1651 webrtc::RtpParameters reduced_params = parameters;
1597 reduced_params.codecs.clear(); 1652 reduced_params.codecs.clear();
1598 it->second->SetRtpParameters(reduced_params); 1653 return it->second->SetRtpParameters(reduced_params);
1599 return true;
1600 } 1654 }
1601 1655
1602 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters( 1656 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1603 uint32_t ssrc) const { 1657 uint32_t ssrc) const {
1604 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1658 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1605 auto it = recv_streams_.find(ssrc); 1659 auto it = recv_streams_.find(ssrc);
1606 if (it == recv_streams_.end()) { 1660 if (it == recv_streams_.end()) {
1607 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream " 1661 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1608 << "with ssrc " << ssrc << " which doesn't exist."; 1662 << "with ssrc " << ssrc << " which doesn't exist.";
1609 return webrtc::RtpParameters(); 1663 return webrtc::RtpParameters();
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
1749 } 1803 }
1750 dtmf_payload_type_ = rtc::Optional<int>(codec.id); 1804 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1751 break; 1805 break;
1752 } 1806 }
1753 } 1807 }
1754 1808
1755 // Scan through the list to figure out the codec to use for sending, along 1809 // Scan through the list to figure out the codec to use for sending, along
1756 // with the proper configuration for VAD, CNG, NACK and Opus-specific 1810 // with the proper configuration for VAD, CNG, NACK and Opus-specific
1757 // parameters. 1811 // parameters.
1758 // TODO(solenberg): Refactor this logic once we create AudioEncoders here. 1812 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1759 SendCodecSpec send_codec_spec; 1813 webrtc::AudioSendStream::Config::SendCodecSpec send_codec_spec;
1760 { 1814 {
1761 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled; 1815 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1762 1816
1763 // Find send codec (the first non-telephone-event/CN codec). 1817 // Find send codec (the first non-telephone-event/CN codec).
1764 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec( 1818 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1765 codecs, &send_codec_spec.codec_inst); 1819 codecs, &send_codec_spec.codec_inst);
1766 if (!codec) { 1820 if (!codec) {
1767 LOG(LS_WARNING) << "Received empty list of codecs."; 1821 LOG(LS_WARNING) << "Received empty list of codecs.";
1768 return false; 1822 return false;
1769 } 1823 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1823 break; 1877 break;
1824 } 1878 }
1825 } 1879 }
1826 } 1880 }
1827 1881
1828 // Apply new settings to all streams. 1882 // Apply new settings to all streams.
1829 if (send_codec_spec_ != send_codec_spec) { 1883 if (send_codec_spec_ != send_codec_spec) {
1830 send_codec_spec_ = std::move(send_codec_spec); 1884 send_codec_spec_ = std::move(send_codec_spec);
1831 for (const auto& kv : send_streams_) { 1885 for (const auto& kv : send_streams_) {
1832 kv.second->RecreateAudioSendStream(send_codec_spec_); 1886 kv.second->RecreateAudioSendStream(send_codec_spec_);
1833 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) {
1834 return false;
1835 }
1836 } 1887 }
1837 } 1888 }
1838 1889
1839 // Check if the transport cc feedback or NACK status has changed on the 1890 // Check if the transport cc feedback or NACK status has changed on the
1840 // preferred send codec, and in that case reconfigure all receive streams. 1891 // preferred send codec, and in that case reconfigure all receive streams.
1841 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled || 1892 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled ||
1842 recv_nack_enabled_ != send_codec_spec_.nack_enabled) { 1893 recv_nack_enabled_ != send_codec_spec_.nack_enabled) {
1843 LOG(LS_INFO) << "Recreate all the receive streams because the send " 1894 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1844 "codec has changed."; 1895 "codec has changed.";
1845 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled; 1896 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1846 recv_nack_enabled_ = send_codec_spec_.nack_enabled; 1897 recv_nack_enabled_ = send_codec_spec_.nack_enabled;
1847 for (auto& kv : recv_streams_) { 1898 for (auto& kv : recv_streams_) {
1848 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_, 1899 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1849 recv_nack_enabled_); 1900 recv_nack_enabled_);
1850 } 1901 }
1851 } 1902 }
1852 1903
1853 send_codecs_ = codecs; 1904 send_codecs_ = codecs;
1854 return true; 1905 return true;
1855 } 1906 }
1856 1907
1857 // Apply current codec settings to a single voe::Channel used for sending.
1858 bool WebRtcVoiceMediaChannel::SetSendCodecs(
1859 int channel,
1860 const webrtc::RtpParameters& rtp_parameters) {
1861 // Disable VAD and FEC unless we know the other side wants them.
1862 engine()->voe()->codec()->SetVADStatus(channel, false);
1863 engine()->voe()->codec()->SetFECStatus(channel, false);
1864
1865 // Set the codec immediately, since SetVADStatus() depends on whether
1866 // the current codec is mono or stereo.
1867 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1868 return false;
1869 }
1870
1871 // FEC should be enabled after SetSendCodec.
1872 if (send_codec_spec_.enable_codec_fec) {
1873 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1874 << channel;
1875 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1876 // Enable codec internal FEC. Treat any failure as fatal internal error.
1877 LOG_RTCERR2(SetFECStatus, channel, true);
1878 return false;
1879 }
1880 }
1881
1882 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1883 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1884 // send codec has to be Opus.
1885
1886 // Set Opus internal DTX.
1887 LOG(LS_INFO) << "Attempt to "
1888 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1889 << " Opus DTX on channel "
1890 << channel;
1891 if (engine()->voe()->codec()->SetOpusDtx(channel,
1892 send_codec_spec_.enable_opus_dtx)) {
1893 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1894 return false;
1895 }
1896
1897 // If opus_max_playback_rate <= 0, the default maximum playback rate
1898 // (48 kHz) will be used.
1899 if (send_codec_spec_.opus_max_playback_rate > 0) {
1900 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1901 << send_codec_spec_.opus_max_playback_rate
1902 << " Hz on channel "
1903 << channel;
1904 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1905 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1906 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1907 send_codec_spec_.opus_max_playback_rate);
1908 return false;
1909 }
1910 }
1911 }
1912 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
1913 // Check if it is possible to fuse with the previous call in this function.
1914 SetChannelSendParameters(channel, rtp_parameters);
1915
1916 // Set the CN payloadtype and the VAD status.
1917 if (send_codec_spec_.cng_payload_type != -1) {
1918 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1919 if (send_codec_spec_.cng_plfreq != 8000) {
1920 webrtc::PayloadFrequencies cn_freq;
1921 switch (send_codec_spec_.cng_plfreq) {
1922 case 16000:
1923 cn_freq = webrtc::kFreq16000Hz;
1924 break;
1925 case 32000:
1926 cn_freq = webrtc::kFreq32000Hz;
1927 break;
1928 default:
1929 RTC_NOTREACHED();
1930 return false;
1931 }
1932 if (engine()->voe()->codec()->SetSendCNPayloadType(
1933 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1934 LOG_RTCERR3(SetSendCNPayloadType, channel,
1935 send_codec_spec_.cng_payload_type, cn_freq);
1936 // TODO(ajm): This failure condition will be removed from VoE.
1937 // Restore the return here when we update to a new enough webrtc.
1938 //
1939 // Not returning false because the SetSendCNPayloadType will fail if
1940 // the channel is already sending.
1941 // This can happen if the remote description is applied twice, for
1942 // example in the case of ROAP on top of JSEP, where both side will
1943 // send the offer.
1944 }
1945 }
1946
1947 // Only turn on VAD if we have a CN payload type that matches the
1948 // clockrate for the codec we are going to use.
1949 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1950 send_codec_spec_.codec_inst.channels == 1) {
1951 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1952 // interaction between VAD and Opus FEC.
1953 LOG(LS_INFO) << "Enabling VAD";
1954 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1955 LOG_RTCERR2(SetVADStatus, channel, true);
1956 return false;
1957 }
1958 }
1959 }
1960 return true;
1961 }
1962
1963 bool WebRtcVoiceMediaChannel::SetSendCodec(
1964 int channel, const webrtc::CodecInst& send_codec) {
1965 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1966 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1967
1968 webrtc::CodecInst current_codec = {0};
1969 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1970 (send_codec == current_codec)) {
1971 // Codec is already configured, we can return without setting it again.
1972 return true;
1973 }
1974
1975 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1976 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
1977 return false;
1978 }
1979 return true;
1980 }
1981
1982 void WebRtcVoiceMediaChannel::SetPlayout(bool playout) { 1908 void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1983 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetPlayout"); 1909 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetPlayout");
1984 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1910 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1985 if (playout_ == playout) { 1911 if (playout_ == playout) {
1986 return; 1912 return;
1987 } 1913 }
1988 1914
1989 for (const auto& kv : recv_streams_) { 1915 for (const auto& kv : recv_streams_) {
1990 kv.second->SetPlayout(playout); 1916 kv.second->SetPlayout(playout);
1991 } 1917 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
2076 return false; 2002 return false;
2077 } 2003 }
2078 2004
2079 // Save the channel to send_streams_, so that RemoveSendStream() can still 2005 // Save the channel to send_streams_, so that RemoveSendStream() can still
2080 // delete the channel in case failure happens below. 2006 // delete the channel in case failure happens below.
2081 webrtc::AudioTransport* audio_transport = 2007 webrtc::AudioTransport* audio_transport =
2082 engine()->voe()->base()->audio_transport(); 2008 engine()->voe()->base()->audio_transport();
2083 2009
2084 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream( 2010 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
2085 channel, audio_transport, ssrc, sp.cname, send_codec_spec_, 2011 channel, audio_transport, ssrc, sp.cname, send_codec_spec_,
2086 send_rtp_extensions_, call_, this); 2012 send_rtp_extensions_, max_send_bitrate_bps_, call_, this);
2087 send_streams_.insert(std::make_pair(ssrc, stream)); 2013 send_streams_.insert(std::make_pair(ssrc, stream));
2088 2014
2089 // Set the current codecs to be used for the new channel. We need to do this
2090 // after adding the channel to send_channels_, because of how max bitrate is
2091 // currently being configured by SetSendCodec().
2092 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
2093 RemoveSendStream(ssrc);
2094 return false;
2095 }
2096
2097 // At this point the stream's local SSRC has been updated. If it is the first 2015 // At this point the stream's local SSRC has been updated. If it is the first
2098 // send stream, make sure that all the receive streams are updated with the 2016 // send stream, make sure that all the receive streams are updated with the
2099 // same SSRC in order to send receiver reports. 2017 // same SSRC in order to send receiver reports.
2100 if (send_streams_.size() == 1) { 2018 if (send_streams_.size() == 1) {
2101 receiver_reports_ssrc_ = ssrc; 2019 receiver_reports_ssrc_ = ssrc;
2102 for (const auto& kv : recv_streams_) { 2020 for (const auto& kv : recv_streams_) {
2103 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive 2021 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
2104 // streams instead, so we can avoid recreating the streams here. 2022 // streams instead, so we can avoid recreating the streams here.
2105 kv.second->RecreateAudioReceiveStream(ssrc); 2023 kv.second->RecreateAudioReceiveStream(ssrc);
2106 int recv_channel = kv.second->channel(); 2024 int recv_channel = kv.second->channel();
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
2460 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing(); 2378 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
2461 if (ap) { 2379 if (ap) {
2462 ap->set_output_will_be_muted(all_muted); 2380 ap->set_output_will_be_muted(all_muted);
2463 } 2381 }
2464 return true; 2382 return true;
2465 } 2383 }
2466 2384
2467 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) { 2385 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2468 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate."; 2386 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2469 max_send_bitrate_bps_ = bps; 2387 max_send_bitrate_bps_ = bps;
2470 2388 bool success = true;
2471 for (const auto& kv : send_streams_) { 2389 for (const auto& kv : send_streams_) {
2472 if (!SetChannelSendParameters(kv.second->channel(), 2390 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2473 kv.second->rtp_parameters())) { 2391 success = false;
2474 return false;
2475 } 2392 }
2476 } 2393 }
2477 return true; 2394 return success;
2478 }
2479
2480 bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
2481 int channel,
2482 const webrtc::RtpParameters& parameters) {
2483 RTC_CHECK_EQ(1UL, parameters.encodings.size());
2484 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2485 // different order (which should change the send codec).
2486 return SetMaxSendBitrate(
2487 channel, MinPositive(max_send_bitrate_bps_,
2488 parameters.encodings[0].max_bitrate_bps));
2489 }
2490
2491 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
2492 // Bitrate is auto by default.
2493 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2494 // SetMaxSendBandwith(0), the second call removes the previous limit.
2495 if (bps <= 0) {
2496 return true;
2497 }
2498
2499 if (!HasSendCodec()) {
2500 LOG(LS_INFO) << "The send codec has not been set up yet. "
2501 << "The send bitrate setting will be applied later.";
2502 return true;
2503 }
2504
2505 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
2506 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
2507
2508 if (is_multi_rate) {
2509 // If codec is multi-rate then just set the bitrate.
2510 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2511 codec.rate = std::min(bps, max_bitrate_bps);
2512 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2513 << " bps.";
2514 if (!SetSendCodec(channel, codec)) {
2515 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2516 << bps << " bps.";
2517 return false;
2518 }
2519 return true;
2520 } else {
2521 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2522 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2523 // fixed bitrate then ignore.
2524 if (bps < codec.rate) {
2525 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2526 << bps << " bps"
2527 << ", requires at least " << codec.rate << " bps.";
2528 return false;
2529 }
2530 return true;
2531 }
2532 } 2395 }
2533 2396
2534 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) { 2397 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2535 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 2398 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2536 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); 2399 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2537 call_->SignalChannelNetworkState( 2400 call_->SignalChannelNetworkState(
2538 webrtc::MediaType::AUDIO, 2401 webrtc::MediaType::AUDIO,
2539 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); 2402 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2540 } 2403 }
2541 2404
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
2647 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 2510 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2648 const auto it = send_streams_.find(ssrc); 2511 const auto it = send_streams_.find(ssrc);
2649 if (it != send_streams_.end()) { 2512 if (it != send_streams_.end()) {
2650 return it->second->channel(); 2513 return it->second->channel();
2651 } 2514 }
2652 return -1; 2515 return -1;
2653 } 2516 }
2654 } // namespace cricket 2517 } // namespace cricket
2655 2518
2656 #endif // HAVE_WEBRTC_VOICE 2519 #endif // HAVE_WEBRTC_VOICE
OLDNEW
« no previous file with comments | « webrtc/media/engine/webrtcvoiceengine.h ('k') | webrtc/media/engine/webrtcvoiceengine_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698