diff --git a/src/pc/peer_connection.cpp b/src/pc/peer_connection.cpp index a265122..90f979f 100644 --- a/src/pc/peer_connection.cpp +++ b/src/pc/peer_connection.cpp @@ -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(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; diff --git a/src/pc/peer_connection.h b/src/pc/peer_connection.h index e2a3d7c..3979c9c 100644 --- a/src/pc/peer_connection.h +++ b/src/pc/peer_connection.h @@ -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 audio_encoder_ = nullptr; std::unique_ptr audio_decoder_ = nullptr; + bool audio_codec_inited_ = false; }; #endif \ No newline at end of file diff --git a/src/rtc/x_inner.cpp b/src/rtc/x_inner.cpp index 1b75c73..6b38abc 100644 --- a/src/rtc/x_inner.cpp +++ b/src/rtc/x_inner.cpp @@ -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) { diff --git a/src/rtp/rtp_audio_sender.h b/src/rtp/rtp_audio_sender.h index 8a7a841..3f58868 100644 --- a/src/rtp/rtp_audio_sender.h +++ b/src/rtp/rtp_audio_sender.h @@ -18,7 +18,7 @@ class RtpAudioSender : public ThreadBase { public: RtpAudioSender(); - ~RtpAudioSender(); + virtual ~RtpAudioSender(); public: void Enqueue(std::vector &rtp_packets); diff --git a/src/rtp/rtp_data_sender.h b/src/rtp/rtp_data_sender.h index 53cc0b8..7b8dc39 100644 --- a/src/rtp/rtp_data_sender.h +++ b/src/rtp/rtp_data_sender.h @@ -18,7 +18,7 @@ class RtpDataSender : public ThreadBase { public: RtpDataSender(); - ~RtpDataSender(); + virtual ~RtpDataSender(); public: void Enqueue(std::vector &rtp_packets); diff --git a/src/rtp/rtp_statistics.h b/src/rtp/rtp_statistics.h index b44ffad..05dd41c 100644 --- a/src/rtp/rtp_statistics.h +++ b/src/rtp/rtp_statistics.h @@ -6,7 +6,7 @@ class RtpStatistics : public ThreadBase { public: RtpStatistics(); - ~RtpStatistics(); + virtual ~RtpStatistics(); public: void UpdateSentBytes(uint32_t sent_bytes); diff --git a/src/rtp/rtp_video_receiver.h b/src/rtp/rtp_video_receiver.h index 5e0b34a..009c998 100644 --- a/src/rtp/rtp_video_receiver.h +++ b/src/rtp/rtp_video_receiver.h @@ -17,7 +17,7 @@ class RtpVideoReceiver : public ThreadBase { public: RtpVideoReceiver(); - ~RtpVideoReceiver(); + virtual ~RtpVideoReceiver(); public: void InsertRtpPacket(RtpPacket& rtp_packet); diff --git a/src/rtp/rtp_video_sender.h b/src/rtp/rtp_video_sender.h index ca9cdcb..dd24c3e 100644 --- a/src/rtp/rtp_video_sender.h +++ b/src/rtp/rtp_video_sender.h @@ -12,7 +12,7 @@ class RtpVideoSender : public ThreadBase { public: RtpVideoSender(); - ~RtpVideoSender(); + virtual ~RtpVideoSender(); public: void Enqueue(std::vector &rtp_packets); diff --git a/src/thread/thread_base.cpp b/src/thread/thread_base.cpp index 47c8cc9..f397c2c 100644 --- a/src/thread/thread_base.cpp +++ b/src/thread/thread_base.cpp @@ -4,21 +4,22 @@ ThreadBase::ThreadBase() {} -ThreadBase::~ThreadBase() { Stop(); } +ThreadBase::~ThreadBase() { + if (!stop_) { + Stop(); + } +} void ThreadBase::Start() { - if (!thread_) { - thread_ = std::make_unique(&ThreadBase::Run, this); - } - + std::thread t(&ThreadBase::Run, this); + thread_ = std::move(t); stop_ = false; } void ThreadBase::Stop() { - stop_ = true; - - if (thread_ && thread_->joinable()) { - thread_->join(); + if (thread_.joinable()) { + stop_ = true; + thread_.join(); } } diff --git a/src/thread/thread_base.h b/src/thread/thread_base.h index bd6533f..4f66002 100644 --- a/src/thread/thread_base.h +++ b/src/thread/thread_base.h @@ -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 thread_ = nullptr; + std::thread thread_; std::atomic stop_{false}; std::atomic pause_{false}; diff --git a/src/transmission/ice_transmission.cpp b/src/transmission/ice_transmission.cpp index bb98630..c2933f6 100644 --- a/src/transmission/ice_transmission.cpp +++ b/src/transmission/ice_transmission.cpp @@ -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( diff --git a/src/transmission/ice_transmission.h b/src/transmission/ice_transmission.h index 1beac6a..126dbed 100644 --- a/src/transmission/ice_transmission.h +++ b/src/transmission/ice_transmission.h @@ -133,7 +133,6 @@ class IceTransmission { std::unique_ptr rtp_audio_sender_ = nullptr; std::unique_ptr rtp_data_receiver_ = nullptr; std::unique_ptr rtp_data_sender_ = nullptr; - uint8_t *rtp_payload_ = nullptr; RtpPacket pop_packet_; bool start_send_packet_ = false; diff --git a/src/ws/ws_core.cpp b/src/ws/ws_core.cpp index 5fb5594..7bce983 100644 --- a/src/ws/ws_core.cpp +++ b/src/ws/ws_core.cpp @@ -13,8 +13,8 @@ WsCore::WsCore() { m_endpoint_.init_asio(); m_endpoint_.start_perpetual(); - m_thread_ = websocketpp::lib::make_shared( - &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( - &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) { diff --git a/src/ws/ws_core.h b/src/ws/ws_core.h index 0d08ad8..8a31b0d 100644 --- a/src/ws/ws_core.h +++ b/src/ws/ws_core.h @@ -53,8 +53,8 @@ class WsCore { private: client m_endpoint_; websocketpp::connection_hdl connection_handle_; - websocketpp::lib::shared_ptr m_thread_; - websocketpp::lib::shared_ptr ping_thread_; + std::thread m_thread_; + std::thread ping_thread_; WsStatus ws_status_ = WsStatus::WsClosed; int timeout_count_ = 0;