1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef AUDIO_AUDIO_RECEIVE_STREAM_H_ 12*d9f75844SAndroid Build Coastguard Worker #define AUDIO_AUDIO_RECEIVE_STREAM_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <map> 15*d9f75844SAndroid Build Coastguard Worker #include <memory> 16*d9f75844SAndroid Build Coastguard Worker #include <string> 17*d9f75844SAndroid Build Coastguard Worker #include <vector> 18*d9f75844SAndroid Build Coastguard Worker 19*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h" 20*d9f75844SAndroid Build Coastguard Worker #include "api/audio/audio_mixer.h" 21*d9f75844SAndroid Build Coastguard Worker #include "api/neteq/neteq_factory.h" 22*d9f75844SAndroid Build Coastguard Worker #include "api/rtp_headers.h" 23*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h" 24*d9f75844SAndroid Build Coastguard Worker #include "audio/audio_state.h" 25*d9f75844SAndroid Build Coastguard Worker #include "call/audio_receive_stream.h" 26*d9f75844SAndroid Build Coastguard Worker #include "call/syncable.h" 27*d9f75844SAndroid Build Coastguard Worker #include "modules/rtp_rtcp/source/source_tracker.h" 28*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/no_unique_address.h" 29*d9f75844SAndroid Build Coastguard Worker #include "system_wrappers/include/clock.h" 30*d9f75844SAndroid Build Coastguard Worker 31*d9f75844SAndroid Build Coastguard Worker namespace webrtc { 32*d9f75844SAndroid Build Coastguard Worker class PacketRouter; 33*d9f75844SAndroid Build Coastguard Worker class RtcEventLog; 34*d9f75844SAndroid Build Coastguard Worker class RtpStreamReceiverControllerInterface; 35*d9f75844SAndroid Build Coastguard Worker class RtpStreamReceiverInterface; 36*d9f75844SAndroid Build Coastguard Worker 37*d9f75844SAndroid Build Coastguard Worker namespace voe { 38*d9f75844SAndroid Build Coastguard Worker class ChannelReceiveInterface; 39*d9f75844SAndroid Build Coastguard Worker } // namespace voe 40*d9f75844SAndroid Build Coastguard Worker 41*d9f75844SAndroid Build Coastguard Worker namespace internal { 42*d9f75844SAndroid Build Coastguard Worker class AudioSendStream; 43*d9f75844SAndroid Build Coastguard Worker } // namespace internal 44*d9f75844SAndroid Build Coastguard Worker 45*d9f75844SAndroid Build Coastguard Worker class AudioReceiveStreamImpl final : public webrtc::AudioReceiveStreamInterface, 46*d9f75844SAndroid Build Coastguard Worker public AudioMixer::Source, 47*d9f75844SAndroid Build Coastguard Worker public Syncable { 48*d9f75844SAndroid Build Coastguard Worker public: 49*d9f75844SAndroid Build Coastguard Worker AudioReceiveStreamImpl( 50*d9f75844SAndroid Build Coastguard Worker Clock* clock, 51*d9f75844SAndroid Build Coastguard Worker PacketRouter* packet_router, 52*d9f75844SAndroid Build Coastguard Worker NetEqFactory* neteq_factory, 53*d9f75844SAndroid Build Coastguard Worker const webrtc::AudioReceiveStreamInterface::Config& config, 54*d9f75844SAndroid Build Coastguard Worker const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 55*d9f75844SAndroid Build Coastguard Worker webrtc::RtcEventLog* event_log); 56*d9f75844SAndroid Build Coastguard Worker // For unit tests, which need to supply a mock channel receive. 57*d9f75844SAndroid Build Coastguard Worker AudioReceiveStreamImpl( 58*d9f75844SAndroid Build Coastguard Worker Clock* clock, 59*d9f75844SAndroid Build Coastguard Worker PacketRouter* packet_router, 60*d9f75844SAndroid Build Coastguard Worker const webrtc::AudioReceiveStreamInterface::Config& config, 61*d9f75844SAndroid Build Coastguard Worker const rtc::scoped_refptr<webrtc::AudioState>& audio_state, 62*d9f75844SAndroid Build Coastguard Worker webrtc::RtcEventLog* event_log, 63*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<voe::ChannelReceiveInterface> channel_receive); 64*d9f75844SAndroid Build Coastguard Worker 65*d9f75844SAndroid Build Coastguard Worker AudioReceiveStreamImpl() = delete; 66*d9f75844SAndroid Build Coastguard Worker AudioReceiveStreamImpl(const AudioReceiveStreamImpl&) = delete; 67*d9f75844SAndroid Build Coastguard Worker AudioReceiveStreamImpl& operator=(const AudioReceiveStreamImpl&) = delete; 68*d9f75844SAndroid Build Coastguard Worker 69*d9f75844SAndroid Build Coastguard Worker // Destruction happens on the worker thread. Prior to destruction the caller 70*d9f75844SAndroid Build Coastguard Worker // must ensure that a registration with the transport has been cleared. See 71*d9f75844SAndroid Build Coastguard Worker // `RegisterWithTransport` for details. 72*d9f75844SAndroid Build Coastguard Worker // TODO(tommi): As a further improvement to this, performing the full 73*d9f75844SAndroid Build Coastguard Worker // destruction on the network thread could be made the default. 74*d9f75844SAndroid Build Coastguard Worker ~AudioReceiveStreamImpl() override; 75*d9f75844SAndroid Build Coastguard Worker 76*d9f75844SAndroid Build Coastguard Worker // Called on the network thread to register/unregister with the network 77*d9f75844SAndroid Build Coastguard Worker // transport. 78*d9f75844SAndroid Build Coastguard Worker void RegisterWithTransport( 79*d9f75844SAndroid Build Coastguard Worker RtpStreamReceiverControllerInterface* receiver_controller); 80*d9f75844SAndroid Build Coastguard Worker // If registration has previously been done (via `RegisterWithTransport`) then 81*d9f75844SAndroid Build Coastguard Worker // `UnregisterFromTransport` must be called prior to destruction, on the 82*d9f75844SAndroid Build Coastguard Worker // network thread. 83*d9f75844SAndroid Build Coastguard Worker void UnregisterFromTransport(); 84*d9f75844SAndroid Build Coastguard Worker 85*d9f75844SAndroid Build Coastguard Worker // webrtc::AudioReceiveStreamInterface implementation. 86*d9f75844SAndroid Build Coastguard Worker void Start() override; 87*d9f75844SAndroid Build Coastguard Worker void Stop() override; 88*d9f75844SAndroid Build Coastguard Worker bool transport_cc() const override; 89*d9f75844SAndroid Build Coastguard Worker void SetTransportCc(bool transport_cc) override; 90*d9f75844SAndroid Build Coastguard Worker bool IsRunning() const override; 91*d9f75844SAndroid Build Coastguard Worker void SetDepacketizerToDecoderFrameTransformer( 92*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) 93*d9f75844SAndroid Build Coastguard Worker override; 94*d9f75844SAndroid Build Coastguard Worker void SetDecoderMap(std::map<int, SdpAudioFormat> decoder_map) override; 95*d9f75844SAndroid Build Coastguard Worker void SetNackHistory(int history_ms) override; 96*d9f75844SAndroid Build Coastguard Worker void SetNonSenderRttMeasurement(bool enabled) override; 97*d9f75844SAndroid Build Coastguard Worker void SetFrameDecryptor(rtc::scoped_refptr<webrtc::FrameDecryptorInterface> 98*d9f75844SAndroid Build Coastguard Worker frame_decryptor) override; 99*d9f75844SAndroid Build Coastguard Worker void SetRtpExtensions(std::vector<RtpExtension> extensions) override; 100*d9f75844SAndroid Build Coastguard Worker const std::vector<RtpExtension>& GetRtpExtensions() const override; 101*d9f75844SAndroid Build Coastguard Worker RtpHeaderExtensionMap GetRtpExtensionMap() const override; 102*d9f75844SAndroid Build Coastguard Worker 103*d9f75844SAndroid Build Coastguard Worker webrtc::AudioReceiveStreamInterface::Stats GetStats( 104*d9f75844SAndroid Build Coastguard Worker bool get_and_clear_legacy_stats) const override; 105*d9f75844SAndroid Build Coastguard Worker void SetSink(AudioSinkInterface* sink) override; 106*d9f75844SAndroid Build Coastguard Worker void SetGain(float gain) override; 107*d9f75844SAndroid Build Coastguard Worker bool SetBaseMinimumPlayoutDelayMs(int delay_ms) override; 108*d9f75844SAndroid Build Coastguard Worker int GetBaseMinimumPlayoutDelayMs() const override; 109*d9f75844SAndroid Build Coastguard Worker std::vector<webrtc::RtpSource> GetSources() const override; 110*d9f75844SAndroid Build Coastguard Worker 111*d9f75844SAndroid Build Coastguard Worker // AudioMixer::Source 112*d9f75844SAndroid Build Coastguard Worker AudioFrameInfo GetAudioFrameWithInfo(int sample_rate_hz, 113*d9f75844SAndroid Build Coastguard Worker AudioFrame* audio_frame) override; 114*d9f75844SAndroid Build Coastguard Worker int Ssrc() const override; 115*d9f75844SAndroid Build Coastguard Worker int PreferredSampleRate() const override; 116*d9f75844SAndroid Build Coastguard Worker 117*d9f75844SAndroid Build Coastguard Worker // Syncable 118*d9f75844SAndroid Build Coastguard Worker uint32_t id() const override; 119*d9f75844SAndroid Build Coastguard Worker absl::optional<Syncable::Info> GetInfo() const override; 120*d9f75844SAndroid Build Coastguard Worker bool GetPlayoutRtpTimestamp(uint32_t* rtp_timestamp, 121*d9f75844SAndroid Build Coastguard Worker int64_t* time_ms) const override; 122*d9f75844SAndroid Build Coastguard Worker void SetEstimatedPlayoutNtpTimestampMs(int64_t ntp_timestamp_ms, 123*d9f75844SAndroid Build Coastguard Worker int64_t time_ms) override; 124*d9f75844SAndroid Build Coastguard Worker bool SetMinimumPlayoutDelay(int delay_ms) override; 125*d9f75844SAndroid Build Coastguard Worker 126*d9f75844SAndroid Build Coastguard Worker void AssociateSendStream(internal::AudioSendStream* send_stream); 127*d9f75844SAndroid Build Coastguard Worker void DeliverRtcp(const uint8_t* packet, size_t length); 128*d9f75844SAndroid Build Coastguard Worker 129*d9f75844SAndroid Build Coastguard Worker void SetSyncGroup(absl::string_view sync_group); 130*d9f75844SAndroid Build Coastguard Worker 131*d9f75844SAndroid Build Coastguard Worker void SetLocalSsrc(uint32_t local_ssrc); 132*d9f75844SAndroid Build Coastguard Worker 133*d9f75844SAndroid Build Coastguard Worker uint32_t local_ssrc() const; 134*d9f75844SAndroid Build Coastguard Worker remote_ssrc()135*d9f75844SAndroid Build Coastguard Worker uint32_t remote_ssrc() const override { 136*d9f75844SAndroid Build Coastguard Worker // The remote_ssrc member variable of config_ will never change and can be 137*d9f75844SAndroid Build Coastguard Worker // considered const. 138*d9f75844SAndroid Build Coastguard Worker return config_.rtp.remote_ssrc; 139*d9f75844SAndroid Build Coastguard Worker } 140*d9f75844SAndroid Build Coastguard Worker 141*d9f75844SAndroid Build Coastguard Worker // Returns a reference to the currently set sync group of the stream. 142*d9f75844SAndroid Build Coastguard Worker // Must be called on the packet delivery thread. 143*d9f75844SAndroid Build Coastguard Worker const std::string& sync_group() const; 144*d9f75844SAndroid Build Coastguard Worker 145*d9f75844SAndroid Build Coastguard Worker const AudioSendStream* GetAssociatedSendStreamForTesting() const; 146*d9f75844SAndroid Build Coastguard Worker 147*d9f75844SAndroid Build Coastguard Worker // TODO(tommi): Remove this method. 148*d9f75844SAndroid Build Coastguard Worker void ReconfigureForTesting( 149*d9f75844SAndroid Build Coastguard Worker const webrtc::AudioReceiveStreamInterface::Config& config); 150*d9f75844SAndroid Build Coastguard Worker 151*d9f75844SAndroid Build Coastguard Worker private: 152*d9f75844SAndroid Build Coastguard Worker internal::AudioState* audio_state() const; 153*d9f75844SAndroid Build Coastguard Worker 154*d9f75844SAndroid Build Coastguard Worker RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_thread_checker_; 155*d9f75844SAndroid Build Coastguard Worker // TODO(bugs.webrtc.org/11993): This checker conceptually represents 156*d9f75844SAndroid Build Coastguard Worker // operations that belong to the network thread. The Call class is currently 157*d9f75844SAndroid Build Coastguard Worker // moving towards handling network packets on the network thread and while 158*d9f75844SAndroid Build Coastguard Worker // that work is ongoing, this checker may in practice represent the worker 159*d9f75844SAndroid Build Coastguard Worker // thread, but still serves as a mechanism of grouping together concepts 160*d9f75844SAndroid Build Coastguard Worker // that belong to the network thread. Once the packets are fully delivered 161*d9f75844SAndroid Build Coastguard Worker // on the network thread, this comment will be deleted. 162*d9f75844SAndroid Build Coastguard Worker RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_; 163*d9f75844SAndroid Build Coastguard Worker webrtc::AudioReceiveStreamInterface::Config config_; 164*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<webrtc::AudioState> audio_state_; 165*d9f75844SAndroid Build Coastguard Worker SourceTracker source_tracker_; 166*d9f75844SAndroid Build Coastguard Worker const std::unique_ptr<voe::ChannelReceiveInterface> channel_receive_; 167*d9f75844SAndroid Build Coastguard Worker AudioSendStream* associated_send_stream_ 168*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(packet_sequence_checker_) = nullptr; 169*d9f75844SAndroid Build Coastguard Worker 170*d9f75844SAndroid Build Coastguard Worker bool playing_ RTC_GUARDED_BY(worker_thread_checker_) = false; 171*d9f75844SAndroid Build Coastguard Worker 172*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<RtpStreamReceiverInterface> rtp_stream_receiver_ 173*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(packet_sequence_checker_); 174*d9f75844SAndroid Build Coastguard Worker }; 175*d9f75844SAndroid Build Coastguard Worker } // namespace webrtc 176*d9f75844SAndroid Build Coastguard Worker 177*d9f75844SAndroid Build Coastguard Worker #endif // AUDIO_AUDIO_RECEIVE_STREAM_H_ 178