blob: 9d557a3163411885b70d709b4538fab9b634cd1d [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"
Erik Språngec475652018-05-15 15:12:55 +020019#include "api/video/video_bitrate_allocation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "api/video/video_frame.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_video/include/video_frame.h"
23#include "rtc_base/checks.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020024#include "typedefs.h" // NOLINT(build/include)
ilnikd60d06a2017-04-05 03:02:20 -070025
26namespace webrtc {
27
28class RTPFragmentationHeader;
29// TODO(pbos): Expose these through a public (root) header or change these APIs.
30struct CodecSpecificInfo;
31class VideoCodec;
32
33class EncodedImageCallback {
34 public:
35 virtual ~EncodedImageCallback() {}
36
37 struct Result {
38 enum Error {
39 OK,
40
41 // Failed to send the packet.
42 ERROR_SEND_FAILED,
43 };
44
mflodman351424e2017-08-10 02:43:14 -070045 explicit Result(Error error) : error(error) {}
ilnikd60d06a2017-04-05 03:02:20 -070046 Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {}
47
48 Error error;
49
50 // Frame ID assigned to the frame. The frame ID should be the same as the ID
51 // seen by the receiver for this frame. RTP timestamp of the frame is used
52 // as frame ID when RTP is used to send video. Must be used only when
53 // error=OK.
54 uint32_t frame_id = 0;
55
56 // Tells the encoder that the next frame is should be dropped.
57 bool drop_next_frame = false;
58 };
59
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020060 // Used to signal the encoder about reason a frame is dropped.
61 // kDroppedByMediaOptimizations - dropped by MediaOptimizations (for rate
62 // limiting purposes).
63 // kDroppedByEncoder - dropped by encoder's internal rate limiter.
64 enum class DropReason : uint8_t {
65 kDroppedByMediaOptimizations,
66 kDroppedByEncoder
67 };
68
ilnikd60d06a2017-04-05 03:02:20 -070069 // Callback function which is called when an image has been encoded.
70 virtual Result OnEncodedImage(
71 const EncodedImage& encoded_image,
72 const CodecSpecificInfo* codec_specific_info,
73 const RTPFragmentationHeader* fragmentation) = 0;
74
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020075 // Deprecated. TODO(ilnik): Remove this in few weeks.
ilnikd60d06a2017-04-05 03:02:20 -070076 virtual void OnDroppedFrame() {}
Ilya Nikolaevskiyd79314f2017-10-23 10:45:37 +020077
78 virtual void OnDroppedFrame(DropReason reason) {}
ilnikd60d06a2017-04-05 03:02:20 -070079};
80
81class VideoEncoder {
82 public:
ilnikd60d06a2017-04-05 03:02:20 -070083 struct QpThresholds {
84 QpThresholds(int l, int h) : low(l), high(h) {}
85 QpThresholds() : low(-1), high(-1) {}
86 int low;
87 int high;
88 };
Niels Möller225c7872018-02-22 15:03:53 +010089 // Quality scaling is enabled if thresholds are provided.
ilnikd60d06a2017-04-05 03:02:20 -070090 struct ScalingSettings {
Niels Möller225c7872018-02-22 15:03:53 +010091 private:
92 // Private magic type for kOff, implicitly convertible to
93 // ScalingSettings.
94 struct KOff {};
95
96 public:
97 // TODO(nisse): Would be nicer if kOff were a constant ScalingSettings
98 // rather than a magic value. However, rtc::Optional is not trivially copy
99 // constructible, and hence a constant ScalingSettings needs a static
100 // initializer, which is strongly discouraged in Chrome. We can hopefully
101 // fix this when we switch to absl::optional or std::optional.
102 static constexpr KOff kOff = {};
103
104 ScalingSettings(int low, int high);
105 ScalingSettings(int low, int high, int min_pixels);
mflodman351424e2017-08-10 02:43:14 -0700106 ScalingSettings(const ScalingSettings&);
Niels Möller225c7872018-02-22 15:03:53 +0100107 ScalingSettings(KOff); // NOLINT(runtime/explicit)
mflodman351424e2017-08-10 02:43:14 -0700108 ~ScalingSettings();
109
ilnikd60d06a2017-04-05 03:02:20 -0700110 const rtc::Optional<QpThresholds> thresholds;
asapersson142fcc92017-08-17 08:58:54 -0700111
112 // We will never ask for a resolution lower than this.
113 // TODO(kthelgason): Lower this limit when better testing
114 // on MediaCodec and fallback implementations are in place.
115 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206
116 const int min_pixels_per_frame = 320 * 180;
Niels Möller225c7872018-02-22 15:03:53 +0100117
118 private:
119 // Private constructor; to get an object without thresholds, use
120 // the magic constant ScalingSettings::kOff.
121 ScalingSettings();
ilnikd60d06a2017-04-05 03:02:20 -0700122 };
ilnikd60d06a2017-04-05 03:02:20 -0700123
124 static VideoCodecVP8 GetDefaultVp8Settings();
125 static VideoCodecVP9 GetDefaultVp9Settings();
126 static VideoCodecH264 GetDefaultH264Settings();
127
128 virtual ~VideoEncoder() {}
129
130 // Initialize the encoder with the information from the codecSettings
131 //
132 // Input:
133 // - codec_settings : Codec settings
134 // - number_of_cores : Number of cores available for the encoder
135 // - max_payload_size : The maximum size each payload is allowed
136 // to have. Usually MTU - overhead.
137 //
138 // Return value : Set bit rate if OK
139 // <0 - Errors:
140 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
141 // WEBRTC_VIDEO_CODEC_ERR_SIZE
142 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
143 // WEBRTC_VIDEO_CODEC_MEMORY
144 // WEBRTC_VIDEO_CODEC_ERROR
145 virtual int32_t InitEncode(const VideoCodec* codec_settings,
146 int32_t number_of_cores,
147 size_t max_payload_size) = 0;
148
149 // Register an encode complete callback object.
150 //
151 // Input:
152 // - callback : Callback object which handles encoded images.
153 //
154 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
155 virtual int32_t RegisterEncodeCompleteCallback(
156 EncodedImageCallback* callback) = 0;
157
158 // Free encoder memory.
159 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
160 virtual int32_t Release() = 0;
161
162 // Encode an I420 image (as a part of a video stream). The encoded image
163 // will be returned to the user through the encode complete callback.
164 //
165 // Input:
166 // - frame : Image to be encoded
167 // - frame_types : Frame type to be generated by the encoder.
168 //
169 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
170 // <0 - Errors:
171 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
172 // WEBRTC_VIDEO_CODEC_MEMORY
173 // WEBRTC_VIDEO_CODEC_ERROR
174 // WEBRTC_VIDEO_CODEC_TIMEOUT
175 virtual int32_t Encode(const VideoFrame& frame,
176 const CodecSpecificInfo* codec_specific_info,
177 const std::vector<FrameType>* frame_types) = 0;
178
179 // Inform the encoder of the new packet loss rate and the round-trip time of
180 // the network.
181 //
182 // Input:
183 // - packet_loss : Fraction lost
184 // (loss rate in percent = 100 * packetLoss / 255)
185 // - rtt : Round-trip time in milliseconds
186 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
187 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
188 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
189
190 // Inform the encoder about the new target bit rate.
191 //
192 // Input:
193 // - bitrate : New target bit rate
194 // - framerate : The target frame rate
195 //
196 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
mflodman351424e2017-08-10 02:43:14 -0700197 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700198
199 // Default fallback: Just use the sum of bitrates as the single target rate.
200 // TODO(sprang): Remove this default implementation when we remove SetRates().
Erik Språng566124a2018-04-23 12:32:22 +0200201 virtual int32_t SetRateAllocation(const VideoBitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700202 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700203
204 // Any encoder implementation wishing to use the WebRTC provided
205 // quality scaler must implement this method.
mflodman351424e2017-08-10 02:43:14 -0700206 virtual ScalingSettings GetScalingSettings() const;
ilnikd60d06a2017-04-05 03:02:20 -0700207
mflodman351424e2017-08-10 02:43:14 -0700208 virtual bool SupportsNativeHandle() const;
209 virtual const char* ImplementationName() const;
ilnikd60d06a2017-04-05 03:02:20 -0700210};
ilnikd60d06a2017-04-05 03:02:20 -0700211} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200212#endif // API_VIDEO_CODECS_VIDEO_ENCODER_H_