blob: 7b6bd3241605a9a4587c167377980268ddca1d2e [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;
Erik Språng08127a92016-11-16 16:41:30 +010038class VideoBitrateAllocator;
Henrik Kjellander2557b862015-11-18 22:00:21 +010039class VideoEncoder;
40class VideoDecoder;
41struct CodecSpecificInfo;
42
43class EventFactory {
44 public:
45 virtual ~EventFactory() {}
46
47 virtual EventWrapper* CreateEvent() = 0;
48};
49
50class EventFactoryImpl : public EventFactory {
51 public:
52 virtual ~EventFactoryImpl() {}
53
philipel9d3ab612015-12-21 04:12:39 -080054 virtual EventWrapper* CreateEvent() { return EventWrapper::Create(); }
Henrik Kjellander2557b862015-11-18 22:00:21 +010055};
56
57// Used to indicate which decode with errors mode should be used.
58enum VCMDecodeErrorMode {
philipel9d3ab612015-12-21 04:12:39 -080059 kNoErrors, // Never decode with errors. Video will freeze
60 // if nack is disabled.
61 kSelectiveErrors, // Frames that are determined decodable in
62 // VCMSessionInfo may be decoded with missing
63 // packets. As not all incomplete frames will be
64 // decodable, video will freeze if nack is disabled.
65 kWithErrors // Release frames as needed. Errors may be
66 // introduced as some encoded frames may not be
67 // complete.
Henrik Kjellander2557b862015-11-18 22:00:21 +010068};
69
philipel9d3ab612015-12-21 04:12:39 -080070class VideoCodingModule : public Module {
71 public:
72 enum SenderNackMode { kNackNone, kNackAll, kNackSelective };
Henrik Kjellander2557b862015-11-18 22:00:21 +010073
philipel9d3ab612015-12-21 04:12:39 -080074 enum ReceiverRobustness { kNone, kHardNack, kSoftNack, kReferenceSelection };
Henrik Kjellander2557b862015-11-18 22:00:21 +010075
perkjf5b2e512016-07-05 08:34:04 -070076 static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory);
Henrik Kjellander2557b862015-11-18 22:00:21 +010077
philipel83f831a2016-03-12 03:30:23 -080078 static VideoCodingModule* Create(
79 Clock* clock,
philipel83f831a2016-03-12 03:30:23 -080080 VCMQMSettingsCallback* qm_settings_callback,
81 NackSender* nack_sender,
sprang3911c262016-04-15 01:24:14 -070082 KeyFrameRequestSender* keyframe_request_sender,
83 EncodedImageCallback* pre_decode_image_callback);
philipel83f831a2016-03-12 03:30:23 -080084
philipel83f831a2016-03-12 03:30:23 -080085 static VideoCodingModule* Create(
86 Clock* clock,
87 EventFactory* event_factory,
88 NackSender* nack_sender,
89 KeyFrameRequestSender* keyframe_request_sender);
90
philipel9d3ab612015-12-21 04:12:39 -080091 // Get supported codec settings using codec type
92 //
93 // Input:
94 // - codecType : The codec type to get settings for
95 // - codec : Memory where the codec settings will be stored
96 //
97 // Return value : VCM_OK, on success
98 // VCM_PARAMETER_ERROR if codec not supported
Peter Boström7776e782016-01-07 15:42:47 +010099 static void Codec(VideoCodecType codecType, VideoCodec* codec);
Henrik Kjellander2557b862015-11-18 22:00:21 +0100100
philipel9d3ab612015-12-21 04:12:39 -0800101 /*
102 * Sender
103 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100104
philipel9d3ab612015-12-21 04:12:39 -0800105 // Registers a codec to be used for encoding. Calling this
106 // API multiple times overwrites any previously registered codecs.
107 //
108 // NOTE: Must be called on the thread that constructed the VCM instance.
109 //
110 // Input:
111 // - sendCodec : Settings for the codec to be registered.
112 // - numberOfCores : The number of cores the codec is allowed
113 // to use.
114 // - maxPayloadSize : The maximum size each payload is allowed
115 // to have. Usually MTU - overhead.
116 //
117 // Return value : VCM_OK, on success.
118 // < 0, on error.
119 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec,
120 uint32_t numberOfCores,
121 uint32_t maxPayloadSize) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100122
philipel9d3ab612015-12-21 04:12:39 -0800123 // Register an external encoder object. This can not be used together with
124 // external decoder callbacks.
125 //
126 // Input:
127 // - externalEncoder : Encoder object to be used for encoding frames
128 // inserted
129 // with the AddVideoFrame API.
130 // - payloadType : The payload type bound which this encoder is bound
131 // to.
132 //
133 // Return value : VCM_OK, on success.
134 // < 0, on error.
135 // TODO(pbos): Remove return type when unused elsewhere.
136 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
137 uint8_t payloadType,
138 bool internalSource = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100139
philipel9d3ab612015-12-21 04:12:39 -0800140 // API to get currently configured encoder target bitrate in bits/s.
141 //
142 // Return value : 0, on success.
143 // < 0, on error.
144 virtual int Bitrate(unsigned int* bitrate) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100145
philipel9d3ab612015-12-21 04:12:39 -0800146 // API to get currently configured encoder target frame rate.
147 //
148 // Return value : 0, on success.
149 // < 0, on error.
150 virtual int FrameRate(unsigned int* framerate) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100151
philipel9d3ab612015-12-21 04:12:39 -0800152 // Sets the parameters describing the send channel. These parameters are
153 // inputs to the
154 // Media Optimization inside the VCM and also specifies the target bit rate
155 // for the
156 // encoder. Bit rate used by NACK should already be compensated for by the
157 // user.
158 //
159 // Input:
160 // - target_bitrate : The target bitrate for VCM in bits/s.
161 // - lossRate : Fractions of lost packets the past second.
162 // (loss rate in percent = 100 * packetLoss /
163 // 255)
164 // - rtt : Current round-trip time in ms.
165 //
166 // Return value : VCM_OK, on success.
167 // < 0, on error.
168 virtual int32_t SetChannelParameters(uint32_t target_bitrate,
169 uint8_t lossRate,
170 int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100171
philipel9d3ab612015-12-21 04:12:39 -0800172 // Sets the parameters describing the receive channel. These parameters are
173 // inputs to the
174 // Media Optimization inside the VCM.
175 //
176 // Input:
177 // - rtt : Current round-trip time in ms.
178 // with the most amount available bandwidth in
179 // a conference
180 // scenario
181 //
182 // Return value : VCM_OK, on success.
183 // < 0, on error.
184 virtual int32_t SetReceiveChannelParameters(int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100185
Per69b332d2016-06-02 15:45:42 +0200186 // Deprecated: This method currently does not have any effect.
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).
Per69b332d2016-06-02 15:45:42 +0200189 // TODO(perkj): Remove once no projects use it.
philipel9d3ab612015-12-21 04:12:39 -0800190 virtual int32_t RegisterProtectionCallback(
191 VCMProtectionCallback* protection) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100192
philipel9d3ab612015-12-21 04:12:39 -0800193 // Enable or disable a video protection method.
194 //
195 // Input:
196 // - videoProtection : The method to enable or disable.
197 // - enable : True if the method should be enabled, false if
198 // it should be disabled.
199 //
200 // Return value : VCM_OK, on success.
201 // < 0, on error.
202 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
203 bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100204
philipel9d3ab612015-12-21 04:12:39 -0800205 // Add one raw video frame to the encoder. This function does all the
206 // necessary
207 // processing, then decides what frame type to encode, or if the frame should
208 // be
209 // dropped. If the frame should be encoded it passes the frame to the encoder
210 // before it returns.
211 //
212 // Input:
213 // - videoFrame : Video frame to encode.
214 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed
215 // in-band signaling.
216 //
217 // Return value : VCM_OK, on success.
218 // < 0, on error.
219 virtual int32_t AddVideoFrame(
220 const VideoFrame& videoFrame,
philipel9d3ab612015-12-21 04:12:39 -0800221 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100222
philipel9d3ab612015-12-21 04:12:39 -0800223 // Next frame encoded should be an intra frame (keyframe).
224 //
225 // Return value : VCM_OK, on success.
226 // < 0, on error.
perkj600246e2016-05-04 11:26:51 -0700227 virtual int32_t IntraFrameRequest(size_t stream_index) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100228
philipel9d3ab612015-12-21 04:12:39 -0800229 // Frame Dropper enable. Can be used to disable the frame dropping when the
230 // encoder
231 // over-uses its bit rate. This API is designed to be used when the encoded
232 // frames
233 // are supposed to be stored to an AVI file, or when the I420 codec is used
234 // and the
235 // target bit rate shouldn't affect the frame rate.
236 //
237 // Input:
238 // - enable : True to enable the setting, false to disable it.
239 //
240 // Return value : VCM_OK, on success.
241 // < 0, on error.
242 virtual int32_t EnableFrameDropper(bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100243
philipel9d3ab612015-12-21 04:12:39 -0800244 /*
245 * Receiver
246 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100247
philipel9d3ab612015-12-21 04:12:39 -0800248 // Register possible receive codecs, can be called multiple times for
249 // different codecs.
250 // The module will automatically switch between registered codecs depending on
251 // the
252 // payload type of incoming frames. The actual decoder will be created when
253 // needed.
254 //
255 // Input:
256 // - receiveCodec : Settings for the codec to be registered.
257 // - numberOfCores : Number of CPU cores that the decoder is allowed
258 // to use.
259 // - requireKeyFrame : Set this to true if you don't want any delta
260 // frames
261 // to be decoded until the first key frame has been
262 // decoded.
263 //
264 // Return value : VCM_OK, on success.
265 // < 0, on error.
266 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
267 int32_t numberOfCores,
268 bool requireKeyFrame = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100269
philipel9d3ab612015-12-21 04:12:39 -0800270 // Register an externally defined decoder/renderer object. Can be a decoder
271 // only or a
272 // decoder coupled with a renderer. Note that RegisterReceiveCodec must be
273 // called to
274 // be used for decoding incoming streams.
275 //
276 // Input:
277 // - externalDecoder : The external decoder/renderer object.
278 // - payloadType : The payload type which this decoder should
279 // be
280 // registered to.
281 //
282 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
283 uint8_t payloadType) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100284
philipel9d3ab612015-12-21 04:12:39 -0800285 // Register a receive callback. Will be called whenever there is a new frame
286 // ready
287 // for rendering.
288 //
289 // Input:
290 // - receiveCallback : The callback object to be used by the
291 // module when a
292 // frame is ready for rendering.
293 // De-register with a NULL pointer.
294 //
295 // Return value : VCM_OK, on success.
296 // < 0, on error.
297 virtual int32_t RegisterReceiveCallback(
298 VCMReceiveCallback* receiveCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100299
philipel9d3ab612015-12-21 04:12:39 -0800300 // Register a receive statistics callback which will be called to deliver
301 // information
302 // about the video stream received by the receiving side of the VCM, for
303 // instance the
304 // average frame rate and bit rate.
305 //
306 // Input:
307 // - receiveStats : The callback object to register.
308 //
309 // Return value : VCM_OK, on success.
310 // < 0, on error.
311 virtual int32_t RegisterReceiveStatisticsCallback(
312 VCMReceiveStatisticsCallback* receiveStats) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100313
philipel9d3ab612015-12-21 04:12:39 -0800314 // Register a decoder timing callback which will be called to deliver
315 // information about the timing of the decoder in the receiving side of the
316 // VCM, for instance the current and maximum frame decode latency.
317 //
318 // Input:
319 // - decoderTiming : The callback object to register.
320 //
321 // Return value : VCM_OK, on success.
322 // < 0, on error.
323 virtual int32_t RegisterDecoderTimingCallback(
324 VCMDecoderTimingCallback* decoderTiming) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100325
philipel9d3ab612015-12-21 04:12:39 -0800326 // Register a frame type request callback. This callback will be called when
327 // the
328 // module needs to request specific frame types from the send side.
329 //
330 // Input:
331 // - frameTypeCallback : The callback object to be used by the
332 // module when
333 // requesting a specific type of frame from
334 // the send side.
335 // De-register with a NULL pointer.
336 //
337 // Return value : VCM_OK, on success.
338 // < 0, on error.
339 virtual int32_t RegisterFrameTypeCallback(
340 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100341
philipel9d3ab612015-12-21 04:12:39 -0800342 // Registers a callback which is called whenever the receive side of the VCM
343 // encounters holes in the packet sequence and needs packets to be
344 // retransmitted.
345 //
346 // Input:
347 // - callback : The callback to be registered in the VCM.
348 //
349 // Return value : VCM_OK, on success.
350 // <0, on error.
351 virtual int32_t RegisterPacketRequestCallback(
352 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100353
philipel9d3ab612015-12-21 04:12:39 -0800354 // Waits for the next frame in the jitter buffer to become complete
355 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
356 // decoding.
357 // Should be called as often as possible to get the most out of the decoder.
358 //
359 // Return value : VCM_OK, on success.
360 // < 0, on error.
361 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100362
philipel9d3ab612015-12-21 04:12:39 -0800363 // API to get the codec which is currently used for decoding by the module.
364 //
365 // Input:
366 // - currentReceiveCodec : Settings for the codec to be registered.
367 //
368 // Return value : VCM_OK, on success.
369 // < 0, on error.
370 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100371
philipel9d3ab612015-12-21 04:12:39 -0800372 // API to get the codec type currently used for decoding by the module.
373 //
374 // Return value : codecy type, on success.
375 // kVideoCodecUnknown, on error or if no receive codec is
376 // registered
377 virtual VideoCodecType ReceiveCodec() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100378
philipel9d3ab612015-12-21 04:12:39 -0800379 // Insert a parsed packet into the receiver side of the module. Will be placed
380 // in the
381 // jitter buffer waiting for the frame to become complete. Returns as soon as
382 // the packet
383 // has been placed in the jitter buffer.
384 //
385 // Input:
386 // - incomingPayload : Payload of the packet.
387 // - payloadLength : Length of the payload.
388 // - rtpInfo : The parsed header.
389 //
390 // Return value : VCM_OK, on success.
391 // < 0, on error.
392 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
393 size_t payloadLength,
394 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100395
philipel9d3ab612015-12-21 04:12:39 -0800396 // Minimum playout delay (Used for lip-sync). This is the minimum delay
397 // required
398 // to sync with audio. Not included in VideoCodingModule::Delay()
399 // Defaults to 0 ms.
400 //
401 // Input:
402 // - minPlayoutDelayMs : Additional delay in ms.
403 //
404 // Return value : VCM_OK, on success.
405 // < 0, on error.
406 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100407
philipel9d3ab612015-12-21 04:12:39 -0800408 // Set the time required by the renderer to render a frame.
409 //
410 // Input:
411 // - timeMS : The time in ms required by the renderer to render a
412 // frame.
413 //
414 // Return value : VCM_OK, on success.
415 // < 0, on error.
416 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100417
philipel9d3ab612015-12-21 04:12:39 -0800418 // The total delay desired by the VCM. Can be less than the minimum
419 // delay set with SetMinimumPlayoutDelay.
420 //
421 // Return value : Total delay in ms, on success.
422 // < 0, on error.
423 virtual int32_t Delay() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100424
philipel9d3ab612015-12-21 04:12:39 -0800425 // Returns the number of packets discarded by the jitter buffer due to being
426 // too late. This can include duplicated packets which arrived after the
427 // frame was sent to the decoder. Therefore packets which were prematurely
428 // NACKed will be counted.
429 virtual uint32_t DiscardedPackets() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100430
philipel9d3ab612015-12-21 04:12:39 -0800431 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100432
philipel9d3ab612015-12-21 04:12:39 -0800433 // Set the receiver robustness mode. The mode decides how the receiver
434 // responds to losses in the stream. The type of counter-measure (soft or
435 // hard NACK, dual decoder, RPS, etc.) is selected through the
436 // robustnessMode parameter. The errorMode parameter decides if it is
437 // allowed to display frames corrupted by losses. Note that not all
438 // combinations of the two parameters are feasible. An error will be
439 // returned for invalid combinations.
440 // Input:
441 // - robustnessMode : selected robustness mode.
442 // - errorMode : selected error mode.
443 //
444 // Return value : VCM_OK, on success;
445 // < 0, on error.
446 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
447 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100448
philipel9d3ab612015-12-21 04:12:39 -0800449 // Set the decode error mode. The mode decides which errors (if any) are
450 // allowed in decodable frames. Note that setting decode_error_mode to
451 // anything other than kWithErrors without enabling nack will cause
452 // long-term freezes (resulting from frequent key frame requests) if
453 // packet loss occurs.
454 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100455
philipel9d3ab612015-12-21 04:12:39 -0800456 // Sets the maximum number of sequence numbers that we are allowed to NACK
457 // and the oldest sequence number that we will consider to NACK. If a
458 // sequence number older than |max_packet_age_to_nack| is missing
459 // a key frame will be requested. A key frame will also be requested if the
460 // time of incomplete or non-continuous frames in the jitter buffer is above
461 // |max_incomplete_time_ms|.
462 virtual void SetNackSettings(size_t max_nack_list_size,
463 int max_packet_age_to_nack,
464 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100465
philipel9d3ab612015-12-21 04:12:39 -0800466 // Setting a desired delay to the VCM receiver. Video rendering will be
467 // delayed by at least desired_delay_ms.
468 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100469
philipel9d3ab612015-12-21 04:12:39 -0800470 virtual void RegisterPostEncodeImageCallback(
471 EncodedImageCallback* post_encode_callback) = 0;
472 // Releases pending decode calls, permitting faster thread shutdown.
473 virtual void TriggerDecoderShutdown() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100474};
475
476} // namespace webrtc
477
philipel9d3ab612015-12-21 04:12:39 -0800478#endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_