Updated copyright year
[harmattan/cameraplus] / lib / qtcamimagemode.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 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 "qtcamimagemode.h"
22 #include "qtcammode_p.h"
23 #include "qtcamdevice_p.h"
24 #include "qtcamimagesettings.h"
25 #include "qtcamdevice.h"
26
27 class QtCamImageModePrivate : public QtCamModePrivate {
28 public:
29   QtCamImageModePrivate(QtCamDevicePrivate *dev) :
30   QtCamModePrivate(dev),
31   settings(dev->conf->imageSettings(dev->id)),
32   resolution(settings->defaultResolution()) {
33
34   }
35
36   ~QtCamImageModePrivate() {
37     delete settings;
38   }
39
40   QtCamImageSettings *settings;
41   QtCamImageResolution resolution;
42 };
43
44 QtCamImageMode::QtCamImageMode(QtCamDevicePrivate *dev, QObject *parent) :
45   QtCamMode(new QtCamImageModePrivate(dev), "mode-image", parent) {
46
47   d_ptr->init(new DoneHandler(d_ptr, "image-done", this));
48
49   d = (QtCamImageModePrivate *)d_ptr;
50
51   QString name = d_ptr->dev->conf->imageEncodingProfileName();
52   QString path = d_ptr->dev->conf->imageEncodingProfilePath();
53
54   if (!name.isEmpty() && !path.isEmpty()) {
55     GstEncodingProfile *profile = d_ptr->loadProfile(path, name);
56     if (profile) {
57       setProfile(profile);
58     }
59   }
60 }
61
62 QtCamImageMode::~QtCamImageMode() {
63   d = 0;
64 }
65
66 bool QtCamImageMode::canCapture() {
67   return QtCamMode::canCapture() && d_ptr->dev->isWrapperReady();
68 }
69
70 void QtCamImageMode::applySettings() {
71   bool night = d_ptr->inNightMode();
72
73   int fps = night ? d->resolution.nightFrameRate() : d->resolution.frameRate();
74
75   d_ptr->setCaps("viewfinder-caps", d->resolution.viewfinderResolution(), fps);
76
77   // FIXME:
78   // Ideally, we should query the image-capture-supported-caps and get a proper framerate
79   // as it seems that subdevsrc2 can only capture 15 FPS for at least the highest resolution
80   // we use. For now we will not set any FPS.
81   d_ptr->setCaps("image-capture-caps", d->resolution.captureResolution(), -1);
82
83   d_ptr->setPreviewSize(d->resolution.previewResolution());
84
85   // If we don't reset the caps then: if we switch from video to image then we fail
86   // the next time we restart the pipeline.
87   d_ptr->resetCaps("video-capture-caps");
88 }
89
90 void QtCamImageMode::start() {
91   d_ptr->enableViewfinderFilters();
92 }
93
94 void QtCamImageMode::stop() {
95   // Nothing
96 }
97
98 bool QtCamImageMode::capture(const QString& fileName) {
99   if (!canCapture()) {
100     return false;
101   }
102
103   if (fileName.isEmpty()) {
104     return false;
105   }
106
107   d_ptr->setFileName(fileName);
108
109   g_object_set(d_ptr->dev->cameraBin, "location", fileName.toUtf8().data(), NULL);
110   g_signal_emit_by_name(d_ptr->dev->cameraBin, "start-capture", NULL);
111
112   return true;
113 }
114
115 bool QtCamImageMode::setResolution(const QtCamImageResolution& resolution) {
116   d->resolution = resolution;
117
118   if (!d_ptr->dev->q_ptr->isRunning()) {
119     // We will return true here because setting the resolution on a non-running pipeline
120     // doesn't make much sense (Probably the only use case is as a kind of optimization only).
121     // We will set it anyway when the pipeline gets started.
122     return true;
123   }
124
125   if (!d_ptr->dev->q_ptr->isIdle()) {
126     return false;
127   }
128
129   applySettings();
130
131   return true;
132 }
133
134 void QtCamImageMode::setProfile(GstEncodingProfile *profile) {
135   if (!d_ptr->dev->cameraBin) {
136     gst_encoding_profile_unref(profile);
137     return;
138   }
139
140   g_object_set(d_ptr->dev->cameraBin, "image-profile", profile, NULL);
141 }
142
143 QtCamImageSettings *QtCamImageMode::settings() const {
144   return d->settings;
145 }