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