blob: 2584568533a21fda42aef162f670a26710fc64b5 [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;
27struct VideoCodec;
28
29class EncodedImageCallback {
30 public:
31 virtual ~EncodedImageCallback() {}
32
Sergey Ulanov4c7f4cd2016-08-02 15:14:39 -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.
Sergey Ulanov4c7f4cd2016-08-02 15:14:39 -070057 virtual Result OnEncodedImage(const EncodedImage& encoded_image,
58 const CodecSpecificInfo* codec_specific_info,
59 const RTPFragmentationHeader* fragmentation) {
60 return (Encoded(encoded_image, codec_specific_info, fragmentation) == 0)
61 ? Result(Result::OK, 0)
62 : Result(Result::ERROR_SEND_FAILED);
63 }
64
65 // DEPRECATED.
66 // TODO(sergeyu): Remove this method.
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +000067 virtual int32_t Encoded(const EncodedImage& encoded_image,
68 const CodecSpecificInfo* codec_specific_info,
Sergey Ulanov4c7f4cd2016-08-02 15:14:39 -070069 const RTPFragmentationHeader* fragmentation) {
70 Result result =
71 OnEncodedImage(encoded_image, codec_specific_info, fragmentation);
72 return (result.error != Result::OK) ? -1 : (result.drop_next_frame ? 1 : 0);
73 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000074};
75
76class VideoEncoder {
77 public:
78 enum EncoderType {
Zeke Chin71f6f442015-06-29 14:34:58 -070079 kH264,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000080 kVp8,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000081 kVp9,
Peter Boström4d71ede2015-05-19 23:09:35 +020082 kUnsupportedCodec,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000083 };
84
85 static VideoEncoder* Create(EncoderType codec_type);
86
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000087 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000088 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000089 static VideoCodecH264 GetDefaultH264Settings();
90
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000091 virtual ~VideoEncoder() {}
92
marpan@webrtc.org5b883172014-11-01 06:10:48 +000093 // Initialize the encoder with the information from the codecSettings
94 //
95 // Input:
96 // - codec_settings : Codec settings
97 // - number_of_cores : Number of cores available for the encoder
98 // - max_payload_size : The maximum size each payload is allowed
99 // to have. Usually MTU - overhead.
100 //
101 // Return value : Set bit rate if OK
102 // <0 - Errors:
103 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
104 // WEBRTC_VIDEO_CODEC_ERR_SIZE
105 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
106 // WEBRTC_VIDEO_CODEC_MEMORY
107 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000108 virtual int32_t InitEncode(const VideoCodec* codec_settings,
109 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000110 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000111
112 // Register an encode complete callback object.
113 //
114 // Input:
115 // - callback : Callback object which handles encoded images.
116 //
117 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000118 virtual int32_t RegisterEncodeCompleteCallback(
119 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000120
121 // Free encoder memory.
122 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000123 virtual int32_t Release() = 0;
124
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000125 // Encode an I420 image (as a part of a video stream). The encoded image
126 // will be returned to the user through the encode complete callback.
127 //
128 // Input:
129 // - frame : Image to be encoded
130 // - frame_types : Frame type to be generated by the encoder.
131 //
132 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
133 // <0 - Errors:
134 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
135 // WEBRTC_VIDEO_CODEC_MEMORY
136 // WEBRTC_VIDEO_CODEC_ERROR
137 // WEBRTC_VIDEO_CODEC_TIMEOUT
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700138 virtual int32_t Encode(const VideoFrame& frame,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000139 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700140 const std::vector<FrameType>* frame_types) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000141
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000142 // Inform the encoder of the new packet loss rate and the round-trip time of
143 // the network.
144 //
145 // Input:
146 // - packet_loss : Fraction lost
147 // (loss rate in percent = 100 * packetLoss / 255)
148 // - rtt : Round-trip time in milliseconds
149 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
150 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000151 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000152
153 // Inform the encoder about the new target bit rate.
154 //
155 // Input:
156 // - bitrate : New target bit rate
157 // - framerate : The target frame rate
158 //
159 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000160 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
161
162 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200163 virtual void OnDroppedFrame() {}
164 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100165 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000166};
167
Peter Boström4d71ede2015-05-19 23:09:35 +0200168// Class used to wrap external VideoEncoders to provide a fallback option on
169// software encoding when a hardware encoder fails to encode a stream due to
170// hardware restrictions, such as max resolution.
171class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
172 public:
173 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
174 webrtc::VideoEncoder* encoder);
175
176 int32_t InitEncode(const VideoCodec* codec_settings,
177 int32_t number_of_cores,
178 size_t max_payload_size) override;
179
180 int32_t RegisterEncodeCompleteCallback(
181 EncodedImageCallback* callback) override;
182
183 int32_t Release() override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700184 int32_t Encode(const VideoFrame& frame,
Peter Boström4d71ede2015-05-19 23:09:35 +0200185 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700186 const std::vector<FrameType>* frame_types) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200187 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
188
189 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override;
190 void OnDroppedFrame() override;
Peter Boströmeb66e802015-06-05 11:08:03 +0200191 bool SupportsNativeHandle() const override;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100192 const char* ImplementationName() const override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200193
194 private:
noahricb1ce6632015-10-21 23:54:51 -0700195 bool InitFallbackEncoder();
196
197 // Settings used in the last InitEncode call and used if a dynamic fallback to
198 // software is required.
199 VideoCodec codec_settings_;
200 int32_t number_of_cores_;
201 size_t max_payload_size_;
202
203 // The last bitrate/framerate set, and a flag for noting they are set.
204 bool rates_set_;
205 uint32_t bitrate_;
206 uint32_t framerate_;
207
208 // The last channel parameters set, and a flag for noting they are set.
209 bool channel_parameters_set_;
210 uint32_t packet_loss_;
211 int64_t rtt_;
212
Peter Boström4d71ede2015-05-19 23:09:35 +0200213 const EncoderType encoder_type_;
214 webrtc::VideoEncoder* const encoder_;
215
kwibergc891eb42016-03-02 03:41:34 -0800216 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100217 std::string fallback_implementation_name_;
Peter Boström4d71ede2015-05-19 23:09:35 +0200218 EncodedImageCallback* callback_;
219};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000220} // namespace webrtc
221#endif // WEBRTC_VIDEO_ENCODER_H_