Skip to content

Commit 718b291

Browse files
committed
Improve stability of worker thread
1 parent 73ff770 commit 718b291

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

v4l2capture.cpp

+19-17
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ class Device_manager_Worker_thread_args
992992

993993
std::vector<unsigned char *> decodedFrameBuff;
994994
std::vector<unsigned> decodedFrameLenBuff;
995-
int decodedFrameBuffMaxSize;
995+
unsigned decodedFrameBuffMaxSize;
996996

997997
Device_manager_Worker_thread_args(const char *devNameIn)
998998
{
@@ -1008,7 +1008,7 @@ class Device_manager_Worker_thread_args
10081008
frameWidth = 0;
10091009
frameHeight = 0;
10101010
decodedFrameBuffMaxSize = 10;
1011-
verbose = 0;
1011+
verbose = 1;
10121012
}
10131013

10141014
virtual ~Device_manager_Worker_thread_args()
@@ -1374,11 +1374,14 @@ class Device_manager_Worker_thread_args
13741374
if(this->deviceStarted)
13751375
StopDeviceInternal();
13761376

1377-
for(int i = 0; i < this->buffer_counts; i++)
1377+
if(this->buffers!= NULL)
13781378
{
1379-
v4l2_munmap(buffers[i].start, buffers[i].length);
1379+
for(int i = 0; i < this->buffer_counts; i++)
1380+
{
1381+
v4l2_munmap(this->buffers[i].start, this->buffers[i].length);
1382+
}
1383+
delete [] this->buffers;
13801384
}
1381-
delete [] this->buffers;
13821385
this->buffers = NULL;
13831386

13841387
//Release memory
@@ -1406,25 +1409,24 @@ class Device_manager_Worker_thread_args
14061409
if(deviceStarted) this->ReadFrame();
14071410

14081411
pthread_mutex_lock(&this->lock);
1412+
try
1413+
{
1414+
14091415
if(this->openDeviceFlag.size() > 0)
14101416
{
14111417
std::string devName = this->openDeviceFlag[this->openDeviceFlag.size()-1];
14121418
this->openDeviceFlag.pop_back();
14131419
this->OpenDeviceInternal();
14141420
}
1415-
pthread_mutex_unlock(&this->lock);
1416-
1417-
pthread_mutex_lock(&this->lock);
1421+
14181422
if(this->setFormatFlags.size() > 0
14191423
&& this->openDeviceFlag.size() == 0)
14201424
{
14211425
class SetFormatParams params = this->setFormatFlags[this->setFormatFlags.size()-1];
14221426
this->setFormatFlags.pop_back();
14231427
this->SetFormatInternal(params);
14241428
}
1425-
pthread_mutex_unlock(&this->lock);
14261429

1427-
pthread_mutex_lock(&this->lock);
14281430
if(this->startDeviceFlag.size() > 0
14291431
&& this->openDeviceFlag.size() == 0
14301432
&& this->setFormatFlags.size() == 0)
@@ -1433,9 +1435,7 @@ class Device_manager_Worker_thread_args
14331435
this->startDeviceFlag.pop_back();
14341436
this->StartDeviceInternal(buffer_count);
14351437
}
1436-
pthread_mutex_unlock(&this->lock);
14371438

1438-
pthread_mutex_lock(&this->lock);
14391439
if(this->stopDeviceFlag
14401440
&& this->openDeviceFlag.size() == 0
14411441
&& this->setFormatFlags.size() == 0
@@ -1444,9 +1444,7 @@ class Device_manager_Worker_thread_args
14441444
this->StopDeviceInternal();
14451445
this->stopDeviceFlag = 0;
14461446
}
1447-
pthread_mutex_unlock(&this->lock);
14481447

1449-
pthread_mutex_lock(&this->lock);
14501448
if(this->closeDeviceFlag
14511449
&& this->openDeviceFlag.size() == 0
14521450
&& this->setFormatFlags.size() == 0
@@ -1456,10 +1454,14 @@ class Device_manager_Worker_thread_args
14561454
this->CloseDeviceInternal();
14571455
this->closeDeviceFlag = 0;
14581456
}
1459-
pthread_mutex_unlock(&this->lock);
1460-
1461-
pthread_mutex_lock(&this->lock);
1457+
14621458
running = !this->stop;
1459+
}
1460+
catch(std::exception &err)
1461+
{
1462+
if(verbose) printf("An exception has occured: %s\n", err.what());
1463+
running = 0;
1464+
}
14631465
pthread_mutex_unlock(&this->lock);
14641466
}
14651467
}

0 commit comments

Comments
 (0)