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