Add callback for connection status

This commit is contained in:
dijunkun
2023-10-11 17:01:14 +08:00
parent 4261dff416
commit 34274001dc
6 changed files with 53 additions and 9 deletions

View File

@@ -59,6 +59,7 @@ uint32_t start_time, end_time, elapsed_time;
uint32_t frame_count = 0; uint32_t frame_count = 0;
int fps = 0; int fps = 0;
std::string window_title = "Remote Desk Client"; std::string window_title = "Remote Desk Client";
std::string connection_status = "-";
// Refresh Event // Refresh Event
#define REFRESH_EVENT (SDL_USEREVENT + 1) #define REFRESH_EVENT (SDL_USEREVENT + 1)
@@ -70,6 +71,9 @@ PeerPtr *peer_client = nullptr;
bool joined = false; bool joined = false;
bool received_frame = false; bool received_frame = false;
static bool connect_button_pressed = false;
static const char *connect_label = "Connect";
ScreenCaptureWgc *screen_capture = nullptr; ScreenCaptureWgc *screen_capture = nullptr;
char *nv12_buffer_ = nullptr; char *nv12_buffer_ = nullptr;
@@ -258,6 +262,25 @@ void ReceiveDataBuffer(const char *data, size_t size, const char *user_id,
#endif #endif
} }
void ConnectionStatus(ConnectionStatus status) {
if (ConnectionStatus::Connecting == status) {
connection_status = "Connecting";
} else if (ConnectionStatus::Connected == status) {
connection_status = "Connected";
} else if (ConnectionStatus::Failed == status) {
connection_status = "Failed";
} else if (ConnectionStatus::Closed == status) {
connection_status = "Closed";
} else if (ConnectionStatus::IncorrectPassword == status) {
connection_status = "Incorrect password";
if (connect_button_pressed) {
connect_button_pressed = false;
joined = false;
connect_label = connect_button_pressed ? "Disconnect" : "Connect";
}
}
}
std::string GetMac(char *mac_addr) { std::string GetMac(char *mac_addr) {
int len = 0; int len = 0;
#ifdef _WIN32 #ifdef _WIN32
@@ -372,6 +395,7 @@ int main() {
params.on_receive_video_buffer = ReceiveVideoBuffer; params.on_receive_video_buffer = ReceiveVideoBuffer;
params.on_receive_audio_buffer = ReceiveAudioBuffer; params.on_receive_audio_buffer = ReceiveAudioBuffer;
params.on_receive_data_buffer = ReceiveDataBuffer; params.on_receive_data_buffer = ReceiveDataBuffer;
params.on_connection_status = ConnectionStatus;
std::string transmission_id = "000001"; std::string transmission_id = "000001";
char mac_addr[10]; char mac_addr[10];
@@ -549,8 +573,6 @@ int main() {
ImGui::Spacing(); ImGui::Spacing();
{ {
static bool connect_button_pressed = false;
static const char *connect_label = "Connect";
{ {
static char remote_id[20] = ""; static char remote_id[20] = "";
if (strcmp(remote_id, "") == 0) { if (strcmp(remote_id, "") == 0) {
@@ -583,6 +605,7 @@ int main() {
LeaveConnection(peer_client); LeaveConnection(peer_client);
joined = false; joined = false;
} }
connect_button_pressed = !connect_button_pressed; connect_button_pressed = !connect_button_pressed;
connect_label = connect_button_pressed ? "Disconnect" : "Connect"; connect_label = connect_button_pressed ? "Disconnect" : "Connect";
} }
@@ -662,7 +685,8 @@ int main() {
if (elapsed_time >= 1000) { if (elapsed_time >= 1000) {
fps = frame_count / (elapsed_time / 1000); fps = frame_count / (elapsed_time / 1000);
frame_count = 0; frame_count = 0;
window_title = "Remote Desk Client FPS [" + std::to_string(fps) + "]"; window_title = "Remote Desk Client FPS [" + std::to_string(fps) +
"] status [" + connection_status + "]";
// For MacOS, UI frameworks can only be called from the main thread // For MacOS, UI frameworks can only be called from the main thread
SDL_SetWindowTitle(window, window_title.c_str()); SDL_SetWindowTitle(window, window_title.c_str());
start_time = end_time; start_time = end_time;

View File

@@ -4,8 +4,14 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
enum ws_status { WS_CONNECTING = 0, WS_OPEN, WS_FAILED, WS_CLOSED, WS_UNKNOWN };
enum DATA_TYPE { VIDEO = 0, AUDIO, DATA }; enum DATA_TYPE { VIDEO = 0, AUDIO, DATA };
enum ConnectionStatus {
Connecting = 0,
Connected,
Failed,
Closed,
IncorrectPassword
};
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -15,6 +21,8 @@ typedef struct Peer PeerPtr;
typedef void (*OnReceiveBuffer)(const char*, size_t, const char*, size_t); typedef void (*OnReceiveBuffer)(const char*, size_t, const char*, size_t);
typedef void (*OnConnectionStatus)(ConnectionStatus status);
typedef void (*NetStatusReport)(const unsigned short, const unsigned short); typedef void (*NetStatusReport)(const unsigned short, const unsigned short);
typedef struct { typedef struct {
@@ -22,6 +30,7 @@ typedef struct {
OnReceiveBuffer on_receive_video_buffer; OnReceiveBuffer on_receive_video_buffer;
OnReceiveBuffer on_receive_audio_buffer; OnReceiveBuffer on_receive_audio_buffer;
OnReceiveBuffer on_receive_data_buffer; OnReceiveBuffer on_receive_data_buffer;
OnConnectionStatus on_connection_status;
NetStatusReport net_status_report; NetStatusReport net_status_report;
} Params; } Params;

View File

@@ -65,6 +65,7 @@ int PeerConnection::Init(PeerConnectionParams params,
on_receive_video_buffer_ = params.on_receive_video_buffer; on_receive_video_buffer_ = params.on_receive_video_buffer;
on_receive_audio_buffer_ = params.on_receive_audio_buffer; on_receive_audio_buffer_ = params.on_receive_audio_buffer;
on_receive_data_buffer_ = params.on_receive_data_buffer; on_receive_data_buffer_ = params.on_receive_data_buffer;
on_connection_status_ = params.on_connection_status;
on_receive_ws_msg_ = [this](const std::string &msg) { ProcessSignal(msg); }; on_receive_ws_msg_ = [this](const std::string &msg) { ProcessSignal(msg); };
@@ -97,6 +98,7 @@ int PeerConnection::Init(PeerConnectionParams params,
on_ice_status_change_ = [this](std::string ice_status) { on_ice_status_change_ = [this](std::string ice_status) {
if ("completed" == ice_status) { if ("completed" == ice_status) {
ice_ready_ = true; ice_ready_ = true;
on_connection_status_(ConnectionStatus::Connected);
b_force_i_frame_ = true; b_force_i_frame_ = true;
LOG_INFO("Ice finish"); LOG_INFO("Ice finish");
} else { } else {
@@ -111,7 +113,7 @@ int PeerConnection::Init(PeerConnectionParams params,
} }
do { do {
} while (SignalStatus::Connected != GetSignalStatus()); } while (SignalStatus::SignalConnected != GetSignalStatus());
nv12_data_ = new char[1280 * 720 * 3 / 2]; nv12_data_ = new char[1280 * 720 * 3 / 2];
@@ -238,7 +240,7 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
LOG_INFO("Receive local peer websocket connection id [{}]", LOG_INFO("Receive local peer websocket connection id [{}]",
ws_connection_id_); ws_connection_id_);
std::lock_guard<std::mutex> l(signal_status_mutex_); std::lock_guard<std::mutex> l(signal_status_mutex_);
signal_status_ = SignalStatus::Connected; signal_status_ = SignalStatus::SignalConnected;
break; break;
} }
case "transmission_id"_H: { case "transmission_id"_H: {
@@ -260,6 +262,7 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
if (status == "failed") { if (status == "failed") {
std::string reason = j["reason"].get<std::string>(); std::string reason = j["reason"].get<std::string>();
LOG_ERROR("{}", reason); LOG_ERROR("{}", reason);
on_connection_status_(ConnectionStatus::IncorrectPassword);
} else { } else {
if (user_id_list_.empty()) { if (user_id_list_.empty()) {
LOG_WARN("Wait for host create transmission [{}]", transmission_id); LOG_WARN("Wait for host create transmission [{}]", transmission_id);
@@ -295,6 +298,8 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
cfg_turn_server_password_); cfg_turn_server_password_);
ice_transmission_list_[remote_user_id]->JoinTransmission(); ice_transmission_list_[remote_user_id]->JoinTransmission();
} }
on_connection_status_(ConnectionStatus::Connecting);
} }
break; break;

View File

@@ -9,12 +9,15 @@
#include "video_decoder_factory.h" #include "video_decoder_factory.h"
#include "video_encoder_factory.h" #include "video_encoder_factory.h"
#include "ws_transmission.h" #include "ws_transmission.h"
#include "x.h"
enum SignalStatus { Connecting = 0, Connected, Closed }; enum SignalStatus { SignalConnecting = 0, SignalConnected, SignalClosed };
typedef void (*OnReceiveBuffer)(const char *, size_t, const char *, typedef void (*OnReceiveBuffer)(const char *, size_t, const char *,
const size_t); const size_t);
typedef void (*OnConnectionStatus)(ConnectionStatus status);
typedef void (*NetStatusReport)(const unsigned short, const unsigned short); typedef void (*NetStatusReport)(const unsigned short, const unsigned short);
typedef struct { typedef struct {
@@ -22,6 +25,7 @@ typedef struct {
OnReceiveBuffer on_receive_video_buffer; OnReceiveBuffer on_receive_video_buffer;
OnReceiveBuffer on_receive_audio_buffer; OnReceiveBuffer on_receive_audio_buffer;
OnReceiveBuffer on_receive_data_buffer; OnReceiveBuffer on_receive_data_buffer;
OnConnectionStatus on_connection_status;
NetStatusReport net_status_report; NetStatusReport net_status_report;
} PeerConnectionParams; } PeerConnectionParams;
@@ -81,7 +85,7 @@ class PeerConnection {
std::string user_id_ = ""; std::string user_id_ = "";
std::string transmission_id_ = ""; std::string transmission_id_ = "";
std::vector<std::string> user_id_list_; std::vector<std::string> user_id_list_;
SignalStatus signal_status_ = SignalStatus::Closed; SignalStatus signal_status_ = SignalStatus::SignalClosed;
std::mutex signal_status_mutex_; std::mutex signal_status_mutex_;
private: private:
@@ -99,6 +103,7 @@ class PeerConnection {
OnReceiveBuffer on_receive_video_buffer_; OnReceiveBuffer on_receive_video_buffer_;
OnReceiveBuffer on_receive_audio_buffer_; OnReceiveBuffer on_receive_audio_buffer_;
OnReceiveBuffer on_receive_data_buffer_; OnReceiveBuffer on_receive_data_buffer_;
OnConnectionStatus on_connection_status_;
char *nv12_data_ = nullptr; char *nv12_data_ = nullptr;
bool inited_ = false; bool inited_ = false;
std::string password_; std::string password_;

View File

@@ -19,6 +19,7 @@ PeerPtr *CreatePeer(const Params *params) {
peer_ptr->pc_params.on_receive_video_buffer = params->on_receive_video_buffer; peer_ptr->pc_params.on_receive_video_buffer = params->on_receive_video_buffer;
peer_ptr->pc_params.on_receive_audio_buffer = params->on_receive_audio_buffer; peer_ptr->pc_params.on_receive_audio_buffer = params->on_receive_audio_buffer;
peer_ptr->pc_params.on_receive_data_buffer = params->on_receive_data_buffer; peer_ptr->pc_params.on_receive_data_buffer = params->on_receive_data_buffer;
peer_ptr->pc_params.on_connection_status = params->on_connection_status;
peer_ptr->pc_params.net_status_report = params->net_status_report; peer_ptr->pc_params.net_status_report = params->net_status_report;
return peer_ptr; return peer_ptr;

View File

@@ -168,7 +168,7 @@ target("pc")
add_deps("ws", "ice", "transmission", "inih", "common", "media") add_deps("ws", "ice", "transmission", "inih", "common", "media")
add_files("src/pc/*.cpp") add_files("src/pc/*.cpp")
add_packages("asio", "nlohmann_json", "cuda") add_packages("asio", "nlohmann_json", "cuda")
add_includedirs("src/transmission", {public = true}) add_includedirs("src/transmission", "src/interface", {public = true})
target("projectx") target("projectx")