Blame tests/icles/playbin-text.c

Packit 0652a1
/* GStreamer
Packit 0652a1
 *
Packit 0652a1
 * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
Packit 0652a1
 *
Packit 0652a1
 * This library is free software; you can redistribute it and/or
Packit 0652a1
 * modify it under the terms of the GNU Library General Public
Packit 0652a1
 * License as published by the Free Software Foundation; either
Packit 0652a1
 * version 2 of the License, or (at your option) any later version.
Packit 0652a1
 *
Packit 0652a1
 * This library is distributed in the hope that it will be useful,
Packit 0652a1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0652a1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0652a1
 * Library General Public License for more details.
Packit 0652a1
 *
Packit 0652a1
 * You should have received a copy of the GNU Library General Public
Packit 0652a1
 * License along with this library; if not, write to the
Packit 0652a1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 0652a1
 * Boston, MA 02110-1301, USA.
Packit 0652a1
 */
Packit 0652a1
Packit 0652a1
#ifdef HAVE_CONFIG_H
Packit 0652a1
#include "config.h"
Packit 0652a1
#endif
Packit 0652a1
Packit 0652a1
#include <gst/gst.h>
Packit 0652a1
Packit 0652a1
#include <stdio.h>
Packit 0652a1
#include <string.h>
Packit 0652a1
#include <stdlib.h>
Packit 0652a1
Packit 0652a1
typedef struct _App App;
Packit 0652a1
Packit 0652a1
struct _App
Packit 0652a1
{
Packit 0652a1
  GstElement *playbin;
Packit 0652a1
  GstElement *textsink;
Packit 0652a1
Packit 0652a1
  GMainLoop *loop;
Packit 0652a1
};
Packit 0652a1
Packit 0652a1
static App s_app;
Packit 0652a1
Packit 0652a1
static gboolean
Packit 0652a1
bus_message (GstBus * bus, GstMessage * message, App * app)
Packit 0652a1
{
Packit 0652a1
  GST_DEBUG ("got message %s",
Packit 0652a1
      gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
Packit 0652a1
Packit 0652a1
  switch (GST_MESSAGE_TYPE (message)) {
Packit 0652a1
    case GST_MESSAGE_ERROR:
Packit 0652a1
    {
Packit 0652a1
      GError *gerror;
Packit 0652a1
      gchar *debug;
Packit 0652a1
Packit 0652a1
      gst_message_parse_error (message, &gerror, &debug);
Packit 0652a1
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
Packit 0652a1
      g_error_free (gerror);
Packit 0652a1
      g_free (debug);
Packit 0652a1
Packit 0652a1
      g_main_loop_quit (app->loop);
Packit 0652a1
      break;
Packit 0652a1
    }
Packit 0652a1
    case GST_MESSAGE_WARNING:
Packit 0652a1
    {
Packit 0652a1
      GError *gerror;
Packit 0652a1
      gchar *debug;
Packit 0652a1
Packit 0652a1
      gst_message_parse_warning (message, &gerror, &debug);
Packit 0652a1
      gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
Packit 0652a1
      g_error_free (gerror);
Packit 0652a1
      g_free (debug);
Packit 0652a1
      break;
Packit 0652a1
    }
Packit 0652a1
    case GST_MESSAGE_EOS:
Packit 0652a1
      g_message ("received EOS");
Packit 0652a1
      g_main_loop_quit (app->loop);
Packit 0652a1
      break;
Packit 0652a1
    default:
Packit 0652a1
      break;
Packit 0652a1
  }
Packit 0652a1
  return TRUE;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
static GstFlowReturn
Packit 0652a1
have_subtitle (GstElement * appsink, App * app)
Packit 0652a1
{
Packit 0652a1
  GstBuffer *buffer;
Packit 0652a1
  GstSample *sample;
Packit 0652a1
Packit 0652a1
  /* get the buffer, we can also wakeup the mainloop to get the subtitle from
Packit 0652a1
   * appsink in the mainloop */
Packit 0652a1
  g_signal_emit_by_name (appsink, "pull-sample", &sample);
Packit 0652a1
  if (sample) {
Packit 0652a1
    GstMapInfo map;
Packit 0652a1
    gint64 position;
Packit 0652a1
    GstClock *clock;
Packit 0652a1
    GstClockTime base_time, running_time;
Packit 0652a1
Packit 0652a1
    buffer = gst_sample_get_buffer (sample);
Packit 0652a1
    gst_element_query_position (appsink, GST_FORMAT_TIME, &position);
Packit 0652a1
Packit 0652a1
    clock = gst_element_get_clock (appsink);
Packit 0652a1
    base_time = gst_element_get_base_time (appsink);
Packit 0652a1
Packit 0652a1
    running_time = gst_clock_get_time (clock) - base_time;
Packit 0652a1
Packit 0652a1
    gst_object_unref (clock);
Packit 0652a1
Packit 0652a1
    g_message ("received a subtitle at position %" GST_TIME_FORMAT
Packit 0652a1
        ", running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (position),
Packit 0652a1
        GST_TIME_ARGS (running_time));
Packit 0652a1
Packit 0652a1
    gst_buffer_map (buffer, &map, GST_MAP_READ);
Packit 0652a1
    gst_util_dump_mem (map.data, map.size);
Packit 0652a1
    gst_buffer_unmap (buffer, &map);
Packit 0652a1
    gst_sample_unref (sample);
Packit 0652a1
  }
Packit 0652a1
  return GST_FLOW_OK;
Packit 0652a1
}
Packit 0652a1
Packit 0652a1
int
Packit 0652a1
main (int argc, char *argv[])
Packit 0652a1
{
Packit 0652a1
  App *app = &s_app;
Packit 0652a1
  GstBus *bus;
Packit 0652a1
  GstCaps *subcaps;
Packit 0652a1
Packit 0652a1
  gst_init (&argc, &argv);
Packit 0652a1
Packit 0652a1
  if (argc < 2) {
Packit 0652a1
    g_print ("usage: %s <uri> [<suburi>]\n", argv[0]);
Packit 0652a1
    return -1;
Packit 0652a1
  }
Packit 0652a1
Packit 0652a1
  /* create a mainloop to get messages */
Packit 0652a1
  app->loop = g_main_loop_new (NULL, TRUE);
Packit 0652a1
Packit 0652a1
  app->playbin = gst_element_factory_make ("playbin", NULL);
Packit 0652a1
  g_assert (app->playbin);
Packit 0652a1
Packit 0652a1
  /* set appsink to get the subtitles */
Packit 0652a1
  app->textsink = gst_element_factory_make ("appsink", "subtitle_sink");
Packit 0652a1
  g_object_set (G_OBJECT (app->textsink), "emit-signals", TRUE, NULL);
Packit 0652a1
  g_object_set (G_OBJECT (app->textsink), "ts-offset", 0 * GST_SECOND, NULL);
Packit 0652a1
  g_signal_connect (app->textsink, "new-sample", G_CALLBACK (have_subtitle),
Packit 0652a1
      app);
Packit 0652a1
  subcaps = gst_caps_from_string ("text/x-raw, format={ utf8, pango-markup }");
Packit 0652a1
  g_object_set (G_OBJECT (app->textsink), "caps", subcaps, NULL);
Packit 0652a1
  gst_caps_unref (subcaps);
Packit 0652a1
Packit 0652a1
  g_object_set (G_OBJECT (app->playbin), "text-sink", app->textsink, NULL);
Packit 0652a1
Packit 0652a1
  bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
Packit 0652a1
Packit 0652a1
  /* add watch for messages */
Packit 0652a1
  gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
Packit 0652a1
Packit 0652a1
  /* set to read from appsrc */
Packit 0652a1
  g_object_set (app->playbin, "uri", argv[1], NULL);
Packit 0652a1
Packit 0652a1
  if (argc > 2)
Packit 0652a1
    g_object_set (app->playbin, "suburi", argv[2], NULL);
Packit 0652a1
Packit 0652a1
  /* go to playing and wait in a mainloop. */
Packit 0652a1
  gst_element_set_state (app->playbin, GST_STATE_PLAYING);
Packit 0652a1
Packit 0652a1
  /* this mainloop is stopped when we receive an error or EOS */
Packit 0652a1
  g_main_loop_run (app->loop);
Packit 0652a1
Packit 0652a1
  g_message ("stopping");
Packit 0652a1
Packit 0652a1
  gst_element_set_state (app->playbin, GST_STATE_NULL);
Packit 0652a1
Packit 0652a1
  gst_object_unref (bus);
Packit 0652a1
  g_main_loop_unref (app->loop);
Packit 0652a1
Packit 0652a1
  return 0;
Packit 0652a1
}