configure.ac: export [_]*{gst,Gst,GST}.* symbols from plugins
[platform/upstream/gst-plugins-good.git] / ext / lame / gstlame.c
index a7bf213..0b32b4b 100644 (file)
@@ -28,62 +28,39 @@ static GstElementDetails gst_lame_details =
 {
   "L.A.M.E. mp3 encoder",
   "Codec/Audio/Encoder",
-  "LGPL",
   "High-quality free MP3 encoder",
-  VERSION,
   "Erik Walthinsen <omega@cse.ogi.edu>",
-  "(C) 2000",
 };
 
-GST_PAD_TEMPLATE_FACTORY (gst_lame_sink_factory,
+static GstStaticPadTemplate gst_lame_sink_template =
+GST_STATIC_PAD_TEMPLATE (
   "sink",
   GST_PAD_SINK,
   GST_PAD_ALWAYS,
-  GST_CAPS_NEW (
-    "gstlame_sink",
-    "audio/x-raw-int",
-      "endianness", GST_PROPS_INT (G_BYTE_ORDER),
-      "signed",     GST_PROPS_BOOLEAN (TRUE),
-      "width",      GST_PROPS_INT (16),
-      "depth",      GST_PROPS_INT (16),
-      "rate",       GST_PROPS_LIST (
-                     GST_PROPS_INT (8000), 
-                     GST_PROPS_INT (11025), 
-                     GST_PROPS_INT (12000), 
-                     GST_PROPS_INT (16000), 
-                     GST_PROPS_INT (22050),
-                     GST_PROPS_INT (24000),
-                     GST_PROPS_INT (32000),
-                     GST_PROPS_INT (44100),
-                     GST_PROPS_INT (48000)
-                   ),
-      "channels",   GST_PROPS_INT_RANGE (1, 2)
+  GST_STATIC_CAPS (
+    "audio/x-raw-int, "
+      "endianness = (int) BYTE_ORDER, "
+      "signed = (boolean) true, "
+      "width = (int) 16, "
+      "depth = (int) 16, "
+      "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
+      "channels = (int) [ 1, 2 ]"
   )
-)
+);
 
-GST_PAD_TEMPLATE_FACTORY (gst_lame_src_factory,
+static GstStaticPadTemplate gst_lame_src_template =
+GST_STATIC_PAD_TEMPLATE (
   "src",
   GST_PAD_SRC,
   GST_PAD_ALWAYS,
-  GST_CAPS_NEW (
-    "gstlame_src",
-    "audio/mpeg",
-      "mpegversion", GST_PROPS_INT (1),
-      "layer",      GST_PROPS_INT (3),
-      "rate",       GST_PROPS_LIST (
-                     GST_PROPS_INT (8000), 
-                     GST_PROPS_INT (11025), 
-                     GST_PROPS_INT (12000), 
-                     GST_PROPS_INT (16000), 
-                     GST_PROPS_INT (22050),
-                     GST_PROPS_INT (24000),
-                     GST_PROPS_INT (32000),
-                     GST_PROPS_INT (44100),
-                     GST_PROPS_INT (48000)
-                   ),
-      "channels",   GST_PROPS_INT_RANGE (1, 2)
+  GST_STATIC_CAPS (
+    "audio/mpeg, "
+      "mpegversion = (int) 1, "
+      "layer = (int) 3, "
+      "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
+      "channels = (int) [ 1, 2 ]"
   )
-)
+);
 
 /********** Define useful types for non-programmatic interfaces **********/
 #define GST_TYPE_LAME_MODE (gst_lame_mode_get_type())
@@ -185,10 +162,9 @@ enum {
   ARG_ALLOW_DIFF_SHORT,
   ARG_NO_SHORT_BLOCKS,
   ARG_EMPHASIS,
-  ARG_METADATA,
 };
 
-
+static void                     gst_lame_base_init      (gpointer g_class);
 static void                    gst_lame_class_init     (GstLameClass *klass);
 static void                    gst_lame_init           (GstLame *gst_lame);
 
@@ -196,7 +172,7 @@ static void                 gst_lame_set_property   (GObject *object, guint prop_id,
                                                         const GValue *value, GParamSpec *pspec);
 static void                    gst_lame_get_property   (GObject *object, guint prop_id, 
                                                         GValue *value, GParamSpec *pspec);
-static void                    gst_lame_chain          (GstPad *pad, GstBuffer *buf);
+static void                    gst_lame_chain          (GstPad *pad, GstData *_data);
 static gboolean                gst_lame_setup          (GstLame *lame);
 static GstElementStateReturn   gst_lame_change_state   (GstElement *element);
 
@@ -211,7 +187,7 @@ gst_lame_get_type (void)
   if (!gst_lame_type) {
     static const GTypeInfo gst_lame_info = {
       sizeof (GstLameClass),      
-      NULL,
+      gst_lame_base_init,
       NULL,
       (GClassInitFunc) gst_lame_class_init,
       NULL,
@@ -220,12 +196,33 @@ gst_lame_get_type (void)
       0,
       (GInstanceInitFunc) gst_lame_init,
     };
+
+    static const GInterfaceInfo tag_setter_info = {
+      NULL,
+      NULL,
+      NULL
+    };
+
     gst_lame_type = g_type_register_static (GST_TYPE_ELEMENT, "GstLame", &gst_lame_info, 0);
+    g_type_add_interface_static (gst_lame_type, GST_TYPE_TAG_SETTER, &tag_setter_info);
+
   }
   return gst_lame_type;
 }
 
 static void
+gst_lame_base_init (gpointer g_class)
+{
+  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
+
+  gst_element_class_add_pad_template (element_class, 
+      gst_static_pad_template_get (&gst_lame_src_template));
+  gst_element_class_add_pad_template (element_class, 
+      gst_static_pad_template_get (&gst_lame_sink_template));
+  gst_element_class_set_details (element_class, &gst_lame_details);
+}
+
+static void
 gst_lame_class_init (GstLameClass *klass)
 {
   GObjectClass *gobject_class;
@@ -336,11 +333,6 @@ gst_lame_class_init (GstLameClass *klass)
     g_param_spec_boolean ("emphasis", "Emphasis", "Emphasis",
                           TRUE, G_PARAM_READWRITE));
 
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_METADATA,
-    g_param_spec_boxed ("metadata", "Metadata", "Metadata to add to the stream,",
-            GST_TYPE_CAPS, G_PARAM_READWRITE));
-
-
   gobject_class->set_property = gst_lame_set_property;
   gobject_class->get_property = gst_lame_get_property;
 
