blob: 4dd952295667f7a231497b1202cd70ab10f5ed53 [file] [log] [blame]
solenbergc7a8b082015-10-16 21:35:071/*
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 23:34:4915#include "webrtc/audio/audio_state.h"
solenberg85a04962015-10-27 10:35:2116#include "webrtc/audio/conversion.h"
solenberg566ef242015-11-06 23:34:4917#include "webrtc/audio/scoped_voe_interface.h"
solenbergc7a8b082015-10-16 21:35:0718#include "webrtc/base/checks.h"
19#include "webrtc/base/logging.h"
solenberg13725082015-11-25 16:16:5220#include "webrtc/voice_engine/channel_proxy.h"
solenberg85a04962015-10-27 10:35:2121#include "webrtc/voice_engine/include/voe_audio_processing.h"
22#include "webrtc/voice_engine/include/voe_codec.h"
23#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
24#include "webrtc/voice_engine/include/voe_volume_control.h"
solenberg13725082015-11-25 16:16:5225#include "webrtc/voice_engine/voice_engine_impl.h"
solenbergc7a8b082015-10-16 21:35:0726
27namespace webrtc {
28std::string AudioSendStream::Config::Rtp::ToString() const {
29 std::stringstream ss;
30 ss << "{ssrc: " << ssrc;
31 ss << ", extensions: [";
32 for (size_t i = 0; i < extensions.size(); ++i) {
33 ss << extensions[i].ToString();
solenberg85a04962015-10-27 10:35:2134 if (i != extensions.size() - 1) {
solenbergc7a8b082015-10-16 21:35:0735 ss << ", ";
solenberg85a04962015-10-27 10:35:2136 }
solenbergc7a8b082015-10-16 21:35:0737 }
38 ss << ']';
solenberg3a941542015-11-16 15:34:5039 ss << ", c_name: " << c_name;
solenbergc7a8b082015-10-16 21:35:0740 ss << '}';
41 return ss.str();
42}
43
44std::string AudioSendStream::Config::ToString() const {
45 std::stringstream ss;
46 ss << "{rtp: " << rtp.ToString();
47 ss << ", voe_channel_id: " << voe_channel_id;
48 // TODO(solenberg): Encoder config.
49 ss << ", cng_payload_type: " << cng_payload_type;
50 ss << ", red_payload_type: " << red_payload_type;
51 ss << '}';
52 return ss.str();
53}
54
55namespace internal {
solenberg566ef242015-11-06 23:34:4956AudioSendStream::AudioSendStream(
57 const webrtc::AudioSendStream::Config& config,
58 const rtc::scoped_refptr<webrtc::AudioState>& audio_state)
59 : config_(config), audio_state_(audio_state) {
solenbergc7a8b082015-10-16 21:35:0760 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg566ef242015-11-06 23:34:4961 RTC_DCHECK_NE(config_.voe_channel_id, -1);
62 RTC_DCHECK(audio_state_.get());
solenberg3a941542015-11-16 15:34:5063
solenberg13725082015-11-25 16:16:5264 VoiceEngineImpl* voe_impl = static_cast<VoiceEngineImpl*>(voice_engine());
65 channel_proxy_ = voe_impl->GetChannelProxy(config_.voe_channel_id);
66 channel_proxy_->SetRTCPStatus(true);
67 channel_proxy_->SetLocalSSRC(config.rtp.ssrc);
68 channel_proxy_->SetRTCP_CNAME(config.rtp.c_name);
solenberg3a941542015-11-16 15:34:5069 for (const auto& extension : config.rtp.extensions) {
solenberg3a941542015-11-16 15:34:5070 if (extension.name == RtpExtension::kAbsSendTime) {
solenberg358057b2015-11-27 18:46:4271 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id);
solenberg3a941542015-11-16 15:34:5072 } else if (extension.name == RtpExtension::kAudioLevel) {
solenberg358057b2015-11-27 18:46:4273 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
solenberg3a941542015-11-16 15:34:5074 } else {
75 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
76 }
77 }
solenbergc7a8b082015-10-16 21:35:0778}
79
80AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 10:35:2181 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 21:35:0782 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
83}
84
solenberg3a941542015-11-16 15:34:5085void AudioSendStream::Start() {
86 RTC_DCHECK(thread_checker_.CalledOnValidThread());
87}
88
89void AudioSendStream::Stop() {
90 RTC_DCHECK(thread_checker_.CalledOnValidThread());
91}
92
93void AudioSendStream::SignalNetworkState(NetworkState state) {
94 RTC_DCHECK(thread_checker_.CalledOnValidThread());
95}
96
97bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
98 // TODO(solenberg): Tests call this function on a network thread, libjingle
99 // calls on the worker thread. We should move towards always using a network
100 // thread. Then this check can be enabled.
101 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
102 return false;
103}
104
solenbergc7a8b082015-10-16 21:35:07105webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 10:35:21106 RTC_DCHECK(thread_checker_.CalledOnValidThread());
107 webrtc::AudioSendStream::Stats stats;
108 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 15:34:50109 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
110 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg3a941542015-11-16 15:34:50111 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 10:35:21112
solenberg358057b2015-11-27 18:46:42113 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 10:35:21114 stats.bytes_sent = call_stats.bytesSent;
115 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 17:48:04116 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
117 // returns 0 to indicate an error value.
118 if (call_stats.rttMs > 0) {
119 stats.rtt_ms = call_stats.rttMs;
120 }
121 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
122 // implementation.
123 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 10:35:21124
125 webrtc::CodecInst codec_inst = {0};
126 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
127 RTC_DCHECK_NE(codec_inst.pltype, -1);
128 stats.codec_name = codec_inst.plname;
129
130 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 18:46:42131 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 17:48:04132 // Lookup report for send ssrc only.
133 if (block.source_SSRC == stats.local_ssrc) {
134 stats.packets_lost = block.cumulative_num_packets_lost;
135 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
136 stats.ext_seqnum = block.extended_highest_sequence_number;
137 // Convert samples to milliseconds.
138 if (codec_inst.plfreq / 1000 > 0) {
139 stats.jitter_ms =
140 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 10:35:21141 }
solenberg8b85de22015-11-16 17:48:04142 break;
solenberg85a04962015-10-27 10:35:21143 }
144 }
145 }
146
solenberg85a04962015-10-27 10:35:21147 // Local speech level.
148 {
149 unsigned int level = 0;
solenberg358057b2015-11-27 18:46:42150 int error = volume->GetSpeechInputLevelFullRange(level);
solenberg8b85de22015-11-16 17:48:04151 RTC_DCHECK_EQ(0, error);
152 stats.audio_level = static_cast<int32_t>(level);
solenberg85a04962015-10-27 10:35:21153 }
154
solenberg85a04962015-10-27 10:35:21155 bool echo_metrics_on = false;
solenberg358057b2015-11-27 18:46:42156 int error = processing->GetEcMetricsStatus(echo_metrics_on);
solenberg8b85de22015-11-16 17:48:04157 RTC_DCHECK_EQ(0, error);
158 if (echo_metrics_on) {
solenberg85a04962015-10-27 10:35:21159 // These can also be negative, but in practice -1 is only used to signal
160 // insufficient data, since the resolution is limited to multiples of 4 ms.
161 int median = -1;
162 int std = -1;
163 float dummy = 0.0f;
solenberg8b85de22015-11-16 17:48:04164 error = processing->GetEcDelayMetrics(median, std, dummy);
165 RTC_DCHECK_EQ(0, error);
166 stats.echo_delay_median_ms = median;
167 stats.echo_delay_std_ms = std;
solenberg85a04962015-10-27 10:35:21168
169 // These can take on valid negative values, so use the lowest possible level
170 // as default rather than -1.
171 int erl = -100;
172 int erle = -100;
173 int dummy1 = 0;
174 int dummy2 = 0;
solenberg8b85de22015-11-16 17:48:04175 error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2);
176 RTC_DCHECK_EQ(0, error);
177 stats.echo_return_loss = erl;
178 stats.echo_return_loss_enhancement = erle;
solenberg85a04962015-10-27 10:35:21179 }
180
solenberg3a941542015-11-16 15:34:50181 internal::AudioState* audio_state =
182 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 23:34:49183 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 10:35:21184
185 return stats;
186}
187
188const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
189 RTC_DCHECK(thread_checker_.CalledOnValidThread());
190 return config_;
solenbergc7a8b082015-10-16 21:35:07191}
192
solenberg3a941542015-11-16 15:34:50193VoiceEngine* AudioSendStream::voice_engine() const {
194 internal::AudioState* audio_state =
195 static_cast<internal::AudioState*>(audio_state_.get());
196 VoiceEngine* voice_engine = audio_state->voice_engine();
197 RTC_DCHECK(voice_engine);
198 return voice_engine;
solenbergc7a8b082015-10-16 21:35:07199}
200} // namespace internal
201} // namespace webrtc