1 /* 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 11 #ifndef AUDIO_AUDIO_SEND_STREAM_H_ 12 #define AUDIO_AUDIO_SEND_STREAM_H_ 13 14 #include <memory> 15 #include <utility> 16 #include <vector> 17 18 #include "audio/audio_level.h" 19 #include "audio/channel_send.h" 20 #include "call/audio_send_stream.h" 21 #include "call/audio_state.h" 22 #include "call/bitrate_allocator.h" 23 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h" 24 #include "rtc_base/constructor_magic.h" 25 #include "rtc_base/experiments/struct_parameters_parser.h" 26 #include "rtc_base/race_checker.h" 27 #include "rtc_base/synchronization/mutex.h" 28 #include "rtc_base/task_queue.h" 29 #include "rtc_base/thread_checker.h" 30 31 namespace webrtc { 32 class RtcEventLog; 33 class RtcpBandwidthObserver; 34 class RtcpRttStats; 35 class RtpTransportControllerSendInterface; 36 37 struct 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 }; 52 namespace internal { 53 class AudioState; 54 55 class AudioSendStream final : public webrtc::AudioSendStream, 56 public webrtc::BitrateAllocatorObserver { 57 public: 58 AudioSendStream(Clock* clock, 59 const webrtc::AudioSendStream::Config& config, 60 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 61 TaskQueueFactory* task_queue_factory, 62 ProcessThread* module_process_thread, 63 RtpTransportControllerSendInterface* rtp_transport, 64 BitrateAllocatorInterface* bitrate_allocator, 65 RtcEventLog* event_log, 66 RtcpRttStats* rtcp_rtt_stats, 67 const absl::optional<RtpState>& suspended_rtp_state); 68 // For unit tests, which need to supply a mock ChannelSend. 69 AudioSendStream(Clock* clock, 70 const webrtc::AudioSendStream::Config& config, 71 const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 72 TaskQueueFactory* task_queue_factory, 73 RtpTransportControllerSendInterface* rtp_transport, 74 BitrateAllocatorInterface* bitrate_allocator, 75 RtcEventLog* event_log, 76 const absl::optional<RtpState>& suspended_rtp_state, 77 std::unique_ptr<voe::ChannelSendInterface> channel_send); 78 ~AudioSendStream() override; 79 80 // webrtc::AudioSendStream implementation. 81 const webrtc::AudioSendStream::Config& GetConfig() const override; 82 void Reconfigure(const webrtc::AudioSendStream::Config& config) override; 83 void Start() override; 84 void Stop() override; 85 void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override; 86 bool SendTelephoneEvent(int payload_type, 87 int payload_frequency, 88 int event, 89 int duration_ms) override; 90 void SetMuted(bool muted) override; 91 webrtc::AudioSendStream::Stats GetStats() const override; 92 webrtc::AudioSendStream::Stats GetStats( 93 bool has_remote_tracks) const override; 94 95 void DeliverRtcp(const uint8_t* packet, size_t length); 96 97 // Implements BitrateAllocatorObserver. 98 uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override; 99 100 void SetTransportOverhead(int transport_overhead_per_packet_bytes); 101 102 RtpState GetRtpState() const; 103 const voe::ChannelSendInterface* GetChannel() const; 104 105 // Returns combined per-packet overhead. 106 size_t TestOnlyGetPerPacketOverheadBytes() const 107 RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_); 108 109 private: 110 class TimedTransport; 111 // Constraints including overhead. 112 struct TargetAudioBitrateConstraints { 113 DataRate min; 114 DataRate max; 115 }; 116 117 internal::AudioState* audio_state(); 118 const internal::AudioState* audio_state() const; 119 120 void StoreEncoderProperties(int sample_rate_hz, size_t num_channels); 121 122 void ConfigureStream(const Config& new_config, bool first_time); 123 bool SetupSendCodec(const Config& new_config); 124 bool ReconfigureSendCodec(const Config& new_config); 125 void ReconfigureANA(const Config& new_config); 126 void ReconfigureCNG(const Config& new_config); 127 void ReconfigureBitrateObserver(const Config& new_config); 128 129 void ConfigureBitrateObserver() RTC_RUN_ON(worker_queue_); 130 void RemoveBitrateObserver(); 131 132 // Returns bitrate constraints, maybe including overhead when enabled by 133 // field trial. 134 TargetAudioBitrateConstraints GetMinMaxBitrateConstraints() const 135 RTC_RUN_ON(worker_queue_); 136 137 // Sets per-packet overhead on encoded (for ANA) based on current known values 138 // of transport and packetization overheads. 139 void UpdateOverheadForEncoder() 140 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_); 141 142 // Returns combined per-packet overhead. 143 size_t GetPerPacketOverheadBytes() const 144 RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_); 145 146 void RegisterCngPayloadType(int payload_type, int clockrate_hz); 147 Clock* clock_; 148 149 rtc::ThreadChecker worker_thread_checker_; 150 rtc::ThreadChecker pacer_thread_checker_; 151 rtc::RaceChecker audio_capture_race_checker_; 152 rtc::TaskQueue* worker_queue_; 153 154 const bool audio_send_side_bwe_; 155 const bool allocate_audio_without_feedback_; 156 const bool force_no_audio_feedback_ = allocate_audio_without_feedback_; 157 const bool enable_audio_alr_probing_; 158 const bool send_side_bwe_with_overhead_; 159 const AudioAllocationConfig allocation_settings_; 160 161 webrtc::AudioSendStream::Config config_; 162 rtc::scoped_refptr<webrtc::AudioState> audio_state_; 163 const std::unique_ptr<voe::ChannelSendInterface> channel_send_; 164 RtcEventLog* const event_log_; 165 const bool use_legacy_overhead_calculation_; 166 167 int encoder_sample_rate_hz_ = 0; 168 size_t encoder_num_channels_ = 0; 169 bool sending_ = false; 170 mutable Mutex audio_level_lock_; 171 // Keeps track of audio level, total audio energy and total samples duration. 172 // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy 173 webrtc::voe::AudioLevel audio_level_ RTC_GUARDED_BY(audio_level_lock_); 174 175 BitrateAllocatorInterface* const bitrate_allocator_ 176 RTC_GUARDED_BY(worker_queue_); 177 RtpTransportControllerSendInterface* const rtp_transport_; 178 179 RtpRtcpInterface* const rtp_rtcp_module_; 180 absl::optional<RtpState> const suspended_rtp_state_; 181 182 // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is 183 // reserved for padding and MUST NOT be used as a local identifier. 184 // So it should be safe to use 0 here to indicate "not configured". 185 struct ExtensionIds { 186 int audio_level = 0; 187 int abs_send_time = 0; 188 int abs_capture_time = 0; 189 int transport_sequence_number = 0; 190 int mid = 0; 191 int rid = 0; 192 int repaired_rid = 0; 193 }; 194 static ExtensionIds FindExtensionIds( 195 const std::vector<RtpExtension>& extensions); 196 static int TransportSeqNumId(const Config& config); 197 198 mutable Mutex overhead_per_packet_lock_; 199 size_t overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_) = 0; 200 201 // Current transport overhead (ICE, TURN, etc.) 202 size_t transport_overhead_per_packet_bytes_ 203 RTC_GUARDED_BY(overhead_per_packet_lock_) = 0; 204 205 bool registered_with_allocator_ RTC_GUARDED_BY(worker_queue_) = false; 206 size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_queue_) = 0; 207 absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_ 208 RTC_GUARDED_BY(worker_queue_); 209 210 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream); 211 }; 212 } // namespace internal 213 } // namespace webrtc 214 215 #endif // AUDIO_AUDIO_SEND_STREAM_H_ 216