@@ -348,36 +340,37 @@ gst_lame_class_init (GstLameClass *klass)
 }
 
 static GstPadLinkReturn
-gst_lame_sinkconnect (GstPad *pad, GstCaps *caps)
+gst_lame_sink_link (GstPad *pad, const GstCaps *caps)
 {
   GstLame *lame;
+  gint out_samplerate;
+  GstStructure *structure;
+  GstCaps *othercaps;
 
   lame = GST_LAME (gst_pad_get_parent (pad));
+  structure = gst_caps_get_structure (caps, 0);
 
-  if (!GST_CAPS_IS_FIXED (caps)) {
-    GST_DEBUG ("caps on lame pad %s:%s not fixed, delayed",
-              GST_DEBUG_PAD_NAME (pad));
-    return GST_PAD_LINK_DELAYED;
-  }
-
-  gst_caps_get_int (caps, "rate", &lame->samplerate);
-  gst_caps_get_int (caps, "channels", &lame->num_channels);
+  gst_structure_get_int (structure, "rate", &lame->samplerate);
+  gst_structure_get_int (structure, "channels", &lame->num_channels);
 
   if (!gst_lame_setup (lame)) {
-    gst_element_gerror(GST_ELEMENT (lame), GST_ERROR_UNKNOWN,
-      g_strdup ("unconverted error, file a bug"),
-      g_strdup_printf("could not initialize encoder (wrong parameters?)"));
+    GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
+                      ("could not initialize encoder (wrong parameters?)"));
     return GST_PAD_LINK_REFUSED;
   }
 
