rtsp-media: Unblock all streams
[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 static void
55 on_sender_ssrc_active (GObject * session, GObject * source,
56     GstRTSPMedia * media)
57 {
58   GstStructure *stats;
59
60   GST_INFO ("source %p in session %p is active", source, session);
61
62   g_object_get (source, "stats", &stats, NULL);
63   if (stats) {
64     gchar *sstr;
65
66     sstr = gst_structure_to_string (stats);
67     g_print ("Sender stats:\nstructure: %s\n", sstr);
68     g_free (sstr);
69
70     gst_structure_free (stats);
71   }
72 }
73
74 /* signal callback when the media is prepared for streaming. We can get the
75  * session manager for each of the streams and connect to some signals. */
76 static void
77 media_prepared_cb (GstRTSPMedia * media)
78 {
79   guint i, n_streams;
80
81   n_streams = gst_rtsp_media_n_streams (media);
82
83   GST_INFO ("media %p is prepared and has %u streams", media, n_streams);
84
85   for (i = 0; i < n_streams; i++) {
86     GstRTSPStream *stream;
87     GObject *session;
88
89     stream = gst_rtsp_media_get_stream (media, i);
90     if (stream == NULL)
91       continue;
92
93     session = gst_rtsp_stream_get_rtpsession (stream);
94     GST_INFO ("watching session %p on stream %u", session, i);
95
96     g_signal_connect (session, "on-ssrc-active",
97         (GCallback) on_ssrc_active, media);
98     g_signal_connect (session, "on-sender-ssrc-active",
99         (GCallback) on_sender_ssrc_active, media);
100   }
101 }
102
103 static void
104 media_configure_cb (GstRTSPMediaFactory * factory, GstRTSPMedia * media)
105 {
106   /* connect our prepared signal so that we can see when this media is
107    * prepared for streaming */
108   g_signal_connect (media, "prepared", (GCallback) media_prepared_cb, factory);
109 }
110
111 int
112 main (int argc, char *argv[])
113 {
114   GMainLoop *loop;
115   GstRTSPServer *server;
116   GstRTSPMountPoints *mounts;
117   GstRTSPMediaFactory *factory;
118   GOptionContext *optctx;
119   GError *error = NULL;
120   gchar *str;
121
122   optctx = g_option_context_new ("<filename.mp4> - Test RTSP Server, MP4");
123   g_option_context_add_main_entries (optctx, entries, NULL);
124   g_option_context_add_group (optctx, gst_init_get_option_group ());
125   if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
126     g_printerr ("Error parsing options: %s\n", error->message);
127     g_option_context_free (optctx);
128     g_clear_error (&error);
129     return -1;
130   }
131
132   if (argc < 2) {
133     g_print ("%s\n", g_option_context_get_help (optctx, TRUE, NULL));
134     return 1;
135   }
136   g_option_context_free (optctx);
137
138   loop = g_main_loop_new (NULL, FALSE);
139
140   /* create a server instance */
141   server = gst_rtsp_server_new ();
142   g_object_set (server, "service", port, NULL);
143
144   /* get the mount points for this server, every server has a default object
145    * that be used to map uri mount points to media factories */
146   mounts = gst_rtsp_server_get_mount_points (server);
147
148   str = g_strdup_printf ("( "
149       "filesrc location=\"%s\" ! qtdemux name=d "
150       "d. ! queue ! rtph264pay pt=96 name=pay0 "
151       "d. ! queue ! rtpmp4apay pt=97 name=pay1 " ")", argv[1]);
152
153   /* make a media factory for a test stream. The default media factory can use
154    * gst-launch syntax to create pipelines. 
155    * any launch line works as long as it contains elements named pay%d. Each
156    * element with pay%d names will be a stream */
157   factory = gst_rtsp_media_factory_new ();
158   gst_rtsp_media_factory_set_launch (factory, str);
159   g_signal_connect (factory, "media-configure", (GCallback) media_configure_cb,
160       factory);
161   g_free (str);
162
163   /* attach the test factory to the /test url */
164   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
165
166   /* don't need the ref to the mapper anymore */
167   g_object_unref (mounts);
168
169   /* attach the server to the default maincontext */
170   gst_rtsp_server_attach (server, NULL);
171
172   /* start serving */
173   g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
174   g_main_loop_run (loop);
175
176   return 0;
177 }