implement gui/tui
[gst-scaletempo-demo-rj.git] / src / tui.c
blob3db77d4c864932f3fa17d34c685e4b6207530249
1 /* tui.c
2 * Copyright (C) 2008 Rov Juvano <rovjuvano@users.sourceforge.net>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "tui.h"
20 static GMainLoop *loop;
22 static int stop_osd = 0;
23 static int clear_osd = 0;
24 #define CLEAR_OSD_TIME "\r "
25 #define CLEAR_OSD_OTHER " "
26 #define APPEND_OSD(pipe, msg...) \
27 g_print (CLEAR_OSD_TIME CLEAR_OSD_OTHER); \
28 (pipe)?print_osd_time (pipe):g_print (CLEAR_OSD_TIME); \
29 g_print (" " msg); \
30 clear_osd = 3;
31 #define OSD_ERROR(msg...) APPEND_OSD(NULL, msg);
33 static void
34 print_osd_time (GstElement * pipeline)
36 GstFormat time_format;
37 gint64 dur, pos;
39 if (clear_osd == 1) g_print (CLEAR_OSD_TIME CLEAR_OSD_OTHER);
40 if (clear_osd > 0) clear_osd--;
42 time_format = GST_FORMAT_TIME;
43 if (gst_element_query_duration (pipeline, &time_format, &dur) &&
44 gst_element_query_position (pipeline, &time_format, &pos) ) {
45 g_print ("\r %" GST_TIME_FORMAT " / %" GST_TIME_FORMAT,
46 GST_TIME_ARGS (pos), GST_TIME_ARGS (dur));
47 } else {
48 g_print ("\r Could not read position and duration");
52 static int print_osd_time_cb (gpointer data) {
53 print_osd_time(GST_ELEMENT_CAST (data));
54 if (stop_osd) {
55 stop_osd = 0;
56 return 0;
58 return 1;
61 static gboolean
62 tui_change_rate (GstElement * pipeline, gfloat rate, gboolean abs)
64 if (abs) { APPEND_OSD (pipeline, "changing rate to %3.2f", rate) }
65 else { APPEND_OSD (pipeline, "changing rate by %3.2f", rate); }
66 if (!change_rate(pipeline, rate, abs)) {
67 OSD_ERROR ("rate change failed!\n");
69 return 1;
72 static void
73 tui_rate_changed (GstElement * object, gdouble new_rate, gpointer data)
75 APPEND_OSD (object, "Rate changed to: %3.2f", new_rate);
78 static gboolean
79 tui_seek (GstElement * pipeline, gint seconds, gboolean abs)
81 if (abs) { APPEND_OSD (pipeline, "seeking to %3i", seconds) }
82 else { APPEND_OSD (pipeline, "seeking %3i seconds", seconds); }
83 if (!seek(pipeline, seconds, abs)) {
84 OSD_ERROR ("seek failed!\n");
86 return 1;
90 static gboolean
91 tui_play (GstElement * pipeline)
93 APPEND_OSD (pipeline, "PLAY pipeline");
94 if (play(pipeline))
95 g_timeout_add(1000, print_osd_time_cb, pipeline);
96 else {
97 OSD_ERROR ("PLAY failed\n");
99 return 1;
102 static gboolean
103 tui_pause (GstElement * pipeline)
105 APPEND_OSD (pipeline, "PAUSE pipeline");
106 if (pause(pipeline))
107 stop_osd = 1;
108 else {
109 OSD_ERROR ("PAUSE failed\n");
111 return 1;
114 static void
115 tui_stop (GstElement * pipeline) {
116 g_main_loop_quit (loop);
120 static void
121 tui_unknown_key (const gchar * key) {
122 if (g_str_equal (key, "Return")) {
123 g_print ("\n");
124 } else {
125 OSD_ERROR ("key: %s", key);
130 static void tui_playing_started (GstElement * pipeline) {
131 APPEND_OSD (pipeline, "PLAYING Started");
134 static void tui_playing_paused (GstElement * pipeline) {
135 APPEND_OSD (pipeline, "PLAYING Paused");
139 void tui_play_uri (const gchar *uri, const gchar * filter_name) {
140 GstElement * pipeline = build_pipeline (uri, filter_name);
141 if (!pipeline) {
142 OSD_ERROR ("An error occured building pipeline for: %s\n", uri);
143 return;
146 set_change_rate_around (tui_change_rate);
147 set_rate_changed_cb (tui_rate_changed);
148 set_seek_around (tui_seek);
149 set_play_around (tui_play);
150 set_playing_started_cb (tui_playing_started);
151 set_playing_paused_cb (tui_playing_paused);
152 set_pause_around (tui_pause);
153 set_stop_cb (tui_stop);
154 set_unknown_key_cb (tui_unknown_key);
156 loop = g_main_loop_new (NULL, FALSE);
157 tui_play (pipeline);
158 g_main_loop_run (loop);
160 gst_element_set_state (pipeline, GST_STATE_NULL);
161 gst_object_unref (pipeline);