mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
Use user id instead of ice username
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[signal server]
|
||||
ip = localhost
|
||||
port = 9095
|
||||
port = 9090
|
||||
|
||||
[stun server]
|
||||
ip = 120.77.216.215
|
||||
|
||||
@@ -56,7 +56,7 @@ char *IceAgent::GenerateLocalSdp() {
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
|
||||
LOG_INFO("Set remote sdp");
|
||||
LOG_INFO("[{}] Set remote sdp", (void *)this);
|
||||
juice_set_remote_description(agent_, remote_sdp);
|
||||
// LOG_INFO("Remote description:[\n{}]", remote_sdp);
|
||||
|
||||
|
||||
@@ -15,9 +15,11 @@ const std::vector<std::string> ice_status = {
|
||||
"JUICE_STATE_COMPLETED", "JUICE_STATE_FAILED"};
|
||||
|
||||
IceTransmission::IceTransmission(
|
||||
bool offer_peer, WsTransmission *ice_ws_transmission,
|
||||
bool offer_peer, std::string remote_ice_username,
|
||||
WsTransmission *ice_ws_transmission,
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg)
|
||||
: offer_peer_(offer_peer),
|
||||
remote_ice_username_(remote_ice_username),
|
||||
ice_ws_transport_(ice_ws_transmission),
|
||||
on_receive_ice_msg_cb_(on_receive_ice_msg) {}
|
||||
|
||||
@@ -45,6 +47,7 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
|
||||
if (ice_transmission_obj->offer_peer_) {
|
||||
ice_transmission_obj->GetLocalSdp();
|
||||
ice_transmission_obj->SendOffer();
|
||||
LOG_INFO("[{}] SendOffer", (void *)ice_transmission_obj)
|
||||
} else {
|
||||
ice_transmission_obj->CreateAnswer();
|
||||
ice_transmission_obj->SendAnswer();
|
||||
@@ -90,10 +93,18 @@ int IceTransmission::CreateTransmission(const std::string &transmission_id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::JoinTransmission(const std::string &transmission_id) {
|
||||
int IceTransmission::SetTransmissionId(const std::string &transmission_id) {
|
||||
transmission_id_ = transmission_id;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int IceTransmission::JoinTransmission(const std::string &transmission_id,
|
||||
const std::string &user_id) {
|
||||
LOG_INFO("Join transport");
|
||||
offer_peer_ = true;
|
||||
transmission_id_ = transmission_id;
|
||||
user_id_ = user_id;
|
||||
|
||||
// if (SignalStatus::Connected != signal_status_) {
|
||||
// LOG_ERROR("Not connect to signalserver");
|
||||
@@ -112,6 +123,7 @@ int IceTransmission::GatherCandidates() {
|
||||
|
||||
int IceTransmission::GetLocalSdp() {
|
||||
local_sdp_ = ice_agent_->GenerateLocalSdp();
|
||||
LOG_INFO("Local ice username: [{}]", GetIceUsername(local_sdp_));
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -127,7 +139,7 @@ int IceTransmission::AddRemoteCandidate(const std::string &remote_candidate) {
|
||||
}
|
||||
|
||||
int IceTransmission::CreateOffer() {
|
||||
LOG_INFO("Create offer");
|
||||
LOG_INFO("[{}] Create offer", (void *)this);
|
||||
GatherCandidates();
|
||||
return 0;
|
||||
}
|
||||
@@ -135,6 +147,8 @@ int IceTransmission::CreateOffer() {
|
||||
int IceTransmission::SendOffer() {
|
||||
json message = {{"type", "offer"},
|
||||
{"transmission_id", transmission_id_},
|
||||
{"user_id", user_id_},
|
||||
{"remote_peer", remote_ice_username_},
|
||||
{"sdp", local_sdp_}};
|
||||
// LOG_INFO("Send offer:\n{}", message.dump().c_str());
|
||||
LOG_INFO("Send offer");
|
||||
@@ -166,7 +180,7 @@ int IceTransmission::SendAnswer() {
|
||||
{"transmission_id", transmission_id_},
|
||||
{"sdp", local_sdp_},
|
||||
{"guest", remote_ice_username_}};
|
||||
// LOG_INFO("Send answer:\n{}", message.dump().c_str());
|
||||
|
||||
LOG_INFO("[{}] Send answer to [{}]", GetIceUsername(local_sdp_),
|
||||
remote_ice_username_);
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
|
||||
class IceTransmission {
|
||||
public:
|
||||
IceTransmission(bool offer_peer, WsTransmission *ice_ws_transmission,
|
||||
IceTransmission(bool offer_peer, std::string remote_ice_username,
|
||||
WsTransmission *ice_ws_transmission,
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg);
|
||||
|
||||
~IceTransmission();
|
||||
@@ -18,7 +19,10 @@ class IceTransmission {
|
||||
int DestroyIceTransmission();
|
||||
|
||||
int CreateTransmission(const std::string &transmission_id);
|
||||
int JoinTransmission(const std::string &transmission_id);
|
||||
int JoinTransmission(const std::string &transmission_id,
|
||||
const std::string &user_id);
|
||||
|
||||
int SetTransmissionId(const std::string &transmission_id);
|
||||
|
||||
int SendData(const char *data, size_t size);
|
||||
|
||||
@@ -55,12 +59,13 @@ class IceTransmission {
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg_cb_ = nullptr;
|
||||
std::string local_sdp_;
|
||||
std::string remote_sdp_;
|
||||
std::string remote_ice_username_;
|
||||
std::string local_candidates_;
|
||||
std::string remote_candidates_;
|
||||
unsigned int connection_id_ = 0;
|
||||
std::string transmission_id_ = "";
|
||||
std::string user_id_ = "";
|
||||
bool offer_peer_ = true;
|
||||
std::string remote_ice_username_ = "";
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -27,7 +27,8 @@ PeerPtr* CreatePeer(const Params* params);
|
||||
|
||||
int CreateConnection(PeerPtr* peer_ptr);
|
||||
|
||||
int JoinConnection(PeerPtr* peer_ptr, const char* connection_id);
|
||||
int JoinConnection(PeerPtr* peer_ptr, const char* transmission_id,
|
||||
const char* user_id);
|
||||
|
||||
int SendData(PeerPtr* peer_ptr, const char* data, size_t size);
|
||||
|
||||
|
||||
@@ -21,7 +21,10 @@ PeerConnection::PeerConnection() {}
|
||||
PeerConnection::~PeerConnection() {}
|
||||
|
||||
int PeerConnection::Create(PeerConnectionParams params,
|
||||
const std::string &transmission_id) {
|
||||
const std::string &transmission_id,
|
||||
const std::string &user_id) {
|
||||
user_id_ = user_id;
|
||||
|
||||
INIReader reader(params.cfg_path);
|
||||
cfg_signal_server_ip_ = reader.Get("signal server", "ip", "-1");
|
||||
cfg_signal_server_port_ = reader.Get("signal server", "port", "-1");
|
||||
@@ -70,11 +73,15 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
// IceTransmission *ice_transmission =
|
||||
// new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username] =
|
||||
new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username] = new IceTransmission(
|
||||
false, ice_username, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetTransmissionId(
|
||||
transmission_id_);
|
||||
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
|
||||
ice_transmission_list_[ice_username]->GatherCandidates();
|
||||
@@ -103,9 +110,8 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
// ice_transmission_->CreateTransmission(transmission_id);
|
||||
|
||||
json message = {{"type", "create_transmission"},
|
||||
{"user_id", user_id},
|
||||
{"transmission_id", transmission_id}};
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Send(message.dump());
|
||||
@@ -115,7 +121,11 @@ int PeerConnection::Create(PeerConnectionParams params,
|
||||
}
|
||||
|
||||
int PeerConnection::Join(PeerConnectionParams params,
|
||||
const std::string &transmission_id) {
|
||||
const std::string &transmission_id,
|
||||
const std::string &user_id) {
|
||||
// Todo: checkout user_id unique or not
|
||||
user_id_ = user_id;
|
||||
|
||||
INIReader reader(params.cfg_path);
|
||||
cfg_signal_server_ip_ = reader.Get("signal server", "ip", "-1");
|
||||
cfg_signal_server_port_ = reader.Get("signal server", "port", "-1");
|
||||
@@ -129,12 +139,43 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
stun_server_port_ = stoi(cfg_stun_server_port_);
|
||||
|
||||
on_receive_ws_msg_ = [this](const std::string &msg) {
|
||||
do {
|
||||
} while (ice_transmission_list_.empty());
|
||||
// do {
|
||||
// } while (ice_transmission_list_.empty());
|
||||
auto j = json::parse(msg);
|
||||
std::string type = j["type"];
|
||||
LOG_INFO("msg type :{}", type.c_str());
|
||||
LOG_INFO("msg type :{}", type);
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "transmission_members"_H: {
|
||||
transmission_member_list_ = j["transmission_members"];
|
||||
std::string transmission_id = j["transmission_id"];
|
||||
|
||||
LOG_INFO("Transmission [{}] members: [", transmission_id);
|
||||
for (auto member : transmission_member_list_) {
|
||||
LOG_INFO("{}", member);
|
||||
}
|
||||
LOG_INFO("]");
|
||||
|
||||
if (transmission_member_list_.size() == 1 &&
|
||||
transmission_member_list_[0] == "host") {
|
||||
ice_transmission_list_["host"] = new IceTransmission(
|
||||
true, "host", ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_["host"]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
ice_transmission_list_["host"]->JoinTransmission(transmission_id,
|
||||
user_id_);
|
||||
} else {
|
||||
for (auto &member : transmission_member_list_) {
|
||||
ice_transmission_list_[member] = new IceTransmission(
|
||||
true, member, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[member]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
ice_transmission_list_[member]->JoinTransmission(transmission_id,
|
||||
user_id_);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "ws_connection_id"_H: {
|
||||
ws_connection_id_ = j["ws_connection_id"].get<unsigned int>();
|
||||
LOG_INFO("Receive local peer websocket connection id [{}]",
|
||||
@@ -154,8 +195,8 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
// IceTransmission *ice_transmission =
|
||||
// new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
|
||||
ice_transmission_list_[ice_username] =
|
||||
new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username] = new IceTransmission(
|
||||
false, ice_username, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
|
||||
@@ -174,15 +215,23 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
LOG_INFO("Receive remote sdp from [{}]", ice_username);
|
||||
// LOG_INFO("Receive remote sdp [{}]", remote_sdp);
|
||||
|
||||
// if (ice_transmission_list_.size() == 1 &&
|
||||
// ice_transmission_list_.begin()->first == "host") {
|
||||
// ice_transmission_list_["host"]->SetRemoteSdp(remote_sdp);
|
||||
// } else if (ice_transmission_list_.find(ice_username) ==
|
||||
// ice_transmission_list_.end()) {
|
||||
// ice_transmission_list_[ice_username] = new IceTransmission(
|
||||
// false, ice_username, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
// cfg_stun_server_ip_, stun_server_port_);
|
||||
// ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
// }
|
||||
|
||||
if (ice_transmission_list_.size() == 1 &&
|
||||
ice_transmission_list_.begin()->first == "self") {
|
||||
ice_transmission_list_["self"]->SetRemoteSdp(remote_sdp);
|
||||
} else if (ice_transmission_list_.find(ice_username) ==
|
||||
ice_transmission_list_.begin()->first == "host") {
|
||||
ice_transmission_list_["host"]->SetRemoteSdp(remote_sdp);
|
||||
} else if (ice_transmission_list_.find(ice_username) !=
|
||||
ice_transmission_list_.end()) {
|
||||
ice_transmission_list_[ice_username] =
|
||||
new IceTransmission(false, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_[ice_username]->InitIceTransmission(
|
||||
cfg_stun_server_ip_, stun_server_port_);
|
||||
ice_transmission_list_[ice_username]->SetRemoteSdp(remote_sdp);
|
||||
}
|
||||
|
||||
@@ -222,10 +271,10 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
ws_transport_->Connect(uri_);
|
||||
}
|
||||
|
||||
ice_transmission_list_["self"] =
|
||||
new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
ice_transmission_list_["self"]->InitIceTransmission(cfg_stun_server_ip_,
|
||||
stun_server_port_);
|
||||
// ice_transmission_list_["self"] =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_list_["self"]->InitIceTransmission(cfg_stun_server_ip_,
|
||||
// stun_server_port_);
|
||||
// ice_transmission_ =
|
||||
// new IceTransmission(true, ws_transport_, on_receive_ice_msg_);
|
||||
// ice_transmission_->InitIceTransmission(cfg_stun_server_ip,
|
||||
@@ -235,8 +284,22 @@ int PeerConnection::Join(PeerConnectionParams params,
|
||||
// LOG_INFO("GetSignalStatus = {}", GetSignalStatus());
|
||||
} while (SignalStatus::Connected != GetSignalStatus());
|
||||
|
||||
RequestTransmissionMemberList(transmission_id_);
|
||||
// ice_transmission_->JoinTransmission(transmission_id_);
|
||||
ice_transmission_list_["self"]->JoinTransmission(transmission_id_);
|
||||
// ice_transmission_list_["self"]->JoinTransmission(transmission_id_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PeerConnection::RequestTransmissionMemberList(
|
||||
const std::string &transmission_id) {
|
||||
LOG_INFO("Request member list");
|
||||
|
||||
json message = {{"type", "query_members"},
|
||||
{"transmission_id", transmission_id_}};
|
||||
|
||||
if (ws_transport_) {
|
||||
ws_transport_->Send(message.dump());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,15 @@ class PeerConnection {
|
||||
~PeerConnection();
|
||||
|
||||
public:
|
||||
int Create(PeerConnectionParams params, const std::string &id = "");
|
||||
int Join(PeerConnectionParams params, const std::string &id);
|
||||
int Create(PeerConnectionParams params,
|
||||
const std::string &transmission_id = "",
|
||||
const std::string &user_id = "");
|
||||
int Join(PeerConnectionParams params, const std::string &transmission_id,
|
||||
const std::string &user_id = "");
|
||||
int Destroy();
|
||||
|
||||
int RequestTransmissionMemberList(const std::string &transmission_id);
|
||||
|
||||
SignalStatus GetSignalStatus();
|
||||
|
||||
int SendData(const char *data, size_t size);
|
||||
@@ -44,10 +49,12 @@ class PeerConnection {
|
||||
int stun_server_port_ = 0;
|
||||
WsTransmission *ws_transport_ = nullptr;
|
||||
IceTransmission *ice_transmission_ = nullptr;
|
||||
std::vector<std::string> transmission_member_list_;
|
||||
std::map<std::string, IceTransmission *> ice_transmission_list_;
|
||||
std::function<void(const std::string &)> on_receive_ws_msg_ = nullptr;
|
||||
std::function<void(const char *, size_t)> on_receive_ice_msg_ = nullptr;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
std::string user_id_ = "";
|
||||
std::string transmission_id_ = "";
|
||||
SignalStatus signal_status_ = SignalStatus::Closed;
|
||||
};
|
||||
|
||||
@@ -27,13 +27,17 @@ int CreateConnection(PeerPtr *peer_ptr) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CreateConnection(PeerPtr *peer_ptr, const char *connection_id) {
|
||||
peer_ptr->peer_connection->Create(peer_ptr->pc_params, connection_id);
|
||||
int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
||||
const char *user_id) {
|
||||
peer_ptr->peer_connection->Create(peer_ptr->pc_params, transmission_id,
|
||||
user_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int JoinConnection(PeerPtr *peer_ptr, const char *connection_id) {
|
||||
peer_ptr->peer_connection->Join(peer_ptr->pc_params, connection_id);
|
||||
int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
||||
const char *user_id) {
|
||||
peer_ptr->peer_connection->Join(peer_ptr->pc_params, transmission_id,
|
||||
user_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,12 @@ int main(int argc, char** argv) {
|
||||
params.cfg_path = "../../../../config/config.ini";
|
||||
|
||||
std::string transmission_id = "000000";
|
||||
std::string user_id = argv[1];
|
||||
// std::cout << "Please input which transmisson want to join: ";
|
||||
// std::cin >> transmission_id;
|
||||
|
||||
PeerPtr* peer = CreatePeer(¶ms);
|
||||
JoinConnection(peer, transmission_id.c_str());
|
||||
JoinConnection(peer, transmission_id.c_str(), user_id.c_str());
|
||||
|
||||
std::string msg = "Offer peer";
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ bool SignalServer::on_pong(websocketpp::connection_hdl hdl, std::string s) {
|
||||
}
|
||||
|
||||
void SignalServer::run(uint16_t port) {
|
||||
server_.set_reuse_addr(true);
|
||||
server_.listen(port);
|
||||
|
||||
// Queues a connection accept operation
|
||||
@@ -95,6 +96,7 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "create_transmission"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string user_id = j["user_id"].get<std::string>();
|
||||
LOG_INFO("Receive create transmission request with id [{}]",
|
||||
transmission_id);
|
||||
if (transmission_list_.find(transmission_id) ==
|
||||
@@ -111,7 +113,16 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
transmission_id);
|
||||
}
|
||||
transmission_list_.insert(transmission_id);
|
||||
transmission_manager_.BindHostToTransmission(hdl, transmission_id);
|
||||
|
||||
transmission_manager_.BindWsHandleToTransmission(hdl, transmission_id);
|
||||
transmission_manager_.BindUserIdToTransmission(user_id,
|
||||
transmission_id);
|
||||
transmission_manager_.BindUserIdToWsHandle(user_id, hdl);
|
||||
transmission_manager_.BindUserNameToUserId("host", user_id);
|
||||
|
||||
// if (transmission_manager_.GetUsername(hdl).empty()) {
|
||||
// transmission_manager_.BindUsernameToWsHandle("host", hdl);
|
||||
// }
|
||||
|
||||
LOG_INFO("Create transmission id [{}]", transmission_id);
|
||||
json message = {{"type", "transmission_id"},
|
||||
@@ -129,37 +140,6 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
// LOG_INFO("Receive transmission id [{}] with offer sdp [{}]",
|
||||
// transmission_id, sdp);
|
||||
transmission_manager_.BindGuestToTransmission(hdl, transmission_id);
|
||||
std::string guest_username = GetIceUsername(sdp);
|
||||
transmission_manager_.BindGuestUsernameToWsHandle(guest_username, hdl);
|
||||
|
||||
websocketpp::connection_hdl host_hdl =
|
||||
transmission_manager_.GetHostOfTransmission(transmission_id);
|
||||
std::vector<websocketpp::connection_hdl> guest_hdl_list =
|
||||
transmission_manager_.GetAllGuestsOfTransmission(transmission_id);
|
||||
|
||||
// LOG_INFO("send offer sdp [{}]", sdp);
|
||||
json message = {
|
||||
{"type", "offer"}, {"sdp", sdp}, {"guest", guest_username}};
|
||||
|
||||
LOG_INFO("[{}] send offer sdp to host", guest_username);
|
||||
send_msg(host_hdl, message);
|
||||
|
||||
LOG_INFO("Size of guest_hdl_list: {}", guest_hdl_list.size());
|
||||
for (auto guest_hdl : guest_hdl_list) {
|
||||
if (guest_hdl.lock().get() != hdl.lock().get()) {
|
||||
LOG_INFO("[{}] send offer sdp to [{}]", guest_username,
|
||||
transmission_manager_.GetGuestUsername(guest_hdl));
|
||||
send_msg(guest_hdl, message);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "query_members"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::vector<std::string> member_list =
|
||||
@@ -169,23 +149,44 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
{"transmission_id", transmission_id},
|
||||
{"transmission_members", member_list},
|
||||
{"status", "success"}};
|
||||
|
||||
LOG_INFO("Send member_list: [{}]", message.dump());
|
||||
send_msg(hdl, message);
|
||||
break;
|
||||
}
|
||||
case "offer"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string remote_peer = j["remote_peer"].get<std::string>();
|
||||
// LOG_INFO("Receive transmission id [{}] with offer sdp [{}]",
|
||||
// transmission_id, sdp);
|
||||
transmission_manager_.BindWsHandleToTransmission(hdl, transmission_id);
|
||||
std::string offer_peer = GetIceUsername(sdp);
|
||||
transmission_manager_.BindUsernameToWsHandle(offer_peer, hdl);
|
||||
|
||||
websocketpp::connection_hdl destination_hdl =
|
||||
transmission_manager_.GetWsHandle(remote_peer);
|
||||
|
||||
json message = {{"type", "offer"}, {"sdp", sdp}};
|
||||
|
||||
LOG_INFO("[{}] send offer sdp to [{}]", offer_peer, remote_peer);
|
||||
send_msg(destination_hdl, message);
|
||||
|
||||
break;
|
||||
}
|
||||
case "answer"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string sdp = j["sdp"].get<std::string>();
|
||||
std::string guest_ice_username = j["guest"].get<std::string>();
|
||||
std::string host_ice_username = GetIceUsername(sdp);
|
||||
if (transmission_manager_.GetHostUsername(hdl).empty()) {
|
||||
transmission_manager_.BindHostUsernameToWsHandle(host_ice_username,
|
||||
hdl);
|
||||
if (transmission_manager_.GetUsername(hdl) == "host") {
|
||||
LOG_INFO("Update transmission [{}] [host] to [{}]", transmission_id,
|
||||
host_ice_username);
|
||||
transmission_manager_.UpdateUsernameToWsHandle(host_ice_username, hdl);
|
||||
}
|
||||
// LOG_INFO("Receive transmission id [{}] with answer sdp [{}]",
|
||||
// transmission_id, sdp);
|
||||
|
||||
websocketpp::connection_hdl guest_hdl =
|
||||
transmission_manager_.GetGuestWsHandle(guest_ice_username);
|
||||
transmission_manager_.GetWsHandle(guest_ice_username);
|
||||
|
||||
// LOG_INFO("send answer sdp [{}]", sdp);
|
||||
LOG_INFO("[{}] send answer sdp to [{}]", host_ice_username,
|
||||
|
||||
@@ -6,101 +6,114 @@ TransmissionManager::TransmissionManager() {}
|
||||
|
||||
TransmissionManager::~TransmissionManager() {}
|
||||
|
||||
bool TransmissionManager::BindHostToTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
if (transmission_host_list_.find(transmission_id) !=
|
||||
transmission_host_list_.end()) {
|
||||
LOG_WARN("Transmission already has a host [{}]",
|
||||
transmission_host_list_[transmission_id].lock().get());
|
||||
return false;
|
||||
} else {
|
||||
transmission_host_list_[transmission_id] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// bool TransmissionManager::BindHostToTransmission(
|
||||
// websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
// if (transmission_host_list_.find(transmission_id) !=
|
||||
// transmission_host_list_.end()) {
|
||||
// LOG_WARN("Transmission already has a host [{}]",
|
||||
// transmission_host_list_[transmission_id].lock().get());
|
||||
// return false;
|
||||
// } else {
|
||||
// transmission_host_list_[transmission_id] = hdl;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool TransmissionManager::BindGuestToTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
if (transmission_guest_list_.find(transmission_id) !=
|
||||
transmission_guest_list_.end()) {
|
||||
transmission_guest_list_[transmission_id].push_back(hdl);
|
||||
} else {
|
||||
std::vector<websocketpp::connection_hdl> guest_hdl_list;
|
||||
guest_hdl_list.push_back(hdl);
|
||||
transmission_guest_list_[transmission_id] = guest_hdl_list;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// bool TransmissionManager::BindGuestToTransmission(
|
||||
// websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
// if (transmission_guest_list_.find(transmission_id) !=
|
||||
// transmission_guest_list_.end()) {
|
||||
// transmission_guest_list_[transmission_id].push_back(hdl);
|
||||
// } else {
|
||||
// std::vector<websocketpp::connection_hdl> guest_hdl_list;
|
||||
// guest_hdl_list.push_back(hdl);
|
||||
// transmission_guest_list_[transmission_id] = guest_hdl_list;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool TransmissionManager::ReleaseHostFromTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
// bool TransmissionManager::ReleaseHostFromTransmission(
|
||||
// websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool TransmissionManager::ReleaseGuestFromTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
// bool TransmissionManager::ReleaseGuestFromTransmission(
|
||||
// websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool TransmissionManager::BindHostUsernameToWsHandle(
|
||||
const std::string& host_username, websocketpp::connection_hdl hdl) {
|
||||
if (transmission_host_username_list_.find(host_username) !=
|
||||
transmission_host_username_list_.end()) {
|
||||
LOG_ERROR("Guest already bind to username [{}]", host_username.c_str());
|
||||
return false;
|
||||
} else {
|
||||
transmission_host_username_list_[host_username] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// bool TransmissionManager::BindHostUsernameToWsHandle(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// if (transmission_host_username_list_.find("host") !=
|
||||
// transmission_host_username_list_.end()) {
|
||||
// LOG_ERROR("Host already exist");
|
||||
// return false;
|
||||
// } else {
|
||||
// transmission_host_username_list_["host"] = hdl;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
bool TransmissionManager::BindGuestUsernameToWsHandle(
|
||||
const std::string& guest_username, websocketpp::connection_hdl hdl) {
|
||||
if (transmission_guest_username_list_.find(guest_username) !=
|
||||
transmission_guest_username_list_.end()) {
|
||||
LOG_ERROR("Guest already bind to username [{}]", guest_username.c_str());
|
||||
return false;
|
||||
} else {
|
||||
transmission_guest_username_list_[guest_username] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// bool TransmissionManager::UpdateHostUsernameToWsHandle(
|
||||
// const std::string& host_username, websocketpp::connection_hdl hdl) {
|
||||
// if (transmission_host_username_list_.find("host") ==
|
||||
// transmission_host_username_list_.end()) {
|
||||
// LOG_ERROR("Host not exist");
|
||||
// return false;
|
||||
// }
|
||||
// transmission_host_username_list_.erase("host");
|
||||
// transmission_host_username_list_[host_username] = hdl;
|
||||
|
||||
websocketpp::connection_hdl TransmissionManager::GetHostOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
if (transmission_host_list_.find(transmission_id) !=
|
||||
transmission_host_list_.end()) {
|
||||
return transmission_host_list_[transmission_id];
|
||||
} else {
|
||||
websocketpp::connection_hdl hdl;
|
||||
return hdl;
|
||||
}
|
||||
}
|
||||
// return true;
|
||||
// }
|
||||
|
||||
std::string TransmissionManager::GetHostUsername(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto host : transmission_host_username_list_) {
|
||||
if (host.second.lock().get() == hdl.lock().get()) return host.first;
|
||||
}
|
||||
// bool TransmissionManager::BindGuestUsernameToWsHandle(
|
||||
// const std::string& guest_username, websocketpp::connection_hdl hdl) {
|
||||
// if (transmission_guest_username_list_.find(guest_username) !=
|
||||
// transmission_guest_username_list_.end()) {
|
||||
// LOG_ERROR("Guest already bind to username [{}]", guest_username.c_str());
|
||||
// return false;
|
||||
// } else {
|
||||
// transmission_guest_username_list_[guest_username] = hdl;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
return "";
|
||||
}
|
||||
// websocketpp::connection_hdl TransmissionManager::GetHostOfTransmission(
|
||||
// const std::string& transmission_id) {
|
||||
// if (transmission_host_list_.find(transmission_id) !=
|
||||
// transmission_host_list_.end()) {
|
||||
// return transmission_host_list_[transmission_id];
|
||||
// } else {
|
||||
// websocketpp::connection_hdl hdl;
|
||||
// return hdl;
|
||||
// }
|
||||
// }
|
||||
|
||||
std::string TransmissionManager::GetGuestUsername(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto guest : transmission_guest_username_list_) {
|
||||
if (guest.second.lock().get() == hdl.lock().get()) return guest.first;
|
||||
}
|
||||
// std::string TransmissionManager::GetHostUsername(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// for (auto host : transmission_host_username_list_) {
|
||||
// if (host.second.lock().get() == hdl.lock().get()) return host.first;
|
||||
// }
|
||||
|
||||
return "";
|
||||
}
|
||||
// return "";
|
||||
// }
|
||||
|
||||
// std::string TransmissionManager::GetGuestUsername(
|
||||
// websocketpp::connection_hdl hdl) {
|
||||
// for (auto guest : transmission_guest_username_list_) {
|
||||
// if (guest.second.lock().get() == hdl.lock().get()) return guest.first;
|
||||
// }
|
||||
|
||||
// return "";
|
||||
// }
|
||||
|
||||
std::vector<websocketpp::connection_hdl>
|
||||
TransmissionManager::GetAllGuestsOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
if (transmission_guest_list_.find(transmission_id) !=
|
||||
transmission_guest_list_.end()) {
|
||||
return transmission_guest_list_[transmission_id];
|
||||
if (transmission_user_ws_hdl_list_.find(transmission_id) !=
|
||||
transmission_user_ws_hdl_list_.end()) {
|
||||
return transmission_user_ws_hdl_list_[transmission_id];
|
||||
} else {
|
||||
return std::vector<websocketpp::connection_hdl>();
|
||||
}
|
||||
@@ -121,12 +134,127 @@ std::vector<std::string> TransmissionManager::GetAllMembersOfTransmission(
|
||||
const std::string& transmission_id) {
|
||||
std::vector<std::string> member_list;
|
||||
|
||||
member_list.push_back(
|
||||
GetHostUsername(GetHostOfTransmission(transmission_id)));
|
||||
|
||||
for (auto guest_hdl : GetAllGuestsOfTransmission(transmission_id)) {
|
||||
member_list.push_back(GetGuestUsername(guest_hdl));
|
||||
member_list.push_back(GetUsername(guest_hdl));
|
||||
}
|
||||
|
||||
return member_list;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindWsHandleToTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
if (transmission_user_ws_hdl_list_.find(transmission_id) ==
|
||||
transmission_user_ws_hdl_list_.end()) {
|
||||
transmission_user_ws_hdl_list_[transmission_id].push_back(hdl);
|
||||
return true;
|
||||
} else {
|
||||
auto hdl_list = transmission_user_ws_hdl_list_[transmission_id];
|
||||
for (auto h : hdl_list) {
|
||||
if (h.lock().get() == hdl.lock().get()) {
|
||||
LOG_ERROR("Ws handle [{}] already bind to transmission [{}]",
|
||||
hdl.lock().get(), transmission_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
transmission_user_ws_hdl_list_[transmission_id].push_back(hdl);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUserIdToTransmission(
|
||||
const std::string& user_id, const std::string& transmission_id) {
|
||||
if (transmission_user_id_list_.find(transmission_id) ==
|
||||
transmission_user_id_list_.end()) {
|
||||
transmission_user_id_list_[transmission_id].push_back(user_id);
|
||||
return true;
|
||||
} else {
|
||||
auto user_id_list = transmission_user_id_list_[transmission_id];
|
||||
for (auto id : user_id_list) {
|
||||
if (id == user_id) {
|
||||
LOG_ERROR("User id [{}] already bind to transmission [{}]", user_id,
|
||||
transmission_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
transmission_user_id_list_[transmission_id].push_back(user_id);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUserIdToWsHandle(
|
||||
const std::string& user_id, websocketpp::connection_hdl hdl) {
|
||||
if (user_id_ws_hdl_list_.find(user_id) != user_id_ws_hdl_list_.end()) {
|
||||
LOG_ERROR("User id already bind to websocket handle [{}]", user_id,
|
||||
hdl.lock().get());
|
||||
return false;
|
||||
} else {
|
||||
user_id_ws_hdl_list_[user_id] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUserNameToUserId(const std::string& user_name,
|
||||
const std::string& user_id) {
|
||||
if (user_name_user_id_list_.find(user_id) == user_name_user_id_list_.end()) {
|
||||
user_name_user_id_list_[user_id].push_back(user_name);
|
||||
return true;
|
||||
} else {
|
||||
auto user_name_list = user_name_user_id_list_[user_id];
|
||||
for (auto name : user_name_list) {
|
||||
if (name == user_name) {
|
||||
LOG_ERROR("User name [{}] already bind to user id [{}]", user_name,
|
||||
user_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
user_name_user_id_list_[user_id].push_back(user_name);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::ReleaseWsHandleFromTransmission(
|
||||
websocketpp::connection_hdl hdl, const std::string& transmission_id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::BindUsernameToWsHandle(
|
||||
const std::string& username, websocketpp::connection_hdl hdl) {
|
||||
if (username_ws_hdl_list_.find(username) != username_ws_hdl_list_.end()) {
|
||||
LOG_ERROR("Guest already bind to username [{}]", username.c_str());
|
||||
return false;
|
||||
} else {
|
||||
username_ws_hdl_list_[username] = hdl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::UpdateUsernameToWsHandle(
|
||||
const std::string& username, websocketpp::connection_hdl hdl) {
|
||||
if (username_ws_hdl_list_.find("host") == username_ws_hdl_list_.end()) {
|
||||
LOG_ERROR("Host not exist");
|
||||
return false;
|
||||
}
|
||||
username_ws_hdl_list_.erase("host");
|
||||
username_ws_hdl_list_[username] = hdl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string TransmissionManager::GetUsername(websocketpp::connection_hdl hdl) {
|
||||
for (auto guest : username_ws_hdl_list_) {
|
||||
if (guest.second.lock().get() == hdl.lock().get()) return guest.first;
|
||||
}
|
||||
|
||||
LOG_ERROR("No user with websocket handle [{}]", hdl.lock().get());
|
||||
return "";
|
||||
}
|
||||
|
||||
websocketpp::connection_hdl TransmissionManager::GetWsHandle(
|
||||
const std::string& username) {
|
||||
if (username_ws_hdl_list_.find(username) != username_ws_hdl_list_.end()) {
|
||||
return username_ws_hdl_list_[username];
|
||||
} else {
|
||||
websocketpp::connection_hdl hdl;
|
||||
return hdl;
|
||||
}
|
||||
}
|
||||
@@ -11,22 +11,23 @@ class TransmissionManager {
|
||||
~TransmissionManager();
|
||||
|
||||
public:
|
||||
bool BindHostToTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool BindGuestToTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool ReleaseHostFromTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool ReleaseGuestFromTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
// bool BindHostToTransmission(websocketpp::connection_hdl hdl,
|
||||
// const std::string& transmission_id);
|
||||
// bool BindGuestToTransmission(websocketpp::connection_hdl hdl,
|
||||
// const std::string& transmission_id);
|
||||
// bool ReleaseHostFromTransmission(websocketpp::connection_hdl hdl,
|
||||
// const std::string& transmission_id);
|
||||
// bool ReleaseGuestFromTransmission(websocketpp::connection_hdl hdl,
|
||||
// const std::string& transmission_id);
|
||||
|
||||
bool BindHostUsernameToWsHandle(const std::string& host_username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
bool BindGuestUsernameToWsHandle(const std::string& guest_username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
// bool BindHostUsernameToWsHandle(websocketpp::connection_hdl hdl);
|
||||
// bool UpdateHostUsernameToWsHandle(const std::string& host_username,
|
||||
// websocketpp::connection_hdl hdl);
|
||||
// bool BindGuestUsernameToWsHandle(const std::string& guest_username,
|
||||
// websocketpp::connection_hdl hdl);
|
||||
|
||||
std::string GetHostUsername(websocketpp::connection_hdl hdl);
|
||||
std::string GetGuestUsername(websocketpp::connection_hdl hdl);
|
||||
// std::string GetHostUsername(websocketpp::connection_hdl hdl);
|
||||
// std::string GetGuestUsername(websocketpp::connection_hdl hdl);
|
||||
|
||||
websocketpp::connection_hdl GetHostOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
@@ -38,6 +39,25 @@ class TransmissionManager {
|
||||
std::vector<std::string> GetAllMembersOfTransmission(
|
||||
const std::string& transmission_id);
|
||||
|
||||
public:
|
||||
bool BindWsHandleToTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool BindUserIdToTransmission(const std::string& user_id,
|
||||
const std::string& transmission_id);
|
||||
bool BindUserIdToWsHandle(const std::string& user_id,
|
||||
websocketpp::connection_hdl hdl);
|
||||
bool BindUserNameToUserId(const std::string& user_name,
|
||||
const std::string& user_id);
|
||||
|
||||
bool ReleaseWsHandleFromTransmission(websocketpp::connection_hdl hdl,
|
||||
const std::string& transmission_id);
|
||||
bool BindUsernameToWsHandle(const std::string& username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
bool UpdateUsernameToWsHandle(const std::string& username,
|
||||
websocketpp::connection_hdl hdl);
|
||||
std::string GetUsername(websocketpp::connection_hdl hdl);
|
||||
websocketpp::connection_hdl GetWsHandle(const std::string& username);
|
||||
|
||||
private:
|
||||
std::map<std::string, websocketpp::connection_hdl> transmission_host_list_;
|
||||
std::map<std::string, std::vector<websocketpp::connection_hdl>>
|
||||
@@ -47,6 +67,15 @@ class TransmissionManager {
|
||||
transmission_host_username_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl>
|
||||
transmission_guest_username_list_;
|
||||
|
||||
private:
|
||||
std::map<std::string, std::vector<websocketpp::connection_hdl>>
|
||||
transmission_user_ws_hdl_list_;
|
||||
std::map<std::string, std::vector<std::string>> transmission_user_id_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl> user_id_ws_hdl_list_;
|
||||
std::map<std::string, std::vector<std::string>> user_name_user_id_list_;
|
||||
|
||||
std::map<std::string, websocketpp::connection_hdl> username_ws_hdl_list_;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user