blob: af03f1ea1126e4165f231e8f7a53b1539223913f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_
12#define MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_
Henrik Kjellander2557b862015-11-18 22:00:21 +010013
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
Ying Wang3b790f32018-01-19 17:58:57 +010024#include "api/fec_controller.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "api/video/video_frame.h"
26#include "modules/include/module.h"
27#include "modules/include/module_common_types.h"
28#include "modules/video_coding/include/video_coding_defines.h"
29#include "system_wrappers/include/event_wrapper.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010030
philipel9d3ab612015-12-21 04:12:39 -080031namespace webrtc {
Henrik Kjellander2557b862015-11-18 22:00:21 +010032
33class Clock;
34class EncodedImageCallback;
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +020035class VideoDecoder;
Henrik Kjellander2557b862015-11-18 22:00:21 +010036class VideoEncoder;
Henrik Kjellander2557b862015-11-18 22:00:21 +010037struct 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
philipel9d3ab612015-12-21 04:12:39 -080050 virtual EventWrapper* CreateEvent() { return EventWrapper::Create(); }
Henrik Kjellander2557b862015-11-18 22:00:21 +010051};
52
53// Used to indicate which decode with errors mode should be used.
54enum VCMDecodeErrorMode {
philipel9d3ab612015-12-21 04:12:39 -080055 kNoErrors, // Never decode with errors. Video will freeze
56 // if nack is disabled.
57 kSelectiveErrors, // Frames that are determined decodable in
58 // VCMSessionInfo may be decoded with missing
59 // packets. As not all incomplete frames will be
60 // decodable, video will freeze if nack is disabled.
61 kWithErrors // Release frames as needed. Errors may be
62 // introduced as some encoded frames may not be
63 // complete.
Henrik Kjellander2557b862015-11-18 22:00:21 +010064};
65
philipel9d3ab612015-12-21 04:12:39 -080066class VideoCodingModule : public Module {
67 public:
68 enum SenderNackMode { kNackNone, kNackAll, kNackSelective };
Henrik Kjellander2557b862015-11-18 22:00:21 +010069
tommia5c18d72017-03-20 10:43:23 -070070 // DEPRECATED.
perkjf5b2e512016-07-05 08:34:04 -070071 static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory);
Henrik Kjellander2557b862015-11-18 22:00:21 +010072
philipel9d3ab612015-12-21 04:12:39 -080073 /*
74 * Sender
75 */
Henrik Kjellander2557b862015-11-18 22:00:21 +010076
philipel9d3ab612015-12-21 04:12:39 -080077 // Registers a codec to be used for encoding. Calling this
78 // API multiple times overwrites any previously registered codecs.
79 //
80 // NOTE: Must be called on the thread that constructed the VCM instance.
81 //
82 // Input:
83 // - sendCodec : Settings for the codec to be registered.
84 // - numberOfCores : The number of cores the codec is allowed
85 // to use.
86 // - maxPayloadSize : The maximum size each payload is allowed
87 // to have. Usually MTU - overhead.
88 //
89 // Return value : VCM_OK, on success.
90 // < 0, on error.
91 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec,
92 uint32_t numberOfCores,
93 uint32_t maxPayloadSize) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +010094
philipel9d3ab612015-12-21 04:12:39 -080095 // Register an external encoder object. This can not be used together with
96 // external decoder callbacks.
97 //
98 // Input:
99 // - externalEncoder : Encoder object to be used for encoding frames
100 // inserted
101 // with the AddVideoFrame API.
102 // - payloadType : The payload type bound which this encoder is bound
103 // to.
104 //
105 // Return value : VCM_OK, on success.
106 // < 0, on error.
107 // TODO(pbos): Remove return type when unused elsewhere.
108 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
109 uint8_t payloadType,
110 bool internalSource = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100111
philipel9d3ab612015-12-21 04:12:39 -0800112 // Sets the parameters describing the send channel. These parameters are
113 // inputs to the
114 // Media Optimization inside the VCM and also specifies the target bit rate
115 // for the
116 // encoder. Bit rate used by NACK should already be compensated for by the
117 // user.
118 //
119 // Input:
120 // - target_bitrate : The target bitrate for VCM in bits/s.
121 // - lossRate : Fractions of lost packets the past second.
122 // (loss rate in percent = 100 * packetLoss /
123 // 255)
124 // - rtt : Current round-trip time in ms.
125 //
126 // Return value : VCM_OK, on success.
127 // < 0, on error.
128 virtual int32_t SetChannelParameters(uint32_t target_bitrate,
129 uint8_t lossRate,
130 int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100131
philipel9d3ab612015-12-21 04:12:39 -0800132 // Enable or disable a video protection method.
133 //
134 // Input:
135 // - videoProtection : The method to enable or disable.
136 // - enable : True if the method should be enabled, false if
137 // it should be disabled.
138 //
139 // Return value : VCM_OK, on success.
140 // < 0, on error.
141 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
142 bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100143
philipel9d3ab612015-12-21 04:12:39 -0800144 // Add one raw video frame to the encoder. This function does all the
145 // necessary
146 // processing, then decides what frame type to encode, or if the frame should
147 // be
148 // dropped. If the frame should be encoded it passes the frame to the encoder
149 // before it returns.
150 //
151 // Input:
152 // - videoFrame : Video frame to encode.
153 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed
154 // in-band signaling.
155 //
156 // Return value : VCM_OK, on success.
157 // < 0, on error.
158 virtual int32_t AddVideoFrame(
159 const VideoFrame& videoFrame,
philipel9d3ab612015-12-21 04:12:39 -0800160 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100161
philipel9d3ab612015-12-21 04:12:39 -0800162 // Next frame encoded should be an intra frame (keyframe).
163 //
164 // Return value : VCM_OK, on success.
165 // < 0, on error.
perkj600246e2016-05-04 11:26:51 -0700166 virtual int32_t IntraFrameRequest(size_t stream_index) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100167
philipel9d3ab612015-12-21 04:12:39 -0800168 // Frame Dropper enable. Can be used to disable the frame dropping when the
169 // encoder
170 // over-uses its bit rate. This API is designed to be used when the encoded
171 // frames
172 // are supposed to be stored to an AVI file, or when the I420 codec is used
173 // and the
174 // target bit rate shouldn't affect the frame rate.
175 //
176 // Input:
177 // - enable : True to enable the setting, false to disable it.
178 //
179 // Return value : VCM_OK, on success.
180 // < 0, on error.
181 virtual int32_t EnableFrameDropper(bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100182
philipel9d3ab612015-12-21 04:12:39 -0800183 /*
184 * Receiver
185 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100186
philipel9d3ab612015-12-21 04:12:39 -0800187 // Register possible receive codecs, can be called multiple times for
188 // different codecs.
189 // The module will automatically switch between registered codecs depending on
190 // the
191 // payload type of incoming frames. The actual decoder will be created when
192 // needed.
193 //
194 // Input:
195 // - receiveCodec : Settings for the codec to be registered.
196 // - numberOfCores : Number of CPU cores that the decoder is allowed
197 // to use.
198 // - requireKeyFrame : Set this to true if you don't want any delta
199 // frames
200 // to be decoded until the first key frame has been
201 // decoded.
202 //
203 // Return value : VCM_OK, on success.
204 // < 0, on error.
205 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
206 int32_t numberOfCores,
207 bool requireKeyFrame = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100208
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +0200209 // Register an external decoder object.
210 //
211 // Input:
212 // - externalDecoder : Decoder object to be used for decoding frames.
213 // - payloadType : The payload type which this decoder is bound to.
214 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
215 uint8_t payloadType) = 0;
216
philipel9d3ab612015-12-21 04:12:39 -0800217 // Register a receive callback. Will be called whenever there is a new frame
218 // ready
219 // for rendering.
220 //
221 // Input:
222 // - receiveCallback : The callback object to be used by the
223 // module when a
224 // frame is ready for rendering.
225 // De-register with a NULL pointer.
226 //
227 // Return value : VCM_OK, on success.
228 // < 0, on error.
229 virtual int32_t RegisterReceiveCallback(
230 VCMReceiveCallback* receiveCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100231
philipel9d3ab612015-12-21 04:12:39 -0800232 // Register a frame type request callback. This callback will be called when
233 // the
234 // module needs to request specific frame types from the send side.
235 //
236 // Input:
237 // - frameTypeCallback : The callback object to be used by the
238 // module when
239 // requesting a specific type of frame from
240 // the send side.
241 // De-register with a NULL pointer.
242 //
243 // Return value : VCM_OK, on success.
244 // < 0, on error.
245 virtual int32_t RegisterFrameTypeCallback(
246 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100247
philipel9d3ab612015-12-21 04:12:39 -0800248 // Registers a callback which is called whenever the receive side of the VCM
249 // encounters holes in the packet sequence and needs packets to be
250 // retransmitted.
251 //
252 // Input:
253 // - callback : The callback to be registered in the VCM.
254 //
255 // Return value : VCM_OK, on success.
256 // <0, on error.
257 virtual int32_t RegisterPacketRequestCallback(
258 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100259
philipel9d3ab612015-12-21 04:12:39 -0800260 // Waits for the next frame in the jitter buffer to become complete
261 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
262 // decoding.
263 // Should be called as often as possible to get the most out of the decoder.
264 //
265 // Return value : VCM_OK, on success.
266 // < 0, on error.
267 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100268
philipel9d3ab612015-12-21 04:12:39 -0800269 // Insert a parsed packet into the receiver side of the module. Will be placed
270 // in the
271 // jitter buffer waiting for the frame to become complete. Returns as soon as
272 // the packet
273 // has been placed in the jitter buffer.
274 //
275 // Input:
276 // - incomingPayload : Payload of the packet.
277 // - payloadLength : Length of the payload.
278 // - rtpInfo : The parsed header.
279 //
280 // Return value : VCM_OK, on success.
281 // < 0, on error.
282 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
283 size_t payloadLength,
284 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100285
philipel9d3ab612015-12-21 04:12:39 -0800286 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100287
tommia5c18d72017-03-20 10:43:23 -0700288 // DEPRECATED.
philipel9d3ab612015-12-21 04:12:39 -0800289 // Set the receiver robustness mode. The mode decides how the receiver
tommia5c18d72017-03-20 10:43:23 -0700290 // responds to losses in the stream. The type of counter-measure is selected
291 // through the robustnessMode parameter. The errorMode parameter decides if it
292 // is allowed to display frames corrupted by losses. Note that not all
philipel9d3ab612015-12-21 04:12:39 -0800293 // combinations of the two parameters are feasible. An error will be
294 // returned for invalid combinations.
295 // Input:
296 // - robustnessMode : selected robustness mode.
297 // - errorMode : selected error mode.
298 //
299 // Return value : VCM_OK, on success;
300 // < 0, on error.
tommia5c18d72017-03-20 10:43:23 -0700301 enum ReceiverRobustness { kNone, kHardNack };
philipel9d3ab612015-12-21 04:12:39 -0800302 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
303 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100304
philipel9d3ab612015-12-21 04:12:39 -0800305 // Sets the maximum number of sequence numbers that we are allowed to NACK
306 // and the oldest sequence number that we will consider to NACK. If a
307 // sequence number older than |max_packet_age_to_nack| is missing
308 // a key frame will be requested. A key frame will also be requested if the
309 // time of incomplete or non-continuous frames in the jitter buffer is above
310 // |max_incomplete_time_ms|.
311 virtual void SetNackSettings(size_t max_nack_list_size,
312 int max_packet_age_to_nack,
313 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100314
philipel9d3ab612015-12-21 04:12:39 -0800315 virtual void RegisterPostEncodeImageCallback(
316 EncodedImageCallback* post_encode_callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100317};
318
319} // namespace webrtc
320
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200321#endif // MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_