video_adapter.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/critical_section.h"
  19. #include "rtc_base/thread_annotations.h"
  20. namespace cricket {
  21. // VideoAdapter adapts an input video frame to an output frame based on the
  22. // specified input and output formats. The adaptation includes dropping frames
  23. // to reduce frame rate and scaling frames.
  24. // VideoAdapter is thread safe.
  25. class VideoAdapter {
  26. public:
  27. VideoAdapter();
  28. // The source requests output frames whose width and height are divisible
  29. // by |source_resolution_alignment|.
  30. explicit VideoAdapter(int source_resolution_alignment);
  31. virtual ~VideoAdapter();
  32. // Return the adapted resolution and cropping parameters given the
  33. // input resolution. The input frame should first be cropped, then
  34. // scaled to the final output resolution. Returns true if the frame
  35. // should be adapted, and false if it should be dropped.
  36. bool AdaptFrameResolution(int in_width,
  37. int in_height,
  38. int64_t in_timestamp_ns,
  39. int* cropped_width,
  40. int* cropped_height,
  41. int* out_width,
  42. int* out_height)
  43. RTC_LOCKS_EXCLUDED(critical_section_);
  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(critical_section_);
  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(critical_section_);
  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(critical_section_);
  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(critical_section_);
  89. private:
  90. // Determine if frame should be dropped based on input fps and requested fps.
  91. bool KeepFrame(int64_t in_timestamp_ns)
  92. RTC_EXCLUSIVE_LOCKS_REQUIRED(critical_section_);
  93. int frames_in_ RTC_GUARDED_BY(critical_section_); // Number of input frames.
  94. int frames_out_
  95. RTC_GUARDED_BY(critical_section_); // Number of output frames.
  96. int frames_scaled_
  97. RTC_GUARDED_BY(critical_section_); // Number of frames scaled.
  98. int adaption_changes_
  99. RTC_GUARDED_BY(critical_section_); // Number of changes in scale factor.
  100. int previous_width_
  101. RTC_GUARDED_BY(critical_section_); // Previous adapter output width.
  102. int previous_height_
  103. RTC_GUARDED_BY(critical_section_); // Previous adapter output height.
  104. const bool variable_start_scale_factor_;
  105. // The fixed source resolution alignment requirement.
  106. const int source_resolution_alignment_;
  107. // The currently applied resolution alignment, as given by the requirements:
  108. // - the fixed |source_resolution_alignment_|; and
  109. // - the latest |sink_wants.resolution_alignment|.
  110. int resolution_alignment_ RTC_GUARDED_BY(critical_section_);
  111. // The target timestamp for the next frame based on requested format.
  112. absl::optional<int64_t> next_frame_timestamp_ns_
  113. RTC_GUARDED_BY(critical_section_);
  114. // Max number of pixels/fps requested via calls to OnOutputFormatRequest,
  115. // OnResolutionFramerateRequest respectively.
  116. // The adapted output format is the minimum of these.
  117. absl::optional<std::pair<int, int>> target_landscape_aspect_ratio_
  118. RTC_GUARDED_BY(critical_section_);
  119. absl::optional<int> max_landscape_pixel_count_
  120. RTC_GUARDED_BY(critical_section_);
  121. absl::optional<std::pair<int, int>> target_portrait_aspect_ratio_
  122. RTC_GUARDED_BY(critical_section_);
  123. absl::optional<int> max_portrait_pixel_count_
  124. RTC_GUARDED_BY(critical_section_);
  125. absl::optional<int> max_fps_ RTC_GUARDED_BY(critical_section_);
  126. int resolution_request_target_pixel_count_ RTC_GUARDED_BY(critical_section_);
  127. int resolution_request_max_pixel_count_ RTC_GUARDED_BY(critical_section_);
  128. int max_framerate_request_ RTC_GUARDED_BY(critical_section_);
  129. // The critical section to protect the above variables.
  130. rtc::CriticalSection critical_section_;
  131. RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
  132. };
  133. } // namespace cricket
  134. #endif // MEDIA_BASE_VIDEO_ADAPTER_H_