blob: 17979d5760c0a2ca9ceecf9b7ad0cb5e545d3acc [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/audio/audio_send_stream.h"
12
13#include <string>
14
solenberg566ef242015-11-06 15:34:49 -080015#include "webrtc/audio/audio_state.h"
solenberg85a04962015-10-27 03:35:21 -070016#include "webrtc/audio/conversion.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/audio/scoped_voe_interface.h"
solenbergc7a8b082015-10-16 14:35:07 -070018#include "webrtc/base/checks.h"
19#include "webrtc/base/logging.h"
Stefan Holmer80e12072016-02-23 13:30:42 +010020#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#include "webrtc/modules/pacing/paced_sender.h"
22#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
solenberg13725082015-11-25 08:16:52 -080023#include "webrtc/voice_engine/channel_proxy.h"
solenberg85a04962015-10-27 03:35:21 -070024#include "webrtc/voice_engine/include/voe_audio_processing.h"
25#include "webrtc/voice_engine/include/voe_codec.h"
26#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
27#include "webrtc/voice_engine/include/voe_volume_control.h"
solenberg13725082015-11-25 08:16:52 -080028#include "webrtc/voice_engine/voice_engine_impl.h"
solenbergc7a8b082015-10-16 14:35:07 -070029
30namespace webrtc {
31std::string AudioSendStream::Config::Rtp::ToString() const {
32 std::stringstream ss;
33 ss << "{ssrc: " << ssrc;
34 ss << ", extensions: [";
35 for (size_t i = 0; i < extensions.size(); ++i) {
36 ss << extensions[i].ToString();
solenberg85a04962015-10-27 03:35:21 -070037 if (i != extensions.size() - 1) {
solenbergc7a8b082015-10-16 14:35:07 -070038 ss << ", ";
solenberg85a04962015-10-27 03:35:21 -070039 }
solenbergc7a8b082015-10-16 14:35:07 -070040 }
41 ss << ']';
solenberg971cab02016-06-14 10:02:41 -070042 ss << ", nack: " << nack.ToString();
solenberg3a941542015-11-16 07:34:50 -080043 ss << ", c_name: " << c_name;
solenbergc7a8b082015-10-16 14:35:07 -070044 ss << '}';
45 return ss.str();
46}
47
48std::string AudioSendStream::Config::ToString() const {
49 std::stringstream ss;
50 ss << "{rtp: " << rtp.ToString();
51 ss << ", voe_channel_id: " << voe_channel_id;
52 // TODO(solenberg): Encoder config.
53 ss << ", cng_payload_type: " << cng_payload_type;
solenbergc7a8b082015-10-16 14:35:07 -070054 ss << '}';
55 return ss.str();
56}
57
58namespace internal {
solenberg566ef242015-11-06 15:34:49 -080059AudioSendStream::AudioSendStream(
60 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010061 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
mflodman86cc6ff2016-07-26 04:44:06 -070062 CongestionController* congestion_controller,
63 BitrateAllocator* bitrate_allocator)
perkj8eb37a32016-08-16 02:40:55 -070064 : config_(config),
mflodman86cc6ff2016-07-26 04:44:06 -070065 audio_state_(audio_state),
66 bitrate_allocator_(bitrate_allocator) {
solenbergc7a8b082015-10-16 14:35:07 -070067 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080068 RTC_DCHECK_NE(config_.voe_channel_id, -1);
69 RTC_DCHECK(audio_state_.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +010070 RTC_DCHECK(congestion_controller);
solenberg3a941542015-11-16 07:34:50 -080071
solenberg13725082015-11-25 08:16:52 -080072 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080073 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
stefanbba9dec2016-02-01 04:39:55 -080074 channel_proxy_->RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +010075 congestion_controller->pacer(),
76 congestion_controller->GetTransportFeedbackObserver(),
77 congestion_controller->packet_router());
solenberg13725082015-11-25 08:16:52 -080078 channel_proxy_->SetRTCPStatus(true);
79 channel_proxy_->SetLocalSSRC(config.rtp.ssrc);
80 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name);
solenberg971cab02016-06-14 10:02:41 -070081 // TODO(solenberg): Config NACK history window (which is a packet count),
82 // using the actual packet size for the configured codec.
83 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
84 config_.rtp.nack.rtp_history_ms / 20);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010085
mflodman3d7db262016-04-29 00:57:13 -070086 channel_proxy_->RegisterExternalTransport(config.send_transport);
87
solenberg3a941542015-11-16 07:34:50 -080088 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -070089 if (extension.uri == RtpExtension::kAbsSendTimeUri) {
solenberg358057b2015-11-27 10:46:42 -080090 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id);
isheriff6f8d6862016-05-26 11:24:55 -070091 } else if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -080092 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
isheriff6f8d6862016-05-26 11:24:55 -070093 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
Stefan Holmerb86d4e42015-12-07 10:26:18 +010094 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
solenberg3a941542015-11-16 07:34:50 -080095 } else {
96 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
97 }
98 }
solenbergc7a8b082015-10-16 14:35:07 -070099}
100
101AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 03:35:21 -0700102 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700103 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
mflodman3d7db262016-04-29 00:57:13 -0700104 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800105 channel_proxy_->ResetCongestionControlObjects();
solenbergc7a8b082015-10-16 14:35:07 -0700106}
107
solenberg3a941542015-11-16 07:34:50 -0800108void AudioSendStream::Start() {
109 RTC_DCHECK(thread_checker_.CalledOnValidThread());
mflodman86cc6ff2016-07-26 04:44:06 -0700110 if (config_.min_bitrate_kbps != -1 && config_.max_bitrate_kbps != -1) {
111 RTC_DCHECK_GE(config_.max_bitrate_kbps, config_.min_bitrate_kbps);
perkj8eb37a32016-08-16 02:40:55 -0700112 bitrate_allocator_->AddObserver(this, config_.min_bitrate_kbps * 1000,
113 config_.max_bitrate_kbps * 1000, 0, true);
mflodman86cc6ff2016-07-26 04:44:06 -0700114 }
115
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800116 ScopedVoEInterface<VoEBase> base(voice_engine());
117 int error = base->StartSend(config_.voe_channel_id);
118 if (error != 0) {
119 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error;
120 }
solenberg3a941542015-11-16 07:34:50 -0800121}
122
123void AudioSendStream::Stop() {
124 RTC_DCHECK(thread_checker_.CalledOnValidThread());
perkj8eb37a32016-08-16 02:40:55 -0700125 bitrate_allocator_->RemoveObserver(this);
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800126 ScopedVoEInterface<VoEBase> base(voice_engine());
127 int error = base->StopSend(config_.voe_channel_id);
128 if (error != 0) {
129 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error;
130 }
solenberg3a941542015-11-16 07:34:50 -0800131}
132
solenberg8842c3e2016-03-11 03:06:41 -0800133bool AudioSendStream::SendTelephoneEvent(int payload_type, int event,
134 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100135 RTC_DCHECK(thread_checker_.CalledOnValidThread());
136 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type) &&
137 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
138}
139
solenberg94218532016-06-16 10:53:22 -0700140void AudioSendStream::SetMuted(bool muted) {
141 RTC_DCHECK(thread_checker_.CalledOnValidThread());
142 channel_proxy_->SetInputMute(muted);
143}
144
solenbergc7a8b082015-10-16 14:35:07 -0700145webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -0700146 RTC_DCHECK(thread_checker_.CalledOnValidThread());
147 webrtc::AudioSendStream::Stats stats;
148 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 07:34:50 -0800149 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
150 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg3a941542015-11-16 07:34:50 -0800151 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 03:35:21 -0700152
solenberg358057b2015-11-27 10:46:42 -0800153 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700154 stats.bytes_sent = call_stats.bytesSent;
155 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800156 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
157 // returns 0 to indicate an error value.
158 if (call_stats.rttMs > 0) {
159 stats.rtt_ms = call_stats.rttMs;
160 }
161 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
162 // implementation.
163 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 03:35:21 -0700164
165 webrtc::CodecInst codec_inst = {0};
166 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
167 RTC_DCHECK_NE(codec_inst.pltype, -1);
168 stats.codec_name = codec_inst.plname;
169
170 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800171 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800172 // Lookup report for send ssrc only.
173 if (block.source_SSRC == stats.local_ssrc) {
174 stats.packets_lost = block.cumulative_num_packets_lost;
175 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
176 stats.ext_seqnum = block.extended_highest_sequence_number;
177 // Convert samples to milliseconds.
178 if (codec_inst.plfreq / 1000 > 0) {
179 stats.jitter_ms =
180 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 03:35:21 -0700181 }
solenberg8b85de22015-11-16 09:48:04 -0800182 break;
solenberg85a04962015-10-27 03:35:21 -0700183 }
184 }
185 }
186
solenberg85a04962015-10-27 03:35:21 -0700187 // Local speech level.
188 {
189 unsigned int level = 0;
solenberg358057b2015-11-27 10:46:42 -0800190 int error = volume->GetSpeechInputLevelFullRange(level);
solenberg8b85de22015-11-16 09:48:04 -0800191 RTC_DCHECK_EQ(0, error);
192 stats.audio_level = static_cast<int32_t>(level);
solenberg85a04962015-10-27 03:35:21 -0700193 }
194
solenberg85a04962015-10-27 03:35:21 -0700195 bool echo_metrics_on = false;
solenberg358057b2015-11-27 10:46:42 -0800196 int error = processing->GetEcMetricsStatus(echo_metrics_on);
solenberg8b85de22015-11-16 09:48:04 -0800197 RTC_DCHECK_EQ(0, error);
198 if (echo_metrics_on) {
solenberg85a04962015-10-27 03:35:21 -0700199 // These can also be negative, but in practice -1 is only used to signal
200 // insufficient data, since the resolution is limited to multiples of 4 ms.
201 int median = -1;
202 int std = -1;
203 float dummy = 0.0f;
solenberg8b85de22015-11-16 09:48:04 -0800204 error = processing->GetEcDelayMetrics(median, std, dummy);
205 RTC_DCHECK_EQ(0, error);
206 stats.echo_delay_median_ms = median;
207 stats.echo_delay_std_ms = std;
solenberg85a04962015-10-27 03:35:21 -0700208
209 // These can take on valid negative values, so use the lowest possible level
210 // as default rather than -1.
211 int erl = -100;
212 int erle = -100;
213 int dummy1 = 0;
214 int dummy2 = 0;
solenberg8b85de22015-11-16 09:48:04 -0800215 error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2);
216 RTC_DCHECK_EQ(0, error);
217 stats.echo_return_loss = erl;
218 stats.echo_return_loss_enhancement = erle;
solenberg85a04962015-10-27 03:35:21 -0700219 }
220
solenberg3a941542015-11-16 07:34:50 -0800221 internal::AudioState* audio_state =
222 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 15:34:49 -0800223 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 03:35:21 -0700224
225 return stats;
226}
227
pbos1ba8d392016-05-01 20:18:34 -0700228void AudioSendStream::SignalNetworkState(NetworkState state) {
229 RTC_DCHECK(thread_checker_.CalledOnValidThread());
230}
231
232bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
233 // TODO(solenberg): Tests call this function on a network thread, libjingle
234 // calls on the worker thread. We should move towards always using a network
235 // thread. Then this check can be enabled.
236 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
237 return channel_proxy_->ReceivedRTCPPacket(packet, length);
238}
239
mflodman86cc6ff2016-07-26 04:44:06 -0700240uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
241 uint8_t fraction_loss,
242 int64_t rtt) {
243 RTC_DCHECK_GE(bitrate_bps,
244 static_cast<uint32_t>(config_.min_bitrate_kbps * 1000));
245 // The bitrate allocator might allocate an higher than max configured bitrate
246 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
247 const uint32_t max_bitrate_bps = config_.max_bitrate_kbps * 1000;
248 if (bitrate_bps > max_bitrate_bps)
249 bitrate_bps = max_bitrate_bps;
250
251 channel_proxy_->SetBitrate(bitrate_bps);
252
253 // The amount of audio protection is not exposed by the encoder, hence
254 // always returning 0.
255 return 0;
256}
257
solenberg85a04962015-10-27 03:35:21 -0700258const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
259 RTC_DCHECK(thread_checker_.CalledOnValidThread());
260 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700261}
262
solenberg3a941542015-11-16 07:34:50 -0800263VoiceEngine* 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;
solenbergc7a8b082015-10-16 14:35:07 -0700269}
270} // namespace internal
271} // namespace webrtc