Added a signal idleStateChanged() to QtCamDevice
[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 setAudioCaptureCaps() {
114     QString captureCaps = conf->audioCaptureCaps();
115     if (!captureCaps.isEmpty()) {
116       GstCaps *caps = gst_caps_from_string(captureCaps.toAscii().data());
117       if (caps) {
118         g_object_set(cameraBin, "audio-capture-caps", caps, NULL);
119         gst_caps_unref(caps);
120       }
121     }
122   }
123
124   static void on_idle_changed(GObject *obj, GParamSpec *pspec, QtCamDevicePrivate *d) {
125     Q_UNUSED(obj);
126     Q_UNUSED(pspec);
127
128     QMetaObject::invokeMethod(d->q_ptr, "idleStateChanged", Qt::QueuedConnection,
129                               Q_ARG(bool, d->q_ptr->isIdle()));
130   }
131
132   QString name;
133   QVariant id;
134
135   QtCamDevice *q_ptr;
136
137   GstElement *cameraBin;
138   GstElement *videoSource;
139   GstElement *wrapperVideoSource;
140
141   QtCamImageMode *image;
142   QtCamVideoMode *video;
143   QtCamMode *active;
144
145   QtCamViewfinder *viewfinder;
146   QtCamConfig *conf;
147   QtCamGStreamerMessageListener *listener;
148   bool error;
149 };
150
151 #endif /* QT_CAM_DEVICE_P_H */