blob: eb401afded9fbe5b2e40bc7ea0788b7352b90251 [file] [log] [blame]
ilnikd60d06a2017-04-05 03:02:20 -07001/*
2 * Copyright (c) 2014 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 API_VIDEO_CODECS_VIDEO_ENCODER_H_
12#define API_VIDEO_CODECS_VIDEO_ENCODER_H_
ilnikd60d06a2017-04-05 03:02:20 -070013
14#include <memory>
15#include <string>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/optional.h"
19#include "api/video/video_frame.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020020#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_video/include/video_frame.h"
22#include "rtc_base/checks.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020023#include "typedefs.h" // NOLINT(build/include)
ilnikd60d06a2017-04-05 03:02:20 -070024
25namespace webrtc {
26
27class RTPFragmentationHeader;
28// TODO(pbos): Expose these through a public (root) header or change these APIs.
29struct CodecSpecificInfo;
30class VideoCodec;
31
32class EncodedImageCallback {
33 public:
34 virtual ~EncodedImageCallback() {}
35
36 struct Result {
37 enum Error {
38 OK,
39
40 // Failed to send the packet.
41 ERROR_SEND_FAILED,
42 };
43
mflodman351424e2017-08-10 02:43:14 -070044 explicit Result(Error error) : error(error) {}
ilnikd60d06a2017-04-05 03:02:20 -070045 Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {}
46
47 Error error;
48
49 // Frame ID assigned to the frame. The frame ID should be the same as the ID
50 // seen by the receiver for this frame. RTP timestamp of the frame is used
51 // as frame ID when RTP is used to send video. Must be used only when
52 // error=OK.
53 uint32_t frame_id = 0;
54
55 // Tells the encoder that the next frame is should be dropped.
56 bool drop_next_frame = false;
57 };
58
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020059 // Used to signal the encoder about reason a frame is dropped.
60 // kDroppedByMediaOptimizations - dropped by MediaOptimizations (for rate
61 // limiting purposes).
62 // kDroppedByEncoder - dropped by encoder's internal rate limiter.
63 enum class DropReason : uint8_t {
64 kDroppedByMediaOptimizations,
65 kDroppedByEncoder
66 };
67
ilnikd60d06a2017-04-05 03:02:20 -070068 // Callback function which is called when an image has been encoded.
69 virtual Result OnEncodedImage(
70 const EncodedImage& encoded_image,
71 const CodecSpecificInfo* codec_specific_info,
72 const RTPFragmentationHeader* fragmentation) = 0;
73
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020074 // Deprecated. TODO(ilnik): Remove this in few weeks.
ilnikd60d06a2017-04-05 03:02:20 -070075 virtual void OnDroppedFrame() {}
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020076
77 virtual void OnDroppedFrame(DropReason reason) {}
ilnikd60d06a2017-04-05 03:02:20 -070078};
79
80class VideoEncoder {
81 public:
ilnikd60d06a2017-04-05 03:02:20 -070082 struct QpThresholds {
83 QpThresholds(int l, int h) : low(l), high(h) {}
84 QpThresholds() : low(-1), high(-1) {}
85 int low;
86 int high;
87 };
88 struct ScalingSettings {
mflodman351424e2017-08-10 02:43:14 -070089 ScalingSettings(bool on, int low, int high);
asapersson142fcc92017-08-17 08:58:54 -070090 ScalingSettings(bool on, int low, int high, int min_pixels);
91 ScalingSettings(bool on, int min_pixels);
mflodman351424e2017-08-10 02:43:14 -070092 explicit ScalingSettings(bool on);
93 ScalingSettings(const ScalingSettings&);
94 ~ScalingSettings();
95
ilnikd60d06a2017-04-05 03:02:20 -070096 const bool enabled;
97 const rtc::Optional<QpThresholds> thresholds;
asapersson142fcc92017-08-17 08:58:54 -070098
99 // We will never ask for a resolution lower than this.
100 // TODO(kthelgason): Lower this limit when better testing
101 // on MediaCodec and fallback implementations are in place.
102 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206
103 const int min_pixels_per_frame = 320 * 180;
ilnikd60d06a2017-04-05 03:02:20 -0700104 };
ilnikd60d06a2017-04-05 03:02:20 -0700105
106 static VideoCodecVP8 GetDefaultVp8Settings();
107 static VideoCodecVP9 GetDefaultVp9Settings();
108 static VideoCodecH264 GetDefaultH264Settings();
109
110 virtual ~VideoEncoder() {}
111
112 // Initialize the encoder with the information from the codecSettings
113 //
114 // Input:
115 // - codec_settings : Codec settings
116 // - number_of_cores : Number of cores available for the encoder
117 // - max_payload_size : The maximum size each payload is allowed
118 // to have. Usually MTU - overhead.
119 //
120 // Return value : Set bit rate if OK
121 // <0 - Errors:
122 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
123 // WEBRTC_VIDEO_CODEC_ERR_SIZE
124 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
125 // WEBRTC_VIDEO_CODEC_MEMORY
126 // WEBRTC_VIDEO_CODEC_ERROR
127 virtual int32_t InitEncode(const VideoCodec* codec_settings,
128 int32_t number_of_cores,
129 size_t max_payload_size) = 0;
130
131 // Register an encode complete callback object.
132 //
133 // Input:
134 // - callback : Callback object which handles encoded images.
135 //
136 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
137 virtual int32_t RegisterEncodeCompleteCallback(
138 EncodedImageCallback* callback) = 0;
139
140 // Free encoder memory.
141 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
142 virtual int32_t Release() = 0;
143
144 // Encode an I420 image (as a part of a video stream). The encoded image
145 // will be returned to the user through the encode complete callback.
146 //
147 // Input:
148 // - frame : Image to be encoded
149 // - frame_types : Frame type to be generated by the encoder.
150 //
151 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
152 // <0 - Errors:
153 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
154 // WEBRTC_VIDEO_CODEC_MEMORY
155 // WEBRTC_VIDEO_CODEC_ERROR
156 // WEBRTC_VIDEO_CODEC_TIMEOUT
157 virtual int32_t Encode(const VideoFrame& frame,
158 const CodecSpecificInfo* codec_specific_info,
159 const std::vector<FrameType>* frame_types) = 0;
160
161 // Inform the encoder of the new packet loss rate and the round-trip time of
162 // the network.
163 //
164 // Input:
165 // - packet_loss : Fraction lost
166 // (loss rate in percent = 100 * packetLoss / 255)
167 // - rtt : Round-trip time in milliseconds
168 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
169 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
170 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
171
172 // Inform the encoder about the new target bit rate.
173 //
174 // Input:
175 // - bitrate : New target bit rate
176 // - framerate : The target frame rate
177 //
178 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
mflodman351424e2017-08-10 02:43:14 -0700179 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700180
181 // Default fallback: Just use the sum of bitrates as the single target rate.
182 // TODO(sprang): Remove this default implementation when we remove SetRates().
183 virtual int32_t SetRateAllocation(const BitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700184 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700185
186 // Any encoder implementation wishing to use the WebRTC provided
187 // quality scaler must implement this method.
mflodman351424e2017-08-10 02:43:14 -0700188 virtual ScalingSettings GetScalingSettings() const;
ilnikd60d06a2017-04-05 03:02:20 -0700189
mflodman351424e2017-08-10 02:43:14 -0700190 virtual int32_t SetPeriodicKeyFrames(bool enable);
191 virtual bool SupportsNativeHandle() const;
192 virtual const char* ImplementationName() const;
ilnikd60d06a2017-04-05 03:02:20 -0700193};
ilnikd60d06a2017-04-05 03:02:20 -0700194} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200195#endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_