solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "audio/audio_send_stream.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 13 | #include <memory> |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 14 | #include <string> |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 15 | #include <utility> |
| 16 | #include <vector> |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 17 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "api/audio_codecs/audio_encoder.h" |
| 19 | #include "api/audio_codecs/audio_encoder_factory.h" |
| 20 | #include "api/audio_codecs/audio_format.h" |
| 21 | #include "api/call/transport.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "api/crypto/frame_encryptor_interface.h" |
Artem Titov | 741daaf | 2019-03-21 14:37:36 +0100 | [diff] [blame] | 23 | #include "api/function_view.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 24 | #include "api/rtc_event_log/rtc_event_log.h" |
Niels Möller | 65f17ca | 2019-09-12 13:59:36 +0200 | [diff] [blame] | 25 | #include "api/transport/media/media_transport_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "audio/audio_state.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 27 | #include "audio/channel_send.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "audio/conversion.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include "call/rtp_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "call/rtp_transport_controller_send_interface.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 31 | #include "common_audio/vad/include/vad.h" |
Oskar Sundbom | 56ef305 | 2018-10-30 16:11:02 +0100 | [diff] [blame] | 32 | #include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h" |
Oskar Sundbom | 56ef305 | 2018-10-30 16:11:02 +0100 | [diff] [blame] | 33 | #include "logging/rtc_event_log/rtc_stream_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 35 | #include "modules/audio_processing/include/audio_processing.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #include "rtc_base/checks.h" |
| 37 | #include "rtc_base/event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "rtc_base/logging.h" |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 39 | #include "rtc_base/strings/audio_format_to_string.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | #include "rtc_base/task_queue.h" |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 41 | #include "system_wrappers/include/field_trial.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 42 | |
| 43 | namespace webrtc { |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 44 | namespace { |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 45 | |
Oskar Sundbom | 56ef305 | 2018-10-30 16:11:02 +0100 | [diff] [blame] | 46 | void UpdateEventLogStreamConfig(RtcEventLog* event_log, |
| 47 | const AudioSendStream::Config& config, |
| 48 | const AudioSendStream::Config* old_config) { |
| 49 | using SendCodecSpec = AudioSendStream::Config::SendCodecSpec; |
| 50 | // Only update if any of the things we log have changed. |
| 51 | auto payload_types_equal = [](const absl::optional<SendCodecSpec>& a, |
| 52 | const absl::optional<SendCodecSpec>& b) { |
| 53 | if (a.has_value() && b.has_value()) { |
| 54 | return a->format.name == b->format.name && |
| 55 | a->payload_type == b->payload_type; |
| 56 | } |
| 57 | return !a.has_value() && !b.has_value(); |
| 58 | }; |
| 59 | |
| 60 | if (old_config && config.rtp.ssrc == old_config->rtp.ssrc && |
| 61 | config.rtp.extensions == old_config->rtp.extensions && |
| 62 | payload_types_equal(config.send_codec_spec, |
| 63 | old_config->send_codec_spec)) { |
| 64 | return; |
| 65 | } |
| 66 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 67 | auto rtclog_config = std::make_unique<rtclog::StreamConfig>(); |
Oskar Sundbom | 56ef305 | 2018-10-30 16:11:02 +0100 | [diff] [blame] | 68 | rtclog_config->local_ssrc = config.rtp.ssrc; |
| 69 | rtclog_config->rtp_extensions = config.rtp.extensions; |
| 70 | if (config.send_codec_spec) { |
| 71 | rtclog_config->codecs.emplace_back(config.send_codec_spec->format.name, |
| 72 | config.send_codec_spec->payload_type, 0); |
| 73 | } |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 74 | event_log->Log(std::make_unique<RtcEventAudioSendStreamConfig>( |
Oskar Sundbom | 56ef305 | 2018-10-30 16:11:02 +0100 | [diff] [blame] | 75 | std::move(rtclog_config))); |
| 76 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 77 | } // namespace |
| 78 | |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 79 | constexpr char AudioAllocationConfig::kKey[]; |
| 80 | |
| 81 | std::unique_ptr<StructParametersParser> AudioAllocationConfig::Parser() { |
| 82 | return StructParametersParser::Create( // |
| 83 | "min", &min_bitrate, // |
| 84 | "max", &max_bitrate, // |
| 85 | "prio_rate", &priority_bitrate, // |
| 86 | "prio_rate_raw", &priority_bitrate_raw, // |
| 87 | "rate_prio", &bitrate_priority); |
| 88 | } |
| 89 | |
| 90 | AudioAllocationConfig::AudioAllocationConfig() { |
| 91 | Parser()->Parse(field_trial::FindFullName(kKey)); |
| 92 | if (priority_bitrate_raw && !priority_bitrate.IsZero()) { |
| 93 | RTC_LOG(LS_WARNING) << "'priority_bitrate' and '_raw' are mutually " |
| 94 | "exclusive but both were configured."; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | namespace internal { |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 99 | AudioSendStream::AudioSendStream( |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 100 | Clock* clock, |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 101 | const webrtc::AudioSendStream::Config& config, |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 102 | const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 103 | TaskQueueFactory* task_queue_factory, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 104 | ProcessThread* module_process_thread, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 105 | RtpTransportControllerSendInterface* rtp_transport, |
Niels Möller | 67b011d | 2018-10-22 13:00:40 +0200 | [diff] [blame] | 106 | BitrateAllocatorInterface* bitrate_allocator, |
michaelt | 9332b7d | 2016-11-30 07:51:13 -0800 | [diff] [blame] | 107 | RtcEventLog* event_log, |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 108 | RtcpRttStats* rtcp_rtt_stats, |
Sam Zackrisson | ff05816 | 2018-11-20 17:15:13 +0100 | [diff] [blame] | 109 | const absl::optional<RtpState>& suspended_rtp_state) |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 110 | : AudioSendStream(clock, |
| 111 | config, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 112 | audio_state, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 113 | task_queue_factory, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 114 | rtp_transport, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 115 | bitrate_allocator, |
| 116 | event_log, |
| 117 | rtcp_rtt_stats, |
| 118 | suspended_rtp_state, |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 119 | voe::CreateChannelSend(clock, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 120 | task_queue_factory, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 121 | module_process_thread, |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 122 | config.media_transport_config, |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 123 | /*overhead_observer=*/this, |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 124 | config.send_transport, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 125 | rtcp_rtt_stats, |
| 126 | event_log, |
| 127 | config.frame_encryptor, |
| 128 | config.crypto_options, |
| 129 | config.rtp.extmap_allow_mixed, |
Erik Språng | 4c2c412 | 2019-07-11 15:20:15 +0200 | [diff] [blame] | 130 | config.rtcp_report_interval_ms, |
| 131 | config.rtp.ssrc)) {} |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 132 | |
| 133 | AudioSendStream::AudioSendStream( |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 134 | Clock* clock, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 135 | const webrtc::AudioSendStream::Config& config, |
| 136 | const rtc::scoped_refptr<webrtc::AudioState>& audio_state, |
Sebastian Jansson | 44dd9f2 | 2019-03-08 14:50:30 +0100 | [diff] [blame] | 137 | TaskQueueFactory* task_queue_factory, |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 138 | RtpTransportControllerSendInterface* rtp_transport, |
Niels Möller | 67b011d | 2018-10-22 13:00:40 +0200 | [diff] [blame] | 139 | BitrateAllocatorInterface* bitrate_allocator, |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 140 | RtcEventLog* event_log, |
| 141 | RtcpRttStats* rtcp_rtt_stats, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 142 | const absl::optional<RtpState>& suspended_rtp_state, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 143 | std::unique_ptr<voe::ChannelSendInterface> channel_send) |
Sebastian Jansson | 977b335 | 2019-03-04 17:43:34 +0100 | [diff] [blame] | 144 | : clock_(clock), |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 145 | worker_queue_(rtp_transport->GetWorkerQueue()), |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 146 | audio_send_side_bwe_(field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")), |
| 147 | allocate_audio_without_feedback_( |
| 148 | field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")), |
| 149 | enable_audio_alr_probing_( |
| 150 | !field_trial::IsDisabled("WebRTC-Audio-AlrProbing")), |
| 151 | send_side_bwe_with_overhead_( |
| 152 | field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")), |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 153 | config_(Config(/*send_transport=*/nullptr, MediaTransportConfig())), |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 154 | audio_state_(audio_state), |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 155 | channel_send_(std::move(channel_send)), |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 156 | event_log_(event_log), |
Sebastian Jansson | 62aee93 | 2019-10-02 12:27:06 +0200 | [diff] [blame] | 157 | use_legacy_overhead_calculation_( |
| 158 | !field_trial::IsDisabled("WebRTC-Audio-LegacyOverhead")), |
michaelt | f4caaab | 2017-01-16 23:55:07 -0800 | [diff] [blame] | 159 | bitrate_allocator_(bitrate_allocator), |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 160 | rtp_transport_(rtp_transport), |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 161 | rtp_rtcp_module_(nullptr), |
Sam Zackrisson | ff05816 | 2018-11-20 17:15:13 +0100 | [diff] [blame] | 162 | suspended_rtp_state_(suspended_rtp_state) { |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 163 | RTC_LOG(LS_INFO) << "AudioSendStream: " << config.rtp.ssrc; |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 164 | RTC_DCHECK(worker_queue_); |
| 165 | RTC_DCHECK(audio_state_); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 166 | RTC_DCHECK(channel_send_); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 167 | RTC_DCHECK(bitrate_allocator_); |
Sebastian Jansson | 0b69826 | 2019-03-07 09:17:19 +0100 | [diff] [blame] | 168 | // Currently we require the rtp transport even when media transport is used. |
| 169 | RTC_DCHECK(rtp_transport); |
| 170 | |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 171 | // TODO(nisse): Eventually, we should have only media_transport. But for the |
| 172 | // time being, we can have either. When media transport is injected, there |
| 173 | // should be no rtp_transport, and below check should be strengthened to XOR |
| 174 | // (either rtp_transport or media_transport but not both). |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 175 | RTC_DCHECK(rtp_transport || config.media_transport_config.media_transport); |
| 176 | if (config.media_transport_config.media_transport) { |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 177 | // TODO(sukhanov): Currently media transport audio overhead is considered |
| 178 | // constant, we will not get overhead_observer calls when using |
| 179 | // media_transport. In the future when we introduce RTP media transport we |
| 180 | // should make audio overhead interface consistent and work for both RTP and |
| 181 | // non-RTP implementations. |
| 182 | audio_overhead_per_packet_bytes_ = |
Anton Sukhanov | 4f08faa | 2019-05-21 11:12:57 -0700 | [diff] [blame] | 183 | config.media_transport_config.media_transport->GetAudioPacketOverhead(); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 184 | } |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 185 | rtp_rtcp_module_ = channel_send_->GetRtpRtcp(); |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 186 | RTC_DCHECK(rtp_rtcp_module_); |
mflodman | 3d7db26 | 2016-04-29 00:57:13 -0700 | [diff] [blame] | 187 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 188 | ConfigureStream(config, true); |
elad.alon | d12a8e1 | 2017-03-23 11:04:48 -0700 | [diff] [blame] | 189 | |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 190 | pacer_thread_checker_.Detach(); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | AudioSendStream::~AudioSendStream() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 194 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 195 | RTC_LOG(LS_INFO) << "~AudioSendStream: " << config_.rtp.ssrc; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 196 | RTC_DCHECK(!sending_); |
Sebastian Jansson | 0a6510d | 2019-10-04 09:31:08 +0200 | [diff] [blame] | 197 | channel_send_->ResetSenderCongestionControlObjects(); |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 198 | // Blocking call to synchronize state with worker queue to ensure that there |
| 199 | // are no pending tasks left that keeps references to audio. |
| 200 | rtc::Event thread_sync_event; |
| 201 | worker_queue_->PostTask([&] { thread_sync_event.Set(); }); |
| 202 | thread_sync_event.Wait(rtc::Event::kForever); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 203 | } |
| 204 | |
eladalon | abbc430 | 2017-07-26 02:09:44 -0700 | [diff] [blame] | 205 | const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 206 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
eladalon | abbc430 | 2017-07-26 02:09:44 -0700 | [diff] [blame] | 207 | return config_; |
| 208 | } |
| 209 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 210 | void AudioSendStream::Reconfigure( |
| 211 | const webrtc::AudioSendStream::Config& new_config) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 212 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 213 | ConfigureStream(new_config, false); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 216 | AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds( |
| 217 | const std::vector<RtpExtension>& extensions) { |
| 218 | ExtensionIds ids; |
| 219 | for (const auto& extension : extensions) { |
| 220 | if (extension.uri == RtpExtension::kAudioLevelUri) { |
| 221 | ids.audio_level = extension.id; |
Sebastian Jansson | 71c6b56 | 2019-08-14 11:31:02 +0200 | [diff] [blame] | 222 | } else if (extension.uri == RtpExtension::kAbsSendTimeUri) { |
| 223 | ids.abs_send_time = extension.id; |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 224 | } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) { |
| 225 | ids.transport_sequence_number = extension.id; |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 226 | } else if (extension.uri == RtpExtension::kMidUri) { |
| 227 | ids.mid = extension.id; |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 228 | } else if (extension.uri == RtpExtension::kRidUri) { |
| 229 | ids.rid = extension.id; |
| 230 | } else if (extension.uri == RtpExtension::kRepairedRidUri) { |
| 231 | ids.repaired_rid = extension.id; |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | return ids; |
| 235 | } |
| 236 | |
Sebastian Jansson | 470a5ea | 2019-01-23 12:37:49 +0100 | [diff] [blame] | 237 | int AudioSendStream::TransportSeqNumId(const AudioSendStream::Config& config) { |
| 238 | return FindExtensionIds(config.rtp.extensions).transport_sequence_number; |
| 239 | } |
| 240 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 241 | void AudioSendStream::ConfigureStream( |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 242 | const webrtc::AudioSendStream::Config& new_config, |
| 243 | bool first_time) { |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 244 | RTC_LOG(LS_INFO) << "AudioSendStream::ConfigureStream: " |
| 245 | << new_config.ToString(); |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 246 | UpdateEventLogStreamConfig(event_log_, new_config, |
| 247 | first_time ? nullptr : &config_); |
Oskar Sundbom | 56ef305 | 2018-10-30 16:11:02 +0100 | [diff] [blame] | 248 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 249 | const auto& old_config = config_; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 250 | |
Niels Möller | e977199 | 2018-11-26 10:55:07 +0100 | [diff] [blame] | 251 | // Configuration parameters which cannot be changed. |
| 252 | RTC_DCHECK(first_time || |
| 253 | old_config.send_transport == new_config.send_transport); |
Erik Språng | 70efdde | 2019-08-21 13:36:20 +0200 | [diff] [blame] | 254 | RTC_DCHECK(first_time || old_config.rtp.ssrc == new_config.rtp.ssrc); |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 255 | if (suspended_rtp_state_ && first_time) { |
| 256 | rtp_rtcp_module_->SetRtpState(*suspended_rtp_state_); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 257 | } |
| 258 | if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 259 | channel_send_->SetRTCP_CNAME(new_config.rtp.c_name); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 260 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 261 | |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 262 | // Enable the frame encryptor if a new frame encryptor has been provided. |
| 263 | if (first_time || new_config.frame_encryptor != old_config.frame_encryptor) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 264 | channel_send_->SetFrameEncryptor(new_config.frame_encryptor); |
Benjamin Wright | 84583f6 | 2018-10-04 14:22:34 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 267 | if (first_time || |
| 268 | new_config.rtp.extmap_allow_mixed != old_config.rtp.extmap_allow_mixed) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 269 | channel_send_->SetExtmapAllowMixed(new_config.rtp.extmap_allow_mixed); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Alex Narest | cedd351 | 2017-12-07 20:54:55 +0100 | [diff] [blame] | 272 | const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions); |
| 273 | const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions); |
Yves Gerey | 1704801 | 2019-07-26 17:49:52 +0200 | [diff] [blame] | 274 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 275 | // Audio level indication |
| 276 | if (first_time || new_ids.audio_level != old_ids.audio_level) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 277 | channel_send_->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0, |
| 278 | new_ids.audio_level); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 279 | } |
Sebastian Jansson | 71c6b56 | 2019-08-14 11:31:02 +0200 | [diff] [blame] | 280 | |
| 281 | if (first_time || new_ids.abs_send_time != old_ids.abs_send_time) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 282 | channel_send_->GetRtpRtcp()->DeregisterSendRtpHeaderExtension( |
Sebastian Jansson | 71c6b56 | 2019-08-14 11:31:02 +0200 | [diff] [blame] | 283 | kRtpExtensionAbsoluteSendTime); |
| 284 | if (new_ids.abs_send_time) { |
Sebastian Jansson | f39c815 | 2019-10-14 17:32:21 +0200 | [diff] [blame] | 285 | rtp_rtcp_module_->RegisterRtpHeaderExtension(AbsoluteSendTime::kUri, |
| 286 | new_ids.abs_send_time); |
Sebastian Jansson | 71c6b56 | 2019-08-14 11:31:02 +0200 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Sebastian Jansson | 8d9c540 | 2017-11-15 17:22:16 +0100 | [diff] [blame] | 290 | bool transport_seq_num_id_changed = |
| 291 | new_ids.transport_sequence_number != old_ids.transport_sequence_number; |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 292 | if (first_time || |
| 293 | (transport_seq_num_id_changed && !allocate_audio_without_feedback_)) { |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 294 | if (!first_time) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 295 | channel_send_->ResetSenderCongestionControlObjects(); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Sebastian Jansson | 8d9c540 | 2017-11-15 17:22:16 +0100 | [diff] [blame] | 298 | RtcpBandwidthObserver* bandwidth_observer = nullptr; |
Sebastian Jansson | 470a5ea | 2019-01-23 12:37:49 +0100 | [diff] [blame] | 299 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 300 | if (audio_send_side_bwe_ && !allocate_audio_without_feedback_ && |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 301 | new_ids.transport_sequence_number != 0) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 302 | channel_send_->EnableSendTransportSequenceNumber( |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 303 | new_ids.transport_sequence_number); |
Sebastian Jansson | 8d9c540 | 2017-11-15 17:22:16 +0100 | [diff] [blame] | 304 | // Probing in application limited region is only used in combination with |
| 305 | // send side congestion control, wich depends on feedback packets which |
| 306 | // requires transport sequence numbers to be enabled. |
Sebastian Jansson | 0a6510d | 2019-10-04 09:31:08 +0200 | [diff] [blame] | 307 | // Optionally request ALR probing but do not override any existing |
| 308 | // request from other streams. |
| 309 | if (enable_audio_alr_probing_) { |
| 310 | rtp_transport_->EnablePeriodicAlrProbing(true); |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 311 | } |
Sebastian Jansson | 0a6510d | 2019-10-04 09:31:08 +0200 | [diff] [blame] | 312 | bandwidth_observer = rtp_transport_->GetBandwidthObserver(); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 313 | } |
Sebastian Jansson | 0a6510d | 2019-10-04 09:31:08 +0200 | [diff] [blame] | 314 | channel_send_->RegisterSenderCongestionControlObjects(rtp_transport_, |
| 315 | bandwidth_observer); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 316 | } |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 317 | // MID RTP header extension. |
Steve Anton | 003930a | 2018-03-29 12:37:21 -0700 | [diff] [blame] | 318 | if ((first_time || new_ids.mid != old_ids.mid || |
| 319 | new_config.rtp.mid != old_config.rtp.mid) && |
| 320 | new_ids.mid != 0 && !new_config.rtp.mid.empty()) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 321 | channel_send_->SetMid(new_config.rtp.mid, new_ids.mid); |
Steve Anton | bb50ce5 | 2018-03-26 10:24:32 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 324 | // RID RTP header extension |
| 325 | if ((first_time || new_ids.rid != old_ids.rid || |
| 326 | new_ids.repaired_rid != old_ids.repaired_rid || |
| 327 | new_config.rtp.rid != old_config.rtp.rid)) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 328 | channel_send_->SetRid(new_config.rtp.rid, new_ids.rid, |
| 329 | new_ids.repaired_rid); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 332 | if (!ReconfigureSendCodec(new_config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 333 | RTC_LOG(LS_ERROR) << "Failed to set up send codec state."; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 336 | if (sending_) { |
| 337 | ReconfigureBitrateObserver(new_config); |
Oskar Sundbom | f85e31b | 2017-12-20 16:38:09 +0100 | [diff] [blame] | 338 | } |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 339 | config_ = new_config; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 340 | } |
| 341 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 342 | void AudioSendStream::Start() { |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 343 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 344 | if (sending_) { |
| 345 | return; |
| 346 | } |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 347 | if (!config_.has_dscp && config_.min_bitrate_bps != -1 && |
| 348 | config_.max_bitrate_bps != -1 && |
Sebastian Jansson | cd0eedb | 2019-10-10 13:52:26 +0200 | [diff] [blame] | 349 | (allocate_audio_without_feedback_ || TransportSeqNumId(config_) != 0)) { |
Erik Språng | aa59eca | 2019-07-24 14:52:55 +0200 | [diff] [blame] | 350 | rtp_transport_->AccountForAudioPacketsInPacedSender(true); |
Sebastian Jansson | b686396 | 2018-10-10 10:23:13 +0200 | [diff] [blame] | 351 | rtp_rtcp_module_->SetAsPartOfAllocation(true); |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 352 | rtc::Event thread_sync_event; |
| 353 | worker_queue_->PostTask([&] { |
| 354 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 355 | ConfigureBitrateObserver(); |
| 356 | thread_sync_event.Set(); |
| 357 | }); |
| 358 | thread_sync_event.Wait(rtc::Event::kForever); |
Sebastian Jansson | b686396 | 2018-10-10 10:23:13 +0200 | [diff] [blame] | 359 | } else { |
| 360 | rtp_rtcp_module_->SetAsPartOfAllocation(false); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 361 | } |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 362 | channel_send_->StartSend(); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 363 | sending_ = true; |
| 364 | audio_state()->AddSendingStream(this, encoder_sample_rate_hz_, |
| 365 | encoder_num_channels_); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | void AudioSendStream::Stop() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 369 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 370 | if (!sending_) { |
| 371 | return; |
| 372 | } |
| 373 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 374 | RemoveBitrateObserver(); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 375 | channel_send_->StopSend(); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 376 | sending_ = false; |
| 377 | audio_state()->RemoveSendingStream(this); |
| 378 | } |
| 379 | |
| 380 | void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) { |
| 381 | RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_); |
Henrik Boström | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame] | 382 | RTC_DCHECK_GT(audio_frame->sample_rate_hz_, 0); |
| 383 | double duration = static_cast<double>(audio_frame->samples_per_channel_) / |
| 384 | audio_frame->sample_rate_hz_; |
| 385 | { |
| 386 | // Note: SendAudioData() passes the frame further down the pipeline and it |
| 387 | // may eventually get sent. But this method is invoked even if we are not |
| 388 | // connected, as long as we have an AudioSendStream (created as a result of |
| 389 | // an O/A exchange). This means that we are calculating audio levels whether |
| 390 | // or not we are sending samples. |
| 391 | // TODO(https://crbug.com/webrtc/10771): All "media-source" related stats |
| 392 | // should move from send-streams to the local audio sources or tracks; a |
| 393 | // send-stream should not be required to read the microphone audio levels. |
| 394 | rtc::CritScope cs(&audio_level_lock_); |
| 395 | audio_level_.ComputeLevel(*audio_frame, duration); |
| 396 | } |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 397 | channel_send_->ProcessAndEncodeAudio(std::move(audio_frame)); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 398 | } |
| 399 | |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 400 | bool AudioSendStream::SendTelephoneEvent(int payload_type, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 401 | int payload_frequency, |
| 402 | int event, |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 403 | int duration_ms) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 404 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 405 | channel_send_->SetSendTelephoneEventPayloadType(payload_type, |
| 406 | payload_frequency); |
| 407 | return channel_send_->SendTelephoneEventOutband(event, duration_ms); |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 408 | } |
| 409 | |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 410 | void AudioSendStream::SetMuted(bool muted) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 411 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 412 | channel_send_->SetInputMute(muted); |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 413 | } |
| 414 | |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 415 | webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const { |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 416 | return GetStats(true); |
| 417 | } |
| 418 | |
| 419 | webrtc::AudioSendStream::Stats AudioSendStream::GetStats( |
| 420 | bool has_remote_tracks) const { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 421 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 422 | webrtc::AudioSendStream::Stats stats; |
| 423 | stats.local_ssrc = config_.rtp.ssrc; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 424 | stats.target_bitrate_bps = channel_send_->GetBitrate(); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 425 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 426 | webrtc::CallSendStatistics call_stats = channel_send_->GetRTCPStatistics(); |
Niels Möller | ac0a4cb | 2019-10-09 15:01:33 +0200 | [diff] [blame] | 427 | stats.payload_bytes_sent = call_stats.payload_bytes_sent; |
| 428 | stats.header_and_padding_bytes_sent = |
| 429 | call_stats.header_and_padding_bytes_sent; |
Henrik Boström | cf96e0f | 2019-04-17 13:51:53 +0200 | [diff] [blame] | 430 | stats.retransmitted_bytes_sent = call_stats.retransmitted_bytes_sent; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 431 | stats.packets_sent = call_stats.packetsSent; |
Henrik Boström | cf96e0f | 2019-04-17 13:51:53 +0200 | [diff] [blame] | 432 | stats.retransmitted_packets_sent = call_stats.retransmitted_packets_sent; |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 433 | // RTT isn't known until a RTCP report is received. Until then, VoiceEngine |
| 434 | // returns 0 to indicate an error value. |
| 435 | if (call_stats.rttMs > 0) { |
| 436 | stats.rtt_ms = call_stats.rttMs; |
| 437 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 438 | if (config_.send_codec_spec) { |
| 439 | const auto& spec = *config_.send_codec_spec; |
| 440 | stats.codec_name = spec.format.name; |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 441 | stats.codec_payload_type = spec.payload_type; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 442 | |
| 443 | // Get data from the last remote RTCP report. |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 444 | for (const auto& block : channel_send_->GetRemoteRTCPReportBlocks()) { |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 445 | // Lookup report for send ssrc only. |
| 446 | if (block.source_SSRC == stats.local_ssrc) { |
| 447 | stats.packets_lost = block.cumulative_num_packets_lost; |
| 448 | stats.fraction_lost = Q8ToFloat(block.fraction_lost); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 449 | // Convert timestamps to milliseconds. |
| 450 | if (spec.format.clockrate_hz / 1000 > 0) { |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 451 | stats.jitter_ms = |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 452 | block.interarrival_jitter / (spec.format.clockrate_hz / 1000); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 453 | } |
solenberg | 8b85de2 | 2015-11-16 09:48:04 -0800 | [diff] [blame] | 454 | break; |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Henrik Boström | d2c336f | 2019-07-03 17:11:10 +0200 | [diff] [blame] | 459 | { |
| 460 | rtc::CritScope cs(&audio_level_lock_); |
| 461 | stats.audio_level = audio_level_.LevelFullRange(); |
| 462 | stats.total_input_energy = audio_level_.TotalEnergy(); |
| 463 | stats.total_input_duration = audio_level_.TotalDuration(); |
| 464 | } |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 465 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 466 | stats.typing_noise_detected = audio_state()->typing_noise_detected(); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 467 | stats.ana_statistics = channel_send_->GetANAStatistics(); |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 468 | RTC_DCHECK(audio_state_->audio_processing()); |
| 469 | stats.apm_statistics = |
| 470 | audio_state_->audio_processing()->GetStatistics(has_remote_tracks); |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 471 | |
Henrik Boström | 6e436d1 | 2019-05-27 12:19:33 +0200 | [diff] [blame] | 472 | stats.report_block_datas = std::move(call_stats.report_block_datas); |
| 473 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 474 | return stats; |
| 475 | } |
| 476 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 477 | void AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) { |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 478 | // TODO(solenberg): Tests call this function on a network thread, libjingle |
| 479 | // calls on the worker thread. We should move towards always using a network |
| 480 | // thread. Then this check can be enabled. |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 481 | // RTC_DCHECK(!worker_thread_checker_.IsCurrent()); |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 482 | channel_send_->ReceivedRTCPPacket(packet, length); |
pbos | 1ba8d39 | 2016-05-01 20:18:34 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 485 | uint32_t AudioSendStream::OnBitrateUpdated(BitrateAllocationUpdate update) { |
Sebastian Jansson | 62aee93 | 2019-10-02 12:27:06 +0200 | [diff] [blame] | 486 | RTC_DCHECK_RUN_ON(worker_queue_); |
Daniel Lee | 9356252 | 2019-05-03 14:40:13 +0200 | [diff] [blame] | 487 | // Pick a target bitrate between the constraints. Overrules the allocator if |
| 488 | // it 1) allocated a bitrate of zero to disable the stream or 2) allocated a |
| 489 | // higher than max to allow for e.g. extra FEC. |
| 490 | auto constraints = GetMinMaxBitrateConstraints(); |
| 491 | update.target_bitrate.Clamp(constraints.min, constraints.max); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 492 | |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 493 | channel_send_->OnBitrateAllocation(update); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 494 | |
| 495 | // The amount of audio protection is not exposed by the encoder, hence |
| 496 | // always returning 0. |
| 497 | return 0; |
| 498 | } |
| 499 | |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 500 | void AudioSendStream::SetTransportOverhead( |
| 501 | int transport_overhead_per_packet_bytes) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 502 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 503 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 504 | transport_overhead_per_packet_bytes_ = transport_overhead_per_packet_bytes; |
| 505 | UpdateOverheadForEncoder(); |
| 506 | } |
| 507 | |
| 508 | void AudioSendStream::OnOverheadChanged( |
| 509 | size_t overhead_bytes_per_packet_bytes) { |
| 510 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 511 | audio_overhead_per_packet_bytes_ = overhead_bytes_per_packet_bytes; |
| 512 | UpdateOverheadForEncoder(); |
| 513 | } |
| 514 | |
| 515 | void AudioSendStream::UpdateOverheadForEncoder() { |
| 516 | const size_t overhead_per_packet_bytes = GetPerPacketOverheadBytes(); |
Bjorn A Mellem | 413ccc4 | 2019-04-26 15:41:05 -0700 | [diff] [blame] | 517 | if (overhead_per_packet_bytes == 0) { |
| 518 | return; // Overhead is not known yet, do not tell the encoder. |
| 519 | } |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 520 | channel_send_->CallEncoder([&](AudioEncoder* encoder) { |
| 521 | encoder->OnReceivedOverhead(overhead_per_packet_bytes); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 522 | }); |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 523 | worker_queue_->PostTask([this, overhead_per_packet_bytes] { |
| 524 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 525 | if (total_packet_overhead_bytes_ != overhead_per_packet_bytes) { |
| 526 | total_packet_overhead_bytes_ = overhead_per_packet_bytes; |
| 527 | if (registered_with_allocator_) { |
| 528 | ConfigureBitrateObserver(); |
| 529 | } |
| 530 | } |
| 531 | }); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | size_t AudioSendStream::TestOnlyGetPerPacketOverheadBytes() const { |
| 535 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 536 | return GetPerPacketOverheadBytes(); |
| 537 | } |
| 538 | |
| 539 | size_t AudioSendStream::GetPerPacketOverheadBytes() const { |
| 540 | return transport_overhead_per_packet_bytes_ + |
| 541 | audio_overhead_per_packet_bytes_; |
michaelt | 79e0588 | 2016-11-08 02:50:09 -0800 | [diff] [blame] | 542 | } |
| 543 | |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 544 | RtpState AudioSendStream::GetRtpState() const { |
| 545 | return rtp_rtcp_module_->GetRtpState(); |
| 546 | } |
| 547 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 548 | const voe::ChannelSendInterface* AudioSendStream::GetChannel() const { |
| 549 | return channel_send_.get(); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 552 | internal::AudioState* AudioSendStream::audio_state() { |
| 553 | internal::AudioState* audio_state = |
| 554 | static_cast<internal::AudioState*>(audio_state_.get()); |
| 555 | RTC_DCHECK(audio_state); |
| 556 | return audio_state; |
| 557 | } |
| 558 | |
| 559 | const internal::AudioState* AudioSendStream::audio_state() const { |
| 560 | internal::AudioState* audio_state = |
| 561 | static_cast<internal::AudioState*>(audio_state_.get()); |
| 562 | RTC_DCHECK(audio_state); |
| 563 | return audio_state; |
| 564 | } |
| 565 | |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 566 | void AudioSendStream::StoreEncoderProperties(int sample_rate_hz, |
| 567 | size_t num_channels) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 568 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 569 | encoder_sample_rate_hz_ = sample_rate_hz; |
| 570 | encoder_num_channels_ = num_channels; |
| 571 | if (sending_) { |
| 572 | // Update AudioState's information about the stream. |
| 573 | audio_state()->AddSendingStream(this, sample_rate_hz, num_channels); |
| 574 | } |
| 575 | } |
| 576 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 577 | // Apply current codec settings to a single voe::Channel used for sending. |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 578 | bool AudioSendStream::SetupSendCodec(const Config& new_config) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 579 | RTC_DCHECK(new_config.send_codec_spec); |
| 580 | const auto& spec = *new_config.send_codec_spec; |
minyue | 48368ad | 2017-05-10 04:06:11 -0700 | [diff] [blame] | 581 | |
| 582 | RTC_DCHECK(new_config.encoder_factory); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 583 | std::unique_ptr<AudioEncoder> encoder = |
Karl Wiberg | 77490b9 | 2018-03-21 15:18:42 +0100 | [diff] [blame] | 584 | new_config.encoder_factory->MakeAudioEncoder( |
| 585 | spec.payload_type, spec.format, new_config.codec_pair_id); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 586 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 587 | if (!encoder) { |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 588 | RTC_DLOG(LS_ERROR) << "Unable to create encoder for " |
| 589 | << rtc::ToString(spec.format); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 590 | return false; |
| 591 | } |
Alex Narest | bbbe4e1 | 2018-07-13 10:32:58 +0200 | [diff] [blame] | 592 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 593 | // If a bitrate has been specified for the codec, use it over the |
| 594 | // codec's default. |
Christoffer Rodbro | 110c64b | 2019-03-06 09:51:08 +0100 | [diff] [blame] | 595 | if (spec.target_bitrate_bps) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 596 | encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 597 | } |
| 598 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 599 | // Enable ANA if configured (currently only used by Opus). |
| 600 | if (new_config.audio_network_adaptor_config) { |
| 601 | if (encoder->EnableAudioNetworkAdaptor( |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 602 | *new_config.audio_network_adaptor_config, event_log_)) { |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 603 | RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
| 604 | << new_config.rtp.ssrc; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 605 | } else { |
| 606 | RTC_NOTREACHED(); |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 607 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 608 | } |
| 609 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 610 | // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled. |
| 611 | if (spec.cng_payload_type) { |
Karl Wiberg | 2365936 | 2018-11-01 11:13:44 +0100 | [diff] [blame] | 612 | AudioEncoderCngConfig cng_config; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 613 | cng_config.num_channels = encoder->NumChannels(); |
| 614 | cng_config.payload_type = *spec.cng_payload_type; |
| 615 | cng_config.speech_encoder = std::move(encoder); |
| 616 | cng_config.vad_mode = Vad::kVadNormal; |
Karl Wiberg | 2365936 | 2018-11-01 11:13:44 +0100 | [diff] [blame] | 617 | encoder = CreateComfortNoiseEncoder(std::move(cng_config)); |
ossu | 3b9ff38 | 2017-04-27 08:03:42 -0700 | [diff] [blame] | 618 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 619 | RegisterCngPayloadType(*spec.cng_payload_type, |
| 620 | new_config.send_codec_spec->format.clockrate_hz); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 621 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 622 | |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 623 | // Set currently known overhead (used in ANA, opus only). |
| 624 | // If overhead changes later, it will be updated in UpdateOverheadForEncoder. |
| 625 | { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 626 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 627 | if (GetPerPacketOverheadBytes() > 0) { |
| 628 | encoder->OnReceivedOverhead(GetPerPacketOverheadBytes()); |
Bjorn A Mellem | 413ccc4 | 2019-04-26 15:41:05 -0700 | [diff] [blame] | 629 | } |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 630 | } |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 631 | worker_queue_->PostTask( |
| 632 | [this, length_range = encoder->GetFrameLengthRange()] { |
| 633 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 634 | frame_length_range_ = length_range; |
Sebastian Jansson | 62aee93 | 2019-10-02 12:27:06 +0200 | [diff] [blame] | 635 | }); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 636 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 637 | StoreEncoderProperties(encoder->SampleRateHz(), encoder->NumChannels()); |
| 638 | channel_send_->SetEncoder(new_config.send_codec_spec->payload_type, |
| 639 | std::move(encoder)); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 640 | |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 644 | bool AudioSendStream::ReconfigureSendCodec(const Config& new_config) { |
| 645 | const auto& old_config = config_; |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 646 | |
| 647 | if (!new_config.send_codec_spec) { |
| 648 | // We cannot de-configure a send codec. So we will do nothing. |
| 649 | // By design, the send codec should have not been configured. |
| 650 | RTC_DCHECK(!old_config.send_codec_spec); |
| 651 | return true; |
| 652 | } |
| 653 | |
| 654 | if (new_config.send_codec_spec == old_config.send_codec_spec && |
| 655 | new_config.audio_network_adaptor_config == |
| 656 | old_config.audio_network_adaptor_config) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 657 | return true; |
| 658 | } |
| 659 | |
| 660 | // If we have no encoder, or the format or payload type's changed, create a |
| 661 | // new encoder. |
| 662 | if (!old_config.send_codec_spec || |
| 663 | new_config.send_codec_spec->format != |
| 664 | old_config.send_codec_spec->format || |
| 665 | new_config.send_codec_spec->payload_type != |
| 666 | old_config.send_codec_spec->payload_type) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 667 | return SetupSendCodec(new_config); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 670 | const absl::optional<int>& new_target_bitrate_bps = |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 671 | new_config.send_codec_spec->target_bitrate_bps; |
| 672 | // If a bitrate has been specified for the codec, use it over the |
| 673 | // codec's default. |
Christoffer Rodbro | 110c64b | 2019-03-06 09:51:08 +0100 | [diff] [blame] | 674 | if (new_target_bitrate_bps && |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 675 | new_target_bitrate_bps != |
| 676 | old_config.send_codec_spec->target_bitrate_bps) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 677 | channel_send_->CallEncoder([&](AudioEncoder* encoder) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 678 | encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps); |
| 679 | }); |
| 680 | } |
| 681 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 682 | ReconfigureANA(new_config); |
| 683 | ReconfigureCNG(new_config); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 684 | |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 685 | // Set currently known overhead (used in ANA, opus only). |
| 686 | { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 687 | rtc::CritScope cs(&overhead_per_packet_lock_); |
| 688 | UpdateOverheadForEncoder(); |
Anton Sukhanov | 626015d | 2019-02-04 15:16:06 -0800 | [diff] [blame] | 689 | } |
| 690 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 691 | return true; |
| 692 | } |
| 693 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 694 | void AudioSendStream::ReconfigureANA(const Config& new_config) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 695 | if (new_config.audio_network_adaptor_config == |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 696 | config_.audio_network_adaptor_config) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 697 | return; |
| 698 | } |
| 699 | if (new_config.audio_network_adaptor_config) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 700 | channel_send_->CallEncoder([&](AudioEncoder* encoder) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 701 | if (encoder->EnableAudioNetworkAdaptor( |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 702 | *new_config.audio_network_adaptor_config, event_log_)) { |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 703 | RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC " |
| 704 | << new_config.rtp.ssrc; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 705 | } else { |
| 706 | RTC_NOTREACHED(); |
| 707 | } |
| 708 | }); |
| 709 | } else { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 710 | channel_send_->CallEncoder( |
Sebastian Jansson | 14a7cf9 | 2019-02-13 15:11:42 +0100 | [diff] [blame] | 711 | [&](AudioEncoder* encoder) { encoder->DisableAudioNetworkAdaptor(); }); |
Jonas Olsson | 24ea822 | 2018-01-25 10:14:29 +0100 | [diff] [blame] | 712 | RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC " |
| 713 | << new_config.rtp.ssrc; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 717 | void AudioSendStream::ReconfigureCNG(const Config& new_config) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 718 | if (new_config.send_codec_spec->cng_payload_type == |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 719 | config_.send_codec_spec->cng_payload_type) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
ossu | 3b9ff38 | 2017-04-27 08:03:42 -0700 | [diff] [blame] | 723 | // Register the CNG payload type if it's been added, don't do anything if CNG |
| 724 | // is removed. Payload types must not be redefined. |
| 725 | if (new_config.send_codec_spec->cng_payload_type) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 726 | RegisterCngPayloadType(*new_config.send_codec_spec->cng_payload_type, |
| 727 | new_config.send_codec_spec->format.clockrate_hz); |
ossu | 3b9ff38 | 2017-04-27 08:03:42 -0700 | [diff] [blame] | 728 | } |
| 729 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 730 | // Wrap or unwrap the encoder in an AudioEncoderCNG. |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 731 | channel_send_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) { |
| 732 | std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr)); |
| 733 | auto sub_encoders = old_encoder->ReclaimContainedEncoders(); |
| 734 | if (!sub_encoders.empty()) { |
| 735 | // Replace enc with its sub encoder. We need to put the sub |
| 736 | // encoder in a temporary first, since otherwise the old value |
| 737 | // of enc would be destroyed before the new value got assigned, |
| 738 | // which would be bad since the new value is a part of the old |
| 739 | // value. |
| 740 | auto tmp = std::move(sub_encoders[0]); |
| 741 | old_encoder = std::move(tmp); |
| 742 | } |
| 743 | if (new_config.send_codec_spec->cng_payload_type) { |
| 744 | AudioEncoderCngConfig config; |
| 745 | config.speech_encoder = std::move(old_encoder); |
| 746 | config.num_channels = config.speech_encoder->NumChannels(); |
| 747 | config.payload_type = *new_config.send_codec_spec->cng_payload_type; |
| 748 | config.vad_mode = Vad::kVadNormal; |
| 749 | *encoder_ptr = CreateComfortNoiseEncoder(std::move(config)); |
| 750 | } else { |
| 751 | *encoder_ptr = std::move(old_encoder); |
| 752 | } |
| 753 | }); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | void AudioSendStream::ReconfigureBitrateObserver( |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 757 | const webrtc::AudioSendStream::Config& new_config) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 758 | RTC_DCHECK_RUN_ON(&worker_thread_checker_); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 759 | // Since the Config's default is for both of these to be -1, this test will |
| 760 | // allow us to configure the bitrate observer if the new config has bitrate |
| 761 | // limits set, but would only have us call RemoveBitrateObserver if we were |
| 762 | // previously configured with bitrate limits. |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 763 | if (config_.min_bitrate_bps == new_config.min_bitrate_bps && |
| 764 | config_.max_bitrate_bps == new_config.max_bitrate_bps && |
| 765 | config_.bitrate_priority == new_config.bitrate_priority && |
| 766 | (TransportSeqNumId(config_) == TransportSeqNumId(new_config) || |
| 767 | !audio_send_side_bwe_)) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 768 | return; |
| 769 | } |
| 770 | |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 771 | if (!new_config.has_dscp && new_config.min_bitrate_bps != -1 && |
Sebastian Jansson | cd0eedb | 2019-10-10 13:52:26 +0200 | [diff] [blame] | 772 | new_config.max_bitrate_bps != -1 && TransportSeqNumId(new_config) != 0) { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 773 | rtp_transport_->AccountForAudioPacketsInPacedSender(true); |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 774 | rtc::Event thread_sync_event; |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 775 | worker_queue_->PostTask([&] { |
| 776 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 777 | registered_with_allocator_ = true; |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 778 | // We may get a callback immediately as the observer is registered, so |
| 779 | // make |
| 780 | // sure the bitrate limits in config_ are up-to-date. |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 781 | config_.min_bitrate_bps = new_config.min_bitrate_bps; |
| 782 | config_.max_bitrate_bps = new_config.max_bitrate_bps; |
| 783 | |
| 784 | config_.bitrate_priority = new_config.bitrate_priority; |
| 785 | ConfigureBitrateObserver(); |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 786 | thread_sync_event.Set(); |
| 787 | }); |
| 788 | thread_sync_event.Wait(rtc::Event::kForever); |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 789 | rtp_rtcp_module_->SetAsPartOfAllocation(true); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 790 | } else { |
Sebastian Jansson | 35cf9e7 | 2019-10-04 09:30:32 +0200 | [diff] [blame] | 791 | rtp_transport_->AccountForAudioPacketsInPacedSender(false); |
| 792 | RemoveBitrateObserver(); |
| 793 | rtp_rtcp_module_->SetAsPartOfAllocation(false); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 797 | void AudioSendStream::ConfigureBitrateObserver() { |
| 798 | // This either updates the current observer or adds a new observer. |
| 799 | // TODO(srte): Add overhead compensation here. |
Daniel Lee | 9356252 | 2019-05-03 14:40:13 +0200 | [diff] [blame] | 800 | auto constraints = GetMinMaxBitrateConstraints(); |
| 801 | |
Sebastian Jansson | 0429f78 | 2019-10-03 18:32:45 +0200 | [diff] [blame] | 802 | DataRate priority_bitrate = allocation_settings_.priority_bitrate; |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 803 | if (send_side_bwe_with_overhead_) { |
Sebastian Jansson | 0429f78 | 2019-10-03 18:32:45 +0200 | [diff] [blame] | 804 | if (use_legacy_overhead_calculation_) { |
| 805 | // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12) |
| 806 | constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12; |
| 807 | const TimeDelta kMinPacketDuration = TimeDelta::ms(20); |
| 808 | DataRate max_overhead = |
| 809 | DataSize::bytes(kOverheadPerPacket) / kMinPacketDuration; |
| 810 | priority_bitrate += max_overhead; |
| 811 | } else { |
| 812 | RTC_DCHECK(frame_length_range_); |
| 813 | const DataSize kOverheadPerPacket = |
| 814 | DataSize::bytes(total_packet_overhead_bytes_); |
| 815 | DataRate max_overhead = kOverheadPerPacket / frame_length_range_->first; |
| 816 | priority_bitrate += max_overhead; |
| 817 | } |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 818 | } |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 819 | if (allocation_settings_.priority_bitrate_raw) |
| 820 | priority_bitrate = *allocation_settings_.priority_bitrate_raw; |
| 821 | |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 822 | bitrate_allocator_->AddObserver( |
Daniel Lee | 9356252 | 2019-05-03 14:40:13 +0200 | [diff] [blame] | 823 | this, |
| 824 | MediaStreamAllocationConfig{ |
| 825 | constraints.min.bps<uint32_t>(), constraints.max.bps<uint32_t>(), 0, |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 826 | priority_bitrate.bps(), true, |
| 827 | allocation_settings_.bitrate_priority.value_or( |
Jonas Olsson | 8f119ca | 2019-05-08 10:56:23 +0200 | [diff] [blame] | 828 | config_.bitrate_priority)}); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | void AudioSendStream::RemoveBitrateObserver() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 832 | RTC_DCHECK(worker_thread_checker_.IsCurrent()); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 833 | rtc::Event thread_sync_event; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 834 | worker_queue_->PostTask([this, &thread_sync_event] { |
Sebastian Jansson | 8672cac | 2019-03-01 15:57:55 +0100 | [diff] [blame] | 835 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 836 | registered_with_allocator_ = false; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 837 | bitrate_allocator_->RemoveObserver(this); |
| 838 | thread_sync_event.Set(); |
| 839 | }); |
| 840 | thread_sync_event.Wait(rtc::Event::kForever); |
| 841 | } |
| 842 | |
Daniel Lee | 9356252 | 2019-05-03 14:40:13 +0200 | [diff] [blame] | 843 | AudioSendStream::TargetAudioBitrateConstraints |
| 844 | AudioSendStream::GetMinMaxBitrateConstraints() const { |
| 845 | TargetAudioBitrateConstraints constraints{ |
| 846 | DataRate::bps(config_.min_bitrate_bps), |
| 847 | DataRate::bps(config_.max_bitrate_bps)}; |
| 848 | |
| 849 | // If bitrates were explicitly overriden via field trial, use those values. |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 850 | if (allocation_settings_.min_bitrate) |
| 851 | constraints.min = *allocation_settings_.min_bitrate; |
| 852 | if (allocation_settings_.max_bitrate) |
| 853 | constraints.max = *allocation_settings_.max_bitrate; |
Daniel Lee | 9356252 | 2019-05-03 14:40:13 +0200 | [diff] [blame] | 854 | |
Sebastian Jansson | 62aee93 | 2019-10-02 12:27:06 +0200 | [diff] [blame] | 855 | RTC_DCHECK_GE(constraints.min, DataRate::Zero()); |
| 856 | RTC_DCHECK_GE(constraints.max, DataRate::Zero()); |
| 857 | RTC_DCHECK_GE(constraints.max, constraints.min); |
Sebastian Jansson | f23131f | 2019-10-03 10:03:55 +0200 | [diff] [blame] | 858 | if (send_side_bwe_with_overhead_) { |
Sebastian Jansson | 62aee93 | 2019-10-02 12:27:06 +0200 | [diff] [blame] | 859 | if (use_legacy_overhead_calculation_) { |
| 860 | // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12) |
| 861 | const DataSize kOverheadPerPacket = DataSize::bytes(20 + 8 + 10 + 12); |
| 862 | const TimeDelta kMaxFrameLength = |
| 863 | TimeDelta::ms(60); // Based on Opus spec |
| 864 | const DataRate kMinOverhead = kOverheadPerPacket / kMaxFrameLength; |
| 865 | constraints.min += kMinOverhead; |
| 866 | constraints.max += kMinOverhead; |
| 867 | } else { |
| 868 | RTC_DCHECK(frame_length_range_); |
| 869 | const DataSize kOverheadPerPacket = |
| 870 | DataSize::bytes(total_packet_overhead_bytes_); |
| 871 | constraints.min += kOverheadPerPacket / frame_length_range_->second; |
| 872 | constraints.max += kOverheadPerPacket / frame_length_range_->first; |
| 873 | } |
Daniel Lee | 9356252 | 2019-05-03 14:40:13 +0200 | [diff] [blame] | 874 | } |
| 875 | return constraints; |
| 876 | } |
| 877 | |
ossu | 3b9ff38 | 2017-04-27 08:03:42 -0700 | [diff] [blame] | 878 | void AudioSendStream::RegisterCngPayloadType(int payload_type, |
| 879 | int clockrate_hz) { |
Niels Möller | ee5ccbc | 2019-03-06 16:47:29 +0100 | [diff] [blame] | 880 | channel_send_->RegisterCngPayloadType(payload_type, clockrate_hz); |
ossu | 3b9ff38 | 2017-04-27 08:03:42 -0700 | [diff] [blame] | 881 | } |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 882 | } // namespace internal |
| 883 | } // namespace webrtc |