mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-12-16 20:17:10 +08:00
[feat] make thumbnail save asynchronous to prevent UI blocking
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "OPPOSans_Regular.h"
|
#include "OPPOSans_Regular.h"
|
||||||
#include "device_controller_factory.h"
|
#include "device_controller_factory.h"
|
||||||
@@ -207,7 +208,7 @@ int Render::LoadSettingsFromCacheFile() {
|
|||||||
memset(aes128_iv_, 0, sizeof(aes128_iv_));
|
memset(aes128_iv_, 0, sizeof(aes128_iv_));
|
||||||
|
|
||||||
thumbnail_.reset();
|
thumbnail_.reset();
|
||||||
thumbnail_ = std::make_unique<Thumbnail>(cache_path_ + "/thumbnails/");
|
thumbnail_ = std::make_shared<Thumbnail>(cache_path_ + "/thumbnails/");
|
||||||
thumbnail_->GetKeyAndIv(aes128_key_, aes128_iv_);
|
thumbnail_->GetKeyAndIv(aes128_key_, aes128_iv_);
|
||||||
thumbnail_->DeleteAllFilesInDirectory();
|
thumbnail_->DeleteAllFilesInDirectory();
|
||||||
|
|
||||||
@@ -248,7 +249,7 @@ int Render::LoadSettingsFromCacheFile() {
|
|||||||
memcpy(aes128_iv_, cd_cache_.iv, sizeof(cd_cache_.iv));
|
memcpy(aes128_iv_, cd_cache_.iv, sizeof(cd_cache_.iv));
|
||||||
|
|
||||||
thumbnail_.reset();
|
thumbnail_.reset();
|
||||||
thumbnail_ = std::make_unique<Thumbnail>(cache_path_ + "/thumbnails/",
|
thumbnail_ = std::make_shared<Thumbnail>(cache_path_ + "/thumbnails/",
|
||||||
aes128_key_, aes128_iv_);
|
aes128_key_, aes128_iv_);
|
||||||
|
|
||||||
language_button_value_ = (int)config_center_->GetLanguage();
|
language_button_value_ = (int)config_center_->GetLanguage();
|
||||||
@@ -1237,6 +1238,9 @@ void Render::Cleanup() {
|
|||||||
|
|
||||||
CleanupFactories();
|
CleanupFactories();
|
||||||
CleanupPeers();
|
CleanupPeers();
|
||||||
|
|
||||||
|
WaitForThumbnailSaveTasks();
|
||||||
|
|
||||||
AudioDeviceDestroy();
|
AudioDeviceDestroy();
|
||||||
DestroyMainWindowContext();
|
DestroyMainWindowContext();
|
||||||
DestroyMainWindow();
|
DestroyMainWindow();
|
||||||
@@ -1264,10 +1268,29 @@ void Render::CleanupPeer(std::shared_ptr<SubStreamWindowProperties> props) {
|
|||||||
SDL_FlushEvent(STREAM_REFRESH_EVENT);
|
SDL_FlushEvent(STREAM_REFRESH_EVENT);
|
||||||
|
|
||||||
if (props->dst_buffer_) {
|
if (props->dst_buffer_) {
|
||||||
thumbnail_->SaveToThumbnail(
|
size_t buffer_size = props->dst_buffer_capacity_;
|
||||||
(char*)props->dst_buffer_, props->video_width_, props->video_height_,
|
std::vector<unsigned char> buffer_copy(buffer_size);
|
||||||
props->remote_id_, props->remote_host_name_,
|
memcpy(buffer_copy.data(), props->dst_buffer_, buffer_size);
|
||||||
props->remember_password_ ? props->remote_password_ : "");
|
|
||||||
|
int video_width = props->video_width_;
|
||||||
|
int video_height = props->video_height_;
|
||||||
|
std::string remote_id = props->remote_id_;
|
||||||
|
std::string remote_host_name = props->remote_host_name_;
|
||||||
|
std::string password =
|
||||||
|
props->remember_password_ ? props->remote_password_ : "";
|
||||||
|
|
||||||
|
std::thread save_thread([buffer_copy, video_width, video_height, remote_id,
|
||||||
|
remote_host_name, password,
|
||||||
|
thumbnail = thumbnail_]() {
|
||||||
|
thumbnail->SaveToThumbnail((char*)buffer_copy.data(), video_width,
|
||||||
|
video_height, remote_id, remote_host_name,
|
||||||
|
password);
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(thumbnail_save_threads_mutex_);
|
||||||
|
thumbnail_save_threads_.emplace_back(std::move(save_thread));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (props->peer_) {
|
if (props->peer_) {
|
||||||
@@ -1305,6 +1328,25 @@ void Render::CleanupPeers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Render::WaitForThumbnailSaveTasks() {
|
||||||
|
std::vector<std::thread> threads_to_join;
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(thumbnail_save_threads_mutex_);
|
||||||
|
threads_to_join.swap(thumbnail_save_threads_);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threads_to_join.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& thread : threads_to_join) {
|
||||||
|
if (thread.joinable()) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Render::CleanSubStreamWindowProperties(
|
void Render::CleanSubStreamWindowProperties(
|
||||||
std::shared_ptr<SubStreamWindowProperties> props) {
|
std::shared_ptr<SubStreamWindowProperties> props) {
|
||||||
if (props->stream_texture_) {
|
if (props->stream_texture_) {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "IconsFontAwesome6.h"
|
#include "IconsFontAwesome6.h"
|
||||||
#include "config_center.h"
|
#include "config_center.h"
|
||||||
@@ -301,7 +302,7 @@ class Render {
|
|||||||
// thumbnail
|
// thumbnail
|
||||||
unsigned char aes128_key_[16];
|
unsigned char aes128_key_[16];
|
||||||
unsigned char aes128_iv_[16];
|
unsigned char aes128_iv_[16];
|
||||||
std::unique_ptr<Thumbnail> thumbnail_;
|
std::shared_ptr<Thumbnail> thumbnail_;
|
||||||
|
|
||||||
// recent connections
|
// recent connections
|
||||||
std::vector<std::pair<std::string, Thumbnail::RecentConnection>>
|
std::vector<std::pair<std::string, Thumbnail::RecentConnection>>
|
||||||
@@ -512,6 +513,11 @@ class Render {
|
|||||||
void CloseTab(decltype(client_properties_)::iterator& it);
|
void CloseTab(decltype(client_properties_)::iterator& it);
|
||||||
/* ------ stream window property end ------ */
|
/* ------ stream window property end ------ */
|
||||||
|
|
||||||
|
/* ------ async thumbnail save tasks ------ */
|
||||||
|
std::vector<std::thread> thumbnail_save_threads_;
|
||||||
|
std::mutex thumbnail_save_threads_mutex_;
|
||||||
|
void WaitForThumbnailSaveTasks();
|
||||||
|
|
||||||
/* ------ server mode ------ */
|
/* ------ server mode ------ */
|
||||||
std::unordered_map<std::string, ConnectionStatus> connection_status_;
|
std::unordered_map<std::string, ConnectionStatus> connection_status_;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user