blob: 5fa025453cd4fb4a8dd96258928e0dd42a26b3f8 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org8fe03af2012-01-23 14:56:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_
12#define MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000013
kwiberg3f55dea2016-02-29 05:51:59 -080014#include <memory>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/include/module_common_types.h"
17#include "modules/video_coding/include/video_coding.h"
18#include "modules/video_coding/media_opt_util.h"
19#include "rtc_base/criticalsection.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000021namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000023class Clock;
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000024class FrameDropper;
niklase@google.com470e71d2011-07-07 08:21:25 +000025
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000026namespace media_optimization {
27
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000028class MediaOptimization {
29 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000030 explicit MediaOptimization(Clock* clock);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000031 ~MediaOptimization();
niklase@google.com470e71d2011-07-07 08:21:25 +000032
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000033 // TODO(andresp): Can Reset and SetEncodingData be done at construction time
34 // only?
35 void Reset();
36
37 // Informs media optimization of initial encoding state.
Per69b332d2016-06-02 15:45:42 +020038 // TODO(perkj): Deprecate SetEncodingData once its not used for stats in
Åsa Perssonb52a4d92017-11-08 11:33:37 +010039 // VideoStreamEncoder.
Per69b332d2016-06-02 15:45:42 +020040 void SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000041 uint32_t bit_rate,
Åsa Perssonb52a4d92017-11-08 11:33:37 +010042 uint32_t max_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000043
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000044 // Sets target rates for the encoder given the channel parameters.
asapersson60dfbdb2017-08-07 00:03:03 -070045 // Input: |target bitrate| - the encoder target bitrate in bits/s.
asapersson9abd2752016-12-08 02:19:40 -080046 uint32_t SetTargetRates(uint32_t target_bitrate);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000048 void EnableFrameDropper(bool enable);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000049 bool DropFrame();
niklase@google.com470e71d2011-07-07 08:21:25 +000050
pbos@webrtc.org273a4142014-12-01 15:23:21 +000051 // Informs Media Optimization of encoded output.
Per69b332d2016-06-02 15:45:42 +020052 // TODO(perkj): Deprecate SetEncodingData once its not used for stats in
Åsa Perssonb52a4d92017-11-08 11:33:37 +010053 // VideoStreamEncoder.
Taylor Brandstetter00733012018-02-15 20:07:11 +000054 void UpdateWithEncodedData(const EncodedImage& encoded_image);
mikhal@google.comb29d9402011-08-01 16:39:20 +000055
Erik Språng66a641a2015-06-11 14:20:07 +020056 // InputFrameRate 0 = no frame rate estimate available.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000057 uint32_t InputFrameRate();
niklase@google.com470e71d2011-07-07 08:21:25 +000058
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000059 private:
philipel9d3ab612015-12-21 04:12:39 -080060 enum { kFrameCountHistorySize = 90 };
niklase@google.com470e71d2011-07-07 08:21:25 +000061
danilchap56359be2017-09-07 07:53:45 -070062 void UpdateIncomingFrameRate() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000063 void ProcessIncomingFrameRate(int64_t now)
danilchap56359be2017-09-07 07:53:45 -070064 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -070065 uint32_t InputFrameRateInternal() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000066
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000067 // Protect all members.
kthelgasond701dfd2017-03-27 07:24:57 -070068 rtc::CriticalSection crit_sect_;
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000069
Åsa Perssonb52a4d92017-11-08 11:33:37 +010070 Clock* const clock_ RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -070071 int32_t max_bit_rate_ RTC_GUARDED_BY(crit_sect_);
Åsa Perssonb52a4d92017-11-08 11:33:37 +010072 float max_frame_rate_ RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -070073 std::unique_ptr<FrameDropper> frame_dropper_ RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -070074 float incoming_frame_rate_ RTC_GUARDED_BY(crit_sect_);
75 int64_t incoming_frame_times_[kFrameCountHistorySize] RTC_GUARDED_BY(
76 crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000077};
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000078} // namespace media_optimization
79} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000080
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020081#endif // MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_