video_rtp_track_source.h 2.9 KB

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