blob: 0100239e0a42ab8942de17373cc0bb66a991509f [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
kwibergc891eb42016-03-02 03:41:34 -080014#include <memory>
Peter Boströmb7d9a972015-12-18 16:01:11 +010015#include <string>
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000016#include <vector>
17
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000018#include "webrtc/common_types.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000019#include "webrtc/typedefs.h"
20#include "webrtc/video_frame.h"
21
22namespace webrtc {
23
24class RTPFragmentationHeader;
25// TODO(pbos): Expose these through a public (root) header or change these APIs.
26struct CodecSpecificInfo;
27struct VideoCodec;
28
29class EncodedImageCallback {
30 public:
31 virtual ~EncodedImageCallback() {}
32
33 // Callback function which is called when an image has been encoded.
sergeyuac4dc2c2016-08-02 14:33:16 -070034 // TODO(perkj): Change this to return void.
changbin.shao@webrtc.orgf31f56d2015-02-09 09:14:03 +000035 virtual int32_t Encoded(const EncodedImage& encoded_image,
36 const CodecSpecificInfo* codec_specific_info,
sergeyuac4dc2c2016-08-02 14:33:16 -070037 const RTPFragmentationHeader* fragmentation) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000038};
39
40class VideoEncoder {
41 public:
42 enum EncoderType {
Zeke Chin71f6f442015-06-29 14:34:58 -070043 kH264,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000044 kVp8,
marpan@webrtc.org5b883172014-11-01 06:10:48 +000045 kVp9,
Peter Boström4d71ede2015-05-19 23:09:35 +020046 kUnsupportedCodec,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000047 };
48
49 static VideoEncoder* Create(EncoderType codec_type);
50
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000051 static VideoCodecVP8 GetDefaultVp8Settings();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000052 static VideoCodecVP9 GetDefaultVp9Settings();
pbos@webrtc.org6cd6ba82014-09-18 12:42:28 +000053 static VideoCodecH264 GetDefaultH264Settings();
54
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000055 virtual ~VideoEncoder() {}
56
marpan@webrtc.org5b883172014-11-01 06:10:48 +000057 // Initialize the encoder with the information from the codecSettings
58 //
59 // Input:
60 // - codec_settings : Codec settings
61 // - number_of_cores : Number of cores available for the encoder
62 // - max_payload_size : The maximum size each payload is allowed
63 // to have. Usually MTU - overhead.
64 //
65 // Return value : Set bit rate if OK
66 // <0 - Errors:
67 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
68 // WEBRTC_VIDEO_CODEC_ERR_SIZE
69 // WEBRTC_VIDEO_CODEC_LEVEL_EXCEEDED
70 // WEBRTC_VIDEO_CODEC_MEMORY
71 // WEBRTC_VIDEO_CODEC_ERROR
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000072 virtual int32_t InitEncode(const VideoCodec* codec_settings,
73 int32_t number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000074 size_t max_payload_size) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000075
76 // Register an encode complete callback object.
77 //
78 // Input:
79 // - callback : Callback object which handles encoded images.
80 //
81 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000082 virtual int32_t RegisterEncodeCompleteCallback(
83 EncodedImageCallback* callback) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +000084
85 // Free encoder memory.
86 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000087 virtual int32_t Release() = 0;
88
marpan@webrtc.org5b883172014-11-01 06:10:48 +000089 // Encode an I420 image (as a part of a video stream). The encoded image
90 // will be returned to the user through the encode complete callback.
91 //
92 // Input:
93 // - frame : Image to be encoded
94 // - frame_types : Frame type to be generated by the encoder.
95 //
96 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
97 // <0 - Errors:
98 // WEBRTC_VIDEO_CODEC_ERR_PARAMETER
99 // WEBRTC_VIDEO_CODEC_MEMORY
100 // WEBRTC_VIDEO_CODEC_ERROR
101 // WEBRTC_VIDEO_CODEC_TIMEOUT
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700102 virtual int32_t Encode(const VideoFrame& frame,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000103 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700104 const std::vector<FrameType>* frame_types) = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000105
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000106 // Inform the encoder of the new packet loss rate and the round-trip time of
107 // the network.
108 //
109 // Input:
110 // - packet_loss : Fraction lost
111 // (loss rate in percent = 100 * packetLoss / 255)
112 // - rtt : Round-trip time in milliseconds
113 // Return value : WEBRTC_VIDEO_CODEC_OK if OK
114 // <0 - Errors: WEBRTC_VIDEO_CODEC_ERROR
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000115 virtual int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) = 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000116
117 // Inform the encoder about the new target bit rate.
118 //
119 // Input:
120 // - bitrate : New target bit rate
121 // - framerate : The target frame rate
122 //
123 // Return value : WEBRTC_VIDEO_CODEC_OK if OK, < 0 otherwise.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000124 virtual int32_t SetRates(uint32_t bitrate, uint32_t framerate) = 0;
125
126 virtual int32_t SetPeriodicKeyFrames(bool enable) { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200127 virtual void OnDroppedFrame() {}
128 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100129 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000130};
131
Peter Boström4d71ede2015-05-19 23:09:35 +0200132// Class used to wrap external VideoEncoders to provide a fallback option on
133// software encoding when a hardware encoder fails to encode a stream due to
134// hardware restrictions, such as max resolution.
135class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
136 public:
137 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
138 webrtc::VideoEncoder* encoder);
139
140 int32_t InitEncode(const VideoCodec* codec_settings,
141 int32_t number_of_cores,
142 size_t max_payload_size) override;
143
144 int32_t RegisterEncodeCompleteCallback(
145 EncodedImageCallback* callback) override;
146
147 int32_t Release() override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700148 int32_t Encode(const VideoFrame& frame,
Peter Boström4d71ede2015-05-19 23:09:35 +0200149 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700150 const std::vector<FrameType>* frame_types) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200151 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
152
153 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override;
154 void OnDroppedFrame() 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
kwibergc891eb42016-03-02 03:41:34 -0800180 std::unique_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_