niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 07b45a5 | 2012-02-02 08:37:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef VIDEO_VIDEO_STREAM_ENCODER_H_ |
| 12 | #define VIDEO_VIDEO_STREAM_ENCODER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 14 | #include <atomic> |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 15 | #include <map> |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 16 | #include <memory> |
perkj | 376b192 | 2016-05-02 11:35:24 -0700 | [diff] [blame] | 17 | #include <string> |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 18 | #include <vector> |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/video/video_rotation.h" |
| 21 | #include "api/video_codecs/video_encoder.h" |
Niels Möller | c6ce9c5 | 2018-05-11 11:15:30 +0200 | [diff] [blame^] | 22 | #include "api/video/video_sink_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "call/call.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 24 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "common_video/include/video_bitrate_allocator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/video_coding/include/video_coding_defines.h" |
| 27 | #include "modules/video_coding/utility/quality_scaler.h" |
| 28 | #include "modules/video_coding/video_coding_impl.h" |
| 29 | #include "rtc_base/criticalsection.h" |
| 30 | #include "rtc_base/event.h" |
| 31 | #include "rtc_base/sequenced_task_checker.h" |
| 32 | #include "rtc_base/task_queue.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 33 | #include "typedefs.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "video/overuse_frame_detector.h" |
| 35 | #include "call/video_send_stream.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
| 37 | namespace webrtc { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 38 | |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 39 | class SendStatisticsProxy; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 40 | class VideoBitrateAllocationObserver; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 42 | class VideoStreamEncoderInterface : public rtc::VideoSinkInterface<VideoFrame> { |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 43 | public: |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 44 | // Interface for receiving encoded video frames and notifications about |
| 45 | // configuration changes. |
| 46 | class EncoderSink : public EncodedImageCallback { |
| 47 | public: |
| 48 | virtual void OnEncoderConfigurationChanged( |
| 49 | std::vector<VideoStream> streams, |
| 50 | int min_transmit_bitrate_bps) = 0; |
| 51 | }; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 52 | virtual void SetSource( |
| 53 | rtc::VideoSourceInterface<VideoFrame>* source, |
| 54 | const VideoSendStream::DegradationPreference& degradation_preference) = 0; |
| 55 | virtual void SetSink(EncoderSink* sink, bool rotation_applied) = 0; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 56 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 57 | virtual void SetStartBitrate(int start_bitrate_bps) = 0; |
| 58 | virtual void SendKeyFrame() = 0; |
| 59 | virtual void OnBitrateUpdated(uint32_t bitrate_bps, |
| 60 | uint8_t fraction_lost, |
| 61 | int64_t round_trip_time_ms) = 0; |
| 62 | |
| 63 | virtual void SetBitrateObserver( |
| 64 | VideoBitrateAllocationObserver* bitrate_observer) = 0; |
| 65 | virtual void ConfigureEncoder(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 66 | size_t max_data_payload_length) = 0; |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 67 | virtual void Stop() = 0; |
| 68 | |
| 69 | protected: |
| 70 | ~VideoStreamEncoderInterface() = default; |
| 71 | }; |
| 72 | // VideoStreamEncoder represent a video encoder that accepts raw video frames as |
| 73 | // input and produces an encoded bit stream. |
| 74 | // Usage: |
| 75 | // Instantiate. |
| 76 | // Call SetSink. |
| 77 | // Call SetSource. |
| 78 | // Call ConfigureEncoder with the codec settings. |
| 79 | // Call Stop() when done. |
| 80 | class VideoStreamEncoder : public VideoStreamEncoderInterface, |
| 81 | private EncodedImageCallback, |
| 82 | // Protected only to provide access to tests. |
| 83 | protected AdaptationObserverInterface { |
| 84 | public: |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 85 | // Number of resolution and framerate reductions (-1: disabled). |
| 86 | struct AdaptCounts { |
| 87 | int resolution = 0; |
| 88 | int fps = 0; |
| 89 | }; |
| 90 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 91 | VideoStreamEncoder(uint32_t number_of_cores, |
| 92 | SendStatisticsProxy* stats_proxy, |
| 93 | const VideoSendStream::Config::EncoderSettings& settings, |
| 94 | rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 95 | std::unique_ptr<OveruseFrameDetector> overuse_detector); |
| 96 | ~VideoStreamEncoder(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 98 | // Sets the source that will provide I420 video frames. |
| 99 | // |degradation_preference| control whether or not resolution or frame rate |
| 100 | // may be reduced. |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 101 | void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, |
| 102 | const VideoSendStream::DegradationPreference& |
| 103 | degradation_preference) override; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 104 | |
| 105 | // Sets the |sink| that gets the encoded frames. |rotation_applied| means |
| 106 | // that the source must support rotation. Only set |rotation_applied| if the |
| 107 | // remote side does not support the rotation extension. |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 108 | void SetSink(EncoderSink* sink, bool rotation_applied) override; |
mflodman@webrtc.org | 02270cd | 2015-02-06 13:10:19 +0000 | [diff] [blame] | 109 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 110 | // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 111 | void SetStartBitrate(int start_bitrate_bps) override; |
mflodman@webrtc.org | 9ec883e | 2012-03-05 17:12:41 +0000 | [diff] [blame] | 112 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 113 | void SetBitrateObserver( |
| 114 | VideoBitrateAllocationObserver* bitrate_observer) override; |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 115 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 116 | void ConfigureEncoder(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 117 | size_t max_data_payload_length) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 119 | // Permanently stop encoding. After this method has returned, it is |
| 120 | // guaranteed that no encoded frames will be delivered to the sink. |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 121 | void Stop() override; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 122 | |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 123 | void SendKeyFrame() override; |
mflodman@webrtc.org | d6ec386 | 2012-10-25 11:30:29 +0000 | [diff] [blame] | 124 | |
mflodman | 86aabb2 | 2016-03-11 15:44:32 +0100 | [diff] [blame] | 125 | void OnBitrateUpdated(uint32_t bitrate_bps, |
stefan@webrtc.org | edeea91 | 2014-12-08 19:46:23 +0000 | [diff] [blame] | 126 | uint8_t fraction_lost, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 127 | int64_t round_trip_time_ms) override; |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 128 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 129 | protected: |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 130 | // Used for testing. For example the |ScalingObserverInterface| methods must |
| 131 | // be called on |encoder_queue_|. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 132 | rtc::TaskQueue* encoder_queue() { return &encoder_queue_; } |
| 133 | |
Niels Möller | d692ef9 | 2017-10-04 15:28:55 +0200 | [diff] [blame] | 134 | // AdaptationObserverInterface implementation. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 135 | // These methods are protected for easier testing. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 136 | void AdaptUp(AdaptReason reason) override; |
| 137 | void AdaptDown(AdaptReason reason) override; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 138 | |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 139 | private: |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 140 | class VideoSourceProxy; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 141 | |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 142 | class VideoFrameInfo { |
| 143 | public: |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 144 | VideoFrameInfo(int width, |
| 145 | int height, |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 146 | bool is_texture) |
| 147 | : width(width), |
| 148 | height(height), |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 149 | is_texture(is_texture) {} |
| 150 | int width; |
| 151 | int height; |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 152 | bool is_texture; |
kthelgason | 93f16d7 | 2017-01-16 06:15:23 -0800 | [diff] [blame] | 153 | int pixel_count() const { return width * height; } |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Per | a48ddb7 | 2016-09-29 11:48:50 +0200 | [diff] [blame] | 156 | void ConfigureEncoderOnTaskQueue(VideoEncoderConfig config, |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 157 | size_t max_data_payload_length); |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 158 | void ReconfigureEncoder() RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 159 | |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 160 | void ConfigureQualityScaler(); |
| 161 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 162 | // Implements VideoSinkInterface. |
| 163 | void OnFrame(const VideoFrame& video_frame) override; |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 164 | void OnDiscardedFrame() override; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 165 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 166 | void MaybeEncodeVideoFrame(const VideoFrame& frame, |
| 167 | int64_t time_when_posted_in_ms); |
| 168 | |
perkj | d52063f | 2016-09-07 06:32:18 -0700 | [diff] [blame] | 169 | void EncodeVideoFrame(const VideoFrame& frame, |
| 170 | int64_t time_when_posted_in_ms); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 171 | // Indicates wether frame should be dropped because the pixel count is too |
| 172 | // large for the current bitrate configuration. |
| 173 | bool DropDueToSize(uint32_t pixel_count) const RTC_RUN_ON(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 174 | |
| 175 | // Implements EncodedImageCallback. |
| 176 | EncodedImageCallback::Result OnEncodedImage( |
| 177 | const EncodedImage& encoded_image, |
| 178 | const CodecSpecificInfo* codec_specific_info, |
| 179 | const RTPFragmentationHeader* fragmentation) override; |
| 180 | |
Ilya Nikolaevskiy | d79314f | 2017-10-23 10:45:37 +0200 | [diff] [blame] | 181 | void OnDroppedFrame(EncodedImageCallback::DropReason reason) override; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 182 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 183 | bool EncoderPaused() const; |
| 184 | void TraceFrameDropStart(); |
| 185 | void TraceFrameDropEnd(); |
| 186 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 187 | // Class holding adaptation information. |
| 188 | class AdaptCounter final { |
| 189 | public: |
| 190 | AdaptCounter(); |
| 191 | ~AdaptCounter(); |
| 192 | |
| 193 | // Get number of adaptation downscales for |reason|. |
| 194 | AdaptCounts Counts(int reason) const; |
| 195 | |
| 196 | std::string ToString() const; |
| 197 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 198 | void IncrementFramerate(int reason); |
| 199 | void IncrementResolution(int reason); |
| 200 | void DecrementFramerate(int reason); |
| 201 | void DecrementResolution(int reason); |
| 202 | void DecrementFramerate(int reason, int cur_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 203 | |
| 204 | // Gets the total number of downgrades (for all adapt reasons). |
| 205 | int FramerateCount() const; |
| 206 | int ResolutionCount() const; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 207 | |
| 208 | // Gets the total number of downgrades for |reason|. |
| 209 | int FramerateCount(int reason) const; |
| 210 | int ResolutionCount(int reason) const; |
| 211 | int TotalCount(int reason) const; |
| 212 | |
| 213 | private: |
| 214 | std::string ToString(const std::vector<int>& counters) const; |
| 215 | int Count(const std::vector<int>& counters) const; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 216 | void MoveCount(std::vector<int>* counters, int from_reason); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 217 | |
| 218 | // Degradation counters holding number of framerate/resolution reductions |
| 219 | // per adapt reason. |
| 220 | std::vector<int> fps_counters_; |
| 221 | std::vector<int> resolution_counters_; |
| 222 | }; |
| 223 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 224 | AdaptCounter& GetAdaptCounter() RTC_RUN_ON(&encoder_queue_); |
| 225 | const AdaptCounter& GetConstAdaptCounter() RTC_RUN_ON(&encoder_queue_); |
| 226 | void UpdateAdaptationStats(AdaptReason reason) RTC_RUN_ON(&encoder_queue_); |
| 227 | AdaptCounts GetActiveCounts(AdaptReason reason) RTC_RUN_ON(&encoder_queue_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 228 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 229 | rtc::Event shutdown_event_; |
stefan@webrtc.org | bfacda6 | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 230 | |
pbos@webrtc.org | b238d12 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 231 | const uint32_t number_of_cores_; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 232 | // Counts how many frames we've dropped in the initial rampup phase. |
| 233 | int initial_rampup_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 234 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 235 | const bool quality_scaling_experiment_enabled_; |
| 236 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 237 | const std::unique_ptr<VideoSourceProxy> source_proxy_; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 238 | EncoderSink* sink_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 239 | const VideoSendStream::Config::EncoderSettings settings_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 241 | vcm::VideoSender video_sender_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 242 | const std::unique_ptr<OveruseFrameDetector> overuse_detector_ |
| 243 | RTC_PT_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 244 | std::unique_ptr<QualityScaler> quality_scaler_ |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 245 | RTC_GUARDED_BY(&encoder_queue_) |
| 246 | RTC_PT_GUARDED_BY(&encoder_queue_); |
pwestin@webrtc.org | 49888ce | 2012-04-27 05:25:53 +0000 | [diff] [blame] | 247 | |
Peter Boström | 7083e11 | 2015-09-22 16:28:51 +0200 | [diff] [blame] | 248 | SendStatisticsProxy* const stats_proxy_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 249 | rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 250 | // |thread_checker_| checks that public methods that are related to lifetime |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 251 | // of VideoStreamEncoder are called on the same thread. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 252 | rtc::ThreadChecker thread_checker_; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 253 | |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 254 | VideoEncoderConfig encoder_config_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 255 | std::unique_ptr<VideoEncoder> encoder_ RTC_GUARDED_BY(&encoder_queue_) |
| 256 | RTC_PT_GUARDED_BY(&encoder_queue_); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 257 | std::unique_ptr<VideoBitrateAllocator> rate_allocator_ |
Niels Möller | a8b1508 | 2018-02-07 13:42:09 +0100 | [diff] [blame] | 258 | RTC_GUARDED_BY(&encoder_queue_) |
| 259 | RTC_PT_GUARDED_BY(&encoder_queue_); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 260 | // The maximum frame rate of the current codec configuration, as determined |
| 261 | // at the last ReconfigureEncoder() call. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 262 | int max_framerate_ RTC_GUARDED_BY(&encoder_queue_); |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 263 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 264 | // Set when ConfigureEncoder has been called in order to lazy reconfigure the |
| 265 | // encoder on the next frame. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 266 | bool pending_encoder_reconfiguration_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 267 | // Set when configuration must create a new encoder object, e.g., |
| 268 | // because of a codec change. |
| 269 | bool pending_encoder_creation_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 270 | rtc::Optional<VideoFrameInfo> last_frame_info_ |
| 271 | RTC_GUARDED_BY(&encoder_queue_); |
| 272 | int crop_width_ RTC_GUARDED_BY(&encoder_queue_); |
| 273 | int crop_height_ RTC_GUARDED_BY(&encoder_queue_); |
| 274 | uint32_t encoder_start_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_); |
| 275 | size_t max_data_payload_length_ RTC_GUARDED_BY(&encoder_queue_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 276 | uint32_t last_observed_bitrate_bps_ RTC_GUARDED_BY(&encoder_queue_); |
| 277 | bool encoder_paused_and_dropped_frame_ RTC_GUARDED_BY(&encoder_queue_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 278 | Clock* const clock_; |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 279 | // Counters used for deciding if the video resolution or framerate is |
| 280 | // currently restricted, and if so, why, on a per degradation preference |
| 281 | // basis. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 282 | // TODO(sprang): Replace this with a state holding a relative overuse measure |
| 283 | // instead, that can be translated into suitable down-scale or fps limit. |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 284 | std::map<const VideoSendStream::DegradationPreference, AdaptCounter> |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 285 | adapt_counters_ RTC_GUARDED_BY(&encoder_queue_); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 286 | // Set depending on degradation preferences. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 287 | VideoSendStream::DegradationPreference degradation_preference_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 288 | RTC_GUARDED_BY(&encoder_queue_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 289 | |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 290 | struct AdaptationRequest { |
| 291 | // The pixel count produced by the source at the time of the adaptation. |
| 292 | int input_pixel_count_; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 293 | // Framerate received from the source at the time of the adaptation. |
| 294 | int framerate_fps_; |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 295 | // Indicates if request was to adapt up or down. |
| 296 | enum class Mode { kAdaptUp, kAdaptDown } mode_; |
| 297 | }; |
| 298 | // Stores a snapshot of the last adaptation request triggered by an AdaptUp |
| 299 | // or AdaptDown signal. |
| 300 | rtc::Optional<AdaptationRequest> last_adaptation_request_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 301 | RTC_GUARDED_BY(&encoder_queue_); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 302 | |
| 303 | rtc::RaceChecker incoming_frame_race_checker_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 304 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
Yuwei Huang | d9f99c1 | 2017-10-24 15:40:52 -0700 | [diff] [blame] | 305 | std::atomic<int> posted_frames_waiting_for_encode_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 306 | // Used to make sure incoming time stamp is increasing for every frame. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 307 | int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 308 | // Delta used for translating between NTP and internal timestamps. |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 309 | const int64_t delta_ntp_internal_ms_ |
| 310 | RTC_GUARDED_BY(incoming_frame_race_checker_); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 311 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 312 | int64_t last_frame_log_ms_ RTC_GUARDED_BY(incoming_frame_race_checker_); |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 313 | int captured_frame_count_ RTC_GUARDED_BY(&encoder_queue_); |
| 314 | int dropped_frame_count_ RTC_GUARDED_BY(&encoder_queue_); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 315 | rtc::Optional<VideoFrame> pending_frame_ RTC_GUARDED_BY(&encoder_queue_); |
| 316 | int64_t pending_frame_post_time_us_ RTC_GUARDED_BY(&encoder_queue_); |
asapersson | 6ffb67d | 2016-09-12 00:10:45 -0700 | [diff] [blame] | 317 | |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 318 | VideoBitrateAllocationObserver* bitrate_observer_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 319 | RTC_GUARDED_BY(&encoder_queue_); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 320 | rtc::Optional<int64_t> last_parameters_update_ms_ |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 321 | RTC_GUARDED_BY(&encoder_queue_); |
sprang | 1a646ee | 2016-12-01 06:34:11 -0800 | [diff] [blame] | 322 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 323 | // All public methods are proxied to |encoder_queue_|. It must must be |
| 324 | // destroyed first to make sure no tasks are run that use other members. |
| 325 | rtc::TaskQueue encoder_queue_; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 326 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 327 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 328 | }; |
mflodman@webrtc.org | 84d1783 | 2011-12-01 17:02:23 +0000 | [diff] [blame] | 329 | |
| 330 | } // namespace webrtc |
| 331 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 332 | #endif // VIDEO_VIDEO_STREAM_ENCODER_H_ |