blob: 0f21cae13a767dd54ae5ade8c272dbf3dad89564 [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
11#ifndef WEBRTC_API_VIDEO_CODECS_VIDEO_ENCODER_H_
12#define WEBRTC_API_VIDEO_CODECS_VIDEO_ENCODER_H_
13
14#include <memory>
15#include <string>
16#include <vector>
17
nissef93752a2017-05-10 05:25:59 -070018#include "webrtc/api/video/video_frame.h"
ilnikd60d06a2017-04-05 03:02:20 -070019#include "webrtc/common_types.h"
nisseea3a7982017-05-15 02:42:11 -070020#include "webrtc/common_video/include/video_frame.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020021#include "webrtc/rtc_base/checks.h"
22#include "webrtc/rtc_base/optional.h"
ilnikd60d06a2017-04-05 03:02:20 -070023#include "webrtc/typedefs.h"
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
59 // Callback function which is called when an image has been encoded.
60 virtual Result OnEncodedImage(
61 const EncodedImage& encoded_image,
62 const CodecSpecificInfo* codec_specific_info,
63 const RTPFragmentationHeader* fragmentation) = 0;
64
65 virtual void OnDroppedFrame() {}
66};
67
68class VideoEncoder {
69 public:
ilnikd60d06a2017-04-05 03:02:20 -070070 struct QpThresholds {
71 QpThresholds(int l, int h) : low(l), high(h) {}
72 QpThresholds() : low(-1), high(-1) {}
73 int low;
74 int high;
75 };
76 struct ScalingSettings {
mflodman351424e2017-08-10 02:43:14 -070077 ScalingSettings(bool on, int low, int high);
78 explicit ScalingSettings(bool on);
79 ScalingSettings(const ScalingSettings&);
80 ~ScalingSettings();
81
ilnikd60d06a2017-04-05 03:02:20 -070082 const bool enabled;
83 const rtc::Optional<QpThresholds> thresholds;
84 };
ilnikd60d06a2017-04-05 03:02:20 -070085
86 static VideoCodecVP8 GetDefaultVp8Settings();
87 static VideoCodecVP9 GetDefaultVp9Settings();
88 static VideoCodecH264 GetDefaultH264Settings();
89
90 virtual ~VideoEncoder() {}
91
92 // Initialize the encoder with the information from the codecSettings
93 //
94 // Input:
95 // - codec_settings : Codec settings
96 // - number_of_cores : Number of cores available for the encoder
97 // - max_payload_size : The maximum size each payload is allowed
98 // to have. Usually MTU - overhead.
99 //
100 // Return value : Set bit rate if OK
101 // <0 - Errors:
102 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
103 // WEBRTC_VIDEO_CODEC_ERR_SIZE
104 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
105 // WEBRTC_VIDEO_CODEC_MEMORY
106 // WEBRTC_VIDEO_CODEC_ERROR
107 virtual int32_t InitEncode(const VideoCodec* codec_settings,
108 int32_t number_of_cores,
109 size_t max_payload_size) = 0;
110
111 // Register an encode complete callback object.
112 //
113 // Input:
114 // - callback : Callback object which handles encoded images.
115 //
116 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
117 virtual int32_t RegisterEncodeCompleteCallback(
118 EncodedImageCallback* callback) = 0;
119
120 // Free encoder memory.
121 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
122 virtual int32_t Release() = 0;
123
124 // Encode an I420 image (as a part of a video stream). The encoded image
125 // will be returned to the user through the encode complete callback.
126 //
127 // Input:
128 // - frame : Image to be encoded
129 // - frame_types : Frame type to be generated by the encoder.
130 //
131 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
132 // <0 - Errors:
133 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
134 // WEBRTC_VIDEO_CODEC_MEMORY
135 // WEBRTC_VIDEO_CODEC_ERROR
136 // WEBRTC_VIDEO_CODEC_TIMEOUT
137 virtual int32_t Encode(const VideoFrame& frame,
138 const CodecSpecificInfo* codec_specific_info,
139 const std::vector<FrameType>* frame_types) = 0;
140
141 // Inform the encoder of the new packet loss rate and the round-trip time of
142 // the network.
143 //
144 // Input:
145 // - packet_loss : Fraction lost
146 // (loss rate in percent = 100 * packetLoss / 255)
147 // - rtt : Round-trip time in milliseconds
148 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
149 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
150 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
151
152 // Inform the encoder about the new target bit rate.
153 //
154 // Input:
155 // - bitrate : New target bit rate
156 // - framerate : The target frame rate
157 //
158 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
mflodman351424e2017-08-10 02:43:14 -0700159 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700160
161 // Default fallback: Just use the sum of bitrates as the single target rate.
162 // TODO(sprang): Remove this default implementation when we remove SetRates().
163 virtual int32_t SetRateAllocation(const BitrateAllocation& allocation,
mflodman351424e2017-08-10 02:43:14 -0700164 uint32_t framerate);
ilnikd60d06a2017-04-05 03:02:20 -0700165
166 // Any encoder implementation wishing to use the WebRTC provided
167 // quality scaler must implement this method.
mflodman351424e2017-08-10 02:43:14 -0700168 virtual ScalingSettings GetScalingSettings() const;
ilnikd60d06a2017-04-05 03:02:20 -0700169
mflodman351424e2017-08-10 02:43:14 -0700170 virtual int32_t SetPeriodicKeyFrames(bool enable);
171 virtual bool SupportsNativeHandle() const;
172 virtual const char* ImplementationName() const;
ilnikd60d06a2017-04-05 03:02:20 -0700173};
ilnikd60d06a2017-04-05 03:02:20 -0700174} // namespace webrtc
175#endif // WEBRTC_API_VIDEO_CODECS_VIDEO_ENCODER_H_