player.c 4.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* GStreamer
 * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
 *
 * 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.
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
19

20
#include <gst/play/play.h>
21 22

static GMainLoop *loop = NULL;
23
static gint64 length = 0;
24

25
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
26
print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
27 28 29 30 31 32 33
{
  gint i, count;

  count = gst_tag_list_get_tag_size (list, tag);

  for (i = 0; i < count; i++) {
    gchar *str;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
34

35 36 37
    if (gst_tag_get_type (tag) == G_TYPE_STRING) {
      g_assert (gst_tag_list_get_string_index (list, tag, i, &str));
    } else {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
38 39
      str =
	  g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
40
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
41

42 43 44 45 46 47 48 49 50 51 52
    if (i == 0) {
      g_print ("%15s: %s\n", gst_tag_get_nick (tag), str);
    } else {
      g_print ("               : %s\n", str);
    }

    g_free (str);
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
53
got_found_tag (GstPlay * play, GstElement * source, GstTagList * tag_list)
54 55 56 57
{
  gst_tag_list_foreach (tag_list, print_tag, NULL);
}

58
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59
got_time_tick (GstPlay * play, gint64 time_nanos)
60
{
61
  g_print ("time tick %f\n", time_nanos / (float) GST_SECOND);
62 63 64
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
65
got_stream_length (GstPlay * play, gint64 length_nanos)
66
{
67
  g_print ("got length %llu\n", length_nanos);
68
  length = length_nanos;
69 70 71
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
72
got_video_size (GstPlay * play, gint width, gint height)
73
{
74
  g_print ("got video size %d, %d\n", width, height);
75 76 77
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78
got_eos (GstPlay * play)
79
{
80
  g_print ("End Of Stream\n");
81 82 83 84
  g_main_loop_quit (loop);
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
85
seek_timer (GstPlay * play)
86
{
87
  gst_play_seek_to_time (play, length / 2);
88 89 90 91
  return FALSE;
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
92
idle_iterate (GstPlay * play)
93 94 95 96 97 98 99 100 101 102
{
  gst_bin_iterate (GST_BIN (play));
  return (GST_STATE (GST_ELEMENT (play)) == GST_STATE_PLAYING);
}

int
main (int argc, char *argv[])
{
  GstPlay *play;
  GstElement *data_src, *video_sink, *audio_sink, *vis_element;
103
  GError *error = NULL;
104 105 106 107 108 109 110 111

  /* Initing GStreamer library */
  gst_init (&argc, &argv);

  if (argc != 2) {
    g_print ("usage: %s <video filename>\n", argv[0]);
    exit (-1);
  }
112

113 114 115
  loop = g_main_loop_new (NULL, FALSE);

  /* Creating the GstPlay object */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
  play = gst_play_new (&error);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
117
  if (error) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118 119 120 121
    g_print ("Error: could not create play object:\n%s\n", error->message);
    g_error_free (error);
    return 1;
  }
122 123 124

  /* Getting default audio and video plugins from GConf */
  audio_sink = gst_element_factory_make ("osssink", "audio_sink");
125
  video_sink = gst_element_factory_make ("ximagesink", "video_sink");
126 127
  vis_element = gst_element_factory_make ("goom", "vis_element");
  data_src = gst_element_factory_make ("gnomevfssrc", "source");
128

129 130 131 132 133
  /* Let's send them to GstPlay object */
  gst_play_set_audio_sink (play, audio_sink);
  gst_play_set_video_sink (play, video_sink);
  gst_play_set_data_src (play, data_src);
  gst_play_set_visualization (play, vis_element);
134

135 136 137
  /* Setting location we want to play */
  gst_play_set_location (play, argv[1]);

138
  /* Uncomment that line to get an XML dump of the pipeline */
139
  /* gst_xml_write_file (GST_ELEMENT (play), stdout); */
140

141
  g_signal_connect (G_OBJECT (play), "time_tick",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
142
      G_CALLBACK (got_time_tick), NULL);
143
  g_signal_connect (G_OBJECT (play), "stream_length",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
144
      G_CALLBACK (got_stream_length), NULL);
145
  g_signal_connect (G_OBJECT (play), "have_video_size",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
146
      G_CALLBACK (got_video_size), NULL);
147
  g_signal_connect (G_OBJECT (play), "found_tag",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
148
      G_CALLBACK (got_found_tag), NULL);
149
  g_signal_connect (G_OBJECT (play), "error",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
150 151
      G_CALLBACK (gst_element_default_error), NULL);
  g_signal_connect (G_OBJECT (play), "eos", G_CALLBACK (got_eos), NULL);
152

153 154 155 156 157
  /* Change state to PLAYING */
  gst_element_set_state (GST_ELEMENT (play), GST_STATE_PLAYING);

  g_idle_add ((GSourceFunc) idle_iterate, play);
  g_timeout_add (20000, (GSourceFunc) seek_timer, play);
158

159 160
  g_main_loop_run (loop);

161 162
  g_print ("setting pipeline to ready\n");

163
  gst_element_set_state (GST_ELEMENT (play), GST_STATE_READY);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
164

165
  /* unref 
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166
     gst_object_unref (GST_OBJECT (play)); */
167 168 169

  exit (0);
}