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 | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 15 | #include "webrtc/audio/conversion.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 16 | #include "webrtc/base/checks.h" |
| 17 | #include "webrtc/base/logging.h" |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 18 | #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" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | std::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(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 30 | if (i != extensions.size() - 1) { |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 31 | ss << ", "; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 32 | } |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 33 | } |
| 34 | ss << ']'; |
| 35 | ss << '}'; |
| 36 | return ss.str(); |
| 37 | } |
| 38 | |
| 39 | std::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 | |
| 50 | namespace internal { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 51 | AudioSendStream::AudioSendStream(const webrtc::AudioSendStream::Config& config, |
| 52 | VoiceEngine* voice_engine) |
| 53 | : config_(config), |
| 54 | voice_engine_(voice_engine), |
| 55 | voe_base_(voice_engine) { |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 56 | LOG(LS_INFO) << "AudioSendStream: " << config_.ToString(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 57 | RTC_DCHECK_NE(config.voe_channel_id, -1); |
| 58 | RTC_DCHECK(voice_engine_); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | AudioSendStream::~AudioSendStream() { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 62 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 63 | LOG(LS_INFO) << "~AudioSendStream: " << config_.ToString(); |
| 64 | } |
| 65 | |
| 66 | webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 67 | 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 | |
| 157 | const webrtc::AudioSendStream::Config& AudioSendStream::config() const { |
| 158 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 159 | return config_; |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void AudioSendStream::Start() { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 163 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void AudioSendStream::Stop() { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 167 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void AudioSendStream::SignalNetworkState(NetworkState state) { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 171 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame^] | 175 | // 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()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | } // namespace internal |
| 182 | } // namespace webrtc |