uvccamerasession.cpp 5.9 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2
#include "uvccamerasession.h"

3 4
QMutex UVCCameraSession::sessionMutex;

Thiago Santini's avatar
Thiago Santini committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
UVCCameraSession::UVCCameraSession(QObject *parent)
    : m_surface(Q_NULLPTR),
      streaming(false),
      devh(NULL)
{
}

UVCCameraSession::~UVCCameraSession()
{

}

void UVCCameraSession::setDevice(uvc_device_t *device)
{
    dev = device;
}

void UVCCameraSession::setSurface(QAbstractVideoSurface* surface)
{
    m_surface = surface;
}

void cb(uvc_frame_t *frame, void *ptr) { static_cast<UVCCameraSession*>(ptr)->callback(frame); }
void UVCCameraSession::callback(uvc_frame_t *frame)
{
    qreal t = frameReference.elapsed();

    if (!streaming)
        return;

    if (m_surface) {

        QVideoFrame qFrame;
        switch(frame->frame_format) {
            case UVC_FRAME_FORMAT_MJPEG:
                qFrame = QVideoFrame( (int) frame->data_bytes, QSize(frame->width, frame->height), -1, QVideoFrame::Format_Jpeg);
                qFrame.map(QAbstractVideoBuffer::WriteOnly);
                memcpy( qFrame.bits(), frame->data, frame->data_bytes); // copied; safe to retun from callback now
                qFrame.unmap();
                break;
            default:
                return;
        }

        QMutexLocker locker(&frameMutex);
        QMetaObject::invokeMethod(this, "presentFrame", Qt::QueuedConnection, Q_ARG(const QVideoFrame&, qFrame), Q_ARG(const qreal, t) );
    }
}

void UVCCameraSession::presentFrame(const QVideoFrame &frame, const qreal t)
{
    QMutexLocker locker(&frameMutex);
    qreal latency = frameReference.elapsed() - t;
    if (latency < MAX_LATENCY_MS)
        m_surface->present( frame );
    else
        qDebug() << "Dropping frame (" << latency << "ms old)";
}

bool UVCCameraSession::load()
{
66
    QMutexLocker sessionLocker(&sessionMutex);
Thiago Santini's avatar
Thiago Santini committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    uvc_error_t res;

    res = uvc_open(dev, &devh);
    if (res != UVC_SUCCESS) {
        qDebug() << "uvc_open" << uvc_strerror(res);
        devh = NULL;
        return false;
    }

    // TODO: parametrize this
    uvc_set_focus_auto(devh, 0);
    uvc_set_ae_priority(devh, 0);
    uvc_set_ae_mode(devh, 1);
    uvc_set_saturation(devh, 0);
    uvc_set_exposure_abs(devh, 63);
    uvc_set_backlight_compensation(devh, 2);
    uvc_set_gamma(devh, 100);

    updateSourceCapabilities();

    return true;
}

bool UVCCameraSession::unload()
{
92
    QMutexLocker sessionLocker(&sessionMutex);
Thiago Santini's avatar
Thiago Santini committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
    if (streaming)
        stopPreview();

    uvc_close(devh);
    devh = NULL;

    m_supportedViewfinderSettings.clear();
    return true;
}

bool UVCCameraSession::startPreview()
{
    uvc_error_t res;

    if (streaming)
        stopPreview();

    uvc_frame_format format;

    if (!qPixelFormat2UVCFrameFormat(settings.pixelFormat(), format)) {
        qDebug() << "Unknown pixel format";
        return false;
    }

    res = uvc_get_stream_ctrl_format_size(devh, &ctrl, format, settings.resolution().width(), settings.resolution().height(), settings.maximumFrameRate());
    if (res != UVC_SUCCESS) {
        qDebug() << "uvc_get_stream_ctrl" << uvc_strerror(res);
        return false;
    }

    res = uvc_stream_open_ctrl(devh, &strmh, &ctrl);
    if (res != UVC_SUCCESS) {
        qDebug() << "uvc_stream_open_ctrl" << uvc_strerror(res);
        return false;
    }

    res = uvc_stream_start(strmh, cb, (void*) this, 1.5f, 0);
    if (res != UVC_SUCCESS) {
        qDebug() << "uvc_stream_start" << uvc_strerror(res);
        return false;
    }

    streaming = true;
    frameReference.restart();
    return true;
}

bool UVCCameraSession::qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat)
{
    switch(qFormat) {
        case QVideoFrame::Format_Jpeg:
            uvcFormat = UVC_FRAME_FORMAT_MJPEG;
            return true;
        default:
            return false;
    }
}

void UVCCameraSession::updateSourceCapabilities()
{
    m_supportedViewfinderSettings.clear();

    if (!devh)
        return;

    const uvc_format_desc_t* formatDesc = uvc_get_format_descs(devh);
    while (formatDesc != NULL) {

        QVideoFrame::PixelFormat qFormat;
        switch (formatDesc->bDescriptorSubtype) {
            case UVC_VS_FORMAT_MJPEG:
                qFormat = QVideoFrame::Format_Jpeg;
                break;
            default: // format not supported, next!
                formatDesc = formatDesc->next;
                continue;
        }

        const uvc_frame_desc *frameDesc = formatDesc->frame_descs;
        while (frameDesc!=NULL) {
            QCameraViewfinderSettings settings;
            settings.setResolution(frameDesc->wWidth, frameDesc->wHeight);
            settings.setPixelAspectRatio(frameDesc->wWidth, frameDesc->wHeight);
            settings.setPixelFormat(qFormat);

            uint32_t *intervals = frameDesc->intervals;
            while (*intervals) {
                // libuvc uses 100ns units
                double fps = 1.0e7 / (*intervals);
                settings.setMinimumFrameRate( fps );
                settings.setMaximumFrameRate( fps );
                m_supportedViewfinderSettings.push_back(settings);
                intervals++;
            }

            frameDesc = frameDesc->next;
        }

        formatDesc = formatDesc->next;
    }
}

bool UVCCameraSession::stopPreview()
{
    uvc_error_t res;

    // for some reason libuvc seems to timeout on uvc_stream_stop
    for (unsigned int i=0; i<100;i++)
        if ( (res = uvc_stream_stop(strmh)) != UVC_ERROR_TIMEOUT)
            break;
    if (res != UVC_SUCCESS && res != UVC_ERROR_INVALID_PARAM)
        qDebug() << "uvc_stream_stop" << uvc_strerror(res);

    streaming = false;
    frameReference.invalidate();
    return true;
}

QList<QCameraViewfinderSettings> UVCCameraSession::supportedViewfinderSettings()
{
    return m_supportedViewfinderSettings;
}

QCameraViewfinderSettings UVCCameraSession::viewfinderSettings()
{
    return QCameraViewfinderSettings();
}

void UVCCameraSession::setViewfinderSettings(const QCameraViewfinderSettings &settings)
{
    this->settings = settings;
}