hunting/utils/MediaBase/src/FfmpegBase.h
2024-06-21 15:27:11 +08:00

79 lines
2.1 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FFMPEG_BASE_H
#define FFMPEG_BASE_H
#include "IMediaBase.h"
#include "MediaBase.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavcodec/packet.h>
#include <libavformat/avformat.h>
#include <libavutil/avassert.h>
#include <libavutil/avutil.h>
#include <libavutil/channel_layout.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/opt.h>
#include <libavutil/timestamp.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
#include <thread>
// a wrapper around a single output AVStream
typedef struct OutputStream
{
AVStream *st;
AVCodecContext *enc;
/* pts of the next frame that will be generated */
int64_t next_pts;
int samples_count;
AVFrame *frame;
AVFrame *tmp_frame;
AVPacket *tmp_pkt;
float t, tincr, tincr2;
struct SwsContext *sws_ctx;
struct SwrContext *swr_ctx;
} OutputStream;
class FfmpegBase : public IMediaBase, public std::enable_shared_from_this<FfmpegBase>
{
public:
FfmpegBase();
FfmpegBase(const MediaHandleType &type);
virtual ~FfmpegBase() = default;
protected:
void InitFfmpeg(void);
void UnInitFfmpeg(void);
protected:
static const char *InputFormat(const MediaHandleType &type);
static AVMediaType MediaTypeConvert(const MediaHandleType &type);
protected:
const MediaHandleType mType;
enum AVMediaType mFFmpegMediaType;
bool mTaskRuning;
std::thread mTaskTimerThread;
};
#endif