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

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

Issue 2405183002: Moving WebRtcVoiceMediaChannel::SendSetCodec to AudioSendStream. (Closed)
Patch Set: fixing unittests 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 } // namespace {
470 if (nack_enabled != rhs.nack_enabled) {
471 return false;
472 }
473 if (transport_cc_enabled != rhs.transport_cc_enabled) {
474 return false;
475 }
476 if (enable_codec_fec != rhs.enable_codec_fec) {
477 return false;
478 }
479 if (enable_opus_dtx != rhs.enable_opus_dtx) {
480 return false;
481 }
482 if (opus_max_playback_rate != rhs.opus_max_playback_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
500 bool SendCodecSpec::operator!=(const SendCodecSpec& rhs) const {
501 return !(*this == rhs);
502 }
503 469
504 bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in, 470 bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
505 webrtc::CodecInst* out) { 471 webrtc::CodecInst* out) {
506 return WebRtcVoiceCodecs::ToCodecInst(in, out); 472 return WebRtcVoiceCodecs::ToCodecInst(in, out);
507 } 473 }
508 474
509 WebRtcVoiceEngine::WebRtcVoiceEngine( 475 WebRtcVoiceEngine::WebRtcVoiceEngine(
510 webrtc::AudioDeviceModule* adm, 476 webrtc::AudioDeviceModule* adm,
511 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory) 477 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
512 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) { 478 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 1099
1134 // Add telephone-event codec last 1100 // Add telephone-event codec last
1135 map_format({kDtmfCodecName, 8000, 1}); 1101 map_format({kDtmfCodecName, 8000, 1});
1136 1102
1137 return out; 1103 return out;
1138 } 1104 }
1139 1105
1140 class WebRtcVoiceMediaChannel::WebRtcAudioSendStream 1106 class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
1141 : public AudioSource::Sink { 1107 : public AudioSource::Sink {
1142 public: 1108 public:
1143 WebRtcAudioSendStream(int ch, 1109 WebRtcAudioSendStream(
1144 webrtc::AudioTransport* voe_audio_transport, 1110 int ch,
1145 uint32_t ssrc, 1111 webrtc::AudioTransport* voe_audio_transport,
1146 const std::string& c_name, 1112 uint32_t ssrc,
1147 const SendCodecSpec& send_codec_spec, 1113 const std::string& c_name,
1148 const std::vector<webrtc::RtpExtension>& extensions, 1114 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec,
1149 webrtc::Call* call, 1115 const std::vector<webrtc::RtpExtension>& extensions,
1150 webrtc::Transport* send_transport) 1116 int max_send_bitrate_bps,
1117 webrtc::Call* call,
1118 webrtc::Transport* send_transport)
1151 : voe_audio_transport_(voe_audio_transport), 1119 : voe_audio_transport_(voe_audio_transport),
1152 call_(call), 1120 call_(call),
1153 config_(send_transport), 1121 config_(send_transport),
1122 max_send_bitrate_bps_(max_send_bitrate_bps),
1154 rtp_parameters_(CreateRtpParametersWithOneEncoding()) { 1123 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
1155 RTC_DCHECK_GE(ch, 0); 1124 RTC_DCHECK_GE(ch, 0);
1156 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore: 1125 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1157 // RTC_DCHECK(voe_audio_transport); 1126 // RTC_DCHECK(voe_audio_transport);
1158 RTC_DCHECK(call); 1127 RTC_DCHECK(call);
1159 config_.rtp.ssrc = ssrc; 1128 config_.rtp.ssrc = ssrc;
1160 config_.rtp.c_name = c_name; 1129 config_.rtp.c_name = c_name;
1161 config_.voe_channel_id = ch; 1130 config_.voe_channel_id = ch;
1162 config_.rtp.extensions = extensions; 1131 config_.rtp.extensions = extensions;
1163 RecreateAudioSendStream(send_codec_spec); 1132 RecreateAudioSendStream(send_codec_spec);
1164 } 1133 }
1165 1134
1166 ~WebRtcAudioSendStream() override { 1135 ~WebRtcAudioSendStream() override {
1167 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1136 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1168 ClearSource(); 1137 ClearSource();
1169 call_->DestroyAudioSendStream(stream_); 1138 call_->DestroyAudioSendStream(stream_);
1170 } 1139 }
1171 1140
1172 void RecreateAudioSendStream(const SendCodecSpec& send_codec_spec) { 1141 void RecreateAudioSendStream(
1142 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1173 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1143 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1174 if (stream_) { 1144 if (stream_) {
1175 call_->DestroyAudioSendStream(stream_); 1145 call_->DestroyAudioSendStream(stream_);
1176 stream_ = nullptr; 1146 stream_ = nullptr;
1177 } 1147 }
1178 config_.rtp.nack.rtp_history_ms = 1148 config_.rtp.nack.rtp_history_ms =
1179 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0; 1149 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1150 config_.send_codec_spec = send_codec_spec;
1151 config_.send_codec_spec.codec_inst.rate = DecideSendBitrate();
1180 RTC_DCHECK(!stream_); 1152 RTC_DCHECK(!stream_);
1181 stream_ = call_->CreateAudioSendStream(config_); 1153 stream_ = call_->CreateAudioSendStream(config_);
1182 RTC_CHECK(stream_); 1154 RTC_CHECK(stream_);
1183 UpdateSendState(); 1155 UpdateSendState();
1184 } 1156 }
1185 1157
1186 void RecreateAudioSendStream( 1158 void RecreateAudioSendStream(
1187 const std::vector<webrtc::RtpExtension>& extensions) { 1159 const std::vector<webrtc::RtpExtension>& extensions) {
1188 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1160 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1189 if (stream_) { 1161 if (stream_) {
1190 call_->DestroyAudioSendStream(stream_); 1162 call_->DestroyAudioSendStream(stream_);
1191 stream_ = nullptr; 1163 stream_ = nullptr;
1192 } 1164 }
1193 config_.rtp.extensions = extensions; 1165 config_.rtp.extensions = extensions;
1194 if (webrtc::field_trial::FindFullName("WebRTC-AdaptAudioBitrate") == 1166 if (webrtc::field_trial::FindFullName("WebRTC-AdaptAudioBitrate") ==
1195 "Enabled") { 1167 "Enabled") {
1196 // TODO(mflodman): Keep testing this and set proper values. 1168 // TODO(mflodman): Keep testing this and set proper values.
1197 // Note: This is an early experiment currently only supported by Opus. 1169 // Note: This is an early experiment currently only supported by Opus.
1198 config_.min_bitrate_kbps = kOpusMinBitrate; 1170 config_.min_bitrate_kbps = kOpusMinBitrate;
1199 config_.max_bitrate_kbps = kOpusBitrateFb; 1171 config_.max_bitrate_kbps = kOpusBitrateFb;
1200 } 1172 }
1201 1173
1202 RTC_DCHECK(!stream_); 1174 RTC_DCHECK(!stream_);
1203 stream_ = call_->CreateAudioSendStream(config_); 1175 stream_ = call_->CreateAudioSendStream(config_);
1204 RTC_CHECK(stream_); 1176 RTC_CHECK(stream_);
1205 UpdateSendState(); 1177 UpdateSendState();
1206 } 1178 }
1207 1179
1180 bool SetMaxSendBitrate(int bps) {
1181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1182 if (!IsMaxSendBitrateValid(
1183 MinPositive(bps, rtp_parameters_.encodings[0].max_bitrate_bps))) {
1184 return false;
1185 }
1186 max_send_bitrate_bps_ = bps;
1187
1188 int new_sent_bitrate_bps = DecideSendBitrate();
1189 if (config_.send_codec_spec.codec_inst.rate == new_sent_bitrate_bps)
1190 return true;
1191
1192 // Recreate AudioSendStream with new bit rate.
1193 config_.send_codec_spec.codec_inst.rate = new_sent_bitrate_bps;
1194 if (stream_) {
1195 call_->DestroyAudioSendStream(stream_);
1196 stream_ = nullptr;
1197 }
1198 RTC_DCHECK(!stream_);
1199 stream_ = call_->CreateAudioSendStream(config_);
1200 RTC_CHECK(stream_);
1201 UpdateSendState();
1202 return true;
1203 }
1204
1208 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) { 1205 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
1209 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1206 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1210 RTC_DCHECK(stream_); 1207 RTC_DCHECK(stream_);
1211 return stream_->SendTelephoneEvent(payload_type, event, duration_ms); 1208 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1212 } 1209 }
1213 1210
1214 void SetSend(bool send) { 1211 void SetSend(bool send) {
1215 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1212 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1216 send_ = send; 1213 send_ = send;
1217 UpdateSendState(); 1214 UpdateSendState();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1290 // Accessor to the VoE channel ID. 1287 // Accessor to the VoE channel ID.
1291 int channel() const { 1288 int channel() const {
1292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1289 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1293 return config_.voe_channel_id; 1290 return config_.voe_channel_id;
1294 } 1291 }
1295 1292
1296 const webrtc::RtpParameters& rtp_parameters() const { 1293 const webrtc::RtpParameters& rtp_parameters() const {
1297 return rtp_parameters_; 1294 return rtp_parameters_;
1298 } 1295 }
1299 1296
1300 void SetRtpParameters(const webrtc::RtpParameters& parameters) { 1297 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
1301 RTC_CHECK_EQ(1UL, parameters.encodings.size()); 1298 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1299
1300 if (!IsMaxSendBitrateValid(MinPositive(
1301 max_send_bitrate_bps_, parameters.encodings[0].max_bitrate_bps))) {
1302 return false;
1303 }
1304
1302 rtp_parameters_ = parameters; 1305 rtp_parameters_ = parameters;
1306
1307 // parameters.encodings[0].encodings[0].max_bitrate_bps could have changed.
1308 SetMaxSendBitrate(max_send_bitrate_bps_);
1309
1303 // parameters.encodings[0].active could have changed. 1310 // parameters.encodings[0].active could have changed.
1304 UpdateSendState(); 1311 UpdateSendState();
1312
1313 return true;
1305 } 1314 }
1306 1315
1307 private: 1316 private:
1308 void UpdateSendState() { 1317 void UpdateSendState() {
1309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1318 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1310 RTC_DCHECK(stream_); 1319 RTC_DCHECK(stream_);
1311 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size()); 1320 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1312 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) { 1321 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
1313 stream_->Start(); 1322 stream_->Start();
1314 } else { // !send || source_ = nullptr 1323 } else { // !send || source_ = nullptr
1315 stream_->Stop(); 1324 stream_->Stop();
1316 } 1325 }
1317 } 1326 }
1318 1327
1328 bool IsMaxSendBitrateValid(int bps) const {
1329 if (bps <= 0) {
1330 return true;
1331 }
1332
1333 if (config_.send_codec_spec.codec_inst.pltype == -1) {
1334 LOG(LS_INFO) << "The send codec has not been set up yet. "
1335 << "The send bitrate setting will be applied later.";
1336 return true;
1337 }
1338
1339 if (!WebRtcVoiceCodecs::IsCodecMultiRate(
1340 config_.send_codec_spec.codec_inst) &&
1341 bps < config_.send_codec_spec.codec_inst.rate) {
1342 // If codec is not multi-rate and |max_send_bit_rate_| is less than the
1343 // fixed bitrate then fail. If codec is not multi-rate and |bps| exceeds
1344 // or
1345 // equal the fixed bitrate then ignore.
1346 LOG(LS_ERROR) << "Failed to set codec "
1347 << config_.send_codec_spec.codec_inst.plname
1348 << " to bitrate " << bps << " bps"
1349 << ", requires at least "
1350 << config_.send_codec_spec.codec_inst.rate << " bps.";
1351 return false;
1352 }
1353
1354 return true;
1355 }
1356
1357 int DecideSendBitrate() const {
minyue-webrtc 2016/10/14 13:32:56 Needed to split this into DecideSendBitrate and Is
1358 const int bps = MinPositive(max_send_bitrate_bps_,
1359 rtp_parameters_.encodings[0].max_bitrate_bps);
1360 const int current_rate = config_.send_codec_spec.codec_inst.rate;
1361
1362 // Bitrate is auto by default.
1363 // TODO(bemasc): Fix this so that if SetMaxSendBitrate(50) is followed by
1364 // SetMaxSendBitrate(0), the second call removes the previous limit.
1365 if (bps <= 0) {
1366 return current_rate;
1367 }
1368
1369 if (config_.send_codec_spec.codec_inst.pltype == -1) {
1370 return current_rate;
1371 }
1372
1373 if (!WebRtcVoiceCodecs::IsCodecMultiRate(
1374 config_.send_codec_spec.codec_inst)) {
1375 return current_rate;
1376 }
1377
1378 // If codec is multi-rate then just set the bitrate.
1379 return std::min(bps, WebRtcVoiceCodecs::MaxBitrateBps(
1380 config_.send_codec_spec.codec_inst));
1381 }
1382
1319 rtc::ThreadChecker worker_thread_checker_; 1383 rtc::ThreadChecker worker_thread_checker_;
1320 rtc::RaceChecker audio_capture_race_checker_; 1384 rtc::RaceChecker audio_capture_race_checker_;
1321 webrtc::AudioTransport* const voe_audio_transport_ = nullptr; 1385 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1322 webrtc::Call* call_ = nullptr; 1386 webrtc::Call* call_ = nullptr;
1323 webrtc::AudioSendStream::Config config_; 1387 webrtc::AudioSendStream::Config config_;
1324 // The stream is owned by WebRtcAudioSendStream and may be reallocated if 1388 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1325 // configuration changes. 1389 // configuration changes.
1326 webrtc::AudioSendStream* stream_ = nullptr; 1390 webrtc::AudioSendStream* stream_ = nullptr;
1327 1391
1328 // Raw pointer to AudioSource owned by LocalAudioTrackHandler. 1392 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
1329 // PeerConnection will make sure invalidating the pointer before the object 1393 // PeerConnection will make sure invalidating the pointer before the object
1330 // goes away. 1394 // goes away.
1331 AudioSource* source_ = nullptr; 1395 AudioSource* source_ = nullptr;
1332 bool send_ = false; 1396 bool send_ = false;
1333 bool muted_ = false; 1397 bool muted_ = false;
1398 int max_send_bitrate_bps_;
1334 webrtc::RtpParameters rtp_parameters_; 1399 webrtc::RtpParameters rtp_parameters_;
1335 1400
1336 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream); 1401 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1337 }; 1402 };
1338 1403
1339 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream { 1404 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1340 public: 1405 public:
1341 WebRtcAudioReceiveStream( 1406 WebRtcAudioReceiveStream(
1342 int ch, 1407 int ch,
1343 uint32_t remote_ssrc, 1408 uint32_t remote_ssrc,
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
1584 1649
1585 // TODO(deadbeef): Handle setting parameters with a list of codecs in a 1650 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1586 // different order (which should change the send codec). 1651 // different order (which should change the send codec).
1587 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); 1652 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1588 if (current_parameters.codecs != parameters.codecs) { 1653 if (current_parameters.codecs != parameters.codecs) {
1589 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs " 1654 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1590 << "is not currently supported."; 1655 << "is not currently supported.";
1591 return false; 1656 return false;
1592 } 1657 }
1593 1658
1594 if (!SetChannelSendParameters(it->second->channel(), parameters)) { 1659 // TODO(minyue): The following legacy actions go into
1595 LOG(LS_WARNING) << "Failed to set send RtpParameters."; 1660 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1596 return false; 1661 // though there are two difference:
1597 } 1662 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1663 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1664 // |SetSendCodecs|. The outcome should be the same.
1665 // 2. AudioSendStream can be recreated.
1666
1598 // Codecs are handled at the WebRtcVoiceMediaChannel level. 1667 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1599 webrtc::RtpParameters reduced_params = parameters; 1668 webrtc::RtpParameters reduced_params = parameters;
1600 reduced_params.codecs.clear(); 1669 reduced_params.codecs.clear();
1601 it->second->SetRtpParameters(reduced_params); 1670 return it->second->SetRtpParameters(reduced_params);
minyue-webrtc 2016/10/14 13:32:56 possibility of returning false for this function i
1602 return true;
1603 } 1671 }
1604 1672
1605 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters( 1673 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1606 uint32_t ssrc) const { 1674 uint32_t ssrc) const {
1607 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1675 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1608 auto it = recv_streams_.find(ssrc); 1676 auto it = recv_streams_.find(ssrc);
1609 if (it == recv_streams_.end()) { 1677 if (it == recv_streams_.end()) {
1610 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream " 1678 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1611 << "with ssrc " << ssrc << " which doesn't exist."; 1679 << "with ssrc " << ssrc << " which doesn't exist.";
1612 return webrtc::RtpParameters(); 1680 return webrtc::RtpParameters();
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1761 } 1829 }
1762 dtmf_payload_type_ = rtc::Optional<int>(codec.id); 1830 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1763 break; 1831 break;
1764 } 1832 }
1765 } 1833 }
1766 1834
1767 // Scan through the list to figure out the codec to use for sending, along 1835 // Scan through the list to figure out the codec to use for sending, along
1768 // with the proper configuration for VAD, CNG, NACK and Opus-specific 1836 // with the proper configuration for VAD, CNG, NACK and Opus-specific
1769 // parameters. 1837 // parameters.
1770 // TODO(solenberg): Refactor this logic once we create AudioEncoders here. 1838 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1771 SendCodecSpec send_codec_spec; 1839 webrtc::AudioSendStream::Config::SendCodecSpec send_codec_spec;
1772 { 1840 {
1773 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled; 1841 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1774 1842
1775 // Find send codec (the first non-telephone-event/CN codec). 1843 // Find send codec (the first non-telephone-event/CN codec).
1776 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec( 1844 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1777 codecs, &send_codec_spec.codec_inst); 1845 codecs, &send_codec_spec.codec_inst);
1778 if (!codec) { 1846 if (!codec) {
1779 LOG(LS_WARNING) << "Received empty list of codecs."; 1847 LOG(LS_WARNING) << "Received empty list of codecs.";
1780 return false; 1848 return false;
1781 } 1849 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1835 break; 1903 break;
1836 } 1904 }
1837 } 1905 }
1838 } 1906 }
1839 1907
1840 // Apply new settings to all streams. 1908 // Apply new settings to all streams.
1841 if (send_codec_spec_ != send_codec_spec) { 1909 if (send_codec_spec_ != send_codec_spec) {
1842 send_codec_spec_ = std::move(send_codec_spec); 1910 send_codec_spec_ = std::move(send_codec_spec);
1843 for (const auto& kv : send_streams_) { 1911 for (const auto& kv : send_streams_) {
1844 kv.second->RecreateAudioSendStream(send_codec_spec_); 1912 kv.second->RecreateAudioSendStream(send_codec_spec_);
1845 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) {
1846 return false;
1847 }
1848 } 1913 }
1849 } 1914 }
1850 1915
1851 // Check if the transport cc feedback or NACK status has changed on the 1916 // Check if the transport cc feedback or NACK status has changed on the
1852 // preferred send codec, and in that case reconfigure all receive streams. 1917 // preferred send codec, and in that case reconfigure all receive streams.
1853 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled || 1918 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled ||
1854 recv_nack_enabled_ != send_codec_spec_.nack_enabled) { 1919 recv_nack_enabled_ != send_codec_spec_.nack_enabled) {
1855 LOG(LS_INFO) << "Recreate all the receive streams because the send " 1920 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1856 "codec has changed."; 1921 "codec has changed.";
1857 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled; 1922 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1858 recv_nack_enabled_ = send_codec_spec_.nack_enabled; 1923 recv_nack_enabled_ = send_codec_spec_.nack_enabled;
1859 for (auto& kv : recv_streams_) { 1924 for (auto& kv : recv_streams_) {
1860 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_, 1925 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1861 recv_nack_enabled_); 1926 recv_nack_enabled_);
1862 } 1927 }
1863 } 1928 }
1864 1929
1865 send_codecs_ = codecs; 1930 send_codecs_ = codecs;
1866 return true; 1931 return true;
1867 } 1932 }
1868 1933
1869 // Apply current codec settings to a single voe::Channel used for sending.
1870 bool WebRtcVoiceMediaChannel::SetSendCodecs(
1871 int channel,
1872 const webrtc::RtpParameters& rtp_parameters) {
1873 // Disable VAD and FEC unless we know the other side wants them.
1874 engine()->voe()->codec()->SetVADStatus(channel, false);
1875 engine()->voe()->codec()->SetFECStatus(channel, false);
1876
1877 // Set the codec immediately, since SetVADStatus() depends on whether
1878 // the current codec is mono or stereo.
1879 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1880 return false;
1881 }
1882
1883 // FEC should be enabled after SetSendCodec.
1884 if (send_codec_spec_.enable_codec_fec) {
1885 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1886 << channel;
1887 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1888 // Enable codec internal FEC. Treat any failure as fatal internal error.
1889 LOG_RTCERR2(SetFECStatus, channel, true);
1890 return false;
1891 }
1892 }
1893
1894 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1895 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1896 // send codec has to be Opus.
1897
1898 // Set Opus internal DTX.
1899 LOG(LS_INFO) << "Attempt to "
1900 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1901 << " Opus DTX on channel "
1902 << channel;
1903 if (engine()->voe()->codec()->SetOpusDtx(channel,
1904 send_codec_spec_.enable_opus_dtx)) {
1905 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1906 return false;
1907 }
1908
1909 // If opus_max_playback_rate <= 0, the default maximum playback rate
1910 // (48 kHz) will be used.
1911 if (send_codec_spec_.opus_max_playback_rate > 0) {
1912 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1913 << send_codec_spec_.opus_max_playback_rate
1914 << " Hz on channel "
1915 << channel;
1916 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1917 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1918 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1919 send_codec_spec_.opus_max_playback_rate);
1920 return false;
1921 }
1922 }
1923 }
1924 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
1925 // Check if it is possible to fuse with the previous call in this function.
1926 SetChannelSendParameters(channel, rtp_parameters);
1927
1928 // Set the CN payloadtype and the VAD status.
1929 if (send_codec_spec_.cng_payload_type != -1) {
1930 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1931 if (send_codec_spec_.cng_plfreq != 8000) {
1932 webrtc::PayloadFrequencies cn_freq;
1933 switch (send_codec_spec_.cng_plfreq) {
1934 case 16000:
1935 cn_freq = webrtc::kFreq16000Hz;
1936 break;
1937 case 32000:
1938 cn_freq = webrtc::kFreq32000Hz;
1939 break;
1940 default:
1941 RTC_NOTREACHED();
1942 return false;
1943 }
1944 if (engine()->voe()->codec()->SetSendCNPayloadType(
1945 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1946 LOG_RTCERR3(SetSendCNPayloadType, channel,
1947 send_codec_spec_.cng_payload_type, cn_freq);
1948 // TODO(ajm): This failure condition will be removed from VoE.
1949 // Restore the return here when we update to a new enough webrtc.
1950 //
1951 // Not returning false because the SetSendCNPayloadType will fail if
1952 // the channel is already sending.
1953 // This can happen if the remote description is applied twice, for
1954 // example in the case of ROAP on top of JSEP, where both side will
1955 // send the offer.
1956 }
1957 }
1958
1959 // Only turn on VAD if we have a CN payload type that matches the
1960 // clockrate for the codec we are going to use.
1961 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1962 send_codec_spec_.codec_inst.channels == 1) {
1963 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1964 // interaction between VAD and Opus FEC.
1965 LOG(LS_INFO) << "Enabling VAD";
1966 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1967 LOG_RTCERR2(SetVADStatus, channel, true);
1968 return false;
1969 }
1970 }
1971 }
1972 return true;
1973 }
1974
1975 bool WebRtcVoiceMediaChannel::SetSendCodec(
1976 int channel, const webrtc::CodecInst& send_codec) {
1977 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1978 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1979
1980 webrtc::CodecInst current_codec = {0};
1981 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1982 (send_codec == current_codec)) {
1983 // Codec is already configured, we can return without setting it again.
1984 return true;
1985 }
1986
1987 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1988 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
1989 return false;
1990 }
1991 return true;
1992 }
1993
1994 void WebRtcVoiceMediaChannel::SetPlayout(bool playout) { 1934 void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1995 desired_playout_ = playout; 1935 desired_playout_ = playout;
1996 return ChangePlayout(desired_playout_); 1936 return ChangePlayout(desired_playout_);
1997 } 1937 }
1998 1938
1999 void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) { 1939 void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
2000 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout"); 1940 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
2001 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 1941 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2002 if (playout_ == playout) { 1942 if (playout_ == playout) {
2003 return; 1943 return;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
2093 return false; 2033 return false;
2094 } 2034 }
2095 2035
2096 // Save the channel to send_streams_, so that RemoveSendStream() can still 2036 // Save the channel to send_streams_, so that RemoveSendStream() can still
2097 // delete the channel in case failure happens below. 2037 // delete the channel in case failure happens below.
2098 webrtc::AudioTransport* audio_transport = 2038 webrtc::AudioTransport* audio_transport =
2099 engine()->voe()->base()->audio_transport(); 2039 engine()->voe()->base()->audio_transport();
2100 2040
2101 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream( 2041 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
2102 channel, audio_transport, ssrc, sp.cname, send_codec_spec_, 2042 channel, audio_transport, ssrc, sp.cname, send_codec_spec_,
2103 send_rtp_extensions_, call_, this); 2043 send_rtp_extensions_, max_send_bitrate_bps_, call_, this);
2104 send_streams_.insert(std::make_pair(ssrc, stream)); 2044 send_streams_.insert(std::make_pair(ssrc, stream));
2105 2045
2106 // Set the current codecs to be used for the new channel. We need to do this
2107 // after adding the channel to send_channels_, because of how max bitrate is
2108 // currently being configured by SetSendCodec().
2109 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
2110 RemoveSendStream(ssrc);
2111 return false;
2112 }
2113
2114 // At this point the stream's local SSRC has been updated. If it is the first 2046 // At this point the stream's local SSRC has been updated. If it is the first
2115 // send stream, make sure that all the receive streams are updated with the 2047 // send stream, make sure that all the receive streams are updated with the
2116 // same SSRC in order to send receiver reports. 2048 // same SSRC in order to send receiver reports.
2117 if (send_streams_.size() == 1) { 2049 if (send_streams_.size() == 1) {
2118 receiver_reports_ssrc_ = ssrc; 2050 receiver_reports_ssrc_ = ssrc;
2119 for (const auto& kv : recv_streams_) { 2051 for (const auto& kv : recv_streams_) {
2120 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive 2052 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
2121 // streams instead, so we can avoid recreating the streams here. 2053 // streams instead, so we can avoid recreating the streams here.
2122 kv.second->RecreateAudioReceiveStream(ssrc); 2054 kv.second->RecreateAudioReceiveStream(ssrc);
2123 int recv_channel = kv.second->channel(); 2055 int recv_channel = kv.second->channel();
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
2477 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing(); 2409 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
2478 if (ap) { 2410 if (ap) {
2479 ap->set_output_will_be_muted(all_muted); 2411 ap->set_output_will_be_muted(all_muted);
2480 } 2412 }
2481 return true; 2413 return true;
2482 } 2414 }
2483 2415
2484 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) { 2416 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2485 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate."; 2417 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2486 max_send_bitrate_bps_ = bps; 2418 max_send_bitrate_bps_ = bps;
2487 2419 bool success = true;
minyue-webrtc 2016/10/14 13:32:56 this is now possible to return false as the old co
2488 for (const auto& kv : send_streams_) { 2420 for (const auto& kv : send_streams_) {
2489 if (!SetChannelSendParameters(kv.second->channel(), 2421 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2490 kv.second->rtp_parameters())) { 2422 success = false;
2491 return false;
2492 } 2423 }
2493 } 2424 }
2494 return true; 2425 return success;
2495 }
2496
2497 bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
2498 int channel,
2499 const webrtc::RtpParameters& parameters) {
2500 RTC_CHECK_EQ(1UL, parameters.encodings.size());
2501 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2502 // different order (which should change the send codec).
2503 return SetMaxSendBitrate(
2504 channel, MinPositive(max_send_bitrate_bps_,
2505 parameters.encodings[0].max_bitrate_bps));
2506 }
2507
2508 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
2509 // Bitrate is auto by default.
2510 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2511 // SetMaxSendBandwith(0), the second call removes the previous limit.
2512 if (bps <= 0) {
2513 return true;
2514 }
2515
2516 if (!HasSendCodec()) {
2517 LOG(LS_INFO) << "The send codec has not been set up yet. "
2518 << "The send bitrate setting will be applied later.";
2519 return true;
2520 }
2521
2522 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
2523 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
2524
2525 if (is_multi_rate) {
2526 // If codec is multi-rate then just set the bitrate.
2527 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2528 codec.rate = std::min(bps, max_bitrate_bps);
2529 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2530 << " bps.";
2531 if (!SetSendCodec(channel, codec)) {
2532 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2533 << bps << " bps.";
2534 return false;
2535 }
2536 return true;
2537 } else {
2538 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2539 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2540 // fixed bitrate then ignore.
2541 if (bps < codec.rate) {
2542 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2543 << bps << " bps"
2544 << ", requires at least " << codec.rate << " bps.";
2545 return false;
2546 }
2547 return true;
2548 }
2549 } 2426 }
2550 2427
2551 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) { 2428 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2552 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 2429 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2553 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); 2430 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2554 call_->SignalChannelNetworkState( 2431 call_->SignalChannelNetworkState(
2555 webrtc::MediaType::AUDIO, 2432 webrtc::MediaType::AUDIO,
2556 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); 2433 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2557 } 2434 }
2558 2435
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
2664 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); 2541 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2665 const auto it = send_streams_.find(ssrc); 2542 const auto it = send_streams_.find(ssrc);
2666 if (it != send_streams_.end()) { 2543 if (it != send_streams_.end()) {
2667 return it->second->channel(); 2544 return it->second->channel();
2668 } 2545 }
2669 return -1; 2546 return -1;
2670 } 2547 }
2671 } // namespace cricket 2548 } // namespace cricket
2672 2549
2673 #endif // HAVE_WEBRTC_VOICE 2550 #endif // HAVE_WEBRTC_VOICE
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698