unref camerabin when we get destroyed
[harmattan/cameraplus] / lib / qtcamdevice.cpp
index 93139e3..793eefa 100644 (file)
@@ -1,3 +1,23 @@
+/*!
+ * This file is part of CameraPlus.
+ *
+ * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
 #include "qtcamdevice.h"
 #include "qtcamviewfinder.h"
 #include "qtcamconfig.h"
@@ -8,6 +28,9 @@
 #include "qtcammode.h"
 #include "qtcamimagemode.h"
 #include "qtcamvideomode.h"
+#include "qtcamnotifications.h"
+
+// TODO: we want the ability to change the image and video gep from the app.
 
 QtCamDevice::QtCamDevice(QtCamConfig *config, const QString& name,
                         const QVariant& id, QObject *parent) :
@@ -42,7 +65,6 @@ QtCamDevice::QtCamDevice(QtCamConfig *config, const QString& name,
   // TODO: filters
   // TODO: capabilities
   // TODO: custom properties for jifmux, mp4mux, audio encoder, video encoder, sink & video source
-  // color tune, scene modes
   d_ptr->listener = new QtCamGStreamerMessageListener(gst_element_get_bus(d_ptr->cameraBin),
                                                      d_ptr, this);
 
@@ -60,6 +82,8 @@ QtCamDevice::QtCamDevice(QtCamConfig *config, const QString& name,
 
   d_ptr->image = new QtCamImageMode(d_ptr, this);
   d_ptr->video = new QtCamVideoMode(d_ptr, this);
+
+  d_ptr->notifications = new QtCamNotifications(this, this);
 }
 
 QtCamDevice::~QtCamDevice() {
@@ -70,6 +94,11 @@ QtCamDevice::~QtCamDevice() {
 
   delete d_ptr->image; d_ptr->image = 0;
   delete d_ptr->video; d_ptr->video = 0;
+
+  if (d_ptr->cameraBin) {
+    gst_object_unref(d_ptr->cameraBin);
+  }
+
   delete d_ptr; d_ptr = 0;
 }
 
@@ -140,6 +169,26 @@ bool QtCamDevice::start() {
     return false;
   }
 
+  // We need to wait for startup to complet. There's a race condition somewhere in the pipeline.
+  // If we set the scene mode to night and update the resolution while starting up
+  // then subdevsrc2 barfs:
+  // streaming task paused, reason not-negotiated (-4)
+  GstState state;
+  if (err != GST_STATE_CHANGE_ASYNC) {
+    return true;
+  }
+
+  if (gst_element_get_state(d_ptr->cameraBin, &state, 0, GST_CLOCK_TIME_NONE)
+      != GST_STATE_CHANGE_SUCCESS) {
+    // We are seriously screwed up :(
+    return false;
+  }
+
+  if (state != GST_STATE_PLAYING) {
+    // Huh ? Is this even possible ??
+    return false;
+  }
+
   return true;
 }
 
@@ -166,6 +215,8 @@ bool QtCamDevice::stop() {
     return false;
   }
 
+  d_ptr->viewfinder->stop();
+
   // First we go to ready:
   GstStateChangeReturn st = gst_element_set_state(d_ptr->cameraBin, GST_STATE_READY);
   if (st != GST_STATE_CHANGE_FAILURE) {
@@ -234,4 +285,8 @@ QtCamGStreamerMessageListener *QtCamDevice::listener() const {
   return d_ptr->listener;
 }
 
+QtCamNotifications *QtCamDevice::notifications() const {
+  return d_ptr->notifications;
+}
+
 #include "moc_qtcamdevice.cpp"