blob: a1dab792dd51a2ac360e85a6b2b2eca6f044b7de [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:
solenberg85a04962015-10-27 03:35:21 -070042 AudioSendStream(const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010043 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -070044 rtc::TaskQueue* worker_queue,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010045 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +020046 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020047 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080048 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070049 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 17:15:13 +010050 const absl::optional<RtpState>& suspended_rtp_state);
Niels Möllerdced9f62018-11-19 10:27:07 +010051 // For unit tests, which need to supply a mock ChannelSend.
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010052 AudioSendStream(const webrtc::AudioSendStream::Config& config,
53 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
54 rtc::TaskQueue* worker_queue,
Niels Möller7d76a312018-10-26 12:57:07 +020055 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +020056 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010057 RtcEventLog* event_log,
58 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020059 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 10:27:07 +010060 std::unique_ptr<voe::ChannelSendInterface> channel_send);
solenbergc7a8b082015-10-16 14:35:07 -070061 ~AudioSendStream() override;
62
pbos1ba8d392016-05-01 20:18:34 -070063 // webrtc::AudioSendStream implementation.
eladalonabbc4302017-07-26 02:09:44 -070064 const webrtc::AudioSendStream::Config& GetConfig() const override;
ossu20a4b3f2017-04-27 02:08:52 -070065 void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
solenbergc7a8b082015-10-16 14:35:07 -070066 void Start() override;
67 void Stop() override;
Fredrik Solenberg2a877972017-12-15 16:42:15 +010068 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
Yves Gerey665174f2018-06-19 15:03:05 +020069 bool SendTelephoneEvent(int payload_type,
70 int payload_frequency,
71 int event,
solenberg8842c3e2016-03-11 03:06:41 -080072 int duration_ms) override;
solenberg94218532016-06-16 10:53:22 -070073 void SetMuted(bool muted) override;
solenbergc7a8b082015-10-16 14:35:07 -070074 webrtc::AudioSendStream::Stats GetStats() const override;
Ivo Creusen56d46092017-11-24 17:29:59 +010075 webrtc::AudioSendStream::Stats GetStats(
76 bool has_remote_tracks) const override;
solenbergc7a8b082015-10-16 14:35:07 -070077
pbos1ba8d392016-05-01 20:18:34 -070078 void SignalNetworkState(NetworkState state);
79 bool DeliverRtcp(const uint8_t* packet, size_t length);
mflodman86cc6ff2016-07-26 04:44:06 -070080
81 // Implements BitrateAllocatorObserver.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +020082 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
mflodman86cc6ff2016-07-26 04:44:06 -070083
elad.alond12a8e12017-03-23 11:04:48 -070084 // From PacketFeedbackObserver.
85 void OnPacketAdded(uint32_t ssrc, uint16_t seq_num) override;
86 void OnPacketFeedbackVector(
87 const std::vector<PacketFeedback>& packet_feedback_vector) override;
88
Anton Sukhanov626015d2019-02-04 15:16:06 -080089 void SetTransportOverhead(int transport_overhead_per_packet_bytes);
90
91 // OverheadObserver override reports audio packetization overhead from
92 // RTP/RTCP module or Media Transport.
93 void OnOverheadChanged(size_t overhead_bytes_per_packet_bytes) override;
solenbergc7a8b082015-10-16 14:35:07 -070094
ossuc3d4b482017-05-23 06:07:11 -070095 RtpState GetRtpState() const;
Niels Möllerdced9f62018-11-19 10:27:07 +010096 const voe::ChannelSendInterface* GetChannel() const;
ossuc3d4b482017-05-23 06:07:11 -070097
Anton Sukhanov626015d2019-02-04 15:16:06 -080098 // Returns combined per-packet overhead.
99 size_t TestOnlyGetPerPacketOverheadBytes() const
100 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
101
solenbergc7a8b082015-10-16 14:35:07 -0700102 private:
sazac58f8c02017-07-19 00:39:19 -0700103 class TimedTransport;
104
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100105 internal::AudioState* audio_state();
106 const internal::AudioState* audio_state() const;
solenberg3a941542015-11-16 07:34:50 -0800107
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100108 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
109
ossu20a4b3f2017-04-27 02:08:52 -0700110 // These are all static to make it less likely that (the old) config_ is
111 // accessed unintentionally.
112 static void ConfigureStream(AudioSendStream* stream,
113 const Config& new_config,
114 bool first_time);
115 static bool SetupSendCodec(AudioSendStream* stream, const Config& new_config);
116 static bool ReconfigureSendCodec(AudioSendStream* stream,
117 const Config& new_config);
118 static void ReconfigureANA(AudioSendStream* stream, const Config& new_config);
119 static void ReconfigureCNG(AudioSendStream* stream, const Config& new_config);
120 static void ReconfigureBitrateObserver(AudioSendStream* stream,
121 const Config& new_config);
122
Seth Hampson24722b32017-12-22 09:36:42 -0800123 void ConfigureBitrateObserver(int min_bitrate_bps,
124 int max_bitrate_bps,
Sebastian Jansson79f0d4d2019-01-23 09:41:43 +0100125 double bitrate_priority);
ossu20a4b3f2017-04-27 02:08:52 -0700126 void RemoveBitrateObserver();
minyue7a973442016-10-20 03:27:12 -0700127
Anton Sukhanov626015d2019-02-04 15:16:06 -0800128 // Sets per-packet overhead on encoded (for ANA) based on current known values
129 // of transport and packetization overheads.
130 void UpdateOverheadForEncoder()
131 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
132
133 // Returns combined per-packet overhead.
134 size_t GetPerPacketOverheadBytes() const
135 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
136
ossu3b9ff382017-04-27 08:03:42 -0700137 void RegisterCngPayloadType(int payload_type, int clockrate_hz);
138
elad.alond12a8e12017-03-23 11:04:48 -0700139 rtc::ThreadChecker worker_thread_checker_;
140 rtc::ThreadChecker pacer_thread_checker_;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100141 rtc::RaceChecker audio_capture_race_checker_;
perkj26091b12016-09-01 01:17:40 -0700142 rtc::TaskQueue* worker_queue_;
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100143 const AudioAllocationSettings allocation_settings_;
ossu20a4b3f2017-04-27 02:08:52 -0700144 webrtc::AudioSendStream::Config config_;
solenberg566ef242015-11-06 15:34:49 -0800145 rtc::scoped_refptr<webrtc::AudioState> audio_state_;
Niels Möllerdced9f62018-11-19 10:27:07 +0100146 const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
ossu20a4b3f2017-04-27 02:08:52 -0700147 RtcEventLog* const event_log_;
solenberg85a04962015-10-27 03:35:21 -0700148
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100149 int encoder_sample_rate_hz_ = 0;
150 size_t encoder_num_channels_ = 0;
151 bool sending_ = false;
152
Niels Möller67b011d2018-10-22 13:00:40 +0200153 BitrateAllocatorInterface* const bitrate_allocator_;
Niels Möller7d76a312018-10-26 12:57:07 +0200154 RtpTransportControllerSendInterface* const rtp_transport_;
mflodman86cc6ff2016-07-26 04:44:06 -0700155
elad.alond12a8e12017-03-23 11:04:48 -0700156 rtc::CriticalSection packet_loss_tracker_cs_;
157 TransportFeedbackPacketLossTracker packet_loss_tracker_
danilchapa37de392017-09-09 04:17:22 -0700158 RTC_GUARDED_BY(&packet_loss_tracker_cs_);
elad.alond12a8e12017-03-23 11:04:48 -0700159
ossuc3d4b482017-05-23 06:07:11 -0700160 RtpRtcp* rtp_rtcp_module_;
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200161 absl::optional<RtpState> const suspended_rtp_state_;
ossuc3d4b482017-05-23 06:07:11 -0700162
Alex Narestcedd3512017-12-07 20:54:55 +0100163 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
164 // reserved for padding and MUST NOT be used as a local identifier.
165 // So it should be safe to use 0 here to indicate "not configured".
166 struct ExtensionIds {
167 int audio_level = 0;
168 int transport_sequence_number = 0;
Steve Antonbb50ce52018-03-26 10:24:32 -0700169 int mid = 0;
Amit Hilbuch77938e62018-12-21 09:23:38 -0800170 int rid = 0;
171 int repaired_rid = 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100172 };
173 static ExtensionIds FindExtensionIds(
174 const std::vector<RtpExtension>& extensions);
Sebastian Jansson470a5ea2019-01-23 12:37:49 +0100175 static int TransportSeqNumId(const Config& config);
Alex Narestcedd3512017-12-07 20:54:55 +0100176
Anton Sukhanov626015d2019-02-04 15:16:06 -0800177 rtc::CriticalSection overhead_per_packet_lock_;
178
179 // Current transport overhead (ICE, TURN, etc.)
180 size_t transport_overhead_per_packet_bytes_
181 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
182
183 // Current audio packetization overhead (RTP or Media Transport).
184 size_t audio_overhead_per_packet_bytes_
185 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
186
solenberg85a04962015-10-27 03:35:21 -0700187 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
solenbergc7a8b082015-10-16 14:35:07 -0700188};
189} // namespace internal
190} // namespace webrtc
191
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200192#endif // AUDIO_AUDIO_SEND_STREAM_H_