blob: 160a818323b139381c06ca16164b3d8b667e8491 [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 Holmer80e12072016-02-23 13:30:42 +010020#include "webrtc/modules/congestion_controller/include/congestion_controller.h"
Stefan Holmerb86d4e42015-12-07 10:26:18 +010021#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());
kwibergfffa42b2016-02-23 10:46:32 -080070 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
stefanbba9dec2016-02-01 04:39:55 -080071 channel_proxy_->RegisterSenderCongestionControlObjects(
Stefan Holmerb86d4e42015-12-07 10:26:18 +010072 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();
stefanbba9dec2016-02-01 04:39:55 -080095 channel_proxy_->ResetCongestionControlObjects();
solenbergc7a8b082015-10-16 14:35:07 -070096}
97
solenberg3a941542015-11-16 07:34:50 -080098void AudioSendStream::Start() {
99 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800100 ScopedVoEInterface<VoEBase> base(voice_engine());
101 int error = base->StartSend(config_.voe_channel_id);
102 if (error != 0) {
103 LOG(LS_ERROR) << "AudioSendStream::Start failed with error: " << error;
104 }
solenberg3a941542015-11-16 07:34:50 -0800105}
106
107void AudioSendStream::Stop() {
108 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800109 ScopedVoEInterface<VoEBase> base(voice_engine());
110 int error = base->StopSend(config_.voe_channel_id);
111 if (error != 0) {
112 LOG(LS_ERROR) << "AudioSendStream::Stop failed with error: " << error;
113 }
solenberg3a941542015-11-16 07:34:50 -0800114}
115
116void AudioSendStream::SignalNetworkState(NetworkState state) {
117 RTC_DCHECK(thread_checker_.CalledOnValidThread());
118}
119
120bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
121 // TODO(solenberg): Tests call this function on a network thread, libjingle
122 // calls on the worker thread. We should move towards always using a network
123 // thread. Then this check can be enabled.
124 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
125 return false;
126}
127
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100128bool AudioSendStream::SendTelephoneEvent(int payload_type, uint8_t event,
129 uint32_t duration_ms) {
130 RTC_DCHECK(thread_checker_.CalledOnValidThread());
131 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type) &&
132 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
133}
134
solenbergc7a8b082015-10-16 14:35:07 -0700135webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -0700136 RTC_DCHECK(thread_checker_.CalledOnValidThread());
137 webrtc::AudioSendStream::Stats stats;
138 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 07:34:50 -0800139 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
140 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg3a941542015-11-16 07:34:50 -0800141 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 03:35:21 -0700142
solenberg358057b2015-11-27 10:46:42 -0800143 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700144 stats.bytes_sent = call_stats.bytesSent;
145 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800146 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
147 // returns 0 to indicate an error value.
148 if (call_stats.rttMs > 0) {
149 stats.rtt_ms = call_stats.rttMs;
150 }
151 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
152 // implementation.
153 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 03:35:21 -0700154
155 webrtc::CodecInst codec_inst = {0};
156 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
157 RTC_DCHECK_NE(codec_inst.pltype, -1);
158 stats.codec_name = codec_inst.plname;
159
160 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800161 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800162 // Lookup report for send ssrc only.
163 if (block.source_SSRC == stats.local_ssrc) {
164 stats.packets_lost = block.cumulative_num_packets_lost;
165 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
166 stats.ext_seqnum = block.extended_highest_sequence_number;
167 // Convert samples to milliseconds.
168 if (codec_inst.plfreq / 1000 > 0) {
169 stats.jitter_ms =
170 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 03:35:21 -0700171 }
solenberg8b85de22015-11-16 09:48:04 -0800172 break;
solenberg85a04962015-10-27 03:35:21 -0700173 }
174 }
175 }
176
solenberg85a04962015-10-27 03:35:21 -0700177 // Local speech level.
178 {
179 unsigned int level = 0;
solenberg358057b2015-11-27 10:46:42 -0800180 int error = volume->GetSpeechInputLevelFullRange(level);
solenberg8b85de22015-11-16 09:48:04 -0800181 RTC_DCHECK_EQ(0, error);
182 stats.audio_level = static_cast<int32_t>(level);
solenberg85a04962015-10-27 03:35:21 -0700183 }
184
solenberg85a04962015-10-27 03:35:21 -0700185 bool echo_metrics_on = false;
solenberg358057b2015-11-27 10:46:42 -0800186 int error = processing->GetEcMetricsStatus(echo_metrics_on);
solenberg8b85de22015-11-16 09:48:04 -0800187 RTC_DCHECK_EQ(0, error);
188 if (echo_metrics_on) {
solenberg85a04962015-10-27 03:35:21 -0700189 // These can also be negative, but in practice -1 is only used to signal
190 // insufficient data, since the resolution is limited to multiples of 4 ms.
191 int median = -1;
192 int std = -1;
193 float dummy = 0.0f;
solenberg8b85de22015-11-16 09:48:04 -0800194 error = processing->GetEcDelayMetrics(median, std, dummy);
195 RTC_DCHECK_EQ(0, error);
196 stats.echo_delay_median_ms = median;
197 stats.echo_delay_std_ms = std;
solenberg85a04962015-10-27 03:35:21 -0700198
199 // These can take on valid negative values, so use the lowest possible level
200 // as default rather than -1.
201 int erl = -100;
202 int erle = -100;
203 int dummy1 = 0;
204 int dummy2 = 0;
solenberg8b85de22015-11-16 09:48:04 -0800205 error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2);
206 RTC_DCHECK_EQ(0, error);
207 stats.echo_return_loss = erl;
208 stats.echo_return_loss_enhancement = erle;
solenberg85a04962015-10-27 03:35:21 -0700209 }
210
solenberg3a941542015-11-16 07:34:50 -0800211 internal::AudioState* audio_state =
212 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 15:34:49 -0800213 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 03:35:21 -0700214
215 return stats;
216}
217
218const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
219 RTC_DCHECK(thread_checker_.CalledOnValidThread());
220 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700221}
222
solenberg3a941542015-11-16 07:34:50 -0800223VoiceEngine* AudioSendStream::voice_engine() const {
224 internal::AudioState* audio_state =
225 static_cast<internal::AudioState*>(audio_state_.get());
226 VoiceEngine* voice_engine = audio_state->voice_engine();
227 RTC_DCHECK(voice_engine);
228 return voice_engine;
solenbergc7a8b082015-10-16 14:35:07 -0700229}
230} // namespace internal
231} // namespace webrtc