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