blob: 9e7e4d7040f2fd8401ac934144619612f5fe750a [file] [log] [blame]
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +00001/*
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
Peter Boströmb7d9a972015-12-18 16:01:11 +010014#include <string>
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000015#include <vector>
16
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000017#include "webrtc/common_types.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000018#include "webrtc/typedefs.h"
19#include "webrtc/video_frame.h"
20
21namespace webrtc {
22
23class RTPFragmentationHeader;
24// TODO(pbos): Expose these through a public (root) header or change these APIs.
25struct CodecSpecificInfo;
26struct VideoCodec;
27
28class EncodedImageCallback {
29 public:
30 virtual ~EncodedImageCallback() {}
31
32 // Callback function which is called when an image has been encoded.
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +000033 virtual int32_t Encoded(const EncodedImage& encoded_image,
34 const CodecSpecificInfo* codec_specific_info,
35 const RTPFragmentationHeader* fragmentation) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000036};
37
38class VideoEncoder {
39 public:
40 enum EncoderType {
Zeke Chin71f6f442015-06-29 14:34:58 -070041 kH264,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000042 kVp8,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000043 kVp9,
Peter Boström4d71ede2015-05-19 23:09:35 +020044 kUnsupportedCodec,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000045 };
46
47 static VideoEncoder* Create(EncoderType codec_type);
48
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000049 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000050 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000051 static VideoCodecH264 GetDefaultH264Settings();
52
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000053 virtual ~VideoEncoder() {}
54
marpan@webrtc.org5b883172014-11-01 06:10:48 +000055 // Initialize the encoder with the information from the codecSettings
56 //
57 // Input:
58 // - codec_settings : Codec settings
59 // - number_of_cores : Number of cores available for the encoder
60 // - max_payload_size : The maximum size each payload is allowed
61 // to have. Usually MTU - overhead.
62 //
63 // Return value : Set bit rate if OK
64 // <0 - Errors:
65 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
66 // WEBRTC_VIDEO_CODEC_ERR_SIZE
67 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
68 // WEBRTC_VIDEO_CODEC_MEMORY
69 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000070 virtual int32_t InitEncode(const VideoCodec* codec_settings,
71 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000072 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000073
74 // Register an encode complete callback object.
75 //
76 // Input:
77 // - callback : Callback object which handles encoded images.
78 //
79 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000080 virtual int32_t RegisterEncodeCompleteCallback(
81 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000082
83 // Free encoder memory.
84 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000085 virtual int32_t Release() = 0;
86
marpan@webrtc.org5b883172014-11-01 06:10:48 +000087 // Encode an I420 image (as a part of a video stream). The encoded image
88 // will be returned to the user through the encode complete callback.
89 //
90 // Input:
91 // - frame : Image to be encoded
92 // - frame_types : Frame type to be generated by the encoder.
93 //
94 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
95 // <0 - Errors:
96 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
97 // WEBRTC_VIDEO_CODEC_MEMORY
98 // WEBRTC_VIDEO_CODEC_ERROR
99 // WEBRTC_VIDEO_CODEC_TIMEOUT
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700100 virtual int32_t Encode(const VideoFrame& frame,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000101 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700102 const std::vector<FrameType>* frame_types) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000103
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000104 // Inform the encoder of the new packet loss rate and the round-trip time of
105 // the network.
106 //
107 // Input:
108 // - packet_loss : Fraction lost
109 // (loss rate in percent = 100 * packetLoss / 255)
110 // - rtt : Round-trip time in milliseconds
111 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
112 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000113 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000114
115 // Inform the encoder about the new target bit rate.
116 //
117 // Input:
118 // - bitrate : New target bit rate
119 // - framerate : The target frame rate
120 //
121 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000122 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
123
124 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200125 virtual void OnDroppedFrame() {}
jackychen6e2ce6e2015-07-13 16:26:33 -0700126 virtual int GetTargetFramerate() { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200127 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100128 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000129};
130
Peter Boström4d71ede2015-05-19 23:09:35 +0200131// Class used to wrap external VideoEncoders to provide a fallback option on
132// software encoding when a hardware encoder fails to encode a stream due to
133// hardware restrictions, such as max resolution.
134class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
135 public:
136 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
137 webrtc::VideoEncoder* encoder);
138
139 int32_t InitEncode(const VideoCodec* codec_settings,
140 int32_t number_of_cores,
141 size_t max_payload_size) override;
142
143 int32_t RegisterEncodeCompleteCallback(
144 EncodedImageCallback* callback) override;
145
146 int32_t Release() override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700147 int32_t Encode(const VideoFrame& frame,
Peter Boström4d71ede2015-05-19 23:09:35 +0200148 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700149 const std::vector<FrameType>* frame_types) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200150 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
151
152 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override;
153 void OnDroppedFrame() override;
jackychen6e2ce6e2015-07-13 16:26:33 -0700154 int GetTargetFramerate() override;
Peter Boströmeb66e802015-06-05 11:08:03 +0200155 bool SupportsNativeHandle() const override;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100156 const char* ImplementationName() const override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200157
158 private:
noahricb1ce6632015-10-21 23:54:51 -0700159 bool InitFallbackEncoder();
160
161 // Settings used in the last InitEncode call and used if a dynamic fallback to
162 // software is required.
163 VideoCodec codec_settings_;
164 int32_t number_of_cores_;
165 size_t max_payload_size_;
166
167 // The last bitrate/framerate set, and a flag for noting they are set.
168 bool rates_set_;
169 uint32_t bitrate_;
170 uint32_t framerate_;
171
172 // The last channel parameters set, and a flag for noting they are set.
173 bool channel_parameters_set_;
174 uint32_t packet_loss_;
175 int64_t rtt_;
176
Peter Boström4d71ede2015-05-19 23:09:35 +0200177 const EncoderType encoder_type_;
178 webrtc::VideoEncoder* const encoder_;
179
180 rtc::scoped_ptr<webrtc::VideoEncoder> fallback_encoder_;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100181 std::string fallback_implementation_name_;
Peter Boström4d71ede2015-05-19 23:09:35 +0200182 EncodedImageCallback* callback_;
183};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000184} // namespace webrtc
185#endif // WEBRTC_VIDEO_ENCODER_H_