blob: 39a27a8bfcdb32707a10f759f00e244fc4227bc0 [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
kwibergc891eb42016-03-02 03:41:34 -080014#include <memory>
Peter Boströmb7d9a972015-12-18 16:01:11 +010015#include <string>
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000016#include <vector>
17
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000018#include "webrtc/common_types.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000019#include "webrtc/typedefs.h"
20#include "webrtc/video_frame.h"
21
22namespace webrtc {
23
24class RTPFragmentationHeader;
25// TODO(pbos): Expose these through a public (root) header or change these APIs.
26struct CodecSpecificInfo;
hta257dc392016-10-25 09:05:06 -070027class VideoCodec;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000028
29class EncodedImageCallback {
30 public:
31 virtual ~EncodedImageCallback() {}
32
Sergey Ulanov525df3f2016-08-02 17:46:41 -070033 struct Result {
34 enum Error {
35 OK,
36
37 // Failed to send the packet.
38 ERROR_SEND_FAILED,
39 };
40
41 Result(Error error) : error(error) {}
42 Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {}
43
44 Error error;
45
46 // Frame ID assigned to the frame. The frame ID should be the same as the ID
47 // seen by the receiver for this frame. RTP timestamp of the frame is used
48 // as frame ID when RTP is used to send video. Must be used only when
49 // error=OK.
50 uint32_t frame_id = 0;
51
52 // Tells the encoder that the next frame is should be dropped.
53 bool drop_next_frame = false;
54 };
55
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000056 // Callback function which is called when an image has been encoded.
sergeyu2cb155a2016-11-04 11:39:29 -070057 virtual Result OnEncodedImage(
58 const EncodedImage& encoded_image,
59 const CodecSpecificInfo* codec_specific_info,
60 const RTPFragmentationHeader* fragmentation) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000061};
62
63class VideoEncoder {
64 public:
65 enum EncoderType {
Zeke Chin71f6f442015-06-29 14:34:58 -070066 kH264,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000067 kVp8,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000068 kVp9,
Peter Boström4d71ede2015-05-19 23:09:35 +020069 kUnsupportedCodec,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000070 };
71
72 static VideoEncoder* Create(EncoderType codec_type);
sakal327e9d02016-10-06 05:55:11 -070073 // Returns true if this type of encoder can be created using
74 // VideoEncoder::Create.
75 static bool IsSupportedSoftware(EncoderType codec_type);
76 static EncoderType CodecToEncoderType(VideoCodecType codec_type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000077
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000078 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000079 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000080 static VideoCodecH264 GetDefaultH264Settings();
81
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000082 virtual ~VideoEncoder() {}
83
marpan@webrtc.org5b883172014-11-01 06:10:48 +000084 // Initialize the encoder with the information from the codecSettings
85 //
86 // Input:
87 // - codec_settings : Codec settings
88 // - number_of_cores : Number of cores available for the encoder
89 // - max_payload_size : The maximum size each payload is allowed
90 // to have. Usually MTU - overhead.
91 //
92 // Return value : Set bit rate if OK
93 // <0 - Errors:
94 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
95 // WEBRTC_VIDEO_CODEC_ERR_SIZE
96 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
97 // WEBRTC_VIDEO_CODEC_MEMORY
98 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000099 virtual int32_t InitEncode(const VideoCodec* codec_settings,
100 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000101 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000102
103 // Register an encode complete callback object.
104 //
105 // Input:
106 // - callback : Callback object which handles encoded images.
107 //
108 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000109 virtual int32_t RegisterEncodeCompleteCallback(
110 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000111
112 // Free encoder memory.
113 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000114 virtual int32_t Release() = 0;
115
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000116 // Encode an I420 image (as a part of a video stream). The encoded image
117 // will be returned to the user through the encode complete callback.
118 //
119 // Input:
120 // - frame : Image to be encoded
121 // - frame_types : Frame type to be generated by the encoder.
122 //
123 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
124 // <0 - Errors:
125 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
126 // WEBRTC_VIDEO_CODEC_MEMORY
127 // WEBRTC_VIDEO_CODEC_ERROR
128 // WEBRTC_VIDEO_CODEC_TIMEOUT
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700129 virtual int32_t Encode(const VideoFrame& frame,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000130 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700131 const std::vector<FrameType>* frame_types) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000132
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000133 // Inform the encoder of the new packet loss rate and the round-trip time of
134 // the network.
135 //
136 // Input:
137 // - packet_loss : Fraction lost
138 // (loss rate in percent = 100 * packetLoss / 255)
139 // - rtt : Round-trip time in milliseconds
140 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
141 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000142 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000143
144 // Inform the encoder about the new target bit rate.
145 //
146 // Input:
147 // - bitrate : New target bit rate
148 // - framerate : The target frame rate
149 //
150 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
sprang1369c832016-11-10 08:30:33 -0800151 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000152
153 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200154 virtual void OnDroppedFrame() {}
155 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100156 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000157};
158
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000159} // namespace webrtc
160#endif // WEBRTC_VIDEO_ENCODER_H_