2015-06-06 09:40:08 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2015 by The qTox Project
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
#include <QDebug>
|
2015-06-02 06:34:38 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
2015-05-14 10:46:28 +08:00
|
|
|
extern "C" {
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavdevice/avdevice.h>
|
|
|
|
}
|
|
|
|
#include "cameradevice.h"
|
2015-06-06 07:44:47 +08:00
|
|
|
#include "src/persistence/settings.h"
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#include "src/platform/camera/directshow.h"
|
|
|
|
#endif
|
2015-06-01 22:37:31 +08:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
#include "src/platform/camera/v4l2.h"
|
|
|
|
#endif
|
2015-05-16 05:01:38 +08:00
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
QHash<QString, CameraDevice*> CameraDevice::openDevices;
|
|
|
|
QMutex CameraDevice::openDeviceLock, CameraDevice::iformatLock;
|
|
|
|
AVInputFormat* CameraDevice::iformat{nullptr};
|
2015-06-02 05:40:33 +08:00
|
|
|
AVInputFormat* CameraDevice::idesktopFormat{nullptr};
|
2015-05-14 10:46:28 +08:00
|
|
|
|
|
|
|
CameraDevice::CameraDevice(const QString devName, AVFormatContext *context)
|
|
|
|
: devName{devName}, context{context}, refcount{1}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-05-16 10:01:38 +08:00
|
|
|
CameraDevice* CameraDevice::open(QString devName, AVDictionary** options)
|
2015-05-14 10:46:28 +08:00
|
|
|
{
|
|
|
|
openDeviceLock.lock();
|
|
|
|
AVFormatContext* fctx = nullptr;
|
|
|
|
CameraDevice* dev = openDevices.value(devName);
|
|
|
|
if (dev)
|
|
|
|
goto out;
|
|
|
|
|
2015-06-02 05:40:33 +08:00
|
|
|
AVInputFormat* format;
|
|
|
|
if (devName.startsWith("x11grab#"))
|
|
|
|
{
|
|
|
|
devName = devName.mid(8);
|
|
|
|
format = idesktopFormat;
|
|
|
|
}
|
2015-06-03 17:11:07 +08:00
|
|
|
else if (devName.startsWith("gdigrab#"))
|
|
|
|
{
|
|
|
|
devName = devName.mid(8);
|
|
|
|
format = idesktopFormat;
|
|
|
|
}
|
2015-06-02 05:40:33 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
format = iformat;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avformat_open_input(&fctx, devName.toStdString().c_str(), format, options)<0)
|
2015-05-14 10:46:28 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (avformat_find_stream_info(fctx, NULL) < 0)
|
|
|
|
{
|
|
|
|
avformat_close_input(&fctx);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev = new CameraDevice{devName, fctx};
|
|
|
|
openDevices[devName] = dev;
|
|
|
|
|
|
|
|
out:
|
|
|
|
openDeviceLock.unlock();
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2015-05-16 10:01:38 +08:00
|
|
|
CameraDevice* CameraDevice::open(QString devName)
|
|
|
|
{
|
2015-06-03 17:11:07 +08:00
|
|
|
VideoMode mode{0,0,0};
|
2015-06-02 06:34:38 +08:00
|
|
|
return open(devName, mode);
|
2015-05-16 10:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CameraDevice* CameraDevice::open(QString devName, VideoMode mode)
|
|
|
|
{
|
|
|
|
if (!getDefaultInputFormat())
|
|
|
|
return nullptr;
|
|
|
|
|
2015-06-22 20:59:55 +08:00
|
|
|
if (devName == "none")
|
|
|
|
{
|
|
|
|
qDebug() << "Tried to open the null device";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-16 10:01:38 +08:00
|
|
|
AVDictionary* options = nullptr;
|
2015-07-15 16:46:23 +08:00
|
|
|
if (!iformat);
|
2015-06-02 06:34:38 +08:00
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
else if (devName.startsWith("x11grab#"))
|
|
|
|
{
|
|
|
|
QSize screen;
|
2015-06-05 05:34:17 +08:00
|
|
|
if (mode.width && mode.height)
|
2015-06-02 06:34:38 +08:00
|
|
|
{
|
|
|
|
screen.setWidth(mode.width);
|
|
|
|
screen.setHeight(mode.height);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
screen = QApplication::desktop()->screenGeometry().size();
|
|
|
|
// Workaround https://trac.ffmpeg.org/ticket/4574 by choping 1 px bottom and right
|
|
|
|
screen.setWidth(screen.width()-1);
|
|
|
|
screen.setHeight(screen.height()-1);
|
|
|
|
}
|
|
|
|
av_dict_set(&options, "video_size", QString("%1x%2").arg(screen.width()).arg(screen.height()).toStdString().c_str(), 0);
|
2015-06-05 05:34:17 +08:00
|
|
|
if (mode.FPS)
|
|
|
|
av_dict_set(&options, "framerate", QString().setNum(mode.FPS).toStdString().c_str(), 0);
|
|
|
|
else
|
|
|
|
av_dict_set(&options, "framerate", QString().setNum(5).toStdString().c_str(), 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
else if (devName.startsWith("gdigrab#"))
|
|
|
|
{
|
|
|
|
av_dict_set(&options, "framerate", QString().setNum(5).toStdString().c_str(), 0);
|
2015-06-02 06:34:38 +08:00
|
|
|
}
|
|
|
|
#endif
|
2015-05-16 10:01:38 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2015-06-02 06:34:38 +08:00
|
|
|
else if (iformat->name == QString("dshow") && mode)
|
2015-05-16 10:01:38 +08:00
|
|
|
{
|
|
|
|
av_dict_set(&options, "video_size", QString("%1x%2").arg(mode.width).arg(mode.height).toStdString().c_str(), 0);
|
|
|
|
av_dict_set(&options, "framerate", QString().setNum(mode.FPS).toStdString().c_str(), 0);
|
|
|
|
}
|
2015-06-01 22:37:31 +08:00
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_LINUX
|
2015-06-02 06:34:38 +08:00
|
|
|
else if (iformat->name == QString("video4linux2,v4l2") && mode)
|
2015-06-01 22:37:31 +08:00
|
|
|
{
|
|
|
|
av_dict_set(&options, "video_size", QString("%1x%2").arg(mode.width).arg(mode.height).toStdString().c_str(), 0);
|
|
|
|
av_dict_set(&options, "framerate", QString().setNum(mode.FPS).toStdString().c_str(), 0);
|
|
|
|
}
|
2015-05-16 10:01:38 +08:00
|
|
|
#endif
|
2015-06-02 06:34:38 +08:00
|
|
|
else if (mode)
|
2015-05-16 10:01:38 +08:00
|
|
|
{
|
|
|
|
qWarning() << "Video mode-setting not implemented for input "<<iformat->name;
|
2015-05-16 18:08:29 +08:00
|
|
|
(void)mode;
|
2015-05-16 10:01:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CameraDevice* dev = open(devName, &options);
|
|
|
|
if (options)
|
|
|
|
av_dict_free(&options);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
void CameraDevice::open()
|
|
|
|
{
|
|
|
|
++refcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraDevice::close()
|
|
|
|
{
|
|
|
|
if (--refcount <= 0)
|
|
|
|
{
|
|
|
|
openDeviceLock.lock();
|
|
|
|
openDevices.remove(devName);
|
|
|
|
openDeviceLock.unlock();
|
|
|
|
avformat_close_input(&context);
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
QVector<QPair<QString, QString>> CameraDevice::getRawDeviceListGeneric()
|
2015-05-14 10:46:28 +08:00
|
|
|
{
|
2015-05-16 05:01:38 +08:00
|
|
|
QVector<QPair<QString, QString>> devices;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
|
|
|
if (!getDefaultInputFormat())
|
2015-05-16 05:01:38 +08:00
|
|
|
return devices;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-15 00:40:12 +08:00
|
|
|
// Alloc an input device context
|
|
|
|
AVFormatContext *s;
|
|
|
|
if (!(s = avformat_alloc_context()))
|
2015-05-16 05:01:38 +08:00
|
|
|
return devices;
|
2015-05-15 00:40:12 +08:00
|
|
|
if (!iformat->priv_class || !AV_IS_INPUT_DEVICE(iformat->priv_class->category))
|
|
|
|
{
|
|
|
|
avformat_free_context(s);
|
2015-05-16 05:01:38 +08:00
|
|
|
return devices;
|
2015-05-15 00:40:12 +08:00
|
|
|
}
|
|
|
|
s->iformat = iformat;
|
|
|
|
if (s->iformat->priv_data_size > 0)
|
|
|
|
{
|
|
|
|
s->priv_data = av_mallocz(s->iformat->priv_data_size);
|
|
|
|
if (!s->priv_data)
|
|
|
|
{
|
|
|
|
avformat_free_context(s);
|
2015-05-16 05:01:38 +08:00
|
|
|
return devices;
|
2015-05-15 00:40:12 +08:00
|
|
|
}
|
|
|
|
if (s->iformat->priv_class)
|
|
|
|
{
|
|
|
|
*(const AVClass**)s->priv_data= s->iformat->priv_class;
|
|
|
|
av_opt_set_defaults(s->priv_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s->priv_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the devices for this context
|
2015-05-16 05:01:38 +08:00
|
|
|
AVDeviceInfoList* devlist = nullptr;
|
2015-05-15 00:40:12 +08:00
|
|
|
AVDictionary *tmp = nullptr;
|
|
|
|
av_dict_copy(&tmp, nullptr, 0);
|
|
|
|
if (av_opt_set_dict2(s, &tmp, AV_OPT_SEARCH_CHILDREN) < 0)
|
|
|
|
{
|
|
|
|
av_dict_free(&tmp);
|
|
|
|
avformat_free_context(s);
|
|
|
|
}
|
|
|
|
avdevice_list_devices(s, &devlist);
|
2015-05-15 00:49:38 +08:00
|
|
|
if (!devlist)
|
2015-06-04 23:22:58 +08:00
|
|
|
{
|
2015-05-16 05:01:38 +08:00
|
|
|
qWarning() << "avdevice_list_devices failed";
|
2015-06-04 23:22:58 +08:00
|
|
|
return devices;
|
|
|
|
}
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-15 00:40:12 +08:00
|
|
|
// Convert the list to a QVector
|
2015-05-14 10:46:28 +08:00
|
|
|
devices.resize(devlist->nb_devices);
|
|
|
|
for (int i=0; i<devlist->nb_devices; i++)
|
|
|
|
{
|
|
|
|
AVDeviceInfo* dev = devlist->devices[i];
|
|
|
|
devices[i].first = dev->device_name;
|
|
|
|
devices[i].second = dev->device_description;
|
|
|
|
}
|
2015-05-15 00:40:12 +08:00
|
|
|
avdevice_free_list_devices(&devlist);
|
2015-05-14 10:46:28 +08:00
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
QVector<QPair<QString, QString>> CameraDevice::getDeviceList()
|
|
|
|
{
|
2015-06-02 05:40:33 +08:00
|
|
|
QVector<QPair<QString, QString>> devices;
|
|
|
|
|
2015-09-18 16:26:58 +08:00
|
|
|
devices.append({"none", QObject::tr("None", "No camera device set")});
|
2015-06-22 20:59:55 +08:00
|
|
|
|
2015-05-16 10:01:38 +08:00
|
|
|
if (!getDefaultInputFormat())
|
2015-06-02 05:40:33 +08:00
|
|
|
return devices;
|
2015-05-16 05:01:38 +08:00
|
|
|
|
2015-07-15 16:46:23 +08:00
|
|
|
if (!iformat);
|
2015-05-16 18:08:29 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
else if (iformat->name == QString("dshow"))
|
2015-06-22 20:59:55 +08:00
|
|
|
devices += DirectShow::getDeviceList();
|
2015-05-16 18:08:29 +08:00
|
|
|
#endif
|
2015-05-16 05:01:38 +08:00
|
|
|
else
|
2015-06-22 20:59:55 +08:00
|
|
|
devices += getRawDeviceListGeneric();
|
2015-06-02 05:40:33 +08:00
|
|
|
|
2015-06-03 17:11:07 +08:00
|
|
|
if (idesktopFormat)
|
|
|
|
{
|
|
|
|
if (idesktopFormat->name == QString("x11grab"))
|
2015-09-18 16:26:58 +08:00
|
|
|
devices.push_back(QPair<QString,QString>{"x11grab#:0", QObject::tr("Desktop", "Desktop as a camera input for screen sharing")});
|
2015-06-03 17:11:07 +08:00
|
|
|
if (idesktopFormat->name == QString("gdigrab"))
|
2015-09-18 16:26:58 +08:00
|
|
|
devices.push_back(QPair<QString,QString>{"gdigrab#desktop", QObject::tr("Desktop", "Desktop as a camera input for screen sharing")});
|
2015-06-03 17:11:07 +08:00
|
|
|
}
|
2015-06-02 05:40:33 +08:00
|
|
|
|
|
|
|
return devices;
|
2015-05-16 05:01:38 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
QString CameraDevice::getDefaultDeviceName()
|
|
|
|
{
|
2015-05-16 05:01:38 +08:00
|
|
|
QString defaultdev = Settings::getInstance().getVideoDev();
|
2015-05-14 10:46:28 +08:00
|
|
|
|
|
|
|
if (!getDefaultInputFormat())
|
2015-05-16 05:01:38 +08:00
|
|
|
return defaultdev;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
QVector<QPair<QString, QString>> devlist = getDeviceList();
|
|
|
|
for (const QPair<QString,QString>& device : devlist)
|
|
|
|
if (defaultdev == device.first)
|
|
|
|
return defaultdev;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
if (devlist.isEmpty())
|
|
|
|
return defaultdev;
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
return devlist[0].first;
|
2015-05-14 10:46:28 +08:00
|
|
|
}
|
|
|
|
|
2015-05-16 10:01:38 +08:00
|
|
|
QVector<VideoMode> CameraDevice::getVideoModes(QString devName)
|
|
|
|
{
|
2015-07-15 16:46:23 +08:00
|
|
|
if (!iformat);
|
2015-05-16 10:01:38 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
else if (iformat->name == QString("dshow"))
|
|
|
|
return DirectShow::getDeviceModes(devName);
|
2015-06-01 22:37:31 +08:00
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
else if (iformat->name == QString("video4linux2,v4l2"))
|
|
|
|
return v4l2::getDeviceModes(devName);
|
2015-05-16 10:01:38 +08:00
|
|
|
#endif
|
|
|
|
else
|
|
|
|
qWarning() << "Video mode listing not implemented for input "<<iformat->name;
|
|
|
|
|
2015-05-16 18:08:29 +08:00
|
|
|
(void)devName;
|
2015-05-16 10:01:38 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2015-05-14 10:46:28 +08:00
|
|
|
bool CameraDevice::getDefaultInputFormat()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&iformatLock);
|
|
|
|
if (iformat)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
avdevice_register_all();
|
|
|
|
|
2015-06-02 05:40:33 +08:00
|
|
|
// Desktop capture input formats
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
idesktopFormat = av_find_input_format("x11grab");
|
|
|
|
#endif
|
2015-06-03 17:11:07 +08:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
idesktopFormat = av_find_input_format("gdigrab");
|
|
|
|
#endif
|
2015-06-02 05:40:33 +08:00
|
|
|
|
|
|
|
// Webcam input formats
|
2015-05-16 05:01:38 +08:00
|
|
|
#ifdef Q_OS_LINUX
|
2015-05-14 10:46:28 +08:00
|
|
|
if ((iformat = av_find_input_format("v4l2")))
|
|
|
|
return true;
|
2015-05-16 05:01:38 +08:00
|
|
|
#endif
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
#ifdef Q_OS_WIN
|
2015-05-14 10:46:28 +08:00
|
|
|
if ((iformat = av_find_input_format("dshow")))
|
|
|
|
return true;
|
2015-05-16 05:01:38 +08:00
|
|
|
if ((iformat = av_find_input_format("vfwcap")))
|
|
|
|
#endif
|
2015-05-14 10:46:28 +08:00
|
|
|
|
2015-05-16 05:01:38 +08:00
|
|
|
#ifdef Q_OS_OSX
|
2015-05-14 10:46:28 +08:00
|
|
|
if ((iformat = av_find_input_format("avfoundation")))
|
|
|
|
return true;
|
|
|
|
if ((iformat = av_find_input_format("qtkit")))
|
|
|
|
return true;
|
2015-05-16 05:01:38 +08:00
|
|
|
#endif
|
2015-05-14 10:46:28 +08:00
|
|
|
|
|
|
|
qWarning() << "No valid input format found";
|
|
|
|
return false;
|
|
|
|
}
|