tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

audio_send_stream.h (8980B)


      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 <cstddef>
     15 #include <cstdint>
     16 #include <memory>
     17 #include <optional>
     18 #include <utility>
     19 #include <vector>
     20 
     21 #include "api/array_view.h"
     22 #include "api/call/bitrate_allocation.h"
     23 #include "api/environment/environment.h"
     24 #include "api/field_trials_view.h"
     25 #include "api/rtp_parameters.h"
     26 #include "api/rtp_sender_interface.h"
     27 #include "api/scoped_refptr.h"
     28 #include "api/sequence_checker.h"
     29 #include "api/units/data_rate.h"
     30 #include "api/units/time_delta.h"
     31 #include "audio/audio_level.h"
     32 #include "audio/channel_send.h"
     33 #include "call/audio_send_stream.h"
     34 #include "call/audio_state.h"
     35 #include "call/bitrate_allocator.h"
     36 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
     37 #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
     38 #include "rtc_base/experiments/struct_parameters_parser.h"
     39 #include "rtc_base/race_checker.h"
     40 #include "rtc_base/synchronization/mutex.h"
     41 #include "rtc_base/thread_annotations.h"
     42 
     43 namespace webrtc {
     44 class RtcpRttStats;
     45 class RtpTransportControllerSendInterface;
     46 
     47 struct AudioAllocationConfig {
     48  static constexpr char kKey[] = "WebRTC-Audio-Allocation";
     49  // Field Trial configured bitrates to use as overrides over default/user
     50  // configured bitrate range when audio bitrate allocation is enabled.
     51  std::optional<DataRate> min_bitrate;
     52  std::optional<DataRate> max_bitrate;
     53  DataRate priority_bitrate = DataRate::Zero();
     54  // By default the priority_bitrate is compensated for packet overhead.
     55  // Use this flag to configure a raw value instead.
     56  std::optional<DataRate> priority_bitrate_raw;
     57  std::optional<double> bitrate_priority;
     58 
     59  std::unique_ptr<StructParametersParser> Parser();
     60  explicit AudioAllocationConfig(const FieldTrialsView& field_trials);
     61 };
     62 namespace internal {
     63 class AudioState;
     64 
     65 class AudioSendStream final : public webrtc::AudioSendStream,
     66                              public webrtc::BitrateAllocatorObserver {
     67 public:
     68  AudioSendStream(const Environment& env,
     69                  const webrtc::AudioSendStream::Config& config,
     70                  const scoped_refptr<webrtc::AudioState>& audio_state,
     71                  RtpTransportControllerSendInterface* rtp_transport,
     72                  BitrateAllocatorInterface* bitrate_allocator,
     73                  RtcpRttStats* rtcp_rtt_stats,
     74                  const std::optional<RtpState>& suspended_rtp_state);
     75  // For unit tests, which need to supply a mock ChannelSend.
     76  AudioSendStream(const Environment& env,
     77                  const webrtc::AudioSendStream::Config& config,
     78                  const scoped_refptr<webrtc::AudioState>& audio_state,
     79                  RtpTransportControllerSendInterface* rtp_transport,
     80                  BitrateAllocatorInterface* bitrate_allocator,
     81                  const std::optional<RtpState>& suspended_rtp_state,
     82                  std::unique_ptr<voe::ChannelSendInterface> channel_send);
     83 
     84  AudioSendStream() = delete;
     85  AudioSendStream(const AudioSendStream&) = delete;
     86  AudioSendStream& operator=(const AudioSendStream&) = delete;
     87 
     88  ~AudioSendStream() override;
     89 
     90  // webrtc::AudioSendStream implementation.
     91  const webrtc::AudioSendStream::Config& GetConfig() const override;
     92  void Reconfigure(const webrtc::AudioSendStream::Config& config,
     93                   SetParametersCallback callback) override;
     94  void Start() override;
     95  void Stop() override;
     96  void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
     97  bool SendTelephoneEvent(int payload_type,
     98                          int payload_frequency,
     99                          int event,
    100                          int duration_ms) override;
    101  void SetMuted(bool muted) override;
    102  webrtc::AudioSendStream::Stats GetStats() const override;
    103  webrtc::AudioSendStream::Stats GetStats(
    104      bool has_remote_tracks) const override;
    105 
    106  void DeliverRtcp(ArrayView<const uint8_t> packet);
    107 
    108  // Implements BitrateAllocatorObserver.
    109  uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
    110  std::optional<DataRate> GetUsedRate() const override;
    111 
    112  void SetTransportOverhead(int transport_overhead_per_packet_bytes);
    113 
    114  RtpState GetRtpState() const;
    115  const voe::ChannelSendInterface* GetChannel() const;
    116 
    117  // Returns combined per-packet overhead.
    118  size_t TestOnlyGetPerPacketOverheadBytes() const;
    119 
    120 private:
    121  class TimedTransport;
    122  // Constraints including overhead.
    123  struct TargetAudioBitrateConstraints {
    124    DataRate min;
    125    DataRate max;
    126  };
    127 
    128  internal::AudioState* audio_state();
    129  const internal::AudioState* audio_state() const;
    130 
    131  void StoreEncoderProperties(int sample_rate_hz, size_t num_channels)
    132      RTC_RUN_ON(worker_thread_checker_);
    133 
    134  void ConfigureStream(const Config& new_config,
    135                       bool first_time,
    136                       SetParametersCallback callback)
    137      RTC_RUN_ON(worker_thread_checker_);
    138  bool SetupSendCodec(const Config& new_config)
    139      RTC_RUN_ON(worker_thread_checker_);
    140  bool ReconfigureSendCodec(const Config& new_config)
    141      RTC_RUN_ON(worker_thread_checker_);
    142  void ReconfigureANA(const Config& new_config)
    143      RTC_RUN_ON(worker_thread_checker_);
    144  void ReconfigureCNG(const Config& new_config)
    145      RTC_RUN_ON(worker_thread_checker_);
    146  void ReconfigureBitrateObserver(const Config& new_config)
    147      RTC_RUN_ON(worker_thread_checker_);
    148 
    149  void ConfigureBitrateObserver() RTC_RUN_ON(worker_thread_checker_);
    150  void RemoveBitrateObserver() RTC_RUN_ON(worker_thread_checker_);
    151 
    152  // Returns bitrate constraints, maybe including overhead when enabled by
    153  // field trial.
    154  std::optional<TargetAudioBitrateConstraints> GetMinMaxBitrateConstraints()
    155      const RTC_RUN_ON(worker_thread_checker_);
    156 
    157  // Sets per-packet overhead on encoded (for ANA) based on current known values
    158  // of transport and packetization overheads.
    159  void UpdateOverheadPerPacket();
    160 
    161  void RegisterCngPayloadType(int payload_type, int clockrate_hz)
    162      RTC_RUN_ON(worker_thread_checker_);
    163 
    164  const Environment env_;
    165 
    166  SequenceChecker worker_thread_checker_;
    167  RaceChecker audio_capture_race_checker_;
    168 
    169  const bool allocate_audio_without_feedback_;
    170  const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
    171  const bool enable_audio_alr_probing_;
    172  const AudioAllocationConfig allocation_settings_;
    173 
    174  webrtc::AudioSendStream::Config config_
    175      RTC_GUARDED_BY(worker_thread_checker_);
    176  scoped_refptr<webrtc::AudioState> audio_state_;
    177  const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
    178  const bool use_legacy_overhead_calculation_;
    179  const bool enable_priority_bitrate_;
    180 
    181  int encoder_sample_rate_hz_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
    182  size_t encoder_num_channels_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
    183  bool sending_ RTC_GUARDED_BY(worker_thread_checker_) = false;
    184  mutable Mutex audio_level_lock_;
    185  // Keeps track of audio level, total audio energy and total samples duration.
    186  // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
    187  webrtc::voe::AudioLevel audio_level_ RTC_GUARDED_BY(audio_level_lock_);
    188 
    189  BitrateAllocatorInterface* const bitrate_allocator_
    190      RTC_GUARDED_BY(worker_thread_checker_);
    191  RtpTransportControllerSendInterface* const rtp_transport_;
    192 
    193  RtpRtcpInterface* const rtp_rtcp_module_;
    194  std::optional<RtpState> const suspended_rtp_state_;
    195 
    196  // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
    197  // reserved for padding and MUST NOT be used as a local identifier.
    198  // So it should be safe to use 0 here to indicate "not configured".
    199  struct ExtensionIds {
    200    int audio_level = 0;
    201    int abs_send_time = 0;
    202    int abs_capture_time = 0;
    203    int transport_sequence_number = 0;
    204    int mid = 0;
    205    int rid = 0;
    206    int repaired_rid = 0;
    207  };
    208  static ExtensionIds FindExtensionIds(
    209      const std::vector<RtpExtension>& extensions);
    210  static int TransportSeqNumId(const Config& config);
    211 
    212  // Current transport overhead (ICE, TURN, etc.)
    213  size_t transport_overhead_per_packet_bytes_
    214      RTC_GUARDED_BY(worker_thread_checker_) = 0;
    215  // Total overhead, including transport and RTP headers.
    216  size_t overhead_per_packet_ RTC_GUARDED_BY(worker_thread_checker_) = 0;
    217 
    218  bool registered_with_allocator_ RTC_GUARDED_BY(worker_thread_checker_) =
    219      false;
    220  std::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
    221      RTC_GUARDED_BY(worker_thread_checker_);
    222  std::optional<std::pair<DataRate, DataRate>> bitrate_range_
    223      RTC_GUARDED_BY(worker_thread_checker_);
    224 };
    225 }  // namespace internal
    226 }  // namespace webrtc
    227 
    228 #endif  // AUDIO_AUDIO_SEND_STREAM_H_