Code cleanup
[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
33 QtCamDevice::QtCamDevice(QtCamConfig *config, const QString& name,
34                          const QVariant& id, QObject *parent) :
35   QObject(parent), d_ptr(new QtCamDevicePrivate) {
36
37   d_ptr->q_ptr = this;
38   d_ptr->name = name;
39   d_ptr->id = id;
40   d_ptr->conf = config;
41
42   d_ptr->cameraBin = gst_element_factory_make("camerabin2", "QtCameraCameraBin");
43   if (!d_ptr->cameraBin) {
44     qCritical() << "Failed to create camerabin";
45     return;
46   }
47
48   d_ptr->createAndAddElement(d_ptr->conf->audioSource(), "audio-source", "QtCameraAudioSrc");
49   d_ptr->createAndAddVideoSource();
50
51   int flags =
52     0x00000001 /* no-audio-conversion - Do not use audio conversion elements */
53     | 0x00000002 /* no-video-conversion - Do not use video conversion elements */
54     | 0x00000004 /* no-viewfinder-conversion - Do not use viewfinder conversion elements */
55     | 0x00000008; /* no-image-conversion - Do not use image conversion elements */
56
57   g_object_set(d_ptr->cameraBin, "flags", flags, NULL);
58
59   d_ptr->setAudioCaptureCaps();
60
61   // TODO: audio bitrate
62   // TODO: video bitrate
63   // TODO: filters
64   // TODO: custom properties for jifmux, mp4mux, audio encoder, video encoder, sink & video source
65   d_ptr->listener = new QtCamGStreamerMessageListener(gst_element_get_bus(d_ptr->cameraBin),
66                                                       d_ptr, this);
67
68   QObject::connect(d_ptr->listener, SIGNAL(error(const QString&, int, const QString&)),
69                    this, SLOT(_d_error(const QString&, int, const QString&)));
70   QObject::connect(d_ptr->listener, SIGNAL(started()), this, SLOT(_d_started()));
71   QObject::connect(d_ptr->listener, SIGNAL(stopped()), this, SLOT(_d_stopped()));
72   QObject::connect(d_ptr->listener, SIGNAL(stopping()), this, SLOT(_d_stopping()));
73
74   g_signal_connect(d_ptr->cameraBin, "notify::idle",
75                    G_CALLBACK(QtCamDevicePrivate::on_idle_changed), d_ptr);
76
77   g_signal_connect(d_ptr->wrapperVideoSource, "notify::ready-for-capture",
78                    G_CALLBACK(QtCamDevicePrivate::on_ready_for_capture_changed), d_ptr);
79
80   d_ptr->image = new QtCamImageMode(d_ptr, this);
81   d_ptr->video = new QtCamVideoMode(d_ptr, this);
82
83   d_ptr->notifications = new QtCamNotifications(this, this);
84 }
85
86 QtCamDevice::~QtCamDevice() {
87   stop();
88
89   d_ptr->image->deactivate();
90   d_ptr->video->deactivate();
91
92   delete d_ptr->image; d_ptr->image = 0;
93   delete d_ptr->video; d_ptr->video = 0;
94
95   if (d_ptr->cameraBin) {
96     gst_object_unref(d_ptr->cameraBin);
97   }
98
99   delete d_ptr; d_ptr = 0;
100 }
101
102 bool QtCamDevice::setViewfinder(QtCamViewfinder *viewfinder) {
103   if (isRunning()) {
104     qWarning() << "QtCamDevice: pipeline must be stopped before setting a viewfinder";
105     return false;
106   }
107
108   if (d_ptr->viewfinder == viewfinder) {
109     return true;
110   }
111
112   if (!viewfinder) {
113     qWarning() << "QtCamDevice: viewfinder cannot be unset.";
114     return false;
115   }
116
117   if (d_ptr->viewfinder) {
118     qWarning() << "QtCamDevice: viewfinder cannot be replaced.";
119     return false;
120   }
121
122   if (!viewfinder->setDevice(this)) {
123     return false;
124   }
125
126   d_ptr->viewfinder = viewfinder;
127
128   return true;
129 }
130
131 bool QtCamDevice::start() {
132   if (d_ptr->error) {
133     qWarning() << "Pipeline must be stopped first because of an error.";
134     return false;
135   }
136
137   if (!d_ptr->cameraBin) {
138     qWarning() << "Missing camerabin";
139     return false;
140   }
141
142   if (!d_ptr->viewfinder) {
143     qWarning() << "Viewfinder not set";
144     return false;
145   }
146
147   if (isRunning()) {
148     return true;
149   }
150
151   if (!d_ptr->active) {
152     d_ptr->image->activate();
153   }
154   else {
155     d_ptr->active->applySettings();
156   }
157
158   // Set sink.
159   if (!d_ptr->setViewfinderSink()) {
160     return false;
161   }
162
163   GstStateChangeReturn err = gst_element_set_state(d_ptr->cameraBin, GST_STATE_PLAYING);
164   if (err == GST_STATE_CHANGE_FAILURE) {
165     qWarning() << "Failed to start camera pipeline";
166     return false;
167   }
168
169   // We need to wait for startup to complet. There's a race condition somewhere in the pipeline.
170   // If we set the scene mode to night and update the resolution while starting up
171   // then subdevsrc2 barfs:
172   // streaming task paused, reason not-negotiated (-4)
173   GstState state;
174   if (err != GST_STATE_CHANGE_ASYNC) {
175     return true;
176   }
177
178   if (gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE)
179       != GST_STATE_CHANGE_SUCCESS) {
180     // We are seriously screwed up :(
181     return false;
182   }
183
184   if (state != GST_STATE_PLAYING) {
185     // Huh ? Is this even possible ??
186     return false;
187   }
188
189   return true;
190 }
191
192 bool QtCamDevice::stop() {
193   if (!d_ptr->cameraBin) {
194     return true;
195   }
196
197   if (d_ptr->error) {
198     gst_element_set_state(d_ptr->cameraBin, GST_STATE_NULL);
199     d_ptr->error = false;
200     return true;
201   }
202
203   GstState state;
204   gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE);
205
206   if (state == GST_STATE_NULL) {
207     // Nothing to do.
208     return true;
209   }
210
211   if (!isIdle()) {
212     return false;
213   }
214
215   d_ptr->viewfinder->stop();
216
217   // First we go to ready:
218   GstStateChangeReturn st = gst_element_set_state(d_ptr->cameraBin, GST_STATE_READY);
219   if (st != GST_STATE_CHANGE_FAILURE) {
220     // Flush the bus:
221     d_ptr->listener->flushMessages();
222   }
223
224   // Now to NULL
225   gst_element_set_state(d_ptr->cameraBin, GST_STATE_NULL);
226
227   return true;
228 }
229
230 bool QtCamDevice::isRunning() {
231   if (!d_ptr->cameraBin) {
232     return false;
233   }
234
235   GstState state;
236   GstStateChangeReturn err = gst_element_get_state(d_ptr->cameraBin,
237                                                    &state, 0, GST_CLOCK_TIME_NONE);
238
239   if (err == GST_STATE_CHANGE_FAILURE || state != GST_STATE_PLAYING) {
240     return false;
241   }
242
243   return true;
244 }
245
246 bool QtCamDevice::isIdle() {
247   if (!d_ptr->cameraBin) {
248     return true;
249   }
250
251   gboolean idle = FALSE;
252   g_object_get(d_ptr->cameraBin, "idle", &idle, NULL);
253
254   return idle == TRUE;
255 }
256
257 QtCamImageMode *QtCamDevice::imageMode() const {
258   return d_ptr->image;
259 }
260
261 QtCamVideoMode *QtCamDevice::videoMode() const {
262   return d_ptr->video;
263 }
264
265 QtCamMode *QtCamDevice::activeMode() const {
266   return d_ptr->active;
267 }
268
269 QString QtCamDevice::name() const {
270   return d_ptr->name;
271 }
272
273 QVariant QtCamDevice::id() const {
274   return d_ptr->id;
275 }
276
277 QtCamConfig *QtCamDevice::config() const {
278   return d_ptr->conf;
279 }
280
281 QtCamGStreamerMessageListener *QtCamDevice::listener() const {
282   return d_ptr->listener;
283 }
284
285 QtCamNotifications *QtCamDevice::notifications() const {
286   return d_ptr->notifications;
287 }
288
289 #include "moc_qtcamdevice.cpp"