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