Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_ |
| 12 | #define MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_ |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 13 | |
| 14 | #if defined(WEBRTC_WIN) |
| 15 | // This is a workaround on Windows due to the fact that some Windows |
| 16 | // headers define CreateEvent as a macro to either CreateEventW or CreateEventA. |
| 17 | // This can cause problems since we use that name as well and could |
| 18 | // declare them as one thing here whereas in another place a windows header |
| 19 | // may have been included and then implementing CreateEvent() causes compilation |
| 20 | // errors. So for consistency, we include the main windows header here. |
| 21 | #include <windows.h> |
| 22 | #endif |
| 23 | |
Ying Wang | 3b790f3 | 2018-01-19 17:58:57 +0100 | [diff] [blame] | 24 | #include "api/fec_controller.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "api/video/video_frame.h" |
| 26 | #include "modules/include/module.h" |
| 27 | #include "modules/include/module_common_types.h" |
| 28 | #include "modules/video_coding/include/video_coding_defines.h" |
| 29 | #include "system_wrappers/include/event_wrapper.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 30 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 31 | namespace webrtc { |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 32 | |
| 33 | class Clock; |
| 34 | class EncodedImageCallback; |
| 35 | class VideoEncoder; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 36 | struct CodecSpecificInfo; |
| 37 | |
| 38 | class EventFactory { |
| 39 | public: |
| 40 | virtual ~EventFactory() {} |
| 41 | |
| 42 | virtual EventWrapper* CreateEvent() = 0; |
| 43 | }; |
| 44 | |
| 45 | class EventFactoryImpl : public EventFactory { |
| 46 | public: |
| 47 | virtual ~EventFactoryImpl() {} |
| 48 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 49 | virtual EventWrapper* CreateEvent() { return EventWrapper::Create(); } |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | // Used to indicate which decode with errors mode should be used. |
| 53 | enum VCMDecodeErrorMode { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 54 | kNoErrors, // Never decode with errors. Video will freeze |
| 55 | // if nack is disabled. |
| 56 | kSelectiveErrors, // Frames that are determined decodable in |
| 57 | // VCMSessionInfo may be decoded with missing |
| 58 | // packets. As not all incomplete frames will be |
| 59 | // decodable, video will freeze if nack is disabled. |
| 60 | kWithErrors // Release frames as needed. Errors may be |
| 61 | // introduced as some encoded frames may not be |
| 62 | // complete. |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 65 | class VideoCodingModule : public Module { |
| 66 | public: |
| 67 | enum SenderNackMode { kNackNone, kNackAll, kNackSelective }; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 68 | |
tommi | a5c18d7 | 2017-03-20 10:43:23 -0700 | [diff] [blame] | 69 | // DEPRECATED. |
perkj | f5b2e51 | 2016-07-05 08:34:04 -0700 | [diff] [blame] | 70 | static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory); |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 71 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 72 | /* |
| 73 | * Sender |
| 74 | */ |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 75 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 76 | // Registers a codec to be used for encoding. Calling this |
| 77 | // API multiple times overwrites any previously registered codecs. |
| 78 | // |
| 79 | // NOTE: Must be called on the thread that constructed the VCM instance. |
| 80 | // |
| 81 | // Input: |
| 82 | // - sendCodec : Settings for the codec to be registered. |
| 83 | // - numberOfCores : The number of cores the codec is allowed |
| 84 | // to use. |
| 85 | // - maxPayloadSize : The maximum size each payload is allowed |
| 86 | // to have. Usually MTU - overhead. |
| 87 | // |
| 88 | // Return value : VCM_OK, on success. |
| 89 | // < 0, on error. |
| 90 | virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec, |
| 91 | uint32_t numberOfCores, |
| 92 | uint32_t maxPayloadSize) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 93 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 94 | // Register an external encoder object. This can not be used together with |
| 95 | // external decoder callbacks. |
| 96 | // |
| 97 | // Input: |
| 98 | // - externalEncoder : Encoder object to be used for encoding frames |
| 99 | // inserted |
| 100 | // with the AddVideoFrame API. |
| 101 | // - payloadType : The payload type bound which this encoder is bound |
| 102 | // to. |
| 103 | // |
| 104 | // Return value : VCM_OK, on success. |
| 105 | // < 0, on error. |
| 106 | // TODO(pbos): Remove return type when unused elsewhere. |
| 107 | virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder, |
| 108 | uint8_t payloadType, |
| 109 | bool internalSource = false) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 110 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 111 | // Sets the parameters describing the send channel. These parameters are |
| 112 | // inputs to the |
| 113 | // Media Optimization inside the VCM and also specifies the target bit rate |
| 114 | // for the |
| 115 | // encoder. Bit rate used by NACK should already be compensated for by the |
| 116 | // user. |
| 117 | // |
| 118 | // Input: |
| 119 | // - target_bitrate : The target bitrate for VCM in bits/s. |
| 120 | // - lossRate : Fractions of lost packets the past second. |
| 121 | // (loss rate in percent = 100 * packetLoss / |
| 122 | // 255) |
| 123 | // - rtt : Current round-trip time in ms. |
| 124 | // |
| 125 | // Return value : VCM_OK, on success. |
| 126 | // < 0, on error. |
| 127 | virtual int32_t SetChannelParameters(uint32_t target_bitrate, |
| 128 | uint8_t lossRate, |
| 129 | int64_t rtt) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 130 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 131 | // Enable or disable a video protection method. |
| 132 | // |
| 133 | // Input: |
| 134 | // - videoProtection : The method to enable or disable. |
| 135 | // - enable : True if the method should be enabled, false if |
| 136 | // it should be disabled. |
| 137 | // |
| 138 | // Return value : VCM_OK, on success. |
| 139 | // < 0, on error. |
| 140 | virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection, |
| 141 | bool enable) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 142 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 143 | // Add one raw video frame to the encoder. This function does all the |
| 144 | // necessary |
| 145 | // processing, then decides what frame type to encode, or if the frame should |
| 146 | // be |
| 147 | // dropped. If the frame should be encoded it passes the frame to the encoder |
| 148 | // before it returns. |
| 149 | // |
| 150 | // Input: |
| 151 | // - videoFrame : Video frame to encode. |
| 152 | // - codecSpecificInfo : Extra codec information, e.g., pre-parsed |
| 153 | // in-band signaling. |
| 154 | // |
| 155 | // Return value : VCM_OK, on success. |
| 156 | // < 0, on error. |
| 157 | virtual int32_t AddVideoFrame( |
| 158 | const VideoFrame& videoFrame, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 159 | const CodecSpecificInfo* codecSpecificInfo = NULL) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 160 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 161 | // Next frame encoded should be an intra frame (keyframe). |
| 162 | // |
| 163 | // Return value : VCM_OK, on success. |
| 164 | // < 0, on error. |
perkj | 600246e | 2016-05-04 11:26:51 -0700 | [diff] [blame] | 165 | virtual int32_t IntraFrameRequest(size_t stream_index) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 166 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 167 | // Frame Dropper enable. Can be used to disable the frame dropping when the |
| 168 | // encoder |
| 169 | // over-uses its bit rate. This API is designed to be used when the encoded |
| 170 | // frames |
| 171 | // are supposed to be stored to an AVI file, or when the I420 codec is used |
| 172 | // and the |
| 173 | // target bit rate shouldn't affect the frame rate. |
| 174 | // |
| 175 | // Input: |
| 176 | // - enable : True to enable the setting, false to disable it. |
| 177 | // |
| 178 | // Return value : VCM_OK, on success. |
| 179 | // < 0, on error. |
| 180 | virtual int32_t EnableFrameDropper(bool enable) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 181 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 182 | /* |
| 183 | * Receiver |
| 184 | */ |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 185 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 186 | // Register possible receive codecs, can be called multiple times for |
| 187 | // different codecs. |
| 188 | // The module will automatically switch between registered codecs depending on |
| 189 | // the |
| 190 | // payload type of incoming frames. The actual decoder will be created when |
| 191 | // needed. |
| 192 | // |
| 193 | // Input: |
| 194 | // - receiveCodec : Settings for the codec to be registered. |
| 195 | // - numberOfCores : Number of CPU cores that the decoder is allowed |
| 196 | // to use. |
| 197 | // - requireKeyFrame : Set this to true if you don't want any delta |
| 198 | // frames |
| 199 | // to be decoded until the first key frame has been |
| 200 | // decoded. |
| 201 | // |
| 202 | // Return value : VCM_OK, on success. |
| 203 | // < 0, on error. |
| 204 | virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec, |
| 205 | int32_t numberOfCores, |
| 206 | bool requireKeyFrame = false) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 207 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 208 | // Register a receive callback. Will be called whenever there is a new frame |
| 209 | // ready |
| 210 | // for rendering. |
| 211 | // |
| 212 | // Input: |
| 213 | // - receiveCallback : The callback object to be used by the |
| 214 | // module when a |
| 215 | // frame is ready for rendering. |
| 216 | // De-register with a NULL pointer. |
| 217 | // |
| 218 | // Return value : VCM_OK, on success. |
| 219 | // < 0, on error. |
| 220 | virtual int32_t RegisterReceiveCallback( |
| 221 | VCMReceiveCallback* receiveCallback) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 222 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 223 | // Register a frame type request callback. This callback will be called when |
| 224 | // the |
| 225 | // module needs to request specific frame types from the send side. |
| 226 | // |
| 227 | // Input: |
| 228 | // - frameTypeCallback : The callback object to be used by the |
| 229 | // module when |
| 230 | // requesting a specific type of frame from |
| 231 | // the send side. |
| 232 | // De-register with a NULL pointer. |
| 233 | // |
| 234 | // Return value : VCM_OK, on success. |
| 235 | // < 0, on error. |
| 236 | virtual int32_t RegisterFrameTypeCallback( |
| 237 | VCMFrameTypeCallback* frameTypeCallback) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 238 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 239 | // Registers a callback which is called whenever the receive side of the VCM |
| 240 | // encounters holes in the packet sequence and needs packets to be |
| 241 | // retransmitted. |
| 242 | // |
| 243 | // Input: |
| 244 | // - callback : The callback to be registered in the VCM. |
| 245 | // |
| 246 | // Return value : VCM_OK, on success. |
| 247 | // <0, on error. |
| 248 | virtual int32_t RegisterPacketRequestCallback( |
| 249 | VCMPacketRequestCallback* callback) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 250 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 251 | // Waits for the next frame in the jitter buffer to become complete |
| 252 | // (waits no longer than maxWaitTimeMs), then passes it to the decoder for |
| 253 | // decoding. |
| 254 | // Should be called as often as possible to get the most out of the decoder. |
| 255 | // |
| 256 | // Return value : VCM_OK, on success. |
| 257 | // < 0, on error. |
| 258 | virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 259 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 260 | // Insert a parsed packet into the receiver side of the module. Will be placed |
| 261 | // in the |
| 262 | // jitter buffer waiting for the frame to become complete. Returns as soon as |
| 263 | // the packet |
| 264 | // has been placed in the jitter buffer. |
| 265 | // |
| 266 | // Input: |
| 267 | // - incomingPayload : Payload of the packet. |
| 268 | // - payloadLength : Length of the payload. |
| 269 | // - rtpInfo : The parsed header. |
| 270 | // |
| 271 | // Return value : VCM_OK, on success. |
| 272 | // < 0, on error. |
| 273 | virtual int32_t IncomingPacket(const uint8_t* incomingPayload, |
| 274 | size_t payloadLength, |
| 275 | const WebRtcRTPHeader& rtpInfo) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 276 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 277 | // Robustness APIs |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 278 | |
tommi | a5c18d7 | 2017-03-20 10:43:23 -0700 | [diff] [blame] | 279 | // DEPRECATED. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 280 | // Set the receiver robustness mode. The mode decides how the receiver |
tommi | a5c18d7 | 2017-03-20 10:43:23 -0700 | [diff] [blame] | 281 | // responds to losses in the stream. The type of counter-measure is selected |
| 282 | // through the robustnessMode parameter. The errorMode parameter decides if it |
| 283 | // is allowed to display frames corrupted by losses. Note that not all |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 284 | // combinations of the two parameters are feasible. An error will be |
| 285 | // returned for invalid combinations. |
| 286 | // Input: |
| 287 | // - robustnessMode : selected robustness mode. |
| 288 | // - errorMode : selected error mode. |
| 289 | // |
| 290 | // Return value : VCM_OK, on success; |
| 291 | // < 0, on error. |
tommi | a5c18d7 | 2017-03-20 10:43:23 -0700 | [diff] [blame] | 292 | enum ReceiverRobustness { kNone, kHardNack }; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 293 | virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode, |
| 294 | VCMDecodeErrorMode errorMode) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 295 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 296 | // Sets the maximum number of sequence numbers that we are allowed to NACK |
| 297 | // and the oldest sequence number that we will consider to NACK. If a |
| 298 | // sequence number older than |max_packet_age_to_nack| is missing |
| 299 | // a key frame will be requested. A key frame will also be requested if the |
| 300 | // time of incomplete or non-continuous frames in the jitter buffer is above |
| 301 | // |max_incomplete_time_ms|. |
| 302 | virtual void SetNackSettings(size_t max_nack_list_size, |
| 303 | int max_packet_age_to_nack, |
| 304 | int max_incomplete_time_ms) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 305 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 306 | virtual void RegisterPostEncodeImageCallback( |
| 307 | EncodedImageCallback* post_encode_callback) = 0; |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | } // namespace webrtc |
| 311 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 312 | #endif // MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_ |