Add files from gst-rtmp
[platform/upstream/gstreamer.git] / gst / rtmp2 / gstrtmp2locationhandler.c
1 /* GStreamer
2  * Copyright (C) 2017 Make.TV, Inc. <info@make.tv>
3  *   Contact: Jan Alexander Steffens (heftig) <jsteffens@make.tv>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "gstrtmp2locationhandler.h"
22 #include "rtmp/rtmputils.h"
23 #include "rtmp/rtmpclient.h"
24 #include <string.h>
25
26 #define DEFAULT_SCHEME GST_RTMP_SCHEME_RTMP
27 #define DEFAULT_HOST "localhost"
28 #define DEFAULT_APPLICATION "live"
29 #define DEFAULT_STREAM "myStream"
30 #define DEFAULT_LOCATION "rtmp://" DEFAULT_HOST "/" DEFAULT_APPLICATION "/" DEFAULT_STREAM
31 #define DEFAULT_SECURE_TOKEN NULL
32 #define DEFAULT_USERNAME NULL
33 #define DEFAULT_PASSWORD NULL
34 #define DEFAULT_AUTHMOD  GST_RTMP_AUTHMOD_AUTO
35 #define DEFAULT_TIMEOUT 5
36
37 G_DEFINE_INTERFACE (GstRtmpLocationHandler, gst_rtmp_location_handler, 0);
38
39 #define GST_CAT_DEFAULT gst_rtmp_location_handler_debug_category
40 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
41
42 static void
43 gst_rtmp_location_handler_default_init (GstRtmpLocationHandlerInterface * iface)
44 {
45   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "rtmp2locationhandler", 0,
46       "RTMP2 Location Handling");
47
48   g_object_interface_install_property (iface, g_param_spec_string ("location",
49           "Location", "Location of RTMP stream to access", DEFAULT_LOCATION,
50           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
51   g_object_interface_install_property (iface, g_param_spec_enum ("scheme",
52           "Scheme", "RTMP connection scheme",
53           GST_TYPE_RTMP_SCHEME, DEFAULT_SCHEME,
54           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
55   g_object_interface_install_property (iface, g_param_spec_string ("host",
56           "Host", "RTMP server host name", DEFAULT_HOST,
57           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
58   g_object_interface_install_property (iface, g_param_spec_int ("port", "Port",
59           "RTMP server port", 1, 65535,
60           gst_rtmp_scheme_get_default_port (DEFAULT_SCHEME),
61           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
62   g_object_interface_install_property (iface,
63       g_param_spec_string ("application", "Application",
64           "RTMP application path", DEFAULT_APPLICATION,
65           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
66   g_object_interface_install_property (iface, g_param_spec_string ("stream",
67           "Stream", "RTMP stream path", DEFAULT_STREAM,
68           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
69   g_object_interface_install_property (iface, g_param_spec_string ("username",
70           "User name", "RTMP authorization user name", DEFAULT_USERNAME,
71           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
72   g_object_interface_install_property (iface, g_param_spec_string ("password",
73           "Password", "RTMP authorization password", DEFAULT_PASSWORD,
74           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
75   g_object_interface_install_property (iface,
76       g_param_spec_string ("secure-token", "Secure token",
77           "RTMP authorization token", DEFAULT_SECURE_TOKEN,
78           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
79   g_object_interface_install_property (iface, g_param_spec_enum ("authmod",
80           "Authorization mode", "RTMP authorization mode",
81           GST_TYPE_RTMP_AUTHMOD, DEFAULT_AUTHMOD,
82           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
83   g_object_interface_install_property (iface, g_param_spec_uint ("timeout",
84           "Timeout", "RTMP timeout in seconds", 0, G_MAXUINT, DEFAULT_TIMEOUT,
85           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
86   g_object_interface_install_property (iface,
87       g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
88           "TLS validation flags to use", G_TYPE_TLS_CERTIFICATE_FLAGS,
89           G_TLS_CERTIFICATE_VALIDATE_ALL,
90           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
91 }
92
93 static GstURIType
94 uri_handler_get_type_sink (GType type)
95 {
96   return GST_URI_SINK;
97 }
98
99 static GstURIType
100 uri_handler_get_type_src (GType type)
101 {
102   return GST_URI_SRC;
103 }
104
105 static const gchar *const *
106 uri_handler_get_protocols (GType type)
107 {
108   return gst_rtmp_scheme_get_strings ();
109 }
110
111 static gchar *
112 uri_handler_get_uri (GstURIHandler * handler)
113 {
114   GstRtmpLocation location = { 0, };
115   gchar *string;
116
117   g_object_get (handler, "scheme", &location.scheme, "host", &location.host,
118       "port", &location.port, "application", &location.application,
119       "stream", &location.stream, NULL);
120
121   string = gst_rtmp_location_get_string (&location, TRUE);
122   gst_rtmp_location_clear (&location);
123   return string;
124 }
125
126 static gboolean
127 uri_handler_set_uri (GstURIHandler * handler, const gchar * string,
128     GError ** error)
129 {
130   GstRtmpLocationHandler *self = GST_RTMP_LOCATION_HANDLER (handler);
131   GstUri *uri;
132   const gchar *scheme_sep, *path_sep, *stream_sep, *host, *userinfo;
133   GstRtmpScheme scheme;
134   guint port;
135   gboolean ret = FALSE;
136
137   GST_DEBUG_OBJECT (self, "setting URI from %s", GST_STR_NULL (string));
138   g_return_val_if_fail (string, FALSE);
139
140   scheme_sep = strstr (string, "://");
141   if (!scheme_sep) {
142     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
143         "URI lacks scheme: %s", string);
144     return FALSE;
145   }
146
147   path_sep = strchr (scheme_sep + 3, '/');
148   if (!path_sep) {
149     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
150         "URI lacks path: %s", string);
151     return FALSE;
152   }
153
154   stream_sep = strrchr (path_sep + 1, '/');
155   if (!stream_sep) {
156     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
157         "URI lacks stream: %s", string);
158     return FALSE;
159   }
160
161   {
162     gchar *string_without_path = g_strndup (string, path_sep - string);
163     uri = gst_uri_from_string (string_without_path);
164     g_free (string_without_path);
165   }
166
167   if (!uri) {
168     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
169         "URI failed to parse: %s", string);
170     return FALSE;
171   }
172
173   gst_uri_normalize (uri);
174
175   scheme = gst_rtmp_scheme_from_uri (uri);
176   if (scheme < 0) {
177     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
178         "URI has bad scheme: %s", string);
179     goto out;
180   }
181
182   host = gst_uri_get_host (uri);
183   if (!host) {
184     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
185         "URI lacks hostname: %s", string);
186     goto out;
187   }
188
189   port = gst_uri_get_port (uri);
190   if (port == GST_URI_NO_PORT) {
191     port = gst_rtmp_scheme_get_default_port (scheme);
192   }
193
194   {
195     const gchar *path = path_sep + 1, *stream = stream_sep + 1;
196     gchar *application = g_strndup (path, stream_sep - path);
197
198     GST_DEBUG_OBJECT (self, "setting location to %s://%s:%u/%s stream %s",
199         gst_rtmp_scheme_to_string (scheme), host, port, application, stream);
200
201     g_object_set (self, "scheme", scheme, "host", host, "port", port,
202         "application", application, "stream", stream, "username", NULL,
203         "password", NULL, NULL);
204
205     g_free (application);
206   }
207
208   userinfo = gst_uri_get_userinfo (uri);
209   if (userinfo) {
210     gchar **split = g_strsplit (userinfo, ":", 2);
211
212     if (!split || !split[0] || !split[1]) {
213       g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
214           "Failed to parse username:password data");
215       g_strfreev (split);
216       goto out;
217     }
218
219     g_object_set (self, "username", split[0], "password", split[1], NULL);
220     g_strfreev (split);
221   }
222
223   ret = TRUE;
224
225 out:
226   gst_uri_unref (uri);
227   return ret;
228 }
229
230 void
231 gst_rtmp_location_handler_implement_uri_handler (GstURIHandlerInterface * iface,
232     GstURIType type)
233 {
234   switch (type) {
235     case GST_URI_SINK:
236       iface->get_type = uri_handler_get_type_sink;
237       break;
238     case GST_URI_SRC:
239       iface->get_type = uri_handler_get_type_src;
240       break;
241     default:
242       g_return_if_reached ();
243   }
244   iface->get_protocols = uri_handler_get_protocols;
245   iface->get_uri = uri_handler_get_uri;
246   iface->set_uri = uri_handler_set_uri;
247 }
248
249 gboolean
250 gst_rtmp_location_handler_set_uri (GstRtmpLocationHandler * handler,
251     const gchar * uri)
252 {
253   GError *error = NULL;
254   gboolean ret;
255
256   g_return_val_if_fail (GST_IS_RTMP_LOCATION_HANDLER (handler), FALSE);
257
258   ret = gst_uri_handler_set_uri (GST_URI_HANDLER (handler), uri, &error);
259   if (!ret) {
260     GST_ERROR_OBJECT (handler, "Failed to set URI: %s", error->message);
261     g_object_set (handler, "scheme", DEFAULT_SCHEME, "host", NULL,
262         "port", gst_rtmp_scheme_get_default_port (DEFAULT_SCHEME),
263         "application", NULL, "stream", NULL, NULL);
264     g_error_free (error);
265   }
266   return ret;
267 }