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