Reworked synchronous video recording stop
[harmattan/cameraplus] / lib / qtcamvideomode.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "qtcamvideomode.h"
22 #include "qtcammode_p.h"
23 #include <QDebug>
24 #include "qtcamdevice_p.h"
25 #include "qtcamdevice.h"
26 #include "qtcamvideosettings.h"
27 #include "qtcamnotifications.h"
28 #include <QMutex>
29 #include <QWaitCondition>
30
31 class QtCamVideoModePrivate : public QtCamModePrivate {
32 public:
33   QtCamVideoModePrivate(QtCamDevicePrivate *dev) :
34   QtCamModePrivate(dev),
35   settings(dev->conf->videoSettings(dev->id)),
36   resolution(settings->defaultResolution()) {
37
38   }
39
40   ~QtCamVideoModePrivate() {
41     delete settings;
42   }
43
44   void _d_idleStateChanged(bool isIdle) {
45     if (isIdle && dev->active == dev->video) {
46       QMetaObject::invokeMethod(dev->video, "recordingStateChanged");
47       QMetaObject::invokeMethod(dev->video, "canCaptureChanged");
48     }
49   }
50
51   QtCamVideoSettings *settings;
52   QtCamVideoResolution resolution;
53 };
54
55 class VideoDoneHandler : public DoneHandler {
56 public:
57   VideoDoneHandler(QtCamModePrivate *d, QObject *parent = 0) :
58     DoneHandler(d, "video-done", parent) {}
59
60   virtual void handleMessage(GstMessage *message) {
61     DoneHandler::handleMessage(message);
62
63     wake();
64   }
65
66   void wake() {
67     lock();
68     m_cond.wakeOne();
69     unlock();
70   }
71
72   void lock() {
73     m_mutex.lock();
74   }
75
76   void unlock() {
77     m_mutex.unlock();
78   }
79
80   void wait() {
81     m_cond.wait(&m_mutex);
82   }
83
84 private:
85   QMutex m_mutex;
86   QWaitCondition m_cond;
87 };
88
89 QtCamVideoMode::QtCamVideoMode(QtCamDevicePrivate *dev, QObject *parent) :
90   QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", parent) {
91
92   d_ptr->init(new VideoDoneHandler(d_ptr, this));
93
94   d = (QtCamVideoModePrivate *)QtCamMode::d_ptr;
95
96   QString name = d_ptr->dev->conf->videoEncodingProfileName();
97   QString path = d_ptr->dev->conf->videoEncodingProfilePath();
98
99   if (!name.isEmpty() && !path.isEmpty()) {
100     GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
101     if (profile) {
102       setProfile(profile);
103     }
104   }
105
106   QObject::connect(d_ptr->dev->q_ptr, SIGNAL(idleStateChanged(bool)),
107                    this, SLOT(_d_idleStateChanged(bool)));
108 }
109
110 QtCamVideoMode::~QtCamVideoMode() {
111   d = 0;
112 }
113
114 bool QtCamVideoMode::canCapture() {
115   return QtCamMode::canCapture() && d_ptr->dev->q_ptr->isIdle();
116 }
117
118 void QtCamVideoMode::applySettings() {
119   bool night = d_ptr->inNightMode();
120
121   int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
122
123   d_ptr->setCaps("viewfinder-caps", d->resolution.captureResolution(), fps);
124
125   d_ptr->setCaps("video-capture-caps", d->resolution.captureResolution(), fps);
126
127   d_ptr->setPreviewSize(d->resolution.previewResolution());
128
129   // Not sure this is needed but just in case.
130   d_ptr->resetCaps("image-capture-caps");
131 }
132
133 void QtCamVideoMode::start() {
134   d_ptr->disableViewfinderFilters();
135 }
136
137 void QtCamVideoMode::stop() {
138   if (isRecording()) {
139     stopRecording(true);
140   }
141 }
142
143 bool QtCamVideoMode::isRecording() {
144   return !d_ptr->dev->q_ptr->isIdle();
145 }
146
147 bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpFileName) {
148   if (!canCapture() || isRecording()) {
149     return false;
150   }
151
152   if (fileName.isEmpty()) {
153     return false;
154   }
155
156   d_ptr->setFileName(fileName);
157   d_ptr->setTempFileName(tmpFileName);
158
159   QString file = tmpFileName.isEmpty() ? fileName : tmpFileName;
160
161   QMetaObject::invokeMethod(d_ptr->dev->notifications, "videoRecordingStarted");
162
163   g_object_set(d_ptr->dev->cameraBin, "location", file.toUtf8().data(), NULL);
164   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
165
166   emit recordingStateChanged();
167
168   emit canCaptureChanged();
169
170   return true;
171 }
172
173 void QtCamVideoMode::stopRecording(bool sync) {
174   if (isRecording()) {
175     VideoDoneHandler *handler = dynamic_cast<VideoDoneHandler *>(d_ptr->doneHandler);
176     if (sync) {
177       handler->lock();
178     }
179
180     g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
181
182     if (sync) {
183       handler->wait();
184       handler->unlock();
185     }
186   }
187 }
188
189 bool QtCamVideoMode::setResolution(const QtCamVideoResolution& resolution) {
190   d->resolution = resolution;
191
192   if (!d_ptr->dev->q_ptr->isRunning()) {
193     // We will return true here because setting the resolution on a non-running pipeline
194     // doesn't make much sense (Probably the only use case is as a kind of optimization only).
195     // We will set it anyway when the pipeline gets started.
196     return true;
197   }
198
199   if (isRecording()) {
200     return false;
201   }
202
203   applySettings();
204
205   return true;
206 }
207
208 void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
209   if (!d_ptr->dev->cameraBin) {
210     gst_encoding_profile_unref(profile);
211     return;
212   }
213
214   g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
215 }
216
217 QtCamVideoSettings *QtCamVideoMode::settings() {
218   return d->settings;
219 }
220
221 #include "moc_qtcamvideomode.cpp"