Reworked QtCamAnalysisBin
[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
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   bool isWrapperReady() {
170     if (!wrapperVideoSource) {
171       return false;
172     }
173
174     gboolean ready = FALSE;
175     g_object_get(wrapperVideoSource, "ready-for-capture", &ready, NULL);
176
177     return ready == TRUE;
178   }
179
180   static void on_ready_for_capture_changed(GObject *obj, GParamSpec *pspec,
181                                            QtCamDevicePrivate *d)  {
182     Q_UNUSED(obj);
183     Q_UNUSED(pspec);
184
185     if (!d->active) {
186       return;
187     }
188
189     QMetaObject::invokeMethod(d->active, "canCaptureChanged", Qt::QueuedConnection);
190   }
191
192   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
193     Q_UNUSED(obj);
194     Q_UNUSED(pspec);
195
196     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
197                               Q_ARG(bool, d->q_ptr->isIdle()));
198   }
199
200   QString name;
201   QVariant id;
202
203   QtCamDevice *q_ptr;
204
205   GstElement *cameraBin;
206   GstElement *videoSource;
207   GstElement *wrapperVideoSource;
208
209   QtCamImageMode *image;
210   QtCamVideoMode *video;
211   QtCamMode *active;
212
213   QtCamViewfinder *viewfinder;
214   QtCamConfig *conf;
215   QtCamGStreamerMessageListener *listener;
216   bool error;
217   QtCamNotifications *notifications;
218   QtCamPropertySetter *propertySetter;
219   QtCamAnalysisBin *viewfinderFilters;
220 };
221
222 #endif /* QT_CAM_DEVICE_P_H */