blob: 42be43afb93026773f0de9b024d3f39c73efac3f [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef AUDIO_AUDIO_SEND_STREAM_H_
12#define AUDIO_AUDIO_SEND_STREAM_H_
solenbergc7a8b082015-10-16 14:35:07 -070013
kwibergfffa42b2016-02-23 10:46:32 -080014#include <memory>
Sebastian Jansson62aee932019-10-02 12:27:06 +020015#include <utility>
elad.alond12a8e12017-03-23 11:04:48 -070016#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080017
Per Kjellander828ef912022-10-10 12:53:41 +020018#include "absl/functional/any_invocable.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020019#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Per Kjellander828ef912022-10-10 12:53:41 +020021#include "api/task_queue/task_queue_base.h"
Henrik Boströmd2c336f2019-07-03 17:11:10 +020022#include "audio/audio_level.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010023#include "audio/channel_send.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "call/audio_send_stream.h"
25#include "call/audio_state.h"
26#include "call/bitrate_allocator.h"
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020027#include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
Per Kjellander828ef912022-10-10 12:53:41 +020028#include "modules/utility/maybe_worker_thread.h"
Sebastian Janssonf23131f2019-10-03 10:03:55 +020029#include "rtc_base/experiments/struct_parameters_parser.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010030#include "rtc_base/race_checker.h"
Markus Handell62872802020-07-06 15:15:07 +020031#include "rtc_base/synchronization/mutex.h"
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010032#include "rtc_base/task_queue.h"
solenbergc7a8b082015-10-16 14:35:07 -070033
34namespace webrtc {
tereliuse035e2d2016-09-21 06:51:47 -070035class RtcEventLog;
stefan7de8d642017-02-07 07:14:08 -080036class RtcpBandwidthObserver;
michaelt9332b7d2016-11-30 07:51:13 -080037class RtcpRttStats;
nisseb8f9a322017-03-27 05:36:15 -070038class RtpTransportControllerSendInterface;
solenberg3a941542015-11-16 07:34:50 -080039
Sebastian Janssonf23131f2019-10-03 10:03:55 +020040struct AudioAllocationConfig {
41 static constexpr char kKey[] = "WebRTC-Audio-Allocation";
42 // Field Trial configured bitrates to use as overrides over default/user
43 // configured bitrate range when audio bitrate allocation is enabled.
44 absl::optional<DataRate> min_bitrate;
45 absl::optional<DataRate> max_bitrate;
46 DataRate priority_bitrate = DataRate::Zero();
47 // By default the priority_bitrate is compensated for packet overhead.
48 // Use this flag to configure a raw value instead.
49 absl::optional<DataRate> priority_bitrate_raw;
50 absl::optional<double> bitrate_priority;
51
52 std::unique_ptr<StructParametersParser> Parser();
Jonas Orelande62c2f22022-03-29 11:04:48 +020053 explicit AudioAllocationConfig(const FieldTrialsView& field_trials);
Sebastian Janssonf23131f2019-10-03 10:03:55 +020054};
solenberg13725082015-11-25 08:16:52 -080055namespace internal {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010056class AudioState;
57
mflodman86cc6ff2016-07-26 04:44:06 -070058class AudioSendStream final : public webrtc::AudioSendStream,
Erik Språng04e1bab2020-05-07 18:18:32 +020059 public webrtc::BitrateAllocatorObserver {
solenbergc7a8b082015-10-16 14:35:07 -070060 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010061 AudioSendStream(Clock* clock,
62 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010063 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010064 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020065 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020066 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080067 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070068 RtcpRttStats* rtcp_rtt_stats,
Jonas Orelanda943e732022-03-16 13:50:58 +010069 const absl::optional<RtpState>& suspended_rtp_state,
Jonas Orelande62c2f22022-03-29 11:04:48 +020070 const FieldTrialsView& field_trials);
Niels Möllerdced9f62018-11-19 10:27:07 +010071 // For unit tests, which need to supply a mock ChannelSend.
Sebastian Jansson977b3352019-03-04 17:43:34 +010072 AudioSendStream(Clock* clock,
73 const webrtc::AudioSendStream::Config& config,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010074 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010075 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020076 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020077 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010078 RtcEventLog* event_log,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020079 const absl::optional<RtpState>& suspended_rtp_state,
Jonas Orelanda943e732022-03-16 13:50:58 +010080 std::unique_ptr<voe::ChannelSendInterface> channel_send,
Jonas Orelande62c2f22022-03-29 11:04:48 +020081 const FieldTrialsView& field_trials);
Niels Möllerde953292020-09-29 09:46:21 +020082
83 AudioSendStream() = delete;
84 AudioSendStream(const AudioSendStream&) = delete;
85 AudioSendStream& operator=(const AudioSendStream&) = delete;
86
solenbergc7a8b082015-10-16 14:35:07 -070087 ~AudioSendStream() override;
88
pbos1ba8d392016-05-01 20:18:34 -070089 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070090 const webrtc::AudioSendStream::Config& GetConfig() const override;
Florent Castelliacabb362022-10-18 17:05:16 +020091 void Reconfigure(const webrtc::AudioSendStream::Config& config,
92 SetParametersCallback callback) override;
solenbergc7a8b082015-10-16 14:35:07 -070093 void Start() override;
94 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010095 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020096 bool SendTelephoneEvent(int payload_type,
97 int payload_frequency,
98 int event,
solenberg8842c3e2016-03-11 03:06:41 -080099 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -0700100 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -0700101 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +0100102 webrtc::AudioSendStream::Stats GetStats(
103 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -0700104
Niels Möller8fb1a6a2019-03-05 14:29:42 +0100105 void DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -0700106
107 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200108 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -0700109
Anton Sukhanov626015d2019-02-04 15:16:06 -0800110 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
111
ossuc3d4b482017-05-23 06:07:11 -0700112 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +0100113 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -0700114
Anton Sukhanov626015d2019-02-04 15:16:06 -0800115 // Returns combined per-packet overhead.
116 size_t TestOnlyGetPerPacketOverheadBytes() const
117 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
118
solenbergc7a8b082015-10-16 14:35:07 -0700119 private:
sazac58f8c02017-07-19 00:39:19 -0700120 class TimedTransport;
Daniel Lee93562522019-05-03 14:40:13 +0200121 // Constraints including overhead.
122 struct TargetAudioBitrateConstraints {
123 DataRate min;
124 DataRate max;
125 };
sazac58f8c02017-07-19 00:39:19 -0700126
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100127 internal::AudioState* audio_state();
128 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800129
Artem Titova2088612021-02-03 13:33:28 +0100130 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels)
131 RTC_RUN_ON(worker_thread_checker_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100132
Florent Castelliacabb362022-10-18 17:05:16 +0200133 void ConfigureStream(const Config& new_config,
134 bool first_time,
135 SetParametersCallback callback)
Artem Titova2088612021-02-03 13:33:28 +0100136 RTC_RUN_ON(worker_thread_checker_);
137 bool SetupSendCodec(const Config& new_config)
138 RTC_RUN_ON(worker_thread_checker_);
139 bool ReconfigureSendCodec(const Config& new_config)
140 RTC_RUN_ON(worker_thread_checker_);
141 void ReconfigureANA(const Config& new_config)
142 RTC_RUN_ON(worker_thread_checker_);
143 void ReconfigureCNG(const Config& new_config)
144 RTC_RUN_ON(worker_thread_checker_);
145 void ReconfigureBitrateObserver(const Config& new_config)
146 RTC_RUN_ON(worker_thread_checker_);
ossu20a4b3f2017-04-27 02:08:52 -0700147
Artem Titova2088612021-02-03 13:33:28 +0100148 void ConfigureBitrateObserver() RTC_RUN_ON(worker_thread_checker_);
149 void RemoveBitrateObserver() RTC_RUN_ON(worker_thread_checker_);
minyue7a973442016-10-20 03:27:12 -0700150
Daniel Lee93562522019-05-03 14:40:13 +0200151 // Returns bitrate constraints, maybe including overhead when enabled by
152 // field trial.
Artem Titova2088612021-02-03 13:33:28 +0100153 absl::optional<TargetAudioBitrateConstraints> GetMinMaxBitrateConstraints()
154 const RTC_RUN_ON(worker_thread_checker_);
Daniel Lee93562522019-05-03 14:40:13 +0200155
Anton Sukhanov626015d2019-02-04 15:16:06 -0800156 // Sets per-packet overhead on encoded (for ANA) based on current known values
157 // of transport and packetization overheads.
158 void UpdateOverheadForEncoder()
159 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
160
161 // Returns combined per-packet overhead.
162 size_t GetPerPacketOverheadBytes() const
163 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
164
Artem Titova2088612021-02-03 13:33:28 +0100165 void RegisterCngPayloadType(int payload_type, int clockrate_hz)
166 RTC_RUN_ON(worker_thread_checker_);
167
168 void UpdateCachedTargetAudioBitrateConstraints()
169 RTC_RUN_ON(worker_thread_checker_);
170
Sebastian Jansson977b3352019-03-04 17:43:34 +0100171 Clock* clock_;
Jonas Orelande62c2f22022-03-29 11:04:48 +0200172 const FieldTrialsView& field_trials_;
ossu3b9ff382017-04-27 08:03:42 -0700173
Artem Titova2088612021-02-03 13:33:28 +0100174 SequenceChecker worker_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100175 rtc::RaceChecker audio_capture_race_checker_;
Per Kjellander828ef912022-10-10 12:53:41 +0200176 MaybeWorkerThread* rtp_transport_queue_;
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200177
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200178 const bool allocate_audio_without_feedback_;
179 const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
180 const bool enable_audio_alr_probing_;
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200181 const AudioAllocationConfig allocation_settings_;
182
Artem Titova2088612021-02-03 13:33:28 +0100183 webrtc::AudioSendStream::Config config_
184 RTC_GUARDED_BY(worker_thread_checker_);
solenberg566ef242015-11-06 15:34:49 -0800185 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100186 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700187 RtcEventLog* const event_log_;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200188 const bool use_legacy_overhead_calculation_;
solenberg85a04962015-10-27 03:35:21 -0700189
Artem Titova2088612021-02-03 13:33:28 +0100190 int encoder_sample_rate_hz_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
191 size_t encoder_num_channels_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
192 bool sending_ RTC_GUARDED_BY(worker_thread_checker_) = false;
Markus Handell62872802020-07-06 15:15:07 +0200193 mutable Mutex audio_level_lock_;
Henrik Boströmd2c336f2019-07-03 17:11:10 +0200194 // Keeps track of audio level, total audio energy and total samples duration.
195 // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
Sam Zackrissona166a352020-07-06 17:46:36 +0200196 webrtc::voe::AudioLevel audio_level_ RTC_GUARDED_BY(audio_level_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100197
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100198 BitrateAllocatorInterface* const bitrate_allocator_
Markus Handell3907e7b2021-06-01 09:07:20 +0200199 RTC_GUARDED_BY(rtp_transport_queue_);
Artem Titovb0ea6372021-07-26 11:47:07 +0200200 // Constrains cached to be accessed from `rtp_transport_queue_`.
Artem Titova2088612021-02-03 13:33:28 +0100201 absl::optional<AudioSendStream::TargetAudioBitrateConstraints>
Markus Handell3907e7b2021-06-01 09:07:20 +0200202 cached_constraints_ RTC_GUARDED_BY(rtp_transport_queue_) = absl::nullopt;
Niels Möller7d76a312018-10-26 12:57:07 +0200203 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700204
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200205 RtpRtcpInterface* const rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200206 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700207
Alex Narestcedd3512017-12-07 20:54:55 +0100208 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
209 // reserved for padding and MUST NOT be used as a local identifier.
210 // So it should be safe to use 0 here to indicate "not configured".
211 struct ExtensionIds {
212 int audio_level = 0;
Sebastian Jansson71c6b562019-08-14 11:31:02 +0200213 int abs_send_time = 0;
Minyue Li74dadc12020-03-05 11:33:13 +0100214 int abs_capture_time = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100215 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700216 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800217 int rid = 0;
218 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100219 };
220 static ExtensionIds FindExtensionIds(
221 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100222 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100223
Markus Handell62872802020-07-06 15:15:07 +0200224 mutable Mutex overhead_per_packet_lock_;
Erik Språngcf6544a2020-05-13 14:43:11 +0200225 size_t overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
Anton Sukhanov626015d2019-02-04 15:16:06 -0800226
227 // Current transport overhead (ICE, TURN, etc.)
228 size_t transport_overhead_per_packet_bytes_
229 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
230
Artem Titova2088612021-02-03 13:33:28 +0100231 bool registered_with_allocator_ RTC_GUARDED_BY(worker_thread_checker_) =
232 false;
233 size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_thread_checker_) =
234 0;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200235 absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
Artem Titova2088612021-02-03 13:33:28 +0100236 RTC_GUARDED_BY(worker_thread_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700237};
238} // namespace internal
239} // namespace webrtc
240
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200241#endif // AUDIO_AUDIO_SEND_STREAM_H_