Auto login and no password by default

This commit is contained in:
dijunkun
2023-11-17 17:27:13 +08:00
parent 256f3611ba
commit b68d6820bb

View File

@@ -428,6 +428,67 @@ int main() {
std::string client_user_id = "C-" + std::string(GetMac(mac_addr)); std::string client_user_id = "C-" + std::string(GetMac(mac_addr));
Init(peer_client, client_user_id.c_str()); Init(peer_client, client_user_id.c_str());
{
static char server_password[20] = "";
std::string user_id = "S-" + std::string(GetMac(mac_addr));
CreateConnection(peer_server, mac_addr, server_password);
nv12_buffer = new char[NV12_BUFFER_SIZE];
#ifdef _WIN32
screen_capture = new ScreenCaptureWgc();
RECORD_DESKTOP_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = GetSystemMetrics(SM_CXSCREEN);
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
last_frame_time_ = std::chrono::high_resolution_clock::now();
screen_capture->Init(
rect, 60,
[](unsigned char *data, int size, int width, int height) -> void {
auto now_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = now_time - last_frame_time_;
auto tc = duration.count() * 1000;
if (tc >= 0) {
BGRAToNV12FFmpeg(data, width, height, (unsigned char *)nv12_buffer);
SendData(peer_server, DATA_TYPE::VIDEO, (const char *)nv12_buffer,
NV12_BUFFER_SIZE);
// std::cout << "Send" << std::endl;
last_frame_time_ = now_time;
}
});
screen_capture->Start();
#elif __linux__
screen_capture = new ScreenCaptureX11();
RECORD_DESKTOP_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 0;
rect.bottom = 0;
last_frame_time_ = std::chrono::high_resolution_clock::now();
screen_capture->Init(
rect, 60,
[](unsigned char *data, int size, int width, int height) -> void {
auto now_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = now_time - last_frame_time_;
auto tc = duration.count() * 1000;
if (tc >= 0) {
SendData(peer_server, DATA_TYPE::VIDEO, (const char *)data,
NV12_BUFFER_SIZE);
last_frame_time_ = now_time;
}
});
screen_capture->Start();
#endif
}
// Setup SDL // Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) !=
0) { 0) {
@@ -502,108 +563,117 @@ int main() {
const ImGuiViewport *main_viewport = ImGui::GetMainViewport(); const ImGuiViewport *main_viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Once); ImGui::SetNextWindowPos(ImVec2(0, 0), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(180, 225)); ImGui::SetNextWindowSize(ImVec2(180, 130));
ImGui::Begin("Menu", nullptr, ImGuiWindowFlags_NoResize); ImGui::Begin("Menu", nullptr, ImGuiWindowFlags_NoResize);
{ {
ImGui::Text("LOCAL ID: "); // ImGui::Text("LOCAL ID: ");
ImGui::SameLine(); // ImGui::SameLine();
ImGui::Selectable(mac_addr, false, // ImGui::Selectable(mac_addr, false,
ImGuiSelectableFlags_AllowDoubleClick); // ImGuiSelectableFlags_AllowDoubleClick);
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) { // if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
ImGui::SetClipboardText(mac_addr); // ImGui::SetClipboardText(mac_addr);
} // }
ImGui::Spacing(); // ImGui::Spacing();
ImGui::Text("PASSWORD: "); // ImGui::Text("PASSWORD: ");
ImGui::SameLine(); // ImGui::SameLine();
ImGui::SetNextItemWidth(110); // ImGui::SetNextItemWidth(110);
static char server_password[20] = ""; // static char server_password[20] = "";
ImGui::InputTextWithHint("server_password", "000001", server_password, // ImGui::InputTextWithHint("server_password", "000001",
IM_ARRAYSIZE(server_password), // server_password,
ImGuiInputTextFlags_AllowTabInput); // IM_ARRAYSIZE(server_password),
// ImGuiInputTextFlags_AllowTabInput);
ImGui::Spacing(); // ImGui::Spacing();
{ // {
static bool online_button_pressed = false; // static bool online_button_pressed = false;
static const char *online_label = "Online"; // static const char *online_label = "Online";
if (ImGui::Button(online_label)) { // if (ImGui::Button(online_label)) {
std::string user_id = "S-" + std::string(GetMac(mac_addr)); // std::string user_id = "S-" +
// std::string(GetMac(mac_addr));
if (strcmp(online_label, "Online") == 0) { // if (strcmp(online_label, "Online") == 0) {
CreateConnection(peer_server, mac_addr, server_password); // CreateConnection(peer_server, mac_addr,
// server_password);
nv12_buffer = new char[NV12_BUFFER_SIZE]; // nv12_buffer = new char[NV12_BUFFER_SIZE];
#ifdef _WIN32 // #ifdef _WIN32
screen_capture = new ScreenCaptureWgc(); // screen_capture = new ScreenCaptureWgc();
RECORD_DESKTOP_RECT rect; // RECORD_DESKTOP_RECT rect;
rect.left = 0; // rect.left = 0;
rect.top = 0; // rect.top = 0;
rect.right = GetSystemMetrics(SM_CXSCREEN); // rect.right = GetSystemMetrics(SM_CXSCREEN);
rect.bottom = GetSystemMetrics(SM_CYSCREEN); // rect.bottom = GetSystemMetrics(SM_CYSCREEN);
last_frame_time_ = std::chrono::high_resolution_clock::now(); // last_frame_time_ =
screen_capture->Init( // std::chrono::high_resolution_clock::now();
rect, 60, // screen_capture->Init(
[](unsigned char *data, int size, int width, // rect, 60,
int height) -> void { // [](unsigned char *data, int size, int width,
auto now_time = std::chrono::high_resolution_clock::now(); // int height) -> void {
std::chrono::duration<double> duration = // auto now_time =
now_time - last_frame_time_; // std::chrono::high_resolution_clock::now();
auto tc = duration.count() * 1000; // std::chrono::duration<double> duration =
// now_time - last_frame_time_;
// auto tc = duration.count() * 1000;
if (tc >= 0) { // if (tc >= 0) {
BGRAToNV12FFmpeg(data, width, height, // BGRAToNV12FFmpeg(data, width, height,
(unsigned char *)nv12_buffer); // (unsigned char *)nv12_buffer);
SendData(peer_server, DATA_TYPE::VIDEO, // SendData(peer_server, DATA_TYPE::VIDEO,
(const char *)nv12_buffer, NV12_BUFFER_SIZE); // (const char *)nv12_buffer,
// std::cout << "Send" << std::endl; // NV12_BUFFER_SIZE);
last_frame_time_ = now_time; // // std::cout << "Send" << std::endl;
} // last_frame_time_ = now_time;
}); // }
// });
screen_capture->Start(); // screen_capture->Start();
#elif __linux__ // #elif __linux__
screen_capture = new ScreenCaptureX11(); // screen_capture = new ScreenCaptureX11();
RECORD_DESKTOP_RECT rect; // RECORD_DESKTOP_RECT rect;
rect.left = 0; // rect.left = 0;
rect.top = 0; // rect.top = 0;
rect.right = 0; // rect.right = 0;
rect.bottom = 0; // rect.bottom = 0;
last_frame_time_ = std::chrono::high_resolution_clock::now(); // last_frame_time_ =
screen_capture->Init( // std::chrono::high_resolution_clock::now();
rect, 60, // screen_capture->Init(
[](unsigned char *data, int size, int width, // rect, 60,
int height) -> void { // [](unsigned char *data, int size, int width,
auto now_time = std::chrono::high_resolution_clock::now(); // int height) -> void {
std::chrono::duration<double> duration = // auto now_time =
now_time - last_frame_time_; // std::chrono::high_resolution_clock::now();
auto tc = duration.count() * 1000; // std::chrono::duration<double> duration =
// now_time - last_frame_time_;
// auto tc = duration.count() * 1000;
if (tc >= 0) { // if (tc >= 0) {
SendData(peer_server, DATA_TYPE::VIDEO, // SendData(peer_server, DATA_TYPE::VIDEO,
(const char *)data, NV12_BUFFER_SIZE); // (const char *)data, NV12_BUFFER_SIZE);
last_frame_time_ = now_time; // last_frame_time_ = now_time;
} // }
}); // });
screen_capture->Start(); // screen_capture->Start();
#endif // #endif
} else { // } else {
LeaveConnection(peer_server); // LeaveConnection(peer_server);
} // }
online_button_pressed = !online_button_pressed; // online_button_pressed = !online_button_pressed;
online_label = online_button_pressed ? "Offline" : "Online"; // online_label = online_button_pressed ? "Offline" :
} // "Online";
} // }
// }
ImGui::Spacing(); ImGui::Spacing();
@@ -625,14 +695,14 @@ int main() {
ImGui::Spacing(); ImGui::Spacing();
ImGui::Text("PASSWORD: "); // ImGui::Text("PASSWORD: ");
ImGui::SameLine(); // ImGui::SameLine();
ImGui::SetNextItemWidth(110); // ImGui::SetNextItemWidth(110);
static char client_password[20] = ""; static char client_password[20] = "";
ImGui::InputTextWithHint("client_password", "000003", // ImGui::InputTextWithHint("client_password", "000003",
client_password, // client_password,
IM_ARRAYSIZE(client_password), // IM_ARRAYSIZE(client_password),
ImGuiInputTextFlags_AllowTabInput); // ImGuiInputTextFlags_AllowTabInput);
if (ImGui::Button(connect_label)) { if (ImGui::Button(connect_label)) {
if (strcmp(connect_label, "Connect") == 0 && !joined) { if (strcmp(connect_label, "Connect") == 0 && !joined) {
@@ -734,6 +804,8 @@ int main() {
} }
// Cleanup // Cleanup
LeaveConnection(peer_server);
ImGui_ImplSDLRenderer2_Shutdown(); ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown(); ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext(); ImGui::DestroyContext();