Added gst-plugins-base-subtitles0.10-0.10.34 for Meego Harmattan 1.2
[mafwsubrenderer] / gst-plugins-base-subtitles0.10 / tests / icles / playbin-text.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/gst.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 typedef struct _App App;
32
33 struct _App
34 {
35   GstElement *playbin;
36   GstElement *textsink;
37
38   GMainLoop *loop;
39 };
40
41 static App s_app;
42
43 static gboolean
44 bus_message (GstBus * bus, GstMessage * message, App * app)
45 {
46   GST_DEBUG ("got message %s",
47       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
48
49   switch (GST_MESSAGE_TYPE (message)) {
50     case GST_MESSAGE_ERROR:
51     {
52       GError *gerror;
53       gchar *debug;
54
55       gst_message_parse_error (message, &gerror, &debug);
56       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
57       g_error_free (gerror);
58       g_free (debug);
59
60       g_main_loop_quit (app->loop);
61       break;
62     }
63     case GST_MESSAGE_WARNING:
64     {
65       GError *gerror;
66       gchar *debug;
67
68       gst_message_parse_warning (message, &gerror, &debug);
69       gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
70       g_error_free (gerror);
71       g_free (debug);
72
73       g_main_loop_quit (app->loop);
74       break;
75     }
76     case GST_MESSAGE_EOS:
77       g_message ("received EOS");
78       g_main_loop_quit (app->loop);
79       break;
80     default:
81       break;
82   }
83   return TRUE;
84 }
85
86 static void
87 have_subtitle (GstElement * appsink, App * app)
88 {
89   GstBuffer *buffer;
90
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);
94
95   if (buffer) {
96     guint8 *data;
97     guint size;
98     GstFormat format;
99     gint64 position;
100     GstClock *clock;
101     GstClockTime base_time, running_time;
102
103     format = GST_FORMAT_TIME;
104     gst_element_query_position (appsink, &format, &position);
105
106     clock = gst_element_get_clock (appsink);
107     base_time = gst_element_get_base_time (appsink);
108
109     running_time = gst_clock_get_time (clock) - base_time;
110
111     gst_object_unref (clock);
112
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));
116
117     data = GST_BUFFER_DATA (buffer);
118     size = GST_BUFFER_SIZE (buffer);
119
120     gst_util_dump_mem (data, size);
121   }
122 }
123
124 int
125 main (int argc, char *argv[])
126 {
127   App *app = &s_app;
128   GstBus *bus;
129   GstCaps *subcaps;
130
131   gst_init (&argc, &argv);
132
133   if (argc < 2) {
134     g_print ("usage: %s <filename>\n", argv[0]);
135     return -1;
136   }
137
138   /* create a mainloop to get messages */
139   app->loop = g_main_loop_new (NULL, TRUE);
140
141   app->playbin = gst_element_factory_make ("playbin2", NULL);
142   g_assert (app->playbin);
143
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),
149       app);
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);
153
154   g_object_set (G_OBJECT (app->playbin), "text-sink", app->textsink, NULL);
155
156   bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
157
158   /* add watch for messages */
159   gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
160
161   /* set to read from appsrc */
162   g_object_set (app->playbin, "uri", argv[1], NULL);
163
164   /* go to playing and wait in a mainloop. */
165   gst_element_set_state (app->playbin, GST_STATE_PLAYING);
166
167   /* this mainloop is stopped when we receive an error or EOS */
168   g_main_loop_run (app->loop);
169
170   g_message ("stopping");
171
172   gst_element_set_state (app->playbin, GST_STATE_NULL);
173
174   gst_object_unref (bus);
175   g_main_loop_unref (app->loop);
176
177   return 0;
178 }