Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 config_.rtp.nack.rtp_history_ms = | 1144 config_.rtp.nack.rtp_history_ms = |
| 1175 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0; | 1145 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0; |
| 1146 config_.send_codec_spec = send_codec_spec; | |
| 1147 config_.send_codec_spec.codec_inst.rate = DecideSendBitrate(); | |
| 1176 RecreateAudioSendStream(); | 1148 RecreateAudioSendStream(); |
| 1177 } | 1149 } |
| 1178 | 1150 |
| 1179 void RecreateAudioSendStream( | 1151 void RecreateAudioSendStream( |
| 1180 const std::vector<webrtc::RtpExtension>& extensions) { | 1152 const std::vector<webrtc::RtpExtension>& extensions) { |
| 1181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1153 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1182 config_.rtp.extensions = extensions; | 1154 config_.rtp.extensions = extensions; |
| 1183 RecreateAudioSendStream(); | 1155 RecreateAudioSendStream(); |
| 1184 } | 1156 } |
| 1185 | 1157 |
| 1158 bool SetMaxSendBitrate(int bps) { | |
| 1159 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | |
| 1160 if (!IsMaxSendBitrateValid( | |
| 1161 MinPositive(bps, rtp_parameters_.encodings[0].max_bitrate_bps))) { | |
| 1162 return false; | |
| 1163 } | |
| 1164 max_send_bitrate_bps_ = bps; | |
| 1165 | |
| 1166 int new_sent_bitrate_bps = DecideSendBitrate(); | |
| 1167 if (config_.send_codec_spec.codec_inst.rate != new_sent_bitrate_bps) { | |
| 1168 // Recreate AudioSendStream with new bit rate. | |
| 1169 config_.send_codec_spec.codec_inst.rate = new_sent_bitrate_bps; | |
| 1170 RecreateAudioSendStream(); | |
| 1171 } | |
| 1172 return true; | |
| 1173 } | |
| 1174 | |
| 1186 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) { | 1175 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) { |
| 1187 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1188 RTC_DCHECK(stream_); | 1177 RTC_DCHECK(stream_); |
| 1189 return stream_->SendTelephoneEvent(payload_type, event, duration_ms); | 1178 return stream_->SendTelephoneEvent(payload_type, event, duration_ms); |
| 1190 } | 1179 } |
| 1191 | 1180 |
| 1192 void SetSend(bool send) { | 1181 void SetSend(bool send) { |
| 1193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1194 send_ = send; | 1183 send_ = send; |
| 1195 UpdateSendState(); | 1184 UpdateSendState(); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1268 // Accessor to the VoE channel ID. | 1257 // Accessor to the VoE channel ID. |
| 1269 int channel() const { | 1258 int channel() const { |
| 1270 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1259 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1271 return config_.voe_channel_id; | 1260 return config_.voe_channel_id; |
| 1272 } | 1261 } |
| 1273 | 1262 |
| 1274 const webrtc::RtpParameters& rtp_parameters() const { | 1263 const webrtc::RtpParameters& rtp_parameters() const { |
| 1275 return rtp_parameters_; | 1264 return rtp_parameters_; |
| 1276 } | 1265 } |
| 1277 | 1266 |
| 1278 void SetRtpParameters(const webrtc::RtpParameters& parameters) { | 1267 bool SetRtpParameters(const webrtc::RtpParameters& parameters) { |
| 1279 RTC_CHECK_EQ(1UL, parameters.encodings.size()); | 1268 RTC_CHECK_EQ(1UL, parameters.encodings.size()); |
| 1269 | |
| 1270 if (!IsMaxSendBitrateValid(MinPositive( | |
| 1271 max_send_bitrate_bps_, parameters.encodings[0].max_bitrate_bps))) { | |
| 1272 return false; | |
| 1273 } | |
| 1280 rtp_parameters_ = parameters; | 1274 rtp_parameters_ = parameters; |
| 1281 // parameters.encodings[0].active could have changed. | 1275 |
| 1282 UpdateSendState(); | 1276 // parameters.encodings[0].encodings[0].max_bitrate_bps could have changed. |
| 1277 int new_sent_bitrate_bps = DecideSendBitrate(); | |
| 1278 if (config_.send_codec_spec.codec_inst.rate != new_sent_bitrate_bps) { | |
| 1279 // Recreate AudioSendStream with new bit rate. | |
| 1280 config_.send_codec_spec.codec_inst.rate = new_sent_bitrate_bps; | |
| 1281 RecreateAudioSendStream(); | |
| 1282 } else { | |
| 1283 // parameters.encodings[0].active could have changed. | |
| 1284 UpdateSendState(); | |
| 1285 } | |
| 1286 return true; | |
| 1283 } | 1287 } |
| 1284 | 1288 |
| 1285 private: | 1289 private: |
| 1286 void UpdateSendState() { | 1290 void UpdateSendState() { |
| 1287 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1291 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1288 RTC_DCHECK(stream_); | 1292 RTC_DCHECK(stream_); |
| 1289 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size()); | 1293 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size()); |
| 1290 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) { | 1294 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) { |
| 1291 stream_->Start(); | 1295 stream_->Start(); |
| 1292 } else { // !send || source_ = nullptr | 1296 } else { // !send || source_ = nullptr |
| 1293 stream_->Stop(); | 1297 stream_->Stop(); |
| 1294 } | 1298 } |
| 1295 } | 1299 } |
| 1296 | 1300 |
| 1301 bool IsMaxSendBitrateValid(int bps) const { | |
|
the sun
2016/10/19 13:02:18
Is it possible to combine these two functions into
| |
| 1302 if (bps <= 0) { | |
| 1303 return true; | |
| 1304 } | |
| 1305 | |
| 1306 if (config_.send_codec_spec.codec_inst.pltype == -1) { | |
| 1307 LOG(LS_INFO) << "The send codec has not been set up yet. " | |
| 1308 << "The send bitrate setting will be applied later."; | |
| 1309 return true; | |
| 1310 } | |
| 1311 | |
| 1312 if (!WebRtcVoiceCodecs::IsCodecMultiRate( | |
| 1313 config_.send_codec_spec.codec_inst) && | |
| 1314 bps < config_.send_codec_spec.codec_inst.rate) { | |
| 1315 // If codec is not multi-rate and |max_send_bit_rate_| is less than the | |
| 1316 // fixed bitrate then fail. If codec is not multi-rate and |bps| exceeds | |
| 1317 // or | |
| 1318 // equal the fixed bitrate then ignore. | |
| 1319 LOG(LS_ERROR) << "Failed to set codec " | |
| 1320 << config_.send_codec_spec.codec_inst.plname | |
| 1321 << " to bitrate " << bps << " bps" | |
| 1322 << ", requires at least " | |
| 1323 << config_.send_codec_spec.codec_inst.rate << " bps."; | |
| 1324 return false; | |
| 1325 } | |
| 1326 | |
| 1327 return true; | |
| 1328 } | |
| 1329 | |
| 1330 int DecideSendBitrate() const { | |
|
the sun
2016/10/19 13:02:18
This should be a static function taking 3 paramete
| |
| 1331 const int bps = MinPositive(max_send_bitrate_bps_, | |
| 1332 rtp_parameters_.encodings[0].max_bitrate_bps); | |
|
the sun
2016/10/19 13:02:17
Should there be an RTC_DCHECK(IsMaxSendBitrateVali
| |
| 1333 const int current_rate = config_.send_codec_spec.codec_inst.rate; | |
|
the sun
2016/10/19 13:02:18
codec_rate would be more descriptive
| |
| 1334 | |
| 1335 // Bitrate is auto by default. | |
| 1336 // TODO(bemasc): Fix this so that if SetMaxSendBitrate(50) is followed by | |
| 1337 // SetMaxSendBitrate(0), the second call removes the previous limit. | |
| 1338 if (bps <= 0) { | |
| 1339 return current_rate; | |
| 1340 } | |
| 1341 | |
| 1342 if (config_.send_codec_spec.codec_inst.pltype == -1) { | |
| 1343 return current_rate; | |
| 1344 } | |
| 1345 | |
| 1346 if (!WebRtcVoiceCodecs::IsCodecMultiRate( | |
| 1347 config_.send_codec_spec.codec_inst)) { | |
| 1348 return current_rate; | |
| 1349 } | |
| 1350 | |
| 1351 // If codec is multi-rate then just set the bitrate. | |
| 1352 return std::min(bps, WebRtcVoiceCodecs::MaxBitrateBps( | |
| 1353 config_.send_codec_spec.codec_inst)); | |
| 1354 } | |
| 1355 | |
| 1297 void RecreateAudioSendStream() { | 1356 void RecreateAudioSendStream() { |
| 1298 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1357 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1299 if (stream_) { | 1358 if (stream_) { |
| 1300 call_->DestroyAudioSendStream(stream_); | 1359 call_->DestroyAudioSendStream(stream_); |
| 1301 stream_ = nullptr; | 1360 stream_ = nullptr; |
| 1302 } | 1361 } |
| 1303 RTC_DCHECK(!stream_); | 1362 RTC_DCHECK(!stream_); |
| 1304 if (webrtc::field_trial::FindFullName("WebRTC-AdaptAudioBitrate") == | 1363 if (webrtc::field_trial::FindFullName("WebRTC-AdaptAudioBitrate") == |
| 1305 "Enabled") { | 1364 "Enabled") { |
| 1306 // TODO(mflodman): Keep testing this and set proper values. | 1365 // TODO(mflodman): Keep testing this and set proper values. |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 1321 // The stream is owned by WebRtcAudioSendStream and may be reallocated if | 1380 // The stream is owned by WebRtcAudioSendStream and may be reallocated if |
| 1322 // configuration changes. | 1381 // configuration changes. |
| 1323 webrtc::AudioSendStream* stream_ = nullptr; | 1382 webrtc::AudioSendStream* stream_ = nullptr; |
| 1324 | 1383 |
| 1325 // Raw pointer to AudioSource owned by LocalAudioTrackHandler. | 1384 // Raw pointer to AudioSource owned by LocalAudioTrackHandler. |
| 1326 // PeerConnection will make sure invalidating the pointer before the object | 1385 // PeerConnection will make sure invalidating the pointer before the object |
| 1327 // goes away. | 1386 // goes away. |
| 1328 AudioSource* source_ = nullptr; | 1387 AudioSource* source_ = nullptr; |
| 1329 bool send_ = false; | 1388 bool send_ = false; |
| 1330 bool muted_ = false; | 1389 bool muted_ = false; |
| 1390 int max_send_bitrate_bps_; | |
| 1331 webrtc::RtpParameters rtp_parameters_; | 1391 webrtc::RtpParameters rtp_parameters_; |
| 1332 | 1392 |
| 1333 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream); | 1393 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream); |
| 1334 }; | 1394 }; |
| 1335 | 1395 |
| 1336 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream { | 1396 class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream { |
| 1337 public: | 1397 public: |
| 1338 WebRtcAudioReceiveStream( | 1398 WebRtcAudioReceiveStream( |
| 1339 int ch, | 1399 int ch, |
| 1340 uint32_t remote_ssrc, | 1400 uint32_t remote_ssrc, |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1581 | 1641 |
| 1582 // TODO(deadbeef): Handle setting parameters with a list of codecs in a | 1642 // TODO(deadbeef): Handle setting parameters with a list of codecs in a |
| 1583 // different order (which should change the send codec). | 1643 // different order (which should change the send codec). |
| 1584 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); | 1644 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); |
| 1585 if (current_parameters.codecs != parameters.codecs) { | 1645 if (current_parameters.codecs != parameters.codecs) { |
| 1586 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs " | 1646 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs " |
| 1587 << "is not currently supported."; | 1647 << "is not currently supported."; |
| 1588 return false; | 1648 return false; |
| 1589 } | 1649 } |
| 1590 | 1650 |
| 1591 if (!SetChannelSendParameters(it->second->channel(), parameters)) { | 1651 // TODO(minyue): The following legacy actions go into |
| 1592 LOG(LS_WARNING) << "Failed to set send RtpParameters."; | 1652 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end, |
| 1593 return false; | 1653 // though there are two difference: |
| 1594 } | 1654 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls |
| 1655 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls | |
| 1656 // |SetSendCodecs|. The outcome should be the same. | |
| 1657 // 2. AudioSendStream can be recreated. | |
| 1658 | |
| 1595 // Codecs are handled at the WebRtcVoiceMediaChannel level. | 1659 // Codecs are handled at the WebRtcVoiceMediaChannel level. |
| 1596 webrtc::RtpParameters reduced_params = parameters; | 1660 webrtc::RtpParameters reduced_params = parameters; |
| 1597 reduced_params.codecs.clear(); | 1661 reduced_params.codecs.clear(); |
| 1598 it->second->SetRtpParameters(reduced_params); | 1662 return it->second->SetRtpParameters(reduced_params); |
| 1599 return true; | |
| 1600 } | 1663 } |
| 1601 | 1664 |
| 1602 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters( | 1665 webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters( |
| 1603 uint32_t ssrc) const { | 1666 uint32_t ssrc) const { |
| 1604 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1667 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1605 auto it = recv_streams_.find(ssrc); | 1668 auto it = recv_streams_.find(ssrc); |
| 1606 if (it == recv_streams_.end()) { | 1669 if (it == recv_streams_.end()) { |
| 1607 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream " | 1670 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream " |
| 1608 << "with ssrc " << ssrc << " which doesn't exist."; | 1671 << "with ssrc " << ssrc << " which doesn't exist."; |
| 1609 return webrtc::RtpParameters(); | 1672 return webrtc::RtpParameters(); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1749 } | 1812 } |
| 1750 dtmf_payload_type_ = rtc::Optional<int>(codec.id); | 1813 dtmf_payload_type_ = rtc::Optional<int>(codec.id); |
| 1751 break; | 1814 break; |
| 1752 } | 1815 } |
| 1753 } | 1816 } |
| 1754 | 1817 |
| 1755 // Scan through the list to figure out the codec to use for sending, along | 1818 // 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 | 1819 // with the proper configuration for VAD, CNG, NACK and Opus-specific |
| 1757 // parameters. | 1820 // parameters. |
| 1758 // TODO(solenberg): Refactor this logic once we create AudioEncoders here. | 1821 // TODO(solenberg): Refactor this logic once we create AudioEncoders here. |
| 1759 SendCodecSpec send_codec_spec; | 1822 webrtc::AudioSendStream::Config::SendCodecSpec send_codec_spec; |
| 1760 { | 1823 { |
| 1761 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled; | 1824 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled; |
| 1762 | 1825 |
| 1763 // Find send codec (the first non-telephone-event/CN codec). | 1826 // Find send codec (the first non-telephone-event/CN codec). |
| 1764 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec( | 1827 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec( |
| 1765 codecs, &send_codec_spec.codec_inst); | 1828 codecs, &send_codec_spec.codec_inst); |
| 1766 if (!codec) { | 1829 if (!codec) { |
| 1767 LOG(LS_WARNING) << "Received empty list of codecs."; | 1830 LOG(LS_WARNING) << "Received empty list of codecs."; |
| 1768 return false; | 1831 return false; |
| 1769 } | 1832 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1823 break; | 1886 break; |
| 1824 } | 1887 } |
| 1825 } | 1888 } |
| 1826 } | 1889 } |
| 1827 | 1890 |
| 1828 // Apply new settings to all streams. | 1891 // Apply new settings to all streams. |
| 1829 if (send_codec_spec_ != send_codec_spec) { | 1892 if (send_codec_spec_ != send_codec_spec) { |
| 1830 send_codec_spec_ = std::move(send_codec_spec); | 1893 send_codec_spec_ = std::move(send_codec_spec); |
| 1831 for (const auto& kv : send_streams_) { | 1894 for (const auto& kv : send_streams_) { |
| 1832 kv.second->RecreateAudioSendStream(send_codec_spec_); | 1895 kv.second->RecreateAudioSendStream(send_codec_spec_); |
| 1833 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) { | |
| 1834 return false; | |
| 1835 } | |
| 1836 } | 1896 } |
| 1837 } | 1897 } |
| 1838 | 1898 |
| 1839 // Check if the transport cc feedback or NACK status has changed on the | 1899 // 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. | 1900 // preferred send codec, and in that case reconfigure all receive streams. |
| 1841 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled || | 1901 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled || |
| 1842 recv_nack_enabled_ != send_codec_spec_.nack_enabled) { | 1902 recv_nack_enabled_ != send_codec_spec_.nack_enabled) { |
| 1843 LOG(LS_INFO) << "Recreate all the receive streams because the send " | 1903 LOG(LS_INFO) << "Recreate all the receive streams because the send " |
| 1844 "codec has changed."; | 1904 "codec has changed."; |
| 1845 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled; | 1905 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled; |
| 1846 recv_nack_enabled_ = send_codec_spec_.nack_enabled; | 1906 recv_nack_enabled_ = send_codec_spec_.nack_enabled; |
| 1847 for (auto& kv : recv_streams_) { | 1907 for (auto& kv : recv_streams_) { |
| 1848 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_, | 1908 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_, |
| 1849 recv_nack_enabled_); | 1909 recv_nack_enabled_); |
| 1850 } | 1910 } |
| 1851 } | 1911 } |
| 1852 | 1912 |
| 1853 send_codecs_ = codecs; | 1913 send_codecs_ = codecs; |
| 1854 return true; | 1914 return true; |
| 1855 } | 1915 } |
| 1856 | 1916 |
| 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) { | 1917 void WebRtcVoiceMediaChannel::SetPlayout(bool playout) { |
| 1983 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetPlayout"); | 1918 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetPlayout"); |
| 1984 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 1919 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1985 if (playout_ == playout) { | 1920 if (playout_ == playout) { |
| 1986 return; | 1921 return; |
| 1987 } | 1922 } |
| 1988 | 1923 |
| 1989 for (const auto& kv : recv_streams_) { | 1924 for (const auto& kv : recv_streams_) { |
| 1990 kv.second->SetPlayout(playout); | 1925 kv.second->SetPlayout(playout); |
| 1991 } | 1926 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2076 return false; | 2011 return false; |
| 2077 } | 2012 } |
| 2078 | 2013 |
| 2079 // Save the channel to send_streams_, so that RemoveSendStream() can still | 2014 // Save the channel to send_streams_, so that RemoveSendStream() can still |
| 2080 // delete the channel in case failure happens below. | 2015 // delete the channel in case failure happens below. |
| 2081 webrtc::AudioTransport* audio_transport = | 2016 webrtc::AudioTransport* audio_transport = |
| 2082 engine()->voe()->base()->audio_transport(); | 2017 engine()->voe()->base()->audio_transport(); |
| 2083 | 2018 |
| 2084 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream( | 2019 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream( |
| 2085 channel, audio_transport, ssrc, sp.cname, send_codec_spec_, | 2020 channel, audio_transport, ssrc, sp.cname, send_codec_spec_, |
| 2086 send_rtp_extensions_, call_, this); | 2021 send_rtp_extensions_, max_send_bitrate_bps_, call_, this); |
| 2087 send_streams_.insert(std::make_pair(ssrc, stream)); | 2022 send_streams_.insert(std::make_pair(ssrc, stream)); |
| 2088 | 2023 |
| 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 | 2024 // 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 | 2025 // send stream, make sure that all the receive streams are updated with the |
| 2099 // same SSRC in order to send receiver reports. | 2026 // same SSRC in order to send receiver reports. |
| 2100 if (send_streams_.size() == 1) { | 2027 if (send_streams_.size() == 1) { |
| 2101 receiver_reports_ssrc_ = ssrc; | 2028 receiver_reports_ssrc_ = ssrc; |
| 2102 for (const auto& kv : recv_streams_) { | 2029 for (const auto& kv : recv_streams_) { |
| 2103 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive | 2030 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive |
| 2104 // streams instead, so we can avoid recreating the streams here. | 2031 // streams instead, so we can avoid recreating the streams here. |
| 2105 kv.second->RecreateAudioReceiveStream(ssrc); | 2032 kv.second->RecreateAudioReceiveStream(ssrc); |
| 2106 int recv_channel = kv.second->channel(); | 2033 int recv_channel = kv.second->channel(); |
| (...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2460 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing(); | 2387 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing(); |
| 2461 if (ap) { | 2388 if (ap) { |
| 2462 ap->set_output_will_be_muted(all_muted); | 2389 ap->set_output_will_be_muted(all_muted); |
| 2463 } | 2390 } |
| 2464 return true; | 2391 return true; |
| 2465 } | 2392 } |
| 2466 | 2393 |
| 2467 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) { | 2394 bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) { |
| 2468 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate."; | 2395 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate."; |
| 2469 max_send_bitrate_bps_ = bps; | 2396 max_send_bitrate_bps_ = bps; |
| 2470 | 2397 bool success = true; |
| 2471 for (const auto& kv : send_streams_) { | 2398 for (const auto& kv : send_streams_) { |
| 2472 if (!SetChannelSendParameters(kv.second->channel(), | 2399 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) { |
| 2473 kv.second->rtp_parameters())) { | 2400 success = false; |
| 2474 return false; | |
| 2475 } | 2401 } |
| 2476 } | 2402 } |
| 2477 return true; | 2403 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 } | 2404 } |
| 2533 | 2405 |
| 2534 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) { | 2406 void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) { |
| 2535 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 2407 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 2536 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); | 2408 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 2537 call_->SignalChannelNetworkState( | 2409 call_->SignalChannelNetworkState( |
| 2538 webrtc::MediaType::AUDIO, | 2410 webrtc::MediaType::AUDIO, |
| 2539 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); | 2411 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); |
| 2540 } | 2412 } |
| 2541 | 2413 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2647 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 2519 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 2648 const auto it = send_streams_.find(ssrc); | 2520 const auto it = send_streams_.find(ssrc); |
| 2649 if (it != send_streams_.end()) { | 2521 if (it != send_streams_.end()) { |
| 2650 return it->second->channel(); | 2522 return it->second->channel(); |
| 2651 } | 2523 } |
| 2652 return -1; | 2524 return -1; |
| 2653 } | 2525 } |
| 2654 } // namespace cricket | 2526 } // namespace cricket |
| 2655 | 2527 |
| 2656 #endif // HAVE_WEBRTC_VOICE | 2528 #endif // HAVE_WEBRTC_VOICE |
| OLD | NEW |