From f52142fc00686b5ffc802ab2718795c07db1c42e Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 11 Sep 2023 15:15:59 +0800 Subject: [PATCH] Start thread after created when use ThreadBase --- src/rtp/rtp_video_receiver.cpp | 15 --------------- src/rtp/rtp_video_receiver.h | 7 ------- src/rtp/rtp_video_sender.cpp | 15 --------------- src/rtp/rtp_video_sender.h | 8 -------- src/thread/thread_base.cpp | 14 +++++++++++--- src/thread/thread_base.h | 15 ++++++++++----- src/transmission/ice_transmission.cpp | 4 ---- 7 files changed, 21 insertions(+), 57 deletions(-) diff --git a/src/rtp/rtp_video_receiver.cpp b/src/rtp/rtp_video_receiver.cpp index d9927e3..0f74ad3 100644 --- a/src/rtp/rtp_video_receiver.cpp +++ b/src/rtp/rtp_video_receiver.cpp @@ -88,22 +88,7 @@ bool RtpVideoReceiver::CheckIsFrameCompleted(RtpPacket& rtp_packet) { return false; } -void RtpVideoReceiver::Start() { - std::lock_guard lock_guard(mutex_); - stop_ = false; -} - -void RtpVideoReceiver::Stop() { - std::lock_guard lock_guard(mutex_); - stop_ = true; -} - bool RtpVideoReceiver::Process() { - std::lock_guard lock_guard(mutex_); - if (stop_) { - return false; - } - if (!compelete_video_frame_queue_.isEmpty()) { VideoFrame video_frame; compelete_video_frame_queue_.pop(video_frame); diff --git a/src/rtp/rtp_video_receiver.h b/src/rtp/rtp_video_receiver.h index 6a70195..83bf7c7 100644 --- a/src/rtp/rtp_video_receiver.h +++ b/src/rtp/rtp_video_receiver.h @@ -3,7 +3,6 @@ #include #include -#include #include #include "frame.h" @@ -24,9 +23,6 @@ class RtpVideoReceiver : public ThreadBase { on_receive_complete_frame_ = on_receive_complete_frame; } - void Start(); - void Stop(); - private: bool CheckIsFrameCompleted(RtpPacket& rtp_packet); // void OnReceiveFrame(uint8_t* payload) {} @@ -41,9 +37,6 @@ class RtpVideoReceiver : public ThreadBase { uint32_t last_complete_frame_ts_ = 0; RingBuffer compelete_video_frame_queue_; - - bool stop_ = true; - std::mutex mutex_; }; #endif diff --git a/src/rtp/rtp_video_sender.cpp b/src/rtp/rtp_video_sender.cpp index e01a2de..ebffcee 100644 --- a/src/rtp/rtp_video_sender.cpp +++ b/src/rtp/rtp_video_sender.cpp @@ -14,22 +14,7 @@ void RtpVideoSender::Enqueue(std::vector& rtp_packets) { } } -void RtpVideoSender::Start() { - std::lock_guard lock_guard(mutex_); - stop_ = false; -} - -void RtpVideoSender::Stop() { - std::lock_guard lock_guard(mutex_); - stop_ = true; -} - bool RtpVideoSender::Process() { - std::lock_guard lock_guard(mutex_); - if (stop_) { - return false; - } - for (size_t i = 0; i < 50; i++) if (!rtp_packe_queue_.isEmpty()) { RtpPacket rtp_packet; diff --git a/src/rtp/rtp_video_sender.h b/src/rtp/rtp_video_sender.h index 0bfe45c..ffe347f 100644 --- a/src/rtp/rtp_video_sender.h +++ b/src/rtp/rtp_video_sender.h @@ -2,8 +2,6 @@ #define _RTP_VIDEO_SENDER_H_ #include -#include -#include #include "ringbuffer.h" #include "rtp_packet.h" @@ -23,18 +21,12 @@ class RtpVideoSender : public ThreadBase { rtp_packet_send_func_ = rtp_packet_send_func; } - void Start(); - void Stop(); - private: bool Process() override; private: std::function rtp_packet_send_func_ = nullptr; RingBuffer rtp_packe_queue_; - - bool stop_ = true; - std::mutex mutex_; }; #endif \ No newline at end of file diff --git a/src/thread/thread_base.cpp b/src/thread/thread_base.cpp index fce73a0..b304d59 100644 --- a/src/thread/thread_base.cpp +++ b/src/thread/thread_base.cpp @@ -6,19 +6,27 @@ ThreadBase::ThreadBase() {} ThreadBase::~ThreadBase() {} -void ThreadBase::StartThread() { +void ThreadBase::Start() { if (!thread_) { thread_ = std::make_unique(&ThreadBase::Run, this); } + + stop_ = false; } -void ThreadBase::StopThread() { +void ThreadBase::Stop() { + stop_ = true; + if (thread_ && thread_->joinable()) { thread_->join(); } } +void ThreadBase::Pause() { pause_ = true; } + +void ThreadBase::Resume() { pause_ = false; } + void ThreadBase::Run() { - while (Process()) { + while (!stop_ && Process()) { } } \ No newline at end of file diff --git a/src/thread/thread_base.h b/src/thread/thread_base.h index fe41ee6..c2ddd1d 100644 --- a/src/thread/thread_base.h +++ b/src/thread/thread_base.h @@ -1,7 +1,7 @@ #ifndef _THREAD_BASE_H_ #define _THREAD_BASE_H_ -#include +#include #include class ThreadBase { @@ -10,8 +10,12 @@ class ThreadBase { ~ThreadBase(); public: - void StartThread(); - void StopThread(); + void Start(); + void Stop(); + + void Pause(); + void Resume(); + virtual bool Process() = 0; private: @@ -19,8 +23,9 @@ class ThreadBase { private: std::unique_ptr thread_ = nullptr; - bool start_ = false; - std::mutex mutex_; + + std::atomic stop_ = false; + std::atomic pause_ = false; }; #endif \ No newline at end of file diff --git a/src/transmission/ice_transmission.cpp b/src/transmission/ice_transmission.cpp index 9347d09..e6f0b56 100644 --- a/src/transmission/ice_transmission.cpp +++ b/src/transmission/ice_transmission.cpp @@ -34,12 +34,10 @@ IceTransmission::IceTransmission( IceTransmission::~IceTransmission() { if (rtp_video_sender_) { rtp_video_sender_->Stop(); - rtp_video_sender_->StopThread(); } if (rtp_video_receiver_) { rtp_video_receiver_->Stop(); - rtp_video_receiver_->StopThread(); } if (rtp_payload_) { @@ -59,7 +57,6 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) { remote_user_id_.size()); }); - rtp_video_receiver_->StartThread(); rtp_video_receiver_->Start(); rtp_video_sender_ = std::make_unique(); @@ -70,7 +67,6 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) { } }); - rtp_video_sender_->StartThread(); rtp_video_sender_->Start(); ice_agent_ = std::make_unique(ip, port);