blob: 8ef046aa23193f7f30f2b1ecded11ff73c834d65 [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"
Niels Möllera46bd4b2018-06-08 14:03:44 +020026#include "api/video_codecs/video_codec.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/include/module.h"
28#include "modules/include/module_common_types.h"
29#include "modules/video_coding/include/video_coding_defines.h"
30#include "system_wrappers/include/event_wrapper.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010031
philipel9d3ab612015-12-21 04:12:39 -080032namespace webrtc {
Henrik Kjellander2557b862015-11-18 22:00:21 +010033
34class Clock;
35class EncodedImageCallback;
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +020036class VideoDecoder;
Henrik Kjellander2557b862015-11-18 22:00:21 +010037class VideoEncoder;
Henrik Kjellander2557b862015-11-18 22:00:21 +010038struct CodecSpecificInfo;
39
40class EventFactory {
41 public:
42 virtual ~EventFactory() {}
43
44 virtual EventWrapper* CreateEvent() = 0;
45};
46
47class EventFactoryImpl : public EventFactory {
48 public:
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020049 ~EventFactoryImpl() override {}
Henrik Kjellander2557b862015-11-18 22:00:21 +010050
Stefan Holmerdbdb3a02018-07-17 16:03:46 +020051 EventWrapper* CreateEvent() override;
Henrik Kjellander2557b862015-11-18 22:00:21 +010052};
53
54// Used to indicate which decode with errors mode should be used.
55enum VCMDecodeErrorMode {
philipel9d3ab612015-12-21 04:12:39 -080056 kNoErrors, // Never decode with errors. Video will freeze
57 // if nack is disabled.
58 kSelectiveErrors, // Frames that are determined decodable in
59 // VCMSessionInfo may be decoded with missing
60 // packets. As not all incomplete frames will be
61 // decodable, video will freeze if nack is disabled.
62 kWithErrors // Release frames as needed. Errors may be
63 // introduced as some encoded frames may not be
64 // complete.
Henrik Kjellander2557b862015-11-18 22:00:21 +010065};
66
philipel9d3ab612015-12-21 04:12:39 -080067class VideoCodingModule : public Module {
68 public:
69 enum SenderNackMode { kNackNone, kNackAll, kNackSelective };
Henrik Kjellander2557b862015-11-18 22:00:21 +010070
tommia5c18d72017-03-20 10:43:23 -070071 // DEPRECATED.
perkjf5b2e512016-07-05 08:34:04 -070072 static VideoCodingModule* Create(Clock* clock, EventFactory* event_factory);
Henrik Kjellander2557b862015-11-18 22:00:21 +010073
philipel9d3ab612015-12-21 04:12:39 -080074 /*
Yves Gerey665174f2018-06-19 15:03:05 +020075 * Sender
76 */
Henrik Kjellander2557b862015-11-18 22:00:21 +010077
philipel9d3ab612015-12-21 04:12:39 -080078 // Registers a codec to be used for encoding. Calling this
79 // API multiple times overwrites any previously registered codecs.
80 //
81 // NOTE: Must be called on the thread that constructed the VCM instance.
82 //
83 // Input:
84 // - sendCodec : Settings for the codec to be registered.
85 // - numberOfCores : The number of cores the codec is allowed
86 // to use.
87 // - maxPayloadSize : The maximum size each payload is allowed
88 // to have. Usually MTU - overhead.
89 //
90 // Return value : VCM_OK, on success.
91 // < 0, on error.
92 virtual int32_t RegisterSendCodec(const VideoCodec* sendCodec,
93 uint32_t numberOfCores,
94 uint32_t maxPayloadSize) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +010095
philipel9d3ab612015-12-21 04:12:39 -080096 // Register an external encoder object. This can not be used together with
97 // external decoder callbacks.
98 //
99 // Input:
100 // - externalEncoder : Encoder object to be used for encoding frames
101 // inserted
102 // with the AddVideoFrame API.
103 // - payloadType : The payload type bound which this encoder is bound
104 // to.
105 //
106 // Return value : VCM_OK, on success.
107 // < 0, on error.
108 // TODO(pbos): Remove return type when unused elsewhere.
109 virtual int32_t RegisterExternalEncoder(VideoEncoder* externalEncoder,
110 uint8_t payloadType,
111 bool internalSource = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100112
philipel9d3ab612015-12-21 04:12:39 -0800113 // Sets the parameters describing the send channel. These parameters are
114 // inputs to the
115 // Media Optimization inside the VCM and also specifies the target bit rate
116 // for the
117 // encoder. Bit rate used by NACK should already be compensated for by the
118 // user.
119 //
120 // Input:
121 // - target_bitrate : The target bitrate for VCM in bits/s.
122 // - lossRate : Fractions of lost packets the past second.
123 // (loss rate in percent = 100 * packetLoss /
124 // 255)
125 // - rtt : Current round-trip time in ms.
126 //
127 // Return value : VCM_OK, on success.
128 // < 0, on error.
129 virtual int32_t SetChannelParameters(uint32_t target_bitrate,
130 uint8_t lossRate,
131 int64_t rtt) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100132
philipel9d3ab612015-12-21 04:12:39 -0800133 // Enable or disable a video protection method.
134 //
135 // Input:
136 // - videoProtection : The method to enable or disable.
137 // - enable : True if the method should be enabled, false if
138 // it should be disabled.
139 //
140 // Return value : VCM_OK, on success.
141 // < 0, on error.
142 virtual int32_t SetVideoProtection(VCMVideoProtection videoProtection,
143 bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100144
philipel9d3ab612015-12-21 04:12:39 -0800145 // Add one raw video frame to the encoder. This function does all the
146 // necessary
147 // processing, then decides what frame type to encode, or if the frame should
148 // be
149 // dropped. If the frame should be encoded it passes the frame to the encoder
150 // before it returns.
151 //
152 // Input:
153 // - videoFrame : Video frame to encode.
154 // - codecSpecificInfo : Extra codec information, e.g., pre-parsed
155 // in-band signaling.
156 //
157 // Return value : VCM_OK, on success.
158 // < 0, on error.
159 virtual int32_t AddVideoFrame(
160 const VideoFrame& videoFrame,
philipel9d3ab612015-12-21 04:12:39 -0800161 const CodecSpecificInfo* codecSpecificInfo = NULL) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100162
philipel9d3ab612015-12-21 04:12:39 -0800163 // Next frame encoded should be an intra frame (keyframe).
164 //
165 // Return value : VCM_OK, on success.
166 // < 0, on error.
perkj600246e2016-05-04 11:26:51 -0700167 virtual int32_t IntraFrameRequest(size_t stream_index) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100168
philipel9d3ab612015-12-21 04:12:39 -0800169 // Frame Dropper enable. Can be used to disable the frame dropping when the
170 // encoder
171 // over-uses its bit rate. This API is designed to be used when the encoded
172 // frames
173 // are supposed to be stored to an AVI file, or when the I420 codec is used
174 // and the
175 // target bit rate shouldn't affect the frame rate.
176 //
177 // Input:
178 // - enable : True to enable the setting, false to disable it.
179 //
180 // Return value : VCM_OK, on success.
181 // < 0, on error.
182 virtual int32_t EnableFrameDropper(bool enable) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100183
philipel9d3ab612015-12-21 04:12:39 -0800184 /*
Yves Gerey665174f2018-06-19 15:03:05 +0200185 * Receiver
186 */
Henrik Kjellander2557b862015-11-18 22:00:21 +0100187
philipel9d3ab612015-12-21 04:12:39 -0800188 // Register possible receive codecs, can be called multiple times for
189 // different codecs.
190 // The module will automatically switch between registered codecs depending on
191 // the
192 // payload type of incoming frames. The actual decoder will be created when
193 // needed.
194 //
195 // Input:
196 // - receiveCodec : Settings for the codec to be registered.
197 // - numberOfCores : Number of CPU cores that the decoder is allowed
198 // to use.
199 // - requireKeyFrame : Set this to true if you don't want any delta
200 // frames
201 // to be decoded until the first key frame has been
202 // decoded.
203 //
204 // Return value : VCM_OK, on success.
205 // < 0, on error.
206 virtual int32_t RegisterReceiveCodec(const VideoCodec* receiveCodec,
207 int32_t numberOfCores,
208 bool requireKeyFrame = false) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100209
Anders Carlsson7eb8e9f2018-05-18 10:33:04 +0200210 // Register an external decoder object.
211 //
212 // Input:
213 // - externalDecoder : Decoder object to be used for decoding frames.
214 // - payloadType : The payload type which this decoder is bound to.
215 virtual void RegisterExternalDecoder(VideoDecoder* externalDecoder,
216 uint8_t payloadType) = 0;
217
philipel9d3ab612015-12-21 04:12:39 -0800218 // Register a receive callback. Will be called whenever there is a new frame
219 // ready
220 // for rendering.
221 //
222 // Input:
223 // - receiveCallback : The callback object to be used by the
224 // module when a
225 // frame is ready for rendering.
226 // De-register with a NULL pointer.
227 //
228 // Return value : VCM_OK, on success.
229 // < 0, on error.
230 virtual int32_t RegisterReceiveCallback(
231 VCMReceiveCallback* receiveCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100232
philipel9d3ab612015-12-21 04:12:39 -0800233 // Register a frame type request callback. This callback will be called when
234 // the
235 // module needs to request specific frame types from the send side.
236 //
237 // Input:
238 // - frameTypeCallback : The callback object to be used by the
239 // module when
240 // requesting a specific type of frame from
241 // the send side.
242 // De-register with a NULL pointer.
243 //
244 // Return value : VCM_OK, on success.
245 // < 0, on error.
246 virtual int32_t RegisterFrameTypeCallback(
247 VCMFrameTypeCallback* frameTypeCallback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100248
philipel9d3ab612015-12-21 04:12:39 -0800249 // Registers a callback which is called whenever the receive side of the VCM
250 // encounters holes in the packet sequence and needs packets to be
251 // retransmitted.
252 //
253 // Input:
254 // - callback : The callback to be registered in the VCM.
255 //
256 // Return value : VCM_OK, on success.
257 // <0, on error.
258 virtual int32_t RegisterPacketRequestCallback(
259 VCMPacketRequestCallback* callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100260
philipel9d3ab612015-12-21 04:12:39 -0800261 // Waits for the next frame in the jitter buffer to become complete
262 // (waits no longer than maxWaitTimeMs), then passes it to the decoder for
263 // decoding.
264 // Should be called as often as possible to get the most out of the decoder.
265 //
266 // Return value : VCM_OK, on success.
267 // < 0, on error.
268 virtual int32_t Decode(uint16_t maxWaitTimeMs = 200) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100269
philipel9d3ab612015-12-21 04:12:39 -0800270 // Insert a parsed packet into the receiver side of the module. Will be placed
271 // in the
272 // jitter buffer waiting for the frame to become complete. Returns as soon as
273 // the packet
274 // has been placed in the jitter buffer.
275 //
276 // Input:
277 // - incomingPayload : Payload of the packet.
278 // - payloadLength : Length of the payload.
279 // - rtpInfo : The parsed header.
280 //
281 // Return value : VCM_OK, on success.
282 // < 0, on error.
283 virtual int32_t IncomingPacket(const uint8_t* incomingPayload,
284 size_t payloadLength,
285 const WebRtcRTPHeader& rtpInfo) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100286
philipel9d3ab612015-12-21 04:12:39 -0800287 // Robustness APIs
Henrik Kjellander2557b862015-11-18 22:00:21 +0100288
tommia5c18d72017-03-20 10:43:23 -0700289 // DEPRECATED.
philipel9d3ab612015-12-21 04:12:39 -0800290 // Set the receiver robustness mode. The mode decides how the receiver
tommia5c18d72017-03-20 10:43:23 -0700291 // responds to losses in the stream. The type of counter-measure is selected
292 // through the robustnessMode parameter. The errorMode parameter decides if it
293 // is allowed to display frames corrupted by losses. Note that not all
philipel9d3ab612015-12-21 04:12:39 -0800294 // combinations of the two parameters are feasible. An error will be
295 // returned for invalid combinations.
296 // Input:
297 // - robustnessMode : selected robustness mode.
298 // - errorMode : selected error mode.
299 //
300 // Return value : VCM_OK, on success;
301 // < 0, on error.
tommia5c18d72017-03-20 10:43:23 -0700302 enum ReceiverRobustness { kNone, kHardNack };
philipel9d3ab612015-12-21 04:12:39 -0800303 virtual int SetReceiverRobustnessMode(ReceiverRobustness robustnessMode,
304 VCMDecodeErrorMode errorMode) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100305
philipel9d3ab612015-12-21 04:12:39 -0800306 // Sets the maximum number of sequence numbers that we are allowed to NACK
307 // and the oldest sequence number that we will consider to NACK. If a
308 // sequence number older than |max_packet_age_to_nack| is missing
309 // a key frame will be requested. A key frame will also be requested if the
310 // time of incomplete or non-continuous frames in the jitter buffer is above
311 // |max_incomplete_time_ms|.
312 virtual void SetNackSettings(size_t max_nack_list_size,
313 int max_packet_age_to_nack,
314 int max_incomplete_time_ms) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100315
philipel9d3ab612015-12-21 04:12:39 -0800316 virtual void RegisterPostEncodeImageCallback(
317 EncodedImageCallback* post_encode_callback) = 0;
Henrik Kjellander2557b862015-11-18 22:00:21 +0100318};
319
320} // namespace webrtc
321
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200322#endif // MODULES_VIDEO_CODING_INCLUDE_VIDEO_CODING_H_