blob: 14112deb9ad6d63c4f1544796bafd2e3f13feeea [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"
solenberg85a04962015-10-27 03:35:21 -070020#include "webrtc/voice_engine/include/voe_audio_processing.h"
21#include "webrtc/voice_engine/include/voe_codec.h"
22#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
23#include "webrtc/voice_engine/include/voe_volume_control.h"
solenbergc7a8b082015-10-16 14:35:07 -070024
25namespace webrtc {
26std::string AudioSendStream::Config::Rtp::ToString() const {
27 std::stringstream ss;
28 ss << "{ssrc: " << ssrc;
29 ss << ", extensions: [";
30 for (size_t i = 0; i < extensions.size(); ++i) {
31 ss << extensions[i].ToString();
solenberg85a04962015-10-27 03:35:21 -070032 if (i != extensions.size() - 1) {
solenbergc7a8b082015-10-16 14:35:07 -070033 ss << ", ";
solenberg85a04962015-10-27 03:35:21 -070034 }
solenbergc7a8b082015-10-16 14:35:07 -070035 }
36 ss << ']';
solenberg3a941542015-11-16 07:34:50 -080037 ss << ", c_name: " << c_name;
solenbergc7a8b082015-10-16 14:35:07 -070038 ss << '}';
39 return ss.str();
40}
41
42std::string AudioSendStream::Config::ToString() const {
43 std::stringstream ss;
44 ss << "{rtp: " << rtp.ToString();
45 ss << ", voe_channel_id: " << voe_channel_id;
46 // TODO(solenberg): Encoder config.
47 ss << ", cng_payload_type: " << cng_payload_type;
48 ss << ", red_payload_type: " << red_payload_type;
49 ss << '}';
50 return ss.str();
51}
52
53namespace internal {
solenberg566ef242015-11-06 15:34:49 -080054
55AudioSendStream::AudioSendStream(
56 const webrtc::AudioSendStream::Config& config,
57 const rtc::scoped_refptr<webrtc::AudioState>& audio_state)
58 : config_(config), audio_state_(audio_state) {
solenbergc7a8b082015-10-16 14:35:07 -070059 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg566ef242015-11-06 15:34:49 -080060 RTC_DCHECK_NE(config_.voe_channel_id, -1);
61 RTC_DCHECK(audio_state_.get());
solenberg3a941542015-11-16 07:34:50 -080062
63 const int channel_id = config.voe_channel_id;
64 ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine());
65 int error = rtp->SetRTCPStatus(channel_id, true);
66 RTC_DCHECK_EQ(0, error);
67 error = rtp->SetLocalSSRC(channel_id, config.rtp.ssrc);
68 RTC_DCHECK_EQ(0, error);
69 error = rtp->SetRTCP_CNAME(channel_id, config.rtp.c_name.c_str());
70 RTC_DCHECK_EQ(0, error);
71 for (const auto& extension : config.rtp.extensions) {
72 // One-byte-extension local identifiers are in the range 1-14 inclusive.
73 RTC_DCHECK_GE(extension.id, 1);
74 RTC_DCHECK_LE(extension.id, 14);
75 if (extension.name == RtpExtension::kAbsSendTime) {
76 error = rtp->SetSendAbsoluteSenderTimeStatus(channel_id, true,
77 extension.id);
78 RTC_DCHECK_EQ(0, error);
79 } else if (extension.name == RtpExtension::kAudioLevel) {
80 error = rtp->SetSendAudioLevelIndicationStatus(channel_id, true,
81 extension.id);
82 RTC_DCHECK_EQ(0, error);
83 } else {
84 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
85 }
86 }
solenbergc7a8b082015-10-16 14:35:07 -070087}
88
89AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 03:35:21 -070090 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -070091 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
92}
93
solenberg3a941542015-11-16 07:34:50 -080094void AudioSendStream::Start() {
95 RTC_DCHECK(thread_checker_.CalledOnValidThread());
96}
97
98void AudioSendStream::Stop() {
99 RTC_DCHECK(thread_checker_.CalledOnValidThread());
100}
101
102void AudioSendStream::SignalNetworkState(NetworkState state) {
103 RTC_DCHECK(thread_checker_.CalledOnValidThread());
104}
105
106bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
107 // TODO(solenberg): Tests call this function on a network thread, libjingle
108 // calls on the worker thread. We should move towards always using a network
109 // thread. Then this check can be enabled.
110 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
111 return false;
112}
113
solenbergc7a8b082015-10-16 14:35:07 -0700114webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -0700115 RTC_DCHECK(thread_checker_.CalledOnValidThread());
116 webrtc::AudioSendStream::Stats stats;
117 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 07:34:50 -0800118 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
119 ScopedVoEInterface<VoECodec> codec(voice_engine());
120 ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine());
121 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 03:35:21 -0700122 unsigned int ssrc = 0;
123 webrtc::CallStatistics call_stats = {0};
solenberg3a941542015-11-16 07:34:50 -0800124 // TODO(solenberg): Change error code checking to RTC_CHECK_EQ(..., -1), if
125 // possible...
solenberg85a04962015-10-27 03:35:21 -0700126 if (rtp->GetLocalSSRC(config_.voe_channel_id, ssrc) == -1 ||
127 rtp->GetRTCPStatistics(config_.voe_channel_id, call_stats) == -1) {
128 return stats;
129 }
130
131 stats.bytes_sent = call_stats.bytesSent;
132 stats.packets_sent = call_stats.packetsSent;
133
134 webrtc::CodecInst codec_inst = {0};
135 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
136 RTC_DCHECK_NE(codec_inst.pltype, -1);
137 stats.codec_name = codec_inst.plname;
138
139 // Get data from the last remote RTCP report.
140 std::vector<webrtc::ReportBlock> blocks;
141 if (rtp->GetRemoteRTCPReportBlocks(config_.voe_channel_id, &blocks) != -1) {
142 for (const webrtc::ReportBlock& block : blocks) {
143 // Lookup report for send ssrc only.
144 if (block.source_SSRC == stats.local_ssrc) {
145 stats.packets_lost = block.cumulative_num_packets_lost;
146 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
147 stats.ext_seqnum = block.extended_highest_sequence_number;
148 // Convert samples to milliseconds.
149 if (codec_inst.plfreq / 1000 > 0) {
150 stats.jitter_ms =
151 block.interarrival_jitter / (codec_inst.plfreq / 1000);
152 }
153 break;
154 }
155 }
156 }
157 }
158
159 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
160 // returns 0 to indicate an error value.
161 if (call_stats.rttMs > 0) {
162 stats.rtt_ms = call_stats.rttMs;
163 }
164
165 // Local speech level.
166 {
167 unsigned int level = 0;
168 if (volume->GetSpeechInputLevelFullRange(level) != -1) {
169 stats.audio_level = static_cast<int32_t>(level);
170 }
171 }
172
173 // TODO(ajm): Re-enable this metric once we have a reliable implementation.
174 stats.aec_quality_min = -1;
175
176 bool echo_metrics_on = false;
177 if (processing->GetEcMetricsStatus(echo_metrics_on) != -1 &&
178 echo_metrics_on) {
179 // 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;
184 if (processing->GetEcDelayMetrics(median, std, dummy) != -1) {
185 stats.echo_delay_median_ms = median;
186 stats.echo_delay_std_ms = std;
187 }
188
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;
195 if (processing->GetEchoMetrics(erl, erle, dummy1, dummy2) != -1) {
196 stats.echo_return_loss = erl;
197 stats.echo_return_loss_enhancement = erle;
198 }
199 }
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