2019-05-28 20:11:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkVideEncoder_DEFINED
|
|
|
|
#define SkVideEncoder_DEFINED
|
|
|
|
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
#include "libavformat/avio.h"
|
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
// private to the impl
|
|
|
|
class SkRandomAccessWStream;
|
2019-05-29 17:33:55 +00:00
|
|
|
struct SwsContext;
|
2019-05-28 20:11:03 +00:00
|
|
|
|
|
|
|
class SkVideoEncoder {
|
|
|
|
public:
|
|
|
|
SkVideoEncoder();
|
|
|
|
~SkVideoEncoder();
|
|
|
|
|
2019-06-03 15:04:20 +00:00
|
|
|
/**
|
|
|
|
* Begina a new recording. Balance this (after adding all of your frames) with a call
|
|
|
|
* to endRecording().
|
|
|
|
*/
|
2019-05-29 17:33:55 +00:00
|
|
|
bool beginRecording(SkISize, int fps);
|
2019-05-28 20:11:03 +00:00
|
|
|
|
2019-06-03 15:04:20 +00:00
|
|
|
/**
|
|
|
|
* Returns the preferred ImageInfo for this recording. Only valid if beginRecording() has
|
|
|
|
* been called, and endRecording has not been called (yet).
|
|
|
|
*/
|
|
|
|
SkImageInfo preferredInfo() const { return fInfo; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If you have your own pixmap, call addFrame(). Note this may fail if it uses an unsupported
|
|
|
|
* ColorType or AlphaType, or the dimensions don't match those set in beginRecording.
|
|
|
|
* For best results, use the SkImageInfo returned by preferredInfo().
|
|
|
|
*/
|
|
|
|
bool addFrame(const SkPixmap&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* As an alternative to calling addFrame(), you can call beginFrame/endFrame, and the encoder
|
|
|
|
* will manage allocating a surface/canvas for you.
|
|
|
|
*
|
|
|
|
* SkCanvas* canvas = encoder.beginFrame();
|
|
|
|
* // your drawing code here, drawing into canvas
|
|
|
|
* encoder.endFrame();
|
|
|
|
*/
|
2019-05-28 20:11:03 +00:00
|
|
|
SkCanvas* beginFrame();
|
|
|
|
bool endFrame();
|
|
|
|
|
2019-06-03 15:04:20 +00:00
|
|
|
/**
|
|
|
|
* Call this after having added all of your frames. After calling this, no more frames can
|
|
|
|
* be added to this recording. To record a new video, call beginRecording().
|
|
|
|
*/
|
2019-05-28 20:11:03 +00:00
|
|
|
sk_sp<SkData> endRecording();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void reset();
|
2019-06-03 15:04:20 +00:00
|
|
|
bool init(int fps);
|
2019-05-28 20:11:03 +00:00
|
|
|
bool sendFrame(AVFrame*); // frame can be null
|
|
|
|
|
|
|
|
double computeTimeStamp(const AVFrame*) const;
|
|
|
|
|
2019-05-29 17:33:55 +00:00
|
|
|
SwsContext* fSWScaleCtx = nullptr;
|
2019-05-28 20:11:03 +00:00
|
|
|
AVIOContext* fStreamCtx = nullptr;
|
|
|
|
AVFormatContext* fFormatCtx = nullptr;
|
|
|
|
AVCodecContext* fEncoderCtx = nullptr;
|
|
|
|
AVStream* fStream = nullptr; // we do not free this
|
|
|
|
AVFrame* fFrame = nullptr;
|
|
|
|
AVPacket* fPacket = nullptr;
|
|
|
|
|
2019-06-03 15:04:20 +00:00
|
|
|
SkImageInfo fInfo; // only defined between beginRecording() and endRecording()
|
2019-05-28 20:11:03 +00:00
|
|
|
std::unique_ptr<SkRandomAccessWStream> fWStream;
|
2019-06-03 15:04:20 +00:00
|
|
|
int64_t fCurrentPTS, fDeltaPTS;
|
|
|
|
|
|
|
|
// Lazily allocated, iff the client has called beginFrame() for a given recording session.
|
|
|
|
sk_sp<SkSurface> fSurface;
|
2019-05-28 20:11:03 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|