Reworked how video and wrapper sources are being created
[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 createAndAddVideoSourceAndWrapper() {
69     videoSource = gst_element_factory_make(conf->videoSource().toUtf8().constData(),
70                                            "QtCameraVideoSrc");
71     wrapperVideoSource = gst_element_factory_make(conf->wrapperVideoSource().toUtf8().constData(),
72                                                   "QCameraWrapperVideoSrc");
73
74     if (wrapperVideoSource && videoSource) {
75       g_object_set(wrapperVideoSource, conf->wrapperVideoSourceProperty().toUtf8().constData(),
76                    videoSource, NULL);
77       g_object_set(cameraBin, "camera-source", wrapperVideoSource, NULL);
78     }
79     else if (wrapperVideoSource) {
80       qWarning() << "Failed to create video source";
81       g_object_set(cameraBin, "camera-source", wrapperVideoSource, NULL);
82     }
83     else if (videoSource) {
84       qWarning() << "Failed to create wrapper source";
85       g_object_set(cameraBin, "camera-source", videoSource, NULL);
86     }
87     else {
88       qWarning() << "Failed to create both video and wrapper sources";
89     }
90   }
91
92   void createAndAddVideoSource() {
93     videoSource = gst_element_factory_make(conf->videoSource().toUtf8().constData(),
94                                            "QtCameraVideoSrc");
95     if (!videoSource) {
96       qCritical() << "Failed to create video source";
97     }
98     else {
99       g_object_set(cameraBin, "camera-source", videoSource, NULL);
100     }
101   }
102
103   bool setViewfinderSink() {
104     GstElement *sink;
105     g_object_get(cameraBin, "viewfinder-sink", &sink, NULL);
106
107     if (sink) {
108       gst_object_unref(sink);
109       return true;
110     }
111
112     sink = viewfinder->sinkElement();
113     if (!sink) {
114       qCritical() << "Failed to create GStreamer sink element";
115       return false;
116     }
117
118     g_object_set(cameraBin, "viewfinder-sink", sink, NULL);
119
120     return true;
121   }
122
123   void _d_error(const QString& message, int code, const QString& debug) {
124     error = true;
125
126     QMetaObject::invokeMethod(q_ptr, "error", Q_ARG(QString, message),
127                               Q_ARG(int, code), Q_ARG(QString, debug));
128
129     if (active) {
130       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
131     }
132   }
133
134   void _d_stopped() {
135     QMetaObject::invokeMethod(q_ptr, "stopped");
136   }
137
138   void _d_stopping() {
139     if (active) {
140       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
141     }
142
143     QMetaObject::invokeMethod(q_ptr, "stopping", Qt::QueuedConnection);
144     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
145                               Q_ARG(bool, false));
146   }
147
148   void _d_started() {
149     if (active) {
150       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
151     }
152
153     QMetaObject::invokeMethod(q_ptr, "started", Qt::QueuedConnection);
154     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
155                               Q_ARG(bool, true));
156   }
157
158   void setAudioCaptureCaps() {
159     QString captureCaps = conf->audioCaptureCaps();
160     if (!captureCaps.isEmpty()) {
161       GstCaps *caps = gst_caps_from_string(captureCaps.toAscii().data());
162       if (caps) {
163         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
164         gst_caps_unref(caps);
165       }
166     }
167   }
168
169   void addViewfinderFilters() {
170     addElements("viewfinder-filter", conf->viewfinderFilters());
171   }
172
173   bool isWrapperReady() {
174     if (!wrapperVideoSource) {
175       return false;
176     }
177
178     gboolean ready = FALSE;
179     g_object_get(wrapperVideoSource, "ready-for-capture", &ready, NULL);
180
181     return ready == TRUE;
182   }
183
184   static void on_ready_for_capture_changed(GObject *obj, GParamSpec *pspec,
185                                            QtCamDevicePrivate *d)  {
186     Q_UNUSED(obj);
187     Q_UNUSED(pspec);
188
189     if (!d->active) {
190       return;
191     }
192
193     QMetaObject::invokeMethod(d->active, "canCaptureChanged", Qt::QueuedConnection);
194   }
195
196   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
197     Q_UNUSED(obj);
198     Q_UNUSED(pspec);
199
200     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
201                               Q_ARG(bool, d->q_ptr->isIdle()));
202   }
203
204   void addElements(const char *prop, const QStringList& elements) {
205     QList<GstElement *> list;
206
207     if (elements.isEmpty()) {
208       return;
209     }
210
211     foreach (const QString& element, elements) {
212       GstElement *elem = gst_element_factory_make(element.toUtf8().constData(), NULL);
213       if (!elem) {
214         qWarning() << "Failed to create element" << element;
215       }
216       else {
217         list << elem;
218       }
219     }
220
221     if (list.isEmpty()) {
222       return;
223     }
224
225     GstElement *bin = qt_cam_analysis_bin_create(list, prop);
226     if (!bin) {
227       qWarning() << "Failed to create bin for" << prop;
228       return;
229     }
230
231     g_object_set(cameraBin, prop, bin, NULL);
232   }
233
234   QString name;
235   QVariant id;
236
237   QtCamDevice *q_ptr;
238
239   GstElement *cameraBin;
240   GstElement *videoSource;
241   GstElement *wrapperVideoSource;
242
243   QtCamImageMode *image;
244   QtCamVideoMode *video;
245   QtCamMode *active;
246
247   QtCamViewfinder *viewfinder;
248   QtCamConfig *conf;
249   QtCamGStreamerMessageListener *listener;
250   bool error;
251   QtCamNotifications *notifications;
252   QtCamPropertySetter *propertySetter;
253 };
254
255 #endif /* QT_CAM_DEVICE_P_H */