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