glib gobject gstreamer 安装、测试

默认分类 · 2012-06-13

一、概述

1、gobject是用c语言实现的面向对象的编程啥?不知道。

2、glib含有实现gobject相关的库。

3、gstreamer是基于 gobject库的一个流媒体库。

二、安装

1、我用的ubuntu server 12.04

sudo apt-get install libgstreamer0.10-dev
sudo apt-get install libglib2.0-dev

2、前面还装了啥搞忘了,不过大体应该还装这么些东西

sudo apt-get install libgstreamer*
sudo apt-get install gstreamer-0.10 gstreamer0.10-tools

安装gstreamer0.10-tools主要是为了使用gst-launch

三、测试

1、测试gobject

(1)test.c源程序

#include <glib.h> 
#include <glib/gstdio.h> 
#include "/usr/include/glib-2.0/glib/gprintf.h"
int main(void) 
{ 
g_printf("Hello world!\n"); 
} 

(2)编译

gcc  `pkg-config --cflags --libs glib-2.0`  test.c -o test

编译出错,编译结果为:

/tmp/ccxhQhB2.o: In function `main':
test.c:(.text+0xf): undefined reference to `g_printf'
collect2: ld returned 1 exit status

解决办法:

gcc  `pkg-config --cflags glib-2.0`  test.c -o test `pkg-config --libs glib-2.0`

顺利编译完成

./test

输出:

Hello world!

2、测试gstreamer

(1)helloworld.c源程序

/*** block  from ../../../docs/manual/basics-helloworld.xml ***/
#include <gst/gst.h>
#include <glib.h>


static gboolean
bus_call (GstBus     *bus,
          GstMessage *msg,
          gpointer    data)
{
  GMainLoop *loop = (GMainLoop *) data;

  switch (GST_MESSAGE_TYPE (msg)) {

    case GST_MESSAGE_EOS:
      g_print ("End of stream\n");
      g_main_loop_quit (loop);
      break;

    case GST_MESSAGE_ERROR: {
      gchar  *debug;
      GError *error;

      gst_message_parse_error (msg, &error, &debug);
      g_free (debug);

      g_printerr ("Error: %s\n", error->message);
      g_error_free (error);

      g_main_loop_quit (loop);
      break;
    }
    default:
      break;
  }

  return TRUE;
}


static void
on_pad_added (GstElement *element,
              GstPad     *pad,
              gpointer    data)
{
  GstPad *sinkpad;
  GstElement *decoder = (GstElement *) data;

  /* We can now link this pad with the vorbis-decoder sink pad */
  g_print ("Dynamic pad created, linking demuxer/decoder\n");

  sinkpad = gst_element_get_static_pad (decoder, "sink");

  gst_pad_link (pad, sinkpad);

  gst_object_unref (sinkpad);
}



