blob: 081b2a900a2132d38e0435b033b8b484007024ac [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"
Henrik Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/critical_section_wrapper.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
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000024// Forward declarations.
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000025class Clock;
stefan@webrtc.orgeb917922013-02-18 14:40:18 +000026class FrameDropper;
niklase@google.com470e71d2011-07-07 08:21:25 +000027class VCMContentMetricsProcessing;
niklase@google.com470e71d2011-07-07 08:21:25 +000028
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000029namespace media_optimization {
30
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000031class MediaOptimization {
32 public:
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000033 explicit MediaOptimization(Clock* clock);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000034 ~MediaOptimization();
niklase@google.com470e71d2011-07-07 08:21:25 +000035
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000036 // TODO(andresp): Can Reset and SetEncodingData be done at construction time
37 // only?
38 void Reset();
39
40 // Informs media optimization of initial encoding state.
41 void SetEncodingData(VideoCodecType send_codec_type,
42 int32_t max_bit_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000043 uint32_t bit_rate,
44 uint16_t width,
45 uint16_t height,
Peter Boströmdf664532015-05-12 12:22:14 +020046 uint32_t frame_rate,
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000047 int num_temporal_layers,
48 int32_t mtu);
niklase@google.com470e71d2011-07-07 08:21:25 +000049
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000050 // Sets target rates for the encoder given the channel parameters.
51 // Inputs: target bitrate - the encoder target bitrate in bits/s.
52 // fraction_lost - packet loss rate in % in the network.
53 // round_trip_time_ms - round trip time in milliseconds.
54 // min_bit_rate - the bit rate of the end-point with lowest rate.
55 // max_bit_rate - the bit rate of the end-point with highest rate.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000056 // TODO(andresp): Find if the callbacks can be triggered only after releasing
57 // an internal critical section.
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000058 uint32_t SetTargetRates(uint32_t target_bitrate,
59 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +000060 int64_t round_trip_time_ms,
Peter Boströmad6fc5a2016-05-12 03:01:31 +020061 VCMProtectionCallback* protection_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
pbosba8c15b2015-07-14 09:36:34 -070063 void SetProtectionMethod(VCMProtectionMethodEnum method);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000064 void EnableFrameDropper(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000066 // Lets the sender suspend video when the rate drops below
67 // |threshold_bps|, and turns back on when the rate goes back up above
68 // |threshold_bps| + |window_bps|.
69 void SuspendBelowMinBitrate(int threshold_bps, int window_bps);
70 bool IsVideoSuspended() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000072 bool DropFrame();
niklase@google.com470e71d2011-07-07 08:21:25 +000073
pbos@webrtc.org273a4142014-12-01 15:23:21 +000074 // Informs Media Optimization of encoded output.
75 int32_t UpdateWithEncodedData(const EncodedImage& encoded_image);
mikhal@google.comb29d9402011-08-01 16:39:20 +000076
Erik Språng66a641a2015-06-11 14:20:07 +020077 // InputFrameRate 0 = no frame rate estimate available.
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000078 uint32_t InputFrameRate();
79 uint32_t SentFrameRate();
80 uint32_t SentBitRate();
niklase@google.com470e71d2011-07-07 08:21:25 +000081
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000082 private:
philipel9d3ab612015-12-21 04:12:39 -080083 enum { kFrameCountHistorySize = 90 };
84 enum { kFrameHistoryWinMs = 2000 };
85 enum { kBitrateAverageWinMs = 1000 };
niklase@google.com470e71d2011-07-07 08:21:25 +000086
andresp@webrtc.orge682aa52013-12-19 10:59:48 +000087 struct EncodedFrameSample;
88 typedef std::list<EncodedFrameSample> FrameSampleList;
niklase@google.com470e71d2011-07-07 08:21:25 +000089
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000090 void UpdateIncomingFrameRate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
91 void PurgeOldFrameSamples(int64_t now_ms)
92 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
93 void UpdateSentBitrate(int64_t now_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
94 void UpdateSentFramerate() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000095
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +000096 void ProcessIncomingFrameRate(int64_t now)
97 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
henrik.lundin@webrtc.orgce8e0932013-11-18 12:18:43 +000099 // Checks conditions for suspending the video. The method compares
pbos73674632015-10-29 15:45:00 -0700100 // |video_target_bitrate_| with the threshold values for suspension, and
101 // changes the state of |video_suspended_| accordingly.
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000102 void CheckSuspendConditions() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
henrik.lundin@webrtc.org544b17c2013-09-26 12:05:15 +0000103
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000104 void SetEncodingDataInternal(VideoCodecType send_codec_type,
105 int32_t max_bit_rate,
106 uint32_t frame_rate,
107 uint32_t bit_rate,
108 uint16_t width,
109 uint16_t height,
110 int num_temporal_layers,
111 int32_t mtu)
112 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
113
114 uint32_t InputFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
115
116 uint32_t SentFrameRateInternal() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
117
118 // Protect all members.
kwiberg3f55dea2016-02-29 05:51:59 -0800119 std::unique_ptr<CriticalSectionWrapper> crit_sect_;
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000120
121 Clock* clock_ GUARDED_BY(crit_sect_);
122 int32_t max_bit_rate_ GUARDED_BY(crit_sect_);
123 VideoCodecType send_codec_type_ GUARDED_BY(crit_sect_);
124 uint16_t codec_width_ GUARDED_BY(crit_sect_);
125 uint16_t codec_height_ GUARDED_BY(crit_sect_);
126 float user_frame_rate_ GUARDED_BY(crit_sect_);
kwiberg3f55dea2016-02-29 05:51:59 -0800127 std::unique_ptr<FrameDropper> frame_dropper_ GUARDED_BY(crit_sect_);
128 std::unique_ptr<VCMLossProtectionLogic> loss_prot_logic_
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000129 GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000130 uint8_t fraction_lost_ GUARDED_BY(crit_sect_);
131 uint32_t send_statistics_[4] GUARDED_BY(crit_sect_);
132 uint32_t send_statistics_zero_encode_ GUARDED_BY(crit_sect_);
133 int32_t max_payload_size_ GUARDED_BY(crit_sect_);
pbos73674632015-10-29 15:45:00 -0700134 int video_target_bitrate_ GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000135 float incoming_frame_rate_ GUARDED_BY(crit_sect_);
136 int64_t incoming_frame_times_[kFrameCountHistorySize] GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000137 std::list<EncodedFrameSample> encoded_frame_samples_ GUARDED_BY(crit_sect_);
138 uint32_t avg_sent_bit_rate_bps_ GUARDED_BY(crit_sect_);
139 uint32_t avg_sent_framerate_ GUARDED_BY(crit_sect_);
140 uint32_t key_frame_cnt_ GUARDED_BY(crit_sect_);
141 uint32_t delta_frame_cnt_ GUARDED_BY(crit_sect_);
wuchengli@chromium.orgae7cfd72014-06-30 08:01:47 +0000142 int num_layers_ GUARDED_BY(crit_sect_);
143 bool suspension_enabled_ GUARDED_BY(crit_sect_);
144 bool video_suspended_ GUARDED_BY(crit_sect_);
145 int suspension_threshold_bps_ GUARDED_BY(crit_sect_);
146 int suspension_window_bps_ GUARDED_BY(crit_sect_);
andresp@webrtc.orge682aa52013-12-19 10:59:48 +0000147};
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000148} // namespace media_optimization
149} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
Henrik Kjellander2557b862015-11-18 22:00:21 +0100151#endif // WEBRTC_MODULES_VIDEO_CODING_MEDIA_OPTIMIZATION_H_