test.c 5.06 KB
Newer Older
1
2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Wim Taymans's avatar
Wim Taymans committed
3
 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * 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.
 */
20
21
22
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
23
24
#include <gst/gst.h>

25
26
#include <stdlib.h>

27
28
static GMainLoop *loop;

29
static GstElement *
30
gen_video_element (void)
31
32
33
34
{
  GstElement *element;
  GstElement *conv;
  GstElement *sink;
35
  GstPad *pad;
36

Wim Taymans's avatar
Wim Taymans committed
37
  element = gst_bin_new ("vbin");
38
  conv = gst_element_factory_make ("videoconvert", "conv");
39
  sink = gst_element_factory_make (DEFAULT_VIDEOSINK, "sink");
40
41
42
43
44

  gst_bin_add (GST_BIN (element), conv);
  gst_bin_add (GST_BIN (element), sink);
  gst_element_link_pads (conv, "src", sink, "sink");

45
  pad = gst_element_get_static_pad (conv, "sink");
46
47
  gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
  gst_object_unref (pad);
48
49
50
51
52

  return element;
}

static GstElement *
53
gen_audio_element (void)
54
55
56
57
{
  GstElement *element;
  GstElement *conv;
  GstElement *sink;
58
  GstPad *pad;
59

Wim Taymans's avatar
Wim Taymans committed
60
  element = gst_bin_new ("abin");
61
  conv = gst_element_factory_make ("audioconvert", "conv");
62
  sink = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink");
63
64
65
66
67

  gst_bin_add (GST_BIN (element), conv);
  gst_bin_add (GST_BIN (element), sink);
  gst_element_link_pads (conv, "src", sink, "sink");

68
  pad = gst_element_get_static_pad (conv, "sink");
69
70
  gst_element_add_pad (element, gst_ghost_pad_new ("sink", pad));
  gst_object_unref (pad);
71
72
73
74

  return element;
}

75
76
77
78
79
80
81
82
83
84
85
86
87
static void
cb_newpad (GstElement * decodebin, GstPad * pad, gboolean last, gpointer data)
{
  GstCaps *caps;
  GstStructure *str;
  GstPad *sinkpad;
  GstElement *sink;
  GstElement *pipeline;
  const gchar *name;
  GstStateChangeReturn ret;
  GstPadLinkReturn lret;

  /* check media type */
88
  caps = gst_pad_get_caps (pad, NULL);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  str = gst_caps_get_structure (caps, 0);

  name = gst_structure_get_name (str);
  g_print ("name: %s\n", name);

  if (g_strrstr (name, "audio")) {
    sink = gen_audio_element ();
  } else if (g_strrstr (name, "video")) {
    sink = gen_video_element ();
  } else {
    sink = NULL;
  }
  gst_caps_unref (caps);

  if (sink) {
    pipeline = GST_ELEMENT_CAST (data);

    /* add new sink to the pipeline */
    gst_bin_add (GST_BIN_CAST (pipeline), sink);

    /* set the new sink tp PAUSED as well */
    ret = gst_element_set_state (sink, GST_STATE_PAUSED);
    if (ret == GST_STATE_CHANGE_FAILURE)
      goto state_error;

    /* get the ghostpad of the sink bin */
115
    sinkpad = gst_element_get_static_pad (sink, "sink");
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    /* link'n'play */
    lret = gst_pad_link (pad, sinkpad);
    if (lret != GST_PAD_LINK_OK)
      goto link_failed;

    gst_object_unref (sinkpad);
  }
  return;

  /* ERRORS */
state_error:
  {
    gst_bin_remove (GST_BIN_CAST (pipeline), sink);
    g_warning ("could not change state of new sink (%d)", ret);
    return;
  }
link_failed:
  {
    g_warning ("could not link pad and sink (%d)", lret);
    return;
  }
}

140
141
142
gint
main (gint argc, gchar * argv[])
{
143
  GstElement *pipeline, *filesrc, *decodebin;
144
  GstStateChangeReturn res;
145
146
147

  gst_init (&argc, &argv);

148
149
150
151
152
153
154
155
156
  pipeline = gst_pipeline_new ("pipeline");

  filesrc = gst_element_factory_make ("filesrc", "filesrc");
  g_assert (filesrc);
  decodebin = gst_element_factory_make ("decodebin", "decodebin");
  g_assert (decodebin);

  g_signal_connect (G_OBJECT (decodebin), "new-decoded-pad",
      G_CALLBACK (cb_newpad), pipeline);
157

158
159
  gst_bin_add_many (GST_BIN (pipeline), filesrc, decodebin, NULL);
  gst_element_link (filesrc, decodebin);
160

161
162
163
164
165
166
167
168
169
  if (argc < 2) {
    g_print ("usage: %s <uri>\n", argv[0]);
    exit (-1);
  }
  g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);

  /* set to paused, decodebin will autoplug and signal new_pad callbacks */
  res = gst_element_set_state (pipeline, GST_STATE_PAUSED);
  if (res == GST_STATE_CHANGE_FAILURE) {
170
    g_print ("could not pause\n");
171
172
    return -1;
  }
173
174
175
176
177
  /* wait for paused to complete */
  res = gst_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
  if (res == GST_STATE_CHANGE_FAILURE) {
    g_print ("could not pause\n");
    return -1;
178
  }
179

180
181
182
183
  /* play, now all the sinks are added to the pipeline and are prerolled and
   * ready to play. */
  res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (res == GST_STATE_CHANGE_FAILURE) {
184
185
186
    g_print ("could not play\n");
    return -1;
  }
187
188
189
190

  /* go in the mainloop now */
  loop = g_main_loop_new (NULL, TRUE);
  g_main_loop_run (loop);
191
192
193

  return 0;
}