Commit c4534fb1 authored by Thiago Santini's avatar Thiago Santini

Modernizing the code style a bit

parent 76ab1f19
...@@ -14,7 +14,7 @@ QT_BEGIN_NAMESPACE ...@@ -14,7 +14,7 @@ QT_BEGIN_NAMESPACE
UVCCameraService::UVCCameraService(QObject *parent): UVCCameraService::UVCCameraService(QObject *parent):
QMediaService(parent) QMediaService(parent)
, m_videoRenderer(NULL) , m_videoRenderer(nullptr)
{ {
m_session = new UVCCameraSession(this); m_session = new UVCCameraSession(this);
m_control = new UVCCameraControl(m_session); m_control = new UVCCameraControl(m_session);
...@@ -61,14 +61,14 @@ QMediaControl* UVCCameraService::requestControl(const char *name) ...@@ -61,14 +61,14 @@ QMediaControl* UVCCameraService::requestControl(const char *name)
if (qstrcmp(name, QCameraExposureControl_iid) == 0) if (qstrcmp(name, QCameraExposureControl_iid) == 0)
return m_cameraExposureControl; return m_cameraExposureControl;
return NULL; return nullptr;
} }
void UVCCameraService::releaseControl(QMediaControl *control) void UVCCameraService::releaseControl(QMediaControl *control)
{ {
if (control == m_videoRenderer) { if (control == m_videoRenderer) {
delete m_videoRenderer; delete m_videoRenderer;
m_videoRenderer = NULL; m_videoRenderer = nullptr;
return; return;
} }
} }
......
...@@ -12,13 +12,13 @@ QMutex UVCCameraSession::devicesMutex; ...@@ -12,13 +12,13 @@ QMutex UVCCameraSession::devicesMutex;
UVCCameraSession::UVCCameraSession(QObject *parent) UVCCameraSession::UVCCameraSession(QObject *parent)
: QObject(parent), : QObject(parent),
m_surface(Q_NULLPTR),
streaming(false), streaming(false),
bandwidthFactor(1.3f),
interval(0), interval(0),
ctx(NULL), bandwidthFactor(1.3f),
dev(NULL), m_surface(Q_NULLPTR),
devh(NULL) ctx(nullptr),
dev(nullptr),
devh(nullptr)
{ {
DefaultParameters tmp; DefaultParameters tmp;
...@@ -105,10 +105,10 @@ bool UVCCameraSession::setDevice(const QString &device) ...@@ -105,10 +105,10 @@ bool UVCCameraSession::setDevice(const QString &device)
uvc_error_t res; uvc_error_t res;
if (!ctx) { if (!ctx) {
res = uvc_init(&ctx, NULL); res = uvc_init(&ctx, nullptr);
if (res < 0) { if (res < 0) {
qWarning() << "uvc_init" << uvc_strerror(res); qWarning() << "uvc_init" << uvc_strerror(res);
ctx = NULL; ctx = nullptr;
return false; return false;
} }
} }
...@@ -126,7 +126,7 @@ bool UVCCameraSession::setDevice(const QString &device) ...@@ -126,7 +126,7 @@ bool UVCCameraSession::setDevice(const QString &device)
while (1) { while (1) {
dev = devList[idx]; dev = devList[idx];
idx++; idx++;
if (dev == NULL) if (dev == nullptr)
break; break;
uvc_device_descriptor_t *desc; uvc_device_descriptor_t *desc;
res = uvc_get_device_descriptor(dev, &desc); res = uvc_get_device_descriptor(dev, &desc);
...@@ -158,8 +158,13 @@ void UVCCameraSession::setSurface(QAbstractVideoSurface* surface) ...@@ -158,8 +158,13 @@ void UVCCameraSession::setSurface(QAbstractVideoSurface* surface)
void cb(uvc_frame_t *frame, void *ptr) { if (ptr && frame) 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 = 1e-6 * frameReference.nsecsElapsed();
qreal t = 1e3*frame->capture_time.tv_sec + 1e-3*frame->capture_time.tv_usec; // use libuvc timestamp instead of our own
sw.update(t);
hw.update(1e3*frame->capture_time.tv_sec + 1e-3*frame->capture_time.tv_usec); // use libuvc timestamp instead of our own
hybrid.update(t);
hybrid.correct(hw, settings.maximumFrameRate());
if (!streaming) if (!streaming)
return; return;
...@@ -169,19 +174,19 @@ void UVCCameraSession::callback(uvc_frame_t *frame) ...@@ -169,19 +174,19 @@ void UVCCameraSession::callback(uvc_frame_t *frame)
QVideoFrame qFrame; QVideoFrame qFrame;
switch(frame->frame_format) { switch(frame->frame_format) {
case UVC_FRAME_FORMAT_MJPEG: case UVC_FRAME_FORMAT_MJPEG:
qFrame = QVideoFrame( (int) frame->data_bytes, QSize(frame->width, frame->height), 0, QVideoFrame::Format_Jpeg); qFrame = QVideoFrame( static_cast<int>(frame->data_bytes), QSize(frame->width, frame->height), 0, QVideoFrame::Format_Jpeg);
qFrame.map(QAbstractVideoBuffer::WriteOnly); qFrame.map(QAbstractVideoBuffer::WriteOnly);
memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now
qFrame.unmap(); qFrame.unmap();
break; break;
case UVC_FRAME_FORMAT_YUYV: case UVC_FRAME_FORMAT_YUYV:
qFrame = QVideoFrame( (int) frame->data_bytes, QSize(frame->width, frame->height), 0, QVideoFrame::Format_YUYV); qFrame = QVideoFrame( static_cast<int>(frame->data_bytes), QSize(frame->width, frame->height), 0, QVideoFrame::Format_YUYV);
qFrame.map(QAbstractVideoBuffer::WriteOnly); qFrame.map(QAbstractVideoBuffer::WriteOnly);
memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now
qFrame.unmap(); qFrame.unmap();
break; break;
default: default:
return; return;
} }
#ifdef BLOCKING_FRAME_GRABBING #ifdef BLOCKING_FRAME_GRABBING
m_surface->present( qFrame ); m_surface->present( qFrame );
...@@ -193,9 +198,8 @@ void UVCCameraSession::callback(uvc_frame_t *frame) ...@@ -193,9 +198,8 @@ void UVCCameraSession::callback(uvc_frame_t *frame)
void UVCCameraSession::presentFrame(QVideoFrame frame, const qreal t) void UVCCameraSession::presentFrame(QVideoFrame frame, const qreal t)
{ {
qreal latency = frameReference.elapsed() - t;
frame.setMetaData("timestamp", t); frame.setMetaData("timestamp", t);
if (latency <= MAX_LATENCY_MS && m_surface) if (m_surface)
m_surface->present(frame); m_surface->present(frame);
//else //else
// qWarning() << "Dropping frame (" << latency << "ms old )"; // qWarning() << "Dropping frame (" << latency << "ms old )";
...@@ -223,7 +227,7 @@ bool UVCCameraSession::load() ...@@ -223,7 +227,7 @@ bool UVCCameraSession::load()
res = uvc_open(dev, &devh); res = uvc_open(dev, &devh);
if (res != UVC_SUCCESS) { if (res != UVC_SUCCESS) {
qWarning() << "uvc_open" << uvc_strerror(res); qWarning() << "uvc_open" << uvc_strerror(res);
devh = NULL; devh = nullptr;
return false; return false;
} }
...@@ -243,7 +247,7 @@ bool UVCCameraSession::unload() ...@@ -243,7 +247,7 @@ bool UVCCameraSession::unload()
if (devh) if (devh)
uvc_close(devh); uvc_close(devh);
devh = NULL; devh = nullptr;
m_supportedViewfinderSettings.clear(); m_supportedViewfinderSettings.clear();
return true; return true;
...@@ -253,12 +257,12 @@ void concurrentCustomCallback(UVCCameraSession *session) ...@@ -253,12 +257,12 @@ void concurrentCustomCallback(UVCCameraSession *session)
{ {
QMutexLocker getFrameLocker( &session->getFrameMutex ); QMutexLocker getFrameLocker( &session->getFrameMutex );
while (session->streaming) { while (session->streaming) {
if (session->strmh == NULL) if (session->strmh == nullptr)
continue; continue;
uvc_frame_t *frame = NULL; uvc_frame_t *frame = nullptr;
uvc_error_t res; uvc_error_t res;
res = uvc_stream_get_frame(session->strmh, &frame, 0); res = uvc_stream_get_frame(session->strmh, &frame, 0);
if ( res == UVC_SUCCESS && frame != NULL) if ( res == UVC_SUCCESS && frame != nullptr)
session->callback(frame); session->callback(frame);
else else
if (res != UVC_ERROR_TIMEOUT) if (res != UVC_ERROR_TIMEOUT)
...@@ -298,9 +302,9 @@ bool UVCCameraSession::startPreview() ...@@ -298,9 +302,9 @@ bool UVCCameraSession::startPreview()
} }
#ifdef USE_CUSTOM_CALLBACK #ifdef USE_CUSTOM_CALLBACK
res = uvc_stream_start(strmh, NULL, (void*) this, getBandwidthFactor(), 0); res = uvc_stream_start(strmh, nullptr, (void*) this, getBandwidthFactor(), 0);
#else #else
res = uvc_stream_start(strmh, cb, (void*) this, getBandwidthFactor(), 0); res = uvc_stream_start(strmh, cb, static_cast<void*>(this), getBandwidthFactor(), 0);
#endif #endif
if (res != UVC_SUCCESS) { if (res != UVC_SUCCESS) {
qWarning() << "uvc_stream_start" << uvc_strerror(res); qWarning() << "uvc_stream_start" << uvc_strerror(res);
...@@ -316,7 +320,7 @@ bool UVCCameraSession::startPreview() ...@@ -316,7 +320,7 @@ bool UVCCameraSession::startPreview()
return true; return true;
} }
double UVCCameraSession::getBandwidthFactor() float UVCCameraSession::getBandwidthFactor()
{ {
switch(settings.pixelFormat()) { switch(settings.pixelFormat()) {
// compressed; user can change it through the default settings // compressed; user can change it through the default settings
...@@ -349,19 +353,19 @@ void UVCCameraSession::updateSourceCapabilities() ...@@ -349,19 +353,19 @@ void UVCCameraSession::updateSourceCapabilities()
return; return;
const uvc_format_desc_t* formatDesc = uvc_get_format_descs(devh); const uvc_format_desc_t* formatDesc = uvc_get_format_descs(devh);
while (formatDesc != NULL) { while (formatDesc != nullptr) {
QVideoFrame::PixelFormat qFormat; QVideoFrame::PixelFormat qFormat;
QString formatStr;
switch (formatDesc->bDescriptorSubtype) { switch (formatDesc->bDescriptorSubtype) {
case UVC_VS_FORMAT_MJPEG: case UVC_VS_FORMAT_MJPEG:
qFormat = QVideoFrame::Format_Jpeg; qFormat = QVideoFrame::Format_Jpeg;
break; break;
case UVC_VS_FORMAT_UNCOMPRESSED: case UVC_VS_FORMAT_UNCOMPRESSED:
qFormat = fourcc2PixelFormat(formatDesc->fourccFormat, QString()); qFormat = fourcc2PixelFormat(formatDesc->fourccFormat, formatStr);
if ( qFormat != QVideoFrame::Format_Invalid ) if ( qFormat != QVideoFrame::Format_Invalid )
break; break;
default: // format not supported, next! default: // format not supported, next!
QString formatStr;
qFormat = fourcc2PixelFormat(formatDesc->fourccFormat, formatStr); qFormat = fourcc2PixelFormat(formatDesc->fourccFormat, formatStr);
qInfo() << "UVCEngine: format not supported:" << formatDesc->bDescriptorSubtype << formatStr; qInfo() << "UVCEngine: format not supported:" << formatDesc->bDescriptorSubtype << formatStr;
formatDesc = formatDesc->next; formatDesc = formatDesc->next;
...@@ -369,7 +373,7 @@ void UVCCameraSession::updateSourceCapabilities() ...@@ -369,7 +373,7 @@ void UVCCameraSession::updateSourceCapabilities()
} }
const uvc_frame_desc *frameDesc = formatDesc->frame_descs; const uvc_frame_desc *frameDesc = formatDesc->frame_descs;
while (frameDesc!=NULL) { while (frameDesc!=nullptr) {
QCameraViewfinderSettings settings; QCameraViewfinderSettings settings;
settings.setResolution(frameDesc->wWidth, frameDesc->wHeight); settings.setResolution(frameDesc->wWidth, frameDesc->wHeight);
settings.setPixelAspectRatio(frameDesc->wWidth, frameDesc->wHeight); settings.setPixelAspectRatio(frameDesc->wWidth, frameDesc->wHeight);
...@@ -406,7 +410,7 @@ bool UVCCameraSession::stopPreview() ...@@ -406,7 +410,7 @@ bool UVCCameraSession::stopPreview()
if (res != UVC_SUCCESS && res != UVC_ERROR_INVALID_PARAM) if (res != UVC_SUCCESS && res != UVC_ERROR_INVALID_PARAM)
qWarning() << "uvc_stream_stop" << uvc_strerror(res); qWarning() << "uvc_stream_stop" << uvc_strerror(res);
uvc_stream_close(strmh); uvc_stream_close(strmh);
strmh = NULL; strmh = nullptr;
frameReference.invalidate(); frameReference.invalidate();
return true; return true;
......
...@@ -28,6 +28,28 @@ QT_BEGIN_NAMESPACE ...@@ -28,6 +28,28 @@ QT_BEGIN_NAMESPACE
// Maximum acceptable latency for calling the surface present method // Maximum acceptable latency for calling the surface present method
#define MAX_LATENCY_MS 1.0e3/25 #define MAX_LATENCY_MS 1.0e3/25
class Timing {
public:
void update(const qreal &t) {
prev = cur;
cur = t;
isp = cur - prev;
}
void correct(const Timing &other, const qreal &fps) {
qreal period = 1.0e3 / fps;
qreal diff = isp - period;
qreal otherDiff = other.isp - period;
if ( qAbs(diff) > qAbs(otherDiff) ) {
cur += otherDiff - diff;
isp = cur - prev;
}
}
qreal cur; // current
qreal prev; // previous
qreal isp; // inter-sample period
};
struct DefaultParameters { struct DefaultParameters {
double bandwidthFactor; double bandwidthFactor;
double brightness; double brightness;
...@@ -172,20 +194,24 @@ private: ...@@ -172,20 +194,24 @@ private:
QVideoFrame currentFrame; QVideoFrame currentFrame;
unsigned char* yuvBuffer; unsigned char* yuvBuffer;
long unsigned int yuvBufferSize; long unsigned int yuvBufferSize;
QElapsedTimer frameReference; QElapsedTimer frameReference;
Timing sw;
Timing hw;
Timing hybrid;
// capabilities // capabilities
QList<QCameraViewfinderSettings> m_supportedViewfinderSettings; QList<QCameraViewfinderSettings> m_supportedViewfinderSettings;
void updateSourceCapabilities(); void updateSourceCapabilities();
QCameraViewfinderSettings settings; QCameraViewfinderSettings settings;
bool qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat); bool qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat);
double getBandwidthFactor(); float getBandwidthFactor();
static QMutex sessionMutex; static QMutex sessionMutex;
// device availability // device availability
static QMap<QString,bool> devices; static QMap<QString,bool> devices;
static QMutex UVCCameraSession::devicesMutex; static QMutex devicesMutex;
bool acquire(const QString &device); bool acquire(const QString &device);
void release(const QString &device); void release(const QString &device);
...@@ -194,10 +220,10 @@ private: ...@@ -194,10 +220,10 @@ private:
QVideoFrame::PixelFormat fourcc2PixelFormat(const uint8_t *fourcc, QString &str) { QVideoFrame::PixelFormat fourcc2PixelFormat(const uint8_t *fourcc, QString &str) {
str = QString("%1%2%3%4").arg( str = QString("%1%2%3%4").arg(
(char) fourcc[0]).arg( static_cast<char>(fourcc[0])).arg(
(char) fourcc[1]).arg( static_cast<char>(fourcc[1])).arg(
(char) fourcc[2]).arg( static_cast<char>(fourcc[2])).arg(
(char) fourcc[3]); static_cast<char>(fourcc[3]));
if ( str.compare("yuyv", Qt::CaseInsensitive) == 0 ) if ( str.compare("yuyv", Qt::CaseInsensitive) == 0 )
return QVideoFrame::Format_YUYV; return QVideoFrame::Format_YUYV;
if ( str.compare("yuy2", Qt::CaseInsensitive) == 0 ) // duplicate of yuyv http://www.fourcc.org/pixel-format/yuv-yuy2/ if ( str.compare("yuy2", Qt::CaseInsensitive) == 0 ) // duplicate of yuyv http://www.fourcc.org/pixel-format/yuv-yuy2/
......
...@@ -4,7 +4,7 @@ QMediaService* UVCServicePlugin::create(const QString &key) ...@@ -4,7 +4,7 @@ QMediaService* UVCServicePlugin::create(const QString &key)
{ {
if (key == Q_MEDIASERVICE_CAMERA) if (key == Q_MEDIASERVICE_CAMERA)
return new UVCCameraService(); return new UVCCameraService();
return NULL; return nullptr;
} }
void UVCServicePlugin::release(QMediaService *service) void UVCServicePlugin::release(QMediaService *service)
......
...@@ -8,7 +8,7 @@ bool UVCVideoDeviceControl::loaded = false; ...@@ -8,7 +8,7 @@ bool UVCVideoDeviceControl::loaded = false;
UVCVideoDeviceControl::UVCVideoDeviceControl(QObject *parent) UVCVideoDeviceControl::UVCVideoDeviceControl(QObject *parent)
: QVideoDeviceSelectorControl(parent), : QVideoDeviceSelectorControl(parent),
ctx(NULL), ctx(nullptr),
selected(0) selected(0)
{ {
m_session = qobject_cast<UVCCameraSession*>(parent); m_session = qobject_cast<UVCCameraSession*>(parent);
...@@ -89,7 +89,7 @@ void UVCVideoDeviceControl::updateDevices() ...@@ -89,7 +89,7 @@ void UVCVideoDeviceControl::updateDevices()
uvc_error_t res; uvc_error_t res;
uvc_context_t *ctx; uvc_context_t *ctx;
res = uvc_init(&ctx, NULL); res = uvc_init(&ctx, nullptr);
if (res < 0) { if (res < 0) {
qWarning() << "uvc_init" << uvc_strerror(res); qWarning() << "uvc_init" << uvc_strerror(res);
return ; return ;
...@@ -106,7 +106,7 @@ void UVCVideoDeviceControl::updateDevices() ...@@ -106,7 +106,7 @@ void UVCVideoDeviceControl::updateDevices()
while (1) { while (1) {
uvc_device_t *dev = devList[idx]; uvc_device_t *dev = devList[idx];
idx++; idx++;
if (dev == NULL) if (dev == nullptr)
break; break;
uvc_device_descriptor_t *desc; uvc_device_descriptor_t *desc;
res = uvc_get_device_descriptor(dev, &desc); res = uvc_get_device_descriptor(dev, &desc);
......
...@@ -4,7 +4,7 @@ QT_BEGIN_NAMESPACE ...@@ -4,7 +4,7 @@ QT_BEGIN_NAMESPACE
UVCVideoRendererControl::UVCVideoRendererControl(UVCCameraSession* session, QObject *parent) UVCVideoRendererControl::UVCVideoRendererControl(UVCCameraSession* session, QObject *parent)
:QVideoRendererControl(parent), :QVideoRendererControl(parent),
m_surface(NULL), m_surface(nullptr),
m_session(session) m_session(session)
{ {
} }
......
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