blob: 832f40f56d446e1cf70aa5774acea42f9c4d42a3 [file] [log] [blame]
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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_VIDEO_ENCODER_H_
12#define WEBRTC_VIDEO_ENCODER_H_
13
14#include <vector>
15
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000016#include "webrtc/common_types.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000017#include "webrtc/typedefs.h"
18#include "webrtc/video_frame.h"
19
20namespace webrtc {
21
22class RTPFragmentationHeader;
23// TODO(pbos): Expose these through a public (root) header or change these APIs.
24struct CodecSpecificInfo;
25struct VideoCodec;
26
27class EncodedImageCallback {
28 public:
29 virtual ~EncodedImageCallback() {}
30
31 // Callback function which is called when an image has been encoded.
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +000032 virtual int32_t Encoded(const EncodedImage& encoded_image,
33 const CodecSpecificInfo* codec_specific_info,
34 const RTPFragmentationHeader* fragmentation) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000035};
36
37class VideoEncoder {
38 public:
39 enum EncoderType {
40 kVp8,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000041 kVp9,
Peter Boström4d71ede2015-05-19 23:09:35 +020042 kUnsupportedCodec,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000043 };
44
45 static VideoEncoder* Create(EncoderType codec_type);
46
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000047 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000048 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000049 static VideoCodecH264 GetDefaultH264Settings();
50
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000051 virtual ~VideoEncoder() {}
52
marpan@webrtc.org5b883172014-11-01 06:10:48 +000053 // Initialize the encoder with the information from the codecSettings
54 //
55 // Input:
56 // - codec_settings : Codec settings
57 // - number_of_cores : Number of cores available for the encoder
58 // - max_payload_size : The maximum size each payload is allowed
59 // to have. Usually MTU - overhead.
60 //
61 // Return value : Set bit rate if OK
62 // <0 - Errors:
63 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
64 // WEBRTC_VIDEO_CODEC_ERR_SIZE
65 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
66 // WEBRTC_VIDEO_CODEC_MEMORY
67 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000068 virtual int32_t InitEncode(const VideoCodec* codec_settings,
69 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000070 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000071
72 // Register an encode complete callback object.
73 //
74 // Input:
75 // - callback : Callback object which handles encoded images.
76 //
77 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000078 virtual int32_t RegisterEncodeCompleteCallback(
79 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000080
81 // Free encoder memory.
82 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000083 virtual int32_t Release() = 0;
84
marpan@webrtc.org5b883172014-11-01 06:10:48 +000085 // Encode an I420 image (as a part of a video stream). The encoded image
86 // will be returned to the user through the encode complete callback.
87 //
88 // Input:
89 // - frame : Image to be encoded
90 // - frame_types : Frame type to be generated by the encoder.
91 //
92 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
93 // <0 - Errors:
94 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
95 // WEBRTC_VIDEO_CODEC_MEMORY
96 // WEBRTC_VIDEO_CODEC_ERROR
97 // WEBRTC_VIDEO_CODEC_TIMEOUT
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098 virtual int32_t Encode(const I420VideoFrame& frame,
99 const CodecSpecificInfo* codec_specific_info,
100 const std::vector<VideoFrameType>* frame_types) = 0;
101
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000102 // Inform the encoder of the new packet loss rate and the round-trip time of
103 // the network.
104 //
105 // Input:
106 // - packet_loss : Fraction lost
107 // (loss rate in percent = 100 * packetLoss / 255)
108 // - rtt : Round-trip time in milliseconds
109 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
110 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000111 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000112
113 // Inform the encoder about the new target bit rate.
114 //
115 // Input:
116 // - bitrate : New target bit rate
117 // - framerate : The target frame rate
118 //
119 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000120 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
121
122 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
123 virtual int32_t CodecConfigParameters(uint8_t* /*buffer*/, int32_t /*size*/) {
124 return -1;
125 }
jackychen61b4d512015-04-21 15:30:11 -0700126 virtual void OnDroppedFrame() {};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000127};
128
Peter Boström4d71ede2015-05-19 23:09:35 +0200129// Class used to wrap external VideoEncoders to provide a fallback option on
130// software encoding when a hardware encoder fails to encode a stream due to
131// hardware restrictions, such as max resolution.
132class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
133 public:
134 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
135 webrtc::VideoEncoder* encoder);
136
137 int32_t InitEncode(const VideoCodec* codec_settings,
138 int32_t number_of_cores,
139 size_t max_payload_size) override;
140
141 int32_t RegisterEncodeCompleteCallback(
142 EncodedImageCallback* callback) override;
143
144 int32_t Release() override;
145 int32_t Encode(const I420VideoFrame& frame,
146 const CodecSpecificInfo* codec_specific_info,
147 const std::vector<VideoFrameType>* frame_types) override;
148 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
149
150 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override;
151 void OnDroppedFrame() override;
152
153 private:
154 const EncoderType encoder_type_;
155 webrtc::VideoEncoder* const encoder_;
156
157 rtc::scoped_ptr<webrtc::VideoEncoder> fallback_encoder_;
158 EncodedImageCallback* callback_;
159};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000160} // namespace webrtc
161#endif // WEBRTC_VIDEO_ENCODER_H_