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