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