ext/lame/gstlame.c: Allow fixing the sample rate lame converts to by negotiating...
authorThomas Vander Stichele <thomas@apestaart.org>
Mon, 24 Sep 2007 10:26:21 +0000 (10:26 +0000)
committerThomas Vander Stichele <thomas@apestaart.org>
Mon, 24 Sep 2007 10:26:21 +0000 (10:26 +0000)
Original commit message from CVS:
* ext/lame/gstlame.c:
Allow fixing the sample rate lame converts to by negotiating fixed
sample rate on the src pad caps.
Add docs for it.
* tests/check/Makefile.am:
* tests/check/pipelines/lame.c:
Add a check for it.

ext/lame/gstlame.c
tests/check/pipelines/lame.c [new file with mode: 0644]

index 984214f..8bff507 100644 (file)
  * consideration. See <ulink url="http://www.vorbis.com/">Ogg/Vorbis</ulink>
  * for a royalty free (and often higher quality) alternative.
  * </para>
+ * <title>Output sample rate</title>
+ * <para>
+ * If no fixed output sample rate is negotiated on the element's src pad,
+ * the element will choose an optimal sample rate to resample to internally.
+ * For example, a 16-bit 44.1 KHz mono audio stream encoded at 48 kbit will
+ * get resampled to 32 KHz.  Use filter caps on the src pad to force a
+ * particular sample rate.
+ * </para>
  * <title>Writing metadata (tags)</title>
  * <para>
  * Whilst the lame encoder element does claim to implement the GstTagSetter
  * <programlisting>
  * gst-launch -v cdda://5 ! audioconvert ! lame bitrate=192 ! filesink location=track5.mp3
  * </programlisting>
+ * <para>
+ * Encode to a fixed sample rate:
+ * </para>
+ * <programlisting>
+ * gst-launch -v audiotestsrc num-buffers=10 ! audio/x-raw-int,rate=44100,channels=1 ! lame bitrate=48 mode=3 ! filesink location=test.mp3
+ * </programlisting>
  * </refsect2>
  *
- * Last reviewed on 2006-10-13 (0.10.4)
+ * Last reviewed on 2007-07-24 (0.10.7)
  */
 
 #ifdef HAVE_CONFIG_H
@@ -496,6 +510,13 @@ gst_lame_class_init (GstLameClass * klass)
 }
 
 static gboolean
+gst_lame_src_setcaps (GstPad * pad, GstCaps * caps)
+{
+  GST_DEBUG_OBJECT (pad, "src_setcaps %s", gst_caps_to_string (caps));
+  return TRUE;
+}
+
+static gboolean
 gst_lame_sink_setcaps (GstPad * pad, GstCaps * caps)
 {
   GstLame *lame;
@@ -579,6 +600,8 @@ gst_lame_init (GstLame * lame)
 
   lame->srcpad =
       gst_pad_new_from_static_template (&gst_lame_src_template, "src");
+  gst_pad_set_setcaps_function (lame->srcpad,
+      GST_DEBUG_FUNCPTR (gst_lame_src_setcaps));
   gst_element_add_pad (GST_ELEMENT (lame), lame->srcpad);
 
   /* create an encoder state so we can ask about defaults */
@@ -1130,13 +1153,18 @@ init_error:
 static gboolean
 gst_lame_setup (GstLame * lame)
 {
+
 #define CHECK_ERROR(command) G_STMT_START {\
   if ((command) < 0) { \
     GST_ERROR_OBJECT (lame, "setup failed: " G_STRINGIFY (command)); \
     return FALSE; \
   } \
 }G_STMT_END
+
   int retval;
+  GstCaps *src_caps;
+  GstStructure *structure;
+  gint samplerate;
 
   GST_DEBUG_OBJECT (lame, "starting setup");
 
@@ -1153,12 +1181,22 @@ gst_lame_setup (GstLame * lame)
   if (lame->lgf == NULL)
     return FALSE;
 
-  /* let lame choose a default samplerate */
-  lame_set_out_samplerate (lame->lgf, 0);
-
   /* copy the parameters over */
   lame_set_in_samplerate (lame->lgf, lame->samplerate);
 
+  /* let lame choose default samplerate unless outgoing sample rate is fixed */
+  src_caps = gst_pad_get_allowed_caps (lame->srcpad);
+  structure = gst_caps_get_structure (src_caps, 0);
+
+  if (gst_structure_get_int (structure, "rate", &samplerate)) {
+    GST_DEBUG_OBJECT (lame, "Setting sample rate to %d as fixed in src caps",
+        samplerate);
+    lame_set_out_samplerate (lame->lgf, samplerate);
+  } else {
+    GST_DEBUG_OBJECT (lame, "Letting lame choose sample rate");
+    lame_set_out_samplerate (lame->lgf, 0);
+  }
+
   /* force mono encoding if we only have one channel */
   if (lame->num_channels == 1)
     lame->mode = 3;
diff --git a/tests/check/pipelines/lame.c b/tests/check/pipelines/lame.c
new file mode 100644 (file)
index 0000000..e84d565
--- /dev/null
@@ -0,0 +1,99 @@
+/* GStreamer
+ *
+ * unit test for lame
+ *
+ * Copyright (C) 2007 Thomas Vander Stichele <thomas at apestaart dot org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <gst/check/gstcheck.h>
+#include <gst/check/gstbufferstraw.h>
+
+#ifndef GST_DISABLE_PARSE
+
+GST_START_TEST (test_format)
+{
+  GstElement *bin;
+  GstPad *pad;
+  gchar *pipe_str;
+  GstBuffer *buffer;
+  GError *error = NULL;
+
+  pipe_str = g_strdup_printf ("audiotestsrc num-buffers=1 "
+      "! audio/x-raw-int, rate=22050, channels=1 "
+      "! lame bitrate=24 mode=3 " "! audio/mpeg,rate=22050 ! fakesink");
+
+  bin = gst_parse_launch (pipe_str, &error);
+  fail_unless (bin != NULL, "Error parsing pipeline: %s",
+      error ? error->message : "(invalid error)");
+  g_free (pipe_str);
+
+  /* get the pad */
+  {
+    GstElement *sink = gst_bin_get_by_name (GST_BIN (bin), "fakesink0");
+
+    fail_unless (sink != NULL, "Could not get fakesink out of bin");
+    pad = gst_element_get_pad (sink, "sink");
+    fail_unless (pad != NULL, "Could not get pad out of fakesink");
+    gst_object_unref (sink);
+  }
+
+  gst_buffer_straw_start_pipeline (bin, pad);
+
+  buffer = gst_buffer_straw_get_buffer (bin, pad);
+
+  gst_buffer_straw_stop_pipeline (bin, pad);
+
+  gst_object_unref (pad);
+  gst_object_unref (bin);
+}
+
+GST_END_TEST;
+
+#endif /* #ifndef GST_DISABLE_PARSE */
+
+Suite *
+lame_suite (void)
+{
+  Suite *s = suite_create ("lame");
+  TCase *tc_chain = tcase_create ("general");
+
+  suite_add_tcase (s, tc_chain);
+
+#ifndef GST_DISABLE_PARSE
+  tcase_add_test (tc_chain, test_format);
+#endif
+
+  return s;
+}
+
+int
+main (int argc, char **argv)
+{
+  int nf;
+
+  Suite *s = lame_suite ();
+  SRunner *sr = srunner_create (s);
+
+  gst_check_init (&argc, &argv);
+
+  srunner_run_all (sr, CK_NORMAL);
+  nf = srunner_ntests_failed (sr);
+  srunner_free (sr);
+
+  return nf;
+}