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