Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Joy1024 committed Feb 6, 2025
1 parent a765487 commit 6f06188
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
21 changes: 10 additions & 11 deletions src/lib/video/cameradevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#include <QDesktopWidget>
#include <QScreen>
#include <format>
#include <semaphore>
#include <thread>


extern "C" {
#include <libavdevice/avdevice.h>
Expand Down Expand Up @@ -49,8 +50,6 @@ extern "C" {
namespace lib::video {


std::binary_semaphore sem(1);

/**
* @class CameraDevice
*
Expand Down Expand Up @@ -78,15 +77,15 @@ CameraDevice::~CameraDevice()

void CameraDevice::stop()
{
qDebug() << __func__;
// QMutexLocker locker{&openDeviceLock};
// sem.acquire();
run = false;
// sem.release();
qDebug() << __func__ << "is set" ;
}

bool CameraDevice::isOpened()
{
QMutexLocker locker{&openDeviceLock};
// QMutexLocker locker{&openDeviceLock};
return context;
}

Expand All @@ -96,19 +95,20 @@ void CameraDevice::stream() {
run = true;

forever {
QMutexLocker locker{&openDeviceLock};

// Exit if context is no longer valid
if(!run){
// qWarning() << __func__ << "was Stoped!";
break;
}

if(!isOpened()){
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return;
}

// sem.acquire();
readFrame();
// sem.release();
}

qDebug() << __func__ << "was Finished!";
Expand All @@ -120,10 +120,9 @@ void CameraDevice::stream() {

void CameraDevice::readFrame()
{
QMutexLocker locker{&openDeviceLock};

if (!context) {
// qWarning() << __func__ << "Exited.";
std::this_thread::sleep_for(std::chrono::milliseconds(100));
return;
}

Expand All @@ -132,7 +131,7 @@ void CameraDevice::readFrame()
return;
}

// Forward packets to the decoder and grab the decoded frame
// Forward packets to the decoder and grab the decoded frame
bool isVideo = packet.stream_index == videoStreamIndex;
bool readyToRecive = isVideo && !avcodec_send_packet(cctx, &packet);

Expand Down
8 changes: 3 additions & 5 deletions src/lib/video/camerasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CameraSource::CameraSource(const VideoDevice &dev)


CameraSource::~CameraSource() {

qDebug() << __func__;
closeDevice();
delete device;
qDebug() << __func__ << "Destroyed.";
Expand Down Expand Up @@ -169,16 +169,14 @@ void CameraSource::openDevice() {
* @note
*/
void CameraSource::closeDevice() {
qDebug() << "Closing device" << dev.name;
QMutexLocker locker{&mutex};

if(!device->isOpened()){
qWarning() << "Was closed!";
return;
}

QMutexLocker locker{&mutex};
qDebug() << "Closing device" << dev.name;


//stop device
if (device) {
device->stop();
Expand Down

0 comments on commit 6f06188

Please sign in to comment.