Line data Source code
1 : /*
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_VIDEO_ENCODER_H_
12 : #define WEBRTC_VIDEO_ENCODER_H_
13 :
14 : #include <memory>
15 : #include <string>
16 : #include <vector>
17 :
18 : #include "webrtc/base/checks.h"
19 : #include "webrtc/common_types.h"
20 : #include "webrtc/typedefs.h"
21 : #include "webrtc/video_frame.h"
22 : #include "webrtc/base/optional.h"
23 :
24 : namespace webrtc {
25 :
26 : class RTPFragmentationHeader;
27 : // TODO(pbos): Expose these through a public (root) header or change these APIs.
28 : struct CodecSpecificInfo;
29 : class VideoCodec;
30 :
31 0 : class EncodedImageCallback {
32 : public:
33 0 : virtual ~EncodedImageCallback() {}
34 :
35 : struct Result {
36 : enum Error {
37 : OK,
38 :
39 : // Failed to send the packet.
40 : ERROR_SEND_FAILED,
41 : };
42 :
43 0 : Result(Error error) : error(error) {}
44 0 : Result(Error error, uint32_t frame_id) : error(error), frame_id(frame_id) {}
45 :
46 : Error error;
47 :
48 : // Frame ID assigned to the frame. The frame ID should be the same as the ID
49 : // seen by the receiver for this frame. RTP timestamp of the frame is used
50 : // as frame ID when RTP is used to send video. Must be used only when
51 : // error=OK.
52 : uint32_t frame_id = 0;
53 :
54 : // Tells the encoder that the next frame is should be dropped.
55 : bool drop_next_frame = false;
56 : };
57 :
58 : // Callback function which is called when an image has been encoded.
59 : virtual Result OnEncodedImage(
60 : const EncodedImage& encoded_image,
61 : const CodecSpecificInfo* codec_specific_info,
62 : const RTPFragmentationHeader* fragmentation) = 0;
63 :
64 0 : virtual void OnDroppedFrame() {}
65 : };
66 :
67 0 : class VideoEncoder {
68 : public:
69 : enum EncoderType {
70 : kH264,
71 : kVp8,
72 : kVp9,
73 : kUnsupportedCodec,
74 : };
75 : struct QpThresholds {
76 0 : QpThresholds(int l, int h) : low(l), high(h) {}
77 : QpThresholds() : low(-1), high(-1) {}
78 : int low;
79 : int high;
80 : };
81 0 : struct ScalingSettings {
82 : ScalingSettings(bool on, int low, int high)
83 : : enabled(on),
84 : thresholds(rtc::Optional<QpThresholds>(QpThresholds(low, high))) {}
85 0 : explicit ScalingSettings(bool on) : enabled(on) {}
86 : const bool enabled;
87 : const rtc::Optional<QpThresholds> thresholds;
88 : };
89 : static VideoEncoder* Create(EncoderType codec_type,
90 : bool enable_simulcast = false);
91 : // Returns true if this type of encoder can be created using
92 : // VideoEncoder::Create.
93 : static bool IsSupportedSoftware(EncoderType codec_type);
94 : static EncoderType CodecToEncoderType(VideoCodecType codec_type);
95 :
96 : static VideoCodecVP8 GetDefaultVp8Settings();
97 : static VideoCodecVP9 GetDefaultVp9Settings();
98 : static VideoCodecH264 GetDefaultH264Settings();
99 :
100 0 : virtual ~VideoEncoder() {}
101 :
102 : // Initialize the encoder with the information from the codecSettings
103 : //
104 : // Input:
105 : // - codec_settings : Codec settings
106 : // - number_of_cores : Number of cores available for the encoder
107 : // - max_payload_size : The maximum size each payload is allowed
108 : // to have. Usually MTU - overhead.
109 : //
110 : // Return value : Set bit rate if OK
111 : // <0 - Errors:
112 : // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
113 : // WEBRTC_VIDEO_CODEC_ERR_SIZE
114 : // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
115 : // WEBRTC_VIDEO_CODEC_MEMORY
116 : // WEBRTC_VIDEO_CODEC_ERROR
117 : virtual int32_t InitEncode(const VideoCodec* codec_settings,
118 : int32_t number_of_cores,
119 : size_t max_payload_size) = 0;
120 :
121 : // Register an encode complete callback object.
122 : //
123 : // Input:
124 : // - callback : Callback object which handles encoded images.
125 : //
126 : // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
127 : virtual int32_t RegisterEncodeCompleteCallback(
128 : EncodedImageCallback* callback) = 0;
129 :
130 : // Free encoder memory.
131 : // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
132 : virtual int32_t Release() = 0;
133 :
134 : // Encode an I420 image (as a part of a video stream). The encoded image
135 : // will be returned to the user through the encode complete callback.
136 : //
137 : // Input:
138 : // - frame : Image to be encoded
139 : // - frame_types : Frame type to be generated by the encoder.
140 : //
141 : // Return value : WEBRTC_VIDEO_CODEC_OK if OK
142 : // <0 - Errors:
143 : // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
144 : // WEBRTC_VIDEO_CODEC_MEMORY
145 : // WEBRTC_VIDEO_CODEC_ERROR
146 : // WEBRTC_VIDEO_CODEC_TIMEOUT
147 : virtual int32_t Encode(const VideoFrame& frame,
148 : const CodecSpecificInfo* codec_specific_info,
149 : const std::vector<FrameType>* frame_types) = 0;
150 :
151 : // Inform the encoder of the new packet loss rate and the round-trip time of
152 : // the network.
153 : //
154 : // Input:
155 : // - packet_loss : Fraction lost
156 : // (loss rate in percent = 100 * packetLoss / 255)
157 : // - rtt : Round-trip time in milliseconds
158 : // Return value : WEBRTC_VIDEO_CODEC_OK if OK
159 : // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
160 : virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
161 :
162 : // Inform the encoder about the new target bit rate.
163 : //
164 : // Input:
165 : // - bitrate : New target bit rate
166 : // - framerate : The target frame rate
167 : //
168 : // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
169 0 : virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) {
170 0 : RTC_NOTREACHED() << "SetRate(uint32_t, uint32_t) is deprecated.";
171 0 : return -1;
172 : }
173 :
174 : // Default fallback: Just use the sum of bitrates as the single target rate.
175 : // TODO(sprang): Remove this default implementation when we remove SetRates().
176 0 : virtual int32_t SetRateAllocation(const BitrateAllocation& allocation,
177 : uint32_t framerate) {
178 0 : return SetRates(allocation.get_sum_kbps(), framerate);
179 : }
180 :
181 : // Any encoder implementation wishing to use the WebRTC provided
182 : // quality scaler must implement this method.
183 0 : virtual ScalingSettings GetScalingSettings() const {
184 0 : return ScalingSettings(false);
185 : }
186 :
187 0 : virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
188 0 : virtual bool SupportsNativeHandle() const { return false; }
189 0 : virtual const char* ImplementationName() const { return "unknown"; }
190 : };
191 :
192 : } // namespace webrtc
193 : #endif // WEBRTC_VIDEO_ENCODER_H_
|