media: handle add-added on non-bins too
[platform/upstream/gstreamer.git] / examples / test-video.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 this if you want the resource to only be available when using
25  * user/password as the password */
26 #undef WITH_AUTH
27
28 /* define this if you want the server to use TLS (it will also need WITH_AUTH
29  * to be defined) */
30 #undef WITH_TLS
31
32 /* this timeout is periodically run to clean up the expired sessions from the
33  * pool. This needs to be run explicitly currently but might be done
34  * automatically as part of the mainloop. */
35 static gboolean
36 timeout (GstRTSPServer * server, gboolean ignored)
37 {
38   GstRTSPSessionPool *pool;
39
40   pool = gst_rtsp_server_get_session_pool (server);
41   gst_rtsp_session_pool_cleanup (pool);
42   g_object_unref (pool);
43
44   return TRUE;
45 }
46
47 int
48 main (int argc, char *argv[])
49 {
50   GMainLoop *loop;
51   GstRTSPServer *server;
52   GstRTSPMountPoints *mounts;
53   GstRTSPMediaFactory *factory;
54 #ifdef WITH_AUTH
55   GstRTSPAuth *auth;
56   GstRTSPToken *token;
57   gchar *basic;
58   GstRTSPPermissions *permissions;
59 #endif
60 #ifdef WITH_TLS
61   GTlsCertificate *cert;
62 #endif
63
64   gst_init (&argc, &argv);
65
66   loop = g_main_loop_new (NULL, FALSE);
67
68   /* create a server instance */
69   server = gst_rtsp_server_new ();
70
71 #ifdef WITH_AUTH
72   /* make a new authentication manager. it can be added to control access to all
73    * the factories on the server or on individual factories. */
74   auth = gst_rtsp_auth_new ();
75 #ifdef WITH_TLS
76   cert = g_tls_certificate_new_from_pem ("-----BEGIN CERTIFICATE-----"
77       "MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk"
78       "ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp"
79       "ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5jb20xHTAbBgkq"
80       "hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTExMDExNzE5NDcxN1oXDTIxMDEx"
81       "NDE5NDcxN1owSzETMBEGCgmSJomT8ixkARkWA0NPTTEXMBUGCgmSJomT8ixkARkW"
82       "B0VYQU1QTEUxGzAZBgNVBAMTEnNlcnZlci5leGFtcGxlLmNvbTBcMA0GCSqGSIb3"
83       "DQEBAQUAA0sAMEgCQQDYScTxk55XBmbDM9zzwO+grVySE4rudWuzH2PpObIonqbf"
84       "hRoAalKVluG9jvbHI81eXxCdSObv1KBP1sbN5RzpAgMBAAGjIjAgMAkGA1UdEwQC"
85       "MAAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQADgYEAYx6fMqT1"
86       "Gvo0jq88E8mc+bmp4LfXD4wJ7KxYeadQxt75HFRpj4FhFO3DOpVRFgzHlOEo3Fwk"
87       "PZOKjvkT0cbcoEq5whLH25dHoQxGoVQgFyAP5s+7Vp5AlHh8Y/vAoXeEVyy/RCIH"
88       "QkhUlAflfDMcrrYjsmwoOPSjhx6Mm/AopX4="
89       "-----END CERTIFICATE-----"
90       "-----BEGIN PRIVATE KEY-----"
91       "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2EnE8ZOeVwZmwzPc"
92       "88DvoK1ckhOK7nVrsx9j6TmyKJ6m34UaAGpSlZbhvY72xyPNXl8QnUjm79SgT9bG"
93       "zeUc6QIDAQABAkBRFJZ32VbqWMP9OVwDJLiwC01AlYLnka0mIQZbT/2xq9dUc9GW"
94       "U3kiVw4lL8v/+sPjtTPCYYdzHHOyDen6znVhAiEA9qJT7BtQvRxCvGrAhr9MS022"
95       "tTdPbW829BoUtIeH64cCIQDggG5i48v7HPacPBIH1RaSVhXl8qHCpQD3qrIw3FMw"
96       "DwIga8PqH5Sf5sHedy2+CiK0V4MRfoU4c3zQ6kArI+bEgSkCIQCLA1vXBiE31B5s"
97       "bdHoYa1BXebfZVd+1Hd95IfEM5mbRwIgSkDuQwV55BBlvWph3U8wVIMIb4GStaH8"
98       "W535W8UBbEg=" "-----END PRIVATE KEY-----", -1, NULL);
99   gst_rtsp_auth_set_tls_certificate (auth, cert);
100   g_object_unref (cert);
101 #endif
102
103   /* make user token */
104   token =
105       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
106       "user", NULL);
107   basic = gst_rtsp_auth_make_basic ("user", "password");
108   gst_rtsp_auth_add_basic (auth, basic, token);
109   g_free (basic);
110   gst_rtsp_token_unref (token);
111
112   /* configure in the server */
113   gst_rtsp_server_set_auth (server, auth);
114 #endif
115
116   /* get the mount points for this server, every server has a default object
117    * that be used to map uri mount points to media factories */
118   mounts = gst_rtsp_server_get_mount_points (server);
119
120   /* make a media factory for a test stream. The default media factory can use
121    * gst-launch syntax to create pipelines.
122    * any launch line works as long as it contains elements named pay%d. Each
123    * element with pay%d names will be a stream */
124   factory = gst_rtsp_media_factory_new ();
125   gst_rtsp_media_factory_set_launch (factory, "( "
126       "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
127       "x264enc ! rtph264pay name=pay0 pt=96 "
128       "audiotestsrc ! audio/x-raw,rate=8000 ! "
129       "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
130 #ifdef WITH_AUTH
131   /* add permissions for the user media role */
132   permissions = gst_rtsp_permissions_new ();
133   gst_rtsp_permissions_add_role (permissions, "user",
134       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
135       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
136   gst_rtsp_media_factory_set_permissions (factory, permissions);
137   gst_rtsp_permissions_unref (permissions);
138 #endif
139
140   /* attach the test factory to the /test url */
141   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
142
143   /* don't need the ref to the mapper anymore */
144   g_object_unref (mounts);
145
146   /* attach the server to the default maincontext */
147   if (gst_rtsp_server_attach (server, NULL) == 0)
148     goto failed;
149
150   /* add a timeout for the session cleanup */
151   g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
152
153   /* start serving, this never stops */
154 #ifdef WITH_TLS
155   g_print ("stream ready at rtsps://127.0.0.1:8554/test\n");
156 #else
157   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
158 #endif
159   g_main_loop_run (loop);
160
161   return 0;
162
163   /* ERRORS */
164 failed:
165   {
166     g_print ("failed to attach the server\n");
167     return -1;
168   }
169 }