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