henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 11 | #ifdef HAVE_WEBRTC_VOICE |
| 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "media/engine/webrtcvoiceengine.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
| 15 | #include <algorithm> |
| 16 | #include <cstdio> |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 17 | #include <functional> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | #include <string> |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 19 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/call/audio_sink.h" |
| 23 | #include "media/base/audiosource.h" |
| 24 | #include "media/base/mediaconstants.h" |
| 25 | #include "media/base/streamparams.h" |
| 26 | #include "media/engine/adm_helpers.h" |
| 27 | #include "media/engine/apm_helpers.h" |
| 28 | #include "media/engine/payload_type_mapper.h" |
| 29 | #include "media/engine/webrtcmediaengine.h" |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 30 | #include "modules/audio_device/audio_device_impl.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "modules/audio_mixer/audio_mixer_impl.h" |
| 32 | #include "modules/audio_processing/aec_dump/aec_dump_factory.h" |
| 33 | #include "modules/audio_processing/include/audio_processing.h" |
| 34 | #include "rtc_base/arraysize.h" |
| 35 | #include "rtc_base/base64.h" |
| 36 | #include "rtc_base/byteorder.h" |
| 37 | #include "rtc_base/constructormagic.h" |
| 38 | #include "rtc_base/helpers.h" |
| 39 | #include "rtc_base/logging.h" |
| 40 | #include "rtc_base/race_checker.h" |
| 41 | #include "rtc_base/stringencode.h" |
| 42 | #include "rtc_base/stringutils.h" |
| 43 | #include "rtc_base/trace_event.h" |
| 44 | #include "system_wrappers/include/field_trial.h" |
| 45 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | namespace cricket { |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 48 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | |
solenberg | 418b7d3 | 2017-06-13 00:38:27 -0700 | [diff] [blame] | 50 | constexpr size_t kMaxUnsignaledRecvStreams = 4; |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 51 | |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 52 | constexpr int kNackRtpHistoryMs = 5000; |
minyue@webrtc.org | 2dc6f31 | 2014-10-31 05:33:10 +0000 | [diff] [blame] | 53 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 54 | // Check to verify that the define for the intelligibility enhancer is properly |
| 55 | // set. |
| 56 | #if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \ |
| 57 | (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \ |
| 58 | WEBRTC_INTELLIGIBILITY_ENHANCER != 1) |
| 59 | #error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1" |
| 60 | #endif |
| 61 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 62 | // For SendSideBwe, Opus bitrate should be in the range between 6000 and 32000. |
minyue | 10cbb46 | 2016-11-07 09:29:22 -0800 | [diff] [blame] | 63 | const int kOpusMinBitrateBps = 6000; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 64 | const int kOpusBitrateFbBps = 32000; |
deadbeef | 8034614 | 2016-04-27 14:17:10 -0700 | [diff] [blame] | 65 | |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 66 | // Default audio dscp value. |
| 67 | // See http://tools.ietf.org/html/rfc2474 for details. |
| 68 | // See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00 |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 69 | const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 70 | |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 71 | const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1) |
| 72 | const int kMaxTelephoneEventCode = 255; |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 73 | |
solenberg | 31642aa | 2016-03-14 08:00:37 -0700 | [diff] [blame] | 74 | const int kMinPayloadType = 0; |
| 75 | const int kMaxPayloadType = 127; |
| 76 | |
deadbeef | 884f585 | 2016-01-15 09:20:04 -0800 | [diff] [blame] | 77 | class ProxySink : public webrtc::AudioSinkInterface { |
| 78 | public: |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 79 | explicit ProxySink(AudioSinkInterface* sink) : sink_(sink) { |
| 80 | RTC_DCHECK(sink); |
| 81 | } |
deadbeef | 884f585 | 2016-01-15 09:20:04 -0800 | [diff] [blame] | 82 | |
| 83 | void OnData(const Data& audio) override { sink_->OnData(audio); } |
| 84 | |
| 85 | private: |
| 86 | webrtc::AudioSinkInterface* sink_; |
| 87 | }; |
| 88 | |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 89 | bool ValidateStreamParams(const StreamParams& sp) { |
| 90 | if (sp.ssrcs.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 91 | RTC_LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString(); |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | if (sp.ssrcs.size() > 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 95 | RTC_LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " |
| 96 | << sp.ToString(); |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 97 | return false; |
| 98 | } |
| 99 | return true; |
| 100 | } |
| 101 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | // Dumps an AudioCodec in RFC 2327-ish format. |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 103 | std::string ToString(const AudioCodec& codec) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 104 | std::stringstream ss; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 105 | ss << codec.name << "/" << codec.clockrate << "/" << codec.channels; |
| 106 | if (!codec.params.empty()) { |
| 107 | ss << " {"; |
| 108 | for (const auto& param : codec.params) { |
| 109 | ss << " " << param.first << "=" << param.second; |
| 110 | } |
| 111 | ss << " }"; |
| 112 | } |
| 113 | ss << " (" << codec.id << ")"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | return ss.str(); |
| 115 | } |
Minyue Li | 7100dcd | 2015-03-27 05:05:59 +0100 | [diff] [blame] | 116 | |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 117 | bool IsCodec(const AudioCodec& codec, const char* ref_name) { |
Minyue Li | 7100dcd | 2015-03-27 05:05:59 +0100 | [diff] [blame] | 118 | return (_stricmp(codec.name.c_str(), ref_name) == 0); |
| 119 | } |
| 120 | |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 121 | bool FindCodec(const std::vector<AudioCodec>& codecs, |
solenberg | 26c8c91 | 2015-11-27 04:00:25 -0800 | [diff] [blame] | 122 | const AudioCodec& codec, |
| 123 | AudioCodec* found_codec) { |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 124 | for (const AudioCodec& c : codecs) { |
| 125 | if (c.Matches(codec)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | if (found_codec != NULL) { |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 127 | *found_codec = c; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | } |
| 132 | return false; |
| 133 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 134 | |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 135 | bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) { |
| 136 | if (codecs.empty()) { |
| 137 | return true; |
| 138 | } |
| 139 | std::vector<int> payload_types; |
| 140 | for (const AudioCodec& codec : codecs) { |
| 141 | payload_types.push_back(codec.id); |
| 142 | } |
| 143 | std::sort(payload_types.begin(), payload_types.end()); |
| 144 | auto it = std::unique(payload_types.begin(), payload_types.end()); |
| 145 | return it == payload_types.end(); |
| 146 | } |
| 147 | |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 148 | rtc::Optional<std::string> GetAudioNetworkAdaptorConfig( |
| 149 | const AudioOptions& options) { |
| 150 | if (options.audio_network_adaptor && *options.audio_network_adaptor && |
| 151 | options.audio_network_adaptor_config) { |
| 152 | // Turn on audio network adaptor only when |options_.audio_network_adaptor| |
| 153 | // equals true and |options_.audio_network_adaptor_config| has a value. |
| 154 | return options.audio_network_adaptor_config; |
| 155 | } |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 156 | return rtc::nullopt; |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 157 | } |
| 158 | |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 159 | // |max_send_bitrate_bps| is the bitrate from "b=" in SDP. |
| 160 | // |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters. |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 161 | rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps, |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 162 | rtc::Optional<int> rtp_max_bitrate_bps, |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 163 | const webrtc::AudioCodecSpec& spec) { |
deadbeef | e702b30 | 2017-02-04 12:09:01 -0800 | [diff] [blame] | 164 | // If application-configured bitrate is set, take minimum of that and SDP |
| 165 | // bitrate. |
zstein | a5e0df6 | 2017-06-14 11:41:48 -0700 | [diff] [blame] | 166 | const int bps = |
| 167 | rtp_max_bitrate_bps |
| 168 | ? webrtc::MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps) |
| 169 | : max_send_bitrate_bps; |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 170 | if (bps <= 0) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 171 | return spec.info.default_bitrate_bps; |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 172 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 173 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 174 | if (bps < spec.info.min_bitrate_bps) { |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 175 | // If codec is not multi-rate and |bps| is less than the fixed bitrate then |
| 176 | // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed |
| 177 | // bitrate then ignore. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 178 | RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name |
| 179 | << " to bitrate " << bps << " bps" |
| 180 | << ", requires at least " << spec.info.min_bitrate_bps |
| 181 | << " bps."; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 182 | return rtc::nullopt; |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 183 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 184 | |
| 185 | if (spec.info.HasFixedBitrate()) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 186 | return spec.info.default_bitrate_bps; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 187 | } else { |
| 188 | // If codec is multi-rate then just set the bitrate. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 189 | return std::min(bps, spec.info.max_bitrate_bps); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 190 | } |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 191 | } |
| 192 | |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 193 | } // namespace |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 194 | |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 195 | WebRtcVoiceEngine::WebRtcVoiceEngine( |
| 196 | webrtc::AudioDeviceModule* adm, |
ossu | eb1fde4 | 2017-05-02 06:46:30 -0700 | [diff] [blame] | 197 | const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory, |
gyzhou | 95aa964 | 2016-12-13 14:06:26 -0800 | [diff] [blame] | 198 | const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory, |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 199 | rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer, |
| 200 | rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing) |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 201 | : adm_(adm), |
ossu | eb1fde4 | 2017-05-02 06:46:30 -0700 | [diff] [blame] | 202 | encoder_factory_(encoder_factory), |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 203 | decoder_factory_(decoder_factory), |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 204 | audio_mixer_(audio_mixer), |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 205 | apm_(audio_processing) { |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 206 | // This may be called from any thread, so detach thread checkers. |
| 207 | worker_thread_checker_.DetachFromThread(); |
solenberg | 26c8c91 | 2015-11-27 04:00:25 -0800 | [diff] [blame] | 208 | signal_thread_checker_.DetachFromThread(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 209 | RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine"; |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 210 | RTC_DCHECK(decoder_factory); |
| 211 | RTC_DCHECK(encoder_factory); |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 212 | RTC_DCHECK(audio_processing); |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 213 | // The rest of our initialization will happen in Init. |
| 214 | } |
| 215 | |
| 216 | WebRtcVoiceEngine::~WebRtcVoiceEngine() { |
| 217 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 218 | RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine"; |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 219 | if (initialized_) { |
| 220 | StopAecDump(); |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 221 | |
| 222 | // Stop AudioDevice. |
| 223 | adm()->StopPlayout(); |
| 224 | adm()->StopRecording(); |
| 225 | adm()->RegisterAudioCallback(nullptr); |
| 226 | adm()->Terminate(); |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | void WebRtcVoiceEngine::Init() { |
| 231 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 232 | RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init"; |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 233 | |
| 234 | // TaskQueue expects to be created/destroyed on the same thread. |
| 235 | low_priority_worker_queue_.reset( |
| 236 | new rtc::TaskQueue("rtc-low-prio", rtc::TaskQueue::Priority::LOW)); |
| 237 | |
ossu | eb1fde4 | 2017-05-02 06:46:30 -0700 | [diff] [blame] | 238 | // Load our audio codec lists. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 239 | RTC_LOG(LS_INFO) << "Supported send codecs in order of preference:"; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 240 | send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders()); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 241 | for (const AudioCodec& codec : send_codecs_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 242 | RTC_LOG(LS_INFO) << ToString(codec); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 245 | RTC_LOG(LS_INFO) << "Supported recv codecs in order of preference:"; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 246 | recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders()); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 247 | for (const AudioCodec& codec : recv_codecs_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 248 | RTC_LOG(LS_INFO) << ToString(codec); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 249 | } |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 250 | |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 251 | #if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE) |
| 252 | // No ADM supplied? Create a default one. |
solenberg | ff97631 | 2016-03-30 23:28:51 -0700 | [diff] [blame] | 253 | if (!adm_) { |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 254 | adm_ = webrtc::AudioDeviceModule::Create( |
| 255 | webrtc::AudioDeviceModule::kPlatformDefaultAudio); |
solenberg | ff97631 | 2016-03-30 23:28:51 -0700 | [diff] [blame] | 256 | } |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 257 | #endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE |
| 258 | RTC_CHECK(adm()); |
| 259 | webrtc::adm_helpers::Init(adm()); |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 260 | webrtc::apm_helpers::Init(apm()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 261 | |
| 262 | // Set up AudioState. |
| 263 | { |
| 264 | webrtc::AudioState::Config config; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 265 | if (audio_mixer_) { |
| 266 | config.audio_mixer = audio_mixer_; |
| 267 | } else { |
| 268 | config.audio_mixer = webrtc::AudioMixerImpl::Create(); |
| 269 | } |
| 270 | config.audio_processing = apm_; |
| 271 | config.audio_device_module = adm_; |
| 272 | audio_state_ = webrtc::AudioState::Create(config); |
| 273 | } |
| 274 | |
| 275 | // Connect the ADM to our audio path. |
| 276 | adm()->RegisterAudioCallback(audio_state()->audio_transport()); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 277 | |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 278 | // Save the default AGC configuration settings. This must happen before |
solenberg | 246b817 | 2015-12-08 09:50:23 -0800 | [diff] [blame] | 279 | // calling ApplyOptions or the default will be overwritten. |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 280 | default_agc_config_ = webrtc::apm_helpers::GetAgcConfig(apm()); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 281 | |
solenberg | 0f7d293 | 2016-01-15 01:40:39 -0800 | [diff] [blame] | 282 | // Set default engine options. |
| 283 | { |
| 284 | AudioOptions options; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 285 | options.echo_cancellation = true; |
| 286 | options.auto_gain_control = true; |
| 287 | options.noise_suppression = true; |
| 288 | options.highpass_filter = true; |
| 289 | options.stereo_swapping = false; |
| 290 | options.audio_jitter_buffer_max_packets = 50; |
| 291 | options.audio_jitter_buffer_fast_accelerate = false; |
| 292 | options.typing_detection = true; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 293 | options.experimental_agc = false; |
| 294 | options.extended_filter_aec = false; |
| 295 | options.delay_agnostic_aec = false; |
| 296 | options.experimental_ns = false; |
| 297 | options.intelligibility_enhancer = false; |
| 298 | options.level_control = false; |
| 299 | options.residual_echo_detector = true; |
solenberg | ff97631 | 2016-03-30 23:28:51 -0700 | [diff] [blame] | 300 | bool error = ApplyOptions(options); |
| 301 | RTC_DCHECK(error); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 302 | } |
| 303 | |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 304 | initialized_ = true; |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 305 | } |
| 306 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 307 | rtc::scoped_refptr<webrtc::AudioState> |
| 308 | WebRtcVoiceEngine::GetAudioState() const { |
| 309 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 310 | return audio_state_; |
| 311 | } |
| 312 | |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 313 | VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel( |
| 314 | webrtc::Call* call, |
| 315 | const MediaConfig& config, |
Jelena Marusic | c28a896 | 2015-05-29 15:05:44 +0200 | [diff] [blame] | 316 | const AudioOptions& options) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 317 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 318 | return new WebRtcVoiceMediaChannel(this, config, options, call); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 319 | } |
| 320 | |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 321 | bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 322 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 323 | RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " |
| 324 | << options_in.ToString(); |
solenberg | 0f7d293 | 2016-01-15 01:40:39 -0800 | [diff] [blame] | 325 | AudioOptions options = options_in; // The options are modified below. |
solenberg | 246b817 | 2015-12-08 09:50:23 -0800 | [diff] [blame] | 326 | |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 327 | // Set and adjust echo canceller options. |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 328 | // kEcConference is AEC with high suppression. |
| 329 | webrtc::EcModes ec_mode = webrtc::kEcConference; |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 330 | if (options.aecm_generate_comfort_noise) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 331 | RTC_LOG(LS_VERBOSE) << "Comfort noise explicitly set to " |
| 332 | << *options.aecm_generate_comfort_noise |
| 333 | << " (default is false)."; |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 334 | } |
| 335 | |
kjellander | fcfc804 | 2016-01-14 11:01:09 -0800 | [diff] [blame] | 336 | #if defined(WEBRTC_IOS) |
Jonathan Yu | 7622048 | 2017-12-21 04:18:07 -0800 | [diff] [blame] | 337 | if (options.ios_force_software_aec_HACK && |
| 338 | *options.ios_force_software_aec_HACK) { |
| 339 | // EC may be forced on for a device known to have non-functioning platform |
| 340 | // AEC. |
| 341 | options.echo_cancellation = true; |
| 342 | options.extended_filter_aec = true; |
| 343 | RTC_LOG(LS_WARNING) |
| 344 | << "Force software AEC on iOS. May conflict with platform AEC."; |
| 345 | } else { |
| 346 | // On iOS, VPIO provides built-in EC. |
| 347 | options.echo_cancellation = false; |
| 348 | options.extended_filter_aec = false; |
| 349 | RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead."; |
| 350 | } |
Mirko Bonadei | c8c71b9 | 2017-10-16 11:08:54 +0200 | [diff] [blame] | 351 | #elif defined(WEBRTC_ANDROID) |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 352 | ec_mode = webrtc::kEcAecm; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 353 | options.extended_filter_aec = false; |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 354 | #endif |
| 355 | |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 356 | // Delay Agnostic AEC automatically turns on EC if not set except on iOS |
| 357 | // where the feature is not supported. |
| 358 | bool use_delay_agnostic_aec = false; |
kjellander | fcfc804 | 2016-01-14 11:01:09 -0800 | [diff] [blame] | 359 | #if !defined(WEBRTC_IOS) |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 360 | if (options.delay_agnostic_aec) { |
| 361 | use_delay_agnostic_aec = *options.delay_agnostic_aec; |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 362 | if (use_delay_agnostic_aec) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 363 | options.echo_cancellation = true; |
| 364 | options.extended_filter_aec = true; |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 365 | ec_mode = webrtc::kEcConference; |
| 366 | } |
| 367 | } |
| 368 | #endif |
| 369 | |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 370 | // Set and adjust noise suppressor options. |
| 371 | #if defined(WEBRTC_IOS) |
| 372 | // On iOS, VPIO provides built-in NS. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 373 | options.noise_suppression = false; |
| 374 | options.typing_detection = false; |
| 375 | options.experimental_ns = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 376 | RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead."; |
Mirko Bonadei | c8c71b9 | 2017-10-16 11:08:54 +0200 | [diff] [blame] | 377 | #elif defined(WEBRTC_ANDROID) |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 378 | options.typing_detection = false; |
| 379 | options.experimental_ns = false; |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 380 | #endif |
| 381 | |
| 382 | // Set and adjust gain control options. |
| 383 | #if defined(WEBRTC_IOS) |
| 384 | // On iOS, VPIO provides built-in AGC. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 385 | options.auto_gain_control = false; |
| 386 | options.experimental_agc = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 387 | RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead."; |
Mirko Bonadei | c8c71b9 | 2017-10-16 11:08:54 +0200 | [diff] [blame] | 388 | #elif defined(WEBRTC_ANDROID) |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 389 | options.experimental_agc = false; |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 390 | #endif |
| 391 | |
| 392 | #if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID) |
Mirko Bonadei | c8c71b9 | 2017-10-16 11:08:54 +0200 | [diff] [blame] | 393 | // Turn off the gain control if specified by the field trial. |
| 394 | // The purpose of the field trial is to reduce the amount of resampling |
| 395 | // performed inside the audio processing module on mobile platforms by |
| 396 | // whenever possible turning off the fixed AGC mode and the high-pass filter. |
| 397 | // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181). |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 398 | if (webrtc::field_trial::IsEnabled( |
| 399 | "WebRTC-Audio-MinimizeResamplingOnMobile")) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 400 | options.auto_gain_control = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 401 | RTC_LOG(LS_INFO) << "Disable AGC according to field trial."; |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 402 | if (!(options.noise_suppression.value_or(false) || |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 403 | options.echo_cancellation.value_or(false))) { |
| 404 | // If possible, turn off the high-pass filter. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 405 | RTC_LOG(LS_INFO) |
| 406 | << "Disable high-pass filter in response to field trial."; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 407 | options.highpass_filter = false; |
peah | 8a8ebd9 | 2017-05-22 15:48:47 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | #endif |
| 411 | |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 412 | #if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0) |
| 413 | // Hardcode the intelligibility enhancer to be off. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 414 | options.intelligibility_enhancer = false; |
peah | 1bcfce5 | 2016-08-26 07:16:04 -0700 | [diff] [blame] | 415 | #endif |
| 416 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 417 | if (options.echo_cancellation) { |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 418 | // Check if platform supports built-in EC. Currently only supported on |
| 419 | // Android and in combination with Java based audio layer. |
| 420 | // TODO(henrika): investigate possibility to support built-in EC also |
| 421 | // in combination with Open SL ES audio. |
solenberg | 5b5129a | 2016-04-08 05:35:48 -0700 | [diff] [blame] | 422 | const bool built_in_aec = adm()->BuiltInAECIsAvailable(); |
Bjorn Volcker | 73f7210 | 2015-06-03 14:50:15 +0200 | [diff] [blame] | 423 | if (built_in_aec) { |
Bjorn Volcker | ccfc939 | 2015-05-07 07:43:17 +0200 | [diff] [blame] | 424 | // Built-in EC exists on this device and use_delay_agnostic_aec is not |
| 425 | // overriding it. Enable/Disable it according to the echo_cancellation |
| 426 | // audio option. |
Bjorn Volcker | 73f7210 | 2015-06-03 14:50:15 +0200 | [diff] [blame] | 427 | const bool enable_built_in_aec = |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 428 | *options.echo_cancellation && !use_delay_agnostic_aec; |
solenberg | 5b5129a | 2016-04-08 05:35:48 -0700 | [diff] [blame] | 429 | if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 && |
Bjorn Volcker | 73f7210 | 2015-06-03 14:50:15 +0200 | [diff] [blame] | 430 | enable_built_in_aec) { |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 431 | // Disable internal software EC if built-in EC is enabled, |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 432 | // i.e., replace the software EC with the built-in EC. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 433 | options.echo_cancellation = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 434 | RTC_LOG(LS_INFO) |
| 435 | << "Disabling EC since built-in EC will be used instead"; |
henrika@webrtc.org | a954c07 | 2014-12-09 16:22:09 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 438 | webrtc::apm_helpers::SetEcStatus( |
| 439 | apm(), *options.echo_cancellation, ec_mode); |
Mirko Bonadei | c8c71b9 | 2017-10-16 11:08:54 +0200 | [diff] [blame] | 440 | #if !defined(WEBRTC_ANDROID) |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 441 | webrtc::apm_helpers::SetEcMetricsStatus(apm(), *options.echo_cancellation); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 442 | #endif |
| 443 | if (ec_mode == webrtc::kEcAecm) { |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 444 | bool cn = options.aecm_generate_comfort_noise.value_or(false); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 445 | webrtc::apm_helpers::SetAecmMode(apm(), cn); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 449 | if (options.auto_gain_control) { |
peah | 72a5645 | 2016-08-22 12:08:55 -0700 | [diff] [blame] | 450 | bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable(); |
| 451 | if (built_in_agc_avaliable) { |
solenberg | 5b5129a | 2016-04-08 05:35:48 -0700 | [diff] [blame] | 452 | if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 && |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 453 | *options.auto_gain_control) { |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 454 | // Disable internal software AGC if built-in AGC is enabled, |
| 455 | // i.e., replace the software AGC with the built-in AGC. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 456 | options.auto_gain_control = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 457 | RTC_LOG(LS_INFO) |
| 458 | << "Disabling AGC since built-in AGC will be used instead"; |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 459 | } |
| 460 | } |
henrika | e26456a | 2017-12-13 14:08:48 +0100 | [diff] [blame] | 461 | webrtc::apm_helpers::SetAgcStatus(apm(), *options.auto_gain_control); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 462 | } |
| 463 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 464 | if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain || |
Steve Anton | 606a597 | 2017-12-07 14:31:01 -0800 | [diff] [blame] | 465 | options.tx_agc_limiter) { |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 466 | // Override default_agc_config_. Generally, an unset option means "leave |
| 467 | // the VoE bits alone" in this function, so we want whatever is set to be |
| 468 | // stored as the new "default". If we didn't, then setting e.g. |
| 469 | // tx_agc_target_dbov would reset digital compression gain and limiter |
| 470 | // settings. |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 471 | default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or( |
| 472 | default_agc_config_.targetLeveldBOv); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 473 | default_agc_config_.digitalCompressionGaindB = |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 474 | options.tx_agc_digital_compression_gain.value_or( |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 475 | default_agc_config_.digitalCompressionGaindB); |
| 476 | default_agc_config_.limiterEnable = |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 477 | options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable); |
Steve Anton | 606a597 | 2017-12-07 14:31:01 -0800 | [diff] [blame] | 478 | webrtc::apm_helpers::SetAgcConfig(apm(), default_agc_config_); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 481 | if (options.intelligibility_enhancer) { |
| 482 | intelligibility_enhancer_ = options.intelligibility_enhancer; |
| 483 | } |
| 484 | if (intelligibility_enhancer_ && *intelligibility_enhancer_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 485 | RTC_LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active."; |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 486 | options.noise_suppression = intelligibility_enhancer_; |
| 487 | } |
| 488 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 489 | if (options.noise_suppression) { |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 490 | if (adm()->BuiltInNSIsAvailable()) { |
| 491 | bool builtin_ns = |
| 492 | *options.noise_suppression && |
| 493 | !(intelligibility_enhancer_ && *intelligibility_enhancer_); |
| 494 | if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) { |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 495 | // Disable internal software NS if built-in NS is enabled, |
| 496 | // i.e., replace the software NS with the built-in NS. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 497 | options.noise_suppression = false; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 498 | RTC_LOG(LS_INFO) |
| 499 | << "Disabling NS since built-in NS will be used instead"; |
henrika | c14f5ff | 2015-09-23 14:08:33 +0200 | [diff] [blame] | 500 | } |
| 501 | } |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 502 | webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 503 | } |
| 504 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 505 | if (options.stereo_swapping) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 506 | RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 507 | audio_state()->SetStereoChannelSwapping(*options.stereo_swapping); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 508 | } |
| 509 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 510 | if (options.audio_jitter_buffer_max_packets) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 511 | RTC_LOG(LS_INFO) << "NetEq capacity is " |
| 512 | << *options.audio_jitter_buffer_max_packets; |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 513 | audio_jitter_buffer_max_packets_ = |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 514 | std::max(20, *options.audio_jitter_buffer_max_packets); |
Henrik Lundin | 64dad83 | 2015-05-11 12:44:23 +0200 | [diff] [blame] | 515 | } |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 516 | if (options.audio_jitter_buffer_fast_accelerate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 517 | RTC_LOG(LS_INFO) << "NetEq fast mode? " |
| 518 | << *options.audio_jitter_buffer_fast_accelerate; |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 519 | audio_jitter_buffer_fast_accelerate_ = |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 520 | *options.audio_jitter_buffer_fast_accelerate; |
Henrik Lundin | 5263b3c | 2015-06-01 10:29:41 +0200 | [diff] [blame] | 521 | } |
| 522 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 523 | if (options.typing_detection) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 524 | RTC_LOG(LS_INFO) << "Typing detection is enabled? " |
| 525 | << *options.typing_detection; |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 526 | webrtc::apm_helpers::SetTypingDetectionStatus( |
| 527 | apm(), *options.typing_detection); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 528 | } |
| 529 | |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 530 | webrtc::Config config; |
| 531 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 532 | if (options.delay_agnostic_aec) |
| 533 | delay_agnostic_aec_ = options.delay_agnostic_aec; |
| 534 | if (delay_agnostic_aec_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 535 | RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? " |
| 536 | << *delay_agnostic_aec_; |
henrik.lundin | 0f133b9 | 2015-07-02 00:17:55 -0700 | [diff] [blame] | 537 | config.Set<webrtc::DelayAgnostic>( |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 538 | new webrtc::DelayAgnostic(*delay_agnostic_aec_)); |
Bjorn Volcker | bf395c1 | 2015-03-25 22:45:56 +0100 | [diff] [blame] | 539 | } |
| 540 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 541 | if (options.extended_filter_aec) { |
| 542 | extended_filter_aec_ = options.extended_filter_aec; |
| 543 | } |
| 544 | if (extended_filter_aec_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 545 | RTC_LOG(LS_INFO) << "Extended filter aec is enabled? " |
| 546 | << *extended_filter_aec_; |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 547 | config.Set<webrtc::ExtendedFilter>( |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 548 | new webrtc::ExtendedFilter(*extended_filter_aec_)); |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 549 | } |
| 550 | |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 551 | if (options.experimental_ns) { |
| 552 | experimental_ns_ = options.experimental_ns; |
| 553 | } |
| 554 | if (experimental_ns_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 555 | RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_; |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 556 | config.Set<webrtc::ExperimentalNs>( |
kwiberg | 102c6a6 | 2015-10-30 02:47:38 -0700 | [diff] [blame] | 557 | new webrtc::ExperimentalNs(*experimental_ns_)); |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 558 | } |
buildbot@webrtc.org | 1f8a237 | 2014-08-28 10:52:44 +0000 | [diff] [blame] | 559 | |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 560 | if (intelligibility_enhancer_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 561 | RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? " |
| 562 | << *intelligibility_enhancer_; |
Alejandro Luebs | c9b0c26 | 2016-05-16 15:32:38 -0700 | [diff] [blame] | 563 | config.Set<webrtc::Intelligibility>( |
| 564 | new webrtc::Intelligibility(*intelligibility_enhancer_)); |
| 565 | } |
| 566 | |
peah | a3333bf | 2016-06-30 00:02:34 -0700 | [diff] [blame] | 567 | if (options.level_control) { |
| 568 | level_control_ = options.level_control; |
| 569 | } |
| 570 | |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 571 | webrtc::AudioProcessing::Config apm_config = apm()->GetConfig(); |
| 572 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 573 | RTC_LOG(LS_INFO) << "Level control: " |
| 574 | << (!!level_control_ ? *level_control_ : -1); |
peah | a3333bf | 2016-06-30 00:02:34 -0700 | [diff] [blame] | 575 | if (level_control_) { |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 576 | apm_config.level_controller.enabled = *level_control_; |
aleloi | e33c5d9 | 2016-10-20 01:53:27 -0700 | [diff] [blame] | 577 | if (options.level_control_initial_peak_level_dbfs) { |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 578 | apm_config.level_controller.initial_peak_level_dbfs = |
aleloi | e33c5d9 | 2016-10-20 01:53:27 -0700 | [diff] [blame] | 579 | *options.level_control_initial_peak_level_dbfs; |
| 580 | } |
peah | a3333bf | 2016-06-30 00:02:34 -0700 | [diff] [blame] | 581 | } |
| 582 | |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 583 | if (options.highpass_filter) { |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 584 | apm_config.high_pass_filter.enabled = *options.highpass_filter; |
peah | 8271d04 | 2016-11-22 07:24:52 -0800 | [diff] [blame] | 585 | } |
| 586 | |
ivoc | 4ca1869 | 2017-02-10 05:11:09 -0800 | [diff] [blame] | 587 | if (options.residual_echo_detector) { |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 588 | apm_config.residual_echo_detector.enabled = *options.residual_echo_detector; |
ivoc | 4ca1869 | 2017-02-10 05:11:09 -0800 | [diff] [blame] | 589 | } |
| 590 | |
solenberg | 059fb44 | 2016-10-26 05:12:24 -0700 | [diff] [blame] | 591 | apm()->SetExtraOptions(config); |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 592 | apm()->ApplyConfig(apm_config); |
buildbot@webrtc.org | 13d6776 | 2014-05-02 17:33:29 +0000 | [diff] [blame] | 593 | return true; |
| 594 | } |
| 595 | |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 596 | // TODO(solenberg): Remove, once AudioMonitor is gone. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 597 | int WebRtcVoiceEngine::GetInputLevel() { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 598 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 599 | return audio_state()->GetAudioInputStats().quantized_audio_level; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 600 | } |
| 601 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 602 | const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const { |
| 603 | RTC_DCHECK(signal_thread_checker_.CalledOnValidThread()); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 604 | return send_codecs_; |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 608 | RTC_DCHECK(signal_thread_checker_.CalledOnValidThread()); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 609 | return recv_codecs_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 612 | RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 613 | RTC_DCHECK(signal_thread_checker_.CalledOnValidThread()); |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 614 | RtpCapabilities capabilities; |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 615 | capabilities.header_extensions.push_back( |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 616 | webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri, |
| 617 | webrtc::RtpExtension::kAudioLevelDefaultId)); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 618 | if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 619 | capabilities.header_extensions.push_back(webrtc::RtpExtension( |
| 620 | webrtc::RtpExtension::kTransportSequenceNumberUri, |
| 621 | webrtc::RtpExtension::kTransportSequenceNumberDefaultId)); |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 622 | } |
Stefan Holmer | 9d69c3f | 2015-12-07 10:45:43 +0100 | [diff] [blame] | 623 | return capabilities; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 624 | } |
| 625 | |
solenberg | 63b3454 | 2015-09-29 06:06:31 -0700 | [diff] [blame] | 626 | void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 627 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 628 | RTC_DCHECK(channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 629 | channels_.push_back(channel); |
| 630 | } |
| 631 | |
solenberg | 63b3454 | 2015-09-29 06:06:31 -0700 | [diff] [blame] | 632 | void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 633 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 63b3454 | 2015-09-29 06:06:31 -0700 | [diff] [blame] | 634 | auto it = std::find(channels_.begin(), channels_.end(), channel); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 635 | RTC_DCHECK(it != channels_.end()); |
| 636 | channels_.erase(it); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 637 | } |
| 638 | |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 639 | bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file, |
| 640 | int64_t max_size_bytes) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 641 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 642 | auto aec_dump = webrtc::AecDumpFactory::Create( |
| 643 | file, max_size_bytes, low_priority_worker_queue_.get()); |
aleloi | 048cbdd | 2017-05-29 02:56:27 -0700 | [diff] [blame] | 644 | if (!aec_dump) { |
wu@webrtc.org | a8910d2 | 2014-01-23 22:12:45 +0000 | [diff] [blame] | 645 | return false; |
| 646 | } |
aleloi | 048cbdd | 2017-05-29 02:56:27 -0700 | [diff] [blame] | 647 | apm()->AttachAecDump(std::move(aec_dump)); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 648 | return true; |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 649 | } |
| 650 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 651 | void WebRtcVoiceEngine::StartAecDump(const std::string& filename) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 652 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
aleloi | 048cbdd | 2017-05-29 02:56:27 -0700 | [diff] [blame] | 653 | |
deadbeef | eb02c03 | 2017-06-15 08:29:25 -0700 | [diff] [blame] | 654 | auto aec_dump = webrtc::AecDumpFactory::Create( |
| 655 | filename, -1, low_priority_worker_queue_.get()); |
aleloi | 048cbdd | 2017-05-29 02:56:27 -0700 | [diff] [blame] | 656 | if (aec_dump) { |
| 657 | apm()->AttachAecDump(std::move(aec_dump)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | void WebRtcVoiceEngine::StopAecDump() { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 662 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
aleloi | 048cbdd | 2017-05-29 02:56:27 -0700 | [diff] [blame] | 663 | apm()->DetachAecDump(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | } |
| 665 | |
solenberg | 5b5129a | 2016-04-08 05:35:48 -0700 | [diff] [blame] | 666 | webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() { |
| 667 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 668 | RTC_DCHECK(adm_); |
Fredrik Solenberg | d319534 | 2017-11-21 20:33:05 +0100 | [diff] [blame] | 669 | return adm_.get(); |
solenberg | 5b5129a | 2016-04-08 05:35:48 -0700 | [diff] [blame] | 670 | } |
| 671 | |
peah | b1c9d1d | 2017-07-25 15:45:24 -0700 | [diff] [blame] | 672 | webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const { |
solenberg | 059fb44 | 2016-10-26 05:12:24 -0700 | [diff] [blame] | 673 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 674 | RTC_DCHECK(apm_); |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 675 | return apm_.get(); |
solenberg | 059fb44 | 2016-10-26 05:12:24 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 678 | webrtc::AudioState* WebRtcVoiceEngine::audio_state() { |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 679 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 680 | RTC_DCHECK(audio_state_); |
| 681 | return audio_state_.get(); |
solenberg | 76377c5 | 2017-02-21 00:54:31 -0800 | [diff] [blame] | 682 | } |
| 683 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 684 | AudioCodecs WebRtcVoiceEngine::CollectCodecs( |
| 685 | const std::vector<webrtc::AudioCodecSpec>& specs) const { |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 686 | PayloadTypeMapper mapper; |
| 687 | AudioCodecs out; |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 688 | |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 689 | // Only generate CN payload types for these clockrates: |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 690 | std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false }, |
| 691 | { 16000, false }, |
| 692 | { 32000, false }}; |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 693 | // Only generate telephone-event payload types for these clockrates: |
| 694 | std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false }, |
| 695 | { 16000, false }, |
| 696 | { 32000, false }, |
| 697 | { 48000, false }}; |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 698 | |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 699 | auto map_format = [&mapper](const webrtc::SdpAudioFormat& format, |
| 700 | AudioCodecs* out) { |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 701 | rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format); |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 702 | if (opt_codec) { |
| 703 | if (out) { |
| 704 | out->push_back(*opt_codec); |
| 705 | } |
| 706 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 707 | RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: " |
| 708 | << format; |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 709 | } |
| 710 | |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 711 | return opt_codec; |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 712 | }; |
| 713 | |
ossu | d4e9f62 | 2016-08-18 02:01:17 -0700 | [diff] [blame] | 714 | for (const auto& spec : specs) { |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 715 | // We need to do some extra stuff before adding the main codecs to out. |
| 716 | rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr); |
| 717 | if (opt_codec) { |
| 718 | AudioCodec& codec = *opt_codec; |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 719 | if (spec.info.supports_network_adaption) { |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 720 | codec.AddFeedbackParam( |
| 721 | FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty)); |
| 722 | } |
| 723 | |
ossu | a1a040a | 2017-04-06 10:03:21 -0700 | [diff] [blame] | 724 | if (spec.info.allow_comfort_noise) { |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 725 | // Generate a CN entry if the decoder allows it and we support the |
| 726 | // clockrate. |
| 727 | auto cn = generate_cn.find(spec.format.clockrate_hz); |
| 728 | if (cn != generate_cn.end()) { |
| 729 | cn->second = true; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | // Generate a telephone-event entry if we support the clockrate. |
| 734 | auto dtmf = generate_dtmf.find(spec.format.clockrate_hz); |
| 735 | if (dtmf != generate_dtmf.end()) { |
| 736 | dtmf->second = true; |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 737 | } |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 738 | |
| 739 | out.push_back(codec); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 743 | // Add CN codecs after "proper" audio codecs. |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 744 | for (const auto& cn : generate_cn) { |
| 745 | if (cn.second) { |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 746 | map_format({kCnCodecName, cn.first, 1}, &out); |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 750 | // Add telephone-event codecs last. |
| 751 | for (const auto& dtmf : generate_dtmf) { |
| 752 | if (dtmf.second) { |
ossu | 9def800 | 2017-02-09 05:14:32 -0800 | [diff] [blame] | 753 | map_format({kDtmfCodecName, dtmf.first, 1}, &out); |
solenberg | 2779bab | 2016-11-17 04:45:19 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
ossu | c54071d | 2016-08-17 02:45:41 -0700 | [diff] [blame] | 756 | |
| 757 | return out; |
| 758 | } |
| 759 | |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 760 | class WebRtcVoiceMediaChannel::WebRtcAudioSendStream |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 761 | : public AudioSource::Sink { |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 762 | public: |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 763 | WebRtcAudioSendStream( |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 764 | uint32_t ssrc, |
| 765 | const std::string& c_name, |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 766 | const std::string track_id, |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 767 | const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>& |
| 768 | send_codec_spec, |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 769 | const std::vector<webrtc::RtpExtension>& extensions, |
| 770 | int max_send_bitrate_bps, |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 771 | const rtc::Optional<std::string>& audio_network_adaptor_config, |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 772 | webrtc::Call* call, |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 773 | webrtc::Transport* send_transport, |
| 774 | const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory) |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 775 | : call_(call), |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 776 | config_(send_transport), |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 777 | send_side_bwe_with_overhead_( |
| 778 | webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")), |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 779 | max_send_bitrate_bps_(max_send_bitrate_bps), |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 780 | rtp_parameters_(CreateRtpParametersWithOneEncoding()) { |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 781 | RTC_DCHECK(call); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 782 | RTC_DCHECK(encoder_factory); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 783 | config_.rtp.ssrc = ssrc; |
| 784 | config_.rtp.c_name = c_name; |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 785 | config_.rtp.extensions = extensions; |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 786 | config_.audio_network_adaptor_config = audio_network_adaptor_config; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 787 | config_.encoder_factory = encoder_factory; |
Alex Narest | b3944f0 | 2017-10-13 14:56:18 +0200 | [diff] [blame] | 788 | config_.track_id = track_id; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 789 | rtp_parameters_.encodings[0].ssrc = ssrc; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 790 | |
| 791 | if (send_codec_spec) { |
| 792 | UpdateSendCodecSpec(*send_codec_spec); |
| 793 | } |
| 794 | |
| 795 | stream_ = call_->CreateAudioSendStream(config_); |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 796 | } |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 797 | |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 798 | ~WebRtcAudioSendStream() override { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 799 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 800 | ClearSource(); |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 801 | call_->DestroyAudioSendStream(stream_); |
| 802 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 803 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 804 | void SetSendCodecSpec( |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 805 | const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 806 | UpdateSendCodecSpec(send_codec_spec); |
| 807 | ReconfigureAudioSendStream(); |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 808 | } |
| 809 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 810 | void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) { |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 811 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 812 | config_.rtp.extensions = extensions; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 813 | ReconfigureAudioSendStream(); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 814 | } |
| 815 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 816 | void SetAudioNetworkAdaptorConfig( |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 817 | const rtc::Optional<std::string>& audio_network_adaptor_config) { |
| 818 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 819 | if (config_.audio_network_adaptor_config == audio_network_adaptor_config) { |
| 820 | return; |
| 821 | } |
| 822 | config_.audio_network_adaptor_config = audio_network_adaptor_config; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 823 | UpdateAllowedBitrateRange(); |
| 824 | ReconfigureAudioSendStream(); |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 825 | } |
| 826 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 827 | bool SetMaxSendBitrate(int bps) { |
| 828 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 829 | RTC_DCHECK(config_.send_codec_spec); |
| 830 | RTC_DCHECK(audio_codec_spec_); |
| 831 | auto send_rate = ComputeSendBitrate( |
| 832 | bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_); |
| 833 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 834 | if (!send_rate) { |
| 835 | return false; |
| 836 | } |
| 837 | |
| 838 | max_send_bitrate_bps_ = bps; |
| 839 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 840 | if (send_rate != config_.send_codec_spec->target_bitrate_bps) { |
| 841 | config_.send_codec_spec->target_bitrate_bps = send_rate; |
| 842 | ReconfigureAudioSendStream(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 843 | } |
| 844 | return true; |
| 845 | } |
| 846 | |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 847 | bool SendTelephoneEvent(int payload_type, int payload_freq, int event, |
| 848 | int duration_ms) { |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 849 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 850 | RTC_DCHECK(stream_); |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 851 | return stream_->SendTelephoneEvent(payload_type, payload_freq, event, |
| 852 | duration_ms); |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 855 | void SetSend(bool send) { |
| 856 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 857 | send_ = send; |
| 858 | UpdateSendState(); |
| 859 | } |
| 860 | |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 861 | void SetMuted(bool muted) { |
| 862 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 863 | RTC_DCHECK(stream_); |
| 864 | stream_->SetMuted(muted); |
| 865 | muted_ = muted; |
| 866 | } |
| 867 | |
| 868 | bool muted() const { |
| 869 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 870 | return muted_; |
| 871 | } |
| 872 | |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 873 | webrtc::AudioSendStream::Stats GetStats(bool has_remote_tracks) const { |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 874 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 875 | RTC_DCHECK(stream_); |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 876 | return stream_->GetStats(has_remote_tracks); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 879 | // Starts the sending by setting ourselves as a sink to the AudioSource to |
| 880 | // get data callbacks. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 881 | // This method is called on the libjingle worker thread. |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 882 | // TODO(xians): Make sure Start() is called only once. |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 883 | void SetSource(AudioSource* source) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 884 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 885 | RTC_DCHECK(source); |
| 886 | if (source_) { |
| 887 | RTC_DCHECK(source_ == source); |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 888 | return; |
| 889 | } |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 890 | source->SetSink(this); |
| 891 | source_ = source; |
| 892 | UpdateSendState(); |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 895 | // Stops sending by setting the sink of the AudioSource to nullptr. No data |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 896 | // callback will be received after this method. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 897 | // This method is called on the libjingle worker thread. |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 898 | void ClearSource() { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 899 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 900 | if (source_) { |
| 901 | source_->SetSink(nullptr); |
| 902 | source_ = nullptr; |
solenberg | 98c6886 | 2015-10-09 03:27:14 -0700 | [diff] [blame] | 903 | } |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 904 | UpdateSendState(); |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 907 | // AudioSource::Sink implementation. |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 908 | // This method is called on the audio thread. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 909 | void OnData(const void* audio_data, |
| 910 | int bits_per_sample, |
| 911 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 912 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 913 | size_t number_of_frames) override { |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 914 | RTC_DCHECK_EQ(16, bits_per_sample); |
solenberg | 347ec5c | 2016-09-23 04:21:47 -0700 | [diff] [blame] | 915 | RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 916 | RTC_DCHECK(stream_); |
| 917 | std::unique_ptr<webrtc::AudioFrame> audio_frame(new webrtc::AudioFrame()); |
| 918 | audio_frame->UpdateFrame(audio_frame->timestamp_, |
| 919 | static_cast<const int16_t*>(audio_data), |
| 920 | number_of_frames, |
| 921 | sample_rate, |
| 922 | audio_frame->speech_type_, |
| 923 | audio_frame->vad_activity_, |
| 924 | number_of_channels); |
| 925 | stream_->SendAudioData(std::move(audio_frame)); |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 928 | // Callback from the |source_| when it is going away. In case Start() has |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 929 | // never been called, this callback won't be triggered. |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 930 | void OnClose() override { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 931 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 932 | // Set |source_| to nullptr to make sure no more callback will get into |
| 933 | // the source. |
| 934 | source_ = nullptr; |
| 935 | UpdateSendState(); |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 936 | } |
| 937 | |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 938 | const webrtc::RtpParameters& rtp_parameters() const { |
| 939 | return rtp_parameters_; |
| 940 | } |
| 941 | |
deadbeef | fb2aced | 2017-01-06 23:05:37 -0800 | [diff] [blame] | 942 | bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) { |
| 943 | if (rtp_parameters.encodings.size() != 1) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 944 | RTC_LOG(LS_ERROR) |
deadbeef | fb2aced | 2017-01-06 23:05:37 -0800 | [diff] [blame] | 945 | << "Attempted to set RtpParameters without exactly one encoding"; |
| 946 | return false; |
| 947 | } |
| 948 | if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 949 | RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC"; |
deadbeef | fb2aced | 2017-01-06 23:05:37 -0800 | [diff] [blame] | 950 | return false; |
| 951 | } |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 952 | if (rtp_parameters.encodings[0].bitrate_priority <= 0) { |
| 953 | RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters bitrate_priority to " |
| 954 | "an invalid number."; |
| 955 | return false; |
| 956 | } |
deadbeef | fb2aced | 2017-01-06 23:05:37 -0800 | [diff] [blame] | 957 | return true; |
| 958 | } |
| 959 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 960 | bool SetRtpParameters(const webrtc::RtpParameters& parameters) { |
deadbeef | fb2aced | 2017-01-06 23:05:37 -0800 | [diff] [blame] | 961 | if (!ValidateRtpParameters(parameters)) { |
| 962 | return false; |
| 963 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 964 | |
| 965 | rtc::Optional<int> send_rate; |
| 966 | if (audio_codec_spec_) { |
| 967 | send_rate = ComputeSendBitrate(max_send_bitrate_bps_, |
| 968 | parameters.encodings[0].max_bitrate_bps, |
| 969 | *audio_codec_spec_); |
| 970 | if (!send_rate) { |
| 971 | return false; |
| 972 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 973 | } |
| 974 | |
minyue | cecec10 | 2017-03-27 13:04:25 -0700 | [diff] [blame] | 975 | const rtc::Optional<int> old_rtp_max_bitrate = |
| 976 | rtp_parameters_.encodings[0].max_bitrate_bps; |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 977 | double old_priority = rtp_parameters_.encodings[0].bitrate_priority; |
Lu Liu | 8b77aea | 2017-12-20 23:48:03 +0000 | [diff] [blame] | 978 | rtp_parameters_ = parameters; |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 979 | config_.bitrate_priority = rtp_parameters_.encodings[0].bitrate_priority; |
Lu Liu | 8b77aea | 2017-12-20 23:48:03 +0000 | [diff] [blame] | 980 | |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 981 | bool reconfigure_send_stream = |
| 982 | (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) || |
| 983 | (rtp_parameters_.encodings[0].bitrate_priority != old_priority); |
minyue | cecec10 | 2017-03-27 13:04:25 -0700 | [diff] [blame] | 984 | if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) { |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 985 | // Update the bitrate range. |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 986 | if (send_rate) { |
| 987 | config_.send_codec_spec->target_bitrate_bps = send_rate; |
| 988 | } |
| 989 | UpdateAllowedBitrateRange(); |
Seth Hampson | d2b912a | 2017-12-20 11:56:37 -0800 | [diff] [blame] | 990 | } |
Seth Hampson | 24722b3 | 2017-12-22 09:36:42 -0800 | [diff] [blame] | 991 | if (reconfigure_send_stream) { |
| 992 | ReconfigureAudioSendStream(); |
| 993 | } |
| 994 | // parameters.encodings[0].active could have changed. |
| 995 | UpdateSendState(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 996 | return true; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 997 | } |
| 998 | |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 999 | private: |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1000 | void UpdateSendState() { |
| 1001 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1002 | RTC_DCHECK(stream_); |
Taylor Brandstetter | 55dd708 | 2016-05-03 13:50:11 -0700 | [diff] [blame] | 1003 | RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size()); |
| 1004 | if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) { |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1005 | stream_->Start(); |
| 1006 | } else { // !send || source_ = nullptr |
| 1007 | stream_->Stop(); |
| 1008 | } |
| 1009 | } |
| 1010 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1011 | void UpdateAllowedBitrateRange() { |
michaelt | 53fe19d | 2016-10-18 09:39:22 -0700 | [diff] [blame] | 1012 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1013 | const bool is_opus = |
| 1014 | config_.send_codec_spec && |
| 1015 | !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(), |
| 1016 | kOpusCodecName); |
| 1017 | if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) { |
stefan | e9f36d5 | 2017-01-24 08:18:45 -0800 | [diff] [blame] | 1018 | config_.min_bitrate_bps = kOpusMinBitrateBps; |
minyue | cecec10 | 2017-03-27 13:04:25 -0700 | [diff] [blame] | 1019 | |
| 1020 | // This means that when RtpParameters is reset, we may change the |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1021 | // encoder's bit rate immediately (through ReconfigureAudioSendStream()), |
minyue | cecec10 | 2017-03-27 13:04:25 -0700 | [diff] [blame] | 1022 | // meanwhile change the cap to the output of BWE. |
| 1023 | config_.max_bitrate_bps = |
| 1024 | rtp_parameters_.encodings[0].max_bitrate_bps |
| 1025 | ? *rtp_parameters_.encodings[0].max_bitrate_bps |
| 1026 | : kOpusBitrateFbBps; |
| 1027 | |
michaelt | 53fe19d | 2016-10-18 09:39:22 -0700 | [diff] [blame] | 1028 | // TODO(mflodman): Keep testing this and set proper values. |
| 1029 | // Note: This is an early experiment currently only supported by Opus. |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 1030 | if (send_side_bwe_with_overhead_) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1031 | const int max_packet_size_ms = |
| 1032 | WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60; |
michaelt | 6672b26 | 2017-01-11 10:17:59 -0800 | [diff] [blame] | 1033 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1034 | // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12) |
| 1035 | constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12; |
michaelt | 6672b26 | 2017-01-11 10:17:59 -0800 | [diff] [blame] | 1036 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1037 | int min_overhead_bps = |
| 1038 | kOverheadPerPacket * 8 * 1000 / max_packet_size_ms; |
michaelt | 6672b26 | 2017-01-11 10:17:59 -0800 | [diff] [blame] | 1039 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1040 | // We assume that |config_.max_bitrate_bps| before the next line is |
| 1041 | // a hard limit on the payload bitrate, so we add min_overhead_bps to |
| 1042 | // it to ensure that, when overhead is deducted, the payload rate |
| 1043 | // never goes beyond the limit. |
| 1044 | // Note: this also means that if a higher overhead is forced, we |
| 1045 | // cannot reach the limit. |
| 1046 | // TODO(minyue): Reconsider this when the signaling to BWE is done |
| 1047 | // through a dedicated API. |
| 1048 | config_.max_bitrate_bps += min_overhead_bps; |
michaelt | 6672b26 | 2017-01-11 10:17:59 -0800 | [diff] [blame] | 1049 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1050 | // In contrast to max_bitrate_bps, we let min_bitrate_bps always be |
| 1051 | // reachable. |
| 1052 | config_.min_bitrate_bps += min_overhead_bps; |
michaelt | 6672b26 | 2017-01-11 10:17:59 -0800 | [diff] [blame] | 1053 | } |
michaelt | 53fe19d | 2016-10-18 09:39:22 -0700 | [diff] [blame] | 1054 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | void UpdateSendCodecSpec( |
| 1058 | const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) { |
| 1059 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1060 | config_.rtp.nack.rtp_history_ms = |
| 1061 | send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0; |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1062 | config_.send_codec_spec = send_codec_spec; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1063 | auto info = |
| 1064 | config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format); |
| 1065 | RTC_DCHECK(info); |
| 1066 | // If a specific target bitrate has been set for the stream, use that as |
| 1067 | // the new default bitrate when computing send bitrate. |
| 1068 | if (send_codec_spec.target_bitrate_bps) { |
| 1069 | info->default_bitrate_bps = std::max( |
| 1070 | info->min_bitrate_bps, |
| 1071 | std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps)); |
| 1072 | } |
| 1073 | |
| 1074 | audio_codec_spec_.emplace( |
| 1075 | webrtc::AudioCodecSpec{send_codec_spec.format, *info}); |
| 1076 | |
| 1077 | config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate( |
| 1078 | max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps, |
| 1079 | *audio_codec_spec_); |
| 1080 | |
| 1081 | UpdateAllowedBitrateRange(); |
| 1082 | } |
| 1083 | |
| 1084 | void ReconfigureAudioSendStream() { |
| 1085 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1086 | RTC_DCHECK(stream_); |
| 1087 | stream_->Reconfigure(config_); |
michaelt | 53fe19d | 2016-10-18 09:39:22 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1090 | rtc::ThreadChecker worker_thread_checker_; |
solenberg | 347ec5c | 2016-09-23 04:21:47 -0700 | [diff] [blame] | 1091 | rtc::RaceChecker audio_capture_race_checker_; |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1092 | webrtc::Call* call_ = nullptr; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1093 | webrtc::AudioSendStream::Config config_; |
elad.alon | 0fe1216 | 2017-01-31 05:48:37 -0800 | [diff] [blame] | 1094 | const bool send_side_bwe_with_overhead_; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1095 | // The stream is owned by WebRtcAudioSendStream and may be reallocated if |
| 1096 | // configuration changes. |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1097 | webrtc::AudioSendStream* stream_ = nullptr; |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 1098 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1099 | // Raw pointer to AudioSource owned by LocalAudioTrackHandler. |
mallinath@webrtc.org | 67ee6b9 | 2014-02-03 16:57:16 +0000 | [diff] [blame] | 1100 | // PeerConnection will make sure invalidating the pointer before the object |
| 1101 | // goes away. |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1102 | AudioSource* source_ = nullptr; |
| 1103 | bool send_ = false; |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 1104 | bool muted_ = false; |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 1105 | int max_send_bitrate_bps_; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1106 | webrtc::RtpParameters rtp_parameters_; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1107 | rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_; |
henrike@webrtc.org | a7b9818 | 2014-02-21 15:51:43 +0000 | [diff] [blame] | 1108 | |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1109 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream); |
| 1110 | }; |
| 1111 | |
| 1112 | class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream { |
| 1113 | public: |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 1114 | WebRtcAudioReceiveStream( |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 1115 | uint32_t remote_ssrc, |
| 1116 | uint32_t local_ssrc, |
| 1117 | bool use_transport_cc, |
solenberg | 8189b02 | 2016-06-14 12:13:00 -0700 | [diff] [blame] | 1118 | bool use_nack, |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 1119 | const std::string& sync_group, |
| 1120 | const std::vector<webrtc::RtpExtension>& extensions, |
| 1121 | webrtc::Call* call, |
| 1122 | webrtc::Transport* rtcp_send_transport, |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 1123 | const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1124 | const std::map<int, webrtc::SdpAudioFormat>& decoder_map, |
| 1125 | size_t jitter_buffer_max_packets, |
| 1126 | bool jitter_buffer_fast_accelerate) |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 1127 | : call_(call), config_() { |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1128 | RTC_DCHECK(call); |
| 1129 | config_.rtp.remote_ssrc = remote_ssrc; |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1130 | config_.rtp.local_ssrc = local_ssrc; |
| 1131 | config_.rtp.transport_cc = use_transport_cc; |
| 1132 | config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0; |
| 1133 | config_.rtp.extensions = extensions; |
solenberg | 31fec40 | 2016-05-06 02:13:12 -0700 | [diff] [blame] | 1134 | config_.rtcp_send_transport = rtcp_send_transport; |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1135 | config_.jitter_buffer_max_packets = jitter_buffer_max_packets; |
| 1136 | config_.jitter_buffer_fast_accelerate = jitter_buffer_fast_accelerate; |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1137 | config_.sync_group = sync_group; |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 1138 | config_.decoder_factory = decoder_factory; |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 1139 | config_.decoder_map = decoder_map; |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1140 | RecreateAudioReceiveStream(); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1141 | } |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1142 | |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1143 | ~WebRtcAudioReceiveStream() { |
| 1144 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1145 | call_->DestroyAudioReceiveStream(stream_); |
| 1146 | } |
| 1147 | |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1148 | void SetLocalSsrc(uint32_t local_ssrc) { |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1149 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1150 | config_.rtp.local_ssrc = local_ssrc; |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1151 | ReconfigureAudioReceiveStream(); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1152 | } |
solenberg | 8189b02 | 2016-06-14 12:13:00 -0700 | [diff] [blame] | 1153 | |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1154 | void SetUseTransportCc(bool use_transport_cc, bool use_nack) { |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1155 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1156 | config_.rtp.transport_cc = use_transport_cc; |
| 1157 | config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0; |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1158 | ReconfigureAudioReceiveStream(); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1161 | void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) { |
solenberg | 4a0f7b5 | 2016-06-16 13:07:33 -0700 | [diff] [blame] | 1162 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1163 | config_.rtp.extensions = extensions; |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1164 | ReconfigureAudioReceiveStream(); |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 1167 | // Set a new payload type -> decoder map. |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1168 | void SetDecoderMap(const std::map<int, webrtc::SdpAudioFormat>& decoder_map) { |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1169 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1170 | config_.decoder_map = decoder_map; |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1171 | ReconfigureAudioReceiveStream(); |
solenberg | 4a0f7b5 | 2016-06-16 13:07:33 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
solenberg | 4904fb6 | 2017-02-17 12:01:14 -0800 | [diff] [blame] | 1174 | void MaybeRecreateAudioReceiveStream(const std::string& sync_group) { |
| 1175 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1176 | if (config_.sync_group != sync_group) { |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1177 | RTC_LOG(LS_INFO) << "Recreating AudioReceiveStream for SSRC=" |
| 1178 | << config_.rtp.remote_ssrc |
| 1179 | << " because of sync group change."; |
solenberg | 4904fb6 | 2017-02-17 12:01:14 -0800 | [diff] [blame] | 1180 | config_.sync_group = sync_group; |
| 1181 | RecreateAudioReceiveStream(); |
| 1182 | } |
| 1183 | } |
| 1184 | |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1185 | webrtc::AudioReceiveStream::Stats GetStats() const { |
| 1186 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1187 | RTC_DCHECK(stream_); |
| 1188 | return stream_->GetStats(); |
| 1189 | } |
| 1190 | |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 1191 | int GetOutputLevel() const { |
| 1192 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1193 | RTC_DCHECK(stream_); |
| 1194 | return stream_->GetOutputLevel(); |
| 1195 | } |
| 1196 | |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 1197 | void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1198 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1199 | raw_audio_sink_ = std::move(sink); |
| 1200 | stream_->SetSink(raw_audio_sink_.get()); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1201 | } |
| 1202 | |
solenberg | 217fb66 | 2016-06-17 08:30:54 -0700 | [diff] [blame] | 1203 | void SetOutputVolume(double volume) { |
| 1204 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1205 | stream_->SetGain(volume); |
| 1206 | } |
| 1207 | |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1208 | void SetPlayout(bool playout) { |
| 1209 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1210 | RTC_DCHECK(stream_); |
| 1211 | if (playout) { |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1212 | stream_->Start(); |
| 1213 | } else { |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1214 | stream_->Stop(); |
| 1215 | } |
aleloi | 18e0b67 | 2016-10-04 02:45:47 -0700 | [diff] [blame] | 1216 | playout_ = playout; |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 1219 | std::vector<webrtc::RtpSource> GetSources() { |
| 1220 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1221 | RTC_DCHECK(stream_); |
| 1222 | return stream_->GetSources(); |
| 1223 | } |
| 1224 | |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1225 | private: |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1226 | void RecreateAudioReceiveStream() { |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1227 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1228 | if (stream_) { |
| 1229 | call_->DestroyAudioReceiveStream(stream_); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1230 | } |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1231 | stream_ = call_->CreateAudioReceiveStream(config_); |
| 1232 | RTC_CHECK(stream_); |
aleloi | 18e0b67 | 2016-10-04 02:45:47 -0700 | [diff] [blame] | 1233 | SetPlayout(playout_); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1234 | stream_->SetSink(raw_audio_sink_.get()); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1235 | } |
| 1236 | |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1237 | void ReconfigureAudioReceiveStream() { |
| 1238 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1239 | RTC_DCHECK(stream_); |
| 1240 | stream_->Reconfigure(config_); |
| 1241 | } |
| 1242 | |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1243 | rtc::ThreadChecker worker_thread_checker_; |
| 1244 | webrtc::Call* call_ = nullptr; |
| 1245 | webrtc::AudioReceiveStream::Config config_; |
| 1246 | // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if |
| 1247 | // configuration changes. |
| 1248 | webrtc::AudioReceiveStream* stream_ = nullptr; |
aleloi | 18e0b67 | 2016-10-04 02:45:47 -0700 | [diff] [blame] | 1249 | bool playout_ = false; |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1250 | std::unique_ptr<webrtc::AudioSinkInterface> raw_audio_sink_; |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1251 | |
| 1252 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1253 | }; |
| 1254 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 1255 | WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 1256 | const MediaConfig& config, |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 1257 | const AudioOptions& options, |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 1258 | webrtc::Call* call) |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 1259 | : VoiceMediaChannel(config), engine_(engine), call_(call) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1260 | RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel"; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1261 | RTC_DCHECK(call); |
solenberg | 0a617e2 | 2015-10-20 15:49:38 -0700 | [diff] [blame] | 1262 | engine->RegisterChannel(this); |
Fredrik Solenberg | b071a19 | 2015-09-17 16:42:56 +0200 | [diff] [blame] | 1263 | SetOptions(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1267 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1268 | RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel"; |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1269 | // TODO(solenberg): Should be able to delete the streams directly, without |
| 1270 | // going through RemoveNnStream(), once stream objects handle |
| 1271 | // all (de)configuration. |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1272 | while (!send_streams_.empty()) { |
| 1273 | RemoveSendStream(send_streams_.begin()->first); |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 1274 | } |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1275 | while (!recv_streams_.empty()) { |
| 1276 | RemoveRecvStream(recv_streams_.begin()->first); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1277 | } |
solenberg | 0a617e2 | 2015-10-20 15:49:38 -0700 | [diff] [blame] | 1278 | engine()->UnregisterChannel(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
nisse | 51542be | 2016-02-12 02:27:06 -0800 | [diff] [blame] | 1281 | rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const { |
| 1282 | return kAudioDscpValue; |
| 1283 | } |
| 1284 | |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1285 | bool WebRtcVoiceMediaChannel::SetSendParameters( |
| 1286 | const AudioSendParameters& params) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1287 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1288 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1289 | RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: " |
| 1290 | << params.ToString(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1291 | // TODO(pthatcher): Refactor this to be more clean now that we have |
| 1292 | // all the information at once. |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1293 | |
| 1294 | if (!SetSendCodecs(params.codecs)) { |
| 1295 | return false; |
| 1296 | } |
| 1297 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 1298 | if (!ValidateRtpExtensions(params.extensions)) { |
| 1299 | return false; |
| 1300 | } |
| 1301 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1302 | FilterRtpExtensions(params.extensions, |
| 1303 | webrtc::RtpExtension::IsSupportedForAudio, true); |
| 1304 | if (send_rtp_extensions_ != filtered_extensions) { |
| 1305 | send_rtp_extensions_.swap(filtered_extensions); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1306 | for (auto& it : send_streams_) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1307 | it.second->SetRtpExtensions(send_rtp_extensions_); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1308 | } |
| 1309 | } |
| 1310 | |
deadbeef | 8034614 | 2016-04-27 14:17:10 -0700 | [diff] [blame] | 1311 | if (!SetMaxSendBitrate(params.max_bandwidth_bps)) { |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1312 | return false; |
| 1313 | } |
| 1314 | return SetOptions(params.options); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | bool WebRtcVoiceMediaChannel::SetRecvParameters( |
| 1318 | const AudioRecvParameters& params) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1319 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1320 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1321 | RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: " |
| 1322 | << params.ToString(); |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1323 | // TODO(pthatcher): Refactor this to be more clean now that we have |
| 1324 | // all the information at once. |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1325 | |
| 1326 | if (!SetRecvCodecs(params.codecs)) { |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
solenberg | 7e4e01a | 2015-12-02 08:05:01 -0800 | [diff] [blame] | 1330 | if (!ValidateRtpExtensions(params.extensions)) { |
| 1331 | return false; |
| 1332 | } |
| 1333 | std::vector<webrtc::RtpExtension> filtered_extensions = |
| 1334 | FilterRtpExtensions(params.extensions, |
| 1335 | webrtc::RtpExtension::IsSupportedForAudio, false); |
| 1336 | if (recv_rtp_extensions_ != filtered_extensions) { |
| 1337 | recv_rtp_extensions_.swap(filtered_extensions); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1338 | for (auto& it : recv_streams_) { |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1339 | it.second->SetRtpExtensions(recv_rtp_extensions_); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1340 | } |
| 1341 | } |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1342 | return true; |
Peter Thatcher | c2ee2c8 | 2015-08-07 16:05:34 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1345 | webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters( |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1346 | uint32_t ssrc) const { |
| 1347 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 1348 | auto it = send_streams_.find(ssrc); |
| 1349 | if (it == send_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1350 | RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream " |
| 1351 | << "with ssrc " << ssrc << " which doesn't exist."; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1352 | return webrtc::RtpParameters(); |
| 1353 | } |
| 1354 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 1355 | webrtc::RtpParameters rtp_params = it->second->rtp_parameters(); |
| 1356 | // Need to add the common list of codecs to the send stream-specific |
| 1357 | // RTP parameters. |
| 1358 | for (const AudioCodec& codec : send_codecs_) { |
| 1359 | rtp_params.codecs.push_back(codec.ToCodecParameters()); |
| 1360 | } |
| 1361 | return rtp_params; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1364 | bool WebRtcVoiceMediaChannel::SetRtpSendParameters( |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1365 | uint32_t ssrc, |
| 1366 | const webrtc::RtpParameters& parameters) { |
| 1367 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1368 | auto it = send_streams_.find(ssrc); |
| 1369 | if (it == send_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1370 | RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream " |
| 1371 | << "with ssrc " << ssrc << " which doesn't exist."; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1372 | return false; |
| 1373 | } |
| 1374 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1375 | // TODO(deadbeef): Handle setting parameters with a list of codecs in a |
| 1376 | // different order (which should change the send codec). |
| 1377 | webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); |
| 1378 | if (current_parameters.codecs != parameters.codecs) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1379 | RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs " |
| 1380 | << "is not currently supported."; |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1381 | return false; |
| 1382 | } |
| 1383 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 1384 | // TODO(minyue): The following legacy actions go into |
| 1385 | // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end, |
| 1386 | // though there are two difference: |
| 1387 | // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls |
| 1388 | // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls |
| 1389 | // |SetSendCodecs|. The outcome should be the same. |
| 1390 | // 2. AudioSendStream can be recreated. |
| 1391 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 1392 | // Codecs are handled at the WebRtcVoiceMediaChannel level. |
| 1393 | webrtc::RtpParameters reduced_params = parameters; |
| 1394 | reduced_params.codecs.clear(); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 1395 | return it->second->SetRtpParameters(reduced_params); |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1398 | webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters( |
| 1399 | uint32_t ssrc) const { |
| 1400 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1401 | webrtc::RtpParameters rtp_params; |
| 1402 | // SSRC of 0 represents the default receive stream. |
| 1403 | if (ssrc == 0) { |
| 1404 | if (!default_sink_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1405 | RTC_LOG(LS_WARNING) |
| 1406 | << "Attempting to get RTP parameters for the default, " |
| 1407 | "unsignaled audio receive stream, but not yet " |
| 1408 | "configured to receive such a stream."; |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1409 | return rtp_params; |
| 1410 | } |
| 1411 | rtp_params.encodings.emplace_back(); |
| 1412 | } else { |
| 1413 | auto it = recv_streams_.find(ssrc); |
| 1414 | if (it == recv_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1415 | RTC_LOG(LS_WARNING) |
| 1416 | << "Attempting to get RTP receive parameters for stream " |
| 1417 | << "with ssrc " << ssrc << " which doesn't exist."; |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1418 | return webrtc::RtpParameters(); |
| 1419 | } |
| 1420 | rtp_params.encodings.emplace_back(); |
| 1421 | // TODO(deadbeef): Return stream-specific parameters. |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1422 | rtp_params.encodings[0].ssrc = ssrc; |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1425 | for (const AudioCodec& codec : recv_codecs_) { |
| 1426 | rtp_params.codecs.push_back(codec.ToCodecParameters()); |
| 1427 | } |
| 1428 | return rtp_params; |
| 1429 | } |
| 1430 | |
| 1431 | bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters( |
| 1432 | uint32_t ssrc, |
| 1433 | const webrtc::RtpParameters& parameters) { |
| 1434 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1435 | // SSRC of 0 represents the default receive stream. |
| 1436 | if (ssrc == 0) { |
| 1437 | if (!default_sink_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1438 | RTC_LOG(LS_WARNING) |
| 1439 | << "Attempting to set RTP parameters for the default, " |
| 1440 | "unsignaled audio receive stream, but not yet " |
| 1441 | "configured to receive such a stream."; |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
| 1444 | } else { |
| 1445 | auto it = recv_streams_.find(ssrc); |
| 1446 | if (it == recv_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1447 | RTC_LOG(LS_WARNING) |
| 1448 | << "Attempting to set RTP receive parameters for stream " |
| 1449 | << "with ssrc " << ssrc << " which doesn't exist."; |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1450 | return false; |
| 1451 | } |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc); |
| 1455 | if (current_parameters != parameters) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1456 | RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently " |
| 1457 | << "unsupported."; |
Taylor Brandstetter | db0cd9e | 2016-05-16 11:40:30 -0700 | [diff] [blame] | 1458 | return false; |
| 1459 | } |
| 1460 | return true; |
| 1461 | } |
| 1462 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1463 | bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1464 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1465 | RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1466 | |
| 1467 | // We retain all of the existing options, and apply the given ones |
| 1468 | // on top. This means there is no way to "clear" options such that |
| 1469 | // they go back to the engine default. |
| 1470 | options_.SetAll(options); |
solenberg | 246b817 | 2015-12-08 09:50:23 -0800 | [diff] [blame] | 1471 | if (!engine()->ApplyOptions(options_)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1472 | RTC_LOG(LS_WARNING) |
| 1473 | << "Failed to apply engine options during channel SetOptions."; |
solenberg | 246b817 | 2015-12-08 09:50:23 -0800 | [diff] [blame] | 1474 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1475 | } |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 1476 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1477 | rtc::Optional<std::string> audio_network_adaptor_config = |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 1478 | GetAudioNetworkAdaptorConfig(options_); |
| 1479 | for (auto& it : send_streams_) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1480 | it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config); |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 1481 | } |
| 1482 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1483 | RTC_LOG(LS_INFO) << "Set voice channel options. Current options: " |
| 1484 | << options_.ToString(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1485 | return true; |
| 1486 | } |
| 1487 | |
| 1488 | bool WebRtcVoiceMediaChannel::SetRecvCodecs( |
| 1489 | const std::vector<AudioCodec>& codecs) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1490 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 8fb30c3 | 2015-10-13 03:06:58 -0700 | [diff] [blame] | 1491 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1492 | // Set the payload types to be used for incoming media. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1493 | RTC_LOG(LS_INFO) << "Setting receive voice codecs."; |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 1494 | |
| 1495 | if (!VerifyUniquePayloadTypes(codecs)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1496 | RTC_LOG(LS_ERROR) << "Codec payload types overlap."; |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 1497 | return false; |
| 1498 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1499 | |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1500 | // Create a payload type -> SdpAudioFormat map with all the decoders. Fail |
| 1501 | // unless the factory claims to support all decoders. |
| 1502 | std::map<int, webrtc::SdpAudioFormat> decoder_map; |
| 1503 | for (const AudioCodec& codec : codecs) { |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 1504 | // Log a warning if a codec's payload type is changing. This used to be |
| 1505 | // treated as an error. It's abnormal, but not really illegal. |
| 1506 | AudioCodec old_codec; |
| 1507 | if (FindCodec(recv_codecs_, codec, &old_codec) && |
| 1508 | old_codec.id != codec.id) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1509 | RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type (" |
| 1510 | << codec.id << ", was already mapped to " |
| 1511 | << old_codec.id << ")"; |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 1512 | } |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1513 | auto format = AudioCodecToSdpAudioFormat(codec); |
| 1514 | if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") && |
| 1515 | !engine()->decoder_factory_->IsSupportedDecoder(format)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1516 | RTC_LOG(LS_ERROR) << "Unsupported codec: " << format; |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1517 | return false; |
| 1518 | } |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 1519 | // We allow adding new codecs but don't allow changing the payload type of |
| 1520 | // codecs that are already configured since we might already be receiving |
| 1521 | // packets with that payload type. See RFC3264, Section 8.3.2. |
| 1522 | // TODO(deadbeef): Also need to check for clashes with previously mapped |
| 1523 | // payload types, and not just currently mapped ones. For example, this |
| 1524 | // should be illegal: |
| 1525 | // 1. {100: opus/48000/2, 101: ISAC/16000} |
| 1526 | // 2. {100: opus/48000/2} |
| 1527 | // 3. {100: opus/48000/2, 101: ISAC/32000} |
| 1528 | // Though this check really should happen at a higher level, since this |
| 1529 | // conflict could happen between audio and video codecs. |
| 1530 | auto existing = decoder_map_.find(codec.id); |
| 1531 | if (existing != decoder_map_.end() && !existing->second.Matches(format)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1532 | RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id |
| 1533 | << " for " << codec.name |
| 1534 | << ", but it is already used for " |
| 1535 | << existing->second.name; |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 1536 | return false; |
| 1537 | } |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1538 | decoder_map.insert({codec.id, std::move(format)}); |
| 1539 | } |
| 1540 | |
deadbeef | cb38367 | 2017-04-26 16:28:42 -0700 | [diff] [blame] | 1541 | if (decoder_map == decoder_map_) { |
| 1542 | // There's nothing new to configure. |
| 1543 | return true; |
| 1544 | } |
| 1545 | |
kwiberg | 37b8b11 | 2016-11-03 02:46:53 -0700 | [diff] [blame] | 1546 | if (playout_) { |
| 1547 | // Receive codecs can not be changed while playing. So we temporarily |
| 1548 | // pause playout. |
| 1549 | ChangePlayout(false); |
| 1550 | } |
| 1551 | |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 1552 | decoder_map_ = std::move(decoder_map); |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1553 | for (auto& kv : recv_streams_) { |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1554 | kv.second->SetDecoderMap(decoder_map_); |
solenberg | 26c8c91 | 2015-11-27 04:00:25 -0800 | [diff] [blame] | 1555 | } |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1556 | recv_codecs_ = codecs; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1557 | |
kwiberg | 37b8b11 | 2016-11-03 02:46:53 -0700 | [diff] [blame] | 1558 | if (desired_playout_ && !playout_) { |
| 1559 | ChangePlayout(desired_playout_); |
| 1560 | } |
kwiberg | d32bf75 | 2017-01-19 07:03:59 -0800 | [diff] [blame] | 1561 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1564 | // Utility function called from SetSendParameters() to extract current send |
| 1565 | // codec settings from the given list of codecs (originally from SDP). Both send |
| 1566 | // and receive streams may be reconfigured based on the new settings. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1567 | bool WebRtcVoiceMediaChannel::SetSendCodecs( |
| 1568 | const std::vector<AudioCodec>& codecs) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1569 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1570 | dtmf_payload_type_ = rtc::nullopt; |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1571 | dtmf_payload_freq_ = -1; |
| 1572 | |
| 1573 | // Validate supplied codecs list. |
| 1574 | for (const AudioCodec& codec : codecs) { |
| 1575 | // TODO(solenberg): Validate more aspects of input - that payload types |
| 1576 | // don't overlap, remove redundant/unsupported codecs etc - |
| 1577 | // the same way it is done for RtpHeaderExtensions. |
| 1578 | if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1579 | RTC_LOG(LS_WARNING) << "Codec payload type out of range: " |
| 1580 | << ToString(codec); |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1581 | return false; |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | // Find PT of telephone-event codec with lowest clockrate, as a fallback, in |
| 1586 | // case we don't have a DTMF codec with a rate matching the send codec's, or |
| 1587 | // if this function returns early. |
| 1588 | std::vector<AudioCodec> dtmf_codecs; |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 1589 | for (const AudioCodec& codec : codecs) { |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 1590 | if (IsCodec(codec, kDtmfCodecName)) { |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1591 | dtmf_codecs.push_back(codec); |
| 1592 | if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1593 | dtmf_payload_type_ = codec.id; |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1594 | dtmf_payload_freq_ = codec.clockrate; |
solenberg | 31642aa | 2016-03-14 08:00:37 -0700 | [diff] [blame] | 1595 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1599 | // Scan through the list to figure out the codec to use for sending. |
| 1600 | rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec; |
stefan | 1ccf73f | 2017-03-27 03:51:18 -0700 | [diff] [blame] | 1601 | webrtc::Call::Config::BitrateConfig bitrate_config; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1602 | rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info; |
| 1603 | for (const AudioCodec& voice_codec : codecs) { |
| 1604 | if (!(IsCodec(voice_codec, kCnCodecName) || |
| 1605 | IsCodec(voice_codec, kDtmfCodecName) || |
| 1606 | IsCodec(voice_codec, kRedCodecName))) { |
| 1607 | webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate, |
| 1608 | voice_codec.channels, voice_codec.params); |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1609 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1610 | voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format); |
| 1611 | if (!voice_codec_info) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1612 | RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec); |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1613 | continue; |
| 1614 | } |
| 1615 | |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1616 | send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec( |
| 1617 | voice_codec.id, format); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1618 | if (voice_codec.bitrate > 0) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1619 | send_codec_spec->target_bitrate_bps = voice_codec.bitrate; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1620 | } |
| 1621 | send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec); |
| 1622 | send_codec_spec->nack_enabled = HasNack(voice_codec); |
| 1623 | bitrate_config = GetBitrateConfigForCodec(voice_codec); |
| 1624 | break; |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | if (!send_codec_spec) { |
| 1629 | return false; |
| 1630 | } |
| 1631 | |
| 1632 | RTC_DCHECK(voice_codec_info); |
| 1633 | if (voice_codec_info->allow_comfort_noise) { |
| 1634 | // Loop through the codecs list again to find the CN codec. |
| 1635 | // TODO(solenberg): Break out into a separate function? |
| 1636 | for (const AudioCodec& cn_codec : codecs) { |
ossu | 0c4b849 | 2017-03-02 11:03:25 -0800 | [diff] [blame] | 1637 | if (IsCodec(cn_codec, kCnCodecName) && |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1638 | cn_codec.clockrate == send_codec_spec->format.clockrate_hz) { |
ossu | 0c4b849 | 2017-03-02 11:03:25 -0800 | [diff] [blame] | 1639 | switch (cn_codec.clockrate) { |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1640 | case 8000: |
| 1641 | case 16000: |
| 1642 | case 32000: |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1643 | send_codec_spec->cng_payload_type = cn_codec.id; |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1644 | break; |
| 1645 | default: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1646 | RTC_LOG(LS_WARNING) |
| 1647 | << "CN frequency " << cn_codec.clockrate << " not supported."; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1648 | break; |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1649 | } |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1650 | break; |
| 1651 | } |
| 1652 | } |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1653 | |
| 1654 | // Find the telephone-event PT exactly matching the preferred send codec. |
| 1655 | for (const AudioCodec& dtmf_codec : dtmf_codecs) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1656 | if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) { |
Oskar Sundbom | 7880758 | 2017-11-16 11:09:55 +0100 | [diff] [blame] | 1657 | dtmf_payload_type_ = dtmf_codec.id; |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1658 | dtmf_payload_freq_ = dtmf_codec.clockrate; |
| 1659 | break; |
| 1660 | } |
| 1661 | } |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1662 | } |
| 1663 | |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 1664 | if (send_codec_spec_ != send_codec_spec) { |
| 1665 | send_codec_spec_ = std::move(send_codec_spec); |
stefan | 13f1a0a | 2016-11-30 07:22:58 -0800 | [diff] [blame] | 1666 | // Apply new settings to all streams. |
solenberg | 971cab0 | 2016-06-14 10:02:41 -0700 | [diff] [blame] | 1667 | for (const auto& kv : send_streams_) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1668 | kv.second->SetSendCodecSpec(*send_codec_spec_); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1669 | } |
stefan | 13f1a0a | 2016-11-30 07:22:58 -0800 | [diff] [blame] | 1670 | } else { |
| 1671 | // If the codec isn't changing, set the start bitrate to -1 which means |
| 1672 | // "unchanged" so that BWE isn't affected. |
stefan | 1ccf73f | 2017-03-27 03:51:18 -0700 | [diff] [blame] | 1673 | bitrate_config.start_bitrate_bps = -1; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1674 | } |
stefan | 1ccf73f | 2017-03-27 03:51:18 -0700 | [diff] [blame] | 1675 | call_->SetBitrateConfig(bitrate_config); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1676 | |
solenberg | 8189b02 | 2016-06-14 12:13:00 -0700 | [diff] [blame] | 1677 | // Check if the transport cc feedback or NACK status has changed on the |
| 1678 | // preferred send codec, and in that case reconfigure all receive streams. |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1679 | if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled || |
| 1680 | recv_nack_enabled_ != send_codec_spec_->nack_enabled) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1681 | RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send " |
| 1682 | "codec has changed."; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 1683 | recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled; |
| 1684 | recv_nack_enabled_ = send_codec_spec_->nack_enabled; |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1685 | for (auto& kv : recv_streams_) { |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1686 | kv.second->SetUseTransportCc(recv_transport_cc_enabled_, |
| 1687 | recv_nack_enabled_); |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | |
Taylor Brandstetter | 0cd086b | 2016-04-20 16:23:10 -0700 | [diff] [blame] | 1691 | send_codecs_ = codecs; |
solenberg | 72e29d2 | 2016-03-08 06:35:16 -0800 | [diff] [blame] | 1692 | return true; |
| 1693 | } |
| 1694 | |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1695 | void WebRtcVoiceMediaChannel::SetPlayout(bool playout) { |
kwiberg | 37b8b11 | 2016-11-03 02:46:53 -0700 | [diff] [blame] | 1696 | desired_playout_ = playout; |
| 1697 | return ChangePlayout(desired_playout_); |
| 1698 | } |
| 1699 | |
| 1700 | void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) { |
| 1701 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1702 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1703 | if (playout_ == playout) { |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1704 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1707 | for (const auto& kv : recv_streams_) { |
| 1708 | kv.second->SetPlayout(playout); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1709 | } |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1710 | playout_ = playout; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1713 | void WebRtcVoiceMediaChannel::SetSend(bool send) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1714 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1715 | if (send_ == send) { |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1716 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
solenberg | d53a3f9 | 2016-04-14 13:56:37 -0700 | [diff] [blame] | 1719 | // Apply channel specific options, and initialize the ADM for recording (this |
| 1720 | // may take time on some platforms, e.g. Android). |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1721 | if (send) { |
solenberg | 63b3454 | 2015-09-29 06:06:31 -0700 | [diff] [blame] | 1722 | engine()->ApplyOptions(options_); |
solenberg | d53a3f9 | 2016-04-14 13:56:37 -0700 | [diff] [blame] | 1723 | |
| 1724 | // InitRecording() may return an error if the ADM is already recording. |
| 1725 | if (!engine()->adm()->RecordingIsInitialized() && |
| 1726 | !engine()->adm()->Recording()) { |
| 1727 | if (engine()->adm()->InitRecording() != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1728 | RTC_LOG(LS_WARNING) << "Failed to initialize recording"; |
solenberg | d53a3f9 | 2016-04-14 13:56:37 -0700 | [diff] [blame] | 1729 | } |
| 1730 | } |
solenberg | 63b3454 | 2015-09-29 06:06:31 -0700 | [diff] [blame] | 1731 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1732 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1733 | // Change the settings on each send channel. |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1734 | for (auto& kv : send_streams_) { |
| 1735 | kv.second->SetSend(send); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1736 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1737 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1738 | send_ = send; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1741 | bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc, |
| 1742 | bool enable, |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1743 | const AudioOptions* options, |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1744 | AudioSource* source) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1745 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1746 | // TODO(solenberg): The state change should be fully rolled back if any one of |
| 1747 | // these calls fail. |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1748 | if (!SetLocalSource(ssrc, source)) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1749 | return false; |
| 1750 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1751 | if (!MuteStream(ssrc, !enable)) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1752 | return false; |
| 1753 | } |
solenberg | dfc8f4f | 2015-10-01 02:31:10 -0700 | [diff] [blame] | 1754 | if (enable && options) { |
solenberg | 1dd98f3 | 2015-09-10 01:57:14 -0700 | [diff] [blame] | 1755 | return SetOptions(*options); |
| 1756 | } |
| 1757 | return true; |
| 1758 | } |
| 1759 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1760 | bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1761 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1762 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1763 | RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
solenberg | 0a617e2 | 2015-10-20 15:49:38 -0700 | [diff] [blame] | 1764 | |
| 1765 | uint32_t ssrc = sp.first_ssrc(); |
| 1766 | RTC_DCHECK(0 != ssrc); |
| 1767 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1768 | if (send_streams_.find(ssrc) != send_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1769 | RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1770 | return false; |
| 1771 | } |
| 1772 | |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 1773 | rtc::Optional<std::string> audio_network_adaptor_config = |
| 1774 | GetAudioNetworkAdaptorConfig(options_); |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1775 | WebRtcAudioSendStream* stream = new WebRtcAudioSendStream( |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1776 | ssrc, |
| 1777 | sp.cname, |
| 1778 | sp.id, |
| 1779 | send_codec_spec_, |
| 1780 | send_rtp_extensions_, |
| 1781 | max_send_bitrate_bps_, |
| 1782 | audio_network_adaptor_config, |
| 1783 | call_, |
| 1784 | this, |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 1785 | engine()->encoder_factory_); |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 1786 | send_streams_.insert(std::make_pair(ssrc, stream)); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1787 | |
solenberg | 4a0f7b5 | 2016-06-16 13:07:33 -0700 | [diff] [blame] | 1788 | // At this point the stream's local SSRC has been updated. If it is the first |
| 1789 | // send stream, make sure that all the receive streams are updated with the |
| 1790 | // same SSRC in order to send receiver reports. |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1791 | if (send_streams_.size() == 1) { |
solenberg | 0a617e2 | 2015-10-20 15:49:38 -0700 | [diff] [blame] | 1792 | receiver_reports_ssrc_ = ssrc; |
solenberg | 4a0f7b5 | 2016-06-16 13:07:33 -0700 | [diff] [blame] | 1793 | for (const auto& kv : recv_streams_) { |
| 1794 | // TODO(solenberg): Allow applications to set the RTCP SSRC of receive |
Fredrik Solenberg | 3b903d0 | 2018-01-10 15:17:10 +0100 | [diff] [blame] | 1795 | // streams instead, so we can avoid reconfiguring the streams here. |
| 1796 | kv.second->SetLocalSsrc(ssrc); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1797 | } |
| 1798 | } |
| 1799 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1800 | send_streams_[ssrc]->SetSend(send_); |
| 1801 | return true; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1804 | bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1805 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1806 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1807 | RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 1808 | |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1809 | auto it = send_streams_.find(ssrc); |
| 1810 | if (it == send_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1811 | RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc |
| 1812 | << " which doesn't exist."; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1813 | return false; |
| 1814 | } |
| 1815 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1816 | it->second->SetSend(false); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1817 | |
solenberg | 7602aab | 2016-11-14 11:30:07 -0800 | [diff] [blame] | 1818 | // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find |
| 1819 | // the first active send stream and use that instead, reassociating receive |
| 1820 | // streams. |
| 1821 | |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1822 | delete it->second; |
| 1823 | send_streams_.erase(it); |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1824 | if (send_streams_.empty()) { |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1825 | SetSend(false); |
solenberg | 0a617e2 | 2015-10-20 15:49:38 -0700 | [diff] [blame] | 1826 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1827 | return true; |
| 1828 | } |
| 1829 | |
| 1830 | bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1831 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1832 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1833 | RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString(); |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 1834 | |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 1835 | if (!ValidateStreamParams(sp)) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 1836 | return false; |
| 1837 | } |
| 1838 | |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1839 | const uint32_t ssrc = sp.first_ssrc(); |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 1840 | if (ssrc == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1841 | RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported."; |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 1842 | return false; |
| 1843 | } |
| 1844 | |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 1845 | // If this stream was previously received unsignaled, we promote it, possibly |
| 1846 | // recreating the AudioReceiveStream, if sync_label has changed. |
| 1847 | if (MaybeDeregisterUnsignaledRecvStream(ssrc)) { |
solenberg | 4904fb6 | 2017-02-17 12:01:14 -0800 | [diff] [blame] | 1848 | recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label); |
solenberg | 4904fb6 | 2017-02-17 12:01:14 -0800 | [diff] [blame] | 1849 | return true; |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1850 | } |
solenberg | 0b67546 | 2015-10-09 01:37:09 -0700 | [diff] [blame] | 1851 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1852 | if (recv_streams_.find(ssrc) != recv_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1853 | RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1854 | return false; |
| 1855 | } |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 1856 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1857 | // Create a new channel for receiving audio data. |
stefan | ba4c0e4 | 2016-02-04 04:12:24 -0800 | [diff] [blame] | 1858 | recv_streams_.insert(std::make_pair( |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 1859 | ssrc, |
| 1860 | new WebRtcAudioReceiveStream( |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1861 | ssrc, |
| 1862 | receiver_reports_ssrc_, |
| 1863 | recv_transport_cc_enabled_, |
| 1864 | recv_nack_enabled_, |
| 1865 | sp.sync_label, |
| 1866 | recv_rtp_extensions_, |
| 1867 | call_, |
| 1868 | this, |
| 1869 | engine()->decoder_factory_, |
| 1870 | decoder_map_, |
| 1871 | engine()->audio_jitter_buffer_max_packets_, |
| 1872 | engine()->audio_jitter_buffer_fast_accelerate_))); |
aleloi | 84ef615 | 2016-08-04 05:28:21 -0700 | [diff] [blame] | 1873 | recv_streams_[ssrc]->SetPlayout(playout_); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1874 | |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1875 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1878 | bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 1879 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1880 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1881 | RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 1882 | |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1883 | const auto it = recv_streams_.find(ssrc); |
| 1884 | if (it == recv_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1885 | RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc |
| 1886 | << " which doesn't exist."; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1887 | return false; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1888 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1889 | |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 1890 | MaybeDeregisterUnsignaledRecvStream(ssrc); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1891 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 1892 | it->second->SetRawAudioSink(nullptr); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1893 | delete it->second; |
| 1894 | recv_streams_.erase(it); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame^] | 1895 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1898 | bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc, |
| 1899 | AudioSource* source) { |
solenberg | c96df77 | 2015-10-21 13:01:53 -0700 | [diff] [blame] | 1900 | auto it = send_streams_.find(ssrc); |
| 1901 | if (it == send_streams_.end()) { |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1902 | if (source) { |
| 1903 | // Return an error if trying to set a valid source with an invalid ssrc. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1904 | RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | } |
| 1907 | |
| 1908 | // The channel likely has gone away, do nothing. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1909 | return true; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1912 | if (source) { |
| 1913 | it->second->SetSource(source); |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1914 | } else { |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 1915 | it->second->ClearSource(); |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1916 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1917 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1918 | return true; |
| 1919 | } |
| 1920 | |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 1921 | // TODO(solenberg): Remove, once AudioMonitor is gone. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1922 | bool WebRtcVoiceMediaChannel::GetActiveStreams( |
| 1923 | AudioInfo::StreamList* actives) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1924 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1925 | actives->clear(); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1926 | for (const auto& ch : recv_streams_) { |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 1927 | int level = ch.second->GetOutputLevel(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1928 | if (level > 0) { |
Fredrik Solenberg | af9fb21 | 2015-08-26 10:45:53 +0200 | [diff] [blame] | 1929 | actives->push_back(std::make_pair(ch.first, level)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | return true; |
| 1933 | } |
| 1934 | |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 1935 | // TODO(solenberg): Remove, once AudioMonitor is gone. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1936 | int WebRtcVoiceMediaChannel::GetOutputLevel() { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1937 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1938 | int highest = 0; |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 1939 | for (const auto& ch : recv_streams_) { |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 1940 | highest = std::max(ch.second->GetOutputLevel(), highest); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1941 | } |
| 1942 | return highest; |
| 1943 | } |
| 1944 | |
solenberg | 4bac9c5 | 2015-10-09 02:32:53 -0700 | [diff] [blame] | 1945 | bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1946 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 1947 | std::vector<uint32_t> ssrcs(1, ssrc); |
deadbeef | 3bc1510 | 2017-04-20 19:25:07 -0700 | [diff] [blame] | 1948 | // SSRC of 0 represents the default receive stream. |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1949 | if (ssrc == 0) { |
| 1950 | default_recv_volume_ = volume; |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 1951 | ssrcs = unsignaled_recv_ssrcs_; |
| 1952 | } |
| 1953 | for (uint32_t ssrc : ssrcs) { |
| 1954 | const auto it = recv_streams_.find(ssrc); |
| 1955 | if (it == recv_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1956 | RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc; |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 1957 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1958 | } |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 1959 | it->second->SetOutputVolume(volume); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1960 | RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume |
| 1961 | << " for recv stream with ssrc " << ssrc; |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 1962 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1963 | return true; |
| 1964 | } |
| 1965 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1966 | bool WebRtcVoiceMediaChannel::CanInsertDtmf() { |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 1967 | return dtmf_payload_type_ ? true : false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1970 | bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event, |
| 1971 | int duration) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1972 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1973 | RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf"; |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 1974 | if (!dtmf_payload_type_) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1975 | return false; |
| 1976 | } |
| 1977 | |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 1978 | // Figure out which WebRtcAudioSendStream to send the event on. |
| 1979 | auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin(); |
| 1980 | if (it == send_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1981 | RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use."; |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1982 | return false; |
| 1983 | } |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 1984 | if (event < kMinTelephoneEventCode || |
| 1985 | event > kMaxTelephoneEventCode) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1986 | RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range."; |
solenberg | 1d63dd0 | 2015-12-02 12:35:09 -0800 | [diff] [blame] | 1987 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1988 | } |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 1989 | RTC_DCHECK_NE(-1, dtmf_payload_freq_); |
| 1990 | return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_, |
| 1991 | event, duration); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 1994 | void WebRtcVoiceMediaChannel::OnPacketReceived( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 1995 | rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 1996 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 1997 | |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 1998 | const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp, |
| 1999 | packet_time.not_before); |
| 2000 | webrtc::PacketReceiver::DeliveryStatus delivery_result = |
Danil Chapovalov | 292a73e | 2017-12-07 17:00:40 +0100 | [diff] [blame] | 2001 | call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, *packet, |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2002 | webrtc_packet_time); |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2003 | if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) { |
| 2004 | return; |
| 2005 | } |
| 2006 | |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2007 | // Create an unsignaled receive stream for this previously not received ssrc. |
| 2008 | // If there already is N unsignaled receive streams, delete the oldest. |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2009 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208 |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 2010 | uint32_t ssrc = 0; |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 2011 | if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) { |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 2012 | return; |
| 2013 | } |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2014 | RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(), |
| 2015 | unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end()); |
solenberg | 1ac5614 | 2015-10-13 03:58:19 -0700 | [diff] [blame] | 2016 | |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2017 | // Add new stream. |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2018 | StreamParams sp; |
| 2019 | sp.ssrcs.push_back(ssrc); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2020 | RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc; |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2021 | if (!AddRecvStream(sp)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2022 | RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream."; |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2023 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2024 | } |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2025 | unsignaled_recv_ssrcs_.push_back(ssrc); |
| 2026 | RTC_HISTOGRAM_COUNTS_LINEAR( |
| 2027 | "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1, |
| 2028 | 100, 101); |
solenberg | f748ca4 | 2017-02-06 13:03:19 -0800 | [diff] [blame] | 2029 | |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2030 | // Remove oldest unsignaled stream, if we have too many. |
| 2031 | if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) { |
| 2032 | uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2033 | RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC=" |
| 2034 | << remove_ssrc; |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2035 | RemoveRecvStream(remove_ssrc); |
| 2036 | } |
| 2037 | RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size()); |
| 2038 | |
| 2039 | SetOutputVolume(ssrc, default_recv_volume_); |
| 2040 | |
| 2041 | // The default sink can only be attached to one stream at a time, so we hook |
| 2042 | // it up to the *latest* unsignaled stream we've seen, in order to support the |
| 2043 | // case where the SSRC of one unsignaled stream changes. |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2044 | if (default_sink_) { |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2045 | for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) { |
| 2046 | auto it = recv_streams_.find(drop_ssrc); |
| 2047 | it->second->SetRawAudioSink(nullptr); |
| 2048 | } |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2049 | std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink( |
| 2050 | new ProxySink(default_sink_.get())); |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2051 | SetRawAudioSink(ssrc, std::move(proxy_sink)); |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2052 | } |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2053 | |
Danil Chapovalov | 292a73e | 2017-12-07 17:00:40 +0100 | [diff] [blame] | 2054 | delivery_result = call_->Receiver()->DeliverPacket( |
| 2055 | webrtc::MediaType::AUDIO, *packet, webrtc_packet_time); |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 2056 | RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 2059 | void WebRtcVoiceMediaChannel::OnRtcpReceived( |
jbauch | eec21bd | 2016-03-20 06:15:43 -0700 | [diff] [blame] | 2060 | rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 2061 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Fredrik Solenberg | 4b60c73 | 2015-05-07 14:07:48 +0200 | [diff] [blame] | 2062 | |
Fredrik Solenberg | 709ed67 | 2015-09-15 12:26:33 +0200 | [diff] [blame] | 2063 | // Forward packet to Call as well. |
| 2064 | const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp, |
| 2065 | packet_time.not_before); |
Danil Chapovalov | 292a73e | 2017-12-07 17:00:40 +0100 | [diff] [blame] | 2066 | call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, *packet, |
| 2067 | webrtc_packet_time); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 2070 | void WebRtcVoiceMediaChannel::OnNetworkRouteChanged( |
| 2071 | const std::string& transport_name, |
Honghai Zhang | 0e533ef | 2016-04-19 15:41:36 -0700 | [diff] [blame] | 2072 | const rtc::NetworkRoute& network_route) { |
Zhi Huang | 5f5918f | 2017-11-12 17:26:23 -0800 | [diff] [blame] | 2073 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 2074 | // TODO(zhihaung): Merge these two callbacks. |
Honghai Zhang | 0e533ef | 2016-04-19 15:41:36 -0700 | [diff] [blame] | 2075 | call_->OnNetworkRouteChanged(transport_name, network_route); |
Zhi Huang | 5f5918f | 2017-11-12 17:26:23 -0800 | [diff] [blame] | 2076 | call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO, |
| 2077 | network_route.packet_overhead); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 2080 | bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 2081 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 2082 | const auto it = send_streams_.find(ssrc); |
| 2083 | if (it == send_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2084 | RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2085 | return false; |
| 2086 | } |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 2087 | it->second->SetMuted(muted); |
| 2088 | |
| 2089 | // TODO(solenberg): |
buildbot@webrtc.org | 6b21b71 | 2014-07-31 15:08:53 +0000 | [diff] [blame] | 2090 | // We set the AGC to mute state only when all the channels are muted. |
| 2091 | // This implementation is not ideal, instead we should signal the AGC when |
| 2092 | // the mic channel is muted/unmuted. We can't do it today because there |
| 2093 | // is no good way to know which stream is mapping to the mic channel. |
| 2094 | bool all_muted = muted; |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 2095 | for (const auto& kv : send_streams_) { |
| 2096 | all_muted = all_muted && kv.second->muted(); |
buildbot@webrtc.org | 6b21b71 | 2014-07-31 15:08:53 +0000 | [diff] [blame] | 2097 | } |
solenberg | 059fb44 | 2016-10-26 05:12:24 -0700 | [diff] [blame] | 2098 | engine()->apm()->set_output_will_be_muted(all_muted); |
buildbot@webrtc.org | 6b21b71 | 2014-07-31 15:08:53 +0000 | [diff] [blame] | 2099 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2100 | return true; |
| 2101 | } |
| 2102 | |
deadbeef | 8034614 | 2016-04-27 14:17:10 -0700 | [diff] [blame] | 2103 | bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2104 | RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate."; |
deadbeef | 8034614 | 2016-04-27 14:17:10 -0700 | [diff] [blame] | 2105 | max_send_bitrate_bps_ = bps; |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 2106 | bool success = true; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 2107 | for (const auto& kv : send_streams_) { |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 2108 | if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) { |
| 2109 | success = false; |
skvlad | e0d4637 | 2016-04-07 22:59:22 -0700 | [diff] [blame] | 2110 | } |
| 2111 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 2112 | return success; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
skvlad | 7a43d25 | 2016-03-22 15:32:27 -0700 | [diff] [blame] | 2115 | void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) { |
| 2116 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2117 | RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
skvlad | 7a43d25 | 2016-03-22 15:32:27 -0700 | [diff] [blame] | 2118 | call_->SignalChannelNetworkState( |
| 2119 | webrtc::MediaType::AUDIO, |
| 2120 | ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); |
| 2121 | } |
| 2122 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2123 | bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) { |
Peter Boström | ca8b404 | 2016-03-08 14:24:13 -0800 | [diff] [blame] | 2124 | TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats"); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 2125 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 2126 | RTC_DCHECK(info); |
solenberg | d97ec30 | 2015-10-07 01:40:33 -0700 | [diff] [blame] | 2127 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 2128 | // Get SSRC and stats for each sender. |
hbos | 1acfbd2 | 2016-11-17 23:43:29 -0800 | [diff] [blame] | 2129 | RTC_DCHECK_EQ(info->senders.size(), 0U); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 2130 | for (const auto& stream : send_streams_) { |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 2131 | webrtc::AudioSendStream::Stats stats = |
| 2132 | stream.second->GetStats(recv_streams_.size() > 0); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2133 | VoiceSenderInfo sinfo; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 2134 | sinfo.add_ssrc(stats.local_ssrc); |
| 2135 | sinfo.bytes_sent = stats.bytes_sent; |
| 2136 | sinfo.packets_sent = stats.packets_sent; |
| 2137 | sinfo.packets_lost = stats.packets_lost; |
| 2138 | sinfo.fraction_lost = stats.fraction_lost; |
| 2139 | sinfo.codec_name = stats.codec_name; |
hbos | 1acfbd2 | 2016-11-17 23:43:29 -0800 | [diff] [blame] | 2140 | sinfo.codec_payload_type = stats.codec_payload_type; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 2141 | sinfo.ext_seqnum = stats.ext_seqnum; |
| 2142 | sinfo.jitter_ms = stats.jitter_ms; |
| 2143 | sinfo.rtt_ms = stats.rtt_ms; |
| 2144 | sinfo.audio_level = stats.audio_level; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 2145 | sinfo.total_input_energy = stats.total_input_energy; |
| 2146 | sinfo.total_input_duration = stats.total_input_duration; |
Taylor Brandstetter | 1a018dc | 2016-03-08 12:37:39 -0800 | [diff] [blame] | 2147 | sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false); |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 2148 | sinfo.ana_statistics = stats.ana_statistics; |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 2149 | sinfo.apm_statistics = stats.apm_statistics; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2150 | info->senders.push_back(sinfo); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 2153 | // Get SSRC and stats for each receiver. |
hbos | 1acfbd2 | 2016-11-17 23:43:29 -0800 | [diff] [blame] | 2154 | RTC_DCHECK_EQ(info->receivers.size(), 0U); |
solenberg | 7add058 | 2015-11-20 09:59:34 -0800 | [diff] [blame] | 2155 | for (const auto& stream : recv_streams_) { |
deadbeef | 4e2deab | 2017-09-20 13:56:21 -0700 | [diff] [blame] | 2156 | uint32_t ssrc = stream.first; |
| 2157 | // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but |
| 2158 | // multiple RTP streams can be received over time (if the SSRC changes for |
| 2159 | // whatever reason). We only want the RTCMediaStreamTrackStats to represent |
| 2160 | // the stats for the most recent stream (the one whose audio is actually |
| 2161 | // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs |
| 2162 | // except for the most recent one (last in the vector). This is somewhat of |
| 2163 | // a hack, and means you don't get *any* stats for these inactive streams, |
| 2164 | // but it's slightly better than the previous behavior, which was "highest |
| 2165 | // SSRC wins". |
| 2166 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158 |
| 2167 | if (!unsignaled_recv_ssrcs_.empty()) { |
| 2168 | auto end_it = --unsignaled_recv_ssrcs_.end(); |
| 2169 | if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) { |
| 2170 | continue; |
| 2171 | } |
| 2172 | } |
Fredrik Solenberg | 4f4ec0a | 2015-10-22 10:49:27 +0200 | [diff] [blame] | 2173 | webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats(); |
| 2174 | VoiceReceiverInfo rinfo; |
| 2175 | rinfo.add_ssrc(stats.remote_ssrc); |
| 2176 | rinfo.bytes_rcvd = stats.bytes_rcvd; |
| 2177 | rinfo.packets_rcvd = stats.packets_rcvd; |
| 2178 | rinfo.packets_lost = stats.packets_lost; |
| 2179 | rinfo.fraction_lost = stats.fraction_lost; |
| 2180 | rinfo.codec_name = stats.codec_name; |
hbos | 1acfbd2 | 2016-11-17 23:43:29 -0800 | [diff] [blame] | 2181 | rinfo.codec_payload_type = stats.codec_payload_type; |
Fredrik Solenberg | 4f4ec0a | 2015-10-22 10:49:27 +0200 | [diff] [blame] | 2182 | rinfo.ext_seqnum = stats.ext_seqnum; |
| 2183 | rinfo.jitter_ms = stats.jitter_ms; |
| 2184 | rinfo.jitter_buffer_ms = stats.jitter_buffer_ms; |
| 2185 | rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms; |
| 2186 | rinfo.delay_estimate_ms = stats.delay_estimate_ms; |
| 2187 | rinfo.audio_level = stats.audio_level; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 2188 | rinfo.total_output_energy = stats.total_output_energy; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 2189 | rinfo.total_samples_received = stats.total_samples_received; |
zstein | e76bd3a | 2017-07-14 12:17:49 -0700 | [diff] [blame] | 2190 | rinfo.total_output_duration = stats.total_output_duration; |
Steve Anton | 2dbc69f | 2017-08-24 17:15:13 -0700 | [diff] [blame] | 2191 | rinfo.concealed_samples = stats.concealed_samples; |
Gustaf Ullberg | 9a2e906 | 2017-09-18 09:28:20 +0200 | [diff] [blame] | 2192 | rinfo.concealment_events = stats.concealment_events; |
Gustaf Ullberg | b0a0207 | 2017-10-02 12:00:34 +0200 | [diff] [blame] | 2193 | rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds; |
Fredrik Solenberg | 4f4ec0a | 2015-10-22 10:49:27 +0200 | [diff] [blame] | 2194 | rinfo.expand_rate = stats.expand_rate; |
| 2195 | rinfo.speech_expand_rate = stats.speech_expand_rate; |
| 2196 | rinfo.secondary_decoded_rate = stats.secondary_decoded_rate; |
minyue-webrtc | 0e320ec | 2017-08-28 13:51:27 +0200 | [diff] [blame] | 2197 | rinfo.secondary_discarded_rate = stats.secondary_discarded_rate; |
Fredrik Solenberg | 4f4ec0a | 2015-10-22 10:49:27 +0200 | [diff] [blame] | 2198 | rinfo.accelerate_rate = stats.accelerate_rate; |
| 2199 | rinfo.preemptive_expand_rate = stats.preemptive_expand_rate; |
| 2200 | rinfo.decoding_calls_to_silence_generator = |
| 2201 | stats.decoding_calls_to_silence_generator; |
| 2202 | rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq; |
| 2203 | rinfo.decoding_normal = stats.decoding_normal; |
| 2204 | rinfo.decoding_plc = stats.decoding_plc; |
| 2205 | rinfo.decoding_cng = stats.decoding_cng; |
| 2206 | rinfo.decoding_plc_cng = stats.decoding_plc_cng; |
henrik.lundin | 6348978 | 2016-09-20 01:47:12 -0700 | [diff] [blame] | 2207 | rinfo.decoding_muted_output = stats.decoding_muted_output; |
Fredrik Solenberg | 4f4ec0a | 2015-10-22 10:49:27 +0200 | [diff] [blame] | 2208 | rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms; |
| 2209 | info->receivers.push_back(rinfo); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
hbos | 1acfbd2 | 2016-11-17 23:43:29 -0800 | [diff] [blame] | 2212 | // Get codec info |
| 2213 | for (const AudioCodec& codec : send_codecs_) { |
| 2214 | webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters(); |
| 2215 | info->send_codecs.insert( |
| 2216 | std::make_pair(codec_params.payload_type, std::move(codec_params))); |
| 2217 | } |
| 2218 | for (const AudioCodec& codec : recv_codecs_) { |
| 2219 | webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters(); |
| 2220 | info->receive_codecs.insert( |
| 2221 | std::make_pair(codec_params.payload_type, std::move(codec_params))); |
| 2222 | } |
| 2223 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2224 | return true; |
| 2225 | } |
| 2226 | |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 2227 | void WebRtcVoiceMediaChannel::SetRawAudioSink( |
| 2228 | uint32_t ssrc, |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 2229 | std::unique_ptr<webrtc::AudioSinkInterface> sink) { |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 2230 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2231 | RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" |
| 2232 | << ssrc << " " << (sink ? "(ptr)" : "NULL"); |
deadbeef | 884f585 | 2016-01-15 09:20:04 -0800 | [diff] [blame] | 2233 | if (ssrc == 0) { |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2234 | if (!unsignaled_recv_ssrcs_.empty()) { |
kwiberg | 686a8ef | 2016-02-26 03:00:35 -0800 | [diff] [blame] | 2235 | std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink( |
deadbeef | 884f585 | 2016-01-15 09:20:04 -0800 | [diff] [blame] | 2236 | sink ? new ProxySink(sink.get()) : nullptr); |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2237 | SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink)); |
deadbeef | 884f585 | 2016-01-15 09:20:04 -0800 | [diff] [blame] | 2238 | } |
| 2239 | default_sink_ = std::move(sink); |
| 2240 | return; |
| 2241 | } |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 2242 | const auto it = recv_streams_.find(ssrc); |
| 2243 | if (it == recv_streams_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2244 | RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc; |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 2245 | return; |
| 2246 | } |
deadbeef | 2d110be | 2016-01-13 12:00:26 -0800 | [diff] [blame] | 2247 | it->second->SetRawAudioSink(std::move(sink)); |
Tommi | f888bb5 | 2015-12-12 01:37:01 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 2250 | std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources( |
| 2251 | uint32_t ssrc) const { |
| 2252 | auto it = recv_streams_.find(ssrc); |
Zhi Huang | fa266ef | 2017-12-13 10:27:46 -0800 | [diff] [blame] | 2253 | if (it == recv_streams_.end()) { |
| 2254 | RTC_LOG(LS_ERROR) << "Attempting to get contributing sources for SSRC:" |
| 2255 | << ssrc << " which doesn't exist."; |
| 2256 | return std::vector<webrtc::RtpSource>(); |
| 2257 | } |
hbos | 8d609f6 | 2017-04-10 07:39:05 -0700 | [diff] [blame] | 2258 | return it->second->GetSources(); |
| 2259 | } |
| 2260 | |
solenberg | 2100c0b | 2017-03-01 11:29:29 -0800 | [diff] [blame] | 2261 | bool WebRtcVoiceMediaChannel:: |
| 2262 | MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) { |
| 2263 | RTC_DCHECK(worker_thread_checker_.CalledOnValidThread()); |
| 2264 | auto it = std::find(unsignaled_recv_ssrcs_.begin(), |
| 2265 | unsignaled_recv_ssrcs_.end(), |
| 2266 | ssrc); |
| 2267 | if (it != unsignaled_recv_ssrcs_.end()) { |
| 2268 | unsignaled_recv_ssrcs_.erase(it); |
| 2269 | return true; |
| 2270 | } |
| 2271 | return false; |
| 2272 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2273 | } // namespace cricket |
| 2274 | |
| 2275 | #endif // HAVE_WEBRTC_VOICE |