blob: 7f0d825610336f64f4986aea0f5fafc48886697f [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/audio/audio_send_stream.h"
12
13#include <string>
14
solenberg566ef242015-11-06 15:34:49 -080015#include "webrtc/audio/audio_state.h"
solenberg85a04962015-10-27 03:35:21 -070016#include "webrtc/audio/conversion.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/audio/scoped_voe_interface.h"
solenbergc7a8b082015-10-16 14:35:07 -070018#include "webrtc/base/checks.h"
perkj26091b12016-09-01 01:17:40 -070019#include "webrtc/base/event.h"
solenbergc7a8b082015-10-16 14:35:07 -070020#include "webrtc/base/logging.h"
perkj26091b12016-09-01 01:17:40 -070021#include "webrtc/base/task_queue.h"
elad.alond12a8e12017-03-23 11:04:48 -070022#include "webrtc/base/timeutils.h"
nisseb8f9a322017-03-27 05:36:15 -070023#include "webrtc/call/rtp_transport_controller_send.h"
stefan7de8d642017-02-07 07:14:08 -080024#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
nisse559af382017-03-21 06:41:12 -070025#include "webrtc/modules/congestion_controller/include/send_side_congestion_controller.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010026#include "webrtc/modules/pacing/paced_sender.h"
27#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
solenberg13725082015-11-25 08:16:52 -080028#include "webrtc/voice_engine/channel_proxy.h"
solenbergbd9a77f2017-02-06 12:53:57 -080029#include "webrtc/voice_engine/include/voe_base.h"
solenberg796b8f92017-03-01 17:02:23 -080030#include "webrtc/voice_engine/transmit_mixer.h"
solenberg13725082015-11-25 08:16:52 -080031#include "webrtc/voice_engine/voice_engine_impl.h"
solenbergc7a8b082015-10-16 14:35:07 -070032
33namespace webrtc {
minyue7a973442016-10-20 03:27:12 -070034
35namespace {
36
37constexpr char kOpusCodecName[] = "opus";
38
minyue7a973442016-10-20 03:27:12 -070039bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
solenberg06f240b2017-02-13 04:42:52 -080040 return (STR_CASE_CMP(codec.plname, ref_name) == 0);
minyue7a973442016-10-20 03:27:12 -070041}
minyue7a973442016-10-20 03:27:12 -070042} // namespace
43
solenbergc7a8b082015-10-16 14:35:07 -070044namespace internal {
elad.alond12a8e12017-03-23 11:04:48 -070045// TODO(elad.alon): Subsequent CL will make these values experiment-dependent.
46constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000;
47constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
48constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
49
solenberg566ef242015-11-06 15:34:49 -080050AudioSendStream::AudioSendStream(
51 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010052 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -070053 rtc::TaskQueue* worker_queue,
nisseb8f9a322017-03-27 05:36:15 -070054 RtpTransportControllerSendInterface* transport,
tereliuse035e2d2016-09-21 06:51:47 -070055 BitrateAllocator* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080056 RtcEventLog* event_log,
57 RtcpRttStats* rtcp_rtt_stats)
perkj26091b12016-09-01 01:17:40 -070058 : worker_queue_(worker_queue),
59 config_(config),
mflodman86cc6ff2016-07-26 04:44:06 -070060 audio_state_(audio_state),
michaeltf4caaab2017-01-16 23:55:07 -080061 bitrate_allocator_(bitrate_allocator),
nisseb8f9a322017-03-27 05:36:15 -070062 transport_(transport),
elad.alond12a8e12017-03-23 11:04:48 -070063 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs,
64 kPacketLossRateMinNumAckedPackets,
65 kRecoverablePacketLossRateMinNumAckedPairs) {
solenbergc7a8b082015-10-16 14:35:07 -070066 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080067 RTC_DCHECK_NE(config_.voe_channel_id, -1);
68 RTC_DCHECK(audio_state_.get());
nisseb8f9a322017-03-27 05:36:15 -070069 RTC_DCHECK(transport);
70 RTC_DCHECK(transport->send_side_cc());
solenberg3a941542015-11-16 07:34:50 -080071
solenberg13725082015-11-25 08:16:52 -080072 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080073 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
tereliuse035e2d2016-09-21 06:51:47 -070074 channel_proxy_->SetRtcEventLog(event_log);
michaelt9332b7d2016-11-30 07:51:13 -080075 channel_proxy_->SetRtcpRttStats(rtcp_rtt_stats);
solenberg13725082015-11-25 08:16:52 -080076 channel_proxy_->SetRTCPStatus(true);
77 channel_proxy_->SetLocalSSRC(config.rtp.ssrc);
78 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name);
solenberg971cab02016-06-14 10:02:41 -070079 // 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);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010083
mflodman3d7db262016-04-29 00:57:13 -070084 channel_proxy_->RegisterExternalTransport(config.send_transport);
nisseb8f9a322017-03-27 05:36:15 -070085 transport_->send_side_cc()->RegisterPacketFeedbackObserver(this);
mflodman3d7db262016-04-29 00:57:13 -070086
solenberg3a941542015-11-16 07:34:50 -080087 for (const auto& extension : config.rtp.extensions) {
stefanb521aa72016-11-01 03:17:16 -070088 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -080089 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
isheriff6f8d6862016-05-26 11:24:55 -070090 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
Stefan Holmerb86d4e42015-12-07 10:26:18 +010091 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
nisseb8f9a322017-03-27 05:36:15 -070092 transport->send_side_cc()->EnablePeriodicAlrProbing(true);
93 bandwidth_observer_.reset(transport->send_side_cc()
94 ->GetBitrateController()
95 ->CreateRtcpBandwidthObserver());
solenberg3a941542015-11-16 07:34:50 -080096 } else {
97 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
98 }
99 }
stefan7de8d642017-02-07 07:14:08 -0800100 channel_proxy_->RegisterSenderCongestionControlObjects(
nisseb8f9a322017-03-27 05:36:15 -0700101 transport, bandwidth_observer_.get());
minyue7a973442016-10-20 03:27:12 -0700102 if (!SetupSendCodec()) {
103 LOG(LS_ERROR) << "Failed to set up send codec state.";
104 }
elad.alond12a8e12017-03-23 11:04:48 -0700105
106 pacer_thread_checker_.DetachFromThread();
solenbergc7a8b082015-10-16 14:35:07 -0700107}
108
109AudioSendStream::~AudioSendStream() {
elad.alond12a8e12017-03-23 11:04:48 -0700110 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700111 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
nisseb8f9a322017-03-27 05:36:15 -0700112 transport_->send_side_cc()->DeRegisterPacketFeedbackObserver(this);
mflodman3d7db262016-04-29 00:57:13 -0700113 channel_proxy_->DeRegisterExternalTransport();
nissefdbfdc92017-03-31 05:44:52 -0700114 channel_proxy_->ResetSenderCongestionControlObjects();
tereliuse035e2d2016-09-21 06:51:47 -0700115 channel_proxy_->SetRtcEventLog(nullptr);
michaelt9332b7d2016-11-30 07:51:13 -0800116 channel_proxy_->SetRtcpRttStats(nullptr);
solenbergc7a8b082015-10-16 14:35:07 -0700117}
118
solenberg3a941542015-11-16 07:34:50 -0800119void AudioSendStream::Start() {
elad.alond12a8e12017-03-23 11:04:48 -0700120 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
minyue10cbb462016-11-07 09:29:22 -0800121 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1) {
122 RTC_DCHECK_GE(config_.max_bitrate_bps, config_.min_bitrate_bps);
perkj26091b12016-09-01 01:17:40 -0700123 rtc::Event thread_sync_event(false /* manual_reset */, false);
124 worker_queue_->PostTask([this, &thread_sync_event] {
minyue10cbb462016-11-07 09:29:22 -0800125 bitrate_allocator_->AddObserver(this, config_.min_bitrate_bps,
126 config_.max_bitrate_bps, 0, true);
perkj26091b12016-09-01 01:17:40 -0700127 thread_sync_event.Set();
128 });
129 thread_sync_event.Wait(rtc::Event::kForever);
mflodman86cc6ff2016-07-26 04:44:06 -0700130 }
131
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800132 ScopedVoEInterface<VoEBase> base(voice_engine());
133 int error = base->StartSend(config_.voe_channel_id);
134 if (error != 0) {
135 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error;
136 }
solenberg3a941542015-11-16 07:34:50 -0800137}
138
139void AudioSendStream::Stop() {
elad.alond12a8e12017-03-23 11:04:48 -0700140 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
perkj26091b12016-09-01 01:17:40 -0700141 rtc::Event thread_sync_event(false /* manual_reset */, false);
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
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800148 ScopedVoEInterface<VoEBase> base(voice_engine());
149 int error = base->StopSend(config_.voe_channel_id);
150 if (error != 0) {
151 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error;
152 }
solenberg3a941542015-11-16 07:34:50 -0800153}
154
solenbergffbbcac2016-11-17 05:25:37 -0800155bool AudioSendStream::SendTelephoneEvent(int payload_type,
156 int payload_frequency, int event,
solenberg8842c3e2016-03-11 03:06:41 -0800157 int duration_ms) {
elad.alond12a8e12017-03-23 11:04:48 -0700158 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergffbbcac2016-11-17 05:25:37 -0800159 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type,
160 payload_frequency) &&
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100161 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
162}
163
solenberg94218532016-06-16 10:53:22 -0700164void AudioSendStream::SetMuted(bool muted) {
elad.alond12a8e12017-03-23 11:04:48 -0700165 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -0700166 channel_proxy_->SetInputMute(muted);
167}
168
solenbergc7a8b082015-10-16 14:35:07 -0700169webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
elad.alond12a8e12017-03-23 11:04:48 -0700170 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700171 webrtc::AudioSendStream::Stats stats;
172 stats.local_ssrc = config_.rtp.ssrc;
solenberg85a04962015-10-27 03:35:21 -0700173
solenberg358057b2015-11-27 10:46:42 -0800174 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700175 stats.bytes_sent = call_stats.bytesSent;
176 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800177 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
178 // returns 0 to indicate an error value.
179 if (call_stats.rttMs > 0) {
180 stats.rtt_ms = call_stats.rttMs;
181 }
182 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
183 // implementation.
184 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 03:35:21 -0700185
186 webrtc::CodecInst codec_inst = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800187 if (channel_proxy_->GetSendCodec(&codec_inst)) {
solenberg85a04962015-10-27 03:35:21 -0700188 RTC_DCHECK_NE(codec_inst.pltype, -1);
189 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800190 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
solenberg85a04962015-10-27 03:35:21 -0700191
192 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800193 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800194 // Lookup report for send ssrc only.
195 if (block.source_SSRC == stats.local_ssrc) {
196 stats.packets_lost = block.cumulative_num_packets_lost;
197 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
198 stats.ext_seqnum = block.extended_highest_sequence_number;
199 // Convert samples to milliseconds.
200 if (codec_inst.plfreq / 1000 > 0) {
201 stats.jitter_ms =
202 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 03:35:21 -0700203 }
solenberg8b85de22015-11-16 09:48:04 -0800204 break;
solenberg85a04962015-10-27 03:35:21 -0700205 }
206 }
207 }
208
ivoc7aba0292016-11-14 04:52:06 -0800209 ScopedVoEInterface<VoEBase> base(voice_engine());
solenberg796b8f92017-03-01 17:02:23 -0800210 RTC_DCHECK(base->transmit_mixer());
211 stats.audio_level = base->transmit_mixer()->AudioLevelFullRange();
212 RTC_DCHECK_LE(0, stats.audio_level);
213
ivoc7aba0292016-11-14 04:52:06 -0800214 RTC_DCHECK(base->audio_processing());
215 auto audio_processing_stats = base->audio_processing()->GetStatistics();
216 stats.echo_delay_median_ms = audio_processing_stats.delay_median;
217 stats.echo_delay_std_ms = audio_processing_stats.delay_standard_deviation;
218 stats.echo_return_loss = audio_processing_stats.echo_return_loss.instant();
219 stats.echo_return_loss_enhancement =
220 audio_processing_stats.echo_return_loss_enhancement.instant();
221 stats.residual_echo_likelihood =
222 audio_processing_stats.residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -0800223 stats.residual_echo_likelihood_recent_max =
224 audio_processing_stats.residual_echo_likelihood_recent_max;
ivoc8c63a822016-10-21 04:10:03 -0700225
solenberg3a941542015-11-16 07:34:50 -0800226 internal::AudioState* audio_state =
227 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 15:34:49 -0800228 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 03:35:21 -0700229
230 return stats;
231}
232
pbos1ba8d392016-05-01 20:18:34 -0700233void AudioSendStream::SignalNetworkState(NetworkState state) {
elad.alond12a8e12017-03-23 11:04:48 -0700234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700235}
236
237bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
238 // TODO(solenberg): Tests call this function on a network thread, libjingle
239 // calls on the worker thread. We should move towards always using a network
240 // thread. Then this check can be enabled.
elad.alond12a8e12017-03-23 11:04:48 -0700241 // RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700242 return channel_proxy_->ReceivedRTCPPacket(packet, length);
243}
244
mflodman86cc6ff2016-07-26 04:44:06 -0700245uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
246 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800247 int64_t rtt,
248 int64_t probing_interval_ms) {
stefanfca900a2017-04-10 03:53:00 -0700249 // A send stream may be allocated a bitrate of zero if the allocator decides
250 // to disable it. For now we ignore this decision and keep sending on min
251 // bitrate.
252 if (bitrate_bps == 0) {
253 bitrate_bps = config_.min_bitrate_bps;
254 }
mflodman86cc6ff2016-07-26 04:44:06 -0700255 RTC_DCHECK_GE(bitrate_bps,
minyue10cbb462016-11-07 09:29:22 -0800256 static_cast<uint32_t>(config_.min_bitrate_bps));
mflodman86cc6ff2016-07-26 04:44:06 -0700257 // The bitrate allocator might allocate an higher than max configured bitrate
258 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
minyue10cbb462016-11-07 09:29:22 -0800259 const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
mflodman86cc6ff2016-07-26 04:44:06 -0700260 if (bitrate_bps > max_bitrate_bps)
261 bitrate_bps = max_bitrate_bps;
262
minyue78b4d562016-11-30 04:47:39 -0800263 channel_proxy_->SetBitrate(bitrate_bps, probing_interval_ms);
mflodman86cc6ff2016-07-26 04:44:06 -0700264
265 // The amount of audio protection is not exposed by the encoder, hence
266 // always returning 0.
267 return 0;
268}
269
elad.alond12a8e12017-03-23 11:04:48 -0700270void AudioSendStream::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) {
271 RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
272 // Only packets that belong to this stream are of interest.
273 if (ssrc == config_.rtp.ssrc) {
274 rtc::CritScope lock(&packet_loss_tracker_cs_);
275 // TODO(elad.alon): This function call could potentially reset the window,
276 // setting both PLR and RPLR to unknown. Consider (during upcoming
277 // refactoring) passing an indication of such an event.
278 packet_loss_tracker_.OnPacketAdded(seq_num, rtc::TimeMillis());
279 }
280}
281
282void AudioSendStream::OnPacketFeedbackVector(
283 const std::vector<PacketFeedback>& packet_feedback_vector) {
284 // TODO(elad.alon): This fails in UT; fix and uncomment.
elad.alon4e764512017-03-27 08:53:11 -0700285 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7405
elad.alond12a8e12017-03-23 11:04:48 -0700286 // RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
287 rtc::Optional<float> plr;
elad.alondadb4dc2017-03-23 15:29:50 -0700288 rtc::Optional<float> rplr;
elad.alond12a8e12017-03-23 11:04:48 -0700289 {
290 rtc::CritScope lock(&packet_loss_tracker_cs_);
291 packet_loss_tracker_.OnPacketFeedbackVector(packet_feedback_vector);
292 plr = packet_loss_tracker_.GetPacketLossRate();
elad.alondadb4dc2017-03-23 15:29:50 -0700293 rplr = packet_loss_tracker_.GetRecoverablePacketLossRate();
elad.alond12a8e12017-03-23 11:04:48 -0700294 }
elad.alondadb4dc2017-03-23 15:29:50 -0700295 // TODO(elad.alon): If R/PLR go back to unknown, no indication is given that
elad.alond12a8e12017-03-23 11:04:48 -0700296 // the previously sent value is no longer relevant. This will be taken care
297 // of with some refactoring which is now being done.
298 if (plr) {
299 channel_proxy_->OnTwccBasedUplinkPacketLossRate(*plr);
300 }
elad.alondadb4dc2017-03-23 15:29:50 -0700301 if (rplr) {
302 channel_proxy_->OnRecoverableUplinkPacketLossRate(*rplr);
303 }
elad.alond12a8e12017-03-23 11:04:48 -0700304}
305
solenberg85a04962015-10-27 03:35:21 -0700306const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
elad.alond12a8e12017-03-23 11:04:48 -0700307 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700308 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700309}
310
michaelt79e05882016-11-08 02:50:09 -0800311void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) {
elad.alond12a8e12017-03-23 11:04:48 -0700312 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisseb8f9a322017-03-27 05:36:15 -0700313 transport_->send_side_cc()->SetTransportOverhead(
314 transport_overhead_per_packet);
michaelt79e05882016-11-08 02:50:09 -0800315 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet);
316}
317
solenberg3a941542015-11-16 07:34:50 -0800318VoiceEngine* AudioSendStream::voice_engine() const {
319 internal::AudioState* audio_state =
320 static_cast<internal::AudioState*>(audio_state_.get());
321 VoiceEngine* voice_engine = audio_state->voice_engine();
322 RTC_DCHECK(voice_engine);
323 return voice_engine;
solenbergc7a8b082015-10-16 14:35:07 -0700324}
minyue7a973442016-10-20 03:27:12 -0700325
326// Apply current codec settings to a single voe::Channel used for sending.
327bool AudioSendStream::SetupSendCodec() {
minyue7a973442016-10-20 03:27:12 -0700328 // Disable VAD and FEC unless we know the other side wants them.
solenbergbd9a77f2017-02-06 12:53:57 -0800329 channel_proxy_->SetVADStatus(false);
330 channel_proxy_->SetCodecFECStatus(false);
minyue7a973442016-10-20 03:27:12 -0700331
minyue6f0b9fd2016-11-14 00:51:50 -0800332 // We disable audio network adaptor here. This will on one hand make sure that
333 // audio network adaptor is disabled by default, and on the other allow audio
334 // network adaptor to be reconfigured, since SetReceiverFrameLengthRange can
335 // be only called when audio network adaptor is disabled.
336 channel_proxy_->DisableAudioNetworkAdaptor();
337
minyue7a973442016-10-20 03:27:12 -0700338 const auto& send_codec_spec = config_.send_codec_spec;
339
solenberg940b6d62016-10-25 11:19:07 -0700340 // We set the codec first, since the below extra configuration is only applied
341 // to the "current" codec.
minyue7a973442016-10-20 03:27:12 -0700342
343 // If codec is already configured, we do not it again.
344 // TODO(minyue): check if this check is really needed, or can we move it into
345 // |codec->SetSendCodec|.
346 webrtc::CodecInst current_codec = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800347 if (!channel_proxy_->GetSendCodec(&current_codec) ||
minyue7a973442016-10-20 03:27:12 -0700348 (send_codec_spec.codec_inst != current_codec)) {
solenbergbd9a77f2017-02-06 12:53:57 -0800349 if (!channel_proxy_->SetSendCodec(send_codec_spec.codec_inst)) {
350 LOG(LS_WARNING) << "SetSendCodec() failed.";
minyue7a973442016-10-20 03:27:12 -0700351 return false;
352 }
353 }
354
solenberg940b6d62016-10-25 11:19:07 -0700355 // Codec internal FEC. Treat any failure as fatal internal error.
minyue7a973442016-10-20 03:27:12 -0700356 if (send_codec_spec.enable_codec_fec) {
solenbergbd9a77f2017-02-06 12:53:57 -0800357 if (!channel_proxy_->SetCodecFECStatus(true)) {
358 LOG(LS_WARNING) << "SetCodecFECStatus() failed.";
minyue7a973442016-10-20 03:27:12 -0700359 return false;
360 }
361 }
362
solenberg940b6d62016-10-25 11:19:07 -0700363 // DTX and maxplaybackrate are only set if current codec is Opus.
minyue7a973442016-10-20 03:27:12 -0700364 if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) {
solenbergbd9a77f2017-02-06 12:53:57 -0800365 if (!channel_proxy_->SetOpusDtx(send_codec_spec.enable_opus_dtx)) {
366 LOG(LS_WARNING) << "SetOpusDtx() failed.";
minyue7a973442016-10-20 03:27:12 -0700367 return false;
368 }
369
370 // If opus_max_playback_rate <= 0, the default maximum playback rate
371 // (48 kHz) will be used.
372 if (send_codec_spec.opus_max_playback_rate > 0) {
solenbergbd9a77f2017-02-06 12:53:57 -0800373 if (!channel_proxy_->SetOpusMaxPlaybackRate(
374 send_codec_spec.opus_max_playback_rate)) {
375 LOG(LS_WARNING) << "SetOpusMaxPlaybackRate() failed.";
minyue7a973442016-10-20 03:27:12 -0700376 return false;
377 }
378 }
minyue6b825df2016-10-31 04:08:32 -0700379
380 if (config_.audio_network_adaptor_config) {
381 // Audio network adaptor is only allowed for Opus currently.
382 // |SetReceiverFrameLengthRange| needs to be called before
383 // |EnableAudioNetworkAdaptor|.
384 channel_proxy_->SetReceiverFrameLengthRange(send_codec_spec.min_ptime_ms,
385 send_codec_spec.max_ptime_ms);
386 channel_proxy_->EnableAudioNetworkAdaptor(
387 *config_.audio_network_adaptor_config);
388 LOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
389 << config_.rtp.ssrc;
minyue6b825df2016-10-31 04:08:32 -0700390 }
minyue7a973442016-10-20 03:27:12 -0700391 }
392
393 // Set the CN payloadtype and the VAD status.
394 if (send_codec_spec.cng_payload_type != -1) {
395 // The CN payload type for 8000 Hz clockrate is fixed at 13.
396 if (send_codec_spec.cng_plfreq != 8000) {
397 webrtc::PayloadFrequencies cn_freq;
398 switch (send_codec_spec.cng_plfreq) {
399 case 16000:
400 cn_freq = webrtc::kFreq16000Hz;
401 break;
402 case 32000:
403 cn_freq = webrtc::kFreq32000Hz;
404 break;
405 default:
406 RTC_NOTREACHED();
407 return false;
408 }
solenbergbd9a77f2017-02-06 12:53:57 -0800409 if (!channel_proxy_->SetSendCNPayloadType(
410 send_codec_spec.cng_payload_type, cn_freq)) {
411 LOG(LS_WARNING) << "SetSendCNPayloadType() failed.";
minyue7a973442016-10-20 03:27:12 -0700412 // TODO(ajm): This failure condition will be removed from VoE.
413 // Restore the return here when we update to a new enough webrtc.
414 //
415 // Not returning false because the SetSendCNPayloadType will fail if
416 // the channel is already sending.
417 // This can happen if the remote description is applied twice, for
418 // example in the case of ROAP on top of JSEP, where both side will
419 // send the offer.
420 }
421 }
422
423 // Only turn on VAD if we have a CN payload type that matches the
424 // clockrate for the codec we are going to use.
425 if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq &&
426 send_codec_spec.codec_inst.channels == 1) {
427 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
428 // interaction between VAD and Opus FEC.
solenbergbd9a77f2017-02-06 12:53:57 -0800429 if (!channel_proxy_->SetVADStatus(true)) {
430 LOG(LS_WARNING) << "SetVADStatus() failed.";
minyue7a973442016-10-20 03:27:12 -0700431 return false;
432 }
433 }
434 }
435 return true;
436}
437
solenbergc7a8b082015-10-16 14:35:07 -0700438} // namespace internal
439} // namespace webrtc