OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ | 11 #ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
12 #define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ | 12 #define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
13 | 13 |
14 #include "webrtc/base/constructormagic.h" | 14 #include "webrtc/base/constructormagic.h" |
15 #include "webrtc/base/criticalsection.h" | 15 #include "webrtc/base/criticalsection.h" |
16 #include "webrtc/base/optional.h" | 16 #include "webrtc/base/optional.h" |
17 #include "webrtc/media/base/videocommon.h" | 17 #include "webrtc/media/base/videocommon.h" |
18 | 18 |
19 namespace cricket { | 19 namespace cricket { |
20 | 20 |
21 // VideoAdapter adapts an input video frame to an output frame based on the | 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 | 22 // specified input and output formats. The adaptation includes dropping frames |
23 // to reduce frame rate and scaling frames. | 23 // to reduce frame rate and scaling frames. |
24 // VideoAdapter is thread safe. | 24 // VideoAdapter is thread safe. |
25 class VideoAdapter { | 25 class VideoAdapter { |
26 public: | 26 public: |
27 VideoAdapter(); | 27 VideoAdapter(); |
28 // The output frames will have height and width that is divisible by | 28 VideoAdapter(int required_resolution_alignment); |
29 // |required_resolution_alignment|. | |
30 explicit VideoAdapter(int required_resolution_alignment); | |
31 virtual ~VideoAdapter(); | 29 virtual ~VideoAdapter(); |
32 | 30 |
33 // Return the adapted resolution and cropping parameters given the | 31 // Return the adapted resolution and cropping parameters given the |
34 // input resolution. The input frame should first be cropped, then | 32 // input resolution. The input frame should first be cropped, then |
35 // scaled to the final output resolution. Returns true if the frame | 33 // scaled to the final output resolution. Returns true if the frame |
36 // should be adapted, and false if it should be dropped. | 34 // should be adapted, and false if it should be dropped. |
37 bool AdaptFrameResolution(int in_width, | 35 bool AdaptFrameResolution(int in_width, |
38 int in_height, | 36 int in_height, |
39 int64_t in_timestamp_ns, | 37 int64_t in_timestamp_ns, |
40 int* cropped_width, | 38 int* cropped_width, |
41 int* cropped_height, | 39 int* cropped_height, |
42 int* out_width, | 40 int* out_width, |
43 int* out_height); | 41 int* out_height); |
44 | 42 |
45 // Requests the output frame size and frame interval from | 43 // Requests the output frame size and frame interval from |
46 // |AdaptFrameResolution| to not be larger than |format|. Also, the input | 44 // |AdaptFrameResolution| to not be larger than |format|. Also, the input |
47 // frame size will be cropped to match the requested aspect ratio. The | 45 // frame size will be cropped to match the requested aspect ratio. The |
48 // requested aspect ratio is orientation agnostic and will be adjusted to | 46 // requested aspect ratio is orientation agnostic and will be adjusted to |
49 // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or | 47 // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or |
50 // 720x1280 is requested. | 48 // 720x1280 is requested. |
51 void OnOutputFormatRequest(const VideoFormat& format); | 49 void OnOutputFormatRequest(const VideoFormat& format); |
52 | 50 |
53 // Requests the output frame size from |AdaptFrameResolution| to have as close | 51 // Requests the output frame size from |AdaptFrameResolution| to have as close |
54 // as possible to |target_pixel_count| pixels (if set) but no more than | 52 // as possible to |target_pixel_count|, but no more than |max_pixel_count| |
55 // |max_pixel_count|. | 53 // pixels. If |target_pixel_count| is not set, treat it as being equal to |
56 // |max_framerate_fps| is essentially analogous to |max_pixel_count|, but for | 54 // |max_pixel_count|. If |max_pixel_count| is not set, treat is as being the |
57 // framerate rather than resolution. | 55 // highest resolution available. |
58 // Set |max_pixel_count| and/or |max_framerate_fps| to | 56 void OnResolutionRequest(const rtc::Optional<int>& target_pixel_count, |
59 // std::numeric_limit<int>::max() if no upper limit is desired. | 57 const rtc::Optional<int>& max_pixel_count); |
60 void OnResolutionFramerateRequest( | |
61 const rtc::Optional<int>& target_pixel_count, | |
62 int max_pixel_count, | |
63 int max_framerate_fps); | |
64 | 58 |
65 private: | 59 private: |
66 // Determine if frame should be dropped based on input fps and requested fps. | 60 // Determine if frame should be dropped based on input fps and requested fps. |
67 bool KeepFrame(int64_t in_timestamp_ns); | 61 bool KeepFrame(int64_t in_timestamp_ns); |
68 | 62 |
69 int frames_in_; // Number of input frames. | 63 int frames_in_; // Number of input frames. |
70 int frames_out_; // Number of output frames. | 64 int frames_out_; // Number of output frames. |
71 int frames_scaled_; // Number of frames scaled. | 65 int frames_scaled_; // Number of frames scaled. |
72 int adaption_changes_; // Number of changes in scale factor. | 66 int adaption_changes_; // Number of changes in scale factor. |
73 int previous_width_; // Previous adapter output width. | 67 int previous_width_; // Previous adapter output width. |
74 int previous_height_; // Previous adapter output height. | 68 int previous_height_; // Previous adapter output height. |
75 // Resolution must be divisible by this factor. | 69 // Resolution must be divisible by this factor. |
76 const int required_resolution_alignment_; | 70 const int required_resolution_alignment_; |
77 // The target timestamp for the next frame based on requested format. | 71 // The target timestamp for the next frame based on requested format. |
78 rtc::Optional<int64_t> next_frame_timestamp_ns_ GUARDED_BY(critical_section_); | 72 rtc::Optional<int64_t> next_frame_timestamp_ns_ GUARDED_BY(critical_section_); |
79 | 73 |
80 // Max number of pixels requested via calls to OnOutputFormatRequest, | 74 // Max number of pixels requested via calls to OnOutputFormatRequest, |
81 // OnResolutionRequest respectively. | 75 // OnResolutionRequest respectively. |
82 // The adapted output format is the minimum of these. | 76 // The adapted output format is the minimum of these. |
83 rtc::Optional<VideoFormat> requested_format_ GUARDED_BY(critical_section_); | 77 rtc::Optional<VideoFormat> requested_format_ GUARDED_BY(critical_section_); |
84 int resolution_request_target_pixel_count_ GUARDED_BY(critical_section_); | 78 int resolution_request_target_pixel_count_ GUARDED_BY(critical_section_); |
85 int resolution_request_max_pixel_count_ GUARDED_BY(critical_section_); | 79 int resolution_request_max_pixel_count_ GUARDED_BY(critical_section_); |
86 int max_framerate_request_ GUARDED_BY(critical_section_); | |
87 | 80 |
88 // The critical section to protect the above variables. | 81 // The critical section to protect the above variables. |
89 rtc::CriticalSection critical_section_; | 82 rtc::CriticalSection critical_section_; |
90 | 83 |
91 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); | 84 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
92 }; | 85 }; |
93 | 86 |
94 } // namespace cricket | 87 } // namespace cricket |
95 | 88 |
96 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ | 89 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
OLD | NEW |