Set viewfinder-filters
[harmattan/cameraplus] / lib / qtcamdevice_p.h
1 // -*- c++ -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef QT_CAM_DEVICE_P_H
24 #define QT_CAM_DEVICE_P_H
25
26 #include <QDebug>
27 #include <gst/gst.h>
28 #include "qtcamconfig.h"
29 #include "qtcamviewfinder.h"
30 #include "qtcamdevice.h"
31 #include "qtcammode.h"
32 #include "qtcamanalysisbin.h"
33
34 class QtCamGStreamerMessageListener;
35 class QtCamMode;
36 class QtCamImageMode;
37 class QtCamVideoMode;
38
39 class QtCamDevicePrivate {
40 public:
41   QtCamDevicePrivate() :
42     cameraBin(0),
43     videoSource(0),
44     wrapperVideoSource(0),
45     image(0),
46     video(0),
47     active(0),
48     viewfinder(0),
49     conf(0),
50     error(false),
51     notifications(0) {
52
53   }
54
55   GstElement *createAndAddElement(const QString& elementName, const char *prop, const char *name) {
56     GstElement *elem = gst_element_factory_make(elementName.toAscii(), name);
57     if (!elem) {
58       qWarning() << "Failed to create" << elementName;
59       return 0;
60     }
61
62     g_object_set(cameraBin, prop, elem, NULL);
63
64     return elem;
65   }
66
67   void createAndAddVideoSource() {
68     // TODO: rework this function
69     GstElement *src, *wrapper;
70     QString wrapperSrc = conf->wrapperVideoSource();
71     QString prop = conf->wrapperVideoSourceProperty();
72
73     if (!prop.isEmpty() && !wrapperSrc.isEmpty()) {
74       wrapper = gst_element_factory_make(wrapperSrc.toAscii(), "QCameraWrapperVideoSrc");
75       if (!wrapper) {
76         qCritical() << "Failed to create wrapper source" << wrapperSrc;
77         return;
78       }
79     }
80
81     src = gst_element_factory_make(conf->videoSource().toAscii(),
82                                                "QtCameraVideoSrc");
83     if (!src) {
84       qCritical() << "Failed to create video source";
85       if (wrapper) {
86         gst_object_unref(wrapper);
87       }
88       return;
89     }
90
91     if (wrapper) {
92       g_object_set(wrapper, prop.toAscii(), src, NULL);
93       g_object_set(cameraBin, "camera-source", wrapper, NULL);
94     }
95
96     videoSource = src;
97     wrapperVideoSource = wrapper;
98
99     if (!id.isValid() || id.isNull()) {
100       return;
101     }
102
103     if (conf->deviceScannerType() == SCANNER_TYPE_ENUM) {
104       int dev = id.toInt();
105       g_object_set(src, conf->deviceScannerProperty().toAscii().data(), dev, NULL);
106     }
107     else {
108       QString dev = id.toString();
109       g_object_set(src, conf->deviceScannerProperty().toAscii().data(),
110                    dev.toAscii().data(), NULL);
111     }
112   }
113
114   bool setViewfinderSink() {
115     GstElement *sink;
116     g_object_get(cameraBin, "viewfinder-sink", &sink, NULL);
117
118     if (sink) {
119       gst_object_unref(sink);
120       return true;
121     }
122
123     sink = viewfinder->sinkElement();
124     if (!sink) {
125       qCritical() << "Failed to create GStreamer sink element";
126       return false;
127     }
128
129     g_object_set(cameraBin, "viewfinder-sink", sink, NULL);
130
131     return true;
132   }
133
134   void _d_error(const QString& message, int code, const QString& debug) {
135     error = true;
136
137     QMetaObject::invokeMethod(q_ptr, "error", Q_ARG(QString, message),
138                               Q_ARG(int, code), Q_ARG(QString, debug));
139
140     if (active) {
141       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
142     }
143   }
144
145   void _d_stopped() {
146     QMetaObject::invokeMethod(q_ptr, "stopped");
147   }
148
149   void _d_stopping() {
150     if (active) {
151       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
152     }
153
154     QMetaObject::invokeMethod(q_ptr, "stopping", Qt::QueuedConnection);
155     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
156                               Q_ARG(bool, false));
157   }
158
159   void _d_started() {
160     if (active) {
161       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
162     }
163
164     QMetaObject::invokeMethod(q_ptr, "started", Qt::QueuedConnection);
165     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
166                               Q_ARG(bool, true));
167   }
168
169   void setAudioCaptureCaps() {
170     QString captureCaps = conf->audioCaptureCaps();
171     if (!captureCaps.isEmpty()) {
172       GstCaps *caps = gst_caps_from_string(captureCaps.toAscii().data());
173       if (caps) {
174         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
175         gst_caps_unref(caps);
176       }
177     }
178   }
179
180   void addViewfinderFilters() {
181     addElements("viewfinder-filter", conf->viewfinderFilters());
182   }
183
184   bool isWrapperReady() {
185     if (!wrapperVideoSource) {
186       return false;
187     }
188
189     gboolean ready = FALSE;
190     g_object_get(wrapperVideoSource, "ready-for-capture", &ready, NULL);
191
192     return ready == TRUE;
193   }
194
195   static void on_ready_for_capture_changed(GObject *obj, GParamSpec *pspec,
196                                            QtCamDevicePrivate *d)  {
197     Q_UNUSED(obj);
198     Q_UNUSED(pspec);
199
200     if (!d->active) {
201       return;
202     }
203
204     QMetaObject::invokeMethod(d->active, "canCaptureChanged", Qt::QueuedConnection);
205   }
206
207   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
208     Q_UNUSED(obj);
209     Q_UNUSED(pspec);
210
211     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
212                               Q_ARG(bool, d->q_ptr->isIdle()));
213   }
214
215   void addElements(const char *prop, const QStringList& elements) {
216     QList<GstElement *> list;
217
218     if (elements.isEmpty()) {
219       return;
220     }
221
222     foreach (const QString& element, elements) {
223       GstElement *elem = gst_element_factory_make(element.toUtf8().constData(), NULL);
224       if (!elem) {
225         qWarning() << "Failed to create element" << element;
226       }
227       else {
228         list << elem;
229       }
230     }
231
232     if (list.isEmpty()) {
233       return;
234     }
235
236     GstElement *bin = qt_cam_analysis_bin_create(list, prop);
237     if (!bin) {
238       qWarning() << "Failed to create bin for" << prop;
239       return;
240     }
241
242     g_object_set(cameraBin, prop, bin, NULL);
243   }
244
245 #if 0
246   void resetCapabilities() {
247     g_object_set(cameraBin, "zoom", 1.0, NULL);
248     g_object_set(videoSource, "flash-mode", 0, NULL);
249
250     // TODO: should scene mode be manual or automatic ?
251     g_object_set(videoSource, "scene-mode", 0, NULL);
252   }
253 #endif
254
255   QString name;
256   QVariant id;
257
258   QtCamDevice *q_ptr;
259
260   GstElement *cameraBin;
261   GstElement *videoSource;
262   GstElement *wrapperVideoSource;
263
264   QtCamImageMode *image;
265   QtCamVideoMode *video;
266   QtCamMode *active;
267
268   QtCamViewfinder *viewfinder;
269   QtCamConfig *conf;
270   QtCamGStreamerMessageListener *listener;
271   bool error;
272   QtCamNotifications *notifications;
273 };
274
275 #endif /* QT_CAM_DEVICE_P_H */