Remote desk client supports MacOS

This commit is contained in:
dijunkun
2023-09-19 17:06:00 +08:00
parent ebd7d87e91
commit b16b29780b
19 changed files with 312 additions and 55 deletions

View File

@@ -2,12 +2,10 @@
#define _FFMPEG_DECODER_H_
#ifdef _WIN32
// Windows
extern "C" {
#include "libavcodec/avcodec.h"
};
#else
// Linux...
#ifdef __cplusplus
extern "C" {
#endif

View File

@@ -0,0 +1,44 @@
#include "ffmpeg_encoder.h"
#include <chrono>
#include "log.h"
#define SAVE_ENCODER_STREAM 0
VideoEncoder::VideoEncoder() {
if (SAVE_ENCODER_STREAM) {
file_ = fopen("encode_stream.h264", "w+b");
if (!file_) {
LOG_WARN("Fail to open stream.h264");
}
}
}
VideoEncoder::~VideoEncoder() {
if (SAVE_ENCODER_STREAM && file_) {
fflush(file_);
fclose(file_);
file_ = nullptr;
}
if (nv12_data_) {
free(nv12_data_);
nv12_data_ = nullptr;
}
}
int VideoEncoder::Init() { return 0; }
int VideoEncoder::Encode(
const uint8_t *pData, int nSize,
std::function<int(char *encoded_packets, size_t size)> on_encoded_image) {
return -1;
}
int VideoEncoder::OnEncodedImage(char *encoded_packets, size_t size) {
LOG_INFO("output encoded image");
fwrite(encoded_packets, 1, size, file_);
return 0;
}
void VideoEncoder::ForceIdr() {}

View File

@@ -0,0 +1,47 @@
#ifndef _FFMPEG_ENCODER_H_
#define _FFMPEG_ENCODER_H_
#ifdef _WIN32
extern "C" {
#include "libavcodec/avcodec.h"
};
#else
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#ifdef __cplusplus
};
#endif
#endif
#include <functional>
class VideoEncoder {
public:
VideoEncoder();
~VideoEncoder();
int Init();
int Encode(
const uint8_t* pData, int nSize,
std::function<int(char* encoded_packets, size_t size)> on_encoded_image);
virtual int OnEncodedImage(char* encoded_packets, size_t size);
void ForceIdr();
private:
int frame_width = 1280;
int frame_height = 720;
int keyFrameInterval_ = 3000;
int maxBitrate_ = 2000;
int max_payload_size_ = 3000;
std::vector<std::vector<uint8_t>> encoded_packets_;
unsigned char* encoded_image_ = nullptr;
FILE* file_ = nullptr;
unsigned char* nv12_data_ = nullptr;
unsigned int seq_ = 0;
};
#endif

View File

@@ -60,11 +60,11 @@ int VideoEncoder::Init() {
// TO TEST: not tested yet
// init_params.encodeConfig->gopLength = NVENC_INFINITE_GOPLENGTH;
init_params.encodeConfig->gopLength = keyFrameInterval_;
// Donot use B-frame for realtime application
// Do not use B-frame for realtime application
init_params.encodeConfig->frameIntervalP = 1;
init_params.encodeConfig->rcParams.rateControlMode =
NV_ENC_PARAMS_RC_MODE::NV_ENC_PARAMS_RC_CBR;
init_params.encodeConfig->rcParams.maxBitRate = maxBitrate_ * 1000;
init_params.encodeConfig->rcParams.maxBitRate = maxBitrate_ * 500;
init_params.encodeConfig->encodeCodecConfig.h264Config.sliceMode = 1;
init_params.encodeConfig->encodeCodecConfig.h264Config.sliceModeData =
max_payload_size_;
@@ -81,7 +81,7 @@ int VideoEncoder::Encode(
return -1;
}
if (0 == seq_++ % (30)) {
if (0 == seq_++ % (300)) {
ForceIdr();
}

View File

@@ -28,7 +28,7 @@ class VideoEncoder {
int frame_width = 1280;
int frame_height = 720;
int keyFrameInterval_ = 3000;
int maxBitrate_ = 2000;
int maxBitrate_ = 1000;
int max_payload_size_ = 3000;
NvEncoder* encoder_ = nullptr;
CUcontext cuda_context_ = nullptr;