Fixed a crash if we fail to create viewfinderFilters.
[harmattan/cameraplus] / lib / qtcamdevice.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 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   int flags =
69     0x00000001 /* no-audio-conversion - Do not use audio conversion elements */
70     | 0x00000002 /* no-video-conversion - Do not use video conversion elements */
71     | 0x00000004 /* no-viewfinder-conversion - Do not use viewfinder conversion elements */
72     | 0x00000008; /* no-image-conversion - Do not use image conversion elements */
73
74   g_object_set(d_ptr->cameraBin, "flags", flags, NULL);
75
76   d_ptr->setAudioCaptureCaps();
77
78   QStringList viewfinderFilters = d_ptr->conf->viewfinderFilters();
79   if (!viewfinderFilters.isEmpty()) {
80     d_ptr->viewfinderFilters =
81       QtCamAnalysisBin::create(viewfinderFilters, "QtCamViewfinderFilters");
82
83     if (!d_ptr->viewfinderFilters) {
84       qWarning() << "Failed to create viewfinder filters";
85     }
86     else {
87       g_object_set(d_ptr->cameraBin, "viewfinder-filter", d_ptr->viewfinderFilters->bin(), NULL);
88     }
89   }
90
91   d_ptr->listener = new QtCamGStreamerMessageListener(gst_element_get_bus(d_ptr->cameraBin),
92                                                       d_ptr, this);
93
94   QObject::connect(d_ptr->listener, SIGNAL(error(const QString&, int, const QString&)),
95                    this, SLOT(_d_error(const QString&, int, const QString&)));
96   QObject::connect(d_ptr->listener, SIGNAL(started()), this, SLOT(_d_started()));
97   QObject::connect(d_ptr->listener, SIGNAL(stopped()), this, SLOT(_d_stopped()));
98   QObject::connect(d_ptr->listener, SIGNAL(stopping()), this, SLOT(_d_stopping()));
99
100   g_signal_connect(d_ptr->cameraBin, "notify::idle",
101                    G_CALLBACK(QtCamDevicePrivate::on_idle_changed), d_ptr);
102
103   g_signal_connect(d_ptr->wrapperVideoSource, "notify::ready-for-capture",
104                    G_CALLBACK(QtCamDevicePrivate::on_ready_for_capture_changed), d_ptr);
105
106   d_ptr->image = new QtCamImageMode(d_ptr, this);
107   d_ptr->video = new QtCamVideoMode(d_ptr, this);
108
109   d_ptr->notifications = new QtCamNotifications(this, this);
110 }
111
112 QtCamDevice::~QtCamDevice() {
113   stop(true);
114
115   d_ptr->image->deactivate();
116   d_ptr->video->deactivate();
117
118   delete d_ptr->image; d_ptr->image = 0;
119   delete d_ptr->video; d_ptr->video = 0;
120
121   delete d_ptr->propertySetter;
122
123   delete d_ptr->viewfinderFilters;
124
125   if (d_ptr->cameraBin) {
126     gst_object_unref(d_ptr->cameraBin);
127   }
128
129   delete d_ptr; d_ptr = 0;
130 }
131
132 bool QtCamDevice::setViewfinder(QtCamViewfinder *viewfinder) {
133   if (isRunning()) {
134     qWarning() << "QtCamDevice: pipeline must be stopped before setting a viewfinder";
135     return false;
136   }
137
138   if (d_ptr->viewfinder == viewfinder) {
139     return true;
140   }
141
142   if (!viewfinder) {
143     qWarning() << "QtCamDevice: viewfinder cannot be unset.";
144     return false;
145   }
146
147   if (d_ptr->viewfinder) {
148     qWarning() << "QtCamDevice: viewfinder cannot be replaced.";
149     return false;
150   }
151
152   if (!viewfinder->setDevice(this)) {
153     return false;
154   }
155
156   d_ptr->viewfinder = viewfinder;
157
158   return true;
159 }
160
161 QtCamViewfinder *QtCamDevice::viewfinder() const {
162   return d_ptr->viewfinder;
163 }
164
165 bool QtCamDevice::start() {
166   if (d_ptr->error) {
167     qWarning() << "Pipeline must be stopped first because of an error.";
168     return false;
169   }
170
171   if (!d_ptr->cameraBin) {
172     qWarning() << "Missing camerabin";
173     return false;
174   }
175
176   if (!d_ptr->viewfinder) {
177     qWarning() << "Viewfinder not set";
178     return false;
179   }
180
181   if (isRunning()) {
182     return true;
183   }
184
185   if (!d_ptr->active) {
186     d_ptr->image->activate();
187   }
188   else {
189     d_ptr->active->applySettings();
190   }
191
192   // Set sink.
193   if (!d_ptr->setViewfinderSink()) {
194     return false;
195   }
196
197   GstStateChangeReturn err = gst_element_set_state(d_ptr->cameraBin, GST_STATE_PLAYING);
198   if (err == GST_STATE_CHANGE_FAILURE) {
199     qWarning() << "Failed to start camera pipeline";
200     return false;
201   }
202
203   // We need to wait for startup to complet. There's a race condition somewhere in the pipeline.
204   // If we set the scene mode to night and update the resolution while starting up
205   // then subdevsrc2 barfs:
206   // streaming task paused, reason not-negotiated (-4)
207   GstState state;
208   if (err != GST_STATE_CHANGE_ASYNC) {
209     return true;
210   }
211
212   if (gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE)
213       != GST_STATE_CHANGE_SUCCESS) {
214     // We are seriously screwed up :(
215     return false;
216   }
217
218   if (state != GST_STATE_PLAYING) {
219     // Huh ? Is this even possible ??
220     return false;
221   }
222
223   return true;
224 }
225
226 bool QtCamDevice::stop(bool force) {
227   if (!d_ptr->cameraBin) {
228     return true;
229   }
230
231   if (d_ptr->error) {
232     gst_element_set_state(d_ptr->cameraBin, GST_STATE_NULL);
233     d_ptr->error = false;
234
235     d_ptr->viewfinder->stop();
236
237     return true;
238   }
239
240   GstState state;
241   gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE);
242
243   if (state == GST_STATE_NULL) {
244     // Nothing to do.
245     return true;
246   }
247
248   if (!isIdle()) {
249     if (!force) {
250       return false;
251     }
252   }
253
254   // First we go to ready:
255   GstStateChangeReturn st = gst_element_set_state(d_ptr->cameraBin, GST_STATE_READY);
256   if (st != GST_STATE_CHANGE_FAILURE) {
257     // Flush the bus:
258     d_ptr->listener->flushMessages();
259   }
260
261   // Now to NULL
262   gst_element_set_state(d_ptr->cameraBin, GST_STATE_NULL);
263
264   d_ptr->viewfinder->stop();
265
266   return true;
267 }
268
269 bool QtCamDevice::isRunning() {
270   if (!d_ptr->cameraBin) {
271     return false;
272   }
273
274   GstState state;
275   GstStateChangeReturn err = gst_element_get_state(d_ptr->cameraBin,
276                                                    &state, 0, GST_CLOCK_TIME_NONE);
277
278   if (err == GST_STATE_CHANGE_FAILURE || state != GST_STATE_PLAYING) {
279     return false;
280   }
281
282   return true;
283 }
284
285 bool QtCamDevice::isIdle() {
286   if (!d_ptr->cameraBin) {
287     return true;
288   }
289
290   gboolean idle = FALSE;
291   g_object_get(d_ptr->cameraBin, "idle", &idle, NULL);
292
293   return idle == TRUE;
294 }
295
296 QtCamImageMode *QtCamDevice::imageMode() const {
297   return d_ptr->image;
298 }
299
300 QtCamVideoMode *QtCamDevice::videoMode() const {
301   return d_ptr->video;
302 }
303
304 QtCamMode *QtCamDevice::activeMode() const {
305   return d_ptr->active;
306 }
307
308 QString QtCamDevice::name() const {
309   return d_ptr->name;
310 }
311
312 QVariant QtCamDevice::id() const {
313   return d_ptr->id;
314 }
315
316 QtCamConfig *QtCamDevice::config() const {
317   return d_ptr->conf;
318 }
319
320 QtCamGStreamerMessageListener *QtCamDevice::listener() const {
321   return d_ptr->listener;
322 }
323
324 QtCamNotifications *QtCamDevice::notifications() const {
325   return d_ptr->notifications;
326 }
327
328 #include "moc_qtcamdevice.cpp"