-  caps = GST_CAPS_NEW ("lame_src_caps",
-                      "audio/mpeg",
-                         "mpegversion", GST_PROPS_INT (1),
-                        "layer",    GST_PROPS_INT (3),
-                        "channels", GST_PROPS_INT (lame->num_channels),
-                        "rate",     GST_PROPS_INT (lame->samplerate));
-
-  return gst_pad_try_set_caps (lame->srcpad, caps);
+  out_samplerate = lame_get_out_samplerate (lame->lgf);
+  othercaps = 
+         gst_caps_new_simple (
+                               "audio/mpeg",
+                                "mpegversion", G_TYPE_INT, 1,
+                               "layer", G_TYPE_INT, 3,
+                               "channels", G_TYPE_INT, lame->num_channels,
+                               "rate", G_TYPE_INT, out_samplerate,
+                               NULL
+                             );
+
+  return gst_pad_try_set_caps (lame->srcpad, othercaps);
 }
 
 static void
@@ -385,12 +378,14 @@ gst_lame_init (GstLame *lame)
 {
   GST_DEBUG_OBJECT (lame, "starting initialization");
 
-  lame->sinkpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (gst_lame_sink_factory), "sink");
+  lame->sinkpad = gst_pad_new_from_template (
+                   gst_static_pad_template_get (&gst_lame_sink_template), "sink");
   gst_element_add_pad (GST_ELEMENT (lame), lame->sinkpad);
   gst_pad_set_chain_function (lame->sinkpad, gst_lame_chain);
-  gst_pad_set_link_function (lame->sinkpad, gst_lame_sinkconnect);
+  gst_pad_set_link_function (lame->sinkpad, gst_lame_sink_link);
 
-  lame->srcpad = gst_pad_new_from_template (GST_PAD_TEMPLATE_GET (gst_lame_src_factory), "src");
+  lame->srcpad = gst_pad_new_from_template (
+                   gst_static_pad_template_get (&gst_lame_src_template), "src");
   gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
 
   GST_FLAG_SET (lame, GST_ELEMENT_EVENT_AWARE);
@@ -433,66 +428,105 @@ gst_lame_init (GstLame *lame)
   lame->allow_diff_short = lame_get_allow_diff_short (lame->lgf);
   lame->no_short_blocks = lame_get_no_short_blocks (lame->lgf);
   lame->emphasis = lame_get_emphasis (lame->lgf);
-
-  lame->metadata = GST_CAPS_NEW (
-      "lame_metadata",
-      "application/x-gst-metadata",
-      "comment",     GST_PROPS_STRING ("Track encoded with GStreamer"),
-      "year",        GST_PROPS_STRING (""),
-      "tracknumber", GST_PROPS_STRING (""),
-      "title",       GST_PROPS_STRING (""),
-      "artist",      GST_PROPS_STRING (""),
-      "album",       GST_PROPS_STRING (""),
-      "genre",       GST_PROPS_STRING ("")
-  );
+  lame->tags = gst_tag_list_new ();
 
   id3tag_init (lame->lgf);
   
   GST_DEBUG_OBJECT (lame, "done initializing");
 }
 
