~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to kde/src/lib/video/videomodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 *   Copyright (C) 2012-2014 by Savoir-Faire Linux                          *
 
3
 *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
 
4
 *                                                                          *
 
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.     *
 
9
 *                                                                          *
 
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.                        *
 
14
 *                                                                          *
 
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"
 
19
 
 
20
//Qt
 
21
#include <QtCore/QMutex>
 
22
 
 
23
//SFLPhone
 
24
#include "../dbus/videomanager.h"
 
25
#include "videodevice.h"
 
26
#include <call.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"
 
33
 
 
34
//Static member
 
35
VideoModel* VideoModel::m_spInstance = nullptr;
 
36
 
 
37
///Constructor
 
38
VideoModel::VideoModel():QThread(),m_BufferSize(0),m_ShmKey(0),m_SemKey(0),m_PreviewState(false),m_SSMutex(new QMutex())
 
39
{
 
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))        );
 
44
}
 
45
 
 
46
 
 
47
VideoModel::~VideoModel()
 
48
{
 
49
 
 
50
}
 
51
 
 
52
///Singleton
 
53
VideoModel* VideoModel::instance()
 
54
{
 
55
   if (!m_spInstance) {
 
56
      m_spInstance = new VideoModel();
 
57
   }
 
58
   return m_spInstance;
 
59
}
 
60
 
 
61
///Return the call renderer or nullptr
 
62
VideoRenderer* VideoModel::getRenderer(const Call* call) const
 
63
{
 
64
   if (!call) return nullptr;
 
65
   return m_lRenderers[call->id()];
 
66
}
 
67
 
 
68
///Get the video preview renderer
 
69
VideoRenderer* VideoModel::previewRenderer()
 
70
{
 
71
   if (!m_lRenderers["local"]) {
 
72
      VideoResolution* res = VideoDeviceModel::instance()->activeDevice()->activeChannel()->activeResolution();
 
73
      if (!res) {
 
74
         qWarning() << "Misconfigured video device";
 
75
         return nullptr;
 
76
      }
 
77
      m_lRenderers["local"] = new VideoRenderer("local","",res->size());
 
78
   }
 
79
   return m_lRenderers["local"];
 
80
}
 
81
 
 
82
///Stop video preview
 
83
void VideoModel::stopPreview()
 
84
{
 
85
   VideoManagerInterface& interface = DBus::VideoManager::instance();
 
86
   interface.stopCamera();
 
87
   m_PreviewState = false;
 
88
}
 
89
 
 
90
///Start video preview
 
91
void VideoModel::startPreview()
 
92
{
 
93
   if (m_PreviewState) return;
 
94
   VideoManagerInterface& interface = DBus::VideoManager::instance();
 
95
   interface.startCamera();
 
96
   m_PreviewState = true;
 
97
}
 
98
 
 
99
///Is the video model fetching preview from a camera
 
100
bool VideoModel::isPreviewing()
 
101
{
 
102
   return m_PreviewState;
 
103
}
 
104
 
 
105
///@todo Set the video buffer size
 
106
void VideoModel::setBufferSize(uint size)
 
107
{
 
108
   m_BufferSize = size;
 
109
}
 
110
 
 
111
///Event callback
 
112
void VideoModel::deviceEvent()
 
113
{
 
114
   
 
115
}
 
116
 
 
117
///A video is not being rendered
 
118
void VideoModel::startedDecoding(const QString& id, const QString& shmPath, int width, int height)
 
119
{
 
120
   Q_UNUSED(id)
 
121
 
 
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();
 
127
//    }
 
128
//    else {
 
129
//       res =  QSize(width,height);
 
130
//    }
 
131
 
 
132
   if (m_lRenderers[id] == nullptr ) {
 
133
      m_lRenderers[id] = new VideoRenderer(id,shmPath,res);
 
134
      m_lRenderers[id]->moveToThread(this);
 
135
      if (!isRunning())
 
136
         start();
 
137
   }
 
138
   else {
 
139
      VideoRenderer* renderer = m_lRenderers[id];
 
140
      renderer->setShmPath(shmPath);
 
141
      renderer->setSize(res);
 
142
   }
 
143
 
 
144
   m_lRenderers[id]->startRendering();
 
145
   VideoDevice* dev = VideoDeviceModel::instance()->getDevice(id);
 
146
   if (dev) {
 
147
      emit dev->renderingStarted(m_lRenderers[id]);
 
148
   }
 
149
   if (id != "local") {
 
150
      qDebug() << "Starting video for call" << id;
 
151
      emit videoCallInitiated(m_lRenderers[id]);
 
152
   }
 
153
   else {
 
154
      m_PreviewState = true;
 
155
      emit previewStateChanged(true);
 
156
      emit previewStarted(m_lRenderers[id]);
 
157
   }
 
158
}
 
159
 
 
160
///A video stopped being rendered
 
161
void VideoModel::stoppedDecoding(const QString& id, const QString& shmPath)
 
162
{
 
163
   Q_UNUSED(shmPath)
 
164
   VideoRenderer* r = m_lRenderers[id];
 
165
   if ( r ) {
 
166
      r->stopRendering();
 
167
   }
 
168
   qDebug() << "Video stopped for call" << id <<  "Renderer found:" << (m_lRenderers[id] != nullptr);
 
169
//    emit videoStopped();
 
170
 
 
171
   VideoDevice* dev = VideoDeviceModel::instance()->getDevice(id);
 
172
   if (dev) {
 
173
      emit dev->renderingStopped(r);
 
174
   }
 
175
   if (id == "local") {
 
176
      m_PreviewState = false;
 
177
      emit previewStateChanged(false);
 
178
      emit previewStopped(r);
 
179
   }
 
180
//    r->mutex()->lock();
 
181
   m_lRenderers[id] = nullptr;
 
182
   delete r;
 
183
}
 
184
 
 
185
void VideoModel::switchDevice(const VideoDevice* device) const
 
186
{
 
187
   VideoManagerInterface& interface = DBus::VideoManager::instance();
 
188
   interface.switchInput(device->id());
 
189
}
 
190
 
 
191
QMutex* VideoModel::startStopMutex() const
 
192
{
 
193
   return m_SSMutex;
 
194
}
 
195