| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/audio/audio_send_stream.h" | 11 #include "webrtc/audio/audio_send_stream.h" |
| 12 | 12 |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> |
| 15 #include <vector> |
| 14 | 16 |
| 15 #include "webrtc/audio/audio_state.h" | 17 #include "webrtc/audio/audio_state.h" |
| 16 #include "webrtc/audio/conversion.h" | 18 #include "webrtc/audio/conversion.h" |
| 17 #include "webrtc/audio/scoped_voe_interface.h" | 19 #include "webrtc/audio/scoped_voe_interface.h" |
| 18 #include "webrtc/base/checks.h" | 20 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/event.h" | 21 #include "webrtc/base/event.h" |
| 22 #include "webrtc/base/function_view.h" |
| 20 #include "webrtc/base/logging.h" | 23 #include "webrtc/base/logging.h" |
| 21 #include "webrtc/base/task_queue.h" | 24 #include "webrtc/base/task_queue.h" |
| 22 #include "webrtc/base/timeutils.h" | 25 #include "webrtc/base/timeutils.h" |
| 23 #include "webrtc/call/rtp_transport_controller_send.h" | 26 #include "webrtc/call/rtp_transport_controller_send.h" |
| 27 #include "webrtc/modules/audio_coding/codecs/cng/audio_encoder_cng.h" |
| 24 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" | 28 #include "webrtc/modules/bitrate_controller/include/bitrate_controller.h" |
| 25 #include "webrtc/modules/congestion_controller/include/send_side_congestion_cont
roller.h" | 29 #include "webrtc/modules/congestion_controller/include/send_side_congestion_cont
roller.h" |
| 26 #include "webrtc/modules/pacing/paced_sender.h" | 30 #include "webrtc/modules/pacing/paced_sender.h" |
| 27 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 31 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 28 #include "webrtc/voice_engine/channel_proxy.h" | 32 #include "webrtc/voice_engine/channel_proxy.h" |
| 29 #include "webrtc/voice_engine/include/voe_base.h" | 33 #include "webrtc/voice_engine/include/voe_base.h" |
| 30 #include "webrtc/voice_engine/transmit_mixer.h" | 34 #include "webrtc/voice_engine/transmit_mixer.h" |
| 31 #include "webrtc/voice_engine/voice_engine_impl.h" | 35 #include "webrtc/voice_engine/voice_engine_impl.h" |
| 32 | 36 |
| 33 namespace webrtc { | 37 namespace webrtc { |
| 34 | 38 |
| 35 namespace { | |
| 36 | |
| 37 constexpr char kOpusCodecName[] = "opus"; | |
| 38 | |
| 39 bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { | |
| 40 return (STR_CASE_CMP(codec.plname, ref_name) == 0); | |
| 41 } | |
| 42 } // namespace | |
| 43 | |
| 44 namespace internal { | 39 namespace internal { |
| 45 // TODO(elad.alon): Subsequent CL will make these values experiment-dependent. | 40 // TODO(elad.alon): Subsequent CL will make these values experiment-dependent. |
| 46 constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000; | 41 constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000; |
| 47 constexpr size_t kPacketLossRateMinNumAckedPackets = 50; | 42 constexpr size_t kPacketLossRateMinNumAckedPackets = 50; |
| 48 constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40; | 43 constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40; |
| 49 | 44 |
| 45 namespace { |
| 46 void CallEncoder(const std::unique_ptr<voe::ChannelProxy>& channel_proxy, |
| 47 rtc::FunctionView<void(AudioEncoder*)> lambda) { |
| 48 channel_proxy->ModifyEncoder( |
| 49 [&lambda](std::unique_ptr<AudioEncoder>* encoder_ptr) { |
| 50 RTC_DCHECK(encoder_ptr); |
| 51 lambda(encoder_ptr->get()); |
| 52 }); |
| 53 } |
| 54 } |
| 55 |
| 50 AudioSendStream::AudioSendStream( | 56 AudioSendStream::AudioSendStream( |
| 51 const webrtc::AudioSendStream::Config& config, | 57 const webrtc::AudioSendStream::Config& config, |
| 52 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, | 58 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
| 53 rtc::TaskQueue* worker_queue, | 59 rtc::TaskQueue* worker_queue, |
| 54 RtpTransportControllerSendInterface* transport, | 60 RtpTransportControllerSendInterface* transport, |
| 55 BitrateAllocator* bitrate_allocator, | 61 BitrateAllocator* bitrate_allocator, |
| 56 RtcEventLog* event_log, | 62 RtcEventLog* event_log, |
| 57 RtcpRttStats* rtcp_rtt_stats) | 63 RtcpRttStats* rtcp_rtt_stats) |
| 58 : worker_queue_(worker_queue), | 64 : worker_queue_(worker_queue), |
| 59 config_(config), | 65 config_(config), |
| 60 audio_state_(audio_state), | 66 audio_state_(audio_state), |
| 67 event_log_(event_log), |
| 61 bitrate_allocator_(bitrate_allocator), | 68 bitrate_allocator_(bitrate_allocator), |
| 62 transport_(transport), | 69 transport_(transport), |
| 63 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs, | 70 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs, |
| 64 kPacketLossRateMinNumAckedPackets, | 71 kPacketLossRateMinNumAckedPackets, |
| 65 kRecoverablePacketLossRateMinNumAckedPairs) { | 72 kRecoverablePacketLossRateMinNumAckedPairs) { |
| 66 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); | 73 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
| 67 RTC_DCHECK_NE(config_.voe_channel_id, -1); | 74 RTC_DCHECK_NE(config_.voe_channel_id, -1); |
| 68 RTC_DCHECK(audio_state_.get()); | 75 RTC_DCHECK(audio_state_.get()); |
| 69 RTC_DCHECK(transport); | 76 RTC_DCHECK(transport); |
| 70 RTC_DCHECK(transport->send_side_cc()); | 77 RTC_DCHECK(transport->send_side_cc()); |
| 71 | 78 |
| 72 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); | 79 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); |
| 73 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); | 80 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); |
| 74 channel_proxy_->SetRtcEventLog(event_log); | 81 channel_proxy_->SetRtcEventLog(event_log_); |
| 75 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); | 82 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats); |
| 76 channel_proxy_->SetRTCPStatus(true); | 83 channel_proxy_->SetRTCPStatus(true); |
| 77 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); | 84 channel_proxy_->SetLocalSSRC(config.rtp.ssrc); |
| 78 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); | 85 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); |
| 79 // TODO(solenberg): Config NACK history window (which is a packet count), | 86 // TODO(solenberg): Config NACK history window (which is a packet count), |
| 80 // using the actual packet size for the configured codec. | 87 // using the actual packet size for the configured codec. |
| 81 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, | 88 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, |
| 82 config_.rtp.nack.rtp_history_ms / 20); | 89 config_.rtp.nack.rtp_history_ms / 20); |
| 83 | 90 |
| 84 channel_proxy_->RegisterExternalTransport(config.send_transport); | 91 channel_proxy_->RegisterExternalTransport(config.send_transport); |
| 85 transport_->send_side_cc()->RegisterPacketFeedbackObserver(this); | 92 transport_->send_side_cc()->RegisterPacketFeedbackObserver(this); |
| 86 | 93 |
| 87 for (const auto& extension : config.rtp.extensions) { | 94 for (const auto& extension : config.rtp.extensions) { |
| 88 if (extension.uri == RtpExtension::kAudioLevelUri) { | 95 if (extension.uri == RtpExtension::kAudioLevelUri) { |
| 89 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); | 96 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); |
| 90 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { | 97 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
| 91 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); | 98 channel_proxy_->EnableSendTransportSequenceNumber(extension.id); |
| 92 transport->send_side_cc()->EnablePeriodicAlrProbing(true); | 99 transport->send_side_cc()->EnablePeriodicAlrProbing(true); |
| 93 bandwidth_observer_.reset(transport->send_side_cc() | 100 bandwidth_observer_.reset(transport->send_side_cc() |
| 94 ->GetBitrateController() | 101 ->GetBitrateController() |
| 95 ->CreateRtcpBandwidthObserver()); | 102 ->CreateRtcpBandwidthObserver()); |
| 96 } else { | 103 } else { |
| 97 RTC_NOTREACHED() << "Registering unsupported RTP extension."; | 104 RTC_NOTREACHED() << "Registering unsupported RTP extension."; |
| 98 } | 105 } |
| 99 } | 106 } |
| 100 channel_proxy_->RegisterSenderCongestionControlObjects( | 107 channel_proxy_->RegisterSenderCongestionControlObjects( |
| 101 transport, bandwidth_observer_.get()); | 108 transport, bandwidth_observer_.get()); |
| 102 if (!SetupSendCodec()) { | 109 if (config_.send_codec_spec && !SetupSendCodec(config_)) { |
| 103 LOG(LS_ERROR) << "Failed to set up send codec state."; | 110 LOG(LS_ERROR) << "Failed to set up send codec state."; |
| 104 } | 111 } |
| 105 | 112 |
| 106 pacer_thread_checker_.DetachFromThread(); | 113 pacer_thread_checker_.DetachFromThread(); |
| 107 } | 114 } |
| 108 | 115 |
| 109 AudioSendStream::~AudioSendStream() { | 116 AudioSendStream::~AudioSendStream() { |
| 110 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 117 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 111 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); | 118 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
| 112 transport_->send_side_cc()->DeRegisterPacketFeedbackObserver(this); | 119 transport_->send_side_cc()->DeRegisterPacketFeedbackObserver(this); |
| 113 channel_proxy_->DeRegisterExternalTransport(); | 120 channel_proxy_->DeRegisterExternalTransport(); |
| 114 channel_proxy_->ResetSenderCongestionControlObjects(); | 121 channel_proxy_->ResetSenderCongestionControlObjects(); |
| 115 channel_proxy_->SetRtcEventLog(nullptr); | 122 channel_proxy_->SetRtcEventLog(nullptr); |
| 116 channel_proxy_->SetRtcpRttStats(nullptr); | 123 channel_proxy_->SetRtcpRttStats(nullptr); |
| 117 } | 124 } |
| 118 | 125 |
| 126 void AudioSendStream::Reconfigure( |
| 127 const webrtc::AudioSendStream::Config& new_config) { |
| 128 LOG(LS_INFO) << "AudioSendStream::Reconfigure: " << new_config.ToString(); |
| 129 // TODO(ossu): Really enforce SSRC here? |
| 130 RTC_CHECK_EQ(config_.rtp.ssrc, new_config.rtp.ssrc); |
| 131 if (new_config.rtp.c_name != config_.rtp.c_name) { |
| 132 channel_proxy_->SetRTCP_CNAME(new_config.rtp.c_name); |
| 133 } |
| 134 if (new_config.rtp.nack.rtp_history_ms != config_.rtp.nack.rtp_history_ms) { |
| 135 // TODO(solenberg): Config NACK history window (which is a packet count), |
| 136 // using the actual packet size for the configured codec. |
| 137 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, |
| 138 config_.rtp.nack.rtp_history_ms / 20); |
| 139 } |
| 140 |
| 141 if (new_config.send_transport != config_.send_transport) { |
| 142 channel_proxy_->DeRegisterExternalTransport(); |
| 143 channel_proxy_->RegisterExternalTransport(new_config.send_transport); |
| 144 } |
| 145 |
| 146 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is |
| 147 // reserved for padding and MUST NOT be used as a local identifier. |
| 148 struct ExtensionIds { |
| 149 int audio_level = 0; |
| 150 int transport_sequence_number = 0; |
| 151 }; |
| 152 |
| 153 auto find_extension_ids = [](const std::vector<RtpExtension>& extensions) { |
| 154 ExtensionIds ids; |
| 155 for (const auto& extension : extensions) { |
| 156 if (extension.uri == RtpExtension::kAudioLevelUri) { |
| 157 ids.audio_level = extension.id; |
| 158 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
| 159 ids.transport_sequence_number = extension.id; |
| 160 } |
| 161 } |
| 162 return ids; |
| 163 }; |
| 164 |
| 165 const ExtensionIds old_ids = find_extension_ids(config_.rtp.extensions); |
| 166 const ExtensionIds new_ids = find_extension_ids(new_config.rtp.extensions); |
| 167 // Audio level indication |
| 168 if (new_ids.audio_level != old_ids.audio_level) { |
| 169 channel_proxy_->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0, |
| 170 new_ids.audio_level); |
| 171 } |
| 172 // Transport sequence number |
| 173 if (new_ids.transport_sequence_number != old_ids.transport_sequence_number) { |
| 174 channel_proxy_->ResetSenderCongestionControlObjects(); |
| 175 |
| 176 if (new_ids.transport_sequence_number != 0) { |
| 177 channel_proxy_->EnableSendTransportSequenceNumber( |
| 178 new_ids.transport_sequence_number); |
| 179 transport_->send_side_cc()->EnablePeriodicAlrProbing(true); |
| 180 bandwidth_observer_.reset(transport_->send_side_cc() |
| 181 ->GetBitrateController() |
| 182 ->CreateRtcpBandwidthObserver()); |
| 183 } else { |
| 184 bandwidth_observer_.reset(); |
| 185 } |
| 186 |
| 187 channel_proxy_->RegisterSenderCongestionControlObjects( |
| 188 transport_, bandwidth_observer_.get()); |
| 189 } |
| 190 |
| 191 ReconfigureSendCodec(new_config); |
| 192 ReconfigureBitrateObserver(new_config); |
| 193 |
| 194 config_ = new_config; |
| 195 } |
| 196 |
| 119 void AudioSendStream::Start() { | 197 void AudioSendStream::Start() { |
| 120 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 198 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 121 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { | 199 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) { |
| 122 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps); | 200 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps); |
| 123 rtc::Event thread_sync_event(false /* manual_reset */, false); | |
| 124 worker_queue_->PostTask([this, &thread_sync_event] { | |
| 125 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps, | |
| 126 config_.max_bitrate_bps, 0, true); | |
| 127 thread_sync_event.Set(); | |
| 128 }); | |
| 129 thread_sync_event.Wait(rtc::Event::kForever); | |
| 130 } | 201 } |
| 131 | 202 |
| 132 ScopedVoEInterface<VoEBase> base(voice_engine()); | 203 ScopedVoEInterface<VoEBase> base(voice_engine()); |
| 133 int error = base->StartSend(config_.voe_channel_id); | 204 int error = base->StartSend(config_.voe_channel_id); |
| 134 if (error != 0) { | 205 if (error != 0) { |
| 135 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; | 206 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; |
| 136 } | 207 } |
| 137 } | 208 } |
| 138 | 209 |
| 139 void AudioSendStream::Stop() { | 210 void AudioSendStream::Stop() { |
| 140 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); | 211 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 141 rtc::Event thread_sync_event(false /* manual_reset */, false); | 212 RemoveBitrateObserver(); |
| 142 worker_queue_->PostTask([this, &thread_sync_event] { | |
| 143 bitrate_allocator_->RemoveObserver(this); | |
| 144 thread_sync_event.Set(); | |
| 145 }); | |
| 146 thread_sync_event.Wait(rtc::Event::kForever); | |
| 147 | 213 |
| 148 ScopedVoEInterface<VoEBase> base(voice_engine()); | 214 ScopedVoEInterface<VoEBase> base(voice_engine()); |
| 149 int error = base->StopSend(config_.voe_channel_id); | 215 int error = base->StopSend(config_.voe_channel_id); |
| 150 if (error != 0) { | 216 if (error != 0) { |
| 151 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; | 217 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; |
| 152 } | 218 } |
| 153 } | 219 } |
| 154 | 220 |
| 155 bool AudioSendStream::SendTelephoneEvent(int payload_type, | 221 bool AudioSendStream::SendTelephoneEvent(int payload_type, |
| 156 int payload_frequency, int event, | 222 int payload_frequency, int event, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 176 stats.packets_sent = call_stats.packetsSent; | 242 stats.packets_sent = call_stats.packetsSent; |
| 177 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine | 243 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine |
| 178 // returns 0 to indicate an error value. | 244 // returns 0 to indicate an error value. |
| 179 if (call_stats.rttMs > 0) { | 245 if (call_stats.rttMs > 0) { |
| 180 stats.rtt_ms = call_stats.rttMs; | 246 stats.rtt_ms = call_stats.rttMs; |
| 181 } | 247 } |
| 182 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable | 248 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable |
| 183 // implementation. | 249 // implementation. |
| 184 stats.aec_quality_min = -1; | 250 stats.aec_quality_min = -1; |
| 185 | 251 |
| 186 webrtc::CodecInst codec_inst = {0}; | 252 if (config_.send_codec_spec) { |
| 187 if (channel_proxy_->GetSendCodec(&codec_inst)) { | 253 const auto& spec = *config_.send_codec_spec; |
| 188 RTC_DCHECK_NE(codec_inst.pltype, -1); | 254 stats.codec_name = spec.format.name; |
| 189 stats.codec_name = codec_inst.plname; | 255 stats.codec_payload_type = rtc::Optional<int>(spec.payload_type); |
| 190 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype); | |
| 191 | 256 |
| 192 // Get data from the last remote RTCP report. | 257 // Get data from the last remote RTCP report. |
| 193 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) { | 258 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) { |
| 194 // Lookup report for send ssrc only. | 259 // Lookup report for send ssrc only. |
| 195 if (block.source_SSRC == stats.local_ssrc) { | 260 if (block.source_SSRC == stats.local_ssrc) { |
| 196 stats.packets_lost = block.cumulative_num_packets_lost; | 261 stats.packets_lost = block.cumulative_num_packets_lost; |
| 197 stats.fraction_lost = Q8ToFloat(block.fraction_lost); | 262 stats.fraction_lost = Q8ToFloat(block.fraction_lost); |
| 198 stats.ext_seqnum = block.extended_highest_sequence_number; | 263 stats.ext_seqnum = block.extended_highest_sequence_number; |
| 199 // Convert samples to milliseconds. | 264 // Convert timestamps to milliseconds. |
| 200 if (codec_inst.plfreq / 1000 > 0) { | 265 if (spec.format.clockrate_hz / 1000 > 0) { |
| 201 stats.jitter_ms = | 266 stats.jitter_ms = |
| 202 block.interarrival_jitter / (codec_inst.plfreq / 1000); | 267 block.interarrival_jitter / (spec.format.clockrate_hz / 1000); |
| 203 } | 268 } |
| 204 break; | 269 break; |
| 205 } | 270 } |
| 206 } | 271 } |
| 207 } | 272 } |
| 208 | 273 |
| 209 ScopedVoEInterface<VoEBase> base(voice_engine()); | 274 ScopedVoEInterface<VoEBase> base(voice_engine()); |
| 210 RTC_DCHECK(base->transmit_mixer()); | 275 RTC_DCHECK(base->transmit_mixer()); |
| 211 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange(); | 276 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange(); |
| 212 RTC_DCHECK_LE(0, stats.audio_level); | 277 RTC_DCHECK_LE(0, stats.audio_level); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 376 |
| 312 VoiceEngine* AudioSendStream::voice_engine() const { | 377 VoiceEngine* AudioSendStream::voice_engine() const { |
| 313 internal::AudioState* audio_state = | 378 internal::AudioState* audio_state = |
| 314 static_cast<internal::AudioState*>(audio_state_.get()); | 379 static_cast<internal::AudioState*>(audio_state_.get()); |
| 315 VoiceEngine* voice_engine = audio_state->voice_engine(); | 380 VoiceEngine* voice_engine = audio_state->voice_engine(); |
| 316 RTC_DCHECK(voice_engine); | 381 RTC_DCHECK(voice_engine); |
| 317 return voice_engine; | 382 return voice_engine; |
| 318 } | 383 } |
| 319 | 384 |
| 320 // Apply current codec settings to a single voe::Channel used for sending. | 385 // Apply current codec settings to a single voe::Channel used for sending. |
| 321 bool AudioSendStream::SetupSendCodec() { | 386 bool AudioSendStream::SetupSendCodec(const Config& config) { |
| 322 // Disable VAD and FEC unless we know the other side wants them. | 387 RTC_DCHECK(config.send_codec_spec); |
| 323 channel_proxy_->SetVADStatus(false); | 388 // Explicitly hide config_ here, so we don't accidentally setup a send codec |
| 324 channel_proxy_->SetCodecFECStatus(false); | 389 // with old parameters. |
| 390 auto setup_encoder = [](const Config& config, RtcEventLog* event_log) { |
| 391 const auto& spec = *config.send_codec_spec; |
| 392 std::unique_ptr<AudioEncoder> encoder = |
| 393 config.encoder_factory->MakeAudioEncoder(spec.payload_type, |
| 394 spec.format); |
| 325 | 395 |
| 326 // We disable audio network adaptor here. This will on one hand make sure that | 396 if (!encoder) { |
| 327 // audio network adaptor is disabled by default, and on the other allow audio | 397 LOG(LS_ERROR) << "Unable to create encoder for " << spec.format; |
| 328 // network adaptor to be reconfigured, since SetReceiverFrameLengthRange can | 398 return encoder; |
| 329 // be only called when audio network adaptor is disabled. | |
| 330 channel_proxy_->DisableAudioNetworkAdaptor(); | |
| 331 | |
| 332 const auto& send_codec_spec = config_.send_codec_spec; | |
| 333 | |
| 334 // We set the codec first, since the below extra configuration is only applied | |
| 335 // to the "current" codec. | |
| 336 | |
| 337 // If codec is already configured, we do not it again. | |
| 338 // TODO(minyue): check if this check is really needed, or can we move it into | |
| 339 // |codec->SetSendCodec|. | |
| 340 webrtc::CodecInst current_codec = {0}; | |
| 341 if (!channel_proxy_->GetSendCodec(¤t_codec) || | |
| 342 (send_codec_spec.codec_inst != current_codec)) { | |
| 343 if (!channel_proxy_->SetSendCodec(send_codec_spec.codec_inst)) { | |
| 344 LOG(LS_WARNING) << "SetSendCodec() failed."; | |
| 345 return false; | |
| 346 } | 399 } |
| 347 } | 400 // If a bitrate has been specified for the codec, use it over the |
| 348 | 401 // codec's default. |
| 349 // Codec internal FEC. Treat any failure as fatal internal error. | 402 if (spec.target_bitrate_bps) { |
| 350 if (send_codec_spec.enable_codec_fec) { | 403 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps); |
| 351 if (!channel_proxy_->SetCodecFECStatus(true)) { | |
| 352 LOG(LS_WARNING) << "SetCodecFECStatus() failed."; | |
| 353 return false; | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 // DTX and maxplaybackrate are only set if current codec is Opus. | |
| 358 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) { | |
| 359 if (!channel_proxy_->SetOpusDtx(send_codec_spec.enable_opus_dtx)) { | |
| 360 LOG(LS_WARNING) << "SetOpusDtx() failed."; | |
| 361 return false; | |
| 362 } | 404 } |
| 363 | 405 |
| 364 // If opus_max_playback_rate <= 0, the default maximum playback rate | 406 // Enable ANA if configured (currently only used by Opus). |
| 365 // (48 kHz) will be used. | 407 if (config.audio_network_adaptor_config) { |
| 366 if (send_codec_spec.opus_max_playback_rate > 0) { | 408 if (encoder->EnableAudioNetworkAdaptor( |
| 367 if (!channel_proxy_->SetOpusMaxPlaybackRate( | 409 *config.audio_network_adaptor_config, event_log, |
| 368 send_codec_spec.opus_max_playback_rate)) { | 410 Clock::GetRealTimeClock())) { |
| 369 LOG(LS_WARNING) << "SetOpusMaxPlaybackRate() failed."; | 411 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
| 370 return false; | 412 << config.rtp.ssrc; |
| 413 } else { |
| 414 RTC_NOTREACHED(); |
| 371 } | 415 } |
| 372 } | 416 } |
| 373 | 417 |
| 374 if (config_.audio_network_adaptor_config) { | 418 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled. |
| 375 // Audio network adaptor is only allowed for Opus currently. | 419 if (spec.cng_payload_type) { |
| 376 // |SetReceiverFrameLengthRange| needs to be called before | 420 AudioEncoderCng::Config cng_config; |
| 377 // |EnableAudioNetworkAdaptor|. | 421 cng_config.num_channels = encoder->NumChannels(); |
| 378 channel_proxy_->SetReceiverFrameLengthRange(send_codec_spec.min_ptime_ms, | 422 cng_config.payload_type = *spec.cng_payload_type; |
| 379 send_codec_spec.max_ptime_ms); | 423 cng_config.speech_encoder = std::move(encoder); |
| 380 channel_proxy_->EnableAudioNetworkAdaptor( | 424 cng_config.vad_mode = Vad::kVadNormal; |
| 381 *config_.audio_network_adaptor_config); | 425 encoder.reset(new AudioEncoderCng(std::move(cng_config))); |
| 382 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " | |
| 383 << config_.rtp.ssrc; | |
| 384 } | 426 } |
| 427 |
| 428 return encoder; |
| 429 }; |
| 430 |
| 431 auto encoder = setup_encoder(config, event_log_); |
| 432 if (!encoder) { |
| 433 return false; |
| 434 } |
| 435 channel_proxy_->SetEncoder(config.send_codec_spec->payload_type, |
| 436 std::move(encoder)); |
| 437 return true; |
| 438 } |
| 439 |
| 440 bool AudioSendStream::ReconfigureSendCodec(const Config& new_config) { |
| 441 if (new_config.send_codec_spec == config_.send_codec_spec) { |
| 442 return true; |
| 385 } | 443 } |
| 386 | 444 |
| 387 // Set the CN payloadtype and the VAD status. | 445 // If we have no encoder, or the format or payload type's changed, create a |
| 388 if (send_codec_spec.cng_payload_type != -1) { | 446 // new encoder. |
| 389 // The CN payload type for 8000 Hz clockrate is fixed at 13. | 447 if (!config_.send_codec_spec || |
| 390 if (send_codec_spec.cng_plfreq != 8000) { | 448 new_config.send_codec_spec->format != config_.send_codec_spec->format || |
| 391 webrtc::PayloadFrequencies cn_freq; | 449 new_config.send_codec_spec->payload_type != |
| 392 switch (send_codec_spec.cng_plfreq) { | 450 config_.send_codec_spec->payload_type) { |
| 393 case 16000: | 451 return SetupSendCodec(new_config); |
| 394 cn_freq = webrtc::kFreq16000Hz; | 452 } |
| 395 break; | 453 |
| 396 case 32000: | 454 if (!new_config.send_codec_spec) { |
| 397 cn_freq = webrtc::kFreq32000Hz; | 455 // TODO(ossu): Double-check this! |
| 398 break; | 456 LOG(LS_ERROR) << "Cannot replace the current encoder with no encoder"; |
| 399 default: | 457 RTC_NOTREACHED(); |
| 400 RTC_NOTREACHED(); | 458 return false; |
| 401 return false; | 459 } |
| 460 |
| 461 const rtc::Optional<int>& new_target_bitrate_bps = |
| 462 new_config.send_codec_spec->target_bitrate_bps; |
| 463 // If a bitrate has been specified for the codec, use it over the |
| 464 // codec's default. |
| 465 if (new_target_bitrate_bps && |
| 466 new_target_bitrate_bps != config_.send_codec_spec->target_bitrate_bps) { |
| 467 CallEncoder(channel_proxy_, [&](AudioEncoder* encoder) { |
| 468 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps); |
| 469 }); |
| 470 } |
| 471 |
| 472 ReconfigureANA(new_config); |
| 473 ReconfigureCNG(new_config); |
| 474 |
| 475 return true; |
| 476 } |
| 477 |
| 478 void AudioSendStream::ReconfigureANA(const Config& new_config) { |
| 479 if (new_config.audio_network_adaptor_config == |
| 480 config_.audio_network_adaptor_config) { |
| 481 return; |
| 482 } |
| 483 if (new_config.audio_network_adaptor_config) { |
| 484 CallEncoder(channel_proxy_, [&](AudioEncoder* encoder) { |
| 485 if (encoder->EnableAudioNetworkAdaptor( |
| 486 *new_config.audio_network_adaptor_config, event_log_, |
| 487 Clock::GetRealTimeClock())) { |
| 488 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
| 489 << new_config.rtp.ssrc; |
| 490 } else { |
| 491 RTC_NOTREACHED(); |
| 402 } | 492 } |
| 403 if (!channel_proxy_->SetSendCNPayloadType( | 493 }); |
| 404 send_codec_spec.cng_payload_type, cn_freq)) { | 494 } else { |
| 405 LOG(LS_WARNING) << "SetSendCNPayloadType() failed."; | 495 CallEncoder(channel_proxy_, [&](AudioEncoder* encoder) { |
| 406 // TODO(ajm): This failure condition will be removed from VoE. | 496 encoder->DisableAudioNetworkAdaptor(); |
| 407 // Restore the return here when we update to a new enough webrtc. | 497 }); |
| 408 // | 498 LOG(LS_INFO) << "Audio network adaptor disabled on SSRC " |
| 409 // Not returning false because the SetSendCNPayloadType will fail if | 499 << new_config.rtp.ssrc; |
| 410 // the channel is already sending. | 500 } |
| 411 // This can happen if the remote description is applied twice, for | 501 } |
| 412 // example in the case of ROAP on top of JSEP, where both side will | |
| 413 // send the offer. | |
| 414 } | |
| 415 } | |
| 416 | 502 |
| 417 // Only turn on VAD if we have a CN payload type that matches the | 503 void AudioSendStream::ReconfigureCNG(const Config& new_config) { |
| 418 // clockrate for the codec we are going to use. | 504 if (new_config.send_codec_spec->cng_payload_type == |
| 419 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq && | 505 config_.send_codec_spec->cng_payload_type) { |
| 420 send_codec_spec.codec_inst.channels == 1) { | 506 return; |
| 421 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the | |
| 422 // interaction between VAD and Opus FEC. | |
| 423 if (!channel_proxy_->SetVADStatus(true)) { | |
| 424 LOG(LS_WARNING) << "SetVADStatus() failed."; | |
| 425 return false; | |
| 426 } | |
| 427 } | |
| 428 } | 507 } |
| 429 return true; | 508 |
| 509 // Wrap or unwrap the encoder in an AudioEncoderCNG. |
| 510 channel_proxy_->ModifyEncoder( |
| 511 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) { |
| 512 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr)); |
| 513 auto sub_encoders = old_encoder->ReclaimContainedEncoders(); |
| 514 if (!sub_encoders.empty()) { |
| 515 // Replace enc with its sub encoder. We need to put the sub |
| 516 // encoder in a temporary first, since otherwise the old value |
| 517 // of enc would be destroyed before the new value got assigned, |
| 518 // which would be bad since the new value is a part of the old |
| 519 // value. |
| 520 auto tmp = std::move(sub_encoders[0]); |
| 521 old_encoder = std::move(tmp); |
| 522 } |
| 523 if (new_config.send_codec_spec->cng_payload_type) { |
| 524 AudioEncoderCng::Config config; |
| 525 config.speech_encoder = std::move(old_encoder); |
| 526 config.num_channels = config.speech_encoder->NumChannels(); |
| 527 config.payload_type = *new_config.send_codec_spec->cng_payload_type; |
| 528 config.vad_mode = Vad::kVadNormal; |
| 529 encoder_ptr->reset(new AudioEncoderCng(std::move(config))); |
| 530 } else { |
| 531 *encoder_ptr = std::move(old_encoder); |
| 532 } |
| 533 }); |
| 534 } |
| 535 |
| 536 void AudioSendStream::ReconfigureBitrateObserver( |
| 537 const webrtc::AudioSendStream::Config& new_config) { |
| 538 if (config_.min_bitrate_bps == new_config.min_bitrate_bps && |
| 539 config_.max_bitrate_bps == new_config.max_bitrate_bps) { |
| 540 return; |
| 541 } |
| 542 |
| 543 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1) { |
| 544 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps); |
| 545 } else { |
| 546 RemoveBitrateObserver(); |
| 547 } |
| 548 } |
| 549 |
| 550 void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps, |
| 551 int max_bitrate_bps) { |
| 552 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 553 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps); |
| 554 rtc::Event thread_sync_event(false /* manual_reset */, false); |
| 555 worker_queue_->PostTask([&] { |
| 556 bitrate_allocator_->AddObserver(this, min_bitrate_bps, max_bitrate_bps, 0, |
| 557 true); |
| 558 thread_sync_event.Set(); |
| 559 }); |
| 560 thread_sync_event.Wait(rtc::Event::kForever); |
| 561 } |
| 562 |
| 563 void AudioSendStream::RemoveBitrateObserver() { |
| 564 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 565 rtc::Event thread_sync_event(false /* manual_reset */, false); |
| 566 worker_queue_->PostTask([this, &thread_sync_event] { |
| 567 bitrate_allocator_->RemoveObserver(this); |
| 568 thread_sync_event.Set(); |
| 569 }); |
| 570 thread_sync_event.Wait(rtc::Event::kForever); |
| 430 } | 571 } |
| 431 | 572 |
| 432 } // namespace internal | 573 } // namespace internal |
| 433 } // namespace webrtc | 574 } // namespace webrtc |
| OLD | NEW |