[feat] support dynamic resolution codec

This commit is contained in:
dijunkun
2024-09-05 17:29:27 +08:00
parent 1292018f51
commit 2f72e3957e
7 changed files with 60 additions and 45 deletions

View File

@@ -156,25 +156,27 @@ int Render::LoadSettingsFromCacheFile() {
int Render::StartScreenCapture() {
screen_capturer_ = (ScreenCapturer *)screen_capturer_factory_->Create();
ScreenCapturer::RECORD_DESKTOP_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = screen_width_;
rect.bottom = screen_height_;
last_frame_time_ = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
int screen_capturer_init_ret = screen_capturer_->Init(
rect, 60,
[this](unsigned char *data, int size, int width, int height) -> void {
60, [this](unsigned char *data, int size, int width, int height) -> void {
auto now_time = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
auto duration = now_time - last_frame_time_;
if (duration >= 0 && connection_established_) {
SendData(peer_, DATA_TYPE::VIDEO, (const char *)data,
NV12_BUFFER_SIZE);
// SendData(peer_, DATA_TYPE::VIDEO, (const char *)data,
// NV12_BUFFER_SIZE);
XVideoFrame frame;
frame.data = (const char *)data;
frame.size = size;
frame.width = width;
frame.height = height;
SendVideoFrame(peer_, &frame);
last_frame_time_ = now_time;
}
});
@@ -275,9 +277,12 @@ int Render::CreateConnectionPeer() {
? true
: false;
params_.enable_turn = config_center_.IsEnableTurn();
params_.on_receive_video_buffer = OnReceiveVideoBufferCb;
params_.on_receive_video_buffer = nullptr;
params_.on_receive_audio_buffer = OnReceiveAudioBufferCb;
params_.on_receive_data_buffer = OnReceiveDataBufferCb;
params_.on_receive_video_frame = OnReceiveVideoBufferCb;
params_.on_signal_status = OnSignalStatusCb;
params_.on_connection_status = OnConnectionStatusCb;
params_.net_status_report = NetStatusReport;
@@ -596,7 +601,15 @@ int Render::Run() {
}
} else if (event.type == REFRESH_EVENT) {
if (stream_texture_)
SDL_UpdateTexture(stream_texture_, NULL, dst_buffer_, 1280);
if (video_width_ != texture_width_) {
texture_width_ = video_width_;
texture_height_ = video_height_;
stream_texture_ = SDL_CreateTexture(
main_renderer_, stream_pixformat_, SDL_TEXTUREACCESS_STREAMING,
texture_width_, texture_height_);
}
SDL_UpdateTexture(stream_texture_, NULL, dst_buffer_, texture_width_);
} else {
if (connection_established_) {
ProcessMouseKeyEven(event);

View File

@@ -44,7 +44,7 @@ class Render {
int ConnectionStatusWindow();
public:
static void OnReceiveVideoBufferCb(const char *data, size_t size,
static void OnReceiveVideoBufferCb(const XVideoFrame *video_frame,
const char *user_id, size_t user_id_size,
void *user_data);
@@ -169,6 +169,9 @@ class Render {
int texture_width_ = 1280;
int texture_height_ = 720;
int video_width_ = 1280;
int video_height_ = 720;
SDL_Window *main_window_;
SDL_Renderer *main_renderer_ = nullptr;
@@ -242,7 +245,8 @@ class Render {
unsigned char audio_buffer_[960];
int audio_len_ = 0;
char *nv12_buffer_ = nullptr;
unsigned char *dst_buffer_ = new unsigned char[1280 * 720 * 3];
unsigned char *dst_buffer_ = nullptr;
int dst_buffer_capacity_ = 0;
private:
ScreenCapturerFactory *screen_capturer_factory_ = nullptr;

View File

@@ -115,12 +115,26 @@ void Render::SdlCaptureAudioOut(void *userdata, Uint8 *stream, int len) {
// render->audio_buffer_fresh_ = false;
}
void Render::OnReceiveVideoBufferCb(const char *data, size_t size,
void Render::OnReceiveVideoBufferCb(const XVideoFrame *video_frame,
const char *user_id, size_t user_id_size,
void *user_data) {
Render *render = (Render *)user_data;
if (render->connection_established_) {
memcpy(render->dst_buffer_, data, size);
if (!render->dst_buffer_) {
render->dst_buffer_capacity_ = video_frame->size;
render->dst_buffer_ = new unsigned char[video_frame->size];
}
if (render->dst_buffer_capacity_ < video_frame->size) {
delete render->dst_buffer_;
render->dst_buffer_capacity_ = video_frame->size;
render->dst_buffer_ = new unsigned char[video_frame->size];
}
memcpy(render->dst_buffer_, video_frame->data, video_frame->size);
render->video_width_ = video_frame->width;
render->video_height_ = video_frame->height;
SDL_Event event;
event.type = REFRESH_EVENT;
SDL_PushEvent(&event);