[feat] add config param to control use TURN or not

This commit is contained in:
dijunkun
2024-08-27 17:05:41 +08:00
parent 35f26283de
commit 0b0e61cdc4
8 changed files with 45 additions and 24 deletions

View File

@@ -5,10 +5,12 @@
#include "log.h"
IceAgent::IceAgent(bool trickle_ice, bool offer_peer, std::string &stun_ip,
uint16_t stun_port, std::string &turn_ip, uint16_t turn_port,
IceAgent::IceAgent(bool enable_turn, bool trickle_ice, bool offer_peer,
std::string &stun_ip, uint16_t stun_port,
std::string &turn_ip, uint16_t turn_port,
std::string &turn_username, std::string &turn_password)
: trickle_ice_(trickle_ice),
: enable_turn_(enable_turn),
trickle_ice_(trickle_ice),
stun_ip_(stun_ip),
stun_port_(stun_port),
turn_ip_(turn_ip),
@@ -50,9 +52,12 @@ int IceAgent::CreateIceAgent(nice_cb_state_changed_t on_state_changed,
agent_ = nice_agent_new_full(
g_main_loop_get_context(gloop_), NICE_COMPATIBILITY_RFC5245,
(NiceAgentOption)(trickle_ice_ ? NICE_AGENT_OPTION_ICE_TRICKLE |
NICE_AGENT_OPTION_NONE
: NICE_AGENT_OPTION_NONE));
(NiceAgentOption)(trickle_ice_
? NICE_AGENT_OPTION_ICE_TRICKLE |
(enable_turn_ ? NICE_AGENT_OPTION_NONE
: NICE_AGENT_OPTION_RELIABLE)
: (enable_turn_ ? NICE_AGENT_OPTION_NONE
: NICE_AGENT_OPTION_RELIABLE)));
if (agent_ == nullptr) {
LOG_ERROR("Failed to create agent_");
@@ -78,10 +83,12 @@ int IceAgent::CreateIceAgent(nice_cb_state_changed_t on_state_changed,
nice_agent_set_stream_name(agent_, stream_id_, "video");
if (enable_turn_) {
nice_agent_set_relay_info(agent_, stream_id_, n_components_,
turn_ip_.c_str(), turn_port_,
turn_username_.c_str(), turn_password_.c_str(),
NICE_RELAY_TYPE_TURN_UDP);
NICE_RELAY_TYPE_TURN_TCP);
}
// g_object_set(agent_, "force-relay", true, NULL);

View File

@@ -29,9 +29,10 @@ typedef void (*nice_cb_recv_t)(NiceAgent* agent, guint stream_id,
class IceAgent {
public:
IceAgent(bool trickle_ice, bool offer_peer, std::string& stun_ip,
uint16_t stun_port, std::string& turn_ip, uint16_t turn_port,
std::string& turn_username, std::string& turn_password);
IceAgent(bool enable_turn, bool trickle_ice, bool offer_peer,
std::string& stun_ip, uint16_t stun_port, std::string& turn_ip,
uint16_t turn_port, std::string& turn_username,
std::string& turn_password);
~IceAgent();
int CreateIceAgent(nice_cb_state_changed_t on_state_changed,
@@ -57,6 +58,7 @@ class IceAgent {
int Send(const char* data, size_t size);
public:
bool enable_turn_ = false;
std::string stun_ip_ = "";
uint16_t stun_port_ = 0;
std::string turn_ip_ = "";

View File

@@ -63,6 +63,7 @@ typedef struct {
const char* turn_server_password;
bool hardware_acceleration;
bool av1_encoding;
bool enable_turn;
OnReceiveBuffer on_receive_video_buffer;
OnReceiveBuffer on_receive_audio_buffer;

View File

@@ -75,6 +75,7 @@ int PeerConnection::Init(PeerConnectionParams params,
cfg_turn_server_password_ = params.turn_server_password;
hardware_acceleration_ = params.hardware_acceleration;
av1_encoding_ = params.av1_encoding;
enable_turn_ = params.enable_turn;
cfg_signal_server_port_ = std::to_string(signal_server_port_);
cfg_stun_server_port_ = std::to_string(stun_server_port_);
@@ -185,6 +186,10 @@ int PeerConnection::Init(PeerConnectionParams params,
ice_ready_ = false;
on_connection_status_(ConnectionStatus::Closed, user_data_);
LOG_INFO("Ice closed");
} else if ("failed" == ice_status) {
ice_ready_ = false;
on_connection_status_(ConnectionStatus::Failed, user_data_);
LOG_INFO("Ice failed");
} else {
ice_ready_ = false;
}
@@ -469,8 +474,8 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
// }
ice_transmission_list_[remote_user_id] =
std::make_unique<IceTransmission>(
trickle_ice_, true, transmission_id, user_id_, remote_user_id,
ws_transport_, on_ice_status_change_);
enable_turn_, trickle_ice_, true, transmission_id, user_id_,
remote_user_id, ws_transport_, on_ice_status_change_);
ice_transmission_list_[remote_user_id]->SetOnReceiveVideoFunc(
on_receive_video_);
@@ -522,8 +527,8 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
ice_transmission_list_[remote_user_id] =
std::make_unique<IceTransmission>(
trickle_ice_, false, transmission_id, user_id_, remote_user_id,
ws_transport_, on_ice_status_change_);
enable_turn_, trickle_ice_, false, transmission_id, user_id_,
remote_user_id, ws_transport_, on_ice_status_change_);
ice_transmission_list_[remote_user_id]->SetOnReceiveVideoFunc(
on_receive_video_);

View File

@@ -37,6 +37,7 @@ typedef struct {
const char *turn_server_password;
bool hardware_acceleration;
bool av1_encoding;
bool enable_turn;
OnReceiveBuffer on_receive_video_buffer;
OnReceiveBuffer on_receive_audio_buffer;
@@ -99,6 +100,7 @@ class PeerConnection {
int turn_server_port_ = 0;
bool hardware_acceleration_ = false;
bool av1_encoding_ = false;
bool enable_turn_ = false;
bool trickle_ice_ = true;
TraversalMode mode_ = TraversalMode::P2P;

View File

@@ -27,6 +27,7 @@ PeerPtr *CreatePeer(const Params *params) {
peer_ptr->pc_params.turn_server_password = params->turn_server_password;
peer_ptr->pc_params.hardware_acceleration = params->hardware_acceleration;
peer_ptr->pc_params.av1_encoding = params->av1_encoding;
peer_ptr->pc_params.enable_turn = params->enable_turn;
}
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;

View File

@@ -12,11 +12,13 @@
using nlohmann::json;
IceTransmission::IceTransmission(
bool trickle_ice, bool offer_peer, std::string &transmission_id,
std::string &user_id, std::string &remote_user_id,
bool enable_turn, bool trickle_ice, bool offer_peer,
std::string &transmission_id, std::string &user_id,
std::string &remote_user_id,
std::shared_ptr<WsTransmission> ice_ws_transmission,
std::function<void(std::string)> on_ice_status_change)
: trickle_ice_(trickle_ice),
: enable_turn_(enable_turn),
trickle_ice_(trickle_ice),
offer_peer_(offer_peer),
transmission_id_(transmission_id),
user_id_(user_id),
@@ -163,9 +165,9 @@ int IceTransmission::InitIceTransmission(
remote_user_id_.size());
});
ice_agent_ = std::make_unique<IceAgent>(trickle_ice_, offer_peer_, stun_ip,
stun_port, turn_ip, turn_port,
turn_username, turn_password);
ice_agent_ = std::make_unique<IceAgent>(
enable_turn_, trickle_ice_, offer_peer_, stun_ip, stun_port, turn_ip,
turn_port, turn_username, turn_password);
ice_agent_->CreateIceAgent(
[](NiceAgent *agent, guint stream_id, guint component_id,

View File

@@ -34,7 +34,7 @@ class IceTransmission {
enum TraversalType { TP2P = 0, TRelay = 1, TUnknown = 2 };
public:
IceTransmission(bool trickle_ice, bool offer_peer,
IceTransmission(bool enable_turn, bool trickle_ice, bool offer_peer,
std::string &transmission_id, std::string &user_id,
std::string &remote_user_id,
std::shared_ptr<WsTransmission> ice_ws_transmission,
@@ -105,6 +105,7 @@ class IceTransmission {
uint8_t CheckIsDataPacket(const char *buffer, size_t size);
private:
bool enable_turn_ = false;
bool trickle_ice_ = true;
std::string local_sdp_;
std::string remote_sdp_;