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