blob: 04d3a255a95a9403d701dcf235da21db5f3438f1 [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"
solenberg13725082015-11-25 08:16:52 -080020#include "webrtc/voice_engine/channel_proxy.h"
solenberg85a04962015-10-27 03:35:21 -070021#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 08:16:52 -080025#include "webrtc/voice_engine/voice_engine_impl.h"
solenbergc7a8b082015-10-16 14:35:07 -070026
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 03:35:21 -070034 if (i != extensions.size() - 1) {
solenbergc7a8b082015-10-16 14:35:07 -070035 ss << ", ";
solenberg85a04962015-10-27 03:35:21 -070036 }
solenbergc7a8b082015-10-16 14:35:07 -070037 }
38 ss << ']';
solenberg3a941542015-11-16 07:34:50 -080039 ss << ", c_name: " << c_name;
solenbergc7a8b082015-10-16 14:35:07 -070040 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 15:34:49 -080056AudioSendStream::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 14:35:07 -070060 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080061 RTC_DCHECK_NE(config_.voe_channel_id, -1);
62 RTC_DCHECK(audio_state_.get());
solenberg3a941542015-11-16 07:34:50 -080063
solenberg13725082015-11-25 08:16:52 -080064 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);
69
solenberg3a941542015-11-16 07:34:50 -080070 const int channel_id = config.voe_channel_id;
71 ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine());
solenberg3a941542015-11-16 07:34:50 -080072 for (const auto& extension : config.rtp.extensions) {
73 // One-byte-extension local identifiers are in the range 1-14 inclusive.
74 RTC_DCHECK_GE(extension.id, 1);
75 RTC_DCHECK_LE(extension.id, 14);
76 if (extension.name == RtpExtension::kAbsSendTime) {
solenberg13725082015-11-25 08:16:52 -080077 int error = rtp->SetSendAbsoluteSenderTimeStatus(channel_id, true,
78 extension.id);
solenberg3a941542015-11-16 07:34:50 -080079 RTC_DCHECK_EQ(0, error);
80 } else if (extension.name == RtpExtension::kAudioLevel) {
solenberg13725082015-11-25 08:16:52 -080081 int error = rtp->SetSendAudioLevelIndicationStatus(channel_id, true,
82 extension.id);
solenberg3a941542015-11-16 07:34:50 -080083 RTC_DCHECK_EQ(0, error);
84 } else {
85 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
86 }
87 }
solenbergc7a8b082015-10-16 14:35:07 -070088}
89
90AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 03:35:21 -070091 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -070092 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
93}
94
solenberg3a941542015-11-16 07:34:50 -080095void AudioSendStream::Start() {
96 RTC_DCHECK(thread_checker_.CalledOnValidThread());
97}
98
99void AudioSendStream::Stop() {
100 RTC_DCHECK(thread_checker_.CalledOnValidThread());
101}
102
103void AudioSendStream::SignalNetworkState(NetworkState state) {
104 RTC_DCHECK(thread_checker_.CalledOnValidThread());
105}
106
107bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
108 // TODO(solenberg): Tests call this function on a network thread, libjingle
109 // calls on the worker thread. We should move towards always using a network
110 // thread. Then this check can be enabled.
111 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
112 return false;
113}
114
solenbergc7a8b082015-10-16 14:35:07 -0700115webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -0700116 RTC_DCHECK(thread_checker_.CalledOnValidThread());
117 webrtc::AudioSendStream::Stats stats;
118 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 07:34:50 -0800119 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
120 ScopedVoEInterface<VoECodec> codec(voice_engine());
121 ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine());
122 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 03:35:21 -0700123
solenberg8b85de22015-11-16 09:48:04 -0800124 webrtc::CallStatistics call_stats = {0};
125 int error = rtp->GetRTCPStatistics(config_.voe_channel_id, call_stats);
126 RTC_DCHECK_EQ(0, error);
solenberg85a04962015-10-27 03:35:21 -0700127 stats.bytes_sent = call_stats.bytesSent;
128 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800129 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
130 // returns 0 to indicate an error value.
131 if (call_stats.rttMs > 0) {
132 stats.rtt_ms = call_stats.rttMs;
133 }
134 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
135 // implementation.
136 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 03:35:21 -0700137
138 webrtc::CodecInst codec_inst = {0};
139 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
140 RTC_DCHECK_NE(codec_inst.pltype, -1);
141 stats.codec_name = codec_inst.plname;
142
143 // Get data from the last remote RTCP report.
144 std::vector<webrtc::ReportBlock> blocks;
solenberg8b85de22015-11-16 09:48:04 -0800145 error = rtp->GetRemoteRTCPReportBlocks(config_.voe_channel_id, &blocks);
146 RTC_DCHECK_EQ(0, error);
147 for (const webrtc::ReportBlock& block : blocks) {
148 // Lookup report for send ssrc only.
149 if (block.source_SSRC == stats.local_ssrc) {
150 stats.packets_lost = block.cumulative_num_packets_lost;
151 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
152 stats.ext_seqnum = block.extended_highest_sequence_number;
153 // Convert samples to milliseconds.
154 if (codec_inst.plfreq / 1000 > 0) {
155 stats.jitter_ms =
156 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 03:35:21 -0700157 }
solenberg8b85de22015-11-16 09:48:04 -0800158 break;
solenberg85a04962015-10-27 03:35:21 -0700159 }
160 }
161 }
162
solenberg85a04962015-10-27 03:35:21 -0700163 // Local speech level.
164 {
165 unsigned int level = 0;
solenberg8b85de22015-11-16 09:48:04 -0800166 error = volume->GetSpeechInputLevelFullRange(level);
167 RTC_DCHECK_EQ(0, error);
168 stats.audio_level = static_cast<int32_t>(level);
solenberg85a04962015-10-27 03:35:21 -0700169 }
170
solenberg85a04962015-10-27 03:35:21 -0700171 bool echo_metrics_on = false;
solenberg8b85de22015-11-16 09:48:04 -0800172 error = processing->GetEcMetricsStatus(echo_metrics_on);
173 RTC_DCHECK_EQ(0, error);
174 if (echo_metrics_on) {
solenberg85a04962015-10-27 03:35:21 -0700175 // These can also be negative, but in practice -1 is only used to signal
176 // insufficient data, since the resolution is limited to multiples of 4 ms.
177 int median = -1;
178 int std = -1;
179 float dummy = 0.0f;
solenberg8b85de22015-11-16 09:48:04 -0800180 error = processing->GetEcDelayMetrics(median, std, dummy);
181 RTC_DCHECK_EQ(0, error);
182 stats.echo_delay_median_ms = median;
183 stats.echo_delay_std_ms = std;
solenberg85a04962015-10-27 03:35:21 -0700184
185 // These can take on valid negative values, so use the lowest possible level
186 // as default rather than -1.
187 int erl = -100;
188 int erle = -100;
189 int dummy1 = 0;
190 int dummy2 = 0;
solenberg8b85de22015-11-16 09:48:04 -0800191 error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2);
192 RTC_DCHECK_EQ(0, error);
193 stats.echo_return_loss = erl;
194 stats.echo_return_loss_enhancement = erle;
solenberg85a04962015-10-27 03:35:21 -0700195 }
196
solenberg3a941542015-11-16 07:34:50 -0800197 internal::AudioState* audio_state =
198 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 15:34:49 -0800199 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 03:35:21 -0700200
201 return stats;
202}
203
204const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
205 RTC_DCHECK(thread_checker_.CalledOnValidThread());
206 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700207}
208
solenberg3a941542015-11-16 07:34:50 -0800209VoiceEngine* AudioSendStream::voice_engine() const {
210 internal::AudioState* audio_state =
211 static_cast<internal::AudioState*>(audio_state_.get());
212 VoiceEngine* voice_engine = audio_state->voice_engine();
213 RTC_DCHECK(voice_engine);
214 return voice_engine;
solenbergc7a8b082015-10-16 14:35:07 -0700215}
216} // namespace internal
217} // namespace webrtc