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