blob: b7aba1406a4baff3ba444a90e8bbfa82f27db54c [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
Per766ad3b2016-04-05 15:23:49 +020011#ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
kjellandera96e2d72016-02-04 23:52:28 -080012#define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000014#include "webrtc/base/criticalsection.h"
perkj2d5f0912016-02-29 00:04:41 -080015#include "webrtc/base/optional.h"
kjellandera96e2d72016-02-04 23:52:28 -080016#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
18namespace cricket {
19
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020// VideoAdapter adapts an input video frame to an output frame based on the
21// specified input and output formats. The adaptation includes dropping frames
Per766ad3b2016-04-05 15:23:49 +020022// to reduce frame rate and scaling frames.
23// VideoAdapter is thread safe.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024class VideoAdapter {
25 public:
26 VideoAdapter();
27 virtual ~VideoAdapter();
28
Per766ad3b2016-04-05 15:23:49 +020029 // Sets the expected frame interval. This controls how often frames should
30 // be dropped if |OnOutputFormatRequest| is called with a lower frame
31 // interval.
32 void SetExpectedInputFrameInterval(int64_t interval);
magjed@webrtc.orgf58b4552014-11-19 18:09:14 +000033
34 // Return the adapted resolution given the input resolution. The returned
35 // resolution will be 0x0 if the frame should be dropped.
36 VideoFormat AdaptFrameResolution(int in_width, int in_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
Per766ad3b2016-04-05 15:23:49 +020038 // Requests the output frame size and frame interval from
39 // |AdaptFrameResolution| to not be larger than |format|.
40 void OnOutputFormatRequest(const VideoFormat& format);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000041
Per766ad3b2016-04-05 15:23:49 +020042 // Requests the output frame size from |AdaptFrameResolution| to not have
43 // more than |max_pixel_count| pixels and have "one step" up more pixels than
44 // max_pixel_count_step_up.
45 void OnResolutionRequest(rtc::Optional<int> max_pixel_count,
46 rtc::Optional<int> max_pixel_count_step_up);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +000047
Per766ad3b2016-04-05 15:23:49 +020048 const VideoFormat& input_format() const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
50 private:
Per766ad3b2016-04-05 15:23:49 +020051 void SetInputFormat(const VideoFormat& format);
52 bool Adapt(int max_num_pixels, int max_pixel_count_step_up);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54 VideoFormat input_format_;
55 VideoFormat output_format_;
56 int output_num_pixels_;
Per766ad3b2016-04-05 15:23:49 +020057 int frames_in_; // Number of input frames.
58 int frames_out_; // Number of output frames.
59 int frames_scaled_; // Number of frames scaled.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000060 int adaption_changes_; // Number of changes in scale factor.
Per766ad3b2016-04-05 15:23:49 +020061 int previous_width_; // Previous adapter output width.
62 int previous_height_; // Previous adapter output height.
Peter Boström0c4e06b2015-10-07 12:23:21 +020063 int64_t interval_next_frame_;
Per766ad3b2016-04-05 15:23:49 +020064
65 // Max number of pixels requested via calls to OnOutputFormatRequest,
66 // OnResolutionRequest respectively.
67 // The adapted output format is the minimum of these.
68 int format_request_max_pixel_count_;
69 int resolution_request_max_pixel_count_;
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 // The critical section to protect the above variables.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 rtc::CriticalSection critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
henrikg3c089d72015-09-16 05:37:44 -070074 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075};
76
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077} // namespace cricket
78
Per766ad3b2016-04-05 15:23:49 +020079#endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_