Initialize viewfinderFilters to null
[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
33 class QtCamGStreamerMessageListener;
34 class QtCamMode;
35 class QtCamImageMode;
36 class QtCamVideoMode;
37 class QtCamPropertySetter;
38 class QtCamAnalysisBin;
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     viewfinderFilters(0) {
54
55   }
56
57   GstElement *createAndAddElement(const QString& elementName, const char *prop, const char *name) {
58     GstElement *elem = gst_element_factory_make(elementName.toAscii(), name);
59     if (!elem) {
60       qWarning() << "Failed to create" << elementName;
61       return 0;
62     }
63
64     g_object_set(cameraBin, prop, elem, NULL);
65
66     return elem;
67   }
68
69   void createAndAddVideoSourceAndWrapper() {
70     videoSource = gst_element_factory_make(conf->videoSource().toUtf8().constData(),
71                                            "QtCameraVideoSrc");
72     wrapperVideoSource = gst_element_factory_make(conf->wrapperVideoSource().toUtf8().constData(),
73                                                   "QCameraWrapperVideoSrc");
74
75     if (wrapperVideoSource && videoSource) {
76       g_object_set(wrapperVideoSource, conf->wrapperVideoSourceProperty().toUtf8().constData(),
77                    videoSource, NULL);
78       g_object_set(cameraBin, "camera-source", wrapperVideoSource, NULL);
79     }
80     else if (wrapperVideoSource) {
81       qWarning() << "Failed to create video source";
82       g_object_set(cameraBin, "camera-source", wrapperVideoSource, NULL);
83     }
84     else if (videoSource) {
85       qWarning() << "Failed to create wrapper source";
86       g_object_set(cameraBin, "camera-source", videoSource, NULL);
87     }
88     else {
89       qWarning() << "Failed to create both video and wrapper sources";
90     }
91   }
92
93   void createAndAddVideoSource() {
94     videoSource = gst_element_factory_make(conf->videoSource().toUtf8().constData(),
95                                            "QtCameraVideoSrc");
96     if (!videoSource) {
97       qCritical() << "Failed to create video source";
98     }
99     else {
100       g_object_set(cameraBin, "camera-source", videoSource, NULL);
101     }
102   }
103
104   bool setViewfinderSink() {
105     GstElement *sink = 0;
106     g_object_get(cameraBin, "viewfinder-sink", &sink, NULL);
107
108     if (sink) {
109       gst_object_unref(sink);
110       return true;
111     }
112
113     sink = viewfinder->sinkElement();
114     if (!sink) {
115       qCritical() << "Failed to create GStreamer sink element";
116       return false;
117     }
118
119     g_object_set(cameraBin, "viewfinder-sink", sink, NULL);
120
121     return true;
122   }
123
124   void _d_error(const QString& message, int code, const QString& debug) {
125     error = true;
126
127     QMetaObject::invokeMethod(q_ptr, "error", Q_ARG(QString, message),
128                               Q_ARG(int, code), Q_ARG(QString, debug));
129
130     if (active) {
131       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
132     }
133   }
134
135   void _d_stopped() {
136     QMetaObject::invokeMethod(q_ptr, "stopped");
137   }
138
139   void _d_stopping() {
140     if (active) {
141       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
142     }
143
144     QMetaObject::invokeMethod(q_ptr, "stopping", Qt::QueuedConnection);
145     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
146                               Q_ARG(bool, false));
147   }
148
149   void _d_started() {
150     if (active) {
151       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
152     }
153
154     QMetaObject::invokeMethod(q_ptr, "started", Qt::QueuedConnection);
155     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
156                               Q_ARG(bool, true));
157   }
158
159   void setAudioCaptureCaps() {
160     QString captureCaps = conf->audioCaptureCaps();
161     if (!captureCaps.isEmpty()) {
162       GstCaps *caps = gst_caps_from_string(captureCaps.toAscii().data());
163       if (caps) {
164         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
165         gst_caps_unref(caps);
166       }
167     }
168   }
169
170   bool isWrapperReady() {
171     if (!wrapperVideoSource) {
172       return false;
173     }
174
175     gboolean ready = FALSE;
176     g_object_get(wrapperVideoSource, "ready-for-capture", &ready, NULL);
177
178     return ready == TRUE;
179   }
180
181   static void on_ready_for_capture_changed(GObject *obj, GParamSpec *pspec,
182                                            QtCamDevicePrivate *d)  {
183     Q_UNUSED(obj);
184     Q_UNUSED(pspec);
185
186     if (!d->active) {
187       return;
188     }
189
190     QMetaObject::invokeMethod(d->active, "canCaptureChanged", Qt::QueuedConnection);
191   }
192
193   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
194     Q_UNUSED(obj);
195     Q_UNUSED(pspec);
196
197     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
198                               Q_ARG(bool, d->q_ptr->isIdle()));
199   }
200
201   QString name;
202   QVariant id;
203
204   QtCamDevice *q_ptr;
205
206   GstElement *cameraBin;
207   GstElement *videoSource;
208   GstElement *wrapperVideoSource;
209
210   QtCamImageMode *image;
211   QtCamVideoMode *video;
212   QtCamMode *active;
213
214   QtCamViewfinder *viewfinder;
215   QtCamConfig *conf;
216   QtCamGStreamerMessageListener *listener;
217   bool error;
218   QtCamNotifications *notifications;
219   QtCamPropertySetter *propertySetter;
220   QtCamAnalysisBin *viewfinderFilters;
221 };
222
223 #endif /* QT_CAM_DEVICE_P_H */