1 /* GStreamer
2 *
3 * appsrc_ex.c: example for using appsrc and appsink linked.
4 *
5 * Copyright (C) 2007 David Schleef <ds@schleef.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <gst/gst.h>
29 #include <gst/app/gstappsrc.h>
30 #include <gst/app/gstappsink.h>
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36
37 typedef struct _App App;
38 struct _App
39 {
40 GstElement *pipe;
41 GstElement *src;
42 GstElement *id;
43 GstElement *sink;
44 };
45
46 App s_app;
47
48 int
main(int argc,char * argv[])49 main (int argc, char *argv[])
50 {
51 App *app = &s_app;
52 int i;
53
54 gst_init (&argc, &argv);
55
56 app->pipe = gst_pipeline_new (NULL);
57 g_assert (app->pipe);
58
59 app->src = gst_element_factory_make ("appsrc", NULL);
60 g_assert (app->src);
61 gst_bin_add (GST_BIN (app->pipe), app->src);
62
63 app->id = gst_element_factory_make ("identity", NULL);
64 g_assert (app->id);
65 gst_bin_add (GST_BIN (app->pipe), app->id);
66
67 app->sink = gst_element_factory_make ("appsink", NULL);
68 g_assert (app->sink);
69 gst_bin_add (GST_BIN (app->pipe), app->sink);
70
71 gst_element_link (app->src, app->id);
72 gst_element_link (app->id, app->sink);
73
74 gst_element_set_state (app->pipe, GST_STATE_PLAYING);
75
76 for (i = 0; i < 10; i++) {
77 GstBuffer *buf;
78 GstMapInfo map;
79
80 buf = gst_buffer_new_and_alloc (100);
81 gst_buffer_map (buf, &map, GST_MAP_WRITE);
82 memset (map.data, i, 100);
83 gst_buffer_unmap (buf, &map);
84
85 printf ("%d: pushing buffer for pointer %p, %p\n", i, map.data, buf);
86 gst_app_src_push_buffer (GST_APP_SRC (app->src), buf);
87 }
88
89 /* push EOS */
90 gst_app_src_end_of_stream (GST_APP_SRC (app->src));
91
92 /* _is_eos() does not block and returns TRUE if there is not currently an EOS
93 * to be retrieved */
94 while (!gst_app_sink_is_eos (GST_APP_SINK (app->sink))) {
95 GstSample *sample;
96
97 /* pull the next item, this can return NULL when there is no more data and
98 * EOS has been received */
99 sample = gst_app_sink_pull_sample (GST_APP_SINK (app->sink));
100 printf ("retrieved sample %p\n", sample);
101 if (sample)
102 gst_sample_unref (sample);
103 }
104 gst_element_set_state (app->pipe, GST_STATE_NULL);
105
106 return 0;
107 }
108