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 11 #ifndef WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_ 12 #define WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_ 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 24 #include "webrtc/modules/include/module.h" 25 #include "webrtc/modules/include/module_common_types.h" 26 #include "webrtc/modules/video_coding/include/video_coding_defines.h" 27 #include "webrtc/system_wrappers/include/event_wrapper.h" 28 #include "webrtc/video_frame.h" 29 30 namespace webrtc { 31 32 class Clock; 33 class EncodedImageCallback; 34 class VideoEncoder; 35 class VideoDecoder; 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 49 virtual EventWrapper* CreateEvent() { return EventWrapper::Create(); } 50 }; 51 52 // Used to indicate which decode with errors mode should be used. 53 enum VCMDecodeErrorMode { 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. 63 }; 64 65 class VideoCodingModule : public Module { 66 public: 67 enum SenderNackMode { kNackNone, kNackAll, kNackSelective }; 68 69 enum ReceiverRobustness { kNone, kHardNack, kSoftNack, kReferenceSelection }; 70 71 static VideoCodingModule* Create( 72 Clock* clock, 73 VideoEncoderRateObserver* encoder_rate_observer, 74 VCMQMSettingsCallback* qm_settings_callback); 75 76 static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory); 77 78 static void Destroy(VideoCodingModule* module); 79 80 // Get supported codec settings using codec type 81 // 82 // Input: 83 // - codecType : The codec type to get settings for 84 // - codec : Memory where the codec settings will be stored 85 // 86 // Return value : VCM_OK, on success 87 // VCM_PARAMETER_ERROR if codec not supported 88 static void Codec(VideoCodecType codecType, VideoCodec* codec); 89 90 /* 91 * Sender 92 */ 93 94 // Registers a codec to be used for encoding. Calling this 95 // API multiple times overwrites any previously registered codecs. 96 // 97 // NOTE: Must be called on the thread that constructed the VCM instance. 98 // 99 // Input: 100 // - sendCodec : Settings for the codec to be registered. 101 // - numberOfCores : The number of cores the codec is allowed 102 // to use. 103 // - maxPayloadSize : The maximum size each payload is allowed 104 // to have. Usually MTU - overhead. 105 // 106 // Return value : VCM_OK, on success. 107 // < 0, on error. 108 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec, 109 uint32_t numberOfCores, 110 uint32_t maxPayloadSize) = 0; 111 112 // Register an external encoder object. This can not be used together with 113 // external decoder callbacks. 114 // 115 // Input: 116 // - externalEncoder : Encoder object to be used for encoding frames 117 // inserted 118 // with the AddVideoFrame API. 119 // - payloadType : The payload type bound which this encoder is bound 120 // to. 121 // 122 // Return value : VCM_OK, on success. 123 // < 0, on error. 124 // TODO(pbos): Remove return type when unused elsewhere. 125 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder, 126 uint8_t payloadType, 127 bool internalSource = false) = 0; 128 129 // API to get currently configured encoder target bitrate in bits/s. 130 // 131 // Return value : 0, on success. 132 // < 0, on error. 133 virtual int Bitrate(unsigned int* bitrate) const = 0; 134 135 // API to get currently configured encoder target frame rate. 136 // 137 // Return value : 0, on success. 138 // < 0, on error. 139 virtual int FrameRate(unsigned int* framerate) const = 0; 140 141 // Sets the parameters describing the send channel. These parameters are 142 // inputs to the 143 // Media Optimization inside the VCM and also specifies the target bit rate 144 // for the 145 // encoder. Bit rate used by NACK should already be compensated for by the 146 // user. 147 // 148 // Input: 149 // - target_bitrate : The target bitrate for VCM in bits/s. 150 // - lossRate : Fractions of lost packets the past second. 151 // (loss rate in percent = 100 * packetLoss / 152 // 255) 153 // - rtt : Current round-trip time in ms. 154 // 155 // Return value : VCM_OK, on success. 156 // < 0, on error. 157 virtual int32_t SetChannelParameters(uint32_t target_bitrate, 158 uint8_t lossRate, 159 int64_t rtt) = 0; 160 161 // Sets the parameters describing the receive channel. These parameters are 162 // inputs to the 163 // Media Optimization inside the VCM. 164 // 165 // Input: 166 // - rtt : Current round-trip time in ms. 167 // with the most amount available bandwidth in 168 // a conference 169 // scenario 170 // 171 // Return value : VCM_OK, on success. 172 // < 0, on error. 173 virtual int32_t SetReceiveChannelParameters(int64_t rtt) = 0; 174 175 // Register a transport callback which will be called to deliver the encoded 176 // data and 177 // side information. 178 // 179 // Input: 180 // - transport : The callback object to register. 181 // 182 // Return value : VCM_OK, on success. 183 // < 0, on error. 184 virtual int32_t RegisterTransportCallback( 185 VCMPacketizationCallback* transport) = 0; 186 187 // Register video output information callback which will be called to deliver 188 // information 189 // about the video stream produced by the encoder, for instance the average 190 // frame rate and 191 // bit rate. 192 // 193 // Input: 194 // - outputInformation : The callback object to register. 195 // 196 // Return value : VCM_OK, on success. 197 // < 0, on error. 198 virtual int32_t RegisterSendStatisticsCallback( 199 VCMSendStatisticsCallback* sendStats) = 0; 200 201 // Register a video protection callback which will be called to deliver 202 // the requested FEC rate and NACK status (on/off). 203 // 204 // Input: 205 // - protection : The callback object to register. 206 // 207 // Return value : VCM_OK, on success. 208 // < 0, on error. 209 virtual int32_t RegisterProtectionCallback( 210 VCMProtectionCallback* protection) = 0; 211 212 // Enable or disable a video protection method. 213 // 214 // Input: 215 // - videoProtection : The method to enable or disable. 216 // - enable : True if the method should be enabled, false if 217 // it should be disabled. 218 // 219 // Return value : VCM_OK, on success. 220 // < 0, on error. 221 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection, 222 bool enable) = 0; 223 224 // Add one raw video frame to the encoder. This function does all the 225 // necessary 226 // processing, then decides what frame type to encode, or if the frame should 227 // be 228 // dropped. If the frame should be encoded it passes the frame to the encoder 229 // before it returns. 230 // 231 // Input: 232 // - videoFrame : Video frame to encode. 233 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed 234 // in-band signaling. 235 // 236 // Return value : VCM_OK, on success. 237 // < 0, on error. 238 virtual int32_t AddVideoFrame( 239 const VideoFrame& videoFrame, 240 const VideoContentMetrics* contentMetrics = NULL, 241 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0; 242 243 // Next frame encoded should be an intra frame (keyframe). 244 // 245 // Return value : VCM_OK, on success. 246 // < 0, on error. 247 virtual int32_t IntraFrameRequest(int stream_index) = 0; 248 249 // Frame Dropper enable. Can be used to disable the frame dropping when the 250 // encoder 251 // over-uses its bit rate. This API is designed to be used when the encoded 252 // frames 253 // are supposed to be stored to an AVI file, or when the I420 codec is used 254 // and the 255 // target bit rate shouldn't affect the frame rate. 256 // 257 // Input: 258 // - enable : True to enable the setting, false to disable it. 259 // 260 // Return value : VCM_OK, on success. 261 // < 0, on error. 262 virtual int32_t EnableFrameDropper(bool enable) = 0; 263 264 /* 265 * Receiver 266 */ 267 268 // Register possible receive codecs, can be called multiple times for 269 // different codecs. 270 // The module will automatically switch between registered codecs depending on 271 // the 272 // payload type of incoming frames. The actual decoder will be created when 273 // needed. 274 // 275 // Input: 276 // - receiveCodec : Settings for the codec to be registered. 277 // - numberOfCores : Number of CPU cores that the decoder is allowed 278 // to use. 279 // - requireKeyFrame : Set this to true if you don't want any delta 280 // frames 281 // to be decoded until the first key frame has been 282 // decoded. 283 // 284 // Return value : VCM_OK, on success. 285 // < 0, on error. 286 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec, 287 int32_t numberOfCores, 288 bool requireKeyFrame = false) = 0; 289 290 // Register an externally defined decoder/renderer object. Can be a decoder 291 // only or a 292 // decoder coupled with a renderer. Note that RegisterReceiveCodec must be 293 // called to 294 // be used for decoding incoming streams. 295 // 296 // Input: 297 // - externalDecoder : The external decoder/renderer object. 298 // - payloadType : The payload type which this decoder should 299 // be 300 // registered to. 301 // 302 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder, 303 uint8_t payloadType) = 0; 304 305 // Register a receive callback. Will be called whenever there is a new frame 306 // ready 307 // for rendering. 308 // 309 // Input: 310 // - receiveCallback : The callback object to be used by the 311 // module when a 312 // frame is ready for rendering. 313 // De-register with a NULL pointer. 314 // 315 // Return value : VCM_OK, on success. 316 // < 0, on error. 317 virtual int32_t RegisterReceiveCallback( 318 VCMReceiveCallback* receiveCallback) = 0; 319 320 // Register a receive statistics callback which will be called to deliver 321 // information 322 // about the video stream received by the receiving side of the VCM, for 323 // instance the 324 // average frame rate and bit rate. 325 // 326 // Input: 327 // - receiveStats : The callback object to register. 328 // 329 // Return value : VCM_OK, on success. 330 // < 0, on error. 331 virtual int32_t RegisterReceiveStatisticsCallback( 332 VCMReceiveStatisticsCallback* receiveStats) = 0; 333 334 // Register a decoder timing callback which will be called to deliver 335 // information about the timing of the decoder in the receiving side of the 336 // VCM, for instance the current and maximum frame decode latency. 337 // 338 // Input: 339 // - decoderTiming : The callback object to register. 340 // 341 // Return value : VCM_OK, on success. 342 // < 0, on error. 343 virtual int32_t RegisterDecoderTimingCallback( 344 VCMDecoderTimingCallback* decoderTiming) = 0; 345 346 // Register a frame type request callback. This callback will be called when 347 // the 348 // module needs to request specific frame types from the send side. 349 // 350 // Input: 351 // - frameTypeCallback : The callback object to be used by the 352 // module when 353 // requesting a specific type of frame from 354 // the send side. 355 // De-register with a NULL pointer. 356 // 357 // Return value : VCM_OK, on success. 358 // < 0, on error. 359 virtual int32_t RegisterFrameTypeCallback( 360 VCMFrameTypeCallback* frameTypeCallback) = 0; 361 362 // Registers a callback which is called whenever the receive side of the VCM 363 // encounters holes in the packet sequence and needs packets to be 364 // retransmitted. 365 // 366 // Input: 367 // - callback : The callback to be registered in the VCM. 368 // 369 // Return value : VCM_OK, on success. 370 // <0, on error. 371 virtual int32_t RegisterPacketRequestCallback( 372 VCMPacketRequestCallback* callback) = 0; 373 374 // Waits for the next frame in the jitter buffer to become complete 375 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for 376 // decoding. 377 // Should be called as often as possible to get the most out of the decoder. 378 // 379 // Return value : VCM_OK, on success. 380 // < 0, on error. 381 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0; 382 383 // Registers a callback which conveys the size of the render buffer. 384 virtual int RegisterRenderBufferSizeCallback( 385 VCMRenderBufferSizeCallback* callback) = 0; 386 387 // Reset the decoder state to the initial state. 388 // 389 // Return value : VCM_OK, on success. 390 // < 0, on error. 391 virtual int32_t ResetDecoder() = 0; 392 393 // API to get the codec which is currently used for decoding by the module. 394 // 395 // Input: 396 // - currentReceiveCodec : Settings for the codec to be registered. 397 // 398 // Return value : VCM_OK, on success. 399 // < 0, on error. 400 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const = 0; 401 402 // API to get the codec type currently used for decoding by the module. 403 // 404 // Return value : codecy type, on success. 405 // kVideoCodecUnknown, on error or if no receive codec is 406 // registered 407 virtual VideoCodecType ReceiveCodec() const = 0; 408 409 // Insert a parsed packet into the receiver side of the module. Will be placed 410 // in the 411 // jitter buffer waiting for the frame to become complete. Returns as soon as 412 // the packet 413 // has been placed in the jitter buffer. 414 // 415 // Input: 416 // - incomingPayload : Payload of the packet. 417 // - payloadLength : Length of the payload. 418 // - rtpInfo : The parsed header. 419 // 420 // Return value : VCM_OK, on success. 421 // < 0, on error. 422 virtual int32_t IncomingPacket(const uint8_t* incomingPayload, 423 size_t payloadLength, 424 const WebRtcRTPHeader& rtpInfo) = 0; 425 426 // Minimum playout delay (Used for lip-sync). This is the minimum delay 427 // required 428 // to sync with audio. Not included in VideoCodingModule::Delay() 429 // Defaults to 0 ms. 430 // 431 // Input: 432 // - minPlayoutDelayMs : Additional delay in ms. 433 // 434 // Return value : VCM_OK, on success. 435 // < 0, on error. 436 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0; 437 438 // Set the time required by the renderer to render a frame. 439 // 440 // Input: 441 // - timeMS : The time in ms required by the renderer to render a 442 // frame. 443 // 444 // Return value : VCM_OK, on success. 445 // < 0, on error. 446 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0; 447 448 // The total delay desired by the VCM. Can be less than the minimum 449 // delay set with SetMinimumPlayoutDelay. 450 // 451 // Return value : Total delay in ms, on success. 452 // < 0, on error. 453 virtual int32_t Delay() const = 0; 454 455 // Returns the number of packets discarded by the jitter buffer due to being 456 // too late. This can include duplicated packets which arrived after the 457 // frame was sent to the decoder. Therefore packets which were prematurely 458 // NACKed will be counted. 459 virtual uint32_t DiscardedPackets() const = 0; 460 461 // Robustness APIs 462 463 // Set the receiver robustness mode. The mode decides how the receiver 464 // responds to losses in the stream. The type of counter-measure (soft or 465 // hard NACK, dual decoder, RPS, etc.) is selected through the 466 // robustnessMode parameter. The errorMode parameter decides if it is 467 // allowed to display frames corrupted by losses. Note that not all 468 // combinations of the two parameters are feasible. An error will be 469 // returned for invalid combinations. 470 // Input: 471 // - robustnessMode : selected robustness mode. 472 // - errorMode : selected error mode. 473 // 474 // Return value : VCM_OK, on success; 475 // < 0, on error. 476 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode, 477 VCMDecodeErrorMode errorMode) = 0; 478 479 // Set the decode error mode. The mode decides which errors (if any) are 480 // allowed in decodable frames. Note that setting decode_error_mode to 481 // anything other than kWithErrors without enabling nack will cause 482 // long-term freezes (resulting from frequent key frame requests) if 483 // packet loss occurs. 484 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0; 485 486 // Sets the maximum number of sequence numbers that we are allowed to NACK 487 // and the oldest sequence number that we will consider to NACK. If a 488 // sequence number older than |max_packet_age_to_nack| is missing 489 // a key frame will be requested. A key frame will also be requested if the 490 // time of incomplete or non-continuous frames in the jitter buffer is above 491 // |max_incomplete_time_ms|. 492 virtual void SetNackSettings(size_t max_nack_list_size, 493 int max_packet_age_to_nack, 494 int max_incomplete_time_ms) = 0; 495 496 // Setting a desired delay to the VCM receiver. Video rendering will be 497 // delayed by at least desired_delay_ms. 498 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0; 499 500 // Lets the sender suspend video when the rate drops below 501 // |threshold_bps|, and turns back on when the rate goes back up above 502 // |threshold_bps| + |window_bps|. 503 virtual void SuspendBelowMinBitrate() = 0; 504 505 // Returns true if SuspendBelowMinBitrate is engaged and the video has been 506 // suspended due to bandwidth limitations; otherwise false. 507 virtual bool VideoSuspended() const = 0; 508 509 virtual void RegisterPreDecodeImageCallback( 510 EncodedImageCallback* observer) = 0; 511 virtual void RegisterPostEncodeImageCallback( 512 EncodedImageCallback* post_encode_callback) = 0; 513 // Releases pending decode calls, permitting faster thread shutdown. 514 virtual void TriggerDecoderShutdown() = 0; 515 }; 516 517 } // namespace webrtc 518 519 #endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_ 520