audio_send_stream.h 8.2 KB

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