solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 15 | #include "webrtc/audio/audio_state.h" |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 16 | #include "webrtc/audio/conversion.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 17 | #include "webrtc/audio/scoped_voe_interface.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 18 | #include "webrtc/base/checks.h" |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 19 | #include "webrtc/base/event.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 20 | #include "webrtc/base/logging.h" |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 21 | #include "webrtc/base/task_queue.h" |
Stefan Holmer | 80e1207 | 2016-02-23 13:30:42 +0100 | [diff] [blame] | 22 | #include "webrtc/modules/congestion_controller/include/congestion_controller.h" |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 23 | #include "webrtc/modules/pacing/paced_sender.h" |
| 24 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 25 | #include "webrtc/voice_engine/channel_proxy.h" |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 26 | #include "webrtc/voice_engine/include/voe_audio_processing.h" |
| 27 | #include "webrtc/voice_engine/include/voe_codec.h" |
| 28 | #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" |
| 29 | #include "webrtc/voice_engine/include/voe_volume_control.h" |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 30 | #include "webrtc/voice_engine/voice_engine_impl.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
| 36 | constexpr char kOpusCodecName[] = "opus"; |
| 37 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 38 | bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) { |
| 39 | return (_stricmp(codec.plname, ref_name) == 0); |
| 40 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 41 | } // namespace |
| 42 | |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 43 | namespace internal { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 44 | AudioSendStream::AudioSendStream( |
| 45 | const webrtc::AudioSendStream::Config& config, |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 46 | const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 47 | rtc::TaskQueue* worker_queue, |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 48 | CongestionController* congestion_controller, |
terelius | e035e2d | 2016-09-21 06:51:47 -0700 | [diff] [blame] | 49 | BitrateAllocator* bitrate_allocator, |
sprang | 982bf89 | 2016-10-13 06:23:11 -0700 | [diff] [blame] | 50 | RtcEventLog* event_log) |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 51 | : worker_queue_(worker_queue), |
| 52 | config_(config), |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 53 | audio_state_(audio_state), |
| 54 | bitrate_allocator_(bitrate_allocator) { |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 55 | LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 56 | RTC_DCHECK_NE(config_.voe_channel_id, -1); |
| 57 | RTC_DCHECK(audio_state_.get()); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 58 | RTC_DCHECK(congestion_controller); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 59 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 60 | VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine()); |
kwiberg | fffa42b | 2016-02-23 10:46:32 -0800 | [diff] [blame] | 61 | channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id); |
terelius | e035e2d | 2016-09-21 06:51:47 -0700 | [diff] [blame] | 62 | channel_proxy_->SetRtcEventLog(event_log); |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 63 | channel_proxy_->RegisterSenderCongestionControlObjects( |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 64 | congestion_controller->pacer(), |
| 65 | congestion_controller->GetTransportFeedbackObserver(), |
| 66 | congestion_controller->packet_router()); |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 67 | channel_proxy_->SetRTCPStatus(true); |
| 68 | channel_proxy_->SetLocalSSRC(config.rtp.ssrc); |
| 69 | channel_proxy_->SetRTCP_CNAME(config.rtp.c_name); |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 70 | // TODO(solenberg): Config NACK history window (which is a packet count), |
| 71 | // using the actual packet size for the configured codec. |
| 72 | channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0, |
| 73 | config_.rtp.nack.rtp_history_ms / 20); |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 74 | |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 75 | channel_proxy_->RegisterExternalTransport(config.send_transport); |
| 76 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 77 | for (const auto& extension : config.rtp.extensions) { |
stefan | b521aa7 | 2016-11-01 03:17:16 -0700 | [diff] [blame] | 78 | if (extension.uri == RtpExtension::kAudioLevelUri) { |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 79 | channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id); |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 80 | } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 81 | channel_proxy_->EnableSendTransportSequenceNumber(extension.id); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 82 | } else { |
| 83 | RTC_NOTREACHED() << "Registering unsupported RTP extension."; |
| 84 | } |
| 85 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 86 | if (!SetupSendCodec()) { |
| 87 | LOG(LS_ERROR) << "Failed to set up send codec state."; |
| 88 | } |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | AudioSendStream::~AudioSendStream() { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 92 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 93 | LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 94 | channel_proxy_->DeRegisterExternalTransport(); |
stefan | bba9dec | 2016-02-01 04:39:55 -0800 | [diff] [blame] | 95 | channel_proxy_->ResetCongestionControlObjects(); |
terelius | e035e2d | 2016-09-21 06:51:47 -0700 | [diff] [blame] | 96 | channel_proxy_->SetRtcEventLog(nullptr); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 97 | } |
| 98 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 99 | void AudioSendStream::Start() { |
| 100 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 101 | if (config_.min_bitrate_kbps != -1 && config_.max_bitrate_kbps != -1) { |
| 102 | RTC_DCHECK_GE(config_.max_bitrate_kbps, config_.min_bitrate_kbps); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 103 | rtc::Event thread_sync_event(false /* manual_reset */, false); |
| 104 | worker_queue_->PostTask([this, &thread_sync_event] { |
| 105 | bitrate_allocator_->AddObserver(this, config_.min_bitrate_kbps * 1000, |
| 106 | config_.max_bitrate_kbps * 1000, 0, true); |
| 107 | thread_sync_event.Set(); |
| 108 | }); |
| 109 | thread_sync_event.Wait(rtc::Event::kForever); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 112 | ScopedVoEInterface<VoEBase> base(voice_engine()); |
| 113 | int error = base->StartSend(config_.voe_channel_id); |
| 114 | if (error != 0) { |
| 115 | LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error; |
| 116 | } |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void AudioSendStream::Stop() { |
| 120 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 121 | rtc::Event thread_sync_event(false /* manual_reset */, false); |
| 122 | worker_queue_->PostTask([this, &thread_sync_event] { |
| 123 | bitrate_allocator_->RemoveObserver(this); |
| 124 | thread_sync_event.Set(); |
| 125 | }); |
| 126 | thread_sync_event.Wait(rtc::Event::kForever); |
| 127 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 128 | ScopedVoEInterface<VoEBase> base(voice_engine()); |
| 129 | int error = base->StopSend(config_.voe_channel_id); |
| 130 | if (error != 0) { |
| 131 | LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error; |
| 132 | } |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 133 | } |
| 134 | |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 135 | bool AudioSendStream::SendTelephoneEvent(int payload_type, int event, |
| 136 | int duration_ms) { |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 137 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 | return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type) && |
| 139 | channel_proxy_->SendTelephoneEventOutband(event, duration_ms); |
| 140 | } |
| 141 | |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 142 | void AudioSendStream::SetMuted(bool muted) { |
| 143 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 144 | channel_proxy_->SetInputMute(muted); |
| 145 | } |
| 146 | |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 147 | webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 148 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 | webrtc::AudioSendStream::Stats stats; |
| 150 | stats.local_ssrc = config_.rtp.ssrc; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 151 | ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine()); |
| 152 | ScopedVoEInterface<VoECodec> codec(voice_engine()); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 153 | ScopedVoEInterface<VoEVolumeControl> volume(voice_engine()); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 154 | |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 155 | webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 156 | stats.bytes_sent = call_stats.bytesSent; |
| 157 | stats.packets_sent = call_stats.packetsSent; |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 158 | // RTT isn't known until a RTCP report is received. Until then, VoiceEngine |
| 159 | // returns 0 to indicate an error value. |
| 160 | if (call_stats.rttMs > 0) { |
| 161 | stats.rtt_ms = call_stats.rttMs; |
| 162 | } |
| 163 | // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable |
| 164 | // implementation. |
| 165 | stats.aec_quality_min = -1; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 166 | |
| 167 | webrtc::CodecInst codec_inst = {0}; |
| 168 | if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) { |
| 169 | RTC_DCHECK_NE(codec_inst.pltype, -1); |
| 170 | stats.codec_name = codec_inst.plname; |
| 171 | |
| 172 | // Get data from the last remote RTCP report. |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 173 | for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) { |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 174 | // Lookup report for send ssrc only. |
| 175 | if (block.source_SSRC == stats.local_ssrc) { |
| 176 | stats.packets_lost = block.cumulative_num_packets_lost; |
| 177 | stats.fraction_lost = Q8ToFloat(block.fraction_lost); |
| 178 | stats.ext_seqnum = block.extended_highest_sequence_number; |
| 179 | // Convert samples to milliseconds. |
| 180 | if (codec_inst.plfreq / 1000 > 0) { |
| 181 | stats.jitter_ms = |
| 182 | block.interarrival_jitter / (codec_inst.plfreq / 1000); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 183 | } |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 184 | break; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 189 | // Local speech level. |
| 190 | { |
| 191 | unsigned int level = 0; |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 192 | int error = volume->GetSpeechInputLevelFullRange(level); |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 193 | RTC_DCHECK_EQ(0, error); |
| 194 | stats.audio_level = static_cast<int32_t>(level); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 197 | bool echo_metrics_on = false; |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 198 | int error = processing->GetEcMetricsStatus(echo_metrics_on); |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 199 | RTC_DCHECK_EQ(0, error); |
| 200 | if (echo_metrics_on) { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 201 | // These can also be negative, but in practice -1 is only used to signal |
| 202 | // insufficient data, since the resolution is limited to multiples of 4 ms. |
| 203 | int median = -1; |
| 204 | int std = -1; |
| 205 | float dummy = 0.0f; |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 206 | error = processing->GetEcDelayMetrics(median, std, dummy); |
| 207 | RTC_DCHECK_EQ(0, error); |
| 208 | stats.echo_delay_median_ms = median; |
| 209 | stats.echo_delay_std_ms = std; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 210 | |
| 211 | // These can take on valid negative values, so use the lowest possible level |
| 212 | // as default rather than -1. |
| 213 | int erl = -100; |
| 214 | int erle = -100; |
| 215 | int dummy1 = 0; |
| 216 | int dummy2 = 0; |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 217 | error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2); |
| 218 | RTC_DCHECK_EQ(0, error); |
| 219 | stats.echo_return_loss = erl; |
| 220 | stats.echo_return_loss_enhancement = erle; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 221 | } |
| 222 | |
ivoc | 8c63a82 | 2016-10-21 04:10:03 -0700 | [diff] [blame] | 223 | // TODO(ivoc): Hook this up to the residual echo detector. |
| 224 | stats.residual_echo_likelihood = 0.0f; |
| 225 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 226 | internal::AudioState* audio_state = |
| 227 | static_cast<internal::AudioState*>(audio_state_.get()); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 228 | stats.typing_noise_detected = audio_state->typing_noise_detected(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 229 | |
| 230 | return stats; |
| 231 | } |
| 232 | |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 233 | void AudioSendStream::SignalNetworkState(NetworkState state) { |
| 234 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 235 | } |
| 236 | |
| 237 | bool 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. |
| 241 | // RTC_DCHECK(!thread_checker_.CalledOnValidThread()); |
| 242 | return channel_proxy_->ReceivedRTCPPacket(packet, length); |
| 243 | } |
| 244 | |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 245 | uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps, |
| 246 | uint8_t fraction_loss, |
| 247 | int64_t rtt) { |
| 248 | RTC_DCHECK_GE(bitrate_bps, |
| 249 | static_cast<uint32_t>(config_.min_bitrate_kbps * 1000)); |
| 250 | // The bitrate allocator might allocate an higher than max configured bitrate |
| 251 | // if there is room, to allow for, as example, extra FEC. Ignore that for now. |
| 252 | const uint32_t max_bitrate_bps = config_.max_bitrate_kbps * 1000; |
| 253 | if (bitrate_bps > max_bitrate_bps) |
| 254 | bitrate_bps = max_bitrate_bps; |
| 255 | |
| 256 | channel_proxy_->SetBitrate(bitrate_bps); |
| 257 | |
| 258 | // The amount of audio protection is not exposed by the encoder, hence |
| 259 | // always returning 0. |
| 260 | return 0; |
| 261 | } |
| 262 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 263 | const webrtc::AudioSendStream::Config& AudioSendStream::config() const { |
| 264 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 265 | return config_; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 266 | } |
| 267 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 268 | VoiceEngine* AudioSendStream::voice_engine() const { |
| 269 | internal::AudioState* audio_state = |
| 270 | static_cast<internal::AudioState*>(audio_state_.get()); |
| 271 | VoiceEngine* voice_engine = audio_state->voice_engine(); |
| 272 | RTC_DCHECK(voice_engine); |
| 273 | return voice_engine; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 274 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 275 | |
| 276 | // Apply current codec settings to a single voe::Channel used for sending. |
| 277 | bool AudioSendStream::SetupSendCodec() { |
| 278 | ScopedVoEInterface<VoEBase> base(voice_engine()); |
| 279 | ScopedVoEInterface<VoECodec> codec(voice_engine()); |
| 280 | |
| 281 | const int channel = config_.voe_channel_id; |
| 282 | |
| 283 | // Disable VAD and FEC unless we know the other side wants them. |
| 284 | codec->SetVADStatus(channel, false); |
| 285 | codec->SetFECStatus(channel, false); |
| 286 | |
| 287 | const auto& send_codec_spec = config_.send_codec_spec; |
| 288 | |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 289 | // We set the codec first, since the below extra configuration is only applied |
| 290 | // to the "current" codec. |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 291 | |
| 292 | // If codec is already configured, we do not it again. |
| 293 | // TODO(minyue): check if this check is really needed, or can we move it into |
| 294 | // |codec->SetSendCodec|. |
| 295 | webrtc::CodecInst current_codec = {0}; |
| 296 | if (codec->GetSendCodec(channel, current_codec) != 0 || |
| 297 | (send_codec_spec.codec_inst != current_codec)) { |
| 298 | if (codec->SetSendCodec(channel, send_codec_spec.codec_inst) == -1) { |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 299 | LOG(LS_WARNING) << "SetSendCodec() failed: " << base->LastError(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | } |
| 303 | |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 304 | // Codec internal FEC. Treat any failure as fatal internal error. |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 305 | if (send_codec_spec.enable_codec_fec) { |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 306 | if (codec->SetFECStatus(channel, true) != 0) { |
| 307 | LOG(LS_WARNING) << "SetFECStatus() failed: " << base->LastError(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | } |
| 311 | |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 312 | // DTX and maxplaybackrate are only set if current codec is Opus. |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 313 | if (IsCodec(send_codec_spec.codec_inst, kOpusCodecName)) { |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 314 | if (codec->SetOpusDtx(channel, send_codec_spec.enable_opus_dtx) != 0) { |
| 315 | LOG(LS_WARNING) << "SetOpusDtx() failed: " << base->LastError(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 316 | return false; |
| 317 | } |
| 318 | |
| 319 | // If opus_max_playback_rate <= 0, the default maximum playback rate |
| 320 | // (48 kHz) will be used. |
| 321 | if (send_codec_spec.opus_max_playback_rate > 0) { |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 322 | if (codec->SetOpusMaxPlaybackRate( |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 323 | channel, send_codec_spec.opus_max_playback_rate) != 0) { |
| 324 | LOG(LS_WARNING) << "SetOpusMaxPlaybackRate() failed: " |
| 325 | << base->LastError(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | } |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 329 | |
| 330 | if (config_.audio_network_adaptor_config) { |
| 331 | // Audio network adaptor is only allowed for Opus currently. |
| 332 | // |SetReceiverFrameLengthRange| needs to be called before |
| 333 | // |EnableAudioNetworkAdaptor|. |
| 334 | channel_proxy_->SetReceiverFrameLengthRange(send_codec_spec.min_ptime_ms, |
| 335 | send_codec_spec.max_ptime_ms); |
| 336 | channel_proxy_->EnableAudioNetworkAdaptor( |
| 337 | *config_.audio_network_adaptor_config); |
| 338 | LOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
| 339 | << config_.rtp.ssrc; |
| 340 | } else { |
| 341 | channel_proxy_->DisableAudioNetworkAdaptor(); |
| 342 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // Set the CN payloadtype and the VAD status. |
| 346 | if (send_codec_spec.cng_payload_type != -1) { |
| 347 | // The CN payload type for 8000 Hz clockrate is fixed at 13. |
| 348 | if (send_codec_spec.cng_plfreq != 8000) { |
| 349 | webrtc::PayloadFrequencies cn_freq; |
| 350 | switch (send_codec_spec.cng_plfreq) { |
| 351 | case 16000: |
| 352 | cn_freq = webrtc::kFreq16000Hz; |
| 353 | break; |
| 354 | case 32000: |
| 355 | cn_freq = webrtc::kFreq32000Hz; |
| 356 | break; |
| 357 | default: |
| 358 | RTC_NOTREACHED(); |
| 359 | return false; |
| 360 | } |
| 361 | if (codec->SetSendCNPayloadType(channel, send_codec_spec.cng_payload_type, |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 362 | cn_freq) != 0) { |
| 363 | LOG(LS_WARNING) << "SetSendCNPayloadType() failed: " |
| 364 | << base->LastError(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 365 | // TODO(ajm): This failure condition will be removed from VoE. |
| 366 | // Restore the return here when we update to a new enough webrtc. |
| 367 | // |
| 368 | // Not returning false because the SetSendCNPayloadType will fail if |
| 369 | // the channel is already sending. |
| 370 | // This can happen if the remote description is applied twice, for |
| 371 | // example in the case of ROAP on top of JSEP, where both side will |
| 372 | // send the offer. |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Only turn on VAD if we have a CN payload type that matches the |
| 377 | // clockrate for the codec we are going to use. |
| 378 | if (send_codec_spec.cng_plfreq == send_codec_spec.codec_inst.plfreq && |
| 379 | send_codec_spec.codec_inst.channels == 1) { |
| 380 | // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the |
| 381 | // interaction between VAD and Opus FEC. |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 382 | if (codec->SetVADStatus(channel, true) != 0) { |
| 383 | LOG(LS_WARNING) << "SetVADStatus() failed: " << base->LastError(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return true; |
| 389 | } |
| 390 | |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 391 | } // namespace internal |
| 392 | } // namespace webrtc |