xref: /aosp_15_r20/external/webrtc/pc/audio_rtp_receiver.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2019 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 PC_AUDIO_RTP_RECEIVER_H_
12 #define PC_AUDIO_RTP_RECEIVER_H_
13 
14 #include <stdint.h>
15 
16 #include <string>
17 #include <vector>
18 
19 #include "absl/types/optional.h"
20 #include "api/crypto/frame_decryptor_interface.h"
21 #include "api/dtls_transport_interface.h"
22 #include "api/frame_transformer_interface.h"
23 #include "api/media_stream_interface.h"
24 #include "api/media_types.h"
25 #include "api/rtp_parameters.h"
26 #include "api/rtp_receiver_interface.h"
27 #include "api/scoped_refptr.h"
28 #include "api/sequence_checker.h"
29 #include "api/task_queue/pending_task_safety_flag.h"
30 #include "api/transport/rtp/rtp_source.h"
31 #include "media/base/media_channel.h"
32 #include "pc/audio_track.h"
33 #include "pc/jitter_buffer_delay.h"
34 #include "pc/media_stream_track_proxy.h"
35 #include "pc/remote_audio_source.h"
36 #include "pc/rtp_receiver.h"
37 #include "rtc_base/system/no_unique_address.h"
38 #include "rtc_base/thread.h"
39 #include "rtc_base/thread_annotations.h"
40 
41 namespace webrtc {
42 
43 class AudioRtpReceiver : public ObserverInterface,
44                          public AudioSourceInterface::AudioObserver,
45                          public RtpReceiverInternal {
46  public:
47   // The constructor supports optionally passing the voice channel to the
48   // instance at construction time without having to call `SetMediaChannel()`
49   // on the worker thread straight after construction.
50   // However, when using that, the assumption is that right after construction,
51   // a call to either `SetupUnsignaledMediaChannel` or `SetupMediaChannel`
52   // will be made, which will internally start the source on the worker thread.
53   AudioRtpReceiver(rtc::Thread* worker_thread,
54                    std::string receiver_id,
55                    std::vector<std::string> stream_ids,
56                    bool is_unified_plan,
57                    cricket::VoiceMediaChannel* voice_channel = nullptr);
58   // TODO(https://crbug.com/webrtc/9480): Remove this when streams() is removed.
59   AudioRtpReceiver(
60       rtc::Thread* worker_thread,
61       const std::string& receiver_id,
62       const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
63       bool is_unified_plan,
64       cricket::VoiceMediaChannel* media_channel = nullptr);
65   virtual ~AudioRtpReceiver();
66 
67   // ObserverInterface implementation
68   void OnChanged() override;
69 
70   // AudioSourceInterface::AudioObserver implementation
71   void OnSetVolume(double volume) override;
72 
audio_track()73   rtc::scoped_refptr<AudioTrackInterface> audio_track() const { return track_; }
74 
75   // RtpReceiverInterface implementation
track()76   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
77     return track_;
78   }
79   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override;
80   std::vector<std::string> stream_ids() const override;
81   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
82       const override;
83 
media_type()84   cricket::MediaType media_type() const override {
85     return cricket::MEDIA_TYPE_AUDIO;
86   }
87 
id()88   std::string id() const override { return id_; }
89 
90   RtpParameters GetParameters() const override;
91 
92   void SetFrameDecryptor(
93       rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
94 
95   rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
96       const override;
97 
98   // RtpReceiverInternal implementation.
99   void Stop() override;
100   void SetupMediaChannel(uint32_t ssrc) override;
101   void SetupUnsignaledMediaChannel() override;
102   uint32_t ssrc() const override;
103   void NotifyFirstPacketReceived() override;
104   void set_stream_ids(std::vector<std::string> stream_ids) override;
105   void set_transport(
106       rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override;
107   void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
108                       streams) override;
109   void SetObserver(RtpReceiverObserverInterface* observer) override;
110 
111   void SetJitterBufferMinimumDelay(
112       absl::optional<double> delay_seconds) override;
113 
114   void SetMediaChannel(cricket::MediaChannel* media_channel) override;
115 
116   std::vector<RtpSource> GetSources() const override;
AttachmentId()117   int AttachmentId() const override { return attachment_id_; }
118   void SetDepacketizerToDecoderFrameTransformer(
119       rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)
120       override;
121 
122  private:
123   void RestartMediaChannel(absl::optional<uint32_t> ssrc)
124       RTC_RUN_ON(&signaling_thread_checker_);
125   void RestartMediaChannel_w(absl::optional<uint32_t> ssrc,
126                              bool track_enabled,
127                              MediaSourceInterface::SourceState state)
128       RTC_RUN_ON(worker_thread_);
129   void Reconfigure(bool track_enabled) RTC_RUN_ON(worker_thread_);
130   void SetOutputVolume_w(double volume) RTC_RUN_ON(worker_thread_);
131 
132   RTC_NO_UNIQUE_ADDRESS SequenceChecker signaling_thread_checker_;
133   rtc::Thread* const worker_thread_;
134   const std::string id_;
135   const rtc::scoped_refptr<RemoteAudioSource> source_;
136   const rtc::scoped_refptr<AudioTrackProxyWithInternal<AudioTrack>> track_;
137   cricket::VoiceMediaChannel* media_channel_ RTC_GUARDED_BY(worker_thread_) =
138       nullptr;
139   absl::optional<uint32_t> ssrc_ RTC_GUARDED_BY(worker_thread_);
140   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_
141       RTC_GUARDED_BY(&signaling_thread_checker_);
142   bool cached_track_enabled_ RTC_GUARDED_BY(&signaling_thread_checker_);
143   double cached_volume_ RTC_GUARDED_BY(worker_thread_) = 1.0;
144   RtpReceiverObserverInterface* observer_
145       RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
146   bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
147       false;
148   const int attachment_id_;
149   rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_
150       RTC_GUARDED_BY(worker_thread_);
151   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_
152       RTC_GUARDED_BY(&signaling_thread_checker_);
153   // Stores and updates the playout delay. Handles caching cases if
154   // `SetJitterBufferMinimumDelay` is called before start.
155   JitterBufferDelay delay_ RTC_GUARDED_BY(worker_thread_);
156   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
157       RTC_GUARDED_BY(worker_thread_);
158   const rtc::scoped_refptr<PendingTaskSafetyFlag> worker_thread_safety_;
159 };
160 
161 }  // namespace webrtc
162 
163 #endif  // PC_AUDIO_RTP_RECEIVER_H_
164