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