blob: 14dfd90bf890e61e2f4a8c4d4f64fb9badd2123d [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
Yves Gerey988cc082018-10-23 12:03:01 +020016#include "absl/memory/memory.h"
17#include "api/array_view.h"
18#include "api/audio_codecs/audio_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/call/audio_sink.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "api/rtp_parameters.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "audio/audio_send_stream.h"
22#include "audio/audio_state.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "audio/channel_receive.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "audio/conversion.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "call/rtp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "call/rtp_stream_receiver_controller_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
28#include "rtc_base/logging.h"
Tommifef05002018-02-27 13:51:08 +010029#include "rtc_base/strings/string_builder.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020031
32namespace webrtc {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010033
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020034std::string AudioReceiveStream::Config::Rtp::ToString() const {
Karl Wiberg881f1682018-03-08 15:03:23 +010035 char ss_buf[1024];
36 rtc::SimpleStringBuilder ss(ss_buf);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020037 ss << "{remote_ssrc: " << remote_ssrc;
solenberg85a04962015-10-27 03:35:21 -070038 ss << ", local_ssrc: " << local_ssrc;
solenberg8189b022016-06-14 12:13:00 -070039 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
40 ss << ", nack: " << nack.ToString();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020041 ss << ", extensions: [";
42 for (size_t i = 0; i < extensions.size(); ++i) {
43 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020044 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020045 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020046 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020047 }
48 ss << ']';
49 ss << '}';
50 return ss.str();
51}
52
53std::string AudioReceiveStream::Config::ToString() const {
Karl Wiberg881f1682018-03-08 15:03:23 +010054 char ss_buf[1024];
55 rtc::SimpleStringBuilder ss(ss_buf);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020056 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070057 ss << ", rtcp_send_transport: "
deadbeef922246a2017-02-26 04:18:12 -080058 << (rtcp_send_transport ? "(Transport)" : "null");
Anton Sukhanov4f08faa2019-05-21 11:12:57 -070059 ss << ", media_transport_config: " << media_transport_config.DebugString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020060 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070061 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020062 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020063 ss << '}';
64 return ss.str();
65}
66
67namespace internal {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010068namespace {
Niels Möller349ade32018-11-16 09:50:42 +010069std::unique_ptr<voe::ChannelReceiveInterface> CreateChannelReceive(
Sebastian Jansson977b3352019-03-04 17:43:34 +010070 Clock* clock,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010071 webrtc::AudioState* audio_state,
72 ProcessThread* module_process_thread,
Niels Möllerfa4e1852018-08-14 09:43:34 +020073 const webrtc::AudioReceiveStream::Config& config,
74 RtcEventLog* event_log) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010075 RTC_DCHECK(audio_state);
76 internal::AudioState* internal_audio_state =
77 static_cast<internal::AudioState*>(audio_state);
Niels Möller349ade32018-11-16 09:50:42 +010078 return voe::CreateChannelReceive(
Sebastian Jansson977b3352019-03-04 17:43:34 +010079 clock, module_process_thread, internal_audio_state->audio_device_module(),
Anton Sukhanov4f08faa2019-05-21 11:12:57 -070080 config.media_transport_config, config.rtcp_send_transport, event_log,
Erik Språng70efdde2019-08-21 13:36:20 +020081 config.rtp.local_ssrc, config.rtp.remote_ssrc,
82 config.jitter_buffer_max_packets, config.jitter_buffer_fast_accelerate,
83 config.jitter_buffer_min_delay_ms,
Jakob Ivarsson53eae872019-01-10 15:58:36 +010084 config.jitter_buffer_enable_rtx_handling, config.decoder_factory,
85 config.codec_pair_id, config.frame_decryptor, config.crypto_options);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010086}
87} // namespace
88
89AudioReceiveStream::AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +010090 Clock* clock,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010091 RtpStreamReceiverControllerInterface* receiver_controller,
92 PacketRouter* packet_router,
93 ProcessThread* module_process_thread,
94 const webrtc::AudioReceiveStream::Config& config,
95 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
96 webrtc::RtcEventLog* event_log)
Sebastian Jansson977b3352019-03-04 17:43:34 +010097 : AudioReceiveStream(clock,
98 receiver_controller,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010099 packet_router,
100 config,
101 audio_state,
102 event_log,
Sebastian Jansson977b3352019-03-04 17:43:34 +0100103 CreateChannelReceive(clock,
104 audio_state.get(),
Niels Möller349ade32018-11-16 09:50:42 +0100105 module_process_thread,
106 config,
107 event_log)) {}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100108
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200109AudioReceiveStream::AudioReceiveStream(
Sebastian Jansson977b3352019-03-04 17:43:34 +0100110 Clock* clock,
nisse0f15f922017-06-21 01:05:22 -0700111 RtpStreamReceiverControllerInterface* receiver_controller,
nisse0245da02016-11-30 03:35:20 -0800112 PacketRouter* packet_router,
solenberg566ef242015-11-06 15:34:49 -0800113 const webrtc::AudioReceiveStream::Config& config,
ivoc14d5dbe2016-07-04 07:06:55 -0700114 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100115 webrtc::RtcEventLog* event_log,
Niels Möller349ade32018-11-16 09:50:42 +0100116 std::unique_ptr<voe::ChannelReceiveInterface> channel_receive)
Chen Xing054e3bb2019-08-02 10:29:26 +0000117 : audio_state_(audio_state),
118 channel_receive_(std::move(channel_receive)),
119 source_tracker_(clock) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100120 RTC_LOG(LS_INFO) << "AudioReceiveStream: " << config.rtp.remote_ssrc;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100121 RTC_DCHECK(config.decoder_factory);
Niels Möllerae4237e2018-10-05 11:28:38 +0200122 RTC_DCHECK(config.rtcp_send_transport);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100123 RTC_DCHECK(audio_state_);
Niels Möller349ade32018-11-16 09:50:42 +0100124 RTC_DCHECK(channel_receive_);
solenberg7add0582015-11-20 09:59:34 -0800125
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200126 module_process_thread_checker_.Detach();
solenberg3ebbcb52017-01-31 03:58:40 -0800127
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700128 if (!config.media_transport_config.media_transport) {
Niels Möller7d76a312018-10-26 12:57:07 +0200129 RTC_DCHECK(receiver_controller);
130 RTC_DCHECK(packet_router);
131 // Configure bandwidth estimation.
Niels Möller349ade32018-11-16 09:50:42 +0100132 channel_receive_->RegisterReceiverCongestionControlObjects(packet_router);
nisse0f15f922017-06-21 01:05:22 -0700133
Niels Möller7d76a312018-10-26 12:57:07 +0200134 // Register with transport.
135 rtp_stream_receiver_ = receiver_controller->CreateReceiver(
Niels Möller349ade32018-11-16 09:50:42 +0100136 config.rtp.remote_ssrc, channel_receive_.get());
Niels Möller7d76a312018-10-26 12:57:07 +0200137 }
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100138 ConfigureStream(this, config, true);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200139}
140
pbosa2f30de2015-10-15 05:22:13 -0700141AudioReceiveStream::~AudioReceiveStream() {
solenberg3ebbcb52017-01-31 03:58:40 -0800142 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Jonas Olsson24ea8222018-01-25 10:14:29 +0100143 RTC_LOG(LS_INFO) << "~AudioReceiveStream: " << config_.rtp.remote_ssrc;
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100144 Stop();
Niels Möller349ade32018-11-16 09:50:42 +0100145 channel_receive_->SetAssociatedSendChannel(nullptr);
Anton Sukhanov4f08faa2019-05-21 11:12:57 -0700146 if (!config_.media_transport_config.media_transport) {
Niels Möller349ade32018-11-16 09:50:42 +0100147 channel_receive_->ResetReceiverCongestionControlObjects();
Niels Möller7d76a312018-10-26 12:57:07 +0200148 }
pbosa2f30de2015-10-15 05:22:13 -0700149}
150
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100151void AudioReceiveStream::Reconfigure(
152 const webrtc::AudioReceiveStream::Config& config) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200153 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100154 ConfigureStream(this, config, false);
155}
156
solenberg7add0582015-11-20 09:59:34 -0800157void AudioReceiveStream::Start() {
solenberg3ebbcb52017-01-31 03:58:40 -0800158 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800159 if (playing_) {
160 return;
161 }
Niels Möller349ade32018-11-16 09:50:42 +0100162 channel_receive_->StartPlayout();
aleloi04c07222016-11-22 06:42:53 -0800163 playing_ = true;
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100164 audio_state()->AddReceivingStream(this);
solenberg7add0582015-11-20 09:59:34 -0800165}
166
167void AudioReceiveStream::Stop() {
solenberg3ebbcb52017-01-31 03:58:40 -0800168 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
aleloi04c07222016-11-22 06:42:53 -0800169 if (!playing_) {
170 return;
171 }
Niels Möller349ade32018-11-16 09:50:42 +0100172 channel_receive_->StopPlayout();
aleloi04c07222016-11-22 06:42:53 -0800173 playing_ = false;
Fredrik Solenbergd5247512017-12-18 22:41:03 +0100174 audio_state()->RemoveReceivingStream(this);
solenberg7add0582015-11-20 09:59:34 -0800175}
176
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200177webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800178 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200179 webrtc::AudioReceiveStream::Stats stats;
180 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg8b85de22015-11-16 09:48:04 -0800181
Niels Möller530ead42018-10-04 14:28:39 +0200182 webrtc::CallReceiveStatistics call_stats =
Niels Möller349ade32018-11-16 09:50:42 +0100183 channel_receive_->GetRTCPStatistics();
solenbergbd9a77f2017-02-06 12:53:57 -0800184 // TODO(solenberg): Don't return here if we can't get the codec - return the
185 // stats we *can* get.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100186 auto receive_codec = channel_receive_->GetReceiveCodec();
187 if (!receive_codec) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200188 return stats;
189 }
190
Mirko Bonadeief0627f2019-10-15 08:54:49 +0000191 stats.bytes_rcvd = call_stats.bytesReceived;
solenberg85a04962015-10-27 03:35:21 -0700192 stats.packets_rcvd = call_stats.packetsReceived;
193 stats.packets_lost = call_stats.cumulativeLost;
solenberg8b85de22015-11-16 09:48:04 -0800194 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
Henrik Boström01738c62019-04-15 17:32:00 +0200195 stats.last_packet_received_timestamp_ms =
196 call_stats.last_packet_received_timestamp_ms;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100197 stats.codec_name = receive_codec->second.name;
198 stats.codec_payload_type = receive_codec->first;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100199 int clockrate_khz = receive_codec->second.clockrate_hz / 1000;
200 if (clockrate_khz > 0) {
201 stats.jitter_ms = call_stats.jitterSamples / clockrate_khz;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200202 }
Niels Möller349ade32018-11-16 09:50:42 +0100203 stats.delay_estimate_ms = channel_receive_->GetDelayEstimate();
204 stats.audio_level = channel_receive_->GetSpeechOutputLevelFullRange();
205 stats.total_output_energy = channel_receive_->GetTotalOutputEnergy();
206 stats.total_output_duration = channel_receive_->GetTotalOutputDuration();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200207
solenberg8b85de22015-11-16 09:48:04 -0800208 // Get jitter buffer and total delay (alg + jitter + playout) stats.
Niels Möller349ade32018-11-16 09:50:42 +0100209 auto ns = channel_receive_->GetNetworkStatistics();
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200210 stats.fec_packets_received = ns.fecPacketsReceived;
211 stats.fec_packets_discarded = ns.fecPacketsDiscarded;
solenberg8b85de22015-11-16 09:48:04 -0800212 stats.jitter_buffer_ms = ns.currentBufferSize;
213 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
Steve Anton2dbc69f2017-08-24 17:15:13 -0700214 stats.total_samples_received = ns.totalSamplesReceived;
215 stats.concealed_samples = ns.concealedSamples;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200216 stats.silent_concealed_samples = ns.silentConcealedSamples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +0200217 stats.concealment_events = ns.concealmentEvents;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +0200218 stats.jitter_buffer_delay_seconds =
219 static_cast<double>(ns.jitterBufferDelayMs) /
220 static_cast<double>(rtc::kNumMillisecsPerSec);
Chen Xing0acffb52019-01-15 15:46:29 +0100221 stats.jitter_buffer_emitted_count = ns.jitterBufferEmittedCount;
Ivo Creusen8d8ffdb2019-04-30 09:45:21 +0200222 stats.inserted_samples_for_deceleration = ns.insertedSamplesForDeceleration;
223 stats.removed_samples_for_acceleration = ns.removedSamplesForAcceleration;
solenberg8b85de22015-11-16 09:48:04 -0800224 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
225 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
226 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200227 stats.secondary_discarded_rate = Q14ToFloat(ns.currentSecondaryDiscardedRate);
solenberg8b85de22015-11-16 09:48:04 -0800228 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
229 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Ruslan Burakov8af88962018-11-22 17:21:10 +0100230 stats.jitter_buffer_flushes = ns.packetBufferFlushes;
Jakob Ivarsson352ce5c2018-11-27 12:52:16 +0100231 stats.delayed_packet_outage_samples = ns.delayedPacketOutageSamples;
Jakob Ivarsson232b3fd2019-03-06 09:18:40 +0100232 stats.relative_packet_arrival_delay_seconds =
233 static_cast<double>(ns.relativePacketArrivalDelayMs) /
234 static_cast<double>(rtc::kNumMillisecsPerSec);
Henrik Lundin44125fa2019-04-29 17:00:46 +0200235 stats.interruption_count = ns.interruptionCount;
236 stats.total_interruption_duration_ms = ns.totalInterruptionDurationMs;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200237
Niels Möller349ade32018-11-16 09:50:42 +0100238 auto ds = channel_receive_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800239 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
240 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
241 stats.decoding_normal = ds.decoded_normal;
Alex Narest5b5d97c2019-08-07 18:15:08 +0200242 stats.decoding_plc = ds.decoded_neteq_plc;
243 stats.decoding_codec_plc = ds.decoded_codec_plc;
solenberg8b85de22015-11-16 09:48:04 -0800244 stats.decoding_cng = ds.decoded_cng;
245 stats.decoding_plc_cng = ds.decoded_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -0700246 stats.decoding_muted_output = ds.decoded_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200247
248 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200249}
250
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100251void AudioReceiveStream::SetSink(AudioSinkInterface* sink) {
solenberg3ebbcb52017-01-31 03:58:40 -0800252 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller349ade32018-11-16 09:50:42 +0100253 channel_receive_->SetSink(sink);
Tommif888bb52015-12-12 01:37:01 +0100254}
255
solenberg217fb662016-06-17 08:30:54 -0700256void AudioReceiveStream::SetGain(float gain) {
solenberg3ebbcb52017-01-31 03:58:40 -0800257 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller349ade32018-11-16 09:50:42 +0100258 channel_receive_->SetChannelOutputVolumeScaling(gain);
solenberg217fb662016-06-17 08:30:54 -0700259}
260
Ruslan Burakov3b50f9f2019-02-06 09:45:56 +0100261bool AudioReceiveStream::SetBaseMinimumPlayoutDelayMs(int delay_ms) {
262 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
263 return channel_receive_->SetBaseMinimumPlayoutDelayMs(delay_ms);
264}
265
266int AudioReceiveStream::GetBaseMinimumPlayoutDelayMs() const {
267 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
268 return channel_receive_->GetBaseMinimumPlayoutDelayMs();
269}
270
hbos8d609f62017-04-10 07:39:05 -0700271std::vector<RtpSource> AudioReceiveStream::GetSources() const {
272 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Chen Xing054e3bb2019-08-02 10:29:26 +0000273 return source_tracker_.GetSources();
hbos8d609f62017-04-10 07:39:05 -0700274}
275
solenberg3ebbcb52017-01-31 03:58:40 -0800276AudioMixer::Source::AudioFrameInfo AudioReceiveStream::GetAudioFrameWithInfo(
277 int sample_rate_hz,
278 AudioFrame* audio_frame) {
Chen Xing054e3bb2019-08-02 10:29:26 +0000279 AudioMixer::Source::AudioFrameInfo audio_frame_info =
280 channel_receive_->GetAudioFrameWithInfo(sample_rate_hz, audio_frame);
281 if (audio_frame_info != AudioMixer::Source::AudioFrameInfo::kError) {
282 source_tracker_.OnFrameDelivered(audio_frame->packet_infos_);
283 }
284 return audio_frame_info;
solenberg3ebbcb52017-01-31 03:58:40 -0800285}
286
287int AudioReceiveStream::Ssrc() const {
288 return config_.rtp.remote_ssrc;
289}
290
291int AudioReceiveStream::PreferredSampleRate() const {
Niels Möller349ade32018-11-16 09:50:42 +0100292 return channel_receive_->PreferredSampleRate();
solenberg3ebbcb52017-01-31 03:58:40 -0800293}
294
295int AudioReceiveStream::id() const {
296 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
297 return config_.rtp.remote_ssrc;
298}
299
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200300absl::optional<Syncable::Info> AudioReceiveStream::GetInfo() const {
solenberg3ebbcb52017-01-31 03:58:40 -0800301 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
Niels Möller349ade32018-11-16 09:50:42 +0100302 absl::optional<Syncable::Info> info = channel_receive_->GetSyncInfo();
solenberg3ebbcb52017-01-31 03:58:40 -0800303
Niels Möller848d6d32018-08-08 10:49:16 +0200304 if (!info)
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200305 return absl::nullopt;
solenberg3ebbcb52017-01-31 03:58:40 -0800306
Niels Möller349ade32018-11-16 09:50:42 +0100307 info->current_delay_ms = channel_receive_->GetDelayEstimate();
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100308 return info;
solenberg3ebbcb52017-01-31 03:58:40 -0800309}
310
311uint32_t AudioReceiveStream::GetPlayoutTimestamp() const {
312 // Called on video capture thread.
Niels Möller349ade32018-11-16 09:50:42 +0100313 return channel_receive_->GetPlayoutTimestamp();
solenberg3ebbcb52017-01-31 03:58:40 -0800314}
315
316void AudioReceiveStream::SetMinimumPlayoutDelay(int delay_ms) {
317 RTC_DCHECK_RUN_ON(&module_process_thread_checker_);
Niels Möller349ade32018-11-16 09:50:42 +0100318 return channel_receive_->SetMinimumPlayoutDelay(delay_ms);
pbosa2f30de2015-10-15 05:22:13 -0700319}
320
solenberg7602aab2016-11-14 11:30:07 -0800321void AudioReceiveStream::AssociateSendStream(AudioSendStream* send_stream) {
solenberg3ebbcb52017-01-31 03:58:40 -0800322 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Niels Möller349ade32018-11-16 09:50:42 +0100323 channel_receive_->SetAssociatedSendChannel(
324 send_stream ? send_stream->GetChannel() : nullptr);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100325 associated_send_stream_ = send_stream;
solenberg7602aab2016-11-14 11:30:07 -0800326}
327
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100328void AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos1ba8d392016-05-01 20:18:34 -0700329 // TODO(solenberg): Tests call this function on a network thread, libjingle
330 // calls on the worker thread. We should move towards always using a network
331 // thread. Then this check can be enabled.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200332 // RTC_DCHECK(!thread_checker_.IsCurrent());
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100333 channel_receive_->ReceivedRTCPPacket(packet, length);
pbos1ba8d392016-05-01 20:18:34 -0700334}
335
nisse657bab22017-02-21 06:28:10 -0800336void AudioReceiveStream::OnRtpPacket(const RtpPacketReceived& packet) {
pbos1ba8d392016-05-01 20:18:34 -0700337 // TODO(solenberg): Tests call this function on a network thread, libjingle
338 // calls on the worker thread. We should move towards always using a network
339 // thread. Then this check can be enabled.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200340 // RTC_DCHECK(!thread_checker_.IsCurrent());
Niels Möller349ade32018-11-16 09:50:42 +0100341 channel_receive_->OnRtpPacket(packet);
pbos1ba8d392016-05-01 20:18:34 -0700342}
343
solenberg3ebbcb52017-01-31 03:58:40 -0800344const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
345 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
346 return config_;
aleloi04c07222016-11-22 06:42:53 -0800347}
348
Yves Gerey665174f2018-06-19 15:03:05 +0200349const AudioSendStream* AudioReceiveStream::GetAssociatedSendStreamForTesting()
350 const {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100351 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
352 return associated_send_stream_;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200353}
aleloi04c07222016-11-22 06:42:53 -0800354
solenberg3ebbcb52017-01-31 03:58:40 -0800355internal::AudioState* AudioReceiveStream::audio_state() const {
356 auto* audio_state = static_cast<internal::AudioState*>(audio_state_.get());
357 RTC_DCHECK(audio_state);
358 return audio_state;
359}
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100360
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100361void AudioReceiveStream::ConfigureStream(AudioReceiveStream* stream,
362 const Config& new_config,
363 bool first_time) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100364 RTC_LOG(LS_INFO) << "AudioReceiveStream::ConfigureStream: "
365 << new_config.ToString();
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100366 RTC_DCHECK(stream);
Niels Möller349ade32018-11-16 09:50:42 +0100367 const auto& channel_receive = stream->channel_receive_;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100368 const auto& old_config = stream->config_;
369
370 // Configuration parameters which cannot be changed.
371 RTC_DCHECK(first_time ||
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100372 old_config.rtp.remote_ssrc == new_config.rtp.remote_ssrc);
373 RTC_DCHECK(first_time ||
374 old_config.rtcp_send_transport == new_config.rtcp_send_transport);
375 // Decoder factory cannot be changed because it is configured at
376 // voe::Channel construction time.
377 RTC_DCHECK(first_time ||
378 old_config.decoder_factory == new_config.decoder_factory);
379
Niels Möller30b48392018-08-15 15:05:26 +0200380 if (!first_time) {
Erik Språng70efdde2019-08-21 13:36:20 +0200381 // SSRC can't be changed mid-stream.
382 RTC_DCHECK_EQ(old_config.rtp.local_ssrc, new_config.rtp.local_ssrc);
Niels Möllerf7824922018-05-25 13:41:10 +0200383 RTC_DCHECK_EQ(old_config.rtp.remote_ssrc, new_config.rtp.remote_ssrc);
384 }
385
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100386 // TODO(solenberg): Config NACK history window (which is a packet count),
387 // using the actual packet size for the configured codec.
388 if (first_time || old_config.rtp.nack.rtp_history_ms !=
389 new_config.rtp.nack.rtp_history_ms) {
Niels Möller349ade32018-11-16 09:50:42 +0100390 channel_receive->SetNACKStatus(new_config.rtp.nack.rtp_history_ms != 0,
391 new_config.rtp.nack.rtp_history_ms / 20);
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100392 }
393 if (first_time || old_config.decoder_map != new_config.decoder_map) {
Niels Möller349ade32018-11-16 09:50:42 +0100394 channel_receive->SetReceiveCodecs(new_config.decoder_map);
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100395 }
396
Fredrik Solenberg3b903d02018-01-10 15:17:10 +0100397 stream->config_ = new_config;
398}
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200399} // namespace internal
400} // namespace webrtc