blob: 8c82753d107aab7c9773a625fc6167428b52926d [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.
kjellander02b3d272016-04-20 05:05:54 -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,
37 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() {}
jackychen6e2ce6e2015-07-13 16:26:33 -0700128 virtual int GetTargetFramerate() { return -1; }
Peter Boströmeb66e802015-06-05 11:08:03 +0200129 virtual bool SupportsNativeHandle() const { return false; }
Peter Boströmb7d9a972015-12-18 16:01:11 +0100130 virtual const char* ImplementationName() const { return "unknown"; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000131};
132
Peter Boström4d71ede2015-05-19 23:09:35 +0200133// Class used to wrap external VideoEncoders to provide a fallback option on
134// software encoding when a hardware encoder fails to encode a stream due to
135// hardware restrictions, such as max resolution.
136class VideoEncoderSoftwareFallbackWrapper : public VideoEncoder {
137 public:
138 VideoEncoderSoftwareFallbackWrapper(VideoCodecType codec_type,
139 webrtc::VideoEncoder* encoder);
140
141 int32_t InitEncode(const VideoCodec* codec_settings,
142 int32_t number_of_cores,
143 size_t max_payload_size) override;
144
145 int32_t RegisterEncodeCompleteCallback(
146 EncodedImageCallback* callback) override;
147
148 int32_t Release() override;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700149 int32_t Encode(const VideoFrame& frame,
Peter Boström4d71ede2015-05-19 23:09:35 +0200150 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700151 const std::vector<FrameType>* frame_types) override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200152 int32_t SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
153
154 int32_t SetRates(uint32_t bitrate, uint32_t framerate) override;
155 void OnDroppedFrame() override;
jackychen6e2ce6e2015-07-13 16:26:33 -0700156 int GetTargetFramerate() override;
Peter Boströmeb66e802015-06-05 11:08:03 +0200157 bool SupportsNativeHandle() const override;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100158 const char* ImplementationName() const override;
Peter Boström4d71ede2015-05-19 23:09:35 +0200159
160 private:
noahricb1ce6632015-10-21 23:54:51 -0700161 bool InitFallbackEncoder();
162
163 // Settings used in the last InitEncode call and used if a dynamic fallback to
164 // software is required.
165 VideoCodec codec_settings_;
166 int32_t number_of_cores_;
167 size_t max_payload_size_;
168
169 // The last bitrate/framerate set, and a flag for noting they are set.
170 bool rates_set_;
171 uint32_t bitrate_;
172 uint32_t framerate_;
173
174 // The last channel parameters set, and a flag for noting they are set.
175 bool channel_parameters_set_;
176 uint32_t packet_loss_;
177 int64_t rtt_;
178
Peter Boström4d71ede2015-05-19 23:09:35 +0200179 const EncoderType encoder_type_;
180 webrtc::VideoEncoder* const encoder_;
181
kwibergc891eb42016-03-02 03:41:34 -0800182 std::unique_ptr<webrtc::VideoEncoder> fallback_encoder_;
Peter Boströmb7d9a972015-12-18 16:01:11 +0100183 std::string fallback_implementation_name_;
Peter Boström4d71ede2015-05-19 23:09:35 +0200184 EncodedImageCallback* callback_;
185};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000186} // namespace webrtc
187#endif // WEBRTC_VIDEO_ENCODER_H_