blob: ce2569bff9ba8b8a2b542b557426878a8e8584ec [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,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000042 };
43
44 static VideoEncoder* Create(EncoderType codec_type);
45
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000046 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000047 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000048 static VideoCodecH264 GetDefaultH264Settings();
49
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000050 virtual ~VideoEncoder() {}
51
marpan@webrtc.org5b883172014-11-01 06:10:48 +000052 // Initialize the encoder with the information from the codecSettings
53 //
54 // Input:
55 // - codec_settings : Codec settings
56 // - number_of_cores : Number of cores available for the encoder
57 // - max_payload_size : The maximum size each payload is allowed
58 // to have. Usually MTU - overhead.
59 //
60 // Return value : Set bit rate if OK
61 // <0 - Errors:
62 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
63 // WEBRTC_VIDEO_CODEC_ERR_SIZE
64 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
65 // WEBRTC_VIDEO_CODEC_MEMORY
66 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000067 virtual int32_t InitEncode(const VideoCodec* codec_settings,
68 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000069 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000070
71 // Register an encode complete callback object.
72 //
73 // Input:
74 // - callback : Callback object which handles encoded images.
75 //
76 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000077 virtual int32_t RegisterEncodeCompleteCallback(
78 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000079
80 // Free encoder memory.
81 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000082 virtual int32_t Release() = 0;
83
marpan@webrtc.org5b883172014-11-01 06:10:48 +000084 // Encode an I420 image (as a part of a video stream). The encoded image
85 // will be returned to the user through the encode complete callback.
86 //
87 // Input:
88 // - frame : Image to be encoded
89 // - frame_types : Frame type to be generated by the encoder.
90 //
91 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
92 // <0 - Errors:
93 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
94 // WEBRTC_VIDEO_CODEC_MEMORY
95 // WEBRTC_VIDEO_CODEC_ERROR
96 // WEBRTC_VIDEO_CODEC_TIMEOUT
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000097 virtual int32_t Encode(const I420VideoFrame& frame,
98 const CodecSpecificInfo* codec_specific_info,
99 const std::vector<VideoFrameType>* frame_types) = 0;
100
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000101 // Inform the encoder of the new packet loss rate and the round-trip time of
102 // the network.
103 //
104 // Input:
105 // - packet_loss : Fraction lost
106 // (loss rate in percent = 100 * packetLoss / 255)
107 // - rtt : Round-trip time in milliseconds
108 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
109 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000110 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000111
112 // Inform the encoder about the new target bit rate.
113 //
114 // Input:
115 // - bitrate : New target bit rate
116 // - framerate : The target frame rate
117 //
118 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000119 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
120
121 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
122 virtual int32_t CodecConfigParameters(uint8_t* /*buffer*/, int32_t /*size*/) {
123 return -1;
124 }
jackychen61b4d512015-04-21 15:30:11 -0700125 virtual void OnDroppedFrame() {};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000126};
127
128} // namespace webrtc
129#endif // WEBRTC_VIDEO_ENCODER_H_