blob: c6c9b30b2a29e294de3f63b8028d5292762e658f [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;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020053 ss << ", extensions: [";
54 for (size_t i = 0; i < extensions.size(); ++i) {
55 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020056 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020057 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020058 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020059 }
60 ss << ']';
stefanba4c0e42016-02-04 04:12:24 -080061 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020062 ss << '}';
63 return ss.str();
64}
65
66std::string AudioReceiveStream::Config::ToString() const {
67 std::stringstream ss;
68 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070069 ss << ", rtcp_send_transport: "
70 << (rtcp_send_transport ? "(Transport)" : "nullptr");
pbos8fc7fa72015-07-15 08:02:58 -070071 ss << ", voe_channel_id: " << voe_channel_id;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020072 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070073 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020074 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020075 ss << '}';
76 return ss.str();
77}
78
79namespace internal {
80AudioReceiveStream::AudioReceiveStream(
Stefan Holmer3842c5c2016-01-12 13:55:00 +010081 CongestionController* congestion_controller,
solenberg566ef242015-11-06 15:34:49 -080082 const webrtc::AudioReceiveStream::Config& config,
83 const rtc::scoped_refptr<webrtc::AudioState>& audio_state)
Stefan Holmer3842c5c2016-01-12 13:55:00 +010084 : config_(config),
solenberg566ef242015-11-06 15:34:49 -080085 audio_state_(audio_state),
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020086 rtp_header_parser_(RtpHeaderParser::Create()) {
pbosa2f30de2015-10-15 05:22:13 -070087 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080088 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080089 RTC_DCHECK(audio_state_.get());
Stefan Holmer3842c5c2016-01-12 13:55:00 +010090 RTC_DCHECK(congestion_controller);
solenberg566ef242015-11-06 15:34:49 -080091 RTC_DCHECK(rtp_header_parser_);
solenberg7add0582015-11-20 09:59:34 -080092
solenberg13725082015-11-25 08:16:52 -080093 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080094 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
solenberg13725082015-11-25 08:16:52 -080095 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
mflodman3d7db262016-04-29 00:57:13 -070096
ossu29b1a8d2016-06-13 07:34:51 -070097 // TODO(ossu): This is where we'd like to set the decoder factory to
98 // use. However, since it needs to be included when constructing Channel, we
99 // cannot do that until we're able to move Channel ownership into the
100 // Audio{Send,Receive}Streams. The best we can do is check that we're not
101 // trying to use two different factories using the different interfaces.
102 RTC_CHECK(config.decoder_factory);
103 RTC_CHECK_EQ(config.decoder_factory,
104 channel_proxy_->GetAudioDecoderFactory());
105
mflodman3d7db262016-04-29 00:57:13 -0700106 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
107
solenberg7add0582015-11-20 09:59:34 -0800108 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700109 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800110 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800111 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
112 kRtpExtensionAudioLevel, extension.id);
113 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700114 } else if (extension.uri == RtpExtension::kAbsSendTimeUri) {
solenberg358057b2015-11-27 10:46:42 -0800115 channel_proxy_->SetReceiveAbsoluteSenderTimeStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800116 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
117 kRtpExtensionAbsoluteSendTime, extension.id);
118 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700119 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800120 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
solenberg7add0582015-11-20 09:59:34 -0800121 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
122 kRtpExtensionTransportSequenceNumber, extension.id);
123 RTC_DCHECK(registered);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200124 } else {
125 RTC_NOTREACHED() << "Unsupported RTP extension.";
126 }
127 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100128 // Configure bandwidth estimation.
stefanbba9dec2016-02-01 04:39:55 -0800129 channel_proxy_->RegisterReceiverCongestionControlObjects(
130 congestion_controller->packet_router());
stefanba4c0e42016-02-04 04:12:24 -0800131 if (UseSendSideBwe(config)) {
132 remote_bitrate_estimator_ =
133 congestion_controller->GetRemoteBitrateEstimator(true);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100134 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200135}
136
pbosa2f30de2015-10-15 05:22:13 -0700137AudioReceiveStream::~AudioReceiveStream() {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200138 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700139 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
mflodman3d7db262016-04-29 00:57:13 -0700140 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800141 channel_proxy_->ResetCongestionControlObjects();
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100142 if (remote_bitrate_estimator_) {
143 remote_bitrate_estimator_->RemoveStream(config_.rtp.remote_ssrc);
144 }
pbosa2f30de2015-10-15 05:22:13 -0700145}
146
solenberg7add0582015-11-20 09:59:34 -0800147void AudioReceiveStream::Start() {
148 RTC_DCHECK(thread_checker_.CalledOnValidThread());
149}
150
151void AudioReceiveStream::Stop() {
152 RTC_DCHECK(thread_checker_.CalledOnValidThread());
153}
154
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200155webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200156 RTC_DCHECK(thread_checker_.CalledOnValidThread());
157 webrtc::AudioReceiveStream::Stats stats;
158 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg7add0582015-11-20 09:59:34 -0800159 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg8b85de22015-11-16 09:48:04 -0800160
solenberg358057b2015-11-27 10:46:42 -0800161 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700162 webrtc::CodecInst codec_inst = {0};
solenberg8b85de22015-11-16 09:48:04 -0800163 if (codec->GetRecCodec(config_.voe_channel_id, codec_inst) == -1) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200164 return stats;
165 }
166
solenberg85a04962015-10-27 03:35:21 -0700167 stats.bytes_rcvd = call_stats.bytesReceived;
168 stats.packets_rcvd = call_stats.packetsReceived;
169 stats.packets_lost = call_stats.cumulativeLost;
170 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800171 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700172 if (codec_inst.pltype != -1) {
173 stats.codec_name = codec_inst.plname;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200174 }
solenberg85a04962015-10-27 03:35:21 -0700175 stats.ext_seqnum = call_stats.extendedMax;
176 if (codec_inst.plfreq / 1000 > 0) {
177 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200178 }
solenberg358057b2015-11-27 10:46:42 -0800179 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
180 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200181
solenberg8b85de22015-11-16 09:48:04 -0800182 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800183 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800184 stats.jitter_buffer_ms = ns.currentBufferSize;
185 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
186 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
187 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
188 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
189 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
190 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200191
solenberg358057b2015-11-27 10:46:42 -0800192 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800193 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
194 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
195 stats.decoding_normal = ds.decoded_normal;
196 stats.decoding_plc = ds.decoded_plc;
197 stats.decoding_cng = ds.decoded_cng;
198 stats.decoding_plc_cng = ds.decoded_plc_cng;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200199
200 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200201}
202
kwibergfffa42b2016-02-23 10:46:32 -0800203void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +0100204 RTC_DCHECK(thread_checker_.CalledOnValidThread());
kwibergfffa42b2016-02-23 10:46:32 -0800205 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100206}
207
pbosa2f30de2015-10-15 05:22:13 -0700208const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200209 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700210 return config_;
211}
212
pbos1ba8d392016-05-01 20:18:34 -0700213void AudioReceiveStream::SignalNetworkState(NetworkState state) {
214 RTC_DCHECK(thread_checker_.CalledOnValidThread());
215}
216
217bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
218 // TODO(solenberg): Tests call this function on a network thread, libjingle
219 // calls on the worker thread. We should move towards always using a network
220 // thread. Then this check can be enabled.
221 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
222 return channel_proxy_->ReceivedRTCPPacket(packet, length);
223}
224
225bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
226 size_t length,
227 const PacketTime& packet_time) {
228 // TODO(solenberg): Tests call this function on a network thread, libjingle
229 // calls on the worker thread. We should move towards always using a network
230 // thread. Then this check can be enabled.
231 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
232 RTPHeader header;
233 if (!rtp_header_parser_->Parse(packet, length, &header)) {
234 return false;
235 }
236
237 // Only forward if the parsed header has one of the headers necessary for
238 // bandwidth estimation. RTP timestamps has different rates for audio and
239 // video and shouldn't be mixed.
240 if (remote_bitrate_estimator_ &&
241 header.extension.hasTransportSequenceNumber) {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200242 int64_t arrival_time_ms = rtc::TimeMillis();
pbos1ba8d392016-05-01 20:18:34 -0700243 if (packet_time.timestamp >= 0)
244 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
245 size_t payload_size = length - header.headerLength;
246 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
247 header, false);
248 }
249
250 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
251}
252
solenberg7add0582015-11-20 09:59:34 -0800253VoiceEngine* AudioReceiveStream::voice_engine() const {
254 internal::AudioState* audio_state =
255 static_cast<internal::AudioState*>(audio_state_.get());
256 VoiceEngine* voice_engine = audio_state->voice_engine();
257 RTC_DCHECK(voice_engine);
258 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200259}
260} // namespace internal
261} // namespace webrtc