blob: bc42ad30475ca9f68dcbd05e9e9e76d286199942 [file] [log] [blame]
Ruslan Burakov501bfba2019-02-11 09:29:191/*
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_VIDEO_RTP_RECEIVER_H_
12#define PC_VIDEO_RTP_RECEIVER_H_
13
14#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Florent Castelli8037fc62024-08-29 13:00:4016#include <optional>
Ruslan Burakov501bfba2019-02-11 09:29:1917#include <string>
18#include <vector>
19
Ruslan Burakov501bfba2019-02-11 09:29:1920#include "api/crypto/frame_decryptor_interface.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0821#include "api/dtls_transport_interface.h"
Marina Ciocea412a31b2020-02-28 15:02:0622#include "api/frame_transformer_interface.h"
Ruslan Burakov501bfba2019-02-11 09:29:1923#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"
Artem Titovd15a5752021-02-10 13:31:2428#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0829#include "api/transport/rtp/rtp_source.h"
Ruslan Burakov501bfba2019-02-11 09:29:1930#include "api/video/video_frame.h"
31#include "api/video/video_sink_interface.h"
32#include "api/video/video_source_interface.h"
33#include "media/base/media_channel.h"
Tommi4ccdf9322021-05-17 12:50:1034#include "pc/jitter_buffer_delay.h"
Markus Handella1b82012021-05-26 16:56:3035#include "pc/media_stream_track_proxy.h"
Ruslan Burakov501bfba2019-02-11 09:29:1936#include "pc/rtp_receiver.h"
Markus Handell15f2ff42019-11-22 09:34:3737#include "pc/video_rtp_track_source.h"
Harald Alvestrand1ee33252020-09-24 13:31:1538#include "pc/video_track.h"
Tommi4ccdf9322021-05-17 12:50:1039#include "rtc_base/system/no_unique_address.h"
Ruslan Burakov501bfba2019-02-11 09:29:1940#include "rtc_base/thread.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0841#include "rtc_base/thread_annotations.h"
Ruslan Burakov501bfba2019-02-11 09:29:1942
43namespace webrtc {
44
Tommi4ccdf9322021-05-17 12:50:1045class VideoRtpReceiver : public RtpReceiverInternal {
Ruslan Burakov501bfba2019-02-11 09:29:1946 public:
47 // An SSRC of 0 will create a receiver that will match the first SSRC it
Ruslan Burakov493a6502019-02-27 14:32:4848 // sees. Must be called on signaling thread.
Ruslan Burakov501bfba2019-02-11 09:29:1949 VideoRtpReceiver(rtc::Thread* worker_thread,
50 std::string receiver_id,
51 std::vector<std::string> streams_ids);
52 // TODO(hbos): Remove this when streams() is removed.
53 // https://crbug.com/webrtc/9480
54 VideoRtpReceiver(
55 rtc::Thread* worker_thread,
56 const std::string& receiver_id,
57 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
58
59 virtual ~VideoRtpReceiver();
60
Tommi4ccdf9322021-05-17 12:50:1061 rtc::scoped_refptr<VideoTrackInterface> video_track() const { return track_; }
Ruslan Burakov501bfba2019-02-11 09:29:1962
63 // RtpReceiverInterface implementation
64 rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
Tommi4ccdf9322021-05-17 12:50:1065 return track_;
Ruslan Burakov501bfba2019-02-11 09:29:1966 }
Tommi4ccdf9322021-05-17 12:50:1067 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override;
Ruslan Burakov501bfba2019-02-11 09:29:1968 std::vector<std::string> stream_ids() const override;
69 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
Tommi4ccdf9322021-05-17 12:50:1070 const override;
Ruslan Burakov501bfba2019-02-11 09:29:1971 cricket::MediaType media_type() const override {
72 return cricket::MEDIA_TYPE_VIDEO;
73 }
74
75 std::string id() const override { return id_; }
76
77 RtpParameters GetParameters() const override;
Ruslan Burakov501bfba2019-02-11 09:29:1978
79 void SetFrameDecryptor(
80 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override;
81
82 rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor()
83 const override;
84
Harald Alvestrandb0e70572024-04-23 14:04:1885 void SetFrameTransformer(
Marina Ciocea412a31b2020-02-28 15:02:0686 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override;
87
Ruslan Burakov501bfba2019-02-11 09:29:1988 // RtpReceiverInternal implementation.
89 void Stop() override;
90 void SetupMediaChannel(uint32_t ssrc) override;
Saurav Das7262fc22019-09-11 23:23:0591 void SetupUnsignaledMediaChannel() override;
Florent Castelli8037fc62024-08-29 13:00:4092 std::optional<uint32_t> ssrc() const override;
Ruslan Burakov501bfba2019-02-11 09:29:1993 void NotifyFirstPacketReceived() override;
94 void set_stream_ids(std::vector<std::string> stream_ids) override;
95 void set_transport(
Tommi4ccdf9322021-05-17 12:50:1096 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override;
Ruslan Burakov501bfba2019-02-11 09:29:1997 void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
98 streams) override;
99
100 void SetObserver(RtpReceiverObserverInterface* observer) override;
101
Ruslan Burakov4bac79e2019-04-03 17:55:33102 void SetJitterBufferMinimumDelay(
Florent Castelli8037fc62024-08-29 13:00:40103 std::optional<double> delay_seconds) override;
Ruslan Burakov4bac79e2019-04-03 17:55:33104
Harald Alvestrand36fafc82022-12-08 08:47:42105 void SetMediaChannel(
106 cricket::MediaReceiveChannelInterface* media_channel) override;
Ruslan Burakov501bfba2019-02-11 09:29:19107
108 int AttachmentId() const override { return attachment_id_; }
109
110 std::vector<RtpSource> GetSources() const override;
111
Tommi6589def2022-02-17 22:36:47112 // Combines SetMediaChannel, SetupMediaChannel and
113 // SetupUnsignaledMediaChannel.
Florent Castelli8037fc62024-08-29 13:00:40114 void SetupMediaChannel(std::optional<uint32_t> ssrc,
Harald Alvestrand36fafc82022-12-08 08:47:42115 cricket::MediaReceiveChannelInterface* media_channel);
Tommi6589def2022-02-17 22:36:47116
Ruslan Burakov428dcb22019-04-18 15:49:49117 private:
Florent Castelli8037fc62024-08-29 13:00:40118 void RestartMediaChannel(std::optional<uint32_t> ssrc)
Tommi6589def2022-02-17 22:36:47119 RTC_RUN_ON(&signaling_thread_checker_);
Florent Castelli8037fc62024-08-29 13:00:40120 void RestartMediaChannel_w(std::optional<uint32_t> ssrc,
Tommi6589def2022-02-17 22:36:47121 MediaSourceInterface::SourceState state)
122 RTC_RUN_ON(worker_thread_);
Markus Handell9c27ed22019-12-04 11:57:58123 void SetSink(rtc::VideoSinkInterface<VideoFrame>* sink)
124 RTC_RUN_ON(worker_thread_);
Harald Alvestrand36fafc82022-12-08 08:47:42125 void SetMediaChannel_w(cricket::MediaReceiveChannelInterface* media_channel)
Tommi4ccdf9322021-05-17 12:50:10126 RTC_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 09:29:19127
Markus Handelld5e2f212019-11-26 08:30:08128 // VideoRtpTrackSource::Callback
Tommi4ccdf9322021-05-17 12:50:10129 void OnGenerateKeyFrame();
130 void OnEncodedSinkEnabled(bool enable);
131
Markus Handell9c27ed22019-12-04 11:57:58132 void SetEncodedSinkEnabled(bool enable) RTC_RUN_ON(worker_thread_);
Markus Handelld5e2f212019-11-26 08:30:08133
Tommi4ccdf9322021-05-17 12:50:10134 class SourceCallback : public VideoRtpTrackSource::Callback {
135 public:
136 explicit SourceCallback(VideoRtpReceiver* receiver) : receiver_(receiver) {}
137 ~SourceCallback() override = default;
138
139 private:
140 void OnGenerateKeyFrame() override { receiver_->OnGenerateKeyFrame(); }
141 void OnEncodedSinkEnabled(bool enable) override {
142 receiver_->OnEncodedSinkEnabled(enable);
143 }
144
145 VideoRtpReceiver* const receiver_;
146 } source_callback_{this};
147
148 RTC_NO_UNIQUE_ADDRESS SequenceChecker signaling_thread_checker_;
Ruslan Burakov501bfba2019-02-11 09:29:19149 rtc::Thread* const worker_thread_;
Markus Handelld5e2f212019-11-26 08:30:08150
Ruslan Burakov501bfba2019-02-11 09:29:19151 const std::string id_;
Harald Alvestrandc0d44d92022-12-13 12:57:24152 cricket::VideoMediaReceiveChannelInterface* media_channel_
153 RTC_GUARDED_BY(worker_thread_) = nullptr;
Florent Castelli8037fc62024-08-29 13:00:40154 std::optional<uint32_t> signaled_ssrc_ RTC_GUARDED_BY(worker_thread_);
Artem Titov880fa812021-07-30 20:30:23155 // `source_` is held here to be able to change the state of the source when
Ruslan Burakov501bfba2019-02-11 09:29:19156 // the VideoRtpReceiver is stopped.
Tommi4ccdf9322021-05-17 12:50:10157 const rtc::scoped_refptr<VideoRtpTrackSource> source_;
158 const rtc::scoped_refptr<VideoTrackProxyWithInternal<VideoTrack>> track_;
159 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_
160 RTC_GUARDED_BY(&signaling_thread_checker_);
Tommi4ccdf9322021-05-17 12:50:10161 RtpReceiverObserverInterface* observer_
162 RTC_GUARDED_BY(&signaling_thread_checker_) = nullptr;
163 bool received_first_packet_ RTC_GUARDED_BY(&signaling_thread_checker_) =
164 false;
165 const int attachment_id_;
166 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_
167 RTC_GUARDED_BY(worker_thread_);
168 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_
169 RTC_GUARDED_BY(&signaling_thread_checker_);
Marina Ciocea412a31b2020-02-28 15:02:06170 rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_
171 RTC_GUARDED_BY(worker_thread_);
Tommi4ccdf9322021-05-17 12:50:10172 // Stores the minimum jitter buffer delay. Handles caching cases
Artem Titov880fa812021-07-30 20:30:23173 // if `SetJitterBufferMinimumDelay` is called before start.
Tommi4ccdf9322021-05-17 12:50:10174 JitterBufferDelay delay_ RTC_GUARDED_BY(worker_thread_);
175
Artem Titov880fa812021-07-30 20:30:23176 // Records if we should generate a keyframe when `media_channel_` gets set up
Markus Handell9c27ed22019-12-04 11:57:58177 // or switched.
178 bool saved_generate_keyframe_ RTC_GUARDED_BY(worker_thread_) = false;
179 bool saved_encoded_sink_enabled_ RTC_GUARDED_BY(worker_thread_) = false;
Ruslan Burakov501bfba2019-02-11 09:29:19180};
181
182} // namespace webrtc
183
184#endif // PC_VIDEO_RTP_RECEIVER_H_