blob: 079e97178619878c02d87852608fe6615dbc6a9a [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"
nisse0f15f922017-06-21 01:05:22 -070023#include "webrtc/call/rtp_stream_receiver_controller_interface.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020024#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
solenberg3ebbcb52017-01-31 03:58:40 -080025#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
26#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
solenberg13725082015-11-25 08:16:52 -080027#include "webrtc/voice_engine/channel_proxy.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020028#include "webrtc/voice_engine/include/voe_base.h"
solenberg13725082015-11-25 08:16:52 -080029#include "webrtc/voice_engine/voice_engine_impl.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020030
31namespace webrtc {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010032
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020033std::string AudioReceiveStream::Config::Rtp::ToString() const {
34 std::stringstream ss;
35 ss << "{remote_ssrc: " << remote_ssrc;
solenberg85a04962015-10-27 03:35:21 -070036 ss << ", local_ssrc: " << local_ssrc;
solenberg8189b022016-06-14 12:13:00 -070037 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
38 ss << ", nack: " << nack.ToString();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020039 ss << ", extensions: [";
40 for (size_t i = 0; i < extensions.size(); ++i) {
41 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020042 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020043 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020044 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020045 }
46 ss << ']';
47 ss << '}';
48 return ss.str();
49}
50
51std::string AudioReceiveStream::Config::ToString() const {
52 std::stringstream ss;
53 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070054 ss << ", rtcp_send_transport: "
deadbeef922246a2017-02-26 04:18:12 -080055 << (rtcp_send_transport ? "(Transport)" : "null");
pbos8fc7fa72015-07-15 08:02:58 -070056 ss << ", voe_channel_id: " << voe_channel_id;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020057 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070058 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020059 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020060 ss << '}';
61 return ss.str();
62}
63
64namespace internal {
65AudioReceiveStream::AudioReceiveStream(
nisse0f15f922017-06-21 01:05:22 -070066 RtpStreamReceiverControllerInterface* receiver_controller,
nisse0245da02016-11-30 03:35:20 -080067 PacketRouter* packet_router,
solenberg566ef242015-11-06 15:34:49 -080068 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070069 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
70 webrtc::RtcEventLog* event_log)
nisse0f15f922017-06-21 01:05:22 -070071 : config_(config), audio_state_(audio_state) {
pbosa2f30de2015-10-15 05:22:13 -070072 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080073 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080074 RTC_DCHECK(audio_state_.get());
nisse0245da02016-11-30 03:35:20 -080075 RTC_DCHECK(packet_router);
solenberg7add0582015-11-20 09:59:34 -080076
solenberg3ebbcb52017-01-31 03:58:40 -080077 module_process_thread_checker_.DetachFromThread();
78
solenberg13725082015-11-25 08:16:52 -080079 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080080 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
ivoc14d5dbe2016-07-04 07:06:55 -070081 channel_proxy_->SetRtcEventLog(event_log);
solenberg13725082015-11-25 08:16:52 -080082 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -070083 // TODO(solenberg): Config NACK history window (which is a packet count),
84 // using the actual packet size for the configured codec.
85 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
86 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -070087
ossu29b1a8d2016-06-13 07:34:51 -070088 // TODO(ossu): This is where we'd like to set the decoder factory to
89 // use. However, since it needs to be included when constructing Channel, we
90 // cannot do that until we're able to move Channel ownership into the
91 // Audio{Send,Receive}Streams. The best we can do is check that we're not
92 // trying to use two different factories using the different interfaces.
93 RTC_CHECK(config.decoder_factory);
94 RTC_CHECK_EQ(config.decoder_factory,
95 channel_proxy_->GetAudioDecoderFactory());
96
mflodman3d7db262016-04-29 00:57:13 -070097 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
kwiberg1c07c702017-03-27 07:15:49 -070098 channel_proxy_->SetReceiveCodecs(config.decoder_map);
kwibergd32bf752017-01-19 07:03:59 -080099
solenberg7add0582015-11-20 09:59:34 -0800100 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700101 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800102 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
isheriff6f8d6862016-05-26 11:24:55 -0700103 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800104 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200105 } else {
106 RTC_NOTREACHED() << "Unsupported RTP extension.";
107 }
108 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100109 // Configure bandwidth estimation.
nisse0245da02016-11-30 03:35:20 -0800110 channel_proxy_->RegisterReceiverCongestionControlObjects(packet_router);
nisse0f15f922017-06-21 01:05:22 -0700111
112 // Register with transport.
113 rtp_stream_receiver_ =
114 receiver_controller->CreateReceiver(config_.rtp.remote_ssrc,
115 channel_proxy_.get());
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200116}
117
pbosa2f30de2015-10-15 05:22:13 -0700118AudioReceiveStream::~AudioReceiveStream() {
solenberg3ebbcb52017-01-31 03:58:40 -0800119 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbosa2f30de2015-10-15 05:22:13 -0700120 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
aleloi04c07222016-11-22 06:42:53 -0800121 if (playing_) {
122 Stop();
123 }
solenberg7602aab2016-11-14 11:30:07 -0800124 channel_proxy_->DisassociateSendChannel();
mflodman3d7db262016-04-29 00:57:13 -0700125 channel_proxy_->DeRegisterExternalTransport();
nissefdbfdc92017-03-31 05:44:52 -0700126 channel_proxy_->ResetReceiverCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700127 channel_proxy_->SetRtcEventLog(nullptr);
pbosa2f30de2015-10-15 05:22:13 -0700128}
129
solenberg7add0582015-11-20 09:59:34 -0800130void AudioReceiveStream::Start() {
solenberg3ebbcb52017-01-31 03:58:40 -0800131 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800132 if (playing_) {
133 return;
134 }
135
136 int error = SetVoiceEnginePlayout(true);
aleloi84ef6152016-08-04 05:28:21 -0700137 if (error != 0) {
138 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
aleloi04c07222016-11-22 06:42:53 -0800139 return;
aleloi84ef6152016-08-04 05:28:21 -0700140 }
aleloi04c07222016-11-22 06:42:53 -0800141
142 if (!audio_state()->mixer()->AddSource(this)) {
143 LOG(LS_ERROR) << "Failed to add source to mixer.";
144 SetVoiceEnginePlayout(false);
145 return;
146 }
147
148 playing_ = true;
solenberg7add0582015-11-20 09:59:34 -0800149}
150
151void AudioReceiveStream::Stop() {
solenberg3ebbcb52017-01-31 03:58:40 -0800152 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800153 if (!playing_) {
154 return;
155 }
156 playing_ = false;
157
158 audio_state()->mixer()->RemoveSource(this);
159 SetVoiceEnginePlayout(false);
solenberg7add0582015-11-20 09:59:34 -0800160}
161
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200162webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800163 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200164 webrtc::AudioReceiveStream::Stats stats;
165 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg8b85de22015-11-16 09:48:04 -0800166
solenberg358057b2015-11-27 10:46:42 -0800167 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenbergbd9a77f2017-02-06 12:53:57 -0800168 // TODO(solenberg): Don't return here if we can't get the codec - return the
169 // stats we *can* get.
solenberg85a04962015-10-27 03:35:21 -0700170 webrtc::CodecInst codec_inst = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800171 if (!channel_proxy_->GetRecCodec(&codec_inst)) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200172 return stats;
173 }
174
solenberg85a04962015-10-27 03:35:21 -0700175 stats.bytes_rcvd = call_stats.bytesReceived;
176 stats.packets_rcvd = call_stats.packetsReceived;
177 stats.packets_lost = call_stats.cumulativeLost;
178 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800179 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700180 if (codec_inst.pltype != -1) {
181 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800182 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200183 }
solenberg85a04962015-10-27 03:35:21 -0700184 stats.ext_seqnum = call_stats.extendedMax;
185 if (codec_inst.plfreq / 1000 > 0) {
186 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200187 }
solenberg358057b2015-11-27 10:46:42 -0800188 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
189 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200190
solenberg8b85de22015-11-16 09:48:04 -0800191 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800192 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800193 stats.jitter_buffer_ms = ns.currentBufferSize;
194 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
195 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
196 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
197 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
198 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
199 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200200
solenberg358057b2015-11-27 10:46:42 -0800201 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800202 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
203 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
204 stats.decoding_normal = ds.decoded_normal;
205 stats.decoding_plc = ds.decoded_plc;
206 stats.decoding_cng = ds.decoded_cng;
207 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700208 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200209
210 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200211}
212
solenberg796b8f92017-03-01 17:02:23 -0800213int AudioReceiveStream::GetOutputLevel() const {
214 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
215 return channel_proxy_->GetSpeechOutputLevel();
216}
217
kwibergfffa42b2016-02-23 10:46:32 -0800218void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800219 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
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) {
solenberg3ebbcb52017-01-31 03:58:40 -0800224 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700225 channel_proxy_->SetChannelOutputVolumeScaling(gain);
226}
227
hbos8d609f62017-04-10 07:39:05 -0700228std::vector<RtpSource> AudioReceiveStream::GetSources() const {
229 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
230 return channel_proxy_->GetSources();
231}
232
solenberg3ebbcb52017-01-31 03:58:40 -0800233AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
234 int sample_rate_hz,
235 AudioFrame* audio_frame) {
236 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
237}
238
239int AudioReceiveStream::Ssrc() const {
240 return config_.rtp.remote_ssrc;
241}
242
243int AudioReceiveStream::PreferredSampleRate() const {
244 return channel_proxy_->NeededFrequency();
245}
246
247int AudioReceiveStream::id() const {
248 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
249 return config_.rtp.remote_ssrc;
250}
251
252rtc::Optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
253 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
254 Syncable::Info info;
255
256 RtpRtcp* rtp_rtcp = nullptr;
257 RtpReceiver* rtp_receiver = nullptr;
258 channel_proxy_->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
259 RTC_DCHECK(rtp_rtcp);
260 RTC_DCHECK(rtp_receiver);
261
262 if (!rtp_receiver->Timestamp(&info.latest_received_capture_timestamp)) {
263 return rtc::Optional<Syncable::Info>();
264 }
265 if (!rtp_receiver->LastReceivedTimeMs(&info.latest_receive_time_ms)) {
266 return rtc::Optional<Syncable::Info>();
267 }
268 if (rtp_rtcp->RemoteNTP(&info.capture_time_ntp_secs,
269 &info.capture_time_ntp_frac,
270 nullptr,
271 nullptr,
272 &info.capture_time_source_clock) != 0) {
273 return rtc::Optional<Syncable::Info>();
274 }
275
solenberg08b19df2017-02-15 00:42:31 -0800276 info.current_delay_ms = channel_proxy_->GetDelayEstimate();
solenberg3ebbcb52017-01-31 03:58:40 -0800277 return rtc::Optional<Syncable::Info>(info);
278}
279
280uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
281 // Called on video capture thread.
282 return channel_proxy_->GetPlayoutTimestamp();
283}
284
285void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
286 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
287 return channel_proxy_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700288}
289
solenberg7602aab2016-11-14 11:30:07 -0800290void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800291 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg7602aab2016-11-14 11:30:07 -0800292 if (send_stream) {
293 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
294 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
295 voe_impl->GetChannelProxy(send_stream->config().voe_channel_id);
296 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
297 } else {
298 channel_proxy_->DisassociateSendChannel();
299 }
300}
301
pbos1ba8d392016-05-01 20:18:34 -0700302void AudioReceiveStream::SignalNetworkState(NetworkState state) {
solenberg3ebbcb52017-01-31 03:58:40 -0800303 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700304}
305
306bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
307 // TODO(solenberg): Tests call this function on a network thread, libjingle
308 // calls on the worker thread. We should move towards always using a network
309 // thread. Then this check can be enabled.
310 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
311 return channel_proxy_->ReceivedRTCPPacket(packet, length);
312}
313
nisse657bab22017-02-21 06:28:10 -0800314void AudioReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) {
pbos1ba8d392016-05-01 20:18:34 -0700315 // TODO(solenberg): Tests call this function on a network thread, libjingle
316 // calls on the worker thread. We should move towards always using a network
317 // thread. Then this check can be enabled.
318 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
nisse657bab22017-02-21 06:28:10 -0800319 channel_proxy_->OnRtpPacket(packet);
pbos1ba8d392016-05-01 20:18:34 -0700320}
321
solenberg3ebbcb52017-01-31 03:58:40 -0800322const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
323 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
324 return config_;
aleloi04c07222016-11-22 06:42:53 -0800325}
326
solenberg7add0582015-11-20 09:59:34 -0800327VoiceEngine* AudioReceiveStream::voice_engine() const {
aleloi04c07222016-11-22 06:42:53 -0800328 auto* voice_engine = audio_state()->voice_engine();
solenberg7add0582015-11-20 09:59:34 -0800329 RTC_DCHECK(voice_engine);
330 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200331}
aleloi04c07222016-11-22 06:42:53 -0800332
solenberg3ebbcb52017-01-31 03:58:40 -0800333internal::AudioState* AudioReceiveStream::audio_state() const {
334 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
335 RTC_DCHECK(audio_state);
336 return audio_state;
337}
338
aleloi04c07222016-11-22 06:42:53 -0800339int AudioReceiveStream::SetVoiceEnginePlayout(bool playout) {
340 ScopedVoEInterface<VoEBase> base(voice_engine());
341 if (playout) {
342 return base->StartPlayout(config_.voe_channel_id);
343 } else {
344 return base->StopPlayout(config_.voe_channel_id);
345 }
346}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200347} // namespace internal
348} // namespace webrtc