blob: 421b73396150b87cfa196aed3356d79736abb322 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_VIDEO_STREAM_ENCODER_H_
12#define VIDEO_VIDEO_STREAM_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Yuwei Huangd9f99c12017-10-24 15:40:52 -070014#include <atomic>
sprangc5d62e22017-04-02 23:53:04 -070015#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080016#include <memory>
perkj376b1922016-05-02 11:35:24 -070017#include <string>
mflodman@webrtc.org02270cd2015-02-06 13:10:19 +000018#include <vector>
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000019
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/video_rotation.h"
21#include "api/video_codecs/video_encoder.h"
22#include "call/call.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020023#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#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 Bonadei71207422017-09-15 13:58:09 +020033#include "typedefs.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "video/overuse_frame_detector.h"
35#include "call/video_send_stream.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000036
37namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000038
pbos@webrtc.org273a4142014-12-01 15:23:21 +000039class SendStatisticsProxy;
sprang1a646ee2016-12-01 06:34:11 -080040class VideoBitrateAllocationObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000041
mflodmancc3d4422017-08-03 08:27:51 -070042// VideoStreamEncoder represent a video encoder that accepts raw video frames as
43// input and produces an encoded bit stream.
perkjbc75d972016-05-02 06:31:25 -070044// Usage:
perkj26091b12016-09-01 01:17:40 -070045// Instantiate.
perkja49cbd32016-09-16 07:53:41 -070046// Call SetSink.
47// Call SetSource.
perkj26091b12016-09-01 01:17:40 -070048// Call ConfigureEncoder with the codec settings.
perkj26091b12016-09-01 01:17:40 -070049// Call Stop() when done.
mflodmancc3d4422017-08-03 08:27:51 -070050class VideoStreamEncoder : public rtc::VideoSinkInterface<VideoFrame>,
51 public EncodedImageCallback,
mflodmancc3d4422017-08-03 08:27:51 -070052 public AdaptationObserverInterface {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000053 public:
Per512ecb32016-09-23 15:52:06 +020054 // 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
asapersson09f05612017-05-15 23:40:18 -070063 // Number of resolution and framerate reductions (-1: disabled).
64 struct AdaptCounts {
65 int resolution = 0;
66 int fps = 0;
67 };
68
kthelgason876222f2016-11-29 01:44:11 -080069 // Downscale resolution at most 2 times for CPU reasons.
sprangc5d62e22017-04-02 23:53:04 -070070 static const int kMaxCpuResolutionDowngrades = 2;
71 // Downscale framerate at most 4 times.
72 static const int kMaxCpuFramerateDowngrades = 4;
perkj803d97f2016-11-01 11:45:46 -070073
mflodmancc3d4422017-08-03 08:27:51 -070074 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.com470e71d2011-07-07 08:21:25 +000081
perkj803d97f2016-11-01 11:45:46 -070082 // 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.org02270cd2015-02-06 13:10:19 +000093
perkj26091b12016-09-01 01:17:40 -070094 // TODO(perkj): Can we remove VideoCodec.startBitrate ?
95 void SetStartBitrate(int start_bitrate_bps);
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000096
sprang1a646ee2016-12-01 06:34:11 -080097 void SetBitrateObserver(VideoBitrateAllocationObserver* bitrate_observer);
98
Per512ecb32016-09-23 15:52:06 +020099 void ConfigureEncoder(VideoEncoderConfig config,
asapersson5f7226f2016-11-25 04:37:00 -0800100 size_t max_data_payload_length,
101 bool nack_enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
perkj26091b12016-09-01 01:17:40 -0700103 // 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öm233bfd22016-01-18 20:23:40 +0100107 void SendKeyFrame();
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000108
Peter Boström0013dcc2016-02-19 20:42:19 +0100109 // virtual to test EncoderStateFeedback with mocks.
perkj600246e2016-05-04 11:26:51 -0700110 virtual void OnReceivedIntraFrameRequest(size_t stream_index);
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000111
mflodman86aabb22016-03-11 15:44:32 +0100112 void OnBitrateUpdated(uint32_t bitrate_bps,
stefan@webrtc.orgedeea912014-12-08 19:46:23 +0000113 uint8_t fraction_lost,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000114 int64_t round_trip_time_ms);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000115
perkj803d97f2016-11-01 11:45:46 -0700116 protected:
kthelgason876222f2016-11-29 01:44:11 -0800117 // Used for testing. For example the |ScalingObserverInterface| methods must
118 // be called on |encoder_queue_|.
perkj803d97f2016-11-01 11:45:46 -0700119 rtc::TaskQueue* encoder_queue() { return &encoder_queue_; }
120
Niels Möllerd692ef92017-10-04 15:28:55 +0200121 // AdaptationObserverInterface implementation.
perkj803d97f2016-11-01 11:45:46 -0700122 // These methods are protected for easier testing.
sprangb1ca0732017-02-01 08:38:12 -0800123 void AdaptUp(AdaptReason reason) override;
124 void AdaptDown(AdaptReason reason) override;
sprangfda496a2017-06-15 04:21:07 -0700125 static CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time);
perkj803d97f2016-11-01 11:45:46 -0700126
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000127 private:
Pera48ddb72016-09-29 11:48:50 +0200128 class ConfigureEncoderTask;
perkj26091b12016-09-01 01:17:40 -0700129 class EncodeTask;
perkja49cbd32016-09-16 07:53:41 -0700130 class VideoSourceProxy;
perkj26091b12016-09-01 01:17:40 -0700131
kthelgason93f16d72017-01-16 06:15:23 -0800132 class VideoFrameInfo {
133 public:
perkjfa10b552016-10-02 23:45:26 -0700134 VideoFrameInfo(int width,
135 int height,
perkjfa10b552016-10-02 23:45:26 -0700136 bool is_texture)
137 : width(width),
138 height(height),
perkjfa10b552016-10-02 23:45:26 -0700139 is_texture(is_texture) {}
140 int width;
141 int height;
perkjfa10b552016-10-02 23:45:26 -0700142 bool is_texture;
kthelgason93f16d72017-01-16 06:15:23 -0800143 int pixel_count() const { return width * height; }
perkjfa10b552016-10-02 23:45:26 -0700144 };
145
Pera48ddb72016-09-29 11:48:50 +0200146 void ConfigureEncoderOnTaskQueue(VideoEncoderConfig config,
asapersson5f7226f2016-11-25 04:37:00 -0800147 size_t max_data_payload_length,
148 bool nack_enabled);
perkjfa10b552016-10-02 23:45:26 -0700149 void ReconfigureEncoder();
perkj26091b12016-09-01 01:17:40 -0700150
kthelgason2bc68642017-02-07 07:02:22 -0800151 void ConfigureQualityScaler();
152
perkja49cbd32016-09-16 07:53:41 -0700153 // Implements VideoSinkInterface.
154 void OnFrame(const VideoFrame& video_frame) override;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +0200155 void OnDiscardedFrame() override;
perkja49cbd32016-09-16 07:53:41 -0700156
perkjd52063f2016-09-07 06:32:18 -0700157 void EncodeVideoFrame(const VideoFrame& frame,
158 int64_t time_when_posted_in_ms);
perkj26091b12016-09-01 01:17:40 -0700159
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 Nikolaevskiyd79314f2017-10-23 10:45:37 +0200166 void OnDroppedFrame(EncodedImageCallback::DropReason reason) override;
kthelgason876222f2016-11-29 01:44:11 -0800167
perkj26091b12016-09-01 01:17:40 -0700168 bool EncoderPaused() const;
169 void TraceFrameDropStart();
170 void TraceFrameDropEnd();
171
asapersson09f05612017-05-15 23:40:18 -0700172 // 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
asaperssonf7e294d2017-06-13 23:25:22 -0700183 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);
asapersson09f05612017-05-15 23:40:18 -0700188
189 // Gets the total number of downgrades (for all adapt reasons).
190 int FramerateCount() const;
191 int ResolutionCount() const;
asapersson09f05612017-05-15 23:40:18 -0700192
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;
asaperssonf7e294d2017-06-13 23:25:22 -0700201 void MoveCount(std::vector<int>* counters, int from_reason);
asapersson09f05612017-05-15 23:40:18 -0700202
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
danilchapa37de392017-09-09 04:17:22 -0700209 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_);
sprangc5d62e22017-04-02 23:53:04 -0700213
perkj26091b12016-09-01 01:17:40 -0700214 rtc::Event shutdown_event_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000215
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000216 const uint32_t number_of_cores_;
kthelgason2bc68642017-02-07 07:02:22 -0800217 // Counts how many frames we've dropped in the initial rampup phase.
218 int initial_rampup_;
perkja49cbd32016-09-16 07:53:41 -0700219
220 const std::unique_ptr<VideoSourceProxy> source_proxy_;
Per512ecb32016-09-23 15:52:06 +0200221 EncoderSink* sink_;
perkj26091b12016-09-01 01:17:40 -0700222 const VideoSendStream::Config::EncoderSettings settings_;
Pera48ddb72016-09-29 11:48:50 +0200223 const VideoCodecType codec_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
danilchapa37de392017-09-09 04:17:22 -0700225 vcm::VideoSender video_sender_ RTC_ACCESS_ON(&encoder_queue_);
sprangfda496a2017-06-15 04:21:07 -0700226 std::unique_ptr<OveruseFrameDetector> overuse_detector_
danilchapa37de392017-09-09 04:17:22 -0700227 RTC_ACCESS_ON(&encoder_queue_);
228 std::unique_ptr<QualityScaler> quality_scaler_ RTC_ACCESS_ON(&encoder_queue_);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000229
Peter Boström7083e112015-09-22 16:28:51 +0200230 SendStatisticsProxy* const stats_proxy_;
perkj26091b12016-09-01 01:17:40 -0700231 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_;
perkja49cbd32016-09-16 07:53:41 -0700232 // |thread_checker_| checks that public methods that are related to lifetime
mflodmancc3d4422017-08-03 08:27:51 -0700233 // of VideoStreamEncoder are called on the same thread.
perkja49cbd32016-09-16 07:53:41 -0700234 rtc::ThreadChecker thread_checker_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000235
danilchapa37de392017-09-09 04:17:22 -0700236 VideoEncoderConfig encoder_config_ RTC_ACCESS_ON(&encoder_queue_);
Erik Språng08127a92016-11-16 16:41:30 +0100237 std::unique_ptr<VideoBitrateAllocator> rate_allocator_
danilchapa37de392017-09-09 04:17:22 -0700238 RTC_ACCESS_ON(&encoder_queue_);
sprangfda496a2017-06-15 04:21:07 -0700239 // The maximum frame rate of the current codec configuration, as determined
240 // at the last ReconfigureEncoder() call.
danilchapa37de392017-09-09 04:17:22 -0700241 int max_framerate_ RTC_ACCESS_ON(&encoder_queue_);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000242
perkjfa10b552016-10-02 23:45:26 -0700243 // Set when ConfigureEncoder has been called in order to lazy reconfigure the
244 // encoder on the next frame.
danilchapa37de392017-09-09 04:17:22 -0700245 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_);
perkj26091b12016-09-01 01:17:40 -0700254 Clock* const clock_;
asapersson09f05612017-05-15 23:40:18 -0700255 // 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.
sprangc5d62e22017-04-02 23:53:04 -0700258 // 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.
asapersson09f05612017-05-15 23:40:18 -0700260 std::map<const VideoSendStream::DegradationPreference, AdaptCounter>
danilchapa37de392017-09-09 04:17:22 -0700261 adapt_counters_ RTC_ACCESS_ON(&encoder_queue_);
asapersson09f05612017-05-15 23:40:18 -0700262 // Set depending on degradation preferences.
sprangb1ca0732017-02-01 08:38:12 -0800263 VideoSendStream::DegradationPreference degradation_preference_
danilchapa37de392017-09-09 04:17:22 -0700264 RTC_ACCESS_ON(&encoder_queue_);
perkj803d97f2016-11-01 11:45:46 -0700265
sprang84a37592017-02-10 07:04:27 -0800266 struct AdaptationRequest {
267 // The pixel count produced by the source at the time of the adaptation.
268 int input_pixel_count_;
sprangc5d62e22017-04-02 23:53:04 -0700269 // Framerate received from the source at the time of the adaptation.
270 int framerate_fps_;
sprang84a37592017-02-10 07:04:27 -0800271 // 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_
danilchapa37de392017-09-09 04:17:22 -0700277 RTC_ACCESS_ON(&encoder_queue_);
perkj803d97f2016-11-01 11:45:46 -0700278
279 rtc::RaceChecker incoming_frame_race_checker_
danilchapa37de392017-09-09 04:17:22 -0700280 RTC_GUARDED_BY(incoming_frame_race_checker_);
Yuwei Huangd9f99c12017-10-24 15:40:52 -0700281 std::atomic<int> posted_frames_waiting_for_encode_;
perkj26091b12016-09-01 01:17:40 -0700282 // Used to make sure incoming time stamp is increasing for every frame.
danilchapa37de392017-09-09 04:17:22 -0700283 int64_t last_captured_timestamp_ RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700284 // Delta used for translating between NTP and internal timestamps.
danilchapa37de392017-09-09 04:17:22 -0700285 const int64_t delta_ntp_internal_ms_
286 RTC_GUARDED_BY(incoming_frame_race_checker_);
perkj26091b12016-09-01 01:17:40 -0700287
danilchapa37de392017-09-09 04:17:22 -0700288 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_);
asapersson6ffb67d2016-09-12 00:10:45 -0700291
danilchapa37de392017-09-09 04:17:22 -0700292 VideoBitrateAllocationObserver* bitrate_observer_
293 RTC_ACCESS_ON(&encoder_queue_);
294 rtc::Optional<int64_t> last_parameters_update_ms_
295 RTC_ACCESS_ON(&encoder_queue_);
sprang1a646ee2016-12-01 06:34:11 -0800296
perkj26091b12016-09-01 01:17:40 -0700297 // 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_;
perkja49cbd32016-09-16 07:53:41 -0700300
mflodmancc3d4422017-08-03 08:27:51 -0700301 RTC_DISALLOW_COPY_AND_ASSIGN(VideoStreamEncoder);
niklase@google.com470e71d2011-07-07 08:21:25 +0000302};
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000303
304} // namespace webrtc
305
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200306#endif // VIDEO_VIDEO_STREAM_ENCODER_H_