-static void
-gst_lame_add_metadata (GstLame *lame, GstCaps *caps)
+typedef struct _GstLameTagMatch GstLameTagMatch;
+typedef void (*GstLameTagFunc)(lame_global_flags* gfp, const char *value);
+
+struct _GstLameTagMatch
+{
+  gchar *gstreamer_tag;
+  GstLameTagFunc tag_func;
+};
+
+static GstLameTagMatch tag_matches[] = 
+  {
+    {GST_TAG_TITLE,        id3tag_set_title},
+    {GST_TAG_DATE,         id3tag_set_year},
+    {GST_TAG_TRACK_NUMBER, id3tag_set_track},
+    {GST_TAG_COMMENT,      id3tag_set_comment},
+    {GST_TAG_ARTIST,       id3tag_set_artist},
+    {GST_TAG_ALBUM,        id3tag_set_album},
+    {GST_TAG_GENRE,        (GstLameTagFunc)id3tag_set_genre},
+    {NULL,                 NULL}
+  };
+
+static void 
+add_one_tag (const GstTagList *list, const gchar *tag, 
+            gpointer user_data)
 {
-  GList *props;
-  GstPropsEntry *prop;
+  GstLame *lame;
+  gchar *value;
+  int i = 0;
 
-  if (caps == NULL)
+  lame = GST_LAME (user_data);
+  g_return_if_fail (lame != NULL);
+
+  while (tag_matches[i].gstreamer_tag != NULL) {
+    if (strcmp (tag, tag_matches[i].gstreamer_tag) == 0) {
+      break;
+    }
+    i++;
+  }
+  
+  if (tag_matches[i].tag_func == NULL) {
+    g_print ("Couldn't find matching gstreamer tag for %s\n", tag);
     return;
+  }
 
-  props = gst_caps_get_props (caps)->properties;
-  while (props) {
-    prop = (GstPropsEntry*)(props->data);
-    props = g_list_next(props);
-
-    if (gst_props_entry_get_props_type (prop) == GST_PROPS_STRING_TYPE) {
-      const gchar *name = gst_props_entry_get_name (prop);
-      const gchar *value;
-
-      gst_props_entry_get_string (prop, &value);
-
-      if (!value || strlen (value) == 0)
-        continue;
-
-      if (strcmp (name, "comment") == 0) {
-        id3tag_set_comment (lame->lgf, value);
-      } else if (strcmp (name, "date") == 0) {
-        id3tag_set_year (lame->lgf, value);
-      } else if (strcmp (name, "tracknumber") == 0) {
-        id3tag_set_track (lame->lgf, value);
-      } else if (strcmp (name, "title") == 0) {
-        id3tag_set_title (lame->lgf, value);
-      } else if (strcmp (name, "artist") == 0) {
-        id3tag_set_artist (lame->lgf, value);
-      } else if (strcmp (name, "album") == 0) {
-        id3tag_set_album (lame->lgf, value);
-      } else if (strcmp (name, "genre") == 0) {
-        id3tag_set_genre (lame->lgf, value);
-      }
+  switch (gst_tag_get_type (tag)) {
+  case G_TYPE_UINT: {
+    guint ivalue;
+    if (!gst_tag_list_get_uint (list, tag, &ivalue)) {      
+      GST_DEBUG ("Error reading \"%s\" tag value\n", tag);
+      return;
     }
+    value = g_strdup_printf ("%u", ivalue);
+    break;
+  }
+  case G_TYPE_STRING: 
+    if (!gst_tag_list_get_string (list, tag, &value)) {
+      GST_DEBUG ("Error reading \"%s\" tag value\n", tag);
+      return;
+    };
+    break;
+  default:
+    GST_DEBUG ("Couldn't write tag %s", tag);
+    break;
+  }
+
+  tag_matches[i].tag_func (lame->lgf, value);
+
+  if (gst_tag_get_type (tag) == G_TYPE_UINT) {
+    g_free (value);
+  }
+}
+
+static void
+gst_lame_set_metadata (GstLame *lame)
+{
+  const GstTagList *user_tags;
+  GstTagList *copy;
+
+  g_return_if_fail (lame != NULL);
+  user_tags = gst_tag_setter_get_list (GST_TAG_SETTER (lame));
+  if ((lame->tags == NULL) && (user_tags == NULL)) {
+    return;
   }
+  copy = gst_tag_list_merge (user_tags, lame->tags, 
+                            gst_tag_setter_get_merge_mode (GST_TAG_SETTER (lame)));
+  gst_tag_list_foreach ((GstTagList*)copy, add_one_tag, lame);
+
+  gst_tag_list_free (copy);
 }
 
+
+
 static void
 gst_lame_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
 {
@@ -594,9 +628,6 @@ gst_lame_set_property (GObject *object, guint prop_id, const GValue *value, GPar
     case ARG_EMPHASIS:
       lame->emphasis = g_value_get_boolean (value);
       break;
-    case ARG_METADATA:
-      lame->metadata = g_value_get_boxed (value);
-      break;
     default:
       break;
   }
@@ -704,9 +735,6 @@ gst_lame_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec
     case ARG_EMPHASIS:
       g_value_set_boolean (value, lame->emphasis);
       break;
-    case ARG_METADATA:
-      g_value_set_static_boxed (value, lame->metadata);
-      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -714,8 +742,9 @@ gst_lame_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec
 }
 
 static void
-gst_lame_chain (GstPad *pad, GstBuffer *buf)
+gst_lame_chain (GstPad *pad, GstData *_data)
 {
+  GstBuffer *buf = GST_BUFFER (_data);
   GstLame *lame;
   GstBuffer *outbuf;
   gchar *mp3_data = NULL;
@@ -737,6 +766,15 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
         mp3_size = lame_encode_flush (lame->lgf, mp3_data, mp3_buffer_size);
        gst_event_unref (GST_EVENT (buf));
         break; 
+      case GST_EVENT_TAG:
+       if (lame->tags) {
+         gst_tag_list_insert (lame->tags, gst_event_tag_get_list (GST_EVENT (buf)), 
+                 gst_tag_setter_get_merge_mode (GST_TAG_SETTER (lame)));
+       } else {
+         g_assert_not_reached ();
+       }       
+       //      gst_pad_event_default (pad, GST_EVENT (buf));
+       break;
       default:
        gst_pad_event_default (pad, GST_EVENT (buf));
        break;
@@ -747,9 +785,8 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
 
     if (!lame->initialized) {
       gst_buffer_unref (buf);
-      gst_element_gerror(GST_ELEMENT (lame), GST_ERROR_UNKNOWN,
-        g_strdup ("unconverted error, file a bug"),
-        g_strdup_printf("encoder not initialized (input is not audio?)"));
+      GST_ELEMENT_ERROR (lame, CORE, NEGOTIATION, (NULL),
+                         ("encoder not initialized (input is not audio?)"));
       return;
     }
 
@@ -804,7 +841,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
     GST_BUFFER_OFFSET (outbuf)    = lame->last_offs;
     GST_BUFFER_DURATION (outbuf)  = lame->last_duration;
 
-    gst_pad_push (lame->srcpad,outbuf);
+    gst_pad_push (lame->srcpad,GST_DATA (outbuf));
 
     lame->last_ts = GST_CLOCK_TIME_NONE;
   }
@@ -813,7 +850,7 @@ gst_lame_chain (GstPad *pad, GstBuffer *buf)
   }
 
   if (eos) {
-    gst_pad_push (lame->srcpad, GST_BUFFER (gst_event_new (GST_EVENT_EOS)));
+    gst_pad_push (lame->srcpad, GST_DATA (gst_event_new (GST_EVENT_EOS)));
     gst_element_set_eos (GST_ELEMENT (lame));
   }
 }
