blob: 760831c96a4f7586f40a1b42edbd166c5b29a1ea [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
pwestin@webrtc.org52fd98d2012-02-13 09:03:53 +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 MODULES_VIDEO_CODING_GENERIC_ENCODER_H_
12#define MODULES_VIDEO_CODING_GENERIC_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
philipel9d3ab612015-12-21 04:12:39 -080014#include <stdio.h>
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020015#include <list>
philipel9d3ab612015-12-21 04:12:39 -080016#include <vector>
17
Erik Språngd96b2752018-12-13 11:23:27 +010018#include "api/units/data_rate.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/include/video_codec_interface.h"
20#include "modules/video_coding/include/video_coding_defines.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/race_checker.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000023
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000024namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:59 +000025
philipel9d3ab612015-12-21 04:12:39 -080026class VCMEncodedFrameCallback : public EncodedImageCallback {
27 public:
Niels Möller6bb5ab92019-01-11 11:11:10 +010028 explicit VCMEncodedFrameCallback(EncodedImageCallback* post_encode_callback);
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020029 ~VCMEncodedFrameCallback() override;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
sprang3911c262016-04-15 01:24:14 -070031 // Implements EncodedImageCallback.
Sergey Ulanov525df3f2016-08-02 17:46:41 -070032 EncodedImageCallback::Result OnEncodedImage(
33 const EncodedImage& encoded_image,
34 const CodecSpecificInfo* codec_specific_info,
35 const RTPFragmentationHeader* fragmentation) override;
ilnik04f4d122017-06-19 07:18:55 -070036
sprang3911c262016-04-15 01:24:14 -070037 void SetInternalSource(bool internal_source) {
38 internal_source_ = internal_source;
39 }
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000040
ilnik04f4d122017-06-19 07:18:55 -070041 // Timing frames configuration methods. These 4 should be called before
42 // |OnEncodedImage| at least once.
43 void OnTargetBitrateChanged(size_t bitrate_bytes_per_sec,
44 size_t simulcast_svc_idx);
45
46 void OnFrameRateChanged(size_t framerate);
47
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010048 void OnEncodeStarted(uint32_t rtp_timestamps,
49 int64_t capture_time_ms,
50 size_t simulcast_svc_idx);
ilnik04f4d122017-06-19 07:18:55 -070051
52 void SetTimingFramesThresholds(
53 const VideoCodec::TimingFrameTriggerThresholds& thresholds) {
54 rtc::CritScope crit(&timing_params_lock_);
55 timing_frames_thresholds_ = thresholds;
56 }
57
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020058 // Clears all data stored by OnEncodeStarted().
59 void Reset() {
60 rtc::CritScope crit(&timing_params_lock_);
61 timing_frames_info_.clear();
62 last_timing_frame_time_ms_ = -1;
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +010063 reordered_frames_logged_messages_ = 0;
64 stalled_encoder_logged_messages_ = 0;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020065 }
66
philipel9d3ab612015-12-21 04:12:39 -080067 private:
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +020068 // For non-internal-source encoders, returns encode started time and fixes
69 // capture timestamp for the frame, if corrupted by the encoder.
Danil Chapovalov0040b662018-06-18 10:48:16 +020070 absl::optional<int64_t> ExtractEncodeStartTime(size_t simulcast_svc_idx,
71 EncodedImage* encoded_image)
Ilya Nikolaevskiy764aeb72018-04-03 10:01:52 +020072 RTC_EXCLUSIVE_LOCKS_REQUIRED(timing_params_lock_);
73
74 void FillTimingInfo(size_t simulcast_svc_idx, EncodedImage* encoded_image);
75
ilnik04f4d122017-06-19 07:18:55 -070076 rtc::CriticalSection timing_params_lock_;
sprang3911c262016-04-15 01:24:14 -070077 bool internal_source_;
perkj376b1922016-05-02 11:35:24 -070078 EncodedImageCallback* const post_encode_callback_;
ilnik04f4d122017-06-19 07:18:55 -070079
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020080 struct EncodeStartTimeRecord {
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010081 EncodeStartTimeRecord(uint32_t timestamp,
82 int64_t capture_time,
83 int64_t encode_start_time)
84 : rtp_timestamp(timestamp),
85 capture_time_ms(capture_time),
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020086 encode_start_time_ms(encode_start_time) {}
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +010087 uint32_t rtp_timestamp;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020088 int64_t capture_time_ms;
89 int64_t encode_start_time_ms;
90 };
ilnik04f4d122017-06-19 07:18:55 -070091 struct TimingFramesLayerInfo {
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020092 TimingFramesLayerInfo();
93 ~TimingFramesLayerInfo();
ilnik04f4d122017-06-19 07:18:55 -070094 size_t target_bitrate_bytes_per_sec = 0;
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020095 std::list<EncodeStartTimeRecord> encode_start_list;
ilnik04f4d122017-06-19 07:18:55 -070096 };
97 // Separate instance for each simulcast stream or spatial layer.
98 std::vector<TimingFramesLayerInfo> timing_frames_info_
danilchap56359be2017-09-07 07:53:45 -070099 RTC_GUARDED_BY(timing_params_lock_);
100 size_t framerate_ RTC_GUARDED_BY(timing_params_lock_);
101 int64_t last_timing_frame_time_ms_ RTC_GUARDED_BY(timing_params_lock_);
ilnik04f4d122017-06-19 07:18:55 -0700102 VideoCodec::TimingFrameTriggerThresholds timing_frames_thresholds_
danilchap56359be2017-09-07 07:53:45 -0700103 RTC_GUARDED_BY(timing_params_lock_);
Ilya Nikolaevskiy76f2a852017-11-16 14:33:53 +0100104 size_t incorrect_capture_time_logged_messages_
105 RTC_GUARDED_BY(timing_params_lock_);
Ilya Nikolaevskiyb9fb78f2017-11-14 14:13:47 +0100106 size_t reordered_frames_logged_messages_ RTC_GUARDED_BY(timing_params_lock_);
107 size_t stalled_encoder_logged_messages_ RTC_GUARDED_BY(timing_params_lock_);
ilnik6d5b4d62017-08-30 03:32:14 -0700108
109 // Experiment groups parsed from field trials for realtime video ([0]) and
110 // screenshare ([1]). 0 means no group specified. Positive values are
111 // experiment group numbers incremented by 1.
112 uint8_t experiment_groups_[2];
sprang3911c262016-04-15 01:24:14 -0700113};
sprang@webrtc.org40709352013-11-26 11:41:59 +0000114
philipel9d3ab612015-12-21 04:12:39 -0800115class VCMGenericEncoder {
116 friend class VCMCodecDataBase;
Peter Boström69ccb332015-10-29 16:30:23 +0100117
philipel9d3ab612015-12-21 04:12:39 -0800118 public:
119 VCMGenericEncoder(VideoEncoder* encoder,
philipel9d3ab612015-12-21 04:12:39 -0800120 VCMEncodedFrameCallback* encoded_frame_callback,
sprang3911c262016-04-15 01:24:14 -0700121 bool internal_source);
philipel9d3ab612015-12-21 04:12:39 -0800122 ~VCMGenericEncoder();
philipel9d3ab612015-12-21 04:12:39 -0800123 int32_t Release();
philipel9d3ab612015-12-21 04:12:39 -0800124 int32_t InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -0700125 int32_t number_of_cores,
126 size_t max_payload_size);
127 int32_t Encode(const VideoFrame& frame,
128 const CodecSpecificInfo* codec_specific,
129 const std::vector<FrameType>& frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
Niels Möller6bb5ab92019-01-11 11:11:10 +0100131 void SetEncoderParameters(const VideoBitrateAllocation& target_bitrate,
132 uint32_t input_frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
philipel9d3ab612015-12-21 04:12:39 -0800134 int32_t RequestFrame(const std::vector<FrameType>& frame_types);
philipel9d3ab612015-12-21 04:12:39 -0800135 bool InternalSource() const;
Erik Språngd3438aa2018-11-08 16:56:43 +0100136 VideoEncoder::EncoderInfo GetEncoderInfo() const;
philipel9d3ab612015-12-21 04:12:39 -0800137
138 private:
Peter Boström02bafc62016-07-01 12:45:15 +0200139 rtc::RaceChecker race_checker_;
140
danilchap56359be2017-09-07 07:53:45 -0700141 VideoEncoder* const encoder_ RTC_GUARDED_BY(race_checker_);
philipel9d3ab612015-12-21 04:12:39 -0800142 VCMEncodedFrameCallback* const vcm_encoded_frame_callback_;
143 const bool internal_source_;
pbos5ad935c2016-01-25 03:52:44 -0800144 rtc::CriticalSection params_lock_;
Niels Möller6bb5ab92019-01-11 11:11:10 +0100145 VideoBitrateAllocation bitrate_allocation_ RTC_GUARDED_BY(params_lock_);
146 uint32_t input_frame_rate_ RTC_GUARDED_BY(params_lock_);
Ilya Nikolaevskiye0da9ea2017-11-08 14:39:02 +0100147 size_t streams_or_svc_num_ RTC_GUARDED_BY(race_checker_);
148 VideoCodecType codec_type_ RTC_GUARDED_BY(race_checker_);
sprang3911c262016-04-15 01:24:14 -0700149};
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000151} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200153#endif // MODULES_VIDEO_CODING_GENERIC_ENCODER_H_