int
main (int   argc,
      char *argv[])
{
  GMainLoop *loop;

  GstElement *pipeline, *source, *demuxer, *decoder, *conv, *sink;
  GstBus *bus;

  /* Initialisation */
  gst_init (&argc, &argv);

  loop = g_main_loop_new (NULL, FALSE);


  /* Check input arguments */
  if (argc != 2) {
    g_printerr ("Usage: %s <Ogg/Vorbis filename>\n", argv[0]);
    return -1;
  }


  /* Create gstreamer elements */
  pipeline = gst_pipeline_new ("audio-player");
  source   = gst_element_factory_make ("filesrc",       "file-source");
  demuxer  = gst_element_factory_make ("oggdemux",      "ogg-demuxer");
  decoder  = gst_element_factory_make ("vorbisdec",     "vorbis-decoder");
  conv     = gst_element_factory_make ("audioconvert",  "converter");
  sink     = gst_element_factory_make ("autoaudiosink", "audio-output");

  if (!pipeline || !source || !demuxer || !decoder || !conv || !sink) {
    g_printerr ("One element could not be created. Exiting.\n");
    return -1;
  }

  /* Set up the pipeline */

  /* we set the input filename to the source element */
  g_object_set (G_OBJECT (source), "location", argv[1], NULL);

  /* we add a message handler */
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
  gst_bus_add_watch (bus, bus_call, loop);
  gst_object_unref (bus);

  /* we add all elements into the pipeline */
  /* file-source | ogg-demuxer | vorbis-decoder | converter | alsa-output */
  gst_bin_add_many (GST_BIN (pipeline),
                    source, demuxer, decoder, conv, sink, NULL);

  /* we link the elements together */
  /* file-source -> ogg-demuxer ~> vorbis-decoder -> converter -> alsa-output */
  gst_element_link (source, demuxer);
  gst_element_link_many (decoder, conv, sink, NULL);
  g_signal_connect (demuxer, "pad-added", G_CALLBACK (on_pad_added), decoder);

  /* note that the demuxer will be linked to the decoder dynamically.
     The reason is that Ogg may contain various streams (for example
     audio and video). The source pad(s) will be created at run time,
     by the demuxer when it detects the amount and nature of streams.
     Therefore we connect a callback function which will be executed
     when the "pad-added" is emitted.*/


  /* Set the pipeline to "playing" state*/
  g_print ("Now playing: %s\n", argv[1]);
  gst_element_set_state (pipeline, GST_STATE_PLAYING);


  /* Iterate */
  g_print ("Running...\n");
  g_main_loop_run (loop);


  /* Out of the main loop, clean up nicely */
  g_print ("Returned, stopping playback\n");
  gst_element_set_state (pipeline, GST_STATE_NULL);

  g_print ("Deleting pipeline\n");
  gst_object_unref (GST_OBJECT (pipeline));

  return 0;
}

(2)编译

1)用gstreamer manual.pdf自带的
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) helloworld.c -o helloworld

会出错,出错信息如下

/tmp/ccx6Nptw.o: In function `bus_call':
helloworld.c:(.text+0x3a): undefined reference to `g_print'
helloworld.c:(.text+0x46): undefined reference to `g_main_loop_quit'
helloworld.c:(.text+0x5f): undefined reference to `gst_message_parse_error'
helloworld.c:(.text+0x6b): undefined reference to `g_free'
helloworld.c:(.text+0x85): undefined reference to `g_printerr'
helloworld.c:(.text+0x91): undefined reference to `g_error_free'
helloworld.c:(.text+0x9d): undefined reference to `g_main_loop_quit'
/tmp/ccx6Nptw.o: In function `on_pad_added':
helloworld.c:(.text+0xd2): undefined reference to `g_print'
helloworld.c:(.text+0xe3): undefined reference to `gst_element_get_static_pad'
helloworld.c:(.text+0xfa): undefined reference to `gst_pad_link'
helloworld.c:(.text+0x106): undefined reference to `gst_object_unref'
/tmp/ccx6Nptw.o: In function `main':
helloworld.c:(.text+0x12b): undefined reference to `gst_init'
helloworld.c:(.text+0x13a): undefined reference to `g_main_loop_new'
helloworld.c:(.text+0x15f): undefined reference to `g_printerr'
helloworld.c:(.text+0x173): undefined reference to `gst_pipeline_new'
helloworld.c:(.text+0x186): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x199): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x1ac): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x1bf): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x1d2): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x20f): undefined reference to `g_printerr'
helloworld.c:(.text+0x235): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x24f): undefined reference to `g_object_set'
helloworld.c:(.text+0x254): undefined reference to `gst_pipeline_get_type'
helloworld.c:(.text+0x266): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x26e): undefined reference to `gst_pipeline_get_bus'
helloworld.c:(.text+0x287): undefined reference to `gst_bus_add_watch'
helloworld.c:(.text+0x293): undefined reference to `gst_object_unref'
helloworld.c:(.text+0x298): undefined reference to `gst_bin_get_type'
helloworld.c:(.text+0x2aa): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x2d9): undefined reference to `gst_bin_add_many'
helloworld.c:(.text+0x2ec): undefined reference to `gst_element_link'
helloworld.c:(.text+0x30a): undefined reference to `gst_element_link_many'
helloworld.c:(.text+0x330): undefined reference to `g_signal_connect_data'
helloworld.c:(.text+0x34d): undefined reference to `g_print'
helloworld.c:(.text+0x35e): undefined reference to `gst_element_set_state'
helloworld.c:(.text+0x36d): undefined reference to `g_print'
helloworld.c:(.text+0x379): undefined reference to `g_main_loop_run'
helloworld.c:(.text+0x388): undefined reference to `g_print'
helloworld.c:(.text+0x399): undefined reference to `gst_element_set_state'
helloworld.c:(.text+0x3a8): undefined reference to `g_print'
helloworld.c:(.text+0x3ad): undefined reference to `gst_object_get_type'
helloworld.c:(.text+0x3bf): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x3c7): undefined reference to `gst_object_unref'
collect2: ld returned 1 exit status
2)更改编译选项后如下:
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) helloworld.c -o helloworld `pkg-config --libs glib-2.0`

