3 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
31 typedef struct _App App;
44 bus_message (GstBus * bus, GstMessage * message, App * app)
46 GST_DEBUG ("got message %s",
47 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
49 switch (GST_MESSAGE_TYPE (message)) {
50 case GST_MESSAGE_ERROR:
55 gst_message_parse_error (message, &gerror, &debug);
56 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
57 g_error_free (gerror);
60 g_main_loop_quit (app->loop);
63 case GST_MESSAGE_WARNING:
68 gst_message_parse_warning (message, &gerror, &debug);
69 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
70 g_error_free (gerror);
73 g_main_loop_quit (app->loop);
77 g_message ("received EOS");
78 g_main_loop_quit (app->loop);
87 have_subtitle (GstElement * appsink, App * app)
91 /* get the buffer, we can also wakeup the mainloop to get the subtitle from
92 * appsink in the mainloop */
93 g_signal_emit_by_name (appsink, "pull-buffer", &buffer);
101 GstClockTime base_time, running_time;
103 format = GST_FORMAT_TIME;
104 gst_element_query_position (appsink, &format, &position);
106 clock = gst_element_get_clock (appsink);
107 base_time = gst_element_get_base_time (appsink);
109 running_time = gst_clock_get_time (clock) - base_time;
111 gst_object_unref (clock);
113 g_message ("received a subtitle at position %" GST_TIME_FORMAT
114 ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (position),
115 GST_TIME_ARGS (running_time));
117 data = GST_BUFFER_DATA (buffer);
118 size = GST_BUFFER_SIZE (buffer);
120 gst_util_dump_mem (data, size);
125 main (int argc, char *argv[])
131 gst_init (&argc, &argv);
134 g_print ("usage: %s <filename>\n", argv[0]);
138 /* create a mainloop to get messages */
139 app->loop = g_main_loop_new (NULL, TRUE);
141 app->playbin = gst_element_factory_make ("playbin2", NULL);
142 g_assert (app->playbin);
144 /* set appsink to get the subtitles */
145 app->textsink = gst_element_factory_make ("appsink", "subtitle_sink");
146 g_object_set (G_OBJECT (app->textsink), "emit-signals", TRUE, NULL);
147 g_object_set (G_OBJECT (app->textsink), "ts-offset", 0 * GST_SECOND, NULL);
148 g_signal_connect (app->textsink, "new-buffer", G_CALLBACK (have_subtitle),
150 subcaps = gst_caps_from_string ("text/x-pango-markup; text/plain");
151 g_object_set (G_OBJECT (app->textsink), "caps", subcaps, NULL);
152 gst_caps_unref (subcaps);
154 g_object_set (G_OBJECT (app->playbin), "text-sink", app->textsink, NULL);
156 bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
158 /* add watch for messages */
159 gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
161 /* set to read from appsrc */
162 g_object_set (app->playbin, "uri", argv[1], NULL);
164 /* go to playing and wait in a mainloop. */
165 gst_element_set_state (app->playbin, GST_STATE_PLAYING);
167 /* this mainloop is stopped when we receive an error or EOS */
168 g_main_loop_run (app->loop);
170 g_message ("stopping");
172 gst_element_set_state (app->playbin, GST_STATE_NULL);
174 gst_object_unref (bus);
175 g_main_loop_unref (app->loop);