mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-27 04:35:34 +08:00
1.Use std::move to initialize std::thread; 2.Fix thread cannot exit error
This commit is contained in:
@@ -16,6 +16,9 @@ PeerConnection::~PeerConnection() {
|
|||||||
delete nv12_data_;
|
delete nv12_data_;
|
||||||
nv12_data_ = nullptr;
|
nv12_data_ = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
video_codec_inited_ = false;
|
||||||
|
audio_codec_inited_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int PeerConnection::Init(PeerConnectionParams params,
|
int PeerConnection::Init(PeerConnectionParams params,
|
||||||
@@ -194,6 +197,10 @@ int PeerConnection::Init(PeerConnectionParams params,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
||||||
|
if (video_codec_inited_) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
hardware_acceleration_ = hardware_acceleration;
|
hardware_acceleration_ = hardware_acceleration;
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
if (hardware_acceleration_) {
|
if (hardware_acceleration_) {
|
||||||
@@ -257,10 +264,16 @@ int PeerConnection::CreateVideoCodec(bool hardware_acceleration) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
video_codec_inited_ = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int PeerConnection::CreateAudioCodec() {
|
int PeerConnection::CreateAudioCodec() {
|
||||||
|
if (audio_codec_inited_) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
audio_encoder_ = std::make_unique<AudioEncoder>(AudioEncoder(48000, 1, 480));
|
audio_encoder_ = std::make_unique<AudioEncoder>(AudioEncoder(48000, 1, 480));
|
||||||
if (!audio_encoder_ || 0 != audio_encoder_->Init()) {
|
if (!audio_encoder_ || 0 != audio_encoder_->Init()) {
|
||||||
LOG_ERROR("Audio encoder init failed");
|
LOG_ERROR("Audio encoder init failed");
|
||||||
@@ -273,6 +286,7 @@ int PeerConnection::CreateAudioCodec() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
audio_codec_inited_ = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,6 +353,8 @@ int PeerConnection::Leave() {
|
|||||||
user_id_it.second->DestroyIceTransmission();
|
user_id_it.second->DestroyIceTransmission();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ice_transmission_list_.clear();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,20 +492,15 @@ void PeerConnection::ProcessSignal(const std::string &signal) {
|
|||||||
on_receive_audio_);
|
on_receive_audio_);
|
||||||
ice_transmission_list_[remote_user_id]->SetOnReceiveDataFunc(
|
ice_transmission_list_[remote_user_id]->SetOnReceiveDataFunc(
|
||||||
on_receive_data_);
|
on_receive_data_);
|
||||||
|
|
||||||
ice_transmission_list_[remote_user_id]->InitIceTransmission(
|
ice_transmission_list_[remote_user_id]->InitIceTransmission(
|
||||||
cfg_stun_server_ip_, stun_server_port_, cfg_turn_server_ip_,
|
cfg_stun_server_ip_, stun_server_port_, cfg_turn_server_ip_,
|
||||||
turn_server_port_, cfg_turn_server_username_,
|
turn_server_port_, cfg_turn_server_username_,
|
||||||
cfg_turn_server_password_,
|
cfg_turn_server_password_,
|
||||||
av1_encoding_ ? RtpPacket::AV1 : RtpPacket::H264);
|
av1_encoding_ ? RtpPacket::AV1 : RtpPacket::H264);
|
||||||
|
|
||||||
ice_transmission_list_[remote_user_id]->SetTransmissionId(
|
ice_transmission_list_[remote_user_id]->SetTransmissionId(
|
||||||
transmission_id_);
|
transmission_id_);
|
||||||
|
|
||||||
ice_transmission_list_[remote_user_id]->SetRemoteSdp(remote_sdp);
|
ice_transmission_list_[remote_user_id]->SetRemoteSdp(remote_sdp);
|
||||||
|
|
||||||
ice_transmission_list_[remote_user_id]->GatherCandidates();
|
ice_transmission_list_[remote_user_id]->GatherCandidates();
|
||||||
|
|
||||||
on_connection_status_(ConnectionStatus::Connecting, user_data_);
|
on_connection_status_(ConnectionStatus::Connecting, user_data_);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -139,10 +139,12 @@ class PeerConnection {
|
|||||||
bool hardware_accelerated_encode_ = false;
|
bool hardware_accelerated_encode_ = false;
|
||||||
bool hardware_accelerated_decode_ = false;
|
bool hardware_accelerated_decode_ = false;
|
||||||
bool b_force_i_frame_ = false;
|
bool b_force_i_frame_ = false;
|
||||||
|
bool video_codec_inited_ = false;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<AudioEncoder> audio_encoder_ = nullptr;
|
std::unique_ptr<AudioEncoder> audio_encoder_ = nullptr;
|
||||||
std::unique_ptr<AudioDecoder> audio_decoder_ = nullptr;
|
std::unique_ptr<AudioDecoder> audio_decoder_ = nullptr;
|
||||||
|
bool audio_codec_inited_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -70,14 +70,15 @@ int CreateConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
|||||||
|
|
||||||
int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
int JoinConnection(PeerPtr *peer_ptr, const char *transmission_id,
|
||||||
const char *password) {
|
const char *password) {
|
||||||
|
int ret = -1;
|
||||||
if (!peer_ptr) {
|
if (!peer_ptr) {
|
||||||
LOG_ERROR("peer_ptr not created");
|
LOG_ERROR("peer_ptr not created");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer_ptr->peer_connection->Join(transmission_id, password);
|
ret = peer_ptr->peer_connection->Join(transmission_id, password);
|
||||||
LOG_INFO("JoinConnection [{}] with password [{}]", transmission_id, password);
|
LOG_INFO("JoinConnection [{}] with password [{}]", transmission_id, password);
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int LeaveConnection(PeerPtr *peer_ptr) {
|
int LeaveConnection(PeerPtr *peer_ptr) {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
class RtpAudioSender : public ThreadBase {
|
class RtpAudioSender : public ThreadBase {
|
||||||
public:
|
public:
|
||||||
RtpAudioSender();
|
RtpAudioSender();
|
||||||
~RtpAudioSender();
|
virtual ~RtpAudioSender();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Enqueue(std::vector<RtpPacket> &rtp_packets);
|
void Enqueue(std::vector<RtpPacket> &rtp_packets);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
class RtpDataSender : public ThreadBase {
|
class RtpDataSender : public ThreadBase {
|
||||||
public:
|
public:
|
||||||
RtpDataSender();
|
RtpDataSender();
|
||||||
~RtpDataSender();
|
virtual ~RtpDataSender();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Enqueue(std::vector<RtpPacket> &rtp_packets);
|
void Enqueue(std::vector<RtpPacket> &rtp_packets);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
class RtpStatistics : public ThreadBase {
|
class RtpStatistics : public ThreadBase {
|
||||||
public:
|
public:
|
||||||
RtpStatistics();
|
RtpStatistics();
|
||||||
~RtpStatistics();
|
virtual ~RtpStatistics();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void UpdateSentBytes(uint32_t sent_bytes);
|
void UpdateSentBytes(uint32_t sent_bytes);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
class RtpVideoReceiver : public ThreadBase {
|
class RtpVideoReceiver : public ThreadBase {
|
||||||
public:
|
public:
|
||||||
RtpVideoReceiver();
|
RtpVideoReceiver();
|
||||||
~RtpVideoReceiver();
|
virtual ~RtpVideoReceiver();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void InsertRtpPacket(RtpPacket& rtp_packet);
|
void InsertRtpPacket(RtpPacket& rtp_packet);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
class RtpVideoSender : public ThreadBase {
|
class RtpVideoSender : public ThreadBase {
|
||||||
public:
|
public:
|
||||||
RtpVideoSender();
|
RtpVideoSender();
|
||||||
~RtpVideoSender();
|
virtual ~RtpVideoSender();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Enqueue(std::vector<RtpPacket> &rtp_packets);
|
void Enqueue(std::vector<RtpPacket> &rtp_packets);
|
||||||
|
|||||||
@@ -4,21 +4,22 @@
|
|||||||
|
|
||||||
ThreadBase::ThreadBase() {}
|
ThreadBase::ThreadBase() {}
|
||||||
|
|
||||||
ThreadBase::~ThreadBase() { Stop(); }
|
ThreadBase::~ThreadBase() {
|
||||||
|
if (!stop_) {
|
||||||
|
Stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ThreadBase::Start() {
|
void ThreadBase::Start() {
|
||||||
if (!thread_) {
|
std::thread t(&ThreadBase::Run, this);
|
||||||
thread_ = std::make_unique<std::thread>(&ThreadBase::Run, this);
|
thread_ = std::move(t);
|
||||||
}
|
|
||||||
|
|
||||||
stop_ = false;
|
stop_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadBase::Stop() {
|
void ThreadBase::Stop() {
|
||||||
stop_ = true;
|
if (thread_.joinable()) {
|
||||||
|
stop_ = true;
|
||||||
if (thread_ && thread_->joinable()) {
|
thread_.join();
|
||||||
thread_->join();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
class ThreadBase {
|
class ThreadBase {
|
||||||
public:
|
public:
|
||||||
ThreadBase();
|
ThreadBase();
|
||||||
~ThreadBase();
|
virtual ~ThreadBase();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Start();
|
void Start();
|
||||||
@@ -22,7 +22,7 @@ class ThreadBase {
|
|||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<std::thread> thread_ = nullptr;
|
std::thread thread_;
|
||||||
|
|
||||||
std::atomic<bool> stop_{false};
|
std::atomic<bool> stop_{false};
|
||||||
std::atomic<bool> pause_{false};
|
std::atomic<bool> pause_{false};
|
||||||
|
|||||||
@@ -35,11 +35,6 @@ IceTransmission::~IceTransmission() {
|
|||||||
if (rtp_data_sender_) {
|
if (rtp_data_sender_) {
|
||||||
rtp_data_sender_->Stop();
|
rtp_data_sender_->Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rtp_payload_) {
|
|
||||||
delete rtp_payload_;
|
|
||||||
rtp_payload_ = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int IceTransmission::InitIceTransmission(
|
int IceTransmission::InitIceTransmission(
|
||||||
|
|||||||
@@ -133,7 +133,6 @@ class IceTransmission {
|
|||||||
std::unique_ptr<RtpAudioSender> rtp_audio_sender_ = nullptr;
|
std::unique_ptr<RtpAudioSender> rtp_audio_sender_ = nullptr;
|
||||||
std::unique_ptr<RtpDataReceiver> rtp_data_receiver_ = nullptr;
|
std::unique_ptr<RtpDataReceiver> rtp_data_receiver_ = nullptr;
|
||||||
std::unique_ptr<RtpDataSender> rtp_data_sender_ = nullptr;
|
std::unique_ptr<RtpDataSender> rtp_data_sender_ = nullptr;
|
||||||
uint8_t *rtp_payload_ = nullptr;
|
|
||||||
RtpPacket pop_packet_;
|
RtpPacket pop_packet_;
|
||||||
bool start_send_packet_ = false;
|
bool start_send_packet_ = false;
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ WsCore::WsCore() {
|
|||||||
m_endpoint_.init_asio();
|
m_endpoint_.init_asio();
|
||||||
m_endpoint_.start_perpetual();
|
m_endpoint_.start_perpetual();
|
||||||
|
|
||||||
m_thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(
|
std::thread t(&client::run, &m_endpoint_);
|
||||||
&client::run, &m_endpoint_);
|
m_thread_ = std::move(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
WsCore::~WsCore() {
|
WsCore::~WsCore() {
|
||||||
@@ -32,12 +32,12 @@ WsCore::~WsCore() {
|
|||||||
LOG_INFO("Closing connection error: {}", ec.message());
|
LOG_INFO("Closing connection error: {}", ec.message());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_thread_->joinable()) {
|
if (m_thread_.joinable()) {
|
||||||
m_thread_->join();
|
m_thread_.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ping_thread_->joinable()) {
|
if (ping_thread_.joinable()) {
|
||||||
ping_thread_->join();
|
ping_thread_.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,8 +129,8 @@ void WsCore::OnOpen(client *c, websocketpp::connection_hdl hdl) {
|
|||||||
ws_status_ = WsStatus::WsOpened;
|
ws_status_ = WsStatus::WsOpened;
|
||||||
OnWsStatus(WsStatus::WsOpened);
|
OnWsStatus(WsStatus::WsOpened);
|
||||||
|
|
||||||
ping_thread_ = websocketpp::lib::make_shared<websocketpp::lib::thread>(
|
std::thread t(&WsCore::Ping, this, hdl);
|
||||||
&WsCore::Ping, this, hdl);
|
ping_thread_ = std::move(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) {
|
void WsCore::OnFail(client *c, websocketpp::connection_hdl hdl) {
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ class WsCore {
|
|||||||
private:
|
private:
|
||||||
client m_endpoint_;
|
client m_endpoint_;
|
||||||
websocketpp::connection_hdl connection_handle_;
|
websocketpp::connection_hdl connection_handle_;
|
||||||
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread_;
|
std::thread m_thread_;
|
||||||
websocketpp::lib::shared_ptr<websocketpp::lib::thread> ping_thread_;
|
std::thread ping_thread_;
|
||||||
|
|
||||||
WsStatus ws_status_ = WsStatus::WsClosed;
|
WsStatus ws_status_ = WsStatus::WsClosed;
|
||||||
int timeout_count_ = 0;
|
int timeout_count_ = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user