summaryrefslogtreecommitdiff
path: root/gplay.c
blob: 4702e004ec918af37d2a14dfefc3cfb8aeda9a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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;
}