blob: 15e4f6107a62bcf0c0b66e9c8c2b31a3128251cb [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
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010016#include "webrtc/audio_sink.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/audio/audio_state.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020018#include "webrtc/audio/conversion.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020019#include "webrtc/base/checks.h"
pbosa2f30de2015-10-15 05:22:13 -070020#include "webrtc/base/logging.h"
Niels Möllerd28db7f2016-05-10 16:31:47 +020021#include "webrtc/base/timeutils.h"
Stefan Holmer80e12072016-02-23 13:30:42 +010022#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020023#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
solenberg13725082015-11-25 08:16:52 -080024#include "webrtc/voice_engine/channel_proxy.h"
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020025#include "webrtc/voice_engine/include/voe_base.h"
26#include "webrtc/voice_engine/include/voe_codec.h"
27#include "webrtc/voice_engine/include/voe_neteq_stats.h"
28#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
29#include "webrtc/voice_engine/include/voe_video_sync.h"
30#include "webrtc/voice_engine/include/voe_volume_control.h"
solenberg13725082015-11-25 08:16:52 -080031#include "webrtc/voice_engine/voice_engine_impl.h"
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020032
33namespace webrtc {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010034namespace {
35
36bool UseSendSideBwe(const webrtc::AudioReceiveStream::Config& config) {
37 if (!config.rtp.transport_cc) {
38 return false;
39 }
40 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -070041 if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
Stefan Holmer3842c5c2016-01-12 13:55:00 +010042 return true;
43 }
44 }
45 return false;
46}
47} // namespace
48
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020049std::string AudioReceiveStream::Config::Rtp::ToString() const {
50 std::stringstream ss;
51 ss << "{remote_ssrc: " << remote_ssrc;
solenberg85a04962015-10-27 03:35:21 -070052 ss << ", local_ssrc: " << local_ssrc;
solenberg8189b022016-06-14 12:13:00 -070053 ss << ", transport_cc: " << (transport_cc ? "on" : "off");
54 ss << ", nack: " << nack.ToString();
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020055 ss << ", extensions: [";
56 for (size_t i = 0; i < extensions.size(); ++i) {
57 ss << extensions[i].ToString();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020058 if (i != extensions.size() - 1) {
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020059 ss << ", ";
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020060 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020061 }
62 ss << ']';
63 ss << '}';
64 return ss.str();
65}
66
67std::string AudioReceiveStream::Config::ToString() const {
68 std::stringstream ss;
69 ss << "{rtp: " << rtp.ToString();
solenberg85a04962015-10-27 03:35:21 -070070 ss << ", rtcp_send_transport: "
71 << (rtcp_send_transport ? "(Transport)" : "nullptr");
pbos8fc7fa72015-07-15 08:02:58 -070072 ss << ", voe_channel_id: " << voe_channel_id;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020073 if (!sync_group.empty()) {
pbos8fc7fa72015-07-15 08:02:58 -070074 ss << ", sync_group: " << sync_group;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +020075 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020076 ss << '}';
77 return ss.str();
78}
79
80namespace internal {
81AudioReceiveStream::AudioReceiveStream(
Stefan Holmer3842c5c2016-01-12 13:55:00 +010082 CongestionController* congestion_controller,
solenberg566ef242015-11-06 15:34:49 -080083 const webrtc::AudioReceiveStream::Config& config,
84 const rtc::scoped_refptr<webrtc::AudioState>& audio_state)
Stefan Holmer3842c5c2016-01-12 13:55:00 +010085 : config_(config),
solenberg566ef242015-11-06 15:34:49 -080086 audio_state_(audio_state),
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +020087 rtp_header_parser_(RtpHeaderParser::Create()) {
pbosa2f30de2015-10-15 05:22:13 -070088 LOG(LS_INFO) << "AudioReceiveStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080089 RTC_DCHECK_NE(config_.voe_channel_id, -1);
solenberg566ef242015-11-06 15:34:49 -080090 RTC_DCHECK(audio_state_.get());
Stefan Holmer3842c5c2016-01-12 13:55:00 +010091 RTC_DCHECK(congestion_controller);
solenberg566ef242015-11-06 15:34:49 -080092 RTC_DCHECK(rtp_header_parser_);
solenberg7add0582015-11-20 09:59:34 -080093
solenberg13725082015-11-25 08:16:52 -080094 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
kwibergfffa42b2016-02-23 10:46:32 -080095 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
solenberg13725082015-11-25 08:16:52 -080096 channel_proxy_->SetLocalSSRC(config.rtp.local_ssrc);
solenberg8189b022016-06-14 12:13:00 -070097 // TODO(solenberg): Config NACK history window (which is a packet count),
98 // using the actual packet size for the configured codec.
99 channel_proxy_->SetNACKStatus(config_.rtp.nack.rtp_history_ms != 0,
100 config_.rtp.nack.rtp_history_ms / 20);
mflodman3d7db262016-04-29 00:57:13 -0700101
ossu29b1a8d2016-06-13 07:34:51 -0700102 // TODO(ossu): This is where we'd like to set the decoder factory to
103 // use. However, since it needs to be included when constructing Channel, we
104 // cannot do that until we're able to move Channel ownership into the
105 // Audio{Send,Receive}Streams. The best we can do is check that we're not
106 // trying to use two different factories using the different interfaces.
107 RTC_CHECK(config.decoder_factory);
108 RTC_CHECK_EQ(config.decoder_factory,
109 channel_proxy_->GetAudioDecoderFactory());
110
mflodman3d7db262016-04-29 00:57:13 -0700111 channel_proxy_->RegisterExternalTransport(config.rtcp_send_transport);
112
solenberg7add0582015-11-20 09:59:34 -0800113 for (const auto& extension : config.rtp.extensions) {
isheriff6f8d6862016-05-26 11:24:55 -0700114 if (extension.uri == RtpExtension::kAudioLevelUri) {
solenberg358057b2015-11-27 10:46:42 -0800115 channel_proxy_->SetReceiveAudioLevelIndicationStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800116 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
117 kRtpExtensionAudioLevel, extension.id);
118 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700119 } else if (extension.uri == RtpExtension::kAbsSendTimeUri) {
solenberg358057b2015-11-27 10:46:42 -0800120 channel_proxy_->SetReceiveAbsoluteSenderTimeStatus(true, extension.id);
solenberg7add0582015-11-20 09:59:34 -0800121 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
122 kRtpExtensionAbsoluteSendTime, extension.id);
123 RTC_DCHECK(registered);
isheriff6f8d6862016-05-26 11:24:55 -0700124 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
stefan3313ec92016-01-21 06:32:43 -0800125 channel_proxy_->EnableReceiveTransportSequenceNumber(extension.id);
solenberg7add0582015-11-20 09:59:34 -0800126 bool registered = rtp_header_parser_->RegisterRtpHeaderExtension(
127 kRtpExtensionTransportSequenceNumber, extension.id);
128 RTC_DCHECK(registered);
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200129 } else {
130 RTC_NOTREACHED() << "Unsupported RTP extension.";
131 }
132 }
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100133 // Configure bandwidth estimation.
stefanbba9dec2016-02-01 04:39:55 -0800134 channel_proxy_->RegisterReceiverCongestionControlObjects(
135 congestion_controller->packet_router());
stefanba4c0e42016-02-04 04:12:24 -0800136 if (UseSendSideBwe(config)) {
137 remote_bitrate_estimator_ =
138 congestion_controller->GetRemoteBitrateEstimator(true);
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100139 }
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200140}
141
pbosa2f30de2015-10-15 05:22:13 -0700142AudioReceiveStream::~AudioReceiveStream() {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200143 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700144 LOG(LS_INFO) << "~AudioReceiveStream: " << config_.ToString();
mflodman3d7db262016-04-29 00:57:13 -0700145 channel_proxy_->DeRegisterExternalTransport();
stefanbba9dec2016-02-01 04:39:55 -0800146 channel_proxy_->ResetCongestionControlObjects();
Stefan Holmer3842c5c2016-01-12 13:55:00 +0100147 if (remote_bitrate_estimator_) {
148 remote_bitrate_estimator_->RemoveStream(config_.rtp.remote_ssrc);
149 }
pbosa2f30de2015-10-15 05:22:13 -0700150}
151
solenberg7add0582015-11-20 09:59:34 -0800152void AudioReceiveStream::Start() {
153 RTC_DCHECK(thread_checker_.CalledOnValidThread());
154}
155
156void AudioReceiveStream::Stop() {
157 RTC_DCHECK(thread_checker_.CalledOnValidThread());
158}
159
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200160webrtc::AudioReceiveStream::Stats AudioReceiveStream::GetStats() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200161 RTC_DCHECK(thread_checker_.CalledOnValidThread());
162 webrtc::AudioReceiveStream::Stats stats;
163 stats.remote_ssrc = config_.rtp.remote_ssrc;
solenberg7add0582015-11-20 09:59:34 -0800164 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg8b85de22015-11-16 09:48:04 -0800165
solenberg358057b2015-11-27 10:46:42 -0800166 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700167 webrtc::CodecInst codec_inst = {0};
solenberg8b85de22015-11-16 09:48:04 -0800168 if (codec->GetRecCodec(config_.voe_channel_id, codec_inst) == -1) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200169 return stats;
170 }
171
solenberg85a04962015-10-27 03:35:21 -0700172 stats.bytes_rcvd = call_stats.bytesReceived;
173 stats.packets_rcvd = call_stats.packetsReceived;
174 stats.packets_lost = call_stats.cumulativeLost;
175 stats.fraction_lost = Q8ToFloat(call_stats.fractionLost);
solenberg8b85de22015-11-16 09:48:04 -0800176 stats.capture_start_ntp_time_ms = call_stats.capture_start_ntp_time_ms_;
solenberg85a04962015-10-27 03:35:21 -0700177 if (codec_inst.pltype != -1) {
178 stats.codec_name = codec_inst.plname;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200179 }
solenberg85a04962015-10-27 03:35:21 -0700180 stats.ext_seqnum = call_stats.extendedMax;
181 if (codec_inst.plfreq / 1000 > 0) {
182 stats.jitter_ms = call_stats.jitterSamples / (codec_inst.plfreq / 1000);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200183 }
solenberg358057b2015-11-27 10:46:42 -0800184 stats.delay_estimate_ms = channel_proxy_->GetDelayEstimate();
185 stats.audio_level = channel_proxy_->GetSpeechOutputLevelFullRange();
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200186
solenberg8b85de22015-11-16 09:48:04 -0800187 // Get jitter buffer and total delay (alg + jitter + playout) stats.
solenberg358057b2015-11-27 10:46:42 -0800188 auto ns = channel_proxy_->GetNetworkStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800189 stats.jitter_buffer_ms = ns.currentBufferSize;
190 stats.jitter_buffer_preferred_ms = ns.preferredBufferSize;
191 stats.expand_rate = Q14ToFloat(ns.currentExpandRate);
192 stats.speech_expand_rate = Q14ToFloat(ns.currentSpeechExpandRate);
193 stats.secondary_decoded_rate = Q14ToFloat(ns.currentSecondaryDecodedRate);
194 stats.accelerate_rate = Q14ToFloat(ns.currentAccelerateRate);
195 stats.preemptive_expand_rate = Q14ToFloat(ns.currentPreemptiveRate);
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200196
solenberg358057b2015-11-27 10:46:42 -0800197 auto ds = channel_proxy_->GetDecodingCallStatistics();
solenberg8b85de22015-11-16 09:48:04 -0800198 stats.decoding_calls_to_silence_generator = ds.calls_to_silence_generator;
199 stats.decoding_calls_to_neteq = ds.calls_to_neteq;
200 stats.decoding_normal = ds.decoded_normal;
201 stats.decoding_plc = ds.decoded_plc;
202 stats.decoding_cng = ds.decoded_cng;
203 stats.decoding_plc_cng = ds.decoded_plc_cng;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200204
205 return stats;
Fredrik Solenberg04f49312015-06-08 13:04:56 +0200206}
207
kwibergfffa42b2016-02-23 10:46:32 -0800208void AudioReceiveStream::SetSink(std::unique_ptr<AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +0100209 RTC_DCHECK(thread_checker_.CalledOnValidThread());
kwibergfffa42b2016-02-23 10:46:32 -0800210 channel_proxy_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +0100211}
212
pbosa2f30de2015-10-15 05:22:13 -0700213const webrtc::AudioReceiveStream::Config& AudioReceiveStream::config() const {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +0200214 RTC_DCHECK(thread_checker_.CalledOnValidThread());
pbosa2f30de2015-10-15 05:22:13 -0700215 return config_;
216}
217
pbos1ba8d392016-05-01 20:18:34 -0700218void AudioReceiveStream::SignalNetworkState(NetworkState state) {
219 RTC_DCHECK(thread_checker_.CalledOnValidThread());
220}
221
222bool AudioReceiveStream::DeliverRtcp(const uint8_t* packet, size_t length) {
223 // TODO(solenberg): Tests call this function on a network thread, libjingle
224 // calls on the worker thread. We should move towards always using a network
225 // thread. Then this check can be enabled.
226 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
227 return channel_proxy_->ReceivedRTCPPacket(packet, length);
228}
229
230bool AudioReceiveStream::DeliverRtp(const uint8_t* packet,
231 size_t length,
232 const PacketTime& packet_time) {
233 // TODO(solenberg): Tests call this function on a network thread, libjingle
234 // calls on the worker thread. We should move towards always using a network
235 // thread. Then this check can be enabled.
236 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
237 RTPHeader header;
238 if (!rtp_header_parser_->Parse(packet, length, &header)) {
239 return false;
240 }
241
242 // Only forward if the parsed header has one of the headers necessary for
243 // bandwidth estimation. RTP timestamps has different rates for audio and
244 // video and shouldn't be mixed.
245 if (remote_bitrate_estimator_ &&
246 header.extension.hasTransportSequenceNumber) {
Niels Möllerd28db7f2016-05-10 16:31:47 +0200247 int64_t arrival_time_ms = rtc::TimeMillis();
pbos1ba8d392016-05-01 20:18:34 -0700248 if (packet_time.timestamp >= 0)
249 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
250 size_t payload_size = length - header.headerLength;
251 remote_bitrate_estimator_->IncomingPacket(arrival_time_ms, payload_size,
252 header, false);
253 }
254
255 return channel_proxy_->ReceivedRTPPacket(packet, length, packet_time);
256}
257
solenberg7add0582015-11-20 09:59:34 -0800258VoiceEngine* AudioReceiveStream::voice_engine() const {
259 internal::AudioState* audio_state =
260 static_cast<internal::AudioState*>(audio_state_.get());
261 VoiceEngine* voice_engine = audio_state->voice_engine();
262 RTC_DCHECK(voice_engine);
263 return voice_engine;
Fredrik Solenberg23fba1f2015-04-29 15:24:01 +0200264}
265} // namespace internal
266} // namespace webrtc