blob: 880180d5b96ad5c5f3e6265e50ca2db9bc6f65cc [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.
Peter Boström795dbe42015-11-27 14:09:07 +0100184 // TODO(pbos): Remove return type when unused elsewhere.
Henrik Kjellander2557b862015-11-18 22:00:21 +0100185 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
186 uint8_t payloadType,
187 bool internalSource = false) = 0;
188
189 // API to get currently configured encoder target bitrate in bits/s.
190 //
191 // Return value : 0, on success.
192 // < 0, on error.
193 virtual int Bitrate(unsigned int* bitrate) const = 0;
194
195 // API to get currently configured encoder target frame rate.
196 //
197 // Return value : 0, on success.
198 // < 0, on error.
199 virtual int FrameRate(unsigned int* framerate) const = 0;
200
201 // Sets the parameters describing the send channel. These parameters are inputs to the
202 // Media Optimization inside the VCM and also specifies the target bit rate for the
203 // encoder. Bit rate used by NACK should already be compensated for by the user.
204 //
205 // Input:
206 // - target_bitrate : The target bitrate for VCM in bits/s.
207 // - lossRate : Fractions of lost packets the past second.
208 // (loss rate in percent = 100 * packetLoss / 255)
209 // - rtt : Current round-trip time in ms.
210 //
211 // Return value : VCM_OK, on success.
212 // < 0, on error.
213 virtual int32_t SetChannelParameters(uint32_t target_bitrate,
214 uint8_t lossRate,
215 int64_t rtt) = 0;
216
217 // Sets the parameters describing the receive channel. These parameters are inputs to the
218 // Media Optimization inside the VCM.
219 //
220 // Input:
221 // - rtt : Current round-trip time in ms.
222 // with the most amount available bandwidth in a conference
223 // scenario
224 //
225 // Return value : VCM_OK, on success.
226 // < 0, on error.
227 virtual int32_t SetReceiveChannelParameters(int64_t rtt) = 0;
228
229 // Register a transport callback which will be called to deliver the encoded data and
230 // side information.
231 //
232 // Input:
233 // - transport : The callback object to register.
234 //
235 // Return value : VCM_OK, on success.
236 // < 0, on error.
237 virtual int32_t RegisterTransportCallback(VCMPacketizationCallback* transport) = 0;
238
239 // Register video output information callback which will be called to deliver information
240 // about the video stream produced by the encoder, for instance the average frame rate and
241 // bit rate.
242 //
243 // Input:
244 // - outputInformation : The callback object to register.
245 //
246 // Return value : VCM_OK, on success.
247 // < 0, on error.
248 virtual int32_t RegisterSendStatisticsCallback(
249 VCMSendStatisticsCallback* sendStats) = 0;
250
251 // Register a video protection callback which will be called to deliver
252 // the requested FEC rate and NACK status (on/off).
253 //
254 // Input:
255 // - protection : The callback object to register.
256 //
257 // Return value : VCM_OK, on success.
258 // < 0, on error.
259 virtual int32_t RegisterProtectionCallback(VCMProtectionCallback* protection) = 0;
260
261 // Enable or disable a video protection method.
262 //
263 // Input:
264 // - videoProtection : The method to enable or disable.
265 // - enable : True if the method should be enabled, false if
266 // it should be disabled.
267 //
268 // Return value : VCM_OK, on success.
269 // < 0, on error.
270 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
271 bool enable) = 0;
272
273 // Add one raw video frame to the encoder. This function does all the necessary
274 // processing, then decides what frame type to encode, or if the frame should be
275 // dropped. If the frame should be encoded it passes the frame to the encoder
276 // before it returns.
277 //
278 // Input:
279 // - videoFrame : Video frame to encode.
280 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed in-band signaling.
281 //
282 // Return value : VCM_OK, on success.
283 // < 0, on error.
284 virtual int32_t AddVideoFrame(
285 const VideoFrame& videoFrame,
286 const VideoContentMetrics* contentMetrics = NULL,
287 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
288
289 // Next frame encoded should be an intra frame (keyframe).
290 //
291 // Return value : VCM_OK, on success.
292 // < 0, on error.
293 virtual int32_t IntraFrameRequest(int stream_index) = 0;
294
295 // Frame Dropper enable. Can be used to disable the frame dropping when the encoder
296 // over-uses its bit rate. This API is designed to be used when the encoded frames
297 // are supposed to be stored to an AVI file, or when the I420 codec is used and the
298 // target bit rate shouldn't affect the frame rate.
299 //
300 // Input:
301 // - enable : True to enable the setting, false to disable it.
302 //
303 // Return value : VCM_OK, on success.
304 // < 0, on error.
305 virtual int32_t EnableFrameDropper(bool enable) = 0;
306
307
308 /*
309 * Receiver
310 */
311
312 // Register possible receive codecs, can be called multiple times for different codecs.
313 // The module will automatically switch between registered codecs depending on the
314 // payload type of incoming frames. The actual decoder will be created when needed.
315 //
316 // Input:
317 // - receiveCodec : Settings for the codec to be registered.
318 // - numberOfCores : Number of CPU cores that the decoder is allowed to use.
319 // - requireKeyFrame : Set this to true if you don't want any delta frames
320 // to be decoded until the first key frame has been decoded.
321 //
322 // Return value : VCM_OK, on success.
323 // < 0, on error.
324 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
325 int32_t numberOfCores,
326 bool requireKeyFrame = false) = 0;
327
328 // Register an externally defined decoder/renderer object. Can be a decoder only or a
329 // decoder coupled with a renderer. Note that RegisterReceiveCodec must be called to
330 // be used for decoding incoming streams.
331 //
332 // Input:
333 // - externalDecoder : The external decoder/renderer object.
334 // - payloadType : The payload type which this decoder should be
335 // registered to.
perkj796cfaf2015-12-10 09:27:38 -0800336 //
Peter Boström795dbe42015-11-27 14:09:07 +0100337 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
perkj796cfaf2015-12-10 09:27:38 -0800338 uint8_t payloadType) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100339
340 // Register a receive callback. Will be called whenever there is a new frame ready
341 // for rendering.
342 //
343 // Input:
344 // - receiveCallback : The callback object to be used by the module when a
345 // frame is ready for rendering.
346 // De-register with a NULL pointer.
347 //
348 // Return value : VCM_OK, on success.
349 // < 0, on error.
350 virtual int32_t RegisterReceiveCallback(VCMReceiveCallback* receiveCallback) = 0;
351
352 // Register a receive statistics callback which will be called to deliver information
353 // about the video stream received by the receiving side of the VCM, for instance the
354 // average frame rate and bit rate.
355 //
356 // Input:
357 // - receiveStats : The callback object to register.
358 //
359 // Return value : VCM_OK, on success.
360 // < 0, on error.
361 virtual int32_t RegisterReceiveStatisticsCallback(
362 VCMReceiveStatisticsCallback* receiveStats) = 0;
363
364 // Register a decoder timing callback which will be called to deliver
365 // information about the timing of the decoder in the receiving side of the
366 // VCM, for instance the current and maximum frame decode latency.
367 //
368 // Input:
369 // - decoderTiming : The callback object to register.
370 //
371 // Return value : VCM_OK, on success.
372 // < 0, on error.
373 virtual int32_t RegisterDecoderTimingCallback(
374 VCMDecoderTimingCallback* decoderTiming) = 0;
375
376 // Register a frame type request callback. This callback will be called when the
377 // module needs to request specific frame types from the send side.
378 //
379 // Input:
380 // - frameTypeCallback : The callback object to be used by the module when
381 // requesting a specific type of frame from the send side.
382 // De-register with a NULL pointer.
383 //
384 // Return value : VCM_OK, on success.
385 // < 0, on error.
386 virtual int32_t RegisterFrameTypeCallback(
387 VCMFrameTypeCallback* frameTypeCallback) = 0;
388
389 // Registers a callback which is called whenever the receive side of the VCM
390 // encounters holes in the packet sequence and needs packets to be retransmitted.
391 //
392 // Input:
393 // - callback : The callback to be registered in the VCM.
394 //
395 // Return value : VCM_OK, on success.
396 // <0, on error.
397 virtual int32_t RegisterPacketRequestCallback(
398 VCMPacketRequestCallback* callback) = 0;
399
400 // Waits for the next frame in the jitter buffer to become complete
401 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for decoding.
402 // Should be called as often as possible to get the most out of the decoder.
403 //
404 // Return value : VCM_OK, on success.
405 // < 0, on error.
406 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
407
408 // Registers a callback which conveys the size of the render buffer.
409 virtual int RegisterRenderBufferSizeCallback(
410 VCMRenderBufferSizeCallback* callback) = 0;
411
412 // Reset the decoder state to the initial state.
413 //
414 // Return value : VCM_OK, on success.
415 // < 0, on error.
416 virtual int32_t ResetDecoder() = 0;
417
418 // API to get the codec which is currently used for decoding by the module.
419 //
420 // Input:
421 // - currentReceiveCodec : Settings for the codec to be registered.
422 //
423 // Return value : VCM_OK, on success.
424 // < 0, on error.
425 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const = 0;
426
427 // API to get the codec type currently used for decoding by the module.
428 //
429 // Return value : codecy type, on success.
430 // kVideoCodecUnknown, on error or if no receive codec is registered
431 virtual VideoCodecType ReceiveCodec() const = 0;
432
433 // Insert a parsed packet into the receiver side of the module. Will be placed in the
434 // jitter buffer waiting for the frame to become complete. Returns as soon as the packet
435 // has been placed in the jitter buffer.
436 //
437 // Input:
438 // - incomingPayload : Payload of the packet.
439 // - payloadLength : Length of the payload.
440 // - rtpInfo : The parsed header.
441 //
442 // Return value : VCM_OK, on success.
443 // < 0, on error.
444 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
445 size_t payloadLength,
446 const WebRtcRTPHeader& rtpInfo) = 0;
447
448 // Minimum playout delay (Used for lip-sync). This is the minimum delay required
449 // to sync with audio. Not included in VideoCodingModule::Delay()
450 // Defaults to 0 ms.
451 //
452 // Input:
453 // - minPlayoutDelayMs : Additional delay in ms.
454 //
455 // Return value : VCM_OK, on success.
456 // < 0, on error.
457 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0;
458
459 // Set the time required by the renderer to render a frame.
460 //
461 // Input:
462 // - timeMS : The time in ms required by the renderer to render a frame.
463 //
464 // Return value : VCM_OK, on success.
465 // < 0, on error.
466 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0;
467
468 // The total delay desired by the VCM. Can be less than the minimum
469 // delay set with SetMinimumPlayoutDelay.
470 //
471 // Return value : Total delay in ms, on success.
472 // < 0, on error.
473 virtual int32_t Delay() const = 0;
474
475 // Returns the number of packets discarded by the jitter buffer due to being
476 // too late. This can include duplicated packets which arrived after the
477 // frame was sent to the decoder. Therefore packets which were prematurely
478 // NACKed will be counted.
479 virtual uint32_t DiscardedPackets() const = 0;
480
481
482 // Robustness APIs
483
484 // Set the receiver robustness mode. The mode decides how the receiver
485 // responds to losses in the stream. The type of counter-measure (soft or
486 // hard NACK, dual decoder, RPS, etc.) is selected through the
487 // robustnessMode parameter. The errorMode parameter decides if it is
488 // allowed to display frames corrupted by losses. Note that not all
489 // combinations of the two parameters are feasible. An error will be
490 // returned for invalid combinations.
491 // Input:
492 // - robustnessMode : selected robustness mode.
493 // - errorMode : selected error mode.
494 //
495 // Return value : VCM_OK, on success;
496 // < 0, on error.
497 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
498 VCMDecodeErrorMode errorMode) = 0;
499
500 // Set the decode error mode. The mode decides which errors (if any) are
501 // allowed in decodable frames. Note that setting decode_error_mode to
502 // anything other than kWithErrors without enabling nack will cause
503 // long-term freezes (resulting from frequent key frame requests) if
504 // packet loss occurs.
505 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0;
506
507 // Sets the maximum number of sequence numbers that we are allowed to NACK
508 // and the oldest sequence number that we will consider to NACK. If a
509 // sequence number older than |max_packet_age_to_nack| is missing
510 // a key frame will be requested. A key frame will also be requested if the
511 // time of incomplete or non-continuous frames in the jitter buffer is above
512 // |max_incomplete_time_ms|.
513 virtual void SetNackSettings(size_t max_nack_list_size,
514 int max_packet_age_to_nack,
515 int max_incomplete_time_ms) = 0;
516
517 // Setting a desired delay to the VCM receiver. Video rendering will be
518 // delayed by at least desired_delay_ms.
519 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0;
520
521 // Lets the sender suspend video when the rate drops below
522 // |threshold_bps|, and turns back on when the rate goes back up above
523 // |threshold_bps| + |window_bps|.
524 virtual void SuspendBelowMinBitrate() = 0;
525
526 // Returns true if SuspendBelowMinBitrate is engaged and the video has been
527 // suspended due to bandwidth limitations; otherwise false.
528 virtual bool VideoSuspended() const = 0;
529
530 virtual void RegisterPreDecodeImageCallback(
531 EncodedImageCallback* observer) = 0;
532 virtual void RegisterPostEncodeImageCallback(
533 EncodedImageCallback* post_encode_callback) = 0;
534 // Releases pending decode calls, permitting faster thread shutdown.
535 virtual void TriggerDecoderShutdown() = 0;
536};
537
538} // namespace webrtc
539
540#endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_