niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
pwestin@webrtc.org | 52fd98d | 2012-02-13 09:03:53 +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 | |
| 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_ |
| 13 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 14 | #include <stdio.h> |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 15 | #include <map> |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
| 19 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
tommi@webrtc.org | 558dc40 | 2015-03-07 20:55:56 +0000 | [diff] [blame] | 21 | #include "webrtc/base/criticalsection.h" |
Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [diff] [blame] | 22 | #include "webrtc/base/race_checker.h" |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 23 | |
andresp@webrtc.org | 1df9dc3 | 2014-01-09 08:01:57 +0000 | [diff] [blame] | 24 | namespace webrtc { |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 25 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 26 | namespace media_optimization { |
henrik.lundin@webrtc.org | bec11ef | 2013-09-23 19:54:25 +0000 | [diff] [blame] | 27 | class MediaOptimization; |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 28 | } // namespace media_optimization |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
Peter Boström | 69ccb33 | 2015-10-29 16:30:23 +0100 | [diff] [blame] | 30 | struct EncoderParameters { |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 31 | BitrateAllocation target_bitrate; |
Peter Boström | 69ccb33 | 2015-10-29 16:30:23 +0100 | [diff] [blame] | 32 | uint8_t loss_rate; |
| 33 | int64_t rtt; |
| 34 | uint32_t input_frame_rate; |
| 35 | }; |
| 36 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 37 | class VCMEncodedFrameCallback : public EncodedImageCallback { |
| 38 | public: |
perkj | 376b192 | 2016-05-02 11:35:24 -0700 | [diff] [blame] | 39 | VCMEncodedFrameCallback(EncodedImageCallback* post_encode_callback, |
| 40 | media_optimization::MediaOptimization* media_opt); |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 41 | virtual ~VCMEncodedFrameCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 43 | // Implements EncodedImageCallback. |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 44 | EncodedImageCallback::Result OnEncodedImage( |
| 45 | const EncodedImage& encoded_image, |
| 46 | const CodecSpecificInfo* codec_specific_info, |
| 47 | const RTPFragmentationHeader* fragmentation) override; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 48 | |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 49 | void SetInternalSource(bool internal_source) { |
| 50 | internal_source_ = internal_source; |
| 51 | } |
guoweis@webrtc.org | 54d072e | 2015-03-17 21:54:50 +0000 | [diff] [blame] | 52 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 53 | // 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 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 68 | private: |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 69 | rtc::CriticalSection timing_params_lock_; |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 70 | bool internal_source_; |
perkj | 376b192 | 2016-05-02 11:35:24 -0700 | [diff] [blame] | 71 | EncodedImageCallback* const post_encode_callback_; |
| 72 | media_optimization::MediaOptimization* const media_opt_; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 73 | |
| 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_); |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 85 | }; |
sprang@webrtc.org | 4070935 | 2013-11-26 11:41:59 +0000 | [diff] [blame] | 86 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 87 | class VCMGenericEncoder { |
| 88 | friend class VCMCodecDataBase; |
Peter Boström | 69ccb33 | 2015-10-29 16:30:23 +0100 | [diff] [blame] | 89 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 90 | public: |
| 91 | VCMGenericEncoder(VideoEncoder* encoder, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 92 | VCMEncodedFrameCallback* encoded_frame_callback, |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 93 | bool internal_source); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 94 | ~VCMGenericEncoder(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 95 | int32_t Release(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 96 | int32_t InitEncode(const VideoCodec* settings, |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 97 | int32_t number_of_cores, |
| 98 | size_t max_payload_size); |
| 99 | int32_t Encode(const VideoFrame& frame, |
| 100 | const CodecSpecificInfo* codec_specific, |
| 101 | const std::vector<FrameType>& frame_types); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 103 | void SetEncoderParameters(const EncoderParameters& params); |
| 104 | EncoderParameters GetEncoderParameters() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 106 | int32_t SetPeriodicKeyFrames(bool enable); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 107 | int32_t RequestFrame(const std::vector<FrameType>& frame_types); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 108 | bool InternalSource() const; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | void OnDroppedFrame(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 110 | bool SupportsNativeHandle() const; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 111 | |
| 112 | private: |
Peter Boström | 02bafc6 | 2016-07-01 12:45:15 +0200 | [diff] [blame] | 113 | rtc::RaceChecker race_checker_; |
| 114 | |
| 115 | VideoEncoder* const encoder_ GUARDED_BY(race_checker_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 116 | VCMEncodedFrameCallback* const vcm_encoded_frame_callback_; |
| 117 | const bool internal_source_; |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 118 | rtc::CriticalSection params_lock_; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 119 | EncoderParameters encoder_params_ GUARDED_BY(params_lock_); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 120 | bool is_screenshare_; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 121 | size_t streams_or_svc_num_; |
sprang | 3911c26 | 2016-04-15 01:24:14 -0700 | [diff] [blame] | 122 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 124 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 126 | #endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_ |