video_adapter.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2010 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_VIDEO_ADAPTER_H_
  11. #define MEDIA_BASE_VIDEO_ADAPTER_H_
  12. #include <stdint.h>
  13. #include <utility>
  14. #include "absl/types/optional.h"
  15. #include "api/video/video_source_interface.h"
  16. #include "media/base/video_common.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/synchronization/mutex.h"
  19. #include "rtc_base/system/rtc_export.h"
  20. #include "rtc_base/thread_annotations.h"
  21. namespace cricket {
  22. // VideoAdapter adapts an input video frame to an output frame based on the
  23. // specified input and output formats. The adaptation includes dropping frames
  24. // to reduce frame rate and scaling frames.
  25. // VideoAdapter is thread safe.
  26. class RTC_EXPORT VideoAdapter {
  27. public:
  28. VideoAdapter();
  29. // The source requests output frames whose width and height are divisible
  30. // by |source_resolution_alignment|.
  31. explicit VideoAdapter(int source_resolution_alignment);
  32. virtual ~VideoAdapter();
  33. // Return the adapted resolution and cropping parameters given the
  34. // input resolution. The input frame should first be cropped, then
  35. // scaled to the final output resolution. Returns true if the frame
  36. // should be adapted, and false if it should be dropped.
  37. bool AdaptFrameResolution(int in_width,
  38. int in_height,
  39. int64_t in_timestamp_ns,
  40. int* cropped_width,
  41. int* cropped_height,
  42. int* out_width,
  43. int* out_height) RTC_LOCKS_EXCLUDED(mutex_);
  44. // DEPRECATED. Please use OnOutputFormatRequest below.
  45. // TODO(asapersson): Remove this once it is no longer used.
  46. // Requests the output frame size and frame interval from
  47. // |AdaptFrameResolution| to not be larger than |format|. Also, the input
  48. // frame size will be cropped to match the requested aspect ratio. The
  49. // requested aspect ratio is orientation agnostic and will be adjusted to
  50. // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or
  51. // 720x1280 is requested.
  52. // Note: Should be called from the source only.
  53. void OnOutputFormatRequest(const absl::optional<VideoFormat>& format)
  54. RTC_LOCKS_EXCLUDED(mutex_);
  55. // Requests output frame size and frame interval from |AdaptFrameResolution|.
  56. // |target_aspect_ratio|: The input frame size will be cropped to match the
  57. // requested aspect ratio. The aspect ratio is orientation agnostic and will
  58. // be adjusted to maintain the input orientation (i.e. it doesn't matter if
  59. // e.g. <1280,720> or <720,1280> is requested).
  60. // |max_pixel_count|: The maximum output frame size.
  61. // |max_fps|: The maximum output framerate.
  62. // Note: Should be called from the source only.
  63. void OnOutputFormatRequest(
  64. const absl::optional<std::pair<int, int>>& target_aspect_ratio,
  65. const absl::optional<int>& max_pixel_count,
  66. const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
  67. // Same as above, but allows setting two different target aspect ratios
  68. // depending on incoming frame orientation. This gives more fine-grained
  69. // control and can e.g. be used to force landscape video to be cropped to
  70. // portrait video.
  71. void OnOutputFormatRequest(
  72. const absl::optional<std::pair<int, int>>& target_landscape_aspect_ratio,
  73. const absl::optional<int>& max_landscape_pixel_count,
  74. const absl::optional<std::pair<int, int>>& target_portrait_aspect_ratio,
  75. const absl::optional<int>& max_portrait_pixel_count,
  76. const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
  77. // Requests the output frame size from |AdaptFrameResolution| to have as close
  78. // as possible to |sink_wants.target_pixel_count| pixels (if set)
  79. // but no more than |sink_wants.max_pixel_count|.
  80. // |sink_wants.max_framerate_fps| is essentially analogous to
  81. // |sink_wants.max_pixel_count|, but for framerate rather than resolution.
  82. // Set |sink_wants.max_pixel_count| and/or |sink_wants.max_framerate_fps| to
  83. // std::numeric_limit<int>::max() if no upper limit is desired.
  84. // The sink resolution alignment requirement is given by
  85. // |sink_wants.resolution_alignment|.
  86. // Note: Should be called from the sink only.
  87. void OnSinkWants(const rtc::VideoSinkWants& sink_wants)
  88. RTC_LOCKS_EXCLUDED(mutex_);
  89. // Returns maximum image area, which shouldn't impose any adaptations.
  90. // Can return |numeric_limits<int>::max()| if no limit is set.
  91. int GetTargetPixels() const;
  92. // Returns current frame-rate limit.
  93. // Can return |numeric_limits<float>::infinity()| if no limit is set.
  94. float GetMaxFramerate() const;
  95. private:
  96. // Determine if frame should be dropped based on input fps and requested fps.
  97. bool KeepFrame(int64_t in_timestamp_ns) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  98. int frames_in_ RTC_GUARDED_BY(mutex_); // Number of input frames.
  99. int frames_out_ RTC_GUARDED_BY(mutex_); // Number of output frames.
  100. int frames_scaled_ RTC_GUARDED_BY(mutex_); // Number of frames scaled.
  101. int adaption_changes_
  102. RTC_GUARDED_BY(mutex_); // Number of changes in scale factor.
  103. int previous_width_ RTC_GUARDED_BY(mutex_); // Previous adapter output width.
  104. int previous_height_
  105. RTC_GUARDED_BY(mutex_); // Previous adapter output height.
  106. const bool variable_start_scale_factor_;
  107. // The fixed source resolution alignment requirement.
  108. const int source_resolution_alignment_;
  109. // The currently applied resolution alignment, as given by the requirements:
  110. // - the fixed |source_resolution_alignment_|; and
  111. // - the latest |sink_wants.resolution_alignment|.
  112. int resolution_alignment_ RTC_GUARDED_BY(mutex_);
  113. // The target timestamp for the next frame based on requested format.
  114. absl::optional<int64_t> next_frame_timestamp_ns_ RTC_GUARDED_BY(mutex_);
  115. // Max number of pixels/fps requested via calls to OnOutputFormatRequest,
  116. // OnResolutionFramerateRequest respectively.
  117. // The adapted output format is the minimum of these.
  118. absl::optional<std::pair<int, int>> target_landscape_aspect_ratio_
  119. RTC_GUARDED_BY(mutex_);
  120. absl::optional<int> max_landscape_pixel_count_ RTC_GUARDED_BY(mutex_);
  121. absl::optional<std::pair<int, int>> target_portrait_aspect_ratio_
  122. RTC_GUARDED_BY(mutex_);
  123. absl::optional<int> max_portrait_pixel_count_ RTC_GUARDED_BY(mutex_);
  124. absl::optional<int> max_fps_ RTC_GUARDED_BY(mutex_);
  125. int resolution_request_target_pixel_count_ RTC_GUARDED_BY(mutex_);
  126. int resolution_request_max_pixel_count_ RTC_GUARDED_BY(mutex_);
  127. int max_framerate_request_ RTC_GUARDED_BY(mutex_);
  128. // The critical section to protect the above variables.
  129. mutable webrtc::Mutex mutex_;
  130. RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
  131. };
  132. } // namespace cricket
  133. #endif // MEDIA_BASE_VIDEO_ADAPTER_H_