blob: 35a65521ddd79a6644e72d608b08f262cd0d4cec [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
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
11#include "webrtc/audio/audio_send_stream.h"
12
13#include <string>
14
solenberg566ef242015-11-06 15:34:49 -080015#include "webrtc/audio/audio_state.h"
solenberg85a04962015-10-27 03:35:21 -070016#include "webrtc/audio/conversion.h"
solenberg566ef242015-11-06 15:34:49 -080017#include "webrtc/audio/scoped_voe_interface.h"
solenbergc7a8b082015-10-16 14:35:07 -070018#include "webrtc/base/checks.h"
19#include "webrtc/base/logging.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010020#include "webrtc/call/congestion_controller.h"
21#include "webrtc/modules/pacing/paced_sender.h"
22#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
solenberg13725082015-11-25 08:16:52 -080023#include "webrtc/voice_engine/channel_proxy.h"
solenberg85a04962015-10-27 03:35:21 -070024#include "webrtc/voice_engine/include/voe_audio_processing.h"
25#include "webrtc/voice_engine/include/voe_codec.h"
26#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
27#include "webrtc/voice_engine/include/voe_volume_control.h"
solenberg13725082015-11-25 08:16:52 -080028#include "webrtc/voice_engine/voice_engine_impl.h"
solenbergc7a8b082015-10-16 14:35:07 -070029
30namespace webrtc {
31std::string AudioSendStream::Config::Rtp::ToString() const {
32 std::stringstream ss;
33 ss << "{ssrc: " << ssrc;
34 ss << ", extensions: [";
35 for (size_t i = 0; i < extensions.size(); ++i) {
36 ss << extensions[i].ToString();
solenberg85a04962015-10-27 03:35:21 -070037 if (i != extensions.size() - 1) {
solenbergc7a8b082015-10-16 14:35:07 -070038 ss << ", ";
solenberg85a04962015-10-27 03:35:21 -070039 }
solenbergc7a8b082015-10-16 14:35:07 -070040 }
41 ss << ']';
solenberg3a941542015-11-16 07:34:50 -080042 ss << ", c_name: " << c_name;
solenbergc7a8b082015-10-16 14:35:07 -070043 ss << '}';
44 return ss.str();
45}
46
47std::string AudioSendStream::Config::ToString() const {
48 std::stringstream ss;
49 ss << "{rtp: " << rtp.ToString();
50 ss << ", voe_channel_id: " << voe_channel_id;
51 // TODO(solenberg): Encoder config.
52 ss << ", cng_payload_type: " << cng_payload_type;
53 ss << ", red_payload_type: " << red_payload_type;
54 ss << '}';
55 return ss.str();
56}
57
58namespace internal {
solenberg566ef242015-11-06 15:34:49 -080059AudioSendStream::AudioSendStream(
60 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010061 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
62 CongestionController* congestion_controller)
solenberg566ef242015-11-06 15:34:49 -080063 : config_(config), audio_state_(audio_state) {
solenbergc7a8b082015-10-16 14:35:07 -070064 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080065 RTC_DCHECK_NE(config_.voe_channel_id, -1);
66 RTC_DCHECK(audio_state_.get());
Stefan Holmerb86d4e42015-12-07 10:26:18 +010067 RTC_DCHECK(congestion_controller);
solenberg3a941542015-11-16 07:34:50 -080068
solenberg13725082015-11-25 08:16:52 -080069 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
70 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010071 channel_proxy_->SetCongestionControlObjects(
72 congestion_controller->pacer(),
73 congestion_controller->GetTransportFeedbackObserver(),
74 congestion_controller->packet_router());
solenberg13725082015-11-25 08:16:52 -080075 channel_proxy_->SetRTCPStatus(true);
76 channel_proxy_->SetLocalSSRC(config.rtp.ssrc);
77 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010078
solenberg3a941542015-11-16 07:34:50 -080079 for (const auto& extension : config.rtp.extensions) {
solenberg3a941542015-11-16 07:34:50 -080080 if (extension.name == RtpExtension::kAbsSendTime) {
solenberg358057b2015-11-27 10:46:42 -080081 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id);
solenberg3a941542015-11-16 07:34:50 -080082 } else if (extension.name == RtpExtension::kAudioLevel) {
solenberg358057b2015-11-27 10:46:42 -080083 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
Stefan Holmerb86d4e42015-12-07 10:26:18 +010084 } else if (extension.name == RtpExtension::kTransportSequenceNumber) {
85 channel_proxy_->EnableSendTransportSequenceNumber(extension.id);
solenberg3a941542015-11-16 07:34:50 -080086 } else {
87 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
88 }
89 }
solenbergc7a8b082015-10-16 14:35:07 -070090}
91
92AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 03:35:21 -070093 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -070094 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
Stefan Holmerb86d4e42015-12-07 10:26:18 +010095 channel_proxy_->SetCongestionControlObjects(nullptr, nullptr, nullptr);
solenbergc7a8b082015-10-16 14:35:07 -070096}
97
solenberg3a941542015-11-16 07:34:50 -080098void AudioSendStream::Start() {
99 RTC_DCHECK(thread_checker_.CalledOnValidThread());
100}
101
102void AudioSendStream::Stop() {
103 RTC_DCHECK(thread_checker_.CalledOnValidThread());
104}
105
106void AudioSendStream::SignalNetworkState(NetworkState state) {
107 RTC_DCHECK(thread_checker_.CalledOnValidThread());
108}
109
110bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
111 // TODO(solenberg): Tests call this function on a network thread, libjingle
112 // calls on the worker thread. We should move towards always using a network
113 // thread. Then this check can be enabled.
114 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
115 return false;
116}
117
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100118bool AudioSendStream::SendTelephoneEvent(int payload_type, uint8_t event,
119 uint32_t duration_ms) {
120 RTC_DCHECK(thread_checker_.CalledOnValidThread());
121 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type) &&
122 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
123}
124
solenbergc7a8b082015-10-16 14:35:07 -0700125webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -0700126 RTC_DCHECK(thread_checker_.CalledOnValidThread());
127 webrtc::AudioSendStream::Stats stats;
128 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 07:34:50 -0800129 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
130 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg3a941542015-11-16 07:34:50 -0800131 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 03:35:21 -0700132
solenberg358057b2015-11-27 10:46:42 -0800133 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700134 stats.bytes_sent = call_stats.bytesSent;
135 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800136 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
137 // returns 0 to indicate an error value.
138 if (call_stats.rttMs > 0) {
139 stats.rtt_ms = call_stats.rttMs;
140 }
141 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
142 // implementation.
143 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 03:35:21 -0700144
145 webrtc::CodecInst codec_inst = {0};
146 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
147 RTC_DCHECK_NE(codec_inst.pltype, -1);
148 stats.codec_name = codec_inst.plname;
149
150 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800151 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800152 // Lookup report for send ssrc only.
153 if (block.source_SSRC == stats.local_ssrc) {
154 stats.packets_lost = block.cumulative_num_packets_lost;
155 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
156 stats.ext_seqnum = block.extended_highest_sequence_number;
157 // Convert samples to milliseconds.
158 if (codec_inst.plfreq / 1000 > 0) {
159 stats.jitter_ms =
160 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 03:35:21 -0700161 }
solenberg8b85de22015-11-16 09:48:04 -0800162 break;
solenberg85a04962015-10-27 03:35:21 -0700163 }
164 }
165 }
166
solenberg85a04962015-10-27 03:35:21 -0700167 // Local speech level.
168 {
169 unsigned int level = 0;
solenberg358057b2015-11-27 10:46:42 -0800170 int error = volume->GetSpeechInputLevelFullRange(level);
solenberg8b85de22015-11-16 09:48:04 -0800171 RTC_DCHECK_EQ(0, error);
172 stats.audio_level = static_cast<int32_t>(level);
solenberg85a04962015-10-27 03:35:21 -0700173 }
174
solenberg85a04962015-10-27 03:35:21 -0700175 bool echo_metrics_on = false;
solenberg358057b2015-11-27 10:46:42 -0800176 int error = processing->GetEcMetricsStatus(echo_metrics_on);
solenberg8b85de22015-11-16 09:48:04 -0800177 RTC_DCHECK_EQ(0, error);
178 if (echo_metrics_on) {
solenberg85a04962015-10-27 03:35:21 -0700179 // These can also be negative, but in practice -1 is only used to signal
180 // insufficient data, since the resolution is limited to multiples of 4 ms.
181 int median = -1;
182 int std = -1;
183 float dummy = 0.0f;
solenberg8b85de22015-11-16 09:48:04 -0800184 error = processing->GetEcDelayMetrics(median, std, dummy);
185 RTC_DCHECK_EQ(0, error);
186 stats.echo_delay_median_ms = median;
187 stats.echo_delay_std_ms = std;
solenberg85a04962015-10-27 03:35:21 -0700188
189 // These can take on valid negative values, so use the lowest possible level
190 // as default rather than -1.
191 int erl = -100;
192 int erle = -100;
193 int dummy1 = 0;
194 int dummy2 = 0;
solenberg8b85de22015-11-16 09:48:04 -0800195 error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2);
196 RTC_DCHECK_EQ(0, error);
197 stats.echo_return_loss = erl;
198 stats.echo_return_loss_enhancement = erle;
solenberg85a04962015-10-27 03:35:21 -0700199 }
200
solenberg3a941542015-11-16 07:34:50 -0800201 internal::AudioState* audio_state =
202 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 15:34:49 -0800203 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 03:35:21 -0700204
205 return stats;
206}
207
208const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
209 RTC_DCHECK(thread_checker_.CalledOnValidThread());
210 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700211}
212
solenberg3a941542015-11-16 07:34:50 -0800213VoiceEngine* AudioSendStream::voice_engine() const {
214 internal::AudioState* audio_state =
215 static_cast<internal::AudioState*>(audio_state_.get());
216 VoiceEngine* voice_engine = audio_state->voice_engine();
217 RTC_DCHECK(voice_engine);
218 return voice_engine;
solenbergc7a8b082015-10-16 14:35:07 -0700219}
220} // namespace internal
221} // namespace webrtc