用 Gstreamer 编写一个 mp3 播放器

用 Gstreamer 编写一个 mp3 播放器,支持循环播放


直接上代码

#include <string.h>
#include <stdio.h>
#include <gst/gst.h>

#include "gst_debug_out_define.h"


/* Structure to contain all our information, so we can pass it around */
typedef struct _CustomData {
	GstElement *playbin2;  /* Our one and only element */
	gboolean playing;      /* Are we in the PLAYING state? */
	gboolean terminate;    /* Should we terminate execution? */
	gboolean seek_enabled; /* Is seeking enabled for this media? */
	gboolean seek_done;    /* Have we performed the seek already? */
	gint64 duration;       /* How long does this media last, in nanoseconds */
	gdouble rate;          /* Current playback rate (can be negative) */
	GMainLoop *loop;
} CustomData;

/* Forward definition of the message processing function */
static void handle_message (CustomData *data, GstMessage *msg);



/* Process keyboard input */
static gboolean handle_keyboard (GIOChannel *source, GIOCondition cond, CustomData *data) {
	g_print ("\nhandle_keyboard\n");
	
	gchar *str = NULL;
	
	if (g_io_channel_read_line (source, &str, NULL, NULL, NULL) != G_IO_STATUS_NORMAL) {
		return TRUE;
	}
	
	switch (g_ascii_tolower (str[0])) {
	case 'p':
		data->playing = !data->playing;
		gst_element_set_state (data->playbin2, data->playing ? GST_STATE_PLAYING : GST_STATE_PAUSED);
		g_print ("Setting state to %s\n", data->playing ? "PLAYING" : "PAUSE");
		break;
		
	case 'q':
		data->terminate = TRUE;
		break;
	default:
		break;
	}
	
	g_free (str);
	
	return TRUE;
}


int main(int argc, char *argv[]) {
	CustomData data;
	GstBus *bus;
	GstMessage *msg;
	GstStateChangeReturn ret;
	
	GIOChannel *io_stdin;
	
	data.playing = FALSE;
	data.terminate = FALSE;
	data.seek_enabled = FALSE;
	data.seek_done = FALSE;
	data.duration = GST_CLOCK_TIME_NONE;
	
	/* Initialize GStreamer */
	gst_init (&argc, &argv);
	
	/* Init gst_debug_out */
	USE_OWN_SAME_DEBUG;
	gst_debug_set_threshold_for_name("mydebugout", GST_LEVEL_LOG);
	GST_INFO ("Test message1 output by GST_LOG_SYSTEM\n");
	
	/* Create the elements */
	data.playbin2 = gst_element_factory_make ("playbin", "playbin");
	
	if (!data.playbin2) {
		g_printerr ("Not all elements could be created.\n");
		return -1;
	}
	
	/* Set the URI to play */
	g_object_set (data.playbin2, "uri", "file:///mnt/hgfs/works/projects/03_VCU/mediaFile/05.mp3", NULL);
	
	
	/* Add a keyboard watch so we get notified of keystrokes */
#ifdef _WIN32
	io_stdin = g_io_channel_win32_new_fd (fileno (stdin));
#else
	io_stdin = g_io_channel_unix_new (fileno (stdin));
#endif
	g_io_add_watch (io_stdin, G_IO_IN, (GIOFunc)handle_keyboard, &data);
	
	
	/* Start playing */
	ret = gst_element_set_state (data.playbin2, GST_STATE_PLAYING);
	if (ret == GST_STATE_CHANGE_FAILURE) {
		g_printerr ("Unable to set the pipeline to the playing state.\n");
		gst_object_unref (data.playbin2);
		return -1;
	}
	
	/* Listen to the bus */
	bus = gst_element_get_bus (data.playbin2);
	do {
		msg = gst_bus_timed_pop_filtered (bus, 100 * GST_MSECOND, GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);
		
		/* Parse message */
		if (msg != NULL) {
			handle_message (&data, msg);
		} else {
			/* We got no message, this means the timeout expired */
			if (data.playing) {
				GstFormat fmt = GST_FORMAT_TIME;
				gint64 current = -1;
				
				/* Query the current position of the stream */
				if (!gst_element_query_position (data.playbin2, fmt, &current)) {
					g_printerr ("Could not query current position.\n");
				}
				
				/* If we didn't know it yet, query the stream duration */
				if (!GST_CLOCK_TIME_IS_VALID (data.duration)) {
					if (!gst_element_query_duration (data.playbin2, fmt, &data.duration)) {
						g_printerr ("Could not query current duration.\n");
					}
				}
				
				/* Print current position and total duration */
				GST_TIME_ARGS (current), GST_TIME_ARGS (data.duration));
			}
		}
	} while (!data.terminate);
	
	/* Free resources */
	gst_object_unref (bus);
	g_io_channel_unref (io_stdin);
	gst_element_set_state (data.playbin2, GST_STATE_NULL);
	gst_object_unref (data.playbin2);
	return 0;
}

static void handle_message (CustomData *data, GstMessage *msg) {
	GError *err;
	gchar *debug_info;
	
	switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_ERROR:
		gst_message_parse_error (msg, &err, &debug_info);
		g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
		g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
		g_clear_error (&err);
		g_free (debug_info);
		data->terminate = TRUE;
		break;
    case GST_MESSAGE_EOS:
		g_print ("\nEnd-Of-Stream reached.\n\n");
		gst_element_seek_simple (data->playbin2, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 0 * GST_SECOND);
		//     data->terminate = TRUE;
		break;
    case GST_MESSAGE_DURATION:
		/* The duration has changed, mark the current one as invalid */
		data->duration = GST_CLOCK_TIME_NONE;
		break;
    case GST_MESSAGE_STATE_CHANGED: {
		GstState old_state, new_state, pending_state;
		gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
		if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data->playbin2)) {
			g_print ("Pipeline state changed from %s to %s:\n",
				gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
			
			/* Remember whether we are in the PLAYING state or not */
			data->playing = (new_state == GST_STATE_PLAYING);
			
			if (data->playing) {
				/* We just moved to PLAYING. Check if seeking is possible */
				GstQuery *query;
				gint64 start, end;
				query = gst_query_new_seeking (GST_FORMAT_TIME);
				if (gst_element_query (data->playbin2, query)) {
					gst_query_parse_seeking (query, NULL, &data->seek_enabled, &start, &end);
					if (data->seek_enabled) {
						g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",
							GST_TIME_ARGS (start), GST_TIME_ARGS (end));
					} else {
						g_print ("Seeking is DISABLED for this stream.\n");
					}
				}
				else {
					g_printerr ("Seeking query failed.");
				}
				gst_query_unref (query);
			}
		}
									} break;
    default:
		/* We should not reach here */
		g_printerr ("Unexpected message received.\n");
		break;
	}
	gst_message_unref (msg);
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值