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