adapted_video_track_source.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2016 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 MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_
  11. #define MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_
  12. #include <stdint.h>
  13. #include "absl/types/optional.h"
  14. #include "api/media_stream_interface.h"
  15. #include "api/notifier.h"
  16. #include "api/video/video_frame.h"
  17. #include "api/video/video_sink_interface.h"
  18. #include "api/video/video_source_interface.h"
  19. #include "media/base/video_adapter.h"
  20. #include "media/base/video_broadcaster.h"
  21. #include "rtc_base/synchronization/mutex.h"
  22. #include "rtc_base/system/rtc_export.h"
  23. #include "rtc_base/thread_annotations.h"
  24. namespace rtc {
  25. // Base class for sources which needs video adaptation, e.g., video
  26. // capture sources. Sinks must be added and removed on one and only
  27. // one thread, while AdaptFrame and OnFrame may be called on any
  28. // thread.
  29. class RTC_EXPORT AdaptedVideoTrackSource
  30. : public webrtc::Notifier<webrtc::VideoTrackSourceInterface> {
  31. public:
  32. AdaptedVideoTrackSource();
  33. ~AdaptedVideoTrackSource() override;
  34. protected:
  35. // Allows derived classes to initialize |video_adapter_| with a custom
  36. // alignment.
  37. explicit AdaptedVideoTrackSource(int required_alignment);
  38. // Checks the apply_rotation() flag. If the frame needs rotation, and it is a
  39. // plain memory frame, it is rotated. Subclasses producing native frames must
  40. // handle apply_rotation() themselves.
  41. void OnFrame(const webrtc::VideoFrame& frame);
  42. // Reports the appropriate frame size after adaptation. Returns true
  43. // if a frame is wanted. Returns false if there are no interested
  44. // sinks, or if the VideoAdapter decides to drop the frame.
  45. bool AdaptFrame(int width,
  46. int height,
  47. int64_t time_us,
  48. int* out_width,
  49. int* out_height,
  50. int* crop_width,
  51. int* crop_height,
  52. int* crop_x,
  53. int* crop_y);
  54. // Returns the current value of the apply_rotation flag, derived
  55. // from the VideoSinkWants of registered sinks. The value is derived
  56. // from sinks' wants, in AddOrUpdateSink and RemoveSink. Beware that
  57. // when using this method from a different thread, the value may
  58. // become stale before it is used.
  59. bool apply_rotation();
  60. cricket::VideoAdapter* video_adapter() { return &video_adapter_; }
  61. private:
  62. // Implements rtc::VideoSourceInterface.
  63. void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink,
  64. const rtc::VideoSinkWants& wants) override;
  65. void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override;
  66. // Part of VideoTrackSourceInterface.
  67. bool GetStats(Stats* stats) override;
  68. void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
  69. // Encoded sinks not implemented for AdaptedVideoTrackSource.
  70. bool SupportsEncodedOutput() const override { return false; }
  71. void GenerateKeyFrame() override {}
  72. void AddEncodedSink(
  73. rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
  74. void RemoveEncodedSink(
  75. rtc::VideoSinkInterface<webrtc::RecordableEncodedFrame>* sink) override {}
  76. cricket::VideoAdapter video_adapter_;
  77. webrtc::Mutex stats_mutex_;
  78. absl::optional<Stats> stats_ RTC_GUARDED_BY(stats_mutex_);
  79. VideoBroadcaster broadcaster_;
  80. };
  81. } // namespace rtc
  82. #endif // MEDIA_BASE_ADAPTED_VIDEO_TRACK_SOURCE_H_