audio_send_stream.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/constructor_magic.h"
  22. #include "rtc_base/experiments/struct_parameters_parser.h"
  23. #include "rtc_base/race_checker.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() override;
  71. // webrtc::AudioSendStream implementation.
  72. const webrtc::AudioSendStream::Config& GetConfig() const override;
  73. void Reconfigure(const webrtc::AudioSendStream::Config& config) override;
  74. void Start() override;
  75. void Stop() override;
  76. void SendAudioData(std::unique_ptr<AudioFrame> audio_frame) override;
  77. bool SendTelephoneEvent(int payload_type,
  78. int payload_frequency,
  79. int event,
  80. int duration_ms) override;
  81. void SetMuted(bool muted) override;
  82. webrtc::AudioSendStream::Stats GetStats() const override;
  83. webrtc::AudioSendStream::Stats GetStats(
  84. bool has_remote_tracks) const override;
  85. void DeliverRtcp(const uint8_t* packet, size_t length);
  86. // Implements BitrateAllocatorObserver.
  87. uint32_t OnBitrateUpdated(BitrateAllocationUpdate update) override;
  88. void SetTransportOverhead(int transport_overhead_per_packet_bytes);
  89. RtpState GetRtpState() const;
  90. const voe::ChannelSendInterface* GetChannel() const;
  91. // Returns combined per-packet overhead.
  92. size_t TestOnlyGetPerPacketOverheadBytes() const
  93. RTC_LOCKS_EXCLUDED(overhead_per_packet_lock_);
  94. private:
  95. class TimedTransport;
  96. // Constraints including overhead.
  97. struct TargetAudioBitrateConstraints {
  98. DataRate min;
  99. DataRate max;
  100. };
  101. internal::AudioState* audio_state();
  102. const internal::AudioState* audio_state() const;
  103. void StoreEncoderProperties(int sample_rate_hz, size_t num_channels);
  104. void ConfigureStream(const Config& new_config, bool first_time);
  105. bool SetupSendCodec(const Config& new_config);
  106. bool ReconfigureSendCodec(const Config& new_config);
  107. void ReconfigureANA(const Config& new_config);
  108. void ReconfigureCNG(const Config& new_config);
  109. void ReconfigureBitrateObserver(const Config& new_config);
  110. void ConfigureBitrateObserver() RTC_RUN_ON(worker_queue_);
  111. void RemoveBitrateObserver();
  112. // Returns bitrate constraints, maybe including overhead when enabled by
  113. // field trial.
  114. TargetAudioBitrateConstraints GetMinMaxBitrateConstraints() const
  115. RTC_RUN_ON(worker_queue_);
  116. // Sets per-packet overhead on encoded (for ANA) based on current known values
  117. // of transport and packetization overheads.
  118. void UpdateOverheadForEncoder()
  119. RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
  120. // Returns combined per-packet overhead.
  121. size_t GetPerPacketOverheadBytes() const
  122. RTC_EXCLUSIVE_LOCKS_REQUIRED(overhead_per_packet_lock_);
  123. void RegisterCngPayloadType(int payload_type, int clockrate_hz);
  124. Clock* clock_;
  125. rtc::ThreadChecker worker_thread_checker_;
  126. rtc::ThreadChecker pacer_thread_checker_;
  127. rtc::RaceChecker audio_capture_race_checker_;
  128. rtc::TaskQueue* worker_queue_;
  129. const bool audio_send_side_bwe_;
  130. const bool allocate_audio_without_feedback_;
  131. const bool force_no_audio_feedback_ = allocate_audio_without_feedback_;
  132. const bool enable_audio_alr_probing_;
  133. const bool send_side_bwe_with_overhead_;
  134. const AudioAllocationConfig allocation_settings_;
  135. webrtc::AudioSendStream::Config config_;
  136. rtc::scoped_refptr<webrtc::AudioState> audio_state_;
  137. const std::unique_ptr<voe::ChannelSendInterface> channel_send_;
  138. RtcEventLog* const event_log_;
  139. const bool use_legacy_overhead_calculation_;
  140. int encoder_sample_rate_hz_ = 0;
  141. size_t encoder_num_channels_ = 0;
  142. bool sending_ = false;
  143. rtc::CriticalSection audio_level_lock_;
  144. // Keeps track of audio level, total audio energy and total samples duration.
  145. // https://w3c.github.io/webrtc-stats/#dom-rtcaudiohandlerstats-totalaudioenergy
  146. webrtc::voe::AudioLevel audio_level_;
  147. BitrateAllocatorInterface* const bitrate_allocator_
  148. RTC_GUARDED_BY(worker_queue_);
  149. RtpTransportControllerSendInterface* const rtp_transport_;
  150. RtpRtcpInterface* const rtp_rtcp_module_;
  151. absl::optional<RtpState> const suspended_rtp_state_;
  152. // RFC 5285: Each distinct extension MUST have a unique ID. The value 0 is
  153. // reserved for padding and MUST NOT be used as a local identifier.
  154. // So it should be safe to use 0 here to indicate "not configured".
  155. struct ExtensionIds {
  156. int audio_level = 0;
  157. int abs_send_time = 0;
  158. int abs_capture_time = 0;
  159. int transport_sequence_number = 0;
  160. int mid = 0;
  161. int rid = 0;
  162. int repaired_rid = 0;
  163. };
  164. static ExtensionIds FindExtensionIds(
  165. const std::vector<RtpExtension>& extensions);
  166. static int TransportSeqNumId(const Config& config);
  167. rtc::CriticalSection overhead_per_packet_lock_;
  168. size_t overhead_per_packet_ RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
  169. // Current transport overhead (ICE, TURN, etc.)
  170. size_t transport_overhead_per_packet_bytes_
  171. RTC_GUARDED_BY(overhead_per_packet_lock_) = 0;
  172. bool registered_with_allocator_ RTC_GUARDED_BY(worker_queue_) = false;
  173. size_t total_packet_overhead_bytes_ RTC_GUARDED_BY(worker_queue_) = 0;
  174. absl::optional<std::pair<TimeDelta, TimeDelta>> frame_length_range_
  175. RTC_GUARDED_BY(worker_queue_);
  176. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioSendStream);
  177. };
  178. } // namespace internal
  179. } // namespace webrtc
  180. #endif // AUDIO_AUDIO_SEND_STREAM_H_