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