blob: 8228a600b5e127d311f1cde83cebe5888b3eff81 [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: "
54 << (rtcp_send_transport ? "(Transport)" : "nullptr");
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,
66 RemoteBitrateEstimator* remote_bitrate_estimator,
solenberg566ef242015-11-06 15:34:49 -080067 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -070068 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
69 webrtc::RtcEventLog* event_log)
nisse0245da02016-11-30 03:35:20 -080070 : remote_bitrate_estimator_(remote_bitrate_estimator),
71 config_(config),
solenberg6b341242017-02-06 13:39:38 -080072 audio_state_(audio_state) {
pbosa2f30de2015-10-15 05:22:13 -070073 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080074 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080075 RTC_DCHECK(audio_state_.get());
nisse0245da02016-11-30 03:35:20 -080076 RTC_DCHECK(packet_router);
77 RTC_DCHECK(remote_bitrate_estimator);
solenberg7add0582015-11-20 09:59:34 -080078
solenberg3ebbcb52017-01-31 03:58:40 -080079 module_process_thread_checker_.DetachFromThread();
80
solenberg13725082015-11-25 08:16:52 -080081 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080082 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
ivoc14d5dbe2016-07-04 07:06:55 -070083 channel_proxy_->SetRtcEventLog(event_log);
solenberg13725082015-11-25 08:16:52 -080084 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -070085 // TODO(solenberg): Config NACK history window (which is a packet count),
86 // using the actual packet size for the configured codec.
87 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
88 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -070089
ossu29b1a8d2016-06-13 07:34:51 -070090 // TODO(ossu): This is where we'd like to set the decoder factory to
91 // use. However, since it needs to be included when constructing Channel, we
92 // cannot do that until we're able to move Channel ownership into the
93 // Audio{Send,Receive}Streams. The best we can do is check that we're not
94 // trying to use two different factories using the different interfaces.
95 RTC_CHECK(config.decoder_factory);
96 RTC_CHECK_EQ(config.decoder_factory,
97 channel_proxy_->GetAudioDecoderFactory());
98
mflodman3d7db262016-04-29 00:57:13 -070099 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
100
kwibergd32bf752017-01-19 07:03:59 -0800101 for (const auto& kv : config.decoder_map) {
102 channel_proxy_->SetRecPayloadType(kv.first, kv.second);
103 }
104
solenberg7add0582015-11-20 09:59:34 -0800105 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700106 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800107 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
isheriff6f8d6862016-05-26 11:24:55 -0700108 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800109 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200110 } else {
111 RTC_NOTREACHED() << "Unsupported RTP extension.";
112 }
113 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100114 // Configure bandwidth estimation.
nisse0245da02016-11-30 03:35:20 -0800115 channel_proxy_->RegisterReceiverCongestionControlObjects(packet_router);
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();
stefanbba9dec2016-02-01 04:39:55 -0800126 channel_proxy_->ResetCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700127 channel_proxy_->SetRtcEventLog(nullptr);
nisse0245da02016-11-30 03:35:20 -0800128 remote_bitrate_estimator_->RemoveStream(config_.rtp.remote_ssrc);
pbosa2f30de2015-10-15 05:22:13 -0700129}
130
solenberg7add0582015-11-20 09:59:34 -0800131void AudioReceiveStream::Start() {
solenberg3ebbcb52017-01-31 03:58:40 -0800132 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800133 if (playing_) {
134 return;
135 }
136
137 int error = SetVoiceEnginePlayout(true);
aleloi84ef6152016-08-04 05:28:21 -0700138 if (error != 0) {
139 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
aleloi04c07222016-11-22 06:42:53 -0800140 return;
aleloi84ef6152016-08-04 05:28:21 -0700141 }
aleloi04c07222016-11-22 06:42:53 -0800142
143 if (!audio_state()->mixer()->AddSource(this)) {
144 LOG(LS_ERROR) << "Failed to add source to mixer.";
145 SetVoiceEnginePlayout(false);
146 return;
147 }
148
149 playing_ = true;
solenberg7add0582015-11-20 09:59:34 -0800150}
151
152void AudioReceiveStream::Stop() {
solenberg3ebbcb52017-01-31 03:58:40 -0800153 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800154 if (!playing_) {
155 return;
156 }
157 playing_ = false;
158
159 audio_state()->mixer()->RemoveSource(this);
160 SetVoiceEnginePlayout(false);
solenberg7add0582015-11-20 09:59:34 -0800161}
162
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200163webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800164 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200165 webrtc::AudioReceiveStream::Stats stats;
166 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg8b85de22015-11-16 09:48:04 -0800167
solenberg358057b2015-11-27 10:46:42 -0800168 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenbergbd9a77f2017-02-06 12:53:57 -0800169 // TODO(solenberg): Don't return here if we can't get the codec - return the
170 // stats we *can* get.
solenberg85a04962015-10-27 03:35:21 -0700171 webrtc::CodecInst codec_inst = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800172 if (!channel_proxy_->GetRecCodec(&codec_inst)) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200173 return stats;
174 }
175
solenberg85a04962015-10-27 03:35:21 -0700176 stats.bytes_rcvd = call_stats.bytesReceived;
177 stats.packets_rcvd = call_stats.packetsReceived;
178 stats.packets_lost = call_stats.cumulativeLost;
179 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800180 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700181 if (codec_inst.pltype != -1) {
182 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800183 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200184 }
solenberg85a04962015-10-27 03:35:21 -0700185 stats.ext_seqnum = call_stats.extendedMax;
186 if (codec_inst.plfreq / 1000 > 0) {
187 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200188 }
solenberg358057b2015-11-27 10:46:42 -0800189 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
190 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200191
solenberg8b85de22015-11-16 09:48:04 -0800192 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800193 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800194 stats.jitter_buffer_ms = ns.currentBufferSize;
195 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
196 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
197 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
198 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
199 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
200 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200201
solenberg358057b2015-11-27 10:46:42 -0800202 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800203 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
204 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
205 stats.decoding_normal = ds.decoded_normal;
206 stats.decoding_plc = ds.decoded_plc;
207 stats.decoding_cng = ds.decoded_cng;
208 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700209 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200210
211 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200212}
213
kwibergfffa42b2016-02-23 10:46:32 -0800214void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800215 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
kwibergfffa42b2016-02-23 10:46:32 -0800216 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100217}
218
solenberg217fb662016-06-17 08:30:54 -0700219void AudioReceiveStream::SetGain(float gain) {
solenberg3ebbcb52017-01-31 03:58:40 -0800220 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700221 channel_proxy_->SetChannelOutputVolumeScaling(gain);
222}
223
solenberg3ebbcb52017-01-31 03:58:40 -0800224AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
225 int sample_rate_hz,
226 AudioFrame* audio_frame) {
227 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
228}
229
230int AudioReceiveStream::Ssrc() const {
231 return config_.rtp.remote_ssrc;
232}
233
234int AudioReceiveStream::PreferredSampleRate() const {
235 return channel_proxy_->NeededFrequency();
236}
237
238int AudioReceiveStream::id() const {
239 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
240 return config_.rtp.remote_ssrc;
241}
242
243rtc::Optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
244 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
245 Syncable::Info info;
246
247 RtpRtcp* rtp_rtcp = nullptr;
248 RtpReceiver* rtp_receiver = nullptr;
249 channel_proxy_->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
250 RTC_DCHECK(rtp_rtcp);
251 RTC_DCHECK(rtp_receiver);
252
253 if (!rtp_receiver->Timestamp(&info.latest_received_capture_timestamp)) {
254 return rtc::Optional<Syncable::Info>();
255 }
256 if (!rtp_receiver->LastReceivedTimeMs(&info.latest_receive_time_ms)) {
257 return rtc::Optional<Syncable::Info>();
258 }
259 if (rtp_rtcp->RemoteNTP(&info.capture_time_ntp_secs,
260 &info.capture_time_ntp_frac,
261 nullptr,
262 nullptr,
263 &info.capture_time_source_clock) != 0) {
264 return rtc::Optional<Syncable::Info>();
265 }
266
267 int jitter_buffer_delay_ms = 0;
268 int playout_buffer_delay_ms = 0;
269 channel_proxy_->GetDelayEstimate(&jitter_buffer_delay_ms,
270 &playout_buffer_delay_ms);
271 info.current_delay_ms = jitter_buffer_delay_ms + playout_buffer_delay_ms;
272 return rtc::Optional<Syncable::Info>(info);
273}
274
275uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
276 // Called on video capture thread.
277 return channel_proxy_->GetPlayoutTimestamp();
278}
279
280void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
281 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
282 return channel_proxy_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700283}
284
solenberg7602aab2016-11-14 11:30:07 -0800285void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800286 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg7602aab2016-11-14 11:30:07 -0800287 if (send_stream) {
288 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
289 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
290 voe_impl->GetChannelProxy(send_stream->config().voe_channel_id);
291 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
292 } else {
293 channel_proxy_->DisassociateSendChannel();
294 }
295}
296
pbos1ba8d392016-05-01 20:18:34 -0700297void AudioReceiveStream::SignalNetworkState(NetworkState state) {
solenberg3ebbcb52017-01-31 03:58:40 -0800298 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700299}
300
301bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
302 // TODO(solenberg): Tests call this function on a network thread, libjingle
303 // calls on the worker thread. We should move towards always using a network
304 // thread. Then this check can be enabled.
305 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
306 return channel_proxy_->ReceivedRTCPPacket(packet, length);
307}
308
309bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
310 size_t length,
311 const PacketTime& packet_time) {
312 // TODO(solenberg): Tests call this function on a network thread, libjingle
313 // calls on the worker thread. We should move towards always using a network
314 // thread. Then this check can be enabled.
315 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700316 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
317}
318
solenberg3ebbcb52017-01-31 03:58:40 -0800319const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
320 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
321 return config_;
aleloi04c07222016-11-22 06:42:53 -0800322}
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
solenberg3ebbcb52017-01-31 03:58:40 -0800330internal::AudioState* AudioReceiveStream::audio_state() const {
331 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
332 RTC_DCHECK(audio_state);
333 return audio_state;
334}
335
aleloi04c07222016-11-22 06:42:53 -0800336int AudioReceiveStream::SetVoiceEnginePlayout(bool playout) {
337 ScopedVoEInterface<VoEBase> base(voice_engine());
338 if (playout) {
339 return base->StartPlayout(config_.voe_channel_id);
340 } else {
341 return base->StopPlayout(config_.voe_channel_id);
342 }
343}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200344} // namespace internal
345} // namespace webrtc