blob: d0003ccffb703eeca77fb399ec9ac8010e00cd39 [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/base/checks.h"
20#include "webrtc/common_types.h"
nisseea3a7982017-05-15 02:42:11 -070021#include "webrtc/common_video/include/video_frame.h"
ilnikd60d06a2017-04-05 03:02:20 -070022#include "webrtc/typedefs.h"
ilnikd60d06a2017-04-05 03:02:20 -070023#include "webrtc/base/optional.h"
24
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
44 Result(Error error) : error(error) {}
45 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 {
77 ScalingSettings(bool on, int low, int high)
78 : enabled(on),
79 thresholds(rtc::Optional<QpThresholds>(QpThresholds(low, high))) {}
80 explicit ScalingSettings(bool on) : enabled(on) {}
81 const bool enabled;
82 const rtc::Optional<QpThresholds> thresholds;
83 };
ilnikd60d06a2017-04-05 03:02:20 -070084
85 static VideoCodecVP8 GetDefaultVp8Settings();
86 static VideoCodecVP9 GetDefaultVp9Settings();
87 static VideoCodecH264 GetDefaultH264Settings();
88
89 virtual ~VideoEncoder() {}
90
91 // Initialize the encoder with the information from the codecSettings
92 //
93 // Input:
94 // - codec_settings : Codec settings
95 // - number_of_cores : Number of cores available for the encoder
96 // - max_payload_size : The maximum size each payload is allowed
97 // to have. Usually MTU - overhead.
98 //
99 // Return value : Set bit rate if OK
100 // <0 - Errors:
101 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
102 // WEBRTC_VIDEO_CODEC_ERR_SIZE
103 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
104 // WEBRTC_VIDEO_CODEC_MEMORY
105 // WEBRTC_VIDEO_CODEC_ERROR
106 virtual int32_t InitEncode(const VideoCodec* codec_settings,
107 int32_t number_of_cores,
108 size_t max_payload_size) = 0;
109
110 // Register an encode complete callback object.
111 //
112 // Input:
113 // - callback : Callback object which handles encoded images.
114 //
115 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
116 virtual int32_t RegisterEncodeCompleteCallback(
117 EncodedImageCallback* callback) = 0;
118
119 // Free encoder memory.
120 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
121 virtual int32_t Release() = 0;
122
123 // Encode an I420 image (as a part of a video stream). The encoded image
124 // will be returned to the user through the encode complete callback.
125 //
126 // Input:
127 // - frame : Image to be encoded
128 // - frame_types : Frame type to be generated by the encoder.
129 //
130 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
131 // <0 - Errors:
132 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
133 // WEBRTC_VIDEO_CODEC_MEMORY
134 // WEBRTC_VIDEO_CODEC_ERROR
135 // WEBRTC_VIDEO_CODEC_TIMEOUT
136 virtual int32_t Encode(const VideoFrame& frame,
137 const CodecSpecificInfo* codec_specific_info,
138 const std::vector<FrameType>* frame_types) = 0;
139
140 // Inform the encoder of the new packet loss rate and the round-trip time of
141 // the network.
142 //
143 // Input:
144 // - packet_loss : Fraction lost
145 // (loss rate in percent = 100 * packetLoss / 255)
146 // - rtt : Round-trip time in milliseconds
147 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
148 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
149 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
150
151 // Inform the encoder about the new target bit rate.
152 //
153 // Input:
154 // - bitrate : New target bit rate
155 // - framerate : The target frame rate
156 //
157 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
158 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) {
159 RTC_NOTREACHED() << "SetRate(uint32_t, uint32_t) is deprecated.";
160 return -1;
161 }
162
163 // Default fallback: Just use the sum of bitrates as the single target rate.
164 // TODO(sprang): Remove this default implementation when we remove SetRates().
165 virtual int32_t SetRateAllocation(const BitrateAllocation& allocation,
166 uint32_t framerate) {
167 return SetRates(allocation.get_sum_kbps(), framerate);
168 }
169
170 // Any encoder implementation wishing to use the WebRTC provided
171 // quality scaler must implement this method.
172 virtual ScalingSettings GetScalingSettings() const {
173 return ScalingSettings(false);
174 }
175
176 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
177 virtual bool SupportsNativeHandle() const { return false; }
178 virtual const char* ImplementationName() const { return "unknown"; }
179};
180
181} // namespace webrtc
182#endif // WEBRTC_API_VIDEO_CODECS_VIDEO_ENCODER_H_