blob: f739edb44fe478dee0fea11cb1c85d62c74a2f86 [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>
15#include <vector>
16
Henrik Kjellander2557b862015-11-18 22:00:21 +010017#include "webrtc/modules/video_coding/include/video_codec_interface.h"
18#include "webrtc/modules/video_coding/include/video_coding_defines.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
tommi@webrtc.org558dc402015-03-07 20:55:56 +000020#include "webrtc/base/criticalsection.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000021#include "webrtc/base/scoped_ptr.h"
sprang@webrtc.org40709352013-11-26 11:41:59 +000022
andresp@webrtc.org1df9dc32014-01-09 08:01:57 +000023namespace webrtc {
sprang@webrtc.org40709352013-11-26 11:41:59 +000024class CriticalSectionWrapper;
25
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 {
31 uint32_t target_bitrate;
32 uint8_t loss_rate;
33 int64_t rtt;
34 uint32_t input_frame_rate;
35};
36
niklase@google.com470e71d2011-07-07 08:21:25 +000037/*************************************/
38/* VCMEncodeFrameCallback class */
39/***********************************/
philipel9d3ab612015-12-21 04:12:39 -080040class VCMEncodedFrameCallback : public EncodedImageCallback {
41 public:
42 explicit VCMEncodedFrameCallback(
43 EncodedImageCallback* post_encode_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000044 virtual ~VCMEncodedFrameCallback();
45
46 /*
47 * Callback implementation - codec encode complete
48 */
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000049 int32_t Encoded(
pbos@webrtc.org273a4142014-12-01 15:23:21 +000050 const EncodedImage& encodedImage,
niklase@google.com470e71d2011-07-07 08:21:25 +000051 const CodecSpecificInfo* codecSpecificInfo = NULL,
52 const RTPFragmentationHeader* fragmentationHeader = NULL);
53 /*
niklase@google.com470e71d2011-07-07 08:21:25 +000054 * Callback implementation - generic encoder encode complete
55 */
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000056 int32_t SetTransportCallback(VCMPacketizationCallback* transport);
niklase@google.com470e71d2011-07-07 08:21:25 +000057 /**
58 * Set media Optimization
59 */
philipel9d3ab612015-12-21 04:12:39 -080060 void SetMediaOpt(media_optimization::MediaOptimization* mediaOpt);
niklase@google.com470e71d2011-07-07 08:21:25 +000061
philipel9d3ab612015-12-21 04:12:39 -080062 void SetPayloadType(uint8_t payloadType) {
63 _payloadType = payloadType;
64 }
65
66 void SetInternalSource(bool internalSource) {
67 _internalSource = internalSource;
68 }
niklase@google.com470e71d2011-07-07 08:21:25 +000069
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000070 void SetRotation(VideoRotation rotation) { _rotation = rotation; }
Peter Boströmb7d9a972015-12-18 16:01:11 +010071 void SignalLastEncoderImplementationUsed(
72 const char* encoder_implementation_name);
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000073
philipel9d3ab612015-12-21 04:12:39 -080074 private:
Peter Boströmb7d9a972015-12-18 16:01:11 +010075 VCMPacketizationCallback* send_callback_;
henrik.lundin@webrtc.orgbec11ef2013-09-23 19:54:25 +000076 media_optimization::MediaOptimization* _mediaOpt;
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +000077 uint8_t _payloadType;
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000078 bool _internalSource;
guoweis@webrtc.org54d072e2015-03-17 21:54:50 +000079 VideoRotation _rotation;
sprang@webrtc.org40709352013-11-26 11:41:59 +000080
sprang@webrtc.org40709352013-11-26 11:41:59 +000081 EncodedImageCallback* post_encode_callback_;
82
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +000083#ifdef DEBUG_ENCODER_BIT_STREAM
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000084 FILE* _bitStreamAfterEncoder;
tommi@webrtc.orga9da4c52012-07-20 11:17:23 +000085#endif
philipel9d3ab612015-12-21 04:12:39 -080086}; // end of VCMEncodeFrameCallback class
niklase@google.com470e71d2011-07-07 08:21:25 +000087
88/******************************/
89/* VCMGenericEncoder class */
90/******************************/
philipel9d3ab612015-12-21 04:12:39 -080091class VCMGenericEncoder {
92 friend class VCMCodecDataBase;
Peter Boström69ccb332015-10-29 16:30:23 +010093
philipel9d3ab612015-12-21 04:12:39 -080094 public:
95 VCMGenericEncoder(VideoEncoder* encoder,
96 VideoEncoderRateObserver* rate_observer,
97 VCMEncodedFrameCallback* encoded_frame_callback,
98 bool internalSource);
99 ~VCMGenericEncoder();
100 /**
101 * Free encoder memory
102 */
103 int32_t Release();
104 /**
105 * Initialize the encoder with the information from the VideoCodec
106 */
107 int32_t InitEncode(const VideoCodec* settings,
108 int32_t numberOfCores,
109 size_t maxPayloadSize);
110 /**
111 * Encode raw image
112 * inputFrame : Frame containing raw image
113 * codecSpecificInfo : Specific codec data
114 * cameraFrameRate : Request or information from the remote side
115 * frameType : The requested frame type to encode
116 */
117 int32_t Encode(const VideoFrame& inputFrame,
118 const CodecSpecificInfo* codecSpecificInfo,
119 const std::vector<FrameType>& frameTypes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
philipel9d3ab612015-12-21 04:12:39 -0800121 void SetEncoderParameters(const EncoderParameters& params);
122 EncoderParameters GetEncoderParameters() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
philipel9d3ab612015-12-21 04:12:39 -0800124 int32_t SetPeriodicKeyFrames(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
philipel9d3ab612015-12-21 04:12:39 -0800126 int32_t RequestFrame(const std::vector<FrameType>& frame_types);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
philipel9d3ab612015-12-21 04:12:39 -0800128 bool InternalSource() const;
jackychen61b4d512015-04-21 15:30:11 -0700129
philipel9d3ab612015-12-21 04:12:39 -0800130 void OnDroppedFrame();
Peter Boströmeb66e802015-06-05 11:08:03 +0200131
philipel9d3ab612015-12-21 04:12:39 -0800132 bool SupportsNativeHandle() const;
jackychen6e2ce6e2015-07-13 16:26:33 -0700133
philipel9d3ab612015-12-21 04:12:39 -0800134 int GetTargetFramerate();
135
136 private:
137 VideoEncoder* const encoder_;
138 VideoEncoderRateObserver* const rate_observer_;
139 VCMEncodedFrameCallback* const vcm_encoded_frame_callback_;
140 const bool internal_source_;
141 mutable rtc::CriticalSection params_lock_;
142 EncoderParameters encoder_params_ GUARDED_BY(params_lock_);
143 VideoRotation rotation_;
144 bool is_screenshare_;
145}; // end of VCMGenericEncoder class
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000147} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
philipel9d3ab612015-12-21 04:12:39 -0800149#endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_ENCODER_H_