media: possibility to override range time conversion
[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/admin as the password */
26 #undef WITH_AUTH
27
28 /* define this if you want the server to use TLS */
29 #undef WITH_TLS
30
31 /* this timeout is periodically run to clean up the expired sessions from the
32  * pool. This needs to be run explicitly currently but might be done
33  * automatically as part of the mainloop. */
34 static gboolean
35 timeout (GstRTSPServer * server, gboolean ignored)
36 {
37   GstRTSPSessionPool *pool;
38
39   pool = gst_rtsp_server_get_session_pool (server);
40   gst_rtsp_session_pool_cleanup (pool);
41   g_object_unref (pool);
42
43   return TRUE;
44 }
45
46 int
47 main (int argc, char *argv[])
48 {
49   GMainLoop *loop;
50   GstRTSPServer *server;
51   GstRTSPMountPoints *mounts;
52   GstRTSPMediaFactory *factory;
53 #ifdef WITH_AUTH
54   GstRTSPAuth *auth;
55   gchar *basic;
56 #endif
57 #ifdef WITH_TLS
58   GTlsCertificate *cert;
59 #endif
60
61   gst_init (&argc, &argv);
62
63   loop = g_main_loop_new (NULL, FALSE);
64
65   /* create a server instance */
66   server = gst_rtsp_server_new ();
67 #ifdef WITH_TLS
68   cert = g_tls_certificate_new_from_pem ("-----BEGIN CERTIFICATE-----"
69       "MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk"
70       "ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp"
71       "ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5jb20xHTAbBgkq"
72       "hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTExMDExNzE5NDcxN1oXDTIxMDEx"
73       "NDE5NDcxN1owSzETMBEGCgmSJomT8ixkARkWA0NPTTEXMBUGCgmSJomT8ixkARkW"
74       "B0VYQU1QTEUxGzAZBgNVBAMTEnNlcnZlci5leGFtcGxlLmNvbTBcMA0GCSqGSIb3"
75       "DQEBAQUAA0sAMEgCQQDYScTxk55XBmbDM9zzwO+grVySE4rudWuzH2PpObIonqbf"
76       "hRoAalKVluG9jvbHI81eXxCdSObv1KBP1sbN5RzpAgMBAAGjIjAgMAkGA1UdEwQC"
77       "MAAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQADgYEAYx6fMqT1"
78       "Gvo0jq88E8mc+bmp4LfXD4wJ7KxYeadQxt75HFRpj4FhFO3DOpVRFgzHlOEo3Fwk"
79       "PZOKjvkT0cbcoEq5whLH25dHoQxGoVQgFyAP5s+7Vp5AlHh8Y/vAoXeEVyy/RCIH"
80       "QkhUlAflfDMcrrYjsmwoOPSjhx6Mm/AopX4="
81       "-----END CERTIFICATE-----"
82       "-----BEGIN PRIVATE KEY-----"
83       "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2EnE8ZOeVwZmwzPc"
84       "88DvoK1ckhOK7nVrsx9j6TmyKJ6m34UaAGpSlZbhvY72xyPNXl8QnUjm79SgT9bG"
85       "zeUc6QIDAQABAkBRFJZ32VbqWMP9OVwDJLiwC01AlYLnka0mIQZbT/2xq9dUc9GW"
86       "U3kiVw4lL8v/+sPjtTPCYYdzHHOyDen6znVhAiEA9qJT7BtQvRxCvGrAhr9MS022"
87       "tTdPbW829BoUtIeH64cCIQDggG5i48v7HPacPBIH1RaSVhXl8qHCpQD3qrIw3FMw"
88       "DwIga8PqH5Sf5sHedy2+CiK0V4MRfoU4c3zQ6kArI+bEgSkCIQCLA1vXBiE31B5s"
89       "bdHoYa1BXebfZVd+1Hd95IfEM5mbRwIgSkDuQwV55BBlvWph3U8wVIMIb4GStaH8"
90       "W535W8UBbEg=" "-----END PRIVATE KEY-----", -1, NULL);
91   gst_rtsp_server_set_tls_certificate (server, cert);
92   g_object_unref (cert);
93 #endif
94
95   /* get the mount points for this server, every server has a default object
96    * that be used to map uri mount points to media factories */
97   mounts = gst_rtsp_server_get_mount_points (server);
98
99 #ifdef WITH_AUTH
100   /* make a new authentication manager. it can be added to control access to all
101    * the factories on the server or on individual factories. */
102   auth = gst_rtsp_auth_new ();
103   basic = gst_rtsp_auth_make_basic ("user", "admin");
104   gst_rtsp_auth_set_basic (auth, basic);
105   g_free (basic);
106   /* configure in the server */
107   gst_rtsp_server_set_auth (server, auth);
108 #endif
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       "videotestsrc ! video/x-raw,width=352,height=288,framerate=15/1 ! "
117       "x264enc ! rtph264pay name=pay0 pt=96 "
118       "audiotestsrc ! audio/x-raw,rate=8000 ! "
119       "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
120
121   /* attach the test factory to the /test url */
122   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
123
124   /* don't need the ref to the mapper anymore */
125   g_object_unref (mounts);
126
127   /* attach the server to the default maincontext */
128   if (gst_rtsp_server_attach (server, NULL) == 0)
129     goto failed;
130
131   /* add a timeout for the session cleanup */
132   g_timeout_add_seconds (2, (GSourceFunc) timeout, server);
133
134   /* start serving, this never stops */
135 #ifdef WITH_TLS
136   g_print ("stream ready at rtsps://127.0.0.1:8554/test\n");
137 #else
138   g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
139 #endif
140   g_main_loop_run (loop);
141
142   return 0;
143
144   /* ERRORS */
145 failed:
146   {
147     g_print ("failed to attach the server\n");
148     return -1;
149   }
150 }