media: handle add-added on non-bins too
[platform/upstream/gstreamer.git] / examples / test-appsrc.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <gst/gst.h>
21
22 #include <gst/rtsp-server/rtsp-server.h>
23
24 typedef struct
25 {
26   gboolean white;
27   GstClockTime timestamp;
28 } MyContext;
29
30 /* called when we need to give data to appsrc */
31 static void
32 need_data (GstElement * appsrc, guint unused, MyContext * ctx)
33 {
34   GstBuffer *buffer;
35   guint size;
36   GstFlowReturn ret;
37
38   size = 385 * 288 * 2;
39
40   buffer = gst_buffer_new_allocate (NULL, size, NULL);
41
42   /* this makes the image black/white */
43   gst_buffer_memset (buffer, 0, ctx->white ? 0xff : 0x0, size);
44
45   ctx->white = !ctx->white;
46
47   /* increment the timestamp every 1/2 second */
48   GST_BUFFER_PTS (buffer) = ctx->timestamp;
49   GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 2);
50   ctx->timestamp += GST_BUFFER_DURATION (buffer);
51
52   g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
53 }
54
55 /* called when a new media pipeline is constructed. We can query the
56  * pipeline and configure our appsrc */
57 static void
58 media_configure (GstRTSPMediaFactory * factory, GstRTSPMedia * media,
59     gpointer user_data)
60 {
61   GstElement *element, *appsrc;
62   MyContext *ctx;
63
64   /* get the element used for providing the streams of the media */
65   element = gst_rtsp_media_get_element (media);
66
67   /* get our appsrc, we named it 'mysrc' with the name property */
68   appsrc = gst_bin_get_by_name_recurse_up (GST_BIN (element), "mysrc");
69
70   /* this instructs appsrc that we will be dealing with timed buffer */
71   gst_util_set_object_arg (G_OBJECT (appsrc), "format", "time");
72   /* configure the caps of the video */
73   g_object_set (G_OBJECT (appsrc), "caps",
74       gst_caps_new_simple ("video/x-raw",
75           "format", G_TYPE_STRING, "RGB16",
76           "width", G_TYPE_INT, 384,
77           "height", G_TYPE_INT, 288,
78           "framerate", GST_TYPE_FRACTION, 0, 1, NULL), NULL);
79
80   ctx = g_new0 (MyContext, 1);
81   ctx->white = FALSE;
82   ctx->timestamp = 0;
83   /* make sure ther datais freed when the media is gone */
84   g_object_set_data_full (G_OBJECT (media), "my-extra-data", ctx,
85       (GDestroyNotify) g_free);
86
87   /* install the callback that will be called when a buffer is needed */
88   g_signal_connect (appsrc, "need-data", (GCallback) need_data, ctx);
89 }
90
91 int
92 main (int argc, char *argv[])
93 {
94   GMainLoop *loop;
95   GstRTSPServer *server;
96   GstRTSPMountPoints *mounts;
97   GstRTSPMediaFactory *factory;
98
99   gst_init (&argc, &argv);
100
101   loop = g_main_loop_new (NULL, FALSE);
102
103   /* create a server instance */
104   server = gst_rtsp_server_new ();
105
106   /* get the mount points for this server, every server has a default object
107    * that be used to map uri mount points to media factories */
108   mounts = gst_rtsp_server_get_mount_points (server);
109
110   /* make a media factory for a test stream. The default media factory can use
111    * gst-launch syntax to create pipelines.
112    * any launch line works as long as it contains elements named pay%d. Each
113    * element with pay%d names will be a stream */
114   factory = gst_rtsp_media_factory_new ();
115   gst_rtsp_media_factory_set_launch (factory,
116       "( appsrc name=mysrc ! videoconvert ! x264enc ! rtph264pay name=pay0 pt=96 )");
117
118   /* notify when our media is ready, This is called whenever someone asks for
119    * the media and a new pipeline with our appsrc is created */
120   g_signal_connect (factory, "media-configure", (GCallback) media_configure,
121       NULL);
122
123   /* attach the test factory to the /test url */
124   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
125
126   /* don't need the ref to the mounts anymore */
127   g_object_unref (mounts);
128
129   /* attach the server to the default maincontext */
130   gst_rtsp_server_attach (server, NULL);
131
132   /* start serving */
133   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
134   g_main_loop_run (loop);
135
136   return 0;
137 }