Added QtCamPropertySetter
[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 class QtCamPropertySetter;
39
40 class QtCamDevicePrivate {
41 public:
42   QtCamDevicePrivate() :
43     cameraBin(0),
44     videoSource(0),
45     wrapperVideoSource(0),
46     image(0),
47     video(0),
48     active(0),
49     viewfinder(0),
50     conf(0),
51     error(false),
52     notifications(0) {
53
54   }
55
56   GstElement *createAndAddElement(const QString& elementName, const char *prop, const char *name) {
57     GstElement *elem = gst_element_factory_make(elementName.toAscii(), name);
58     if (!elem) {
59       qWarning() << "Failed to create" << elementName;
60       return 0;
61     }
62
63     g_object_set(cameraBin, prop, elem, NULL);
64
65     return elem;
66   }
67
68   void createAndAddVideoSource() {
69     // TODO: rework this function
70     GstElement *src, *wrapper;
71     QString wrapperSrc = conf->wrapperVideoSource();
72     QString prop = conf->wrapperVideoSourceProperty();
73
74     if (!prop.isEmpty() && !wrapperSrc.isEmpty()) {
75       wrapper = gst_element_factory_make(wrapperSrc.toAscii(), "QCameraWrapperVideoSrc");
76       if (!wrapper) {
77         qCritical() << "Failed to create wrapper source" << wrapperSrc;
78         return;
79       }
80     }
81
82     src = gst_element_factory_make(conf->videoSource().toAscii(),
83                                                "QtCameraVideoSrc");
84     if (!src) {
85       qCritical() << "Failed to create video source";
86       if (wrapper) {
87         gst_object_unref(wrapper);
88       }
89       return;
90     }
91
92     if (wrapper) {
93       g_object_set(wrapper, prop.toAscii(), src, NULL);
94       g_object_set(cameraBin, "camera-source", wrapper, NULL);
95     }
96
97     videoSource = src;
98     wrapperVideoSource = wrapper;
99
100     if (!id.isValid() || id.isNull()) {
101       return;
102     }
103
104     if (conf->deviceScannerType() == SCANNER_TYPE_ENUM) {
105       int dev = id.toInt();
106       g_object_set(src, conf->deviceScannerProperty().toAscii().data(), dev, NULL);
107     }
108     else {
109       QString dev = id.toString();
110       g_object_set(src, conf->deviceScannerProperty().toAscii().data(),
111                    dev.toAscii().data(), NULL);
112     }
113   }
114
115   bool setViewfinderSink() {
116     GstElement *sink;
117     g_object_get(cameraBin, "viewfinder-sink", &sink, NULL);
118
119     if (sink) {
120       gst_object_unref(sink);
121       return true;
122     }
123
124     sink = viewfinder->sinkElement();
125     if (!sink) {
126       qCritical() << "Failed to create GStreamer sink element";
127       return false;
128     }
129
130     g_object_set(cameraBin, "viewfinder-sink", sink, NULL);
131
132     return true;
133   }
134
135   void _d_error(const QString& message, int code, const QString& debug) {
136     error = true;
137
138     QMetaObject::invokeMethod(q_ptr, "error", Q_ARG(QString, message),
139                               Q_ARG(int, code), Q_ARG(QString, debug));
140
141     if (active) {
142       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
143     }
144   }
145
146   void _d_stopped() {
147     QMetaObject::invokeMethod(q_ptr, "stopped");
148   }
149
150   void _d_stopping() {
151     if (active) {
152       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
153     }
154
155     QMetaObject::invokeMethod(q_ptr, "stopping", Qt::QueuedConnection);
156     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
157                               Q_ARG(bool, false));
158   }
159
160   void _d_started() {
161     if (active) {
162       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
163     }
164
165     QMetaObject::invokeMethod(q_ptr, "started", Qt::QueuedConnection);
166     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
167                               Q_ARG(bool, true));
168   }
169
170   void setAudioCaptureCaps() {
171     QString captureCaps = conf->audioCaptureCaps();
172     if (!captureCaps.isEmpty()) {
173       GstCaps *caps = gst_caps_from_string(captureCaps.toAscii().data());
174       if (caps) {
175         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
176         gst_caps_unref(caps);
177       }
178     }
179   }
180
181   void addViewfinderFilters() {
182     addElements("viewfinder-filter", conf->viewfinderFilters());
183   }
184
185   bool isWrapperReady() {
186     if (!wrapperVideoSource) {
187       return false;
188     }
189
190     gboolean ready = FALSE;
191     g_object_get(wrapperVideoSource, "ready-for-capture", &ready, NULL);
192
193     return ready == TRUE;
194   }
195
196   static void on_ready_for_capture_changed(GObject *obj, GParamSpec *pspec,
197                                            QtCamDevicePrivate *d)  {
198     Q_UNUSED(obj);
199     Q_UNUSED(pspec);
200
201     if (!d->active) {
202       return;
203     }
204
205     QMetaObject::invokeMethod(d->active, "canCaptureChanged", Qt::QueuedConnection);
206   }
207
208   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
209     Q_UNUSED(obj);
210     Q_UNUSED(pspec);
211
212     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
213                               Q_ARG(bool, d->q_ptr->isIdle()));
214   }
215
216   void addElements(const char *prop, const QStringList& elements) {
217     QList<GstElement *> list;
218
219     if (elements.isEmpty()) {
220       return;
221     }
222
223     foreach (const QString& element, elements) {
224       GstElement *elem = gst_element_factory_make(element.toUtf8().constData(), NULL);
225       if (!elem) {
226         qWarning() << "Failed to create element" << element;
227       }
228       else {
229         list << elem;
230       }
231     }
232
233     if (list.isEmpty()) {
234       return;
235     }
236
237     GstElement *bin = qt_cam_analysis_bin_create(list, prop);
238     if (!bin) {
239       qWarning() << "Failed to create bin for" << prop;
240       return;
241     }
242
243     g_object_set(cameraBin, prop, bin, NULL);
244   }
245
246 #if 0
247   void resetCapabilities() {
248     g_object_set(cameraBin, "zoom", 1.0, NULL);
249     g_object_set(videoSource, "flash-mode", 0, NULL);
250
251     // TODO: should scene mode be manual or automatic ?
252     g_object_set(videoSource, "scene-mode", 0, NULL);
253   }
254 #endif
255
256   QString name;
257   QVariant id;
258
259   QtCamDevice *q_ptr;
260
261   GstElement *cameraBin;
262   GstElement *videoSource;
263   GstElement *wrapperVideoSource;
264
265   QtCamImageMode *image;
266   QtCamVideoMode *video;
267   QtCamMode *active;
268
269   QtCamViewfinder *viewfinder;
270   QtCamConfig *conf;
271   QtCamGStreamerMessageListener *listener;
272   bool error;
273   QtCamNotifications *notifications;
274   QtCamPropertySetter *propertySetter;
275 };
276
277 #endif /* QT_CAM_DEVICE_P_H */