pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 14 | #include <memory> |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 15 | #include <string> |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 18 | #include "webrtc/common_types.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
| 20 | #include "webrtc/video_frame.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class RTPFragmentationHeader; |
| 25 | // TODO(pbos): Expose these through a public (root) header or change these APIs. |
| 26 | struct CodecSpecificInfo; |
hta | 257dc39 | 2016-10-25 09:05:06 -0700 | [diff] [blame] | 27 | class VideoCodec; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 28 | |
| 29 | class EncodedImageCallback { |
| 30 | public: |
| 31 | virtual ~EncodedImageCallback() {} |
| 32 | |
Sergey Ulanov | 525df3f | 2016-08-02 17:46:41 -0700 | [diff] [blame] | 33 | 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 56 | // Callback function which is called when an image has been encoded. |
kjellander | 91b957d | 2016-11-03 11:53:43 -0700 | [diff] [blame] | 57 | 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | class VideoEncoder { |
| 77 | public: |
| 78 | enum EncoderType { |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 79 | kH264, |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 80 | kVp8, |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 81 | kVp9, |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 82 | kUnsupportedCodec, |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static VideoEncoder* Create(EncoderType codec_type); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 86 | // 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 90 | |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 91 | static VideoCodecVP8 GetDefaultVp8Settings(); |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 92 | static VideoCodecVP9 GetDefaultVp9Settings(); |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 93 | static VideoCodecH264 GetDefaultH264Settings(); |
| 94 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 95 | virtual ~VideoEncoder() {} |
| 96 | |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 97 | // 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 112 | virtual int32_t InitEncode(const VideoCodec* codec_settings, |
| 113 | int32_t number_of_cores, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 114 | size_t max_payload_size) = 0; |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 115 | |
| 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 122 | virtual int32_t RegisterEncodeCompleteCallback( |
| 123 | EncodedImageCallback* callback) = 0; |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 124 | |
| 125 | // Free encoder memory. |
| 126 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 127 | virtual int32_t Release() = 0; |
| 128 | |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 129 | // 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-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 142 | virtual int32_t Encode(const VideoFrame& frame, |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 143 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 144 | const std::vector<FrameType>* frame_types) = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 145 | |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 146 | // 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.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 155 | virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0; |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 156 | |
| 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 164 | virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0; |
| 165 | |
| 166 | virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; } |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 167 | virtual void OnDroppedFrame() {} |
| 168 | virtual bool SupportsNativeHandle() const { return false; } |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 169 | virtual const char* ImplementationName() const { return "unknown"; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 172 | // 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. |
| 175 | class 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-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 188 | int32_t Encode(const VideoFrame& frame, |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 189 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 190 | const std::vector<FrameType>* frame_types) override; |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 191 | 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öm | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 195 | bool SupportsNativeHandle() const override; |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 196 | |
| 197 | private: |
noahric | b1ce663 | 2015-10-21 23:54:51 -0700 | [diff] [blame] | 198 | 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öm | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 216 | const EncoderType encoder_type_; |
| 217 | webrtc::VideoEncoder* const encoder_; |
| 218 | |
kwiberg | c891eb4 | 2016-03-02 03:41:34 -0800 | [diff] [blame] | 219 | std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_; |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 220 | std::string fallback_implementation_name_; |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 221 | EncodedImageCallback* callback_; |
| 222 | }; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 223 | } // namespace webrtc |
| 224 | #endif // WEBRTC_VIDEO_ENCODER_H_ |