blob: d8b7921bc07fad057cfc438021c41c6219b134a6 [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.
kjellander91b957d2016-11-03 11:53:43 -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.
67 virtual int32_t Encoded(const EncodedImage& encoded_image,
68 const CodecSpecificInfo* codec_specific_info,
69 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);
sakal327e9d02016-10-06 05:55:11 -070086 // Returns true if this type of encoder can be created using
87 // VideoEncoder::Create.
88 static bool IsSupportedSoftware(EncoderType codec_type);
89 static EncoderType CodecToEncoderType(VideoCodecType codec_type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000090
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000091 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000092 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000093 static VideoCodecH264 GetDefaultH264Settings();
94
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000095 virtual ~VideoEncoder() {}
96
marpan@webrtc.org5b883172014-11-01 06:10:48 +000097 // Initialize the encoder with the information from the codecSettings
98 //
99 // Input:
100 // - codec_settings : Codec settings
101 // - number_of_cores : Number of cores available for the encoder
102 // - max_payload_size : The maximum size each payload is allowed
103 // to have. Usually MTU - overhead.
104 //
105 // Return value : Set bit rate if OK
106 // <0 - Errors:
107 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
108 // WEBRTC_VIDEO_CODEC_ERR_SIZE
109 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
110 // WEBRTC_VIDEO_CODEC_MEMORY
111 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000112 virtual int32_t InitEncode(const VideoCodec* codec_settings,
113 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000114 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000115
116 // Register an encode complete callback object.
117 //
118 // Input:
119 // - callback : Callback object which handles encoded images.
120 //
121 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000122 virtual int32_t RegisterEncodeCompleteCallback(
123 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000124
125 // Free encoder memory.
126 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000127 virtual int32_t Release() = 0;
128
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000129 // Encode an I420 image (as a part of a video stream). The encoded image
130 // will be returned to the user through the encode complete callback.
131 //
132 // Input:
133 // - frame : Image to be encoded
134 // - frame_types : Frame type to be generated by the encoder.
135 //
136 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
137 // <0 - Errors:
138 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
139 // WEBRTC_VIDEO_CODEC_MEMORY
140 // WEBRTC_VIDEO_CODEC_ERROR
141 // WEBRTC_VIDEO_CODEC_TIMEOUT
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700142 virtual int32_t Encode(const VideoFrame& frame,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000143 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700144 const std::vector<FrameType>* frame_types) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000145
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000146 // Inform the encoder of the new packet loss rate and the round-trip time of
147 // the network.
148 //
149 // Input:
150 // - packet_loss : Fraction lost
151 // (loss rate in percent = 100 * packetLoss / 255)
152 // - rtt : Round-trip time in milliseconds
153 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
154 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000155 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000156
157 // Inform the encoder about the new target bit rate.
158 //
159 // Input:
160 // - bitrate : New target bit rate
161 // - framerate : The target frame rate
162 //
163 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000164 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
165
166 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200167 virtual void OnDroppedFrame() {}
168 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100169 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000170};
171
Peter Boström4d71ede2015-05-19 23:09:35 +0200172// Class used to wrap external VideoEncoders to provide a fallback option on
173// software encoding when a hardware encoder fails to encode a stream due to
174// hardware restrictions, such as max resolution.
175class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
176 public:
177 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
178 webrtc::VideoEncoder* encoder);
179
180 int32_t InitEncode(const VideoCodec* codec_settings,
181 int32_t number_of_cores,
182 size_t max_payload_size) override;
183
184 int32_t RegisterEncodeCompleteCallback(
185 EncodedImageCallback* callback) override;
186
187 int32_t Release() override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700188 int32_t Encode(const VideoFrame& frame,
Peter Boström4d71ede2015-05-19 23:09:35 +0200189 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700190 const std::vector<FrameType>* frame_types) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200191 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
192
193 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override;
194 void OnDroppedFrame() override;
Peter Boströmeb66e802015-06-05 11:08:03 +0200195 bool SupportsNativeHandle() const override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200196
197 private:
noahricb1ce6632015-10-21 23:54:51 -0700198 bool InitFallbackEncoder();
199
200 // Settings used in the last InitEncode call and used if a dynamic fallback to
201 // software is required.
202 VideoCodec codec_settings_;
203 int32_t number_of_cores_;
204 size_t max_payload_size_;
205
206 // The last bitrate/framerate set, and a flag for noting they are set.
207 bool rates_set_;
208 uint32_t bitrate_;
209 uint32_t framerate_;
210
211 // The last channel parameters set, and a flag for noting they are set.
212 bool channel_parameters_set_;
213 uint32_t packet_loss_;
214 int64_t rtt_;
215
Peter Boström4d71ede2015-05-19 23:09:35 +0200216 const EncoderType encoder_type_;
217 webrtc::VideoEncoder* const encoder_;
218
kwibergc891eb42016-03-02 03:41:34 -0800219 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100220 std::string fallback_implementation_name_;
Peter Boström4d71ede2015-05-19 23:09:35 +0200221 EncodedImageCallback* callback_;
222};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000223} // namespace webrtc
224#endif // WEBRTC_VIDEO_ENCODER_H_