Convert TextSwitch to a platform specific component and rename it to CameraTextSwitch
[harmattan/cameraplus] / lib / qtcamdevice.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "qtcamdevice.h"
22 #include "qtcamviewfinder.h"
23 #include "qtcamconfig.h"
24 #include "qtcamdevice_p.h"
25 #include <QDebug>
26 #include <gst/gst.h>
27 #include "qtcamgstreamermessagelistener.h"
28 #include "qtcammode.h"
29 #include "qtcamimagemode.h"
30 #include "qtcamvideomode.h"
31 #include "qtcamnotifications.h"
32 #include "gst/gstcopy.h"
33 #include "qtcampropertysetter.h"
34 #include "qtcamanalysisbin.h"
35
36 QtCamDevice::QtCamDevice(QtCamConfig *config, const QString& name,
37                          const QVariant& id, QObject *parent) :
38   QObject(parent), d_ptr(new QtCamDevicePrivate) {
39
40   static gboolean register_copy = TRUE;
41   if (register_copy) {
42     qt_cam_copy_register();
43     register_copy = FALSE;
44   }
45
46   d_ptr->q_ptr = this;
47   d_ptr->name = name;
48   d_ptr->id = id;
49   d_ptr->conf = config;
50
51   d_ptr->cameraBin = gst_element_factory_make("camerabin2", "QtCameraCameraBin");
52   if (!d_ptr->cameraBin) {
53     qCritical() << "Failed to create camerabin";
54     return;
55   }
56
57   d_ptr->propertySetter = new QtCamPropertySetter(d_ptr);
58
59   d_ptr->createAndAddElement(d_ptr->conf->audioSource(), "audio-source", "QtCameraAudioSrc");
60   if (!d_ptr->conf->wrapperVideoSource().isEmpty() &&
61       !d_ptr->conf->wrapperVideoSourceProperty().isEmpty()) {
62     d_ptr->createAndAddVideoSourceAndWrapper();
63   }
64   else {
65     d_ptr->createAndAddVideoSource();
66   }
67
68   d_ptr->setDevicePoperty();
69
70   int flags =
71     0x00000001 /* no-audio-conversion - Do not use audio conversion elements */
72     | 0x00000002 /* no-video-conversion - Do not use video conversion elements */
73     | 0x00000004 /* no-viewfinder-conversion - Do not use viewfinder conversion elements */
74     | 0x00000008; /* no-image-conversion - Do not use image conversion elements */
75
76   g_object_set(d_ptr->cameraBin, "flags", flags, NULL);
77
78   d_ptr->setAudioCaptureCaps();
79
80   QStringList viewfinderFilters = d_ptr->conf->viewfinderFilters();
81   if (!viewfinderFilters.isEmpty()) {
82     d_ptr->viewfinderFilters =
83       QtCamAnalysisBin::create(viewfinderFilters, "QtCamViewfinderFilters");
84
85     if (!d_ptr->viewfinderFilters) {
86       qWarning() << "Failed to create viewfinder filters";
87     }
88     else {
89       g_object_set(d_ptr->cameraBin, "viewfinder-filter", d_ptr->viewfinderFilters->bin(), NULL);
90     }
91   }
92
93   d_ptr->listener = new QtCamGStreamerMessageListener(gst_element_get_bus(d_ptr->cameraBin),
94                                                       d_ptr, this);
95
96   QObject::connect(d_ptr->listener, SIGNAL(error(const QString&, int, const QString&)),
97                    this, SLOT(_d_error(const QString&, int, const QString&)));
98   QObject::connect(d_ptr->listener, SIGNAL(started()), this, SLOT(_d_started()));
99   QObject::connect(d_ptr->listener, SIGNAL(stopped()), this, SLOT(_d_stopped()));
100   QObject::connect(d_ptr->listener, SIGNAL(stopping()), this, SLOT(_d_stopping()));
101
102   g_signal_connect(d_ptr->cameraBin, "notify::idle",
103                    G_CALLBACK(QtCamDevicePrivate::on_idle_changed), d_ptr);
104
105   if (d_ptr->wrapperVideoSource) {
106     g_signal_connect(d_ptr->wrapperVideoSource, "notify::ready-for-capture",
107                      G_CALLBACK(QtCamDevicePrivate::on_ready_for_capture_changed), d_ptr);
108   }
109   else {
110     g_signal_connect(d_ptr->videoSource, "notify::ready-for-capture",
111                      G_CALLBACK(QtCamDevicePrivate::on_ready_for_capture_changed), d_ptr);
112   }
113
114   d_ptr->image = new QtCamImageMode(d_ptr, this);
115   d_ptr->video = new QtCamVideoMode(d_ptr, this);
116
117   d_ptr->notifications = new QtCamNotifications(this, this);
118 }
119
120 QtCamDevice::~QtCamDevice() {
121   stop(true);
122
123   d_ptr->image->deactivate();
124   d_ptr->video->deactivate();
125
126   delete d_ptr->image; d_ptr->image = 0;
127   delete d_ptr->video; d_ptr->video = 0;
128
129   delete d_ptr->propertySetter;
130
131   delete d_ptr->viewfinderFilters;
132
133   if (d_ptr->cameraBin) {
134     gst_object_unref(d_ptr->cameraBin);
135   }
136
137   delete d_ptr; d_ptr = 0;
138 }
139
140 bool QtCamDevice::setViewfinder(QtCamViewfinder *viewfinder) {
141   if (isRunning()) {
142     qWarning() << "QtCamDevice: pipeline must be stopped before setting a viewfinder";
143     return false;
144   }
145
146   if (d_ptr->viewfinder == viewfinder) {
147     return true;
148   }
149
150   if (!viewfinder) {
151     if (d_ptr->cameraBin) {
152       g_object_set(d_ptr->cameraBin, "viewfinder-sink", NULL, NULL);
153     }
154
155     d_ptr->sink = 0;
156
157     d_ptr->viewfinder = 0;
158     return true;
159   }
160
161   d_ptr->viewfinder = viewfinder;
162   d_ptr->sink = 0;
163
164   return true;
165 }
166
167 QtCamViewfinder *QtCamDevice::viewfinder() const {
168   return d_ptr->viewfinder;
169 }
170
171 bool QtCamDevice::start() {
172   if (d_ptr->error) {
173     qWarning() << "Pipeline must be stopped first because of an error.";
174     return false;
175   }
176
177   if (!d_ptr->cameraBin) {
178     qWarning() << "Missing camerabin";
179     return false;
180   }
181
182   if (!d_ptr->viewfinder) {
183     qWarning() << "Viewfinder not set";
184     return false;
185   }
186
187   if (isRunning()) {
188     return true;
189   }
190
191   if (!d_ptr->active) {
192     d_ptr->image->activate();
193   }
194   else {
195     d_ptr->active->applySettings();
196   }
197
198   // Set sink.
199   if (!d_ptr->setViewfinderSink()) {
200     return false;
201   }
202
203   GstStateChangeReturn err = gst_element_set_state(d_ptr->cameraBin, GST_STATE_PLAYING);
204   if (err == GST_STATE_CHANGE_FAILURE) {
205     qWarning() << "Failed to start camera pipeline";
206     return false;
207   }
208
209   // We need to wait for startup to complet. There's a race condition somewhere in the pipeline.
210   // If we set the scene mode to night and update the resolution while starting up
211   // then subdevsrc2 barfs:
212   // streaming task paused, reason not-negotiated (-4)
213   GstState state;
214   if (err != GST_STATE_CHANGE_ASYNC) {
215     return true;
216   }
217
218   if (gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE)
219       != GST_STATE_CHANGE_SUCCESS) {
220     // We are seriously screwed up :(
221     return false;
222   }
223
224   if (state != GST_STATE_PLAYING) {
225     // Huh ? Is this even possible ??
226     return false;
227   }
228
229   return true;
230 }
231
232 bool QtCamDevice::stop(bool force) {
233   if (!d_ptr->cameraBin) {
234     return true;
235   }
236
237   if (d_ptr->error) {
238     gst_element_set_state(d_ptr->cameraBin, GST_STATE_NULL);
239     d_ptr->error = false;
240
241     d_ptr->viewfinder->stop();
242
243     return true;
244   }
245
246   GstState state;
247   gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE);
248
249   if (state == GST_STATE_NULL) {
250     // Nothing to do.
251     return true;
252   }
253
254   if (!isIdle()) {
255     if (!force) {
256       return false;
257     }
258   }
259
260   // First we go to ready:
261   GstStateChangeReturn st = gst_element_set_state(d_ptr->cameraBin, GST_STATE_READY);
262   if (st != GST_STATE_CHANGE_FAILURE) {
263     // Flush the bus:
264     d_ptr->listener->flushMessages();
265   }
266
267   // Now to NULL
268   gst_element_set_state(d_ptr->cameraBin, GST_STATE_NULL);
269
270   d_ptr->viewfinder->stop();
271
272   return true;
273 }
274
275 bool QtCamDevice::isRunning() {
276   if (!d_ptr->cameraBin) {
277     return false;
278   }
279
280   GstState state;
281   GstStateChangeReturn err = gst_element_get_state(d_ptr->cameraBin,
282                                                    &state, 0, GST_CLOCK_TIME_NONE);
283
284   if (err == GST_STATE_CHANGE_FAILURE || state != GST_STATE_PLAYING) {
285     return false;
286   }
287
288   return true;
289 }
290
291 bool QtCamDevice::isIdle() {
292   if (!d_ptr->cameraBin) {
293     return true;
294   }
295
296   gboolean idle = FALSE;
297   g_object_get(d_ptr->cameraBin, "idle", &idle, NULL);
298
299   return idle == TRUE;
300 }
301
302 QtCamImageMode *QtCamDevice::imageMode() const {
303   return d_ptr->image;
304 }
305
306 QtCamVideoMode *QtCamDevice::videoMode() const {
307   return d_ptr->video;
308 }
309
310 QtCamMode *QtCamDevice::activeMode() const {
311   return d_ptr->active;
312 }
313
314 QString QtCamDevice::name() const {
315   return d_ptr->name;
316 }
317
318 QVariant QtCamDevice::id() const {
319   return d_ptr->id;
320 }
321
322 QtCamConfig *QtCamDevice::config() const {
323   return d_ptr->conf;
324 }
325
326 QtCamGStreamerMessageListener *QtCamDevice::listener() const {
327   return d_ptr->listener;
328 }
329
330 QtCamNotifications *QtCamDevice::notifications() const {
331   return d_ptr->notifications;
332 }
333
334 #include "moc_qtcamdevice.cpp"