uvccamerasession.h 1.98 KB
Newer Older
Thiago Santini's avatar
Thiago Santini committed
1 2 3 4 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 66 67 68 69 70 71 72 73 74 75
#ifndef UVCCAMERASESSION_H
#define UVCCAMERASESSION_H

#include <QObject>
#include <QTime>
#include <QUrl>
#include <QMutex>

#include <qcamera.h>
#include <QtMultimedia/qvideoframe.h>
#include <QtMultimedia/qabstractvideosurface.h>
#include <QtMultimedia/qvideosurfaceformat.h>
#include <QtMultimedia/qcameraimageprocessingcontrol.h>
#include <QtMultimedia/qabstractvideobuffer.h>

#include <QElapsedTimer>

#include <vector>

#include "libuvc/libuvc.h"

QT_BEGIN_NAMESPACE

// Maximum acceptable latency for calling the surface present method
#define MAX_LATENCY_MS 100

class UVCCameraSession : public QObject
{
    Q_OBJECT
public:
    UVCCameraSession(QObject *parent = 0);
    ~UVCCameraSession();

    QCamera::Status status() const { return m_status; }

    void setSurface(QAbstractVideoSurface* surface);

    QList<QCameraViewfinderSettings> supportedViewfinderSettings();
    QCameraViewfinderSettings viewfinderSettings();
    void setViewfinderSettings(const QCameraViewfinderSettings &settings);

    void setDevice(uvc_device_t *device);

    bool load();
    bool unload();
    bool startPreview();
    bool stopPreview();
    void callback(uvc_frame_t *frame);

private Q_SLOTS:
    void presentFrame(const QVideoFrame &frame, const qreal t);

private:
    bool streaming;
    QCamera::Status m_status;
    QAbstractVideoSurface *m_surface;

    // Source (camera)
    uvc_device_t *dev;
    uvc_device_handle_t *devh;
    uvc_stream_ctrl_t ctrl;
    uvc_stream_handle_t *strmh;

    // Frame
    QVideoFrame currentFrame;
    unsigned char* yuvBuffer;
    long unsigned int yuvBufferSize;
    QElapsedTimer frameReference;
    QMutex frameMutex;

    // capabilities
    QList<QCameraViewfinderSettings> m_supportedViewfinderSettings;
    void updateSourceCapabilities();
    QCameraViewfinderSettings settings;
    bool qPixelFormat2UVCFrameFormat(const QVideoFrame::PixelFormat &qFormat, uvc_frame_format &uvcFormat);
76 77

    static QMutex sessionMutex;
Thiago Santini's avatar
Thiago Santini committed
78 79 80 81 82 83
};

QT_END_NAMESPACE


#endif // UVCCAMERASESSION_H