solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 15 | #include "webrtc/audio/audio_state.h" |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 16 | #include "webrtc/audio/conversion.h" |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 17 | #include "webrtc/audio/scoped_voe_interface.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 18 | #include "webrtc/base/checks.h" |
| 19 | #include "webrtc/base/logging.h" |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 20 | #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" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | std::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(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 32 | if (i != extensions.size() - 1) { |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 33 | ss << ", "; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 34 | } |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 35 | } |
| 36 | ss << ']'; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 37 | ss << ", c_name: " << c_name; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 38 | ss << '}'; |
| 39 | return ss.str(); |
| 40 | } |
| 41 | |
| 42 | std::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 | |
| 53 | namespace internal { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 54 | |
| 55 | AudioSendStream::AudioSendStream( |
| 56 | const webrtc::AudioSendStream::Config& config, |
| 57 | const rtc::scoped_refptr<webrtc::AudioState>& audio_state) |
| 58 | : config_(config), audio_state_(audio_state) { |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 59 | LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 60 | RTC_DCHECK_NE(config_.voe_channel_id, -1); |
| 61 | RTC_DCHECK(audio_state_.get()); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 62 | |
| 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 | } |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | AudioSendStream::~AudioSendStream() { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 90 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 91 | LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
| 92 | } |
| 93 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 94 | void AudioSendStream::Start() { |
| 95 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 96 | } |
| 97 | |
| 98 | void AudioSendStream::Stop() { |
| 99 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 100 | } |
| 101 | |
| 102 | void AudioSendStream::SignalNetworkState(NetworkState state) { |
| 103 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 104 | } |
| 105 | |
| 106 | bool 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 | |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 114 | webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 115 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 116 | webrtc::AudioSendStream::Stats stats; |
| 117 | stats.local_ssrc = config_.rtp.ssrc; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 118 | ScopedVoEInterface<VoEAudioProcessing> processing(voice_engine()); |
| 119 | ScopedVoEInterface<VoECodec> codec(voice_engine()); |
| 120 | ScopedVoEInterface<VoERTP_RTCP> rtp(voice_engine()); |
| 121 | ScopedVoEInterface<VoEVolumeControl> volume(voice_engine()); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 122 | unsigned int ssrc = 0; |
| 123 | webrtc::CallStatistics call_stats = {0}; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 124 | // TODO(solenberg): Change error code checking to RTC_CHECK_EQ(..., -1), if |
| 125 | // possible... |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 126 | 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 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 201 | internal::AudioState* audio_state = |
| 202 | static_cast<internal::AudioState*>(audio_state_.get()); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 203 | stats.typing_noise_detected = audio_state->typing_noise_detected(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 204 | |
| 205 | return stats; |
| 206 | } |
| 207 | |
| 208 | const webrtc::AudioSendStream::Config& AudioSendStream::config() const { |
| 209 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 210 | return config_; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 211 | } |
| 212 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame^] | 213 | VoiceEngine* 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; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 219 | } |
| 220 | } // namespace internal |
| 221 | } // namespace webrtc |