blob: 20e07c73784f0a0eb5a69377b7e1702f9e2d2825 [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
11#ifndef WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_
12#define WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_
13
philipel9d3ab612015-12-21 04:12:39 -080014#include <stdio.h>
ilnik04f4d122017-06-19 07:18:55 -070015#include <map>
philipel9d3ab612015-12-21 04:12:39 -080016#include <vector>
17
Henrik Kjellander2557b862015-11-18 22:00:21 +010018#include "webrtc/modules/video_coding/include/video_codec_interface.h"
19#include "webrtc/modules/video_coding/include/video_coding_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/criticalsection.h"
22#include "webrtc/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
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000026namespace media_optimization {
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000027class MediaOptimization;
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000028} // namespace media_optimization
niklase@google.com470e71d2011-07-07 08:21:25 +000029
Peter Boström69ccb332015-10-29 16:30:23 +010030struct EncoderParameters {
Erik Språng08127a92016-11-16 16:41:30 +010031 BitrateAllocation target_bitrate;
Peter Boström69ccb332015-10-29 16:30:23 +010032 uint8_t loss_rate;
33 int64_t rtt;
34 uint32_t input_frame_rate;
35};
36
philipel9d3ab612015-12-21 04:12:39 -080037class VCMEncodedFrameCallback : public EncodedImageCallback {
38 public:
perkj376b1922016-05-02 11:35:24 -070039 VCMEncodedFrameCallback(EncodedImageCallback* post_encode_callback,
40 media_optimization::MediaOptimization* media_opt);
sprang3911c262016-04-15 01:24:14 -070041 virtual ~VCMEncodedFrameCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000042
sprang3911c262016-04-15 01:24:14 -070043 // Implements EncodedImageCallback.
Sergey Ulanov525df3f2016-08-02 17:46:41 -070044 EncodedImageCallback::Result OnEncodedImage(
45 const EncodedImage& encoded_image,
46 const CodecSpecificInfo* codec_specific_info,
47 const RTPFragmentationHeader* fragmentation) override;
ilnik04f4d122017-06-19 07:18:55 -070048
sprang3911c262016-04-15 01:24:14 -070049 void SetInternalSource(bool internal_source) {
50 internal_source_ = internal_source;
51 }
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000052
ilnik04f4d122017-06-19 07:18:55 -070053 // Timing frames configuration methods. These 4 should be called before
54 // |OnEncodedImage| at least once.
55 void OnTargetBitrateChanged(size_t bitrate_bytes_per_sec,
56 size_t simulcast_svc_idx);
57
58 void OnFrameRateChanged(size_t framerate);
59
60 void OnEncodeStarted(int64_t capture_time_ms, size_t simulcast_svc_idx);
61
62 void SetTimingFramesThresholds(
63 const VideoCodec::TimingFrameTriggerThresholds& thresholds) {
64 rtc::CritScope crit(&timing_params_lock_);
65 timing_frames_thresholds_ = thresholds;
66 }
67
philipel9d3ab612015-12-21 04:12:39 -080068 private:
ilnik04f4d122017-06-19 07:18:55 -070069 rtc::CriticalSection timing_params_lock_;
sprang3911c262016-04-15 01:24:14 -070070 bool internal_source_;
perkj376b1922016-05-02 11:35:24 -070071 EncodedImageCallback* const post_encode_callback_;
72 media_optimization::MediaOptimization* const media_opt_;
ilnik04f4d122017-06-19 07:18:55 -070073
74 struct TimingFramesLayerInfo {
75 size_t target_bitrate_bytes_per_sec = 0;
76 std::map<int64_t, int64_t> encode_start_time_ms;
77 };
78 // Separate instance for each simulcast stream or spatial layer.
79 std::vector<TimingFramesLayerInfo> timing_frames_info_
80 GUARDED_BY(timing_params_lock_);
81 size_t framerate_ GUARDED_BY(timing_params_lock_);
82 int64_t last_timing_frame_time_ms_ GUARDED_BY(timing_params_lock_);
83 VideoCodec::TimingFrameTriggerThresholds timing_frames_thresholds_
84 GUARDED_BY(timing_params_lock_);
ilnik6d5b4d62017-08-30 03:32:14 -070085
86 // Experiment groups parsed from field trials for realtime video ([0]) and
87 // screenshare ([1]). 0 means no group specified. Positive values are
88 // experiment group numbers incremented by 1.
89 uint8_t experiment_groups_[2];
sprang3911c262016-04-15 01:24:14 -070090};
sprang@webrtc.org40709352013-11-26 11:41:59 +000091
philipel9d3ab612015-12-21 04:12:39 -080092class VCMGenericEncoder {
93 friend class VCMCodecDataBase;
Peter Boström69ccb332015-10-29 16:30:23 +010094
philipel9d3ab612015-12-21 04:12:39 -080095 public:
96 VCMGenericEncoder(VideoEncoder* encoder,
philipel9d3ab612015-12-21 04:12:39 -080097 VCMEncodedFrameCallback* encoded_frame_callback,
sprang3911c262016-04-15 01:24:14 -070098 bool internal_source);
philipel9d3ab612015-12-21 04:12:39 -080099 ~VCMGenericEncoder();
philipel9d3ab612015-12-21 04:12:39 -0800100 int32_t Release();
philipel9d3ab612015-12-21 04:12:39 -0800101 int32_t InitEncode(const VideoCodec* settings,
sprang3911c262016-04-15 01:24:14 -0700102 int32_t number_of_cores,
103 size_t max_payload_size);
104 int32_t Encode(const VideoFrame& frame,
105 const CodecSpecificInfo* codec_specific,
106 const std::vector<FrameType>& frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
philipel9d3ab612015-12-21 04:12:39 -0800108 void SetEncoderParameters(const EncoderParameters& params);
109 EncoderParameters GetEncoderParameters() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
philipel9d3ab612015-12-21 04:12:39 -0800111 int32_t SetPeriodicKeyFrames(bool enable);
philipel9d3ab612015-12-21 04:12:39 -0800112 int32_t RequestFrame(const std::vector<FrameType>& frame_types);
philipel9d3ab612015-12-21 04:12:39 -0800113 bool InternalSource() const;
philipel9d3ab612015-12-21 04:12:39 -0800114 void OnDroppedFrame();
philipel9d3ab612015-12-21 04:12:39 -0800115 bool SupportsNativeHandle() const;
philipel9d3ab612015-12-21 04:12:39 -0800116
117 private:
Peter Boström02bafc62016-07-01 12:45:15 +0200118 rtc::RaceChecker race_checker_;
119
120 VideoEncoder* const encoder_ GUARDED_BY(race_checker_);
philipel9d3ab612015-12-21 04:12:39 -0800121 VCMEncodedFrameCallback* const vcm_encoded_frame_callback_;
122 const bool internal_source_;
pbos5ad935c2016-01-25 03:52:44 -0800123 rtc::CriticalSection params_lock_;
philipel9d3ab612015-12-21 04:12:39 -0800124 EncoderParameters encoder_params_ GUARDED_BY(params_lock_);
philipel9d3ab612015-12-21 04:12:39 -0800125 bool is_screenshare_;
ilnik04f4d122017-06-19 07:18:55 -0700126 size_t streams_or_svc_num_;
sprang3911c262016-04-15 01:24:14 -0700127};
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000129} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
philipel9d3ab612015-12-21 04:12:39 -0800131#endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_