video_rtp_receiver.h 5.6 KB

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