summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2015-02-07 16:14:36 +0100
committerJavier <dev.git@javispedro.com>2015-02-07 16:14:36 +0100
commitd49b356a0df3a0053126144177e6f364bef91b34 (patch)
tree325ef7f676f604c96345e119e23c5bc5a3d71d59
downloadgplay-d49b356a0df3a0053126144177e6f364bef91b34.tar.gz
gplay-d49b356a0df3a0053126144177e6f364bef91b34.zip
initial import
-rw-r--r--.gitignore3
-rw-r--r--Makefile18
-rw-r--r--gplay.c327
-rw-r--r--gplay.config1
-rw-r--r--gplay.creator1
-rw-r--r--gplay.files2
-rw-r--r--gplay.includes8
7 files changed, 360 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..f451cb5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.o
+gplay
+gplay.creator.user
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..9a0e338
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+CFLAGS?=-Wall -ggdb -O2
+
+gplay_pkgs:=gstreamer-video-1.0 gtk+-2.0
+gplay_cflags:=$(shell pkg-config --cflags $(gplay_pkgs))
+gplay_libs:=$(shell pkg-config --libs $(gplay_pkgs))
+
+gplay_objs:=gplay.o
+
+all: gplay
+
+gplay: $(gplay_objs)
+ $(CC) $(LDFLAGS) -o $@ $+ $(gplay_libs)
+
+$(gplay_objs): %.o: %.c
+ $(CC) $(gplay_cflags) $(CFLAGS) -o $@ -c $<
+
+clean:
+ rm -f gplay *.o
diff --git a/gplay.c b/gplay.c
new file mode 100644
index 0000000..4702e00
--- /dev/null
+++ b/gplay.c
@@ -0,0 +1,327 @@
+#include <stdlib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <gdk/gdkx.h>
+#include <gst/gst.h>
+#include <gst/video/videooverlay.h>
+
+static GtkWindow *main_win;
+static GtkWidget *video_widget;
+static GtkScale *slider_widget;
+static GtkToolItem *play_tool, *pause_tool;
+
+static GstElement *playbin;
+
+static gboolean window_size_updated;
+static gint video_width, video_height;
+static gint64 media_duration;
+
+static gulong slider_changed_connection_id = 0;
+static gulong position_update_source = 0;
+
+static void play_media()
+{
+ if (!playbin) {
+ g_debug("nothing to play");
+ return;
+ }
+ gst_element_set_state(playbin, GST_STATE_PLAYING);
+}
+
+static void pause_media()
+{
+ g_return_if_fail(playbin);
+ gst_element_set_state(playbin, GST_STATE_PAUSED);
+}
+
+static void update_position_ui()
+{
+ GstFormat fmt = GST_FORMAT_TIME;
+ gint64 current;
+
+ if (!GST_CLOCK_TIME_IS_VALID(media_duration)) {
+ if (!gst_element_query_duration(playbin, fmt, &media_duration)) {
+ g_warning("Could not query current duration.\n");
+ } else {
+ /* Set the range of the slider to the clip duration, in SECONDS */
+ gtk_range_set_range(GTK_RANGE(slider_widget), 0, (gdouble)media_duration / GST_SECOND);
+ }
+ }
+
+
+ if (gst_element_query_position(playbin, fmt, &current)) {
+ g_signal_handler_block(slider_widget, slider_changed_connection_id);
+ gtk_range_set_value(GTK_RANGE(slider_widget), (gdouble)current / GST_SECOND);
+ g_signal_handler_unblock(slider_widget, slider_changed_connection_id);
+ }
+}
+
+static void slider_widget_handle_value_changed(GtkRange *range, gpointer user_data) {
+ gdouble value = gtk_range_get_value(range);
+ gst_element_seek_simple(playbin, GST_FORMAT_TIME,
+ GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT,
+ (gint64)(value * GST_SECOND));
+}
+
+static void video_widget_handle_realize(GtkWidget *widget, gpointer user_data)
+{
+ GdkWindow *window = gtk_widget_get_window(widget);
+ g_return_if_fail(window);
+
+ gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(playbin),
+ GDK_WINDOW_XID(window));
+}
+
+static gboolean second_tick(gpointer user_data)
+{
+ update_position_ui();
+
+ return G_SOURCE_CONTINUE;
+}
+
+static void bus_handle_error(GstBus *bus, GstMessage *msg, gpointer user_data)
+{
+ GError *error = NULL;
+ gchar *debug_info;
+ gst_message_parse_error(msg, &error, &debug_info);
+ g_warning("Error received from element '%s': %s",
+ GST_OBJECT_NAME(GST_MESSAGE_SRC(msg)),
+ error->message);
+ if (debug_info) {
+ g_debug("Debug info: %s", debug_info);
+ }
+ g_error_free(error);
+ g_free(debug_info);
+}
+
+static void bus_handle_eos(GstBus *bus, GstMessage *msg, gpointer user_data)
+{
+ g_debug("End-Of-Stream reached");
+ gst_element_set_state(playbin, GST_STATE_READY);
+}
+
+static void bus_handle_state_changed(GstBus *bus, GstMessage *msg, gpointer user_data)
+{
+ 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(playbin)) {
+ g_debug("playbin state change: %s", gst_element_state_get_name(new_state));
+ gboolean update_size;
+ switch(new_state) {
+ case GST_STATE_PLAYING:
+ gtk_widget_hide(GTK_WIDGET(play_tool));
+ gtk_widget_show(GTK_WIDGET(pause_tool));
+ update_size = !window_size_updated;
+ if (!position_update_source) {
+ position_update_source = g_timeout_add_seconds(1, second_tick, NULL);
+ }
+ break;
+ case GST_STATE_PAUSED:
+ gtk_widget_hide(GTK_WIDGET(pause_tool));
+ gtk_widget_show(GTK_WIDGET(play_tool));
+ update_size = !window_size_updated;
+ if (position_update_source) {
+ g_source_remove(position_update_source);
+ position_update_source = 0;
+ }
+ update_position_ui();
+ break;
+ default:
+ gtk_widget_hide(GTK_WIDGET(pause_tool));
+ gtk_widget_show(GTK_WIDGET(play_tool));
+ update_size = FALSE;
+ if (position_update_source) {
+ g_source_remove(position_update_source);
+ position_update_source = 0;
+ }
+ break;
+ }
+
+
+ if (update_size) {
+ GstPad *pad;
+ g_signal_emit_by_name(playbin, "get-video-pad", 0, &pad);
+ if (pad) {
+ GstCaps *caps = gst_pad_get_current_caps(pad);
+ if (caps) {
+ GstStructure *s = gst_caps_get_structure(caps, 0);
+ gint width, height;
+ gst_structure_get_int(s, "width", &video_width);
+ gst_structure_get_int(s, "height", &video_height);
+ g_debug("video width height %dx%d", width, height);
+
+ GtkAllocation allocation, window_allocation;
+ gtk_widget_get_allocation(video_widget, &allocation);
+ gtk_widget_get_allocation(GTK_WIDGET(main_win), &window_allocation);
+
+ g_debug("%d %d %dx%d, %d %d %dx%d", allocation.x, allocation.y, allocation.width, allocation.height,
+ window_allocation.x, window_allocation.y, window_allocation.width, window_allocation.height);
+
+ gtk_window_resize(main_win,
+ MAX(window_allocation.width - allocation.width + video_width, 350),
+ window_allocation.height - allocation.height + video_height);
+
+ window_size_updated = TRUE;
+
+ gst_caps_unref(caps);
+ }
+ gst_object_unref(pad);
+ }
+ }
+ }
+}
+
+static void create_ui()
+{
+ main_win = GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL));
+ g_signal_connect(main_win, "delete-event",
+ G_CALLBACK(gtk_main_quit), NULL);
+
+ GtkBox *vbox = GTK_BOX(gtk_vbox_new(FALSE, 0));
+ gtk_container_add(GTK_CONTAINER(main_win), GTK_WIDGET(vbox));
+
+ GtkMenuBar *menubar = GTK_MENU_BAR(gtk_menu_bar_new());
+ gtk_box_pack_start(vbox, GTK_WIDGET(menubar), FALSE, FALSE, 0);
+
+ GtkMenuShell *file_menu = GTK_MENU_SHELL(gtk_menu_new());
+
+ GtkMenuItem *file_item = GTK_MENU_ITEM(gtk_check_menu_item_new_with_mnemonic(_("_File")));
+ gtk_menu_item_set_submenu(file_item, GTK_WIDGET(file_menu));
+ gtk_menu_shell_append(GTK_MENU_SHELL(menubar), GTK_WIDGET(file_item));
+
+ GtkWidget *open_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, NULL);
+ gtk_menu_shell_append(file_menu, open_item);
+
+ gtk_menu_shell_append(file_menu, gtk_separator_menu_item_new());
+
+ GtkWidget *quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
+ g_signal_connect(quit_item, "activate",
+ G_CALLBACK(gtk_main_quit), NULL);
+ gtk_menu_shell_append(file_menu, quit_item);
+
+ video_widget = gtk_drawing_area_new();
+ gtk_widget_set_double_buffered(GTK_WIDGET(video_widget), FALSE);
+ g_signal_connect(video_widget, "realize",
+ G_CALLBACK(video_widget_handle_realize), NULL);
+ gtk_box_pack_start(vbox, video_widget, TRUE, TRUE, 0);
+
+ GtkToolbar *toolbar = GTK_TOOLBAR(gtk_toolbar_new());
+ gtk_box_pack_start(vbox, GTK_WIDGET(toolbar), FALSE, FALSE, 0);
+
+ play_tool = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PLAY);
+ g_signal_connect(play_tool, "clicked",
+ G_CALLBACK(play_media), NULL);
+ gtk_toolbar_insert(toolbar, play_tool, -1);
+
+ pause_tool = gtk_tool_button_new_from_stock(GTK_STOCK_MEDIA_PAUSE);
+ g_signal_connect(pause_tool, "clicked",
+ G_CALLBACK(pause_media), NULL);
+ gtk_toolbar_insert(toolbar, pause_tool, -1);
+
+ slider_widget = GTK_SCALE(gtk_hscale_new_with_range(0, 100, 1));
+ gtk_scale_set_draw_value(slider_widget, FALSE);
+ slider_changed_connection_id = g_signal_connect(slider_widget, "value-changed",
+ G_CALLBACK(slider_widget_handle_value_changed), NULL);
+ GtkToolItem *slider_tool = gtk_tool_item_new();
+ gtk_tool_item_set_expand(slider_tool, TRUE);
+ gtk_container_add(GTK_CONTAINER(slider_tool), GTK_WIDGET(slider_widget));
+ gtk_toolbar_insert(toolbar, slider_tool, -1);
+
+ gtk_window_set_default_size(main_win, 350, 20);
+
+ gtk_widget_show_all(GTK_WIDGET(vbox));
+ gtk_widget_hide(GTK_WIDGET(pause_tool));
+}
+
+static void create_gst()
+{
+ playbin = gst_element_factory_make("playbin", "playbin");
+ g_return_if_fail(playbin);
+
+ GstBus *bus = gst_element_get_bus(playbin);
+ gst_bus_add_signal_watch(bus);
+ g_signal_connect(bus, "message::eos",
+ G_CALLBACK(bus_handle_eos), NULL);
+ g_signal_connect(bus, "message::error",
+ G_CALLBACK(bus_handle_error), NULL);
+ g_signal_connect(bus, "message::state-changed",
+ G_CALLBACK(bus_handle_state_changed), NULL);
+ gst_object_unref(bus);
+}
+
+static void open_media(const char *uri)
+{
+ g_debug("opening '%s'", uri);
+
+ window_size_updated = FALSE;
+ video_width = 0;
+ video_height = 0;
+ media_duration = GST_CLOCK_TIME_NONE;
+
+ g_object_set(playbin, "uri", uri, NULL);
+
+ gchar *filename = g_filename_from_uri(uri, NULL, NULL);
+ if (filename) {
+ gchar *basename = g_filename_display_basename(filename);
+ gtk_window_set_title(main_win, basename);
+ g_free(basename);
+ g_free(filename);
+ } else {
+ gtk_window_set_title(main_win, "gplay");
+ }
+
+ gst_element_set_state(playbin, GST_STATE_PAUSED);
+}
+
+int main(int argc, char **argv)
+{
+ GOptionContext *ctx = g_option_context_new(_(" - simple gtk media player"));
+ gchar **filenames;
+ gboolean start_playing;
+ const GOptionEntry entries[] = {
+ { "play", 'p', 0, G_OPTION_ARG_NONE, &start_playing, "Start playing immediately", NULL },
+ { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL },
+ { NULL }
+ };
+ GError *error = NULL;
+
+ g_option_context_add_main_entries(ctx, entries, NULL);
+ g_option_context_add_group(ctx, gst_init_get_option_group());
+ g_option_context_add_group(ctx, gtk_get_option_group(TRUE));
+
+ if (!g_option_context_parse(ctx, &argc, &argv, &error)) {
+ g_printerr("Invalid usage: %s", error->message);
+ g_error_free(error);
+ return EXIT_FAILURE;
+ }
+
+ g_option_context_free(ctx);
+
+ create_ui();
+ create_gst();
+
+ if (filenames && *filenames) {
+ const char *file = filenames[0];
+ if (gst_uri_is_valid(file)) {
+ open_media(file);
+ } else {
+ char *uri = gst_filename_to_uri(file, &error);
+ if (uri) {
+ open_media(uri);
+ g_free(uri);
+ } else {
+ g_printerr("Invalid filename '%s':%s", file, error->message);
+ g_error_free(error);
+ return EXIT_FAILURE;
+ }
+ }
+ if (start_playing) {
+ play_media();
+ }
+ }
+
+ gtk_widget_show(GTK_WIDGET(main_win));
+ gtk_main();
+
+ return EXIT_SUCCESS;
+}
diff --git a/gplay.config b/gplay.config
new file mode 100644
index 0000000..8cec188
--- /dev/null
+++ b/gplay.config
@@ -0,0 +1 @@
+// ADD PREDEFINED MACROS HERE!
diff --git a/gplay.creator b/gplay.creator
new file mode 100644
index 0000000..e94cbbd
--- /dev/null
+++ b/gplay.creator
@@ -0,0 +1 @@
+[General]
diff --git a/gplay.files b/gplay.files
new file mode 100644
index 0000000..3b85354
--- /dev/null
+++ b/gplay.files
@@ -0,0 +1,2 @@
+Makefile
+gplay.c
diff --git a/gplay.includes b/gplay.includes
new file mode 100644
index 0000000..a02f390
--- /dev/null
+++ b/gplay.includes
@@ -0,0 +1,8 @@
+/usr/include/gstreamer-1.0
+/usr/include/glib-2.0
+/usr/lib64/glib-2.0/include
+/usr/include/gtk-2.0
+/usr/lib64/gtk-2.0/include
+/usr/include/pango-1.0
+/usr/include/atk-1.0
+/usr/include/cairo