blob: 54a26c2a6f34282a71aaaa9a4436caa5f750356d [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>
elad.alond12a8e12017-03-23 11:04:48 -070015#include <vector>
kwibergfffa42b2016-02-23 10:46:32 -080016
Niels Möllerdced9f62018-11-19 10:27:07 +010017#include "audio/channel_send.h"
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010018#include "audio/transport_feedback_packet_loss_tracker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "call/audio_send_stream.h"
20#include "call/audio_state.h"
21#include "call/bitrate_allocator.h"
22#include "modules/rtp_rtcp/include/rtp_rtcp.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/constructor_magic.h"
Sebastian Jansson470a5ea2019-01-23 12:37:49 +010024#include "rtc_base/experiments/audio_allocation_settings.h"
Fredrik Solenberg2a877972017-12-15 16:42:15 +010025#include "rtc_base/race_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread_checker.h"
solenbergc7a8b082015-10-16 14:35:07 -070027
28namespace webrtc {
tereliuse035e2d2016-09-21 06:51:47 -070029class RtcEventLog;
stefan7de8d642017-02-07 07:14:08 -080030class RtcpBandwidthObserver;
michaelt9332b7d2016-11-30 07:51:13 -080031class RtcpRttStats;
nisseb8f9a322017-03-27 05:36:15 -070032class RtpTransportControllerSendInterface;
solenberg3a941542015-11-16 07:34:50 -080033
solenberg13725082015-11-25 08:16:52 -080034namespace internal {
Fredrik Solenberg2a877972017-12-15 16:42:15 +010035class AudioState;
36
mflodman86cc6ff2016-07-26 04:44:06 -070037class AudioSendStream final : public webrtc::AudioSendStream,
elad.alond12a8e12017-03-23 11:04:48 -070038 public webrtc::BitrateAllocatorObserver,
Anton Sukhanov626015d2019-02-04 15:16:06 -080039 public webrtc::PacketFeedbackObserver,
40 public webrtc::OverheadObserver {
solenbergc7a8b082015-10-16 14:35:07 -070041 public:
Sebastian Jansson977b3352019-03-04 17:43:34 +010042 AudioSendStream(Clock* clock,
43 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010044 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -070045 rtc::TaskQueue* worker_queue,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010046 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +020047 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020048 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080049 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070050 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 17:15:13 +010051 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010052 // For unit tests, which need to supply a mock ChannelSend.
Sebastian Jansson977b3352019-03-04 17:43:34 +010053 AudioSendStream(Clock* clock,
54 const webrtc::AudioSendStream::Config& config,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010055 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
56 rtc::TaskQueue* worker_queue,
Niels Möller7d76a312018-10-26 12:57:07 +020057 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020058 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010059 RtcEventLog* event_log,
60 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020061 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010062 std::unique_ptr<voe::ChannelSendInterface> channel_send);
solenbergc7a8b082015-10-16 14:35:07 -070063 ~AudioSendStream() override;
64
pbos1ba8d392016-05-01 20:18:34 -070065 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070066 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070067 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070068 void Start() override;
69 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010070 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020071 bool SendTelephoneEvent(int payload_type,
72 int payload_frequency,
73 int event,
solenberg8842c3e2016-03-11 03:06:41 -080074 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070075 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070076 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010077 webrtc::AudioSendStream::Stats GetStats(
78 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070079
pbos1ba8d392016-05-01 20:18:34 -070080 void SignalNetworkState(NetworkState state);
Niels Möller8fb1a6a2019-03-05 14:29:42 +010081 void DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -070082
83 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020084 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -070085
elad.alond12a8e12017-03-23 11:04:48 -070086 // From PacketFeedbackObserver.
87 void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
88 void OnPacketFeedbackVector(
89 const std::vector<PacketFeedback>& packet_feedback_vector) override;
90
Anton Sukhanov626015d2019-02-04 15:16:06 -080091 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
92
93 // OverheadObserver override reports audio packetization overhead from
94 // RTP/RTCP module or Media Transport.
95 void OnOverheadChanged(size_t overhead_bytes_per_packet_bytes) override;
solenbergc7a8b082015-10-16 14:35:07 -070096
ossuc3d4b482017-05-23 06:07:11 -070097 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +010098 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -070099
Anton Sukhanov626015d2019-02-04 15:16:06 -0800100 // Returns combined per-packet overhead.
101 size_t TestOnlyGetPerPacketOverheadBytes() const
102 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
103
solenbergc7a8b082015-10-16 14:35:07 -0700104 private:
sazac58f8c02017-07-19 00:39:19 -0700105 class TimedTransport;
106
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100107 internal::AudioState* audio_state();
108 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800109
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100110 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
111
ossu20a4b3f2017-04-27 02:08:52 -0700112 // These are all static to make it less likely that (the old) config_ is
113 // accessed unintentionally.
114 static void ConfigureStream(AudioSendStream* stream,
115 const Config& new_config,
116 bool first_time);
117 static bool SetupSendCodec(AudioSendStream* stream, const Config& new_config);
118 static bool ReconfigureSendCodec(AudioSendStream* stream,
119 const Config& new_config);
120 static void ReconfigureANA(AudioSendStream* stream, const Config& new_config);
121 static void ReconfigureCNG(AudioSendStream* stream, const Config& new_config);
122 static void ReconfigureBitrateObserver(AudioSendStream* stream,
123 const Config& new_config);
124
Seth Hampson24722b32017-12-22 09:36:42 -0800125 void ConfigureBitrateObserver(int min_bitrate_bps,
126 int max_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100127 double bitrate_priority);
ossu20a4b3f2017-04-27 02:08:52 -0700128 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700129
Anton Sukhanov626015d2019-02-04 15:16:06 -0800130 // Sets per-packet overhead on encoded (for ANA) based on current known values
131 // of transport and packetization overheads.
132 void UpdateOverheadForEncoder()
133 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
134
135 // Returns combined per-packet overhead.
136 size_t GetPerPacketOverheadBytes() const
137 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
138
ossu3b9ff382017-04-27 08:03:42 -0700139 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
Sebastian Jansson977b3352019-03-04 17:43:34 +0100140 Clock* clock_;
ossu3b9ff382017-04-27 08:03:42 -0700141
elad.alond12a8e12017-03-23 11:04:48 -0700142 rtc::ThreadChecker worker_thread_checker_;
143 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100144 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700145 rtc::TaskQueue* worker_queue_;
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100146 const AudioAllocationSettings allocation_settings_;
ossu20a4b3f2017-04-27 02:08:52 -0700147 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800148 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100149 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700150 RtcEventLog* const event_log_;
solenberg85a04962015-10-27 03:35:21 -0700151
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100152 int encoder_sample_rate_hz_ = 0;
153 size_t encoder_num_channels_ = 0;
154 bool sending_ = false;
155
Niels Möller67b011d2018-10-22 13:00:40 +0200156 BitrateAllocatorInterface* const bitrate_allocator_;
Niels Möller7d76a312018-10-26 12:57:07 +0200157 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700158
elad.alond12a8e12017-03-23 11:04:48 -0700159 rtc::CriticalSection packet_loss_tracker_cs_;
160 TransportFeedbackPacketLossTracker packet_loss_tracker_
danilchapa37de392017-09-09 04:17:22 -0700161 RTC_GUARDED_BY(&packet_loss_tracker_cs_);
elad.alond12a8e12017-03-23 11:04:48 -0700162
ossuc3d4b482017-05-23 06:07:11 -0700163 RtpRtcp* rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200164 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700165
Alex Narestcedd3512017-12-07 20:54:55 +0100166 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
167 // reserved for padding and MUST NOT be used as a local identifier.
168 // So it should be safe to use 0 here to indicate "not configured".
169 struct ExtensionIds {
170 int audio_level = 0;
171 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700172 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800173 int rid = 0;
174 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100175 };
176 static ExtensionIds FindExtensionIds(
177 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100178 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100179
Anton Sukhanov626015d2019-02-04 15:16:06 -0800180 rtc::CriticalSection overhead_per_packet_lock_;
181
182 // Current transport overhead (ICE, TURN, etc.)
183 size_t transport_overhead_per_packet_bytes_
184 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
185
186 // Current audio packetization overhead (RTP or Media Transport).
187 size_t audio_overhead_per_packet_bytes_
188 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
189
solenberg85a04962015-10-27 03:35:21 -0700190 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
solenbergc7a8b082015-10-16 14:35:07 -0700191};
192} // namespace internal
193} // namespace webrtc
194
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200195#endif // AUDIO_AUDIO_SEND_STREAM_H_