Added sync parameter to QtCamVideo::stopRecording()
[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 "qtcamgstreamermessagelistener.h"
29
30 class QtCamVideoModePrivate : public QtCamModePrivate {
31 public:
32   QtCamVideoModePrivate(QtCamDevicePrivate *dev) :
33   QtCamModePrivate(dev),
34   settings(dev->conf->videoSettings(dev->id)),
35   resolution(settings->defaultResolution()) {
36
37   }
38
39   ~QtCamVideoModePrivate() {
40     delete settings;
41   }
42
43   void _d_idleStateChanged(bool isIdle) {
44     if (isIdle && dev->active == dev->video) {
45       QMetaObject::invokeMethod(dev->video, "recordingStateChanged");
46       QMetaObject::invokeMethod(dev->video, "canCaptureChanged");
47     }
48   }
49
50   QtCamVideoSettings *settings;
51   QtCamVideoResolution resolution;
52 };
53
54 QtCamVideoMode::QtCamVideoMode(QtCamDevicePrivate *dev, QObject *parent) :
55   QtCamMode(new QtCamVideoModePrivate(dev), "mode-video", "video-done", parent) {
56
57   d = (QtCamVideoModePrivate *)QtCamMode::d_ptr;
58
59   QString name = d_ptr->dev->conf->videoEncodingProfileName();
60   QString path = d_ptr->dev->conf->videoEncodingProfilePath();
61
62   if (!name.isEmpty() && !path.isEmpty()) {
63     GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
64     if (profile) {
65       setProfile(profile);
66     }
67   }
68
69   QObject::connect(d_ptr->dev->q_ptr, SIGNAL(idleStateChanged(bool)),
70                    this, SLOT(_d_idleStateChanged(bool)));
71 }
72
73 QtCamVideoMode::~QtCamVideoMode() {
74   d = 0;
75 }
76
77 bool QtCamVideoMode::canCapture() {
78   return QtCamMode::canCapture() && d_ptr->dev->q_ptr->isIdle();
79 }
80
81 void QtCamVideoMode::applySettings() {
82   bool night = d_ptr->inNightMode();
83
84   int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
85
86   d_ptr->setCaps("viewfinder-caps", d->resolution.captureResolution(), fps);
87
88   d_ptr->setCaps("video-capture-caps", d->resolution.captureResolution(), fps);
89
90   d_ptr->setPreviewSize(d->resolution.previewResolution());
91
92   // Not sure this is needed but just in case.
93   d_ptr->resetCaps("image-capture-caps");
94 }
95
96 void QtCamVideoMode::start() {
97   d_ptr->disableViewfinderFilters();
98 }
99
100 void QtCamVideoMode::stop() {
101   if (isRecording()) {
102     stopRecording(true);
103   }
104 }
105
106 bool QtCamVideoMode::isRecording() {
107   return !d_ptr->dev->q_ptr->isIdle();
108 }
109
110 bool QtCamVideoMode::startRecording(const QString& fileName, const QString& tmpFileName) {
111   if (!canCapture() || isRecording()) {
112     return false;
113   }
114
115   if (fileName.isEmpty()) {
116     return false;
117   }
118
119   d_ptr->setFileName(fileName);
120   d_ptr->setTempFileName(tmpFileName);
121
122   QString file = tmpFileName.isEmpty() ? fileName : tmpFileName;
123
124   QMetaObject::invokeMethod(d_ptr->dev->notifications, "videoRecordingStarted");
125
126   g_object_set(d_ptr->dev->cameraBin, "location", file.toUtf8().data(), NULL);
127   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
128
129   emit recordingStateChanged();
130
131   emit canCaptureChanged();
132
133   return true;
134 }
135
136 void QtCamVideoMode::stopRecording(bool sync) {
137   if (isRecording()) {
138     g_signal_emit_by_name(d_ptr->dev->cameraBin, "stop-capture", NULL);
139
140     if (sync) {
141       GstMessage *msg = d_ptr->dev->listener->waitForMessage(QLatin1String("video-done"));
142       if (msg) {
143         gst_message_unref(msg);
144       }
145     }
146   }
147 }
148
149 bool QtCamVideoMode::setResolution(const QtCamVideoResolution& resolution) {
150   d->resolution = resolution;
151
152   if (!d_ptr->dev->q_ptr->isRunning()) {
153     // We will return true here because setting the resolution on a non-running pipeline
154     // doesn't make much sense (Probably the only use case is as a kind of optimization only).
155     // We will set it anyway when the pipeline gets started.
156     return true;
157   }
158
159   if (isRecording()) {
160     return false;
161   }
162
163   applySettings();
164
165   return true;
166 }
167
168 void QtCamVideoMode::setProfile(GstEncodingProfile *profile) {
169   if (!d_ptr->dev->cameraBin) {
170     gst_encoding_profile_unref(profile);
171     return;
172   }
173
174   g_object_set(d_ptr->dev->cameraBin, "video-profile", profile, NULL);
175 }
176
177 QtCamVideoSettings *QtCamVideoMode::settings() {
178   return d->settings;
179 }
180
181 #include "moc_qtcamvideomode.cpp"