tizen 2.4 release
[framework/multimedia/gst-rtsp-server.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   GError *error = NULL;
63 #endif
64
65   gst_init (&argc, &argv);
66
67   loop = g_main_loop_new (NULL, FALSE);
68
69   /* create a server instance */
70   server = gst_rtsp_server_new ();
71
72 #ifdef WITH_AUTH
73   /* make a new authentication manager. it can be added to control access to all
74    * the factories on the server or on individual factories. */
75   auth = gst_rtsp_auth_new ();
76 #ifdef WITH_TLS
77   cert = g_tls_certificate_new_from_pem ("-----BEGIN CERTIFICATE-----"
78       "MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk"
79       "ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp"
80       "ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5jb20xHTAbBgkq"
81       "hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTExMDExNzE5NDcxN1oXDTIxMDEx"
82       "NDE5NDcxN1owSzETMBEGCgmSJomT8ixkARkWA0NPTTEXMBUGCgmSJomT8ixkARkW"
83       "B0VYQU1QTEUxGzAZBgNVBAMTEnNlcnZlci5leGFtcGxlLmNvbTBcMA0GCSqGSIb3"
84       "DQEBAQUAA0sAMEgCQQDYScTxk55XBmbDM9zzwO+grVySE4rudWuzH2PpObIonqbf"
85       "hRoAalKVluG9jvbHI81eXxCdSObv1KBP1sbN5RzpAgMBAAGjIjAgMAkGA1UdEwQC"
86       "MAAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQADgYEAYx6fMqT1"
87       "Gvo0jq88E8mc+bmp4LfXD4wJ7KxYeadQxt75HFRpj4FhFO3DOpVRFgzHlOEo3Fwk"
88       "PZOKjvkT0cbcoEq5whLH25dHoQxGoVQgFyAP5s+7Vp5AlHh8Y/vAoXeEVyy/RCIH"
89       "QkhUlAflfDMcrrYjsmwoOPSjhx6Mm/AopX4="
90       "-----END CERTIFICATE-----"
91       "-----BEGIN PRIVATE KEY-----"
92       "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2EnE8ZOeVwZmwzPc"
93       "88DvoK1ckhOK7nVrsx9j6TmyKJ6m34UaAGpSlZbhvY72xyPNXl8QnUjm79SgT9bG"
94       "zeUc6QIDAQABAkBRFJZ32VbqWMP9OVwDJLiwC01AlYLnka0mIQZbT/2xq9dUc9GW"
95       "U3kiVw4lL8v/+sPjtTPCYYdzHHOyDen6znVhAiEA9qJT7BtQvRxCvGrAhr9MS022"
96       "tTdPbW829BoUtIeH64cCIQDggG5i48v7HPacPBIH1RaSVhXl8qHCpQD3qrIw3FMw"
97       "DwIga8PqH5Sf5sHedy2+CiK0V4MRfoU4c3zQ6kArI+bEgSkCIQCLA1vXBiE31B5s"
98       "bdHoYa1BXebfZVd+1Hd95IfEM5mbRwIgSkDuQwV55BBlvWph3U8wVIMIb4GStaH8"
99       "W535W8UBbEg=" "-----END PRIVATE KEY-----", -1, &error);
100   if (cert == NULL) {
101     g_printerr ("failed to parse PEM: %s\n", error->message);
102     return -1;
103   }
104   gst_rtsp_auth_set_tls_certificate (auth, cert);
105   g_object_unref (cert);
106 #endif
107
108   /* make user token */
109   token =
110       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
111       "user", NULL);
112   basic = gst_rtsp_auth_make_basic ("user", "password");
113   gst_rtsp_auth_add_basic (auth, basic, token);
114   g_free (basic);
115   gst_rtsp_token_unref (token);
116
117   /* configure in the server */
118   gst_rtsp_server_set_auth (server, auth);
119 #endif
120
121   /* get the mount points for this server, every server has a default object
122    * that be used to map uri mount points to media factories */
123   mounts = gst_rtsp_server_get_mount_points (server);
124
125   /* make a media factory for a test stream. The default media factory can use
126    * gst-launch syntax to create pipelines.
127    * any launch line works as long as it contains elements named pay%d. Each
128    * element with pay%d names will be a stream */
129   factory = gst_rtsp_media_factory_new ();
130   gst_rtsp_media_factory_set_launch (factory, "( "
131       "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
132       "x264enc ! rtph264pay name=pay0 pt=96 "
133       "audiotestsrc ! audio/x-raw,rate=8000 ! "
134       "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
135 #ifdef WITH_AUTH
136   /* add permissions for the user media role */
137   permissions = gst_rtsp_permissions_new ();
138   gst_rtsp_permissions_add_role (permissions, "user",
139       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
140       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
141   gst_rtsp_media_factory_set_permissions (factory, permissions);
142   gst_rtsp_permissions_unref (permissions);
143 #ifdef WITH_TLS
144   gst_rtsp_media_factory_set_profiles (factory, GST_RTSP_PROFILE_SAVP);
145 #endif
146 #endif
147
148   /* attach the test factory to the /test url */
149   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
150
151   /* don't need the ref to the mapper anymore */
152   g_object_unref (mounts);
153
154   /* attach the server to the default maincontext */
155   if (gst_rtsp_server_attach (server, NULL) == 0)
156     goto failed;
157
158   /* add a timeout for the session cleanup */
159   g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
160
161   /* start serving, this never stops */
162 #ifdef WITH_TLS
163   g_print ("stream ready at rtsps://127.0.0.1:8554/test\n");
164 #else
165   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
166 #endif
167   g_main_loop_run (loop);
168
169   return 0;
170
171   /* ERRORS */
172 failed:
173   {
174     g_print ("failed to attach the server\n");
175     return -1;
176   }
177 }