blob: 3cd3a6ce48cff437a51fc3af13e5c98f95c01b6d [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"
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010020#include "audio/transport_feedback_packet_loss_tracker.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"
24#include "modules/rtp_rtcp/include/rtp_rtcp.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/constructor_magic.h"
Sebastian Janssonf23131f2019-10-03 10:03:55 +020026#include "rtc_base/experiments/struct_parameters_parser.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010027#include "rtc_base/race_checker.h"
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010028#include "rtc_base/task_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/thread_checker.h"
solenbergc7a8b082015-10-16 14:35:07 -070030
31namespace webrtc {
tereliuse035e2d2016-09-21 06:51:47 -070032class RtcEventLog;
stefan7de8d642017-02-07 07:14:08 -080033class RtcpBandwidthObserver;
michaelt9332b7d2016-11-30 07:51:13 -080034class RtcpRttStats;
nisseb8f9a322017-03-27 05:36:15 -070035class RtpTransportControllerSendInterface;
solenberg3a941542015-11-16 07:34:50 -080036
Sebastian Janssonf23131f2019-10-03 10:03:55 +020037struct AudioAllocationConfig {
38 static constexpr char kKey[] = "WebRTC-Audio-Allocation";
39 // Field Trial configured bitrates to use as overrides over default/user
40 // configured bitrate range when audio bitrate allocation is enabled.
41 absl::optional<DataRate> min_bitrate;
42 absl::optional<DataRate> max_bitrate;
43 DataRate priority_bitrate = DataRate::Zero();
44 // By default the priority_bitrate is compensated for packet overhead.
45 // Use this flag to configure a raw value instead.
46 absl::optional<DataRate> priority_bitrate_raw;
47 absl::optional<double> bitrate_priority;
48
49 std::unique_ptr<StructParametersParser> Parser();
50 AudioAllocationConfig();
51};
solenberg13725082015-11-25 08:16:52 -080052namespace internal {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010053class AudioState;
54
mflodman86cc6ff2016-07-26 04:44:06 -070055class AudioSendStream final : public webrtc::AudioSendStream,
elad.alond12a8e12017-03-23 11:04:48 -070056 public webrtc::BitrateAllocatorObserver,
Anton Sukhanov626015d2019-02-04 15:16:06 -080057 public webrtc::PacketFeedbackObserver,
58 public webrtc::OverheadObserver {
solenbergc7a8b082015-10-16 14:35:07 -070059 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010060 AudioSendStream(Clock* clock,
61 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010062 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010063 TaskQueueFactory* task_queue_factory,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010064 ProcessThread* module_process_thread,
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,
Sam Zackrissonff058162018-11-20 17:15:13 +010069 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010070 // For unit tests, which need to supply a mock ChannelSend.
Sebastian Jansson977b3352019-03-04 17:43:34 +010071 AudioSendStream(Clock* clock,
72 const webrtc::AudioSendStream::Config& config,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010073 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 14:50:30 +010074 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 12:57:07 +020075 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020076 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010077 RtcEventLog* event_log,
78 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020079 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010080 std::unique_ptr<voe::ChannelSendInterface> channel_send);
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
elad.alond12a8e12017-03-23 11:04:48 -0700103 // From PacketFeedbackObserver.
104 void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
105 void OnPacketFeedbackVector(
106 const std::vector<PacketFeedback>& packet_feedback_vector) override;
107
Anton Sukhanov626015d2019-02-04 15:16:06 -0800108 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
109
110 // OverheadObserver override reports audio packetization overhead from
111 // RTP/RTCP module or Media Transport.
112 void OnOverheadChanged(size_t overhead_bytes_per_packet_bytes) override;
solenbergc7a8b082015-10-16 14:35:07 -0700113
ossuc3d4b482017-05-23 06:07:11 -0700114 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +0100115 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -0700116
Anton Sukhanov626015d2019-02-04 15:16:06 -0800117 // Returns combined per-packet overhead.
118 size_t TestOnlyGetPerPacketOverheadBytes() const
119 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
120
solenbergc7a8b082015-10-16 14:35:07 -0700121 private:
sazac58f8c02017-07-19 00:39:19 -0700122 class TimedTransport;
Daniel Lee93562522019-05-03 14:40:13 +0200123 // Constraints including overhead.
124 struct TargetAudioBitrateConstraints {
125 DataRate min;
126 DataRate max;
127 };
sazac58f8c02017-07-19 00:39:19 -0700128
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100129 internal::AudioState* audio_state();
130 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800131
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100132 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
133
ossu20a4b3f2017-04-27 02:08:52 -0700134 // These are all static to make it less likely that (the old) config_ is
135 // accessed unintentionally.
136 static void ConfigureStream(AudioSendStream* stream,
137 const Config& new_config,
138 bool first_time);
139 static bool SetupSendCodec(AudioSendStream* stream, const Config& new_config);
140 static bool ReconfigureSendCodec(AudioSendStream* stream,
141 const Config& new_config);
142 static void ReconfigureANA(AudioSendStream* stream, const Config& new_config);
143 static void ReconfigureCNG(AudioSendStream* stream, const Config& new_config);
144 static void ReconfigureBitrateObserver(AudioSendStream* stream,
145 const Config& new_config);
146
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100147 void ConfigureBitrateObserver() RTC_RUN_ON(worker_queue_);
ossu20a4b3f2017-04-27 02:08:52 -0700148 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700149
Daniel Lee93562522019-05-03 14:40:13 +0200150 // Returns bitrate constraints, maybe including overhead when enabled by
151 // field trial.
Sebastian Jansson62aee932019-10-02 12:27:06 +0200152 TargetAudioBitrateConstraints GetMinMaxBitrateConstraints() const
153 RTC_RUN_ON(worker_queue_);
Daniel Lee93562522019-05-03 14:40:13 +0200154
Anton Sukhanov626015d2019-02-04 15:16:06 -0800155 // Sets per-packet overhead on encoded (for ANA) based on current known values
156 // of transport and packetization overheads.
157 void UpdateOverheadForEncoder()
158 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
159
160 // Returns combined per-packet overhead.
161 size_t GetPerPacketOverheadBytes() const
162 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
163
ossu3b9ff382017-04-27 08:03:42 -0700164 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
Sebastian Jansson977b3352019-03-04 17:43:34 +0100165 Clock* clock_;
ossu3b9ff382017-04-27 08:03:42 -0700166
elad.alond12a8e12017-03-23 11:04:48 -0700167 rtc::ThreadChecker worker_thread_checker_;
168 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100169 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700170 rtc::TaskQueue* worker_queue_;
Sebastian Janssonf23131f2019-10-03 10:03:55 +0200171
172 const bool audio_send_side_bwe_;
173 const bool allocate_audio_without_feedback_;
174 const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
175 const bool enable_audio_alr_probing_;
176 const bool send_side_bwe_with_overhead_;
177 const AudioAllocationConfig allocation_settings_;
178
Yves Gerey17048012019-07-26 17:49:52 +0200179 rtc::CriticalSection config_cs_;
ossu20a4b3f2017-04-27 02:08:52 -0700180 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800181 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100182 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700183 RtcEventLog* const event_log_;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200184 const bool use_legacy_overhead_calculation_;
solenberg85a04962015-10-27 03:35:21 -0700185
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100186 int encoder_sample_rate_hz_ = 0;
187 size_t encoder_num_channels_ = 0;
188 bool sending_ = false;
Henrik Boströmd2c336f2019-07-03 17:11:10 +0200189 rtc::CriticalSection audio_level_lock_;
190 // Keeps track of audio level, total audio energy and total samples duration.
191 // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
192 webrtc::voe::AudioLevel audio_level_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100193
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100194 BitrateAllocatorInterface* const bitrate_allocator_
195 RTC_GUARDED_BY(worker_queue_);
Niels Möller7d76a312018-10-26 12:57:07 +0200196 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700197
elad.alond12a8e12017-03-23 11:04:48 -0700198 rtc::CriticalSection packet_loss_tracker_cs_;
199 TransportFeedbackPacketLossTracker packet_loss_tracker_
danilchapa37de392017-09-09 04:17:22 -0700200 RTC_GUARDED_BY(&packet_loss_tracker_cs_);
elad.alond12a8e12017-03-23 11:04:48 -0700201
ossuc3d4b482017-05-23 06:07:11 -0700202 RtpRtcp* rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200203 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700204
Alex Narestcedd3512017-12-07 20:54:55 +0100205 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
206 // reserved for padding and MUST NOT be used as a local identifier.
207 // So it should be safe to use 0 here to indicate "not configured".
208 struct ExtensionIds {
209 int audio_level = 0;
Sebastian Jansson71c6b562019-08-14 11:31:02 +0200210 int abs_send_time = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100211 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700212 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800213 int rid = 0;
214 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100215 };
216 static ExtensionIds FindExtensionIds(
217 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100218 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100219
Anton Sukhanov626015d2019-02-04 15:16:06 -0800220 rtc::CriticalSection overhead_per_packet_lock_;
221
222 // Current transport overhead (ICE, TURN, etc.)
223 size_t transport_overhead_per_packet_bytes_
224 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
225
226 // Current audio packetization overhead (RTP or Media Transport).
227 size_t audio_overhead_per_packet_bytes_
228 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
229
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100230 bool registered_with_allocator_ RTC_GUARDED_BY(worker_queue_) = false;
231 size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_queue_) = 0;
Sebastian Jansson62aee932019-10-02 12:27:06 +0200232 absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
233 RTC_GUARDED_BY(worker_queue_);
Sebastian Jansson8672cac2019-03-01 15:57:55 +0100234
solenberg85a04962015-10-27 03:35:21 -0700235 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
solenbergc7a8b082015-10-16 14:35:07 -0700236};
237} // namespace internal
238} // namespace webrtc
239
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200240#endif // AUDIO_AUDIO_SEND_STREAM_H_