From c1d6745c041c8831d03b1fba703f79210f0dc242 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 17 Mar 2009 11:24:00 +0100 Subject: [PATCH] examples: add example of intercepting subtitles Add an example of how to install a custom sink for receiving subtitles in playbin2. --- tests/icles/.gitignore | 1 + tests/icles/Makefile.am | 6 +- tests/icles/playbin-text.c | 129 +++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/icles/playbin-text.c diff --git a/tests/icles/.gitignore b/tests/icles/.gitignore index 0a2516e5a5..d8083fdb22 100644 --- a/tests/icles/.gitignore +++ b/tests/icles/.gitignore @@ -4,3 +4,4 @@ test-textoverlay test-scale test-box test-colorkey +playbin-text diff --git a/tests/icles/Makefile.am b/tests/icles/Makefile.am index f0fed9b08b..fa49403ce7 100644 --- a/tests/icles/Makefile.am +++ b/tests/icles/Makefile.am @@ -42,4 +42,8 @@ test_box_SOURCES = test-box.c test_box_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS) test_box_LDADD = $(GST_LIBS) $(LIBM) -noinst_PROGRAMS = $(X_TESTS) $(PANGO_TESTS) stress-playbin test-scale test-box +playbin_text_SOURCES = playbin-text.c +playbin_text_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS) +playbin_text_LDADD = $(GST_LIBS) $(LIBM) + +noinst_PROGRAMS = $(X_TESTS) $(PANGO_TESTS) stress-playbin test-scale test-box playbin-text diff --git a/tests/icles/playbin-text.c b/tests/icles/playbin-text.c new file mode 100644 index 0000000000..dd199a0a20 --- /dev/null +++ b/tests/icles/playbin-text.c @@ -0,0 +1,129 @@ +/* GStreamer + * + * Copyright (C) 2009 Wim Taymans + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include +#include +#include + +typedef struct _App App; + +struct _App +{ + GstElement *playbin; + GstElement *textsink; + + GMainLoop *loop; +}; + +App s_app; + +static gboolean +bus_message (GstBus * bus, GstMessage * message, App * app) +{ + GST_DEBUG ("got message %s", + gst_message_type_get_name (GST_MESSAGE_TYPE (message))); + + switch (GST_MESSAGE_TYPE (message)) { + case GST_MESSAGE_ERROR: + g_error ("received error"); + g_main_loop_quit (app->loop); + break; + case GST_MESSAGE_WARNING: + g_error ("received error"); + g_main_loop_quit (app->loop); + break; + case GST_MESSAGE_EOS: + g_main_loop_quit (app->loop); + break; + default: + break; + } + return TRUE; +} + +static void +have_subtitle (GstElement * fakesink, GstBuffer * buffer, + GstPad * pad, App * app) +{ + guint8 *data; + guint size; + + g_print ("received a subtitle\n"); + + data = GST_BUFFER_DATA (buffer); + size = GST_BUFFER_SIZE (buffer); + + gst_util_dump_mem (data, size); +} + +int +main (int argc, char *argv[]) +{ + App *app = &s_app; + GstBus *bus; + + gst_init (&argc, &argv); + + if (argc < 2) { + g_print ("usage: %s \n", argv[0]); + return -1; + } + + /* create a mainloop to get messages */ + app->loop = g_main_loop_new (NULL, TRUE); + + app->playbin = gst_element_factory_make ("playbin2", NULL); + g_assert (app->playbin); + + /* set fakesink to get the subtitles */ + app->textsink = gst_element_factory_make ("fakesink", "subtitle_sink"); + g_object_set (G_OBJECT (app->textsink), "signal-handoffs", TRUE, NULL); + g_signal_connect (app->textsink, "handoff", G_CALLBACK (have_subtitle), app); + g_object_set (G_OBJECT (app->playbin), "text-sink", app->textsink, NULL); + + bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin)); + + /* add watch for messages */ + gst_bus_add_watch (bus, (GstBusFunc) bus_message, app); + + /* set to read from appsrc */ + g_object_set (app->playbin, "uri", argv[1], NULL); + + /* go to playing and wait in a mainloop. */ + gst_element_set_state (app->playbin, GST_STATE_PLAYING); + + /* this mainloop is stopped when we receive an error or EOS */ + g_main_loop_run (app->loop); + + GST_DEBUG ("stopping"); + + gst_element_set_state (app->playbin, GST_STATE_NULL); + + gst_object_unref (bus); + g_main_loop_unref (app->loop); + + return 0; +}