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