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 VideoAdapter(int required_resolution_alignment); | 28 // The output frames will have height and width that is divisible by |
| 29 // |required_resolution_alignment|. |
| 30 explicit VideoAdapter(int required_resolution_alignment); |
29 virtual ~VideoAdapter(); | 31 virtual ~VideoAdapter(); |
30 | 32 |
31 // Return the adapted resolution and cropping parameters given the | 33 // Return the adapted resolution and cropping parameters given the |
32 // input resolution. The input frame should first be cropped, then | 34 // input resolution. The input frame should first be cropped, then |
33 // scaled to the final output resolution. Returns true if the frame | 35 // scaled to the final output resolution. Returns true if the frame |
34 // should be adapted, and false if it should be dropped. | 36 // should be adapted, and false if it should be dropped. |
35 bool AdaptFrameResolution(int in_width, | 37 bool AdaptFrameResolution(int in_width, |
36 int in_height, | 38 int in_height, |
37 int64_t in_timestamp_ns, | 39 int64_t in_timestamp_ns, |
38 int* cropped_width, | 40 int* cropped_width, |
39 int* cropped_height, | 41 int* cropped_height, |
40 int* out_width, | 42 int* out_width, |
41 int* out_height); | 43 int* out_height); |
42 | 44 |
43 // Requests the output frame size and frame interval from | 45 // Requests the output frame size and frame interval from |
44 // |AdaptFrameResolution| to not be larger than |format|. Also, the input | 46 // |AdaptFrameResolution| to not be larger than |format|. Also, the input |
45 // frame size will be cropped to match the requested aspect ratio. The | 47 // frame size will be cropped to match the requested aspect ratio. The |
46 // requested aspect ratio is orientation agnostic and will be adjusted to | 48 // requested aspect ratio is orientation agnostic and will be adjusted to |
47 // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or | 49 // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or |
48 // 720x1280 is requested. | 50 // 720x1280 is requested. |
49 void OnOutputFormatRequest(const VideoFormat& format); | 51 void OnOutputFormatRequest(const VideoFormat& format); |
50 | 52 |
51 // Requests the output frame size from |AdaptFrameResolution| to have as close | 53 // Requests the output frame size from |AdaptFrameResolution| to have as close |
52 // as possible to |target_pixel_count|, but no more than |max_pixel_count| | 54 // as possible to |target_pixel_count| pixels (if set) but no more than |
53 // pixels. If |target_pixel_count| is not set, treat it as being equal to | 55 // |max_pixel_count|. |
54 // |max_pixel_count|. If |max_pixel_count| is not set, treat is as being the | 56 // |max_framerate_fps| is essentially analogous to |max_pixel_count|, but for |
55 // highest resolution available. | 57 // framerate rather than resolution. |
56 void OnResolutionRequest(const rtc::Optional<int>& target_pixel_count, | 58 // Set |max_pixel_count| and/or |max_framerate_fps| to |
57 const rtc::Optional<int>& max_pixel_count); | 59 // std::numeric_limit<int>::max() if no upper limit is desired. |
| 60 void OnResolutionFramerateRequest( |
| 61 const rtc::Optional<int>& target_pixel_count, |
| 62 int max_pixel_count, |
| 63 int max_framerate_fps); |
58 | 64 |
59 private: | 65 private: |
60 // Determine if frame should be dropped based on input fps and requested fps. | 66 // Determine if frame should be dropped based on input fps and requested fps. |
61 bool KeepFrame(int64_t in_timestamp_ns); | 67 bool KeepFrame(int64_t in_timestamp_ns); |
62 | 68 |
63 int frames_in_; // Number of input frames. | 69 int frames_in_; // Number of input frames. |
64 int frames_out_; // Number of output frames. | 70 int frames_out_; // Number of output frames. |
65 int frames_scaled_; // Number of frames scaled. | 71 int frames_scaled_; // Number of frames scaled. |
66 int adaption_changes_; // Number of changes in scale factor. | 72 int adaption_changes_; // Number of changes in scale factor. |
67 int previous_width_; // Previous adapter output width. | 73 int previous_width_; // Previous adapter output width. |
68 int previous_height_; // Previous adapter output height. | 74 int previous_height_; // Previous adapter output height. |
69 // Resolution must be divisible by this factor. | 75 // Resolution must be divisible by this factor. |
70 const int required_resolution_alignment_; | 76 const int required_resolution_alignment_; |
71 // The target timestamp for the next frame based on requested format. | 77 // The target timestamp for the next frame based on requested format. |
72 rtc::Optional<int64_t> next_frame_timestamp_ns_ GUARDED_BY(critical_section_); | 78 rtc::Optional<int64_t> next_frame_timestamp_ns_ GUARDED_BY(critical_section_); |
73 | 79 |
74 // Max number of pixels requested via calls to OnOutputFormatRequest, | 80 // Max number of pixels requested via calls to OnOutputFormatRequest, |
75 // OnResolutionRequest respectively. | 81 // OnResolutionRequest respectively. |
76 // The adapted output format is the minimum of these. | 82 // The adapted output format is the minimum of these. |
77 rtc::Optional<VideoFormat> requested_format_ GUARDED_BY(critical_section_); | 83 rtc::Optional<VideoFormat> requested_format_ GUARDED_BY(critical_section_); |
78 int resolution_request_target_pixel_count_ GUARDED_BY(critical_section_); | 84 int resolution_request_target_pixel_count_ GUARDED_BY(critical_section_); |
79 int resolution_request_max_pixel_count_ GUARDED_BY(critical_section_); | 85 int resolution_request_max_pixel_count_ GUARDED_BY(critical_section_); |
| 86 int max_framerate_request_ GUARDED_BY(critical_section_); |
80 | 87 |
81 // The critical section to protect the above variables. | 88 // The critical section to protect the above variables. |
82 rtc::CriticalSection critical_section_; | 89 rtc::CriticalSection critical_section_; |
83 | 90 |
84 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); | 91 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
85 }; | 92 }; |
86 | 93 |
87 } // namespace cricket | 94 } // namespace cricket |
88 | 95 |
89 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ | 96 #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
OLD | NEW |