mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 12:45:35 +08:00
Add global hardware acceleration codec switch
This commit is contained in:
@@ -4,16 +4,9 @@
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#define SAVE_ENCODER_STREAM 0
|
||||
#define SAVE_ENCODER_STREAM 1
|
||||
|
||||
FFmpegVideoEncoder::FFmpegVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("encode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
LOG_WARN("Fail to open stream.h264");
|
||||
}
|
||||
}
|
||||
}
|
||||
FFmpegVideoEncoder::FFmpegVideoEncoder() {}
|
||||
FFmpegVideoEncoder::~FFmpegVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM && file_) {
|
||||
fflush(file_);
|
||||
@@ -21,12 +14,14 @@ FFmpegVideoEncoder::~FFmpegVideoEncoder() {
|
||||
file_ = nullptr;
|
||||
}
|
||||
|
||||
av_packet_free(&packet_);
|
||||
|
||||
if (nv12_data_) {
|
||||
free(nv12_data_);
|
||||
nv12_data_ = nullptr;
|
||||
}
|
||||
|
||||
if (packet_) {
|
||||
av_packet_free(&packet_);
|
||||
}
|
||||
}
|
||||
|
||||
int FFmpegVideoEncoder::Init() {
|
||||
@@ -82,6 +77,13 @@ int FFmpegVideoEncoder::Init() {
|
||||
|
||||
packet_ = av_packet_alloc();
|
||||
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("encode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
LOG_WARN("Fail to open stream.h264");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ extern "C" {
|
||||
class FFmpegVideoEncoder : public VideoEncoder {
|
||||
public:
|
||||
FFmpegVideoEncoder();
|
||||
~FFmpegVideoEncoder();
|
||||
virtual ~FFmpegVideoEncoder();
|
||||
|
||||
int Init();
|
||||
int Encode(
|
||||
@@ -57,7 +57,7 @@ class FFmpegVideoEncoder : public VideoEncoder {
|
||||
const AVCodec* codec_ = nullptr;
|
||||
AVCodecContext* codec_ctx_ = nullptr;
|
||||
AVFrame* frame_ = nullptr;
|
||||
AVPacket* packet_;
|
||||
AVPacket* packet_ = nullptr;
|
||||
bool got_output_ = false;
|
||||
uint32_t pts_ = 0;
|
||||
};
|
||||
|
||||
@@ -4,16 +4,9 @@
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#define SAVE_ENCODER_STREAM 0
|
||||
#define SAVE_ENCODER_STREAM 1
|
||||
|
||||
NvidiaVideoEncoder::NvidiaVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("encode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
LOG_WARN("Fail to open stream.h264");
|
||||
}
|
||||
}
|
||||
}
|
||||
NvidiaVideoEncoder::NvidiaVideoEncoder() {}
|
||||
NvidiaVideoEncoder::~NvidiaVideoEncoder() {
|
||||
if (SAVE_ENCODER_STREAM && file_) {
|
||||
fflush(file_);
|
||||
@@ -70,6 +63,13 @@ int NvidiaVideoEncoder::Init() {
|
||||
max_payload_size_;
|
||||
|
||||
encoder_->CreateEncoder(&init_params);
|
||||
|
||||
if (SAVE_ENCODER_STREAM) {
|
||||
file_ = fopen("encode_stream.h264", "w+b");
|
||||
if (!file_) {
|
||||
LOG_WARN("Fail to open stream.h264");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
class NvidiaVideoEncoder : public VideoEncoder {
|
||||
public:
|
||||
NvidiaVideoEncoder();
|
||||
~NvidiaVideoEncoder();
|
||||
virtual ~NvidiaVideoEncoder();
|
||||
|
||||
int Init();
|
||||
int Encode(
|
||||
|
||||
@@ -11,6 +11,9 @@ class VideoEncoder {
|
||||
on_encoded_image) = 0;
|
||||
virtual int OnEncodedImage(char* encoded_packets, size_t size) = 0;
|
||||
virtual void ForceIdr() = 0;
|
||||
|
||||
VideoEncoder() = default;
|
||||
virtual ~VideoEncoder() {}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -7,11 +7,11 @@ VideoEncoderFactory::VideoEncoderFactory() {}
|
||||
|
||||
VideoEncoderFactory::~VideoEncoderFactory() {}
|
||||
|
||||
VideoEncoder *VideoEncoderFactory::CreateVideoEncoder(
|
||||
std::unique_ptr<VideoEncoder> VideoEncoderFactory::CreateVideoEncoder(
|
||||
bool hardware_acceleration) {
|
||||
if (hardware_acceleration) {
|
||||
return new NvidiaVideoEncoder();
|
||||
return std::make_unique<NvidiaVideoEncoder>(NvidiaVideoEncoder());
|
||||
} else {
|
||||
return new FFmpegVideoEncoder();
|
||||
return std::make_unique<FFmpegVideoEncoder>(FFmpegVideoEncoder());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@ class VideoEncoderFactory {
|
||||
VideoEncoderFactory();
|
||||
~VideoEncoderFactory();
|
||||
|
||||
static VideoEncoder *CreateVideoEncoder(bool hardware_acceleration);
|
||||
|
||||
private:
|
||||
bool hardware_acceleration_ = false;
|
||||
static std::unique_ptr<VideoEncoder> CreateVideoEncoder(
|
||||
bool hardware_acceleration);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user