Moved wrapperIsReady to QtCamDevicePrivate and renamed to isWrapperReady
[harmattan/cameraplus] / lib / qtcamdevice_p.h
1 // -*- c++ -*-
2
3 #ifndef QT_CAM_DEVICE_P_H
4 #define QT_CAM_DEVICE_P_H
5
6 #include <QDebug>
7 #include <gst/gst.h>
8 #include "qtcamconfig.h"
9 #include "qtcamviewfinder.h"
10 #include "qtcamdevice.h"
11
12 class QtCamGStreamerMessageListener;
13 class QtCamMode;
14 class QtCamImageMode;
15 class QtCamVideoMode;
16
17 class QtCamDevicePrivate {
18 public:
19   QtCamDevicePrivate() :
20     cameraBin(0),
21     videoSource(0),
22     wrapperVideoSource(0),
23     image(0),
24     video(0),
25     active(0),
26     viewfinder(0),
27     conf(0),
28     error(false) {
29
30   }
31
32   GstElement *createAndAddElement(const QString& elementName, const char *prop, const char *name) {
33     GstElement *elem = gst_element_factory_make(elementName.toAscii(), name);
34     if (!elem) {
35       qWarning() << "Failed to create" << elementName;
36       return 0;
37     }
38
39     g_object_set(cameraBin, prop, elem, NULL);
40
41     return elem;
42   }
43
44   void createAndAddVideoSource() {
45     GstElement *src, *wrapper;
46     QString wrapperSrc = conf->wrapperVideoSource();
47     QString prop = conf->wrapperVideoSourceProperty();
48
49     if (!prop.isEmpty() && !wrapperSrc.isEmpty()) {
50       wrapper = gst_element_factory_make(wrapperSrc.toAscii(), "QCameraWrapperVideoSrc");
51       if (!wrapper) {
52         qCritical() << "Failed to create wrapper source" << wrapperSrc;
53         return;
54       }
55     }
56
57     src = gst_element_factory_make(conf->videoSource().toAscii(),
58                                                "QtCameraVideoSrc");
59     if (!src) {
60       qCritical() << "Failed to create video source";
61       if (wrapper) {
62         gst_object_unref(wrapper);
63       }
64       return;
65     }
66
67     if (wrapper) {
68       g_object_set(wrapper, prop.toAscii(), src, NULL);
69       g_object_set(cameraBin, "camera-source", wrapper, NULL);
70     }
71
72     videoSource = src;
73     wrapperVideoSource = wrapper;
74
75     if (conf->deviceScannerType() == SCANNER_TYPE_ENUM) {
76       int dev = id.toInt();
77       g_object_set(src, conf->deviceScannerProperty().toAscii().data(), dev, NULL);
78     }
79     else {
80       QString dev = id.toString();
81       g_object_set(src, conf->deviceScannerProperty().toAscii().data(),
82                    dev.toAscii().data(), NULL);
83     }
84   }
85
86   bool setViewfinderSink() {
87     GstElement *sink;
88     g_object_get(cameraBin, "viewfinder-sink", &sink, NULL);
89
90     if (sink) {
91       gst_object_unref(sink);
92       return true;
93     }
94
95     sink = viewfinder->sinkElement();
96     if (!sink) {
97       qCritical() << "Failed to create GStreamer sink element";
98       return false;
99     }
100
101     g_object_set(cameraBin, "viewfinder-sink", sink, NULL);
102
103     return true;
104   }
105
106   void _d_error(const QString& message, int code, const QString& debug) {
107     error = true;
108
109     QMetaObject::invokeMethod(q_ptr, "error", Q_ARG(QString, message),
110                               Q_ARG(int, code), Q_ARG(QString, debug));
111   }
112
113   void _d_stopped() {
114     QMetaObject::invokeMethod(q_ptr, "stopped");
115   }
116
117   void _d_stopping() {
118     if (active) {
119       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
120     }
121
122     QMetaObject::invokeMethod(q_ptr, "stopping", Qt::QueuedConnection);
123     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
124                               Q_ARG(bool, false));
125   }
126
127   void _d_started() {
128     if (active) {
129       QMetaObject::invokeMethod(active, "canCaptureChanged", Qt::QueuedConnection);
130     }
131
132     QMetaObject::invokeMethod(q_ptr, "started", Qt::QueuedConnection);
133     QMetaObject::invokeMethod(q_ptr, "runningStateChanged", Qt::QueuedConnection,
134                               Q_ARG(bool, true));
135   }
136
137   void setAudioCaptureCaps() {
138     QString captureCaps = conf->audioCaptureCaps();
139     if (!captureCaps.isEmpty()) {
140       GstCaps *caps = gst_caps_from_string(captureCaps.toAscii().data());
141       if (caps) {
142         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
143         gst_caps_unref(caps);
144       }
145     }
146   }
147
148   bool isWrapperReady() {
149     if (!wrapperVideoSource) {
150       return false;
151     }
152
153     gboolean ready = FALSE;
154     g_object_get(wrapperVideoSource, "ready-for-capture", &ready, NULL);
155
156     return ready == TRUE;
157   }
158
159   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
160     Q_UNUSED(obj);
161     Q_UNUSED(pspec);
162
163     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
164                               Q_ARG(bool, d->q_ptr->isIdle()));
165   }
166
167   QString name;
168   QVariant id;
169
170   QtCamDevice *q_ptr;
171
172   GstElement *cameraBin;
173   GstElement *videoSource;
174   GstElement *wrapperVideoSource;
175
176   QtCamImageMode *image;
177   QtCamVideoMode *video;
178   QtCamMode *active;
179
180   QtCamViewfinder *viewfinder;
181   QtCamConfig *conf;
182   QtCamGStreamerMessageListener *listener;
183   bool error;
184 };
185
186 #endif /* QT_CAM_DEVICE_P_H */