blob: 1e6982e41f7b2b2b70f39c0361ce70a38ba749c9 [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
Henrik Boströmd2c336f2019-07-03 17:11:10 +020018#include "audio/audio_level.h"
Niels Möllerdced9f62018-11-19 10:27:07 +010019#include "audio/channel_send.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/audio_send_stream.h"
21#include "call/audio_state.h"
22#include "call/bitrate_allocator.h"
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +020023#include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
Sebastian Janssonf23131f2019-10-03 10:03:55 +020024#include "rtc_base/experiments/struct_parameters_parser.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010025#include "rtc_base/race_checker.h"
Markus Handell62872802020-07-06 15:15:07 +020026#include "rtc_base/synchronization/mutex.h"
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010027#include "rtc_base/task_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/thread_checker.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,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010061 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +020062 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020063 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080064 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070065 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 17:15:13 +010066 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010067 // For unit tests, which need to supply a mock ChannelSend.
Sebastian Jansson977b3352019-03-04 17:43:34 +010068 AudioSendStream(Clock* clock,
69 const webrtc::AudioSendStream::Config& config,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010070 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010071 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020072 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020073 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010074 RtcEventLog* event_log,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020075 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010076 std::unique_ptr<voe::ChannelSendInterface> channel_send);
Niels Möllerde953292020-09-29 09:46:21 +020077
78 AudioSendStream() = delete;
79 AudioSendStream(const AudioSendStream&) = delete;
80 AudioSendStream& operator=(const AudioSendStream&) = delete;
81
solenbergc7a8b082015-10-16 14:35:07 -070082 ~AudioSendStream() override;
83
pbos1ba8d392016-05-01 20:18:34 -070084 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070085 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070086 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070087 void Start() override;
88 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010089 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020090 bool SendTelephoneEvent(int payload_type,
91 int payload_frequency,
92 int event,
solenberg8842c3e2016-03-11 03:06:41 -080093 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070094 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070095 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010096 webrtc::AudioSendStream::Stats GetStats(
97 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070098
Niels Möller8fb1a6a2019-03-05 14:29:42 +010099 void DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -0700100
101 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200102 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -0700103
Anton Sukhanov626015d2019-02-04 15:16:06 -0800104 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
105
ossuc3d4b482017-05-23 06:07:11 -0700106 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +0100107 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -0700108
Anton Sukhanov626015d2019-02-04 15:16:06 -0800109 // Returns combined per-packet overhead.
110 size_t TestOnlyGetPerPacketOverheadBytes() const
111 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
112
solenbergc7a8b082015-10-16 14:35:07 -0700113 private:
sazac58f8c02017-07-19 00:39:19 -0700114 class TimedTransport;
Daniel Lee93562522019-05-03 14:40:13 +0200115 // Constraints including overhead.
116 struct TargetAudioBitrateConstraints {
117 DataRate min;
118 DataRate max;
119 };
sazac58f8c02017-07-19 00:39:19 -0700120
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100121 internal::AudioState* audio_state();
122 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800123
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100124 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
125
Sebastian Jansson35cf9e72019-10-04 09:30:32 +0200126 void ConfigureStream(const Config& new_config, bool first_time);
127 bool SetupSendCodec(const Config& new_config);
128 bool ReconfigureSendCodec(const Config& new_config);
129 void ReconfigureANA(const Config& new_config);
130 void ReconfigureCNG(const Config& new_config);
131 void ReconfigureBitrateObserver(const Config& new_config);
ossu20a4b3f2017-04-27 02:08:52 -0700132
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100133 void ConfigureBitrateObserver() RTC_RUN_ON(worker_queue_);
ossu20a4b3f2017-04-27 02:08:52 -0700134 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700135
Daniel Lee93562522019-05-03 14:40:13 +0200136 // Returns bitrate constraints, maybe including overhead when enabled by
137 // field trial.
Sebastian Jansson62aee932019-10-02 12:27:06 +0200138 TargetAudioBitrateConstraints GetMinMaxBitrateConstraints() const
139 RTC_RUN_ON(worker_queue_);
Daniel Lee93562522019-05-03 14:40:13 +0200140
Anton Sukhanov626015d2019-02-04 15:16:06 -0800141 // Sets per-packet overhead on encoded (for ANA) based on current known values
142 // of transport and packetization overheads.
143 void UpdateOverheadForEncoder()
144 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
145
146 // Returns combined per-packet overhead.
147 size_t GetPerPacketOverheadBytes() const
148 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
149
ossu3b9ff382017-04-27 08:03:42 -0700150 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
Sebastian Jansson977b3352019-03-04 17:43:34 +0100151 Clock* clock_;
ossu3b9ff382017-04-27 08:03:42 -0700152
elad.alond12a8e12017-03-23 11:04:48 -0700153 rtc::ThreadChecker worker_thread_checker_;
154 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100155 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700156 rtc::TaskQueue* worker_queue_;
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200157
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200158 const bool allocate_audio_without_feedback_;
159 const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
160 const bool enable_audio_alr_probing_;
161 const bool send_side_bwe_with_overhead_;
162 const AudioAllocationConfig allocation_settings_;
163
ossu20a4b3f2017-04-27 02:08:52 -0700164 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800165 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100166 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700167 RtcEventLog* const event_log_;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200168 const bool use_legacy_overhead_calculation_;
solenberg85a04962015-10-27 03:35:21 -0700169
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100170 int encoder_sample_rate_hz_ = 0;
171 size_t encoder_num_channels_ = 0;
172 bool sending_ = false;
Markus Handell62872802020-07-06 15:15:07 +0200173 mutable Mutex audio_level_lock_;
Henrik Boströmd2c336f2019-07-03 17:11:10 +0200174 // Keeps track of audio level, total audio energy and total samples duration.
175 // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
Sam Zackrissona166a352020-07-06 17:46:36 +0200176 webrtc::voe::AudioLevel audio_level_ RTC_GUARDED_BY(audio_level_lock_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100177
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100178 BitrateAllocatorInterface* const bitrate_allocator_
179 RTC_GUARDED_BY(worker_queue_);
Niels Möller7d76a312018-10-26 12:57:07 +0200180 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700181
Tomas Gunnarssonf25761d2020-06-03 22:55:33 +0200182 RtpRtcpInterface* const rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200183 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700184
Alex Narestcedd3512017-12-07 20:54:55 +0100185 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
186 // reserved for padding and MUST NOT be used as a local identifier.
187 // So it should be safe to use 0 here to indicate "not configured".
188 struct ExtensionIds {
189 int audio_level = 0;
Sebastian Jansson71c6b562019-08-14 11:31:02 +0200190 int abs_send_time = 0;
Minyue Li74dadc12020-03-05 11:33:13 +0100191 int abs_capture_time = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100192 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700193 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800194 int rid = 0;
195 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100196 };
197 static ExtensionIds FindExtensionIds(
198 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100199 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100200
Markus Handell62872802020-07-06 15:15:07 +0200201 mutable Mutex overhead_per_packet_lock_;
Erik Språngcf6544a2020-05-13 14:43:11 +0200202 size_t overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
Anton Sukhanov626015d2019-02-04 15:16:06 -0800203
204 // Current transport overhead (ICE, TURN, etc.)
205 size_t transport_overhead_per_packet_bytes_
206 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
207
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100208 bool registered_with_allocator_ RTC_GUARDED_BY(worker_queue_) = false;
209 size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_queue_) = 0;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200210 absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
211 RTC_GUARDED_BY(worker_queue_);
solenbergc7a8b082015-10-16 14:35:07 -0700212};
213} // namespace internal
214} // namespace webrtc
215
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200216#endif // AUDIO_AUDIO_SEND_STREAM_H_