1
/****************************************************************************
2
* Copyright (C) 2012-2014 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Lesser General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2.1 of the License, or (at your option) any later version. *
10
* This library is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13
* Lesser General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17
***************************************************************************/
18
#include "videomodel.h"
21
#include <QtCore/QMutex>
24
#include "../dbus/videomanager.h"
25
#include "videodevice.h"
27
#include <callmodel.h>
28
#include "videorenderer.h"
29
#include "videodevicemodel.h"
30
#include "videochannel.h"
31
#include "videorate.h"
32
#include "videoresolution.h"
35
VideoModel* VideoModel::m_spInstance = nullptr;
38
VideoModel::VideoModel():QThread(),m_BufferSize(0),m_ShmKey(0),m_SemKey(0),m_PreviewState(false),m_SSMutex(new QMutex())
40
VideoManagerInterface& interface = DBus::VideoManager::instance();
41
connect( &interface , SIGNAL(deviceEvent()) , this, SLOT(deviceEvent()) );
42
connect( &interface , SIGNAL(startedDecoding(QString,QString,int,int,bool)), this, SLOT(startedDecoding(QString,QString,int,int)));
43
connect( &interface , SIGNAL(stoppedDecoding(QString,QString,bool)) , this, SLOT(stoppedDecoding(QString,QString)) );
47
VideoModel::~VideoModel()
53
VideoModel* VideoModel::instance()
56
m_spInstance = new VideoModel();
61
///Return the call renderer or nullptr
62
VideoRenderer* VideoModel::getRenderer(const Call* call) const
64
if (!call) return nullptr;
65
return m_lRenderers[call->id()];
68
///Get the video preview renderer
69
VideoRenderer* VideoModel::previewRenderer()
71
if (!m_lRenderers["local"]) {
72
VideoResolution* res = VideoDeviceModel::instance()->activeDevice()->activeChannel()->activeResolution();
74
qWarning() << "Misconfigured video device";
77
m_lRenderers["local"] = new VideoRenderer("local","",res->size());
79
return m_lRenderers["local"];
83
void VideoModel::stopPreview()
85
VideoManagerInterface& interface = DBus::VideoManager::instance();
86
interface.stopCamera();
87
m_PreviewState = false;
90
///Start video preview
91
void VideoModel::startPreview()
93
if (m_PreviewState) return;
94
VideoManagerInterface& interface = DBus::VideoManager::instance();
95
interface.startCamera();
96
m_PreviewState = true;
99
///Is the video model fetching preview from a camera
100
bool VideoModel::isPreviewing()
102
return m_PreviewState;
105
///@todo Set the video buffer size
106
void VideoModel::setBufferSize(uint size)
112
void VideoModel::deviceEvent()
117
///A video is not being rendered
118
void VideoModel::startedDecoding(const QString& id, const QString& shmPath, int width, int height)
122
QSize res = QSize(width,height);
123
// if (VideoDeviceModel::instance()->activeDevice()
124
// && VideoDeviceModel::instance()->activeDevice()->activeChannel()->activeResolution()->width() == width) {
125
// //FIXME flawed logic
126
// res = VideoDeviceModel::instance()->activeDevice()->activeChannel()->activeResolution()->size();
129
// res = QSize(width,height);
132
if (m_lRenderers[id] == nullptr ) {
133
m_lRenderers[id] = new VideoRenderer(id,shmPath,res);
134
m_lRenderers[id]->moveToThread(this);
139
VideoRenderer* renderer = m_lRenderers[id];
140
renderer->setShmPath(shmPath);
141
renderer->setSize(res);
144
m_lRenderers[id]->startRendering();
145
VideoDevice* dev = VideoDeviceModel::instance()->getDevice(id);
147
emit dev->renderingStarted(m_lRenderers[id]);
150
qDebug() << "Starting video for call" << id;
151
emit videoCallInitiated(m_lRenderers[id]);
154
m_PreviewState = true;
155
emit previewStateChanged(true);
156
emit previewStarted(m_lRenderers[id]);
160
///A video stopped being rendered
161
void VideoModel::stoppedDecoding(const QString& id, const QString& shmPath)
164
VideoRenderer* r = m_lRenderers[id];
168
qDebug() << "Video stopped for call" << id << "Renderer found:" << (m_lRenderers[id] != nullptr);
169
// emit videoStopped();
171
VideoDevice* dev = VideoDeviceModel::instance()->getDevice(id);
173
emit dev->renderingStopped(r);
176
m_PreviewState = false;
177
emit previewStateChanged(false);
178
emit previewStopped(r);
180
// r->mutex()->lock();
181
m_lRenderers[id] = nullptr;
185
void VideoModel::switchDevice(const VideoDevice* device) const
187
VideoManagerInterface& interface = DBus::VideoManager::instance();
188
interface.switchInput(device->id());
191
QMutex* VideoModel::startStopMutex() const