Start thread after created when use ThreadBase

This commit is contained in:
dijunkun
2023-09-11 15:15:59 +08:00
parent 0899fe2f1d
commit f52142fc00
7 changed files with 21 additions and 57 deletions

View File

@@ -88,22 +88,7 @@ bool RtpVideoReceiver::CheckIsFrameCompleted(RtpPacket& rtp_packet) {
return false; return false;
} }
void RtpVideoReceiver::Start() {
std::lock_guard<std::mutex> lock_guard(mutex_);
stop_ = false;
}
void RtpVideoReceiver::Stop() {
std::lock_guard<std::mutex> lock_guard(mutex_);
stop_ = true;
}
bool RtpVideoReceiver::Process() { bool RtpVideoReceiver::Process() {
std::lock_guard<std::mutex> lock_guard(mutex_);
if (stop_) {
return false;
}
if (!compelete_video_frame_queue_.isEmpty()) { if (!compelete_video_frame_queue_.isEmpty()) {
VideoFrame video_frame; VideoFrame video_frame;
compelete_video_frame_queue_.pop(video_frame); compelete_video_frame_queue_.pop(video_frame);

View File

@@ -3,7 +3,6 @@
#include <functional> #include <functional>
#include <map> #include <map>
#include <mutex>
#include <queue> #include <queue>
#include "frame.h" #include "frame.h"
@@ -24,9 +23,6 @@ class RtpVideoReceiver : public ThreadBase {
on_receive_complete_frame_ = on_receive_complete_frame; on_receive_complete_frame_ = on_receive_complete_frame;
} }
void Start();
void Stop();
private: private:
bool CheckIsFrameCompleted(RtpPacket& rtp_packet); bool CheckIsFrameCompleted(RtpPacket& rtp_packet);
// void OnReceiveFrame(uint8_t* payload) {} // void OnReceiveFrame(uint8_t* payload) {}
@@ -41,9 +37,6 @@ class RtpVideoReceiver : public ThreadBase {
uint32_t last_complete_frame_ts_ = 0; uint32_t last_complete_frame_ts_ = 0;
RingBuffer<VideoFrame> compelete_video_frame_queue_; RingBuffer<VideoFrame> compelete_video_frame_queue_;
bool stop_ = true;
std::mutex mutex_;
}; };
#endif #endif

View File

@@ -14,22 +14,7 @@ void RtpVideoSender::Enqueue(std::vector<RtpPacket>& rtp_packets) {
} }
} }
void RtpVideoSender::Start() {
std::lock_guard<std::mutex> lock_guard(mutex_);
stop_ = false;
}
void RtpVideoSender::Stop() {
std::lock_guard<std::mutex> lock_guard(mutex_);
stop_ = true;
}
bool RtpVideoSender::Process() { bool RtpVideoSender::Process() {
std::lock_guard<std::mutex> lock_guard(mutex_);
if (stop_) {
return false;
}
for (size_t i = 0; i < 50; i++) for (size_t i = 0; i < 50; i++)
if (!rtp_packe_queue_.isEmpty()) { if (!rtp_packe_queue_.isEmpty()) {
RtpPacket rtp_packet; RtpPacket rtp_packet;

View File

@@ -2,8 +2,6 @@
#define _RTP_VIDEO_SENDER_H_ #define _RTP_VIDEO_SENDER_H_
#include <functional> #include <functional>
#include <mutex>
#include <thread>
#include "ringbuffer.h" #include "ringbuffer.h"
#include "rtp_packet.h" #include "rtp_packet.h"
@@ -23,18 +21,12 @@ class RtpVideoSender : public ThreadBase {
rtp_packet_send_func_ = rtp_packet_send_func; rtp_packet_send_func_ = rtp_packet_send_func;
} }
void Start();
void Stop();
private: private:
bool Process() override; bool Process() override;
private: private:
std::function<void(RtpPacket &)> rtp_packet_send_func_ = nullptr; std::function<void(RtpPacket &)> rtp_packet_send_func_ = nullptr;
RingBuffer<RtpPacket> rtp_packe_queue_; RingBuffer<RtpPacket> rtp_packe_queue_;
bool stop_ = true;
std::mutex mutex_;
}; };
#endif #endif

View File

@@ -6,19 +6,27 @@ ThreadBase::ThreadBase() {}
ThreadBase::~ThreadBase() {} ThreadBase::~ThreadBase() {}
void ThreadBase::StartThread() { void ThreadBase::Start() {
if (!thread_) { if (!thread_) {
thread_ = std::make_unique<std::thread>(&ThreadBase::Run, this); thread_ = std::make_unique<std::thread>(&ThreadBase::Run, this);
} }
stop_ = false;
} }
void ThreadBase::StopThread() { void ThreadBase::Stop() {
stop_ = true;
if (thread_ && thread_->joinable()) { if (thread_ && thread_->joinable()) {
thread_->join(); thread_->join();
} }
} }
void ThreadBase::Pause() { pause_ = true; }
void ThreadBase::Resume() { pause_ = false; }
void ThreadBase::Run() { void ThreadBase::Run() {
while (Process()) { while (!stop_ && Process()) {
} }
} }

View File

@@ -1,7 +1,7 @@
#ifndef _THREAD_BASE_H_ #ifndef _THREAD_BASE_H_
#define _THREAD_BASE_H_ #define _THREAD_BASE_H_
#include <mutex> #include <atomic>
#include <thread> #include <thread>
class ThreadBase { class ThreadBase {
@@ -10,8 +10,12 @@ class ThreadBase {
~ThreadBase(); ~ThreadBase();
public: public:
void StartThread(); void Start();
void StopThread(); void Stop();
void Pause();
void Resume();
virtual bool Process() = 0; virtual bool Process() = 0;
private: private:
@@ -19,8 +23,9 @@ class ThreadBase {
private: private:
std::unique_ptr<std::thread> thread_ = nullptr; std::unique_ptr<std::thread> thread_ = nullptr;
bool start_ = false;
std::mutex mutex_; std::atomic<bool> stop_ = false;
std::atomic<bool> pause_ = false;
}; };
#endif #endif

View File

@@ -34,12 +34,10 @@ IceTransmission::IceTransmission(
IceTransmission::~IceTransmission() { IceTransmission::~IceTransmission() {
if (rtp_video_sender_) { if (rtp_video_sender_) {
rtp_video_sender_->Stop(); rtp_video_sender_->Stop();
rtp_video_sender_->StopThread();
} }
if (rtp_video_receiver_) { if (rtp_video_receiver_) {
rtp_video_receiver_->Stop(); rtp_video_receiver_->Stop();
rtp_video_receiver_->StopThread();
} }
if (rtp_payload_) { if (rtp_payload_) {
@@ -59,7 +57,6 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
remote_user_id_.size()); remote_user_id_.size());
}); });
rtp_video_receiver_->StartThread();
rtp_video_receiver_->Start(); rtp_video_receiver_->Start();
rtp_video_sender_ = std::make_unique<RtpVideoSender>(); rtp_video_sender_ = std::make_unique<RtpVideoSender>();
@@ -70,7 +67,6 @@ int IceTransmission::InitIceTransmission(std::string &ip, int port) {
} }
}); });
rtp_video_sender_->StartThread();
rtp_video_sender_->Start(); rtp_video_sender_->Start();
ice_agent_ = std::make_unique<IceAgent>(ip, port); ice_agent_ = std::make_unique<IceAgent>(ip, port);