Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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_VIDEO_RTP_RECEIVER_H_ |
| 12 | #define PC_VIDEO_RTP_RECEIVER_H_ |
| 13 | |
| 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "absl/types/optional.h" |
| 20 | #include "api/crypto/frame_decryptor_interface.h" |
Marina Ciocea | 412a31b | 2020-02-28 16:02:06 +0100 | [diff] [blame] | 21 | #include "api/frame_transformer_interface.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 22 | #include "api/media_stream_interface.h" |
| 23 | #include "api/media_types.h" |
| 24 | #include "api/rtp_parameters.h" |
| 25 | #include "api/rtp_receiver_interface.h" |
| 26 | #include "api/scoped_refptr.h" |
| 27 | #include "api/video/video_frame.h" |
| 28 | #include "api/video/video_sink_interface.h" |
| 29 | #include "api/video/video_source_interface.h" |
| 30 | #include "media/base/media_channel.h" |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 31 | #include "pc/jitter_buffer_delay_interface.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 32 | #include "pc/rtp_receiver.h" |
Markus Handell | 15f2ff4 | 2019-11-22 10:34:37 +0100 | [diff] [blame] | 33 | #include "pc/video_rtp_track_source.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 34 | #include "rtc_base/ref_counted_object.h" |
| 35 | #include "rtc_base/thread.h" |
| 36 | |
| 37 | namespace webrtc { |
| 38 | |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 39 | class VideoRtpReceiver : public rtc::RefCountedObject<RtpReceiverInternal>, |
| 40 | public VideoRtpTrackSource::Callback { |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 41 | public: |
| 42 | // An SSRC of 0 will create a receiver that will match the first SSRC it |
Ruslan Burakov | 493a650 | 2019-02-27 15:32:48 +0100 | [diff] [blame] | 43 | // sees. Must be called on signaling thread. |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 44 | VideoRtpReceiver(rtc::Thread* worker_thread, |
| 45 | std::string receiver_id, |
| 46 | std::vector<std::string> streams_ids); |
| 47 | // TODO(hbos): Remove this when streams() is removed. |
| 48 | // https://crbug.com/webrtc/9480 |
| 49 | VideoRtpReceiver( |
| 50 | rtc::Thread* worker_thread, |
| 51 | const std::string& receiver_id, |
| 52 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams); |
| 53 | |
| 54 | virtual ~VideoRtpReceiver(); |
| 55 | |
| 56 | rtc::scoped_refptr<VideoTrackInterface> video_track() const { |
| 57 | return track_.get(); |
| 58 | } |
| 59 | |
| 60 | // RtpReceiverInterface implementation |
| 61 | rtc::scoped_refptr<MediaStreamTrackInterface> track() const override { |
| 62 | return track_.get(); |
| 63 | } |
| 64 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport() const override { |
| 65 | return dtls_transport_; |
| 66 | } |
| 67 | std::vector<std::string> stream_ids() const override; |
| 68 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams() |
| 69 | const override { |
| 70 | return streams_; |
| 71 | } |
| 72 | |
| 73 | cricket::MediaType media_type() const override { |
| 74 | return cricket::MEDIA_TYPE_VIDEO; |
| 75 | } |
| 76 | |
| 77 | std::string id() const override { return id_; } |
| 78 | |
| 79 | RtpParameters GetParameters() const override; |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 80 | |
| 81 | void SetFrameDecryptor( |
| 82 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) override; |
| 83 | |
| 84 | rtc::scoped_refptr<FrameDecryptorInterface> GetFrameDecryptor() |
| 85 | const override; |
| 86 | |
Marina Ciocea | 412a31b | 2020-02-28 16:02:06 +0100 | [diff] [blame] | 87 | void SetDepacketizerToDecoderFrameTransformer( |
| 88 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) override; |
| 89 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 90 | // RtpReceiverInternal implementation. |
| 91 | void Stop() override; |
| 92 | void SetupMediaChannel(uint32_t ssrc) override; |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 93 | void SetupUnsignaledMediaChannel() override; |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 94 | uint32_t ssrc() const override { return ssrc_.value_or(0); } |
| 95 | void NotifyFirstPacketReceived() override; |
| 96 | void set_stream_ids(std::vector<std::string> stream_ids) override; |
| 97 | void set_transport( |
| 98 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) override { |
| 99 | dtls_transport_ = dtls_transport; |
| 100 | } |
| 101 | void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& |
| 102 | streams) override; |
| 103 | |
| 104 | void SetObserver(RtpReceiverObserverInterface* observer) override; |
| 105 | |
Ruslan Burakov | 4bac79e | 2019-04-03 19:55:33 +0200 | [diff] [blame] | 106 | void SetJitterBufferMinimumDelay( |
| 107 | absl::optional<double> delay_seconds) override; |
| 108 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 109 | void SetMediaChannel(cricket::MediaChannel* media_channel) override; |
| 110 | |
| 111 | int AttachmentId() const override { return attachment_id_; } |
| 112 | |
| 113 | std::vector<RtpSource> GetSources() const override; |
| 114 | |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 115 | private: |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 116 | void RestartMediaChannel(absl::optional<uint32_t> ssrc); |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 117 | void SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) |
| 118 | RTC_RUN_ON(worker_thread_); |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 119 | |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 120 | // VideoRtpTrackSource::Callback |
| 121 | void OnGenerateKeyFrame() override; |
| 122 | void OnEncodedSinkEnabled(bool enable) override; |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 123 | void SetEncodedSinkEnabled(bool enable) RTC_RUN_ON(worker_thread_); |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 124 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 125 | rtc::Thread* const worker_thread_; |
Markus Handell | d5e2f21 | 2019-11-26 09:30:08 +0100 | [diff] [blame] | 126 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 127 | const std::string id_; |
| 128 | cricket::VideoMediaChannel* media_channel_ = nullptr; |
| 129 | absl::optional<uint32_t> ssrc_; |
| 130 | // |source_| is held here to be able to change the state of the source when |
| 131 | // the VideoRtpReceiver is stopped. |
| 132 | rtc::scoped_refptr<VideoRtpTrackSource> source_; |
| 133 | rtc::scoped_refptr<VideoTrackInterface> track_; |
| 134 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_; |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 135 | bool stopped_ = true; |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 136 | RtpReceiverObserverInterface* observer_ = nullptr; |
| 137 | bool received_first_packet_ = false; |
| 138 | int attachment_id_ = 0; |
| 139 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor_; |
| 140 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport_; |
Marina Ciocea | 412a31b | 2020-02-28 16:02:06 +0100 | [diff] [blame] | 141 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer_ |
| 142 | RTC_GUARDED_BY(worker_thread_); |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 143 | // Allows to thread safely change jitter buffer delay. Handles caching cases |
| 144 | // if |SetJitterBufferMinimumDelay| is called before start. |
| 145 | rtc::scoped_refptr<JitterBufferDelayInterface> delay_; |
Markus Handell | 9c27ed2 | 2019-12-04 12:57:58 +0100 | [diff] [blame] | 146 | // Records if we should generate a keyframe when |media_channel_| gets set up |
| 147 | // or switched. |
| 148 | bool saved_generate_keyframe_ RTC_GUARDED_BY(worker_thread_) = false; |
| 149 | bool saved_encoded_sink_enabled_ RTC_GUARDED_BY(worker_thread_) = false; |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | } // namespace webrtc |
| 153 | |
| 154 | #endif // PC_VIDEO_RTP_RECEIVER_H_ |