1 /* GStreamer
2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
4 * <2015> Jan Schmidt <jan at centricular dot com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 /*
22 * Unless otherwise indicated, Source Code is licensed under MIT license.
23 * See further explanation attached in License Statement (distributed in the file
24 * LICENSE).
25 *
26 * Permission is hereby granted, free of charge, to any person obtaining a copy of
27 * this software and associated documentation files (the "Software"), to deal in
28 * the Software without restriction, including without limitation the rights to
29 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
30 * of the Software, and to permit persons to whom the Software is furnished to do
31 * so, subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be included in all
34 * copies or substantial portions of the Software.
35 *
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42 * SOFTWARE.
43 */
44 /**
45 * SECTION:element-rtspclientsink
46 *
47 * Makes a connection to an RTSP server and send data via RTSP RECORD.
48 * rtspclientsink strictly follows RFC 2326
49 *
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspclientsink will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPClientSink:protocols property.
54 *
55 * rtspclientsink will internally instantiate an RTP session manager element
56 * that will handle the RTCP messages to and from the server, jitter removal,
57 * and packet reordering.
58 * This feature is implemented using the gstrtpbin element.
59 *
60 * rtspclientsink accepts any stream for which there is an installed payloader,
61 * creates the payloader and manages payload-types, as well as RTX setup.
62 * The new-payloader signal is fired when a payloader is created, in case
63 * an app wants to do custom configuration (such as for MTU).
64 *
65 * <refsect2>
66 * <title>Example launch line</title>
67 * |[
68 * gst-launch-1.0 videotestsrc ! jpegenc ! rtspclientsink location=rtsp://some.server/url
69 * ]| Establish a connection to an RTSP server and send JPEG encoded video packets
70 * </refsect2>
71 */
72
73 /* FIXMEs
74 * - Handle EOS properly and shutdown. The problem with EOS is we don't know
75 * when the server has received all data, so we don't know when to do teardown.
76 * At the moment, we forward EOS to the app as soon as we stop sending. Is there
77 * a way to know from the receiver that it's got all data? Some session timeout?
78 * - Implement extension support for Real / WMS if they support RECORD?
79 * - Add support for network clock synchronised streaming?
80 * - Fix crypto key nego so SAVP/SAVPF profiles work.
81 * - Test (&fix?) HTTP tunnel support
82 * - Add an address pool object for GstRTSPStreams to use for multicast
83 * - Test multicast UDP transport
84 */
85
86 #ifdef HAVE_CONFIG_H
87 #include "config.h"
88 #endif
89
90 #ifdef HAVE_UNISTD_H
91 #include <unistd.h>
92 #endif /* HAVE_UNISTD_H */
93 #include <stdlib.h>
94 #include <string.h>
95 #include <stdio.h>
96 #include <stdarg.h>
97
98 #include <gst/net/gstnet.h>
99 #include <gst/sdp/gstsdpmessage.h>
100 #include <gst/sdp/gstmikey.h>
101 #include <gst/rtp/rtp.h>
102
103 #include "gstrtspclientsink.h"
104
105 typedef struct _GstRtspClientSinkPad GstRtspClientSinkPad;
106 typedef GstGhostPadClass GstRtspClientSinkPadClass;
107
108 struct _GstRtspClientSinkPad
109 {
110 GstGhostPad parent;
111 GstElement *custom_payloader;
112 guint ulpfec_percentage;
113 };
114
115 enum
116 {
117 PROP_PAD_0,
118 PROP_PAD_PAYLOADER,
119 PROP_PAD_ULPFEC_PERCENTAGE
120 };
121
122 #define DEFAULT_PAD_ULPFEC_PERCENTAGE 0
123
124 static GType gst_rtsp_client_sink_pad_get_type (void);
125 G_DEFINE_TYPE (GstRtspClientSinkPad, gst_rtsp_client_sink_pad,
126 GST_TYPE_GHOST_PAD);
127 #define GST_TYPE_RTSP_CLIENT_SINK_PAD (gst_rtsp_client_sink_pad_get_type ())
128 #define GST_RTSP_CLIENT_SINK_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTSP_CLIENT_SINK_PAD,GstRtspClientSinkPad))
129
130 static void
gst_rtsp_client_sink_pad_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)131 gst_rtsp_client_sink_pad_set_property (GObject * object, guint prop_id,
132 const GValue * value, GParamSpec * pspec)
133 {
134 GstRtspClientSinkPad *pad;
135
136 pad = GST_RTSP_CLIENT_SINK_PAD (object);
137
138 switch (prop_id) {
139 case PROP_PAD_PAYLOADER:
140 GST_OBJECT_LOCK (pad);
141 if (pad->custom_payloader)
142 gst_object_unref (pad->custom_payloader);
143 pad->custom_payloader = g_value_get_object (value);
144 gst_object_ref_sink (pad->custom_payloader);
145 GST_OBJECT_UNLOCK (pad);
146 break;
147 case PROP_PAD_ULPFEC_PERCENTAGE:
148 GST_OBJECT_LOCK (pad);
149 pad->ulpfec_percentage = g_value_get_uint (value);
150 GST_OBJECT_UNLOCK (pad);
151 break;
152 default:
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154 break;
155 }
156 }
157
158 static void
gst_rtsp_client_sink_pad_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)159 gst_rtsp_client_sink_pad_get_property (GObject * object, guint prop_id,
160 GValue * value, GParamSpec * pspec)
161 {
162 GstRtspClientSinkPad *pad;
163
164 pad = GST_RTSP_CLIENT_SINK_PAD (object);
165
166 switch (prop_id) {
167 case PROP_PAD_PAYLOADER:
168 GST_OBJECT_LOCK (pad);
169 g_value_set_object (value, pad->custom_payloader);
170 GST_OBJECT_UNLOCK (pad);
171 break;
172 case PROP_PAD_ULPFEC_PERCENTAGE:
173 GST_OBJECT_LOCK (pad);
174 g_value_set_uint (value, pad->ulpfec_percentage);
175 GST_OBJECT_UNLOCK (pad);
176 break;
177 default:
178 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 break;
180 }
181 }
182
183 static void
gst_rtsp_client_sink_pad_dispose(GObject * object)184 gst_rtsp_client_sink_pad_dispose (GObject * object)
185 {
186 GstRtspClientSinkPad *pad = GST_RTSP_CLIENT_SINK_PAD (object);
187
188 if (pad->custom_payloader)
189 gst_object_unref (pad->custom_payloader);
190
191 G_OBJECT_CLASS (gst_rtsp_client_sink_pad_parent_class)->dispose (object);
192 }
193
194 static void
gst_rtsp_client_sink_pad_class_init(GstRtspClientSinkPadClass * klass)195 gst_rtsp_client_sink_pad_class_init (GstRtspClientSinkPadClass * klass)
196 {
197 GObjectClass *gobject_klass;
198
199 gobject_klass = (GObjectClass *) klass;
200
201 gobject_klass->set_property = gst_rtsp_client_sink_pad_set_property;
202 gobject_klass->get_property = gst_rtsp_client_sink_pad_get_property;
203 gobject_klass->dispose = gst_rtsp_client_sink_pad_dispose;
204
205 g_object_class_install_property (gobject_klass, PROP_PAD_PAYLOADER,
206 g_param_spec_object ("payloader", "Payloader",
207 "The payloader element to use (NULL = default automatically selected)",
208 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209
210 g_object_class_install_property (gobject_klass, PROP_PAD_ULPFEC_PERCENTAGE,
211 g_param_spec_uint ("ulpfec-percentage", "ULPFEC percentage",
212 "The percentage of ULP redundancy to apply", 0, 100,
213 DEFAULT_PAD_ULPFEC_PERCENTAGE,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 }
216
217 static void
gst_rtsp_client_sink_pad_init(GstRtspClientSinkPad * pad)218 gst_rtsp_client_sink_pad_init (GstRtspClientSinkPad * pad)
219 {
220 }
221
222 static GstPad *
gst_rtsp_client_sink_pad_new(const GstPadTemplate * pad_tmpl,const gchar * name)223 gst_rtsp_client_sink_pad_new (const GstPadTemplate * pad_tmpl,
224 const gchar * name)
225 {
226 GstRtspClientSinkPad *ret;
227
228 ret =
229 g_object_new (GST_TYPE_RTSP_CLIENT_SINK_PAD, "direction", GST_PAD_SINK,
230 "template", pad_tmpl, "name", name, NULL);
231 gst_ghost_pad_construct (GST_GHOST_PAD_CAST (ret));
232
233 return GST_PAD (ret);
234 }
235
236 GST_DEBUG_CATEGORY_STATIC (rtsp_client_sink_debug);
237 #define GST_CAT_DEFAULT (rtsp_client_sink_debug)
238
239 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
240 GST_PAD_SINK,
241 GST_PAD_REQUEST,
242 GST_STATIC_CAPS_ANY); /* Actual caps come from available set of payloaders */
243
244 enum
245 {
246 SIGNAL_HANDLE_REQUEST,
247 SIGNAL_NEW_MANAGER,
248 SIGNAL_NEW_PAYLOADER,
249 SIGNAL_REQUEST_RTCP_KEY,
250 SIGNAL_ACCEPT_CERTIFICATE,
251 LAST_SIGNAL
252 };
253
254 enum _GstRTSPClientSinkNtpTimeSource
255 {
256 NTP_TIME_SOURCE_NTP,
257 NTP_TIME_SOURCE_UNIX,
258 NTP_TIME_SOURCE_RUNNING_TIME,
259 NTP_TIME_SOURCE_CLOCK_TIME
260 };
261
262 #define GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE (gst_rtsp_client_sink_ntp_time_source_get_type())
263 static GType
gst_rtsp_client_sink_ntp_time_source_get_type(void)264 gst_rtsp_client_sink_ntp_time_source_get_type (void)
265 {
266 static GType ntp_time_source_type = 0;
267 static const GEnumValue ntp_time_source_values[] = {
268 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
269 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
270 {NTP_TIME_SOURCE_RUNNING_TIME,
271 "Running time based on pipeline clock",
272 "running-time"},
273 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
274 {0, NULL, NULL},
275 };
276
277 if (!ntp_time_source_type) {
278 ntp_time_source_type =
279 g_enum_register_static ("GstRTSPClientSinkNtpTimeSource",
280 ntp_time_source_values);
281 }
282 return ntp_time_source_type;
283 }
284
285 #define DEFAULT_LOCATION NULL
286 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
287 #define DEFAULT_DEBUG FALSE
288 #define DEFAULT_RETRY 20
289 #define DEFAULT_TIMEOUT 5000000
290 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
291 #define DEFAULT_TCP_TIMEOUT 20000000
292 #define DEFAULT_LATENCY_MS 2000
293 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
294 #define DEFAULT_PROXY NULL
295 #define DEFAULT_RTP_BLOCKSIZE 0
296 #define DEFAULT_USER_ID NULL
297 #define DEFAULT_USER_PW NULL
298 #define DEFAULT_PORT_RANGE NULL
299 #define DEFAULT_UDP_RECONNECT TRUE
300 #define DEFAULT_MULTICAST_IFACE NULL
301 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
302 #define DEFAULT_TLS_DATABASE NULL
303 #define DEFAULT_TLS_INTERACTION NULL
304 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
305 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
306 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
307 #define DEFAULT_RTX_TIME_MS 500
308
309 enum
310 {
311 PROP_0,
312 PROP_LOCATION,
313 PROP_PROTOCOLS,
314 PROP_DEBUG,
315 PROP_RETRY,
316 PROP_TIMEOUT,
317 PROP_TCP_TIMEOUT,
318 PROP_LATENCY,
319 PROP_RTX_TIME,
320 PROP_DO_RTSP_KEEP_ALIVE,
321 PROP_PROXY,
322 PROP_PROXY_ID,
323 PROP_PROXY_PW,
324 PROP_RTP_BLOCKSIZE,
325 PROP_USER_ID,
326 PROP_USER_PW,
327 PROP_PORT_RANGE,
328 PROP_UDP_BUFFER_SIZE,
329 PROP_UDP_RECONNECT,
330 PROP_MULTICAST_IFACE,
331 PROP_SDES,
332 PROP_TLS_VALIDATION_FLAGS,
333 PROP_TLS_DATABASE,
334 PROP_TLS_INTERACTION,
335 PROP_NTP_TIME_SOURCE,
336 PROP_USER_AGENT,
337 PROP_PROFILES
338 };
339
340 static void gst_rtsp_client_sink_finalize (GObject * object);
341
342 static void gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
343 const GValue * value, GParamSpec * pspec);
344 static void gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
345 GValue * value, GParamSpec * pspec);
346
347 static GstClock *gst_rtsp_client_sink_provide_clock (GstElement * element);
348
349 static void gst_rtsp_client_sink_uri_handler_init (gpointer g_iface,
350 gpointer iface_data);
351
352 static gboolean gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp,
353 const gchar * proxy);
354 static void gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink *
355 rtsp_client_sink, guint64 timeout);
356
357 static GstStateChangeReturn gst_rtsp_client_sink_change_state (GstElement *
358 element, GstStateChange transition);
359 static void gst_rtsp_client_sink_handle_message (GstBin * bin,
360 GstMessage * message);
361
362 static gboolean gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
363 GstRTSPMessage * response);
364
365 static gboolean gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink,
366 gint cmd, gint mask);
367
368 static GstRTSPResult gst_rtsp_client_sink_open (GstRTSPClientSink * sink,
369 gboolean async);
370 static GstRTSPResult gst_rtsp_client_sink_record (GstRTSPClientSink * sink,
371 gboolean async);
372 static GstRTSPResult gst_rtsp_client_sink_pause (GstRTSPClientSink * sink,
373 gboolean async);
374 static GstRTSPResult gst_rtsp_client_sink_close (GstRTSPClientSink * sink,
375 gboolean async, gboolean only_close);
376 static gboolean gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink);
377
378 static gboolean gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler,
379 const gchar * uri, GError ** error);
380 static gchar *gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler);
381
382 static gboolean gst_rtsp_client_sink_loop (GstRTSPClientSink * sink);
383 static void gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink,
384 gboolean flush);
385
386 static GstPad *gst_rtsp_client_sink_request_new_pad (GstElement * element,
387 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
388 static void gst_rtsp_client_sink_release_pad (GstElement * element,
389 GstPad * pad);
390
391 /* commands we send to out loop to notify it of events */
392 #define CMD_OPEN (1 << 0)
393 #define CMD_RECORD (1 << 1)
394 #define CMD_PAUSE (1 << 2)
395 #define CMD_CLOSE (1 << 3)
396 #define CMD_WAIT (1 << 4)
397 #define CMD_RECONNECT (1 << 5)
398 #define CMD_LOOP (1 << 6)
399
400 /* mask for all commands */
401 #define CMD_ALL ((CMD_LOOP << 1) - 1)
402
403 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
404 G_STMT_START { \
405 gchar *__txt = _gst_element_error_printf text; \
406 gst_element_post_message (GST_ELEMENT_CAST (el), \
407 gst_message_new_progress (GST_OBJECT_CAST (el), \
408 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
409 g_free (__txt); \
410 } G_STMT_END
411
412 static guint gst_rtsp_client_sink_signals[LAST_SIGNAL] = { 0 };
413
414 /*********************************
415 * GstChildProxy implementation *
416 *********************************/
417 static GObject *
gst_rtsp_client_sink_child_proxy_get_child_by_index(GstChildProxy * child_proxy,guint index)418 gst_rtsp_client_sink_child_proxy_get_child_by_index (GstChildProxy *
419 child_proxy, guint index)
420 {
421 GObject *obj;
422 GstRTSPClientSink *cs = GST_RTSP_CLIENT_SINK (child_proxy);
423
424 GST_OBJECT_LOCK (cs);
425 if ((obj = g_list_nth_data (GST_ELEMENT (cs)->sinkpads, index)))
426 g_object_ref (obj);
427 GST_OBJECT_UNLOCK (cs);
428
429 return obj;
430 }
431
432 static guint
gst_rtsp_client_sink_child_proxy_get_children_count(GstChildProxy * child_proxy)433 gst_rtsp_client_sink_child_proxy_get_children_count (GstChildProxy *
434 child_proxy)
435 {
436 guint count = 0;
437
438 GST_OBJECT_LOCK (child_proxy);
439 count = GST_ELEMENT (child_proxy)->numsinkpads;
440 GST_OBJECT_UNLOCK (child_proxy);
441
442 GST_INFO_OBJECT (child_proxy, "Children Count: %d", count);
443
444 return count;
445 }
446
447 static void
gst_rtsp_client_sink_child_proxy_init(gpointer g_iface,gpointer iface_data)448 gst_rtsp_client_sink_child_proxy_init (gpointer g_iface, gpointer iface_data)
449 {
450 GstChildProxyInterface *iface = g_iface;
451
452 GST_INFO ("intializing child proxy interface");
453 iface->get_child_by_index =
454 gst_rtsp_client_sink_child_proxy_get_child_by_index;
455 iface->get_children_count =
456 gst_rtsp_client_sink_child_proxy_get_children_count;
457 }
458
459 #define gst_rtsp_client_sink_parent_class parent_class
460 G_DEFINE_TYPE_WITH_CODE (GstRTSPClientSink, gst_rtsp_client_sink, GST_TYPE_BIN,
461 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
462 gst_rtsp_client_sink_uri_handler_init);
463 G_IMPLEMENT_INTERFACE (GST_TYPE_CHILD_PROXY,
464 gst_rtsp_client_sink_child_proxy_init);
465 );
466
467 #ifndef GST_DISABLE_GST_DEBUG
468 static inline const gchar *
cmd_to_string(guint cmd)469 cmd_to_string (guint cmd)
470 {
471 switch (cmd) {
472 case CMD_OPEN:
473 return "OPEN";
474 case CMD_RECORD:
475 return "RECORD";
476 case CMD_PAUSE:
477 return "PAUSE";
478 case CMD_CLOSE:
479 return "CLOSE";
480 case CMD_WAIT:
481 return "WAIT";
482 case CMD_RECONNECT:
483 return "RECONNECT";
484 case CMD_LOOP:
485 return "LOOP";
486 }
487
488 return "unknown";
489 }
490 #endif
491
492 static void
gst_rtsp_client_sink_class_init(GstRTSPClientSinkClass * klass)493 gst_rtsp_client_sink_class_init (GstRTSPClientSinkClass * klass)
494 {
495 GObjectClass *gobject_class;
496 GstElementClass *gstelement_class;
497 GstBinClass *gstbin_class;
498
499 gobject_class = (GObjectClass *) klass;
500 gstelement_class = (GstElementClass *) klass;
501 gstbin_class = (GstBinClass *) klass;
502
503 GST_DEBUG_CATEGORY_INIT (rtsp_client_sink_debug, "rtspclientsink", 0,
504 "RTSP sink element");
505
506 gobject_class->set_property = gst_rtsp_client_sink_set_property;
507 gobject_class->get_property = gst_rtsp_client_sink_get_property;
508
509 gobject_class->finalize = gst_rtsp_client_sink_finalize;
510
511 g_object_class_install_property (gobject_class, PROP_LOCATION,
512 g_param_spec_string ("location", "RTSP Location",
513 "Location of the RTSP url to read",
514 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
515
516 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
517 g_param_spec_flags ("protocols", "Protocols",
518 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
519 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520
521 g_object_class_install_property (gobject_class, PROP_PROFILES,
522 g_param_spec_flags ("profiles", "Profiles",
523 "Allowed RTSP profiles", GST_TYPE_RTSP_PROFILE,
524 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
525
526 g_object_class_install_property (gobject_class, PROP_DEBUG,
527 g_param_spec_boolean ("debug", "Debug",
528 "Dump request and response messages to stdout",
529 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
530
531 g_object_class_install_property (gobject_class, PROP_RETRY,
532 g_param_spec_uint ("retry", "Retry",
533 "Max number of retries when allocating RTP ports.",
534 0, G_MAXUINT16, DEFAULT_RETRY,
535 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
536
537 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
538 g_param_spec_uint64 ("timeout", "Timeout",
539 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
540 0, G_MAXUINT64, DEFAULT_TIMEOUT,
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
542
543 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
544 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
545 "Fail after timeout microseconds on TCP connections (0 = disabled)",
546 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
547 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
548
549 g_object_class_install_property (gobject_class, PROP_LATENCY,
550 g_param_spec_uint ("latency", "Buffer latency in ms",
551 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
552 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
553
554 g_object_class_install_property (gobject_class, PROP_RTX_TIME,
555 g_param_spec_uint ("rtx-time", "Retransmission buffer in ms",
556 "Amount of ms to buffer for retransmission. 0 disables retransmission",
557 0, G_MAXUINT, DEFAULT_RTX_TIME_MS,
558 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
559
560 /**
561 * GstRTSPClientSink:do-rtsp-keep-alive:
562 *
563 * Enable RTSP keep alive support. Some old server don't like RTSP
564 * keep alive and then this property needs to be set to FALSE.
565 */
566 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
567 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
568 "Send RTSP keep alive packets, disable for old incompatible server.",
569 DEFAULT_DO_RTSP_KEEP_ALIVE,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571
572 /**
573 * GstRTSPClientSink:proxy:
574 *
575 * Set the proxy parameters. This has to be a string of the format
576 * [http://][user:passwd@]host[:port].
577 */
578 g_object_class_install_property (gobject_class, PROP_PROXY,
579 g_param_spec_string ("proxy", "Proxy",
580 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
581 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
582 /**
583 * GstRTSPClientSink:proxy-id:
584 *
585 * Sets the proxy URI user id for authentication. If the URI set via the
586 * "proxy" property contains a user-id already, that will take precedence.
587 *
588 */
589 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
590 g_param_spec_string ("proxy-id", "proxy-id",
591 "HTTP proxy URI user id for authentication", "",
592 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
593 /**
594 * GstRTSPClientSink:proxy-pw:
595 *
596 * Sets the proxy URI password for authentication. If the URI set via the
597 * "proxy" property contains a password already, that will take precedence.
598 *
599 */
600 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
601 g_param_spec_string ("proxy-pw", "proxy-pw",
602 "HTTP proxy URI user password for authentication", "",
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
604
605 /**
606 * GstRTSPClientSink:rtp-blocksize:
607 *
608 * RTP package size to suggest to server.
609 */
610 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
611 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
612 "RTP package size to suggest to server (0 = disabled)",
613 0, 65536, DEFAULT_RTP_BLOCKSIZE,
614 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
615
616 g_object_class_install_property (gobject_class,
617 PROP_USER_ID,
618 g_param_spec_string ("user-id", "user-id",
619 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
620 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 g_object_class_install_property (gobject_class, PROP_USER_PW,
622 g_param_spec_string ("user-pw", "user-pw",
623 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
624 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
625
626 /**
627 * GstRTSPClientSink:port-range:
628 *
629 * Configure the client port numbers that can be used to receive
630 * RTCP.
631 */
632 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
633 g_param_spec_string ("port-range", "Port range",
634 "Client port range that can be used to receive RTCP data, "
635 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
636 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
637
638 /**
639 * GstRTSPClientSink:udp-buffer-size:
640 *
641 * Size of the kernel UDP receive buffer in bytes.
642 */
643 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
644 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
645 "Size of the kernel UDP receive buffer in bytes, 0=default",
646 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
647 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
648
649 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
650 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
651 "Reconnect to the server if RTSP connection is closed when doing UDP",
652 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
653
654 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
655 g_param_spec_string ("multicast-iface", "Multicast Interface",
656 "The network interface on which to join the multicast group",
657 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
658
659 g_object_class_install_property (gobject_class, PROP_SDES,
660 g_param_spec_boxed ("sdes", "SDES",
661 "The SDES items of this session",
662 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
663
664 /**
665 * GstRTSPClientSink::tls-validation-flags:
666 *
667 * TLS certificate validation flags used to validate server
668 * certificate.
669 *
670 */
671 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
672 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
673 "TLS certificate validation flags used to validate the server certificate",
674 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
675 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676
677 /**
678 * GstRTSPClientSink::tls-database:
679 *
680 * TLS database with anchor certificate authorities used to validate
681 * the server certificate.
682 *
683 */
684 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
685 g_param_spec_object ("tls-database", "TLS database",
686 "TLS database with anchor certificate authorities used to validate the server certificate",
687 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
688
689 /**
690 * GstRTSPClientSink::tls-interaction:
691 *
692 * A #GTlsInteraction object to be used when the connection or certificate
693 * database need to interact with the user. This will be used to prompt the
694 * user for passwords where necessary.
695 *
696 */
697 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
698 g_param_spec_object ("tls-interaction", "TLS interaction",
699 "A GTlsInteraction object to prompt the user for password or certificate",
700 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
701
702 /**
703 * GstRTSPClientSink::ntp-time-source:
704 *
705 * allows to select the time source that should be used
706 * for the NTP time in outgoing packets
707 *
708 */
709 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
710 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
711 "NTP time source for RTCP packets",
712 GST_TYPE_RTSP_CLIENT_SINK_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
713 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
714
715 /**
716 * GstRTSPClientSink::user-agent:
717 *
718 * The string to set in the User-Agent header.
719 *
720 */
721 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
722 g_param_spec_string ("user-agent", "User Agent",
723 "The User-Agent string to send to the server",
724 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
725
726 /**
727 * GstRTSPClientSink::handle-request:
728 * @rtsp_client_sink: a #GstRTSPClientSink
729 * @request: a #GstRTSPMessage
730 * @response: a #GstRTSPMessage
731 *
732 * Handle a server request in @request and prepare @response.
733 *
734 * This signal is called from the streaming thread, you should therefore not
735 * do any state changes on @rtsp_client_sink because this might deadlock. If you want
736 * to modify the state as a result of this signal, post a
737 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
738 * in some other way.
739 *
740 */
741 gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST] =
742 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
743 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
744 G_TYPE_POINTER, G_TYPE_POINTER);
745
746 /**
747 * GstRTSPClientSink::new-manager:
748 * @rtsp_client_sink: a #GstRTSPClientSink
749 * @manager: a #GstElement
750 *
751 * Emitted after a new manager (like rtpbin) was created and the default
752 * properties were configured.
753 *
754 */
755 gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER] =
756 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
757 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
758 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
759
760 /**
761 * GstRTSPClientSink::new-payloader:
762 * @rtsp_client_sink: a #GstRTSPClientSink
763 * @payloader: a #GstElement
764 *
765 * Emitted after a new RTP payloader was created and the default
766 * properties were configured.
767 *
768 */
769 gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER] =
770 g_signal_new_class_handler ("new-payloader", G_TYPE_FROM_CLASS (klass),
771 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
772 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
773
774 /**
775 * GstRTSPClientSink::request-rtcp-key:
776 * @rtsp_client_sink: a #GstRTSPClientSink
777 * @num: the stream number
778 *
779 * Signal emitted to get the crypto parameters relevant to the RTCP
780 * stream. User should provide the key and the RTCP encryption ciphers
781 * and authentication, and return them wrapped in a GstCaps.
782 *
783 */
784 gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY] =
785 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
786 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
787
788 /**
789 * GstRTSPClientSink::accept-certificate:
790 * @rtsp_client_sink: a #GstRTSPClientSink
791 * @peer_cert: the peer's #GTlsCertificate
792 * @errors: the problems with @peer_cert
793 * @user_data: user data set when the signal handler was connected.
794 *
795 * This will directly map to #GTlsConnection 's "accept-certificate"
796 * signal and be performed after the default checks of #GstRTSPConnection
797 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
798 * have failed. If no #GTlsDatabase is set on this connection, only this
799 * signal will be emitted.
800 *
801 * Since: 1.14
802 */
803 gst_rtsp_client_sink_signals[SIGNAL_ACCEPT_CERTIFICATE] =
804 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
805 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
806 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
807 G_TYPE_TLS_CERTIFICATE_FLAGS);
808
809 gstelement_class->provide_clock = gst_rtsp_client_sink_provide_clock;
810 gstelement_class->change_state = gst_rtsp_client_sink_change_state;
811 gstelement_class->request_new_pad =
812 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_request_new_pad);
813 gstelement_class->release_pad =
814 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_release_pad);
815
816 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
817 &rtptemplate, GST_TYPE_RTSP_CLIENT_SINK_PAD);
818
819 gst_element_class_set_static_metadata (gstelement_class,
820 "RTSP RECORD client", "Sink/Network",
821 "Send data over the network via RTSP RECORD(RFC 2326)",
822 "Jan Schmidt <jan@centricular.com>");
823
824 gstbin_class->handle_message = gst_rtsp_client_sink_handle_message;
825 }
826
827 static void
gst_rtsp_client_sink_init(GstRTSPClientSink * sink)828 gst_rtsp_client_sink_init (GstRTSPClientSink * sink)
829 {
830 sink->conninfo.location = g_strdup (DEFAULT_LOCATION);
831 sink->protocols = DEFAULT_PROTOCOLS;
832 sink->debug = DEFAULT_DEBUG;
833 sink->retry = DEFAULT_RETRY;
834 sink->udp_timeout = DEFAULT_TIMEOUT;
835 gst_rtsp_client_sink_set_tcp_timeout (sink, DEFAULT_TCP_TIMEOUT);
836 sink->latency = DEFAULT_LATENCY_MS;
837 sink->rtx_time = DEFAULT_RTX_TIME_MS;
838 sink->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
839 gst_rtsp_client_sink_set_proxy (sink, DEFAULT_PROXY);
840 sink->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
841 sink->user_id = g_strdup (DEFAULT_USER_ID);
842 sink->user_pw = g_strdup (DEFAULT_USER_PW);
843 sink->client_port_range.min = 0;
844 sink->client_port_range.max = 0;
845 sink->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
846 sink->udp_reconnect = DEFAULT_UDP_RECONNECT;
847 sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
848 sink->sdes = NULL;
849 sink->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
850 sink->tls_database = DEFAULT_TLS_DATABASE;
851 sink->tls_interaction = DEFAULT_TLS_INTERACTION;
852 sink->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
853 sink->user_agent = g_strdup (DEFAULT_USER_AGENT);
854
855 sink->profiles = DEFAULT_PROFILES;
856
857 /* protects the streaming thread in interleaved mode or the polling
858 * thread in UDP mode. */
859 g_rec_mutex_init (&sink->stream_rec_lock);
860
861 /* protects our state changes from multiple invocations */
862 g_rec_mutex_init (&sink->state_rec_lock);
863
864 g_mutex_init (&sink->send_lock);
865
866 g_mutex_init (&sink->preroll_lock);
867 g_cond_init (&sink->preroll_cond);
868
869 sink->state = GST_RTSP_STATE_INVALID;
870
871 g_mutex_init (&sink->conninfo.send_lock);
872 g_mutex_init (&sink->conninfo.recv_lock);
873
874 g_mutex_init (&sink->block_streams_lock);
875 g_cond_init (&sink->block_streams_cond);
876
877 g_mutex_init (&sink->open_conn_lock);
878 g_cond_init (&sink->open_conn_cond);
879
880 sink->internal_bin = (GstBin *) gst_bin_new ("rtspbin");
881 gst_element_set_locked_state (GST_ELEMENT_CAST (sink->internal_bin), TRUE);
882 gst_bin_add (GST_BIN (sink), GST_ELEMENT_CAST (sink->internal_bin));
883
884 sink->next_dyn_pt = 96;
885
886 gst_sdp_message_init (&sink->cursdp);
887
888 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_SINK);
889 }
890
891 static void
gst_rtsp_client_sink_finalize(GObject * object)892 gst_rtsp_client_sink_finalize (GObject * object)
893 {
894 GstRTSPClientSink *rtsp_client_sink;
895
896 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
897
898 gst_sdp_message_uninit (&rtsp_client_sink->cursdp);
899
900 g_free (rtsp_client_sink->conninfo.location);
901 gst_rtsp_url_free (rtsp_client_sink->conninfo.url);
902 g_free (rtsp_client_sink->conninfo.url_str);
903 g_free (rtsp_client_sink->user_id);
904 g_free (rtsp_client_sink->user_pw);
905 g_free (rtsp_client_sink->multi_iface);
906 g_free (rtsp_client_sink->user_agent);
907
908 if (rtsp_client_sink->uri_sdp) {
909 gst_sdp_message_free (rtsp_client_sink->uri_sdp);
910 rtsp_client_sink->uri_sdp = NULL;
911 }
912 if (rtsp_client_sink->provided_clock)
913 gst_object_unref (rtsp_client_sink->provided_clock);
914
915 if (rtsp_client_sink->sdes)
916 gst_structure_free (rtsp_client_sink->sdes);
917
918 if (rtsp_client_sink->tls_database)
919 g_object_unref (rtsp_client_sink->tls_database);
920
921 if (rtsp_client_sink->tls_interaction)
922 g_object_unref (rtsp_client_sink->tls_interaction);
923
924 /* free locks */
925 g_rec_mutex_clear (&rtsp_client_sink->stream_rec_lock);
926 g_rec_mutex_clear (&rtsp_client_sink->state_rec_lock);
927
928 g_mutex_clear (&rtsp_client_sink->conninfo.send_lock);
929 g_mutex_clear (&rtsp_client_sink->conninfo.recv_lock);
930
931 g_mutex_clear (&rtsp_client_sink->send_lock);
932
933 g_mutex_clear (&rtsp_client_sink->preroll_lock);
934 g_cond_clear (&rtsp_client_sink->preroll_cond);
935
936 g_mutex_clear (&rtsp_client_sink->block_streams_lock);
937 g_cond_clear (&rtsp_client_sink->block_streams_cond);
938
939 g_mutex_clear (&rtsp_client_sink->open_conn_lock);
940 g_cond_clear (&rtsp_client_sink->open_conn_cond);
941
942 G_OBJECT_CLASS (parent_class)->finalize (object);
943 }
944
945 static gboolean
gst_rtp_payloader_filter_func(GstPluginFeature * feature,gpointer user_data)946 gst_rtp_payloader_filter_func (GstPluginFeature * feature, gpointer user_data)
947 {
948 GstElementFactory *factory = NULL;
949 const gchar *klass;
950
951 if (!GST_IS_ELEMENT_FACTORY (feature))
952 return FALSE;
953
954 factory = GST_ELEMENT_FACTORY (feature);
955
956 if (gst_plugin_feature_get_rank (feature) == GST_RANK_NONE)
957 return FALSE;
958
959 if (!gst_element_factory_list_is_type (factory,
960 GST_ELEMENT_FACTORY_TYPE_PAYLOADER))
961 return FALSE;
962
963 klass =
964 gst_element_factory_get_metadata (factory, GST_ELEMENT_METADATA_KLASS);
965 if (strstr (klass, "Codec") == NULL)
966 return FALSE;
967 if (strstr (klass, "RTP") == NULL)
968 return FALSE;
969
970 return TRUE;
971 }
972
973 static gint
compare_ranks(GstPluginFeature * f1,GstPluginFeature * f2)974 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
975 {
976 gint diff;
977 const gchar *rname1, *rname2;
978 GstRank rank1, rank2;
979
980 rname1 = gst_plugin_feature_get_name (f1);
981 rname2 = gst_plugin_feature_get_name (f2);
982
983 rank1 = gst_plugin_feature_get_rank (f1);
984 rank2 = gst_plugin_feature_get_rank (f2);
985
986 /* HACK: Prefer rtpmp4apay over rtpmp4gpay */
987 if (g_str_equal (rname1, "rtpmp4apay"))
988 rank1 = GST_RANK_SECONDARY + 1;
989 if (g_str_equal (rname2, "rtpmp4apay"))
990 rank2 = GST_RANK_SECONDARY + 1;
991
992 diff = rank2 - rank1;
993 if (diff != 0)
994 return diff;
995
996 diff = strcmp (rname2, rname1);
997
998 return diff;
999 }
1000
1001 static GList *
gst_rtsp_client_sink_get_factories(void)1002 gst_rtsp_client_sink_get_factories (void)
1003 {
1004 static GList *payloader_factories = NULL;
1005
1006 if (g_once_init_enter (&payloader_factories)) {
1007 GList *all_factories;
1008
1009 all_factories =
1010 gst_registry_feature_filter (gst_registry_get (),
1011 gst_rtp_payloader_filter_func, FALSE, NULL);
1012
1013 all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
1014
1015 g_once_init_leave (&payloader_factories, all_factories);
1016 }
1017
1018 return payloader_factories;
1019 }
1020
1021 static GstCaps *
gst_rtsp_client_sink_get_payloader_caps(GstElementFactory * factory)1022 gst_rtsp_client_sink_get_payloader_caps (GstElementFactory * factory)
1023 {
1024 const GList *tmp;
1025 GstCaps *caps = gst_caps_new_empty ();
1026
1027 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1028 tmp; tmp = g_list_next (tmp)) {
1029 GstStaticPadTemplate *template = tmp->data;
1030
1031 if (template->direction == GST_PAD_SINK) {
1032 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1033
1034 GST_LOG ("Found pad template %s on factory %s",
1035 template->name_template, gst_plugin_feature_get_name (factory));
1036
1037 if (static_caps)
1038 caps = gst_caps_merge (caps, static_caps);
1039
1040 /* Early out, any is absorbing */
1041 if (gst_caps_is_any (caps))
1042 goto out;
1043 }
1044 }
1045
1046 out:
1047 return caps;
1048 }
1049
1050 static GstCaps *
gst_rtsp_client_sink_get_all_payloaders_caps(void)1051 gst_rtsp_client_sink_get_all_payloaders_caps (void)
1052 {
1053 /* Cached caps result */
1054 static GstCaps *ret;
1055
1056 if (g_once_init_enter (&ret)) {
1057 GList *factories, *cur;
1058 GstCaps *caps = gst_caps_new_empty ();
1059
1060 factories = gst_rtsp_client_sink_get_factories ();
1061 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
1062 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
1063 GstCaps *payloader_caps =
1064 gst_rtsp_client_sink_get_payloader_caps (factory);
1065
1066 caps = gst_caps_merge (caps, payloader_caps);
1067
1068 /* Early out, any is absorbing */
1069 if (gst_caps_is_any (caps))
1070 goto out;
1071 }
1072
1073 out:
1074 g_once_init_leave (&ret, caps);
1075 }
1076
1077 /* Return cached result */
1078 return gst_caps_ref (ret);
1079 }
1080
1081 static GstElement *
gst_rtsp_client_sink_make_payloader(GstCaps * caps)1082 gst_rtsp_client_sink_make_payloader (GstCaps * caps)
1083 {
1084 GList *factories, *cur;
1085
1086 factories = gst_rtsp_client_sink_get_factories ();
1087 for (cur = factories; cur != NULL; cur = g_list_next (cur)) {
1088 GstElementFactory *factory = GST_ELEMENT_FACTORY (cur->data);
1089 const GList *tmp;
1090
1091 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1092 tmp; tmp = g_list_next (tmp)) {
1093 GstStaticPadTemplate *template = tmp->data;
1094
1095 if (template->direction == GST_PAD_SINK) {
1096 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1097 GstElement *payloader = NULL;
1098
1099 if (gst_caps_can_intersect (static_caps, caps)) {
1100 GST_DEBUG ("caps %" GST_PTR_FORMAT " intersects with template %"
1101 GST_PTR_FORMAT " for payloader %s", caps, static_caps,
1102 gst_plugin_feature_get_name (factory));
1103 payloader = gst_element_factory_create (factory, NULL);
1104 }
1105
1106 gst_caps_unref (static_caps);
1107
1108 if (payloader)
1109 return payloader;
1110 }
1111 }
1112 }
1113
1114 return NULL;
1115 }
1116
1117 static GstRTSPStream *
gst_rtsp_client_sink_create_stream(GstRTSPClientSink * sink,GstRTSPStreamContext * context,GstElement * payloader,GstPad * pad)1118 gst_rtsp_client_sink_create_stream (GstRTSPClientSink * sink,
1119 GstRTSPStreamContext * context, GstElement * payloader, GstPad * pad)
1120 {
1121 GstRTSPStream *stream = NULL;
1122 guint pt, aux_pt, ulpfec_pt;
1123
1124 GST_OBJECT_LOCK (sink);
1125
1126 g_object_get (G_OBJECT (payloader), "pt", &pt, NULL);
1127 if (pt >= 96 && pt <= sink->next_dyn_pt) {
1128 /* Payloader has a dynamic PT, but one that's already used */
1129 /* FIXME: Create a caps->ptmap instead? */
1130 pt = sink->next_dyn_pt;
1131
1132 if (pt > 127)
1133 goto no_free_pt;
1134
1135 GST_DEBUG_OBJECT (sink, "Assigning pt %u to stream %d", pt, context->index);
1136
1137 sink->next_dyn_pt++;
1138 } else {
1139 GST_DEBUG_OBJECT (sink, "Keeping existing pt %u for stream %d",
1140 pt, context->index);
1141 }
1142
1143 aux_pt = sink->next_dyn_pt;
1144 if (aux_pt > 127)
1145 goto no_free_pt;
1146 sink->next_dyn_pt++;
1147
1148 ulpfec_pt = sink->next_dyn_pt;
1149 if (ulpfec_pt > 127)
1150 goto no_free_pt;
1151 sink->next_dyn_pt++;
1152
1153 GST_OBJECT_UNLOCK (sink);
1154
1155
1156 g_object_set (G_OBJECT (payloader), "pt", pt, NULL);
1157
1158 stream = gst_rtsp_stream_new (context->index, payloader, pad);
1159
1160 gst_rtsp_stream_set_client_side (stream, TRUE);
1161 gst_rtsp_stream_set_retransmission_time (stream,
1162 (GstClockTime) (sink->rtx_time) * GST_MSECOND);
1163 gst_rtsp_stream_set_protocols (stream, sink->protocols);
1164 gst_rtsp_stream_set_profiles (stream, sink->profiles);
1165 gst_rtsp_stream_set_retransmission_pt (stream, aux_pt);
1166 gst_rtsp_stream_set_buffer_size (stream, sink->udp_buffer_size);
1167 if (sink->rtp_blocksize > 0)
1168 gst_rtsp_stream_set_mtu (stream, sink->rtp_blocksize);
1169 gst_rtsp_stream_set_multicast_iface (stream, sink->multi_iface);
1170
1171 gst_rtsp_stream_set_ulpfec_pt (stream, ulpfec_pt);
1172 gst_rtsp_stream_set_ulpfec_percentage (stream, context->ulpfec_percentage);
1173
1174 #if 0
1175 if (priv->pool)
1176 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1177 #endif
1178
1179 return stream;
1180 no_free_pt:
1181 GST_OBJECT_UNLOCK (sink);
1182
1183 GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL),
1184 ("Ran out of dynamic payload types."));
1185
1186 return NULL;
1187 }
1188
1189 static GstPadProbeReturn
handle_payloader_block(GstPad * pad,GstPadProbeInfo * info,GstRTSPStreamContext * context)1190 handle_payloader_block (GstPad * pad, GstPadProbeInfo * info,
1191 GstRTSPStreamContext * context)
1192 {
1193 GstRTSPClientSink *sink = context->parent;
1194
1195 GST_INFO_OBJECT (sink, "Block on pad %" GST_PTR_FORMAT, pad);
1196
1197 g_mutex_lock (&sink->preroll_lock);
1198 context->prerolled = TRUE;
1199 g_cond_broadcast (&sink->preroll_cond);
1200 g_mutex_unlock (&sink->preroll_lock);
1201
1202 GST_INFO_OBJECT (sink, "Announced preroll on pad %" GST_PTR_FORMAT, pad);
1203
1204 return GST_PAD_PROBE_OK;
1205 }
1206
1207 static gboolean
gst_rtsp_client_sink_setup_payloader(GstRTSPClientSink * sink,GstPad * pad,GstCaps * caps)1208 gst_rtsp_client_sink_setup_payloader (GstRTSPClientSink * sink, GstPad * pad,
1209 GstCaps * caps)
1210 {
1211 GstRTSPStreamContext *context;
1212 GstRtspClientSinkPad *cspad = GST_RTSP_CLIENT_SINK_PAD (pad);
1213
1214 GstElement *payloader;
1215 GstPad *sinkpad, *srcpad, *ghostsink;
1216
1217 context = gst_pad_get_element_private (pad);
1218
1219 if (cspad->custom_payloader) {
1220 payloader = cspad->custom_payloader;
1221 } else {
1222 /* Find the payloader. */
1223 payloader = gst_rtsp_client_sink_make_payloader (caps);
1224 }
1225
1226 if (payloader == NULL)
1227 return FALSE;
1228
1229 GST_DEBUG_OBJECT (sink, "Configuring payloader %" GST_PTR_FORMAT
1230 " for pad %" GST_PTR_FORMAT, payloader, pad);
1231
1232 sinkpad = gst_element_get_static_pad (payloader, "sink");
1233 if (sinkpad == NULL)
1234 goto no_sinkpad;
1235
1236 srcpad = gst_element_get_static_pad (payloader, "src");
1237 if (srcpad == NULL)
1238 goto no_srcpad;
1239
1240 gst_bin_add (GST_BIN (sink->internal_bin), payloader);
1241 ghostsink = gst_ghost_pad_new (NULL, sinkpad);
1242 gst_pad_set_active (ghostsink, TRUE);
1243 gst_element_add_pad (GST_ELEMENT (sink->internal_bin), ghostsink);
1244
1245 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_PAYLOADER], 0,
1246 payloader);
1247
1248 GST_RTSP_STATE_LOCK (sink);
1249 context->payloader_block_id =
1250 gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM,
1251 (GstPadProbeCallback) handle_payloader_block, context, NULL);
1252 context->payloader = payloader;
1253
1254 payloader = gst_object_ref (payloader);
1255
1256 gst_ghost_pad_set_target (GST_GHOST_PAD (pad), ghostsink);
1257 gst_object_unref (GST_OBJECT (sinkpad));
1258 GST_RTSP_STATE_UNLOCK (sink);
1259
1260 context->ulpfec_percentage = cspad->ulpfec_percentage;
1261
1262 gst_element_sync_state_with_parent (payloader);
1263
1264 gst_object_unref (payloader);
1265 gst_object_unref (GST_OBJECT (srcpad));
1266
1267 return TRUE;
1268
1269 no_sinkpad:
1270 GST_ERROR_OBJECT (sink,
1271 "Could not find sink pad on payloader %" GST_PTR_FORMAT, payloader);
1272 if (!cspad->custom_payloader)
1273 gst_object_unref (payloader);
1274 return FALSE;
1275
1276 no_srcpad:
1277 GST_ERROR_OBJECT (sink,
1278 "Could not find src pad on payloader %" GST_PTR_FORMAT, payloader);
1279 gst_object_unref (GST_OBJECT (sinkpad));
1280 gst_object_unref (payloader);
1281 return TRUE;
1282 }
1283
1284 static gboolean
gst_rtsp_client_sink_sinkpad_event(GstPad * pad,GstObject * parent,GstEvent * event)1285 gst_rtsp_client_sink_sinkpad_event (GstPad * pad, GstObject * parent,
1286 GstEvent * event)
1287 {
1288 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
1289 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1290 if (target == NULL) {
1291 GstCaps *caps;
1292
1293 /* No target yet - choose a payloader and configure it */
1294 gst_event_parse_caps (event, &caps);
1295
1296 GST_DEBUG_OBJECT (parent,
1297 "Have set caps event on pad %" GST_PTR_FORMAT
1298 " caps %" GST_PTR_FORMAT, pad, caps);
1299
1300 if (!gst_rtsp_client_sink_setup_payloader (GST_RTSP_CLIENT_SINK (parent),
1301 pad, caps)) {
1302 GstRtspClientSinkPad *cspad = GST_RTSP_CLIENT_SINK_PAD (pad);
1303 GST_ELEMENT_ERROR (parent, CORE, NEGOTIATION,
1304 ("Could not create payloader"),
1305 ("Custom payloader: %p, caps: %" GST_PTR_FORMAT,
1306 cspad->custom_payloader, caps));
1307 gst_event_unref (event);
1308 return FALSE;
1309 }
1310 } else {
1311 gst_object_unref (target);
1312 }
1313 }
1314
1315 return gst_pad_event_default (pad, parent, event);
1316 }
1317
1318 static gboolean
gst_rtsp_client_sink_sinkpad_query(GstPad * pad,GstObject * parent,GstQuery * query)1319 gst_rtsp_client_sink_sinkpad_query (GstPad * pad, GstObject * parent,
1320 GstQuery * query)
1321 {
1322 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1323 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
1324 if (target == NULL) {
1325 GstRtspClientSinkPad *cspad = GST_RTSP_CLIENT_SINK_PAD (pad);
1326 GstCaps *caps;
1327
1328 if (cspad->custom_payloader) {
1329 GstPad *sinkpad =
1330 gst_element_get_static_pad (cspad->custom_payloader, "sink");
1331
1332 if (sinkpad) {
1333 caps = gst_pad_query_caps (sinkpad, NULL);
1334 gst_object_unref (sinkpad);
1335 } else {
1336 GST_ELEMENT_ERROR (parent, CORE, NEGOTIATION, (NULL),
1337 ("Custom payloaders are expected to expose a sink pad named 'sink'"));
1338 return FALSE;
1339 }
1340 } else {
1341 /* No target yet - return the union of all payloader caps */
1342 caps = gst_rtsp_client_sink_get_all_payloaders_caps ();
1343 }
1344
1345 GST_TRACE_OBJECT (parent, "Returning payloader caps %" GST_PTR_FORMAT,
1346 caps);
1347
1348 gst_query_set_caps_result (query, caps);
1349 gst_caps_unref (caps);
1350
1351 return TRUE;
1352 }
1353 gst_object_unref (target);
1354 }
1355
1356 return gst_pad_query_default (pad, parent, query);
1357 }
1358
1359 static GstPad *
gst_rtsp_client_sink_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)1360 gst_rtsp_client_sink_request_new_pad (GstElement * element,
1361 GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
1362 {
1363 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1364 GstPad *pad;
1365 GstRTSPStreamContext *context;
1366 guint idx = (guint) - 1;
1367 gchar *tmpname;
1368
1369 g_mutex_lock (&sink->preroll_lock);
1370 if (sink->streams_collected) {
1371 GST_WARNING_OBJECT (element, "Can't add streams to a running session");
1372 g_mutex_unlock (&sink->preroll_lock);
1373 return NULL;
1374 }
1375 g_mutex_unlock (&sink->preroll_lock);
1376
1377 GST_OBJECT_LOCK (sink);
1378 if (name) {
1379 if (!sscanf (name, "sink_%u", &idx)) {
1380 GST_OBJECT_UNLOCK (sink);
1381 GST_ERROR_OBJECT (element, "Invalid sink pad name %s", name);
1382 return NULL;
1383 }
1384
1385 if (idx >= sink->next_pad_id)
1386 sink->next_pad_id = idx + 1;
1387 }
1388 if (idx == (guint) - 1) {
1389 idx = sink->next_pad_id;
1390 sink->next_pad_id++;
1391 }
1392 GST_OBJECT_UNLOCK (sink);
1393
1394 tmpname = g_strdup_printf ("sink_%u", idx);
1395 pad = gst_rtsp_client_sink_pad_new (templ, tmpname);
1396 g_free (tmpname);
1397
1398 GST_DEBUG_OBJECT (element, "Creating request pad %" GST_PTR_FORMAT, pad);
1399
1400 gst_pad_set_event_function (pad,
1401 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_event));
1402 gst_pad_set_query_function (pad,
1403 GST_DEBUG_FUNCPTR (gst_rtsp_client_sink_sinkpad_query));
1404
1405 context = g_new0 (GstRTSPStreamContext, 1);
1406 context->parent = sink;
1407 context->index = idx;
1408
1409 gst_pad_set_element_private (pad, context);
1410
1411 /* The rest of the context is configured on a caps set */
1412 gst_pad_set_active (pad, TRUE);
1413 gst_element_add_pad (element, pad);
1414 gst_child_proxy_child_added (GST_CHILD_PROXY (element), G_OBJECT (pad),
1415 GST_PAD_NAME (pad));
1416
1417 (void) gst_rtsp_client_sink_get_factories ();
1418
1419 g_mutex_init (&context->conninfo.send_lock);
1420 g_mutex_init (&context->conninfo.recv_lock);
1421
1422 GST_RTSP_STATE_LOCK (sink);
1423 sink->contexts = g_list_prepend (sink->contexts, context);
1424 GST_RTSP_STATE_UNLOCK (sink);
1425
1426 return pad;
1427 }
1428
1429 static void
gst_rtsp_client_sink_release_pad(GstElement * element,GstPad * pad)1430 gst_rtsp_client_sink_release_pad (GstElement * element, GstPad * pad)
1431 {
1432 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1433 GstRTSPStreamContext *context;
1434
1435 context = gst_pad_get_element_private (pad);
1436
1437 /* FIXME: we may need to change our blocking state waiting for
1438 * GstRTSPStreamBlocking messages */
1439
1440 GST_RTSP_STATE_LOCK (sink);
1441 sink->contexts = g_list_remove (sink->contexts, context);
1442 GST_RTSP_STATE_UNLOCK (sink);
1443
1444 /* FIXME: Shut down and clean up streaming on this pad,
1445 * do teardown if needed */
1446 GST_LOG_OBJECT (sink,
1447 "Cleaning up payloader and stream for released pad %" GST_PTR_FORMAT,
1448 pad);
1449
1450 if (context->stream_transport) {
1451 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1452 gst_object_unref (context->stream_transport);
1453 context->stream_transport = NULL;
1454 }
1455 if (context->stream) {
1456 if (context->joined) {
1457 gst_rtsp_stream_leave_bin (context->stream,
1458 GST_BIN (sink->internal_bin), sink->rtpbin);
1459 context->joined = FALSE;
1460 }
1461 gst_object_unref (context->stream);
1462 context->stream = NULL;
1463 }
1464 if (context->srtcpparams)
1465 gst_caps_unref (context->srtcpparams);
1466
1467 g_free (context->conninfo.location);
1468 context->conninfo.location = NULL;
1469
1470 g_mutex_clear (&context->conninfo.send_lock);
1471 g_mutex_clear (&context->conninfo.recv_lock);
1472
1473 g_free (context);
1474
1475 gst_element_remove_pad (element, pad);
1476 }
1477
1478 static GstClock *
gst_rtsp_client_sink_provide_clock(GstElement * element)1479 gst_rtsp_client_sink_provide_clock (GstElement * element)
1480 {
1481 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (element);
1482 GstClock *clock;
1483
1484 if ((clock = sink->provided_clock) != NULL)
1485 gst_object_ref (clock);
1486
1487 return clock;
1488 }
1489
1490 /* a proxy string of the format [user:passwd@]host[:port] */
1491 static gboolean
gst_rtsp_client_sink_set_proxy(GstRTSPClientSink * rtsp,const gchar * proxy)1492 gst_rtsp_client_sink_set_proxy (GstRTSPClientSink * rtsp, const gchar * proxy)
1493 {
1494 gchar *p, *at, *col;
1495
1496 g_free (rtsp->proxy_user);
1497 rtsp->proxy_user = NULL;
1498 g_free (rtsp->proxy_passwd);
1499 rtsp->proxy_passwd = NULL;
1500 g_free (rtsp->proxy_host);
1501 rtsp->proxy_host = NULL;
1502 rtsp->proxy_port = 0;
1503
1504 p = (gchar *) proxy;
1505
1506 if (p == NULL)
1507 return TRUE;
1508
1509 /* we allow http:// in front but ignore it */
1510 if (g_str_has_prefix (p, "http://"))
1511 p += 7;
1512
1513 at = strchr (p, '@');
1514 if (at) {
1515 /* look for user:passwd */
1516 col = strchr (proxy, ':');
1517 if (col == NULL || col > at)
1518 return FALSE;
1519
1520 rtsp->proxy_user = g_strndup (p, col - p);
1521 col++;
1522 rtsp->proxy_passwd = g_strndup (col, at - col);
1523
1524 /* move to host */
1525 p = at + 1;
1526 } else {
1527 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1528 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1529 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1530 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1531 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1532 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1533 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1534 }
1535 }
1536 col = strchr (p, ':');
1537
1538 if (col) {
1539 /* everything before the colon is the hostname */
1540 rtsp->proxy_host = g_strndup (p, col - p);
1541 p = col + 1;
1542 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1543 } else {
1544 rtsp->proxy_host = g_strdup (p);
1545 rtsp->proxy_port = 8080;
1546 }
1547 return TRUE;
1548 }
1549
1550 static void
gst_rtsp_client_sink_set_tcp_timeout(GstRTSPClientSink * rtsp_client_sink,guint64 timeout)1551 gst_rtsp_client_sink_set_tcp_timeout (GstRTSPClientSink * rtsp_client_sink,
1552 guint64 timeout)
1553 {
1554 rtsp_client_sink->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1555 rtsp_client_sink->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1556
1557 if (timeout != 0)
1558 rtsp_client_sink->ptcp_timeout = &rtsp_client_sink->tcp_timeout;
1559 else
1560 rtsp_client_sink->ptcp_timeout = NULL;
1561 }
1562
1563 static void
gst_rtsp_client_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1564 gst_rtsp_client_sink_set_property (GObject * object, guint prop_id,
1565 const GValue * value, GParamSpec * pspec)
1566 {
1567 GstRTSPClientSink *rtsp_client_sink;
1568
1569 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1570
1571 switch (prop_id) {
1572 case PROP_LOCATION:
1573 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (rtsp_client_sink),
1574 g_value_get_string (value), NULL);
1575 break;
1576 case PROP_PROTOCOLS:
1577 rtsp_client_sink->protocols = g_value_get_flags (value);
1578 break;
1579 case PROP_PROFILES:
1580 rtsp_client_sink->profiles = g_value_get_flags (value);
1581 break;
1582 case PROP_DEBUG:
1583 rtsp_client_sink->debug = g_value_get_boolean (value);
1584 break;
1585 case PROP_RETRY:
1586 rtsp_client_sink->retry = g_value_get_uint (value);
1587 break;
1588 case PROP_TIMEOUT:
1589 rtsp_client_sink->udp_timeout = g_value_get_uint64 (value);
1590 break;
1591 case PROP_TCP_TIMEOUT:
1592 gst_rtsp_client_sink_set_tcp_timeout (rtsp_client_sink,
1593 g_value_get_uint64 (value));
1594 break;
1595 case PROP_LATENCY:
1596 rtsp_client_sink->latency = g_value_get_uint (value);
1597 break;
1598 case PROP_RTX_TIME:
1599 rtsp_client_sink->rtx_time = g_value_get_uint (value);
1600 break;
1601 case PROP_DO_RTSP_KEEP_ALIVE:
1602 rtsp_client_sink->do_rtsp_keep_alive = g_value_get_boolean (value);
1603 break;
1604 case PROP_PROXY:
1605 gst_rtsp_client_sink_set_proxy (rtsp_client_sink,
1606 g_value_get_string (value));
1607 break;
1608 case PROP_PROXY_ID:
1609 if (rtsp_client_sink->prop_proxy_id)
1610 g_free (rtsp_client_sink->prop_proxy_id);
1611 rtsp_client_sink->prop_proxy_id = g_value_dup_string (value);
1612 break;
1613 case PROP_PROXY_PW:
1614 if (rtsp_client_sink->prop_proxy_pw)
1615 g_free (rtsp_client_sink->prop_proxy_pw);
1616 rtsp_client_sink->prop_proxy_pw = g_value_dup_string (value);
1617 break;
1618 case PROP_RTP_BLOCKSIZE:
1619 rtsp_client_sink->rtp_blocksize = g_value_get_uint (value);
1620 break;
1621 case PROP_USER_ID:
1622 if (rtsp_client_sink->user_id)
1623 g_free (rtsp_client_sink->user_id);
1624 rtsp_client_sink->user_id = g_value_dup_string (value);
1625 break;
1626 case PROP_USER_PW:
1627 if (rtsp_client_sink->user_pw)
1628 g_free (rtsp_client_sink->user_pw);
1629 rtsp_client_sink->user_pw = g_value_dup_string (value);
1630 break;
1631 case PROP_PORT_RANGE:
1632 {
1633 const gchar *str;
1634
1635 str = g_value_get_string (value);
1636 if (!str || !sscanf (str, "%u-%u",
1637 &rtsp_client_sink->client_port_range.min,
1638 &rtsp_client_sink->client_port_range.max)) {
1639 rtsp_client_sink->client_port_range.min = 0;
1640 rtsp_client_sink->client_port_range.max = 0;
1641 }
1642 break;
1643 }
1644 case PROP_UDP_BUFFER_SIZE:
1645 rtsp_client_sink->udp_buffer_size = g_value_get_int (value);
1646 break;
1647 case PROP_UDP_RECONNECT:
1648 rtsp_client_sink->udp_reconnect = g_value_get_boolean (value);
1649 break;
1650 case PROP_MULTICAST_IFACE:
1651 g_free (rtsp_client_sink->multi_iface);
1652
1653 if (g_value_get_string (value) == NULL)
1654 rtsp_client_sink->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1655 else
1656 rtsp_client_sink->multi_iface = g_value_dup_string (value);
1657 break;
1658 case PROP_SDES:
1659 rtsp_client_sink->sdes = g_value_dup_boxed (value);
1660 break;
1661 case PROP_TLS_VALIDATION_FLAGS:
1662 rtsp_client_sink->tls_validation_flags = g_value_get_flags (value);
1663 break;
1664 case PROP_TLS_DATABASE:
1665 g_clear_object (&rtsp_client_sink->tls_database);
1666 rtsp_client_sink->tls_database = g_value_dup_object (value);
1667 break;
1668 case PROP_TLS_INTERACTION:
1669 g_clear_object (&rtsp_client_sink->tls_interaction);
1670 rtsp_client_sink->tls_interaction = g_value_dup_object (value);
1671 break;
1672 case PROP_NTP_TIME_SOURCE:
1673 rtsp_client_sink->ntp_time_source = g_value_get_enum (value);
1674 break;
1675 case PROP_USER_AGENT:
1676 g_free (rtsp_client_sink->user_agent);
1677 rtsp_client_sink->user_agent = g_value_dup_string (value);
1678 break;
1679 default:
1680 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1681 break;
1682 }
1683 }
1684
1685 static void
gst_rtsp_client_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1686 gst_rtsp_client_sink_get_property (GObject * object, guint prop_id,
1687 GValue * value, GParamSpec * pspec)
1688 {
1689 GstRTSPClientSink *rtsp_client_sink;
1690
1691 rtsp_client_sink = GST_RTSP_CLIENT_SINK (object);
1692
1693 switch (prop_id) {
1694 case PROP_LOCATION:
1695 g_value_set_string (value, rtsp_client_sink->conninfo.location);
1696 break;
1697 case PROP_PROTOCOLS:
1698 g_value_set_flags (value, rtsp_client_sink->protocols);
1699 break;
1700 case PROP_PROFILES:
1701 g_value_set_flags (value, rtsp_client_sink->profiles);
1702 break;
1703 case PROP_DEBUG:
1704 g_value_set_boolean (value, rtsp_client_sink->debug);
1705 break;
1706 case PROP_RETRY:
1707 g_value_set_uint (value, rtsp_client_sink->retry);
1708 break;
1709 case PROP_TIMEOUT:
1710 g_value_set_uint64 (value, rtsp_client_sink->udp_timeout);
1711 break;
1712 case PROP_TCP_TIMEOUT:
1713 {
1714 guint64 timeout;
1715
1716 timeout = rtsp_client_sink->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1717 rtsp_client_sink->tcp_timeout.tv_usec;
1718 g_value_set_uint64 (value, timeout);
1719 break;
1720 }
1721 case PROP_LATENCY:
1722 g_value_set_uint (value, rtsp_client_sink->latency);
1723 break;
1724 case PROP_RTX_TIME:
1725 g_value_set_uint (value, rtsp_client_sink->rtx_time);
1726 break;
1727 case PROP_DO_RTSP_KEEP_ALIVE:
1728 g_value_set_boolean (value, rtsp_client_sink->do_rtsp_keep_alive);
1729 break;
1730 case PROP_PROXY:
1731 {
1732 gchar *str;
1733
1734 if (rtsp_client_sink->proxy_host) {
1735 str =
1736 g_strdup_printf ("%s:%d", rtsp_client_sink->proxy_host,
1737 rtsp_client_sink->proxy_port);
1738 } else {
1739 str = NULL;
1740 }
1741 g_value_take_string (value, str);
1742 break;
1743 }
1744 case PROP_PROXY_ID:
1745 g_value_set_string (value, rtsp_client_sink->prop_proxy_id);
1746 break;
1747 case PROP_PROXY_PW:
1748 g_value_set_string (value, rtsp_client_sink->prop_proxy_pw);
1749 break;
1750 case PROP_RTP_BLOCKSIZE:
1751 g_value_set_uint (value, rtsp_client_sink->rtp_blocksize);
1752 break;
1753 case PROP_USER_ID:
1754 g_value_set_string (value, rtsp_client_sink->user_id);
1755 break;
1756 case PROP_USER_PW:
1757 g_value_set_string (value, rtsp_client_sink->user_pw);
1758 break;
1759 case PROP_PORT_RANGE:
1760 {
1761 gchar *str;
1762
1763 if (rtsp_client_sink->client_port_range.min != 0) {
1764 str = g_strdup_printf ("%u-%u", rtsp_client_sink->client_port_range.min,
1765 rtsp_client_sink->client_port_range.max);
1766 } else {
1767 str = NULL;
1768 }
1769 g_value_take_string (value, str);
1770 break;
1771 }
1772 case PROP_UDP_BUFFER_SIZE:
1773 g_value_set_int (value, rtsp_client_sink->udp_buffer_size);
1774 break;
1775 case PROP_UDP_RECONNECT:
1776 g_value_set_boolean (value, rtsp_client_sink->udp_reconnect);
1777 break;
1778 case PROP_MULTICAST_IFACE:
1779 g_value_set_string (value, rtsp_client_sink->multi_iface);
1780 break;
1781 case PROP_SDES:
1782 g_value_set_boxed (value, rtsp_client_sink->sdes);
1783 break;
1784 case PROP_TLS_VALIDATION_FLAGS:
1785 g_value_set_flags (value, rtsp_client_sink->tls_validation_flags);
1786 break;
1787 case PROP_TLS_DATABASE:
1788 g_value_set_object (value, rtsp_client_sink->tls_database);
1789 break;
1790 case PROP_TLS_INTERACTION:
1791 g_value_set_object (value, rtsp_client_sink->tls_interaction);
1792 break;
1793 case PROP_NTP_TIME_SOURCE:
1794 g_value_set_enum (value, rtsp_client_sink->ntp_time_source);
1795 break;
1796 case PROP_USER_AGENT:
1797 g_value_set_string (value, rtsp_client_sink->user_agent);
1798 break;
1799 default:
1800 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1801 break;
1802 }
1803 }
1804
1805 static const gchar *
get_aggregate_control(GstRTSPClientSink * sink)1806 get_aggregate_control (GstRTSPClientSink * sink)
1807 {
1808 const gchar *base;
1809
1810 if (sink->control)
1811 base = sink->control;
1812 else if (sink->content_base)
1813 base = sink->content_base;
1814 else if (sink->conninfo.url_str)
1815 base = sink->conninfo.url_str;
1816 else
1817 base = "/";
1818
1819 return base;
1820 }
1821
1822 static void
gst_rtsp_client_sink_cleanup(GstRTSPClientSink * sink)1823 gst_rtsp_client_sink_cleanup (GstRTSPClientSink * sink)
1824 {
1825 GList *walk;
1826
1827 GST_DEBUG_OBJECT (sink, "cleanup");
1828
1829 gst_element_set_state (GST_ELEMENT (sink->internal_bin), GST_STATE_NULL);
1830
1831 /* Clean up any left over stream objects */
1832 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
1833 GstRTSPStreamContext *context = (GstRTSPStreamContext *) (walk->data);
1834 if (context->stream_transport) {
1835 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
1836 gst_object_unref (context->stream_transport);
1837 context->stream_transport = NULL;
1838 }
1839
1840 if (context->stream) {
1841 if (context->joined) {
1842 gst_rtsp_stream_leave_bin (context->stream,
1843 GST_BIN (sink->internal_bin), sink->rtpbin);
1844 context->joined = FALSE;
1845 }
1846 gst_object_unref (context->stream);
1847 context->stream = NULL;
1848 }
1849
1850 if (context->srtcpparams) {
1851 gst_caps_unref (context->srtcpparams);
1852 context->srtcpparams = NULL;
1853 }
1854 g_free (context->conninfo.location);
1855 context->conninfo.location = NULL;
1856 }
1857
1858 if (sink->rtpbin) {
1859 gst_element_set_state (sink->rtpbin, GST_STATE_NULL);
1860 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), sink->rtpbin);
1861 sink->rtpbin = NULL;
1862 }
1863
1864 g_free (sink->content_base);
1865 sink->content_base = NULL;
1866
1867 g_free (sink->control);
1868 sink->control = NULL;
1869
1870 if (sink->range)
1871 gst_rtsp_range_free (sink->range);
1872 sink->range = NULL;
1873
1874 /* don't clear the SDP when it was used in the url */
1875 if (sink->uri_sdp && !sink->from_sdp) {
1876 gst_sdp_message_free (sink->uri_sdp);
1877 sink->uri_sdp = NULL;
1878 }
1879
1880 if (sink->provided_clock) {
1881 gst_object_unref (sink->provided_clock);
1882 sink->provided_clock = NULL;
1883 }
1884
1885 g_free (sink->server_ip);
1886 sink->server_ip = NULL;
1887
1888 sink->next_pad_id = 0;
1889 sink->next_dyn_pt = 96;
1890 }
1891
1892 static GstRTSPResult
gst_rtsp_client_sink_connection_send(GstRTSPClientSink * sink,GstRTSPConnInfo * conninfo,GstRTSPMessage * message,GTimeVal * timeout)1893 gst_rtsp_client_sink_connection_send (GstRTSPClientSink * sink,
1894 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1895 {
1896 GstRTSPResult ret;
1897
1898 if (conninfo->connection) {
1899 g_mutex_lock (&conninfo->send_lock);
1900 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
1901 g_mutex_unlock (&conninfo->send_lock);
1902 } else {
1903 ret = GST_RTSP_ERROR;
1904 }
1905
1906 return ret;
1907 }
1908
1909 static GstRTSPResult
gst_rtsp_client_sink_connection_receive(GstRTSPClientSink * sink,GstRTSPConnInfo * conninfo,GstRTSPMessage * message,GTimeVal * timeout)1910 gst_rtsp_client_sink_connection_receive (GstRTSPClientSink * sink,
1911 GstRTSPConnInfo * conninfo, GstRTSPMessage * message, GTimeVal * timeout)
1912 {
1913 GstRTSPResult ret;
1914
1915 if (conninfo->connection) {
1916 g_mutex_lock (&conninfo->recv_lock);
1917 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
1918 g_mutex_unlock (&conninfo->recv_lock);
1919 } else {
1920 ret = GST_RTSP_ERROR;
1921 }
1922
1923 return ret;
1924 }
1925
1926 static gboolean
accept_certificate_cb(GTlsConnection * conn,GTlsCertificate * peer_cert,GTlsCertificateFlags errors,gpointer user_data)1927 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
1928 GTlsCertificateFlags errors, gpointer user_data)
1929 {
1930 GstRTSPClientSink *sink = user_data;
1931 gboolean accept = FALSE;
1932
1933 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_ACCEPT_CERTIFICATE],
1934 0, conn, peer_cert, errors, &accept);
1935
1936 return accept;
1937 }
1938
1939 static GstRTSPResult
gst_rtsp_conninfo_connect(GstRTSPClientSink * sink,GstRTSPConnInfo * info,gboolean async)1940 gst_rtsp_conninfo_connect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
1941 gboolean async)
1942 {
1943 GstRTSPResult res;
1944
1945 if (info->connection == NULL) {
1946 if (info->url == NULL) {
1947 GST_DEBUG_OBJECT (sink, "parsing uri (%s)...", info->location);
1948 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
1949 goto parse_error;
1950 }
1951
1952 /* create connection */
1953 GST_DEBUG_OBJECT (sink, "creating connection (%s)...", info->location);
1954 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
1955 goto could_not_create;
1956
1957 if (info->url_str)
1958 g_free (info->url_str);
1959 info->url_str = gst_rtsp_url_get_request_uri (info->url);
1960
1961 GST_DEBUG_OBJECT (sink, "sanitized uri %s", info->url_str);
1962
1963 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
1964 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
1965 sink->tls_validation_flags))
1966 GST_WARNING_OBJECT (sink, "Unable to set TLS validation flags");
1967
1968 if (sink->tls_database)
1969 gst_rtsp_connection_set_tls_database (info->connection,
1970 sink->tls_database);
1971
1972 if (sink->tls_interaction)
1973 gst_rtsp_connection_set_tls_interaction (info->connection,
1974 sink->tls_interaction);
1975
1976 gst_rtsp_connection_set_accept_certificate_func (info->connection,
1977 accept_certificate_cb, sink, NULL);
1978 }
1979
1980 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
1981 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
1982
1983 if (sink->proxy_host) {
1984 GST_DEBUG_OBJECT (sink, "setting proxy %s:%d", sink->proxy_host,
1985 sink->proxy_port);
1986 gst_rtsp_connection_set_proxy (info->connection, sink->proxy_host,
1987 sink->proxy_port);
1988 }
1989 }
1990
1991 if (!info->connected) {
1992 /* connect */
1993 if (async)
1994 GST_ELEMENT_PROGRESS (sink, CONTINUE, "connect",
1995 ("Connecting to %s", info->location));
1996 GST_DEBUG_OBJECT (sink, "connecting (%s)...", info->location);
1997 if ((res =
1998 gst_rtsp_connection_connect (info->connection,
1999 sink->ptcp_timeout)) < 0)
2000 goto could_not_connect;
2001
2002 info->connected = TRUE;
2003 }
2004 return GST_RTSP_OK;
2005
2006 /* ERRORS */
2007 parse_error:
2008 {
2009 GST_ERROR_OBJECT (sink, "No valid RTSP URL was provided");
2010 return res;
2011 }
2012 could_not_create:
2013 {
2014 gchar *str = gst_rtsp_strresult (res);
2015 GST_ERROR_OBJECT (sink, "Could not create connection. (%s)", str);
2016 g_free (str);
2017 return res;
2018 }
2019 could_not_connect:
2020 {
2021 gchar *str = gst_rtsp_strresult (res);
2022 GST_ERROR_OBJECT (sink, "Could not connect to server. (%s)", str);
2023 g_free (str);
2024 return res;
2025 }
2026 }
2027
2028 static GstRTSPResult
gst_rtsp_conninfo_close(GstRTSPClientSink * sink,GstRTSPConnInfo * info,gboolean free)2029 gst_rtsp_conninfo_close (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
2030 gboolean free)
2031 {
2032 GST_RTSP_STATE_LOCK (sink);
2033 if (info->connected) {
2034 GST_DEBUG_OBJECT (sink, "closing connection...");
2035 gst_rtsp_connection_close (info->connection);
2036 info->connected = FALSE;
2037 }
2038 if (free && info->connection) {
2039 /* free connection */
2040 GST_DEBUG_OBJECT (sink, "freeing connection...");
2041 gst_rtsp_connection_free (info->connection);
2042 g_mutex_lock (&sink->preroll_lock);
2043 info->connection = NULL;
2044 g_cond_broadcast (&sink->preroll_cond);
2045 g_mutex_unlock (&sink->preroll_lock);
2046 }
2047 GST_RTSP_STATE_UNLOCK (sink);
2048 return GST_RTSP_OK;
2049 }
2050
2051 static GstRTSPResult
gst_rtsp_conninfo_reconnect(GstRTSPClientSink * sink,GstRTSPConnInfo * info,gboolean async)2052 gst_rtsp_conninfo_reconnect (GstRTSPClientSink * sink, GstRTSPConnInfo * info,
2053 gboolean async)
2054 {
2055 GstRTSPResult res;
2056
2057 GST_DEBUG_OBJECT (sink, "reconnecting connection...");
2058 gst_rtsp_conninfo_close (sink, info, FALSE);
2059 res = gst_rtsp_conninfo_connect (sink, info, async);
2060
2061 return res;
2062 }
2063
2064 static void
gst_rtsp_client_sink_connection_flush(GstRTSPClientSink * sink,gboolean flush)2065 gst_rtsp_client_sink_connection_flush (GstRTSPClientSink * sink, gboolean flush)
2066 {
2067 GList *walk;
2068
2069 GST_DEBUG_OBJECT (sink, "set flushing %d", flush);
2070 g_mutex_lock (&sink->preroll_lock);
2071 if (sink->conninfo.connection && sink->conninfo.flushing != flush) {
2072 GST_DEBUG_OBJECT (sink, "connection flush");
2073 gst_rtsp_connection_flush (sink->conninfo.connection, flush);
2074 sink->conninfo.flushing = flush;
2075 }
2076 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
2077 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
2078 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
2079 GST_DEBUG_OBJECT (sink, "stream %p flush", stream);
2080 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
2081 stream->conninfo.flushing = flush;
2082 }
2083 }
2084 g_cond_broadcast (&sink->preroll_cond);
2085 g_mutex_unlock (&sink->preroll_lock);
2086 }
2087
2088 static GstRTSPResult
gst_rtsp_client_sink_init_request(GstRTSPClientSink * sink,GstRTSPMessage * msg,GstRTSPMethod method,const gchar * uri)2089 gst_rtsp_client_sink_init_request (GstRTSPClientSink * sink,
2090 GstRTSPMessage * msg, GstRTSPMethod method, const gchar * uri)
2091 {
2092 GstRTSPResult res;
2093
2094 res = gst_rtsp_message_init_request (msg, method, uri);
2095 if (res < 0)
2096 return res;
2097
2098 /* set user-agent */
2099 if (sink->user_agent)
2100 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT,
2101 sink->user_agent);
2102
2103 return res;
2104 }
2105
2106 /* FIXME, handle server request, reply with OK, for now */
2107 static GstRTSPResult
gst_rtsp_client_sink_handle_request(GstRTSPClientSink * sink,GstRTSPConnInfo * conninfo,GstRTSPMessage * request)2108 gst_rtsp_client_sink_handle_request (GstRTSPClientSink * sink,
2109 GstRTSPConnInfo * conninfo, GstRTSPMessage * request)
2110 {
2111 GstRTSPMessage response = { 0 };
2112 GstRTSPResult res;
2113
2114 GST_DEBUG_OBJECT (sink, "got server request message");
2115
2116 if (sink->debug)
2117 gst_rtsp_message_dump (request);
2118
2119 /* default implementation, send OK */
2120 GST_DEBUG_OBJECT (sink, "prepare OK reply");
2121 res =
2122 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
2123 request);
2124 if (res < 0)
2125 goto send_error;
2126
2127 /* let app parse and reply */
2128 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_HANDLE_REQUEST],
2129 0, request, &response);
2130
2131 if (sink->debug)
2132 gst_rtsp_message_dump (&response);
2133
2134 res = gst_rtsp_client_sink_connection_send (sink, conninfo, &response, NULL);
2135 if (res < 0)
2136 goto send_error;
2137
2138 gst_rtsp_message_unset (&response);
2139
2140 return GST_RTSP_OK;
2141
2142 /* ERRORS */
2143 send_error:
2144 {
2145 gst_rtsp_message_unset (&response);
2146 return res;
2147 }
2148 }
2149
2150 /* send server keep-alive */
2151 static GstRTSPResult
gst_rtsp_client_sink_send_keep_alive(GstRTSPClientSink * sink)2152 gst_rtsp_client_sink_send_keep_alive (GstRTSPClientSink * sink)
2153 {
2154 GstRTSPMessage request = { 0 };
2155 GstRTSPResult res;
2156 GstRTSPMethod method;
2157 const gchar *control;
2158
2159 if (sink->do_rtsp_keep_alive == FALSE) {
2160 GST_DEBUG_OBJECT (sink, "do-rtsp-keep-alive is FALSE, not sending.");
2161 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
2162 return GST_RTSP_OK;
2163 }
2164
2165 GST_DEBUG_OBJECT (sink, "creating server keep-alive");
2166
2167 /* find a method to use for keep-alive */
2168 if (sink->methods & GST_RTSP_GET_PARAMETER)
2169 method = GST_RTSP_GET_PARAMETER;
2170 else
2171 method = GST_RTSP_OPTIONS;
2172
2173 control = get_aggregate_control (sink);
2174 if (control == NULL)
2175 goto no_control;
2176
2177 res = gst_rtsp_client_sink_init_request (sink, &request, method, control);
2178 if (res < 0)
2179 goto send_error;
2180
2181 if (sink->debug)
2182 gst_rtsp_message_dump (&request);
2183
2184 res =
2185 gst_rtsp_client_sink_connection_send (sink, &sink->conninfo,
2186 &request, NULL);
2187 if (res < 0)
2188 goto send_error;
2189
2190 gst_rtsp_connection_reset_timeout (sink->conninfo.connection);
2191 gst_rtsp_message_unset (&request);
2192
2193 return GST_RTSP_OK;
2194
2195 /* ERRORS */
2196 no_control:
2197 {
2198 GST_WARNING_OBJECT (sink, "no control url to send keepalive");
2199 return GST_RTSP_OK;
2200 }
2201 send_error:
2202 {
2203 gchar *str = gst_rtsp_strresult (res);
2204
2205 gst_rtsp_message_unset (&request);
2206 GST_ELEMENT_WARNING (sink, RESOURCE, WRITE, (NULL),
2207 ("Could not send keep-alive. (%s)", str));
2208 g_free (str);
2209 return res;
2210 }
2211 }
2212
2213 static GstFlowReturn
gst_rtsp_client_sink_loop_rx(GstRTSPClientSink * sink)2214 gst_rtsp_client_sink_loop_rx (GstRTSPClientSink * sink)
2215 {
2216 GstRTSPResult res;
2217 GstRTSPMessage message = { 0 };
2218 gint retry = 0;
2219
2220 while (TRUE) {
2221 GTimeVal tv_timeout;
2222
2223 /* get the next timeout interval */
2224 gst_rtsp_connection_next_timeout (sink->conninfo.connection, &tv_timeout);
2225
2226 GST_DEBUG_OBJECT (sink, "doing receive with timeout %d seconds",
2227 (gint) tv_timeout.tv_sec);
2228
2229 gst_rtsp_message_unset (&message);
2230
2231 /* we should continue reading the TCP socket because the server might
2232 * send us requests. When the session timeout expires, we need to send a
2233 * keep-alive request to keep the session open. */
2234 res =
2235 gst_rtsp_client_sink_connection_receive (sink,
2236 &sink->conninfo, &message, &tv_timeout);
2237
2238 switch (res) {
2239 case GST_RTSP_OK:
2240 GST_DEBUG_OBJECT (sink, "we received a server message");
2241 break;
2242 case GST_RTSP_EINTR:
2243 /* we got interrupted, see what we have to do */
2244 goto interrupt;
2245 case GST_RTSP_ETIMEOUT:
2246 /* send keep-alive, ignore the result, a warning will be posted. */
2247 GST_DEBUG_OBJECT (sink, "timeout, sending keep-alive");
2248 if ((res =
2249 gst_rtsp_client_sink_send_keep_alive (sink)) == GST_RTSP_EINTR)
2250 goto interrupt;
2251 continue;
2252 case GST_RTSP_EEOF:
2253 /* server closed the connection. not very fatal for UDP, reconnect and
2254 * see what happens. */
2255 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2256 ("The server closed the connection."));
2257 if (sink->udp_reconnect) {
2258 if ((res =
2259 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2260 FALSE)) < 0)
2261 goto connect_error;
2262 } else {
2263 goto server_eof;
2264 }
2265 continue;
2266 break;
2267 case GST_RTSP_ENET:
2268 GST_DEBUG_OBJECT (sink, "An ethernet problem occured.");
2269 default:
2270 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2271 ("Unhandled return value %d.", res));
2272 goto receive_error;
2273 }
2274
2275 switch (message.type) {
2276 case GST_RTSP_MESSAGE_REQUEST:
2277 /* server sends us a request message, handle it */
2278 res =
2279 gst_rtsp_client_sink_handle_request (sink,
2280 &sink->conninfo, &message);
2281 if (res == GST_RTSP_EEOF)
2282 goto server_eof;
2283 else if (res < 0)
2284 goto handle_request_failed;
2285 break;
2286 case GST_RTSP_MESSAGE_RESPONSE:
2287 /* we ignore response and data messages */
2288 GST_DEBUG_OBJECT (sink, "ignoring response message");
2289 if (sink->debug)
2290 gst_rtsp_message_dump (&message);
2291 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
2292 GST_DEBUG_OBJECT (sink, "but is Unauthorized response ...");
2293 if (gst_rtsp_client_sink_setup_auth (sink, &message) && !(retry++)) {
2294 GST_DEBUG_OBJECT (sink, "so retrying keep-alive");
2295 if ((res =
2296 gst_rtsp_client_sink_send_keep_alive (sink)) ==
2297 GST_RTSP_EINTR)
2298 goto interrupt;
2299 }
2300 } else {
2301 retry = 0;
2302 }
2303 break;
2304 case GST_RTSP_MESSAGE_DATA:
2305 /* we ignore response and data messages */
2306 GST_DEBUG_OBJECT (sink, "ignoring data message");
2307 break;
2308 default:
2309 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2310 message.type);
2311 break;
2312 }
2313 }
2314 g_assert_not_reached ();
2315
2316 /* we get here when the connection got interrupted */
2317 interrupt:
2318 {
2319 gst_rtsp_message_unset (&message);
2320 GST_DEBUG_OBJECT (sink, "got interrupted");
2321 return GST_FLOW_FLUSHING;
2322 }
2323 connect_error:
2324 {
2325 gchar *str = gst_rtsp_strresult (res);
2326 GstFlowReturn ret;
2327
2328 sink->conninfo.connected = FALSE;
2329 if (res != GST_RTSP_EINTR) {
2330 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
2331 ("Could not connect to server. (%s)", str));
2332 g_free (str);
2333 ret = GST_FLOW_ERROR;
2334 } else {
2335 ret = GST_FLOW_FLUSHING;
2336 }
2337 return ret;
2338 }
2339 receive_error:
2340 {
2341 gchar *str = gst_rtsp_strresult (res);
2342
2343 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2344 ("Could not receive message. (%s)", str));
2345 g_free (str);
2346 return GST_FLOW_ERROR;
2347 }
2348 handle_request_failed:
2349 {
2350 gchar *str = gst_rtsp_strresult (res);
2351 GstFlowReturn ret;
2352
2353 gst_rtsp_message_unset (&message);
2354 if (res != GST_RTSP_EINTR) {
2355 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2356 ("Could not handle server message. (%s)", str));
2357 g_free (str);
2358 ret = GST_FLOW_ERROR;
2359 } else {
2360 ret = GST_FLOW_FLUSHING;
2361 }
2362 return ret;
2363 }
2364 server_eof:
2365 {
2366 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2367 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2368 ("The server closed the connection."));
2369 sink->conninfo.connected = FALSE;
2370 gst_rtsp_message_unset (&message);
2371 return GST_FLOW_EOS;
2372 }
2373 }
2374
2375 static GstRTSPResult
gst_rtsp_client_sink_reconnect(GstRTSPClientSink * sink,gboolean async)2376 gst_rtsp_client_sink_reconnect (GstRTSPClientSink * sink, gboolean async)
2377 {
2378 GstRTSPResult res = GST_RTSP_OK;
2379 gboolean restart = FALSE;
2380
2381 GST_DEBUG_OBJECT (sink, "doing reconnect");
2382
2383 GST_FIXME_OBJECT (sink, "Reconnection is not yet implemented");
2384
2385 /* no need to restart, we're done */
2386 if (!restart)
2387 goto done;
2388
2389 /* we can try only TCP now */
2390 sink->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
2391
2392 /* close and cleanup our state */
2393 if ((res = gst_rtsp_client_sink_close (sink, async, FALSE)) < 0)
2394 goto done;
2395
2396 /* see if we have TCP left to try. Also don't try TCP when we were configured
2397 * with an SDP. */
2398 if (!(sink->protocols & GST_RTSP_LOWER_TRANS_TCP) || sink->from_sdp)
2399 goto no_protocols;
2400
2401 /* We post a warning message now to inform the user
2402 * that nothing happened. It's most likely a firewall thing. */
2403 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2404 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2405 "firewall is blocking it. Retrying using a TCP connection.",
2406 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2407
2408 /* open new connection using tcp */
2409 if (gst_rtsp_client_sink_open (sink, async) < 0)
2410 goto open_failed;
2411
2412 /* start recording */
2413 if (gst_rtsp_client_sink_record (sink, async) < 0)
2414 goto play_failed;
2415
2416 done:
2417 return res;
2418
2419 /* ERRORS */
2420 no_protocols:
2421 {
2422 sink->cur_protocols = 0;
2423 /* no transport possible, post an error and stop */
2424 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2425 ("Could not receive any UDP packets for %.4f seconds, maybe your "
2426 "firewall is blocking it. No other protocols to try.",
2427 gst_guint64_to_gdouble (sink->udp_timeout / 1000000.0)));
2428 return GST_RTSP_ERROR;
2429 }
2430 open_failed:
2431 {
2432 GST_DEBUG_OBJECT (sink, "open failed");
2433 return GST_RTSP_OK;
2434 }
2435 play_failed:
2436 {
2437 GST_DEBUG_OBJECT (sink, "play failed");
2438 return GST_RTSP_OK;
2439 }
2440 }
2441
2442 static void
gst_rtsp_client_sink_loop_start_cmd(GstRTSPClientSink * sink,gint cmd)2443 gst_rtsp_client_sink_loop_start_cmd (GstRTSPClientSink * sink, gint cmd)
2444 {
2445 switch (cmd) {
2446 case CMD_OPEN:
2447 GST_ELEMENT_PROGRESS (sink, START, "open", ("Opening Stream"));
2448 break;
2449 case CMD_RECORD:
2450 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending RECORD request"));
2451 break;
2452 case CMD_PAUSE:
2453 GST_ELEMENT_PROGRESS (sink, START, "request", ("Sending PAUSE request"));
2454 break;
2455 case CMD_CLOSE:
2456 GST_ELEMENT_PROGRESS (sink, START, "close", ("Closing Stream"));
2457 break;
2458 default:
2459 break;
2460 }
2461 }
2462
2463 static void
gst_rtsp_client_sink_loop_complete_cmd(GstRTSPClientSink * sink,gint cmd)2464 gst_rtsp_client_sink_loop_complete_cmd (GstRTSPClientSink * sink, gint cmd)
2465 {
2466 switch (cmd) {
2467 case CMD_OPEN:
2468 GST_ELEMENT_PROGRESS (sink, COMPLETE, "open", ("Opened Stream"));
2469 break;
2470 case CMD_RECORD:
2471 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent RECORD request"));
2472 break;
2473 case CMD_PAUSE:
2474 GST_ELEMENT_PROGRESS (sink, COMPLETE, "request", ("Sent PAUSE request"));
2475 break;
2476 case CMD_CLOSE:
2477 GST_ELEMENT_PROGRESS (sink, COMPLETE, "close", ("Closed Stream"));
2478 break;
2479 default:
2480 break;
2481 }
2482 }
2483
2484 static void
gst_rtsp_client_sink_loop_cancel_cmd(GstRTSPClientSink * sink,gint cmd)2485 gst_rtsp_client_sink_loop_cancel_cmd (GstRTSPClientSink * sink, gint cmd)
2486 {
2487 switch (cmd) {
2488 case CMD_OPEN:
2489 GST_ELEMENT_PROGRESS (sink, CANCELED, "open", ("Open canceled"));
2490 break;
2491 case CMD_RECORD:
2492 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("RECORD canceled"));
2493 break;
2494 case CMD_PAUSE:
2495 GST_ELEMENT_PROGRESS (sink, CANCELED, "request", ("PAUSE canceled"));
2496 break;
2497 case CMD_CLOSE:
2498 GST_ELEMENT_PROGRESS (sink, CANCELED, "close", ("Close canceled"));
2499 break;
2500 default:
2501 break;
2502 }
2503 }
2504
2505 static void
gst_rtsp_client_sink_loop_error_cmd(GstRTSPClientSink * sink,gint cmd)2506 gst_rtsp_client_sink_loop_error_cmd (GstRTSPClientSink * sink, gint cmd)
2507 {
2508 switch (cmd) {
2509 case CMD_OPEN:
2510 GST_ELEMENT_PROGRESS (sink, ERROR, "open", ("Open failed"));
2511 break;
2512 case CMD_RECORD:
2513 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("RECORD failed"));
2514 break;
2515 case CMD_PAUSE:
2516 GST_ELEMENT_PROGRESS (sink, ERROR, "request", ("PAUSE failed"));
2517 break;
2518 case CMD_CLOSE:
2519 GST_ELEMENT_PROGRESS (sink, ERROR, "close", ("Close failed"));
2520 break;
2521 default:
2522 break;
2523 }
2524 }
2525
2526 static void
gst_rtsp_client_sink_loop_end_cmd(GstRTSPClientSink * sink,gint cmd,GstRTSPResult ret)2527 gst_rtsp_client_sink_loop_end_cmd (GstRTSPClientSink * sink, gint cmd,
2528 GstRTSPResult ret)
2529 {
2530 if (ret == GST_RTSP_OK)
2531 gst_rtsp_client_sink_loop_complete_cmd (sink, cmd);
2532 else if (ret == GST_RTSP_EINTR)
2533 gst_rtsp_client_sink_loop_cancel_cmd (sink, cmd);
2534 else
2535 gst_rtsp_client_sink_loop_error_cmd (sink, cmd);
2536 }
2537
2538 static gboolean
gst_rtsp_client_sink_loop_send_cmd(GstRTSPClientSink * sink,gint cmd,gint mask)2539 gst_rtsp_client_sink_loop_send_cmd (GstRTSPClientSink * sink, gint cmd,
2540 gint mask)
2541 {
2542 gint old;
2543 gboolean flushed = FALSE;
2544
2545 /* start new request */
2546 gst_rtsp_client_sink_loop_start_cmd (sink, cmd);
2547
2548 GST_DEBUG_OBJECT (sink, "sending cmd %s", cmd_to_string (cmd));
2549
2550 GST_OBJECT_LOCK (sink);
2551 old = sink->pending_cmd;
2552 if (old == CMD_RECONNECT) {
2553 GST_DEBUG_OBJECT (sink, "ignore, we were reconnecting");
2554 cmd = CMD_RECONNECT;
2555 }
2556 if (old != CMD_WAIT) {
2557 sink->pending_cmd = CMD_WAIT;
2558 GST_OBJECT_UNLOCK (sink);
2559 /* cancel previous request */
2560 GST_DEBUG_OBJECT (sink, "cancel previous request %s", cmd_to_string (old));
2561 gst_rtsp_client_sink_loop_cancel_cmd (sink, old);
2562 GST_OBJECT_LOCK (sink);
2563 }
2564 sink->pending_cmd = cmd;
2565 /* interrupt if allowed */
2566 if (sink->busy_cmd & mask) {
2567 GST_DEBUG_OBJECT (sink, "connection flush busy %s",
2568 cmd_to_string (sink->busy_cmd));
2569 gst_rtsp_client_sink_connection_flush (sink, TRUE);
2570 flushed = TRUE;
2571 } else {
2572 GST_DEBUG_OBJECT (sink, "not interrupting busy cmd %s",
2573 cmd_to_string (sink->busy_cmd));
2574 }
2575 if (sink->task)
2576 gst_task_start (sink->task);
2577 GST_OBJECT_UNLOCK (sink);
2578
2579 return flushed;
2580 }
2581
2582 static gboolean
gst_rtsp_client_sink_loop(GstRTSPClientSink * sink)2583 gst_rtsp_client_sink_loop (GstRTSPClientSink * sink)
2584 {
2585 GstFlowReturn ret;
2586
2587 if (!sink->conninfo.connection || !sink->conninfo.connected)
2588 goto no_connection;
2589
2590 ret = gst_rtsp_client_sink_loop_rx (sink);
2591 if (ret != GST_FLOW_OK)
2592 goto pause;
2593
2594 return TRUE;
2595
2596 /* ERRORS */
2597 no_connection:
2598 {
2599 GST_WARNING_OBJECT (sink, "we are not connected");
2600 ret = GST_FLOW_FLUSHING;
2601 goto pause;
2602 }
2603 pause:
2604 {
2605 const gchar *reason = gst_flow_get_name (ret);
2606
2607 GST_DEBUG_OBJECT (sink, "pausing task, reason %s", reason);
2608 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_LOOP);
2609 return FALSE;
2610 }
2611 }
2612
2613 #ifndef GST_DISABLE_GST_DEBUG
2614 static const gchar *
gst_rtsp_auth_method_to_string(GstRTSPAuthMethod method)2615 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
2616 {
2617 gint index = 0;
2618
2619 while (method != 0) {
2620 index++;
2621 method >>= 1;
2622 }
2623 switch (index) {
2624 case 0:
2625 return "None";
2626 case 1:
2627 return "Basic";
2628 case 2:
2629 return "Digest";
2630 }
2631
2632 return "Unknown";
2633 }
2634 #endif
2635
2636 /* Parse a WWW-Authenticate Response header and determine the
2637 * available authentication methods
2638 *
2639 * This code should also cope with the fact that each WWW-Authenticate
2640 * header can contain multiple challenge methods + tokens
2641 *
2642 * At the moment, for Basic auth, we just do a minimal check and don't
2643 * even parse out the realm */
2644 static void
gst_rtsp_client_sink_parse_auth_hdr(GstRTSPMessage * response,GstRTSPAuthMethod * methods,GstRTSPConnection * conn,gboolean * stale)2645 gst_rtsp_client_sink_parse_auth_hdr (GstRTSPMessage * response,
2646 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
2647 {
2648 GstRTSPAuthCredential **credentials, **credential;
2649
2650 g_return_if_fail (response != NULL);
2651 g_return_if_fail (methods != NULL);
2652 g_return_if_fail (stale != NULL);
2653
2654 credentials =
2655 gst_rtsp_message_parse_auth_credentials (response,
2656 GST_RTSP_HDR_WWW_AUTHENTICATE);
2657 if (!credentials)
2658 return;
2659
2660 credential = credentials;
2661 while (*credential) {
2662 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
2663 *methods |= GST_RTSP_AUTH_BASIC;
2664 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
2665 GstRTSPAuthParam **param = (*credential)->params;
2666
2667 *methods |= GST_RTSP_AUTH_DIGEST;
2668
2669 gst_rtsp_connection_clear_auth_params (conn);
2670 *stale = FALSE;
2671
2672 while (*param) {
2673 if (strcmp ((*param)->name, "stale") == 0
2674 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
2675 *stale = TRUE;
2676 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
2677 (*param)->value);
2678 param++;
2679 }
2680 }
2681
2682 credential++;
2683 }
2684
2685 gst_rtsp_auth_credentials_free (credentials);
2686 }
2687
2688 /**
2689 * gst_rtsp_client_sink_setup_auth:
2690 * @src: the rtsp source
2691 *
2692 * Configure a username and password and auth method on the
2693 * connection object based on a response we received from the
2694 * peer.
2695 *
2696 * Currently, this requires that a username and password were supplied
2697 * in the uri. In the future, they may be requested on demand by sending
2698 * a message up the bus.
2699 *
2700 * Returns: TRUE if authentication information could be set up correctly.
2701 */
2702 static gboolean
gst_rtsp_client_sink_setup_auth(GstRTSPClientSink * sink,GstRTSPMessage * response)2703 gst_rtsp_client_sink_setup_auth (GstRTSPClientSink * sink,
2704 GstRTSPMessage * response)
2705 {
2706 gchar *user = NULL;
2707 gchar *pass = NULL;
2708 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
2709 GstRTSPAuthMethod method;
2710 GstRTSPResult auth_result;
2711 GstRTSPUrl *url;
2712 GstRTSPConnection *conn;
2713 gboolean stale = FALSE;
2714
2715 conn = sink->conninfo.connection;
2716
2717 /* Identify the available auth methods and see if any are supported */
2718 gst_rtsp_client_sink_parse_auth_hdr (response, &avail_methods, conn, &stale);
2719
2720 if (avail_methods == GST_RTSP_AUTH_NONE)
2721 goto no_auth_available;
2722
2723 /* For digest auth, if the response indicates that the session
2724 * data are stale, we just update them in the connection object and
2725 * return TRUE to retry the request */
2726 if (stale)
2727 sink->tried_url_auth = FALSE;
2728
2729 url = gst_rtsp_connection_get_url (conn);
2730
2731 /* Do we have username and password available? */
2732 if (url != NULL && !sink->tried_url_auth && url->user != NULL
2733 && url->passwd != NULL) {
2734 user = url->user;
2735 pass = url->passwd;
2736 sink->tried_url_auth = TRUE;
2737 GST_DEBUG_OBJECT (sink,
2738 "Attempting authentication using credentials from the URL");
2739 } else {
2740 user = sink->user_id;
2741 pass = sink->user_pw;
2742 GST_DEBUG_OBJECT (sink,
2743 "Attempting authentication using credentials from the properties");
2744 }
2745
2746 /* FIXME: If the url didn't contain username and password or we tried them
2747 * already, request a username and passwd from the application via some kind
2748 * of credentials request message */
2749
2750 /* If we don't have a username and passwd at this point, bail out. */
2751 if (user == NULL || pass == NULL)
2752 goto no_user_pass;
2753
2754 /* Try to configure for each available authentication method, strongest to
2755 * weakest */
2756 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
2757 /* Check if this method is available on the server */
2758 if ((method & avail_methods) == 0)
2759 continue;
2760
2761 /* Pass the credentials to the connection to try on the next request */
2762 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
2763 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
2764 * ignore it and end up retrying later */
2765 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
2766 GST_DEBUG_OBJECT (sink, "Attempting %s authentication",
2767 gst_rtsp_auth_method_to_string (method));
2768 break;
2769 }
2770 }
2771
2772 if (method == GST_RTSP_AUTH_NONE)
2773 goto no_auth_available;
2774
2775 return TRUE;
2776
2777 no_auth_available:
2778 {
2779 /* Output an error indicating that we couldn't connect because there were
2780 * no supported authentication protocols */
2781 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
2782 ("No supported authentication protocol was found"));
2783 return FALSE;
2784 }
2785 no_user_pass:
2786 {
2787 /* We don't fire an error message, we just return FALSE and let the
2788 * normal NOT_AUTHORIZED error be propagated */
2789 return FALSE;
2790 }
2791 }
2792
2793 static GstRTSPResult
gst_rtsp_client_sink_try_send(GstRTSPClientSink * sink,GstRTSPConnInfo * conninfo,GstRTSPMessage * request,GstRTSPMessage * response,GstRTSPStatusCode * code)2794 gst_rtsp_client_sink_try_send (GstRTSPClientSink * sink,
2795 GstRTSPConnInfo * conninfo, GstRTSPMessage * request,
2796 GstRTSPMessage * response, GstRTSPStatusCode * code)
2797 {
2798 GstRTSPResult res;
2799 GstRTSPStatusCode thecode;
2800 gchar *content_base = NULL;
2801 gint try = 0;
2802
2803 again:
2804 GST_DEBUG_OBJECT (sink, "sending message");
2805
2806 if (sink->debug)
2807 gst_rtsp_message_dump (request);
2808
2809 g_mutex_lock (&sink->send_lock);
2810
2811 res =
2812 gst_rtsp_client_sink_connection_send (sink, conninfo, request,
2813 sink->ptcp_timeout);
2814 if (res < 0) {
2815 g_mutex_unlock (&sink->send_lock);
2816 goto send_error;
2817 }
2818
2819 gst_rtsp_connection_reset_timeout (conninfo->connection);
2820
2821 /* See if we should handle the response */
2822 if (response == NULL) {
2823 g_mutex_unlock (&sink->send_lock);
2824 return GST_RTSP_OK;
2825 }
2826 next:
2827 res =
2828 gst_rtsp_client_sink_connection_receive (sink, conninfo, response,
2829 sink->ptcp_timeout);
2830
2831 g_mutex_unlock (&sink->send_lock);
2832
2833 if (res < 0)
2834 goto receive_error;
2835
2836 if (sink->debug)
2837 gst_rtsp_message_dump (response);
2838
2839
2840 switch (response->type) {
2841 case GST_RTSP_MESSAGE_REQUEST:
2842 res = gst_rtsp_client_sink_handle_request (sink, conninfo, response);
2843 if (res == GST_RTSP_EEOF)
2844 goto server_eof;
2845 else if (res < 0)
2846 goto handle_request_failed;
2847 g_mutex_lock (&sink->send_lock);
2848 goto next;
2849 case GST_RTSP_MESSAGE_RESPONSE:
2850 /* ok, a response is good */
2851 GST_DEBUG_OBJECT (sink, "received response message");
2852 break;
2853 case GST_RTSP_MESSAGE_DATA:
2854 /* we ignore data messages */
2855 GST_DEBUG_OBJECT (sink, "ignoring data message");
2856 g_mutex_lock (&sink->send_lock);
2857 goto next;
2858 default:
2859 GST_WARNING_OBJECT (sink, "ignoring unknown message type %d",
2860 response->type);
2861 g_mutex_lock (&sink->send_lock);
2862 goto next;
2863 }
2864
2865 thecode = response->type_data.response.code;
2866
2867 GST_DEBUG_OBJECT (sink, "got response message %d", thecode);
2868
2869 /* if the caller wanted the result code, we store it. */
2870 if (code)
2871 *code = thecode;
2872
2873 /* If the request didn't succeed, bail out before doing any more */
2874 if (thecode != GST_RTSP_STS_OK)
2875 return GST_RTSP_OK;
2876
2877 /* store new content base if any */
2878 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
2879 &content_base, 0);
2880 if (content_base) {
2881 g_free (sink->content_base);
2882 sink->content_base = g_strdup (content_base);
2883 }
2884
2885 return GST_RTSP_OK;
2886
2887 /* ERRORS */
2888 send_error:
2889 {
2890 gchar *str = gst_rtsp_strresult (res);
2891
2892 if (res != GST_RTSP_EINTR) {
2893 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
2894 ("Could not send message. (%s)", str));
2895 } else {
2896 GST_WARNING_OBJECT (sink, "send interrupted");
2897 }
2898 g_free (str);
2899 return res;
2900 }
2901 receive_error:
2902 {
2903 switch (res) {
2904 case GST_RTSP_EEOF:
2905 GST_WARNING_OBJECT (sink, "server closed connection");
2906 if ((try == 0) && !sink->interleaved && sink->udp_reconnect) {
2907 try++;
2908 /* if reconnect succeeds, try again */
2909 if ((res =
2910 gst_rtsp_conninfo_reconnect (sink, &sink->conninfo,
2911 FALSE)) == 0)
2912 goto again;
2913 }
2914 /* only try once after reconnect, then fallthrough and error out */
2915 default:
2916 {
2917 gchar *str = gst_rtsp_strresult (res);
2918
2919 if (res != GST_RTSP_EINTR) {
2920 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2921 ("Could not receive message. (%s)", str));
2922 } else {
2923 GST_WARNING_OBJECT (sink, "receive interrupted");
2924 }
2925 g_free (str);
2926 break;
2927 }
2928 }
2929 return res;
2930 }
2931 handle_request_failed:
2932 {
2933 /* ERROR was posted */
2934 gst_rtsp_message_unset (response);
2935 return res;
2936 }
2937 server_eof:
2938 {
2939 GST_DEBUG_OBJECT (sink, "we got an eof from the server");
2940 GST_ELEMENT_WARNING (sink, RESOURCE, READ, (NULL),
2941 ("The server closed the connection."));
2942 gst_rtsp_message_unset (response);
2943 return res;
2944 }
2945 }
2946
2947 static void
gst_rtsp_client_sink_set_state(GstRTSPClientSink * sink,GstState state)2948 gst_rtsp_client_sink_set_state (GstRTSPClientSink * sink, GstState state)
2949 {
2950 GST_DEBUG_OBJECT (sink, "Setting internal state to %s",
2951 gst_element_state_get_name (state));
2952 gst_element_set_state (GST_ELEMENT (sink->internal_bin), state);
2953 }
2954
2955 /**
2956 * gst_rtsp_client_sink_send:
2957 * @src: the rtsp source
2958 * @conn: the connection to send on
2959 * @request: must point to a valid request
2960 * @response: must point to an empty #GstRTSPMessage
2961 * @code: an optional code result
2962 *
2963 * send @request and retrieve the response in @response. optionally @code can be
2964 * non-NULL in which case it will contain the status code of the response.
2965 *
2966 * If This function returns #GST_RTSP_OK, @response will contain a valid response
2967 * message that should be cleaned with gst_rtsp_message_unset() after usage.
2968 *
2969 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
2970 * @response message) if the response code was not 200 (OK).
2971 *
2972 * If the attempt results in an authentication failure, then this will attempt
2973 * to retrieve authentication credentials via gst_rtsp_client_sink_setup_auth and retry
2974 * the request.
2975 *
2976 * Returns: #GST_RTSP_OK if the processing was successful.
2977 */
2978 static GstRTSPResult
gst_rtsp_client_sink_send(GstRTSPClientSink * sink,GstRTSPConnInfo * conninfo,GstRTSPMessage * request,GstRTSPMessage * response,GstRTSPStatusCode * code)2979 gst_rtsp_client_sink_send (GstRTSPClientSink * sink, GstRTSPConnInfo * conninfo,
2980 GstRTSPMessage * request, GstRTSPMessage * response,
2981 GstRTSPStatusCode * code)
2982 {
2983 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
2984 GstRTSPResult res = GST_RTSP_ERROR;
2985 gint count;
2986 gboolean retry;
2987 GstRTSPMethod method = GST_RTSP_INVALID;
2988
2989 count = 0;
2990 do {
2991 retry = FALSE;
2992
2993 /* make sure we don't loop forever */
2994 if (count++ > 8)
2995 break;
2996
2997 /* save method so we can disable it when the server complains */
2998 method = request->type_data.request.method;
2999
3000 if ((res =
3001 gst_rtsp_client_sink_try_send (sink, conninfo, request, response,
3002 &int_code)) < 0)
3003 goto error;
3004
3005 switch (int_code) {
3006 case GST_RTSP_STS_UNAUTHORIZED:
3007 if (gst_rtsp_client_sink_setup_auth (sink, response)) {
3008 /* Try the request/response again after configuring the auth info
3009 * and loop again */
3010 retry = TRUE;
3011 }
3012 break;
3013 default:
3014 break;
3015 }
3016 } while (retry == TRUE);
3017
3018 /* If the user requested the code, let them handle errors, otherwise
3019 * post an error below */
3020 if (code != NULL)
3021 *code = int_code;
3022 else if (int_code != GST_RTSP_STS_OK)
3023 goto error_response;
3024
3025 return res;
3026
3027 /* ERRORS */
3028 error:
3029 {
3030 GST_DEBUG_OBJECT (sink, "got error %d", res);
3031 return res;
3032 }
3033 error_response:
3034 {
3035 res = GST_RTSP_ERROR;
3036
3037 switch (response->type_data.response.code) {
3038 case GST_RTSP_STS_NOT_FOUND:
3039 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL), ("%s",
3040 response->type_data.response.reason));
3041 break;
3042 case GST_RTSP_STS_UNAUTHORIZED:
3043 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
3044 response->type_data.response.reason));
3045 break;
3046 case GST_RTSP_STS_MOVED_PERMANENTLY:
3047 case GST_RTSP_STS_MOVE_TEMPORARILY:
3048 {
3049 gchar *new_location;
3050 GstRTSPLowerTrans transports;
3051
3052 GST_DEBUG_OBJECT (sink, "got redirection");
3053 /* if we don't have a Location Header, we must error */
3054 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
3055 &new_location, 0) < 0)
3056 break;
3057
3058 /* When we receive a redirect result, we go back to the INIT state after
3059 * parsing the new URI. The caller should do the needed steps to issue
3060 * a new setup when it detects this state change. */
3061 GST_DEBUG_OBJECT (sink, "redirection to %s", new_location);
3062
3063 /* save current transports */
3064 if (sink->conninfo.url)
3065 transports = sink->conninfo.url->transports;
3066 else
3067 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
3068
3069 gst_rtsp_client_sink_uri_set_uri (GST_URI_HANDLER (sink), new_location,
3070 NULL);
3071
3072 /* set old transports */
3073 if (sink->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
3074 sink->conninfo.url->transports = transports;
3075
3076 sink->need_redirect = TRUE;
3077 sink->state = GST_RTSP_STATE_INIT;
3078 res = GST_RTSP_OK;
3079 break;
3080 }
3081 case GST_RTSP_STS_NOT_ACCEPTABLE:
3082 case GST_RTSP_STS_NOT_IMPLEMENTED:
3083 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
3084 GST_WARNING_OBJECT (sink, "got NOT IMPLEMENTED, disable method %s",
3085 gst_rtsp_method_as_text (method));
3086 sink->methods &= ~method;
3087 res = GST_RTSP_OK;
3088 break;
3089 default:
3090 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3091 ("Got error response: %d (%s).", response->type_data.response.code,
3092 response->type_data.response.reason));
3093 break;
3094 }
3095 /* if we return ERROR we should unset the response ourselves */
3096 if (res == GST_RTSP_ERROR)
3097 gst_rtsp_message_unset (response);
3098
3099 return res;
3100 }
3101 }
3102
3103 /* parse the response and collect all the supported methods. We need this
3104 * information so that we don't try to send an unsupported request to the
3105 * server.
3106 */
3107 static gboolean
gst_rtsp_client_sink_parse_methods(GstRTSPClientSink * sink,GstRTSPMessage * response)3108 gst_rtsp_client_sink_parse_methods (GstRTSPClientSink * sink,
3109 GstRTSPMessage * response)
3110 {
3111 GstRTSPHeaderField field;
3112 gchar *respoptions;
3113 gint indx = 0;
3114
3115 /* reset supported methods */
3116 sink->methods = 0;
3117
3118 /* Try Allow Header first */
3119 field = GST_RTSP_HDR_ALLOW;
3120 while (TRUE) {
3121 respoptions = NULL;
3122 gst_rtsp_message_get_header (response, field, &respoptions, indx);
3123 if (indx == 0 && !respoptions) {
3124 /* if no Allow header was found then try the Public header... */
3125 field = GST_RTSP_HDR_PUBLIC;
3126 gst_rtsp_message_get_header (response, field, &respoptions, indx);
3127 }
3128 if (!respoptions)
3129 break;
3130
3131 sink->methods |= gst_rtsp_options_from_text (respoptions);
3132
3133 indx++;
3134 }
3135
3136 if (sink->methods == 0) {
3137 /* neither Allow nor Public are required, assume the server supports
3138 * at least SETUP. */
3139 GST_DEBUG_OBJECT (sink, "could not get OPTIONS");
3140 sink->methods = GST_RTSP_SETUP;
3141 }
3142
3143 /* Even if the server replied, and didn't say it supports
3144 * RECORD|ANNOUNCE, try anyway by assuming it does */
3145 sink->methods |= GST_RTSP_ANNOUNCE | GST_RTSP_RECORD;
3146
3147 if (!(sink->methods & GST_RTSP_SETUP))
3148 goto no_setup;
3149
3150 return TRUE;
3151
3152 /* ERRORS */
3153 no_setup:
3154 {
3155 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ, (NULL),
3156 ("Server does not support SETUP."));
3157 return FALSE;
3158 }
3159 }
3160
3161 static GstRTSPResult
gst_rtsp_client_sink_connect_to_server(GstRTSPClientSink * sink,gboolean async)3162 gst_rtsp_client_sink_connect_to_server (GstRTSPClientSink * sink,
3163 gboolean async)
3164 {
3165 GstRTSPResult res;
3166 GstRTSPMessage request = { 0 };
3167 GstRTSPMessage response = { 0 };
3168 GSocket *conn_socket;
3169 GSocketAddress *sa;
3170 GInetAddress *ia;
3171
3172 sink->need_redirect = FALSE;
3173
3174 /* can't continue without a valid url */
3175 if (G_UNLIKELY (sink->conninfo.url == NULL)) {
3176 res = GST_RTSP_EINVAL;
3177 goto no_url;
3178 }
3179 sink->tried_url_auth = FALSE;
3180
3181 if ((res = gst_rtsp_conninfo_connect (sink, &sink->conninfo, async)) < 0)
3182 goto connect_failed;
3183
3184 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
3185 sa = g_socket_get_remote_address (conn_socket, NULL);
3186 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
3187
3188 sink->server_ip = g_inet_address_to_string (ia);
3189
3190 g_object_unref (sa);
3191
3192 /* create OPTIONS */
3193 GST_DEBUG_OBJECT (sink, "create options...");
3194 res =
3195 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_OPTIONS,
3196 sink->conninfo.url_str);
3197 if (res < 0)
3198 goto create_request_failed;
3199
3200 /* send OPTIONS */
3201 GST_DEBUG_OBJECT (sink, "send options...");
3202
3203 if (async)
3204 GST_ELEMENT_PROGRESS (sink, CONTINUE, "open",
3205 ("Retrieving server options"));
3206
3207 if ((res =
3208 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
3209 &response, NULL)) < 0)
3210 goto send_error;
3211
3212 /* parse OPTIONS */
3213 if (!gst_rtsp_client_sink_parse_methods (sink, &response))
3214 goto methods_error;
3215
3216 /* FIXME: Do we need to handle REDIRECT responses for OPTIONS? */
3217
3218 /* clean up any messages */
3219 gst_rtsp_message_unset (&request);
3220 gst_rtsp_message_unset (&response);
3221
3222 return res;
3223
3224 /* ERRORS */
3225 no_url:
3226 {
3227 GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (NULL),
3228 ("No valid RTSP URL was provided"));
3229 goto cleanup_error;
3230 }
3231 connect_failed:
3232 {
3233 gchar *str = gst_rtsp_strresult (res);
3234
3235 if (res != GST_RTSP_EINTR) {
3236 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_READ_WRITE, (NULL),
3237 ("Failed to connect. (%s)", str));
3238 } else {
3239 GST_WARNING_OBJECT (sink, "connect interrupted");
3240 }
3241 g_free (str);
3242 goto cleanup_error;
3243 }
3244 create_request_failed:
3245 {
3246 gchar *str = gst_rtsp_strresult (res);
3247
3248 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3249 ("Could not create request. (%s)", str));
3250 g_free (str);
3251 goto cleanup_error;
3252 }
3253 send_error:
3254 {
3255 /* Don't post a message - the rtsp_send method will have
3256 * taken care of it because we passed NULL for the response code */
3257 goto cleanup_error;
3258 }
3259 methods_error:
3260 {
3261 /* error was posted */
3262 res = GST_RTSP_ERROR;
3263 goto cleanup_error;
3264 }
3265 cleanup_error:
3266 {
3267 if (sink->conninfo.connection) {
3268 GST_DEBUG_OBJECT (sink, "free connection");
3269 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3270 }
3271 gst_rtsp_message_unset (&request);
3272 gst_rtsp_message_unset (&response);
3273 return res;
3274 }
3275 }
3276
3277 static GstRTSPResult
gst_rtsp_client_sink_open(GstRTSPClientSink * sink,gboolean async)3278 gst_rtsp_client_sink_open (GstRTSPClientSink * sink, gboolean async)
3279 {
3280 GstRTSPResult ret;
3281
3282 sink->methods =
3283 GST_RTSP_SETUP | GST_RTSP_RECORD | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
3284
3285 g_mutex_lock (&sink->open_conn_lock);
3286 sink->open_conn_start = TRUE;
3287 g_cond_broadcast (&sink->open_conn_cond);
3288 GST_DEBUG_OBJECT (sink, "connection to server started");
3289 g_mutex_unlock (&sink->open_conn_lock);
3290
3291 if ((ret = gst_rtsp_client_sink_connect_to_server (sink, async)) < 0)
3292 goto open_failed;
3293
3294 if (async)
3295 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3296
3297 return ret;
3298
3299 /* ERRORS */
3300 open_failed:
3301 {
3302 GST_WARNING_OBJECT (sink, "Failed to connect to server");
3303 sink->open_error = TRUE;
3304 if (async)
3305 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_OPEN, ret);
3306 return ret;
3307 }
3308 }
3309
3310 static GstRTSPResult
gst_rtsp_client_sink_close(GstRTSPClientSink * sink,gboolean async,gboolean only_close)3311 gst_rtsp_client_sink_close (GstRTSPClientSink * sink, gboolean async,
3312 gboolean only_close)
3313 {
3314 GstRTSPMessage request = { 0 };
3315 GstRTSPMessage response = { 0 };
3316 GstRTSPResult res = GST_RTSP_OK;
3317 GList *walk;
3318 const gchar *control;
3319
3320 GST_DEBUG_OBJECT (sink, "TEARDOWN...");
3321
3322 gst_rtsp_client_sink_set_state (sink, GST_STATE_NULL);
3323
3324 if (sink->state < GST_RTSP_STATE_READY) {
3325 GST_DEBUG_OBJECT (sink, "not ready, doing cleanup");
3326 goto close;
3327 }
3328
3329 if (only_close)
3330 goto close;
3331
3332 /* construct a control url */
3333 control = get_aggregate_control (sink);
3334
3335 if (!(sink->methods & (GST_RTSP_RECORD | GST_RTSP_TEARDOWN)))
3336 goto not_supported;
3337
3338 /* stop streaming */
3339 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3340 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3341
3342 if (context->stream_transport)
3343 gst_rtsp_stream_transport_set_active (context->stream_transport, FALSE);
3344
3345 if (context->joined) {
3346 gst_rtsp_stream_leave_bin (context->stream, GST_BIN (sink->internal_bin),
3347 sink->rtpbin);
3348 context->joined = FALSE;
3349 }
3350 }
3351
3352 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3353 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3354 const gchar *setup_url;
3355 GstRTSPConnInfo *info;
3356
3357 GST_DEBUG_OBJECT (sink, "Looking at stream %p for teardown",
3358 context->stream);
3359
3360 /* try aggregate control first but do non-aggregate control otherwise */
3361 if (control)
3362 setup_url = control;
3363 else if ((setup_url = context->conninfo.location) == NULL) {
3364 GST_DEBUG_OBJECT (sink, "Skipping TEARDOWN stream %p - no setup URL",
3365 context->stream);
3366 continue;
3367 }
3368
3369 if (sink->conninfo.connection) {
3370 info = &sink->conninfo;
3371 } else if (context->conninfo.connection) {
3372 info = &context->conninfo;
3373 } else {
3374 continue;
3375 }
3376 if (!info->connected)
3377 goto next;
3378
3379 /* do TEARDOWN */
3380 GST_DEBUG_OBJECT (sink, "Sending teardown for stream %p at URL %s",
3381 context->stream, setup_url);
3382 res =
3383 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_TEARDOWN,
3384 setup_url);
3385 if (res < 0)
3386 goto create_request_failed;
3387
3388 if (async)
3389 GST_ELEMENT_PROGRESS (sink, CONTINUE, "close", ("Closing stream"));
3390
3391 if ((res =
3392 gst_rtsp_client_sink_send (sink, info, &request,
3393 &response, NULL)) < 0)
3394 goto send_error;
3395
3396 /* FIXME, parse result? */
3397 gst_rtsp_message_unset (&request);
3398 gst_rtsp_message_unset (&response);
3399
3400 next:
3401 /* early exit when we did aggregate control */
3402 if (control)
3403 break;
3404 }
3405
3406 close:
3407 /* close connections */
3408 GST_DEBUG_OBJECT (sink, "closing connection...");
3409 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
3410 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3411 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
3412 gst_rtsp_conninfo_close (sink, &stream->conninfo, TRUE);
3413 }
3414
3415 /* cleanup */
3416 gst_rtsp_client_sink_cleanup (sink);
3417
3418 sink->state = GST_RTSP_STATE_INVALID;
3419
3420 if (async)
3421 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_CLOSE, res);
3422
3423 return res;
3424
3425 /* ERRORS */
3426 create_request_failed:
3427 {
3428 gchar *str = gst_rtsp_strresult (res);
3429
3430 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
3431 ("Could not create request. (%s)", str));
3432 g_free (str);
3433 goto close;
3434 }
3435 send_error:
3436 {
3437 gchar *str = gst_rtsp_strresult (res);
3438
3439 gst_rtsp_message_unset (&request);
3440 if (res != GST_RTSP_EINTR) {
3441 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
3442 ("Could not send message. (%s)", str));
3443 } else {
3444 GST_WARNING_OBJECT (sink, "TEARDOWN interrupted");
3445 }
3446 g_free (str);
3447 goto close;
3448 }
3449 not_supported:
3450 {
3451 GST_DEBUG_OBJECT (sink,
3452 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
3453 goto close;
3454 }
3455 }
3456
3457 static gboolean
gst_rtsp_client_sink_configure_manager(GstRTSPClientSink * sink)3458 gst_rtsp_client_sink_configure_manager (GstRTSPClientSink * sink)
3459 {
3460 GstElement *rtpbin;
3461 GstStateChangeReturn ret;
3462
3463 rtpbin = sink->rtpbin;
3464
3465 if (rtpbin == NULL) {
3466 GObjectClass *klass;
3467
3468 rtpbin = gst_element_factory_make ("rtpbin", NULL);
3469 if (rtpbin == NULL)
3470 goto no_rtpbin;
3471
3472 gst_bin_add (GST_BIN_CAST (sink->internal_bin), rtpbin);
3473
3474 sink->rtpbin = rtpbin;
3475
3476 /* Any more settings we should configure on rtpbin here? */
3477 g_object_set (sink->rtpbin, "latency", sink->latency, NULL);
3478
3479 klass = G_OBJECT_GET_CLASS (G_OBJECT (rtpbin));
3480
3481 if (g_object_class_find_property (klass, "ntp-time-source")) {
3482 g_object_set (sink->rtpbin, "ntp-time-source", sink->ntp_time_source,
3483 NULL);
3484 }
3485
3486 if (sink->sdes && g_object_class_find_property (klass, "sdes")) {
3487 g_object_set (sink->rtpbin, "sdes", sink->sdes, NULL);
3488 }
3489
3490 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_NEW_MANAGER], 0,
3491 sink->rtpbin);
3492 }
3493
3494 ret = gst_element_set_state (rtpbin, GST_STATE_PAUSED);
3495 if (ret == GST_STATE_CHANGE_FAILURE)
3496 goto start_manager_failure;
3497
3498 return TRUE;
3499
3500 no_rtpbin:
3501 {
3502 GST_WARNING ("no rtpbin element");
3503 g_warning ("failed to create element 'rtpbin', check your installation");
3504 return FALSE;
3505 }
3506 start_manager_failure:
3507 {
3508 GST_DEBUG_OBJECT (sink, "could not start session manager");
3509 gst_bin_remove (GST_BIN_CAST (sink->internal_bin), rtpbin);
3510 return FALSE;
3511 }
3512 }
3513
3514 static GstElement *
request_aux_sender(GstElement * rtpbin,guint sessid,GstRTSPClientSink * sink)3515 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPClientSink * sink)
3516 {
3517 GstRTSPStream *stream = NULL;
3518 GstElement *ret = NULL;
3519 GList *walk;
3520
3521 GST_RTSP_STATE_LOCK (sink);
3522 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3523 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3524
3525 if (sessid == gst_rtsp_stream_get_index (context->stream)) {
3526 stream = context->stream;
3527 break;
3528 }
3529 }
3530
3531 if (stream != NULL) {
3532 GST_DEBUG_OBJECT (sink, "Creating aux sender for stream %u", sessid);
3533 ret = gst_rtsp_stream_request_aux_sender (stream, sessid);
3534 }
3535
3536 GST_RTSP_STATE_UNLOCK (sink);
3537
3538 return ret;
3539 }
3540
3541 static GstElement *
request_fec_encoder(GstElement * rtpbin,guint sessid,GstRTSPClientSink * sink)3542 request_fec_encoder (GstElement * rtpbin, guint sessid,
3543 GstRTSPClientSink * sink)
3544 {
3545 GstRTSPStream *stream = NULL;
3546 GstElement *ret = NULL;
3547 GList *walk;
3548
3549 GST_RTSP_STATE_LOCK (sink);
3550 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3551 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3552
3553 if (sessid == gst_rtsp_stream_get_index (context->stream)) {
3554 stream = context->stream;
3555 break;
3556 }
3557 }
3558
3559 if (stream != NULL) {
3560 ret = gst_rtsp_stream_request_ulpfec_encoder (stream, sessid);
3561 }
3562
3563 GST_RTSP_STATE_UNLOCK (sink);
3564
3565 return ret;
3566 }
3567
3568 static gboolean
gst_rtsp_client_sink_collect_streams(GstRTSPClientSink * sink)3569 gst_rtsp_client_sink_collect_streams (GstRTSPClientSink * sink)
3570 {
3571 GstRTSPStreamContext *context;
3572 GList *walk;
3573 const gchar *base;
3574 gboolean has_slash;
3575
3576 GST_DEBUG_OBJECT (sink, "Collecting stream information");
3577
3578 if (!gst_rtsp_client_sink_configure_manager (sink))
3579 return FALSE;
3580
3581 base = get_aggregate_control (sink);
3582 /* check if the base ends with / */
3583 has_slash = g_str_has_suffix (base, "/");
3584
3585 g_mutex_lock (&sink->preroll_lock);
3586 while (sink->contexts == NULL && !sink->conninfo.flushing) {
3587 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3588 }
3589 g_mutex_unlock (&sink->preroll_lock);
3590
3591 /* FIXME: Need different locking - need to protect against pad releases
3592 * and potential state changes ruining things here */
3593 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3594 GstPad *srcpad;
3595
3596 context = (GstRTSPStreamContext *) walk->data;
3597 if (context->stream)
3598 continue;
3599
3600 g_mutex_lock (&sink->preroll_lock);
3601 while (!context->prerolled && !sink->conninfo.flushing) {
3602 GST_DEBUG_OBJECT (sink, "Waiting for caps on stream %d", context->index);
3603 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3604 }
3605 if (sink->conninfo.flushing) {
3606 g_mutex_unlock (&sink->preroll_lock);
3607 break;
3608 }
3609 g_mutex_unlock (&sink->preroll_lock);
3610
3611 if (context->payloader == NULL)
3612 continue;
3613
3614 srcpad = gst_element_get_static_pad (context->payloader, "src");
3615
3616 GST_DEBUG_OBJECT (sink, "Creating stream object for stream %d",
3617 context->index);
3618 context->stream =
3619 gst_rtsp_client_sink_create_stream (sink, context, context->payloader,
3620 srcpad);
3621
3622 /* concatenate the two strings, insert / when not present */
3623 g_free (context->conninfo.location);
3624 context->conninfo.location =
3625 g_strdup_printf ("%s%sstream=%d", base, has_slash ? "" : "/",
3626 context->index);
3627
3628 if (sink->rtx_time > 0) {
3629 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
3630 g_signal_connect (sink->rtpbin, "request-aux-sender",
3631 (GCallback) request_aux_sender, sink);
3632 }
3633
3634 g_signal_connect (sink->rtpbin, "request-fec-encoder",
3635 (GCallback) request_fec_encoder, sink);
3636
3637 if (!gst_rtsp_stream_join_bin (context->stream,
3638 GST_BIN (sink->internal_bin), sink->rtpbin, GST_STATE_PAUSED)) {
3639 goto join_bin_failed;
3640 }
3641 context->joined = TRUE;
3642
3643 /* Block the stream, as it does not have any transport parts yet */
3644 gst_rtsp_stream_set_blocked (context->stream, TRUE);
3645
3646 /* Let the stream object receive data */
3647 gst_pad_remove_probe (srcpad, context->payloader_block_id);
3648
3649 gst_object_unref (srcpad);
3650 }
3651
3652 /* Now wait for the preroll of the rtp bin */
3653 g_mutex_lock (&sink->preroll_lock);
3654 while (!sink->prerolled && sink->conninfo.connection
3655 && !sink->conninfo.flushing) {
3656 GST_LOG_OBJECT (sink, "Waiting for preroll before continuing");
3657 g_cond_wait (&sink->preroll_cond, &sink->preroll_lock);
3658 }
3659 GST_LOG_OBJECT (sink, "Marking streams as collected");
3660 sink->streams_collected = TRUE;
3661 g_mutex_unlock (&sink->preroll_lock);
3662
3663 return TRUE;
3664
3665 join_bin_failed:
3666
3667 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
3668 ("Could not start stream %d", context->index));
3669 return FALSE;
3670 }
3671
3672 static GstRTSPResult
gst_rtsp_client_sink_create_transports_string(GstRTSPClientSink * sink,GstRTSPStreamContext * context,GSocketFamily family,GstRTSPLowerTrans protocols,GstRTSPProfile profiles,gchar ** transports)3673 gst_rtsp_client_sink_create_transports_string (GstRTSPClientSink * sink,
3674 GstRTSPStreamContext * context, GSocketFamily family,
3675 GstRTSPLowerTrans protocols, GstRTSPProfile profiles, gchar ** transports)
3676 {
3677 GString *result;
3678 GstRTSPStream *stream = context->stream;
3679 gboolean first = TRUE;
3680
3681 /* the default RTSP transports */
3682 result = g_string_new ("RTP");
3683
3684 while (profiles != 0) {
3685 if (!first)
3686 g_string_append (result, ",RTP");
3687
3688 if (profiles & GST_RTSP_PROFILE_SAVPF) {
3689 g_string_append (result, "/SAVPF");
3690 profiles &= ~GST_RTSP_PROFILE_SAVPF;
3691 } else if (profiles & GST_RTSP_PROFILE_SAVP) {
3692 g_string_append (result, "/SAVP");
3693 profiles &= ~GST_RTSP_PROFILE_SAVP;
3694 } else if (profiles & GST_RTSP_PROFILE_AVPF) {
3695 g_string_append (result, "/AVPF");
3696 profiles &= ~GST_RTSP_PROFILE_AVPF;
3697 } else if (profiles & GST_RTSP_PROFILE_AVP) {
3698 g_string_append (result, "/AVP");
3699 profiles &= ~GST_RTSP_PROFILE_AVP;
3700 } else {
3701 GST_WARNING_OBJECT (sink, "Unimplemented profile(s) 0x%x", profiles);
3702 break;
3703 }
3704
3705 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
3706 GstRTSPRange ports;
3707
3708 GST_DEBUG_OBJECT (sink, "adding UDP unicast");
3709 gst_rtsp_stream_get_server_port (stream, &ports, family);
3710
3711 g_string_append_printf (result, "/UDP;unicast;client_port=%d-%d",
3712 ports.min, ports.max);
3713 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3714 GstRTSPAddress *addr =
3715 gst_rtsp_stream_get_multicast_address (stream, family);
3716 if (addr) {
3717 GST_DEBUG_OBJECT (sink, "adding UDP multicast");
3718 g_string_append_printf (result, "/UDP;multicast;client_port=%d-%d",
3719 addr->port, addr->port + addr->n_ports - 1);
3720 gst_rtsp_address_free (addr);
3721 }
3722 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
3723 GST_DEBUG_OBJECT (sink, "adding TCP");
3724 g_string_append_printf (result, "/TCP;unicast;interleaved=%d-%d",
3725 sink->free_channel, sink->free_channel + 1);
3726 }
3727
3728 g_string_append (result, ";mode=RECORD");
3729 /* FIXME: Support appending too:
3730 if (sink->append)
3731 g_string_append (result, ";append");
3732 */
3733
3734 first = FALSE;
3735 }
3736
3737 if (first) {
3738 /* No valid transport could be constructed */
3739 GST_ERROR_OBJECT (sink, "No supported profiles configured");
3740 goto fail;
3741 }
3742
3743 *transports = g_string_free (result, FALSE);
3744
3745 GST_DEBUG_OBJECT (sink, "prepared transports %s", GST_STR_NULL (*transports));
3746
3747 return GST_RTSP_OK;
3748 fail:
3749 g_string_free (result, TRUE);
3750 return GST_RTSP_ERROR;
3751 }
3752
3753 static GstCaps *
signal_get_srtcp_params(GstRTSPClientSink * sink,GstRTSPStreamContext * context)3754 signal_get_srtcp_params (GstRTSPClientSink * sink,
3755 GstRTSPStreamContext * context)
3756 {
3757 GstCaps *caps = NULL;
3758
3759 g_signal_emit (sink, gst_rtsp_client_sink_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
3760 context->index, &caps);
3761
3762 if (caps != NULL)
3763 GST_DEBUG_OBJECT (sink, "SRTP parameters received");
3764
3765 return caps;
3766 }
3767
3768 static gchar *
gst_rtsp_client_sink_stream_make_keymgmt(GstRTSPClientSink * sink,GstRTSPStreamContext * context)3769 gst_rtsp_client_sink_stream_make_keymgmt (GstRTSPClientSink * sink,
3770 GstRTSPStreamContext * context)
3771 {
3772 gchar *base64, *result = NULL;
3773 GstMIKEYMessage *mikey_msg;
3774
3775 context->srtcpparams = signal_get_srtcp_params (sink, context);
3776 if (context->srtcpparams == NULL)
3777 context->srtcpparams = gst_rtsp_stream_get_caps (context->stream);
3778
3779 mikey_msg = gst_mikey_message_new_from_caps (context->srtcpparams);
3780 if (mikey_msg) {
3781 guint send_ssrc, send_rtx_ssrc;
3782 const GstStructure *s = gst_caps_get_structure (context->srtcpparams, 0);
3783
3784 /* add policy '0' for our SSRC */
3785 gst_rtsp_stream_get_ssrc (context->stream, &send_ssrc);
3786 GST_LOG_OBJECT (sink, "Stream %p ssrc %x", context->stream, send_ssrc);
3787 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_ssrc, 0);
3788
3789 if (gst_structure_get_uint (s, "rtx-ssrc", &send_rtx_ssrc))
3790 gst_mikey_message_add_cs_srtp (mikey_msg, 0, send_rtx_ssrc, 0);
3791
3792 base64 = gst_mikey_message_base64_encode (mikey_msg);
3793 gst_mikey_message_unref (mikey_msg);
3794
3795 if (base64) {
3796 result = gst_sdp_make_keymgmt (context->conninfo.location, base64);
3797 g_free (base64);
3798 }
3799 }
3800
3801 return result;
3802 }
3803
3804 /* masks to be kept in sync with the hardcoded protocol order of preference
3805 * in code below */
3806 static const guint protocol_masks[] = {
3807 GST_RTSP_LOWER_TRANS_UDP,
3808 GST_RTSP_LOWER_TRANS_UDP_MCAST,
3809 GST_RTSP_LOWER_TRANS_TCP,
3810 0
3811 };
3812
3813 /* Same for profile_masks */
3814 static const guint profile_masks[] = {
3815 GST_RTSP_PROFILE_SAVPF,
3816 GST_RTSP_PROFILE_SAVP,
3817 GST_RTSP_PROFILE_AVPF,
3818 GST_RTSP_PROFILE_AVP,
3819 0
3820 };
3821
3822 static gboolean
do_send_data(GstBuffer * buffer,guint8 channel,GstRTSPStreamContext * context)3823 do_send_data (GstBuffer * buffer, guint8 channel,
3824 GstRTSPStreamContext * context)
3825 {
3826 GstRTSPClientSink *sink = context->parent;
3827 GstRTSPMessage message = { 0 };
3828 GstRTSPResult res = GST_RTSP_OK;
3829 GstMapInfo map_info;
3830 guint8 *data;
3831 guint usize;
3832
3833 gst_rtsp_message_init_data (&message, channel);
3834
3835 /* FIXME, need some sort of iovec RTSPMessage here */
3836 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
3837 return FALSE;
3838
3839 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
3840
3841 res =
3842 gst_rtsp_client_sink_try_send (sink, &sink->conninfo, &message,
3843 NULL, NULL);
3844
3845 gst_rtsp_message_steal_body (&message, &data, &usize);
3846 gst_buffer_unmap (buffer, &map_info);
3847
3848 gst_rtsp_message_unset (&message);
3849
3850 gst_rtsp_stream_transport_message_sent (context->stream_transport);
3851
3852 return res == GST_RTSP_OK;
3853 }
3854
3855 static gboolean
do_send_data_list(GstBufferList * buffer_list,guint8 channel,GstRTSPStreamContext * context)3856 do_send_data_list (GstBufferList * buffer_list, guint8 channel,
3857 GstRTSPStreamContext * context)
3858 {
3859 gboolean ret = TRUE;
3860 guint i, n = gst_buffer_list_length (buffer_list);
3861
3862 /* TODO: Needs support for a) queueing up multiple messages on the
3863 * GstRTSPWatch in do_send_data() above and b) for one message having a body
3864 * consisting of multiple parts here */
3865 for (i = 0; i < n; i++) {
3866 GstBuffer *buffer = gst_buffer_list_get (buffer_list, i);
3867
3868 ret = do_send_data (buffer, channel, context);
3869 if (!ret)
3870 break;
3871 }
3872
3873 return ret;
3874 }
3875
3876 static GstRTSPResult
gst_rtsp_client_sink_setup_streams(GstRTSPClientSink * sink,gboolean async)3877 gst_rtsp_client_sink_setup_streams (GstRTSPClientSink * sink, gboolean async)
3878 {
3879 GstRTSPResult res = GST_RTSP_ERROR;
3880 GstRTSPMessage request = { 0 };
3881 GstRTSPMessage response = { 0 };
3882 GstRTSPLowerTrans protocols;
3883 GstRTSPStatusCode code;
3884 GSocketFamily family;
3885 GSocketAddress *sa;
3886 GSocket *conn_socket;
3887 GstRTSPUrl *url;
3888 GList *walk;
3889 gchar *hval;
3890
3891 if (sink->conninfo.connection) {
3892 url = gst_rtsp_connection_get_url (sink->conninfo.connection);
3893 /* we initially allow all configured lower transports. based on the URL
3894 * transports and the replies from the server we narrow them down. */
3895 protocols = url->transports & sink->cur_protocols;
3896 } else {
3897 url = NULL;
3898 protocols = sink->cur_protocols;
3899 }
3900
3901 if (protocols == 0)
3902 goto no_protocols;
3903
3904 GST_RTSP_STATE_LOCK (sink);
3905
3906 if (G_UNLIKELY (sink->contexts == NULL))
3907 goto no_streams;
3908
3909 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
3910 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
3911 GstRTSPStream *stream;
3912
3913 GstRTSPConnInfo *info;
3914 GstRTSPProfile profiles;
3915 GstRTSPProfile cur_profile;
3916 gchar *transports;
3917 gint retry = 0;
3918 guint profile_mask = 0;
3919 guint mask = 0;
3920 GstCaps *caps;
3921 const GstSDPMedia *media;
3922
3923 stream = context->stream;
3924 profiles = gst_rtsp_stream_get_profiles (stream);
3925
3926 caps = gst_rtsp_stream_get_caps (stream);
3927 if (caps == NULL) {
3928 GST_DEBUG_OBJECT (sink, "skipping stream %p, no caps", stream);
3929 continue;
3930 }
3931 gst_caps_unref (caps);
3932 media = gst_sdp_message_get_media (&sink->cursdp, context->sdp_index);
3933 if (media == NULL) {
3934 GST_DEBUG_OBJECT (sink, "skipping stream %p, no SDP info", stream);
3935 continue;
3936 }
3937
3938 /* skip setup if we have no URL for it */
3939 if (context->conninfo.location == NULL) {
3940 GST_DEBUG_OBJECT (sink, "skipping stream %p, no setup", stream);
3941 continue;
3942 }
3943
3944 if (sink->conninfo.connection == NULL) {
3945 if (!gst_rtsp_conninfo_connect (sink, &context->conninfo, async)) {
3946 GST_DEBUG_OBJECT (sink, "skipping stream %p, failed to connect",
3947 stream);
3948 continue;
3949 }
3950 info = &context->conninfo;
3951 } else {
3952 info = &sink->conninfo;
3953 }
3954 GST_DEBUG_OBJECT (sink, "doing setup of stream %p with %s", stream,
3955 context->conninfo.location);
3956
3957 conn_socket = gst_rtsp_connection_get_read_socket (info->connection);
3958 sa = g_socket_get_local_address (conn_socket, NULL);
3959 family = g_socket_address_get_family (sa);
3960 g_object_unref (sa);
3961
3962 next_protocol:
3963 /* first selectable profile */
3964 while (profile_masks[profile_mask]
3965 && !(profiles & profile_masks[profile_mask]))
3966 profile_mask++;
3967 if (!profile_masks[profile_mask])
3968 goto no_profiles;
3969
3970 /* first selectable protocol */
3971 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
3972 mask++;
3973 if (!protocol_masks[mask])
3974 goto no_protocols;
3975
3976 retry:
3977 GST_DEBUG_OBJECT (sink, "protocols = 0x%x, protocol mask = 0x%x", protocols,
3978 protocol_masks[mask]);
3979 /* create a string with first transport in line */
3980 transports = NULL;
3981 cur_profile = profiles & profile_masks[profile_mask];
3982 res = gst_rtsp_client_sink_create_transports_string (sink, context, family,
3983 protocols & protocol_masks[mask], cur_profile, &transports);
3984 if (res < 0 || transports == NULL)
3985 goto setup_transport_failed;
3986
3987 if (strlen (transports) == 0) {
3988 g_free (transports);
3989 GST_DEBUG_OBJECT (sink, "no transports found");
3990 mask++;
3991 profile_mask = 0;
3992 goto next_protocol;
3993 }
3994
3995 GST_DEBUG_OBJECT (sink, "transport is %s", GST_STR_NULL (transports));
3996
3997 /* create SETUP request */
3998 res =
3999 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_SETUP,
4000 context->conninfo.location);
4001 if (res < 0) {
4002 g_free (transports);
4003 goto create_request_failed;
4004 }
4005
4006 /* set up keys */
4007 if (cur_profile == GST_RTSP_PROFILE_SAVP ||
4008 cur_profile == GST_RTSP_PROFILE_SAVPF) {
4009 hval = gst_rtsp_client_sink_stream_make_keymgmt (sink, context);
4010 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
4011 }
4012
4013 /* if the user wants a non default RTP packet size we add the blocksize
4014 * parameter */
4015 if (sink->rtp_blocksize > 0) {
4016 hval = g_strdup_printf ("%d", sink->rtp_blocksize);
4017 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
4018 }
4019
4020 if (async)
4021 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request", ("SETUP stream %d",
4022 context->index));
4023
4024 {
4025 GstRTSPTransport *transport;
4026
4027 gst_rtsp_transport_new (&transport);
4028 if (gst_rtsp_transport_parse (transports, transport) != GST_RTSP_OK)
4029 goto parse_transport_failed;
4030 if (transport->lower_transport != GST_RTSP_LOWER_TRANS_TCP) {
4031 if (!gst_rtsp_stream_allocate_udp_sockets (stream, family, transport,
4032 FALSE)) {
4033 gst_rtsp_transport_free (transport);
4034 goto allocate_udp_ports_failed;
4035 }
4036 }
4037 if (!gst_rtsp_stream_complete_stream (stream, transport)) {
4038 gst_rtsp_transport_free (transport);
4039 goto complete_stream_failed;
4040 }
4041
4042 gst_rtsp_transport_free (transport);
4043 gst_rtsp_stream_set_blocked (stream, FALSE);
4044 }
4045
4046 /* FIXME:
4047 * the creation of the transports string depends on
4048 * calling stream_get_server_port, which only starts returning
4049 * something meaningful after a call to stream_allocate_udp_sockets
4050 * has been made, this function expects a transport that we parse
4051 * from the transport string ...
4052 *
4053 * Significant refactoring is in order, but does not look entirely
4054 * trivial, for now we put a band aid on and create a second transport
4055 * string after the stream has been completed, to pass it in
4056 * the request headers instead of the previous, incomplete one.
4057 */
4058 g_free (transports);
4059 transports = NULL;
4060 res = gst_rtsp_client_sink_create_transports_string (sink, context, family,
4061 protocols & protocol_masks[mask], cur_profile, &transports);
4062
4063 if (res < 0 || transports == NULL)
4064 goto setup_transport_failed;
4065
4066 /* select transport */
4067 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
4068
4069 /* handle the code ourselves */
4070 res = gst_rtsp_client_sink_send (sink, info, &request, &response, &code);
4071 if (res < 0)
4072 goto send_error;
4073
4074 switch (code) {
4075 case GST_RTSP_STS_OK:
4076 break;
4077 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
4078 gst_rtsp_message_unset (&request);
4079 gst_rtsp_message_unset (&response);
4080
4081 /* Try another profile. If no more, move to the next protocol */
4082 profile_mask++;
4083 while (profile_masks[profile_mask]
4084 && !(profiles & profile_masks[profile_mask]))
4085 profile_mask++;
4086 if (profile_masks[profile_mask])
4087 goto retry;
4088
4089 /* select next available protocol, give up on this stream if none */
4090 /* Reset profiles to try: */
4091 profile_mask = 0;
4092
4093 mask++;
4094 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
4095 mask++;
4096 if (!protocol_masks[mask])
4097 continue;
4098 else
4099 goto retry;
4100 default:
4101 goto response_error;
4102 }
4103
4104 /* parse response transport */
4105 {
4106 gchar *resptrans = NULL;
4107 GstRTSPTransport *transport;
4108
4109 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
4110 &resptrans, 0);
4111 if (!resptrans) {
4112 goto no_transport;
4113 }
4114
4115 gst_rtsp_transport_new (&transport);
4116
4117 /* parse transport, go to next stream on parse error */
4118 if (gst_rtsp_transport_parse (resptrans, transport) != GST_RTSP_OK) {
4119 GST_WARNING_OBJECT (sink, "failed to parse transport %s", resptrans);
4120 goto next;
4121 }
4122
4123 /* update allowed transports for other streams. once the transport of
4124 * one stream has been determined, we make sure that all other streams
4125 * are configured in the same way */
4126 switch (transport->lower_transport) {
4127 case GST_RTSP_LOWER_TRANS_TCP:
4128 GST_DEBUG_OBJECT (sink, "stream %p as TCP interleaved", stream);
4129 protocols = GST_RTSP_LOWER_TRANS_TCP;
4130 sink->interleaved = TRUE;
4131 /* update free channels */
4132 sink->free_channel =
4133 MAX (transport->interleaved.min, sink->free_channel);
4134 sink->free_channel =
4135 MAX (transport->interleaved.max, sink->free_channel);
4136 sink->free_channel++;
4137 break;
4138 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4139 /* only allow multicast for other streams */
4140 GST_DEBUG_OBJECT (sink, "stream %p as UDP multicast", stream);
4141 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
4142 break;
4143 case GST_RTSP_LOWER_TRANS_UDP:
4144 /* only allow unicast for other streams */
4145 GST_DEBUG_OBJECT (sink, "stream %p as UDP unicast", stream);
4146 protocols = GST_RTSP_LOWER_TRANS_UDP;
4147 /* Update transport with server destination if not provided by the server */
4148 if (transport->destination == NULL) {
4149 transport->destination = g_strdup (sink->server_ip);
4150 }
4151 break;
4152 default:
4153 GST_DEBUG_OBJECT (sink, "stream %p unknown transport %d", stream,
4154 transport->lower_transport);
4155 break;
4156 }
4157
4158 if (!retry) {
4159 GST_DEBUG ("Configuring the stream transport for stream %d",
4160 context->index);
4161 if (context->stream_transport == NULL)
4162 context->stream_transport =
4163 gst_rtsp_stream_transport_new (stream, transport);
4164 else
4165 gst_rtsp_stream_transport_set_transport (context->stream_transport,
4166 transport);
4167
4168 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
4169 /* our callbacks to send data on this TCP connection */
4170 gst_rtsp_stream_transport_set_callbacks (context->stream_transport,
4171 (GstRTSPSendFunc) do_send_data,
4172 (GstRTSPSendFunc) do_send_data, context, NULL);
4173 gst_rtsp_stream_transport_set_list_callbacks
4174 (context->stream_transport,
4175 (GstRTSPSendListFunc) do_send_data_list,
4176 (GstRTSPSendListFunc) do_send_data_list, context, NULL);
4177 }
4178
4179 /* The stream_transport now owns the transport */
4180 transport = NULL;
4181
4182 gst_rtsp_stream_transport_set_active (context->stream_transport, TRUE);
4183 }
4184 next:
4185 if (transport)
4186 gst_rtsp_transport_free (transport);
4187 /* clean up used RTSP messages */
4188 gst_rtsp_message_unset (&request);
4189 gst_rtsp_message_unset (&response);
4190 }
4191 }
4192 GST_RTSP_STATE_UNLOCK (sink);
4193
4194 /* store the transport protocol that was configured */
4195 sink->cur_protocols = protocols;
4196
4197 return res;
4198
4199 no_streams:
4200 {
4201 GST_RTSP_STATE_UNLOCK (sink);
4202 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4203 ("SDP contains no streams"));
4204 return GST_RTSP_ERROR;
4205 }
4206 setup_transport_failed:
4207 {
4208 GST_RTSP_STATE_UNLOCK (sink);
4209 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4210 ("Could not setup transport."));
4211 res = GST_RTSP_ERROR;
4212 goto cleanup_error;
4213 }
4214 no_profiles:
4215 {
4216 GST_RTSP_STATE_UNLOCK (sink);
4217 /* no transport possible, post an error and stop */
4218 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
4219 ("Could not connect to server, no profiles left"));
4220 return GST_RTSP_ERROR;
4221 }
4222 no_protocols:
4223 {
4224 GST_RTSP_STATE_UNLOCK (sink);
4225 /* no transport possible, post an error and stop */
4226 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
4227 ("Could not connect to server, no protocols left"));
4228 return GST_RTSP_ERROR;
4229 }
4230 no_transport:
4231 {
4232 GST_RTSP_STATE_UNLOCK (sink);
4233 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4234 ("Server did not select transport."));
4235 res = GST_RTSP_ERROR;
4236 goto cleanup_error;
4237 }
4238 create_request_failed:
4239 {
4240 gchar *str = gst_rtsp_strresult (res);
4241
4242 GST_RTSP_STATE_UNLOCK (sink);
4243 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4244 ("Could not create request. (%s)", str));
4245 g_free (str);
4246 goto cleanup_error;
4247 }
4248 parse_transport_failed:
4249 {
4250 GST_RTSP_STATE_UNLOCK (sink);
4251 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4252 ("Could not parse transport."));
4253 res = GST_RTSP_ERROR;
4254 goto cleanup_error;
4255 }
4256 allocate_udp_ports_failed:
4257 {
4258 GST_RTSP_STATE_UNLOCK (sink);
4259 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4260 ("Could not parse transport."));
4261 res = GST_RTSP_ERROR;
4262 goto cleanup_error;
4263 }
4264 complete_stream_failed:
4265 {
4266 GST_RTSP_STATE_UNLOCK (sink);
4267 GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
4268 ("Could not parse transport."));
4269 res = GST_RTSP_ERROR;
4270 goto cleanup_error;
4271 }
4272 send_error:
4273 {
4274 gchar *str = gst_rtsp_strresult (res);
4275
4276 GST_RTSP_STATE_UNLOCK (sink);
4277 if (res != GST_RTSP_EINTR) {
4278 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4279 ("Could not send message. (%s)", str));
4280 } else {
4281 GST_WARNING_OBJECT (sink, "send interrupted");
4282 }
4283 g_free (str);
4284 goto cleanup_error;
4285 }
4286 response_error:
4287 {
4288 const gchar *str = gst_rtsp_status_as_text (code);
4289
4290 GST_RTSP_STATE_UNLOCK (sink);
4291 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4292 ("Error (%d): %s", code, GST_STR_NULL (str)));
4293 res = GST_RTSP_ERROR;
4294 goto cleanup_error;
4295 }
4296 cleanup_error:
4297 {
4298 gst_rtsp_message_unset (&request);
4299 gst_rtsp_message_unset (&response);
4300 return res;
4301 }
4302 }
4303
4304 static GstRTSPResult
gst_rtsp_client_sink_ensure_open(GstRTSPClientSink * sink,gboolean async)4305 gst_rtsp_client_sink_ensure_open (GstRTSPClientSink * sink, gboolean async)
4306 {
4307 GstRTSPResult res = GST_RTSP_OK;
4308
4309 if (sink->state < GST_RTSP_STATE_READY) {
4310 res = GST_RTSP_ERROR;
4311 if (sink->open_error) {
4312 GST_DEBUG_OBJECT (sink, "the stream was in error");
4313 goto done;
4314 }
4315 if (async)
4316 gst_rtsp_client_sink_loop_start_cmd (sink, CMD_OPEN);
4317
4318 if ((res = gst_rtsp_client_sink_open (sink, async)) < 0) {
4319 GST_DEBUG_OBJECT (sink, "failed to open stream");
4320 goto done;
4321 }
4322 }
4323
4324 done:
4325 return res;
4326 }
4327
4328 static GstRTSPResult
gst_rtsp_client_sink_record(GstRTSPClientSink * sink,gboolean async)4329 gst_rtsp_client_sink_record (GstRTSPClientSink * sink, gboolean async)
4330 {
4331 GstRTSPMessage request = { 0 };
4332 GstRTSPMessage response = { 0 };
4333 GstRTSPResult res = GST_RTSP_OK;
4334 GstSDPMessage *sdp;
4335 guint sdp_index = 0;
4336 GstSDPInfo info = { 0, };
4337 gchar *keymgmt;
4338 guint i;
4339
4340 const gchar *proto;
4341 gchar *sess_id, *client_ip, *str;
4342 GSocketAddress *sa;
4343 GInetAddress *ia;
4344 GSocket *conn_socket;
4345 GList *walk;
4346
4347 g_mutex_lock (&sink->preroll_lock);
4348 if (sink->state == GST_RTSP_STATE_PLAYING) {
4349 /* Already recording, don't send another request */
4350 GST_LOG_OBJECT (sink, "Already in RECORD. Skipping duplicate request.");
4351 g_mutex_unlock (&sink->preroll_lock);
4352 goto done;
4353 }
4354 g_mutex_unlock (&sink->preroll_lock);
4355
4356 /* Collect all our input streams and create
4357 * stream objects before actually returning.
4358 * The streams are blocked at this point as we do not have any transport
4359 * parts yet. */
4360 gst_rtsp_client_sink_collect_streams (sink);
4361
4362 g_mutex_lock (&sink->block_streams_lock);
4363 /* Wait for streams to be blocked */
4364 while (sink->n_streams_blocked < g_list_length (sink->contexts)) {
4365 GST_DEBUG_OBJECT (sink, "waiting for streams to be blocked");
4366 g_cond_wait (&sink->block_streams_cond, &sink->block_streams_lock);
4367 }
4368 g_mutex_unlock (&sink->block_streams_lock);
4369
4370 /* Send announce, then setup for all streams */
4371 gst_sdp_message_init (&sink->cursdp);
4372 sdp = &sink->cursdp;
4373
4374 /* some standard things first */
4375 gst_sdp_message_set_version (sdp, "0");
4376
4377 /* session ID doesn't have to be super-unique in this case */
4378 sess_id = g_strdup_printf ("%u", g_random_int ());
4379
4380 if (sink->conninfo.connection == NULL)
4381 return GST_RTSP_ERROR;
4382
4383 conn_socket = gst_rtsp_connection_get_read_socket (sink->conninfo.connection);
4384
4385 sa = g_socket_get_local_address (conn_socket, NULL);
4386 ia = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (sa));
4387 client_ip = g_inet_address_to_string (ia);
4388 if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV6) {
4389 info.is_ipv6 = TRUE;
4390 proto = "IP6";
4391 } else if (g_socket_address_get_family (sa) == G_SOCKET_FAMILY_IPV4)
4392 proto = "IP4";
4393 else
4394 g_assert_not_reached ();
4395 g_object_unref (sa);
4396
4397 /* FIXME: Should this actually be the server's IP or ours? */
4398 info.server_ip = sink->server_ip;
4399
4400 gst_sdp_message_set_origin (sdp, "-", sess_id, "1", "IN", proto, client_ip);
4401
4402 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
4403 gst_sdp_message_set_information (sdp, "rtspclientsink");
4404 gst_sdp_message_add_time (sdp, "0", "0", NULL);
4405 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
4406
4407 /* add stream */
4408 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4409 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
4410
4411 gst_rtsp_sdp_from_stream (sdp, &info, context->stream);
4412 context->sdp_index = sdp_index++;
4413 }
4414
4415 g_free (sess_id);
4416 g_free (client_ip);
4417
4418 /* send ANNOUNCE request */
4419 GST_DEBUG_OBJECT (sink, "create ANNOUNCE request...");
4420 res =
4421 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_ANNOUNCE,
4422 sink->conninfo.url_str);
4423 if (res < 0)
4424 goto create_request_failed;
4425
4426 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
4427 "application/sdp");
4428
4429 /* add SDP to the request body */
4430 str = gst_sdp_message_as_text (sdp);
4431 gst_rtsp_message_take_body (&request, (guint8 *) str, strlen (str));
4432
4433 /* send ANNOUNCE */
4434 GST_DEBUG_OBJECT (sink, "sending announce...");
4435
4436 if (async)
4437 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record",
4438 ("Sending server stream info"));
4439
4440 if ((res =
4441 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4442 &response, NULL)) < 0)
4443 goto send_error;
4444
4445 /* parse the keymgmt */
4446 i = 0;
4447 walk = sink->contexts;
4448 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_KEYMGMT,
4449 &keymgmt, i++) == GST_RTSP_OK) {
4450 GstRTSPStreamContext *context = (GstRTSPStreamContext *) walk->data;
4451 walk = g_list_next (walk);
4452 if (!gst_rtsp_stream_handle_keymgmt (context->stream, keymgmt))
4453 goto keymgmt_error;
4454 }
4455
4456 /* send setup for all streams */
4457 if ((res = gst_rtsp_client_sink_setup_streams (sink, async)) < 0)
4458 goto setup_failed;
4459
4460 res = gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_RECORD,
4461 sink->conninfo.url_str);
4462
4463 if (res < 0)
4464 goto create_request_failed;
4465
4466 #if 0 /* FIXME: Configure a range based on input segments? */
4467 if (src->need_range) {
4468 hval = gen_range_header (src, segment);
4469
4470 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
4471 }
4472
4473 if (segment->rate != 1.0) {
4474 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
4475
4476 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
4477 if (src->skip)
4478 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
4479 else
4480 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
4481 }
4482 #endif
4483
4484 if (async)
4485 GST_ELEMENT_PROGRESS (sink, CONTINUE, "record", ("Starting recording"));
4486 if ((res =
4487 gst_rtsp_client_sink_send (sink, &sink->conninfo, &request,
4488 &response, NULL)) < 0)
4489 goto send_error;
4490
4491 #if 0 /* FIXME: Check if servers return these for record: */
4492 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
4493 * for the RTP packets. If this is not present, we assume all starts from 0...
4494 * This is info for the RTP session manager that we pass to it in caps. */
4495 hval_idx = 0;
4496 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
4497 &hval, hval_idx++) == GST_RTSP_OK)
4498 gst_rtspsrc_parse_rtpinfo (src, hval);
4499
4500 /* some servers indicate RTCP parameters in PLAY response,
4501 * rather than properly in SDP */
4502 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
4503 &hval, 0) == GST_RTSP_OK)
4504 gst_rtspsrc_handle_rtcp_interval (src, hval);
4505 #endif
4506
4507 gst_rtsp_client_sink_set_state (sink, GST_STATE_PLAYING);
4508 sink->state = GST_RTSP_STATE_PLAYING;
4509
4510 /* clean up any messages */
4511 gst_rtsp_message_unset (&request);
4512 gst_rtsp_message_unset (&response);
4513
4514 done:
4515 return res;
4516
4517 create_request_failed:
4518 {
4519 gchar *str = gst_rtsp_strresult (res);
4520
4521 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4522 ("Could not create request. (%s)", str));
4523 g_free (str);
4524 goto cleanup_error;
4525 }
4526 send_error:
4527 {
4528 /* Don't post a message - the rtsp_send method will have
4529 * taken care of it because we passed NULL for the response code */
4530 goto cleanup_error;
4531 }
4532 keymgmt_error:
4533 {
4534 GST_ELEMENT_ERROR (sink, STREAM, DECRYPT_NOKEY, (NULL),
4535 ("Could not handle KeyMgmt"));
4536 }
4537 setup_failed:
4538 {
4539 GST_ERROR_OBJECT (sink, "setup failed");
4540 goto cleanup_error;
4541 }
4542 cleanup_error:
4543 {
4544 if (sink->conninfo.connection) {
4545 GST_DEBUG_OBJECT (sink, "free connection");
4546 gst_rtsp_conninfo_close (sink, &sink->conninfo, TRUE);
4547 }
4548 gst_rtsp_message_unset (&request);
4549 gst_rtsp_message_unset (&response);
4550 return res;
4551 }
4552 }
4553
4554 static GstRTSPResult
gst_rtsp_client_sink_pause(GstRTSPClientSink * sink,gboolean async)4555 gst_rtsp_client_sink_pause (GstRTSPClientSink * sink, gboolean async)
4556 {
4557 GstRTSPResult res = GST_RTSP_OK;
4558 GstRTSPMessage request = { 0 };
4559 GstRTSPMessage response = { 0 };
4560 GList *walk;
4561 const gchar *control;
4562
4563 GST_DEBUG_OBJECT (sink, "PAUSE...");
4564
4565 if ((res = gst_rtsp_client_sink_ensure_open (sink, async)) < 0)
4566 goto open_failed;
4567
4568 if (!(sink->methods & GST_RTSP_PAUSE))
4569 goto not_supported;
4570
4571 if (sink->state == GST_RTSP_STATE_READY)
4572 goto was_paused;
4573
4574 if (!sink->conninfo.connection || !sink->conninfo.connected)
4575 goto no_connection;
4576
4577 /* construct a control url */
4578 control = get_aggregate_control (sink);
4579
4580 /* loop over the streams. We might exit the loop early when we could do an
4581 * aggregate control */
4582 for (walk = sink->contexts; walk; walk = g_list_next (walk)) {
4583 GstRTSPStreamContext *stream = (GstRTSPStreamContext *) walk->data;
4584 GstRTSPConnInfo *info;
4585 const gchar *setup_url;
4586
4587 /* try aggregate control first but do non-aggregate control otherwise */
4588 if (control)
4589 setup_url = control;
4590 else if ((setup_url = stream->conninfo.location) == NULL)
4591 continue;
4592
4593 if (sink->conninfo.connection) {
4594 info = &sink->conninfo;
4595 } else if (stream->conninfo.connection) {
4596 info = &stream->conninfo;
4597 } else {
4598 continue;
4599 }
4600
4601 if (async)
4602 GST_ELEMENT_PROGRESS (sink, CONTINUE, "request",
4603 ("Sending PAUSE request"));
4604
4605 if ((res =
4606 gst_rtsp_client_sink_init_request (sink, &request, GST_RTSP_PAUSE,
4607 setup_url)) < 0)
4608 goto create_request_failed;
4609
4610 if ((res =
4611 gst_rtsp_client_sink_send (sink, info, &request, &response,
4612 NULL)) < 0)
4613 goto send_error;
4614
4615 gst_rtsp_message_unset (&request);
4616 gst_rtsp_message_unset (&response);
4617
4618 /* exit early when we did agregate control */
4619 if (control)
4620 break;
4621 }
4622
4623 /* change element states now */
4624 gst_rtsp_client_sink_set_state (sink, GST_STATE_PAUSED);
4625
4626 no_connection:
4627 sink->state = GST_RTSP_STATE_READY;
4628
4629 done:
4630 if (async)
4631 gst_rtsp_client_sink_loop_end_cmd (sink, CMD_PAUSE, res);
4632
4633 return res;
4634
4635 /* ERRORS */
4636 open_failed:
4637 {
4638 GST_DEBUG_OBJECT (sink, "failed to open stream");
4639 goto done;
4640 }
4641 not_supported:
4642 {
4643 GST_DEBUG_OBJECT (sink, "PAUSE is not supported");
4644 goto done;
4645 }
4646 was_paused:
4647 {
4648 GST_DEBUG_OBJECT (sink, "we were already PAUSED");
4649 goto done;
4650 }
4651 create_request_failed:
4652 {
4653 gchar *str = gst_rtsp_strresult (res);
4654
4655 GST_ELEMENT_ERROR (sink, LIBRARY, INIT, (NULL),
4656 ("Could not create request. (%s)", str));
4657 g_free (str);
4658 goto done;
4659 }
4660 send_error:
4661 {
4662 gchar *str = gst_rtsp_strresult (res);
4663
4664 gst_rtsp_message_unset (&request);
4665 if (res != GST_RTSP_EINTR) {
4666 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
4667 ("Could not send message. (%s)", str));
4668 } else {
4669 GST_WARNING_OBJECT (sink, "PAUSE interrupted");
4670 }
4671 g_free (str);
4672 goto done;
4673 }
4674 }
4675
4676 static void
gst_rtsp_client_sink_handle_message(GstBin * bin,GstMessage * message)4677 gst_rtsp_client_sink_handle_message (GstBin * bin, GstMessage * message)
4678 {
4679 GstRTSPClientSink *rtsp_client_sink;
4680
4681 rtsp_client_sink = GST_RTSP_CLIENT_SINK (bin);
4682
4683 switch (GST_MESSAGE_TYPE (message)) {
4684 case GST_MESSAGE_ELEMENT:
4685 {
4686 const GstStructure *s = gst_message_get_structure (message);
4687
4688 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
4689 gboolean ignore_timeout;
4690
4691 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
4692
4693 GST_OBJECT_LOCK (rtsp_client_sink);
4694 ignore_timeout = rtsp_client_sink->ignore_timeout;
4695 rtsp_client_sink->ignore_timeout = TRUE;
4696 GST_OBJECT_UNLOCK (rtsp_client_sink);
4697
4698 /* we only act on the first udp timeout message, others are irrelevant
4699 * and can be ignored. */
4700 if (!ignore_timeout)
4701 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECONNECT,
4702 CMD_LOOP);
4703 /* eat and free */
4704 gst_message_unref (message);
4705 return;
4706 } else if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
4707 /* An RTSPStream has prerolled */
4708 GST_DEBUG_OBJECT (rtsp_client_sink, "received GstRTSPStreamBlocking");
4709 g_mutex_lock (&rtsp_client_sink->block_streams_lock);
4710 rtsp_client_sink->n_streams_blocked++;
4711 g_cond_broadcast (&rtsp_client_sink->block_streams_cond);
4712 g_mutex_unlock (&rtsp_client_sink->block_streams_lock);
4713 }
4714 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4715 break;
4716 }
4717 case GST_MESSAGE_ASYNC_START:{
4718 GstObject *sender;
4719
4720 sender = GST_MESSAGE_SRC (message);
4721
4722 GST_LOG_OBJECT (rtsp_client_sink,
4723 "Have async-start from %" GST_PTR_FORMAT, sender);
4724 if (sender == GST_OBJECT (rtsp_client_sink->internal_bin)) {
4725 GST_LOG_OBJECT (rtsp_client_sink, "child bin is now ASYNC");
4726 }
4727 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4728 break;
4729 }
4730 case GST_MESSAGE_ASYNC_DONE:
4731 {
4732 GstObject *sender;
4733 gboolean need_async_done;
4734
4735 sender = GST_MESSAGE_SRC (message);
4736 GST_LOG_OBJECT (rtsp_client_sink, "Have async-done from %" GST_PTR_FORMAT,
4737 sender);
4738
4739 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4740 if (sender == GST_OBJECT_CAST (rtsp_client_sink->internal_bin)) {
4741 GST_LOG_OBJECT (rtsp_client_sink, "child bin is no longer ASYNC");
4742 }
4743 need_async_done = rtsp_client_sink->in_async;
4744 if (rtsp_client_sink->in_async) {
4745 rtsp_client_sink->in_async = FALSE;
4746 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4747 }
4748 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4749
4750 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4751
4752 if (need_async_done) {
4753 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-DONE");
4754 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4755 gst_message_new_async_done (GST_OBJECT_CAST (rtsp_client_sink),
4756 GST_CLOCK_TIME_NONE));
4757 }
4758 break;
4759 }
4760 case GST_MESSAGE_ERROR:
4761 {
4762 GstObject *sender;
4763
4764 sender = GST_MESSAGE_SRC (message);
4765
4766 GST_DEBUG_OBJECT (rtsp_client_sink, "got error from %s",
4767 GST_ELEMENT_NAME (sender));
4768
4769 /* FIXME: Ignore errors on RTCP? */
4770 /* fatal but not our message, forward */
4771 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4772 break;
4773 }
4774 case GST_MESSAGE_STATE_CHANGED:
4775 {
4776 if (GST_MESSAGE_SRC (message) ==
4777 (GstObject *) rtsp_client_sink->internal_bin) {
4778 GstState newstate, pending;
4779 gst_message_parse_state_changed (message, NULL, &newstate, &pending);
4780 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4781 rtsp_client_sink->prerolled = (newstate >= GST_STATE_PAUSED)
4782 && pending == GST_STATE_VOID_PENDING;
4783 g_cond_broadcast (&rtsp_client_sink->preroll_cond);
4784 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4785 GST_DEBUG_OBJECT (bin,
4786 "Internal bin changed state to %s (pending %s). Prerolled now %d",
4787 gst_element_state_get_name (newstate),
4788 gst_element_state_get_name (pending), rtsp_client_sink->prerolled);
4789 }
4790 /* fallthrough */
4791 }
4792 default:
4793 {
4794 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
4795 break;
4796 }
4797 }
4798 }
4799
4800 /* the thread where everything happens */
4801 static void
gst_rtsp_client_sink_thread(GstRTSPClientSink * sink)4802 gst_rtsp_client_sink_thread (GstRTSPClientSink * sink)
4803 {
4804 gint cmd;
4805
4806 GST_OBJECT_LOCK (sink);
4807 cmd = sink->pending_cmd;
4808 if (cmd == CMD_RECONNECT || cmd == CMD_RECORD || cmd == CMD_PAUSE
4809 || cmd == CMD_LOOP || cmd == CMD_OPEN)
4810 sink->pending_cmd = CMD_LOOP;
4811 else
4812 sink->pending_cmd = CMD_WAIT;
4813 GST_DEBUG_OBJECT (sink, "got command %s", cmd_to_string (cmd));
4814
4815 /* we got the message command, so ensure communication is possible again */
4816 gst_rtsp_client_sink_connection_flush (sink, FALSE);
4817
4818 sink->busy_cmd = cmd;
4819 GST_OBJECT_UNLOCK (sink);
4820
4821 switch (cmd) {
4822 case CMD_OPEN:
4823 if (gst_rtsp_client_sink_open (sink, TRUE) == GST_RTSP_ERROR)
4824 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT,
4825 CMD_ALL & ~CMD_CLOSE);
4826 break;
4827 case CMD_RECORD:
4828 gst_rtsp_client_sink_record (sink, TRUE);
4829 break;
4830 case CMD_PAUSE:
4831 gst_rtsp_client_sink_pause (sink, TRUE);
4832 break;
4833 case CMD_CLOSE:
4834 gst_rtsp_client_sink_close (sink, TRUE, FALSE);
4835 break;
4836 case CMD_LOOP:
4837 gst_rtsp_client_sink_loop (sink);
4838 break;
4839 case CMD_RECONNECT:
4840 gst_rtsp_client_sink_reconnect (sink, FALSE);
4841 break;
4842 default:
4843 break;
4844 }
4845
4846 GST_OBJECT_LOCK (sink);
4847 /* and go back to sleep */
4848 if (sink->pending_cmd == CMD_WAIT) {
4849 if (sink->task)
4850 gst_task_pause (sink->task);
4851 }
4852 /* reset waiting */
4853 sink->busy_cmd = CMD_WAIT;
4854 GST_OBJECT_UNLOCK (sink);
4855 }
4856
4857 static gboolean
gst_rtsp_client_sink_start(GstRTSPClientSink * sink)4858 gst_rtsp_client_sink_start (GstRTSPClientSink * sink)
4859 {
4860 GST_DEBUG_OBJECT (sink, "starting");
4861
4862 sink->streams_collected = FALSE;
4863 gst_element_set_locked_state (GST_ELEMENT (sink->internal_bin), TRUE);
4864
4865 gst_rtsp_client_sink_set_state (sink, GST_STATE_READY);
4866
4867 GST_OBJECT_LOCK (sink);
4868 sink->pending_cmd = CMD_WAIT;
4869
4870 if (sink->task == NULL) {
4871 sink->task =
4872 gst_task_new ((GstTaskFunction) gst_rtsp_client_sink_thread, sink,
4873 NULL);
4874 if (sink->task == NULL)
4875 goto task_error;
4876
4877 gst_task_set_lock (sink->task, GST_RTSP_STREAM_GET_LOCK (sink));
4878 }
4879 GST_OBJECT_UNLOCK (sink);
4880
4881 return TRUE;
4882
4883 /* ERRORS */
4884 task_error:
4885 {
4886 GST_OBJECT_UNLOCK (sink);
4887 GST_ERROR_OBJECT (sink, "failed to create task");
4888 return FALSE;
4889 }
4890 }
4891
4892 static gboolean
gst_rtsp_client_sink_stop(GstRTSPClientSink * sink)4893 gst_rtsp_client_sink_stop (GstRTSPClientSink * sink)
4894 {
4895 GstTask *task;
4896
4897 GST_DEBUG_OBJECT (sink, "stopping");
4898
4899 /* also cancels pending task */
4900 gst_rtsp_client_sink_loop_send_cmd (sink, CMD_WAIT, CMD_ALL & ~CMD_CLOSE);
4901
4902 GST_OBJECT_LOCK (sink);
4903 if ((task = sink->task)) {
4904 sink->task = NULL;
4905 GST_OBJECT_UNLOCK (sink);
4906
4907 gst_task_stop (task);
4908
4909 /* make sure it is not running */
4910 GST_RTSP_STREAM_LOCK (sink);
4911 GST_RTSP_STREAM_UNLOCK (sink);
4912
4913 /* now wait for the task to finish */
4914 gst_task_join (task);
4915
4916 /* and free the task */
4917 gst_object_unref (GST_OBJECT (task));
4918
4919 GST_OBJECT_LOCK (sink);
4920 }
4921 GST_OBJECT_UNLOCK (sink);
4922
4923 /* ensure synchronously all is closed and clean */
4924 gst_rtsp_client_sink_close (sink, FALSE, TRUE);
4925
4926 return TRUE;
4927 }
4928
4929 static GstStateChangeReturn
gst_rtsp_client_sink_change_state(GstElement * element,GstStateChange transition)4930 gst_rtsp_client_sink_change_state (GstElement * element,
4931 GstStateChange transition)
4932 {
4933 GstRTSPClientSink *rtsp_client_sink;
4934 GstStateChangeReturn ret;
4935
4936 rtsp_client_sink = GST_RTSP_CLIENT_SINK (element);
4937
4938 switch (transition) {
4939 case GST_STATE_CHANGE_NULL_TO_READY:
4940 if (!gst_rtsp_client_sink_start (rtsp_client_sink))
4941 goto start_failed;
4942 break;
4943 case GST_STATE_CHANGE_READY_TO_PAUSED:
4944 /* init some state */
4945 rtsp_client_sink->cur_protocols = rtsp_client_sink->protocols;
4946 /* first attempt, don't ignore timeouts */
4947 rtsp_client_sink->ignore_timeout = FALSE;
4948 rtsp_client_sink->open_error = FALSE;
4949
4950 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_PAUSED);
4951
4952 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4953 if (rtsp_client_sink->in_async) {
4954 GST_DEBUG_OBJECT (rtsp_client_sink, "Posting ASYNC-START");
4955 gst_element_post_message (GST_ELEMENT_CAST (rtsp_client_sink),
4956 gst_message_new_async_start (GST_OBJECT_CAST (rtsp_client_sink)));
4957 }
4958 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4959
4960 break;
4961 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4962 /* fall-through */
4963 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4964 /* unblock the tcp tasks and make the loop waiting */
4965 if (gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_WAIT,
4966 CMD_LOOP)) {
4967 /* make sure it is waiting before we send PLAY below */
4968 GST_RTSP_STREAM_LOCK (rtsp_client_sink);
4969 GST_RTSP_STREAM_UNLOCK (rtsp_client_sink);
4970 }
4971 break;
4972 case GST_STATE_CHANGE_PAUSED_TO_READY:
4973 gst_rtsp_client_sink_set_state (rtsp_client_sink, GST_STATE_READY);
4974 break;
4975 default:
4976 break;
4977 }
4978
4979 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4980 if (ret == GST_STATE_CHANGE_FAILURE)
4981 goto done;
4982
4983 switch (transition) {
4984 case GST_STATE_CHANGE_NULL_TO_READY:
4985 ret = GST_STATE_CHANGE_SUCCESS;
4986 break;
4987 case GST_STATE_CHANGE_READY_TO_PAUSED:
4988 /* Return ASYNC and preroll input streams */
4989 g_mutex_lock (&rtsp_client_sink->preroll_lock);
4990 if (rtsp_client_sink->in_async)
4991 ret = GST_STATE_CHANGE_ASYNC;
4992 g_mutex_unlock (&rtsp_client_sink->preroll_lock);
4993 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_OPEN, 0);
4994
4995 /* CMD_OPEN has been scheduled. Wait until the sink thread starts
4996 * opening connection to the server */
4997 g_mutex_lock (&rtsp_client_sink->open_conn_lock);
4998 while (!rtsp_client_sink->open_conn_start) {
4999 GST_DEBUG_OBJECT (rtsp_client_sink,
5000 "wait for connection to be started");
5001 g_cond_wait (&rtsp_client_sink->open_conn_cond,
5002 &rtsp_client_sink->open_conn_lock);
5003 }
5004 rtsp_client_sink->open_conn_start = FALSE;
5005 g_mutex_unlock (&rtsp_client_sink->open_conn_lock);
5006 break;
5007 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:{
5008 GST_DEBUG_OBJECT (rtsp_client_sink,
5009 "Switching to playing -sending RECORD");
5010 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_RECORD, 0);
5011 ret = GST_STATE_CHANGE_SUCCESS;
5012 break;
5013 }
5014 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
5015 /* send pause request and keep the idle task around */
5016 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_PAUSE,
5017 CMD_LOOP);
5018 ret = GST_STATE_CHANGE_NO_PREROLL;
5019 break;
5020 case GST_STATE_CHANGE_PAUSED_TO_READY:
5021 gst_rtsp_client_sink_loop_send_cmd (rtsp_client_sink, CMD_CLOSE,
5022 CMD_PAUSE);
5023 ret = GST_STATE_CHANGE_SUCCESS;
5024 break;
5025 case GST_STATE_CHANGE_READY_TO_NULL:
5026 gst_rtsp_client_sink_stop (rtsp_client_sink);
5027 ret = GST_STATE_CHANGE_SUCCESS;
5028 break;
5029 default:
5030 break;
5031 }
5032
5033 done:
5034 return ret;
5035
5036 start_failed:
5037 {
5038 GST_DEBUG_OBJECT (rtsp_client_sink, "start failed");
5039 return GST_STATE_CHANGE_FAILURE;
5040 }
5041 }
5042
5043 /*** GSTURIHANDLER INTERFACE *************************************************/
5044
5045 static GstURIType
gst_rtsp_client_sink_uri_get_type(GType type)5046 gst_rtsp_client_sink_uri_get_type (GType type)
5047 {
5048 return GST_URI_SINK;
5049 }
5050
5051 static const gchar *const *
gst_rtsp_client_sink_uri_get_protocols(GType type)5052 gst_rtsp_client_sink_uri_get_protocols (GType type)
5053 {
5054 static const gchar *protocols[] =
5055 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
5056 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
5057 };
5058
5059 return protocols;
5060 }
5061
5062 static gchar *
gst_rtsp_client_sink_uri_get_uri(GstURIHandler * handler)5063 gst_rtsp_client_sink_uri_get_uri (GstURIHandler * handler)
5064 {
5065 GstRTSPClientSink *sink = GST_RTSP_CLIENT_SINK (handler);
5066
5067 /* FIXME: make thread-safe */
5068 return g_strdup (sink->conninfo.location);
5069 }
5070
5071 static gboolean
gst_rtsp_client_sink_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)5072 gst_rtsp_client_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
5073 GError ** error)
5074 {
5075 GstRTSPClientSink *sink;
5076 GstRTSPResult res;
5077 GstSDPResult sres;
5078 GstRTSPUrl *newurl = NULL;
5079 GstSDPMessage *sdp = NULL;
5080
5081 sink = GST_RTSP_CLIENT_SINK (handler);
5082
5083 /* same URI, we're fine */
5084 if (sink->conninfo.location && uri && !strcmp (uri, sink->conninfo.location))
5085 goto was_ok;
5086
5087 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
5088 sres = gst_sdp_message_new (&sdp);
5089 if (sres < 0)
5090 goto sdp_failed;
5091
5092 GST_DEBUG_OBJECT (sink, "parsing SDP message");
5093 sres = gst_sdp_message_parse_uri (uri, sdp);
5094 if (sres < 0)
5095 goto invalid_sdp;
5096 } else {
5097 /* try to parse */
5098 GST_DEBUG_OBJECT (sink, "parsing URI");
5099 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
5100 goto parse_error;
5101 }
5102
5103 /* if worked, free previous and store new url object along with the original
5104 * location. */
5105 GST_DEBUG_OBJECT (sink, "configuring URI");
5106 g_free (sink->conninfo.location);
5107 sink->conninfo.location = g_strdup (uri);
5108 gst_rtsp_url_free (sink->conninfo.url);
5109 sink->conninfo.url = newurl;
5110 g_free (sink->conninfo.url_str);
5111 if (newurl)
5112 sink->conninfo.url_str = gst_rtsp_url_get_request_uri (sink->conninfo.url);
5113 else
5114 sink->conninfo.url_str = NULL;
5115
5116 if (sink->uri_sdp)
5117 gst_sdp_message_free (sink->uri_sdp);
5118 sink->uri_sdp = sdp;
5119 sink->from_sdp = sdp != NULL;
5120
5121 GST_DEBUG_OBJECT (sink, "set uri: %s", GST_STR_NULL (uri));
5122 GST_DEBUG_OBJECT (sink, "request uri is: %s",
5123 GST_STR_NULL (sink->conninfo.url_str));
5124
5125 return TRUE;
5126
5127 /* Special cases */
5128 was_ok:
5129 {
5130 GST_DEBUG_OBJECT (sink, "URI was ok: '%s'", GST_STR_NULL (uri));
5131 return TRUE;
5132 }
5133 sdp_failed:
5134 {
5135 GST_ERROR_OBJECT (sink, "Could not create new SDP (%d)", sres);
5136 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
5137 "Could not create SDP");
5138 return FALSE;
5139 }
5140 invalid_sdp:
5141 {
5142 GST_ERROR_OBJECT (sink, "Not a valid SDP (%d) '%s'", sres,
5143 GST_STR_NULL (uri));
5144 gst_sdp_message_free (sdp);
5145 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
5146 "Invalid SDP");
5147 return FALSE;
5148 }
5149 parse_error:
5150 {
5151 GST_ERROR_OBJECT (sink, "Not a valid RTSP url '%s' (%d)",
5152 GST_STR_NULL (uri), res);
5153 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
5154 "Invalid RTSP URI");
5155 return FALSE;
5156 }
5157 }
5158
5159 static void
gst_rtsp_client_sink_uri_handler_init(gpointer g_iface,gpointer iface_data)5160 gst_rtsp_client_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
5161 {
5162 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
5163
5164 iface->get_type = gst_rtsp_client_sink_uri_get_type;
5165 iface->get_protocols = gst_rtsp_client_sink_uri_get_protocols;
5166 iface->get_uri = gst_rtsp_client_sink_uri_get_uri;
5167 iface->set_uri = gst_rtsp_client_sink_uri_set_uri;
5168 }
5169