mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
Libjuice deprecated
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include <thread>
|
||||
|
||||
#include "gio/gnetworking.h"
|
||||
#include "glib.h"
|
||||
#include "nice/agent.h"
|
||||
|
||||
#define NICE_MAX_SDP_STRING_LEN 4096
|
||||
@@ -1,152 +0,0 @@
|
||||
#include "ice_agent.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
IceAgent::IceAgent(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)
|
||||
: stun_ip_(stun_ip),
|
||||
stun_port_(stun_port),
|
||||
turn_ip_(turn_ip),
|
||||
turn_port_(turn_port),
|
||||
turn_username_(turn_username),
|
||||
turn_password_(turn_password) {}
|
||||
|
||||
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_set_log_handler([](juice_log_level_t level, const char *message) {
|
||||
if (JUICE_LOG_LEVEL_VERBOSE == level) {
|
||||
LOG_INFO("{}", message);
|
||||
} else if (JUICE_LOG_LEVEL_DEBUG == level) {
|
||||
LOG_INFO("{}", message);
|
||||
} else if (JUICE_LOG_LEVEL_INFO == level) {
|
||||
LOG_INFO("{}", message);
|
||||
} else if (JUICE_LOG_LEVEL_WARN == level) {
|
||||
LOG_WARN("{}", message);
|
||||
} else if (JUICE_LOG_LEVEL_ERROR == level) {
|
||||
LOG_ERROR("{}", message);
|
||||
} else if (JUICE_LOG_LEVEL_FATAL == level) {
|
||||
LOG_FATAL("{}", message);
|
||||
} else if (JUICE_LOG_LEVEL_NONE == level) {
|
||||
LOG_INFO("{}", message);
|
||||
}
|
||||
});
|
||||
|
||||
memset(&config_, 0, sizeof(config_));
|
||||
|
||||
config_.stun_server_host = stun_ip_.c_str();
|
||||
config_.stun_server_port = stun_port_;
|
||||
|
||||
if (!turn_ip_.empty() && -1 != turn_port_ && !turn_username_.empty() &&
|
||||
!turn_password_.empty()) {
|
||||
memset(&turn_server_, 0, sizeof(turn_server_));
|
||||
turn_server_.host = turn_ip_.c_str();
|
||||
turn_server_.port = turn_port_;
|
||||
turn_server_.username = turn_username_.c_str();
|
||||
turn_server_.password = turn_password_.c_str();
|
||||
config_.turn_servers = &turn_server_;
|
||||
config_.turn_servers_count = 1;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
config_.local_port_range_begin = 40000;
|
||||
config_.local_port_range_end = 50000;
|
||||
|
||||
agent_ = juice_create(&config_);
|
||||
|
||||
LOG_INFO("Juice agent init finish");
|
||||
|
||||
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_);
|
||||
|
||||
return local_sdp_;
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteSdp(const char *remote_sdp) {
|
||||
return juice_set_remote_description(agent_, remote_sdp);
|
||||
}
|
||||
|
||||
int IceAgent::GatherCandidates() { return juice_gather_candidates(agent_); }
|
||||
|
||||
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) {
|
||||
return juice_add_remote_candidate(agent_, remote_candidates);
|
||||
}
|
||||
|
||||
int IceAgent::SetRemoteGatheringDone() {
|
||||
return juice_set_remote_gathering_done(agent_);
|
||||
}
|
||||
|
||||
int IceAgent::Send(const char *data, size_t size) {
|
||||
if (juice_state_t::JUICE_STATE_COMPLETED != juice_get_state(agent_)) {
|
||||
return -1;
|
||||
}
|
||||
return juice_send(agent_, data, size);
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
#ifndef _ICE_AGENT_H_
|
||||
#define _ICE_AGENT_H_
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "juice/juice.h"
|
||||
#include "nice/agent.h"
|
||||
|
||||
class IceAgent {
|
||||
public:
|
||||
IceAgent(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(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 stun_ip_ = "";
|
||||
uint16_t stun_port_ = 0;
|
||||
std::string turn_ip_ = "";
|
||||
uint16_t turn_port_ = 0;
|
||||
std::string turn_username_ = "";
|
||||
std::string turn_password_ = "";
|
||||
juice_agent_t* agent_ = nullptr;
|
||||
char local_sdp_[JUICE_MAX_SDP_STRING_LEN];
|
||||
juice_state_t state_;
|
||||
juice_config_t config_;
|
||||
juice_turn_server_t turn_server_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -115,7 +115,6 @@ int IceTransmission::InitIceTransmission(std::string &stun_ip, int stun_port,
|
||||
std::make_unique<IceAgent>(offer_peer_, stun_ip, stun_port, turn_ip,
|
||||
turn_port, turn_username, turn_password);
|
||||
|
||||
#ifdef USE_NICE
|
||||
ice_agent_->CreateIceAgent(
|
||||
[](NiceAgent *agent, guint stream_id, guint component_id,
|
||||
NiceComponentState state, gpointer user_ptr) {
|
||||
@@ -171,66 +170,6 @@ int IceTransmission::InitIceTransmission(std::string &stun_ip, int stun_port,
|
||||
}
|
||||
},
|
||||
this);
|
||||
#else
|
||||
ice_agent_->CreateIceAgent(
|
||||
[](juice_agent_t *agent, juice_state_t state, void *user_ptr) {
|
||||
if (user_ptr) {
|
||||
IceTransmission *ice_transmission_obj =
|
||||
static_cast<IceTransmission *>(user_ptr);
|
||||
LOG_INFO("[{}->{}] state_change: {}", ice_transmission_obj->user_id_,
|
||||
ice_transmission_obj->remote_user_id_,
|
||||
juice_state_to_string(state));
|
||||
ice_transmission_obj->state_ = state;
|
||||
ice_transmission_obj->on_ice_status_change_(
|
||||
juice_state_to_string(state));
|
||||
} else {
|
||||
LOG_INFO("state_change: {}", juice_state_to_string(state));
|
||||
}
|
||||
},
|
||||
[](juice_agent_t *agent, const char *sdp, void *user_ptr) {
|
||||
LOG_INFO("candadite: {}", sdp);
|
||||
// trickle
|
||||
// static_cast<IceTransmission
|
||||
// *>(user_ptr)->SendOfferLocalCandidate(sdp);
|
||||
},
|
||||
[](juice_agent_t *agent, void *user_ptr) {
|
||||
// non-trickle
|
||||
if (user_ptr) {
|
||||
IceTransmission *ice_transmission_obj =
|
||||
static_cast<IceTransmission *>(user_ptr);
|
||||
LOG_INFO("[{}] gather_done", ice_transmission_obj->user_id_);
|
||||
|
||||
if (ice_transmission_obj->offer_peer_) {
|
||||
ice_transmission_obj->GetLocalSdp();
|
||||
ice_transmission_obj->SendOffer();
|
||||
|
||||
} else {
|
||||
ice_transmission_obj->CreateAnswer();
|
||||
ice_transmission_obj->SendAnswer();
|
||||
}
|
||||
}
|
||||
},
|
||||
[](juice_agent_t *agent, const char *buffer, size_t size,
|
||||
void *user_ptr) {
|
||||
if (user_ptr) {
|
||||
IceTransmission *ice_transmission_obj =
|
||||
static_cast<IceTransmission *>(user_ptr);
|
||||
if (ice_transmission_obj) {
|
||||
if (ice_transmission_obj->CheckIsVideoPacket(buffer, size)) {
|
||||
RtpPacket packet((uint8_t *)buffer, size);
|
||||
ice_transmission_obj->rtp_video_receiver_->InsertRtpPacket(
|
||||
packet);
|
||||
} else if (ice_transmission_obj->CheckIsDataPacket(buffer, size)) {
|
||||
RtpPacket packet((uint8_t *)buffer, size);
|
||||
ice_transmission_obj->rtp_data_receiver_->InsertRtpPacket(packet);
|
||||
} else if (ice_transmission_obj->CheckIsRtcpPacket(buffer, size)) {
|
||||
// LOG_ERROR("Rtcp packet [{}]", (uint8_t)(buffer[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
this);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -316,11 +255,7 @@ int IceTransmission::SendAnswer() {
|
||||
}
|
||||
|
||||
int IceTransmission::SendData(DATA_TYPE type, const char *data, size_t size) {
|
||||
#ifdef USE_NICE
|
||||
if (NiceComponentState::NICE_COMPONENT_STATE_READY == state_) {
|
||||
#else
|
||||
if (juice_state_t::JUICE_STATE_COMPLETED == state_) {
|
||||
#endif
|
||||
std::vector<RtpPacket> packets;
|
||||
|
||||
if (DATA_TYPE::VIDEO == type) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "congestion_control.h"
|
||||
#include "ice_agent.h"
|
||||
#include "ringbuffer.h"
|
||||
#include "rtp_codec.h"
|
||||
#include "rtp_data_receiver.h"
|
||||
@@ -13,14 +14,6 @@
|
||||
#include "rtp_video_sender.h"
|
||||
#include "ws_transmission.h"
|
||||
|
||||
#define USE_NICE 1
|
||||
|
||||
#ifdef USE_NICE
|
||||
#include "libnice/ice_agent.h"
|
||||
#else
|
||||
#include "libjuice/ice_agent.h"
|
||||
#endif
|
||||
|
||||
class IceTransmission {
|
||||
public:
|
||||
typedef enum { VIDEO = 96, AUDIO = 97, DATA = 127 } DATA_TYPE;
|
||||
@@ -106,11 +99,7 @@ class IceTransmission {
|
||||
std::string remote_user_id_ = "";
|
||||
bool offer_peer_ = true;
|
||||
std::string remote_ice_username_ = "";
|
||||
#ifdef USE_NICE
|
||||
NiceComponentState state_ = NICE_COMPONENT_STATE_DISCONNECTED;
|
||||
#else
|
||||
juice_state_t state_ = JUICE_STATE_DISCONNECTED;
|
||||
#endif
|
||||
|
||||
private:
|
||||
std::unique_ptr<RtpCodec> video_rtp_codec_ = nullptr;
|
||||
|
||||
Reference in New Issue
Block a user