~ubuntu-branches/ubuntu/trusty/gstreamer1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to tests/examples/manual/blockprobe.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2012-10-08 09:59:20 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20121008095920-3k2vlenl0zf6lu7i
Tags: 1.0.1-1
* New upstream stable release:
  + debian/libgstreamer.symbols:
    - Add new symbols.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*** block  from ../../../docs/manual/advanced-dataaccess.xml ***/
 
3
#include <gst/gst.h>
 
4
 
 
5
static GMainLoop *loop;
 
6
static volatile gint counter;
 
7
static GstBus *bus;
 
8
static gboolean prerolled = FALSE;
 
9
static GstPad *sinkpad;
 
10
 
 
11
static void
 
12
dec_counter (GstElement * pipeline)
 
13
{
 
14
  if (prerolled)
 
15
    return;
 
16
 
 
17
  if (g_atomic_int_dec_and_test (&counter)) {
 
18
    /* all probes blocked and no-more-pads signaled, post
 
19
     * message on the bus. */
 
20
    prerolled = TRUE;
 
21
 
 
22
    gst_bus_post (bus, gst_message_new_application (
 
23
          GST_OBJECT_CAST (pipeline),
 
24
          gst_structure_new_empty ("ExPrerolled")));
 
25
  }
 
26
}
 
27
 
 
28
/* called when a source pad of uridecodebin is blocked */
 
29
static GstPadProbeReturn
 
30
cb_blocked (GstPad          *pad,
 
31
            GstPadProbeInfo *info,
 
32
            gpointer         user_data)
 
33
{
 
34
  GstElement *pipeline = GST_ELEMENT (user_data);
 
35
 
 
36
  if (prerolled)
 
37
    return GST_PAD_PROBE_REMOVE;
 
38
 
 
39
  dec_counter (pipeline);
 
40
 
 
41
  return GST_PAD_PROBE_OK;
 
42
}
 
43
 
 
44
/* called when uridecodebin has a new pad */
 
45
static void
 
46
cb_pad_added (GstElement *element,
 
47
              GstPad     *pad,
 
48
              gpointer    user_data)
 
49
{
 
50
  GstElement *pipeline = GST_ELEMENT (user_data);
 
51
 
 
52
  if (prerolled)
 
53
    return;
 
54
 
 
55
  g_atomic_int_inc (&counter);
 
56
 
 
57
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
 
58
      (GstPadProbeCallback) cb_blocked, pipeline, NULL);
 
59
 
 
60
  /* try to link to the video pad */
 
61
  gst_pad_link (pad, sinkpad);
 
62
}
 
63
 
 
64
/* called when uridecodebin has created all pads */
 
65
static void
 
66
cb_no_more_pads (GstElement *element,
 
67
                 gpointer    user_data)
 
68
{
 
69
  GstElement *pipeline = GST_ELEMENT (user_data);
 
70
 
 
71
  if (prerolled)
 
72
    return;
 
73
 
 
74
  dec_counter (pipeline);
 
75
}
 
76
 
 
77
/* called when a new message is posted on the bus */
 
78
static void
 
79
cb_message (GstBus     *bus,
 
80
            GstMessage *message,
 
81
            gpointer    user_data)
 
82
{
 
83
  GstElement *pipeline = GST_ELEMENT (user_data);
 
84
 
 
85
  switch (GST_MESSAGE_TYPE (message)) {
 
86
    case GST_MESSAGE_ERROR:
 
87
      g_print ("we received an error!\n");
 
88
      g_main_loop_quit (loop);
 
89
      break;
 
90
    case GST_MESSAGE_EOS:
 
91
      g_print ("we reached EOS\n");
 
92
      g_main_loop_quit (loop);
 
93
      break;
 
94
    case GST_MESSAGE_APPLICATION:
 
95
    {
 
96
      if (gst_message_has_name (message, "ExPrerolled")) {
 
97
        /* it's our message */
 
98
        g_print ("we are all prerolled, do seek\n");
 
99
        gst_element_seek (pipeline,
 
100
            1.0, GST_FORMAT_TIME,
 
101
            GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
 
102
            GST_SEEK_TYPE_SET, 2 * GST_SECOND,
 
103
            GST_SEEK_TYPE_SET, 5 * GST_SECOND);
 
104
 
 
105
        gst_element_set_state (pipeline, GST_STATE_PLAYING);
 
106
      }
 
107
      break;
 
108
    }
 
109
    default:
 
110
      break;
 
111
  }
 
112
}
 
113
 
 
114
gint
 
115
main (gint   argc,
 
116
      gchar *argv[])
 
117
{
 
118
  GstElement *pipeline, *src, *csp, *vs, *sink;
 
119
 
 
120
  /* init GStreamer */
 
121
  gst_init (&argc, &argv);
 
122
  loop = g_main_loop_new (NULL, FALSE);
 
123
 
 
124
  if (argc < 2) {
 
125
    g_print ("usage: %s <uri>", argv[0]);
 
126
    return -1;
 
127
  }
 
128
 
 
129
  /* build */
 
130
  pipeline = gst_pipeline_new ("my-pipeline");
 
131
 
 
132
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
 
133
  gst_bus_add_signal_watch (bus);
 
134
  g_signal_connect (bus, "message", (GCallback) cb_message,
 
135
      pipeline);
 
136
 
 
137
  src = gst_element_factory_make ("uridecodebin", "src");
 
138
  if (src == NULL)
 
139
    g_error ("Could not create 'uridecodebin' element");
 
140
 
 
141
  g_object_set (src, "uri", argv[1], NULL);
 
142
 
 
143
  csp = gst_element_factory_make ("videoconvert", "csp");
 
144
  if (csp == NULL)
 
145
    g_error ("Could not create 'videoconvert' element");
 
146
 
 
147
  vs = gst_element_factory_make ("videoscale", "vs");
 
148
  if (csp == NULL)
 
149
    g_error ("Could not create 'videoscale' element");
 
150
 
 
151
  sink = gst_element_factory_make ("autovideosink", "sink");
 
152
  if (sink == NULL)
 
153
    g_error ("Could not create 'autovideosink' element");
 
154
 
 
155
  gst_bin_add_many (GST_BIN (pipeline), src, csp, vs, sink, NULL);
 
156
 
 
157
  /* can't link src yet, it has no pads */
 
158
  gst_element_link_many (csp, vs, sink, NULL);
 
159
 
 
160
  sinkpad = gst_element_get_static_pad (csp, "sink");
 
161
 
 
162
  /* for each pad block that is installed, we will increment
 
163
   * the counter. for each pad block that is signaled, we
 
164
   * decrement the counter. When the counter is 0 we post
 
165
   * an app message to tell the app that all pads are
 
166
   * blocked. Start with 1 that is decremented when no-more-pads
 
167
   * is signaled to make sure that we only post the message
 
168
   * after no-more-pads */
 
169
  g_atomic_int_set (&counter, 1);
 
170
 
 
171
  g_signal_connect (src, "pad-added",
 
172
      (GCallback) cb_pad_added, pipeline);
 
173
  g_signal_connect (src, "no-more-pads",
 
174
      (GCallback) cb_no_more_pads, pipeline);
 
175
 
 
176
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
 
177
 
 
178
  g_main_loop_run (loop);
 
179
 
 
180
  gst_element_set_state (pipeline, GST_STATE_NULL);
 
181
 
 
182
  gst_object_unref (sinkpad);
 
183
  gst_object_unref (bus);
 
184
  gst_object_unref (pipeline);
 
185
  g_main_loop_unref (loop);
 
186
 
 
187
  return 0;
 
188
}