xref: /aosp_15_r20/external/webrtc/pc/video_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_VIDEO_RTP_RECEIVER_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_VIDEO_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/transport/rtp/rtp_source.h"
30*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_frame.h"
31*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_sink_interface.h"
32*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_source_interface.h"
33*d9f75844SAndroid Build Coastguard Worker #include "media/base/media_channel.h"
34*d9f75844SAndroid Build Coastguard Worker #include "pc/jitter_buffer_delay.h"
35*d9f75844SAndroid Build Coastguard Worker #include "pc/media_stream_track_proxy.h"
36*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_receiver.h"
37*d9f75844SAndroid Build Coastguard Worker #include "pc/video_rtp_track_source.h"
38*d9f75844SAndroid Build Coastguard Worker #include "pc/video_track.h"
39*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/no_unique_address.h"
40*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread.h"
41*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
42*d9f75844SAndroid Build Coastguard Worker 
43*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker class VideoRtpReceiver : public RtpReceiverInternal {
46*d9f75844SAndroid Build Coastguard Worker  public:
47*d9f75844SAndroid Build Coastguard Worker   // An SSRC of 0 will create a receiver that will match the first SSRC it
48*d9f75844SAndroid Build Coastguard Worker   // sees. Must be called on signaling thread.
49*d9f75844SAndroid Build Coastguard Worker   VideoRtpReceiver(rtc::Thread* worker_thread,
50*d9f75844SAndroid Build Coastguard Worker                    std::string receiver_id,
51*d9f75844SAndroid Build Coastguard Worker                    std::vector<std::string> streams_ids);
52*d9f75844SAndroid Build Coastguard Worker   // TODO(hbos): Remove this when streams() is removed.
53*d9f75844SAndroid Build Coastguard Worker   // https://crbug.com/webrtc/9480
54*d9f75844SAndroid Build Coastguard Worker   VideoRtpReceiver(
55*d9f75844SAndroid Build Coastguard Worker       rtc::Thread* worker_thread,
56*d9f75844SAndroid Build Coastguard Worker       const std::string& receiver_id,
57*d9f75844SAndroid Build Coastguard Worker       const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   virtual ~VideoRtpReceiver();
60*d9f75844SAndroid Build Coastguard Worker 
video_track()61*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<VideoTrackInterface> video_track() const { return track_; }
62*d9f75844SAndroid Build Coastguard Worker 
63*d9f75844SAndroid Build Coastguard Worker   // RtpReceiverInterface implementation
track()64*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
65*d9f75844SAndroid Build Coastguard Worker     return track_;
66*d9f75844SAndroid Build Coastguard Worker   }
67*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override;
68*d9f75844SAndroid Build Coastguard Worker   std::vector<std::string> stream_ids() const override;
69*d9f75844SAndroid Build Coastguard Worker   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
70*d9f75844SAndroid Build Coastguard Worker       const override;
media_type()71*d9f75844SAndroid Build Coastguard Worker   cricket::MediaType media_type() const override {
72*d9f75844SAndroid Build Coastguard Worker     return cricket::MEDIA_TYPE_VIDEO;
73*d9f75844SAndroid Build Coastguard Worker   }
74*d9f75844SAndroid Build Coastguard Worker 
id()75*d9f75844SAndroid Build Coastguard Worker   std::string id() const override { return id_; }
76*d9f75844SAndroid Build Coastguard Worker 
77*d9f75844SAndroid Build Coastguard Worker   RtpParameters GetParameters() const override;
78*d9f75844SAndroid Build Coastguard Worker 
79*d9f75844SAndroid Build Coastguard Worker   void SetFrameDecryptor(
80*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
83*d9f75844SAndroid Build Coastguard Worker       const override;
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   void SetDepacketizerToDecoderFrameTransformer(
86*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
87*d9f75844SAndroid Build Coastguard Worker 
88*d9f75844SAndroid Build Coastguard Worker   // RtpReceiverInternal implementation.
89*d9f75844SAndroid Build Coastguard Worker   void Stop() override;
90*d9f75844SAndroid Build Coastguard Worker   void SetupMediaChannel(uint32_t ssrc) override;
91*d9f75844SAndroid Build Coastguard Worker   void SetupUnsignaledMediaChannel() override;
92*d9f75844SAndroid Build Coastguard Worker   uint32_t ssrc() const override;
93*d9f75844SAndroid Build Coastguard Worker   void NotifyFirstPacketReceived() override;
94*d9f75844SAndroid Build Coastguard Worker   void set_stream_ids(std::vector<std::string> stream_ids) override;
95*d9f75844SAndroid Build Coastguard Worker   void set_transport(
96*d9f75844SAndroid Build Coastguard Worker       rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override;
97*d9f75844SAndroid Build Coastguard Worker   void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
98*d9f75844SAndroid Build Coastguard Worker                       streams) override;
99*d9f75844SAndroid Build Coastguard Worker 
100*d9f75844SAndroid Build Coastguard Worker   void SetObserver(RtpReceiverObserverInterface* observer) override;
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   void SetJitterBufferMinimumDelay(
103*d9f75844SAndroid Build Coastguard Worker       absl::optional<double> delay_seconds) override;
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   void SetMediaChannel(cricket::MediaChannel* media_channel) override;
106*d9f75844SAndroid Build Coastguard Worker 
AttachmentId()107*d9f75844SAndroid Build Coastguard Worker   int AttachmentId() const override { return attachment_id_; }
108*d9f75844SAndroid Build Coastguard Worker 
109*d9f75844SAndroid Build Coastguard Worker   std::vector<RtpSource> GetSources() const override;
110*d9f75844SAndroid Build Coastguard Worker 
111*d9f75844SAndroid Build Coastguard Worker   // Combines SetMediaChannel, SetupMediaChannel and
112*d9f75844SAndroid Build Coastguard Worker   // SetupUnsignaledMediaChannel.
113*d9f75844SAndroid Build Coastguard Worker   void SetupMediaChannel(absl::optional<uint32_t> ssrc,
114*d9f75844SAndroid Build Coastguard Worker                          cricket::MediaChannel* media_channel);
115*d9f75844SAndroid Build Coastguard Worker 
116*d9f75844SAndroid Build Coastguard Worker  private:
117*d9f75844SAndroid Build Coastguard Worker   void RestartMediaChannel(absl::optional<uint32_t> ssrc)
118*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(&signaling_thread_checker_);
119*d9f75844SAndroid Build Coastguard Worker   void RestartMediaChannel_w(absl::optional<uint32_t> ssrc,
120*d9f75844SAndroid Build Coastguard Worker                              MediaSourceInterface::SourceState state)
121*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(worker_thread_);
122*d9f75844SAndroid Build Coastguard Worker   void SetSink(rtc::VideoSinkInterface<VideoFrame>* sink)
123*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(worker_thread_);
124*d9f75844SAndroid Build Coastguard Worker   void SetMediaChannel_w(cricket::MediaChannel* media_channel)
125*d9f75844SAndroid Build Coastguard Worker       RTC_RUN_ON(worker_thread_);
126*d9f75844SAndroid Build Coastguard Worker 
127*d9f75844SAndroid Build Coastguard Worker   // VideoRtpTrackSource::Callback
128*d9f75844SAndroid Build Coastguard Worker   void OnGenerateKeyFrame();
129*d9f75844SAndroid Build Coastguard Worker   void OnEncodedSinkEnabled(bool enable);
130*d9f75844SAndroid Build Coastguard Worker 
131*d9f75844SAndroid Build Coastguard Worker   void SetEncodedSinkEnabled(bool enable) RTC_RUN_ON(worker_thread_);
132*d9f75844SAndroid Build Coastguard Worker 
133*d9f75844SAndroid Build Coastguard Worker   class SourceCallback : public VideoRtpTrackSource::Callback {
134*d9f75844SAndroid Build Coastguard Worker    public:
SourceCallback(VideoRtpReceiver * receiver)135*d9f75844SAndroid Build Coastguard Worker     explicit SourceCallback(VideoRtpReceiver* receiver) : receiver_(receiver) {}
136*d9f75844SAndroid Build Coastguard Worker     ~SourceCallback() override = default;
137*d9f75844SAndroid Build Coastguard Worker 
138*d9f75844SAndroid Build Coastguard Worker    private:
OnGenerateKeyFrame()139*d9f75844SAndroid Build Coastguard Worker     void OnGenerateKeyFrame() override { receiver_->OnGenerateKeyFrame(); }
OnEncodedSinkEnabled(bool enable)140*d9f75844SAndroid Build Coastguard Worker     void OnEncodedSinkEnabled(bool enable) override {
141*d9f75844SAndroid Build Coastguard Worker       receiver_->OnEncodedSinkEnabled(enable);
142*d9f75844SAndroid Build Coastguard Worker     }
143*d9f75844SAndroid Build Coastguard Worker 
144*d9f75844SAndroid Build Coastguard Worker     VideoRtpReceiver* const receiver_;
145*d9f75844SAndroid Build Coastguard Worker   } source_callback_{this};
146*d9f75844SAndroid Build Coastguard Worker 
147*d9f75844SAndroid Build Coastguard Worker   RTC_NO_UNIQUE_ADDRESS SequenceChecker signaling_thread_checker_;
148*d9f75844SAndroid Build Coastguard Worker   rtc::Thread* const worker_thread_;
149*d9f75844SAndroid Build Coastguard Worker 
150*d9f75844SAndroid Build Coastguard Worker   const std::string id_;
151*d9f75844SAndroid Build Coastguard Worker   cricket::VideoMediaChannel* media_channel_ RTC_GUARDED_BY(worker_thread_) =
152*d9f75844SAndroid Build Coastguard Worker       nullptr;
153*d9f75844SAndroid Build Coastguard Worker   absl::optional<uint32_t> ssrc_ RTC_GUARDED_BY(worker_thread_);
154*d9f75844SAndroid Build Coastguard Worker   // `source_` is held here to be able to change the state of the source when
155*d9f75844SAndroid Build Coastguard Worker   // the VideoRtpReceiver is stopped.
156*d9f75844SAndroid Build Coastguard Worker   const rtc::scoped_refptr<VideoRtpTrackSource> source_;
157*d9f75844SAndroid Build Coastguard Worker   const rtc::scoped_refptr<VideoTrackProxyWithInternal<VideoTrack>> track_;
158*d9f75844SAndroid Build Coastguard Worker   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_
159*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(&signaling_thread_checker_);
160*d9f75844SAndroid Build Coastguard Worker   RtpReceiverObserverInterface* observer_
161*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
162*d9f75844SAndroid Build Coastguard Worker   bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
163*d9f75844SAndroid Build Coastguard Worker       false;
164*d9f75844SAndroid Build Coastguard Worker   const int attachment_id_;
165*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_
166*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(worker_thread_);
167*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_
168*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(&signaling_thread_checker_);
169*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
170*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(worker_thread_);
171*d9f75844SAndroid Build Coastguard Worker   // Stores the minimum jitter buffer delay. Handles caching cases
172*d9f75844SAndroid Build Coastguard Worker   // if `SetJitterBufferMinimumDelay` is called before start.
173*d9f75844SAndroid Build Coastguard Worker   JitterBufferDelay delay_ RTC_GUARDED_BY(worker_thread_);
174*d9f75844SAndroid Build Coastguard Worker 
175*d9f75844SAndroid Build Coastguard Worker   // Records if we should generate a keyframe when `media_channel_` gets set up
176*d9f75844SAndroid Build Coastguard Worker   // or switched.
177*d9f75844SAndroid Build Coastguard Worker   bool saved_generate_keyframe_ RTC_GUARDED_BY(worker_thread_) = false;
178*d9f75844SAndroid Build Coastguard Worker   bool saved_encoded_sink_enabled_ RTC_GUARDED_BY(worker_thread_) = false;
179*d9f75844SAndroid Build Coastguard Worker };
180*d9f75844SAndroid Build Coastguard Worker 
181*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
182*d9f75844SAndroid Build Coastguard Worker 
183*d9f75844SAndroid Build Coastguard Worker #endif  // PC_VIDEO_RTP_RECEIVER_H_
184