blob: 5a8efc223ef80f26ffec66122fefe25afb8897ef [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
kjellandera69d9732016-08-31 07:33:05 -070016#include "webrtc/api/call/audio_sink.h"
solenberg7602aab2016-11-14 11:30:07 -080017#include "webrtc/audio/audio_send_stream.h"
solenberg566ef242015-11-06 15:34:49 -080018#include "webrtc/audio/audio_state.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020019#include "webrtc/audio/conversion.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020020#include "webrtc/base/checks.h"
pbosa2f30de2015-10-15 05:22:13 -070021#include "webrtc/base/logging.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020022#include "webrtc/base/timeutils.h"
Stefan Holmer80e12072016-02-23 13:30:42 +010023#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020024#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
solenberg13725082015-11-25 08:16:52 -080025#include "webrtc/voice_engine/channel_proxy.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020026#include "webrtc/voice_engine/include/voe_base.h"
27#include "webrtc/voice_engine/include/voe_codec.h"
28#include "webrtc/voice_engine/include/voe_neteq_stats.h"
29#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
30#include "webrtc/voice_engine/include/voe_video_sync.h"
31#include "webrtc/voice_engine/include/voe_volume_control.h"
solenberg13725082015-11-25 08:16:52 -080032#include "webrtc/voice_engine/voice_engine_impl.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020033
34namespace webrtc {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010035namespace {
36
37bool UseSendSideBwe(const webrtc::AudioReceiveStream::Config& config) {
38 if (!config.rtp.transport_cc) {
39 return false;
40 }
41 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -070042 if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010043 return true;
44 }
45 }
46 return false;
47}
48} // namespace
49
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020050std::string AudioReceiveStream::Config::Rtp::ToString() const {
51 std::stringstream ss;
52 ss << "{remote_ssrc: " << remote_ssrc;
solenberg85a04962015-10-27 03:35:21 -070053 ss << ", local_ssrc: " << local_ssrc;
solenberg8189b022016-06-14 12:13:00 -070054 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
55 ss << ", nack: " << nack.ToString();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020056 ss << ", extensions: [";
57 for (size_t i = 0; i < extensions.size(); ++i) {
58 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020059 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020060 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020061 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020062 }
63 ss << ']';
64 ss << '}';
65 return ss.str();
66}
67
68std::string AudioReceiveStream::Config::ToString() const {
69 std::stringstream ss;
70 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070071 ss << ", rtcp_send_transport: "
72 << (rtcp_send_transport ? "(Transport)" : "nullptr");
pbos8fc7fa72015-07-15 08:02:58 -070073 ss << ", voe_channel_id: " << voe_channel_id;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020074 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070075 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020076 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020077 ss << '}';
78 return ss.str();
79}
80
81namespace internal {
82AudioReceiveStream::AudioReceiveStream(
Stefan Holmer3842c5c2016-01-12 13:55:00 +010083 CongestionController* congestion_controller,
solenberg566ef242015-11-06 15:34:49 -080084 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070085 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
86 webrtc::RtcEventLog* event_log)
Stefan Holmer3842c5c2016-01-12 13:55:00 +010087 : config_(config),
solenberg566ef242015-11-06 15:34:49 -080088 audio_state_(audio_state),
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020089 rtp_header_parser_(RtpHeaderParser::Create()) {
pbosa2f30de2015-10-15 05:22:13 -070090 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080091 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080092 RTC_DCHECK(audio_state_.get());
Stefan Holmer3842c5c2016-01-12 13:55:00 +010093 RTC_DCHECK(congestion_controller);
solenberg566ef242015-11-06 15:34:49 -080094 RTC_DCHECK(rtp_header_parser_);
solenberg7add0582015-11-20 09:59:34 -080095
solenberg13725082015-11-25 08:16:52 -080096 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080097 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
ivoc14d5dbe2016-07-04 07:06:55 -070098 channel_proxy_->SetRtcEventLog(event_log);
solenberg13725082015-11-25 08:16:52 -080099 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -0700100 // TODO(solenberg): Config NACK history window (which is a packet count),
101 // using the actual packet size for the configured codec.
102 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
103 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -0700104
ossu29b1a8d2016-06-13 07:34:51 -0700105 // TODO(ossu): This is where we'd like to set the decoder factory to
106 // use. However, since it needs to be included when constructing Channel, we
107 // cannot do that until we're able to move Channel ownership into the
108 // Audio{Send,Receive}Streams. The best we can do is check that we're not
109 // trying to use two different factories using the different interfaces.
110 RTC_CHECK(config.decoder_factory);
111 RTC_CHECK_EQ(config.decoder_factory,
112 channel_proxy_->GetAudioDecoderFactory());
113
mflodman3d7db262016-04-29 00:57:13 -0700114 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
115
solenberg7add0582015-11-20 09:59:34 -0800116 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700117 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800118 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800119 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
120 kRtpExtensionAudioLevel, extension.id);
121 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700122 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800123 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
solenberg7add0582015-11-20 09:59:34 -0800124 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
125 kRtpExtensionTransportSequenceNumber, extension.id);
126 RTC_DCHECK(registered);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200127 } else {
128 RTC_NOTREACHED() << "Unsupported RTP extension.";
129 }
130 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100131 // Configure bandwidth estimation.
stefanbba9dec2016-02-01 04:39:55 -0800132 channel_proxy_->RegisterReceiverCongestionControlObjects(
133 congestion_controller->packet_router());
stefanba4c0e42016-02-04 04:12:24 -0800134 if (UseSendSideBwe(config)) {
135 remote_bitrate_estimator_ =
136 congestion_controller->GetRemoteBitrateEstimator(true);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100137 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200138}
139
pbosa2f30de2015-10-15 05:22:13 -0700140AudioReceiveStream::~AudioReceiveStream() {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200141 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700142 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
aleloi18e0b672016-10-04 02:45:47 -0700143 Stop();
solenberg7602aab2016-11-14 11:30:07 -0800144 channel_proxy_->DisassociateSendChannel();
mflodman3d7db262016-04-29 00:57:13 -0700145 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800146 channel_proxy_->ResetCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700147 channel_proxy_->SetRtcEventLog(nullptr);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100148 if (remote_bitrate_estimator_) {
149 remote_bitrate_estimator_->RemoveStream(config_.rtp.remote_ssrc);
150 }
pbosa2f30de2015-10-15 05:22:13 -0700151}
152
solenberg7add0582015-11-20 09:59:34 -0800153void AudioReceiveStream::Start() {
154 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloi84ef6152016-08-04 05:28:21 -0700155 ScopedVoEInterface<VoEBase> base(voice_engine());
156 int error = base->StartPlayout(config_.voe_channel_id);
157 if (error != 0) {
158 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
159 }
solenberg7add0582015-11-20 09:59:34 -0800160}
161
162void AudioReceiveStream::Stop() {
163 RTC_DCHECK(thread_checker_.CalledOnValidThread());
aleloi84ef6152016-08-04 05:28:21 -0700164 ScopedVoEInterface<VoEBase> base(voice_engine());
165 base->StopPlayout(config_.voe_channel_id);
solenberg7add0582015-11-20 09:59:34 -0800166}
167
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200168webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200169 RTC_DCHECK(thread_checker_.CalledOnValidThread());
170 webrtc::AudioReceiveStream::Stats stats;
171 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg7add0582015-11-20 09:59:34 -0800172 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg8b85de22015-11-16 09:48:04 -0800173
solenberg358057b2015-11-27 10:46:42 -0800174 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700175 webrtc::CodecInst codec_inst = {0};
solenberg8b85de22015-11-16 09:48:04 -0800176 if (codec->GetRecCodec(config_.voe_channel_id, codec_inst) == -1) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200177 return stats;
178 }
179
solenberg85a04962015-10-27 03:35:21 -0700180 stats.bytes_rcvd = call_stats.bytesReceived;
181 stats.packets_rcvd = call_stats.packetsReceived;
182 stats.packets_lost = call_stats.cumulativeLost;
183 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800184 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700185 if (codec_inst.pltype != -1) {
186 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800187 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200188 }
solenberg85a04962015-10-27 03:35:21 -0700189 stats.ext_seqnum = call_stats.extendedMax;
190 if (codec_inst.plfreq / 1000 > 0) {
191 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200192 }
solenberg358057b2015-11-27 10:46:42 -0800193 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
194 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200195
solenberg8b85de22015-11-16 09:48:04 -0800196 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800197 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800198 stats.jitter_buffer_ms = ns.currentBufferSize;
199 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
200 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
201 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
202 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
203 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
204 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200205
solenberg358057b2015-11-27 10:46:42 -0800206 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800207 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
208 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
209 stats.decoding_normal = ds.decoded_normal;
210 stats.decoding_plc = ds.decoded_plc;
211 stats.decoding_cng = ds.decoded_cng;
212 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700213 stats.decoding_muted_output = ds.decoded_muted_output;
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
solenberg7602aab2016-11-14 11:30:07 -0800233void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
234 RTC_DCHECK(thread_checker_.CalledOnValidThread());
235 if (send_stream) {
236 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
237 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
238 voe_impl->GetChannelProxy(send_stream->config().voe_channel_id);
239 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
240 } else {
241 channel_proxy_->DisassociateSendChannel();
242 }
243}
244
pbos1ba8d392016-05-01 20:18:34 -0700245void AudioReceiveStream::SignalNetworkState(NetworkState state) {
246 RTC_DCHECK(thread_checker_.CalledOnValidThread());
247}
248
249bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
250 // TODO(solenberg): Tests call this function on a network thread, libjingle
251 // calls on the worker thread. We should move towards always using a network
252 // thread. Then this check can be enabled.
253 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
254 return channel_proxy_->ReceivedRTCPPacket(packet, length);
255}
256
257bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
258 size_t length,
259 const PacketTime& packet_time) {
260 // TODO(solenberg): Tests call this function on a network thread, libjingle
261 // calls on the worker thread. We should move towards always using a network
262 // thread. Then this check can be enabled.
263 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
264 RTPHeader header;
265 if (!rtp_header_parser_->Parse(packet, length, &header)) {
266 return false;
267 }
268
269 // Only forward if the parsed header has one of the headers necessary for
270 // bandwidth estimation. RTP timestamps has different rates for audio and
271 // video and shouldn't be mixed.
272 if (remote_bitrate_estimator_ &&
273 header.extension.hasTransportSequenceNumber) {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200274 int64_t arrival_time_ms = rtc::TimeMillis();
pbos1ba8d392016-05-01 20:18:34 -0700275 if (packet_time.timestamp >= 0)
276 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
277 size_t payload_size = length - header.headerLength;
278 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
pbos2169d8b2016-06-20 11:53:02 -0700279 header);
pbos1ba8d392016-05-01 20:18:34 -0700280 }
281
282 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
283}
284
aleloi6c278492016-10-20 14:24:39 -0700285AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
286 int sample_rate_hz,
287 AudioFrame* audio_frame) {
288 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700289}
290
aleloi051f6782016-10-31 03:26:40 -0700291int AudioReceiveStream::PreferredSampleRate() const {
292 return channel_proxy_->NeededFrequency();
293}
294
295int AudioReceiveStream::Ssrc() const {
aleloiaed581a2016-10-20 06:32:39 -0700296 return config_.rtp.local_ssrc;
297}
298
solenberg7add0582015-11-20 09:59:34 -0800299VoiceEngine* AudioReceiveStream::voice_engine() const {
300 internal::AudioState* audio_state =
301 static_cast<internal::AudioState*>(audio_state_.get());
302 VoiceEngine* voice_engine = audio_state->voice_engine();
303 RTC_DCHECK(voice_engine);
304 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200305}
306} // namespace internal
307} // namespace webrtc