3334cb433e0f00b44e9267e0978681ce3011a088
[harmattan/cameraplus] / lib / qtcamdevice_p.h
1 // -*- c++ -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012-2013 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.toLatin1(), 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   void setDevicePoperty() {
105     if (videoSource) {
106       if (conf->deviceScannerType() == SCANNER_TYPE_ENUM) {
107         int dev = id.toInt();
108         g_object_set(videoSource, conf->deviceScannerProperty().toLatin1().constData(), dev, NULL);
109       }
110       else {
111         QString dev = id.toString();
112         g_object_set(videoSource, conf->deviceScannerProperty().toLatin1().constData(),
113                      dev.toLatin1().constData(), NULL);
114       }
115     }
116   }
117
118   bool setViewfinderSink() {
119     GstElement *sink = 0;
120     g_object_get(cameraBin, "viewfinder-sink", &sink, NULL);
121
122     if (sink) {
123       gst_object_unref(sink);
124       return true;
125     }
126
127     sink = viewfinder->sinkElement();
128     if (!sink) {
129       qCritical() << "Failed to create GStreamer sink element";
130       return false;
131     }
132
133     g_object_set(cameraBin, "viewfinder-sink", sink, NULL);
134
135     return true;
136   }
137
138   void _d_error(const QString& message, int code, const QString& debug) {
139     error = true;
140
141     QMetaObject::invokeMethod(q_ptr, "error", Q_ARG(QString, message),
142                               Q_ARG(int, code), Q_ARG(QString, debug));
143
144     if (active) {
145       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
146     }
147   }
148
149   void _d_stopped() {
150     QMetaObject::invokeMethod(q_ptr, "stopped");
151   }
152
153   void _d_stopping() {
154     if (active) {
155       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
156     }
157
158     QMetaObject::invokeMethod(q_ptr, "stopping", Qt::QueuedConnection);
159     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
160                               Q_ARG(bool, false));
161   }
162
163   void _d_started() {
164     if (active) {
165       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
166     }
167
168     QMetaObject::invokeMethod(q_ptr, "started", Qt::QueuedConnection);
169     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
170                               Q_ARG(bool, true));
171   }
172
173   void setAudioCaptureCaps() {
174     QString captureCaps = conf->audioCaptureCaps();
175     if (!captureCaps.isEmpty()) {
176       GstCaps *caps = gst_caps_from_string(captureCaps.toLatin1().data());
177       if (caps) {
178         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
179         gst_caps_unref(caps);
180       }
181     }
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   QString name;
216   QVariant id;
217
218   QtCamDevice *q_ptr;
219
220   GstElement *cameraBin;
221   GstElement *videoSource;
222   GstElement *wrapperVideoSource;
223
224   QtCamImageMode *image;
225   QtCamVideoMode *video;
226   QtCamMode *active;
227
228   QtCamViewfinder *viewfinder;
229   QtCamConfig *conf;
230   QtCamGStreamerMessageListener *listener;
231   bool error;
232   QtCamNotifications *notifications;
233   QtCamPropertySetter *propertySetter;
234   QtCamAnalysisBin *viewfinderFilters;
235 };
236
237 #endif /* QT_CAM_DEVICE_P_H */