blob: ccfdca546dcf5c9699b85f7745b21c4c2ade671f [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
solenberg85a04962015-10-27 03:35:21 -070015#include "webrtc/audio/conversion.h"
solenbergc7a8b082015-10-16 14:35:07 -070016#include "webrtc/base/checks.h"
17#include "webrtc/base/logging.h"
solenberg85a04962015-10-27 03:35:21 -070018#include "webrtc/voice_engine/include/voe_audio_processing.h"
19#include "webrtc/voice_engine/include/voe_codec.h"
20#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
21#include "webrtc/voice_engine/include/voe_volume_control.h"
solenbergc7a8b082015-10-16 14:35:07 -070022
23namespace webrtc {
24std::string AudioSendStream::Config::Rtp::ToString() const {
25 std::stringstream ss;
26 ss << "{ssrc: " << ssrc;
27 ss << ", extensions: [";
28 for (size_t i = 0; i < extensions.size(); ++i) {
29 ss << extensions[i].ToString();
solenberg85a04962015-10-27 03:35:21 -070030 if (i != extensions.size() - 1) {
solenbergc7a8b082015-10-16 14:35:07 -070031 ss << ", ";
solenberg85a04962015-10-27 03:35:21 -070032 }
solenbergc7a8b082015-10-16 14:35:07 -070033 }
34 ss << ']';
35 ss << '}';
36 return ss.str();
37}
38
39std::string AudioSendStream::Config::ToString() const {
40 std::stringstream ss;
41 ss << "{rtp: " << rtp.ToString();
42 ss << ", voe_channel_id: " << voe_channel_id;
43 // TODO(solenberg): Encoder config.
44 ss << ", cng_payload_type: " << cng_payload_type;
45 ss << ", red_payload_type: " << red_payload_type;
46 ss << '}';
47 return ss.str();
48}
49
50namespace internal {
solenberg85a04962015-10-27 03:35:21 -070051AudioSendStream::AudioSendStream(const webrtc::AudioSendStream::Config& config,
52 VoiceEngine* voice_engine)
53 : config_(config),
54 voice_engine_(voice_engine),
55 voe_base_(voice_engine) {
solenbergc7a8b082015-10-16 14:35:07 -070056 LOG(LS_INFO) << "AudioSendStream: " << config_.ToString();
solenberg85a04962015-10-27 03:35:21 -070057 RTC_DCHECK_NE(config.voe_channel_id, -1);
58 RTC_DCHECK(voice_engine_);
solenbergc7a8b082015-10-16 14:35:07 -070059}
60
61AudioSendStream::~AudioSendStream() {
solenberg85a04962015-10-27 03:35:21 -070062 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -070063 LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString();
64}
65
66webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
solenberg85a04962015-10-27 03:35:21 -070067 RTC_DCHECK(thread_checker_.CalledOnValidThread());
68 webrtc::AudioSendStream::Stats stats;
69 stats.local_ssrc = config_.rtp.ssrc;
70 ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine_);
71 ScopedVoEInterface<VoECodec> codec(voice_engine_);
72 ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine_);
73 ScopedVoEInterface<VoEVolumeControl> volume(voice_engine_);
74 unsigned int ssrc = 0;
75 webrtc::CallStatistics call_stats = {0};
76 if (rtp->GetLocalSSRC(config_.voe_channel_id, ssrc) == -1 ||
77 rtp->GetRTCPStatistics(config_.voe_channel_id, call_stats) == -1) {
78 return stats;
79 }
80
81 stats.bytes_sent = call_stats.bytesSent;
82 stats.packets_sent = call_stats.packetsSent;
83
84 webrtc::CodecInst codec_inst = {0};
85 if (codec->GetSendCodec(config_.voe_channel_id, codec_inst) != -1) {
86 RTC_DCHECK_NE(codec_inst.pltype, -1);
87 stats.codec_name = codec_inst.plname;
88
89 // Get data from the last remote RTCP report.
90 std::vector<webrtc::ReportBlock> blocks;
91 if (rtp->GetRemoteRTCPReportBlocks(config_.voe_channel_id, &blocks) != -1) {
92 for (const webrtc::ReportBlock& block : blocks) {
93 // Lookup report for send ssrc only.
94 if (block.source_SSRC == stats.local_ssrc) {
95 stats.packets_lost = block.cumulative_num_packets_lost;
96 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
97 stats.ext_seqnum = block.extended_highest_sequence_number;
98 // Convert samples to milliseconds.
99 if (codec_inst.plfreq / 1000 > 0) {
100 stats.jitter_ms =
101 block.interarrival_jitter / (codec_inst.plfreq / 1000);
102 }
103 break;
104 }
105 }
106 }
107 }
108
109 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
110 // returns 0 to indicate an error value.
111 if (call_stats.rttMs > 0) {
112 stats.rtt_ms = call_stats.rttMs;
113 }
114
115 // Local speech level.
116 {
117 unsigned int level = 0;
118 if (volume->GetSpeechInputLevelFullRange(level) != -1) {
119 stats.audio_level = static_cast<int32_t>(level);
120 }
121 }
122
123 // TODO(ajm): Re-enable this metric once we have a reliable implementation.
124 stats.aec_quality_min = -1;
125
126 bool echo_metrics_on = false;
127 if (processing->GetEcMetricsStatus(echo_metrics_on) != -1 &&
128 echo_metrics_on) {
129 // These can also be negative, but in practice -1 is only used to signal
130 // insufficient data, since the resolution is limited to multiples of 4 ms.
131 int median = -1;
132 int std = -1;
133 float dummy = 0.0f;
134 if (processing->GetEcDelayMetrics(median, std, dummy) != -1) {
135 stats.echo_delay_median_ms = median;
136 stats.echo_delay_std_ms = std;
137 }
138
139 // These can take on valid negative values, so use the lowest possible level
140 // as default rather than -1.
141 int erl = -100;
142 int erle = -100;
143 int dummy1 = 0;
144 int dummy2 = 0;
145 if (processing->GetEchoMetrics(erl, erle, dummy1, dummy2) != -1) {
146 stats.echo_return_loss = erl;
147 stats.echo_return_loss_enhancement = erle;
148 }
149 }
150
151 // TODO(solenberg): Collect typing noise warnings here too!
152 // bool typing_noise_detected = typing_noise_detected_;
153
154 return stats;
155}
156
157const webrtc::AudioSendStream::Config& AudioSendStream::config() const {
158 RTC_DCHECK(thread_checker_.CalledOnValidThread());
159 return config_;
solenbergc7a8b082015-10-16 14:35:07 -0700160}
161
162void AudioSendStream::Start() {
solenberg85a04962015-10-27 03:35:21 -0700163 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700164}
165
166void AudioSendStream::Stop() {
solenberg85a04962015-10-27 03:35:21 -0700167 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700168}
169
170void AudioSendStream::SignalNetworkState(NetworkState state) {
solenberg85a04962015-10-27 03:35:21 -0700171 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700172}
173
174bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
solenberg85a04962015-10-27 03:35:21 -0700175 // TODO(solenberg): Tests call this function on a network thread, libjingle
176 // calls on the worker thread. We should move towards always using a network
177 // thread. Then this check can be enabled.
178 // RTC_DCHECK(!thread_checker_.CalledOnValidThread());
solenbergc7a8b082015-10-16 14:35:07 -0700179 return false;
180}
181} // namespace internal
182} // namespace webrtc