Eable leave connection

This commit is contained in:
dijunkun
2023-10-09 14:11:29 +08:00
parent 85582f8339
commit 95d2d74979
8 changed files with 83 additions and 121 deletions

View File

@@ -49,6 +49,8 @@ std::string window_title = "Remote Desk Client";
int thread_exit = 0; int thread_exit = 0;
PeerPtr *peer = nullptr; PeerPtr *peer = nullptr;
bool joined = false;
bool received_frame = false;
typedef enum { mouse = 0, keyboard } ControlType; typedef enum { mouse = 0, keyboard } ControlType;
typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag; typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag;
@@ -72,30 +74,6 @@ typedef struct {
}; };
} RemoteAction; } RemoteAction;
inline void FreshVideo() {
sdlRect.x = 0;
sdlRect.y = 0;
sdlRect.w = screen_w;
sdlRect.h = screen_h;
SDL_UpdateTexture(sdlTexture, NULL, dst_buffer, pixel_w);
SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
SDL_RenderPresent(sdlRenderer);
frame_count++;
end_time = SDL_GetTicks();
elapsed_time = end_time - start_time;
if (elapsed_time >= 1000) {
fps = frame_count / (elapsed_time / 1000);
frame_count = 0;
window_title = "Remote Desk Client FPS [" + std::to_string(fps) + "]";
// For MacOS, UI frameworks can only be called from the main thread
SDL_SetWindowTitle(window, window_title.c_str());
start_time = end_time;
}
}
inline int ProcessMouseKeyEven(SDL_Event &ev) { inline int ProcessMouseKeyEven(SDL_Event &ev) {
float ratio = 1280.0 / screen_w; float ratio = 1280.0 / screen_w;
@@ -192,6 +170,7 @@ void ReceiveVideoBuffer(const char *data, size_t size, const char *user_id,
SDL_Event event; SDL_Event event;
event.type = REFRESH_EVENT; event.type = REFRESH_EVENT;
SDL_PushEvent(&event); SDL_PushEvent(&event);
received_frame = true;
} }
void ReceiveAudioBuffer(const char *data, size_t size, const char *user_id, void ReceiveAudioBuffer(const char *data, size_t size, const char *user_id,
@@ -302,7 +281,7 @@ int main() {
std::string user_id = "C-" + std::string(GetMac(mac_addr)); std::string user_id = "C-" + std::string(GetMac(mac_addr));
peer = CreatePeer(&params); peer = CreatePeer(&params);
JoinConnection(peer, transmission_id.c_str(), user_id.c_str()); // JoinConnection(peer, transmission_id.c_str(), user_id.c_str());
// 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) !=
@@ -377,11 +356,15 @@ int main() {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event); ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT) done = true; if (event.type == SDL_QUIT) {
if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
done = true; done = true;
} else if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window)) {
done = true;
} else {
ProcessMouseKeyEven(event);
}
} }
// Start the Dear ImGui frame // Start the Dear ImGui frame
@@ -392,102 +375,42 @@ int main() {
ImGui::BeginMainMenuBar(); ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("Main Menu", true)) { if (ImGui::BeginMenu("Main Menu", true)) {
if (ImGui::MenuItem("Connect")) { if (ImGui::MenuItem("Connect") && !joined) {
if (ImGui::Button("Button", ImVec2(100, 50))) { JoinConnection(peer, transmission_id.c_str(), user_id.c_str());
LOG_ERROR("!!!!!!!!!!") joined = true;
}
ImGui::SameLine();
} }
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Disconnect")) { if (ImGui::MenuItem("Disconnect")) {
LeaveConnection(peer);
joined = false;
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
ImGui::Separator(); // ImGui::Separator();
if (ImGui::BeginMenu("Second Menu", true)) { // if (ImGui::BeginMenu("Second Menu", true)) {
if (ImGui::MenuItem("Item 1", "item 1")) { // if (ImGui::MenuItem("Item 1", "item 1")) {
} // }
ImGui::Separator(); // ImGui::Separator();
if (ImGui::MenuItem("Item 2", "item 2")) { // if (ImGui::MenuItem("Item 2", "item 2")) {
} // }
ImGui::EndMenu(); // ImGui::EndMenu();
} // }
ImGui::Separator(); // ImGui::Separator();
if (ImGui::BeginMenu("Third Menu", true)) { // if (ImGui::BeginMenu("Third Menu", true)) {
if (ImGui::MenuItem("Item 3", "item 3")) { // if (ImGui::MenuItem("Item 3", "item 3")) {
} // }
ImGui::Separator(); // ImGui::Separator();
if (ImGui::MenuItem("Item 4", "item 4")) { // if (ImGui::MenuItem("Item 4", "item 4")) {
} // }
ImGui::EndMenu(); // ImGui::EndMenu();
} // }
ImGui::EndMainMenuBar(); ImGui::EndMainMenuBar();
// 1. Show the big demo window (Most of the sample code is in
// ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear
// ImGui!).
// if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window);
// 2. Show a simple window that we create ourselves.We use a Begin /
// End pair to create a named window.
// {
// static float f = 0.0f;
// static int counter = 0;
// ImGui::Begin("Hello, world!", NULL,
// ImGuiWindowFlags_NoCollapse); // Create a window called
// // "Hello, world!"
// // and append into it.
// ImGui::SetCursorPos(ImVec2(0, 0));
// ImGui::SetWindowSize(ImVec2(500, 500));
// ImGui::Text("This is some useful text."); // Display some text (you
// can
// // use a format strings too)
// ImGui::Checkbox(
// "Demo Window",
// &show_demo_window); // Edit bools storing our window open/close
// state
// ImGui::Checkbox("Another Window", &show_another_window);
// ImGui::SliderFloat(
// "float", &f, 0.0f,
// 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
// ImGui::ColorEdit3(
// "clear color",
// (float *)&clear_color); // Edit 3 floats representing a color
// if (ImGui::Button(
// "Button")) // Buttons return true when clicked (most widgets
// // return true when edited/activated)
// counter++;
// ImGui::SameLine();
// ImGui::Text("counter = %d", counter);
// ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
// 1000.0f / io.Framerate, io.Framerate);
// ImGui::End();
// }
// 3. Show another simple window.
// if (show_another_window) {
// ImGui::Begin(
// "Another Window",
// &show_another_window); // Pass a pointer to our bool variable (the
// // window will have a closing button that
// will
// // clear the bool when clicked)
// ImGui::Text("Hello from another window!");
// if (ImGui::Button("Close Me")) show_another_window = false;
// ImGui::End();
// }
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
SDL_RenderSetScale(sdlRenderer, io.DisplayFramebufferScale.x, SDL_RenderSetScale(sdlRenderer, io.DisplayFramebufferScale.x,
@@ -500,7 +423,13 @@ int main() {
SDL_UpdateTexture(sdlTexture, NULL, dst_buffer, pixel_w); SDL_UpdateTexture(sdlTexture, NULL, dst_buffer, pixel_w);
SDL_RenderClear(sdlRenderer); SDL_RenderClear(sdlRenderer);
if (!joined || !received_frame) {
SDL_SetRenderDrawColor(
sdlRenderer, (Uint8)(clear_color.x * 0), (Uint8)(clear_color.y * 0),
(Uint8)(clear_color.z * 0), (Uint8)(clear_color.w * 0));
} else {
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect);
}
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData());
SDL_RenderPresent(sdlRenderer); SDL_RenderPresent(sdlRenderer);

View File

@@ -119,9 +119,9 @@ void RemoteDeskServer::ReceiveDataBuffer(const char *data, size_t size,
ip.mi.time = 0; ip.mi.time = 0;
// Set cursor pos // Set cursor pos
// SetCursorPos(ip.mi.dx, ip.mi.dy); SetCursorPos(ip.mi.dx, ip.mi.dy);
// Send the press // Send the press
// SendInput(1, &ip, sizeof(INPUT)); SendInput(1, &ip, sizeof(INPUT));
std::cout << "Receive data from [" << user << "], " << ip.type << " " std::cout << "Receive data from [" << user << "], " << ip.type << " "
<< ip.mi.dwFlags << " " << ip.mi.dx << " " << ip.mi.dy << ip.mi.dwFlags << " " << ip.mi.dx << " " << ip.mi.dy

View File

@@ -33,6 +33,8 @@ int CreateConnection(PeerPtr* peer_ptr, const char* transmission_id,
int JoinConnection(PeerPtr* peer_ptr, const char* transmission_id, int JoinConnection(PeerPtr* peer_ptr, const char* transmission_id,
const char* user_id); const char* user_id);
int LeaveConnection(PeerPtr* peer_ptr);
int SendData(PeerPtr* peer_ptr, DATA_TYPE data_type, const char* data, int SendData(PeerPtr* peer_ptr, DATA_TYPE data_type, const char* data,
size_t size); size_t size);

View File

@@ -180,7 +180,7 @@ int PeerConnection::Create(PeerConnectionParams params,
const std::string &user_id) { const std::string &user_id) {
int ret = 0; int ret = 0;
ret = Init(params, transmission_id, user_id); // ret = Init(params, transmission_id, user_id);
json message = {{"type", "create_transmission"}, json message = {{"type", "create_transmission"},
{"user_id", user_id}, {"user_id", user_id},
@@ -198,13 +198,23 @@ int PeerConnection::Join(PeerConnectionParams params,
const std::string &user_id) { const std::string &user_id) {
int ret = 0; int ret = 0;
ret = Init(params, transmission_id, user_id); // ret = Init(params, transmission_id, user_id);
transmission_id_ = transmission_id; transmission_id_ = transmission_id;
ret = RequestTransmissionMemberList(transmission_id_); ret = RequestTransmissionMemberList(transmission_id_);
return ret; return ret;
} }
int PeerConnection::Leave() {
for (auto &ice_transmission : ice_transmission_list_) {
ice_transmission.second->DestroyIceTransmission();
}
ice_transmission_list_.erase(ice_transmission_list_.begin(),
ice_transmission_list_.end());
return 0;
}
void PeerConnection::ProcessSignal(const std::string &signal) { void PeerConnection::ProcessSignal(const std::string &signal) {
auto j = json::parse(signal); auto j = json::parse(signal);
std::string type = j["type"]; std::string type = j["type"];

View File

@@ -31,6 +31,9 @@ class PeerConnection {
~PeerConnection(); ~PeerConnection();
public: public:
int Init(PeerConnectionParams params, const std::string &transmission_id,
const std::string &user_id);
int Create(PeerConnectionParams params, int Create(PeerConnectionParams params,
const std::string &transmission_id = "", const std::string &transmission_id = "",
const std::string &user_id = ""); const std::string &user_id = "");
@@ -38,6 +41,8 @@ class PeerConnection {
int Join(PeerConnectionParams params, const std::string &transmission_id, int Join(PeerConnectionParams params, const std::string &transmission_id,
const std::string &user_id = ""); const std::string &user_id = "");
int Leave();
int Destroy(); int Destroy();
SignalStatus GetSignalStatus(); SignalStatus GetSignalStatus();
@@ -47,9 +52,6 @@ class PeerConnection {
int SendUserData(const char *data, size_t size); int SendUserData(const char *data, size_t size);
private: private:
int Init(PeerConnectionParams params, const std::string &transmission_id,
const std::string &user_id);
int CreateVideoCodec(bool hardware_acceleration); int CreateVideoCodec(bool hardware_acceleration);
void ProcessSignal(const std::string &signal); void ProcessSignal(const std::string &signal);

View File

@@ -62,6 +62,9 @@ class RingBuffer {
if (isFull()) { if (isFull()) {
return false; return false;
} }
if (!m_data) {
return false;
}
m_data[m_rear] = value; m_data[m_rear] = value;
m_rear = (m_rear + 1) % m_size; m_rear = (m_rear + 1) % m_size;
return true; return true;
@@ -71,6 +74,9 @@ class RingBuffer {
if (isFull()) { if (isFull()) {
return false; return false;
} }
if (!m_data) {
return false;
}
m_data[m_rear] = *value; m_data[m_rear] = *value;
m_rear = (m_rear + 1) % m_size; m_rear = (m_rear + 1) % m_size;
return true; return true;

View File

@@ -31,6 +31,9 @@ PeerPtr *CreatePeer(const Params *params) {
int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id, int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id,
const char *user_id) { const char *user_id) {
peer_ptr->peer_connection->Init(peer_ptr->pc_params, transmission_id,
user_id);
peer_ptr->peer_connection->Create(peer_ptr->pc_params, transmission_id, peer_ptr->peer_connection->Create(peer_ptr->pc_params, transmission_id,
user_id); user_id);
return 0; return 0;
@@ -38,11 +41,19 @@ int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id,
int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id, int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id,
const char *user_id) { const char *user_id) {
peer_ptr->peer_connection->Init(peer_ptr->pc_params, transmission_id,
user_id);
peer_ptr->peer_connection->Join(peer_ptr->pc_params, transmission_id, peer_ptr->peer_connection->Join(peer_ptr->pc_params, transmission_id,
user_id); user_id);
return 0; return 0;
} }
int LeaveConnection(PeerPtr *peer_ptr) {
peer_ptr->peer_connection->Leave();
return 0;
}
int SendData(PeerPtr *peer_ptr, DATA_TYPE data_type, const char *data, int SendData(PeerPtr *peer_ptr, DATA_TYPE data_type, const char *data,
size_t size) { size_t size) {
if (DATA_TYPE::VIDEO == data_type) { if (DATA_TYPE::VIDEO == data_type) {

View File

@@ -32,7 +32,9 @@ WsCore::~WsCore() {
LOG_INFO("> Error closing connection {}", ec.message()); LOG_INFO("> Error closing connection {}", ec.message());
} }
if (m_thread_->joinable()) {
m_thread_->join(); m_thread_->join();
}
} }
int WsCore::Connect(std::string const &uri) { int WsCore::Connect(std::string const &uri) {