blob: 5e02eab5750350dbac6936292a5e5104ca517b31 [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
nisseaf916892017-01-10 07:44:26 -080024#include "webrtc/api/video/video_frame.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010025#include "webrtc/modules/include/module.h"
26#include "webrtc/modules/include/module_common_types.h"
27#include "webrtc/modules/video_coding/include/video_coding_defines.h"
28#include "webrtc/system_wrappers/include/event_wrapper.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010029
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 frame type request callback. This callback will be called when
315 // the
316 // module needs to request specific frame types from the send side.
317 //
318 // Input:
319 // - frameTypeCallback : The callback object to be used by the
320 // module when
321 // requesting a specific type of frame from
322 // the send side.
323 // De-register with a NULL pointer.
324 //
325 // Return value : VCM_OK, on success.
326 // < 0, on error.
327 virtual int32_t RegisterFrameTypeCallback(
328 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100329
philipel9d3ab612015-12-21 04:12:39 -0800330 // Registers a callback which is called whenever the receive side of the VCM
331 // encounters holes in the packet sequence and needs packets to be
332 // retransmitted.
333 //
334 // Input:
335 // - callback : The callback to be registered in the VCM.
336 //
337 // Return value : VCM_OK, on success.
338 // <0, on error.
339 virtual int32_t RegisterPacketRequestCallback(
340 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100341
philipel9d3ab612015-12-21 04:12:39 -0800342 // Waits for the next frame in the jitter buffer to become complete
343 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
344 // decoding.
345 // Should be called as often as possible to get the most out of the decoder.
346 //
347 // Return value : VCM_OK, on success.
348 // < 0, on error.
349 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100350
philipel9d3ab612015-12-21 04:12:39 -0800351 // Insert a parsed packet into the receiver side of the module. Will be placed
352 // in the
353 // jitter buffer waiting for the frame to become complete. Returns as soon as
354 // the packet
355 // has been placed in the jitter buffer.
356 //
357 // Input:
358 // - incomingPayload : Payload of the packet.
359 // - payloadLength : Length of the payload.
360 // - rtpInfo : The parsed header.
361 //
362 // Return value : VCM_OK, on success.
363 // < 0, on error.
364 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
365 size_t payloadLength,
366 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100367
philipel9d3ab612015-12-21 04:12:39 -0800368 // Minimum playout delay (Used for lip-sync). This is the minimum delay
369 // required
370 // to sync with audio. Not included in VideoCodingModule::Delay()
371 // Defaults to 0 ms.
372 //
373 // Input:
374 // - minPlayoutDelayMs : Additional delay in ms.
375 //
376 // Return value : VCM_OK, on success.
377 // < 0, on error.
378 virtual int32_t SetMinimumPlayoutDelay(uint32_t minPlayoutDelayMs) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100379
philipel9d3ab612015-12-21 04:12:39 -0800380 // Set the time required by the renderer to render a frame.
381 //
382 // Input:
383 // - timeMS : The time in ms required by the renderer to render a
384 // frame.
385 //
386 // Return value : VCM_OK, on success.
387 // < 0, on error.
388 virtual int32_t SetRenderDelay(uint32_t timeMS) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100389
philipel9d3ab612015-12-21 04:12:39 -0800390 // The total delay desired by the VCM. Can be less than the minimum
391 // delay set with SetMinimumPlayoutDelay.
392 //
393 // Return value : Total delay in ms, on success.
394 // < 0, on error.
395 virtual int32_t Delay() const = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100396
philipel9d3ab612015-12-21 04:12:39 -0800397 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100398
philipel9d3ab612015-12-21 04:12:39 -0800399 // Set the receiver robustness mode. The mode decides how the receiver
400 // responds to losses in the stream. The type of counter-measure (soft or
401 // hard NACK, dual decoder, RPS, etc.) is selected through the
402 // robustnessMode parameter. The errorMode parameter decides if it is
403 // allowed to display frames corrupted by losses. Note that not all
404 // combinations of the two parameters are feasible. An error will be
405 // returned for invalid combinations.
406 // Input:
407 // - robustnessMode : selected robustness mode.
408 // - errorMode : selected error mode.
409 //
410 // Return value : VCM_OK, on success;
411 // < 0, on error.
412 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
413 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100414
philipel9d3ab612015-12-21 04:12:39 -0800415 // Set the decode error mode. The mode decides which errors (if any) are
416 // allowed in decodable frames. Note that setting decode_error_mode to
417 // anything other than kWithErrors without enabling nack will cause
418 // long-term freezes (resulting from frequent key frame requests) if
419 // packet loss occurs.
420 virtual void SetDecodeErrorMode(VCMDecodeErrorMode decode_error_mode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100421
philipel9d3ab612015-12-21 04:12:39 -0800422 // Sets the maximum number of sequence numbers that we are allowed to NACK
423 // and the oldest sequence number that we will consider to NACK. If a
424 // sequence number older than |max_packet_age_to_nack| is missing
425 // a key frame will be requested. A key frame will also be requested if the
426 // time of incomplete or non-continuous frames in the jitter buffer is above
427 // |max_incomplete_time_ms|.
428 virtual void SetNackSettings(size_t max_nack_list_size,
429 int max_packet_age_to_nack,
430 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100431
philipel9d3ab612015-12-21 04:12:39 -0800432 // Setting a desired delay to the VCM receiver. Video rendering will be
433 // delayed by at least desired_delay_ms.
434 virtual int SetMinReceiverDelay(int desired_delay_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100435
philipel9d3ab612015-12-21 04:12:39 -0800436 virtual void RegisterPostEncodeImageCallback(
437 EncodedImageCallback* post_encode_callback) = 0;
438 // Releases pending decode calls, permitting faster thread shutdown.
439 virtual void TriggerDecoderShutdown() = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100440};
441
442} // namespace webrtc
443
philipel9d3ab612015-12-21 04:12:39 -0800444#endif // WEBRTC_MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_