Commit 2492954f authored by Thiago Santini's avatar Thiago Santini

Adds mutex protection for surface access

parent 72b37013
...@@ -27,12 +27,13 @@ void UVCCameraSession::setDevice(uvc_device_t *device) ...@@ -27,12 +27,13 @@ void UVCCameraSession::setDevice(uvc_device_t *device)
void UVCCameraSession::setSurface(QAbstractVideoSurface* surface) void UVCCameraSession::setSurface(QAbstractVideoSurface* surface)
{ {
QMutexLocker locker(&surfaceMutex);
m_surface = surface; m_surface = surface;
} }
// If the following is defined, frame grabbing is performed by the callback thread // If the following is defined, frame grabbing is performed by the callback thread
//#define BLOCKING_FRAME_GRABBING //#define BLOCKING_FRAME_GRABBING
void cb(uvc_frame_t *frame, void *ptr) { static_cast<UVCCameraSession*>(ptr)->callback(frame); } void cb(uvc_frame_t *frame, void *ptr) { if (ptr && frame) static_cast<UVCCameraSession*>(ptr)->callback(frame); }
void UVCCameraSession::callback(uvc_frame_t *frame) void UVCCameraSession::callback(uvc_frame_t *frame)
{ {
qreal t = frameReference.elapsed(); qreal t = frameReference.elapsed();
...@@ -40,6 +41,7 @@ void UVCCameraSession::callback(uvc_frame_t *frame) ...@@ -40,6 +41,7 @@ void UVCCameraSession::callback(uvc_frame_t *frame)
if (!streaming) if (!streaming)
return; return;
QMutexLocker locker(&surfaceMutex);
if (m_surface) { if (m_surface) {
QVideoFrame qFrame; QVideoFrame qFrame;
switch(frame->frame_format) { switch(frame->frame_format) {
...@@ -55,7 +57,7 @@ void UVCCameraSession::callback(uvc_frame_t *frame) ...@@ -55,7 +57,7 @@ void UVCCameraSession::callback(uvc_frame_t *frame)
#ifdef BLOCKING_FRAME_GRABBING #ifdef BLOCKING_FRAME_GRABBING
m_surface->present( qFrame ); m_surface->present( qFrame );
#else #else
QMetaObject::invokeMethod(this, "presentFrame", Qt::QueuedConnection, Q_ARG(QVideoFrame, qFrame), Q_ARG(const qreal, t) ); QMetaObject::invokeMethod(this, "presentFrame", Qt::QueuedConnection, Q_ARG(const QVideoFrame&, qFrame), Q_ARG(const qreal, t) );
#endif #endif
} }
} }
...@@ -64,7 +66,7 @@ void UVCCameraSession::presentFrame(QVideoFrame frame, const qreal t) ...@@ -64,7 +66,7 @@ void UVCCameraSession::presentFrame(QVideoFrame frame, const qreal t)
{ {
qreal latency = frameReference.elapsed() - t; qreal latency = frameReference.elapsed() - t;
frame.setMetaData("timestamp", t); frame.setMetaData("timestamp", t);
if (latency <= MAX_LATENCY_MS) if (latency <= MAX_LATENCY_MS && m_surface)
m_surface->present(frame); m_surface->present(frame);
//else //else
// qWarning() << "Dropping frame (" << latency << "ms old )"; // qWarning() << "Dropping frame (" << latency << "ms old )";
......
...@@ -61,6 +61,7 @@ private: ...@@ -61,6 +61,7 @@ private:
float bandwidthFactor; float bandwidthFactor;
QCamera::Status m_status; QCamera::Status m_status;
QAbstractVideoSurface *m_surface; QAbstractVideoSurface *m_surface;
QMutex surfaceMutex;
// Source (camera) // Source (camera)
uvc_device_t *dev; uvc_device_t *dev;
...@@ -83,6 +84,7 @@ private: ...@@ -83,6 +84,7 @@ private:
bool isPupilFieldCamera(); bool isPupilFieldCamera();
bool isPupilEyeCamera(); bool isPupilEyeCamera();
}; };
QT_END_NAMESPACE QT_END_NAMESPACE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment