video_rtp_track_source.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_TRACK_SOURCE_H_
  11. #define PC_VIDEO_RTP_TRACK_SOURCE_H_
  12. #include <vector>
  13. #include "media/base/video_broadcaster.h"
  14. #include "pc/video_track_source.h"
  15. #include "rtc_base/callback.h"
  16. #include "rtc_base/critical_section.h"
  17. namespace webrtc {
  18. // Video track source in use by VideoRtpReceiver
  19. class VideoRtpTrackSource : public VideoTrackSource {
  20. public:
  21. class Callback {
  22. public:
  23. virtual ~Callback() = default;
  24. // Called when a keyframe should be generated
  25. virtual void OnGenerateKeyFrame() = 0;
  26. // Called when the implementor should eventually start to serve encoded
  27. // frames using BroadcastEncodedFrameBuffer.
  28. // The implementor should cause a keyframe to be eventually generated.
  29. virtual void OnEncodedSinkEnabled(bool enable) = 0;
  30. };
  31. explicit VideoRtpTrackSource(Callback* callback);
  32. // Call before the object implementing Callback finishes it's destructor. No
  33. // more callbacks will be fired after completion. Must be called on the
  34. // worker thread
  35. void ClearCallback();
  36. // Call to broadcast an encoded frame to registered sinks.
  37. // This method can be called on any thread or queue.
  38. void BroadcastRecordableEncodedFrame(
  39. const RecordableEncodedFrame& frame) const;
  40. // VideoTrackSource
  41. rtc::VideoSourceInterface<VideoFrame>* source() override;
  42. rtc::VideoSinkInterface<VideoFrame>* sink();
  43. // Returns true. This method can be called on any thread.
  44. bool SupportsEncodedOutput() const override;
  45. // Generates a key frame. Must be called on the worker thread.
  46. void GenerateKeyFrame() override;
  47. // Adds an encoded sink. Must be called on the worker thread.
  48. void AddEncodedSink(
  49. rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) override;
  50. // Removes an encoded sink. Must be called on the worker thread.
  51. void RemoveEncodedSink(
  52. rtc::VideoSinkInterface<RecordableEncodedFrame>* sink) override;
  53. private:
  54. SequenceChecker worker_sequence_checker_;
  55. // |broadcaster_| is needed since the decoder can only handle one sink.
  56. // It might be better if the decoder can handle multiple sinks and consider
  57. // the VideoSinkWants.
  58. rtc::VideoBroadcaster broadcaster_;
  59. rtc::CriticalSection mu_;
  60. std::vector<rtc::VideoSinkInterface<RecordableEncodedFrame>*> encoded_sinks_
  61. RTC_GUARDED_BY(mu_);
  62. Callback* callback_ RTC_GUARDED_BY(worker_sequence_checker_);
  63. RTC_DISALLOW_COPY_AND_ASSIGN(VideoRtpTrackSource);
  64. };
  65. } // namespace webrtc
  66. #endif // PC_VIDEO_RTP_TRACK_SOURCE_H_