blob: b40750891c3883bf230aaec803a57e70060ecb13 [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
Artem Titovd15a5752021-02-10 14:31:24 +010018#include "api/sequence_checker.h"
Henrik Boströmd2c336f2019-07-03 17:11:10 +020019#include "audio/audio_level.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010020#include "audio/channel_send.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "call/audio_send_stream.h"
22#include "call/audio_state.h"
23#include "call/bitrate_allocator.h"
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020024#include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
Sebastian Janssonf23131f2019-10-03 10:03:55 +020025#include "rtc_base/experiments/struct_parameters_parser.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010026#include "rtc_base/race_checker.h"
Markus Handell62872802020-07-06 15:15:07 +020027#include "rtc_base/synchronization/mutex.h"
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010028#include "rtc_base/task_queue.h"
solenbergc7a8b082015-10-16 14:35:07 -070029
30namespace webrtc {
tereliuse035e2d2016-09-21 06:51:47 -070031class RtcEventLog;
stefan7de8d642017-02-07 07:14:08 -080032class RtcpBandwidthObserver;
michaelt9332b7d2016-11-30 07:51:13 -080033class RtcpRttStats;
nisseb8f9a322017-03-27 05:36:15 -070034class RtpTransportControllerSendInterface;
solenberg3a941542015-11-16 07:34:50 -080035
Sebastian Janssonf23131f2019-10-03 10:03:55 +020036struct AudioAllocationConfig {
37 static constexpr char kKey[] = "WebRTC-Audio-Allocation";
38 // Field Trial configured bitrates to use as overrides over default/user
39 // configured bitrate range when audio bitrate allocation is enabled.
40 absl::optional<DataRate> min_bitrate;
41 absl::optional<DataRate> max_bitrate;
42 DataRate priority_bitrate = DataRate::Zero();
43 // By default the priority_bitrate is compensated for packet overhead.
44 // Use this flag to configure a raw value instead.
45 absl::optional<DataRate> priority_bitrate_raw;
46 absl::optional<double> bitrate_priority;
47
48 std::unique_ptr<StructParametersParser> Parser();
49 AudioAllocationConfig();
50};
solenberg13725082015-11-25 08:16:52 -080051namespace internal {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010052class AudioState;
53
mflodman86cc6ff2016-07-26 04:44:06 -070054class AudioSendStream final : public webrtc::AudioSendStream,
Erik Språng04e1bab2020-05-07 18:18:32 +020055 public webrtc::BitrateAllocatorObserver {
solenbergc7a8b082015-10-16 14:35:07 -070056 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010057 AudioSendStream(Clock* clock,
58 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010059 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010060 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020061 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020062 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080063 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070064 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 17:15:13 +010065 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010066 // For unit tests, which need to supply a mock ChannelSend.
Sebastian Jansson977b3352019-03-04 17:43:34 +010067 AudioSendStream(Clock* clock,
68 const webrtc::AudioSendStream::Config& config,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010069 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010070 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020071 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020072 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010073 RtcEventLog* event_log,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020074 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010075 std::unique_ptr<voe::ChannelSendInterface> channel_send);
Niels Möllerde953292020-09-29 09:46:21 +020076
77 AudioSendStream() = delete;
78 AudioSendStream(const AudioSendStream&) = delete;
79 AudioSendStream& operator=(const AudioSendStream&) = delete;
80
solenbergc7a8b082015-10-16 14:35:07 -070081 ~AudioSendStream() override;
82
pbos1ba8d392016-05-01 20:18:34 -070083 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070084 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070085 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070086 void Start() override;
87 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010088 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020089 bool SendTelephoneEvent(int payload_type,
90 int payload_frequency,
91 int event,
solenberg8842c3e2016-03-11 03:06:41 -080092 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070093 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070094 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010095 webrtc::AudioSendStream::Stats GetStats(
96 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070097
Niels Möller8fb1a6a2019-03-05 14:29:42 +010098 void DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -070099
100 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200101 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -0700102
Anton Sukhanov626015d2019-02-04 15:16:06 -0800103 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
104
ossuc3d4b482017-05-23 06:07:11 -0700105 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +0100106 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -0700107
Anton Sukhanov626015d2019-02-04 15:16:06 -0800108 // Returns combined per-packet overhead.
109 size_t TestOnlyGetPerPacketOverheadBytes() const
110 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
111
solenbergc7a8b082015-10-16 14:35:07 -0700112 private:
sazac58f8c02017-07-19 00:39:19 -0700113 class TimedTransport;
Daniel Lee93562522019-05-03 14:40:13 +0200114 // Constraints including overhead.
115 struct TargetAudioBitrateConstraints {
116 DataRate min;
117 DataRate max;
118 };
sazac58f8c02017-07-19 00:39:19 -0700119
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100120 internal::AudioState* audio_state();
121 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800122
Artem Titova2088612021-02-03 13:33:28 +0100123 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels)
124 RTC_RUN_ON(worker_thread_checker_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100125
Artem Titova2088612021-02-03 13:33:28 +0100126 void ConfigureStream(const Config& new_config, bool first_time)
127 RTC_RUN_ON(worker_thread_checker_);
128 bool SetupSendCodec(const Config& new_config)
129 RTC_RUN_ON(worker_thread_checker_);
130 bool ReconfigureSendCodec(const Config& new_config)
131 RTC_RUN_ON(worker_thread_checker_);
132 void ReconfigureANA(const Config& new_config)
133 RTC_RUN_ON(worker_thread_checker_);
134 void ReconfigureCNG(const Config& new_config)
135 RTC_RUN_ON(worker_thread_checker_);
136 void ReconfigureBitrateObserver(const Config& new_config)
137 RTC_RUN_ON(worker_thread_checker_);
ossu20a4b3f2017-04-27 02:08:52 -0700138
Artem Titova2088612021-02-03 13:33:28 +0100139 void ConfigureBitrateObserver() RTC_RUN_ON(worker_thread_checker_);
140 void RemoveBitrateObserver() RTC_RUN_ON(worker_thread_checker_);
minyue7a973442016-10-20 03:27:12 -0700141
Daniel Lee93562522019-05-03 14:40:13 +0200142 // Returns bitrate constraints, maybe including overhead when enabled by
143 // field trial.
Artem Titova2088612021-02-03 13:33:28 +0100144 absl::optional<TargetAudioBitrateConstraints> GetMinMaxBitrateConstraints()
145 const RTC_RUN_ON(worker_thread_checker_);
Daniel Lee93562522019-05-03 14:40:13 +0200146
Anton Sukhanov626015d2019-02-04 15:16:06 -0800147 // Sets per-packet overhead on encoded (for ANA) based on current known values
148 // of transport and packetization overheads.
149 void UpdateOverheadForEncoder()
150 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
151
152 // Returns combined per-packet overhead.
153 size_t GetPerPacketOverheadBytes() const
154 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
155
Artem Titova2088612021-02-03 13:33:28 +0100156 void RegisterCngPayloadType(int payload_type, int clockrate_hz)
157 RTC_RUN_ON(worker_thread_checker_);
158
159 void UpdateCachedTargetAudioBitrateConstraints()
160 RTC_RUN_ON(worker_thread_checker_);
161
Sebastian Jansson977b3352019-03-04 17:43:34 +0100162 Clock* clock_;
ossu3b9ff382017-04-27 08:03:42 -0700163
Artem Titova2088612021-02-03 13:33:28 +0100164 SequenceChecker worker_thread_checker_;
165 SequenceChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100166 rtc::RaceChecker audio_capture_race_checker_;
Markus Handell3907e7b2021-06-01 09:07:20 +0200167 rtc::TaskQueue* rtp_transport_queue_;
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200168
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200169 const bool allocate_audio_without_feedback_;
170 const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
171 const bool enable_audio_alr_probing_;
172 const bool send_side_bwe_with_overhead_;
173 const AudioAllocationConfig allocation_settings_;
174
Artem Titova2088612021-02-03 13:33:28 +0100175 webrtc::AudioSendStream::Config config_
176 RTC_GUARDED_BY(worker_thread_checker_);
solenberg566ef242015-11-06 15:34:49 -0800177 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100178 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700179 RtcEventLog* const event_log_;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200180 const bool use_legacy_overhead_calculation_;
solenberg85a04962015-10-27 03:35:21 -0700181
Artem Titova2088612021-02-03 13:33:28 +0100182 int encoder_sample_rate_hz_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
183 size_t encoder_num_channels_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
184 bool sending_ RTC_GUARDED_BY(worker_thread_checker_) = false;
Markus Handell62872802020-07-06 15:15:07 +0200185 mutable Mutex audio_level_lock_;
Henrik Boströmd2c336f2019-07-03 17:11:10 +0200186 // Keeps track of audio level, total audio energy and total samples duration.
187 // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
Sam Zackrissona166a352020-07-06 17:46:36 +0200188 webrtc::voe::AudioLevel audio_level_ RTC_GUARDED_BY(audio_level_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100189
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100190 BitrateAllocatorInterface* const bitrate_allocator_
Markus Handell3907e7b2021-06-01 09:07:20 +0200191 RTC_GUARDED_BY(rtp_transport_queue_);
Artem Titovb0ea6372021-07-26 11:47:07 +0200192 // Constrains cached to be accessed from `rtp_transport_queue_`.
Artem Titova2088612021-02-03 13:33:28 +0100193 absl::optional<AudioSendStream::TargetAudioBitrateConstraints>
Markus Handell3907e7b2021-06-01 09:07:20 +0200194 cached_constraints_ RTC_GUARDED_BY(rtp_transport_queue_) = absl::nullopt;
Niels Möller7d76a312018-10-26 12:57:07 +0200195 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700196
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200197 RtpRtcpInterface* const rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200198 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700199
Alex Narestcedd3512017-12-07 20:54:55 +0100200 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
201 // reserved for padding and MUST NOT be used as a local identifier.
202 // So it should be safe to use 0 here to indicate "not configured".
203 struct ExtensionIds {
204 int audio_level = 0;
Sebastian Jansson71c6b562019-08-14 11:31:02 +0200205 int abs_send_time = 0;
Minyue Li74dadc12020-03-05 11:33:13 +0100206 int abs_capture_time = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100207 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700208 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800209 int rid = 0;
210 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100211 };
212 static ExtensionIds FindExtensionIds(
213 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100214 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100215
Markus Handell62872802020-07-06 15:15:07 +0200216 mutable Mutex overhead_per_packet_lock_;
Erik Språngcf6544a2020-05-13 14:43:11 +0200217 size_t overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
Anton Sukhanov626015d2019-02-04 15:16:06 -0800218
219 // Current transport overhead (ICE, TURN, etc.)
220 size_t transport_overhead_per_packet_bytes_
221 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
222
Artem Titova2088612021-02-03 13:33:28 +0100223 bool registered_with_allocator_ RTC_GUARDED_BY(worker_thread_checker_) =
224 false;
225 size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_thread_checker_) =
226 0;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200227 absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
Artem Titova2088612021-02-03 13:33:28 +0100228 RTC_GUARDED_BY(worker_thread_checker_);
solenbergc7a8b082015-10-16 14:35:07 -0700229};
230} // namespace internal
231} // namespace webrtc
232
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200233#endif // AUDIO_AUDIO_SEND_STREAM_H_