blob: 2dc8334d625144806d589fd11294c36ec53e1ce5 [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
sprang647bf432016-11-10 06:46:20 -080018#include "webrtc/base/checks.h"
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000019#include "webrtc/common_types.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000020#include "webrtc/typedefs.h"
21#include "webrtc/video_frame.h"
22
23namespace webrtc {
24
25class RTPFragmentationHeader;
26// TODO(pbos): Expose these through a public (root) header or change these APIs.
27struct CodecSpecificInfo;
hta257dc392016-10-25 09:05:06 -070028class VideoCodec;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000029
30class EncodedImageCallback {
31 public:
32 virtual ~EncodedImageCallback() {}
33
Sergey Ulanov525df3f2016-08-02 17:46:41 -070034 struct Result {
35 enum Error {
36 OK,
37
38 // Failed to send the packet.
39 ERROR_SEND_FAILED,
40 };
41
42 Result(Error error) : error(error) {}
43 Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {}
44
45 Error error;
46
47 // Frame ID assigned to the frame. The frame ID should be the same as the ID
48 // seen by the receiver for this frame. RTP timestamp of the frame is used
49 // as frame ID when RTP is used to send video. Must be used only when
50 // error=OK.
51 uint32_t frame_id = 0;
52
53 // Tells the encoder that the next frame is should be dropped.
54 bool drop_next_frame = false;
55 };
56
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000057 // Callback function which is called when an image has been encoded.
sergeyu2cb155a2016-11-04 11:39:29 -070058 virtual Result OnEncodedImage(
59 const EncodedImage& encoded_image,
60 const CodecSpecificInfo* codec_specific_info,
61 const RTPFragmentationHeader* fragmentation) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000062};
63
64class VideoEncoder {
65 public:
66 enum EncoderType {
Zeke Chin71f6f442015-06-29 14:34:58 -070067 kH264,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000068 kVp8,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000069 kVp9,
Peter Boström4d71ede2015-05-19 23:09:35 +020070 kUnsupportedCodec,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000071 };
72
73 static VideoEncoder* Create(EncoderType codec_type);
sakal327e9d02016-10-06 05:55:11 -070074 // Returns true if this type of encoder can be created using
75 // VideoEncoder::Create.
76 static bool IsSupportedSoftware(EncoderType codec_type);
77 static EncoderType CodecToEncoderType(VideoCodecType codec_type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000078
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000079 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000080 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000081 static VideoCodecH264 GetDefaultH264Settings();
82
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000083 virtual ~VideoEncoder() {}
84
marpan@webrtc.org5b883172014-11-01 06:10:48 +000085 // Initialize the encoder with the information from the codecSettings
86 //
87 // Input:
88 // - codec_settings : Codec settings
89 // - number_of_cores : Number of cores available for the encoder
90 // - max_payload_size : The maximum size each payload is allowed
91 // to have. Usually MTU - overhead.
92 //
93 // Return value : Set bit rate if OK
94 // <0 - Errors:
95 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
96 // WEBRTC_VIDEO_CODEC_ERR_SIZE
97 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
98 // WEBRTC_VIDEO_CODEC_MEMORY
99 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000100 virtual int32_t InitEncode(const VideoCodec* codec_settings,
101 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000102 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000103
104 // Register an encode complete callback object.
105 //
106 // Input:
107 // - callback : Callback object which handles encoded images.
108 //
109 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000110 virtual int32_t RegisterEncodeCompleteCallback(
111 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000112
113 // Free encoder memory.
114 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000115 virtual int32_t Release() = 0;
116
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000117 // Encode an I420 image (as a part of a video stream). The encoded image
118 // will be returned to the user through the encode complete callback.
119 //
120 // Input:
121 // - frame : Image to be encoded
122 // - frame_types : Frame type to be generated by the encoder.
123 //
124 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
125 // <0 - Errors:
126 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
127 // WEBRTC_VIDEO_CODEC_MEMORY
128 // WEBRTC_VIDEO_CODEC_ERROR
129 // WEBRTC_VIDEO_CODEC_TIMEOUT
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700130 virtual int32_t Encode(const VideoFrame& frame,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000131 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700132 const std::vector<FrameType>* frame_types) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000133
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000134 // Inform the encoder of the new packet loss rate and the round-trip time of
135 // the network.
136 //
137 // Input:
138 // - packet_loss : Fraction lost
139 // (loss rate in percent = 100 * packetLoss / 255)
140 // - rtt : Round-trip time in milliseconds
141 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
142 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000143 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000144
145 // Inform the encoder about the new target bit rate.
146 //
147 // Input:
148 // - bitrate : New target bit rate
149 // - framerate : The target frame rate
150 //
151 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
sprang647bf432016-11-10 06:46:20 -0800152 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) {
153 RTC_NOTREACHED() << "SetRate(uint32_t, uint32_t) is deprecated.";
154 return -1;
155 }
156
157 // Default fallback: Just use the sum of bitrates as the single target rate.
158 // TODO(sprang): Remove this default implementation when we remove SetRates().
159 virtual int32_t SetRateAllocation(const BitrateAllocation& allocation,
160 uint32_t framerate) {
161 return SetRates(allocation.get_sum_kbps(), framerate);
162 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000163
164 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200165 virtual void OnDroppedFrame() {}
166 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100167 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000168};
169
Peter Boström4d71ede2015-05-19 23:09:35 +0200170// Class used to wrap external VideoEncoders to provide a fallback option on
171// software encoding when a hardware encoder fails to encode a stream due to
172// hardware restrictions, such as max resolution.
173class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
174 public:
175 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
176 webrtc::VideoEncoder* encoder);
177
178 int32_t InitEncode(const VideoCodec* codec_settings,
179 int32_t number_of_cores,
180 size_t max_payload_size) override;
181
182 int32_t RegisterEncodeCompleteCallback(
183 EncodedImageCallback* callback) override;
184
185 int32_t Release() override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700186 int32_t Encode(const VideoFrame& frame,
Peter Boström4d71ede2015-05-19 23:09:35 +0200187 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700188 const std::vector<FrameType>* frame_types) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200189 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
sprang647bf432016-11-10 06:46:20 -0800190 int32_t SetRateAllocation(const BitrateAllocation& bitrate_allocation,
191 uint32_t framerate) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200192 void OnDroppedFrame() override;
Peter Boströmeb66e802015-06-05 11:08:03 +0200193 bool SupportsNativeHandle() const override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200194
195 private:
noahricb1ce6632015-10-21 23:54:51 -0700196 bool InitFallbackEncoder();
197
198 // Settings used in the last InitEncode call and used if a dynamic fallback to
199 // software is required.
200 VideoCodec codec_settings_;
201 int32_t number_of_cores_;
202 size_t max_payload_size_;
203
204 // The last bitrate/framerate set, and a flag for noting they are set.
205 bool rates_set_;
sprang647bf432016-11-10 06:46:20 -0800206 BitrateAllocation bitrate_allocation_;
noahricb1ce6632015-10-21 23:54:51 -0700207 uint32_t framerate_;
208
209 // The last channel parameters set, and a flag for noting they are set.
210 bool channel_parameters_set_;
211 uint32_t packet_loss_;
212 int64_t rtt_;
213
Peter Boström4d71ede2015-05-19 23:09:35 +0200214 const EncoderType encoder_type_;
215 webrtc::VideoEncoder* const encoder_;
216
kwibergc891eb42016-03-02 03:41:34 -0800217 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100218 std::string fallback_implementation_name_;
Peter Boström4d71ede2015-05-19 23:09:35 +0200219 EncodedImageCallback* callback_;
220};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000221} // namespace webrtc
222#endif // WEBRTC_VIDEO_ENCODER_H_