blob: 5cca45e2c1d9dd6a2e766d9423de3502be270e0c [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),
solenberg566ef242015-11-06 15:34:49 -080072 audio_state_(audio_state),
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020073 rtp_header_parser_(RtpHeaderParser::Create()) {
pbosa2f30de2015-10-15 05:22:13 -070074 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080075 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080076 RTC_DCHECK(audio_state_.get());
nisse0245da02016-11-30 03:35:20 -080077 RTC_DCHECK(packet_router);
78 RTC_DCHECK(remote_bitrate_estimator);
solenberg566ef242015-11-06 15:34:49 -080079 RTC_DCHECK(rtp_header_parser_);
solenberg7add0582015-11-20 09:59:34 -080080
solenberg3ebbcb52017-01-31 03:58:40 -080081 module_process_thread_checker_.DetachFromThread();
82
solenberg13725082015-11-25 08:16:52 -080083 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080084 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
ivoc14d5dbe2016-07-04 07:06:55 -070085 channel_proxy_->SetRtcEventLog(event_log);
solenberg13725082015-11-25 08:16:52 -080086 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -070087 // TODO(solenberg): Config NACK history window (which is a packet count),
88 // using the actual packet size for the configured codec.
89 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
90 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -070091
ossu29b1a8d2016-06-13 07:34:51 -070092 // TODO(ossu): This is where we'd like to set the decoder factory to
93 // use. However, since it needs to be included when constructing Channel, we
94 // cannot do that until we're able to move Channel ownership into the
95 // Audio{Send,Receive}Streams. The best we can do is check that we're not
96 // trying to use two different factories using the different interfaces.
97 RTC_CHECK(config.decoder_factory);
98 RTC_CHECK_EQ(config.decoder_factory,
99 channel_proxy_->GetAudioDecoderFactory());
100
mflodman3d7db262016-04-29 00:57:13 -0700101 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
102
kwibergd32bf752017-01-19 07:03:59 -0800103 for (const auto& kv : config.decoder_map) {
104 channel_proxy_->SetRecPayloadType(kv.first, kv.second);
105 }
106
solenberg7add0582015-11-20 09:59:34 -0800107 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700108 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800109 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800110 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
111 kRtpExtensionAudioLevel, extension.id);
112 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700113 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800114 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
solenberg7add0582015-11-20 09:59:34 -0800115 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
116 kRtpExtensionTransportSequenceNumber, extension.id);
117 RTC_DCHECK(registered);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200118 } else {
119 RTC_NOTREACHED() << "Unsupported RTP extension.";
120 }
121 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100122 // Configure bandwidth estimation.
nisse0245da02016-11-30 03:35:20 -0800123 channel_proxy_->RegisterReceiverCongestionControlObjects(packet_router);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200124}
125
pbosa2f30de2015-10-15 05:22:13 -0700126AudioReceiveStream::~AudioReceiveStream() {
solenberg3ebbcb52017-01-31 03:58:40 -0800127 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbosa2f30de2015-10-15 05:22:13 -0700128 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
aleloi04c07222016-11-22 06:42:53 -0800129 if (playing_) {
130 Stop();
131 }
solenberg7602aab2016-11-14 11:30:07 -0800132 channel_proxy_->DisassociateSendChannel();
mflodman3d7db262016-04-29 00:57:13 -0700133 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800134 channel_proxy_->ResetCongestionControlObjects();
ivoc14d5dbe2016-07-04 07:06:55 -0700135 channel_proxy_->SetRtcEventLog(nullptr);
nisse0245da02016-11-30 03:35:20 -0800136 remote_bitrate_estimator_->RemoveStream(config_.rtp.remote_ssrc);
pbosa2f30de2015-10-15 05:22:13 -0700137}
138
solenberg7add0582015-11-20 09:59:34 -0800139void AudioReceiveStream::Start() {
solenberg3ebbcb52017-01-31 03:58:40 -0800140 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800141 if (playing_) {
142 return;
143 }
144
145 int error = SetVoiceEnginePlayout(true);
aleloi84ef6152016-08-04 05:28:21 -0700146 if (error != 0) {
147 LOG(LS_ERROR) << "AudioReceiveStream::Start failed with error: " << error;
aleloi04c07222016-11-22 06:42:53 -0800148 return;
aleloi84ef6152016-08-04 05:28:21 -0700149 }
aleloi04c07222016-11-22 06:42:53 -0800150
151 if (!audio_state()->mixer()->AddSource(this)) {
152 LOG(LS_ERROR) << "Failed to add source to mixer.";
153 SetVoiceEnginePlayout(false);
154 return;
155 }
156
157 playing_ = true;
solenberg7add0582015-11-20 09:59:34 -0800158}
159
160void AudioReceiveStream::Stop() {
solenberg3ebbcb52017-01-31 03:58:40 -0800161 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800162 if (!playing_) {
163 return;
164 }
165 playing_ = false;
166
167 audio_state()->mixer()->RemoveSource(this);
168 SetVoiceEnginePlayout(false);
solenberg7add0582015-11-20 09:59:34 -0800169}
170
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200171webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800172 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200173 webrtc::AudioReceiveStream::Stats stats;
174 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg8b85de22015-11-16 09:48:04 -0800175
solenberg358057b2015-11-27 10:46:42 -0800176 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenbergbd9a77f2017-02-06 12:53:57 -0800177 // TODO(solenberg): Don't return here if we can't get the codec - return the
178 // stats we *can* get.
solenberg85a04962015-10-27 03:35:21 -0700179 webrtc::CodecInst codec_inst = {0};
solenbergbd9a77f2017-02-06 12:53:57 -0800180 if (!channel_proxy_->GetRecCodec(&codec_inst)) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200181 return stats;
182 }
183
solenberg85a04962015-10-27 03:35:21 -0700184 stats.bytes_rcvd = call_stats.bytesReceived;
185 stats.packets_rcvd = call_stats.packetsReceived;
186 stats.packets_lost = call_stats.cumulativeLost;
187 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800188 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700189 if (codec_inst.pltype != -1) {
190 stats.codec_name = codec_inst.plname;
hbos1acfbd22016-11-17 23:43:29 -0800191 stats.codec_payload_type = rtc::Optional<int>(codec_inst.pltype);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200192 }
solenberg85a04962015-10-27 03:35:21 -0700193 stats.ext_seqnum = call_stats.extendedMax;
194 if (codec_inst.plfreq / 1000 > 0) {
195 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200196 }
solenberg358057b2015-11-27 10:46:42 -0800197 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
198 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200199
solenberg8b85de22015-11-16 09:48:04 -0800200 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800201 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800202 stats.jitter_buffer_ms = ns.currentBufferSize;
203 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
204 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
205 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
206 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
207 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
208 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200209
solenberg358057b2015-11-27 10:46:42 -0800210 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800211 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
212 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
213 stats.decoding_normal = ds.decoded_normal;
214 stats.decoding_plc = ds.decoded_plc;
215 stats.decoding_cng = ds.decoded_cng;
216 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700217 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200218
219 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200220}
221
kwibergfffa42b2016-02-23 10:46:32 -0800222void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800223 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
kwibergfffa42b2016-02-23 10:46:32 -0800224 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100225}
226
solenberg217fb662016-06-17 08:30:54 -0700227void AudioReceiveStream::SetGain(float gain) {
solenberg3ebbcb52017-01-31 03:58:40 -0800228 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg217fb662016-06-17 08:30:54 -0700229 channel_proxy_->SetChannelOutputVolumeScaling(gain);
230}
231
solenberg3ebbcb52017-01-31 03:58:40 -0800232AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
233 int sample_rate_hz,
234 AudioFrame* audio_frame) {
235 return channel_proxy_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
236}
237
238int AudioReceiveStream::Ssrc() const {
239 return config_.rtp.remote_ssrc;
240}
241
242int AudioReceiveStream::PreferredSampleRate() const {
243 return channel_proxy_->NeededFrequency();
244}
245
246int AudioReceiveStream::id() const {
247 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
248 return config_.rtp.remote_ssrc;
249}
250
251rtc::Optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
252 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
253 Syncable::Info info;
254
255 RtpRtcp* rtp_rtcp = nullptr;
256 RtpReceiver* rtp_receiver = nullptr;
257 channel_proxy_->GetRtpRtcp(&rtp_rtcp, &rtp_receiver);
258 RTC_DCHECK(rtp_rtcp);
259 RTC_DCHECK(rtp_receiver);
260
261 if (!rtp_receiver->Timestamp(&info.latest_received_capture_timestamp)) {
262 return rtc::Optional<Syncable::Info>();
263 }
264 if (!rtp_receiver->LastReceivedTimeMs(&info.latest_receive_time_ms)) {
265 return rtc::Optional<Syncable::Info>();
266 }
267 if (rtp_rtcp->RemoteNTP(&info.capture_time_ntp_secs,
268 &info.capture_time_ntp_frac,
269 nullptr,
270 nullptr,
271 &info.capture_time_source_clock) != 0) {
272 return rtc::Optional<Syncable::Info>();
273 }
274
275 int jitter_buffer_delay_ms = 0;
276 int playout_buffer_delay_ms = 0;
277 channel_proxy_->GetDelayEstimate(&jitter_buffer_delay_ms,
278 &playout_buffer_delay_ms);
279 info.current_delay_ms = jitter_buffer_delay_ms + playout_buffer_delay_ms;
280 return rtc::Optional<Syncable::Info>(info);
281}
282
283uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
284 // Called on video capture thread.
285 return channel_proxy_->GetPlayoutTimestamp();
286}
287
288void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
289 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
290 return channel_proxy_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700291}
292
solenberg7602aab2016-11-14 11:30:07 -0800293void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800294 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
solenberg7602aab2016-11-14 11:30:07 -0800295 if (send_stream) {
296 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
297 std::unique_ptr<voe::ChannelProxy> send_channel_proxy =
298 voe_impl->GetChannelProxy(send_stream->config().voe_channel_id);
299 channel_proxy_->AssociateSendChannel(*send_channel_proxy.get());
300 } else {
301 channel_proxy_->DisassociateSendChannel();
302 }
303}
304
pbos1ba8d392016-05-01 20:18:34 -0700305void AudioReceiveStream::SignalNetworkState(NetworkState state) {
solenberg3ebbcb52017-01-31 03:58:40 -0800306 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
pbos1ba8d392016-05-01 20:18:34 -0700307}
308
309bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
310 // TODO(solenberg): Tests call this function on a network thread, libjingle
311 // calls on the worker thread. We should move towards always using a network
312 // thread. Then this check can be enabled.
313 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
314 return channel_proxy_->ReceivedRTCPPacket(packet, length);
315}
316
317bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
318 size_t length,
319 const PacketTime& packet_time) {
320 // TODO(solenberg): Tests call this function on a network thread, libjingle
321 // calls on the worker thread. We should move towards always using a network
322 // thread. Then this check can be enabled.
323 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
324 RTPHeader header;
325 if (!rtp_header_parser_->Parse(packet, length, &header)) {
326 return false;
327 }
328
pbos1ba8d392016-05-01 20:18:34 -0700329 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
330}
331
solenberg3ebbcb52017-01-31 03:58:40 -0800332const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
333 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
334 return config_;
aleloi04c07222016-11-22 06:42:53 -0800335}
336
solenberg7add0582015-11-20 09:59:34 -0800337VoiceEngine* AudioReceiveStream::voice_engine() const {
aleloi04c07222016-11-22 06:42:53 -0800338 auto* voice_engine = audio_state()->voice_engine();
solenberg7add0582015-11-20 09:59:34 -0800339 RTC_DCHECK(voice_engine);
340 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200341}
aleloi04c07222016-11-22 06:42:53 -0800342
solenberg3ebbcb52017-01-31 03:58:40 -0800343internal::AudioState* AudioReceiveStream::audio_state() const {
344 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
345 RTC_DCHECK(audio_state);
346 return audio_state;
347}
348
aleloi04c07222016-11-22 06:42:53 -0800349int AudioReceiveStream::SetVoiceEnginePlayout(bool playout) {
350 ScopedVoEInterface<VoEBase> base(voice_engine());
351 if (playout) {
352 return base->StartPlayout(config_.voe_channel_id);
353 } else {
354 return base->StopPlayout(config_.voe_channel_id);
355 }
356}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200357} // namespace internal
358} // namespace webrtc