blob: 062dda70806986d2497c4f8688021229e868d725 [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
Per69b332d2016-06-02 15:45:42 +0200191 // Deprecated: This method currently does not have any effect.
philipel9d3ab612015-12-21 04:12:39 -0800192 // Register a video protection callback which will be called to deliver
193 // the requested FEC rate and NACK status (on/off).
Per69b332d2016-06-02 15:45:42 +0200194 // TODO(perkj): Remove once no projects use it.
philipel9d3ab612015-12-21 04:12:39 -0800195 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,
philipel9d3ab612015-12-21 04:12:39 -0800226 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100227
philipel9d3ab612015-12-21 04:12:39 -0800228 // Next frame encoded should be an intra frame (keyframe).
229 //
230 // Return value : VCM_OK, on success.
231 // < 0, on error.
perkj600246e2016-05-04 11:26:51 -0700232 virtual int32_t IntraFrameRequest(size_t stream_index) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100233
philipel9d3ab612015-12-21 04:12:39 -0800234 // Frame Dropper enable. Can be used to disable the frame dropping when the
235 // encoder
236 // over-uses its bit rate. This API is designed to be used when the encoded
237 // frames
238 // are supposed to be stored to an AVI file, or when the I420 codec is used
239 // and the
240 // target bit rate shouldn't affect the frame rate.
241 //
242 // Input:
243 // - enable : True to enable the setting, false to disable it.
244 //
245 // Return value : VCM_OK, on success.
246 // < 0, on error.
247 virtual int32_t EnableFrameDropper(bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100248
philipel9d3ab612015-12-21 04:12:39 -0800249 /*
250 * Receiver
251 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100252
philipel9d3ab612015-12-21 04:12:39 -0800253 // Register possible receive codecs, can be called multiple times for
254 // different codecs.
255 // The module will automatically switch between registered codecs depending on
256 // the
257 // payload type of incoming frames. The actual decoder will be created when
258 // needed.
259 //
260 // Input:
261 // - receiveCodec : Settings for the codec to be registered.
262 // - numberOfCores : Number of CPU cores that the decoder is allowed
263 // to use.
264 // - requireKeyFrame : Set this to true if you don't want any delta
265 // frames
266 // to be decoded until the first key frame has been
267 // decoded.
268 //
269 // Return value : VCM_OK, on success.
270 // < 0, on error.
271 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
272 int32_t numberOfCores,
273 bool requireKeyFrame = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100274
philipel9d3ab612015-12-21 04:12:39 -0800275 // Register an externally defined decoder/renderer object. Can be a decoder
276 // only or a
277 // decoder coupled with a renderer. Note that RegisterReceiveCodec must be
278 // called to
279 // be used for decoding incoming streams.
280 //
281 // Input:
282 // - externalDecoder : The external decoder/renderer object.
283 // - payloadType : The payload type which this decoder should
284 // be
285 // registered to.
286 //
287 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
288 uint8_t payloadType) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100289
philipel9d3ab612015-12-21 04:12:39 -0800290 // Register a receive callback. Will be called whenever there is a new frame
291 // ready
292 // for rendering.
293 //
294 // Input:
295 // - receiveCallback : The callback object to be used by the
296 // module when a
297 // frame is ready for rendering.
298 // De-register with a NULL pointer.
299 //
300 // Return value : VCM_OK, on success.
301 // < 0, on error.
302 virtual int32_t RegisterReceiveCallback(
303 VCMReceiveCallback* receiveCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100304
philipel9d3ab612015-12-21 04:12:39 -0800305 // Register a receive statistics callback which will be called to deliver
306 // information
307 // about the video stream received by the receiving side of the VCM, for
308 // instance the
309 // average frame rate and bit rate.
310 //
311 // Input:
312 // - receiveStats : The callback object to register.
313 //
314 // Return value : VCM_OK, on success.
315 // < 0, on error.
316 virtual int32_t RegisterReceiveStatisticsCallback(
317 VCMReceiveStatisticsCallback* receiveStats) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100318
philipel9d3ab612015-12-21 04:12:39 -0800319 // Register a decoder timing callback which will be called to deliver
320 // information about the timing of the decoder in the receiving side of the
321 // VCM, for instance the current and maximum frame decode latency.
322 //
323 // Input:
324 // - decoderTiming : The callback object to register.
325 //
326 // Return value : VCM_OK, on success.
327 // < 0, on error.
328 virtual int32_t RegisterDecoderTimingCallback(
329 VCMDecoderTimingCallback* decoderTiming) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100330
philipel9d3ab612015-12-21 04:12:39 -0800331 // Register a frame type request callback. This callback will be called when
332 // the
333 // module needs to request specific frame types from the send side.
334 //
335 // Input:
336 // - frameTypeCallback : The callback object to be used by the
337 // module when
338 // requesting a specific type of frame from
339 // the send side.
340 // De-register with a NULL pointer.
341 //
342 // Return value : VCM_OK, on success.
343 // < 0, on error.
344 virtual int32_t RegisterFrameTypeCallback(
345 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100346
philipel9d3ab612015-12-21 04:12:39 -0800347 // Registers a callback which is called whenever the receive side of the VCM
348 // encounters holes in the packet sequence and needs packets to be
349 // retransmitted.
350 //
351 // Input:
352 // - callback : The callback to be registered in the VCM.
353 //
354 // Return value : VCM_OK, on success.
355 // <0, on error.
356 virtual int32_t RegisterPacketRequestCallback(
357 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100358
philipel9d3ab612015-12-21 04:12:39 -0800359 // Waits for the next frame in the jitter buffer to become complete
360 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
361 // decoding.
362 // Should be called as often as possible to get the most out of the decoder.
363 //
364 // Return value : VCM_OK, on success.
365 // < 0, on error.
366 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100367
philipel9d3ab612015-12-21 04:12:39 -0800368 // API to get the codec which is currently used for decoding by the module.
369 //
370 // Input:
371 // - currentReceiveCodec : Settings for the codec to be registered.
372 //
373 // Return value : VCM_OK, on success.
374 // < 0, on error.
375 virtual int32_t ReceiveCodec(VideoCodec* currentReceiveCodec) const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100376
philipel9d3ab612015-12-21 04:12:39 -0800377 // API to get the codec type currently used for decoding by the module.
378 //
379 // Return value : codecy type, on success.
380 // kVideoCodecUnknown, on error or if no receive codec is
381 // registered
382 virtual VideoCodecType ReceiveCodec() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100383
philipel9d3ab612015-12-21 04:12:39 -0800384 // Insert a parsed packet into the receiver side of the module. Will be placed
385 // in the
386 // jitter buffer waiting for the frame to become complete. Returns as soon as
387 // the packet
388 // has been placed in the jitter buffer.
389 //
390 // Input:
391 // - incomingPayload : Payload of the packet.
392 // - payloadLength : Length of the payload.
393 // - rtpInfo : The parsed header.
394 //
395 // Return value : VCM_OK, on success.
396 // < 0, on error.
397 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
398 size_t payloadLength,
399 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100400
philipel9d3ab612015-12-21 04:12:39 -0800401 // Minimum playout delay (Used for lip-sync). This is the minimum delay
402 // required
403 // to sync with audio. Not included in VideoCodingModule::Delay()
404 // Defaults to 0 ms.
405 //
406 // Input:
407 // - minPlayoutDelayMs : Additional delay in ms.
408 //
409 // Return value : VCM_OK, on success.
410 // < 0, on error.
411 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100412
philipel9d3ab612015-12-21 04:12:39 -0800413 // Set the time required by the renderer to render a frame.
414 //
415 // Input:
416 // - timeMS : The time in ms required by the renderer to render a
417 // frame.
418 //
419 // Return value : VCM_OK, on success.
420 // < 0, on error.
421 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100422
philipel9d3ab612015-12-21 04:12:39 -0800423 // The total delay desired by the VCM. Can be less than the minimum
424 // delay set with SetMinimumPlayoutDelay.
425 //
426 // Return value : Total delay in ms, on success.
427 // < 0, on error.
428 virtual int32_t Delay() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100429
philipel9d3ab612015-12-21 04:12:39 -0800430 // Returns the number of packets discarded by the jitter buffer due to being
431 // too late. This can include duplicated packets which arrived after the
432 // frame was sent to the decoder. Therefore packets which were prematurely
433 // NACKed will be counted.
434 virtual uint32_t DiscardedPackets() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100435
philipel9d3ab612015-12-21 04:12:39 -0800436 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100437
philipel9d3ab612015-12-21 04:12:39 -0800438 // Set the receiver robustness mode. The mode decides how the receiver
439 // responds to losses in the stream. The type of counter-measure (soft or
440 // hard NACK, dual decoder, RPS, etc.) is selected through the
441 // robustnessMode parameter. The errorMode parameter decides if it is
442 // allowed to display frames corrupted by losses. Note that not all
443 // combinations of the two parameters are feasible. An error will be
444 // returned for invalid combinations.
445 // Input:
446 // - robustnessMode : selected robustness mode.
447 // - errorMode : selected error mode.
448 //
449 // Return value : VCM_OK, on success;
450 // < 0, on error.
451 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
452 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100453
philipel9d3ab612015-12-21 04:12:39 -0800454 // Set the decode error mode. The mode decides which errors (if any) are
455 // allowed in decodable frames. Note that setting decode_error_mode to
456 // anything other than kWithErrors without enabling nack will cause
457 // long-term freezes (resulting from frequent key frame requests) if
458 // packet loss occurs.
459 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100460
philipel9d3ab612015-12-21 04:12:39 -0800461 // Sets the maximum number of sequence numbers that we are allowed to NACK
462 // and the oldest sequence number that we will consider to NACK. If a
463 // sequence number older than |max_packet_age_to_nack| is missing
464 // a key frame will be requested. A key frame will also be requested if the
465 // time of incomplete or non-continuous frames in the jitter buffer is above
466 // |max_incomplete_time_ms|.
467 virtual void SetNackSettings(size_t max_nack_list_size,
468 int max_packet_age_to_nack,
469 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100470
philipel9d3ab612015-12-21 04:12:39 -0800471 // Setting a desired delay to the VCM receiver. Video rendering will be
472 // delayed by at least desired_delay_ms.
473 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100474
philipel9d3ab612015-12-21 04:12:39 -0800475 virtual void RegisterPostEncodeImageCallback(
476 EncodedImageCallback* post_encode_callback) = 0;
477 // Releases pending decode calls, permitting faster thread shutdown.
478 virtual void TriggerDecoderShutdown() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100479};
480
481} // namespace webrtc
482
philipel9d3ab612015-12-21 04:12:39 -0800483#endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_