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. |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 57 | virtual Result OnEncodedImage( |
| 58 | const EncodedImage& encoded_image, |
| 59 | const CodecSpecificInfo* codec_specific_info, |
| 60 | const RTPFragmentationHeader* fragmentation) = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | class VideoEncoder { |
| 64 | public: |
| 65 | enum EncoderType { |
Zeke Chin | 71f6f44 | 2015-06-29 14:34:58 -0700 | [diff] [blame] | 66 | kH264, |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 67 | kVp8, |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 68 | kVp9, |
Peter Boström | 4d71ede | 2015-05-19 23:09:35 +0200 | [diff] [blame] | 69 | kUnsupportedCodec, |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static VideoEncoder* Create(EncoderType codec_type); |
sakal | 327e9d0 | 2016-10-06 05:55:11 -0700 | [diff] [blame] | 73 | // 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 77 | |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 78 | static VideoCodecVP8 GetDefaultVp8Settings(); |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 79 | static VideoCodecVP9 GetDefaultVp9Settings(); |
pbos@webrtc.org | 6cd6ba8 | 2014-09-18 12:42:28 +0000 | [diff] [blame] | 80 | static VideoCodecH264 GetDefaultH264Settings(); |
| 81 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 82 | virtual ~VideoEncoder() {} |
| 83 | |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 84 | // 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 99 | virtual int32_t InitEncode(const VideoCodec* codec_settings, |
| 100 | int32_t number_of_cores, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 101 | size_t max_payload_size) = 0; |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 102 | |
| 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 109 | virtual int32_t RegisterEncodeCompleteCallback( |
| 110 | EncodedImageCallback* callback) = 0; |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 111 | |
| 112 | // Free encoder memory. |
| 113 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 114 | virtual int32_t Release() = 0; |
| 115 | |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 116 | // 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-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 129 | virtual int32_t Encode(const VideoFrame& frame, |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 130 | const CodecSpecificInfo* codec_specific_info, |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 131 | const std::vector<FrameType>* frame_types) = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 132 | |
marpan@webrtc.org | 5b88317 | 2014-11-01 06:10:48 +0000 | [diff] [blame] | 133 | // 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.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 142 | 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] | 143 | |
| 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. |
sprang | 1369c83 | 2016-11-10 08:30:33 -0800 | [diff] [blame] | 151 | virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 152 | |
| 153 | virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; } |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 154 | virtual void OnDroppedFrame() {} |
| 155 | virtual bool SupportsNativeHandle() const { return false; } |
Peter Boström | b7d9a97 | 2015-12-18 16:01:11 +0100 | [diff] [blame] | 156 | virtual const char* ImplementationName() const { return "unknown"; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 159 | } // namespace webrtc |
| 160 | #endif // WEBRTC_VIDEO_ENCODER_H_ |