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