@@ -868,7 +905,7 @@ gst_lame_setup (GstLame *lame)
   lame_set_no_short_blocks (lame->lgf, lame->no_short_blocks);
   lame_set_emphasis (lame->lgf, lame->emphasis);
 
-  gst_lame_add_metadata (lame, lame->metadata);
+  gst_lame_set_metadata (lame);
 
   /* initialize the lame encoder */
   if (lame_init_params (lame->lgf) < 0) {
@@ -920,32 +957,21 @@ gst_lame_change_state (GstElement *element)
 }
 
 static gboolean
-plugin_init (GModule *module, GstPlugin *plugin)
+plugin_init (GstPlugin *plugin)
 {
-  GstElementFactory *factory;
-
-  /* create an elementfactory for the gst_lame element */
-  factory = gst_element_factory_new ("lame", GST_TYPE_LAME,
-                                    &gst_lame_details);
-  g_return_val_if_fail (factory != NULL, FALSE);
-
-  /* register the source's padtemplate */
-  gst_element_factory_add_pad_template (factory, 
-               GST_PAD_TEMPLATE_GET (gst_lame_src_factory));
-
-  /* register the sink's padtemplate */
-  gst_element_factory_add_pad_template (factory, 
-               GST_PAD_TEMPLATE_GET (gst_lame_sink_factory));
-
-  /* and add the gst_lame element factory to the plugin */
-  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
-
+  if (!gst_element_register (plugin, "lame", GST_RANK_NONE, GST_TYPE_LAME))
+    return FALSE;
+  
   return TRUE;
 }
 
-GstPluginDesc plugin_desc = {
+GST_PLUGIN_DEFINE (
   GST_VERSION_MAJOR,
   GST_VERSION_MINOR,
   "lame",
-  plugin_init
-};
+  "Encode MP3's with LAME",
+  plugin_init,
+  VERSION,
+  "LGPL",
+  GST_PACKAGE,
+  GST_ORIGIN)