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