mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Use factory to create encoder/decoder
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "ffmpeg_decoder.h"
|
||||
#include "ffmpeg_video_decoder.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
@@ -10,7 +10,7 @@ extern "C" {
|
||||
#include <libswscale/swscale.h>
|
||||
};
|
||||
|
||||
VideoDecoder::VideoDecoder() {
|
||||
FfmpegVideoDecoder::FfmpegVideoDecoder() {
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("decode_stream.yuv", "w+b");
|
||||
if (!file_) {
|
||||
@@ -19,7 +19,7 @@ VideoDecoder::VideoDecoder() {
|
||||
}
|
||||
}
|
||||
|
||||
VideoDecoder::~VideoDecoder() {
|
||||
FfmpegVideoDecoder::~FfmpegVideoDecoder() {
|
||||
if (SAVE_ENCODER_STREAM && file_) {
|
||||
fflush(file_);
|
||||
fclose(file_);
|
||||
@@ -38,7 +38,7 @@ VideoDecoder::~VideoDecoder() {
|
||||
av_free(codec_ctx_);
|
||||
}
|
||||
|
||||
int VideoDecoder::Init() {
|
||||
int FfmpegVideoDecoder::Init() {
|
||||
// av_log_set_level(AV_LOG_DEBUG);
|
||||
|
||||
codec_id_ = AV_CODEC_ID_H264;
|
||||
@@ -80,7 +80,7 @@ int VideoDecoder::Init() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VideoDecoder::Decode(
|
||||
int FfmpegVideoDecoder::Decode(
|
||||
const uint8_t *data, int size,
|
||||
std::function<void(VideoFrame)> on_receive_decoded_frame) {
|
||||
if (!first_) {
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _FFMPEG_DECODER_H_
|
||||
#define _FFMPEG_DECODER_H_
|
||||
#ifndef _FFMPEG_VIDEO_DECODER_H_
|
||||
#define _FFMPEG_VIDEO_DECODER_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
extern "C" {
|
||||
@@ -17,12 +17,12 @@ extern "C" {
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "frame.h"
|
||||
#include "video_decoder.h"
|
||||
|
||||
class VideoDecoder {
|
||||
class FfmpegVideoDecoder : public VideoDecoder {
|
||||
public:
|
||||
VideoDecoder();
|
||||
~VideoDecoder();
|
||||
FfmpegVideoDecoder();
|
||||
~FfmpegVideoDecoder();
|
||||
|
||||
public:
|
||||
int Init();
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "nv_decoder.h"
|
||||
#include "nvidia_video_decoder.h"
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#define SAVE_ENCODER_STREAM 0
|
||||
|
||||
VideoDecoder::VideoDecoder() {
|
||||
NvidiaVideoDecoder::NvidiaVideoDecoder() {
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("decode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
@@ -12,7 +12,7 @@ VideoDecoder::VideoDecoder() {
|
||||
}
|
||||
}
|
||||
}
|
||||
VideoDecoder::~VideoDecoder() {
|
||||
NvidiaVideoDecoder::~NvidiaVideoDecoder() {
|
||||
if (SAVE_ENCODER_STREAM && file_) {
|
||||
fflush(file_);
|
||||
fclose(file_);
|
||||
@@ -20,7 +20,7 @@ VideoDecoder::~VideoDecoder() {
|
||||
}
|
||||
}
|
||||
|
||||
int VideoDecoder::Init() {
|
||||
int NvidiaVideoDecoder::Init() {
|
||||
ck(cuInit(0));
|
||||
int nGpu = 0;
|
||||
int iGpu = 0;
|
||||
@@ -43,7 +43,7 @@ int VideoDecoder::Init() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VideoDecoder::Decode(
|
||||
int NvidiaVideoDecoder::Decode(
|
||||
const uint8_t *data, int size,
|
||||
std::function<void(VideoFrame)> on_receive_decoded_frame) {
|
||||
if (!decoder) {
|
||||
@@ -1,15 +1,15 @@
|
||||
#ifndef _NV_DECODER_H_
|
||||
#define _NV_DECODER_H_
|
||||
#ifndef _NVIDIA_VIDEO_DECODER_H_
|
||||
#define _NVIDIA_VIDEO_DECODER_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "NvDecoder.h"
|
||||
#include "frame.h"
|
||||
#include "video_decoder.h"
|
||||
|
||||
class VideoDecoder {
|
||||
class NvidiaVideoDecoder : public VideoDecoder {
|
||||
public:
|
||||
VideoDecoder();
|
||||
~VideoDecoder();
|
||||
NvidiaVideoDecoder();
|
||||
~NvidiaVideoDecoder();
|
||||
|
||||
public:
|
||||
int Init();
|
||||
16
src/media/video/decode/video_decoder.h
Normal file
16
src/media/video/decode/video_decoder.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _VIDEO_DECODER_H_
|
||||
#define _VIDEO_DECODER_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "frame.h"
|
||||
|
||||
class VideoDecoder {
|
||||
public:
|
||||
virtual int Init() = 0;
|
||||
virtual int Decode(
|
||||
const uint8_t *data, int size,
|
||||
std::function<void(VideoFrame)> on_receive_decoded_frame) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
17
src/media/video/decode/video_decoder_factory.cpp
Normal file
17
src/media/video/decode/video_decoder_factory.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "video_decoder_factory.h"
|
||||
|
||||
#include "ffmpeg/ffmpeg_video_decoder.h"
|
||||
#include "nvcodec/nvidia_video_decoder.h"
|
||||
|
||||
VideoDecoderFactory::VideoDecoderFactory() {}
|
||||
|
||||
VideoDecoderFactory::~VideoDecoderFactory() {}
|
||||
|
||||
VideoDecoder *VideoDecoderFactory::CreateVideoDecoder(
|
||||
bool hardware_acceleration) {
|
||||
if (hardware_acceleration) {
|
||||
return new NvidiaVideoDecoder();
|
||||
} else {
|
||||
return new FfmpegVideoDecoder();
|
||||
}
|
||||
}
|
||||
16
src/media/video/decode/video_decoder_factory.h
Normal file
16
src/media/video/decode/video_decoder_factory.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _VIDEO_DECODER_FACTORY_H_
|
||||
#define _VIDEO_DECODER_FACTORY_H_
|
||||
|
||||
#include "video_decoder.h"
|
||||
class VideoDecoderFactory {
|
||||
public:
|
||||
VideoDecoderFactory();
|
||||
~VideoDecoderFactory();
|
||||
|
||||
static VideoDecoder *CreateVideoDecoder(bool hardware_acceleration);
|
||||
|
||||
private:
|
||||
bool hardware_acceleration_ = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ffmpeg_encoder.h"
|
||||
#include "ffmpeg_video_encoder.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#define SAVE_ENCODER_STREAM 0
|
||||
|
||||
VideoEncoder::VideoEncoder() {
|
||||
FFmpegVideoEncoder::FFmpegVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("encode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
@@ -14,7 +14,7 @@ VideoEncoder::VideoEncoder() {
|
||||
}
|
||||
}
|
||||
}
|
||||
VideoEncoder::~VideoEncoder() {
|
||||
FFmpegVideoEncoder::~FFmpegVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM && file_) {
|
||||
fflush(file_);
|
||||
fclose(file_);
|
||||
@@ -29,7 +29,7 @@ VideoEncoder::~VideoEncoder() {
|
||||
}
|
||||
}
|
||||
|
||||
int VideoEncoder::Init() {
|
||||
int FFmpegVideoEncoder::Init() {
|
||||
av_log_set_level(AV_LOG_ERROR);
|
||||
|
||||
codec_ = avcodec_find_encoder(AV_CODEC_ID_H264);
|
||||
@@ -85,7 +85,7 @@ int VideoEncoder::Init() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VideoEncoder::Encode(
|
||||
int FFmpegVideoEncoder::Encode(
|
||||
const uint8_t *pData, int nSize,
|
||||
std::function<int(char *encoded_packets, size_t size)> on_encoded_image) {
|
||||
if (!codec_ctx_) {
|
||||
@@ -132,9 +132,9 @@ int VideoEncoder::Encode(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VideoEncoder::OnEncodedImage(char *encoded_packets, size_t size) {
|
||||
int FFmpegVideoEncoder::OnEncodedImage(char *encoded_packets, size_t size) {
|
||||
LOG_INFO("OnEncodedImage not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void VideoEncoder::ForceIdr() {}
|
||||
void FFmpegVideoEncoder::ForceIdr() {}
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef _FFMPEG_ENCODER_H_
|
||||
#define _FFMPEG_ENCODER_H_
|
||||
#ifndef _FFMPEG_VIDEO_ENCODER_H_
|
||||
#define _FFMPEG_VIDEO_ENCODER_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
extern "C" {
|
||||
@@ -24,10 +24,12 @@ extern "C" {
|
||||
#endif
|
||||
#include <functional>
|
||||
|
||||
class VideoEncoder {
|
||||
#include "video_encoder.h"
|
||||
|
||||
class FFmpegVideoEncoder : public VideoEncoder {
|
||||
public:
|
||||
VideoEncoder();
|
||||
~VideoEncoder();
|
||||
FFmpegVideoEncoder();
|
||||
~FFmpegVideoEncoder();
|
||||
|
||||
int Init();
|
||||
int Encode(
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "nv_encoder.h"
|
||||
#include "nvidia_video_encoder.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#define SAVE_ENCODER_STREAM 0
|
||||
|
||||
VideoEncoder::VideoEncoder() {
|
||||
NvidiaVideoEncoder::NvidiaVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("encode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
@@ -14,7 +14,7 @@ VideoEncoder::VideoEncoder() {
|
||||
}
|
||||
}
|
||||
}
|
||||
VideoEncoder::~VideoEncoder() {
|
||||
NvidiaVideoEncoder::~NvidiaVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM && file_) {
|
||||
fflush(file_);
|
||||
fclose(file_);
|
||||
@@ -27,7 +27,7 @@ VideoEncoder::~VideoEncoder() {
|
||||
}
|
||||
}
|
||||
|
||||
int VideoEncoder::Init() {
|
||||
int NvidiaVideoEncoder::Init() {
|
||||
// Init cuda context
|
||||
int num_of_GPUs = 0;
|
||||
CUdevice cuda_device;
|
||||
@@ -73,7 +73,7 @@ int VideoEncoder::Init() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VideoEncoder::Encode(
|
||||
int NvidiaVideoEncoder::Encode(
|
||||
const uint8_t *pData, int nSize,
|
||||
std::function<int(char *encoded_packets, size_t size)> on_encoded_image) {
|
||||
if (!encoder_) {
|
||||
@@ -130,12 +130,12 @@ int VideoEncoder::Encode(
|
||||
return 0;
|
||||
}
|
||||
|
||||
int VideoEncoder::OnEncodedImage(char *encoded_packets, size_t size) {
|
||||
int NvidiaVideoEncoder::OnEncodedImage(char *encoded_packets, size_t size) {
|
||||
LOG_INFO("OnEncodedImage not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void VideoEncoder::ForceIdr() {
|
||||
void NvidiaVideoEncoder::ForceIdr() {
|
||||
NV_ENC_RECONFIGURE_PARAMS reconfig_params;
|
||||
reconfig_params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
#ifndef _NV_ENCODER_H_
|
||||
#define _NV_ENCODER_H_
|
||||
#ifndef _NVIDIA_VIDEO_ENCODER_H_
|
||||
#define _NVIDIA_VIDEO_ENCODER_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "NvEncoderCuda.h"
|
||||
#include "video_encoder.h"
|
||||
|
||||
class VideoEncoder {
|
||||
class NvidiaVideoEncoder : public VideoEncoder {
|
||||
public:
|
||||
VideoEncoder();
|
||||
~VideoEncoder();
|
||||
NvidiaVideoEncoder();
|
||||
~NvidiaVideoEncoder();
|
||||
|
||||
int Init();
|
||||
int Encode(
|
||||
16
src/media/video/encode/video_encoder.h
Normal file
16
src/media/video/encode/video_encoder.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _VIDEO_ENCODER_H_
|
||||
#define _VIDEO_ENCODER_H_
|
||||
|
||||
#include <functional>
|
||||
|
||||
class VideoEncoder {
|
||||
public:
|
||||
virtual int Init() = 0;
|
||||
virtual int Encode(const uint8_t* pData, int nSize,
|
||||
std::function<int(char* encoded_packets, size_t size)>
|
||||
on_encoded_image) = 0;
|
||||
virtual int OnEncodedImage(char* encoded_packets, size_t size) = 0;
|
||||
virtual void ForceIdr() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
17
src/media/video/encode/video_encoder_factory.cpp
Normal file
17
src/media/video/encode/video_encoder_factory.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "video_encoder_factory.h"
|
||||
|
||||
#include "ffmpeg/ffmpeg_video_encoder.h"
|
||||
#include "nvcodec/nvidia_video_encoder.h"
|
||||
|
||||
VideoEncoderFactory::VideoEncoderFactory() {}
|
||||
|
||||
VideoEncoderFactory::~VideoEncoderFactory() {}
|
||||
|
||||
VideoEncoder *VideoEncoderFactory::CreateVideoEncoder(
|
||||
bool hardware_acceleration) {
|
||||
if (hardware_acceleration) {
|
||||
return new NvidiaVideoEncoder();
|
||||
} else {
|
||||
return new FFmpegVideoEncoder();
|
||||
}
|
||||
}
|
||||
16
src/media/video/encode/video_encoder_factory.h
Normal file
16
src/media/video/encode/video_encoder_factory.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _VIDEO_ENCODER_FACTORY_H_
|
||||
#define _VIDEO_ENCODER_FACTORY_H_
|
||||
|
||||
#include "video_encoder.h"
|
||||
class VideoEncoderFactory {
|
||||
public:
|
||||
VideoEncoderFactory();
|
||||
~VideoEncoderFactory();
|
||||
|
||||
static VideoEncoder *CreateVideoEncoder(bool hardware_acceleration);
|
||||
|
||||
private:
|
||||
bool hardware_acceleration_ = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user