audio_receive_stream.h (7202B)
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_RECEIVE_STREAM_H_ 12 #define AUDIO_AUDIO_RECEIVE_STREAM_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <map> 17 #include <memory> 18 #include <optional> 19 #include <string> 20 #include <vector> 21 22 #include "absl/strings/string_view.h" 23 #include "api/array_view.h" 24 #include "api/audio/audio_frame.h" 25 #include "api/audio/audio_mixer.h" 26 #include "api/audio_codecs/audio_format.h" 27 #include "api/crypto/frame_decryptor_interface.h" 28 #include "api/environment/environment.h" 29 #include "api/frame_transformer_interface.h" 30 #include "api/neteq/neteq_factory.h" 31 #include "api/rtp_headers.h" 32 #include "api/scoped_refptr.h" 33 #include "api/sequence_checker.h" 34 #include "api/transport/rtp/rtp_source.h" 35 #include "api/units/time_delta.h" 36 #include "api/units/timestamp.h" 37 // This can be removed after Bug 1768116 enables 38 // c++20 builds across the entire Mozilla tree. 39 #if !defined(WEBRTC_MOZILLA_BUILD) 40 #include "audio/audio_state.h" 41 #endif 42 #include "call/audio_receive_stream.h" 43 #include "call/audio_state.h" 44 #include "call/syncable.h" 45 #include "rtc_base/system/no_unique_address.h" 46 #include "rtc_base/thread_annotations.h" 47 #include "system_wrappers/include/ntp_time.h" 48 49 namespace webrtc { 50 class PacketRouter; 51 class RtpStreamReceiverControllerInterface; 52 class RtpStreamReceiverInterface; 53 54 // This can be removed after Bug 1768116 enables 55 // c++20 builds across the entire Mozilla tree. 56 #if defined(WEBRTC_MOZILLA_BUILD) 57 namespace internal { 58 class AudioState; 59 } 60 #endif 61 62 namespace voe { 63 class ChannelReceiveInterface; 64 } // namespace voe 65 66 class AudioReceiveStreamImpl final : public webrtc::AudioReceiveStreamInterface, 67 public AudioMixer::Source, 68 public Syncable { 69 public: 70 AudioReceiveStreamImpl( 71 const Environment& env, 72 PacketRouter* packet_router, 73 NetEqFactory* neteq_factory, 74 const webrtc::AudioReceiveStreamInterface::Config& config, 75 const scoped_refptr<webrtc::AudioState>& audio_state); 76 // For unit tests, which need to supply a mock channel receive. 77 AudioReceiveStreamImpl( 78 const Environment& env, 79 PacketRouter* packet_router, 80 const webrtc::AudioReceiveStreamInterface::Config& config, 81 const scoped_refptr<webrtc::AudioState>& audio_state, 82 std::unique_ptr<voe::ChannelReceiveInterface> channel_receive); 83 84 AudioReceiveStreamImpl() = delete; 85 AudioReceiveStreamImpl(const AudioReceiveStreamImpl&) = delete; 86 AudioReceiveStreamImpl& operator=(const AudioReceiveStreamImpl&) = delete; 87 88 // Destruction happens on the worker thread. Prior to destruction the caller 89 // must ensure that a registration with the transport has been cleared. See 90 // `RegisterWithTransport` for details. 91 // TODO(tommi): As a further improvement to this, performing the full 92 // destruction on the network thread could be made the default. 93 ~AudioReceiveStreamImpl() override; 94 95 // Called on the network thread to register/unregister with the network 96 // transport. 97 void RegisterWithTransport( 98 RtpStreamReceiverControllerInterface* receiver_controller); 99 // If registration has previously been done (via `RegisterWithTransport`) then 100 // `UnregisterFromTransport` must be called prior to destruction, on the 101 // network thread. 102 void UnregisterFromTransport(); 103 104 // webrtc::AudioReceiveStreamInterface implementation. 105 void Start() override; 106 void Stop() override; 107 bool IsRunning() const override; 108 void SetDepacketizerToDecoderFrameTransformer( 109 scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) 110 override; 111 void SetDecoderMap(std::map<int, SdpAudioFormat> decoder_map) override; 112 void SetNackHistory(int history_ms) override; 113 void SetRtcpMode(RtcpMode mode) override; 114 void SetNonSenderRttMeasurement(bool enabled) override; 115 void SetFrameDecryptor( 116 scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) override; 117 118 webrtc::AudioReceiveStreamInterface::Stats GetStats( 119 bool get_and_clear_legacy_stats) const override; 120 void SetSink(AudioSinkInterface* sink) override; 121 void SetGain(float gain) override; 122 bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override; 123 int GetBaseMinimumPlayoutDelayMs() const override; 124 std::vector<webrtc::RtpSource> GetSources() const override; 125 AudioMixer::Source* source() override { return this; } 126 127 // AudioMixer::Source 128 AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz, 129 AudioFrame* audio_frame) override; 130 int Ssrc() const override; 131 int PreferredSampleRate() const override; 132 133 // Syncable 134 uint32_t id() const override; 135 std::optional<Syncable::Info> GetInfo() const override; 136 std::optional<Syncable::PlayoutInfo> GetPlayoutRtpTimestamp() const override; 137 void SetEstimatedPlayoutNtpTimestamp(NtpTime ntp_time, 138 Timestamp time) override; 139 bool SetMinimumPlayoutDelay(TimeDelta delay) override; 140 141 void DeliverRtcp(ArrayView<const uint8_t> packet); 142 143 void SetSyncGroup(absl::string_view sync_group); 144 145 void SetLocalSsrc(uint32_t local_ssrc); 146 147 uint32_t local_ssrc() const; 148 149 uint32_t remote_ssrc() const override { 150 // The remote_ssrc member variable of config_ will never change and can be 151 // considered const. 152 return config_.rtp.remote_ssrc; 153 } 154 155 // Returns a reference to the currently set sync group of the stream. 156 // Must be called on the packet delivery thread. 157 const std::string& sync_group() const; 158 159 // TODO(tommi): Remove this method. 160 void ReconfigureForTesting( 161 const webrtc::AudioReceiveStreamInterface::Config& config); 162 163 private: 164 internal::AudioState* audio_state() const; 165 166 const Environment env_; 167 RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_thread_checker_; 168 // TODO(bugs.webrtc.org/11993): This checker conceptually represents 169 // operations that belong to the network thread. The Call class is currently 170 // moving towards handling network packets on the network thread and while 171 // that work is ongoing, this checker may in practice represent the worker 172 // thread, but still serves as a mechanism of grouping together concepts 173 // that belong to the network thread. Once the packets are fully delivered 174 // on the network thread, this comment will be deleted. 175 RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_{ 176 SequenceChecker::kDetached}; 177 webrtc::AudioReceiveStreamInterface::Config config_; 178 scoped_refptr<webrtc::AudioState> audio_state_; 179 const std::unique_ptr<voe::ChannelReceiveInterface> channel_receive_; 180 181 bool playing_ RTC_GUARDED_BY(worker_thread_checker_) = false; 182 183 std::unique_ptr<RtpStreamReceiverInterface> rtp_stream_receiver_ 184 RTC_GUARDED_BY(packet_sequence_checker_); 185 }; 186 } // namespace webrtc 187 188 #endif // AUDIO_AUDIO_RECEIVE_STREAM_H_