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