还是会出错,出错结果如下:

/tmp/ccpJd8nF.o: In function `bus_call':
helloworld.c:(.text+0x5f): undefined reference to `gst_message_parse_error'
/tmp/ccpJd8nF.o: In function `on_pad_added':
helloworld.c:(.text+0xe3): undefined reference to `gst_element_get_static_pad'
helloworld.c:(.text+0xfa): undefined reference to `gst_pad_link'
helloworld.c:(.text+0x106): undefined reference to `gst_object_unref'
/tmp/ccpJd8nF.o: In function `main':
helloworld.c:(.text+0x12b): undefined reference to `gst_init'
helloworld.c:(.text+0x173): undefined reference to `gst_pipeline_new'
helloworld.c:(.text+0x186): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x199): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x1ac): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x1bf): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x1d2): undefined reference to `gst_element_factory_make'
helloworld.c:(.text+0x235): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x24f): undefined reference to `g_object_set'
helloworld.c:(.text+0x254): undefined reference to `gst_pipeline_get_type'
helloworld.c:(.text+0x266): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x26e): undefined reference to `gst_pipeline_get_bus'
helloworld.c:(.text+0x287): undefined reference to `gst_bus_add_watch'
helloworld.c:(.text+0x293): undefined reference to `gst_object_unref'
helloworld.c:(.text+0x298): undefined reference to `gst_bin_get_type'
helloworld.c:(.text+0x2aa): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x2d9): undefined reference to `gst_bin_add_many'
helloworld.c:(.text+0x2ec): undefined reference to `gst_element_link'
helloworld.c:(.text+0x30a): undefined reference to `gst_element_link_many'
helloworld.c:(.text+0x330): undefined reference to `g_signal_connect_data'
helloworld.c:(.text+0x35e): undefined reference to `gst_element_set_state'
helloworld.c:(.text+0x399): undefined reference to `gst_element_set_state'
helloworld.c:(.text+0x3ad): undefined reference to `gst_object_get_type'
helloworld.c:(.text+0x3bf): undefined reference to `g_type_check_instance_cast'
helloworld.c:(.text+0x3c7): undefined reference to `gst_object_unref'
collect2: ld returned 1 exit status
3)再更改编译选项进行编译
gcc -Wall $(pkg-config --cflags --libs gstreamer-0.10) helloworld.c -o helloworld `pkg-config --libs glib-2.0` `pkg-config --libs gstreamer-0.10`

编译顺利通过。

4)./helloworld How_Does_If_Feel.ogg

就能听到音乐声了。。。

我靠,真不容易啊

编程
Theme Jasmine by Kent Liao