blob: 0f8567963a06c837d2f111b84e8bfc0e5d80df5f [file] [log] [blame]
Henrik Kjellander2557b862015-11-18 22:00:21 +01001/*
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
philipel9d3ab612015-12-21 04:12:39 -080030namespace webrtc {
Henrik Kjellander2557b862015-11-18 22:00:21 +010031
32class Clock;
33class EncodedImageCallback;
Peter Boströmad6fc5a2016-05-12 03:01:31 +020034// TODO(pbos): Remove VCMQMSettingsCallback completely. This might be done by
35// removing the VCM and use VideoSender/VideoReceiver as a public interface
36// directly.
37class VCMQMSettingsCallback;
Henrik Kjellander2557b862015-11-18 22:00:21 +010038class VideoEncoder;
39class VideoDecoder;
40struct CodecSpecificInfo;
41
42class EventFactory {
43 public:
44 virtual ~EventFactory() {}
45
46 virtual EventWrapper* CreateEvent() = 0;
47};
48
49class EventFactoryImpl : public EventFactory {
50 public:
51 virtual ~EventFactoryImpl() {}
52
philipel9d3ab612015-12-21 04:12:39 -080053 virtual EventWrapper* CreateEvent() { return EventWrapper::Create(); }
Henrik Kjellander2557b862015-11-18 22:00:21 +010054};
55
56// Used to indicate which decode with errors mode should be used.
57enum VCMDecodeErrorMode {
philipel9d3ab612015-12-21 04:12:39 -080058 kNoErrors, // Never decode with errors. Video will freeze
59 // if nack is disabled.
60 kSelectiveErrors, // Frames that are determined decodable in
61 // VCMSessionInfo may be decoded with missing
62 // packets. As not all incomplete frames will be
63 // decodable, video will freeze if nack is disabled.
64 kWithErrors // Release frames as needed. Errors may be
65 // introduced as some encoded frames may not be
66 // complete.
Henrik Kjellander2557b862015-11-18 22:00:21 +010067};
68
philipel9d3ab612015-12-21 04:12:39 -080069class VideoCodingModule : public Module {
70 public:
71 enum SenderNackMode { kNackNone, kNackAll, kNackSelective };
Henrik Kjellander2557b862015-11-18 22:00:21 +010072
philipel9d3ab612015-12-21 04:12:39 -080073 enum ReceiverRobustness { kNone, kHardNack, kSoftNack, kReferenceSelection };
Henrik Kjellander2557b862015-11-18 22:00:21 +010074
philipel9d3ab612015-12-21 04:12:39 -080075 static VideoCodingModule* Create(
76 Clock* clock,
77 VideoEncoderRateObserver* encoder_rate_observer,
78 VCMQMSettingsCallback* qm_settings_callback);
Henrik Kjellander2557b862015-11-18 22:00:21 +010079
philipel83f831a2016-03-12 03:30:23 -080080 static VideoCodingModule* Create(
81 Clock* clock,
82 VideoEncoderRateObserver* encoder_rate_observer,
83 VCMQMSettingsCallback* qm_settings_callback,
84 NackSender* nack_sender,
sprang3911c262016-04-15 01:24:14 -070085 KeyFrameRequestSender* keyframe_request_sender,
86 EncodedImageCallback* pre_decode_image_callback);
philipel83f831a2016-03-12 03:30:23 -080087
philipel9d3ab612015-12-21 04:12:39 -080088 static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory);
Henrik Kjellander2557b862015-11-18 22:00:21 +010089
philipel83f831a2016-03-12 03:30:23 -080090 static VideoCodingModule* Create(
91 Clock* clock,
92 EventFactory* event_factory,
93 NackSender* nack_sender,
94 KeyFrameRequestSender* keyframe_request_sender);
95
philipel9d3ab612015-12-21 04:12:39 -080096 // Get supported codec settings using codec type
97 //
98 // Input:
99 // - codecType : The codec type to get settings for
100 // - codec : Memory where the codec settings will be stored
101 //
102 // Return value : VCM_OK, on success
103 // VCM_PARAMETER_ERROR if codec not supported
Peter Boström7776e782016-01-07 15:42:47 +0100104 static void Codec(VideoCodecType codecType, VideoCodec* codec);
Henrik Kjellander2557b862015-11-18 22:00:21 +0100105
philipel9d3ab612015-12-21 04:12:39 -0800106 /*
107 * Sender
108 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100109
philipel9d3ab612015-12-21 04:12:39 -0800110 // Registers a codec to be used for encoding. Calling this
111 // API multiple times overwrites any previously registered codecs.
112 //
113 // NOTE: Must be called on the thread that constructed the VCM instance.
114 //
115 // Input:
116 // - sendCodec : Settings for the codec to be registered.
117 // - numberOfCores : The number of cores the codec is allowed
118 // to use.
119 // - maxPayloadSize : The maximum size each payload is allowed
120 // to have. Usually MTU - overhead.
121 //
122 // Return value : VCM_OK, on success.
123 // < 0, on error.
124 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec,
125 uint32_t numberOfCores,
126 uint32_t maxPayloadSize) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100127
philipel9d3ab612015-12-21 04:12:39 -0800128 // Register an external encoder object. This can not be used together with
129 // external decoder callbacks.
130 //
131 // Input:
132 // - externalEncoder : Encoder object to be used for encoding frames
133 // inserted
134 // with the AddVideoFrame API.
135 // - payloadType : The payload type bound which this encoder is bound
136 // to.
137 //
138 // Return value : VCM_OK, on success.
139 // < 0, on error.
140 // TODO(pbos): Remove return type when unused elsewhere.
141 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
142 uint8_t payloadType,
143 bool internalSource = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100144
philipel9d3ab612015-12-21 04:12:39 -0800145 // API to get currently configured encoder target bitrate in bits/s.
146 //
147 // Return value : 0, on success.
148 // < 0, on error.
149 virtual int Bitrate(unsigned int* bitrate) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100150
philipel9d3ab612015-12-21 04:12:39 -0800151 // API to get currently configured encoder target frame rate.
152 //
153 // Return value : 0, on success.
154 // < 0, on error.
155 virtual int FrameRate(unsigned int* framerate) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100156
philipel9d3ab612015-12-21 04:12:39 -0800157 // Sets the parameters describing the send channel. These parameters are
158 // inputs to the
159 // Media Optimization inside the VCM and also specifies the target bit rate
160 // for the
161 // encoder. Bit rate used by NACK should already be compensated for by the
162 // user.
163 //
164 // Input:
165 // - target_bitrate : The target bitrate for VCM in bits/s.
166 // - lossRate : Fractions of lost packets the past second.
167 // (loss rate in percent = 100 * packetLoss /
168 // 255)
169 // - rtt : Current round-trip time in ms.
170 //
171 // Return value : VCM_OK, on success.
172 // < 0, on error.
173 virtual int32_t SetChannelParameters(uint32_t target_bitrate,
174 uint8_t lossRate,
175 int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100176
philipel9d3ab612015-12-21 04:12:39 -0800177 // Sets the parameters describing the receive channel. These parameters are
178 // inputs to the
179 // Media Optimization inside the VCM.
180 //
181 // Input:
182 // - rtt : Current round-trip time in ms.
183 // with the most amount available bandwidth in
184 // a conference
185 // scenario
186 //
187 // Return value : VCM_OK, on success.
188 // < 0, on error.
189 virtual int32_t SetReceiveChannelParameters(int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100190
philipel9d3ab612015-12-21 04:12:39 -0800191 // Register a video protection callback which will be called to deliver
192 // the requested FEC rate and NACK status (on/off).
193 //
194 // Input:
195 // - protection : The callback object to register.
196 //
197 // Return value : VCM_OK, on success.
198 // < 0, on error.
199 virtual int32_t RegisterProtectionCallback(
200 VCMProtectionCallback* protection) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100201
philipel9d3ab612015-12-21 04:12:39 -0800202 // Enable or disable a video protection method.
203 //
204 // Input:
205 // - videoProtection : The method to enable or disable.
206 // - enable : True if the method should be enabled, false if
207 // it should be disabled.
208 //
209 // Return value : VCM_OK, on success.
210 // < 0, on error.
211 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
212 bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100213
philipel9d3ab612015-12-21 04:12:39 -0800214 // Add one raw video frame to the encoder. This function does all the
215 // necessary
216 // processing, then decides what frame type to encode, or if the frame should
217 // be
218 // dropped. If the frame should be encoded it passes the frame to the encoder
219 // before it returns.
220 //
221 // Input:
222 // - videoFrame : Video frame to encode.
223 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed
224 // in-band signaling.
225 //
226 // Return value : VCM_OK, on success.
227 // < 0, on error.
228 virtual int32_t AddVideoFrame(
229 const VideoFrame& videoFrame,
philipel9d3ab612015-12-21 04:12:39 -0800230 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100231
philipel9d3ab612015-12-21 04:12:39 -0800232 // Next frame encoded should be an intra frame (keyframe).
233 //
234 // Return value : VCM_OK, on success.
235 // < 0, on error.
perkj600246e2016-05-04 11:26:51 -0700236 virtual int32_t IntraFrameRequest(size_t stream_index) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100237
philipel9d3ab612015-12-21 04:12:39 -0800238 // Frame Dropper enable. Can be used to disable the frame dropping when the
239 // encoder
240 // over-uses its bit rate. This API is designed to be used when the encoded
241 // frames
242 // are supposed to be stored to an AVI file, or when the I420 codec is used
243 // and the
244 // target bit rate shouldn't affect the frame rate.
245 //
246 // Input:
247 // - enable : True to enable the setting, false to disable it.
248 //
249 // Return value : VCM_OK, on success.
250 // < 0, on error.
251 virtual int32_t EnableFrameDropper(bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100252
philipel9d3ab612015-12-21 04:12:39 -0800253 /*
254 * Receiver
255 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100256
philipel9d3ab612015-12-21 04:12:39 -0800257 // Register possible receive codecs, can be called multiple times for
258 // different codecs.
259 // The module will automatically switch between registered codecs depending on
260 // the
261 // payload type of incoming frames. The actual decoder will be created when
262 // needed.
263 //
264 // Input:
265 // - receiveCodec : Settings for the codec to be registered.
266 // - numberOfCores : Number of CPU cores that the decoder is allowed
267 // to use.
268 // - requireKeyFrame : Set this to true if you don't want any delta
269 // frames
270 // to be decoded until the first key frame has been
271 // decoded.
272 //
273 // Return value : VCM_OK, on success.
274 // < 0, on error.
275 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
276 int32_t numberOfCores,
277 bool requireKeyFrame = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100278
philipel9d3ab612015-12-21 04:12:39 -0800279 // Register an externally defined decoder/renderer object. Can be a decoder
280 // only or a
281 // decoder coupled with a renderer. Note that RegisterReceiveCodec must be
282 // called to
283 // be used for decoding incoming streams.
284 //
285 // Input:
286 // - externalDecoder : The external decoder/renderer object.
287 // - payloadType : The payload type which this decoder should
288 // be
289 // registered to.
290 //
291 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
292 uint8_t payloadType) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100293
philipel9d3ab612015-12-21 04:12:39 -0800294 // Register a receive callback. Will be called whenever there is a new frame
295 // ready
296 // for rendering.
297 //
298 // Input:
299 // - receiveCallback : The callback object to be used by the
300 // module when a
301 // frame is ready for rendering.
302 // De-register with a NULL pointer.
303 //
304 // Return value : VCM_OK, on success.
305 // < 0, on error.
306 virtual int32_t RegisterReceiveCallback(
307 VCMReceiveCallback* receiveCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100308
philipel9d3ab612015-12-21 04:12:39 -0800309 // Register a receive statistics callback which will be called to deliver
310 // information
311 // about the video stream received by the receiving side of the VCM, for
312 // instance the
313 // average frame rate and bit rate.
314 //
315 // Input:
316 // - receiveStats : The callback object to register.
317 //
318 // Return value : VCM_OK, on success.
319 // < 0, on error.
320 virtual int32_t RegisterReceiveStatisticsCallback(
321 VCMReceiveStatisticsCallback* receiveStats) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100322
philipel9d3ab612015-12-21 04:12:39 -0800323 // Register a decoder timing callback which will be called to deliver
324 // information about the timing of the decoder in the receiving side of the
325 // VCM, for instance the current and maximum frame decode latency.
326 //
327 // Input:
328 // - decoderTiming : The callback object to register.
329 //
330 // Return value : VCM_OK, on success.
331 // < 0, on error.
332 virtual int32_t RegisterDecoderTimingCallback(
333 VCMDecoderTimingCallback* decoderTiming) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100334
philipel9d3ab612015-12-21 04:12:39 -0800335 // Register a frame type request callback. This callback will be called when
336 // the
337 // module needs to request specific frame types from the send side.
338 //
339 // Input:
340 // - frameTypeCallback : The callback object to be used by the
341 // module when
342 // requesting a specific type of frame from
343 // the send side.
344 // De-register with a NULL pointer.
345 //
346 // Return value : VCM_OK, on success.
347 // < 0, on error.
348 virtual int32_t RegisterFrameTypeCallback(
349 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100350
philipel9d3ab612015-12-21 04:12:39 -0800351 // Registers a callback which is called whenever the receive side of the VCM
352 // encounters holes in the packet sequence and needs packets to be
353 // retransmitted.
354 //
355 // Input:
356 // - callback : The callback to be registered in the VCM.
357 //
358 // Return value : VCM_OK, on success.
359 // <0, on error.
360 virtual int32_t RegisterPacketRequestCallback(
361 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100362
philipel9d3ab612015-12-21 04:12:39 -0800363 // Waits for the next frame in the jitter buffer to become complete
364 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
365 // decoding.
366 // Should be called as often as possible to get the most out of the decoder.
367 //
368 // Return value : VCM_OK, on success.
369 // < 0, on error.
370 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100371
philipel9d3ab612015-12-21 04:12:39 -0800372 // API to get the codec which is currently used for decoding by the module.
373 //
374 // Input:
375 // - currentReceiveCodec : Settings for the codec to be registered.
376 //
377 // Return value : VCM_OK, on success.
378 // < 0, on error.
379 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100380
philipel9d3ab612015-12-21 04:12:39 -0800381 // API to get the codec type currently used for decoding by the module.
382 //
383 // Return value : codecy type, on success.
384 // kVideoCodecUnknown, on error or if no receive codec is
385 // registered
386 virtual VideoCodecType ReceiveCodec() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100387
philipel9d3ab612015-12-21 04:12:39 -0800388 // Insert a parsed packet into the receiver side of the module. Will be placed
389 // in the
390 // jitter buffer waiting for the frame to become complete. Returns as soon as
391 // the packet
392 // has been placed in the jitter buffer.
393 //
394 // Input:
395 // - incomingPayload : Payload of the packet.
396 // - payloadLength : Length of the payload.
397 // - rtpInfo : The parsed header.
398 //
399 // Return value : VCM_OK, on success.
400 // < 0, on error.
401 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
402 size_t payloadLength,
403 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100404
philipel9d3ab612015-12-21 04:12:39 -0800405 // Minimum playout delay (Used for lip-sync). This is the minimum delay
406 // required
407 // to sync with audio. Not included in VideoCodingModule::Delay()
408 // Defaults to 0 ms.
409 //
410 // Input:
411 // - minPlayoutDelayMs : Additional delay in ms.
412 //
413 // Return value : VCM_OK, on success.
414 // < 0, on error.
415 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100416
philipel9d3ab612015-12-21 04:12:39 -0800417 // Set the time required by the renderer to render a frame.
418 //
419 // Input:
420 // - timeMS : The time in ms required by the renderer to render a
421 // frame.
422 //
423 // Return value : VCM_OK, on success.
424 // < 0, on error.
425 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100426
philipel9d3ab612015-12-21 04:12:39 -0800427 // The total delay desired by the VCM. Can be less than the minimum
428 // delay set with SetMinimumPlayoutDelay.
429 //
430 // Return value : Total delay in ms, on success.
431 // < 0, on error.
432 virtual int32_t Delay() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100433
philipel9d3ab612015-12-21 04:12:39 -0800434 // Returns the number of packets discarded by the jitter buffer due to being
435 // too late. This can include duplicated packets which arrived after the
436 // frame was sent to the decoder. Therefore packets which were prematurely
437 // NACKed will be counted.
438 virtual uint32_t DiscardedPackets() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100439
philipel9d3ab612015-12-21 04:12:39 -0800440 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100441
philipel9d3ab612015-12-21 04:12:39 -0800442 // Set the receiver robustness mode. The mode decides how the receiver
443 // responds to losses in the stream. The type of counter-measure (soft or
444 // hard NACK, dual decoder, RPS, etc.) is selected through the
445 // robustnessMode parameter. The errorMode parameter decides if it is
446 // allowed to display frames corrupted by losses. Note that not all
447 // combinations of the two parameters are feasible. An error will be
448 // returned for invalid combinations.
449 // Input:
450 // - robustnessMode : selected robustness mode.
451 // - errorMode : selected error mode.
452 //
453 // Return value : VCM_OK, on success;
454 // < 0, on error.
455 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
456 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100457
philipel9d3ab612015-12-21 04:12:39 -0800458 // Set the decode error mode. The mode decides which errors (if any) are
459 // allowed in decodable frames. Note that setting decode_error_mode to
460 // anything other than kWithErrors without enabling nack will cause
461 // long-term freezes (resulting from frequent key frame requests) if
462 // packet loss occurs.
463 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100464
philipel9d3ab612015-12-21 04:12:39 -0800465 // Sets the maximum number of sequence numbers that we are allowed to NACK
466 // and the oldest sequence number that we will consider to NACK. If a
467 // sequence number older than |max_packet_age_to_nack| is missing
468 // a key frame will be requested. A key frame will also be requested if the
469 // time of incomplete or non-continuous frames in the jitter buffer is above
470 // |max_incomplete_time_ms|.
471 virtual void SetNackSettings(size_t max_nack_list_size,
472 int max_packet_age_to_nack,
473 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100474
philipel9d3ab612015-12-21 04:12:39 -0800475 // Setting a desired delay to the VCM receiver. Video rendering will be
476 // delayed by at least desired_delay_ms.
477 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100478
philipel9d3ab612015-12-21 04:12:39 -0800479 // Lets the sender suspend video when the rate drops below
480 // |threshold_bps|, and turns back on when the rate goes back up above
481 // |threshold_bps| + |window_bps|.
482 virtual void SuspendBelowMinBitrate() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100483
philipel9d3ab612015-12-21 04:12:39 -0800484 // Returns true if SuspendBelowMinBitrate is engaged and the video has been
485 // suspended due to bandwidth limitations; otherwise false.
486 virtual bool VideoSuspended() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100487
philipel9d3ab612015-12-21 04:12:39 -0800488 virtual void RegisterPostEncodeImageCallback(
489 EncodedImageCallback* post_encode_callback) = 0;
490 // Releases pending decode calls, permitting faster thread shutdown.
491 virtual void TriggerDecoderShutdown() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100492};
493
494} // namespace webrtc
495
philipel9d3ab612015-12-21 04:12:39 -0800496#endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_