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