blob: 74f17731ab77c170ec89e18a59af50b25ec3b50d [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/call/audio_sink.h"
17#include "audio/audio_send_stream.h"
18#include "audio/audio_state.h"
19#include "audio/conversion.h"
20#include "call/rtp_stream_receiver_controller_interface.h"
21#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
22#include "modules/rtp_rtcp/include/rtp_receiver.h"
23#include "modules/rtp_rtcp/include/rtp_rtcp.h"
24#include "rtc_base/checks.h"
25#include "rtc_base/logging.h"
26#include "rtc_base/timeutils.h"
27#include "voice_engine/channel_proxy.h"
28#include "voice_engine/include/voe_base.h"
29#include "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) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010072 RTC_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
solenberg1c239d42017-09-29 06:00:28 -070097 channel_proxy_->RegisterTransport(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_);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100120 RTC_LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100121 Stop();
solenberg7602aab2016-11-14 11:30:07 -0800122 channel_proxy_->DisassociateSendChannel();
solenberg1c239d42017-09-29 06:00:28 -0700123 channel_proxy_->RegisterTransport(nullptr);
nissefdbfdc92017-03-31 05:44:52 -0700124 channel_proxy_->ResetReceiverCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700125 channel_proxy_->SetRtcEventLog(nullptr);
pbosa2f30de2015-10-15 05:22:13 -0700126}
127
solenberg7add0582015-11-20 09:59:34 -0800128void AudioReceiveStream::Start() {
solenberg3ebbcb52017-01-31 03:58:40 -0800129 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800130 if (playing_) {
131 return;
132 }
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100133 channel_proxy_->StartPlayout();
aleloi04c07222016-11-22 06:42:53 -0800134 playing_ = true;
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100135 audio_state()->AddReceivingStream(this);
solenberg7add0582015-11-20 09:59:34 -0800136}
137
138void AudioReceiveStream::Stop() {
solenberg3ebbcb52017-01-31 03:58:40 -0800139 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800140 if (!playing_) {
141 return;
142 }
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100143 channel_proxy_->StopPlayout();
aleloi04c07222016-11-22 06:42:53 -0800144 playing_ = false;
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100145 audio_state()->RemoveReceivingStream(this);
solenberg7add0582015-11-20 09:59:34 -0800146}
147
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200148webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800149 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200150 webrtc::AudioReceiveStream::Stats stats;
151 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg8b85de22015-11-16 09:48:04 -0800152
solenberg358057b2015-11-27 10:46:42 -0800153 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenbergbd9a77f2017-02-06 12:53:57 -0800154 // TODO(solenberg): Don't return here if we can't get the codec - return the
155 // stats we *can* get.
solenberg85a04962015-10-27 03:35:21 -0700156 webrtc::CodecInst codec_inst = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800157 if (!channel_proxy_->GetRecCodec(&codec_inst)) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200158 return stats;
159 }
160
solenberg85a04962015-10-27 03:35:21 -0700161 stats.bytes_rcvd = call_stats.bytesReceived;
162 stats.packets_rcvd = call_stats.packetsReceived;
163 stats.packets_lost = call_stats.cumulativeLost;
164 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800165 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700166 if (codec_inst.pltype != -1) {
167 stats.codec_name = codec_inst.plname;
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100168 stats.codec_payload_type = codec_inst.pltype;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200169 }
solenberg85a04962015-10-27 03:35:21 -0700170 stats.ext_seqnum = call_stats.extendedMax;
171 if (codec_inst.plfreq / 1000 > 0) {
172 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200173 }
solenberg358057b2015-11-27 10:46:42 -0800174 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
175 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
zsteine76bd3a2017-07-14 12:17:49 -0700176 stats.total_output_energy = channel_proxy_->GetTotalOutputEnergy();
177 stats.total_output_duration = channel_proxy_->GetTotalOutputDuration();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200178
solenberg8b85de22015-11-16 09:48:04 -0800179 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800180 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800181 stats.jitter_buffer_ms = ns.currentBufferSize;
182 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700183 stats.total_samples_received = ns.totalSamplesReceived;
184 stats.concealed_samples = ns.concealedSamples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200185 stats.concealment_events = ns.concealmentEvents;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200186 stats.jitter_buffer_delay_seconds =
187 static_cast<double>(ns.jitterBufferDelayMs) /
188 static_cast<double>(rtc::kNumMillisecsPerSec);
solenberg8b85de22015-11-16 09:48:04 -0800189 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
190 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
191 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200192 stats.secondary_discarded_rate = Q14ToFloat(ns.currentSecondaryDiscardedRate);
solenberg8b85de22015-11-16 09:48:04 -0800193 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
194 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200195
solenberg358057b2015-11-27 10:46:42 -0800196 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800197 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
198 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
199 stats.decoding_normal = ds.decoded_normal;
200 stats.decoding_plc = ds.decoded_plc;
201 stats.decoding_cng = ds.decoded_cng;
202 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700203 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200204
205 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200206}
207
solenberg796b8f92017-03-01 17:02:23 -0800208int AudioReceiveStream::GetOutputLevel() const {
209 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
210 return channel_proxy_->GetSpeechOutputLevel();
211}
212
kwibergfffa42b2016-02-23 10:46:32 -0800213void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800214 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
kwibergfffa42b2016-02-23 10:46:32 -0800215 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100216}
217
solenberg217fb662016-06-17 08:30:54 -0700218void AudioReceiveStream::SetGain(float gain) {
solenberg3ebbcb52017-01-31 03:58:40 -0800219 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700220 channel_proxy_->SetChannelOutputVolumeScaling(gain);
221}
222
hbos8d609f62017-04-10 07:39:05 -0700223std::vector<RtpSource> AudioReceiveStream::GetSources() const {
224 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
225 return channel_proxy_->GetSources();
226}
227
solenberg3ebbcb52017-01-31 03:58:40 -0800228AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
229 int sample_rate_hz,
230 AudioFrame* audio_frame) {
231 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
232}
233
234int AudioReceiveStream::Ssrc() const {
235 return config_.rtp.remote_ssrc;
236}
237
238int AudioReceiveStream::PreferredSampleRate() const {
solenberg2397b9a2017-09-22 06:48:10 -0700239 return channel_proxy_->PreferredSampleRate();
solenberg3ebbcb52017-01-31 03:58:40 -0800240}
241
242int AudioReceiveStream::id() const {
243 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
244 return config_.rtp.remote_ssrc;
245}
246
247rtc::Optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
248 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
249 Syncable::Info info;
250
251 RtpRtcp* rtp_rtcp = nullptr;
252 RtpReceiver* rtp_receiver = nullptr;
253 channel_proxy_->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
254 RTC_DCHECK(rtp_rtcp);
255 RTC_DCHECK(rtp_receiver);
256
Niels Möllerc3fa8e12017-10-03 15:28:26 +0200257 if (!rtp_receiver->GetLatestTimestamps(
258 &info.latest_received_capture_timestamp,
259 &info.latest_receive_time_ms)) {
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100260 return rtc::nullopt;
solenberg3ebbcb52017-01-31 03:58:40 -0800261 }
262 if (rtp_rtcp->RemoteNTP(&info.capture_time_ntp_secs,
263 &info.capture_time_ntp_frac,
264 nullptr,
265 nullptr,
266 &info.capture_time_source_clock) != 0) {
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100267 return rtc::nullopt;
solenberg3ebbcb52017-01-31 03:58:40 -0800268 }
269
solenberg08b19df2017-02-15 00:42:31 -0800270 info.current_delay_ms = channel_proxy_->GetDelayEstimate();
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100271 return info;
solenberg3ebbcb52017-01-31 03:58:40 -0800272}
273
274uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
275 // Called on video capture thread.
276 return channel_proxy_->GetPlayoutTimestamp();
277}
278
279void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
280 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
281 return channel_proxy_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700282}
283
solenberg7602aab2016-11-14 11:30:07 -0800284void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800285 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg7602aab2016-11-14 11:30:07 -0800286 if (send_stream) {
287 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
288 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
eladalonabbc4302017-07-26 02:09:44 -0700289 voe_impl->GetChannelProxy(send_stream->GetConfig().voe_channel_id);
solenberg7602aab2016-11-14 11:30:07 -0800290 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
291 } else {
292 channel_proxy_->DisassociateSendChannel();
293 }
294}
295
pbos1ba8d392016-05-01 20:18:34 -0700296void AudioReceiveStream::SignalNetworkState(NetworkState state) {
solenberg3ebbcb52017-01-31 03:58:40 -0800297 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700298}
299
300bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
301 // TODO(solenberg): Tests call this function on a network thread, libjingle
302 // calls on the worker thread. We should move towards always using a network
303 // thread. Then this check can be enabled.
304 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
305 return channel_proxy_->ReceivedRTCPPacket(packet, length);
306}
307
nisse657bab22017-02-21 06:28:10 -0800308void AudioReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) {
pbos1ba8d392016-05-01 20:18:34 -0700309 // 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());
nisse657bab22017-02-21 06:28:10 -0800313 channel_proxy_->OnRtpPacket(packet);
pbos1ba8d392016-05-01 20:18:34 -0700314}
315
solenberg3ebbcb52017-01-31 03:58:40 -0800316const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
317 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
318 return config_;
aleloi04c07222016-11-22 06:42:53 -0800319}
320
solenberg7add0582015-11-20 09:59:34 -0800321VoiceEngine* AudioReceiveStream::voice_engine() const {
aleloi04c07222016-11-22 06:42:53 -0800322 auto* voice_engine = audio_state()->voice_engine();
solenberg7add0582015-11-20 09:59:34 -0800323 RTC_DCHECK(voice_engine);
324 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200325}
aleloi04c07222016-11-22 06:42:53 -0800326
solenberg3ebbcb52017-01-31 03:58:40 -0800327internal::AudioState* AudioReceiveStream::audio_state() const {
328 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
329 RTC_DCHECK(audio_state);
330 return audio_state;
331}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200332} // namespace internal
333} // namespace webrtc