tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / tests / examples / app / appsrc_ex.c
1
2
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6
7 #include <gst/gst.h>
8 #include <gst/app/gstappsrc.h>
9 #include <gst/app/gstappbuffer.h>
10 #include <gst/app/gstappsink.h>
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15
16
17 typedef struct _App App;
18 struct _App
19 {
20   GstElement *pipe;
21   GstElement *src;
22   GstElement *id;
23   GstElement *sink;
24 };
25
26 App s_app;
27
28 static void dont_eat_my_chicken_wings (void *priv);
29
30 int
31 main (int argc, char *argv[])
32 {
33   App *app = &s_app;
34   int i;
35
36   gst_init (&argc, &argv);
37
38   app->pipe = gst_pipeline_new (NULL);
39   g_assert (app->pipe);
40
41   app->src = gst_element_factory_make ("appsrc", NULL);
42   g_assert (app->src);
43   gst_bin_add (GST_BIN (app->pipe), app->src);
44
45   app->id = gst_element_factory_make ("identity", NULL);
46   g_assert (app->id);
47   gst_bin_add (GST_BIN (app->pipe), app->id);
48
49   app->sink = gst_element_factory_make ("appsink", NULL);
50   g_assert (app->sink);
51   gst_bin_add (GST_BIN (app->pipe), app->sink);
52
53   gst_element_link (app->src, app->id);
54   gst_element_link (app->id, app->sink);
55
56   gst_element_set_state (app->pipe, GST_STATE_PLAYING);
57
58   for (i = 0; i < 10; i++) {
59     GstBuffer *buf;
60     void *data;
61
62     data = malloc (100);
63     memset (data, i, 100);
64
65     buf = gst_app_buffer_new (data, 100, dont_eat_my_chicken_wings, data);
66     printf ("%d: creating buffer for pointer %p, %p\n", i, data, buf);
67     gst_app_src_push_buffer (GST_APP_SRC (app->src), buf);
68   }
69
70   /* push EOS */
71   gst_app_src_end_of_stream (GST_APP_SRC (app->src));
72
73   /* _is_eos() does not block and returns TRUE if there is not currently an EOS
74    * to be retrieved */
75   while (!gst_app_sink_is_eos (GST_APP_SINK (app->sink))) {
76     GstBuffer *buf;
77
78     /* pull the next item, this can return NULL when there is no more data and
79      * EOS has been received */
80     buf = gst_app_sink_pull_buffer (GST_APP_SINK (app->sink));
81     printf ("retrieved buffer %p\n", buf);
82     if (buf)
83       gst_buffer_unref (buf);
84   }
85   gst_element_set_state (app->pipe, GST_STATE_NULL);
86
87   return 0;
88 }
89
90 static void
91 dont_eat_my_chicken_wings (void *priv)
92 {
93   printf ("freeing buffer for pointer %p\n", priv);
94   free (priv);
95 }