[gnac] Start refactoring



commit 41458fa84d29c03f5fd6f35df6374a4572a3d548
Author: David Joaquim <djoaquim src gnome org>
Date:   Mon Apr 26 21:43:20 2010 +0200

    Start refactoring

 libgnac/libgnac-gst-utils.c  |  187 ++++++++++++++++++++++++++++++++
 libgnac/libgnac-gst-utils.h  |   71 ++++++++++++
 libgnac/libgnac-media-item.c |  243 ++++++++++++++++++++++++++++++++++++++++++
 libgnac/libgnac-media-item.h |  106 ++++++++++++++++++
 libgnac/libgnac-profile.h    |   44 ++++++++
 5 files changed, 651 insertions(+), 0 deletions(-)
---
diff --git a/libgnac/libgnac-gst-utils.c b/libgnac/libgnac-gst-utils.c
new file mode 100644
index 0000000..5b615d7
--- /dev/null
+++ b/libgnac/libgnac-gst-utils.c
@@ -0,0 +1,187 @@
+/*
+ * This file is part of GNAC - Gnome Audio Converter
+ *
+ * Copyright (C) 2007 - 2009 Gnac
+ *    
+ *    - DUPASQUIER  Benoit    <bdupasqu src gnome org>
+ *    - JOAQUIM     David     <djoaquim src gnome org>
+ *    - ROUX        Alexandre <alexroux src gnome org>
+ *
+ * GNAC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GNAC 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNAC; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <glib/gi18n.h>
+
+#include "libgnac-debug.h"
+#include "libgnac-error.h"
+#include "libgnac-gst-utils.h"
+
+
+GstElement *
+libgnac_gstu_make_pipeline_element(GstElement   *bin,
+                                  const gchar  *element,
+                                  const gchar  *name,
+                                  GError      **error)
+{
+  GstElement *elem;
+
+  elem = gst_element_factory_make(element, name);
+  if (!elem) {
+    libgnac_debug("Failed to create %s element", element);
+    g_set_error(error,
+        LIBGNAC_ERROR,
+        LIBGNAC_ERROR_MISSING_PLUGIN,
+        _("Failed to create %s element"),
+        element);
+    return NULL;
+  }
+
+  if (!gst_bin_add(GST_BIN(bin), elem)) {
+    libgnac_debug("Failed to add %s element to the bin", element);
+    g_set_error(error,
+        LIBGNAC_ERROR,
+        LIBGNAC_ERROR_PIPELINE_CREATION,
+        _("Failed to add %s element"),
+        element);
+    return NULL;
+  }
+
+  return elem;
+}
+
+
+GstElement *
+libgnac_gstu_pipeline_new(GError **error)
+{
+  GstElement *pipeline;
+  pipeline = gst_pipeline_new("pipeline");
+  if (!pipeline) {
+    libgnac_warning("Pipeline creation failed");
+    g_set_error(error,
+        LIBGNAC_ERROR,
+        LIBGNAC_ERROR_PIPELINE_CREATION,
+        _("Unable to create pipeline"));
+    return NULL;
+  }
+
+  return pipeline;
+}
+
+
+gboolean
+libgnac_gstu_bin_add(GstElement  *bin,
+                    GstElement  *elem,
+                    GError     **error)
+{
+  gchar *name;
+
+  name = gst_element_get_name(elem);
+
+  if (!gst_bin_add(GST_BIN(bin), elem)) {
+    libgnac_debug("Failed to add %s element to the bin", name);
+    g_set_error(error,
+        LIBGNAC_ERROR,
+        LIBGNAC_ERROR_PIPELINE_CREATION,
+        _("Failed to add %s element"),
+        name);
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+
+gboolean
+libgnac_gstu_element_link(GstElement  *src,
+                         GstElement  *dst,
+                         GError     **error)
+{
+  gboolean ret;
+  ret = gst_element_link(src, dst);
+  if (!ret) {
+    libgnac_debug("Failed to link element %s to %s",
+        gst_element_get_name(src), gst_element_get_name(dst));
+    g_set_error(error,
+        LIBGNAC_ERROR,
+        LIBGNAC_ERROR_PIPELINE_CREATION,
+        _("Unable to link element %s to %s"),
+        gst_element_get_name(src), gst_element_get_name(dst));
+  }
+
+  return ret;
+}
+
+
+GstPadLinkReturn
+libgnac_gstu_pad_link(GstPad  *src,
+                     GstPad  *sink,
+                     GError **error)
+{
+  GstPadLinkReturn ret;
+  ret = gst_pad_link(src, sink);
+  if (ret != GST_PAD_LINK_OK) {
+    libgnac_debug("Failed to link pad %s to %s",
+        gst_pad_get_name(src), gst_pad_get_name(sink));
+    g_set_error(error,
+        LIBGNAC_ERROR,
+        LIBGNAC_ERROR_PIPELINE_CREATION,
+        _("Unable to link pad %s to %s"),
+        gst_pad_get_name(src), gst_pad_get_name(sink));
+  }
+
+  return ret;
+}
+
+
+gboolean
+libgnac_gstu_get_compatible_pad(GstElement   *element,
+                               GstPad       *pad,
+                               GstCaps      *caps,
+                               const gchar  *type)
+{
+  GstPad *sink_pad;
+  sink_pad = gst_element_get_compatible_pad(element, pad, caps);
+  if (!sink_pad) {
+    libgnac_debug("Unable to find a compatible %s pad "
+        "(sink_pad = %s, caps = %s\n)", type, gst_pad_get_name(pad),
+        gst_caps_to_string(caps));
+    gst_caps_unref(caps);
+    return FALSE;
+  }
+
+  libgnac_debug("%s: %s -> %s", type,
+      gst_pad_get_name(pad), gst_pad_get_name(sink_pad));
+
+  if (GST_PAD_IS_LINKED(sink_pad)) {
+    libgnac_debug("%s pad %s is already linked", type,
+        gst_pad_get_name(sink_pad));
+    g_object_unref(sink_pad);
+    gst_caps_unref(caps);
+    return FALSE;
+  }
+
+  g_return_val_if_fail(
+      libgnac_gstu_pad_link(pad, sink_pad, NULL) == GST_PAD_LINK_OK,
+      FALSE);
+  g_object_unref(sink_pad);
+
+  return TRUE;
+}
+
diff --git a/libgnac/libgnac-gst-utils.h b/libgnac/libgnac-gst-utils.h
new file mode 100644
index 0000000..9f3e0ae
--- /dev/null
+++ b/libgnac/libgnac-gst-utils.h
@@ -0,0 +1,71 @@
+/*
+ * This file is part of GNAC - Gnome Audio Converter
+ *
+ * Copyright (C) 2007 - 2009 Gnac
+ *    
+ *    - DUPASQUIER  Benoit    <bdupasqu src gnome org>
+ *    - JOAQUIM     David     <djoaquim src gnome org>
+ *    - ROUX        Alexandre <alexroux src gnome org>
+ *
+ * GNAC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GNAC 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNAC; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifndef __LIBGNAC_GST_UTILS_H__
+#define __LIBGNAC_GST_UTILS_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gst/gst.h>
+
+G_BEGIN_DECLS
+
+
+GstElement *
+libgnac_gstu_make_pipeline_element(GstElement   *bin,
+    const gchar  *element,
+    const gchar  *name,
+    GError      **error);
+
+GstElement *
+libgnac_gstu_pipeline_new(GError **error);
+
+
+gboolean
+libgnac_gstu_bin_add(GstElement  *bin,
+    GstElement  *elem,
+    GError     **error);
+
+gboolean
+libgnac_gstu_element_link(GstElement  *src,
+    GstElement  *dst,
+    GError     **error);
+
+GstPadLinkReturn
+libgnac_gstu_pad_link(GstPad  *src,
+    GstPad  *sink,
+    GError **error);
+
+
+gboolean
+libgnac_gstu_get_compatible_pad(GstElement   *element,
+    GstPad       *pad,
+    GstCaps      *caps,
+    const gchar  *type);
+
+G_END_DECLS
+
+#endif /* __LIBGNAC_GST_UTILS_H__ */
+
diff --git a/libgnac/libgnac-media-item.c b/libgnac/libgnac-media-item.c
new file mode 100644
index 0000000..3fdf6bb
--- /dev/null
+++ b/libgnac/libgnac-media-item.c
@@ -0,0 +1,243 @@
+/*
+ * This file is part of GNAC - Gnome Audio Converter
+ *
+ * Copyright (C) 2007 - 2009 Gnac
+ *    
+ *    - DUPASQUIER  Benoit    <bdupasqu src gnome org>
+ *    - JOAQUIM     David     <djoaquim src gnome org>
+ *    - ROUX        Alexandre <alexroux src gnome org>
+ *
+ * GNAC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GNAC 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNAC; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <glib/gi18n.h>
+
+#include "libgnac-debug.h"
+#include "libgnac-error.h"
+#include "libgnac-gst-utils.h"
+#include "libgnac-gst.h"
+#include "libgnac-metadata.h"
+
+
+extern LibgnacMetadata *metadata;
+
+LibgnacMediaItem *
+libgnac_item_new(GFile            *source,
+                 LibgnacProfile    *profile,
+                 gpointer          parent)
+{
+  LibgnacMediaItem *item;
+
+  item = (LibgnacMediaItem*)g_malloc(sizeof(LibgnacMediaItem));
+  
+  item->parent = parent;
+  item->source = g_file_dup(source);
+  item->profile = profile;
+  item->destination = NULL;
+  item->pipeline = NULL;
+  item->audio_encoder = NULL;
+  item->bus = NULL;
+  item->timeout_id = 0;
+  item->pos = 0;
+
+  return item;
+}
+
+
+void
+libgnac_item_build(LibgnacMediaItem  *item, 
+                   GError               **error)
+{
+  gboolean has_video = FALSE;
+  GError *err = NULL;
+  LibgnacTags *tags;
+
+  g_return_if_fail(error == NULL || *error == NULL);
+
+  /* is it a video file? */
+  // TODO why not passing error ?
+  tags = libgnac_metadata_extract(metadata, item->source, NULL);
+  if (tags && libgnac_metadata_tag_exists(tags, GNAC_TAG_HAS_VIDEO)) {
+    has_video = TRUE;
+  }
+
+  libgnac_gst_build_pipeline(item, item->profile, has_video, &err);
+  if (err) {
+    libgnac_warning("Unable to build pipeline");
+    libgnac_gst_clean_pipeline(item);
+    g_propagate_error(error, err);
+
+    return;
+  }
+
+  /* Connect item callbacks*/
+  g_signal_connect(G_OBJECT(item->bus), "message::eos", 
+      G_CALLBACK(libgnac_item_eos_cb), item);
+  g_signal_connect(G_OBJECT(item->bus), "message::error", 
+      G_CALLBACK(libgnac_item_error_cb), item);
+
+
+  /* Connect parent callbacks */
+  // TODO move 
+  g_signal_connect(G_OBJECT(item->bus), "message::eos", 
+      G_CALLBACK(libgnac_converter_eos_cb), item->parent);
+  g_signal_connect(G_OBJECT(item->bus), "message::error", 
+      G_CALLBACK(libgnac_converter_error_cb), item->parent);
+
+
+}
+
+
+void
+libgnac_item_run(LibgnacMediaItem  *item, 
+                           GError               **error)
+{
+  GError *err = NULL;
+  g_return_if_fail(error == NULL || *error == NULL);
+
+  /* Build pipeline if doesn't exist */
+  if (!item->pipeline) {
+    libgnac_item_build(item, &err);
+    if(err)
+    {
+      g_propagate_error(error, err);
+      return;
+    }
+  }
+
+
+  libgnac_item_init_event_src(item);
+  
+  libgnac_gst_run(item, &err);
+  if (err) {
+    libgnac_warning("Unable to run pipeline");
+    libgnac_gst_clean_pipeline(item);
+    g_propagate_error(error, err);
+    return;
+  }
+}
+
+
+void
+libgnac_item_stop(LibgnacMediaItem  *item, 
+                            GError               **error)
+{
+  GError *err = NULL;
+  g_return_if_fail(error == NULL || *error == NULL);
+
+  libgnac_item_clear_event_src(item);
+  libgnac_gst_stop(item, &err);
+  // TODO propagate error here
+  //g_propagate_error(error, err);
+
+  /* Remove not completed file */
+  if (G_IS_FILE(item->destination)) 
+  {
+    g_file_delete(item->destination, NULL, &err);
+    if (err) {
+      // Warning, but not critical
+      libgnac_warning("Unable to remove partial file");
+      g_clear_error(&err);
+    }
+  }
+}
+
+
+/*void
+libgnac_item_pause(LibgnacMediaItem  *item, 
+                             GError               **error)
+{
+  g_return_if_fail(error == NULL || *error == NULL);
+}
+
+
+void
+libgnac_item_resume(LibgnacMediaItem  *item, 
+                              GError               **error)
+{
+  g_return_if_fail(error == NULL || *error == NULL);
+}*/
+
+
+void
+libgnac_item_free(LibgnacMediaItem *item)
+{
+  libgnac_item_clear_event_src(item);
+
+  g_object_unref(item->source);
+  item->bus = NULL;
+  item->source = NULL;
+  item->parent = NULL;
+  // the profile is freed by the converter
+  item->profile = NULL;
+  if (item->destination) {
+    g_object_unref(item->destination);
+    item->destination = NULL;
+  }
+
+  libgnac_gst_clean_pipeline(item);
+
+  g_free(item);
+}
+
+
+void
+libgnac_item_init_event_src(LibgnacMediaItem *item)
+{
+  item->timeout_id = g_timeout_add(PROGRESS_TIMEOUT, 
+      (GSourceFunc)libgnac_converter_percentage_cb, item->parent);
+}
+
+
+void
+libgnac_item_clear_event_src(LibgnacMediaItem *item)
+{
+  if (item->timeout_id) {
+    g_source_remove(item->timeout_id);
+    item->timeout_id = 0;
+  }
+}
+
+/* Callbacks */
+
+void
+libgnac_item_eos_cb(GstBus     *bus, 
+                   GstMessage *message, 
+                   gpointer    data)
+{
+  LibgnacMediaItem *item;
+
+  item = (LibgnacMediaItem*)data;
+  libgnac_item_clear_event_src(item);
+  libgnac_gst_clean_pipeline(item);
+}
+
+
+void
+libgnac_item_error_cb(GstBus     *bus, 
+                     GstMessage *message, 
+                     gpointer    data)
+{
+  LibgnacMediaItem *item;
+
+  item = (LibgnacMediaItem*)data;
+  libgnac_item_clear_event_src(item);
+//  libgnac_gst_clean_pipeline(item);
+}
diff --git a/libgnac/libgnac-media-item.h b/libgnac/libgnac-media-item.h
new file mode 100644
index 0000000..6aa1ac3
--- /dev/null
+++ b/libgnac/libgnac-media-item.h
@@ -0,0 +1,106 @@
+/*
+ * This file is part of GNAC - Gnome Audio Converter
+ *
+ * Copyright (C) 2007 - 2009 Gnac
+ *    
+ *    - DUPASQUIER  Benoit    <bdupasqu src gnome org>
+ *    - JOAQUIM     David     <djoaquim src gnome org>
+ *    - ROUX        Alexandre <alexroux src gnome org>
+ *
+ * GNAC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GNAC 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNAC; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifndef __LIBGNAC_MEDIA_ITEM_H__
+#define __LIBGNAC_MEDIA_ITEM_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gst/gst.h>
+
+#include "libgnac-profile.h"
+
+G_BEGIN_DECLS
+
+/* Encoder item that handle a file conversion */
+
+
+typedef struct 
+{
+  gpointer           parent;
+  GFile              *source;
+  GFile              *destination;
+  LibgnacProfile     *profile;
+  GstElement         *pipeline;
+  GstElement         *audio_encoder;
+  GstElement         *muxer;
+  GstElement         *video_encoder;
+  GstBus             *bus;
+  guint               timeout_id;
+  gint64              pos;
+} LibgnacMediaItem;
+
+
+LibgnacMediaItem *
+libgnac_item_new(GFile            *source,
+                 LibgnacProfile    *profile,
+                 gpointer          parent);
+
+void
+libgnac_item_build(LibgnacMediaItem  *item, 
+                   GError           **error);
+
+void
+libgnac_item_run(LibgnacMediaItem  *item, 
+                 GError           **error);
+
+void
+libgnac_item_stop(LibgnacMediaItem  *item, 
+                  GError           **error);
+
+void
+libgnac_item_pause(LibgnacMediaItem  *item, 
+                   GError           **error);
+
+void
+libgnac_item_resume(LibgnacMediaItem  *item, 
+                    GError           **error);
+
+void
+libgnac_item_free(LibgnacMediaItem *item);
+
+void
+libgnac_item_init_event_src(LibgnacMediaItem *item);
+
+void
+libgnac_item_clear_event_src(LibgnacMediaItem *item);
+
+/* Callbacks */
+
+void
+libgnac_item_eos_cb(GstBus     *bus, 
+                   GstMessage *message, 
+                   gpointer    data);
+
+void
+libgnac_item_error_cb(GstBus     *bus, 
+                     GstMessage *message, 
+                     gpointer    data);
+
+G_END_DECLS
+
+#endif /* __LIBGNAC_MEDIA_ITEM_H__ */
+
+
diff --git a/libgnac/libgnac-profile.h b/libgnac/libgnac-profile.h
new file mode 100644
index 0000000..54aa290
--- /dev/null
+++ b/libgnac/libgnac-profile.h
@@ -0,0 +1,44 @@
+/*
+ * This file is part of GNAC - Gnome Audio Converter
+ *
+ * Copyright (C) 2007 - 2009 Gnac
+ *    
+ *    - DUPASQUIER  Benoit    <bdupasqu src gnome org>
+ *    - JOAQUIM     David     <djoaquim src gnome org>
+ *    - ROUX        Alexandre <alexroux src gnome org>
+ *
+ * GNAC is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * GNAC 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNAC; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, 
+ * Boston, MA  02110-1301  USA
+ */
+
+#ifndef __LIBGNAC_PROFILE_H__
+#define __LIBGNAC_PROFILE_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef struct 
+{
+  gchar *audio_desc;
+  gchar *video_desc;
+  gchar *muxer_desc;
+} LibgnacProfile;
+
+G_END_DECLS
+
+#endif /* __LIBGNAC_PROFILE_H__ */
+
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]