blob: be5c18a57c4068a6b8f0da58c6eeb26489c7027e [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"
nisse0f15f922017-06-21 01:05:22 -070020#include "webrtc/call/rtp_stream_receiver_controller_interface.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020021#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
solenberg3ebbcb52017-01-31 03:58:40 -080022#include "webrtc/modules/rtp_rtcp/include/rtp_receiver.h"
23#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020024#include "webrtc/rtc_base/checks.h"
25#include "webrtc/rtc_base/logging.h"
26#include "webrtc/rtc_base/timeutils.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();
zsteine76bd3a2017-07-14 12:17:49 -0700190 stats.total_output_energy = channel_proxy_->GetTotalOutputEnergy();
191 stats.total_output_duration = channel_proxy_->GetTotalOutputDuration();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200192
solenberg8b85de22015-11-16 09:48:04 -0800193 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800194 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800195 stats.jitter_buffer_ms = ns.currentBufferSize;
196 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
197 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
198 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
199 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
200 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
201 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200202
solenberg358057b2015-11-27 10:46:42 -0800203 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800204 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
205 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
206 stats.decoding_normal = ds.decoded_normal;
207 stats.decoding_plc = ds.decoded_plc;
208 stats.decoding_cng = ds.decoded_cng;
209 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700210 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200211
212 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200213}
214
solenberg796b8f92017-03-01 17:02:23 -0800215int AudioReceiveStream::GetOutputLevel() const {
216 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
217 return channel_proxy_->GetSpeechOutputLevel();
218}
219
kwibergfffa42b2016-02-23 10:46:32 -0800220void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800221 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
kwibergfffa42b2016-02-23 10:46:32 -0800222 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100223}
224
solenberg217fb662016-06-17 08:30:54 -0700225void AudioReceiveStream::SetGain(float gain) {
solenberg3ebbcb52017-01-31 03:58:40 -0800226 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700227 channel_proxy_->SetChannelOutputVolumeScaling(gain);
228}
229
hbos8d609f62017-04-10 07:39:05 -0700230std::vector<RtpSource> AudioReceiveStream::GetSources() const {
231 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
232 return channel_proxy_->GetSources();
233}
234
solenberg3ebbcb52017-01-31 03:58:40 -0800235AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
236 int sample_rate_hz,
237 AudioFrame* audio_frame) {
238 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
239}
240
241int AudioReceiveStream::Ssrc() const {
242 return config_.rtp.remote_ssrc;
243}
244
245int AudioReceiveStream::PreferredSampleRate() const {
246 return channel_proxy_->NeededFrequency();
247}
248
249int AudioReceiveStream::id() const {
250 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
251 return config_.rtp.remote_ssrc;
252}
253
254rtc::Optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
255 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
256 Syncable::Info info;
257
258 RtpRtcp* rtp_rtcp = nullptr;
259 RtpReceiver* rtp_receiver = nullptr;
260 channel_proxy_->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
261 RTC_DCHECK(rtp_rtcp);
262 RTC_DCHECK(rtp_receiver);
263
264 if (!rtp_receiver->Timestamp(&info.latest_received_capture_timestamp)) {
265 return rtc::Optional<Syncable::Info>();
266 }
267 if (!rtp_receiver->LastReceivedTimeMs(&info.latest_receive_time_ms)) {
268 return rtc::Optional<Syncable::Info>();
269 }
270 if (rtp_rtcp->RemoteNTP(&info.capture_time_ntp_secs,
271 &info.capture_time_ntp_frac,
272 nullptr,
273 nullptr,
274 &info.capture_time_source_clock) != 0) {
275 return rtc::Optional<Syncable::Info>();
276 }
277
solenberg08b19df2017-02-15 00:42:31 -0800278 info.current_delay_ms = channel_proxy_->GetDelayEstimate();
solenberg3ebbcb52017-01-31 03:58:40 -0800279 return rtc::Optional<Syncable::Info>(info);
280}
281
282uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
283 // Called on video capture thread.
284 return channel_proxy_->GetPlayoutTimestamp();
285}
286
287void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
288 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
289 return channel_proxy_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700290}
291
solenberg7602aab2016-11-14 11:30:07 -0800292void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800293 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg7602aab2016-11-14 11:30:07 -0800294 if (send_stream) {
295 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
296 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
eladalonabbc4302017-07-26 02:09:44 -0700297 voe_impl->GetChannelProxy(send_stream->GetConfig().voe_channel_id);
solenberg7602aab2016-11-14 11:30:07 -0800298 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
299 } else {
300 channel_proxy_->DisassociateSendChannel();
301 }
302}
303
pbos1ba8d392016-05-01 20:18:34 -0700304void AudioReceiveStream::SignalNetworkState(NetworkState state) {
solenberg3ebbcb52017-01-31 03:58:40 -0800305 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700306}
307
308bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
309 // TODO(solenberg): Tests call this function on a network thread, libjingle
310 // calls on the worker thread. We should move towards always using a network
311 // thread. Then this check can be enabled.
312 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
313 return channel_proxy_->ReceivedRTCPPacket(packet, length);
314}
315
nisse657bab22017-02-21 06:28:10 -0800316void AudioReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) {
pbos1ba8d392016-05-01 20:18:34 -0700317 // TODO(solenberg): Tests call this function on a network thread, libjingle
318 // calls on the worker thread. We should move towards always using a network
319 // thread. Then this check can be enabled.
320 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
nisse657bab22017-02-21 06:28:10 -0800321 channel_proxy_->OnRtpPacket(packet);
pbos1ba8d392016-05-01 20:18:34 -0700322}
323
solenberg3ebbcb52017-01-31 03:58:40 -0800324const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
325 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
326 return config_;
aleloi04c07222016-11-22 06:42:53 -0800327}
328
solenberg7add0582015-11-20 09:59:34 -0800329VoiceEngine* AudioReceiveStream::voice_engine() const {
aleloi04c07222016-11-22 06:42:53 -0800330 auto* voice_engine = audio_state()->voice_engine();
solenberg7add0582015-11-20 09:59:34 -0800331 RTC_DCHECK(voice_engine);
332 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200333}
aleloi04c07222016-11-22 06:42:53 -0800334
solenberg3ebbcb52017-01-31 03:58:40 -0800335internal::AudioState* AudioReceiveStream::audio_state() const {
336 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
337 RTC_DCHECK(audio_state);
338 return audio_state;
339}
340
aleloi04c07222016-11-22 06:42:53 -0800341int AudioReceiveStream::SetVoiceEnginePlayout(bool playout) {
342 ScopedVoEInterface<VoEBase> base(voice_engine());
343 if (playout) {
344 return base->StartPlayout(config_.voe_channel_id);
345 } else {
346 return base->StopPlayout(config_.voe_channel_id);
347 }
348}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200349} // namespace internal
350} // namespace webrtc