Updated copyright year
[harmattan/cameraplus] / lib / qtcamanalysisbin.cpp
index ef81a2c..f8d357f 100644 (file)
@@ -1,7 +1,7 @@
 /*!
  * This file is part of CameraPlus.
  *
- * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ * Copyright (C) 2012-2013 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
  */
 
 #include "qtcamanalysisbin.h"
+#include <QStringList>
 #include <QDebug>
+#include <QHash>
 
 #define FACTORY_NAME(x) gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(gst_element_get_factory(x)))
 
-GstElement *qt_cam_analysis_bin_create(QList<GstElement *>& children, const char *name) {
-  GstElement *bin = 0;
+static QtCamAnalysisBinPrivate *qt_cam_analysis_bin_create(const QStringList& factories,
+                                                          const char *name);
+static QtCamAnalysisBinPrivate *qt_cam_analysis_bin_create(GstElement *child,
+                                                          const char *name);
 
-  QList<GstElement *> added;
+class QtCamAnalysisBinPrivate {
+public:
+  QtCamAnalysisBinPrivate() :
+    bin(0),
+    probe(0),
+    queuePad(0) {
+
+  }
+
+  static gboolean block_buffers(GstPad *pad, GstMiniObject *o) {
+    Q_UNUSED(pad);
+    Q_UNUSED(o);
+
+    // Drop data
+    return FALSE;
+  }
+
+  GstElement *bin;
+  gulong probe;
+  GstPad *queuePad;
+  QMultiHash<QString, GstElement *> elements;
+};
+
+QtCamAnalysisBin::QtCamAnalysisBin(QtCamAnalysisBinPrivate *d) :
+  d_ptr(d) {
+  gst_object_ref(d_ptr->bin);
+}
+
+QtCamAnalysisBin::~QtCamAnalysisBin() {
+  d_ptr->elements.clear();
+
+  setBlocked(false);
+  gst_object_unref(GST_OBJECT(d_ptr->queuePad));
+  gst_object_unref(d_ptr->bin);
+  delete d_ptr; d_ptr = 0;
+}
+
+void QtCamAnalysisBin::setBlocked(bool blocked) {
+  if (blocked == isBlocked()) {
+    return;
+  }
+
+  if (blocked) {
+    d_ptr->probe = gst_pad_add_buffer_probe(d_ptr->queuePad,
+                                           G_CALLBACK(QtCamAnalysisBinPrivate::block_buffers),
+                                           d_ptr);
+  }
+  else {
+    gst_pad_remove_data_probe(d_ptr->queuePad, d_ptr->probe);
+    d_ptr->probe = 0;
+  }
+}
+
+bool QtCamAnalysisBin::isBlocked() const {
+  return d_ptr->probe != 0;
+}
 
-  if (children.isEmpty()) {
+GstElement *QtCamAnalysisBin::bin() {
+  return d_ptr->bin;
+}
+
+QtCamAnalysisBin *QtCamAnalysisBin::create(const QStringList& factories, const char *name) {
+  QList<GstElement *> elements;
+  if (factories.isEmpty()) {
     return 0;
   }
 
-  if (children.size() == 1) {
-    return qt_cam_analysis_bin_create(children.takeFirst(), name);
+  QtCamAnalysisBinPrivate *d = qt_cam_analysis_bin_create(factories, name);
+  if (!d) {
+    return 0;
   }
 
-  bin = gst_bin_new("analysis_bin_bin");
+  return new QtCamAnalysisBin(d);
+}
+
+QtCamAnalysisBinPrivate *qt_cam_analysis_bin_create(const QStringList& factories,
+                                                   const char *name) {
+
+  if (factories.isEmpty()) {
+    return 0;
+  }
+
+  GstElement *bin = 0;
+  QHash<QString, GstElement *> elements;
+  QList<GstElement *> added;
 
-  while (!children.isEmpty()) {
-    GstElement *elem = children.takeFirst();
+  bin = gst_bin_new("analysis-bin-bin");
 
-    if (!gst_bin_add(GST_BIN(bin), elem)) {
-      qWarning() << "Failed to add element" << FACTORY_NAME(elem) << "to bin";
-      gst_object_unref(elem);
+  foreach (const QString& factory, factories) {
+    GstElement *element = gst_element_factory_make(factory.toUtf8().constData(), NULL);
+    if (!element) {
+      qWarning() << "Failed to create element" << factory;
+      continue;
     }
-    else {
-      added << elem;
+
+    if (!gst_bin_add(GST_BIN(bin), element)) {
+      qWarning() << "Failed to add element" << factory << "to bin";
+      gst_object_unref(element);
     }
+
+    elements.insert(factory, element);
+    added << element;
   }
 
-  for (int x = 1; x < added.count(); x++) {
-    GstElement *elem = added[x];
-    GstElement *prev = added[x - 1];
+  if (added.size() > 1) {
+    for (int x = 1; x < added.count(); x++) {
+      GstElement *elem = added[x];
+      GstElement *prev = added[x - 1];
 
-    if (!gst_element_link(prev, elem)) {
-      qWarning() << "Failed to link" << FACTORY_NAME(prev) << "and" << FACTORY_NAME(elem);
+      if (!gst_element_link(prev, elem)) {
+       qWarning() << "Failed to link" << FACTORY_NAME(prev) << "and" << FACTORY_NAME(elem);
+      }
     }
   }
 
@@ -67,7 +153,14 @@ GstElement *qt_cam_analysis_bin_create(QList<GstElement *>& children, const char
   gst_element_add_pad(bin, gst_ghost_pad_new("src", pad));
   gst_object_unref(GST_OBJECT(pad));
 
-  return qt_cam_analysis_bin_create(bin, name);
+  QtCamAnalysisBinPrivate *d = qt_cam_analysis_bin_create(bin, name);
+  if (!d) {
+    return 0;
+  }
+
+  d->elements = elements;
+
+  return d;
 }
 
 /*
@@ -75,8 +168,10 @@ GstElement *qt_cam_analysis_bin_create(QList<GstElement *>& children, const char
  * tee -
  *       -- queue -- copy -- filters -- fakesink
  */
-GstElement *qt_cam_analysis_bin_create(GstElement *child, const char *name) {
+QtCamAnalysisBinPrivate *qt_cam_analysis_bin_create(GstElement *child, const char *name) {
   GstPad *pad = 0;
+  GstPad *queuePad = 0;
+  QtCamAnalysisBinPrivate *d = 0;
 
   GstElement *bin = gst_bin_new(name);
 
@@ -116,7 +211,12 @@ GstElement *qt_cam_analysis_bin_create(GstElement *child, const char *name) {
   g_object_set(tee, "alloc-pad", pad, NULL);
   gst_object_unref(GST_OBJECT(pad));
 
-  return bin;
+  queuePad = gst_element_get_static_pad(queue, "src");
+
+  d = new QtCamAnalysisBinPrivate;
+  d->queuePad = queuePad;
+  d->bin = bin;
+  return d;
 
  free_and_out:
   if (bin) {
@@ -148,3 +248,11 @@ GstElement *qt_cam_analysis_bin_create(GstElement *child, const char *name) {
 
   return 0;
 }
+
+QList<GstElement *> QtCamAnalysisBin::lookup(const QString& factory) {
+  if (d_ptr->elements.contains(factory)) {
+    return d_ptr->elements.values(factory);
+  }
+
+  return QList<GstElement *>();
+}