I'm trying to use QMediaDevices
just to grab the list of cameras input. QMediaDevices::videoInputs()
return an empty list; no error is thrown in compilation time. On run-time I got a sort of warning / log:
Available HW decoding frameworks:
dxva2
d3d11va
Strange thing being, if I load the Qt example Camera
, everything (including the list of my devices) work fine.
That's what I've got so far:
#include "videomanager.h"
#include <opencv2/imgcodecs.hpp> // I need opencv2 for other reasons
#include <QtNetwork>
#include <QThread>
#include <QMediaDevices>
#include <QCameraDevice>
#include <QCamera>
#include <iostream>
[...]
QJsonArray VideoManager::cameraList() const
{
//QCamera *camera = new QCamera; // // As suggeste by https://stackoverflow.com/a/43326541/5321862 - Not working
const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
QJsonArray json_cameras;
for (int i = 0; i < cameras.length(); i++) {
QCameraDevice camera = cameras[i];
QJsonObject o;
o["description"] = QJsonValue(camera.description());
o["index"] = QJsonValue(i);
o["id"] = QJsonValue(QString(camera.id().toBase64()));
json_cameras.append(QJsonValue(o));
}
return json_cameras;
}
My .pro
file (automatically generated) follows:
QT -= gui
QT += network multimedia
requires(qtConfig(udpsocket))
CONFIG += c++17 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
definitions.cpp \
main.cpp \
message.cpp \
tcpconnectionmanager.cpp \
videograbber.cpp \
videomanager.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
definitions.h \
message.h \
tcpconnectionmanager.h \
videograbber.h \
videomanager.h
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../local/custom-libs/lib/ -lopencv_core470
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../local/custom-libs/lib/ -lopencv_core470d
else:unix:!macx: LIBS += -L$$PWD/../../local/custom-libs/lib/ -lopencv_core470
INCLUDEPATH += $$PWD/../../local/custom-libs/include
DEPENDPATH += $$PWD/../../local/custom-libs/include
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/libopencv_core470.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/libopencv_core470d.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/opencv_core470.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/opencv_core470d.lib
else:unix:!macx: PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/libopencv_core470.a
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../local/custom-libs/lib/ -lopencv_videoio470
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../local/custom-libs/lib/ -lopencv_videoio470d
else:unix:!macx: LIBS += -L$$PWD/../../local/custom-libs/lib/ -lopencv_videoio470
INCLUDEPATH += $$PWD/../../local/custom-libs/include
DEPENDPATH += $$PWD/../../local/custom-libs/include
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/libopencv_videoio470.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/libopencv_videoio470d.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/opencv_videoio470.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/opencv_videoio470d.lib
else:unix:!macx: PRE_TARGETDEPS += $$PWD/../../local/custom-libs/lib/libopencv_videoio470.a
DISTFILES += \
defaultConfig.json
Edit
A call to QMediaDevices::audioInputs()
before videoInputs()
solves the problem, but I can't figure out why and, above all, I think a useless call is not an actual solution
#include <QAudioDevice>
[...]
const auto audios = QMediaDevices::audioInputs(); // Useless but allows videoInputs() to return the right camera.
const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
Furthermore, I've opened an issue on Qt
Edit 2
Issue has been solved