blob: 1dbb1349213fce4abeef4c7f567bb1b2a06988d2 [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"
23#include "modules/rtp_rtcp/include/rtp_rtcp.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/constructor_magic.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"
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,
elad.alond12a8e12017-03-23 11:04:48 -070055 public webrtc::BitrateAllocatorObserver,
Anton Sukhanov626015d2019-02-04 15:16:06 -080056 public webrtc::OverheadObserver {
solenbergc7a8b082015-10-16 14:35:07 -070057 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010058 AudioSendStream(Clock* clock,
59 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010060 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010061 TaskQueueFactory* task_queue_factory,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010062 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +020063 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020064 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080065 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070066 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 17:15:13 +010067 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010068 // For unit tests, which need to supply a mock ChannelSend.
Sebastian Jansson977b3352019-03-04 17:43:34 +010069 AudioSendStream(Clock* clock,
70 const webrtc::AudioSendStream::Config& config,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010071 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010072 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020073 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020074 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010075 RtcEventLog* event_log,
76 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020077 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010078 std::unique_ptr<voe::ChannelSendInterface> channel_send);
solenbergc7a8b082015-10-16 14:35:07 -070079 ~AudioSendStream() override;
80
pbos1ba8d392016-05-01 20:18:34 -070081 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070082 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070083 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070084 void Start() override;
85 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010086 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020087 bool SendTelephoneEvent(int payload_type,
88 int payload_frequency,
89 int event,
solenberg8842c3e2016-03-11 03:06:41 -080090 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070091 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070092 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010093 webrtc::AudioSendStream::Stats GetStats(
94 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070095
Niels Möller8fb1a6a2019-03-05 14:29:42 +010096 void DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -070097
98 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020099 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -0700100
Anton Sukhanov626015d2019-02-04 15:16:06 -0800101 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
102
103 // OverheadObserver override reports audio packetization overhead from
104 // RTP/RTCP module or Media Transport.
105 void OnOverheadChanged(size_t overhead_bytes_per_packet_bytes) override;
solenbergc7a8b082015-10-16 14:35:07 -0700106
ossuc3d4b482017-05-23 06:07:11 -0700107 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +0100108 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -0700109
Anton Sukhanov626015d2019-02-04 15:16:06 -0800110 // Returns combined per-packet overhead.
111 size_t TestOnlyGetPerPacketOverheadBytes() const
112 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
113
solenbergc7a8b082015-10-16 14:35:07 -0700114 private:
sazac58f8c02017-07-19 00:39:19 -0700115 class TimedTransport;
Daniel Lee93562522019-05-03 14:40:13 +0200116 // Constraints including overhead.
117 struct TargetAudioBitrateConstraints {
118 DataRate min;
119 DataRate max;
120 };
sazac58f8c02017-07-19 00:39:19 -0700121
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100122 internal::AudioState* audio_state();
123 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800124
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100125 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
126
Sebastian Jansson35cf9e72019-10-04 09:30:32 +0200127 void ConfigureStream(const Config& new_config, bool first_time);
128 bool SetupSendCodec(const Config& new_config);
129 bool ReconfigureSendCodec(const Config& new_config);
130 void ReconfigureANA(const Config& new_config);
131 void ReconfigureCNG(const Config& new_config);
132 void ReconfigureBitrateObserver(const Config& new_config);
ossu20a4b3f2017-04-27 02:08:52 -0700133
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100134 void ConfigureBitrateObserver() RTC_RUN_ON(worker_queue_);
ossu20a4b3f2017-04-27 02:08:52 -0700135 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700136
Daniel Lee93562522019-05-03 14:40:13 +0200137 // Returns bitrate constraints, maybe including overhead when enabled by
138 // field trial.
Sebastian Jansson62aee932019-10-02 12:27:06 +0200139 TargetAudioBitrateConstraints GetMinMaxBitrateConstraints() const
140 RTC_RUN_ON(worker_queue_);
Daniel Lee93562522019-05-03 14:40:13 +0200141
Anton Sukhanov626015d2019-02-04 15:16:06 -0800142 // Sets per-packet overhead on encoded (for ANA) based on current known values
143 // of transport and packetization overheads.
144 void UpdateOverheadForEncoder()
145 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
146
147 // Returns combined per-packet overhead.
148 size_t GetPerPacketOverheadBytes() const
149 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
150
ossu3b9ff382017-04-27 08:03:42 -0700151 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
Sebastian Jansson977b3352019-03-04 17:43:34 +0100152 Clock* clock_;
ossu3b9ff382017-04-27 08:03:42 -0700153
elad.alond12a8e12017-03-23 11:04:48 -0700154 rtc::ThreadChecker worker_thread_checker_;
155 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100156 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700157 rtc::TaskQueue* worker_queue_;
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200158
159 const bool audio_send_side_bwe_;
160 const bool allocate_audio_without_feedback_;
161 const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
162 const bool enable_audio_alr_probing_;
163 const bool send_side_bwe_with_overhead_;
164 const AudioAllocationConfig allocation_settings_;
165
ossu20a4b3f2017-04-27 02:08:52 -0700166 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800167 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100168 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700169 RtcEventLog* const event_log_;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200170 const bool use_legacy_overhead_calculation_;
solenberg85a04962015-10-27 03:35:21 -0700171
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100172 int encoder_sample_rate_hz_ = 0;
173 size_t encoder_num_channels_ = 0;
174 bool sending_ = false;
Henrik Boströmd2c336f2019-07-03 17:11:10 +0200175 rtc::CriticalSection audio_level_lock_;
176 // Keeps track of audio level, total audio energy and total samples duration.
177 // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
178 webrtc::voe::AudioLevel audio_level_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100179
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100180 BitrateAllocatorInterface* const bitrate_allocator_
181 RTC_GUARDED_BY(worker_queue_);
Niels Möller7d76a312018-10-26 12:57:07 +0200182 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700183
Sebastian Jansson6298b562020-01-14 17:55:19 +0100184 RtpRtcp* const rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200185 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700186
Alex Narestcedd3512017-12-07 20:54:55 +0100187 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
188 // reserved for padding and MUST NOT be used as a local identifier.
189 // So it should be safe to use 0 here to indicate "not configured".
190 struct ExtensionIds {
191 int audio_level = 0;
Sebastian Jansson71c6b562019-08-14 11:31:02 +0200192 int abs_send_time = 0;
Minyue Li74dadc12020-03-05 11:33:13 +0100193 int abs_capture_time = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100194 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700195 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800196 int rid = 0;
197 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100198 };
199 static ExtensionIds FindExtensionIds(
200 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100201 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100202
Anton Sukhanov626015d2019-02-04 15:16:06 -0800203 rtc::CriticalSection overhead_per_packet_lock_;
204
205 // Current transport overhead (ICE, TURN, etc.)
206 size_t transport_overhead_per_packet_bytes_
207 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
208
209 // Current audio packetization overhead (RTP or Media Transport).
210 size_t audio_overhead_per_packet_bytes_
211 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
212
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100213 bool registered_with_allocator_ RTC_GUARDED_BY(worker_queue_) = false;
214 size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_queue_) = 0;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200215 absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
216 RTC_GUARDED_BY(worker_queue_);
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100217
solenberg85a04962015-10-27 03:35:21 -0700218 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
solenbergc7a8b082015-10-16 14:35:07 -0700219};
220} // namespace internal
221} // namespace webrtc
222
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200223#endif // AUDIO_AUDIO_SEND_STREAM_H_