blob: 2ff388bbca10821249e27542c6867164e4e99e1f [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);
solenberg3a941542015-11-16 07:34:50 -080069 for (const auto& extension : config.rtp.extensions) {
solenberg3a941542015-11-16 07:34:50 -080070 if (extension.name == RtpExtension::kAbsSendTime) {
solenberg358057b2015-11-27 10:46:42 -080071 channel_proxy_->SetSendAbsoluteSenderTimeStatus(true, extension.id);
solenberg3a941542015-11-16 07:34:50 -080072 } else if (extension.name == RtpExtension::kAudioLevel) {
solenberg358057b2015-11-27 10:46:42 -080073 channel_proxy_->SetSendAudioLevelIndicationStatus(true, extension.id);
solenberg3a941542015-11-16 07:34:50 -080074 } else {
75 RTC_NOTREACHED() << "Registering unsupported RTP extension.";
76 }
77 }
solenbergc7a8b082015-10-16 14:35:07 -070078}
79
80AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 03:35:21 -070081 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -070082 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
83}
84
solenberg3a941542015-11-16 07:34:50 -080085void 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
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100105bool AudioSendStream::SendTelephoneEvent(int payload_type, uint8_t event,
106 uint32_t duration_ms) {
107 RTC_DCHECK(thread_checker_.CalledOnValidThread());
108 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type) &&
109 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
110}
111
solenbergc7a8b082015-10-16 14:35:07 -0700112webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -0700113 RTC_DCHECK(thread_checker_.CalledOnValidThread());
114 webrtc::AudioSendStream::Stats stats;
115 stats.local_ssrc = config_.rtp.ssrc;
solenberg3a941542015-11-16 07:34:50 -0800116 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine());
117 ScopedVoEInterface<VoECodec> codec(voice_engine());
solenberg3a941542015-11-16 07:34:50 -0800118 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine());
solenberg85a04962015-10-27 03:35:21 -0700119
solenberg358057b2015-11-27 10:46:42 -0800120 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700121 stats.bytes_sent = call_stats.bytesSent;
122 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800123 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
124 // returns 0 to indicate an error value.
125 if (call_stats.rttMs > 0) {
126 stats.rtt_ms = call_stats.rttMs;
127 }
128 // TODO(solenberg): [was ajm]: Re-enable this metric once we have a reliable
129 // implementation.
130 stats.aec_quality_min = -1;
solenberg85a04962015-10-27 03:35:21 -0700131
132 webrtc::CodecInst codec_inst = {0};
133 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
134 RTC_DCHECK_NE(codec_inst.pltype, -1);
135 stats.codec_name = codec_inst.plname;
136
137 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800138 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800139 // Lookup report for send ssrc only.
140 if (block.source_SSRC == stats.local_ssrc) {
141 stats.packets_lost = block.cumulative_num_packets_lost;
142 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
143 stats.ext_seqnum = block.extended_highest_sequence_number;
144 // Convert samples to milliseconds.
145 if (codec_inst.plfreq / 1000 > 0) {
146 stats.jitter_ms =
147 block.interarrival_jitter / (codec_inst.plfreq / 1000);
solenberg85a04962015-10-27 03:35:21 -0700148 }
solenberg8b85de22015-11-16 09:48:04 -0800149 break;
solenberg85a04962015-10-27 03:35:21 -0700150 }
151 }
152 }
153
solenberg85a04962015-10-27 03:35:21 -0700154 // Local speech level.
155 {
156 unsigned int level = 0;
solenberg358057b2015-11-27 10:46:42 -0800157 int error = volume->GetSpeechInputLevelFullRange(level);
solenberg8b85de22015-11-16 09:48:04 -0800158 RTC_DCHECK_EQ(0, error);
159 stats.audio_level = static_cast<int32_t>(level);
solenberg85a04962015-10-27 03:35:21 -0700160 }
161
solenberg85a04962015-10-27 03:35:21 -0700162 bool echo_metrics_on = false;
solenberg358057b2015-11-27 10:46:42 -0800163 int error = processing->GetEcMetricsStatus(echo_metrics_on);
solenberg8b85de22015-11-16 09:48:04 -0800164 RTC_DCHECK_EQ(0, error);
165 if (echo_metrics_on) {
solenberg85a04962015-10-27 03:35:21 -0700166 // These can also be negative, but in practice -1 is only used to signal
167 // insufficient data, since the resolution is limited to multiples of 4 ms.
168 int median = -1;
169 int std = -1;
170 float dummy = 0.0f;
solenberg8b85de22015-11-16 09:48:04 -0800171 error = processing->GetEcDelayMetrics(median, std, dummy);
172 RTC_DCHECK_EQ(0, error);
173 stats.echo_delay_median_ms = median;
174 stats.echo_delay_std_ms = std;
solenberg85a04962015-10-27 03:35:21 -0700175
176 // These can take on valid negative values, so use the lowest possible level
177 // as default rather than -1.
178 int erl = -100;
179 int erle = -100;
180 int dummy1 = 0;
181 int dummy2 = 0;
solenberg8b85de22015-11-16 09:48:04 -0800182 error = processing->GetEchoMetrics(erl, erle, dummy1, dummy2);
183 RTC_DCHECK_EQ(0, error);
184 stats.echo_return_loss = erl;
185 stats.echo_return_loss_enhancement = erle;
solenberg85a04962015-10-27 03:35:21 -0700186 }
187
solenberg3a941542015-11-16 07:34:50 -0800188 internal::AudioState* audio_state =
189 static_cast<internal::AudioState*>(audio_state_.get());
solenberg566ef242015-11-06 15:34:49 -0800190 stats.typing_noise_detected = audio_state->typing_noise_detected();
solenberg85a04962015-10-27 03:35:21 -0700191
192 return stats;
193}
194
195const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
196 RTC_DCHECK(thread_checker_.CalledOnValidThread());
197 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700198}
199
solenberg3a941542015-11-16 07:34:50 -0800200VoiceEngine* AudioSendStream::voice_engine() const {
201 internal::AudioState* audio_state =
202 static_cast<internal::AudioState*>(audio_state_.get());
203 VoiceEngine* voice_engine = audio_state->voice_engine();
204 RTC_DCHECK(voice_engine);
205 return voice_engine;
solenbergc7a8b082015-10-16 14:35:07 -0700206}
207} // namespace internal
208} // namespace webrtc