blob: 295911d0338709841bce81bd217db2e81ee48495 [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;
34class VideoEncoder;
35class VideoDecoder;
36struct CodecSpecificInfo;
37
38class EventFactory {
39 public:
40 virtual ~EventFactory() {}
41
42 virtual EventWrapper* CreateEvent() = 0;
43};
44
45class EventFactoryImpl : public EventFactory {
46 public:
47 virtual ~EventFactoryImpl() {}
48
philipel9d3ab612015-12-21 04:12:39 -080049 virtual EventWrapper* CreateEvent() { return EventWrapper::Create(); }
Henrik Kjellander2557b862015-11-18 22:00:21 +010050};
51
52// Used to indicate which decode with errors mode should be used.
53enum VCMDecodeErrorMode {
philipel9d3ab612015-12-21 04:12:39 -080054 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 Kjellander2557b862015-11-18 22:00:21 +010063};
64
philipel9d3ab612015-12-21 04:12:39 -080065class VideoCodingModule : public Module {
66 public:
67 enum SenderNackMode { kNackNone, kNackAll, kNackSelective };
Henrik Kjellander2557b862015-11-18 22:00:21 +010068
philipel9d3ab612015-12-21 04:12:39 -080069 enum ReceiverRobustness { kNone, kHardNack, kSoftNack, kReferenceSelection };
Henrik Kjellander2557b862015-11-18 22:00:21 +010070
philipel9d3ab612015-12-21 04:12:39 -080071 static VideoCodingModule* Create(
72 Clock* clock,
73 VideoEncoderRateObserver* encoder_rate_observer,
74 VCMQMSettingsCallback* qm_settings_callback);
Henrik Kjellander2557b862015-11-18 22:00:21 +010075
philipel83f831a2016-03-12 03:30:23 -080076 static VideoCodingModule* Create(
77 Clock* clock,
78 VideoEncoderRateObserver* encoder_rate_observer,
79 VCMQMSettingsCallback* qm_settings_callback,
80 NackSender* nack_sender,
sprang3911c262016-04-15 01:24:14 -070081 KeyFrameRequestSender* keyframe_request_sender,
82 EncodedImageCallback* pre_decode_image_callback);
philipel83f831a2016-03-12 03:30:23 -080083
philipel9d3ab612015-12-21 04:12:39 -080084 static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory);
Henrik Kjellander2557b862015-11-18 22:00:21 +010085
philipel83f831a2016-03-12 03:30:23 -080086 static VideoCodingModule* Create(
87 Clock* clock,
88 EventFactory* event_factory,
89 NackSender* nack_sender,
90 KeyFrameRequestSender* keyframe_request_sender);
91
philipel9d3ab612015-12-21 04:12:39 -080092 // Get supported codec settings using codec type
93 //
94 // Input:
95 // - codecType : The codec type to get settings for
96 // - codec : Memory where the codec settings will be stored
97 //
98 // Return value : VCM_OK, on success
99 // VCM_PARAMETER_ERROR if codec not supported
Peter Boström7776e782016-01-07 15:42:47 +0100100 static void Codec(VideoCodecType codecType, VideoCodec* codec);
Henrik Kjellander2557b862015-11-18 22:00:21 +0100101
philipel9d3ab612015-12-21 04:12:39 -0800102 /*
103 * Sender
104 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100105
philipel9d3ab612015-12-21 04:12:39 -0800106 // Registers a codec to be used for encoding. Calling this
107 // API multiple times overwrites any previously registered codecs.
108 //
109 // NOTE: Must be called on the thread that constructed the VCM instance.
110 //
111 // Input:
112 // - sendCodec : Settings for the codec to be registered.
113 // - numberOfCores : The number of cores the codec is allowed
114 // to use.
115 // - maxPayloadSize : The maximum size each payload is allowed
116 // to have. Usually MTU - overhead.
117 //
118 // Return value : VCM_OK, on success.
119 // < 0, on error.
120 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec,
121 uint32_t numberOfCores,
122 uint32_t maxPayloadSize) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100123
philipel9d3ab612015-12-21 04:12:39 -0800124 // Register an external encoder object. This can not be used together with
125 // external decoder callbacks.
126 //
127 // Input:
128 // - externalEncoder : Encoder object to be used for encoding frames
129 // inserted
130 // with the AddVideoFrame API.
131 // - payloadType : The payload type bound which this encoder is bound
132 // to.
133 //
134 // Return value : VCM_OK, on success.
135 // < 0, on error.
136 // TODO(pbos): Remove return type when unused elsewhere.
137 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
138 uint8_t payloadType,
139 bool internalSource = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100140
philipel9d3ab612015-12-21 04:12:39 -0800141 // API to get currently configured encoder target bitrate in bits/s.
142 //
143 // Return value : 0, on success.
144 // < 0, on error.
145 virtual int Bitrate(unsigned int* bitrate) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100146
philipel9d3ab612015-12-21 04:12:39 -0800147 // API to get currently configured encoder target frame rate.
148 //
149 // Return value : 0, on success.
150 // < 0, on error.
151 virtual int FrameRate(unsigned int* framerate) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100152
philipel9d3ab612015-12-21 04:12:39 -0800153 // Sets the parameters describing the send channel. These parameters are
154 // inputs to the
155 // Media Optimization inside the VCM and also specifies the target bit rate
156 // for the
157 // encoder. Bit rate used by NACK should already be compensated for by the
158 // user.
159 //
160 // Input:
161 // - target_bitrate : The target bitrate for VCM in bits/s.
162 // - lossRate : Fractions of lost packets the past second.
163 // (loss rate in percent = 100 * packetLoss /
164 // 255)
165 // - rtt : Current round-trip time in ms.
166 //
167 // Return value : VCM_OK, on success.
168 // < 0, on error.
169 virtual int32_t SetChannelParameters(uint32_t target_bitrate,
170 uint8_t lossRate,
171 int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100172
philipel9d3ab612015-12-21 04:12:39 -0800173 // Sets the parameters describing the receive channel. These parameters are
174 // inputs to the
175 // Media Optimization inside the VCM.
176 //
177 // Input:
178 // - rtt : Current round-trip time in ms.
179 // with the most amount available bandwidth in
180 // a conference
181 // scenario
182 //
183 // Return value : VCM_OK, on success.
184 // < 0, on error.
185 virtual int32_t SetReceiveChannelParameters(int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100186
philipel9d3ab612015-12-21 04:12:39 -0800187 // Register a video protection callback which will be called to deliver
188 // the requested FEC rate and NACK status (on/off).
189 //
190 // Input:
191 // - protection : The callback object to register.
192 //
193 // Return value : VCM_OK, on success.
194 // < 0, on error.
195 virtual int32_t RegisterProtectionCallback(
196 VCMProtectionCallback* protection) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100197
philipel9d3ab612015-12-21 04:12:39 -0800198 // Enable or disable a video protection method.
199 //
200 // Input:
201 // - videoProtection : The method to enable or disable.
202 // - enable : True if the method should be enabled, false if
203 // it should be disabled.
204 //
205 // Return value : VCM_OK, on success.
206 // < 0, on error.
207 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
208 bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100209
philipel9d3ab612015-12-21 04:12:39 -0800210 // Add one raw video frame to the encoder. This function does all the
211 // necessary
212 // processing, then decides what frame type to encode, or if the frame should
213 // be
214 // dropped. If the frame should be encoded it passes the frame to the encoder
215 // before it returns.
216 //
217 // Input:
218 // - videoFrame : Video frame to encode.
219 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed
220 // in-band signaling.
221 //
222 // Return value : VCM_OK, on success.
223 // < 0, on error.
224 virtual int32_t AddVideoFrame(
225 const VideoFrame& videoFrame,
226 const VideoContentMetrics* contentMetrics = NULL,
227 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100228
philipel9d3ab612015-12-21 04:12:39 -0800229 // Next frame encoded should be an intra frame (keyframe).
230 //
231 // Return value : VCM_OK, on success.
232 // < 0, on error.
233 virtual int32_t IntraFrameRequest(int stream_index) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100234
philipel9d3ab612015-12-21 04:12:39 -0800235 // Frame Dropper enable. Can be used to disable the frame dropping when the
236 // encoder
237 // over-uses its bit rate. This API is designed to be used when the encoded
238 // frames
239 // are supposed to be stored to an AVI file, or when the I420 codec is used
240 // and the
241 // target bit rate shouldn't affect the frame rate.
242 //
243 // Input:
244 // - enable : True to enable the setting, false to disable it.
245 //
246 // Return value : VCM_OK, on success.
247 // < 0, on error.
248 virtual int32_t EnableFrameDropper(bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100249
philipel9d3ab612015-12-21 04:12:39 -0800250 /*
251 * Receiver
252 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100253
philipel9d3ab612015-12-21 04:12:39 -0800254 // Register possible receive codecs, can be called multiple times for
255 // different codecs.
256 // The module will automatically switch between registered codecs depending on
257 // the
258 // payload type of incoming frames. The actual decoder will be created when
259 // needed.
260 //
261 // Input:
262 // - receiveCodec : Settings for the codec to be registered.
263 // - numberOfCores : Number of CPU cores that the decoder is allowed
264 // to use.
265 // - requireKeyFrame : Set this to true if you don't want any delta
266 // frames
267 // to be decoded until the first key frame has been
268 // decoded.
269 //
270 // Return value : VCM_OK, on success.
271 // < 0, on error.
272 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
273 int32_t numberOfCores,
274 bool requireKeyFrame = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100275
philipel9d3ab612015-12-21 04:12:39 -0800276 // Register an externally defined decoder/renderer object. Can be a decoder
277 // only or a
278 // decoder coupled with a renderer. Note that RegisterReceiveCodec must be
279 // called to
280 // be used for decoding incoming streams.
281 //
282 // Input:
283 // - externalDecoder : The external decoder/renderer object.
284 // - payloadType : The payload type which this decoder should
285 // be
286 // registered to.
287 //
288 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
289 uint8_t payloadType) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100290
philipel9d3ab612015-12-21 04:12:39 -0800291 // Register a receive callback. Will be called whenever there is a new frame
292 // ready
293 // for rendering.
294 //
295 // Input:
296 // - receiveCallback : The callback object to be used by the
297 // module when a
298 // frame is ready for rendering.
299 // De-register with a NULL pointer.
300 //
301 // Return value : VCM_OK, on success.
302 // < 0, on error.
303 virtual int32_t RegisterReceiveCallback(
304 VCMReceiveCallback* receiveCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100305
philipel9d3ab612015-12-21 04:12:39 -0800306 // Register a receive statistics callback which will be called to deliver
307 // information
308 // about the video stream received by the receiving side of the VCM, for
309 // instance the
310 // average frame rate and bit rate.
311 //
312 // Input:
313 // - receiveStats : The callback object to register.
314 //
315 // Return value : VCM_OK, on success.
316 // < 0, on error.
317 virtual int32_t RegisterReceiveStatisticsCallback(
318 VCMReceiveStatisticsCallback* receiveStats) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100319
philipel9d3ab612015-12-21 04:12:39 -0800320 // Register a decoder timing callback which will be called to deliver
321 // information about the timing of the decoder in the receiving side of the
322 // VCM, for instance the current and maximum frame decode latency.
323 //
324 // Input:
325 // - decoderTiming : The callback object to register.
326 //
327 // Return value : VCM_OK, on success.
328 // < 0, on error.
329 virtual int32_t RegisterDecoderTimingCallback(
330 VCMDecoderTimingCallback* decoderTiming) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100331
philipel9d3ab612015-12-21 04:12:39 -0800332 // Register a frame type request callback. This callback will be called when
333 // the
334 // module needs to request specific frame types from the send side.
335 //
336 // Input:
337 // - frameTypeCallback : The callback object to be used by the
338 // module when
339 // requesting a specific type of frame from
340 // the send side.
341 // De-register with a NULL pointer.
342 //
343 // Return value : VCM_OK, on success.
344 // < 0, on error.
345 virtual int32_t RegisterFrameTypeCallback(
346 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100347
philipel9d3ab612015-12-21 04:12:39 -0800348 // Registers a callback which is called whenever the receive side of the VCM
349 // encounters holes in the packet sequence and needs packets to be
350 // retransmitted.
351 //
352 // Input:
353 // - callback : The callback to be registered in the VCM.
354 //
355 // Return value : VCM_OK, on success.
356 // <0, on error.
357 virtual int32_t RegisterPacketRequestCallback(
358 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100359
philipel9d3ab612015-12-21 04:12:39 -0800360 // Waits for the next frame in the jitter buffer to become complete
361 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
362 // decoding.
363 // Should be called as often as possible to get the most out of the decoder.
364 //
365 // Return value : VCM_OK, on success.
366 // < 0, on error.
367 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100368
philipel9d3ab612015-12-21 04:12:39 -0800369 // API to get the codec which is currently used for decoding by the module.
370 //
371 // Input:
372 // - currentReceiveCodec : Settings for the codec to be registered.
373 //
374 // Return value : VCM_OK, on success.
375 // < 0, on error.
376 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100377
philipel9d3ab612015-12-21 04:12:39 -0800378 // API to get the codec type currently used for decoding by the module.
379 //
380 // Return value : codecy type, on success.
381 // kVideoCodecUnknown, on error or if no receive codec is
382 // registered
383 virtual VideoCodecType ReceiveCodec() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100384
philipel9d3ab612015-12-21 04:12:39 -0800385 // Insert a parsed packet into the receiver side of the module. Will be placed
386 // in the
387 // jitter buffer waiting for the frame to become complete. Returns as soon as
388 // the packet
389 // has been placed in the jitter buffer.
390 //
391 // Input:
392 // - incomingPayload : Payload of the packet.
393 // - payloadLength : Length of the payload.
394 // - rtpInfo : The parsed header.
395 //
396 // Return value : VCM_OK, on success.
397 // < 0, on error.
398 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
399 size_t payloadLength,
400 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100401
philipel9d3ab612015-12-21 04:12:39 -0800402 // Minimum playout delay (Used for lip-sync). This is the minimum delay
403 // required
404 // to sync with audio. Not included in VideoCodingModule::Delay()
405 // Defaults to 0 ms.
406 //
407 // Input:
408 // - minPlayoutDelayMs : Additional delay in ms.
409 //
410 // Return value : VCM_OK, on success.
411 // < 0, on error.
412 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100413
philipel9d3ab612015-12-21 04:12:39 -0800414 // Set the time required by the renderer to render a frame.
415 //
416 // Input:
417 // - timeMS : The time in ms required by the renderer to render a
418 // frame.
419 //
420 // Return value : VCM_OK, on success.
421 // < 0, on error.
422 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100423
philipel9d3ab612015-12-21 04:12:39 -0800424 // The total delay desired by the VCM. Can be less than the minimum
425 // delay set with SetMinimumPlayoutDelay.
426 //
427 // Return value : Total delay in ms, on success.
428 // < 0, on error.
429 virtual int32_t Delay() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100430
philipel9d3ab612015-12-21 04:12:39 -0800431 // Returns the number of packets discarded by the jitter buffer due to being
432 // too late. This can include duplicated packets which arrived after the
433 // frame was sent to the decoder. Therefore packets which were prematurely
434 // NACKed will be counted.
435 virtual uint32_t DiscardedPackets() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100436
philipel9d3ab612015-12-21 04:12:39 -0800437 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100438
philipel9d3ab612015-12-21 04:12:39 -0800439 // Set the receiver robustness mode. The mode decides how the receiver
440 // responds to losses in the stream. The type of counter-measure (soft or
441 // hard NACK, dual decoder, RPS, etc.) is selected through the
442 // robustnessMode parameter. The errorMode parameter decides if it is
443 // allowed to display frames corrupted by losses. Note that not all
444 // combinations of the two parameters are feasible. An error will be
445 // returned for invalid combinations.
446 // Input:
447 // - robustnessMode : selected robustness mode.
448 // - errorMode : selected error mode.
449 //
450 // Return value : VCM_OK, on success;
451 // < 0, on error.
452 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
453 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100454
philipel9d3ab612015-12-21 04:12:39 -0800455 // Set the decode error mode. The mode decides which errors (if any) are
456 // allowed in decodable frames. Note that setting decode_error_mode to
457 // anything other than kWithErrors without enabling nack will cause
458 // long-term freezes (resulting from frequent key frame requests) if
459 // packet loss occurs.
460 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100461
philipel9d3ab612015-12-21 04:12:39 -0800462 // Sets the maximum number of sequence numbers that we are allowed to NACK
463 // and the oldest sequence number that we will consider to NACK. If a
464 // sequence number older than |max_packet_age_to_nack| is missing
465 // a key frame will be requested. A key frame will also be requested if the
466 // time of incomplete or non-continuous frames in the jitter buffer is above
467 // |max_incomplete_time_ms|.
468 virtual void SetNackSettings(size_t max_nack_list_size,
469 int max_packet_age_to_nack,
470 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100471
philipel9d3ab612015-12-21 04:12:39 -0800472 // Setting a desired delay to the VCM receiver. Video rendering will be
473 // delayed by at least desired_delay_ms.
474 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100475
philipel9d3ab612015-12-21 04:12:39 -0800476 // Lets the sender suspend video when the rate drops below
477 // |threshold_bps|, and turns back on when the rate goes back up above
478 // |threshold_bps| + |window_bps|.
479 virtual void SuspendBelowMinBitrate() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100480
philipel9d3ab612015-12-21 04:12:39 -0800481 // Returns true if SuspendBelowMinBitrate is engaged and the video has been
482 // suspended due to bandwidth limitations; otherwise false.
483 virtual bool VideoSuspended() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100484
philipel9d3ab612015-12-21 04:12:39 -0800485 virtual void RegisterPostEncodeImageCallback(
486 EncodedImageCallback* post_encode_callback) = 0;
487 // Releases pending decode calls, permitting faster thread shutdown.
488 virtual void TriggerDecoderShutdown() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100489};
490
491} // namespace webrtc
492
philipel9d3ab612015-12-21 04:12:39 -0800493#endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_