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 #define DEFAULT_FLASH_VERSION "LNX 10,0,32,18"
37
38 G_DEFINE_INTERFACE (GstRtmpLocationHandler, gst_rtmp_location_handler, 0);
39
40 #define GST_CAT_DEFAULT gst_rtmp_location_handler_debug_category
41 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
42
43 static void
gst_rtmp_location_handler_default_init(GstRtmpLocationHandlerInterface * iface)44 gst_rtmp_location_handler_default_init (GstRtmpLocationHandlerInterface * iface)
45 {
46 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "rtmp2locationhandler", 0,
47 "RTMP2 Location Handling");
48
49 g_object_interface_install_property (iface, g_param_spec_string ("location",
50 "Location", "Location of RTMP stream to access", DEFAULT_LOCATION,
51 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
52 g_object_interface_install_property (iface, g_param_spec_enum ("scheme",
53 "Scheme", "RTMP connection scheme",
54 GST_TYPE_RTMP_SCHEME, DEFAULT_SCHEME,
55 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
56 g_object_interface_install_property (iface, g_param_spec_string ("host",
57 "Host", "RTMP server host name", DEFAULT_HOST,
58 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
59 g_object_interface_install_property (iface, g_param_spec_int ("port", "Port",
60 "RTMP server port", 1, 65535,
61 gst_rtmp_scheme_get_default_port (DEFAULT_SCHEME),
62 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
63 g_object_interface_install_property (iface,
64 g_param_spec_string ("application", "Application",
65 "RTMP application path", DEFAULT_APPLICATION,
66 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
67 g_object_interface_install_property (iface, g_param_spec_string ("stream",
68 "Stream", "RTMP stream path", DEFAULT_STREAM,
69 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
70 g_object_interface_install_property (iface, g_param_spec_string ("username",
71 "User name", "RTMP authorization user name", DEFAULT_USERNAME,
72 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
73 g_object_interface_install_property (iface, g_param_spec_string ("password",
74 "Password", "RTMP authorization password", DEFAULT_PASSWORD,
75 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
76 g_object_interface_install_property (iface,
77 g_param_spec_string ("secure-token", "Secure token",
78 "RTMP authorization token", DEFAULT_SECURE_TOKEN,
79 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
80 g_object_interface_install_property (iface, g_param_spec_enum ("authmod",
81 "Authorization mode", "RTMP authorization mode",
82 GST_TYPE_RTMP_AUTHMOD, DEFAULT_AUTHMOD,
83 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
84 g_object_interface_install_property (iface, g_param_spec_uint ("timeout",
85 "Timeout", "RTMP timeout in seconds", 0, G_MAXUINT, DEFAULT_TIMEOUT,
86 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
87 g_object_interface_install_property (iface,
88 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
89 "TLS validation flags to use", G_TYPE_TLS_CERTIFICATE_FLAGS,
90 G_TLS_CERTIFICATE_VALIDATE_ALL,
91 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
92 g_object_interface_install_property (iface,
93 g_param_spec_string ("flash-version", "Flash version",
94 "Flash version reported to the server", DEFAULT_FLASH_VERSION,
95 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 }
97
98 static GstURIType
uri_handler_get_type_sink(GType type)99 uri_handler_get_type_sink (GType type)
100 {
101 return GST_URI_SINK;
102 }
103
104 static GstURIType
uri_handler_get_type_src(GType type)105 uri_handler_get_type_src (GType type)
106 {
107 return GST_URI_SRC;
108 }
109
110 static const gchar *const *
uri_handler_get_protocols(GType type)111 uri_handler_get_protocols (GType type)
112 {
113 return gst_rtmp_scheme_get_strings ();
114 }
115
116 static gchar *
uri_handler_get_uri(GstURIHandler * handler)117 uri_handler_get_uri (GstURIHandler * handler)
118 {
119 GstRtmpLocation location = { 0, };
120 gchar *string;
121
122 g_object_get (handler, "scheme", &location.scheme, "host", &location.host,
123 "port", &location.port, "application", &location.application,
124 "stream", &location.stream, NULL);
125
126 string = gst_rtmp_location_get_string (&location, TRUE);
127 gst_rtmp_location_clear (&location);
128 return string;
129 }
130
131 static gboolean
uri_handler_set_uri(GstURIHandler * handler,const gchar * string,GError ** error)132 uri_handler_set_uri (GstURIHandler * handler, const gchar * string,
133 GError ** error)
134 {
135 GstRtmpLocationHandler *self = GST_RTMP_LOCATION_HANDLER (handler);
136 GstUri *uri;
137 const gchar *scheme_sep, *path_sep, *stream_sep, *host, *userinfo;
138 GstRtmpScheme scheme;
139 guint port;
140 gboolean ret = FALSE;
141
142 GST_DEBUG_OBJECT (self, "setting URI from %s", GST_STR_NULL (string));
143 g_return_val_if_fail (string, FALSE);
144
145 scheme_sep = strstr (string, "://");
146 if (!scheme_sep) {
147 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
148 "URI lacks scheme: %s", string);
149 return FALSE;
150 }
151
152 path_sep = strchr (scheme_sep + 3, '/');
153 if (!path_sep) {
154 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
155 "URI lacks path: %s", string);
156 return FALSE;
157 }
158
159 stream_sep = strrchr (path_sep + 1, '/');
160 if (!stream_sep) {
161 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
162 "URI lacks stream: %s", string);
163 return FALSE;
164 }
165
166 {
167 gchar *string_without_path = g_strndup (string, path_sep - string);
168 uri = gst_uri_from_string_escaped (string_without_path);
169 g_free (string_without_path);
170 }
171
172 if (!uri) {
173 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
174 "URI failed to parse: %s", string);
175 return FALSE;
176 }
177
178 gst_uri_normalize (uri);
179
180 scheme = gst_rtmp_scheme_from_uri (uri);
181 if (scheme < 0) {
182 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
183 "URI has bad scheme: %s", string);
184 goto out;
185 }
186
187 host = gst_uri_get_host (uri);
188 if (!host) {
189 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
190 "URI lacks hostname: %s", string);
191 goto out;
192 }
193
194 port = gst_uri_get_port (uri);
195 if (port == GST_URI_NO_PORT) {
196 port = gst_rtmp_scheme_get_default_port (scheme);
197 }
198
199 {
200 const gchar *path = path_sep + 1, *stream = stream_sep + 1;
201 gchar *application = g_strndup (path, stream_sep - path);
202
203 GST_DEBUG_OBJECT (self, "setting location to %s://%s:%u/%s stream %s",
204 gst_rtmp_scheme_to_string (scheme), host, port, application, stream);
205
206 g_object_set (self, "scheme", scheme, "host", host, "port", port,
207 "application", application, "stream", stream, "username", NULL,
208 "password", NULL, NULL);
209
210 g_free (application);
211 }
212
213 userinfo = gst_uri_get_userinfo (uri);
214 if (userinfo) {
215 gchar *user, *pass;
216 gchar **split = g_strsplit (userinfo, ":", 2);
217
218 if (!split || !split[0] || !split[1]) {
219 g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_REFERENCE,
220 "Failed to parse username:password data");
221 g_strfreev (split);
222 goto out;
223 }
224
225 if (g_strrstr (split[1], ":") != NULL)
226 GST_WARNING_OBJECT (self, "userinfo %s contains more than one ':', will "
227 "assume that the first ':' delineates user:pass. You should escape "
228 "the user and pass before adding to the URI.", userinfo);
229
230 user = g_uri_unescape_string (split[0], NULL);
231 pass = g_uri_unescape_string (split[1], NULL);
232 g_strfreev (split);
233
234 g_object_set (self, "username", user, "password", pass, NULL);
235 g_free (user);
236 g_free (pass);
237 }
238
239 ret = TRUE;
240
241 out:
242 gst_uri_unref (uri);
243 return ret;
244 }
245
246 void
gst_rtmp_location_handler_implement_uri_handler(GstURIHandlerInterface * iface,GstURIType type)247 gst_rtmp_location_handler_implement_uri_handler (GstURIHandlerInterface * iface,
248 GstURIType type)
249 {
250 switch (type) {
251 case GST_URI_SINK:
252 iface->get_type = uri_handler_get_type_sink;
253 break;
254 case GST_URI_SRC:
255 iface->get_type = uri_handler_get_type_src;
256 break;
257 default:
258 g_return_if_reached ();
259 }
260 iface->get_protocols = uri_handler_get_protocols;
261 iface->get_uri = uri_handler_get_uri;
262 iface->set_uri = uri_handler_set_uri;
263 }
264
265 gboolean
gst_rtmp_location_handler_set_uri(GstRtmpLocationHandler * handler,const gchar * uri)266 gst_rtmp_location_handler_set_uri (GstRtmpLocationHandler * handler,
267 const gchar * uri)
268 {
269 GError *error = NULL;
270 gboolean ret;
271
272 g_return_val_if_fail (GST_IS_RTMP_LOCATION_HANDLER (handler), FALSE);
273
274 ret = gst_uri_handler_set_uri (GST_URI_HANDLER (handler), uri, &error);
275 if (!ret) {
276 GST_ERROR_OBJECT (handler, "Failed to set URI: %s", error->message);
277 g_object_set (handler, "scheme", DEFAULT_SCHEME, "host", NULL,
278 "port", gst_rtmp_scheme_get_default_port (DEFAULT_SCHEME),
279 "application", NULL, "stream", NULL, NULL);
280 g_error_free (error);
281 }
282 return ret;
283 }
284