blob: 81ffdade5ce62a4029e633b6f70f51d0e623e5b9 [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() {
aleloi04c07222016-11-22 06:42:53 -0800141 RTC_DCHECK_RUN_ON(&thread_checker_);
pbosa2f30de2015-10-15 05:22:13 -0700142 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
aleloi04c07222016-11-22 06:42:53 -0800143 if (playing_) {
144 Stop();
145 }
solenberg7602aab2016-11-14 11:30:07 -0800146 channel_proxy_->DisassociateSendChannel();
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() {
aleloi04c07222016-11-22 06:42:53 -0800156 RTC_DCHECK_RUN_ON(&thread_checker_);
157 if (playing_) {
158 return;
159 }
160
161 int error = SetVoiceEnginePlayout(true);
aleloi84ef6152016-08-04 05:28:21 -0700162 if (error != 0) {
163 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
aleloi04c07222016-11-22 06:42:53 -0800164 return;
aleloi84ef6152016-08-04 05:28:21 -0700165 }
aleloi04c07222016-11-22 06:42:53 -0800166
167 if (!audio_state()->mixer()->AddSource(this)) {
168 LOG(LS_ERROR) << "Failed to add source to mixer.";
169 SetVoiceEnginePlayout(false);
170 return;
171 }
172
173 playing_ = true;
solenberg7add0582015-11-20 09:59:34 -0800174}
175
176void AudioReceiveStream::Stop() {
aleloi04c07222016-11-22 06:42:53 -0800177 RTC_DCHECK_RUN_ON(&thread_checker_);
178 if (!playing_) {
179 return;
180 }
181 playing_ = false;
182
183 audio_state()->mixer()->RemoveSource(this);
184 SetVoiceEnginePlayout(false);
solenberg7add0582015-11-20 09:59:34 -0800185}
186
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200187webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
aleloi04c07222016-11-22 06:42:53 -0800188 RTC_DCHECK_RUN_ON(&thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200189 webrtc::AudioReceiveStream::Stats stats;
190 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg7add0582015-11-20 09:59:34 -0800191 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg8b85de22015-11-16 09:48:04 -0800192
solenberg358057b2015-11-27 10:46:42 -0800193 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700194 webrtc::CodecInst codec_inst = {0};
solenberg8b85de22015-11-16 09:48:04 -0800195 if (codec->GetRecCodec(config_.voe_channel_id, codec_inst) == -1) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200196 return stats;
197 }
198
solenberg85a04962015-10-27 03:35:21 -0700199 stats.bytes_rcvd = call_stats.bytesReceived;
200 stats.packets_rcvd = call_stats.packetsReceived;
201 stats.packets_lost = call_stats.cumulativeLost;
202 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800203 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700204 if (codec_inst.pltype != -1) {
205 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800206 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200207 }
solenberg85a04962015-10-27 03:35:21 -0700208 stats.ext_seqnum = call_stats.extendedMax;
209 if (codec_inst.plfreq / 1000 > 0) {
210 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200211 }
solenberg358057b2015-11-27 10:46:42 -0800212 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
213 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200214
solenberg8b85de22015-11-16 09:48:04 -0800215 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800216 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800217 stats.jitter_buffer_ms = ns.currentBufferSize;
218 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
219 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
220 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
221 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
222 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
223 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200224
solenberg358057b2015-11-27 10:46:42 -0800225 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800226 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
227 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
228 stats.decoding_normal = ds.decoded_normal;
229 stats.decoding_plc = ds.decoded_plc;
230 stats.decoding_cng = ds.decoded_cng;
231 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700232 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200233
234 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200235}
236
kwibergfffa42b2016-02-23 10:46:32 -0800237void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
aleloi04c07222016-11-22 06:42:53 -0800238 RTC_DCHECK_RUN_ON(&thread_checker_);
kwibergfffa42b2016-02-23 10:46:32 -0800239 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100240}
241
solenberg217fb662016-06-17 08:30:54 -0700242void AudioReceiveStream::SetGain(float gain) {
aleloi04c07222016-11-22 06:42:53 -0800243 RTC_DCHECK_RUN_ON(&thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700244 channel_proxy_->SetChannelOutputVolumeScaling(gain);
245}
246
pbosa2f30de2015-10-15 05:22:13 -0700247const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
aleloi04c07222016-11-22 06:42:53 -0800248 RTC_DCHECK_RUN_ON(&thread_checker_);
pbosa2f30de2015-10-15 05:22:13 -0700249 return config_;
250}
251
solenberg7602aab2016-11-14 11:30:07 -0800252void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
253 RTC_DCHECK(thread_checker_.CalledOnValidThread());
254 if (send_stream) {
255 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
256 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
257 voe_impl->GetChannelProxy(send_stream->config().voe_channel_id);
258 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
259 } else {
260 channel_proxy_->DisassociateSendChannel();
261 }
262}
263
pbos1ba8d392016-05-01 20:18:34 -0700264void AudioReceiveStream::SignalNetworkState(NetworkState state) {
aleloi04c07222016-11-22 06:42:53 -0800265 RTC_DCHECK_RUN_ON(&thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700266}
267
268bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
269 // TODO(solenberg): Tests call this function on a network thread, libjingle
270 // calls on the worker thread. We should move towards always using a network
271 // thread. Then this check can be enabled.
272 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
273 return channel_proxy_->ReceivedRTCPPacket(packet, length);
274}
275
276bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
277 size_t length,
278 const PacketTime& packet_time) {
279 // TODO(solenberg): Tests call this function on a network thread, libjingle
280 // calls on the worker thread. We should move towards always using a network
281 // thread. Then this check can be enabled.
282 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
283 RTPHeader header;
284 if (!rtp_header_parser_->Parse(packet, length, &header)) {
285 return false;
286 }
287
288 // Only forward if the parsed header has one of the headers necessary for
289 // bandwidth estimation. RTP timestamps has different rates for audio and
290 // video and shouldn't be mixed.
291 if (remote_bitrate_estimator_ &&
292 header.extension.hasTransportSequenceNumber) {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200293 int64_t arrival_time_ms = rtc::TimeMillis();
pbos1ba8d392016-05-01 20:18:34 -0700294 if (packet_time.timestamp >= 0)
295 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
296 size_t payload_size = length - header.headerLength;
297 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
pbos2169d8b2016-06-20 11:53:02 -0700298 header);
pbos1ba8d392016-05-01 20:18:34 -0700299 }
300
301 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
302}
303
aleloi6c278492016-10-20 14:24:39 -0700304AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
305 int sample_rate_hz,
306 AudioFrame* audio_frame) {
307 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
aleloiaed581a2016-10-20 06:32:39 -0700308}
309
aleloi051f6782016-10-31 03:26:40 -0700310int AudioReceiveStream::PreferredSampleRate() const {
311 return channel_proxy_->NeededFrequency();
312}
313
314int AudioReceiveStream::Ssrc() const {
aleloiaed581a2016-10-20 06:32:39 -0700315 return config_.rtp.local_ssrc;
316}
317
aleloi04c07222016-11-22 06:42:53 -0800318internal::AudioState* AudioReceiveStream::audio_state() const {
319 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
320 RTC_DCHECK(audio_state);
321 return audio_state;
322}
323
solenberg7add0582015-11-20 09:59:34 -0800324VoiceEngine* AudioReceiveStream::voice_engine() const {
aleloi04c07222016-11-22 06:42:53 -0800325 auto* voice_engine = audio_state()->voice_engine();
solenberg7add0582015-11-20 09:59:34 -0800326 RTC_DCHECK(voice_engine);
327 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200328}
aleloi04c07222016-11-22 06:42:53 -0800329
330int AudioReceiveStream::SetVoiceEnginePlayout(bool playout) {
331 ScopedVoEInterface<VoEBase> base(voice_engine());
332 if (playout) {
333 return base->StartPlayout(config_.voe_channel_id);
334 } else {
335 return base->StopPlayout(config_.voe_channel_id);
336 }
337}
338
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200339} // namespace internal
340} // namespace webrtc