// Copyright 2021 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H #define ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H #include #include #include #include #include #include #include #include namespace android { struct BitstreamBuffer; class VideoEncoder { public: // The InputFrame class can be used to store raw video frames. // Note: The InputFrame does not take ownership of the data. The file descriptor is not // duplicated and the caller is responsible for keeping the data alive until the buffer // is returned by an InputBufferDoneCB() call. class InputFrame { public: InputFrame(std::vector&& fds, std::vector&& planes, VideoPixelFormat pixelFormat, uint64_t index, int64_t timestamp); ~InputFrame() = default; const std::vector& fds() const { return mFds; } const std::vector& planes() const { return mPlanes; } VideoPixelFormat pixelFormat() const { return mPixelFormat; } uint64_t index() const { return mIndex; } int64_t timestamp() const { return mTimestamp; } private: const std::vector mFds; std::vector mPlanes; VideoPixelFormat mPixelFormat; uint64_t mIndex = 0; int64_t mTimestamp = 0; }; using FetchOutputBufferCB = base::RepeatingCallback* buffer)>; // TODO(dstaessens): Change callbacks to OnceCallback provided when requesting encode/drain. using InputBufferDoneCB = base::RepeatingCallback; using OutputBufferDoneCB = base::RepeatingCallback buffer)>; using DrainDoneCB = base::RepeatingCallback; using ErrorCB = base::RepeatingCallback; virtual ~VideoEncoder() = default; // Encode the frame, |InputBufferDoneCB| and |OutputBufferDoneCB| will be called when done. virtual bool encode(std::unique_ptr buffer) = 0; // Drain the encoder, |mDrainDoneCb| will be called when done. virtual void drain() = 0; // Flush the encoder, pending drain operations will be aborted. virtual void flush() = 0; // Set the target bitrate to the specified value, will affect all non-processed frames. virtual bool setBitrate(uint32_t bitrate) = 0; // Set the peak bitrate to the specified value. The peak bitrate must be larger or equal to the // target bitrate and is ignored if the bitrate mode is constant. virtual bool setPeakBitrate(uint32_t peakBitrate) = 0; // Set the framerate to the specified value, will affect all non-processed frames. virtual bool setFramerate(uint32_t framerate) = 0; // Request the next frame encoded to be a key frame, will affect the next non-processed frame. virtual void requestKeyframe() = 0; virtual VideoPixelFormat inputFormat() const = 0; virtual const ui::Size& visibleSize() const = 0; virtual const ui::Size& codedSize() const = 0; }; } // namespace android #endif // ANDROID_V4L2_CODEC2_COMPONENTS_VIDEO_ENCODER_H