ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [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_API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
| 12 | #define WEBRTC_API_VIDEO_CODECS_VIDEO_ENCODER_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
kwiberg | 5b18967 | 2017-09-05 15:43:13 | [diff] [blame] | 18 | #include "webrtc/api/optional.h" |
nisse | d1df7af | 2017-05-10 12:25:59 | [diff] [blame] | 19 | #include "webrtc/api/video/video_frame.h" |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 20 | #include "webrtc/common_types.h" |
nisse | a24bcce | 2017-05-15 09:42:11 | [diff] [blame] | 21 | #include "webrtc/common_video/include/video_frame.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 22 | #include "webrtc/rtc_base/checks.h" |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 23 | #include "webrtc/typedefs.h" |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | class RTPFragmentationHeader; |
| 28 | // TODO(pbos): Expose these through a public (root) header or change these APIs. |
| 29 | struct CodecSpecificInfo; |
| 30 | class VideoCodec; |
| 31 | |
| 32 | class EncodedImageCallback { |
| 33 | public: |
| 34 | virtual ~EncodedImageCallback() {} |
| 35 | |
| 36 | struct Result { |
| 37 | enum Error { |
| 38 | OK, |
| 39 | |
| 40 | // Failed to send the packet. |
| 41 | ERROR_SEND_FAILED, |
| 42 | }; |
| 43 | |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 44 | explicit Result(Error error) : error(error) {} |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 45 | Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {} |
| 46 | |
| 47 | Error error; |
| 48 | |
| 49 | // Frame ID assigned to the frame. The frame ID should be the same as the ID |
| 50 | // seen by the receiver for this frame. RTP timestamp of the frame is used |
| 51 | // as frame ID when RTP is used to send video. Must be used only when |
| 52 | // error=OK. |
| 53 | uint32_t frame_id = 0; |
| 54 | |
| 55 | // Tells the encoder that the next frame is should be dropped. |
| 56 | bool drop_next_frame = false; |
| 57 | }; |
| 58 | |
| 59 | // Callback function which is called when an image has been encoded. |
| 60 | virtual Result OnEncodedImage( |
| 61 | const EncodedImage& encoded_image, |
| 62 | const CodecSpecificInfo* codec_specific_info, |
| 63 | const RTPFragmentationHeader* fragmentation) = 0; |
| 64 | |
| 65 | virtual void OnDroppedFrame() {} |
| 66 | }; |
| 67 | |
| 68 | class VideoEncoder { |
| 69 | public: |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 70 | struct QpThresholds { |
| 71 | QpThresholds(int l, int h) : low(l), high(h) {} |
| 72 | QpThresholds() : low(-1), high(-1) {} |
| 73 | int low; |
| 74 | int high; |
| 75 | }; |
| 76 | struct ScalingSettings { |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 77 | ScalingSettings(bool on, int low, int high); |
asapersson | 7647da7 | 2017-08-17 15:58:54 | [diff] [blame] | 78 | ScalingSettings(bool on, int low, int high, int min_pixels); |
| 79 | ScalingSettings(bool on, int min_pixels); |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 80 | explicit ScalingSettings(bool on); |
| 81 | ScalingSettings(const ScalingSettings&); |
| 82 | ~ScalingSettings(); |
| 83 | |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 84 | const bool enabled; |
| 85 | const rtc::Optional<QpThresholds> thresholds; |
asapersson | 7647da7 | 2017-08-17 15:58:54 | [diff] [blame] | 86 | |
| 87 | // We will never ask for a resolution lower than this. |
| 88 | // TODO(kthelgason): Lower this limit when better testing |
| 89 | // on MediaCodec and fallback implementations are in place. |
| 90 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
| 91 | const int min_pixels_per_frame = 320 * 180; |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 92 | }; |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 93 | |
| 94 | static VideoCodecVP8 GetDefaultVp8Settings(); |
| 95 | static VideoCodecVP9 GetDefaultVp9Settings(); |
| 96 | static VideoCodecH264 GetDefaultH264Settings(); |
| 97 | |
| 98 | virtual ~VideoEncoder() {} |
| 99 | |
| 100 | // Initialize the encoder with the information from the codecSettings |
| 101 | // |
| 102 | // Input: |
| 103 | // - codec_settings : Codec settings |
| 104 | // - number_of_cores : Number of cores available for the encoder |
| 105 | // - max_payload_size : The maximum size each payload is allowed |
| 106 | // to have. Usually MTU - overhead. |
| 107 | // |
| 108 | // Return value : Set bit rate if OK |
| 109 | // <0 - Errors: |
| 110 | // WEBRTC_VIDEO_CODEC_ERR_PARAMETER |
| 111 | // WEBRTC_VIDEO_CODEC_ERR_SIZE |
| 112 | // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED |
| 113 | // WEBRTC_VIDEO_CODEC_MEMORY |
| 114 | // WEBRTC_VIDEO_CODEC_ERROR |
| 115 | virtual int32_t InitEncode(const VideoCodec* codec_settings, |
| 116 | int32_t number_of_cores, |
| 117 | size_t max_payload_size) = 0; |
| 118 | |
| 119 | // Register an encode complete callback object. |
| 120 | // |
| 121 | // Input: |
| 122 | // - callback : Callback object which handles encoded images. |
| 123 | // |
| 124 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
| 125 | virtual int32_t RegisterEncodeCompleteCallback( |
| 126 | EncodedImageCallback* callback) = 0; |
| 127 | |
| 128 | // Free encoder memory. |
| 129 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
| 130 | virtual int32_t Release() = 0; |
| 131 | |
| 132 | // Encode an I420 image (as a part of a video stream). The encoded image |
| 133 | // will be returned to the user through the encode complete callback. |
| 134 | // |
| 135 | // Input: |
| 136 | // - frame : Image to be encoded |
| 137 | // - frame_types : Frame type to be generated by the encoder. |
| 138 | // |
| 139 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK |
| 140 | // <0 - Errors: |
| 141 | // WEBRTC_VIDEO_CODEC_ERR_PARAMETER |
| 142 | // WEBRTC_VIDEO_CODEC_MEMORY |
| 143 | // WEBRTC_VIDEO_CODEC_ERROR |
| 144 | // WEBRTC_VIDEO_CODEC_TIMEOUT |
| 145 | virtual int32_t Encode(const VideoFrame& frame, |
| 146 | const CodecSpecificInfo* codec_specific_info, |
| 147 | const std::vector<FrameType>* frame_types) = 0; |
| 148 | |
| 149 | // Inform the encoder of the new packet loss rate and the round-trip time of |
| 150 | // the network. |
| 151 | // |
| 152 | // Input: |
| 153 | // - packet_loss : Fraction lost |
| 154 | // (loss rate in percent = 100 * packetLoss / 255) |
| 155 | // - rtt : Round-trip time in milliseconds |
| 156 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK |
| 157 | // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR |
| 158 | virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0; |
| 159 | |
| 160 | // Inform the encoder about the new target bit rate. |
| 161 | // |
| 162 | // Input: |
| 163 | // - bitrate : New target bit rate |
| 164 | // - framerate : The target frame rate |
| 165 | // |
| 166 | // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise. |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 167 | virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate); |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 168 | |
| 169 | // Default fallback: Just use the sum of bitrates as the single target rate. |
| 170 | // TODO(sprang): Remove this default implementation when we remove SetRates(). |
| 171 | virtual int32_t SetRateAllocation(const BitrateAllocation& allocation, |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 172 | uint32_t framerate); |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 173 | |
| 174 | // Any encoder implementation wishing to use the WebRTC provided |
| 175 | // quality scaler must implement this method. |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 176 | virtual ScalingSettings GetScalingSettings() const; |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 177 | |
mflodman | 109fa97 | 2017-08-10 09:43:14 | [diff] [blame] | 178 | virtual int32_t SetPeriodicKeyFrames(bool enable); |
| 179 | virtual bool SupportsNativeHandle() const; |
| 180 | virtual const char* ImplementationName() const; |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 181 | }; |
ilnik | 9b1cf9e | 2017-04-05 10:02:20 | [diff] [blame] | 182 | } // namespace webrtc |
| 183 | #endif // WEBRTC_API_VIDEO_CODECS_VIDEO_ENCODER_H_ |