rtsp-server: Use single-include rtsp header to make sure we get all definitions
[platform/upstream/gstreamer.git] / examples / test-mp4.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 #define DEFAULT_RTSP_PORT "8554"
25
26 static char *port = (char *) DEFAULT_RTSP_PORT;
27
28 static GOptionEntry entries[] = {
29   {"port", 'p', 0, G_OPTION_ARG_STRING, &port,
30       "Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
31   {NULL}
32 };
33
34 /* called when a stream has received an RTCP packet from the client */
35 static void
36 on_ssrc_active (GObject * session, GObject * source, GstRTSPMedia * media)
37 {
38   GstStructure *stats;
39
40   GST_INFO ("source %p in session %p is active", source, session);
41
42   g_object_get (source, "stats", &stats, NULL);
43   if (stats) {
44     gchar *sstr;
45
46     sstr = gst_structure_to_string (stats);
47     g_print ("structure: %s\n", sstr);
48     g_free (sstr);
49
50     gst_structure_free (stats);
51   }
52 }
53
54 /* signal callback when the media is prepared for streaming. We can get the
55  * session manager for each of the streams and connect to some signals. */
56 static void
57 media_prepared_cb (GstRTSPMedia * media)
58 {
59   guint i, n_streams;
60
61   n_streams = gst_rtsp_media_n_streams (media);
62
63   GST_INFO ("media %p is prepared and has %u streams", media, n_streams);
64
65   for (i = 0; i < n_streams; i++) {
66     GstRTSPStream *stream;
67     GObject *session;
68
69     stream = gst_rtsp_media_get_stream (media, i);
70     if (stream == NULL)
71       continue;
72
73     session = gst_rtsp_stream_get_rtpsession (stream);
74     GST_INFO ("watching session %p on stream %u", session, i);
75
76     g_signal_connect (session, "on-ssrc-active",
77         (GCallback) on_ssrc_active, media);
78   }
79 }
80
81 static void
82 media_configure_cb (GstRTSPMediaFactory * factory, GstRTSPMedia * media)
83 {
84   /* connect our prepared signal so that we can see when this media is
85    * prepared for streaming */
86   g_signal_connect (media, "prepared", (GCallback) media_prepared_cb, factory);
87 }
88
89 int
90 main (int argc, char *argv[])
91 {
92   GMainLoop *loop;
93   GstRTSPServer *server;
94   GstRTSPMountPoints *mounts;
95   GstRTSPMediaFactory *factory;
96   GOptionContext *optctx;
97   GError *error = NULL;
98   gchar *str;
99
100   optctx = g_option_context_new ("<filename.mp4> - Test RTSP Server, MP4");
101   g_option_context_add_main_entries (optctx, entries, NULL);
102   g_option_context_add_group (optctx, gst_init_get_option_group ());
103   if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
104     g_printerr ("Error parsing options: %s\n", error->message);
105     return -1;
106   }
107   g_option_context_free (optctx);
108
109   loop = g_main_loop_new (NULL, FALSE);
110
111   /* create a server instance */
112   server = gst_rtsp_server_new ();
113   g_object_set (server, "service", port, NULL);
114
115   /* get the mount points for this server, every server has a default object
116    * that be used to map uri mount points to media factories */
117   mounts = gst_rtsp_server_get_mount_points (server);
118
119   str = g_strdup_printf ("( "
120       "filesrc location=%s ! qtdemux name=d "
121       "d. ! queue ! rtph264pay pt=96 name=pay0 "
122       "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")", argv[1]);
123
124   /* make a media factory for a test stream. The default media factory can use
125    * gst-launch syntax to create pipelines. 
126    * any launch line works as long as it contains elements named pay%d. Each
127    * element with pay%d names will be a stream */
128   factory = gst_rtsp_media_factory_new ();
129   gst_rtsp_media_factory_set_launch (factory, str);
130   g_signal_connect (factory, "media-configure", (GCallback) media_configure_cb,
131       factory);
132   g_free (str);
133
134   /* attach the test factory to the /test url */
135   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
136
137   /* don't need the ref to the mapper anymore */
138   g_object_unref (mounts);
139
140   /* attach the server to the default maincontext */
141   gst_rtsp_server_attach (server, NULL);
142
143   /* start serving */
144   g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
145   g_main_loop_run (loop);
146
147   return 0;
148 }