blob: 1e4179ca68e7468b526207e0fccdf96f55bf70cc [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
Henrik Kjellander2557b862015-11-18 22:00:21 +010011#ifndef WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_
12#define WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000013
14#include <list>
kwiberg3f55dea2016-02-29 05:51:59 -080015#include <memory>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010017#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/include/video_coding.h"
19#include "webrtc/modules/video_coding/media_opt_util.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020020#include "webrtc/rtc_base/criticalsection.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000024class Clock;
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000025class FrameDropper;
niklase@google.com470e71d2011-07-07 08:21:25 +000026class VCMContentMetricsProcessing;
niklase@google.com470e71d2011-07-07 08:21:25 +000027
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000028namespace media_optimization {
29
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000030class MediaOptimization {
31 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000032 explicit MediaOptimization(Clock* clock);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000033 ~MediaOptimization();
niklase@google.com470e71d2011-07-07 08:21:25 +000034
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000035 // TODO(andresp): Can Reset and SetEncodingData be done at construction time
36 // only?
37 void Reset();
38
39 // Informs media optimization of initial encoding state.
Per69b332d2016-06-02 15:45:42 +020040 // TODO(perkj): Deprecate SetEncodingData once its not used for stats in
41 // VieEncoder.
42 void SetEncodingData(int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000043 uint32_t bit_rate,
asapersson60dfbdb2017-08-07 00:03:03 -070044 uint32_t frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000046 // Sets target rates for the encoder given the channel parameters.
asapersson60dfbdb2017-08-07 00:03:03 -070047 // Input: |target bitrate| - the encoder target bitrate in bits/s.
asapersson9abd2752016-12-08 02:19:40 -080048 uint32_t SetTargetRates(uint32_t target_bitrate);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000050 void EnableFrameDropper(bool enable);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000051 bool DropFrame();
niklase@google.com470e71d2011-07-07 08:21:25 +000052
pbos@webrtc.org273a4142014-12-01 15:23:21 +000053 // Informs Media Optimization of encoded output.
Per69b332d2016-06-02 15:45:42 +020054 // TODO(perkj): Deprecate SetEncodingData once its not used for stats in
55 // VieEncoder.
pbos@webrtc.org273a4142014-12-01 15:23:21 +000056 int32_t UpdateWithEncodedData(const EncodedImage& encoded_image);
mikhal@google.comb29d9402011-08-01 16:39:20 +000057
Erik Språng66a641a2015-06-11 14:20:07 +020058 // InputFrameRate 0 = no frame rate estimate available.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000059 uint32_t InputFrameRate();
60 uint32_t SentFrameRate();
61 uint32_t SentBitRate();
niklase@google.com470e71d2011-07-07 08:21:25 +000062
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000063 private:
philipel9d3ab612015-12-21 04:12:39 -080064 enum { kFrameCountHistorySize = 90 };
65 enum { kFrameHistoryWinMs = 2000 };
66 enum { kBitrateAverageWinMs = 1000 };
niklase@google.com470e71d2011-07-07 08:21:25 +000067
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000068 struct EncodedFrameSample;
69 typedef std::list<EncodedFrameSample> FrameSampleList;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000071 void UpdateIncomingFrameRate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Sebastian Jansson9e3f1e42017-07-06 16:52:09 +020072 void PurgeOldFrameSamples(int64_t threshold_ms)
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000073 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000074 void UpdateSentFramerate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000076 void ProcessIncomingFrameRate(int64_t now)
77 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +000079 // Checks conditions for suspending the video. The method compares
pbos73674632015-10-29 15:45:00 -070080 // |video_target_bitrate_| with the threshold values for suspension, and
81 // changes the state of |video_suspended_| accordingly.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000082 void CheckSuspendConditions() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +000083
Per69b332d2016-06-02 15:45:42 +020084 void SetEncodingDataInternal(int32_t max_bit_rate,
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000085 uint32_t frame_rate,
asapersson60dfbdb2017-08-07 00:03:03 -070086 uint32_t bit_rate)
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000087 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
88
89 uint32_t InputFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
90
91 uint32_t SentFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
92
93 // Protect all members.
kthelgasond701dfd2017-03-27 07:24:57 -070094 rtc::CriticalSection crit_sect_;
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000095
96 Clock* clock_ GUARDED_BY(crit_sect_);
97 int32_t max_bit_rate_ GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000098 float user_frame_rate_ GUARDED_BY(crit_sect_);
kwiberg3f55dea2016-02-29 05:51:59 -080099 std::unique_ptr<FrameDropper> frame_dropper_ GUARDED_BY(crit_sect_);
pbos73674632015-10-29 15:45:00 -0700100 int video_target_bitrate_ GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000101 float incoming_frame_rate_ GUARDED_BY(crit_sect_);
102 int64_t incoming_frame_times_[kFrameCountHistorySize] GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000103 std::list<EncodedFrameSample> encoded_frame_samples_ GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000104 uint32_t avg_sent_framerate_ GUARDED_BY(crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000105};
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000106} // namespace media_optimization
107} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
Henrik Kjellander2557b862015-11-18 22:00:21 +0100109#endif // WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_