Use user id instead of ice username

This commit is contained in:
dijunkun
2023-08-21 17:31:44 +08:00
parent 515f0c06bd
commit d5c1c26fc9
118 changed files with 26528 additions and 26275 deletions

View File

@@ -1,127 +1,127 @@
#include "ice_agent.h"
#include <string.h>
#include <iostream>
#include "log.h"
IceAgent::IceAgent(std::string &ip, uint16_t port) : ip_(ip), port_(port) {}
IceAgent::~IceAgent() {}
int IceAgent::CreateIceAgent(juice_cb_state_changed_t on_state_changed,
juice_cb_candidate_t on_candidate,
juice_cb_gathering_done_t on_gathering_done,
juice_cb_recv_t on_recv, void *user_ptr) {
// juice_set_log_level(JUICE_LOG_LEVEL_DEBUG);
juice_config_t config;
memset(&config, 0, sizeof(config));
LOG_INFO("stun server ip[{}] port[{}]", ip_, port_);
// STUN server example
config.stun_server_host = ip_.c_str();
config.stun_server_port = port_;
config.cb_state_changed = on_state_changed;
config.cb_candidate = on_candidate;
config.cb_gathering_done = on_gathering_done;
config.cb_recv = on_recv;
config.user_ptr = user_ptr;
agent_ = juice_create(&config);
return 0;
}
int IceAgent::DestoryIceAgent() {
juice_destroy(agent_);
return 0;
}
char *IceAgent::GenerateLocalSdp() {
if (nullptr == agent_) {
LOG_INFO("agent_ is nullptr");
return nullptr;
}
juice_get_local_description(agent_, local_sdp_, JUICE_MAX_SDP_STRING_LEN);
// LOG_INFO("Generate local sdp:[\n{}]", local_sdp_);
LOG_INFO("Generate local sdp");
return local_sdp_;
}
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
LOG_INFO("Set remote sdp");
juice_set_remote_description(agent_, remote_sdp);
// LOG_INFO("Remote description:[\n{}]", remote_sdp);
return 0;
}
int IceAgent::GatherCandidates() {
LOG_INFO("[{}] Gather candidates", (void *)this);
juice_gather_candidates(agent_);
return 0;
}
juice_state_t IceAgent::GetIceState() {
state_ = juice_get_state(agent_);
return state_;
}
bool IceAgent::GetSelectedCandidates() {
char local[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
char remote[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
bool success = state_ == JUICE_STATE_COMPLETED;
if (success &= (juice_get_selected_candidates(
agent_, local, JUICE_MAX_CANDIDATE_SDP_STRING_LEN, remote,
JUICE_MAX_CANDIDATE_SDP_STRING_LEN) == 0)) {
LOG_INFO("Local candidate 1: {}", local);
LOG_INFO("Remote candidate 1: {}", remote);
if ((!strstr(local, "typ host") && !strstr(local, "typ prflx")) ||
(!strstr(remote, "typ host") && !strstr(remote, "typ prflx")))
success = false; // local connection should be possible
}
return success;
}
bool IceAgent::GetSelectedAddresses() {
char localAddr[JUICE_MAX_ADDRESS_STRING_LEN];
char remoteAddr[JUICE_MAX_ADDRESS_STRING_LEN];
bool success = state_ == JUICE_STATE_COMPLETED;
if (success &= (juice_get_selected_addresses(
agent_, localAddr, JUICE_MAX_ADDRESS_STRING_LEN,
remoteAddr, JUICE_MAX_ADDRESS_STRING_LEN) == 0)) {
LOG_INFO("Local address 1: {}", localAddr);
LOG_INFO("Remote address 1: {}", remoteAddr);
}
return success;
}
int IceAgent::AddRemoteCandidates(const char *remote_candidates) {
juice_add_remote_candidate(agent_, remote_candidates);
return 0;
}
int IceAgent::SetRemoteGatheringDone() {
juice_set_remote_gathering_done(agent_);
return 0;
}
int IceAgent::Send(const char *data, size_t size) {
juice_send(agent_, data, size);
return 0;
#include "ice_agent.h"
#include <string.h>
#include <iostream>
#include "log.h"
IceAgent::IceAgent(std::string &ip, uint16_t port) : ip_(ip), port_(port) {}
IceAgent::~IceAgent() {}
int IceAgent::CreateIceAgent(juice_cb_state_changed_t on_state_changed,
juice_cb_candidate_t on_candidate,
juice_cb_gathering_done_t on_gathering_done,
juice_cb_recv_t on_recv, void *user_ptr) {
// juice_set_log_level(JUICE_LOG_LEVEL_DEBUG);
juice_config_t config;
memset(&config, 0, sizeof(config));
LOG_INFO("stun server ip[{}] port[{}]", ip_, port_);
// STUN server example
config.stun_server_host = ip_.c_str();
config.stun_server_port = port_;
config.cb_state_changed = on_state_changed;
config.cb_candidate = on_candidate;
config.cb_gathering_done = on_gathering_done;
config.cb_recv = on_recv;
config.user_ptr = user_ptr;
agent_ = juice_create(&config);
return 0;
}
int IceAgent::DestoryIceAgent() {
juice_destroy(agent_);
return 0;
}
char *IceAgent::GenerateLocalSdp() {
if (nullptr == agent_) {
LOG_INFO("agent_ is nullptr");
return nullptr;
}
juice_get_local_description(agent_, local_sdp_, JUICE_MAX_SDP_STRING_LEN);
// LOG_INFO("Generate local sdp:[\n{}]", local_sdp_);
LOG_INFO("Generate local sdp");
return local_sdp_;
}
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
LOG_INFO("[{}] Set remote sdp", (void *)this);
juice_set_remote_description(agent_, remote_sdp);
// LOG_INFO("Remote description:[\n{}]", remote_sdp);
return 0;
}
int IceAgent::GatherCandidates() {
LOG_INFO("[{}] Gather candidates", (void *)this);
juice_gather_candidates(agent_);
return 0;
}
juice_state_t IceAgent::GetIceState() {
state_ = juice_get_state(agent_);
return state_;
}
bool IceAgent::GetSelectedCandidates() {
char local[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
char remote[JUICE_MAX_CANDIDATE_SDP_STRING_LEN];
bool success = state_ == JUICE_STATE_COMPLETED;
if (success &= (juice_get_selected_candidates(
agent_, local, JUICE_MAX_CANDIDATE_SDP_STRING_LEN, remote,
JUICE_MAX_CANDIDATE_SDP_STRING_LEN) == 0)) {
LOG_INFO("Local candidate 1: {}", local);
LOG_INFO("Remote candidate 1: {}", remote);
if ((!strstr(local, "typ host") && !strstr(local, "typ prflx")) ||
(!strstr(remote, "typ host") && !strstr(remote, "typ prflx")))
success = false; // local connection should be possible
}
return success;
}
bool IceAgent::GetSelectedAddresses() {
char localAddr[JUICE_MAX_ADDRESS_STRING_LEN];
char remoteAddr[JUICE_MAX_ADDRESS_STRING_LEN];
bool success = state_ == JUICE_STATE_COMPLETED;
if (success &= (juice_get_selected_addresses(
agent_, localAddr, JUICE_MAX_ADDRESS_STRING_LEN,
remoteAddr, JUICE_MAX_ADDRESS_STRING_LEN) == 0)) {
LOG_INFO("Local address 1: {}", localAddr);
LOG_INFO("Remote address 1: {}", remoteAddr);
}
return success;
}
int IceAgent::AddRemoteCandidates(const char *remote_candidates) {
juice_add_remote_candidate(agent_, remote_candidates);
return 0;
}
int IceAgent::SetRemoteGatheringDone() {
juice_set_remote_gathering_done(agent_);
return 0;
}
int IceAgent::Send(const char *data, size_t size) {
juice_send(agent_, data, size);
return 0;
}

View File

@@ -1,46 +1,46 @@
#ifndef _ICE_AGENT_H_
#define _ICE_AGENT_H_
#include <iostream>
#include "juice/juice.h"
class IceAgent {
public:
IceAgent(std::string& ip, uint16_t port);
~IceAgent();
int CreateIceAgent(juice_cb_state_changed_t on_state_changed,
juice_cb_candidate_t on_candidate,
juice_cb_gathering_done_t on_gathering_done,
juice_cb_recv_t on_recv, void* user_ptr);
int DestoryIceAgent();
char* GenerateLocalSdp();
int SetRemoteSdp(const char* remote_sdp);
int GatherCandidates();
juice_state_t GetIceState();
bool GetSelectedCandidates();
bool GetSelectedAddresses();
int AddRemoteCandidates(const char* remote_candidates);
int SetRemoteGatheringDone();
int Send(const char* data, size_t size);
private:
std::string ip_ = "";
uint16_t port_ = 0;
juice_agent_t* agent_ = nullptr;
char local_sdp_[JUICE_MAX_SDP_STRING_LEN];
juice_state_t state_;
};
#ifndef _ICE_AGENT_H_
#define _ICE_AGENT_H_
#include <iostream>
#include "juice/juice.h"
class IceAgent {
public:
IceAgent(std::string& ip, uint16_t port);
~IceAgent();
int CreateIceAgent(juice_cb_state_changed_t on_state_changed,
juice_cb_candidate_t on_candidate,
juice_cb_gathering_done_t on_gathering_done,
juice_cb_recv_t on_recv, void* user_ptr);
int DestoryIceAgent();
char* GenerateLocalSdp();
int SetRemoteSdp(const char* remote_sdp);
int GatherCandidates();
juice_state_t GetIceState();
bool GetSelectedCandidates();
bool GetSelectedAddresses();
int AddRemoteCandidates(const char* remote_candidates);
int SetRemoteGatheringDone();
int Send(const char* data, size_t size);
private:
std::string ip_ = "";
uint16_t port_ = 0;
juice_agent_t* agent_ = nullptr;
char local_sdp_[JUICE_MAX_SDP_STRING_LEN];
juice_state_t state_;
};
#endif

View File

@@ -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_);

View File

@@ -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