blob: ca2c08b34f57e733383aa0ac005fd1977acd2cd6 [file] [log] [blame]
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +02001/*
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
Peter Boström5c389d32015-09-25 13:58:30 +020011#include "webrtc/audio/audio_receive_stream.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020012
13#include <string>
Tommif888bb52015-12-12 01:37:01 +010014#include <utility>
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020015
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010016#include "webrtc/audio_sink.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/audio/audio_state.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020018#include "webrtc/audio/conversion.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020019#include "webrtc/base/checks.h"
pbosa2f30de2015-10-15 05:22:13 -070020#include "webrtc/base/logging.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020021#include "webrtc/base/timeutils.h"
Stefan Holmer80e12072016-02-23 13:30:42 +010022#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020023#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
solenberg13725082015-11-25 08:16:52 -080024#include "webrtc/voice_engine/channel_proxy.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020025#include "webrtc/voice_engine/include/voe_base.h"
26#include "webrtc/voice_engine/include/voe_codec.h"
27#include "webrtc/voice_engine/include/voe_neteq_stats.h"
28#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
29#include "webrtc/voice_engine/include/voe_video_sync.h"
30#include "webrtc/voice_engine/include/voe_volume_control.h"
solenberg13725082015-11-25 08:16:52 -080031#include "webrtc/voice_engine/voice_engine_impl.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020032
33namespace webrtc {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010034namespace {
35
36bool UseSendSideBwe(const webrtc::AudioReceiveStream::Config& config) {
37 if (!config.rtp.transport_cc) {
38 return false;
39 }
40 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -070041 if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010042 return true;
43 }
44 }
45 return false;
46}
47} // namespace
48
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020049std::string AudioReceiveStream::Config::Rtp::ToString() const {
50 std::stringstream ss;
51 ss << "{remote_ssrc: " << remote_ssrc;
solenberg85a04962015-10-27 03:35:21 -070052 ss << ", local_ssrc: " << local_ssrc;
solenberg8189b022016-06-14 12:13:00 -070053 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
54 ss << ", nack: " << nack.ToString();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020055 ss << ", extensions: [";
56 for (size_t i = 0; i < extensions.size(); ++i) {
57 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020058 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020059 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020060 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020061 }
62 ss << ']';
63 ss << '}';
64 return ss.str();
65}
66
67std::string AudioReceiveStream::Config::ToString() const {
68 std::stringstream ss;
69 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070070 ss << ", rtcp_send_transport: "
71 << (rtcp_send_transport ? "(Transport)" : "nullptr");
pbos8fc7fa72015-07-15 08:02:58 -070072 ss << ", voe_channel_id: " << voe_channel_id;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020073 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070074 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020075 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020076 ss << '}';
77 return ss.str();
78}
79
80namespace internal {
81AudioReceiveStream::AudioReceiveStream(
Stefan Holmer3842c5c2016-01-12 13:55:00 +010082 CongestionController* congestion_controller,
solenberg566ef242015-11-06 15:34:49 -080083 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070084 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
85 webrtc::RtcEventLog* event_log)
Stefan Holmer3842c5c2016-01-12 13:55:00 +010086 : config_(config),
solenberg566ef242015-11-06 15:34:49 -080087 audio_state_(audio_state),
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020088 rtp_header_parser_(RtpHeaderParser::Create()) {
pbosa2f30de2015-10-15 05:22:13 -070089 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080090 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080091 RTC_DCHECK(audio_state_.get());
Stefan Holmer3842c5c2016-01-12 13:55:00 +010092 RTC_DCHECK(congestion_controller);
solenberg566ef242015-11-06 15:34:49 -080093 RTC_DCHECK(rtp_header_parser_);
solenberg7add0582015-11-20 09:59:34 -080094
solenberg13725082015-11-25 08:16:52 -080095 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080096 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
ivoc14d5dbe2016-07-04 07:06:55 -070097 channel_proxy_->SetRtcEventLog(event_log);
solenberg13725082015-11-25 08:16:52 -080098 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -070099 // TODO(solenberg): Config NACK history window (which is a packet count),
100 // using the actual packet size for the configured codec.
101 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
102 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -0700103
ossu29b1a8d2016-06-13 07:34:51 -0700104 // TODO(ossu): This is where we'd like to set the decoder factory to
105 // use. However, since it needs to be included when constructing Channel, we
106 // cannot do that until we're able to move Channel ownership into the
107 // Audio{Send,Receive}Streams. The best we can do is check that we're not
108 // trying to use two different factories using the different interfaces.
109 RTC_CHECK(config.decoder_factory);
110 RTC_CHECK_EQ(config.decoder_factory,
111 channel_proxy_->GetAudioDecoderFactory());
112
mflodman3d7db262016-04-29 00:57:13 -0700113 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
114
solenberg7add0582015-11-20 09:59:34 -0800115 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700116 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800117 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800118 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
119 kRtpExtensionAudioLevel, extension.id);
120 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700121 } else if (extension.uri == RtpExtension::kAbsSendTimeUri) {
solenberg358057b2015-11-27 10:46:42 -0800122 channel_proxy_->SetReceiveAbsoluteSenderTimeStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800123 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
124 kRtpExtensionAbsoluteSendTime, extension.id);
125 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700126 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800127 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
solenberg7add0582015-11-20 09:59:34 -0800128 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
129 kRtpExtensionTransportSequenceNumber, extension.id);
130 RTC_DCHECK(registered);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200131 } else {
132 RTC_NOTREACHED() << "Unsupported RTP extension.";
133 }
134 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100135 // Configure bandwidth estimation.
stefanbba9dec2016-02-01 04:39:55 -0800136 channel_proxy_->RegisterReceiverCongestionControlObjects(
137 congestion_controller->packet_router());
stefanba4c0e42016-02-04 04:12:24 -0800138 if (UseSendSideBwe(config)) {
139 remote_bitrate_estimator_ =
140 congestion_controller->GetRemoteBitrateEstimator(true);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100141 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200142}
143
pbosa2f30de2015-10-15 05:22:13 -0700144AudioReceiveStream::~AudioReceiveStream() {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200145 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700146 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
mflodman3d7db262016-04-29 00:57:13 -0700147 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800148 channel_proxy_->ResetCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700149 channel_proxy_->SetRtcEventLog(nullptr);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100150 if (remote_bitrate_estimator_) {
151 remote_bitrate_estimator_->RemoveStream(config_.rtp.remote_ssrc);
152 }
pbosa2f30de2015-10-15 05:22:13 -0700153}
154
solenberg7add0582015-11-20 09:59:34 -0800155void AudioReceiveStream::Start() {
156 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloi84ef6152016-08-04 05:28:21 -0700157 ScopedVoEInterface<VoEBase> base(voice_engine());
158 int error = base->StartPlayout(config_.voe_channel_id);
159 if (error != 0) {
160 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
161 }
solenberg7add0582015-11-20 09:59:34 -0800162}
163
164void AudioReceiveStream::Stop() {
165 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloi84ef6152016-08-04 05:28:21 -0700166 ScopedVoEInterface<VoEBase> base(voice_engine());
167 base->StopPlayout(config_.voe_channel_id);
solenberg7add0582015-11-20 09:59:34 -0800168}
169
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200170webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200171 RTC_DCHECK(thread_checker_.CalledOnValidThread());
172 webrtc::AudioReceiveStream::Stats stats;
173 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg7add0582015-11-20 09:59:34 -0800174 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg8b85de22015-11-16 09:48:04 -0800175
solenberg358057b2015-11-27 10:46:42 -0800176 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700177 webrtc::CodecInst codec_inst = {0};
solenberg8b85de22015-11-16 09:48:04 -0800178 if (codec->GetRecCodec(config_.voe_channel_id, codec_inst) == -1) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200179 return stats;
180 }
181
solenberg85a04962015-10-27 03:35:21 -0700182 stats.bytes_rcvd = call_stats.bytesReceived;
183 stats.packets_rcvd = call_stats.packetsReceived;
184 stats.packets_lost = call_stats.cumulativeLost;
185 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800186 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700187 if (codec_inst.pltype != -1) {
188 stats.codec_name = codec_inst.plname;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200189 }
solenberg85a04962015-10-27 03:35:21 -0700190 stats.ext_seqnum = call_stats.extendedMax;
191 if (codec_inst.plfreq / 1000 > 0) {
192 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200193 }
solenberg358057b2015-11-27 10:46:42 -0800194 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
195 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200196
solenberg8b85de22015-11-16 09:48:04 -0800197 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800198 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800199 stats.jitter_buffer_ms = ns.currentBufferSize;
200 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
201 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
202 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
203 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
204 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
205 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200206
solenberg358057b2015-11-27 10:46:42 -0800207 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800208 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
209 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
210 stats.decoding_normal = ds.decoded_normal;
211 stats.decoding_plc = ds.decoded_plc;
212 stats.decoding_cng = ds.decoded_cng;
213 stats.decoding_plc_cng = ds.decoded_plc_cng;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200214
215 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200216}
217
kwibergfffa42b2016-02-23 10:46:32 -0800218void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +0100219 RTC_DCHECK(thread_checker_.CalledOnValidThread());
kwibergfffa42b2016-02-23 10:46:32 -0800220 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100221}
222
solenberg217fb662016-06-17 08:30:54 -0700223void AudioReceiveStream::SetGain(float gain) {
224 RTC_DCHECK(thread_checker_.CalledOnValidThread());
225 channel_proxy_->SetChannelOutputVolumeScaling(gain);
226}
227
pbosa2f30de2015-10-15 05:22:13 -0700228const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200229 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700230 return config_;
231}
232
pbos1ba8d392016-05-01 20:18:34 -0700233void AudioReceiveStream::SignalNetworkState(NetworkState state) {
234 RTC_DCHECK(thread_checker_.CalledOnValidThread());
235}
236
237bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
238 // TODO(solenberg): Tests call this function on a network thread, libjingle
239 // calls on the worker thread. We should move towards always using a network
240 // thread. Then this check can be enabled.
241 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
242 return channel_proxy_->ReceivedRTCPPacket(packet, length);
243}
244
245bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
246 size_t length,
247 const PacketTime& packet_time) {
248 // TODO(solenberg): Tests call this function on a network thread, libjingle
249 // calls on the worker thread. We should move towards always using a network
250 // thread. Then this check can be enabled.
251 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
252 RTPHeader header;
253 if (!rtp_header_parser_->Parse(packet, length, &header)) {
254 return false;
255 }
256
257 // Only forward if the parsed header has one of the headers necessary for
258 // bandwidth estimation. RTP timestamps has different rates for audio and
259 // video and shouldn't be mixed.
260 if (remote_bitrate_estimator_ &&
261 header.extension.hasTransportSequenceNumber) {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200262 int64_t arrival_time_ms = rtc::TimeMillis();
pbos1ba8d392016-05-01 20:18:34 -0700263 if (packet_time.timestamp >= 0)
264 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
265 size_t payload_size = length - header.headerLength;
266 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
pbos2169d8b2016-06-20 11:53:02 -0700267 header);
pbos1ba8d392016-05-01 20:18:34 -0700268 }
269
270 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
271}
272
solenberg7add0582015-11-20 09:59:34 -0800273VoiceEngine* AudioReceiveStream::voice_engine() const {
274 internal::AudioState* audio_state =
275 static_cast<internal::AudioState*>(audio_state_.get());
276 VoiceEngine* voice_engine = audio_state->voice_engine();
277 RTC_DCHECK(voice_engine);
278 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200279}
280} // namespace internal
281} // namespace webrtc