blob: 8c073d9c8c3250e29d132d4cbb27566c999687da [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"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020023#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
solenberg3ebbcb52017-01-31 03:58:40 -080024#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
25#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
solenberg13725082015-11-25 08:16:52 -080026#include "webrtc/voice_engine/channel_proxy.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020027#include "webrtc/voice_engine/include/voe_base.h"
solenberg13725082015-11-25 08:16:52 -080028#include "webrtc/voice_engine/voice_engine_impl.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020029
30namespace webrtc {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010031
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020032std::string AudioReceiveStream::Config::Rtp::ToString() const {
33 std::stringstream ss;
34 ss << "{remote_ssrc: " << remote_ssrc;
solenberg85a04962015-10-27 03:35:21 -070035 ss << ", local_ssrc: " << local_ssrc;
solenberg8189b022016-06-14 12:13:00 -070036 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
37 ss << ", nack: " << nack.ToString();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020038 ss << ", extensions: [";
39 for (size_t i = 0; i < extensions.size(); ++i) {
40 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020041 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020042 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020043 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020044 }
45 ss << ']';
46 ss << '}';
47 return ss.str();
48}
49
50std::string AudioReceiveStream::Config::ToString() const {
51 std::stringstream ss;
52 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070053 ss << ", rtcp_send_transport: "
deadbeef922246a2017-02-26 04:18:12 -080054 << (rtcp_send_transport ? "(Transport)" : "null");
pbos8fc7fa72015-07-15 08:02:58 -070055 ss << ", voe_channel_id: " << voe_channel_id;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020056 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070057 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020058 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020059 ss << '}';
60 return ss.str();
61}
62
63namespace internal {
64AudioReceiveStream::AudioReceiveStream(
nisse0245da02016-11-30 03:35:20 -080065 PacketRouter* packet_router,
solenberg566ef242015-11-06 15:34:49 -080066 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070067 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
68 webrtc::RtcEventLog* event_log)
nisse4709e892017-02-07 01:18:43 -080069 : config_(config),
solenberg6b341242017-02-06 13:39:38 -080070 audio_state_(audio_state) {
pbosa2f30de2015-10-15 05:22:13 -070071 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080072 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080073 RTC_DCHECK(audio_state_.get());
nisse0245da02016-11-30 03:35:20 -080074 RTC_DCHECK(packet_router);
solenberg7add0582015-11-20 09:59:34 -080075
solenberg3ebbcb52017-01-31 03:58:40 -080076 module_process_thread_checker_.DetachFromThread();
77
solenberg13725082015-11-25 08:16:52 -080078 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080079 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
ivoc14d5dbe2016-07-04 07:06:55 -070080 channel_proxy_->SetRtcEventLog(event_log);
solenberg13725082015-11-25 08:16:52 -080081 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -070082 // TODO(solenberg): Config NACK history window (which is a packet count),
83 // using the actual packet size for the configured codec.
84 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
85 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -070086
ossu29b1a8d2016-06-13 07:34:51 -070087 // TODO(ossu): This is where we'd like to set the decoder factory to
88 // use. However, since it needs to be included when constructing Channel, we
89 // cannot do that until we're able to move Channel ownership into the
90 // Audio{Send,Receive}Streams. The best we can do is check that we're not
91 // trying to use two different factories using the different interfaces.
92 RTC_CHECK(config.decoder_factory);
93 RTC_CHECK_EQ(config.decoder_factory,
94 channel_proxy_->GetAudioDecoderFactory());
95
mflodman3d7db262016-04-29 00:57:13 -070096 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
kwiberg670a7f32017-03-24 05:56:21 -070097
98 for (const auto& kv : config.decoder_map) {
99 channel_proxy_->SetRecPayloadType(kv.first, kv.second);
100 }
kwibergd32bf752017-01-19 07:03:59 -0800101
solenberg7add0582015-11-20 09:59:34 -0800102 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700103 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800104 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
isheriff6f8d6862016-05-26 11:24:55 -0700105 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800106 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200107 } else {
108 RTC_NOTREACHED() << "Unsupported RTP extension.";
109 }
110 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100111 // Configure bandwidth estimation.
nisse0245da02016-11-30 03:35:20 -0800112 channel_proxy_->RegisterReceiverCongestionControlObjects(packet_router);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200113}
114
pbosa2f30de2015-10-15 05:22:13 -0700115AudioReceiveStream::~AudioReceiveStream() {
solenberg3ebbcb52017-01-31 03:58:40 -0800116 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbosa2f30de2015-10-15 05:22:13 -0700117 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
aleloi04c07222016-11-22 06:42:53 -0800118 if (playing_) {
119 Stop();
120 }
solenberg7602aab2016-11-14 11:30:07 -0800121 channel_proxy_->DisassociateSendChannel();
mflodman3d7db262016-04-29 00:57:13 -0700122 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800123 channel_proxy_->ResetCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700124 channel_proxy_->SetRtcEventLog(nullptr);
pbosa2f30de2015-10-15 05:22:13 -0700125}
126
solenberg7add0582015-11-20 09:59:34 -0800127void AudioReceiveStream::Start() {
solenberg3ebbcb52017-01-31 03:58:40 -0800128 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800129 if (playing_) {
130 return;
131 }
132
133 int error = SetVoiceEnginePlayout(true);
aleloi84ef6152016-08-04 05:28:21 -0700134 if (error != 0) {
135 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
aleloi04c07222016-11-22 06:42:53 -0800136 return;
aleloi84ef6152016-08-04 05:28:21 -0700137 }
aleloi04c07222016-11-22 06:42:53 -0800138
139 if (!audio_state()->mixer()->AddSource(this)) {
140 LOG(LS_ERROR) << "Failed to add source to mixer.";
141 SetVoiceEnginePlayout(false);
142 return;
143 }
144
145 playing_ = true;
solenberg7add0582015-11-20 09:59:34 -0800146}
147
148void AudioReceiveStream::Stop() {
solenberg3ebbcb52017-01-31 03:58:40 -0800149 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800150 if (!playing_) {
151 return;
152 }
153 playing_ = false;
154
155 audio_state()->mixer()->RemoveSource(this);
156 SetVoiceEnginePlayout(false);
solenberg7add0582015-11-20 09:59:34 -0800157}
158
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200159webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800160 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200161 webrtc::AudioReceiveStream::Stats stats;
162 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg8b85de22015-11-16 09:48:04 -0800163
solenberg358057b2015-11-27 10:46:42 -0800164 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenbergbd9a77f2017-02-06 12:53:57 -0800165 // TODO(solenberg): Don't return here if we can't get the codec - return the
166 // stats we *can* get.
solenberg85a04962015-10-27 03:35:21 -0700167 webrtc::CodecInst codec_inst = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800168 if (!channel_proxy_->GetRecCodec(&codec_inst)) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200169 return stats;
170 }
171
solenberg85a04962015-10-27 03:35:21 -0700172 stats.bytes_rcvd = call_stats.bytesReceived;
173 stats.packets_rcvd = call_stats.packetsReceived;
174 stats.packets_lost = call_stats.cumulativeLost;
175 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800176 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700177 if (codec_inst.pltype != -1) {
178 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800179 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200180 }
solenberg85a04962015-10-27 03:35:21 -0700181 stats.ext_seqnum = call_stats.extendedMax;
182 if (codec_inst.plfreq / 1000 > 0) {
183 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200184 }
solenberg358057b2015-11-27 10:46:42 -0800185 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
186 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200187
solenberg8b85de22015-11-16 09:48:04 -0800188 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800189 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800190 stats.jitter_buffer_ms = ns.currentBufferSize;
191 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
192 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
193 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
194 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
195 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
196 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200197
solenberg358057b2015-11-27 10:46:42 -0800198 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800199 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
200 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
201 stats.decoding_normal = ds.decoded_normal;
202 stats.decoding_plc = ds.decoded_plc;
203 stats.decoding_cng = ds.decoded_cng;
204 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700205 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200206
207 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200208}
209
solenberg796b8f92017-03-01 17:02:23 -0800210int AudioReceiveStream::GetOutputLevel() const {
211 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
212 return channel_proxy_->GetSpeechOutputLevel();
213}
214
kwibergfffa42b2016-02-23 10:46:32 -0800215void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800216 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
kwibergfffa42b2016-02-23 10:46:32 -0800217 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100218}
219
solenberg217fb662016-06-17 08:30:54 -0700220void AudioReceiveStream::SetGain(float gain) {
solenberg3ebbcb52017-01-31 03:58:40 -0800221 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700222 channel_proxy_->SetChannelOutputVolumeScaling(gain);
223}
224
solenberg3ebbcb52017-01-31 03:58:40 -0800225AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
226 int sample_rate_hz,
227 AudioFrame* audio_frame) {
228 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
229}
230
231int AudioReceiveStream::Ssrc() const {
232 return config_.rtp.remote_ssrc;
233}
234
235int AudioReceiveStream::PreferredSampleRate() const {
236 return channel_proxy_->NeededFrequency();
237}
238
239int AudioReceiveStream::id() const {
240 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
241 return config_.rtp.remote_ssrc;
242}
243
244rtc::Optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
245 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
246 Syncable::Info info;
247
248 RtpRtcp* rtp_rtcp = nullptr;
249 RtpReceiver* rtp_receiver = nullptr;
250 channel_proxy_->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
251 RTC_DCHECK(rtp_rtcp);
252 RTC_DCHECK(rtp_receiver);
253
254 if (!rtp_receiver->Timestamp(&info.latest_received_capture_timestamp)) {
255 return rtc::Optional<Syncable::Info>();
256 }
257 if (!rtp_receiver->LastReceivedTimeMs(&info.latest_receive_time_ms)) {
258 return rtc::Optional<Syncable::Info>();
259 }
260 if (rtp_rtcp->RemoteNTP(&info.capture_time_ntp_secs,
261 &info.capture_time_ntp_frac,
262 nullptr,
263 nullptr,
264 &info.capture_time_source_clock) != 0) {
265 return rtc::Optional<Syncable::Info>();
266 }
267
solenberg08b19df2017-02-15 00:42:31 -0800268 info.current_delay_ms = channel_proxy_->GetDelayEstimate();
solenberg3ebbcb52017-01-31 03:58:40 -0800269 return rtc::Optional<Syncable::Info>(info);
270}
271
272uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
273 // Called on video capture thread.
274 return channel_proxy_->GetPlayoutTimestamp();
275}
276
277void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
278 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
279 return channel_proxy_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700280}
281
solenberg7602aab2016-11-14 11:30:07 -0800282void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800283 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg7602aab2016-11-14 11:30:07 -0800284 if (send_stream) {
285 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
286 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
287 voe_impl->GetChannelProxy(send_stream->config().voe_channel_id);
288 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
289 } else {
290 channel_proxy_->DisassociateSendChannel();
291 }
292}
293
pbos1ba8d392016-05-01 20:18:34 -0700294void AudioReceiveStream::SignalNetworkState(NetworkState state) {
solenberg3ebbcb52017-01-31 03:58:40 -0800295 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700296}
297
298bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
299 // TODO(solenberg): Tests call this function on a network thread, libjingle
300 // calls on the worker thread. We should move towards always using a network
301 // thread. Then this check can be enabled.
302 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
303 return channel_proxy_->ReceivedRTCPPacket(packet, length);
304}
305
nisse657bab22017-02-21 06:28:10 -0800306void AudioReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) {
pbos1ba8d392016-05-01 20:18:34 -0700307 // 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());
nisse657bab22017-02-21 06:28:10 -0800311 channel_proxy_->OnRtpPacket(packet);
pbos1ba8d392016-05-01 20:18:34 -0700312}
313
solenberg3ebbcb52017-01-31 03:58:40 -0800314const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
315 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
316 return config_;
aleloi04c07222016-11-22 06:42:53 -0800317}
318
solenberg7add0582015-11-20 09:59:34 -0800319VoiceEngine* AudioReceiveStream::voice_engine() const {
aleloi04c07222016-11-22 06:42:53 -0800320 auto* voice_engine = audio_state()->voice_engine();
solenberg7add0582015-11-20 09:59:34 -0800321 RTC_DCHECK(voice_engine);
322 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200323}
aleloi04c07222016-11-22 06:42:53 -0800324
solenberg3ebbcb52017-01-31 03:58:40 -0800325internal::AudioState* AudioReceiveStream::audio_state() const {
326 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
327 RTC_DCHECK(audio_state);
328 return audio_state;
329}
330
aleloi04c07222016-11-22 06:42:53 -0800331int AudioReceiveStream::SetVoiceEnginePlayout(bool playout) {
332 ScopedVoEInterface<VoEBase> base(voice_engine());
333 if (playout) {
334 return base->StartPlayout(config_.voe_channel_id);
335 } else {
336 return base->StopPlayout(config_.voe_channel_id);
337 }
338}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200339} // namespace internal
340} // namespace webrtc