/* playmp3.c */
#include <gst/gst.h> #include <unistd.h> void show_version(void); int main(int argc, char *argv[]) { GstElement *pipeline, *filesrc, *decoder, *conv, *audiosink; gchar *name; GstElementFactory *factory; GstState *state; gchar *status; gst_init(&argc, &argv); show_version(); if (argc != 2) { g_print("usage: %s <filename>\n", argv[0]); return -1; } /* 创建一条新的管道 */ pipeline = gst_pipeline_new("pipeline"); /* 生成用于读取硬盘数据的元件 */ filesrc = gst_element_factory_make("filesrc", "file-source"); g_object_set(G_OBJECT(filesrc), "location", argv[1], NULL); /* get name */ g_object_get (G_OBJECT (filesrc), "name", &name, NULL); g_print ("The name of the element is '%s'.\n", name); g_free (name); /* get factory */ factory = gst_element_factory_find ("filesrc"); if (!factory) { g_print ("You don't have the 'filesrc' element installed!\n"); return -1; } /* display information */ g_print ("The '%s' element is a member of the category %s.\n" "Description: %s\n", gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)), gst_element_factory_get_klass (factory), gst_element_factory_get_description (factory)); /* 创建解码器元件 */ // decoder = gst_element_factory_make ("wavparse", "wav-decoder"); decoder = gst_element_factory_make("mad", "mad-decoder"); conv = gst_element_factory_make("audioconvert", "converter"); /* 创建音频回放元件 */ audiosink = gst_element_factory_make("autoaudiosink", "audio-output"); if (!pipeline || !filesrc || !decoder || !conv || !audiosink) { g_print("One element could not be created\n"); g_print("%p %p %p %p %p\n", pipeline, filesrc, decoder, conv, audiosink); return -1; } /* 将生成的元件添加到管道中 */ gst_bin_add_many(GST_BIN(pipeline), filesrc, decoder, conv, audiosink, NULL); /* 连接各个元件 */ if (!gst_element_link_many(filesrc, decoder, conv, audiosink, NULL)) { g_warning("Failed to link elements!"); return -1; } /* 开始播放 */ gst_element_set_state(pipeline, GST_STATE_PLAYING); gst_element_get_state (pipeline, state, NULL, 1000); switch (*state) { case GST_STATE_VOID_PENDING: status = "VOID_PENDING"; break; case GST_STATE_NULL: status = "NULL"; break; case GST_STATE_READY: status = "READY"; break; case GST_STATE_PAUSED: status = "PAUSED"; break; case GST_STATE_PLAYING: status = "PLAYING"; break; default: status = "UNKNOWN"; break; } g_print("\rpipeline status: %s", status); while (gst_bin_iterate_recurse(GST_BIN(pipeline))) { usleep(100); } gst_element_get_state (pipeline, state, NULL, 1000); switch (*state) { case GST_STATE_VOID_PENDING: status = "VOID_PENDING"; break; case GST_STATE_NULL: status = "NULL"; break; case GST_STATE_READY: status = "READY"; break; case GST_STATE_PAUSED: status = "PAUSED"; break; case GST_STATE_PLAYING: status = "PLAYING"; break; default: status = "UNKNOWN"; break; } g_print("\rpipeline status: %s", status); /* 停止管道处理流程 */ gst_element_set_state(pipeline, GST_STATE_NULL); /* 释放占用的资源 */ gst_object_unref(GST_OBJECT(pipeline)); return 0; } void show_version(void) { const gchar *nano_str; guint major, minor, micro, nano; gst_version(&major, &minor, µ, &nano); if (nano == 1) nano_str = "(CVS)"; else if (nano == 2) nano_s