1 /* GStreamer
2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20 /*
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
23 * LICENSE).
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 * SOFTWARE.
42 */
43 /**
44 * SECTION:element-rtspsrc
45 * @title: rtspsrc
46 *
47 * Makes a connection to an RTSP server and read the data.
48 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
49 * RealMedia/Quicktime/Microsoft extensions.
50 *
51 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
52 * default rtspsrc will negotiate a connection in the following order:
53 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
54 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 *
56 * rtspsrc currently understands SDP as the format of the session description.
57 * For each stream listed in the SDP a new rtp_stream\%d pad will be created
58 * with caps derived from the SDP media description. This is a caps of mime type
59 * "application/x-rtp" that can be connected to any available RTP depayloader
60 * element.
61 *
62 * rtspsrc will internally instantiate an RTP session manager element
63 * that will handle the RTCP messages to and from the server, jitter removal,
64 * packet reordering along with providing a clock for the pipeline.
65 * This feature is implemented using the gstrtpbin element.
66 *
67 * rtspsrc acts like a live source and will therefore only generate data in the
68 * PLAYING state.
69 *
70 * If a RTP session times out then the rtspsrc will generate an element message
71 * named "GstRTSPSrcTimeout". Currently this is only supported for timeouts
72 * triggered by RTCP.
73 *
74 * The message's structure contains three fields:
75 *
76 * #GstRTSPSrcTimeoutCause `cause`: the cause of the timeout.
77 *
78 * #gint `stream-number`: an internal identifier of the stream that timed out.
79 *
80 * #guint `ssrc`: the SSRC of the stream that timed out.
81 *
82 * ## Example launch line
83 * |[
84 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
85 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
86 * fakesink.
87 *
88 */
89
90 #ifdef HAVE_CONFIG_H
91 #include "config.h"
92 #endif
93
94 #ifdef HAVE_UNISTD_H
95 #include <unistd.h>
96 #endif /* HAVE_UNISTD_H */
97 #include <stdlib.h>
98 #include <string.h>
99 #include <stdio.h>
100 #include <stdarg.h>
101
102 #include <gst/net/gstnet.h>
103 #include <gst/sdp/gstsdpmessage.h>
104 #include <gst/sdp/gstmikey.h>
105 #include <gst/rtp/rtp.h>
106
107 #include "gst/gst-i18n-plugin.h"
108
109 #include "gstrtspsrc.h"
110
111 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
112 #define GST_CAT_DEFAULT (rtspsrc_debug)
113
114 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
115 GST_PAD_SRC,
116 GST_PAD_SOMETIMES,
117 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
118
119 /* templates used internally */
120 static GstStaticPadTemplate anysrctemplate =
121 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
122 GST_PAD_SRC,
123 GST_PAD_SOMETIMES,
124 GST_STATIC_CAPS_ANY);
125
126 static GstStaticPadTemplate anysinktemplate =
127 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
128 GST_PAD_SINK,
129 GST_PAD_SOMETIMES,
130 GST_STATIC_CAPS_ANY);
131
132 enum
133 {
134 SIGNAL_HANDLE_REQUEST,
135 SIGNAL_ON_SDP,
136 SIGNAL_SELECT_STREAM,
137 SIGNAL_NEW_MANAGER,
138 SIGNAL_REQUEST_RTCP_KEY,
139 SIGNAL_ACCEPT_CERTIFICATE,
140 SIGNAL_BEFORE_SEND,
141 SIGNAL_PUSH_BACKCHANNEL_BUFFER,
142 SIGNAL_GET_PARAMETER,
143 SIGNAL_GET_PARAMETERS,
144 SIGNAL_SET_PARAMETER,
145 LAST_SIGNAL
146 };
147
148 enum _GstRtspSrcRtcpSyncMode
149 {
150 RTCP_SYNC_ALWAYS,
151 RTCP_SYNC_INITIAL,
152 RTCP_SYNC_RTP
153 };
154
155 enum _GstRtspSrcBufferMode
156 {
157 BUFFER_MODE_NONE,
158 BUFFER_MODE_SLAVE,
159 BUFFER_MODE_BUFFER,
160 BUFFER_MODE_AUTO,
161 BUFFER_MODE_SYNCED
162 };
163
164 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
165 static GType
gst_rtsp_src_buffer_mode_get_type(void)166 gst_rtsp_src_buffer_mode_get_type (void)
167 {
168 static GType buffer_mode_type = 0;
169 static const GEnumValue buffer_modes[] = {
170 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
171 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
172 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
173 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
174 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
175 {0, NULL, NULL},
176 };
177
178 if (!buffer_mode_type) {
179 buffer_mode_type =
180 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
181 }
182 return buffer_mode_type;
183 }
184
185 enum _GstRtspSrcNtpTimeSource
186 {
187 NTP_TIME_SOURCE_NTP,
188 NTP_TIME_SOURCE_UNIX,
189 NTP_TIME_SOURCE_RUNNING_TIME,
190 NTP_TIME_SOURCE_CLOCK_TIME
191 };
192
193 #define DEBUG_RTSP(__self,msg) gst_rtspsrc_print_rtsp_message (__self, msg)
194 #define DEBUG_SDP(__self,msg) gst_rtspsrc_print_sdp_message (__self, msg)
195
196 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
197 static GType
gst_rtsp_src_ntp_time_source_get_type(void)198 gst_rtsp_src_ntp_time_source_get_type (void)
199 {
200 static GType ntp_time_source_type = 0;
201 static const GEnumValue ntp_time_source_values[] = {
202 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
203 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
204 {NTP_TIME_SOURCE_RUNNING_TIME,
205 "Running time based on pipeline clock",
206 "running-time"},
207 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
208 {0, NULL, NULL},
209 };
210
211 if (!ntp_time_source_type) {
212 ntp_time_source_type =
213 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
214 ntp_time_source_values);
215 }
216 return ntp_time_source_type;
217 }
218
219 enum _GstRtspBackchannel
220 {
221 BACKCHANNEL_NONE,
222 BACKCHANNEL_ONVIF
223 };
224
225 #define GST_TYPE_RTSP_BACKCHANNEL (gst_rtsp_backchannel_get_type())
226 static GType
gst_rtsp_backchannel_get_type(void)227 gst_rtsp_backchannel_get_type (void)
228 {
229 static GType backchannel_type = 0;
230 static const GEnumValue backchannel_values[] = {
231 {BACKCHANNEL_NONE, "No backchannel", "none"},
232 {BACKCHANNEL_ONVIF, "ONVIF audio backchannel", "onvif"},
233 {0, NULL, NULL},
234 };
235
236 if (G_UNLIKELY (backchannel_type == 0)) {
237 backchannel_type =
238 g_enum_register_static ("GstRTSPBackchannel", backchannel_values);
239 }
240 return backchannel_type;
241 }
242
243 #define BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL "www.onvif.org/ver20/backchannel"
244
245 #define DEFAULT_LOCATION NULL
246 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
247 #define DEFAULT_DEBUG FALSE
248 #define DEFAULT_RETRY 20
249 #define DEFAULT_TIMEOUT 5000000
250 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
251 #define DEFAULT_TCP_TIMEOUT 20000000
252 #define DEFAULT_LATENCY_MS 2000
253 #define DEFAULT_DROP_ON_LATENCY FALSE
254 #define DEFAULT_CONNECTION_SPEED 0
255 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
256 #define DEFAULT_DO_RTCP TRUE
257 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
258 #define DEFAULT_PROXY NULL
259 #define DEFAULT_RTP_BLOCKSIZE 0
260 #define DEFAULT_USER_ID NULL
261 #define DEFAULT_USER_PW NULL
262 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
263 #define DEFAULT_PORT_RANGE NULL
264 #define DEFAULT_SHORT_HEADER FALSE
265 #define DEFAULT_PROBATION 2
266 #define DEFAULT_UDP_RECONNECT TRUE
267 #define DEFAULT_MULTICAST_IFACE NULL
268 #define DEFAULT_NTP_SYNC FALSE
269 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
270 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
271 #define DEFAULT_TLS_DATABASE NULL
272 #define DEFAULT_TLS_INTERACTION NULL
273 #define DEFAULT_DO_RETRANSMISSION TRUE
274 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
275 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
276 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
277 #define DEFAULT_RFC7273_SYNC FALSE
278 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
279 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
280 #define DEFAULT_VERSION GST_RTSP_VERSION_1_0
281 #define DEFAULT_BACKCHANNEL GST_RTSP_BACKCHANNEL_NONE
282 #define DEFAULT_TEARDOWN_TIMEOUT (100 * GST_MSECOND)
283
284 enum
285 {
286 PROP_0,
287 PROP_LOCATION,
288 PROP_PROTOCOLS,
289 PROP_DEBUG,
290 PROP_RETRY,
291 PROP_TIMEOUT,
292 PROP_TCP_TIMEOUT,
293 PROP_LATENCY,
294 PROP_DROP_ON_LATENCY,
295 PROP_CONNECTION_SPEED,
296 PROP_NAT_METHOD,
297 PROP_DO_RTCP,
298 PROP_DO_RTSP_KEEP_ALIVE,
299 PROP_PROXY,
300 PROP_PROXY_ID,
301 PROP_PROXY_PW,
302 PROP_RTP_BLOCKSIZE,
303 PROP_USER_ID,
304 PROP_USER_PW,
305 PROP_BUFFER_MODE,
306 PROP_PORT_RANGE,
307 PROP_UDP_BUFFER_SIZE,
308 PROP_SHORT_HEADER,
309 PROP_PROBATION,
310 PROP_UDP_RECONNECT,
311 PROP_MULTICAST_IFACE,
312 PROP_NTP_SYNC,
313 PROP_USE_PIPELINE_CLOCK,
314 PROP_SDES,
315 PROP_TLS_VALIDATION_FLAGS,
316 PROP_TLS_DATABASE,
317 PROP_TLS_INTERACTION,
318 PROP_DO_RETRANSMISSION,
319 PROP_NTP_TIME_SOURCE,
320 PROP_USER_AGENT,
321 PROP_MAX_RTCP_RTP_TIME_DIFF,
322 PROP_RFC7273_SYNC,
323 PROP_MAX_TS_OFFSET_ADJUSTMENT,
324 PROP_MAX_TS_OFFSET,
325 PROP_DEFAULT_VERSION,
326 PROP_BACKCHANNEL,
327 PROP_TEARDOWN_TIMEOUT,
328 };
329
330 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
331 static GType
gst_rtsp_nat_method_get_type(void)332 gst_rtsp_nat_method_get_type (void)
333 {
334 static GType rtsp_nat_method_type = 0;
335 static const GEnumValue rtsp_nat_method[] = {
336 {GST_RTSP_NAT_NONE, "None", "none"},
337 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
338 {0, NULL, NULL},
339 };
340
341 if (!rtsp_nat_method_type) {
342 rtsp_nat_method_type =
343 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
344 }
345 return rtsp_nat_method_type;
346 }
347
348 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
349 do { \
350 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
351 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
352 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
353 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
354 } while (0)
355
356 typedef struct _ParameterRequest
357 {
358 gint cmd;
359 gchar *content_type;
360 GString *body;
361 GstPromise *promise;
362 } ParameterRequest;
363
364 static void gst_rtspsrc_finalize (GObject * object);
365
366 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
367 const GValue * value, GParamSpec * pspec);
368 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
369 GValue * value, GParamSpec * pspec);
370
371 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
372
373 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
374 gpointer iface_data);
375
376 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
377 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
378
379 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
380 GstStateChange transition);
381 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
382 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
383
384 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
385 GstRTSPMessage * response);
386
387 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
388 gint mask);
389 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
390 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
391
392 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
393 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
394 gboolean async, const gchar * seek_style);
395 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
396 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
397 gboolean only_close);
398
399 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
400 const gchar * uri, GError ** error);
401 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
402
403 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
404 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
405 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
406 GstRTSPStream * stream, GstEvent * event);
407 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
408 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
409 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
410 GstRTSPConnInfo * info, gboolean free);
411 static void
412 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg);
413 static void
414 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg);
415
416 static GstRTSPResult
417 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req);
418
419 static GstRTSPResult
420 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req);
421
422 static gboolean get_parameter (GstRTSPSrc * src, const gchar * parameter,
423 const gchar * content_type, GstPromise * promise);
424
425 static gboolean get_parameters (GstRTSPSrc * src, gchar ** parameters,
426 const gchar * content_type, GstPromise * promise);
427
428 static gboolean set_parameter (GstRTSPSrc * src, const gchar * name,
429 const gchar * value, const gchar * content_type, GstPromise * promise);
430
431 static GstFlowReturn gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src,
432 guint id, GstSample * sample);
433
434 typedef struct
435 {
436 guint8 pt;
437 GstCaps *caps;
438 } PtMapItem;
439
440 /* commands we send to out loop to notify it of events */
441 #define CMD_OPEN (1 << 0)
442 #define CMD_PLAY (1 << 1)
443 #define CMD_PAUSE (1 << 2)
444 #define CMD_CLOSE (1 << 3)
445 #define CMD_WAIT (1 << 4)
446 #define CMD_RECONNECT (1 << 5)
447 #define CMD_LOOP (1 << 6)
448 #define CMD_GET_PARAMETER (1 << 7)
449 #define CMD_SET_PARAMETER (1 << 8)
450
451 /* mask for all commands */
452 #define CMD_ALL ((CMD_SET_PARAMETER << 1) - 1)
453
454 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
455 G_STMT_START { \
456 gchar *__txt = _gst_element_error_printf text; \
457 gst_element_post_message (GST_ELEMENT_CAST (el), \
458 gst_message_new_progress (GST_OBJECT_CAST (el), \
459 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
460 g_free (__txt); \
461 } G_STMT_END
462
463 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
464
465 #define gst_rtspsrc_parent_class parent_class
466 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
467 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
468
469 #ifndef GST_DISABLE_GST_DEBUG
470 static inline const char *
cmd_to_string(guint cmd)471 cmd_to_string (guint cmd)
472 {
473 switch (cmd) {
474 case CMD_OPEN:
475 return "OPEN";
476 case CMD_PLAY:
477 return "PLAY";
478 case CMD_PAUSE:
479 return "PAUSE";
480 case CMD_CLOSE:
481 return "CLOSE";
482 case CMD_WAIT:
483 return "WAIT";
484 case CMD_RECONNECT:
485 return "RECONNECT";
486 case CMD_LOOP:
487 return "LOOP";
488 case CMD_GET_PARAMETER:
489 return "GET_PARAMETER";
490 case CMD_SET_PARAMETER:
491 return "SET_PARAMETER";
492 }
493
494 return "unknown";
495 }
496 #endif
497
498 static gboolean
default_select_stream(GstRTSPSrc * src,guint id,GstCaps * caps)499 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
500 {
501 GST_DEBUG_OBJECT (src, "default handler");
502 return TRUE;
503 }
504
505 static gboolean
select_stream_accum(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer data)506 select_stream_accum (GSignalInvocationHint * ihint,
507 GValue * return_accu, const GValue * handler_return, gpointer data)
508 {
509 gboolean myboolean;
510
511 myboolean = g_value_get_boolean (handler_return);
512 GST_DEBUG ("accum %d", myboolean);
513 g_value_set_boolean (return_accu, myboolean);
514
515 /* stop emission if FALSE */
516 return myboolean;
517 }
518
519 static gboolean
default_before_send(GstRTSPSrc * src,GstRTSPMessage * msg)520 default_before_send (GstRTSPSrc * src, GstRTSPMessage * msg)
521 {
522 GST_DEBUG_OBJECT (src, "default handler");
523 return TRUE;
524 }
525
526 static gboolean
before_send_accum(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer data)527 before_send_accum (GSignalInvocationHint * ihint,
528 GValue * return_accu, const GValue * handler_return, gpointer data)
529 {
530 gboolean myboolean;
531
532 myboolean = g_value_get_boolean (handler_return);
533 g_value_set_boolean (return_accu, myboolean);
534
535 /* prevent send if FALSE */
536 return myboolean;
537 }
538
539 static void
gst_rtspsrc_class_init(GstRTSPSrcClass * klass)540 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
541 {
542 GObjectClass *gobject_class;
543 GstElementClass *gstelement_class;
544 GstBinClass *gstbin_class;
545
546 gobject_class = (GObjectClass *) klass;
547 gstelement_class = (GstElementClass *) klass;
548 gstbin_class = (GstBinClass *) klass;
549
550 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
551
552 gobject_class->set_property = gst_rtspsrc_set_property;
553 gobject_class->get_property = gst_rtspsrc_get_property;
554
555 gobject_class->finalize = gst_rtspsrc_finalize;
556
557 g_object_class_install_property (gobject_class, PROP_LOCATION,
558 g_param_spec_string ("location", "RTSP Location",
559 "Location of the RTSP url to read",
560 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561
562 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
563 g_param_spec_flags ("protocols", "Protocols",
564 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
565 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566
567 g_object_class_install_property (gobject_class, PROP_DEBUG,
568 g_param_spec_boolean ("debug", "Debug",
569 "Dump request and response messages to stdout"
570 "(DEPRECATED: Printed all RTSP message to gstreamer log as 'log' level)",
571 DEFAULT_DEBUG,
572 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
573
574 g_object_class_install_property (gobject_class, PROP_RETRY,
575 g_param_spec_uint ("retry", "Retry",
576 "Max number of retries when allocating RTP ports.",
577 0, G_MAXUINT16, DEFAULT_RETRY,
578 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579
580 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
581 g_param_spec_uint64 ("timeout", "Timeout",
582 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
583 0, G_MAXUINT64, DEFAULT_TIMEOUT,
584 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
585
586 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
587 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
588 "Fail after timeout microseconds on TCP connections (0 = disabled)",
589 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
590 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
591
592 g_object_class_install_property (gobject_class, PROP_LATENCY,
593 g_param_spec_uint ("latency", "Buffer latency in ms",
594 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
595 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
596
597 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
598 g_param_spec_boolean ("drop-on-latency",
599 "Drop buffers when maximum latency is reached",
600 "Tells the jitterbuffer to never exceed the given latency in size",
601 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
602
603 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
604 g_param_spec_uint64 ("connection-speed", "Connection Speed",
605 "Network connection speed in kbps (0 = unknown)",
606 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
607 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
608
609 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
610 g_param_spec_enum ("nat-method", "NAT Method",
611 "Method to use for traversing firewalls and NAT",
612 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
613 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
614
615 /**
616 * GstRTSPSrc:do-rtcp:
617 *
618 * Enable RTCP support. Some old server don't like RTCP and then this property
619 * needs to be set to FALSE.
620 */
621 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
622 g_param_spec_boolean ("do-rtcp", "Do RTCP",
623 "Send RTCP packets, disable for old incompatible server.",
624 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
625
626 /**
627 * GstRTSPSrc:do-rtsp-keep-alive:
628 *
629 * Enable RTSP keep alive support. Some old server don't like RTSP
630 * keep alive and then this property needs to be set to FALSE.
631 */
632 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
633 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
634 "Send RTSP keep alive packets, disable for old incompatible server.",
635 DEFAULT_DO_RTSP_KEEP_ALIVE,
636 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
637
638 /**
639 * GstRTSPSrc:proxy:
640 *
641 * Set the proxy parameters. This has to be a string of the format
642 * [http://][user:passwd@]host[:port].
643 */
644 g_object_class_install_property (gobject_class, PROP_PROXY,
645 g_param_spec_string ("proxy", "Proxy",
646 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
647 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
648 /**
649 * GstRTSPSrc:proxy-id:
650 *
651 * Sets the proxy URI user id for authentication. If the URI set via the
652 * "proxy" property contains a user-id already, that will take precedence.
653 *
654 * Since: 1.2
655 */
656 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
657 g_param_spec_string ("proxy-id", "proxy-id",
658 "HTTP proxy URI user id for authentication", "",
659 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
660 /**
661 * GstRTSPSrc:proxy-pw:
662 *
663 * Sets the proxy URI password for authentication. If the URI set via the
664 * "proxy" property contains a password already, that will take precedence.
665 *
666 * Since: 1.2
667 */
668 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
669 g_param_spec_string ("proxy-pw", "proxy-pw",
670 "HTTP proxy URI user password for authentication", "",
671 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
672
673 /**
674 * GstRTSPSrc:rtp-blocksize:
675 *
676 * RTP package size to suggest to server.
677 */
678 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
679 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
680 "RTP package size to suggest to server (0 = disabled)",
681 0, 65536, DEFAULT_RTP_BLOCKSIZE,
682 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
683
684 g_object_class_install_property (gobject_class,
685 PROP_USER_ID,
686 g_param_spec_string ("user-id", "user-id",
687 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
688 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
689 g_object_class_install_property (gobject_class, PROP_USER_PW,
690 g_param_spec_string ("user-pw", "user-pw",
691 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
693
694 /**
695 * GstRTSPSrc:buffer-mode:
696 *
697 * Control the buffering and timestamping mode used by the jitterbuffer.
698 */
699 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
700 g_param_spec_enum ("buffer-mode", "Buffer Mode",
701 "Control the buffering algorithm in use",
702 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
704
705 /**
706 * GstRTSPSrc:port-range:
707 *
708 * Configure the client port numbers that can be used to receive RTP and
709 * RTCP.
710 */
711 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
712 g_param_spec_string ("port-range", "Port range",
713 "Client port range that can be used to receive RTP and RTCP data, "
714 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
715 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
716
717 /**
718 * GstRTSPSrc:udp-buffer-size:
719 *
720 * Size of the kernel UDP receive buffer in bytes.
721 */
722 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
723 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
724 "Size of the kernel UDP receive buffer in bytes, 0=default",
725 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
726 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
727
728 /**
729 * GstRTSPSrc:short-header:
730 *
731 * Only send the basic RTSP headers for broken encoders.
732 */
733 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
734 g_param_spec_boolean ("short-header", "Short Header",
735 "Only send the basic RTSP headers for broken encoders",
736 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
737
738 g_object_class_install_property (gobject_class, PROP_PROBATION,
739 g_param_spec_uint ("probation", "Number of probations",
740 "Consecutive packet sequence numbers to accept the source",
741 0, G_MAXUINT, DEFAULT_PROBATION,
742 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
743
744 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
745 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
746 "Reconnect to the server if RTSP connection is closed when doing UDP",
747 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
748
749 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
750 g_param_spec_string ("multicast-iface", "Multicast Interface",
751 "The network interface on which to join the multicast group",
752 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
753
754 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
755 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
756 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
757 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
758
759 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
760 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
761 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
762 "(DEPRECATED: Use ntp-time-source property)",
763 DEFAULT_USE_PIPELINE_CLOCK,
764 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
765
766 g_object_class_install_property (gobject_class, PROP_SDES,
767 g_param_spec_boxed ("sdes", "SDES",
768 "The SDES items of this session",
769 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
770
771 /**
772 * GstRTSPSrc::tls-validation-flags:
773 *
774 * TLS certificate validation flags used to validate server
775 * certificate.
776 *
777 * Since: 1.2.1
778 */
779 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
780 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
781 "TLS certificate validation flags used to validate the server certificate",
782 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
783 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
784
785 /**
786 * GstRTSPSrc::tls-database:
787 *
788 * TLS database with anchor certificate authorities used to validate
789 * the server certificate.
790 *
791 * Since: 1.4
792 */
793 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
794 g_param_spec_object ("tls-database", "TLS database",
795 "TLS database with anchor certificate authorities used to validate the server certificate",
796 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
797
798 /**
799 * GstRTSPSrc::tls-interaction:
800 *
801 * A #GTlsInteraction object to be used when the connection or certificate
802 * database need to interact with the user. This will be used to prompt the
803 * user for passwords where necessary.
804 *
805 * Since: 1.6
806 */
807 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
808 g_param_spec_object ("tls-interaction", "TLS interaction",
809 "A GTlsInteraction object to promt the user for password or certificate",
810 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
811
812 /**
813 * GstRTSPSrc::do-retransmission:
814 *
815 * Attempt to ask the server to retransmit lost packets according to RFC4588.
816 *
817 * Note: currently only works with SSRC-multiplexed retransmission streams
818 *
819 * Since: 1.6
820 */
821 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
822 g_param_spec_boolean ("do-retransmission", "Retransmission",
823 "Ask the server to retransmit lost packets",
824 DEFAULT_DO_RETRANSMISSION,
825 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
826
827 /**
828 * GstRTSPSrc::ntp-time-source:
829 *
830 * allows to select the time source that should be used
831 * for the NTP time in RTCP packets
832 *
833 * Since: 1.6
834 */
835 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
836 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
837 "NTP time source for RTCP packets",
838 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
839 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
840
841 /**
842 * GstRTSPSrc::user-agent:
843 *
844 * The string to set in the User-Agent header.
845 *
846 * Since: 1.6
847 */
848 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
849 g_param_spec_string ("user-agent", "User Agent",
850 "The User-Agent string to send to the server",
851 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
852
853 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
854 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
855 "Maximum amount of time in ms that the RTP time in RTCP SRs "
856 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
857 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
858 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
859
860 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
861 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
862 "Synchronize received streams to the RFC7273 clock "
863 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
864 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
865
866 /**
867 * GstRTSPSrc:default-rtsp-version:
868 *
869 * The preferred RTSP version to use while negotiating the version with the server.
870 *
871 * Since: 1.14
872 */
873 g_object_class_install_property (gobject_class, PROP_DEFAULT_VERSION,
874 g_param_spec_enum ("default-rtsp-version",
875 "The RTSP version to try first",
876 "The RTSP version that should be tried first when negotiating version.",
877 GST_TYPE_RTSP_VERSION, DEFAULT_VERSION,
878 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
879
880 /**
881 * GstRTSPSrc:max-ts-offset-adjustment:
882 *
883 * Syncing time stamps to NTP time adds a time offset. This parameter
884 * specifies the maximum number of nanoseconds per frame that this time offset
885 * may be adjusted with. This is used to avoid sudden large changes to time
886 * stamps.
887 */
888 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
889 g_param_spec_uint64 ("max-ts-offset-adjustment",
890 "Max Timestamp Offset Adjustment",
891 "The maximum number of nanoseconds per frame that time stamp offsets "
892 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
893 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
894 G_PARAM_STATIC_STRINGS));
895
896 /**
897 * GstRTSPSrc:max-ts-offset:
898 *
899 * Used to set an upper limit of how large a time offset may be. This
900 * is used to protect against unrealistic values as a result of either
901 * client,server or clock issues.
902 */
903 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
904 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
905 "The maximum absolute value of the time offset in (nanoseconds). "
906 "Note, if the ntp-sync parameter is set the default value is "
907 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
908 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
909
910 /**
911 * GstRTSPSrc:backchannel
912 *
913 * Select a type of backchannel to setup with the RTSP server.
914 * Default value is "none". Allowed values are "none" and "onvif".
915 *
916 * Since: 1.14
917 */
918 g_object_class_install_property (gobject_class, PROP_BACKCHANNEL,
919 g_param_spec_enum ("backchannel", "Backchannel type",
920 "The type of backchannel to setup. Default is 'none'.",
921 GST_TYPE_RTSP_BACKCHANNEL, BACKCHANNEL_NONE,
922 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
923
924 /**
925 * GstRtspSrc:teardown-timeout
926 *
927 * When transitioning PAUSED-READY, allow up to timeout (in nanoseconds)
928 * delay in order to send teardown (0 = disabled)
929 *
930 * Since: 1.14
931 */
932 g_object_class_install_property (gobject_class, PROP_TEARDOWN_TIMEOUT,
933 g_param_spec_uint64 ("teardown-timeout", "Teardown Timeout",
934 "When transitioning PAUSED-READY, allow up to timeout (in nanoseconds) "
935 "delay in order to send teardown (0 = disabled)",
936 0, G_MAXUINT64, DEFAULT_TEARDOWN_TIMEOUT,
937 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
938
939 /**
940 * GstRTSPSrc::handle-request:
941 * @rtspsrc: a #GstRTSPSrc
942 * @request: a #GstRTSPMessage
943 * @response: a #GstRTSPMessage
944 *
945 * Handle a server request in @request and prepare @response.
946 *
947 * This signal is called from the streaming thread, you should therefore not
948 * do any state changes on @rtspsrc because this might deadlock. If you want
949 * to modify the state as a result of this signal, post a
950 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
951 * in some other way.
952 *
953 * Since: 1.2
954 */
955 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
956 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
957 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
958 G_TYPE_POINTER, G_TYPE_POINTER);
959
960 /**
961 * GstRTSPSrc::on-sdp:
962 * @rtspsrc: a #GstRTSPSrc
963 * @sdp: a #GstSDPMessage
964 *
965 * Emitted when the client has retrieved the SDP and before it configures the
966 * streams in the SDP. @sdp can be inspected and modified.
967 *
968 * This signal is called from the streaming thread, you should therefore not
969 * do any state changes on @rtspsrc because this might deadlock. If you want
970 * to modify the state as a result of this signal, post a
971 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
972 * in some other way.
973 *
974 * Since: 1.2
975 */
976 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
977 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
978 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
979 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
980
981 /**
982 * GstRTSPSrc::select-stream:
983 * @rtspsrc: a #GstRTSPSrc
984 * @num: the stream number
985 * @caps: the stream caps
986 *
987 * Emitted before the client decides to configure the stream @num with
988 * @caps.
989 *
990 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
991 * is to be ignored.
992 *
993 * Since: 1.2
994 */
995 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
996 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
997 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
998 (GCallback) default_select_stream, select_stream_accum, NULL,
999 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
1000 GST_TYPE_CAPS);
1001 /**
1002 * GstRTSPSrc::new-manager:
1003 * @rtspsrc: a #GstRTSPSrc
1004 * @manager: a #GstElement
1005 *
1006 * Emitted after a new manager (like rtpbin) was created and the default
1007 * properties were configured.
1008 *
1009 * Since: 1.4
1010 */
1011 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
1012 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
1013 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
1014 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
1015
1016 /**
1017 * GstRTSPSrc::request-rtcp-key:
1018 * @rtspsrc: a #GstRTSPSrc
1019 * @num: the stream number
1020 *
1021 * Signal emitted to get the crypto parameters relevant to the RTCP
1022 * stream. User should provide the key and the RTCP encryption ciphers
1023 * and authentication, and return them wrapped in a GstCaps.
1024 *
1025 * Since: 1.4
1026 */
1027 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
1028 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
1029 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
1030
1031 /**
1032 * GstRTSPSrc::accept-certificate:
1033 * @rtspsrc: a #GstRTSPSrc
1034 * @peer_cert: the peer's #GTlsCertificate
1035 * @errors: the problems with @peer_cert
1036 * @user_data: user data set when the signal handler was connected.
1037 *
1038 * This will directly map to #GTlsConnection 's "accept-certificate"
1039 * signal and be performed after the default checks of #GstRTSPConnection
1040 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
1041 * have failed. If no #GTlsDatabase is set on this connection, only this
1042 * signal will be emitted.
1043 *
1044 * Since: 1.14
1045 */
1046 gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE] =
1047 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
1048 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
1049 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
1050 G_TYPE_TLS_CERTIFICATE_FLAGS);
1051
1052 /*
1053 * GstRTSPSrc::before-send
1054 * @rtspsrc: a #GstRTSPSrc
1055 * @num: the stream number
1056 *
1057 * Emitted before each RTSP request is sent, in order to allow
1058 * the application to modify send parameters or to skip the message entirely.
1059 * This can be used, for example, to work with ONVIF Profile G servers,
1060 * which need a different/additional range, rate-control, and intra/x
1061 * parameters.
1062 *
1063 * Returns: %TRUE when the command should be sent, %FALSE when the
1064 * command should be dropped.
1065 *
1066 * Since: 1.14
1067 */
1068 gst_rtspsrc_signals[SIGNAL_BEFORE_SEND] =
1069 g_signal_new_class_handler ("before-send", G_TYPE_FROM_CLASS (klass),
1070 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
1071 (GCallback) default_before_send, before_send_accum, NULL,
1072 g_cclosure_marshal_generic, G_TYPE_BOOLEAN,
1073 1, GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
1074
1075 /**
1076 * GstRTSPSrc::push-backchannel-buffer:
1077 * @rtspsrc: a #GstRTSPSrc
1078 * @buffer: RTP buffer to send back
1079 *
1080 *
1081 */
1082 gst_rtspsrc_signals[SIGNAL_PUSH_BACKCHANNEL_BUFFER] =
1083 g_signal_new ("push-backchannel-buffer", G_TYPE_FROM_CLASS (klass),
1084 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1085 push_backchannel_buffer), NULL, NULL, NULL, GST_TYPE_FLOW_RETURN, 2,
1086 G_TYPE_UINT, GST_TYPE_BUFFER);
1087
1088 /**
1089 * GstRTSPSrc::get-parameter:
1090 * @rtspsrc: a #GstRTSPSrc
1091 * @parameter: the parameter name
1092 * @parameter: the content type
1093 * @parameter: a pointer to #GstPromise
1094 *
1095 * Handle the GET_PARAMETER signal.
1096 *
1097 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1098 *
1099 */
1100 gst_rtspsrc_signals[SIGNAL_GET_PARAMETER] =
1101 g_signal_new ("get-parameter", G_TYPE_FROM_CLASS (klass),
1102 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1103 get_parameter), NULL, NULL, g_cclosure_marshal_generic,
1104 G_TYPE_BOOLEAN, 3, G_TYPE_STRING, G_TYPE_STRING, GST_TYPE_PROMISE);
1105
1106 /**
1107 * GstRTSPSrc::get-parameters:
1108 * @rtspsrc: a #GstRTSPSrc
1109 * @parameter: a NULL-terminated array of parameters
1110 * @parameter: the content type
1111 * @parameter: a pointer to #GstPromise
1112 *
1113 * Handle the GET_PARAMETERS signal.
1114 *
1115 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1116 *
1117 */
1118 gst_rtspsrc_signals[SIGNAL_GET_PARAMETERS] =
1119 g_signal_new ("get-parameters", G_TYPE_FROM_CLASS (klass),
1120 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1121 get_parameters), NULL, NULL, g_cclosure_marshal_generic,
1122 G_TYPE_BOOLEAN, 3, G_TYPE_STRV, G_TYPE_STRING, GST_TYPE_PROMISE);
1123
1124 /**
1125 * GstRTSPSrc::set-parameter:
1126 * @rtspsrc: a #GstRTSPSrc
1127 * @parameter: the parameter name
1128 * @parameter: the parameter value
1129 * @parameter: the content type
1130 * @parameter: a pointer to #GstPromise
1131 *
1132 * Handle the SET_PARAMETER signal.
1133 *
1134 * Returns: %TRUE when the command could be issued, %FALSE otherwise
1135 *
1136 */
1137 gst_rtspsrc_signals[SIGNAL_SET_PARAMETER] =
1138 g_signal_new ("set-parameter", G_TYPE_FROM_CLASS (klass),
1139 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1140 set_parameter), NULL, NULL, g_cclosure_marshal_generic,
1141 G_TYPE_BOOLEAN, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
1142 GST_TYPE_PROMISE);
1143
1144 gstelement_class->send_event = gst_rtspsrc_send_event;
1145 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
1146 gstelement_class->change_state = gst_rtspsrc_change_state;
1147
1148 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
1149
1150 gst_element_class_set_static_metadata (gstelement_class,
1151 "RTSP packet receiver", "Source/Network",
1152 "Receive data over the network via RTSP (RFC 2326)",
1153 "Wim Taymans <wim@fluendo.com>, "
1154 "Thijs Vermeir <thijs.vermeir@barco.com>, "
1155 "Lutz Mueller <lutz@topfrose.de>");
1156
1157 gstbin_class->handle_message = gst_rtspsrc_handle_message;
1158
1159 klass->push_backchannel_buffer = gst_rtspsrc_push_backchannel_buffer;
1160 klass->get_parameter = GST_DEBUG_FUNCPTR (get_parameter);
1161 klass->get_parameters = GST_DEBUG_FUNCPTR (get_parameters);
1162 klass->set_parameter = GST_DEBUG_FUNCPTR (set_parameter);
1163
1164 gst_rtsp_ext_list_init ();
1165 }
1166
1167 static gboolean
validate_set_get_parameter_name(const gchar * parameter_name)1168 validate_set_get_parameter_name (const gchar * parameter_name)
1169 {
1170 gchar *ptr = (gchar *) parameter_name;
1171
1172 while (*ptr) {
1173 /* Don't allow '\r', '\n', \'t', ' ' etc in the parameter name */
1174 if (g_ascii_isspace (*ptr) || g_ascii_iscntrl (*ptr)) {
1175 GST_DEBUG ("invalid parameter name '%s'", parameter_name);
1176 return FALSE;
1177 }
1178 ptr++;
1179 }
1180 return TRUE;
1181 }
1182
1183 static gboolean
validate_set_get_parameters(gchar ** parameter_names)1184 validate_set_get_parameters (gchar ** parameter_names)
1185 {
1186 while (*parameter_names) {
1187 if (!validate_set_get_parameter_name (*parameter_names)) {
1188 return FALSE;
1189 }
1190 parameter_names++;
1191 }
1192 return TRUE;
1193 }
1194
1195 static gboolean
get_parameter(GstRTSPSrc * src,const gchar * parameter,const gchar * content_type,GstPromise * promise)1196 get_parameter (GstRTSPSrc * src, const gchar * parameter,
1197 const gchar * content_type, GstPromise * promise)
1198 {
1199 gchar *parameters[] = { (gchar *) parameter, NULL };
1200
1201 GST_LOG_OBJECT (src, "get_parameter: %s", GST_STR_NULL (parameter));
1202
1203 if (parameter == NULL || parameter[0] == '\0' || promise == NULL) {
1204 GST_DEBUG ("invalid input");
1205 return FALSE;
1206 }
1207
1208 return get_parameters (src, parameters, content_type, promise);
1209 }
1210
1211 static gboolean
get_parameters(GstRTSPSrc * src,gchar ** parameters,const gchar * content_type,GstPromise * promise)1212 get_parameters (GstRTSPSrc * src, gchar ** parameters,
1213 const gchar * content_type, GstPromise * promise)
1214 {
1215 ParameterRequest *req;
1216
1217 GST_LOG_OBJECT (src, "get_parameters: %d", g_strv_length (parameters));
1218
1219 if (parameters == NULL || promise == NULL) {
1220 GST_DEBUG ("invalid input");
1221 return FALSE;
1222 }
1223
1224 if (src->state == GST_RTSP_STATE_INVALID) {
1225 GST_DEBUG ("invalid state");
1226 return FALSE;
1227 }
1228
1229 if (!validate_set_get_parameters (parameters)) {
1230 return FALSE;
1231 }
1232
1233 req = g_new0 (ParameterRequest, 1);
1234 req->promise = gst_promise_ref (promise);
1235 req->cmd = CMD_GET_PARAMETER;
1236 /* Set the request body according to RFC 2326 or RFC 7826 */
1237 req->body = g_string_new (NULL);
1238 while (*parameters) {
1239 g_string_append_printf (req->body, "%s:\r\n", *parameters);
1240 parameters++;
1241 }
1242 if (content_type)
1243 req->content_type = g_strdup (content_type);
1244
1245 GST_OBJECT_LOCK (src);
1246 g_queue_push_tail (&src->set_get_param_q, req);
1247 GST_OBJECT_UNLOCK (src);
1248
1249 gst_rtspsrc_loop_send_cmd (src, CMD_GET_PARAMETER, CMD_LOOP);
1250
1251 return TRUE;
1252 }
1253
1254 static gboolean
set_parameter(GstRTSPSrc * src,const gchar * name,const gchar * value,const gchar * content_type,GstPromise * promise)1255 set_parameter (GstRTSPSrc * src, const gchar * name, const gchar * value,
1256 const gchar * content_type, GstPromise * promise)
1257 {
1258 ParameterRequest *req;
1259
1260 GST_LOG_OBJECT (src, "set_parameter: %s: %s", GST_STR_NULL (name),
1261 GST_STR_NULL (value));
1262
1263 if (name == NULL || name[0] == '\0' || value == NULL || promise == NULL) {
1264 GST_DEBUG ("invalid input");
1265 return FALSE;
1266 }
1267
1268 if (src->state == GST_RTSP_STATE_INVALID) {
1269 GST_DEBUG ("invalid state");
1270 return FALSE;
1271 }
1272
1273 if (!validate_set_get_parameter_name (name)) {
1274 return FALSE;
1275 }
1276
1277 req = g_new0 (ParameterRequest, 1);
1278 req->cmd = CMD_SET_PARAMETER;
1279 req->promise = gst_promise_ref (promise);
1280 req->body = g_string_new (NULL);
1281 /* Set the request body according to RFC 2326 or RFC 7826 */
1282 g_string_append_printf (req->body, "%s: %s\r\n", name, value);
1283 if (content_type)
1284 req->content_type = g_strdup (content_type);
1285
1286 GST_OBJECT_LOCK (src);
1287 g_queue_push_tail (&src->set_get_param_q, req);
1288 GST_OBJECT_UNLOCK (src);
1289
1290 gst_rtspsrc_loop_send_cmd (src, CMD_SET_PARAMETER, CMD_LOOP);
1291
1292 return TRUE;
1293 }
1294
1295 static void
gst_rtspsrc_init(GstRTSPSrc * src)1296 gst_rtspsrc_init (GstRTSPSrc * src)
1297 {
1298 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
1299 src->protocols = DEFAULT_PROTOCOLS;
1300 src->debug = DEFAULT_DEBUG;
1301 src->retry = DEFAULT_RETRY;
1302 src->udp_timeout = DEFAULT_TIMEOUT;
1303 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
1304 src->latency = DEFAULT_LATENCY_MS;
1305 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
1306 src->connection_speed = DEFAULT_CONNECTION_SPEED;
1307 src->nat_method = DEFAULT_NAT_METHOD;
1308 src->do_rtcp = DEFAULT_DO_RTCP;
1309 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
1310 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
1311 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
1312 src->user_id = g_strdup (DEFAULT_USER_ID);
1313 src->user_pw = g_strdup (DEFAULT_USER_PW);
1314 src->buffer_mode = DEFAULT_BUFFER_MODE;
1315 src->client_port_range.min = 0;
1316 src->client_port_range.max = 0;
1317 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
1318 src->short_header = DEFAULT_SHORT_HEADER;
1319 src->probation = DEFAULT_PROBATION;
1320 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
1321 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1322 src->ntp_sync = DEFAULT_NTP_SYNC;
1323 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1324 src->sdes = NULL;
1325 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
1326 src->tls_database = DEFAULT_TLS_DATABASE;
1327 src->tls_interaction = DEFAULT_TLS_INTERACTION;
1328 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
1329 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
1330 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
1331 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
1332 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
1333 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
1334 src->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1335 src->max_ts_offset_is_set = FALSE;
1336 src->default_version = DEFAULT_VERSION;
1337 src->version = GST_RTSP_VERSION_INVALID;
1338 src->teardown_timeout = DEFAULT_TEARDOWN_TIMEOUT;
1339
1340 /* get a list of all extensions */
1341 src->extensions = gst_rtsp_ext_list_get ();
1342
1343 /* connect to send signal */
1344 gst_rtsp_ext_list_connect (src->extensions, "send",
1345 (GCallback) gst_rtspsrc_send_cb, src);
1346
1347 /* protects the streaming thread in interleaved mode or the polling
1348 * thread in UDP mode. */
1349 g_rec_mutex_init (&src->stream_rec_lock);
1350
1351 /* protects our state changes from multiple invocations */
1352 g_rec_mutex_init (&src->state_rec_lock);
1353
1354 g_queue_init (&src->set_get_param_q);
1355
1356 src->state = GST_RTSP_STATE_INVALID;
1357
1358 g_mutex_init (&src->conninfo.send_lock);
1359 g_mutex_init (&src->conninfo.recv_lock);
1360 g_cond_init (&src->cmd_cond);
1361
1362 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
1363 gst_bin_set_suppressed_flags (GST_BIN (src),
1364 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
1365 }
1366
1367 static void
free_param_data(ParameterRequest * req)1368 free_param_data (ParameterRequest * req)
1369 {
1370 gst_promise_unref (req->promise);
1371 if (req->body)
1372 g_string_free (req->body, TRUE);
1373 g_free (req->content_type);
1374 g_free (req);
1375 }
1376
1377 static void
free_param_queue(gpointer data)1378 free_param_queue (gpointer data)
1379 {
1380 ParameterRequest *req = data;
1381
1382 gst_promise_expire (req->promise);
1383 free_param_data (req);
1384 }
1385
1386 static void
gst_rtspsrc_finalize(GObject * object)1387 gst_rtspsrc_finalize (GObject * object)
1388 {
1389 GstRTSPSrc *rtspsrc;
1390
1391 rtspsrc = GST_RTSPSRC (object);
1392
1393 gst_rtsp_ext_list_free (rtspsrc->extensions);
1394 g_free (rtspsrc->conninfo.location);
1395 gst_rtsp_url_free (rtspsrc->conninfo.url);
1396 g_free (rtspsrc->conninfo.url_str);
1397 g_free (rtspsrc->user_id);
1398 g_free (rtspsrc->user_pw);
1399 g_free (rtspsrc->multi_iface);
1400 g_free (rtspsrc->user_agent);
1401
1402 if (rtspsrc->sdp) {
1403 gst_sdp_message_free (rtspsrc->sdp);
1404 rtspsrc->sdp = NULL;
1405 }
1406 if (rtspsrc->provided_clock)
1407 gst_object_unref (rtspsrc->provided_clock);
1408
1409 if (rtspsrc->sdes)
1410 gst_structure_free (rtspsrc->sdes);
1411
1412 if (rtspsrc->tls_database)
1413 g_object_unref (rtspsrc->tls_database);
1414
1415 if (rtspsrc->tls_interaction)
1416 g_object_unref (rtspsrc->tls_interaction);
1417
1418 /* free locks */
1419 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1420 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1421
1422 g_mutex_clear (&rtspsrc->conninfo.send_lock);
1423 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
1424 g_cond_clear (&rtspsrc->cmd_cond);
1425
1426 G_OBJECT_CLASS (parent_class)->finalize (object);
1427 }
1428
1429 static GstClock *
gst_rtspsrc_provide_clock(GstElement * element)1430 gst_rtspsrc_provide_clock (GstElement * element)
1431 {
1432 GstRTSPSrc *src = GST_RTSPSRC (element);
1433 GstClock *clock;
1434
1435 if ((clock = src->provided_clock) != NULL)
1436 return gst_object_ref (clock);
1437
1438 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
1439 }
1440
1441 /* a proxy string of the format [user:passwd@]host[:port] */
1442 static gboolean
gst_rtspsrc_set_proxy(GstRTSPSrc * rtsp,const gchar * proxy)1443 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1444 {
1445 gchar *p, *at, *col;
1446
1447 g_free (rtsp->proxy_user);
1448 rtsp->proxy_user = NULL;
1449 g_free (rtsp->proxy_passwd);
1450 rtsp->proxy_passwd = NULL;
1451 g_free (rtsp->proxy_host);
1452 rtsp->proxy_host = NULL;
1453 rtsp->proxy_port = 0;
1454
1455 p = (gchar *) proxy;
1456
1457 if (p == NULL)
1458 return TRUE;
1459
1460 /* we allow http:// in front but ignore it */
1461 if (g_str_has_prefix (p, "http://"))
1462 p += 7;
1463
1464 at = strchr (p, '@');
1465 if (at) {
1466 /* look for user:passwd */
1467 col = strchr (proxy, ':');
1468 if (col == NULL || col > at)
1469 return FALSE;
1470
1471 rtsp->proxy_user = g_strndup (p, col - p);
1472 col++;
1473 rtsp->proxy_passwd = g_strndup (col, at - col);
1474
1475 /* move to host */
1476 p = at + 1;
1477 } else {
1478 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1479 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1480 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1481 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1482 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1483 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1484 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1485 }
1486 }
1487 col = strchr (p, ':');
1488
1489 if (col) {
1490 /* everything before the colon is the hostname */
1491 rtsp->proxy_host = g_strndup (p, col - p);
1492 p = col + 1;
1493 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1494 } else {
1495 rtsp->proxy_host = g_strdup (p);
1496 rtsp->proxy_port = 8080;
1497 }
1498 return TRUE;
1499 }
1500
1501 static void
gst_rtspsrc_set_tcp_timeout(GstRTSPSrc * rtspsrc,guint64 timeout)1502 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1503 {
1504 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1505 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1506
1507 if (timeout != 0)
1508 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1509 else
1510 rtspsrc->ptcp_timeout = NULL;
1511 }
1512
1513 static void
gst_rtspsrc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1514 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1515 GParamSpec * pspec)
1516 {
1517 GstRTSPSrc *rtspsrc;
1518
1519 rtspsrc = GST_RTSPSRC (object);
1520
1521 switch (prop_id) {
1522 case PROP_LOCATION:
1523 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1524 g_value_get_string (value), NULL);
1525 break;
1526 case PROP_PROTOCOLS:
1527 rtspsrc->protocols = g_value_get_flags (value);
1528 break;
1529 case PROP_DEBUG:
1530 rtspsrc->debug = g_value_get_boolean (value);
1531 break;
1532 case PROP_RETRY:
1533 rtspsrc->retry = g_value_get_uint (value);
1534 break;
1535 case PROP_TIMEOUT:
1536 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1537 break;
1538 case PROP_TCP_TIMEOUT:
1539 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1540 break;
1541 case PROP_LATENCY:
1542 rtspsrc->latency = g_value_get_uint (value);
1543 break;
1544 case PROP_DROP_ON_LATENCY:
1545 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1546 break;
1547 case PROP_CONNECTION_SPEED:
1548 rtspsrc->connection_speed = g_value_get_uint64 (value);
1549 break;
1550 case PROP_NAT_METHOD:
1551 rtspsrc->nat_method = g_value_get_enum (value);
1552 break;
1553 case PROP_DO_RTCP:
1554 rtspsrc->do_rtcp = g_value_get_boolean (value);
1555 break;
1556 case PROP_DO_RTSP_KEEP_ALIVE:
1557 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1558 break;
1559 case PROP_PROXY:
1560 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1561 break;
1562 case PROP_PROXY_ID:
1563 g_free (rtspsrc->prop_proxy_id);
1564 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1565 break;
1566 case PROP_PROXY_PW:
1567 g_free (rtspsrc->prop_proxy_pw);
1568 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1569 break;
1570 case PROP_RTP_BLOCKSIZE:
1571 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1572 break;
1573 case PROP_USER_ID:
1574 g_free (rtspsrc->user_id);
1575 rtspsrc->user_id = g_value_dup_string (value);
1576 break;
1577 case PROP_USER_PW:
1578 g_free (rtspsrc->user_pw);
1579 rtspsrc->user_pw = g_value_dup_string (value);
1580 break;
1581 case PROP_BUFFER_MODE:
1582 rtspsrc->buffer_mode = g_value_get_enum (value);
1583 break;
1584 case PROP_PORT_RANGE:
1585 {
1586 const gchar *str;
1587
1588 str = g_value_get_string (value);
1589 if (str == NULL || sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1590 &rtspsrc->client_port_range.max) != 2) {
1591 rtspsrc->client_port_range.min = 0;
1592 rtspsrc->client_port_range.max = 0;
1593 }
1594 break;
1595 }
1596 case PROP_UDP_BUFFER_SIZE:
1597 rtspsrc->udp_buffer_size = g_value_get_int (value);
1598 break;
1599 case PROP_SHORT_HEADER:
1600 rtspsrc->short_header = g_value_get_boolean (value);
1601 break;
1602 case PROP_PROBATION:
1603 rtspsrc->probation = g_value_get_uint (value);
1604 break;
1605 case PROP_UDP_RECONNECT:
1606 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1607 break;
1608 case PROP_MULTICAST_IFACE:
1609 g_free (rtspsrc->multi_iface);
1610
1611 if (g_value_get_string (value) == NULL)
1612 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1613 else
1614 rtspsrc->multi_iface = g_value_dup_string (value);
1615 break;
1616 case PROP_NTP_SYNC:
1617 rtspsrc->ntp_sync = g_value_get_boolean (value);
1618 /* The default value of max_ts_offset depends on ntp_sync. If user
1619 * hasn't set it then change default value */
1620 if (!rtspsrc->max_ts_offset_is_set) {
1621 if (rtspsrc->ntp_sync) {
1622 rtspsrc->max_ts_offset = 0;
1623 } else {
1624 rtspsrc->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1625 }
1626 }
1627 break;
1628 case PROP_USE_PIPELINE_CLOCK:
1629 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1630 break;
1631 case PROP_SDES:
1632 rtspsrc->sdes = g_value_dup_boxed (value);
1633 break;
1634 case PROP_TLS_VALIDATION_FLAGS:
1635 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1636 break;
1637 case PROP_TLS_DATABASE:
1638 g_clear_object (&rtspsrc->tls_database);
1639 rtspsrc->tls_database = g_value_dup_object (value);
1640 break;
1641 case PROP_TLS_INTERACTION:
1642 g_clear_object (&rtspsrc->tls_interaction);
1643 rtspsrc->tls_interaction = g_value_dup_object (value);
1644 break;
1645 case PROP_DO_RETRANSMISSION:
1646 rtspsrc->do_retransmission = g_value_get_boolean (value);
1647 break;
1648 case PROP_NTP_TIME_SOURCE:
1649 rtspsrc->ntp_time_source = g_value_get_enum (value);
1650 break;
1651 case PROP_USER_AGENT:
1652 g_free (rtspsrc->user_agent);
1653 rtspsrc->user_agent = g_value_dup_string (value);
1654 break;
1655 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1656 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1657 break;
1658 case PROP_RFC7273_SYNC:
1659 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1660 break;
1661 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1662 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1663 break;
1664 case PROP_MAX_TS_OFFSET:
1665 rtspsrc->max_ts_offset = g_value_get_int64 (value);
1666 rtspsrc->max_ts_offset_is_set = TRUE;
1667 break;
1668 case PROP_DEFAULT_VERSION:
1669 rtspsrc->default_version = g_value_get_enum (value);
1670 break;
1671 case PROP_BACKCHANNEL:
1672 rtspsrc->backchannel = g_value_get_enum (value);
1673 break;
1674 case PROP_TEARDOWN_TIMEOUT:
1675 rtspsrc->teardown_timeout = g_value_get_uint64 (value);
1676 break;
1677 default:
1678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1679 break;
1680 }
1681 }
1682
1683 static void
gst_rtspsrc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1684 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1685 GParamSpec * pspec)
1686 {
1687 GstRTSPSrc *rtspsrc;
1688
1689 rtspsrc = GST_RTSPSRC (object);
1690
1691 switch (prop_id) {
1692 case PROP_LOCATION:
1693 g_value_set_string (value, rtspsrc->conninfo.location);
1694 break;
1695 case PROP_PROTOCOLS:
1696 g_value_set_flags (value, rtspsrc->protocols);
1697 break;
1698 case PROP_DEBUG:
1699 g_value_set_boolean (value, rtspsrc->debug);
1700 break;
1701 case PROP_RETRY:
1702 g_value_set_uint (value, rtspsrc->retry);
1703 break;
1704 case PROP_TIMEOUT:
1705 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1706 break;
1707 case PROP_TCP_TIMEOUT:
1708 {
1709 guint64 timeout;
1710
1711 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1712 rtspsrc->tcp_timeout.tv_usec;
1713 g_value_set_uint64 (value, timeout);
1714 break;
1715 }
1716 case PROP_LATENCY:
1717 g_value_set_uint (value, rtspsrc->latency);
1718 break;
1719 case PROP_DROP_ON_LATENCY:
1720 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1721 break;
1722 case PROP_CONNECTION_SPEED:
1723 g_value_set_uint64 (value, rtspsrc->connection_speed);
1724 break;
1725 case PROP_NAT_METHOD:
1726 g_value_set_enum (value, rtspsrc->nat_method);
1727 break;
1728 case PROP_DO_RTCP:
1729 g_value_set_boolean (value, rtspsrc->do_rtcp);
1730 break;
1731 case PROP_DO_RTSP_KEEP_ALIVE:
1732 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1733 break;
1734 case PROP_PROXY:
1735 {
1736 gchar *str;
1737
1738 if (rtspsrc->proxy_host) {
1739 str =
1740 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1741 } else {
1742 str = NULL;
1743 }
1744 g_value_take_string (value, str);
1745 break;
1746 }
1747 case PROP_PROXY_ID:
1748 g_value_set_string (value, rtspsrc->prop_proxy_id);
1749 break;
1750 case PROP_PROXY_PW:
1751 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1752 break;
1753 case PROP_RTP_BLOCKSIZE:
1754 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1755 break;
1756 case PROP_USER_ID:
1757 g_value_set_string (value, rtspsrc->user_id);
1758 break;
1759 case PROP_USER_PW:
1760 g_value_set_string (value, rtspsrc->user_pw);
1761 break;
1762 case PROP_BUFFER_MODE:
1763 g_value_set_enum (value, rtspsrc->buffer_mode);
1764 break;
1765 case PROP_PORT_RANGE:
1766 {
1767 gchar *str;
1768
1769 if (rtspsrc->client_port_range.min != 0) {
1770 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1771 rtspsrc->client_port_range.max);
1772 } else {
1773 str = NULL;
1774 }
1775 g_value_take_string (value, str);
1776 break;
1777 }
1778 case PROP_UDP_BUFFER_SIZE:
1779 g_value_set_int (value, rtspsrc->udp_buffer_size);
1780 break;
1781 case PROP_SHORT_HEADER:
1782 g_value_set_boolean (value, rtspsrc->short_header);
1783 break;
1784 case PROP_PROBATION:
1785 g_value_set_uint (value, rtspsrc->probation);
1786 break;
1787 case PROP_UDP_RECONNECT:
1788 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1789 break;
1790 case PROP_MULTICAST_IFACE:
1791 g_value_set_string (value, rtspsrc->multi_iface);
1792 break;
1793 case PROP_NTP_SYNC:
1794 g_value_set_boolean (value, rtspsrc->ntp_sync);
1795 break;
1796 case PROP_USE_PIPELINE_CLOCK:
1797 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1798 break;
1799 case PROP_SDES:
1800 g_value_set_boxed (value, rtspsrc->sdes);
1801 break;
1802 case PROP_TLS_VALIDATION_FLAGS:
1803 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1804 break;
1805 case PROP_TLS_DATABASE:
1806 g_value_set_object (value, rtspsrc->tls_database);
1807 break;
1808 case PROP_TLS_INTERACTION:
1809 g_value_set_object (value, rtspsrc->tls_interaction);
1810 break;
1811 case PROP_DO_RETRANSMISSION:
1812 g_value_set_boolean (value, rtspsrc->do_retransmission);
1813 break;
1814 case PROP_NTP_TIME_SOURCE:
1815 g_value_set_enum (value, rtspsrc->ntp_time_source);
1816 break;
1817 case PROP_USER_AGENT:
1818 g_value_set_string (value, rtspsrc->user_agent);
1819 break;
1820 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1821 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1822 break;
1823 case PROP_RFC7273_SYNC:
1824 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1825 break;
1826 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1827 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
1828 break;
1829 case PROP_MAX_TS_OFFSET:
1830 g_value_set_int64 (value, rtspsrc->max_ts_offset);
1831 break;
1832 case PROP_DEFAULT_VERSION:
1833 g_value_set_enum (value, rtspsrc->default_version);
1834 break;
1835 case PROP_BACKCHANNEL:
1836 g_value_set_enum (value, rtspsrc->backchannel);
1837 break;
1838 case PROP_TEARDOWN_TIMEOUT:
1839 g_value_set_uint64 (value, rtspsrc->teardown_timeout);
1840 break;
1841 default:
1842 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1843 break;
1844 }
1845 }
1846
1847 static gint
find_stream_by_id(GstRTSPStream * stream,gint * id)1848 find_stream_by_id (GstRTSPStream * stream, gint * id)
1849 {
1850 if (stream->id == *id)
1851 return 0;
1852
1853 return -1;
1854 }
1855
1856 static gint
find_stream_by_channel(GstRTSPStream * stream,gint * channel)1857 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1858 {
1859 /* ignore unconfigured channels here (e.g., those that
1860 * were explicitly skipped during SETUP) */
1861 if ((stream->channelpad[0] != NULL) &&
1862 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1863 return 0;
1864
1865 return -1;
1866 }
1867
1868 static gint
find_stream_by_udpsrc(GstRTSPStream * stream,gconstpointer a)1869 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1870 {
1871 GstElement *src = (GstElement *) a;
1872
1873 if (stream->udpsrc[0] == src)
1874 return 0;
1875 if (stream->udpsrc[1] == src)
1876 return 0;
1877
1878 return -1;
1879 }
1880
1881 static gint
find_stream_by_setup(GstRTSPStream * stream,gconstpointer a)1882 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1883 {
1884 if (stream->conninfo.location) {
1885 /* check qualified setup_url */
1886 if (!strcmp (stream->conninfo.location, (gchar *) a))
1887 return 0;
1888 }
1889 if (stream->control_url) {
1890 /* check original control_url */
1891 if (!strcmp (stream->control_url, (gchar *) a))
1892 return 0;
1893
1894 /* check if qualified setup_url ends with string */
1895 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1896 return 0;
1897 }
1898
1899 return -1;
1900 }
1901
1902 static GstRTSPStream *
find_stream(GstRTSPSrc * src,gconstpointer data,gconstpointer func)1903 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1904 {
1905 GList *lstream;
1906
1907 /* find and get stream */
1908 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1909 return (GstRTSPStream *) lstream->data;
1910
1911 return NULL;
1912 }
1913
1914 static const GstSDPBandwidth *
gst_rtspsrc_get_bandwidth(GstRTSPSrc * src,const GstSDPMessage * sdp,const GstSDPMedia * media,const gchar * type)1915 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1916 const GstSDPMedia * media, const gchar * type)
1917 {
1918 guint i, len;
1919
1920 /* first look in the media specific section */
1921 len = gst_sdp_media_bandwidths_len (media);
1922 for (i = 0; i < len; i++) {
1923 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1924
1925 if (strcmp (bw->bwtype, type) == 0)
1926 return bw;
1927 }
1928 /* then look in the message specific section */
1929 len = gst_sdp_message_bandwidths_len (sdp);
1930 for (i = 0; i < len; i++) {
1931 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1932
1933 if (strcmp (bw->bwtype, type) == 0)
1934 return bw;
1935 }
1936 return NULL;
1937 }
1938
1939 static void
gst_rtspsrc_collect_bandwidth(GstRTSPSrc * src,const GstSDPMessage * sdp,const GstSDPMedia * media,GstRTSPStream * stream)1940 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1941 const GstSDPMedia * media, GstRTSPStream * stream)
1942 {
1943 const GstSDPBandwidth *bw;
1944
1945 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1946 stream->as_bandwidth = bw->bandwidth;
1947 else
1948 stream->as_bandwidth = -1;
1949
1950 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1951 stream->rr_bandwidth = bw->bandwidth;
1952 else
1953 stream->rr_bandwidth = -1;
1954
1955 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1956 stream->rs_bandwidth = bw->bandwidth;
1957 else
1958 stream->rs_bandwidth = -1;
1959 }
1960
1961 static void
gst_rtspsrc_do_stream_connection(GstRTSPSrc * src,GstRTSPStream * stream,const GstSDPConnection * conn)1962 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1963 const GstSDPConnection * conn)
1964 {
1965 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1966 return;
1967
1968 if (conn->addrtype == NULL)
1969 return;
1970
1971 /* check for IPV6 */
1972 if (strcmp (conn->addrtype, "IP4") == 0)
1973 stream->is_ipv6 = FALSE;
1974 else if (strcmp (conn->addrtype, "IP6") == 0)
1975 stream->is_ipv6 = TRUE;
1976 else
1977 return;
1978
1979 /* save address */
1980 g_free (stream->destination);
1981 stream->destination = g_strdup (conn->address);
1982
1983 /* check for multicast */
1984 stream->is_multicast =
1985 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1986 conn->address);
1987 stream->ttl = conn->ttl;
1988 }
1989
1990 /* Go over the connections for a stream.
1991 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1992 * receiving.
1993 * - If we are dealing with a localhost address, we disable multicast
1994 */
1995 static void
gst_rtspsrc_collect_connections(GstRTSPSrc * src,const GstSDPMessage * sdp,const GstSDPMedia * media,GstRTSPStream * stream)1996 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1997 const GstSDPMedia * media, GstRTSPStream * stream)
1998 {
1999 const GstSDPConnection *conn;
2000 guint i, len;
2001
2002 /* first look in the media specific section */
2003 len = gst_sdp_media_connections_len (media);
2004 for (i = 0; i < len; i++) {
2005 conn = gst_sdp_media_get_connection (media, i);
2006
2007 gst_rtspsrc_do_stream_connection (src, stream, conn);
2008 }
2009 /* then look in the message specific section */
2010 if ((conn = gst_sdp_message_get_connection (sdp))) {
2011 gst_rtspsrc_do_stream_connection (src, stream, conn);
2012 }
2013 }
2014
2015 static gchar *
make_stream_id(GstRTSPStream * stream,const GstSDPMedia * media)2016 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
2017 {
2018 gchar *stream_id =
2019 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
2020 media->num_ports, media->proto, stream->default_pt);
2021
2022 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
2023
2024 return stream_id;
2025 }
2026
2027 /* m=<media> <UDP port> RTP/AVP <payload>
2028 */
2029 static void
gst_rtspsrc_collect_payloads(GstRTSPSrc * src,const GstSDPMessage * sdp,const GstSDPMedia * media,GstRTSPStream * stream)2030 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
2031 const GstSDPMedia * media, GstRTSPStream * stream)
2032 {
2033 guint i, len;
2034 const gchar *proto;
2035 GstCaps *global_caps;
2036
2037 /* get proto */
2038 proto = gst_sdp_media_get_proto (media);
2039 if (proto == NULL)
2040 goto no_proto;
2041
2042 if (g_str_equal (proto, "RTP/AVP"))
2043 stream->profile = GST_RTSP_PROFILE_AVP;
2044 else if (g_str_equal (proto, "RTP/SAVP"))
2045 stream->profile = GST_RTSP_PROFILE_SAVP;
2046 else if (g_str_equal (proto, "RTP/AVPF"))
2047 stream->profile = GST_RTSP_PROFILE_AVPF;
2048 else if (g_str_equal (proto, "RTP/SAVPF"))
2049 stream->profile = GST_RTSP_PROFILE_SAVPF;
2050 else
2051 goto unknown_proto;
2052
2053 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
2054 /* We want to setup caps for streams configured as backchannel */
2055 !stream->is_backchannel && src->backchannel != BACKCHANNEL_NONE)
2056 goto sendonly_media;
2057
2058 /* Parse global SDP attributes once */
2059 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
2060 GST_DEBUG ("mapping sdp session level attributes to caps");
2061 gst_sdp_message_attributes_to_caps (sdp, global_caps);
2062 GST_DEBUG ("mapping sdp media level attributes to caps");
2063 gst_sdp_media_attributes_to_caps (media, global_caps);
2064
2065 /* Keep a copy of the SDP key management */
2066 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
2067 if (stream->mikey == NULL)
2068 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
2069
2070 len = gst_sdp_media_formats_len (media);
2071 for (i = 0; i < len; i++) {
2072 gint pt;
2073 GstCaps *caps, *outcaps;
2074 GstStructure *s;
2075 const gchar *enc;
2076 PtMapItem item;
2077
2078 pt = atoi (gst_sdp_media_get_format (media, i));
2079
2080 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
2081
2082 /* convert caps */
2083 caps = gst_sdp_media_get_caps_from_media (media, pt);
2084 if (caps == NULL) {
2085 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
2086 continue;
2087 }
2088
2089 /* do some tweaks */
2090 s = gst_caps_get_structure (caps, 0);
2091 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
2092 stream->is_real = (strstr (enc, "-REAL") != NULL);
2093 if (strcmp (enc, "X-ASF-PF") == 0)
2094 stream->container = TRUE;
2095 }
2096
2097 /* Merge in global caps */
2098 /* Intersect will merge in missing fields to the current caps */
2099 outcaps = gst_caps_intersect (caps, global_caps);
2100 gst_caps_unref (caps);
2101
2102 /* the first pt will be the default */
2103 if (stream->ptmap->len == 0)
2104 stream->default_pt = pt;
2105
2106 item.pt = pt;
2107 item.caps = outcaps;
2108
2109 g_array_append_val (stream->ptmap, item);
2110 }
2111
2112 stream->stream_id = make_stream_id (stream, media);
2113
2114 gst_caps_unref (global_caps);
2115 return;
2116
2117 no_proto:
2118 {
2119 GST_ERROR_OBJECT (src, "can't find proto in media");
2120 return;
2121 }
2122 unknown_proto:
2123 {
2124 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
2125 return;
2126 }
2127 sendonly_media:
2128 {
2129 GST_DEBUG_OBJECT (src, "sendonly media ignored, no backchannel");
2130 return;
2131 }
2132 }
2133
2134 static const gchar *
get_aggregate_control(GstRTSPSrc * src)2135 get_aggregate_control (GstRTSPSrc * src)
2136 {
2137 const gchar *base;
2138
2139 if (src->control)
2140 base = src->control;
2141 else if (src->content_base)
2142 base = src->content_base;
2143 else if (src->conninfo.url_str)
2144 base = src->conninfo.url_str;
2145 else
2146 base = "/";
2147
2148 return base;
2149 }
2150
2151 static void
clear_ptmap_item(PtMapItem * item)2152 clear_ptmap_item (PtMapItem * item)
2153 {
2154 if (item->caps)
2155 gst_caps_unref (item->caps);
2156 }
2157
2158 static GstRTSPStream *
gst_rtspsrc_create_stream(GstRTSPSrc * src,GstSDPMessage * sdp,gint idx,gint n_streams)2159 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
2160 gint n_streams)
2161 {
2162 GstRTSPStream *stream;
2163 const gchar *control_url;
2164 const GstSDPMedia *media;
2165
2166 /* get media, should not return NULL */
2167 media = gst_sdp_message_get_media (sdp, idx);
2168 if (media == NULL)
2169 return NULL;
2170
2171 stream = g_new0 (GstRTSPStream, 1);
2172 stream->parent = src;
2173 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
2174 * the element. */
2175 stream->last_ret = GST_FLOW_NOT_LINKED;
2176 stream->added = FALSE;
2177 stream->setup = FALSE;
2178 stream->skipped = FALSE;
2179 stream->id = idx;
2180 stream->eos = FALSE;
2181 stream->discont = TRUE;
2182 stream->seqbase = -1;
2183 stream->timebase = -1;
2184 stream->send_ssrc = g_random_int ();
2185 stream->profile = GST_RTSP_PROFILE_AVP;
2186 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
2187 stream->mikey = NULL;
2188 stream->stream_id = NULL;
2189 stream->is_backchannel = FALSE;
2190 g_mutex_init (&stream->conninfo.send_lock);
2191 g_mutex_init (&stream->conninfo.recv_lock);
2192 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
2193
2194 /* stream is sendonly and onvif backchannel is requested */
2195 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
2196 src->backchannel != BACKCHANNEL_NONE)
2197 stream->is_backchannel = TRUE;
2198
2199 /* collect bandwidth information for this steam. FIXME, configure in the RTP
2200 * session manager to scale RTCP. */
2201 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
2202
2203 /* collect connection info */
2204 gst_rtspsrc_collect_connections (src, sdp, media, stream);
2205
2206 /* make the payload type map */
2207 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
2208
2209 /* collect port number */
2210 stream->port = gst_sdp_media_get_port (media);
2211
2212 /* get control url to construct the setup url. The setup url is used to
2213 * configure the transport of the stream and is used to identity the stream in
2214 * the RTP-Info header field returned from PLAY. */
2215 control_url = gst_sdp_media_get_attribute_val (media, "control");
2216 if (control_url == NULL)
2217 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
2218
2219 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
2220 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
2221 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
2222 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
2223
2224 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
2225 if (control_url == NULL && n_streams == 1) {
2226 control_url = "";
2227 }
2228
2229 if (control_url != NULL) {
2230 stream->control_url = g_strdup (control_url);
2231 /* Build a fully qualified url using the content_base if any or by prefixing
2232 * the original request.
2233 * If the control_url starts with a '/' or a non rtsp: protocol we will most
2234 * likely build a URL that the server will fail to understand, this is ok,
2235 * we will fail then. */
2236 if (g_str_has_prefix (control_url, "rtsp://"))
2237 stream->conninfo.location = g_strdup (control_url);
2238 else {
2239 const gchar *base;
2240 gboolean has_slash;
2241
2242 if (g_strcmp0 (control_url, "*") == 0)
2243 control_url = "";
2244
2245 base = get_aggregate_control (src);
2246
2247 /* check if the base ends or control starts with / */
2248 has_slash = g_str_has_prefix (control_url, "/");
2249 has_slash = has_slash || g_str_has_suffix (base, "/");
2250
2251 /* concatenate the two strings, insert / when not present */
2252 stream->conninfo.location =
2253 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
2254 }
2255 }
2256 GST_DEBUG_OBJECT (src, " setup: %s",
2257 GST_STR_NULL (stream->conninfo.location));
2258
2259 /* we keep track of all streams */
2260 src->streams = g_list_append (src->streams, stream);
2261
2262 return stream;
2263
2264 /* ERRORS */
2265 }
2266
2267 static void
gst_rtspsrc_stream_free(GstRTSPSrc * src,GstRTSPStream * stream)2268 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
2269 {
2270 gint i;
2271
2272 GST_DEBUG_OBJECT (src, "free stream %p", stream);
2273
2274 g_array_free (stream->ptmap, TRUE);
2275
2276 g_free (stream->destination);
2277 g_free (stream->control_url);
2278 g_free (stream->conninfo.location);
2279 g_free (stream->stream_id);
2280
2281 for (i = 0; i < 2; i++) {
2282 if (stream->udpsrc[i]) {
2283 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2284 if (gst_object_has_as_parent (GST_OBJECT (stream->udpsrc[i]),
2285 GST_OBJECT (src)))
2286 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
2287 gst_object_unref (stream->udpsrc[i]);
2288 }
2289 if (stream->channelpad[i])
2290 gst_object_unref (stream->channelpad[i]);
2291
2292 if (stream->udpsink[i]) {
2293 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
2294 if (gst_object_has_as_parent (GST_OBJECT (stream->udpsink[i]),
2295 GST_OBJECT (src)))
2296 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
2297 gst_object_unref (stream->udpsink[i]);
2298 }
2299 }
2300 if (stream->rtpsrc) {
2301 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
2302 gst_bin_remove (GST_BIN_CAST (src), stream->rtpsrc);
2303 gst_object_unref (stream->rtpsrc);
2304 }
2305 if (stream->srcpad) {
2306 gst_pad_set_active (stream->srcpad, FALSE);
2307 if (stream->added)
2308 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2309 }
2310 if (stream->srtpenc)
2311 gst_object_unref (stream->srtpenc);
2312 if (stream->srtpdec)
2313 gst_object_unref (stream->srtpdec);
2314 if (stream->srtcpparams)
2315 gst_caps_unref (stream->srtcpparams);
2316 if (stream->mikey)
2317 gst_mikey_message_unref (stream->mikey);
2318 if (stream->rtcppad)
2319 gst_object_unref (stream->rtcppad);
2320 if (stream->session)
2321 g_object_unref (stream->session);
2322 if (stream->rtx_pt_map)
2323 gst_structure_free (stream->rtx_pt_map);
2324
2325 g_mutex_clear (&stream->conninfo.send_lock);
2326 g_mutex_clear (&stream->conninfo.recv_lock);
2327
2328 g_free (stream);
2329 }
2330
2331 static void
gst_rtspsrc_cleanup(GstRTSPSrc * src)2332 gst_rtspsrc_cleanup (GstRTSPSrc * src)
2333 {
2334 GList *walk;
2335
2336 GST_DEBUG_OBJECT (src, "cleanup");
2337
2338 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2339 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2340
2341 gst_rtspsrc_stream_free (src, stream);
2342 }
2343 g_list_free (src->streams);
2344 src->streams = NULL;
2345 if (src->manager) {
2346 if (src->manager_sig_id) {
2347 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
2348 src->manager_sig_id = 0;
2349 }
2350 gst_element_set_state (src->manager, GST_STATE_NULL);
2351 gst_bin_remove (GST_BIN_CAST (src), src->manager);
2352 src->manager = NULL;
2353 }
2354 if (src->props)
2355 gst_structure_free (src->props);
2356 src->props = NULL;
2357
2358 g_free (src->content_base);
2359 src->content_base = NULL;
2360
2361 g_free (src->control);
2362 src->control = NULL;
2363
2364 if (src->range)
2365 gst_rtsp_range_free (src->range);
2366 src->range = NULL;
2367
2368 /* don't clear the SDP when it was used in the url */
2369 if (src->sdp && !src->from_sdp) {
2370 gst_sdp_message_free (src->sdp);
2371 src->sdp = NULL;
2372 }
2373
2374 src->need_segment = FALSE;
2375
2376 if (src->provided_clock) {
2377 gst_object_unref (src->provided_clock);
2378 src->provided_clock = NULL;
2379 }
2380
2381 /* free parameter requests queue */
2382 if (!g_queue_is_empty (&src->set_get_param_q))
2383 g_queue_free_full (&src->set_get_param_q, free_param_queue);
2384
2385 }
2386
2387 static gboolean
gst_rtspsrc_alloc_udp_ports(GstRTSPStream * stream,gint * rtpport,gint * rtcpport)2388 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2389 gint * rtpport, gint * rtcpport)
2390 {
2391 GstRTSPSrc *src;
2392 GstStateChangeReturn ret;
2393 GstElement *udpsrc0, *udpsrc1;
2394 gint tmp_rtp, tmp_rtcp;
2395 guint count;
2396 const gchar *host;
2397
2398 src = stream->parent;
2399
2400 udpsrc0 = NULL;
2401 udpsrc1 = NULL;
2402 count = 0;
2403
2404 /* Start at next port */
2405 tmp_rtp = src->next_port_num;
2406
2407 if (stream->is_ipv6)
2408 host = "udp://[::0]";
2409 else
2410 host = "udp://0.0.0.0";
2411
2412 /* try to allocate 2 UDP ports, the RTP port should be an even
2413 * number and the RTCP port should be the next (uneven) port */
2414 again:
2415
2416 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2417 tmp_rtp >= src->client_port_range.max)
2418 goto no_ports;
2419
2420 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2421 if (udpsrc0 == NULL)
2422 goto no_udp_protocol;
2423 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2424
2425 if (src->udp_buffer_size != 0)
2426 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2427 NULL);
2428
2429 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2430 if (ret == GST_STATE_CHANGE_FAILURE) {
2431 if (tmp_rtp != 0) {
2432 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2433
2434 tmp_rtp += 2;
2435 if (++count > src->retry)
2436 goto no_ports;
2437
2438 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2439 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2440 gst_object_unref (udpsrc0);
2441 udpsrc0 = NULL;
2442
2443 GST_DEBUG_OBJECT (src, "retry %d", count);
2444 goto again;
2445 }
2446 goto no_udp_protocol;
2447 }
2448
2449 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2450 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2451
2452 /* check if port is even */
2453 if ((tmp_rtp & 0x01) != 0) {
2454 /* port not even, close and allocate another */
2455 if (++count > src->retry)
2456 goto no_ports;
2457
2458 GST_DEBUG_OBJECT (src, "RTP port not even");
2459
2460 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2461 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2462 gst_object_unref (udpsrc0);
2463 udpsrc0 = NULL;
2464
2465 GST_DEBUG_OBJECT (src, "retry %d", count);
2466 tmp_rtp++;
2467 goto again;
2468 }
2469
2470 /* allocate port+1 for RTCP now */
2471 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2472 if (udpsrc1 == NULL)
2473 goto no_udp_rtcp_protocol;
2474
2475 /* set port */
2476 tmp_rtcp = tmp_rtp + 1;
2477 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2478 goto no_ports;
2479
2480 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2481
2482 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2483 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2484 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2485 if (ret == GST_STATE_CHANGE_FAILURE) {
2486 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2487
2488 if (++count > src->retry)
2489 goto no_ports;
2490
2491 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2492 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2493 gst_object_unref (udpsrc0);
2494 udpsrc0 = NULL;
2495
2496 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2497 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2498 gst_object_unref (udpsrc1);
2499 udpsrc1 = NULL;
2500
2501 tmp_rtp += 2;
2502 GST_DEBUG_OBJECT (src, "retry %d", count);
2503 goto again;
2504 }
2505
2506 /* all fine, do port check */
2507 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2508 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2509
2510 /* this should not happen... */
2511 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2512 goto port_error;
2513
2514 /* we keep these elements, we configure all in configure_transport when the
2515 * server told us to really use the UDP ports. */
2516 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2517 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2518 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2519 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2520
2521 /* keep track of next available port number when we have a range
2522 * configured */
2523 if (src->next_port_num != 0)
2524 src->next_port_num = tmp_rtcp + 1;
2525
2526 return TRUE;
2527
2528 /* ERRORS */
2529 no_udp_protocol:
2530 {
2531 GST_DEBUG_OBJECT (src, "could not get UDP source");
2532 goto cleanup;
2533 }
2534 no_ports:
2535 {
2536 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2537 count);
2538 goto cleanup;
2539 }
2540 no_udp_rtcp_protocol:
2541 {
2542 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2543 goto cleanup;
2544 }
2545 port_error:
2546 {
2547 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2548 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2549 goto cleanup;
2550 }
2551 cleanup:
2552 {
2553 if (udpsrc0) {
2554 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2555 gst_object_unref (udpsrc0);
2556 }
2557 if (udpsrc1) {
2558 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2559 gst_object_unref (udpsrc1);
2560 }
2561 return FALSE;
2562 }
2563 }
2564
2565 static void
gst_rtspsrc_set_state(GstRTSPSrc * src,GstState state)2566 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2567 {
2568 GList *walk;
2569
2570 if (src->manager)
2571 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2572
2573 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2574 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2575 gint i;
2576
2577 for (i = 0; i < 2; i++) {
2578 if (stream->udpsrc[i])
2579 gst_element_set_state (stream->udpsrc[i], state);
2580 }
2581 }
2582 }
2583
2584 static void
gst_rtspsrc_flush(GstRTSPSrc * src,gboolean flush,gboolean playing,guint32 seqnum)2585 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing,
2586 guint32 seqnum)
2587 {
2588 GstEvent *event;
2589 gint cmd;
2590 GstState state;
2591
2592 if (flush) {
2593 event = gst_event_new_flush_start ();
2594 gst_event_set_seqnum (event, seqnum);
2595 GST_DEBUG_OBJECT (src, "start flush");
2596 cmd = CMD_WAIT;
2597 state = GST_STATE_PAUSED;
2598 } else {
2599 event = gst_event_new_flush_stop (FALSE);
2600 gst_event_set_seqnum (event, seqnum);
2601 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2602 cmd = CMD_LOOP;
2603 if (playing)
2604 state = GST_STATE_PLAYING;
2605 else
2606 state = GST_STATE_PAUSED;
2607 }
2608 gst_rtspsrc_push_event (src, event);
2609 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2610 gst_rtspsrc_set_state (src, state);
2611 }
2612
2613 static GstRTSPResult
gst_rtspsrc_connection_send(GstRTSPSrc * src,GstRTSPConnInfo * conninfo,GstRTSPMessage * message,GTimeVal * timeout)2614 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2615 GstRTSPMessage * message, GTimeVal * timeout)
2616 {
2617 GstRTSPResult ret;
2618
2619 if (conninfo->connection) {
2620 g_mutex_lock (&conninfo->send_lock);
2621 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2622 g_mutex_unlock (&conninfo->send_lock);
2623 } else {
2624 ret = GST_RTSP_ERROR;
2625 }
2626
2627 return ret;
2628 }
2629
2630 static GstRTSPResult
gst_rtspsrc_connection_receive(GstRTSPSrc * src,GstRTSPConnInfo * conninfo,GstRTSPMessage * message,GTimeVal * timeout)2631 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2632 GstRTSPMessage * message, GTimeVal * timeout)
2633 {
2634 GstRTSPResult ret;
2635
2636 if (conninfo->connection) {
2637 g_mutex_lock (&conninfo->recv_lock);
2638 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2639 g_mutex_unlock (&conninfo->recv_lock);
2640 } else {
2641 ret = GST_RTSP_ERROR;
2642 }
2643
2644 return ret;
2645 }
2646
2647 static void
gst_rtspsrc_get_position(GstRTSPSrc * src)2648 gst_rtspsrc_get_position (GstRTSPSrc * src)
2649 {
2650 GstQuery *query;
2651 GList *walk;
2652
2653 query = gst_query_new_position (GST_FORMAT_TIME);
2654 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2655 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2656 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2657 GstFormat fmt;
2658 gint64 pos;
2659
2660 if (stream->srcpad) {
2661 if (gst_pad_query (stream->srcpad, query)) {
2662 gst_query_parse_position (query, &fmt, &pos);
2663 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2664 GST_TIME_ARGS (pos));
2665 src->last_pos = pos;
2666 goto out;
2667 }
2668 }
2669 }
2670
2671 src->last_pos = 0;
2672
2673 out:
2674
2675 gst_query_unref (query);
2676 }
2677
2678 static gboolean
gst_rtspsrc_perform_seek(GstRTSPSrc * src,GstEvent * event)2679 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2680 {
2681 gdouble rate;
2682 GstFormat format;
2683 GstSeekFlags flags;
2684 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2685 gint64 cur, stop;
2686 gboolean flush, skip;
2687 gboolean update;
2688 gboolean playing;
2689 GstSegment seeksegment = { 0, };
2690 GList *walk;
2691 const gchar *seek_style = NULL;
2692
2693 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2694
2695 gst_event_parse_seek (event, &rate, &format, &flags,
2696 &cur_type, &cur, &stop_type, &stop);
2697
2698 /* no negative rates yet */
2699 if (rate < 0.0)
2700 goto negative_rate;
2701
2702 /* we need TIME format */
2703 if (format != src->segment.format)
2704 goto no_format;
2705
2706 /* Check if we are not at all seekable */
2707 if (src->seekable == -1.0)
2708 goto not_seekable;
2709
2710 /* Additional seeking-to-beginning-only check */
2711 if (src->seekable == 0.0 && cur != 0)
2712 goto not_seekable;
2713
2714 if (flags & GST_SEEK_FLAG_SEGMENT)
2715 goto invalid_segment_flag;
2716
2717 /* get flush flag */
2718 flush = flags & GST_SEEK_FLAG_FLUSH;
2719 skip = flags & GST_SEEK_FLAG_SKIP;
2720
2721 /* now we need to make sure the streaming thread is stopped. We do this by
2722 * either sending a FLUSH_START event downstream which will cause the
2723 * streaming thread to stop with a WRONG_STATE.
2724 * For a non-flushing seek we simply pause the task, which will happen as soon
2725 * as it completes one iteration (and thus might block when the sink is
2726 * blocking in preroll). */
2727 if (flush) {
2728 GST_DEBUG_OBJECT (src, "starting flush");
2729 gst_rtspsrc_flush (src, TRUE, FALSE, gst_event_get_seqnum (event));
2730 } else {
2731 if (src->task) {
2732 gst_task_pause (src->task);
2733 }
2734 }
2735
2736 /* we should now be able to grab the streaming thread because we stopped it
2737 * with the above flush/pause code */
2738 GST_RTSP_STREAM_LOCK (src);
2739
2740 GST_DEBUG_OBJECT (src, "stopped streaming");
2741
2742 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2743 gst_rtspsrc_connection_flush (src, FALSE);
2744
2745 /* copy segment, we need this because we still need the old
2746 * segment when we close the current segment. */
2747 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2748
2749 /* configure the seek parameters in the seeksegment. We will then have the
2750 * right values in the segment to perform the seek */
2751 GST_DEBUG_OBJECT (src, "configuring seek");
2752 gst_segment_do_seek (&seeksegment, rate, format, flags,
2753 cur_type, cur, stop_type, stop, &update);
2754
2755 /* figure out the last position we need to play. If it's configured (stop !=
2756 * -1), use that, else we play until the total duration of the file */
2757 if ((stop = seeksegment.stop) == -1)
2758 stop = seeksegment.duration;
2759
2760 /* if we were playing, pause first */
2761 playing = (src->state == GST_RTSP_STATE_PLAYING);
2762 if (playing) {
2763 /* obtain current position in case seek fails */
2764 gst_rtspsrc_get_position (src);
2765 gst_rtspsrc_pause (src, FALSE);
2766 }
2767 src->skip = skip;
2768
2769 src->state = GST_RTSP_STATE_SEEKING;
2770
2771 /* PLAY will add the range header now. */
2772 src->need_range = TRUE;
2773
2774 /* prepare for streaming again */
2775 if (flush) {
2776 /* if we started flush, we stop now */
2777 GST_DEBUG_OBJECT (src, "stopping flush");
2778 gst_rtspsrc_flush (src, FALSE, playing, gst_event_get_seqnum (event));
2779 }
2780
2781 /* now we did the seek and can activate the new segment values */
2782 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2783
2784 /* if we're doing a segment seek, post a SEGMENT_START message */
2785 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2786 gst_element_post_message (GST_ELEMENT_CAST (src),
2787 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2788 src->segment.format, src->segment.position));
2789 }
2790
2791 /* now create the newsegment */
2792 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2793 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2794
2795 /* mark discont */
2796 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2797 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2798 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2799 stream->discont = TRUE;
2800 }
2801
2802 /* and continue playing if needed */
2803 GST_OBJECT_LOCK (src);
2804 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2805 && GST_STATE (src) == GST_STATE_PLAYING)
2806 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2807 GST_OBJECT_UNLOCK (src);
2808
2809 if (src->version >= GST_RTSP_VERSION_2_0) {
2810 if (flags & GST_SEEK_FLAG_ACCURATE)
2811 seek_style = "RAP";
2812 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
2813 seek_style = "CoRAP";
2814 else if (flags & GST_SEEK_FLAG_KEY_UNIT
2815 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
2816 seek_style = "First-Prior";
2817 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
2818 seek_style = "Next";
2819 }
2820
2821 if (playing)
2822 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
2823
2824 GST_RTSP_STREAM_UNLOCK (src);
2825
2826 return TRUE;
2827
2828 /* ERRORS */
2829 negative_rate:
2830 {
2831 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2832 return FALSE;
2833 }
2834 no_format:
2835 {
2836 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2837 return FALSE;
2838 }
2839 not_seekable:
2840 {
2841 GST_DEBUG_OBJECT (src, "stream is not seekable");
2842 return FALSE;
2843 }
2844 invalid_segment_flag:
2845 {
2846 GST_WARNING_OBJECT (src, "Segment seeks not supported");
2847 return FALSE;
2848 }
2849 }
2850
2851 static gboolean
gst_rtspsrc_handle_src_event(GstPad * pad,GstObject * parent,GstEvent * event)2852 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2853 GstEvent * event)
2854 {
2855 GstRTSPSrc *src;
2856 gboolean res = TRUE;
2857 gboolean forward;
2858
2859 src = GST_RTSPSRC_CAST (parent);
2860
2861 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2862 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2863
2864 switch (GST_EVENT_TYPE (event)) {
2865 case GST_EVENT_SEEK:
2866 res = gst_rtspsrc_perform_seek (src, event);
2867 forward = FALSE;
2868 break;
2869 case GST_EVENT_QOS:
2870 case GST_EVENT_NAVIGATION:
2871 case GST_EVENT_LATENCY:
2872 default:
2873 forward = TRUE;
2874 break;
2875 }
2876 if (forward) {
2877 GstPad *target;
2878
2879 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2880 res = gst_pad_send_event (target, event);
2881 gst_object_unref (target);
2882 } else {
2883 gst_event_unref (event);
2884 }
2885 } else {
2886 gst_event_unref (event);
2887 }
2888
2889 return res;
2890 }
2891
2892 static gboolean
gst_rtspsrc_handle_src_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)2893 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2894 GstEvent * event)
2895 {
2896 GstRTSPStream *stream;
2897
2898 stream = gst_pad_get_element_private (pad);
2899
2900 switch (GST_EVENT_TYPE (event)) {
2901 case GST_EVENT_STREAM_START:{
2902 const gchar *upstream_id;
2903 gchar *stream_id;
2904
2905 gst_event_parse_stream_start (event, &upstream_id);
2906 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2907
2908 gst_event_unref (event);
2909 event = gst_event_new_stream_start (stream_id);
2910 g_free (stream_id);
2911 break;
2912 }
2913 default:
2914 break;
2915 }
2916
2917 return gst_pad_push_event (stream->srcpad, event);
2918 }
2919
2920 /* this is the final event function we receive on the internal source pad when
2921 * we deal with TCP connections */
2922 static gboolean
gst_rtspsrc_handle_internal_src_event(GstPad * pad,GstObject * parent,GstEvent * event)2923 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2924 GstEvent * event)
2925 {
2926 gboolean res;
2927
2928 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2929
2930 switch (GST_EVENT_TYPE (event)) {
2931 case GST_EVENT_SEEK:
2932 case GST_EVENT_QOS:
2933 case GST_EVENT_NAVIGATION:
2934 case GST_EVENT_LATENCY:
2935 default:
2936 gst_event_unref (event);
2937 res = TRUE;
2938 break;
2939 }
2940 return res;
2941 }
2942
2943 /* this is the final query function we receive on the internal source pad when
2944 * we deal with TCP connections */
2945 static gboolean
gst_rtspsrc_handle_internal_src_query(GstPad * pad,GstObject * parent,GstQuery * query)2946 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2947 GstQuery * query)
2948 {
2949 GstRTSPSrc *src;
2950 gboolean res = TRUE;
2951
2952 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2953
2954 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2955 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2956
2957 switch (GST_QUERY_TYPE (query)) {
2958 case GST_QUERY_POSITION:
2959 {
2960 /* no idea */
2961 break;
2962 }
2963 case GST_QUERY_DURATION:
2964 {
2965 GstFormat format;
2966
2967 gst_query_parse_duration (query, &format, NULL);
2968
2969 switch (format) {
2970 case GST_FORMAT_TIME:
2971 gst_query_set_duration (query, format, src->segment.duration);
2972 break;
2973 default:
2974 res = FALSE;
2975 break;
2976 }
2977 break;
2978 }
2979 case GST_QUERY_LATENCY:
2980 {
2981 /* we are live with a min latency of 0 and unlimited max latency, this
2982 * result will be updated by the session manager if there is any. */
2983 gst_query_set_latency (query, TRUE, 0, -1);
2984 break;
2985 }
2986 default:
2987 break;
2988 }
2989
2990 return res;
2991 }
2992
2993 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2994 static gboolean
gst_rtspsrc_handle_src_query(GstPad * pad,GstObject * parent,GstQuery * query)2995 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2996 GstQuery * query)
2997 {
2998 GstRTSPSrc *src;
2999 gboolean res = FALSE;
3000
3001 src = GST_RTSPSRC_CAST (parent);
3002
3003 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
3004 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
3005
3006 switch (GST_QUERY_TYPE (query)) {
3007 case GST_QUERY_DURATION:
3008 {
3009 GstFormat format;
3010
3011 gst_query_parse_duration (query, &format, NULL);
3012
3013 switch (format) {
3014 case GST_FORMAT_TIME:
3015 gst_query_set_duration (query, format, src->segment.duration);
3016 res = TRUE;
3017 break;
3018 default:
3019 break;
3020 }
3021 break;
3022 }
3023 case GST_QUERY_SEEKING:
3024 {
3025 GstFormat format;
3026
3027 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
3028 if (format == GST_FORMAT_TIME) {
3029 gboolean seekable =
3030 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
3031 GstClockTime start = 0, duration = src->segment.duration;
3032
3033 /* seeking without duration is unlikely */
3034 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
3035 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
3036
3037 if (seekable) {
3038 if (src->seekable > 0.0) {
3039 start = src->last_pos - src->seekable * GST_SECOND;
3040 } else {
3041 /* src->seekable == 0 means that we can only seek to 0 */
3042 start = 0;
3043 duration = 0;
3044 }
3045 }
3046
3047 GST_LOG_OBJECT (src, "seekable : %d", seekable);
3048
3049 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
3050 duration);
3051 res = TRUE;
3052 }
3053 break;
3054 }
3055 case GST_QUERY_URI:
3056 {
3057 gchar *uri;
3058
3059 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
3060 if (uri != NULL) {
3061 gst_query_set_uri (query, uri);
3062 g_free (uri);
3063 res = TRUE;
3064 }
3065 break;
3066 }
3067 default:
3068 {
3069 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
3070
3071 /* forward the query to the proxy target pad */
3072 if (target) {
3073 res = gst_pad_query (target, query);
3074 gst_object_unref (target);
3075 }
3076 break;
3077 }
3078 }
3079
3080 return res;
3081 }
3082
3083 /* callback for RTCP messages to be sent to the server when operating in TCP
3084 * mode. */
3085 static GstFlowReturn
gst_rtspsrc_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)3086 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
3087 {
3088 GstRTSPSrc *src;
3089 GstRTSPStream *stream;
3090 GstFlowReturn res = GST_FLOW_OK;
3091 GstMapInfo map;
3092 guint8 *data;
3093 guint size;
3094 GstRTSPResult ret;
3095 GstRTSPMessage message = { 0 };
3096 GstRTSPConnInfo *conninfo;
3097
3098 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
3099 src = stream->parent;
3100
3101 gst_buffer_map (buffer, &map, GST_MAP_READ);
3102 size = map.size;
3103 data = map.data;
3104
3105 gst_rtsp_message_init_data (&message, stream->channel[1]);
3106
3107 /* lend the body data to the message */
3108 gst_rtsp_message_take_body (&message, data, size);
3109
3110 if (stream->conninfo.connection)
3111 conninfo = &stream->conninfo;
3112 else
3113 conninfo = &src->conninfo;
3114
3115 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
3116 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
3117 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
3118
3119 /* and steal it away again because we will free it when unreffing the
3120 * buffer */
3121 gst_rtsp_message_steal_body (&message, &data, &size);
3122 gst_rtsp_message_unset (&message);
3123
3124 gst_buffer_unmap (buffer, &map);
3125 gst_buffer_unref (buffer);
3126
3127 return res;
3128 }
3129
3130 static GstFlowReturn
gst_rtspsrc_push_backchannel_buffer(GstRTSPSrc * src,guint id,GstSample * sample)3131 gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src, guint id,
3132 GstSample * sample)
3133 {
3134 GstFlowReturn res = GST_FLOW_OK;
3135 GstRTSPStream *stream;
3136
3137 if (!src->conninfo.connected || src->state != GST_RTSP_STATE_PLAYING)
3138 goto out;
3139
3140 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3141 if (stream == NULL) {
3142 GST_ERROR_OBJECT (src, "no stream with id %u", id);
3143 goto out;
3144 }
3145
3146 if (src->interleaved) {
3147 GstBuffer *buffer;
3148 GstMapInfo map;
3149 guint8 *data;
3150 guint size;
3151 GstRTSPResult ret;
3152 GstRTSPMessage message = { 0 };
3153 GstRTSPConnInfo *conninfo;
3154
3155 buffer = gst_sample_get_buffer (sample);
3156
3157 gst_buffer_map (buffer, &map, GST_MAP_READ);
3158 size = map.size;
3159 data = map.data;
3160
3161 gst_rtsp_message_init_data (&message, stream->channel[0]);
3162
3163 /* lend the body data to the message */
3164 gst_rtsp_message_take_body (&message, data, size);
3165
3166 if (stream->conninfo.connection)
3167 conninfo = &stream->conninfo;
3168 else
3169 conninfo = &src->conninfo;
3170
3171 GST_DEBUG_OBJECT (src, "sending %u bytes backchannel RTP", size);
3172 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
3173 GST_DEBUG_OBJECT (src, "sent backchannel RTP, %d", ret);
3174
3175 /* and steal it away again because we will free it when unreffing the
3176 * buffer */
3177 gst_rtsp_message_steal_body (&message, &data, &size);
3178 gst_rtsp_message_unset (&message);
3179
3180 gst_buffer_unmap (buffer, &map);
3181
3182 res = GST_FLOW_OK;
3183 } else {
3184 g_signal_emit_by_name (stream->rtpsrc, "push-sample", sample, &res);
3185 GST_DEBUG_OBJECT (src, "sent backchannel RTP sample %p: %s", sample,
3186 gst_flow_get_name (res));
3187 }
3188
3189 out:
3190 gst_sample_unref (sample);
3191
3192 return res;
3193 }
3194
3195 static GstPadProbeReturn
pad_blocked(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)3196 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3197 {
3198 GstRTSPSrc *src = user_data;
3199
3200 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
3201 GST_DEBUG_PAD_NAME (pad));
3202
3203 /* activate the streams */
3204 GST_OBJECT_LOCK (src);
3205 if (!src->need_activate)
3206 goto was_ok;
3207
3208 src->need_activate = FALSE;
3209 GST_OBJECT_UNLOCK (src);
3210
3211 gst_rtspsrc_activate_streams (src);
3212
3213 return GST_PAD_PROBE_OK;
3214
3215 was_ok:
3216 {
3217 GST_OBJECT_UNLOCK (src);
3218 return GST_PAD_PROBE_OK;
3219 }
3220 }
3221
3222 static GstPadProbeReturn
udpsrc_probe_cb(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)3223 udpsrc_probe_cb (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3224 {
3225 guint32 *segment_seqnum = user_data;
3226
3227 switch (GST_EVENT_TYPE (info->data)) {
3228 case GST_EVENT_SEGMENT:
3229 if (!gst_event_is_writable (info->data))
3230 info->data = gst_event_make_writable (info->data);
3231
3232 *segment_seqnum = gst_event_get_seqnum (info->data);
3233 default:
3234 break;
3235 }
3236
3237 return GST_PAD_PROBE_OK;
3238 }
3239
3240 static gboolean
copy_sticky_events(GstPad * pad,GstEvent ** event,gpointer user_data)3241 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
3242 {
3243 GstPad *gpad = GST_PAD_CAST (user_data);
3244
3245 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
3246 gst_pad_store_sticky_event (gpad, *event);
3247
3248 return TRUE;
3249 }
3250
3251 static gboolean
add_backchannel_fakesink(GstRTSPSrc * src,GstRTSPStream * stream,GstPad * srcpad)3252 add_backchannel_fakesink (GstRTSPSrc * src, GstRTSPStream * stream,
3253 GstPad * srcpad)
3254 {
3255 GstPad *sinkpad;
3256 GstElement *fakesink;
3257
3258 fakesink = gst_element_factory_make ("fakesink", NULL);
3259 if (fakesink == NULL) {
3260 GST_ERROR_OBJECT (src, "no fakesink");
3261 return FALSE;
3262 }
3263
3264 sinkpad = gst_element_get_static_pad (fakesink, "sink");
3265
3266 GST_DEBUG_OBJECT (src, "backchannel stream %p, hooking fakesink", stream);
3267
3268 gst_bin_add (GST_BIN_CAST (src), fakesink);
3269 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
3270 GST_WARNING_OBJECT (src, "could not link to fakesink");
3271 return FALSE;
3272 }
3273
3274 gst_object_unref (sinkpad);
3275
3276 gst_element_sync_state_with_parent (fakesink);
3277 return TRUE;
3278 }
3279
3280 /* this callback is called when the session manager generated a new src pad with
3281 * payloaded RTP packets. We simply ghost the pad here. */
3282 static void
new_manager_pad(GstElement * manager,GstPad * pad,GstRTSPSrc * src)3283 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
3284 {
3285 gchar *name;
3286 GstPadTemplate *template;
3287 gint id, ssrc, pt;
3288 GList *ostreams;
3289 GstRTSPStream *stream;
3290 gboolean all_added;
3291 GstPad *internal_src;
3292
3293 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3294
3295 GST_RTSP_STATE_LOCK (src);
3296 /* find stream */
3297 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3298 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3299 goto unknown_stream;
3300
3301 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3302
3303 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3304 if (stream == NULL)
3305 goto unknown_stream;
3306
3307 /* save SSRC */
3308 stream->ssrc = ssrc;
3309
3310 /* we'll add it later see below */
3311 stream->added = TRUE;
3312
3313 /* check if we added all streams */
3314 all_added = TRUE;
3315 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3316 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3317
3318 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3319 ostream, ostream->container, ostream->added, ostream->setup);
3320
3321 /* if we find a stream for which we did a setup that is not added, we
3322 * need to wait some more */
3323 if (ostream->setup && !ostream->added) {
3324 all_added = FALSE;
3325 break;
3326 }
3327 }
3328 GST_RTSP_STATE_UNLOCK (src);
3329
3330 /* create a new pad we will use to stream to */
3331 template = gst_static_pad_template_get (&rtptemplate);
3332 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3333 gst_object_unref (template);
3334 g_free (name);
3335
3336 /* We intercept and modify the stream start event */
3337 internal_src =
3338 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
3339 gst_pad_set_element_private (internal_src, stream);
3340 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
3341 gst_object_unref (internal_src);
3342
3343 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3344 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3345 gst_pad_set_active (stream->srcpad, TRUE);
3346 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3347
3348 /* don't add the srcpad if this is a sendonly stream */
3349 if (stream->is_backchannel)
3350 add_backchannel_fakesink (src, stream, stream->srcpad);
3351 else
3352 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3353
3354 if (all_added) {
3355 GST_DEBUG_OBJECT (src, "We added all streams");
3356 /* when we get here, all stream are added and we can fire the no-more-pads
3357 * signal. */
3358 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3359 }
3360
3361 return;
3362
3363 /* ERRORS */
3364 unknown_stream:
3365 {
3366 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3367 GST_RTSP_STATE_UNLOCK (src);
3368 g_free (name);
3369 return;
3370 }
3371 }
3372
3373 static GstCaps *
stream_get_caps_for_pt(GstRTSPStream * stream,guint pt)3374 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3375 {
3376 guint i, len;
3377
3378 len = stream->ptmap->len;
3379 for (i = 0; i < len; i++) {
3380 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3381 if (item->pt == pt)
3382 return item->caps;
3383 }
3384 return NULL;
3385 }
3386
3387 static GstCaps *
request_pt_map(GstElement * manager,guint session,guint pt,GstRTSPSrc * src)3388 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3389 {
3390 GstRTSPStream *stream;
3391 GstCaps *caps;
3392
3393 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3394
3395 GST_RTSP_STATE_LOCK (src);
3396 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3397 if (!stream)
3398 goto unknown_stream;
3399
3400 if ((caps = stream_get_caps_for_pt (stream, pt)))
3401 gst_caps_ref (caps);
3402 GST_RTSP_STATE_UNLOCK (src);
3403
3404 return caps;
3405
3406 unknown_stream:
3407 {
3408 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3409 GST_RTSP_STATE_UNLOCK (src);
3410 return NULL;
3411 }
3412 }
3413
3414 static void
gst_rtspsrc_do_stream_eos(GstRTSPSrc * src,GstRTSPStream * stream)3415 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3416 {
3417 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3418
3419 if (stream->eos)
3420 goto was_eos;
3421
3422 stream->eos = TRUE;
3423 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3424 return;
3425
3426 /* ERRORS */
3427 was_eos:
3428 {
3429 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3430 return;
3431 }
3432 }
3433
3434 static void
on_bye_ssrc(GObject * session,GObject * source,GstRTSPStream * stream)3435 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3436 {
3437 GstRTSPSrc *src = stream->parent;
3438 guint ssrc;
3439
3440 g_object_get (source, "ssrc", &ssrc, NULL);
3441
3442 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3443 ssrc, stream->ssrc, stream->id);
3444
3445 if (ssrc == stream->ssrc)
3446 gst_rtspsrc_do_stream_eos (src, stream);
3447 }
3448
3449 static void
on_timeout_common(GObject * session,GObject * source,GstRTSPStream * stream)3450 on_timeout_common (GObject * session, GObject * source, GstRTSPStream * stream)
3451 {
3452 GstRTSPSrc *src = stream->parent;
3453 guint ssrc;
3454
3455 g_object_get (source, "ssrc", &ssrc, NULL);
3456
3457 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3458 ssrc, stream->ssrc, stream->id);
3459
3460 if (ssrc == stream->ssrc)
3461 gst_rtspsrc_do_stream_eos (src, stream);
3462 }
3463
3464 static void
on_timeout(GObject * session,GObject * source,GstRTSPStream * stream)3465 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3466 {
3467 GstRTSPSrc *src = stream->parent;
3468
3469 /* timeout, post element message */
3470 gst_element_post_message (GST_ELEMENT_CAST (src),
3471 gst_message_new_element (GST_OBJECT_CAST (src),
3472 gst_structure_new ("GstRTSPSrcTimeout",
3473 "cause", G_TYPE_ENUM, GST_RTSP_SRC_TIMEOUT_CAUSE_RTCP,
3474 "stream-number", G_TYPE_INT, stream->id, "ssrc", G_TYPE_UINT,
3475 stream->ssrc, NULL)));
3476
3477 on_timeout_common (session, source, stream);
3478 }
3479
3480 static void
on_npt_stop(GstElement * rtpbin,guint session,guint ssrc,GstRTSPSrc * src)3481 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3482 {
3483 GstRTSPStream *stream;
3484
3485 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3486
3487 /* get stream for session */
3488 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3489 if (stream) {
3490 gst_rtspsrc_do_stream_eos (src, stream);
3491 }
3492 }
3493
3494 static void
on_ssrc_active(GObject * session,GObject * source,GstRTSPStream * stream)3495 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3496 {
3497 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3498 stream->id);
3499 }
3500
3501 static void
set_manager_buffer_mode(GstRTSPSrc * src)3502 set_manager_buffer_mode (GstRTSPSrc * src)
3503 {
3504 GObjectClass *klass;
3505
3506 if (src->manager == NULL)
3507 return;
3508
3509 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3510
3511 if (!g_object_class_find_property (klass, "buffer-mode"))
3512 return;
3513
3514 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3515 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3516
3517 return;
3518 }
3519
3520 GST_DEBUG_OBJECT (src,
3521 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3522
3523 if (src->provided_clock) {
3524 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3525
3526 if (clock == src->provided_clock) {
3527 GST_DEBUG_OBJECT (src, "selected synced");
3528 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3529
3530 if (clock)
3531 gst_object_unref (clock);
3532
3533 return;
3534 }
3535
3536 /* Otherwise fall-through and use another buffer mode */
3537 if (clock)
3538 gst_object_unref (clock);
3539 }
3540
3541 GST_DEBUG_OBJECT (src, "auto buffering mode");
3542 if (src->use_buffering) {
3543 GST_DEBUG_OBJECT (src, "selected buffer");
3544 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3545 } else {
3546 GST_DEBUG_OBJECT (src, "selected slave");
3547 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3548 }
3549 }
3550
3551 static GstCaps *
request_key(GstElement * srtpdec,guint ssrc,GstRTSPStream * stream)3552 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3553 {
3554 guint i;
3555 GstCaps *caps;
3556 GstMIKEYMessage *msg = stream->mikey;
3557
3558 GST_DEBUG ("request key SSRC %u", ssrc);
3559
3560 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3561 caps = gst_caps_make_writable (caps);
3562
3563 /* parse crypto sessions and look for the SSRC rollover counter */
3564 msg = stream->mikey;
3565 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3566 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3567
3568 if (ssrc == map->ssrc) {
3569 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3570 break;
3571 }
3572 }
3573
3574 return caps;
3575 }
3576
3577 static GstElement *
request_rtp_decoder(GstElement * rtpbin,guint session,GstRTSPStream * stream)3578 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3579 {
3580 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3581 if (stream->id != session)
3582 return NULL;
3583
3584 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3585 stream->profile != GST_RTSP_PROFILE_SAVPF)
3586 return NULL;
3587
3588 if (stream->srtpdec == NULL) {
3589 gchar *name;
3590
3591 name = g_strdup_printf ("srtpdec_%u", session);
3592 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3593 g_free (name);
3594
3595 if (stream->srtpdec == NULL) {
3596 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3597 ("no srtpdec element present!"));
3598 return NULL;
3599 }
3600 g_signal_connect (stream->srtpdec, "request-key",
3601 (GCallback) request_key, stream);
3602 }
3603 return gst_object_ref (stream->srtpdec);
3604 }
3605
3606 static GstElement *
request_rtcp_encoder(GstElement * rtpbin,guint session,GstRTSPStream * stream)3607 request_rtcp_encoder (GstElement * rtpbin, guint session,
3608 GstRTSPStream * stream)
3609 {
3610 gchar *name;
3611 GstPad *pad;
3612
3613 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3614 if (stream->id != session)
3615 return NULL;
3616
3617 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3618 stream->profile != GST_RTSP_PROFILE_SAVPF)
3619 return NULL;
3620
3621 if (stream->srtpenc == NULL) {
3622 GstStructure *s;
3623
3624 name = g_strdup_printf ("srtpenc_%u", session);
3625 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3626 g_free (name);
3627
3628 if (stream->srtpenc == NULL) {
3629 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3630 ("no srtpenc element present!"));
3631 return NULL;
3632 }
3633
3634 /* get RTCP crypto parameters from caps */
3635 s = gst_caps_get_structure (stream->srtcpparams, 0);
3636 if (s) {
3637 GstBuffer *buf;
3638 const gchar *str;
3639 GType ciphertype, authtype;
3640 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3641
3642 ciphertype = g_type_from_name ("GstSrtpCipherType");
3643 authtype = g_type_from_name ("GstSrtpAuthType");
3644 g_value_init (&rtcp_cipher, ciphertype);
3645 g_value_init (&rtcp_auth, authtype);
3646
3647 str = gst_structure_get_string (s, "srtcp-cipher");
3648 gst_value_deserialize (&rtcp_cipher, str);
3649 str = gst_structure_get_string (s, "srtcp-auth");
3650 gst_value_deserialize (&rtcp_auth, str);
3651 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3652
3653 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3654 &rtcp_cipher);
3655 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3656 &rtcp_auth);
3657 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3658 &rtcp_cipher);
3659 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3660 &rtcp_auth);
3661 g_object_set (stream->srtpenc, "key", buf, NULL);
3662
3663 g_value_unset (&rtcp_cipher);
3664 g_value_unset (&rtcp_auth);
3665 gst_buffer_unref (buf);
3666 }
3667 }
3668 name = g_strdup_printf ("rtcp_sink_%d", session);
3669 pad = gst_element_get_request_pad (stream->srtpenc, name);
3670 g_free (name);
3671 gst_object_unref (pad);
3672
3673 return gst_object_ref (stream->srtpenc);
3674 }
3675
3676 static GstElement *
request_aux_receiver(GstElement * rtpbin,guint sessid,GstRTSPSrc * src)3677 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3678 {
3679 GstElement *rtx, *bin;
3680 GstPad *pad;
3681 gchar *name;
3682 GstRTSPStream *stream;
3683
3684 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3685 if (!stream) {
3686 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3687 return NULL;
3688 }
3689
3690 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3691 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3692 bin = gst_bin_new (NULL);
3693 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3694 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3695 gst_bin_add (GST_BIN (bin), rtx);
3696
3697 pad = gst_element_get_static_pad (rtx, "src");
3698 name = g_strdup_printf ("src_%u", sessid);
3699 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3700 g_free (name);
3701 gst_object_unref (pad);
3702
3703 pad = gst_element_get_static_pad (rtx, "sink");
3704 name = g_strdup_printf ("sink_%u", sessid);
3705 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3706 g_free (name);
3707 gst_object_unref (pad);
3708
3709 return bin;
3710 }
3711
3712 static void
add_retransmission(GstRTSPSrc * src,GstRTSPTransport * transport)3713 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3714 {
3715 GList *walk;
3716 guint signal_id;
3717 gboolean do_retransmission = FALSE;
3718
3719 if (transport->trans != GST_RTSP_TRANS_RTP)
3720 return;
3721 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3722 transport->profile != GST_RTSP_PROFILE_SAVPF)
3723 return;
3724
3725 signal_id = g_signal_lookup ("request-aux-receiver",
3726 G_OBJECT_TYPE (src->manager));
3727 /* there's already something connected */
3728 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3729 NULL, NULL, NULL) != 0) {
3730 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3731 "\"request-aux-receiver\" signal is "
3732 "already used by the application");
3733 return;
3734 }
3735
3736 /* build the retransmission payload type map */
3737 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3738 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3739 gboolean do_retransmission_stream = FALSE;
3740 int i;
3741
3742 if (stream->rtx_pt_map)
3743 gst_structure_free (stream->rtx_pt_map);
3744 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3745
3746 for (i = 0; i < stream->ptmap->len; i++) {
3747 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3748 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3749 const gchar *encoding;
3750
3751 /* we only care about RTX streams */
3752 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3753 && g_strcmp0 (encoding, "RTX") == 0) {
3754 const gchar *stream_pt_s;
3755 gint rtx_pt;
3756
3757 if (gst_structure_get_int (s, "payload", &rtx_pt)
3758 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3759
3760 if (rtx_pt != 0) {
3761 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3762 rtx_pt, NULL);
3763 do_retransmission_stream = TRUE;
3764 }
3765 }
3766 }
3767 }
3768
3769 if (do_retransmission_stream) {
3770 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3771 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3772 do_retransmission = TRUE;
3773 } else {
3774 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3775 "id %i", stream->id);
3776 gst_structure_free (stream->rtx_pt_map);
3777 stream->rtx_pt_map = NULL;
3778 }
3779 }
3780
3781 if (do_retransmission) {
3782 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3783
3784 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3785
3786 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3787 * as the "aux" element of rtpbin */
3788 g_signal_connect (src->manager, "request-aux-receiver",
3789 (GCallback) request_aux_receiver, src);
3790 } else {
3791 GST_DEBUG_OBJECT (src,
3792 "Not enabling retransmissions as no stream had a retransmission payload map");
3793 }
3794 }
3795
3796 /* try to get and configure a manager */
3797 static gboolean
gst_rtspsrc_stream_configure_manager(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPTransport * transport)3798 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3799 GstRTSPTransport * transport)
3800 {
3801 const gchar *manager;
3802 gchar *name;
3803 GstStateChangeReturn ret;
3804
3805 /* find a manager */
3806 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3807 goto no_manager;
3808
3809 if (manager) {
3810 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3811
3812 /* configure the manager */
3813 if (src->manager == NULL) {
3814 GObjectClass *klass;
3815
3816 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3817 /* fallback */
3818 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3819 goto no_manager;
3820
3821 if (!manager)
3822 goto use_no_manager;
3823
3824 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3825 goto manager_failed;
3826 }
3827
3828 /* we manage this element */
3829 gst_element_set_locked_state (src->manager, TRUE);
3830 gst_bin_add (GST_BIN_CAST (src), src->manager);
3831
3832 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3833 if (ret == GST_STATE_CHANGE_FAILURE)
3834 goto start_manager_failure;
3835
3836 g_object_set (src->manager, "latency", src->latency, NULL);
3837
3838 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3839
3840 if (g_object_class_find_property (klass, "ntp-sync")) {
3841 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3842 }
3843
3844 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3845 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3846 }
3847
3848 if (src->use_pipeline_clock) {
3849 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3850 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3851 }
3852 } else {
3853 if (g_object_class_find_property (klass, "ntp-time-source")) {
3854 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3855 NULL);
3856 }
3857 }
3858
3859 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3860 g_object_set (src->manager, "sdes", src->sdes, NULL);
3861 }
3862
3863 if (g_object_class_find_property (klass, "drop-on-latency")) {
3864 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3865 NULL);
3866 }
3867
3868 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3869 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3870 src->max_rtcp_rtp_time_diff, NULL);
3871 }
3872
3873 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3874 g_object_set (src->manager, "max-ts-offset-adjustment",
3875 src->max_ts_offset_adjustment, NULL);
3876 }
3877
3878 if (g_object_class_find_property (klass, "max-ts-offset")) {
3879 gint64 max_ts_offset;
3880
3881 /* setting max-ts-offset in the manager has side effects so only do it
3882 * if the value differs */
3883 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
3884 if (max_ts_offset != src->max_ts_offset) {
3885 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
3886 NULL);
3887 }
3888 }
3889
3890 /* buffer mode pauses are handled by adding offsets to buffer times,
3891 * but some depayloaders may have a hard time syncing output times
3892 * with such input times, e.g. container ones, most notably ASF */
3893 /* TODO alternatives are having an event that indicates these shifts,
3894 * or having rtsp extensions provide suggestion on buffer mode */
3895 /* valid duration implies not likely live pipeline,
3896 * so slaving in jitterbuffer does not make much sense
3897 * (and might mess things up due to bursts) */
3898 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3899 src->segment.duration && stream->container) {
3900 src->use_buffering = TRUE;
3901 } else {
3902 src->use_buffering = FALSE;
3903 }
3904
3905 set_manager_buffer_mode (src);
3906
3907 /* connect to signals */
3908 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3909 stream);
3910 src->manager_sig_id =
3911 g_signal_connect (src->manager, "pad-added",
3912 (GCallback) new_manager_pad, src);
3913 src->manager_ptmap_id =
3914 g_signal_connect (src->manager, "request-pt-map",
3915 (GCallback) request_pt_map, src);
3916
3917 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3918 src);
3919
3920 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3921 src->manager);
3922
3923 if (src->do_retransmission)
3924 add_retransmission (src, transport);
3925 }
3926 g_signal_connect (src->manager, "request-rtp-decoder",
3927 (GCallback) request_rtp_decoder, stream);
3928 g_signal_connect (src->manager, "request-rtcp-decoder",
3929 (GCallback) request_rtp_decoder, stream);
3930 g_signal_connect (src->manager, "request-rtcp-encoder",
3931 (GCallback) request_rtcp_encoder, stream);
3932
3933 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3934 * into a separate RTP session. */
3935 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3936 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3937 g_free (name);
3938 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3939 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3940 g_free (name);
3941
3942 /* now configure the bandwidth in the manager */
3943 if (g_signal_lookup ("get-internal-session",
3944 G_OBJECT_TYPE (src->manager)) != 0) {
3945 GObject *rtpsession;
3946
3947 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3948 &rtpsession);
3949 if (rtpsession) {
3950 GstRTPProfile rtp_profile;
3951
3952 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3953
3954 stream->session = rtpsession;
3955
3956 if (stream->as_bandwidth != -1) {
3957 GST_INFO_OBJECT (src, "setting AS: %f",
3958 (gdouble) (stream->as_bandwidth * 1000));
3959 g_object_set (rtpsession, "bandwidth",
3960 (gdouble) (stream->as_bandwidth * 1000), NULL);
3961 }
3962 if (stream->rr_bandwidth != -1) {
3963 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3964 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3965 NULL);
3966 }
3967 if (stream->rs_bandwidth != -1) {
3968 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3969 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3970 NULL);
3971 }
3972
3973 switch (stream->profile) {
3974 case GST_RTSP_PROFILE_AVPF:
3975 rtp_profile = GST_RTP_PROFILE_AVPF;
3976 break;
3977 case GST_RTSP_PROFILE_SAVP:
3978 rtp_profile = GST_RTP_PROFILE_SAVP;
3979 break;
3980 case GST_RTSP_PROFILE_SAVPF:
3981 rtp_profile = GST_RTP_PROFILE_SAVPF;
3982 break;
3983 case GST_RTSP_PROFILE_AVP:
3984 default:
3985 rtp_profile = GST_RTP_PROFILE_AVP;
3986 break;
3987 }
3988
3989 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3990
3991 g_object_set (rtpsession, "probation", src->probation, NULL);
3992
3993 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3994
3995 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3996 stream);
3997 g_signal_connect (rtpsession, "on-bye-timeout",
3998 (GCallback) on_timeout_common, stream);
3999 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
4000 stream);
4001 g_signal_connect (rtpsession, "on-ssrc-active",
4002 (GCallback) on_ssrc_active, stream);
4003 }
4004 }
4005 }
4006
4007 use_no_manager:
4008 return TRUE;
4009
4010 /* ERRORS */
4011 no_manager:
4012 {
4013 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4014 return FALSE;
4015 }
4016 manager_failed:
4017 {
4018 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
4019 return FALSE;
4020 }
4021 start_manager_failure:
4022 {
4023 GST_DEBUG_OBJECT (src, "could not start session manager");
4024 return FALSE;
4025 }
4026 }
4027
4028 /* free the UDP sources allocated when negotiating a transport.
4029 * This function is called when the server negotiated to a transport where the
4030 * UDP sources are not needed anymore, such as TCP or multicast. */
4031 static void
gst_rtspsrc_stream_free_udp(GstRTSPStream * stream)4032 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
4033 {
4034 gint i;
4035
4036 for (i = 0; i < 2; i++) {
4037 if (stream->udpsrc[i]) {
4038 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
4039 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
4040 gst_object_unref (stream->udpsrc[i]);
4041 stream->udpsrc[i] = NULL;
4042 }
4043 }
4044 }
4045
4046 /* for TCP, create pads to send and receive data to and from the manager and to
4047 * intercept various events and queries
4048 */
4049 static gboolean
gst_rtspsrc_stream_configure_tcp(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPTransport * transport,GstPad ** outpad)4050 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
4051 GstRTSPTransport * transport, GstPad ** outpad)
4052 {
4053 gchar *name;
4054 GstPadTemplate *template;
4055 GstPad *pad0, *pad1;
4056
4057 /* configure for interleaved delivery, nothing needs to be done
4058 * here, the loop function will call the chain functions of the
4059 * session manager. */
4060 stream->channel[0] = transport->interleaved.min;
4061 stream->channel[1] = transport->interleaved.max;
4062 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
4063 stream->channel[0], stream->channel[1]);
4064
4065 /* we can remove the allocated UDP ports now */
4066 gst_rtspsrc_stream_free_udp (stream);
4067
4068 /* no session manager, send data to srcpad directly */
4069 if (!stream->channelpad[0]) {
4070 GST_DEBUG_OBJECT (src, "no manager, creating pad");
4071
4072 /* create a new pad we will use to stream to */
4073 name = g_strdup_printf ("stream_%u", stream->id);
4074 template = gst_static_pad_template_get (&rtptemplate);
4075 stream->channelpad[0] = gst_pad_new_from_template (template, name);
4076 gst_object_unref (template);
4077 g_free (name);
4078
4079 /* set caps and activate */
4080 gst_pad_use_fixed_caps (stream->channelpad[0]);
4081 gst_pad_set_active (stream->channelpad[0], TRUE);
4082
4083 *outpad = gst_object_ref (stream->channelpad[0]);
4084 } else {
4085 GST_DEBUG_OBJECT (src, "using manager source pad");
4086
4087 template = gst_static_pad_template_get (&anysrctemplate);
4088
4089 /* allocate pads for sending the channel data into the manager */
4090 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
4091 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
4092 gst_object_unref (stream->channelpad[0]);
4093 stream->channelpad[0] = pad0;
4094 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
4095 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
4096 gst_pad_set_element_private (pad0, src);
4097 gst_pad_set_active (pad0, TRUE);
4098
4099 if (stream->channelpad[1]) {
4100 /* if we have a sinkpad for the other channel, create a pad and link to the
4101 * manager. */
4102 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
4103 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
4104 gst_pad_link_full (pad1, stream->channelpad[1],
4105 GST_PAD_LINK_CHECK_NOTHING);
4106 gst_object_unref (stream->channelpad[1]);
4107 stream->channelpad[1] = pad1;
4108 gst_pad_set_active (pad1, TRUE);
4109 }
4110 gst_object_unref (template);
4111 }
4112 /* setup RTCP transport back to the server if we have to. */
4113 if (src->manager && src->do_rtcp) {
4114 GstPad *pad;
4115
4116 template = gst_static_pad_template_get (&anysinktemplate);
4117
4118 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
4119 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
4120 gst_pad_set_element_private (stream->rtcppad, stream);
4121 gst_pad_set_active (stream->rtcppad, TRUE);
4122
4123 /* get session RTCP pad */
4124 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4125 pad = gst_element_get_request_pad (src->manager, name);
4126 g_free (name);
4127
4128 /* and link */
4129 if (pad) {
4130 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4131 gst_object_unref (pad);
4132 }
4133
4134 gst_object_unref (template);
4135 }
4136 return TRUE;
4137 }
4138
4139 static void
gst_rtspsrc_get_transport_info(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPTransport * transport,const gchar ** destination,gint * min,gint * max,guint * ttl)4140 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
4141 GstRTSPTransport * transport, const gchar ** destination, gint * min,
4142 gint * max, guint * ttl)
4143 {
4144 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4145 if (destination) {
4146 if (!(*destination = transport->destination))
4147 *destination = stream->destination;
4148 }
4149 if (min && max) {
4150 /* transport first */
4151 *min = transport->port.min;
4152 *max = transport->port.max;
4153 if (*min == -1 && *max == -1) {
4154 /* then try from SDP */
4155 if (stream->port != 0) {
4156 *min = stream->port;
4157 *max = stream->port + 1;
4158 }
4159 }
4160 }
4161
4162 if (ttl) {
4163 if (!(*ttl = transport->ttl))
4164 *ttl = stream->ttl;
4165 }
4166 } else {
4167 if (destination) {
4168 /* first take the source, then the endpoint to figure out where to send
4169 * the RTCP. */
4170 if (!(*destination = transport->source)) {
4171 if (src->conninfo.connection)
4172 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
4173 else if (stream->conninfo.connection)
4174 *destination =
4175 gst_rtsp_connection_get_ip (stream->conninfo.connection);
4176 }
4177 }
4178 if (min && max) {
4179 /* for unicast we only expect the ports here */
4180 *min = transport->server_port.min;
4181 *max = transport->server_port.max;
4182 }
4183 }
4184 }
4185
4186 /* For multicast create UDP sources and join the multicast group. */
4187 static gboolean
gst_rtspsrc_stream_configure_mcast(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPTransport * transport,GstPad ** outpad)4188 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
4189 GstRTSPTransport * transport, GstPad ** outpad)
4190 {
4191 gchar *uri;
4192 const gchar *destination;
4193 gint min, max;
4194
4195 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
4196
4197 /* we can remove the allocated UDP ports now */
4198 gst_rtspsrc_stream_free_udp (stream);
4199
4200 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
4201 &max, NULL);
4202
4203 /* we need a destination now */
4204 if (destination == NULL)
4205 goto no_destination;
4206
4207 /* we really need ports now or we won't be able to receive anything at all */
4208 if (min == -1 && max == -1)
4209 goto no_ports;
4210
4211 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
4212 destination, min, max);
4213
4214 /* creating UDP source for RTP */
4215 if (min != -1) {
4216 uri = g_strdup_printf ("udp://%s:%d", destination, min);
4217 stream->udpsrc[0] =
4218 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
4219 g_free (uri);
4220 if (stream->udpsrc[0] == NULL)
4221 goto no_element;
4222
4223 /* take ownership */
4224 gst_object_ref_sink (stream->udpsrc[0]);
4225
4226 if (src->udp_buffer_size != 0)
4227 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
4228 src->udp_buffer_size, NULL);
4229
4230 if (src->multi_iface != NULL)
4231 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
4232 src->multi_iface, NULL);
4233
4234 /* change state */
4235 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4236 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
4237 }
4238
4239 /* creating another UDP source for RTCP */
4240 if (max != -1) {
4241 GstCaps *caps;
4242
4243 uri = g_strdup_printf ("udp://%s:%d", destination, max);
4244 stream->udpsrc[1] =
4245 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
4246 g_free (uri);
4247 if (stream->udpsrc[1] == NULL)
4248 goto no_element;
4249
4250 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4251 stream->profile == GST_RTSP_PROFILE_SAVPF)
4252 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4253 else
4254 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4255 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4256 gst_caps_unref (caps);
4257
4258 /* take ownership */
4259 gst_object_ref_sink (stream->udpsrc[1]);
4260
4261 if (src->multi_iface != NULL)
4262 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
4263 src->multi_iface, NULL);
4264
4265 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
4266 }
4267 return TRUE;
4268
4269 /* ERRORS */
4270 no_element:
4271 {
4272 GST_DEBUG_OBJECT (src, "no UDP source element found");
4273 return FALSE;
4274 }
4275 no_destination:
4276 {
4277 GST_DEBUG_OBJECT (src, "no destination found");
4278 return FALSE;
4279 }
4280 no_ports:
4281 {
4282 GST_DEBUG_OBJECT (src, "no ports found");
4283 return FALSE;
4284 }
4285 }
4286
4287 /* configure the remainder of the UDP ports */
4288 static gboolean
gst_rtspsrc_stream_configure_udp(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPTransport * transport,GstPad ** outpad)4289 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
4290 GstRTSPTransport * transport, GstPad ** outpad)
4291 {
4292 /* we manage the UDP elements now. For unicast, the UDP sources where
4293 * allocated in the stream when we suggested a transport. */
4294 if (stream->udpsrc[0]) {
4295 GstCaps *caps;
4296
4297 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
4298 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
4299
4300 GST_DEBUG_OBJECT (src, "setting up UDP source");
4301
4302 /* configure a timeout on the UDP port. When the timeout message is
4303 * posted, we assume UDP transport is not possible. We reconnect using TCP
4304 * if we can. */
4305 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
4306 src->udp_timeout * 1000, NULL);
4307
4308 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
4309 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4310
4311 /* get output pad of the UDP source. */
4312 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
4313
4314 /* save it so we can unblock */
4315 stream->blockedpad = *outpad;
4316
4317 /* configure pad block on the pad. As soon as there is dataflow on the
4318 * UDP source, we know that UDP is not blocked by a firewall and we can
4319 * configure all the streams to let the application autoplug decoders. */
4320 stream->blockid =
4321 gst_pad_add_probe (stream->blockedpad,
4322 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4323 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4324
4325 gst_pad_add_probe (stream->blockedpad,
4326 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4327 &(stream->segment_seqnum[0]), NULL);
4328
4329 if (stream->channelpad[0]) {
4330 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4331 /* configure for UDP delivery, we need to connect the UDP pads to
4332 * the session plugin. */
4333 gst_pad_link_full (*outpad, stream->channelpad[0],
4334 GST_PAD_LINK_CHECK_NOTHING);
4335 gst_object_unref (*outpad);
4336 *outpad = NULL;
4337 /* we connected to pad-added signal to get pads from the manager */
4338 } else {
4339 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4340 }
4341 }
4342
4343 /* RTCP port */
4344 if (stream->udpsrc[1]) {
4345 GstCaps *caps;
4346
4347 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4348 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4349
4350 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4351 stream->profile == GST_RTSP_PROFILE_SAVPF)
4352 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4353 else
4354 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4355 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4356 gst_caps_unref (caps);
4357
4358 if (stream->channelpad[1]) {
4359 GstPad *pad;
4360
4361 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4362
4363 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4364 gst_pad_add_probe (pad,
4365 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, udpsrc_probe_cb,
4366 &(stream->segment_seqnum[1]), NULL);
4367 gst_pad_link_full (pad, stream->channelpad[1],
4368 GST_PAD_LINK_CHECK_NOTHING);
4369 gst_object_unref (pad);
4370 } else {
4371 /* leave unlinked */
4372 }
4373 }
4374 return TRUE;
4375 }
4376
4377 /* configure the UDP sink back to the server for status reports */
4378 static gboolean
gst_rtspsrc_stream_configure_udp_sinks(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPTransport * transport)4379 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4380 GstRTSPStream * stream, GstRTSPTransport * transport)
4381 {
4382 GstPad *pad;
4383 gint rtp_port, rtcp_port;
4384 gboolean do_rtp, do_rtcp;
4385 const gchar *destination;
4386 gchar *uri, *name;
4387 guint ttl = 0;
4388 GSocket *socket;
4389
4390 /* get transport info */
4391 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4392 &rtp_port, &rtcp_port, &ttl);
4393
4394 /* see what we need to do */
4395 do_rtp = (rtp_port != -1);
4396 /* it's possible that the server does not want us to send RTCP in which case
4397 * the port is -1 */
4398 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4399
4400 /* we need a destination when we have RTP or RTCP ports */
4401 if (destination == NULL && (do_rtp || do_rtcp))
4402 goto no_destination;
4403
4404 /* try to construct the fakesrc to the RTP port of the server to open up any
4405 * NAT firewalls or, if backchannel, construct an appsrc */
4406 if (do_rtp) {
4407 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4408 rtp_port);
4409
4410 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4411 stream->udpsink[0] =
4412 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4413 g_free (uri);
4414 if (stream->udpsink[0] == NULL)
4415 goto no_sink_element;
4416
4417 /* don't join multicast group, we will have the source socket do that */
4418 /* no sync or async state changes needed */
4419 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4420 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4421 if (ttl > 0)
4422 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4423
4424 if (stream->udpsrc[0]) {
4425 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4426 * so that NAT firewalls will open a hole for us */
4427 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4428 if (!socket)
4429 goto no_socket;
4430
4431 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4432 /* configure socket and make sure udpsink does not close it when shutting
4433 * down, it belongs to udpsrc after all. */
4434 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4435 "close-socket", FALSE, NULL);
4436 g_object_unref (socket);
4437 }
4438
4439 if (stream->is_backchannel) {
4440 /* appsrc is for the app to shovel data using push-backchannel-buffer */
4441 stream->rtpsrc = gst_element_factory_make ("appsrc", NULL);
4442 if (stream->rtpsrc == NULL)
4443 goto no_appsrc_element;
4444
4445 /* interal use only, don't emit signals */
4446 g_object_set (G_OBJECT (stream->rtpsrc), "emit-signals", TRUE,
4447 "is-live", TRUE, NULL);
4448 } else {
4449 /* the source for the dummy packets to open up NAT */
4450 stream->rtpsrc = gst_element_factory_make ("fakesrc", NULL);
4451 if (stream->rtpsrc == NULL)
4452 goto no_fakesrc_element;
4453
4454 /* random data in 5 buffers, a size of 200 bytes should be fine */
4455 g_object_set (G_OBJECT (stream->rtpsrc), "filltype", 3, "num-buffers", 5,
4456 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4457 }
4458
4459 /* keep everything locked */
4460 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4461 gst_element_set_locked_state (stream->rtpsrc, TRUE);
4462
4463 gst_object_ref (stream->udpsink[0]);
4464 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4465 gst_object_ref (stream->rtpsrc);
4466 gst_bin_add (GST_BIN_CAST (src), stream->rtpsrc);
4467
4468 gst_element_link_pads_full (stream->rtpsrc, "src", stream->udpsink[0],
4469 "sink", GST_PAD_LINK_CHECK_NOTHING);
4470 }
4471 if (do_rtcp) {
4472 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4473 rtcp_port);
4474
4475 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4476 stream->udpsink[1] =
4477 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4478 g_free (uri);
4479 if (stream->udpsink[1] == NULL)
4480 goto no_sink_element;
4481
4482 /* don't join multicast group, we will have the source socket do that */
4483 /* no sync or async state changes needed */
4484 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4485 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4486 if (ttl > 0)
4487 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4488
4489 if (stream->udpsrc[1]) {
4490 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4491 * because some servers check the port number of where it sends RTCP to identify
4492 * the RTCP packets it receives */
4493 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4494 if (!socket)
4495 goto no_socket;
4496
4497 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4498 /* configure socket and make sure udpsink does not close it when shutting
4499 * down, it belongs to udpsrc after all. */
4500 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4501 "close-socket", FALSE, NULL);
4502 g_object_unref (socket);
4503 }
4504
4505 /* we keep this playing always */
4506 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4507 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4508
4509 gst_object_ref (stream->udpsink[1]);
4510 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4511
4512 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4513
4514 /* get session RTCP pad */
4515 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4516 pad = gst_element_get_request_pad (src->manager, name);
4517 g_free (name);
4518
4519 /* and link */
4520 if (pad) {
4521 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4522 gst_object_unref (pad);
4523 }
4524 }
4525
4526 return TRUE;
4527
4528 /* ERRORS */
4529 no_destination:
4530 {
4531 GST_ERROR_OBJECT (src, "no destination address specified");
4532 return FALSE;
4533 }
4534 no_sink_element:
4535 {
4536 GST_ERROR_OBJECT (src, "no UDP sink element found");
4537 return FALSE;
4538 }
4539 no_appsrc_element:
4540 {
4541 GST_ERROR_OBJECT (src, "no appsrc element found");
4542 return FALSE;
4543 }
4544 no_fakesrc_element:
4545 {
4546 GST_ERROR_OBJECT (src, "no fakesrc element found");
4547 return FALSE;
4548 }
4549 no_socket:
4550 {
4551 GST_ERROR_OBJECT (src, "failed to create socket");
4552 return FALSE;
4553 }
4554 }
4555
4556 /* sets up all elements needed for streaming over the specified transport.
4557 * Does not yet expose the element pads, this will be done when there is actuall
4558 * dataflow detected, which might never happen when UDP is blocked in a
4559 * firewall, for example.
4560 */
4561 static gboolean
gst_rtspsrc_stream_configure_transport(GstRTSPStream * stream,GstRTSPTransport * transport)4562 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4563 GstRTSPTransport * transport)
4564 {
4565 GstRTSPSrc *src;
4566 GstPad *outpad = NULL;
4567 GstPadTemplate *template;
4568 gchar *name;
4569 const gchar *media_type;
4570 guint i, len;
4571
4572 src = stream->parent;
4573
4574 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4575
4576 /* get the proper media type for this stream now */
4577 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4578 goto unknown_transport;
4579 if (!media_type)
4580 goto unknown_transport;
4581
4582 /* configure the final media type */
4583 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4584
4585 len = stream->ptmap->len;
4586 for (i = 0; i < len; i++) {
4587 GstStructure *s;
4588 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4589
4590 if (item->caps == NULL)
4591 continue;
4592
4593 s = gst_caps_get_structure (item->caps, 0);
4594 gst_structure_set_name (s, media_type);
4595 /* set ssrc if known */
4596 if (transport->ssrc)
4597 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4598 }
4599
4600 /* try to get and configure a manager, channelpad[0-1] will be configured with
4601 * the pads for the manager, or NULL when no manager is needed. */
4602 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4603 goto no_manager;
4604
4605 switch (transport->lower_transport) {
4606 case GST_RTSP_LOWER_TRANS_TCP:
4607 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4608 goto transport_failed;
4609 break;
4610 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4611 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4612 goto transport_failed;
4613 /* fallthrough, the rest is the same for UDP and MCAST */
4614 case GST_RTSP_LOWER_TRANS_UDP:
4615 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4616 goto transport_failed;
4617 /* configure udpsinks back to the server for RTCP messages, for the
4618 * dummy RTP messages to open NAT, and for the backchannel */
4619 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4620 goto transport_failed;
4621 break;
4622 default:
4623 goto unknown_transport;
4624 }
4625
4626 /* using backchannel and no manager, hence no srcpad for this stream */
4627 if (outpad && stream->is_backchannel) {
4628 add_backchannel_fakesink (src, stream, outpad);
4629 gst_object_unref (outpad);
4630 } else if (outpad) {
4631 GST_DEBUG_OBJECT (src, "creating ghostpad for stream %p", stream);
4632
4633 gst_pad_use_fixed_caps (outpad);
4634
4635 /* create ghostpad, don't add just yet, this will be done when we activate
4636 * the stream. */
4637 name = g_strdup_printf ("stream_%u", stream->id);
4638 template = gst_static_pad_template_get (&rtptemplate);
4639 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4640 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4641 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4642 gst_object_unref (template);
4643 g_free (name);
4644
4645 gst_object_unref (outpad);
4646 }
4647 /* mark pad as ok */
4648 stream->last_ret = GST_FLOW_OK;
4649
4650 return TRUE;
4651
4652 /* ERRORS */
4653 transport_failed:
4654 {
4655 GST_WARNING_OBJECT (src, "failed to configure transport");
4656 return FALSE;
4657 }
4658 unknown_transport:
4659 {
4660 GST_WARNING_OBJECT (src, "unknown transport");
4661 return FALSE;
4662 }
4663 no_manager:
4664 {
4665 GST_WARNING_OBJECT (src, "cannot get a session manager");
4666 return FALSE;
4667 }
4668 }
4669
4670 /* send a couple of dummy random packets on the receiver RTP port to the server,
4671 * this should make a firewall think we initiated the data transfer and
4672 * hopefully allow packets to go from the sender port to our RTP receiver port */
4673 static gboolean
gst_rtspsrc_send_dummy_packets(GstRTSPSrc * src)4674 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4675 {
4676 GList *walk;
4677
4678 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4679 return TRUE;
4680
4681 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4682 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4683
4684 if (!stream->rtpsrc || !stream->udpsink[0])
4685 continue;
4686
4687 if (stream->is_backchannel)
4688 GST_DEBUG_OBJECT (src, "starting backchannel stream %p", stream);
4689 else
4690 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4691
4692 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4693 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
4694 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4695 gst_element_set_state (stream->rtpsrc, GST_STATE_PLAYING);
4696 }
4697 return TRUE;
4698 }
4699
4700 /* Adds the source pads of all configured streams to the element.
4701 * This code is performed when we detected dataflow.
4702 *
4703 * We detect dataflow from either the _loop function or with pad probes on the
4704 * udp sources.
4705 */
4706 static gboolean
gst_rtspsrc_activate_streams(GstRTSPSrc * src)4707 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4708 {
4709 GList *walk;
4710
4711 GST_DEBUG_OBJECT (src, "activating streams");
4712
4713 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4714 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4715
4716 if (stream->udpsrc[0]) {
4717 /* remove timeout, we are streaming now and timeouts will be handled by
4718 * the session manager and jitter buffer */
4719 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4720 }
4721 if (stream->srcpad) {
4722 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4723 gst_pad_set_active (stream->srcpad, TRUE);
4724
4725 /* if we don't have a session manager, set the caps now. If we have a
4726 * session, we will get a notification of the pad and the caps. */
4727 if (!src->manager) {
4728 GstCaps *caps;
4729
4730 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4731 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4732 gst_pad_set_caps (stream->srcpad, caps);
4733 }
4734 /* add the pad */
4735 if (!stream->added) {
4736 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4737 if (stream->is_backchannel)
4738 add_backchannel_fakesink (src, stream, stream->srcpad);
4739 else
4740 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4741 stream->added = TRUE;
4742 }
4743 }
4744 }
4745
4746 /* unblock all pads */
4747 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4748 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4749
4750 if (stream->blockid) {
4751 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4752 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4753 stream->blockid = 0;
4754 }
4755 }
4756
4757 return TRUE;
4758 }
4759
4760 static void
gst_rtspsrc_configure_caps(GstRTSPSrc * src,GstSegment * segment,gboolean reset_manager)4761 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4762 gboolean reset_manager)
4763 {
4764 GList *walk;
4765 guint64 start, stop;
4766 gdouble play_speed, play_scale;
4767
4768 GST_DEBUG_OBJECT (src, "configuring stream caps");
4769
4770 start = segment->position;
4771 stop = segment->duration;
4772 play_speed = segment->rate;
4773 play_scale = segment->applied_rate;
4774
4775 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4776 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4777 guint j, len;
4778
4779 if (!stream->setup)
4780 continue;
4781
4782 len = stream->ptmap->len;
4783 for (j = 0; j < len; j++) {
4784 GstCaps *caps;
4785 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4786
4787 if (item->caps == NULL)
4788 continue;
4789
4790 caps = gst_caps_make_writable (item->caps);
4791 /* update caps */
4792 if (stream->timebase != -1)
4793 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4794 (guint) stream->timebase, NULL);
4795 if (stream->seqbase != -1)
4796 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4797 (guint) stream->seqbase, NULL);
4798 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4799 if (stop != -1)
4800 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4801 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4802 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4803
4804 item->caps = caps;
4805 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4806 item->pt, caps);
4807
4808 if (item->pt == stream->default_pt) {
4809 if (stream->udpsrc[0])
4810 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4811 stream->need_caps = TRUE;
4812 }
4813 }
4814 }
4815 if (reset_manager && src->manager) {
4816 GST_DEBUG_OBJECT (src, "clear session");
4817 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4818 }
4819 }
4820
4821 static GstFlowReturn
gst_rtspsrc_combine_flows(GstRTSPSrc * src,GstRTSPStream * stream,GstFlowReturn ret)4822 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4823 GstFlowReturn ret)
4824 {
4825 GList *streams;
4826
4827 /* store the value */
4828 stream->last_ret = ret;
4829
4830 /* if it's success we can return the value right away */
4831 if (ret == GST_FLOW_OK)
4832 goto done;
4833
4834 /* any other error that is not-linked can be returned right
4835 * away */
4836 if (ret != GST_FLOW_NOT_LINKED)
4837 goto done;
4838
4839 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4840 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4841 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4842
4843 ret = ostream->last_ret;
4844 /* some other return value (must be SUCCESS but we can return
4845 * other values as well) */
4846 if (ret != GST_FLOW_NOT_LINKED)
4847 goto done;
4848 }
4849 /* if we get here, all other pads were unlinked and we return
4850 * NOT_LINKED then */
4851 done:
4852 return ret;
4853 }
4854
4855 static gboolean
gst_rtspsrc_stream_push_event(GstRTSPSrc * src,GstRTSPStream * stream,GstEvent * event)4856 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4857 GstEvent * event)
4858 {
4859 gboolean res = TRUE;
4860
4861 /* only streams that have a connection to the outside world */
4862 if (!stream->setup)
4863 goto done;
4864
4865 if (stream->udpsrc[0]) {
4866 GstEvent *sent_event;
4867
4868 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4869 sent_event = gst_event_new_eos ();
4870 gst_event_set_seqnum (sent_event, stream->segment_seqnum[0]);
4871 } else {
4872 sent_event = gst_event_ref (event);
4873 }
4874
4875 res = gst_element_send_event (stream->udpsrc[0], sent_event);
4876 } else if (stream->channelpad[0]) {
4877 gst_event_ref (event);
4878 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4879 res = gst_pad_push_event (stream->channelpad[0], event);
4880 else
4881 res = gst_pad_send_event (stream->channelpad[0], event);
4882 }
4883
4884 if (stream->udpsrc[1]) {
4885 GstEvent *sent_event;
4886
4887 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
4888 sent_event = gst_event_new_eos ();
4889 if (stream->segment_seqnum[1] != GST_SEQNUM_INVALID) {
4890 gst_event_set_seqnum (sent_event, stream->segment_seqnum[1]);
4891 }
4892 } else {
4893 sent_event = gst_event_ref (event);
4894 }
4895
4896 res &= gst_element_send_event (stream->udpsrc[1], sent_event);
4897 } else if (stream->channelpad[1]) {
4898 gst_event_ref (event);
4899 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4900 res &= gst_pad_push_event (stream->channelpad[1], event);
4901 else
4902 res &= gst_pad_send_event (stream->channelpad[1], event);
4903 }
4904
4905 done:
4906 gst_event_unref (event);
4907
4908 return res;
4909 }
4910
4911 static gboolean
gst_rtspsrc_push_event(GstRTSPSrc * src,GstEvent * event)4912 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4913 {
4914 GList *streams;
4915 gboolean res = TRUE;
4916
4917 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4918 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4919
4920 gst_event_ref (event);
4921 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4922 }
4923 gst_event_unref (event);
4924
4925 return res;
4926 }
4927
4928 static gboolean
accept_certificate_cb(GTlsConnection * conn,GTlsCertificate * peer_cert,GTlsCertificateFlags errors,gpointer user_data)4929 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
4930 GTlsCertificateFlags errors, gpointer user_data)
4931 {
4932 GstRTSPSrc *src = user_data;
4933 gboolean accept = FALSE;
4934
4935 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
4936 peer_cert, errors, &accept);
4937
4938 return accept;
4939 }
4940
4941 static GstRTSPResult
gst_rtsp_conninfo_connect(GstRTSPSrc * src,GstRTSPConnInfo * info,gboolean async)4942 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4943 gboolean async)
4944 {
4945 GstRTSPResult res;
4946 GstRTSPMessage response;
4947 gboolean retry = FALSE;
4948 memset (&response, 0, sizeof (response));
4949 gst_rtsp_message_init (&response);
4950 do {
4951 if (info->connection == NULL) {
4952 if (info->url == NULL) {
4953 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4954 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4955 goto parse_error;
4956 }
4957 /* create connection */
4958 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4959 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4960 goto could_not_create;
4961
4962 if (retry) {
4963 gst_rtspsrc_setup_auth (src, &response);
4964 }
4965
4966 g_free (info->url_str);
4967 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4968
4969 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4970
4971 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4972 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4973 src->tls_validation_flags))
4974 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4975
4976 if (src->tls_database)
4977 gst_rtsp_connection_set_tls_database (info->connection,
4978 src->tls_database);
4979
4980 if (src->tls_interaction)
4981 gst_rtsp_connection_set_tls_interaction (info->connection,
4982 src->tls_interaction);
4983 gst_rtsp_connection_set_accept_certificate_func (info->connection,
4984 accept_certificate_cb, src, NULL);
4985 }
4986
4987 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4988 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4989
4990 if (src->proxy_host) {
4991 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4992 src->proxy_port);
4993 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4994 src->proxy_port);
4995 }
4996 }
4997
4998 if (!info->connected) {
4999 /* connect */
5000 if (async)
5001 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
5002 ("Connecting to %s", info->location));
5003 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
5004 res = gst_rtsp_connection_connect_with_response (info->connection,
5005 src->ptcp_timeout, &response);
5006
5007 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
5008 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5009 gst_rtsp_conninfo_close (src, info, TRUE);
5010 if (!retry)
5011 retry = TRUE;
5012 else
5013 retry = FALSE; // we should not retry more than once
5014 } else {
5015 retry = FALSE;
5016 }
5017
5018 if (res == GST_RTSP_OK)
5019 info->connected = TRUE;
5020 else if (!retry)
5021 goto could_not_connect;
5022 }
5023 } while (!info->connected && retry);
5024
5025 gst_rtsp_message_unset (&response);
5026 return GST_RTSP_OK;
5027
5028 /* ERRORS */
5029 parse_error:
5030 {
5031 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
5032 gst_rtsp_message_unset (&response);
5033 return res;
5034 }
5035 could_not_create:
5036 {
5037 gchar *str = gst_rtsp_strresult (res);
5038 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
5039 g_free (str);
5040 gst_rtsp_message_unset (&response);
5041 return res;
5042 }
5043 could_not_connect:
5044 {
5045 gchar *str = gst_rtsp_strresult (res);
5046 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
5047 g_free (str);
5048 gst_rtsp_message_unset (&response);
5049 return res;
5050 }
5051 }
5052
5053 static GstRTSPResult
gst_rtsp_conninfo_close(GstRTSPSrc * src,GstRTSPConnInfo * info,gboolean free)5054 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
5055 gboolean free)
5056 {
5057 GST_RTSP_STATE_LOCK (src);
5058 if (info->connected) {
5059 GST_DEBUG_OBJECT (src, "closing connection...");
5060 gst_rtsp_connection_close (info->connection);
5061 info->connected = FALSE;
5062 }
5063 if (free && info->connection) {
5064 /* free connection */
5065 GST_DEBUG_OBJECT (src, "freeing connection...");
5066 gst_rtsp_connection_free (info->connection);
5067 info->connection = NULL;
5068 info->flushing = FALSE;
5069 }
5070 GST_RTSP_STATE_UNLOCK (src);
5071 return GST_RTSP_OK;
5072 }
5073
5074 static GstRTSPResult
gst_rtsp_conninfo_reconnect(GstRTSPSrc * src,GstRTSPConnInfo * info,gboolean async)5075 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
5076 gboolean async)
5077 {
5078 GstRTSPResult res;
5079
5080 GST_DEBUG_OBJECT (src, "reconnecting connection...");
5081 gst_rtsp_conninfo_close (src, info, FALSE);
5082 res = gst_rtsp_conninfo_connect (src, info, async);
5083
5084 return res;
5085 }
5086
5087 static void
gst_rtspsrc_connection_flush(GstRTSPSrc * src,gboolean flush)5088 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
5089 {
5090 GList *walk;
5091
5092 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
5093 GST_RTSP_STATE_LOCK (src);
5094 if (src->conninfo.connection && src->conninfo.flushing != flush) {
5095 GST_DEBUG_OBJECT (src, "connection flush");
5096 gst_rtsp_connection_flush (src->conninfo.connection, flush);
5097 src->conninfo.flushing = flush;
5098 }
5099 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5100 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5101 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
5102 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
5103 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
5104 stream->conninfo.flushing = flush;
5105 }
5106 }
5107 GST_RTSP_STATE_UNLOCK (src);
5108 }
5109
5110 static GstRTSPResult
gst_rtspsrc_init_request(GstRTSPSrc * src,GstRTSPMessage * msg,GstRTSPMethod method,const gchar * uri)5111 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
5112 GstRTSPMethod method, const gchar * uri)
5113 {
5114 GstRTSPResult res;
5115
5116 res = gst_rtsp_message_init_request (msg, method, uri);
5117 if (res < 0)
5118 return res;
5119
5120 /* set user-agent */
5121 if (src->user_agent)
5122 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
5123
5124 return res;
5125 }
5126
5127 /* FIXME, handle server request, reply with OK, for now */
5128 static GstRTSPResult
gst_rtspsrc_handle_request(GstRTSPSrc * src,GstRTSPConnInfo * conninfo,GstRTSPMessage * request)5129 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5130 GstRTSPMessage * request)
5131 {
5132 GstRTSPMessage response = { 0 };
5133 GstRTSPResult res;
5134
5135 GST_DEBUG_OBJECT (src, "got server request message");
5136
5137 DEBUG_RTSP (src, request);
5138
5139 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
5140
5141 if (res == GST_RTSP_ENOTIMPL) {
5142 /* default implementation, send OK */
5143 GST_DEBUG_OBJECT (src, "prepare OK reply");
5144 res =
5145 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
5146 request);
5147 if (res < 0)
5148 goto send_error;
5149
5150 /* let app parse and reply */
5151 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
5152 0, request, &response);
5153
5154 DEBUG_RTSP (src, &response);
5155
5156 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
5157 if (res < 0)
5158 goto send_error;
5159
5160 gst_rtsp_message_unset (&response);
5161 } else if (res == GST_RTSP_EEOF)
5162 return res;
5163
5164 return GST_RTSP_OK;
5165
5166 /* ERRORS */
5167 send_error:
5168 {
5169 gst_rtsp_message_unset (&response);
5170 return res;
5171 }
5172 }
5173
5174 /* send server keep-alive */
5175 static GstRTSPResult
gst_rtspsrc_send_keep_alive(GstRTSPSrc * src)5176 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
5177 {
5178 GstRTSPMessage request = { 0 };
5179 GstRTSPResult res;
5180 GstRTSPMethod method;
5181 const gchar *control;
5182
5183 if (src->do_rtsp_keep_alive == FALSE) {
5184 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
5185 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5186 return GST_RTSP_OK;
5187 }
5188
5189 GST_DEBUG_OBJECT (src, "creating server keep-alive");
5190
5191 /* find a method to use for keep-alive */
5192 if (src->methods & GST_RTSP_GET_PARAMETER)
5193 method = GST_RTSP_GET_PARAMETER;
5194 else
5195 method = GST_RTSP_OPTIONS;
5196
5197 control = get_aggregate_control (src);
5198 if (control == NULL)
5199 goto no_control;
5200
5201 res = gst_rtspsrc_init_request (src, &request, method, control);
5202 if (res < 0)
5203 goto send_error;
5204
5205 request.type_data.request.version = src->version;
5206
5207 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
5208 if (res < 0)
5209 goto send_error;
5210
5211 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
5212 gst_rtsp_message_unset (&request);
5213
5214 return GST_RTSP_OK;
5215
5216 /* ERRORS */
5217 no_control:
5218 {
5219 GST_WARNING_OBJECT (src, "no control url to send keepalive");
5220 return GST_RTSP_OK;
5221 }
5222 send_error:
5223 {
5224 gchar *str = gst_rtsp_strresult (res);
5225
5226 gst_rtsp_message_unset (&request);
5227 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
5228 ("Could not send keep-alive. (%s)", str));
5229 g_free (str);
5230 return res;
5231 }
5232 }
5233
5234 static GstFlowReturn
gst_rtspsrc_handle_data(GstRTSPSrc * src,GstRTSPMessage * message)5235 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
5236 {
5237 GstFlowReturn ret = GST_FLOW_OK;
5238 gint channel;
5239 GstRTSPStream *stream;
5240 GstPad *outpad = NULL;
5241 guint8 *data;
5242 guint size;
5243 GstBuffer *buf;
5244 gboolean is_rtcp;
5245
5246 channel = message->type_data.data.channel;
5247
5248 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
5249 if (!stream)
5250 goto unknown_stream;
5251
5252 if (channel == stream->channel[0]) {
5253 outpad = stream->channelpad[0];
5254 is_rtcp = FALSE;
5255 } else if (channel == stream->channel[1]) {
5256 outpad = stream->channelpad[1];
5257 is_rtcp = TRUE;
5258 } else {
5259 is_rtcp = FALSE;
5260 }
5261
5262 /* take a look at the body to figure out what we have */
5263 gst_rtsp_message_get_body (message, &data, &size);
5264 if (size < 2)
5265 goto invalid_length;
5266
5267 /* channels are not correct on some servers, do extra check */
5268 if (data[1] >= 200 && data[1] <= 204) {
5269 /* hmm RTCP message switch to the RTCP pad of the same stream. */
5270 outpad = stream->channelpad[1];
5271 is_rtcp = TRUE;
5272 }
5273
5274 /* we have no clue what this is, just ignore then. */
5275 if (outpad == NULL)
5276 goto unknown_stream;
5277
5278 /* take the message body for further processing */
5279 gst_rtsp_message_steal_body (message, &data, &size);
5280
5281 /* strip the trailing \0 */
5282 size -= 1;
5283
5284 buf = gst_buffer_new ();
5285 gst_buffer_append_memory (buf,
5286 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
5287
5288 /* don't need message anymore */
5289 gst_rtsp_message_unset (message);
5290
5291 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
5292 channel);
5293
5294 if (src->need_activate) {
5295 gchar *stream_id;
5296 GstEvent *event;
5297 GChecksum *cs;
5298 gchar *uri;
5299 GList *streams;
5300 guint group_id = gst_util_group_id_next ();
5301
5302 /* generate an SHA256 sum of the URI */
5303 cs = g_checksum_new (G_CHECKSUM_SHA256);
5304 uri = src->conninfo.location;
5305 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
5306
5307 for (streams = src->streams; streams; streams = g_list_next (streams)) {
5308 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
5309 GstCaps *caps;
5310
5311 stream_id =
5312 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
5313 event = gst_event_new_stream_start (stream_id);
5314 gst_event_set_group_id (event, group_id);
5315
5316 g_free (stream_id);
5317 gst_rtspsrc_stream_push_event (src, ostream, event);
5318
5319 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
5320 /* only streams that have a connection to the outside world */
5321 if (ostream->setup) {
5322 if (ostream->udpsrc[0]) {
5323 gst_element_send_event (ostream->udpsrc[0],
5324 gst_event_new_caps (caps));
5325 } else if (ostream->channelpad[0]) {
5326 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
5327 gst_pad_push_event (ostream->channelpad[0],
5328 gst_event_new_caps (caps));
5329 else
5330 gst_pad_send_event (ostream->channelpad[0],
5331 gst_event_new_caps (caps));
5332 }
5333 ostream->need_caps = FALSE;
5334
5335 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
5336 ostream->profile == GST_RTSP_PROFILE_SAVPF)
5337 caps = gst_caps_new_empty_simple ("application/x-srtcp");
5338 else
5339 caps = gst_caps_new_empty_simple ("application/x-rtcp");
5340
5341 if (ostream->udpsrc[1]) {
5342 gst_element_send_event (ostream->udpsrc[1],
5343 gst_event_new_caps (caps));
5344 } else if (ostream->channelpad[1]) {
5345 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
5346 gst_pad_push_event (ostream->channelpad[1],
5347 gst_event_new_caps (caps));
5348 else
5349 gst_pad_send_event (ostream->channelpad[1],
5350 gst_event_new_caps (caps));
5351 }
5352
5353 gst_caps_unref (caps);
5354 }
5355 }
5356 }
5357 g_checksum_free (cs);
5358
5359 gst_rtspsrc_activate_streams (src);
5360 src->need_activate = FALSE;
5361 src->need_segment = TRUE;
5362 }
5363
5364 if (src->base_time == -1) {
5365 /* Take current running_time. This timestamp will be put on
5366 * the first buffer of each stream because we are a live source and so we
5367 * timestamp with the running_time. When we are dealing with TCP, we also
5368 * only timestamp the first buffer (using the DISCONT flag) because a server
5369 * typically bursts data, for which we don't want to compensate by speeding
5370 * up the media. The other timestamps will be interpollated from this one
5371 * using the RTP timestamps. */
5372 GST_OBJECT_LOCK (src);
5373 if (GST_ELEMENT_CLOCK (src)) {
5374 GstClockTime now;
5375 GstClockTime base_time;
5376
5377 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5378 base_time = GST_ELEMENT_CAST (src)->base_time;
5379
5380 src->base_time = now - base_time;
5381
5382 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5383 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5384 }
5385 GST_OBJECT_UNLOCK (src);
5386 }
5387
5388 /* If needed send a new segment, don't forget we are live and buffer are
5389 * timestamped with running time */
5390 if (src->need_segment) {
5391 GstSegment segment;
5392 src->need_segment = FALSE;
5393 gst_segment_init (&segment, GST_FORMAT_TIME);
5394 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5395 }
5396
5397 if (stream->need_caps) {
5398 GstCaps *caps;
5399
5400 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
5401 /* only streams that have a connection to the outside world */
5402 if (stream->setup) {
5403 /* Only need to update the TCP caps here, UDP is already handled */
5404 if (stream->channelpad[0]) {
5405 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5406 gst_pad_push_event (stream->channelpad[0],
5407 gst_event_new_caps (caps));
5408 else
5409 gst_pad_send_event (stream->channelpad[0],
5410 gst_event_new_caps (caps));
5411 }
5412 stream->need_caps = FALSE;
5413 }
5414 }
5415
5416 stream->need_caps = FALSE;
5417 }
5418
5419 if (stream->discont && !is_rtcp) {
5420 /* mark first RTP buffer as discont */
5421 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5422 stream->discont = FALSE;
5423 /* first buffer gets the timestamp, other buffers are not timestamped and
5424 * their presentation time will be interpollated from the rtp timestamps. */
5425 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5426 GST_TIME_ARGS (src->base_time));
5427
5428 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5429 }
5430
5431 /* chain to the peer pad */
5432 if (GST_PAD_IS_SINK (outpad))
5433 ret = gst_pad_chain (outpad, buf);
5434 else
5435 ret = gst_pad_push (outpad, buf);
5436
5437 if (!is_rtcp) {
5438 /* combine all stream flows for the data transport */
5439 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5440 }
5441 return ret;
5442
5443 /* ERRORS */
5444 unknown_stream:
5445 {
5446 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5447 gst_rtsp_message_unset (message);
5448 return GST_FLOW_OK;
5449 }
5450 invalid_length:
5451 {
5452 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5453 ("Short message received, ignoring."));
5454 gst_rtsp_message_unset (message);
5455 return GST_FLOW_OK;
5456 }
5457 }
5458
5459 static GstFlowReturn
gst_rtspsrc_loop_interleaved(GstRTSPSrc * src)5460 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5461 {
5462 GstRTSPMessage message = { 0 };
5463 GstRTSPResult res;
5464 GstFlowReturn ret = GST_FLOW_OK;
5465 GTimeVal tv_timeout;
5466
5467 while (TRUE) {
5468 /* get the next timeout interval */
5469 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5470
5471 /* see if the timeout period expired */
5472 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
5473 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
5474 /* send keep-alive, only act on interrupt, a warning will be posted for
5475 * other errors. */
5476 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5477 goto interrupt;
5478 /* get new timeout */
5479 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5480 }
5481
5482 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
5483 tv_timeout.tv_sec, tv_timeout.tv_usec);
5484
5485 /* protect the connection with the connection lock so that we can see when
5486 * we are finished doing server communication */
5487 res =
5488 gst_rtspsrc_connection_receive (src, &src->conninfo,
5489 &message, src->ptcp_timeout);
5490
5491 switch (res) {
5492 case GST_RTSP_OK:
5493 GST_DEBUG_OBJECT (src, "we received a server message");
5494 break;
5495 case GST_RTSP_EINTR:
5496 /* we got interrupted this means we need to stop */
5497 goto interrupt;
5498 case GST_RTSP_ETIMEOUT:
5499 /* no reply, send keep alive */
5500 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5501 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5502 goto interrupt;
5503 continue;
5504 case GST_RTSP_EEOF:
5505 /* go EOS when the server closed the connection */
5506 goto server_eof;
5507 default:
5508 goto receive_error;
5509 }
5510
5511 switch (message.type) {
5512 case GST_RTSP_MESSAGE_REQUEST:
5513 /* server sends us a request message, handle it */
5514 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5515 if (res == GST_RTSP_EEOF)
5516 goto server_eof;
5517 else if (res < 0)
5518 goto handle_request_failed;
5519 break;
5520 case GST_RTSP_MESSAGE_RESPONSE:
5521 /* we ignore response messages */
5522 GST_DEBUG_OBJECT (src, "ignoring response message");
5523 DEBUG_RTSP (src, &message);
5524 break;
5525 case GST_RTSP_MESSAGE_DATA:
5526 GST_DEBUG_OBJECT (src, "got data message");
5527 ret = gst_rtspsrc_handle_data (src, &message);
5528 if (ret != GST_FLOW_OK)
5529 goto handle_data_failed;
5530 break;
5531 default:
5532 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5533 message.type);
5534 break;
5535 }
5536 }
5537 g_assert_not_reached ();
5538
5539 /* ERRORS */
5540 server_eof:
5541 {
5542 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5543 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5544 ("The server closed the connection."));
5545 src->conninfo.connected = FALSE;
5546 gst_rtsp_message_unset (&message);
5547 return GST_FLOW_EOS;
5548 }
5549 interrupt:
5550 {
5551 gst_rtsp_message_unset (&message);
5552 GST_DEBUG_OBJECT (src, "got interrupted");
5553 return GST_FLOW_FLUSHING;
5554 }
5555 receive_error:
5556 {
5557 gchar *str = gst_rtsp_strresult (res);
5558
5559 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5560 ("Could not receive message. (%s)", str));
5561 g_free (str);
5562
5563 gst_rtsp_message_unset (&message);
5564 return GST_FLOW_ERROR;
5565 }
5566 handle_request_failed:
5567 {
5568 gchar *str = gst_rtsp_strresult (res);
5569
5570 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5571 ("Could not handle server message. (%s)", str));
5572 g_free (str);
5573 gst_rtsp_message_unset (&message);
5574 return GST_FLOW_ERROR;
5575 }
5576 handle_data_failed:
5577 {
5578 GST_DEBUG_OBJECT (src, "could no handle data message");
5579 return ret;
5580 }
5581 }
5582
5583 static GstFlowReturn
gst_rtspsrc_loop_udp(GstRTSPSrc * src)5584 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5585 {
5586 GstRTSPResult res;
5587 GstRTSPMessage message = { 0 };
5588 gint retry = 0;
5589
5590 while (TRUE) {
5591 GTimeVal tv_timeout;
5592
5593 /* get the next timeout interval */
5594 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5595
5596 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5597 (gint) tv_timeout.tv_sec);
5598
5599 gst_rtsp_message_unset (&message);
5600
5601 /* we should continue reading the TCP socket because the server might
5602 * send us requests. When the session timeout expires, we need to send a
5603 * keep-alive request to keep the session open. */
5604 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
5605 &message, &tv_timeout);
5606
5607 switch (res) {
5608 case GST_RTSP_OK:
5609 GST_DEBUG_OBJECT (src, "we received a server message");
5610 break;
5611 case GST_RTSP_EINTR:
5612 /* we got interrupted, see what we have to do */
5613 goto interrupt;
5614 case GST_RTSP_ETIMEOUT:
5615 /* send keep-alive, ignore the result, a warning will be posted. */
5616 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5617 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5618 goto interrupt;
5619 continue;
5620 case GST_RTSP_EEOF:
5621 /* server closed the connection. not very fatal for UDP, reconnect and
5622 * see what happens. */
5623 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5624 ("The server closed the connection."));
5625 if (src->udp_reconnect) {
5626 if ((res =
5627 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5628 goto connect_error;
5629 } else {
5630 goto server_eof;
5631 }
5632 continue;
5633 case GST_RTSP_ENET:
5634 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5635 default:
5636 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5637 ("Unhandled return value %d.", res));
5638 goto receive_error;
5639 }
5640
5641 switch (message.type) {
5642 case GST_RTSP_MESSAGE_REQUEST:
5643 /* server sends us a request message, handle it */
5644 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5645 if (res == GST_RTSP_EEOF)
5646 goto server_eof;
5647 else if (res < 0)
5648 goto handle_request_failed;
5649 break;
5650 case GST_RTSP_MESSAGE_RESPONSE:
5651 /* we ignore response and data messages */
5652 GST_DEBUG_OBJECT (src, "ignoring response message");
5653 DEBUG_RTSP (src, &message);
5654 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5655 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5656 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5657 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5658 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5659 goto interrupt;
5660 }
5661 } else {
5662 retry = 0;
5663 }
5664 break;
5665 case GST_RTSP_MESSAGE_DATA:
5666 /* we ignore response and data messages */
5667 GST_DEBUG_OBJECT (src, "ignoring data message");
5668 break;
5669 default:
5670 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5671 message.type);
5672 break;
5673 }
5674 }
5675 g_assert_not_reached ();
5676
5677 /* we get here when the connection got interrupted */
5678 interrupt:
5679 {
5680 gst_rtsp_message_unset (&message);
5681 GST_DEBUG_OBJECT (src, "got interrupted");
5682 return GST_FLOW_FLUSHING;
5683 }
5684 connect_error:
5685 {
5686 gchar *str = gst_rtsp_strresult (res);
5687 GstFlowReturn ret;
5688
5689 src->conninfo.connected = FALSE;
5690 if (res != GST_RTSP_EINTR) {
5691 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5692 ("Could not connect to server. (%s)", str));
5693 g_free (str);
5694 ret = GST_FLOW_ERROR;
5695 } else {
5696 ret = GST_FLOW_FLUSHING;
5697 }
5698 return ret;
5699 }
5700 receive_error:
5701 {
5702 gchar *str = gst_rtsp_strresult (res);
5703
5704 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5705 ("Could not receive message. (%s)", str));
5706 g_free (str);
5707 return GST_FLOW_ERROR;
5708 }
5709 handle_request_failed:
5710 {
5711 gchar *str = gst_rtsp_strresult (res);
5712 GstFlowReturn ret;
5713
5714 gst_rtsp_message_unset (&message);
5715 if (res != GST_RTSP_EINTR) {
5716 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5717 ("Could not handle server message. (%s)", str));
5718 g_free (str);
5719 ret = GST_FLOW_ERROR;
5720 } else {
5721 ret = GST_FLOW_FLUSHING;
5722 }
5723 return ret;
5724 }
5725 server_eof:
5726 {
5727 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5728 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5729 ("The server closed the connection."));
5730 src->conninfo.connected = FALSE;
5731 gst_rtsp_message_unset (&message);
5732 return GST_FLOW_EOS;
5733 }
5734 }
5735
5736 static GstRTSPResult
gst_rtspsrc_reconnect(GstRTSPSrc * src,gboolean async)5737 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5738 {
5739 GstRTSPResult res = GST_RTSP_OK;
5740 gboolean restart;
5741
5742 GST_DEBUG_OBJECT (src, "doing reconnect");
5743
5744 GST_OBJECT_LOCK (src);
5745 /* only restart when the pads were not yet activated, else we were
5746 * streaming over UDP */
5747 restart = src->need_activate;
5748 GST_OBJECT_UNLOCK (src);
5749
5750 /* no need to restart, we're done */
5751 if (!restart)
5752 goto done;
5753
5754 /* we can try only TCP now */
5755 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5756
5757 /* close and cleanup our state */
5758 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5759 goto done;
5760
5761 /* see if we have TCP left to try. Also don't try TCP when we were configured
5762 * with an SDP. */
5763 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5764 goto no_protocols;
5765
5766 /* We post a warning message now to inform the user
5767 * that nothing happened. It's most likely a firewall thing. */
5768 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5769 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5770 "firewall is blocking it. Retrying using a tcp connection.",
5771 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5772
5773 /* open new connection using tcp */
5774 if (gst_rtspsrc_open (src, async) < 0)
5775 goto open_failed;
5776
5777 /* start playback */
5778 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
5779 goto play_failed;
5780
5781 done:
5782 return res;
5783
5784 /* ERRORS */
5785 no_protocols:
5786 {
5787 src->cur_protocols = 0;
5788 /* no transport possible, post an error and stop */
5789 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5790 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5791 "firewall is blocking it. No other protocols to try.",
5792 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5793 return GST_RTSP_ERROR;
5794 }
5795 open_failed:
5796 {
5797 GST_DEBUG_OBJECT (src, "open failed");
5798 return GST_RTSP_OK;
5799 }
5800 play_failed:
5801 {
5802 GST_DEBUG_OBJECT (src, "play failed");
5803 return GST_RTSP_OK;
5804 }
5805 }
5806
5807 static void
gst_rtspsrc_loop_start_cmd(GstRTSPSrc * src,gint cmd)5808 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5809 {
5810 switch (cmd) {
5811 case CMD_OPEN:
5812 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5813 break;
5814 case CMD_PLAY:
5815 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5816 break;
5817 case CMD_PAUSE:
5818 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5819 break;
5820 case CMD_GET_PARAMETER:
5821 GST_ELEMENT_PROGRESS (src, START, "request",
5822 ("Sending GET_PARAMETER request"));
5823 break;
5824 case CMD_SET_PARAMETER:
5825 GST_ELEMENT_PROGRESS (src, START, "request",
5826 ("Sending SET_PARAMETER request"));
5827 break;
5828 case CMD_CLOSE:
5829 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5830 break;
5831 default:
5832 break;
5833 }
5834 }
5835
5836 static void
gst_rtspsrc_loop_complete_cmd(GstRTSPSrc * src,gint cmd)5837 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5838 {
5839 switch (cmd) {
5840 case CMD_OPEN:
5841 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5842 break;
5843 case CMD_PLAY:
5844 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5845 break;
5846 case CMD_PAUSE:
5847 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5848 break;
5849 case CMD_GET_PARAMETER:
5850 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
5851 ("Sent GET_PARAMETER request"));
5852 break;
5853 case CMD_SET_PARAMETER:
5854 GST_ELEMENT_PROGRESS (src, COMPLETE, "request",
5855 ("Sent SET_PARAMETER request"));
5856 break;
5857 case CMD_CLOSE:
5858 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5859 break;
5860 default:
5861 break;
5862 }
5863 }
5864
5865 static void
gst_rtspsrc_loop_cancel_cmd(GstRTSPSrc * src,gint cmd)5866 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5867 {
5868 switch (cmd) {
5869 case CMD_OPEN:
5870 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5871 break;
5872 case CMD_PLAY:
5873 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5874 break;
5875 case CMD_PAUSE:
5876 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5877 break;
5878 case CMD_GET_PARAMETER:
5879 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
5880 ("GET_PARAMETER canceled"));
5881 break;
5882 case CMD_SET_PARAMETER:
5883 GST_ELEMENT_PROGRESS (src, CANCELED, "request",
5884 ("SET_PARAMETER canceled"));
5885 break;
5886 case CMD_CLOSE:
5887 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5888 break;
5889 default:
5890 break;
5891 }
5892 }
5893
5894 static void
gst_rtspsrc_loop_error_cmd(GstRTSPSrc * src,gint cmd)5895 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5896 {
5897 switch (cmd) {
5898 case CMD_OPEN:
5899 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5900 break;
5901 case CMD_PLAY:
5902 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5903 break;
5904 case CMD_PAUSE:
5905 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5906 break;
5907 case CMD_GET_PARAMETER:
5908 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("GET_PARAMETER failed"));
5909 break;
5910 case CMD_SET_PARAMETER:
5911 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("SET_PARAMETER failed"));
5912 break;
5913 case CMD_CLOSE:
5914 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5915 break;
5916 default:
5917 break;
5918 }
5919 }
5920
5921 static void
gst_rtspsrc_loop_end_cmd(GstRTSPSrc * src,gint cmd,GstRTSPResult ret)5922 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5923 {
5924 if (ret == GST_RTSP_OK)
5925 gst_rtspsrc_loop_complete_cmd (src, cmd);
5926 else if (ret == GST_RTSP_EINTR)
5927 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5928 else
5929 gst_rtspsrc_loop_error_cmd (src, cmd);
5930 }
5931
5932 static gboolean
gst_rtspsrc_loop_send_cmd(GstRTSPSrc * src,gint cmd,gint mask)5933 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5934 {
5935 gint old;
5936 gboolean flushed = FALSE;
5937
5938 /* start new request */
5939 gst_rtspsrc_loop_start_cmd (src, cmd);
5940
5941 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5942
5943 GST_OBJECT_LOCK (src);
5944 old = src->pending_cmd;
5945
5946 if (old == CMD_RECONNECT) {
5947 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5948 cmd = CMD_RECONNECT;
5949 } else if (old == CMD_CLOSE) {
5950 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5951 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5952 * still pending). We just avoid it here by making sure CMD_CLOSE is
5953 * still the pending command. */
5954 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5955 cmd = CMD_CLOSE;
5956 } else if (old == CMD_SET_PARAMETER) {
5957 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
5958 cmd = CMD_SET_PARAMETER;
5959 } else if (old == CMD_GET_PARAMETER) {
5960 GST_DEBUG_OBJECT (src, "ignore, we have a pending %s", cmd_to_string (old));
5961 cmd = CMD_GET_PARAMETER;
5962 } else if (old != CMD_WAIT) {
5963 src->pending_cmd = CMD_WAIT;
5964 GST_OBJECT_UNLOCK (src);
5965 /* cancel previous request */
5966 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5967 gst_rtspsrc_loop_cancel_cmd (src, old);
5968 GST_OBJECT_LOCK (src);
5969 }
5970 src->pending_cmd = cmd;
5971 /* interrupt if allowed */
5972 if (src->busy_cmd & mask) {
5973 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5974 cmd_to_string (src->busy_cmd));
5975 gst_rtspsrc_connection_flush (src, TRUE);
5976 flushed = TRUE;
5977 } else {
5978 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5979 cmd_to_string (src->busy_cmd));
5980 }
5981 if (src->task)
5982 gst_task_start (src->task);
5983 GST_OBJECT_UNLOCK (src);
5984
5985 return flushed;
5986 }
5987
5988 static gboolean
gst_rtspsrc_loop_send_cmd_and_wait(GstRTSPSrc * src,gint cmd,gint mask,GstClockTime timeout)5989 gst_rtspsrc_loop_send_cmd_and_wait (GstRTSPSrc * src, gint cmd, gint mask,
5990 GstClockTime timeout)
5991 {
5992 gboolean flushed = gst_rtspsrc_loop_send_cmd (src, cmd, mask);
5993
5994 if (timeout > 0) {
5995 gint64 end_time = g_get_monotonic_time () + (timeout / 1000);
5996 GST_OBJECT_LOCK (src);
5997 while (src->pending_cmd == cmd || src->busy_cmd == cmd) {
5998 if (!g_cond_wait_until (&src->cmd_cond, GST_OBJECT_GET_LOCK (src),
5999 end_time)) {
6000 GST_WARNING_OBJECT (src,
6001 "Timed out waiting for TEARDOWN to be processed.");
6002 break; /* timeout passed */
6003 }
6004 }
6005 GST_OBJECT_UNLOCK (src);
6006 }
6007 return flushed;
6008 }
6009
6010 static gboolean
gst_rtspsrc_loop(GstRTSPSrc * src)6011 gst_rtspsrc_loop (GstRTSPSrc * src)
6012 {
6013 GstFlowReturn ret;
6014
6015 if (!src->conninfo.connection || !src->conninfo.connected)
6016 goto no_connection;
6017
6018 if (src->interleaved)
6019 ret = gst_rtspsrc_loop_interleaved (src);
6020 else
6021 ret = gst_rtspsrc_loop_udp (src);
6022
6023 if (ret != GST_FLOW_OK)
6024 goto pause;
6025
6026 return TRUE;
6027
6028 /* ERRORS */
6029 no_connection:
6030 {
6031 GST_WARNING_OBJECT (src, "we are not connected");
6032 ret = GST_FLOW_FLUSHING;
6033 goto pause;
6034 }
6035 pause:
6036 {
6037 const gchar *reason = gst_flow_get_name (ret);
6038
6039 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
6040 src->running = FALSE;
6041 if (ret == GST_FLOW_EOS) {
6042 /* perform EOS logic */
6043 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
6044 gst_element_post_message (GST_ELEMENT_CAST (src),
6045 gst_message_new_segment_done (GST_OBJECT_CAST (src),
6046 src->segment.format, src->segment.position));
6047 gst_rtspsrc_push_event (src,
6048 gst_event_new_segment_done (src->segment.format,
6049 src->segment.position));
6050 } else {
6051 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6052 }
6053 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
6054 /* for fatal errors we post an error message, post the error before the
6055 * EOS so the app knows about the error first. */
6056 GST_ELEMENT_FLOW_ERROR (src, ret);
6057 gst_rtspsrc_push_event (src, gst_event_new_eos ());
6058 }
6059 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
6060 return FALSE;
6061 }
6062 }
6063
6064 #ifndef GST_DISABLE_GST_DEBUG
6065 static const gchar *
gst_rtsp_auth_method_to_string(GstRTSPAuthMethod method)6066 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
6067 {
6068 gint index = 0;
6069
6070 while (method != 0) {
6071 index++;
6072 method >>= 1;
6073 }
6074 switch (index) {
6075 case 0:
6076 return "None";
6077 case 1:
6078 return "Basic";
6079 case 2:
6080 return "Digest";
6081 }
6082
6083 return "Unknown";
6084 }
6085 #endif
6086
6087 /* Parse a WWW-Authenticate Response header and determine the
6088 * available authentication methods
6089 *
6090 * This code should also cope with the fact that each WWW-Authenticate
6091 * header can contain multiple challenge methods + tokens
6092 *
6093 * At the moment, for Basic auth, we just do a minimal check and don't
6094 * even parse out the realm */
6095 static void
gst_rtspsrc_parse_auth_hdr(GstRTSPMessage * response,GstRTSPAuthMethod * methods,GstRTSPConnection * conn,gboolean * stale)6096 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
6097 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
6098 {
6099 GstRTSPAuthCredential **credentials, **credential;
6100
6101 g_return_if_fail (response != NULL);
6102 g_return_if_fail (methods != NULL);
6103 g_return_if_fail (stale != NULL);
6104
6105 credentials =
6106 gst_rtsp_message_parse_auth_credentials (response,
6107 GST_RTSP_HDR_WWW_AUTHENTICATE);
6108 if (!credentials)
6109 return;
6110
6111 credential = credentials;
6112 while (*credential) {
6113 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
6114 *methods |= GST_RTSP_AUTH_BASIC;
6115 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
6116 GstRTSPAuthParam **param = (*credential)->params;
6117
6118 *methods |= GST_RTSP_AUTH_DIGEST;
6119
6120 gst_rtsp_connection_clear_auth_params (conn);
6121 *stale = FALSE;
6122
6123 while (*param) {
6124 if (strcmp ((*param)->name, "stale") == 0
6125 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
6126 *stale = TRUE;
6127 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
6128 (*param)->value);
6129 param++;
6130 }
6131 }
6132
6133 credential++;
6134 }
6135
6136 gst_rtsp_auth_credentials_free (credentials);
6137 }
6138
6139 /**
6140 * gst_rtspsrc_setup_auth:
6141 * @src: the rtsp source
6142 *
6143 * Configure a username and password and auth method on the
6144 * connection object based on a response we received from the
6145 * peer.
6146 *
6147 * Currently, this requires that a username and password were supplied
6148 * in the uri. In the future, they may be requested on demand by sending
6149 * a message up the bus.
6150 *
6151 * Returns: TRUE if authentication information could be set up correctly.
6152 */
6153 static gboolean
gst_rtspsrc_setup_auth(GstRTSPSrc * src,GstRTSPMessage * response)6154 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
6155 {
6156 gchar *user = NULL;
6157 gchar *pass = NULL;
6158 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
6159 GstRTSPAuthMethod method;
6160 GstRTSPResult auth_result;
6161 GstRTSPUrl *url;
6162 GstRTSPConnection *conn;
6163 gboolean stale = FALSE;
6164
6165 conn = src->conninfo.connection;
6166
6167 /* Identify the available auth methods and see if any are supported */
6168 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
6169
6170 if (avail_methods == GST_RTSP_AUTH_NONE)
6171 goto no_auth_available;
6172
6173 /* For digest auth, if the response indicates that the session
6174 * data are stale, we just update them in the connection object and
6175 * return TRUE to retry the request */
6176 if (stale)
6177 src->tried_url_auth = FALSE;
6178
6179 url = gst_rtsp_connection_get_url (conn);
6180
6181 /* Do we have username and password available? */
6182 if (url != NULL && !src->tried_url_auth && url->user != NULL
6183 && url->passwd != NULL) {
6184 user = url->user;
6185 pass = url->passwd;
6186 src->tried_url_auth = TRUE;
6187 GST_DEBUG_OBJECT (src,
6188 "Attempting authentication using credentials from the URL");
6189 } else {
6190 user = src->user_id;
6191 pass = src->user_pw;
6192 GST_DEBUG_OBJECT (src,
6193 "Attempting authentication using credentials from the properties");
6194 }
6195
6196 /* FIXME: If the url didn't contain username and password or we tried them
6197 * already, request a username and passwd from the application via some kind
6198 * of credentials request message */
6199
6200 /* If we don't have a username and passwd at this point, bail out. */
6201 if (user == NULL || pass == NULL)
6202 goto no_user_pass;
6203
6204 /* Try to configure for each available authentication method, strongest to
6205 * weakest */
6206 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
6207 /* Check if this method is available on the server */
6208 if ((method & avail_methods) == 0)
6209 continue;
6210
6211 /* Pass the credentials to the connection to try on the next request */
6212 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
6213 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
6214 * ignore it and end up retrying later */
6215 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
6216 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
6217 gst_rtsp_auth_method_to_string (method));
6218 break;
6219 }
6220 }
6221
6222 if (method == GST_RTSP_AUTH_NONE)
6223 goto no_auth_available;
6224
6225 return TRUE;
6226
6227 no_auth_available:
6228 {
6229 /* Output an error indicating that we couldn't connect because there were
6230 * no supported authentication protocols */
6231 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6232 ("No supported authentication protocol was found"));
6233 return FALSE;
6234 }
6235 no_user_pass:
6236 {
6237 /* We don't fire an error message, we just return FALSE and let the
6238 * normal NOT_AUTHORIZED error be propagated */
6239 return FALSE;
6240 }
6241 }
6242
6243 static GstRTSPResult
gst_rtsp_src_receive_response(GstRTSPSrc * src,GstRTSPConnInfo * conninfo,GstRTSPMessage * response,GstRTSPStatusCode * code)6244 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6245 GstRTSPMessage * response, GstRTSPStatusCode * code)
6246 {
6247 GstRTSPStatusCode thecode;
6248 gchar *content_base = NULL;
6249 GstRTSPResult res = gst_rtspsrc_connection_receive (src, conninfo,
6250 response, src->ptcp_timeout);
6251
6252 if (res < 0)
6253 goto receive_error;
6254
6255 DEBUG_RTSP (src, response);
6256
6257 switch (response->type) {
6258 case GST_RTSP_MESSAGE_REQUEST:
6259 res = gst_rtspsrc_handle_request (src, conninfo, response);
6260 if (res == GST_RTSP_EEOF)
6261 goto server_eof;
6262 else if (res < 0)
6263 goto handle_request_failed;
6264
6265 /* Not a response, receive next message */
6266 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6267 case GST_RTSP_MESSAGE_RESPONSE:
6268 /* ok, a response is good */
6269 GST_DEBUG_OBJECT (src, "received response message");
6270 break;
6271 case GST_RTSP_MESSAGE_DATA:
6272 /* get next response */
6273 GST_DEBUG_OBJECT (src, "handle data response message");
6274 gst_rtspsrc_handle_data (src, response);
6275
6276 /* Not a response, receive next message */
6277 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6278 default:
6279 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
6280 response->type);
6281
6282 /* Not a response, receive next message */
6283 return gst_rtsp_src_receive_response (src, conninfo, response, code);
6284 }
6285
6286 thecode = response->type_data.response.code;
6287
6288 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
6289
6290 /* if the caller wanted the result code, we store it. */
6291 if (code)
6292 *code = thecode;
6293
6294 /* If the request didn't succeed, bail out before doing any more */
6295 if (thecode != GST_RTSP_STS_OK)
6296 return GST_RTSP_OK;
6297
6298 /* store new content base if any */
6299 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
6300 &content_base, 0);
6301 if (content_base) {
6302 g_free (src->content_base);
6303 src->content_base = g_strdup (content_base);
6304 }
6305
6306 return GST_RTSP_OK;
6307
6308 /* ERRORS */
6309 receive_error:
6310 {
6311 switch (res) {
6312 case GST_RTSP_EEOF:
6313 return GST_RTSP_EEOF;
6314 default:
6315 {
6316 gchar *str = gst_rtsp_strresult (res);
6317
6318 if (res != GST_RTSP_EINTR) {
6319 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6320 ("Could not receive message. (%s)", str));
6321 } else {
6322 GST_WARNING_OBJECT (src, "receive interrupted");
6323 }
6324 g_free (str);
6325 break;
6326 }
6327 }
6328 return res;
6329 }
6330 handle_request_failed:
6331 {
6332 /* ERROR was posted */
6333 gst_rtsp_message_unset (response);
6334 return res;
6335 }
6336 server_eof:
6337 {
6338 GST_DEBUG_OBJECT (src, "we got an eof from the server");
6339 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
6340 ("The server closed the connection."));
6341 gst_rtsp_message_unset (response);
6342 return res;
6343 }
6344 }
6345
6346
6347 static GstRTSPResult
gst_rtspsrc_try_send(GstRTSPSrc * src,GstRTSPConnInfo * conninfo,GstRTSPMessage * request,GstRTSPMessage * response,GstRTSPStatusCode * code)6348 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6349 GstRTSPMessage * request, GstRTSPMessage * response,
6350 GstRTSPStatusCode * code)
6351 {
6352 GstRTSPResult res;
6353 gint try = 0;
6354 gboolean allow_send = TRUE;
6355
6356 again:
6357 if (!src->short_header)
6358 gst_rtsp_ext_list_before_send (src->extensions, request);
6359
6360 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
6361 request, &allow_send);
6362 if (!allow_send) {
6363 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
6364 return GST_RTSP_OK;
6365 }
6366
6367 GST_DEBUG_OBJECT (src, "sending message");
6368
6369 DEBUG_RTSP (src, request);
6370
6371 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
6372 if (res < 0)
6373 goto send_error;
6374
6375 gst_rtsp_connection_reset_timeout (conninfo->connection);
6376 if (!response)
6377 return res;
6378
6379 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
6380 if (res == GST_RTSP_EEOF) {
6381 GST_WARNING_OBJECT (src, "server closed connection");
6382 /* only try once after reconnect, then fallthrough and error out */
6383 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
6384 try++;
6385 /* if reconnect succeeds, try again */
6386 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
6387 goto again;
6388 }
6389 }
6390 gst_rtsp_ext_list_after_send (src->extensions, request, response);
6391
6392 return res;
6393
6394 send_error:
6395 {
6396 gchar *str = gst_rtsp_strresult (res);
6397
6398 if (res != GST_RTSP_EINTR) {
6399 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6400 ("Could not send message. (%s)", str));
6401 } else {
6402 GST_WARNING_OBJECT (src, "send interrupted");
6403 }
6404 g_free (str);
6405 return res;
6406 }
6407 }
6408
6409 /**
6410 * gst_rtspsrc_send:
6411 * @src: the rtsp source
6412 * @conninfo: the connection information to send on
6413 * @request: must point to a valid request
6414 * @response: must point to an empty #GstRTSPMessage
6415 * @code: an optional code result
6416 * @versions: List of versions to try, setting it back onto the @request message
6417 * if not set, `src->version` will be used as RTSP version.
6418 *
6419 * send @request and retrieve the response in @response. optionally @code can be
6420 * non-NULL in which case it will contain the status code of the response.
6421 *
6422 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6423 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6424 *
6425 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6426 * @response message) if the response code was not 200 (OK).
6427 *
6428 * If the attempt results in an authentication failure, then this will attempt
6429 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6430 * the request.
6431 *
6432 * Returns: #GST_RTSP_OK if the processing was successful.
6433 */
6434 static GstRTSPResult
gst_rtspsrc_send(GstRTSPSrc * src,GstRTSPConnInfo * conninfo,GstRTSPMessage * request,GstRTSPMessage * response,GstRTSPStatusCode * code,GstRTSPVersion * versions)6435 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6436 GstRTSPMessage * request, GstRTSPMessage * response,
6437 GstRTSPStatusCode * code, GstRTSPVersion * versions)
6438 {
6439 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6440 GstRTSPResult res = GST_RTSP_ERROR;
6441 gint count;
6442 gboolean retry;
6443 GstRTSPMethod method = GST_RTSP_INVALID;
6444 gint version_retry = 0;
6445
6446 count = 0;
6447 do {
6448 retry = FALSE;
6449
6450 /* make sure we don't loop forever */
6451 if (count++ > 8)
6452 break;
6453
6454 /* save method so we can disable it when the server complains */
6455 method = request->type_data.request.method;
6456
6457 if (!versions)
6458 request->type_data.request.version = src->version;
6459
6460 if ((res =
6461 gst_rtspsrc_try_send (src, conninfo, request, response,
6462 &int_code)) < 0)
6463 goto error;
6464
6465 switch (int_code) {
6466 case GST_RTSP_STS_UNAUTHORIZED:
6467 case GST_RTSP_STS_NOT_FOUND:
6468 if (gst_rtspsrc_setup_auth (src, response)) {
6469 /* Try the request/response again after configuring the auth info
6470 * and loop again */
6471 retry = TRUE;
6472 }
6473 break;
6474 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
6475 GST_INFO_OBJECT (src, "Version %s not supported by the server",
6476 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
6477 "unknown");
6478 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
6479 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
6480 gst_rtsp_version_as_text (request->type_data.request.version),
6481 gst_rtsp_version_as_text (versions[version_retry]));
6482 request->type_data.request.version = versions[version_retry];
6483 retry = TRUE;
6484 version_retry++;
6485 break;
6486 }
6487 /* falltrough */
6488 default:
6489 break;
6490 }
6491 } while (retry == TRUE);
6492
6493 /* If the user requested the code, let them handle errors, otherwise
6494 * post an error below */
6495 if (code != NULL)
6496 *code = int_code;
6497 else if (int_code != GST_RTSP_STS_OK)
6498 goto error_response;
6499
6500 return res;
6501
6502 /* ERRORS */
6503 error:
6504 {
6505 GST_DEBUG_OBJECT (src, "got error %d", res);
6506 return res;
6507 }
6508 error_response:
6509 {
6510 res = GST_RTSP_ERROR;
6511
6512 switch (response->type_data.response.code) {
6513 case GST_RTSP_STS_NOT_FOUND:
6514 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
6515 "Not found");
6516 break;
6517 case GST_RTSP_STS_UNAUTHORIZED:
6518 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
6519 "Unauthorized");
6520 break;
6521 case GST_RTSP_STS_MOVED_PERMANENTLY:
6522 case GST_RTSP_STS_MOVE_TEMPORARILY:
6523 {
6524 gchar *new_location;
6525 GstRTSPLowerTrans transports;
6526
6527 GST_DEBUG_OBJECT (src, "got redirection");
6528 /* if we don't have a Location Header, we must error */
6529 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6530 &new_location, 0) < 0)
6531 break;
6532
6533 /* When we receive a redirect result, we go back to the INIT state after
6534 * parsing the new URI. The caller should do the needed steps to issue
6535 * a new setup when it detects this state change. */
6536 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6537
6538 /* save current transports */
6539 if (src->conninfo.url)
6540 transports = src->conninfo.url->transports;
6541 else
6542 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6543
6544 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6545
6546 /* set old transports */
6547 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6548 src->conninfo.url->transports = transports;
6549
6550 src->need_redirect = TRUE;
6551 res = GST_RTSP_OK;
6552 break;
6553 }
6554 case GST_RTSP_STS_NOT_ACCEPTABLE:
6555 case GST_RTSP_STS_NOT_IMPLEMENTED:
6556 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6557 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6558 gst_rtsp_method_as_text (method));
6559 src->methods &= ~method;
6560 res = GST_RTSP_OK;
6561 break;
6562 default:
6563 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
6564 "Unhandled error");
6565 break;
6566 }
6567 /* if we return ERROR we should unset the response ourselves */
6568 if (res == GST_RTSP_ERROR)
6569 gst_rtsp_message_unset (response);
6570
6571 return res;
6572 }
6573 }
6574
6575 static GstRTSPResult
gst_rtspsrc_send_cb(GstRTSPExtension * ext,GstRTSPMessage * request,GstRTSPMessage * response,GstRTSPSrc * src)6576 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6577 GstRTSPMessage * response, GstRTSPSrc * src)
6578 {
6579 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
6580 }
6581
6582
6583 /* parse the response and collect all the supported methods. We need this
6584 * information so that we don't try to send an unsupported request to the
6585 * server.
6586 */
6587 static gboolean
gst_rtspsrc_parse_methods(GstRTSPSrc * src,GstRTSPMessage * response)6588 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6589 {
6590 GstRTSPHeaderField field;
6591 gchar *respoptions;
6592 gint indx = 0;
6593
6594 /* reset supported methods */
6595 src->methods = 0;
6596
6597 /* Try Allow Header first */
6598 field = GST_RTSP_HDR_ALLOW;
6599 while (TRUE) {
6600 respoptions = NULL;
6601 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6602 if (!respoptions)
6603 break;
6604
6605 src->methods |= gst_rtsp_options_from_text (respoptions);
6606
6607 indx++;
6608 }
6609
6610 indx = 0;
6611 field = GST_RTSP_HDR_PUBLIC;
6612 while (TRUE) {
6613 respoptions = NULL;
6614 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6615 if (!respoptions)
6616 break;
6617
6618 src->methods |= gst_rtsp_options_from_text (respoptions);
6619
6620 indx++;
6621 }
6622
6623 if (src->methods == 0) {
6624 /* neither Allow nor Public are required, assume the server supports
6625 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6626 * well. */
6627 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6628 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6629 }
6630 /* always assume PLAY, FIXME, extensions should be able to override
6631 * this */
6632 src->methods |= GST_RTSP_PLAY;
6633 /* also assume it will support Range */
6634 src->seekable = G_MAXFLOAT;
6635
6636 /* we need describe and setup */
6637 if (!(src->methods & GST_RTSP_DESCRIBE))
6638 goto no_describe;
6639 if (!(src->methods & GST_RTSP_SETUP))
6640 goto no_setup;
6641
6642 return TRUE;
6643
6644 /* ERRORS */
6645 no_describe:
6646 {
6647 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6648 ("Server does not support DESCRIBE."));
6649 return FALSE;
6650 }
6651 no_setup:
6652 {
6653 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6654 ("Server does not support SETUP."));
6655 return FALSE;
6656 }
6657 }
6658
6659 /* masks to be kept in sync with the hardcoded protocol order of preference
6660 * in code below */
6661 static const guint protocol_masks[] = {
6662 GST_RTSP_LOWER_TRANS_UDP,
6663 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6664 GST_RTSP_LOWER_TRANS_TCP,
6665 0
6666 };
6667
6668 static GstRTSPResult
gst_rtspsrc_create_transports_string(GstRTSPSrc * src,GstRTSPLowerTrans protocols,GstRTSPProfile profile,gchar ** transports)6669 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6670 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6671 {
6672 GstRTSPResult res;
6673 GString *result;
6674 gboolean add_udp_str;
6675
6676 *transports = NULL;
6677
6678 res =
6679 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6680
6681 if (res < 0)
6682 goto failed;
6683
6684 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6685
6686 /* extension listed transports, use those */
6687 if (*transports != NULL)
6688 return GST_RTSP_OK;
6689
6690 /* it's the default */
6691 add_udp_str = FALSE;
6692
6693 /* the default RTSP transports */
6694 result = g_string_new ("RTP");
6695
6696 switch (profile) {
6697 case GST_RTSP_PROFILE_AVP:
6698 g_string_append (result, "/AVP");
6699 break;
6700 case GST_RTSP_PROFILE_SAVP:
6701 g_string_append (result, "/SAVP");
6702 break;
6703 case GST_RTSP_PROFILE_AVPF:
6704 g_string_append (result, "/AVPF");
6705 break;
6706 case GST_RTSP_PROFILE_SAVPF:
6707 g_string_append (result, "/SAVPF");
6708 break;
6709 default:
6710 break;
6711 }
6712
6713 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6714 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6715 if (add_udp_str)
6716 g_string_append (result, "/UDP");
6717 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6718 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6719 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6720 /* we don't have to allocate any UDP ports yet, if the selected transport
6721 * turns out to be multicast we can create them and join the multicast
6722 * group indicated in the transport reply */
6723 if (add_udp_str)
6724 g_string_append (result, "/UDP");
6725 g_string_append (result, ";multicast");
6726 if (src->next_port_num != 0) {
6727 if (src->client_port_range.max > 0 &&
6728 src->next_port_num >= src->client_port_range.max)
6729 goto no_ports;
6730
6731 g_string_append_printf (result, ";client_port=%d-%d",
6732 src->next_port_num, src->next_port_num + 1);
6733 }
6734 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6735 GST_DEBUG_OBJECT (src, "adding TCP");
6736
6737 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6738 }
6739 *transports = g_string_free (result, FALSE);
6740
6741 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6742
6743 return GST_RTSP_OK;
6744
6745 /* ERRORS */
6746 failed:
6747 {
6748 GST_ERROR ("extension gave error %d", res);
6749 return res;
6750 }
6751 no_ports:
6752 {
6753 GST_ERROR ("no more ports available");
6754 return GST_RTSP_ERROR;
6755 }
6756 }
6757
6758 static GstRTSPResult
gst_rtspsrc_prepare_transports(GstRTSPStream * stream,gchar ** transports,gint orig_rtpport,gint orig_rtcpport)6759 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6760 gint orig_rtpport, gint orig_rtcpport)
6761 {
6762 GstRTSPSrc *src;
6763 gint nr_udp, nr_int;
6764 gchar *next, *p;
6765 gint rtpport = 0, rtcpport = 0;
6766 GString *str;
6767
6768 src = stream->parent;
6769
6770 /* find number of placeholders first */
6771 if (strstr (*transports, "%%i2"))
6772 nr_int = 2;
6773 else if (strstr (*transports, "%%i1"))
6774 nr_int = 1;
6775 else
6776 nr_int = 0;
6777
6778 if (strstr (*transports, "%%u2"))
6779 nr_udp = 2;
6780 else if (strstr (*transports, "%%u1"))
6781 nr_udp = 1;
6782 else
6783 nr_udp = 0;
6784
6785 if (nr_udp == 0 && nr_int == 0)
6786 goto done;
6787
6788 if (nr_udp > 0) {
6789 if (!orig_rtpport || !orig_rtcpport) {
6790 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6791 goto failed;
6792 } else {
6793 rtpport = orig_rtpport;
6794 rtcpport = orig_rtcpport;
6795 }
6796 }
6797
6798 str = g_string_new ("");
6799 p = *transports;
6800 while ((next = strstr (p, "%%"))) {
6801 g_string_append_len (str, p, next - p);
6802 if (next[2] == 'u') {
6803 if (next[3] == '1')
6804 g_string_append_printf (str, "%d", rtpport);
6805 else if (next[3] == '2')
6806 g_string_append_printf (str, "%d", rtcpport);
6807 }
6808 if (next[2] == 'i') {
6809 if (next[3] == '1')
6810 g_string_append_printf (str, "%d", src->free_channel);
6811 else if (next[3] == '2')
6812 g_string_append_printf (str, "%d", src->free_channel + 1);
6813
6814 }
6815
6816 p = next + 4;
6817 }
6818 if (src->version >= GST_RTSP_VERSION_2_0)
6819 src->free_channel += 2;
6820
6821 /* append final part */
6822 g_string_append (str, p);
6823
6824 g_free (*transports);
6825 *transports = g_string_free (str, FALSE);
6826
6827 done:
6828 return GST_RTSP_OK;
6829
6830 /* ERRORS */
6831 failed:
6832 {
6833 GST_ERROR ("failed to allocate udp ports");
6834 return GST_RTSP_ERROR;
6835 }
6836 }
6837
6838 static GstCaps *
signal_get_srtcp_params(GstRTSPSrc * src,GstRTSPStream * stream)6839 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6840 {
6841 GstCaps *caps = NULL;
6842
6843 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6844 stream->id, &caps);
6845
6846 if (caps != NULL)
6847 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6848
6849 return caps;
6850 }
6851
6852 static GstCaps *
default_srtcp_params(void)6853 default_srtcp_params (void)
6854 {
6855 guint i;
6856 GstCaps *caps;
6857 GstBuffer *buf;
6858 guint8 *key_data;
6859 #define KEY_SIZE 30
6860 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6861
6862 /* create a random key */
6863 key_data = g_malloc (data_size);
6864 for (i = 0; i < data_size; i += 4)
6865 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6866
6867 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6868
6869 caps = gst_caps_new_simple ("application/x-srtcp",
6870 "srtp-key", GST_TYPE_BUFFER, buf,
6871 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6872 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6873 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6874 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6875
6876 gst_buffer_unref (buf);
6877
6878 return caps;
6879 }
6880
6881 static gchar *
gst_rtspsrc_stream_make_keymgmt(GstRTSPSrc * src,GstRTSPStream * stream)6882 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6883 {
6884 gchar *base64, *result = NULL;
6885 GstMIKEYMessage *mikey_msg;
6886
6887 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6888 if (stream->srtcpparams == NULL)
6889 stream->srtcpparams = default_srtcp_params ();
6890
6891 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6892 if (mikey_msg) {
6893 /* add policy '0' for our SSRC */
6894 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6895
6896 base64 = gst_mikey_message_base64_encode (mikey_msg);
6897 gst_mikey_message_unref (mikey_msg);
6898
6899 if (base64) {
6900 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6901 g_free (base64);
6902 }
6903 }
6904
6905 return result;
6906 }
6907
6908 static GstRTSPResult
gst_rtsp_src_setup_stream_from_response(GstRTSPSrc * src,GstRTSPStream * stream,GstRTSPMessage * response,GstRTSPLowerTrans * protocols,gint retry,gint * rtpport,gint * rtcpport)6909 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
6910 GstRTSPStream * stream, GstRTSPMessage * response,
6911 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
6912 {
6913 gchar *resptrans = NULL;
6914 GstRTSPTransport transport = { 0 };
6915
6916 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
6917 if (!resptrans) {
6918 gst_rtspsrc_stream_free_udp (stream);
6919 goto no_transport;
6920 }
6921
6922 /* parse transport, go to next stream on parse error */
6923 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6924 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6925 return GST_RTSP_ELAST;
6926 }
6927
6928 /* update allowed transports for other streams. once the transport of
6929 * one stream has been determined, we make sure that all other streams
6930 * are configured in the same way */
6931 switch (transport.lower_transport) {
6932 case GST_RTSP_LOWER_TRANS_TCP:
6933 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6934 if (protocols)
6935 *protocols = GST_RTSP_LOWER_TRANS_TCP;
6936 src->interleaved = TRUE;
6937 if (src->version < GST_RTSP_VERSION_2_0) {
6938 /* update free channels */
6939 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
6940 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
6941 src->free_channel++;
6942 }
6943 break;
6944 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6945 /* only allow multicast for other streams */
6946 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6947 if (protocols)
6948 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6949 /* if the server selected our ports, increment our counters so that
6950 * we select a new port later */
6951 if (src->next_port_num == transport.port.min &&
6952 src->next_port_num + 1 == transport.port.max) {
6953 src->next_port_num += 2;
6954 }
6955 break;
6956 case GST_RTSP_LOWER_TRANS_UDP:
6957 /* only allow unicast for other streams */
6958 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6959 if (protocols)
6960 *protocols = GST_RTSP_LOWER_TRANS_UDP;
6961 break;
6962 default:
6963 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6964 transport.lower_transport);
6965 break;
6966 }
6967
6968 if (!src->interleaved || !retry) {
6969 /* now configure the stream with the selected transport */
6970 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6971 GST_DEBUG_OBJECT (src,
6972 "could not configure stream %p transport, skipping stream", stream);
6973 goto done;
6974 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
6975 /* retain the first allocated UDP port pair */
6976 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
6977 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
6978 }
6979 }
6980 /* we need to activate at least one stream when we detect activity */
6981 src->need_activate = TRUE;
6982
6983 /* stream is setup now */
6984 stream->setup = TRUE;
6985 stream->waiting_setup_response = FALSE;
6986
6987 if (src->version >= GST_RTSP_VERSION_2_0) {
6988 gchar *prop, *media_properties;
6989 gchar **props;
6990 gint i;
6991
6992 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
6993 &media_properties, 0) != GST_RTSP_OK) {
6994 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6995 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
6996 " - this header is mandatory."));
6997
6998 gst_rtsp_message_unset (response);
6999 return GST_RTSP_ERROR;
7000 }
7001
7002 props = g_strsplit (media_properties, ",", -2);
7003 for (i = 0; props[i]; i++) {
7004 prop = props[i];
7005
7006 while (*prop == ' ')
7007 prop++;
7008
7009 if (strstr (prop, "Random-Access")) {
7010 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
7011
7012 if (!random_seekable_val[1])
7013 src->seekable = G_MAXFLOAT;
7014 else
7015 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
7016
7017 g_strfreev (random_seekable_val);
7018 } else if (!g_strcmp0 (prop, "No-Seeking")) {
7019 src->seekable = -1.0;
7020 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
7021 src->seekable = 0.0;
7022 }
7023 }
7024
7025 g_strfreev (props);
7026 }
7027
7028 done:
7029 /* clean up our transport struct */
7030 gst_rtsp_transport_init (&transport);
7031 /* clean up used RTSP messages */
7032 gst_rtsp_message_unset (response);
7033
7034 return GST_RTSP_OK;
7035
7036 no_transport:
7037 {
7038 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7039 ("Server did not select transport."));
7040
7041 gst_rtsp_message_unset (response);
7042 return GST_RTSP_ERROR;
7043 }
7044 }
7045
7046 static GstRTSPResult
gst_rtspsrc_setup_streams_end(GstRTSPSrc * src,gboolean async)7047 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
7048 {
7049 GList *tmp;
7050 GstRTSPConnInfo *conninfo;
7051
7052 g_assert (src->version >= GST_RTSP_VERSION_2_0);
7053
7054 conninfo = &src->conninfo;
7055 for (tmp = src->streams; tmp; tmp = tmp->next) {
7056 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
7057 GstRTSPMessage response = { 0, };
7058
7059 if (!stream->waiting_setup_response)
7060 continue;
7061
7062 if (!src->conninfo.connection)
7063 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
7064
7065 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
7066
7067 gst_rtsp_src_setup_stream_from_response (src, stream,
7068 &response, NULL, 0, NULL, NULL);
7069 }
7070
7071 return GST_RTSP_OK;
7072 }
7073
7074 /* Perform the SETUP request for all the streams.
7075 *
7076 * We ask the server for a specific transport, which initially includes all the
7077 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
7078 * two local UDP ports that we send to the server.
7079 *
7080 * Once the server replied with a transport, we configure the other streams
7081 * with the same transport.
7082 *
7083 * In case setup request are not pipelined, this function will also configure the
7084 * stream for the selected transport, * which basically means creating the pipeline.
7085 * Otherwise, the first stream is setup right away from the reply and a
7086 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
7087 * remaining streams from the RTSP thread.
7088 */
7089 static GstRTSPResult
gst_rtspsrc_setup_streams_start(GstRTSPSrc * src,gboolean async)7090 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
7091 {
7092 GList *walk;
7093 GstRTSPResult res = GST_RTSP_ERROR;
7094 GstRTSPMessage request = { 0 };
7095 GstRTSPMessage response = { 0 };
7096 GstRTSPStream *stream = NULL;
7097 GstRTSPLowerTrans protocols;
7098 GstRTSPStatusCode code;
7099 gboolean unsupported_real = FALSE;
7100 gint rtpport, rtcpport;
7101 GstRTSPUrl *url;
7102 gchar *hval;
7103 gchar *pipelined_request_id = NULL;
7104
7105 if (src->conninfo.connection) {
7106 url = gst_rtsp_connection_get_url (src->conninfo.connection);
7107 /* we initially allow all configured lower transports. based on the URL
7108 * transports and the replies from the server we narrow them down. */
7109 protocols = url->transports & src->cur_protocols;
7110 } else {
7111 url = NULL;
7112 protocols = src->cur_protocols;
7113 }
7114
7115 if (protocols == 0)
7116 goto no_protocols;
7117
7118 /* reset some state */
7119 src->free_channel = 0;
7120 src->interleaved = FALSE;
7121 src->need_activate = FALSE;
7122 /* keep track of next port number, 0 is random */
7123 src->next_port_num = src->client_port_range.min;
7124 rtpport = rtcpport = 0;
7125
7126 if (G_UNLIKELY (src->streams == NULL))
7127 goto no_streams;
7128
7129 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7130 GstRTSPConnInfo *conninfo;
7131 gchar *transports;
7132 gint retry = 0;
7133 guint mask = 0;
7134 gboolean selected;
7135 GstCaps *caps;
7136
7137 stream = (GstRTSPStream *) walk->data;
7138
7139 caps = stream_get_caps_for_pt (stream, stream->default_pt);
7140 if (caps == NULL) {
7141 GST_WARNING_OBJECT (src, "skipping stream %p, no caps", stream);
7142 continue;
7143 }
7144
7145 if (stream->skipped) {
7146 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
7147 continue;
7148 }
7149
7150 /* see if we need to configure this stream */
7151 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
7152 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
7153 stream);
7154 continue;
7155 }
7156
7157 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
7158 stream->id, caps, &selected);
7159 if (!selected) {
7160 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
7161 continue;
7162 }
7163
7164 /* merge/overwrite global caps */
7165 if (caps) {
7166 guint j, num;
7167 GstStructure *s;
7168
7169 s = gst_caps_get_structure (caps, 0);
7170
7171 num = gst_structure_n_fields (src->props);
7172 for (j = 0; j < num; j++) {
7173 const gchar *name;
7174 const GValue *val;
7175
7176 name = gst_structure_nth_field_name (src->props, j);
7177 val = gst_structure_get_value (src->props, name);
7178 gst_structure_set_value (s, name, val);
7179
7180 GST_DEBUG_OBJECT (src, "copied %s", name);
7181 }
7182 }
7183
7184 /* skip setup if we have no URL for it */
7185 if (stream->conninfo.location == NULL) {
7186 GST_WARNING_OBJECT (src, "skipping stream %p, no setup", stream);
7187 continue;
7188 }
7189
7190 if (src->conninfo.connection == NULL) {
7191 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
7192 GST_WARNING_OBJECT (src, "skipping stream %p, failed to connect",
7193 stream);
7194 continue;
7195 }
7196 conninfo = &stream->conninfo;
7197 } else {
7198 conninfo = &src->conninfo;
7199 }
7200 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
7201 stream->conninfo.location);
7202
7203 /* if we have a multicast connection, only suggest multicast from now on */
7204 if (stream->is_multicast)
7205 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
7206
7207 next_protocol:
7208 /* first selectable protocol */
7209 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7210 mask++;
7211 if (!protocol_masks[mask])
7212 goto no_protocols;
7213
7214 retry:
7215 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
7216 protocol_masks[mask]);
7217 /* create a string with first transport in line */
7218 transports = NULL;
7219 res = gst_rtspsrc_create_transports_string (src,
7220 protocols & protocol_masks[mask], stream->profile, &transports);
7221 if (res < 0 || transports == NULL)
7222 goto setup_transport_failed;
7223
7224 if (strlen (transports) == 0) {
7225 g_free (transports);
7226 GST_DEBUG_OBJECT (src, "no transports found");
7227 mask++;
7228 goto next_protocol;
7229 }
7230
7231 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
7232
7233 /* replace placeholders with real values, this function will optionally
7234 * allocate UDP ports and other info needed to execute the setup request */
7235 res = gst_rtspsrc_prepare_transports (stream, &transports,
7236 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
7237 if (res < 0) {
7238 g_free (transports);
7239 goto setup_transport_failed;
7240 }
7241
7242 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
7243 /* create SETUP request */
7244 res =
7245 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
7246 stream->conninfo.location);
7247 if (res < 0) {
7248 g_free (transports);
7249 goto create_request_failed;
7250 }
7251
7252 if (src->version >= GST_RTSP_VERSION_2_0) {
7253 if (!pipelined_request_id)
7254 pipelined_request_id = g_strdup_printf ("%d",
7255 g_random_int_range (0, G_MAXINT32));
7256
7257 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
7258 pipelined_request_id);
7259 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
7260 "npt, clock, smpte, clock");
7261 }
7262
7263 /* select transport */
7264 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
7265
7266 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
7267 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7268 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7269
7270 /* set up keys */
7271 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
7272 stream->profile == GST_RTSP_PROFILE_SAVPF) {
7273 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
7274 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
7275 }
7276
7277 /* if the user wants a non default RTP packet size we add the blocksize
7278 * parameter */
7279 if (src->rtp_blocksize > 0) {
7280 hval = g_strdup_printf ("%d", src->rtp_blocksize);
7281 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
7282 }
7283
7284 if (async)
7285 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
7286 stream->id));
7287
7288 /* handle the code ourselves */
7289 res =
7290 gst_rtspsrc_send (src, conninfo, &request,
7291 pipelined_request_id ? NULL : &response, &code, NULL);
7292 if (res < 0)
7293 goto send_error;
7294
7295 switch (code) {
7296 case GST_RTSP_STS_OK:
7297 break;
7298 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
7299 gst_rtsp_message_unset (&request);
7300 gst_rtsp_message_unset (&response);
7301 /* cleanup of leftover transport */
7302 gst_rtspsrc_stream_free_udp (stream);
7303 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
7304 * we might be in this case */
7305 if (stream->container && rtpport && rtcpport && !retry) {
7306 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
7307 rtpport, rtcpport);
7308 retry++;
7309 goto retry;
7310 }
7311 /* this transport did not go down well, but we may have others to try
7312 * that we did not send yet, try those and only give up then
7313 * but not without checking for lost cause/extension so we can
7314 * post a nicer/more useful error message later */
7315 if (!unsupported_real)
7316 unsupported_real = stream->is_real;
7317 /* select next available protocol, give up on this stream if none */
7318 mask++;
7319 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
7320 mask++;
7321 if (!protocol_masks[mask] || unsupported_real)
7322 continue;
7323 else
7324 goto retry;
7325 default:
7326 /* cleanup of leftover transport and move to the next stream */
7327 gst_rtspsrc_stream_free_udp (stream);
7328 goto response_error;
7329 }
7330
7331
7332 if (!pipelined_request_id) {
7333 /* parse response transport */
7334 res = gst_rtsp_src_setup_stream_from_response (src, stream,
7335 &response, &protocols, retry, &rtpport, &rtcpport);
7336 switch (res) {
7337 case GST_RTSP_ERROR:
7338 goto cleanup_error;
7339 case GST_RTSP_ELAST:
7340 goto retry;
7341 default:
7342 break;
7343 }
7344 } else {
7345 stream->waiting_setup_response = TRUE;
7346 /* we need to activate at least one stream when we detect activity */
7347 src->need_activate = TRUE;
7348 }
7349
7350 {
7351 GList *skip = walk;
7352
7353 while (TRUE) {
7354 GstRTSPStream *sskip;
7355
7356 skip = g_list_next (skip);
7357 if (skip == NULL)
7358 break;
7359
7360 sskip = (GstRTSPStream *) skip->data;
7361
7362 /* skip all streams with the same control url */
7363 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
7364 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
7365 sskip, sskip->conninfo.location);
7366 sskip->skipped = TRUE;
7367 }
7368 }
7369 }
7370 gst_rtsp_message_unset (&request);
7371 }
7372
7373 if (pipelined_request_id) {
7374 gst_rtspsrc_setup_streams_end (src, TRUE);
7375 }
7376
7377 /* store the transport protocol that was configured */
7378 src->cur_protocols = protocols;
7379
7380 gst_rtsp_ext_list_stream_select (src->extensions, url);
7381
7382 if (pipelined_request_id)
7383 g_free (pipelined_request_id);
7384
7385 /* if there is nothing to activate, error out */
7386 if (!src->need_activate)
7387 goto nothing_to_activate;
7388
7389 return res;
7390
7391 /* ERRORS */
7392 no_protocols:
7393 {
7394 /* no transport possible, post an error and stop */
7395 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
7396 ("Could not connect to server, no protocols left"));
7397 return GST_RTSP_ERROR;
7398 }
7399 no_streams:
7400 {
7401 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7402 ("SDP contains no streams"));
7403 return GST_RTSP_ERROR;
7404 }
7405 create_request_failed:
7406 {
7407 gchar *str = gst_rtsp_strresult (res);
7408
7409 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7410 ("Could not create request. (%s)", str));
7411 g_free (str);
7412 goto cleanup_error;
7413 }
7414 setup_transport_failed:
7415 {
7416 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7417 ("Could not setup transport."));
7418 res = GST_RTSP_ERROR;
7419 goto cleanup_error;
7420 }
7421 response_error:
7422 {
7423 const gchar *str = gst_rtsp_status_as_text (code);
7424
7425 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7426 ("Error (%d): %s", code, GST_STR_NULL (str)));
7427 res = GST_RTSP_ERROR;
7428 goto cleanup_error;
7429 }
7430 send_error:
7431 {
7432 gchar *str = gst_rtsp_strresult (res);
7433
7434 if (res != GST_RTSP_EINTR) {
7435 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7436 ("Could not send message. (%s)", str));
7437 } else {
7438 GST_WARNING_OBJECT (src, "send interrupted");
7439 }
7440 g_free (str);
7441 goto cleanup_error;
7442 }
7443 nothing_to_activate:
7444 {
7445 /* none of the available error codes is really right .. */
7446 if (unsupported_real) {
7447 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7448 (_("No supported stream was found. You might need to install a "
7449 "GStreamer RTSP extension plugin for Real media streams.")),
7450 (NULL));
7451 } else {
7452 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7453 (_("No supported stream was found. You might need to allow "
7454 "more transport protocols or may otherwise be missing "
7455 "the right GStreamer RTSP extension plugin.")), (NULL));
7456 }
7457 return GST_RTSP_ERROR;
7458 }
7459 cleanup_error:
7460 {
7461 if (pipelined_request_id)
7462 g_free (pipelined_request_id);
7463 gst_rtsp_message_unset (&request);
7464 gst_rtsp_message_unset (&response);
7465 return res;
7466 }
7467 }
7468
7469 static gboolean
gst_rtspsrc_parse_range(GstRTSPSrc * src,const gchar * range,GstSegment * segment)7470 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7471 GstSegment * segment)
7472 {
7473 gint64 seconds;
7474 GstRTSPTimeRange *therange;
7475
7476 if (src->range)
7477 gst_rtsp_range_free (src->range);
7478
7479 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7480 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7481 src->range = therange;
7482 } else {
7483 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7484 src->range = NULL;
7485 gst_segment_init (segment, GST_FORMAT_TIME);
7486 return FALSE;
7487 }
7488
7489 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7490 therange->min.type, therange->min.seconds, therange->max.type,
7491 therange->max.seconds);
7492
7493 if (therange->min.type == GST_RTSP_TIME_NOW)
7494 seconds = 0;
7495 else if (therange->min.type == GST_RTSP_TIME_END)
7496 seconds = 0;
7497 else
7498 seconds = therange->min.seconds * GST_SECOND;
7499
7500 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7501 GST_TIME_ARGS (seconds));
7502
7503 /* we need to start playback without clipping from the position reported by
7504 * the server */
7505 segment->start = seconds;
7506 segment->position = seconds;
7507
7508 if (therange->max.type == GST_RTSP_TIME_NOW)
7509 seconds = -1;
7510 else if (therange->max.type == GST_RTSP_TIME_END)
7511 seconds = -1;
7512 else
7513 seconds = therange->max.seconds * GST_SECOND;
7514
7515 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7516 GST_TIME_ARGS (seconds));
7517
7518 /* live (WMS) server might send overflowed large max as its idea of infinity,
7519 * compensate to prevent problems later on */
7520 if (seconds != -1 && seconds < 0) {
7521 seconds = -1;
7522 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7523 }
7524
7525 /* live (WMS) might send min == max, which is not worth recording */
7526 if (segment->duration == -1 && seconds == segment->start)
7527 seconds = -1;
7528
7529 /* don't change duration with unknown value, we might have a valid value
7530 * there that we want to keep. */
7531 if (seconds != -1)
7532 segment->duration = seconds;
7533
7534 return TRUE;
7535 }
7536
7537 /* Parse clock profived by the server with following syntax:
7538 *
7539 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7540 */
7541 static gboolean
gst_rtspsrc_parse_gst_clock(GstRTSPSrc * src,const gchar * gstclock)7542 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7543 {
7544 gboolean res = FALSE;
7545
7546 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7547 gchar **fields = NULL, **parts = NULL;
7548 gchar *remote_ip, *str;
7549 gint port;
7550 GstClockTime base_time;
7551 GstClock *netclock;
7552
7553 fields = g_strsplit (gstclock, " ", 0);
7554
7555 /* wrapped clock, not very interesting for now */
7556 if (fields[1] == NULL)
7557 goto cleanup;
7558
7559 /* remote IP address and port */
7560 if ((str = fields[2]) == NULL)
7561 goto cleanup;
7562
7563 parts = g_strsplit (str, ":", 0);
7564
7565 if ((remote_ip = parts[0]) == NULL)
7566 goto cleanup;
7567
7568 if ((str = parts[1]) == NULL)
7569 goto cleanup;
7570
7571 port = atoi (str);
7572 if (port == 0)
7573 goto cleanup;
7574
7575 /* base-time */
7576 if ((str = fields[3]) == NULL)
7577 goto cleanup;
7578
7579 base_time = g_ascii_strtoull (str, NULL, 10);
7580
7581 netclock =
7582 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7583 base_time);
7584
7585 if (src->provided_clock)
7586 gst_object_unref (src->provided_clock);
7587 src->provided_clock = netclock;
7588
7589 gst_element_post_message (GST_ELEMENT_CAST (src),
7590 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7591 src->provided_clock, TRUE));
7592
7593 res = TRUE;
7594 cleanup:
7595 g_strfreev (fields);
7596 g_strfreev (parts);
7597 }
7598 return res;
7599 }
7600
7601 /* must be called with the RTSP state lock */
7602 static GstRTSPResult
gst_rtspsrc_open_from_sdp(GstRTSPSrc * src,GstSDPMessage * sdp,gboolean async)7603 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7604 gboolean async)
7605 {
7606 GstRTSPResult res;
7607 gint i, n_streams;
7608
7609 /* prepare global stream caps properties */
7610 if (src->props)
7611 gst_structure_remove_all_fields (src->props);
7612 else
7613 src->props = gst_structure_new_empty ("RTSPProperties");
7614
7615 DEBUG_SDP (src, sdp);
7616
7617 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7618
7619 /* let the app inspect and change the SDP */
7620 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7621
7622 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7623
7624 /* parse range for duration reporting. */
7625 {
7626 const gchar *range;
7627
7628 for (i = 0;; i++) {
7629 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7630 if (range == NULL)
7631 break;
7632
7633 /* keep track of the range and configure it in the segment */
7634 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7635 break;
7636 }
7637 }
7638 /* parse clock information. This is GStreamer specific, a server can tell the
7639 * client what clock it is using and wrap that in a network clock. The
7640 * advantage of that is that we can slave to it. */
7641 {
7642 const gchar *gstclock;
7643
7644 for (i = 0;; i++) {
7645 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7646 if (gstclock == NULL)
7647 break;
7648
7649 /* parse the clock and expose it in the provide_clock method */
7650 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7651 break;
7652 }
7653 }
7654 /* try to find a global control attribute. Note that a '*' means that we should
7655 * do aggregate control with the current url (so we don't do anything and
7656 * leave the current connection as is) */
7657 {
7658 const gchar *control;
7659
7660 for (i = 0;; i++) {
7661 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7662 if (control == NULL)
7663 break;
7664
7665 /* only take fully qualified urls */
7666 if (g_str_has_prefix (control, "rtsp://"))
7667 break;
7668 }
7669 if (control) {
7670 g_free (src->conninfo.location);
7671 src->conninfo.location = g_strdup (control);
7672 /* make a connection for this, if there was a connection already, nothing
7673 * happens. */
7674 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7675 GST_ERROR_OBJECT (src, "could not connect");
7676 }
7677 }
7678 /* we need to keep the control url separate from the connection url because
7679 * the rules for constructing the media control url need it */
7680 g_free (src->control);
7681 src->control = g_strdup (control);
7682 }
7683
7684 /* create streams */
7685 n_streams = gst_sdp_message_medias_len (sdp);
7686 for (i = 0; i < n_streams; i++) {
7687 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
7688 }
7689
7690 src->state = GST_RTSP_STATE_INIT;
7691
7692 /* setup streams */
7693 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
7694 goto setup_failed;
7695
7696 /* reset our state */
7697 src->need_range = TRUE;
7698 src->skip = FALSE;
7699
7700 src->state = GST_RTSP_STATE_READY;
7701
7702 return res;
7703
7704 /* ERRORS */
7705 setup_failed:
7706 {
7707 GST_ERROR_OBJECT (src, "setup failed");
7708 gst_rtspsrc_cleanup (src);
7709 return res;
7710 }
7711 }
7712
7713 static GstRTSPResult
gst_rtspsrc_retrieve_sdp(GstRTSPSrc * src,GstSDPMessage ** sdp,gboolean async)7714 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7715 gboolean async)
7716 {
7717 GstRTSPResult res;
7718 GstRTSPMessage request = { 0 };
7719 GstRTSPMessage response = { 0 };
7720 guint8 *data;
7721 guint size;
7722 gchar *respcont = NULL;
7723 GstRTSPVersion versions[] =
7724 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
7725
7726 src->version = src->default_version;
7727 if (src->default_version == GST_RTSP_VERSION_2_0) {
7728 versions[0] = GST_RTSP_VERSION_1_0;
7729 }
7730
7731 restart:
7732 src->need_redirect = FALSE;
7733
7734 /* can't continue without a valid url */
7735 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7736 res = GST_RTSP_EINVAL;
7737 goto no_url;
7738 }
7739 src->tried_url_auth = FALSE;
7740
7741 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7742 goto connect_failed;
7743
7744 /* create OPTIONS */
7745 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
7746 res =
7747 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7748 src->conninfo.url_str);
7749 if (res < 0)
7750 goto create_request_failed;
7751
7752 /* send OPTIONS */
7753 request.type_data.request.version = src->version;
7754 GST_DEBUG_OBJECT (src, "send options...");
7755
7756 if (async)
7757 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7758
7759 if ((res =
7760 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7761 NULL, versions)) < 0) {
7762 goto send_error;
7763 }
7764
7765 src->version = request.type_data.request.version;
7766 GST_INFO_OBJECT (src, "Now using version: %s",
7767 gst_rtsp_version_as_text (src->version));
7768
7769 /* parse OPTIONS */
7770 if (!gst_rtspsrc_parse_methods (src, &response))
7771 goto methods_error;
7772
7773 /* create DESCRIBE */
7774 GST_DEBUG_OBJECT (src, "create describe...");
7775 res =
7776 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7777 src->conninfo.url_str);
7778 if (res < 0)
7779 goto create_request_failed;
7780
7781 /* we only accept SDP for now */
7782 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7783 "application/sdp");
7784
7785 if (src->backchannel == BACKCHANNEL_ONVIF)
7786 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7787 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7788 /* TODO: Handle the case when backchannel is unsupported and goto restart */
7789
7790 /* send DESCRIBE */
7791 GST_DEBUG_OBJECT (src, "send describe...");
7792
7793 if (async)
7794 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7795
7796 if ((res =
7797 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7798 NULL, NULL)) < 0)
7799 goto send_error;
7800
7801 /* we only perform redirect for describe and play, currently */
7802 if (src->need_redirect) {
7803 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7804 * result. */
7805 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7806
7807 gst_rtsp_message_unset (&request);
7808 gst_rtsp_message_unset (&response);
7809
7810 /* and now retry */
7811 goto restart;
7812 }
7813
7814 /* it could be that the DESCRIBE method was not implemented */
7815 if (!(src->methods & GST_RTSP_DESCRIBE))
7816 goto no_describe;
7817
7818 /* check if reply is SDP */
7819 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7820 0);
7821 /* could not be set but since the request returned OK, we assume it
7822 * was SDP, else check it. */
7823 if (respcont) {
7824 const gchar *props = strchr (respcont, ';');
7825
7826 if (props) {
7827 gchar *mimetype = g_strndup (respcont, props - respcont);
7828
7829 mimetype = g_strstrip (mimetype);
7830 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
7831 g_free (mimetype);
7832 goto wrong_content_type;
7833 }
7834
7835 /* TODO: Check for charset property and do conversions of all messages if
7836 * needed. Some servers actually send that property */
7837
7838 g_free (mimetype);
7839 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
7840 goto wrong_content_type;
7841 }
7842 }
7843
7844 /* get message body and parse as SDP */
7845 gst_rtsp_message_get_body (&response, &data, &size);
7846 if (data == NULL || size == 0)
7847 goto no_describe;
7848
7849 GST_DEBUG_OBJECT (src, "parse SDP...");
7850 gst_sdp_message_new (sdp);
7851 gst_sdp_message_parse_buffer (data, size, *sdp);
7852
7853 /* clean up any messages */
7854 gst_rtsp_message_unset (&request);
7855 gst_rtsp_message_unset (&response);
7856
7857 return res;
7858
7859 /* ERRORS */
7860 no_url:
7861 {
7862 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7863 ("No valid RTSP URL was provided"));
7864 goto cleanup_error;
7865 }
7866 connect_failed:
7867 {
7868 gchar *str = gst_rtsp_strresult (res);
7869
7870 if (res != GST_RTSP_EINTR) {
7871 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7872 ("Failed to connect. (%s)", str));
7873 } else {
7874 GST_WARNING_OBJECT (src, "connect interrupted");
7875 }
7876 g_free (str);
7877 goto cleanup_error;
7878 }
7879 create_request_failed:
7880 {
7881 gchar *str = gst_rtsp_strresult (res);
7882
7883 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7884 ("Could not create request. (%s)", str));
7885 g_free (str);
7886 goto cleanup_error;
7887 }
7888 send_error:
7889 {
7890 /* Don't post a message - the rtsp_send method will have
7891 * taken care of it because we passed NULL for the response code */
7892 goto cleanup_error;
7893 }
7894 methods_error:
7895 {
7896 /* error was posted */
7897 res = GST_RTSP_ERROR;
7898 goto cleanup_error;
7899 }
7900 wrong_content_type:
7901 {
7902 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7903 ("Server does not support SDP, got %s.", respcont));
7904 res = GST_RTSP_ERROR;
7905 goto cleanup_error;
7906 }
7907 no_describe:
7908 {
7909 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7910 ("Server can not provide an SDP."));
7911 res = GST_RTSP_ERROR;
7912 goto cleanup_error;
7913 }
7914 cleanup_error:
7915 {
7916 if (src->conninfo.connection) {
7917 GST_DEBUG_OBJECT (src, "free connection");
7918 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7919 }
7920 gst_rtsp_message_unset (&request);
7921 gst_rtsp_message_unset (&response);
7922 return res;
7923 }
7924 }
7925
7926 static GstRTSPResult
gst_rtspsrc_open(GstRTSPSrc * src,gboolean async)7927 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7928 {
7929 GstRTSPResult ret;
7930
7931 src->methods =
7932 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7933
7934 if (src->sdp == NULL) {
7935 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7936 goto no_sdp;
7937 }
7938
7939 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7940 goto open_failed;
7941
7942 done:
7943 if (async)
7944 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7945
7946 return ret;
7947
7948 /* ERRORS */
7949 no_sdp:
7950 {
7951 GST_WARNING_OBJECT (src, "can't get sdp");
7952 src->open_error = TRUE;
7953 goto done;
7954 }
7955 open_failed:
7956 {
7957 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7958 src->open_error = TRUE;
7959 goto done;
7960 }
7961 }
7962
7963 static GstRTSPResult
gst_rtspsrc_close(GstRTSPSrc * src,gboolean async,gboolean only_close)7964 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7965 {
7966 GstRTSPMessage request = { 0 };
7967 GstRTSPMessage response = { 0 };
7968 GstRTSPResult res = GST_RTSP_OK;
7969 GList *walk;
7970 const gchar *control;
7971
7972 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7973
7974 gst_rtspsrc_set_state (src, GST_STATE_READY);
7975
7976 if (src->state < GST_RTSP_STATE_READY) {
7977 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7978 goto close;
7979 }
7980
7981 if (only_close)
7982 goto close;
7983
7984 /* construct a control url */
7985 control = get_aggregate_control (src);
7986
7987 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7988 goto not_supported;
7989
7990 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7991 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7992 const gchar *setup_url;
7993 GstRTSPConnInfo *info;
7994
7995 /* try aggregate control first but do non-aggregate control otherwise */
7996 if (control)
7997 setup_url = control;
7998 else if ((setup_url = stream->conninfo.location) == NULL)
7999 continue;
8000
8001 if (src->conninfo.connection) {
8002 info = &src->conninfo;
8003 } else if (stream->conninfo.connection) {
8004 info = &stream->conninfo;
8005 } else {
8006 continue;
8007 }
8008 if (!info->connected)
8009 goto next;
8010
8011 /* do TEARDOWN */
8012 res =
8013 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
8014 GST_LOG_OBJECT (src, "Teardown on %s", setup_url);
8015 if (res < 0)
8016 goto create_request_failed;
8017
8018 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
8019 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8020 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8021
8022 if (async)
8023 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
8024
8025 if ((res =
8026 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
8027 goto send_error;
8028
8029 /* FIXME, parse result? */
8030 gst_rtsp_message_unset (&request);
8031 gst_rtsp_message_unset (&response);
8032
8033 next:
8034 /* early exit when we did aggregate control */
8035 if (control)
8036 break;
8037 }
8038
8039 close:
8040 /* close connections */
8041 GST_DEBUG_OBJECT (src, "closing connection...");
8042 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
8043 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8044 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8045 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
8046 }
8047
8048 /* cleanup */
8049 gst_rtspsrc_cleanup (src);
8050
8051 src->state = GST_RTSP_STATE_INVALID;
8052
8053 if (async)
8054 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
8055
8056 return res;
8057
8058 /* ERRORS */
8059 create_request_failed:
8060 {
8061 gchar *str = gst_rtsp_strresult (res);
8062
8063 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8064 ("Could not create request. (%s)", str));
8065 g_free (str);
8066 goto close;
8067 }
8068 send_error:
8069 {
8070 gchar *str = gst_rtsp_strresult (res);
8071
8072 gst_rtsp_message_unset (&request);
8073 if (res != GST_RTSP_EINTR) {
8074 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8075 ("Could not send message. (%s)", str));
8076 } else {
8077 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
8078 }
8079 g_free (str);
8080 goto close;
8081 }
8082 not_supported:
8083 {
8084 GST_DEBUG_OBJECT (src,
8085 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
8086 goto close;
8087 }
8088 }
8089
8090 /* RTP-Info is of the format:
8091 *
8092 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
8093 *
8094 * rtptime corresponds to the timestamp for the NPT time given in the header
8095 * seqbase corresponds to the next sequence number we received. This number
8096 * indicates the first seqnum after the seek and should be used to discard
8097 * packets that are from before the seek.
8098 */
8099 static gboolean
gst_rtspsrc_parse_rtpinfo(GstRTSPSrc * src,gchar * rtpinfo)8100 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
8101 {
8102 gchar **infos;
8103 gint i, j;
8104
8105 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
8106
8107 infos = g_strsplit (rtpinfo, ",", 0);
8108 for (i = 0; infos[i]; i++) {
8109 gchar **fields;
8110 GstRTSPStream *stream;
8111 gint32 seqbase;
8112 gint64 timebase;
8113
8114 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
8115
8116 /* init values, types of seqbase and timebase are bigger than needed so we
8117 * can store -1 as uninitialized values */
8118 stream = NULL;
8119 seqbase = -1;
8120 timebase = -1;
8121
8122 /* parse url, find stream for url.
8123 * parse seq and rtptime. The seq number should be configured in the rtp
8124 * depayloader or session manager to detect gaps. Same for the rtptime, it
8125 * should be used to create an initial time newsegment. */
8126 fields = g_strsplit (infos[i], ";", 0);
8127 for (j = 0; fields[j]; j++) {
8128 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
8129 /* remove leading whitespace */
8130 fields[j] = g_strchug (fields[j]);
8131 if (g_str_has_prefix (fields[j], "url=")) {
8132 /* get the url and the stream */
8133 stream =
8134 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
8135 } else if (g_str_has_prefix (fields[j], "seq=")) {
8136 seqbase = atoi (fields[j] + 4);
8137 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
8138 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
8139 }
8140 }
8141 g_strfreev (fields);
8142 /* now we need to store the values for the caps of the stream */
8143 if (stream != NULL) {
8144 GST_DEBUG_OBJECT (src,
8145 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
8146 stream, seqbase, timebase);
8147
8148 /* we have a stream, configure detected params */
8149 stream->seqbase = seqbase;
8150 stream->timebase = timebase;
8151 }
8152 }
8153 g_strfreev (infos);
8154
8155 return TRUE;
8156 }
8157
8158 static void
gst_rtspsrc_handle_rtcp_interval(GstRTSPSrc * src,gchar * rtcp)8159 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
8160 {
8161 guint64 interval;
8162 GList *walk;
8163
8164 interval = strtoul (rtcp, NULL, 10);
8165 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
8166
8167 if (!interval)
8168 return;
8169
8170 interval *= GST_MSECOND;
8171
8172 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8173 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8174
8175 /* already (optionally) retrieved this when configuring manager */
8176 if (stream->session) {
8177 GObject *rtpsession = stream->session;
8178
8179 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
8180 rtpsession);
8181 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
8182 }
8183 }
8184
8185 /* now it happens that (Xenon) server sending this may also provide bogus
8186 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
8187 * and just use RTP-Info to sync */
8188 if (src->manager) {
8189 GObjectClass *klass;
8190
8191 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
8192 if (g_object_class_find_property (klass, "rtcp-sync")) {
8193 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
8194 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
8195 }
8196 }
8197 }
8198
8199 static gdouble
gst_rtspsrc_get_float(const gchar * dstr)8200 gst_rtspsrc_get_float (const gchar * dstr)
8201 {
8202 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8203
8204 /* canonicalise floating point string so we can handle float strings
8205 * in the form "24.930" or "24,930" irrespective of the current locale */
8206 g_strlcpy (s, dstr, sizeof (s));
8207 g_strdelimit (s, ",", '.');
8208 return g_ascii_strtod (s, NULL);
8209 }
8210
8211 static gchar *
gen_range_header(GstRTSPSrc * src,GstSegment * segment)8212 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
8213 {
8214 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
8215
8216 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
8217 g_strlcpy (val_str, "now", sizeof (val_str));
8218 } else {
8219 if (segment->position == 0) {
8220 g_strlcpy (val_str, "0", sizeof (val_str));
8221 } else {
8222 g_ascii_dtostr (val_str, sizeof (val_str),
8223 ((gdouble) segment->position) / GST_SECOND);
8224 }
8225 }
8226 return g_strdup_printf ("npt=%s-", val_str);
8227 }
8228
8229 static void
clear_rtp_base(GstRTSPSrc * src,GstRTSPStream * stream)8230 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
8231 {
8232 guint i, len;
8233
8234 stream->timebase = -1;
8235 stream->seqbase = -1;
8236
8237 len = stream->ptmap->len;
8238 for (i = 0; i < len; i++) {
8239 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
8240 GstStructure *s;
8241
8242 if (item->caps == NULL)
8243 continue;
8244
8245 item->caps = gst_caps_make_writable (item->caps);
8246 s = gst_caps_get_structure (item->caps, 0);
8247 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
8248 if (item->pt == stream->default_pt && stream->udpsrc[0])
8249 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
8250 }
8251 stream->need_caps = TRUE;
8252 }
8253
8254 static GstRTSPResult
gst_rtspsrc_ensure_open(GstRTSPSrc * src,gboolean async)8255 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
8256 {
8257 GstRTSPResult res = GST_RTSP_OK;
8258
8259 if (src->state < GST_RTSP_STATE_READY) {
8260 res = GST_RTSP_ERROR;
8261 if (src->open_error) {
8262 GST_DEBUG_OBJECT (src, "the stream was in error");
8263 goto done;
8264 }
8265 if (async)
8266 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
8267
8268 if ((res = gst_rtspsrc_open (src, async)) < 0) {
8269 GST_DEBUG_OBJECT (src, "failed to open stream");
8270 goto done;
8271 }
8272 }
8273
8274 done:
8275 return res;
8276 }
8277
8278 static GstRTSPResult
gst_rtspsrc_play(GstRTSPSrc * src,GstSegment * segment,gboolean async,const gchar * seek_style)8279 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
8280 const gchar * seek_style)
8281 {
8282 GstRTSPMessage request = { 0 };
8283 GstRTSPMessage response = { 0 };
8284 GstRTSPResult res = GST_RTSP_OK;
8285 GList *walk;
8286 gchar *hval;
8287 gint hval_idx;
8288 const gchar *control;
8289
8290 GST_DEBUG_OBJECT (src, "PLAY...");
8291
8292 restart:
8293 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8294 goto open_failed;
8295
8296 if (!(src->methods & GST_RTSP_PLAY))
8297 goto not_supported;
8298
8299 if (src->state == GST_RTSP_STATE_PLAYING)
8300 goto was_playing;
8301
8302 if (!src->conninfo.connection || !src->conninfo.connected)
8303 goto done;
8304
8305 /* send some dummy packets before we activate the receive in the
8306 * udp sources */
8307 gst_rtspsrc_send_dummy_packets (src);
8308
8309 /* require new SR packets */
8310 if (src->manager)
8311 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
8312
8313 /* construct a control url */
8314 control = get_aggregate_control (src);
8315
8316 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8317 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8318 const gchar *setup_url;
8319 GstRTSPConnInfo *conninfo;
8320
8321 /* try aggregate control first but do non-aggregate control otherwise */
8322 if (control)
8323 setup_url = control;
8324 else if ((setup_url = stream->conninfo.location) == NULL)
8325 continue;
8326
8327 if (src->conninfo.connection) {
8328 conninfo = &src->conninfo;
8329 } else if (stream->conninfo.connection) {
8330 conninfo = &stream->conninfo;
8331 } else {
8332 continue;
8333 }
8334
8335 /* do play */
8336 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
8337 if (res < 0)
8338 goto create_request_failed;
8339
8340 if (src->need_range && src->seekable >= 0.0) {
8341 hval = gen_range_header (src, segment);
8342
8343 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
8344
8345 /* store the newsegment event so it can be sent from the streaming thread. */
8346 src->need_segment = TRUE;
8347 }
8348
8349 if (segment->rate != 1.0) {
8350 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
8351
8352 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
8353 if (src->skip)
8354 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
8355 else
8356 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
8357 }
8358
8359 if (seek_style)
8360 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
8361 seek_style);
8362
8363 /* when we have an ONVIF audio backchannel, the PLAY request must have the
8364 * Require: header when doing either aggregate or non-aggregate control */
8365 if (src->backchannel == BACKCHANNEL_ONVIF &&
8366 (control || stream->is_backchannel))
8367 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8368 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8369
8370 if (async)
8371 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
8372
8373 if ((res =
8374 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
8375 < 0)
8376 goto send_error;
8377
8378 if (src->need_redirect) {
8379 GST_DEBUG_OBJECT (src,
8380 "redirect: tearing down and restarting with new url");
8381 /* teardown and restart with new url */
8382 gst_rtspsrc_close (src, TRUE, FALSE);
8383 /* reset protocols to force re-negotiation with redirected url */
8384 src->cur_protocols = src->protocols;
8385 gst_rtsp_message_unset (&request);
8386 gst_rtsp_message_unset (&response);
8387 goto restart;
8388 }
8389
8390 /* seek may have silently failed as it is not supported */
8391 if (!(src->methods & GST_RTSP_PLAY)) {
8392 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
8393
8394 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
8395 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
8396 " playing with range failed... Ignoring information.");
8397 }
8398 /* obviously it is supported as we made it here */
8399 src->methods |= GST_RTSP_PLAY;
8400 src->seekable = -1.0;
8401 /* but there is nothing to parse in the response,
8402 * so convey we have no idea and not to expect anything particular */
8403 clear_rtp_base (src, stream);
8404 if (control) {
8405 GList *run;
8406
8407 /* need to do for all streams */
8408 for (run = src->streams; run; run = g_list_next (run))
8409 clear_rtp_base (src, (GstRTSPStream *) run->data);
8410 }
8411 /* NOTE the above also disables npt based eos detection */
8412 /* and below forces position to 0,
8413 * which is visible feedback we lost the plot */
8414 segment->start = segment->position = src->last_pos;
8415 }
8416
8417 gst_rtsp_message_unset (&request);
8418
8419 /* parse RTP npt field. This is the current position in the stream (Normal
8420 * Play Time) and should be put in the NEWSEGMENT position field. */
8421 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8422 0) == GST_RTSP_OK)
8423 gst_rtspsrc_parse_range (src, hval, segment);
8424
8425 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8426 segment->rate = 1.0;
8427
8428 /* parse Speed header. This is the intended playback rate of the stream
8429 * and should be put in the NEWSEGMENT rate field. */
8430 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8431 0) == GST_RTSP_OK) {
8432 segment->rate = gst_rtspsrc_get_float (hval);
8433 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8434 &hval, 0) == GST_RTSP_OK) {
8435 segment->rate = gst_rtspsrc_get_float (hval);
8436 }
8437
8438 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
8439 * for the RTP packets. If this is not present, we assume all starts from 0...
8440 * This is info for the RTP session manager that we pass to it in caps. */
8441 hval_idx = 0;
8442 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
8443 &hval, hval_idx++) == GST_RTSP_OK)
8444 gst_rtspsrc_parse_rtpinfo (src, hval);
8445
8446 /* some servers indicate RTCP parameters in PLAY response,
8447 * rather than properly in SDP */
8448 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
8449 &hval, 0) == GST_RTSP_OK)
8450 gst_rtspsrc_handle_rtcp_interval (src, hval);
8451
8452 gst_rtsp_message_unset (&response);
8453
8454 /* early exit when we did aggregate control */
8455 if (control)
8456 break;
8457 }
8458 /* configure the caps of the streams after we parsed all headers. Only reset
8459 * the manager object when we set a new Range header (we did a seek) */
8460 gst_rtspsrc_configure_caps (src, segment, src->need_range);
8461
8462 /* set to PLAYING after we have configured the caps, otherwise we
8463 * might end up calling request_key (with SRTP) while caps are still
8464 * being configured. */
8465 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
8466
8467 /* set again when needed */
8468 src->need_range = FALSE;
8469
8470 src->running = TRUE;
8471 src->base_time = -1;
8472 src->state = GST_RTSP_STATE_PLAYING;
8473
8474 /* mark discont */
8475 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
8476 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8477 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8478 stream->discont = TRUE;
8479 }
8480
8481 done:
8482 if (async)
8483 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
8484
8485 return res;
8486
8487 /* ERRORS */
8488 open_failed:
8489 {
8490 GST_WARNING_OBJECT (src, "failed to open stream");
8491 goto done;
8492 }
8493 not_supported:
8494 {
8495 GST_WARNING_OBJECT (src, "PLAY is not supported");
8496 goto done;
8497 }
8498 was_playing:
8499 {
8500 GST_WARNING_OBJECT (src, "we were already PLAYING");
8501 goto done;
8502 }
8503 create_request_failed:
8504 {
8505 gchar *str = gst_rtsp_strresult (res);
8506
8507 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8508 ("Could not create request. (%s)", str));
8509 g_free (str);
8510 goto done;
8511 }
8512 send_error:
8513 {
8514 gchar *str = gst_rtsp_strresult (res);
8515
8516 gst_rtsp_message_unset (&request);
8517 if (res != GST_RTSP_EINTR) {
8518 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8519 ("Could not send message. (%s)", str));
8520 } else {
8521 GST_WARNING_OBJECT (src, "PLAY interrupted");
8522 }
8523 g_free (str);
8524 goto done;
8525 }
8526 }
8527
8528 static GstRTSPResult
gst_rtspsrc_pause(GstRTSPSrc * src,gboolean async)8529 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
8530 {
8531 GstRTSPResult res = GST_RTSP_OK;
8532 GstRTSPMessage request = { 0 };
8533 GstRTSPMessage response = { 0 };
8534 GList *walk;
8535 const gchar *control;
8536
8537 GST_DEBUG_OBJECT (src, "PAUSE...");
8538
8539 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8540 goto open_failed;
8541
8542 if (!(src->methods & GST_RTSP_PAUSE))
8543 goto not_supported;
8544
8545 if (src->state == GST_RTSP_STATE_READY)
8546 goto was_paused;
8547
8548 if (!src->conninfo.connection || !src->conninfo.connected)
8549 goto no_connection;
8550
8551 /* construct a control url */
8552 control = get_aggregate_control (src);
8553
8554 /* loop over the streams. We might exit the loop early when we could do an
8555 * aggregate control */
8556 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8557 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8558 GstRTSPConnInfo *conninfo;
8559 const gchar *setup_url;
8560
8561 /* try aggregate control first but do non-aggregate control otherwise */
8562 if (control)
8563 setup_url = control;
8564 else if ((setup_url = stream->conninfo.location) == NULL)
8565 continue;
8566
8567 if (src->conninfo.connection) {
8568 conninfo = &src->conninfo;
8569 } else if (stream->conninfo.connection) {
8570 conninfo = &stream->conninfo;
8571 } else {
8572 continue;
8573 }
8574
8575 if (async)
8576 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
8577 ("Sending PAUSE request"));
8578
8579 if ((res =
8580 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
8581 setup_url)) < 0)
8582 goto create_request_failed;
8583
8584 /* when we have an ONVIF audio backchannel, the PAUSE request must have the
8585 * Require: header when doing either aggregate or non-aggregate control */
8586 if (src->backchannel == BACKCHANNEL_ONVIF &&
8587 (control || stream->is_backchannel))
8588 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8589 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8590
8591 if ((res =
8592 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
8593 NULL)) < 0)
8594 goto send_error;
8595
8596 gst_rtsp_message_unset (&request);
8597 gst_rtsp_message_unset (&response);
8598
8599 /* exit early when we did agregate control */
8600 if (control)
8601 break;
8602 }
8603
8604 /* change element states now */
8605 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
8606
8607 no_connection:
8608 src->state = GST_RTSP_STATE_READY;
8609
8610 done:
8611 if (async)
8612 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
8613
8614 return res;
8615
8616 /* ERRORS */
8617 open_failed:
8618 {
8619 GST_DEBUG_OBJECT (src, "failed to open stream");
8620 goto done;
8621 }
8622 not_supported:
8623 {
8624 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
8625 goto done;
8626 }
8627 was_paused:
8628 {
8629 GST_DEBUG_OBJECT (src, "we were already PAUSED");
8630 goto done;
8631 }
8632 create_request_failed:
8633 {
8634 gchar *str = gst_rtsp_strresult (res);
8635
8636 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8637 ("Could not create request. (%s)", str));
8638 g_free (str);
8639 goto done;
8640 }
8641 send_error:
8642 {
8643 gchar *str = gst_rtsp_strresult (res);
8644
8645 gst_rtsp_message_unset (&request);
8646 if (res != GST_RTSP_EINTR) {
8647 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8648 ("Could not send message. (%s)", str));
8649 } else {
8650 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8651 }
8652 g_free (str);
8653 goto done;
8654 }
8655 }
8656
8657 static void
gst_rtspsrc_handle_message(GstBin * bin,GstMessage * message)8658 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8659 {
8660 GstRTSPSrc *rtspsrc;
8661
8662 rtspsrc = GST_RTSPSRC (bin);
8663
8664 switch (GST_MESSAGE_TYPE (message)) {
8665 case GST_MESSAGE_EOS:
8666 gst_message_unref (message);
8667 break;
8668 case GST_MESSAGE_ELEMENT:
8669 {
8670 const GstStructure *s = gst_message_get_structure (message);
8671
8672 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8673 gboolean ignore_timeout;
8674
8675 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8676
8677 GST_OBJECT_LOCK (rtspsrc);
8678 ignore_timeout = rtspsrc->ignore_timeout;
8679 rtspsrc->ignore_timeout = TRUE;
8680 GST_OBJECT_UNLOCK (rtspsrc);
8681
8682 /* we only act on the first udp timeout message, others are irrelevant
8683 * and can be ignored. */
8684 if (!ignore_timeout)
8685 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8686 /* eat and free */
8687 gst_message_unref (message);
8688 return;
8689 }
8690 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8691 break;
8692 }
8693 case GST_MESSAGE_ERROR:
8694 {
8695 GstObject *udpsrc;
8696 GstRTSPStream *stream;
8697 GstFlowReturn ret;
8698
8699 udpsrc = GST_MESSAGE_SRC (message);
8700
8701 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8702 GST_ELEMENT_NAME (udpsrc));
8703
8704 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8705 if (!stream)
8706 goto forward;
8707
8708 /* we ignore the RTCP udpsrc */
8709 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8710 goto done;
8711
8712 /* if we get error messages from the udp sources, that's not a problem as
8713 * long as not all of them error out. We also don't really know what the
8714 * problem is, the message does not give enough detail... */
8715 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8716 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8717 if (ret != GST_FLOW_OK)
8718 goto forward;
8719
8720 done:
8721 gst_message_unref (message);
8722 break;
8723
8724 forward:
8725 /* fatal but not our message, forward */
8726 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8727 break;
8728 }
8729 default:
8730 {
8731 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8732 break;
8733 }
8734 }
8735 }
8736
8737 /* the thread where everything happens */
8738 static void
gst_rtspsrc_thread(GstRTSPSrc * src)8739 gst_rtspsrc_thread (GstRTSPSrc * src)
8740 {
8741 gint cmd;
8742 ParameterRequest *req = NULL;
8743
8744 GST_OBJECT_LOCK (src);
8745 cmd = src->pending_cmd;
8746 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8747 || cmd == CMD_LOOP || cmd == CMD_OPEN || cmd == CMD_GET_PARAMETER
8748 || cmd == CMD_SET_PARAMETER) {
8749 if (g_queue_is_empty (&src->set_get_param_q)) {
8750 src->pending_cmd = CMD_LOOP;
8751 } else {
8752 ParameterRequest *next_req;
8753 req = g_queue_pop_head (&src->set_get_param_q);
8754 next_req = g_queue_peek_head (&src->set_get_param_q);
8755 src->pending_cmd = next_req ? next_req->cmd : CMD_LOOP;
8756 }
8757 } else
8758 src->pending_cmd = CMD_WAIT;
8759 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8760
8761 /* we got the message command, so ensure communication is possible again */
8762 gst_rtspsrc_connection_flush (src, FALSE);
8763
8764 src->busy_cmd = cmd;
8765 GST_OBJECT_UNLOCK (src);
8766
8767 switch (cmd) {
8768 case CMD_OPEN:
8769 gst_rtspsrc_open (src, TRUE);
8770 break;
8771 case CMD_PLAY:
8772 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
8773 break;
8774 case CMD_PAUSE:
8775 gst_rtspsrc_pause (src, TRUE);
8776 break;
8777 case CMD_CLOSE:
8778 gst_rtspsrc_close (src, TRUE, FALSE);
8779 break;
8780 case CMD_GET_PARAMETER:
8781 gst_rtspsrc_get_parameter (src, req);
8782 break;
8783 case CMD_SET_PARAMETER:
8784 gst_rtspsrc_set_parameter (src, req);
8785 break;
8786 case CMD_LOOP:
8787 gst_rtspsrc_loop (src);
8788 break;
8789 case CMD_RECONNECT:
8790 gst_rtspsrc_reconnect (src, FALSE);
8791 break;
8792 default:
8793 break;
8794 }
8795
8796 GST_OBJECT_LOCK (src);
8797 /* No more cmds, wake any waiters */
8798 g_cond_broadcast (&src->cmd_cond);
8799 /* and go back to sleep */
8800 if (src->pending_cmd == CMD_WAIT) {
8801 if (src->task)
8802 gst_task_pause (src->task);
8803 }
8804 /* reset waiting */
8805 src->busy_cmd = CMD_WAIT;
8806 GST_OBJECT_UNLOCK (src);
8807 }
8808
8809 static gboolean
gst_rtspsrc_start(GstRTSPSrc * src)8810 gst_rtspsrc_start (GstRTSPSrc * src)
8811 {
8812 GST_DEBUG_OBJECT (src, "starting");
8813
8814 GST_OBJECT_LOCK (src);
8815
8816 src->pending_cmd = CMD_WAIT;
8817
8818 if (src->task == NULL) {
8819 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8820 if (src->task == NULL)
8821 goto task_error;
8822
8823 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8824 }
8825 GST_OBJECT_UNLOCK (src);
8826
8827 return TRUE;
8828
8829 /* ERRORS */
8830 task_error:
8831 {
8832 GST_OBJECT_UNLOCK (src);
8833 GST_ERROR_OBJECT (src, "failed to create task");
8834 return FALSE;
8835 }
8836 }
8837
8838 static gboolean
gst_rtspsrc_stop(GstRTSPSrc * src)8839 gst_rtspsrc_stop (GstRTSPSrc * src)
8840 {
8841 GstTask *task;
8842
8843 GST_DEBUG_OBJECT (src, "stopping");
8844
8845 /* also cancels pending task */
8846 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8847
8848 GST_OBJECT_LOCK (src);
8849 if ((task = src->task)) {
8850 src->task = NULL;
8851 GST_OBJECT_UNLOCK (src);
8852
8853 gst_task_stop (task);
8854
8855 /* make sure it is not running */
8856 GST_RTSP_STREAM_LOCK (src);
8857 GST_RTSP_STREAM_UNLOCK (src);
8858
8859 /* now wait for the task to finish */
8860 gst_task_join (task);
8861
8862 /* and free the task */
8863 gst_object_unref (GST_OBJECT (task));
8864
8865 GST_OBJECT_LOCK (src);
8866 }
8867 GST_OBJECT_UNLOCK (src);
8868
8869 /* ensure synchronously all is closed and clean */
8870 gst_rtspsrc_close (src, FALSE, TRUE);
8871
8872 return TRUE;
8873 }
8874
8875 static GstStateChangeReturn
gst_rtspsrc_change_state(GstElement * element,GstStateChange transition)8876 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8877 {
8878 GstRTSPSrc *rtspsrc;
8879 GstStateChangeReturn ret;
8880
8881 rtspsrc = GST_RTSPSRC (element);
8882
8883 switch (transition) {
8884 case GST_STATE_CHANGE_NULL_TO_READY:
8885 if (!gst_rtspsrc_start (rtspsrc))
8886 goto start_failed;
8887 break;
8888 case GST_STATE_CHANGE_READY_TO_PAUSED:
8889 /* init some state */
8890 rtspsrc->cur_protocols = rtspsrc->protocols;
8891 /* first attempt, don't ignore timeouts */
8892 rtspsrc->ignore_timeout = FALSE;
8893 rtspsrc->open_error = FALSE;
8894 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8895 break;
8896 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8897 set_manager_buffer_mode (rtspsrc);
8898 /* fall-through */
8899 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8900 /* unblock the tcp tasks and make the loop waiting */
8901 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8902 /* make sure it is waiting before we send PAUSE or PLAY below */
8903 GST_RTSP_STREAM_LOCK (rtspsrc);
8904 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8905 }
8906 break;
8907 case GST_STATE_CHANGE_PAUSED_TO_READY:
8908 break;
8909 default:
8910 break;
8911 }
8912
8913 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8914 if (ret == GST_STATE_CHANGE_FAILURE)
8915 goto done;
8916
8917 switch (transition) {
8918 case GST_STATE_CHANGE_NULL_TO_READY:
8919 ret = GST_STATE_CHANGE_SUCCESS;
8920 break;
8921 case GST_STATE_CHANGE_READY_TO_PAUSED:
8922 ret = GST_STATE_CHANGE_NO_PREROLL;
8923 break;
8924 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8925 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8926 ret = GST_STATE_CHANGE_SUCCESS;
8927 break;
8928 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8929 /* send pause request and keep the idle task around */
8930 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8931 ret = GST_STATE_CHANGE_NO_PREROLL;
8932 break;
8933 case GST_STATE_CHANGE_PAUSED_TO_READY:
8934 gst_rtspsrc_loop_send_cmd_and_wait (rtspsrc, CMD_CLOSE, CMD_ALL,
8935 rtspsrc->teardown_timeout);
8936 ret = GST_STATE_CHANGE_SUCCESS;
8937 break;
8938 case GST_STATE_CHANGE_READY_TO_NULL:
8939 gst_rtspsrc_stop (rtspsrc);
8940 ret = GST_STATE_CHANGE_SUCCESS;
8941 break;
8942 default:
8943 /* Otherwise it's success, we don't want to return spurious
8944 * NO_PREROLL or ASYNC from internal elements as we care for
8945 * state changes ourselves here
8946 *
8947 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
8948 */
8949 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
8950 ret = GST_STATE_CHANGE_NO_PREROLL;
8951 else
8952 ret = GST_STATE_CHANGE_SUCCESS;
8953 break;
8954 }
8955
8956 done:
8957 return ret;
8958
8959 start_failed:
8960 {
8961 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8962 return GST_STATE_CHANGE_FAILURE;
8963 }
8964 }
8965
8966 static gboolean
gst_rtspsrc_send_event(GstElement * element,GstEvent * event)8967 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8968 {
8969 gboolean res;
8970 GstRTSPSrc *rtspsrc;
8971
8972 rtspsrc = GST_RTSPSRC (element);
8973
8974 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8975 res = gst_rtspsrc_push_event (rtspsrc, event);
8976 } else {
8977 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8978 }
8979
8980 return res;
8981 }
8982
8983
8984 /*** GSTURIHANDLER INTERFACE *************************************************/
8985
8986 static GstURIType
gst_rtspsrc_uri_get_type(GType type)8987 gst_rtspsrc_uri_get_type (GType type)
8988 {
8989 return GST_URI_SRC;
8990 }
8991
8992 static const gchar *const *
gst_rtspsrc_uri_get_protocols(GType type)8993 gst_rtspsrc_uri_get_protocols (GType type)
8994 {
8995 static const gchar *protocols[] =
8996 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8997 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8998 };
8999
9000 return protocols;
9001 }
9002
9003 static gchar *
gst_rtspsrc_uri_get_uri(GstURIHandler * handler)9004 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
9005 {
9006 GstRTSPSrc *src = GST_RTSPSRC (handler);
9007
9008 /* FIXME: make thread-safe */
9009 return g_strdup (src->conninfo.location);
9010 }
9011
9012 static gboolean
gst_rtspsrc_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)9013 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
9014 GError ** error)
9015 {
9016 GstRTSPSrc *src;
9017 GstRTSPResult res;
9018 GstSDPResult sres;
9019 GstRTSPUrl *newurl = NULL;
9020 GstSDPMessage *sdp = NULL;
9021
9022 src = GST_RTSPSRC (handler);
9023
9024 /* same URI, we're fine */
9025 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
9026 goto was_ok;
9027
9028 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
9029 sres = gst_sdp_message_new (&sdp);
9030 if (sres < 0)
9031 goto sdp_failed;
9032
9033 GST_DEBUG_OBJECT (src, "parsing SDP message");
9034 sres = gst_sdp_message_parse_uri (uri, sdp);
9035 if (sres < 0)
9036 goto invalid_sdp;
9037 } else {
9038 /* try to parse */
9039 GST_DEBUG_OBJECT (src, "parsing URI");
9040 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
9041 goto parse_error;
9042 }
9043
9044 /* if worked, free previous and store new url object along with the original
9045 * location. */
9046 GST_DEBUG_OBJECT (src, "configuring URI");
9047 g_free (src->conninfo.location);
9048 src->conninfo.location = g_strdup (uri);
9049 gst_rtsp_url_free (src->conninfo.url);
9050 src->conninfo.url = newurl;
9051 g_free (src->conninfo.url_str);
9052 if (newurl)
9053 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
9054 else
9055 src->conninfo.url_str = NULL;
9056
9057 if (src->sdp)
9058 gst_sdp_message_free (src->sdp);
9059 src->sdp = sdp;
9060 src->from_sdp = sdp != NULL;
9061
9062 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
9063 GST_DEBUG_OBJECT (src, "request uri is: %s",
9064 GST_STR_NULL (src->conninfo.url_str));
9065
9066 return TRUE;
9067
9068 /* Special cases */
9069 was_ok:
9070 {
9071 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
9072 return TRUE;
9073 }
9074 sdp_failed:
9075 {
9076 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
9077 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9078 "Could not create SDP");
9079 return FALSE;
9080 }
9081 invalid_sdp:
9082 {
9083 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
9084 GST_STR_NULL (uri));
9085 gst_sdp_message_free (sdp);
9086 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9087 "Invalid SDP");
9088 return FALSE;
9089 }
9090 parse_error:
9091 {
9092 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
9093 GST_STR_NULL (uri), res);
9094 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
9095 "Invalid RTSP URI");
9096 return FALSE;
9097 }
9098 }
9099
9100 static void
gst_rtspsrc_uri_handler_init(gpointer g_iface,gpointer iface_data)9101 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
9102 {
9103 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
9104
9105 iface->get_type = gst_rtspsrc_uri_get_type;
9106 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
9107 iface->get_uri = gst_rtspsrc_uri_get_uri;
9108 iface->set_uri = gst_rtspsrc_uri_set_uri;
9109 }
9110
9111
9112 /* send GET_PARAMETER */
9113 static GstRTSPResult
gst_rtspsrc_get_parameter(GstRTSPSrc * src,ParameterRequest * req)9114 gst_rtspsrc_get_parameter (GstRTSPSrc * src, ParameterRequest * req)
9115 {
9116 GstRTSPMessage request = { 0 };
9117 GstRTSPMessage response = { 0 };
9118 GstRTSPResult res;
9119 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9120 const gchar *control;
9121 gchar *recv_body = NULL;
9122 guint recv_body_len;
9123
9124 GST_DEBUG_OBJECT (src, "creating server get_parameter");
9125
9126 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9127 goto open_failed;
9128
9129 control = get_aggregate_control (src);
9130 if (control == NULL)
9131 goto no_control;
9132
9133 if (!(src->methods & GST_RTSP_GET_PARAMETER))
9134 goto not_supported;
9135
9136 gst_rtspsrc_connection_flush (src, FALSE);
9137
9138 res = gst_rtsp_message_init_request (&request, GST_RTSP_GET_PARAMETER,
9139 control);
9140 if (res < 0)
9141 goto create_request_failed;
9142
9143 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9144 req->content_type == NULL ? "text/parameters" : req->content_type);
9145 if (res < 0)
9146 goto add_content_hdr_failed;
9147
9148 if (req->body && req->body->len) {
9149 res =
9150 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9151 req->body->len);
9152 if (res < 0)
9153 goto set_body_failed;
9154 }
9155
9156 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9157 &request, &response, &code, NULL)) < 0)
9158 goto send_error;
9159
9160 res = gst_rtsp_message_get_body (&response, (guint8 **) & recv_body,
9161 &recv_body_len);
9162 if (res < 0)
9163 goto get_body_failed;
9164
9165 done:
9166 {
9167 gst_promise_reply (req->promise,
9168 gst_structure_new ("get-parameter-reply",
9169 "rtsp-result", G_TYPE_INT, res,
9170 "rtsp-code", G_TYPE_INT, code,
9171 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9172 "body", G_TYPE_STRING, GST_STR_NULL (recv_body), NULL));
9173 free_param_data (req);
9174
9175
9176 gst_rtsp_message_unset (&request);
9177 gst_rtsp_message_unset (&response);
9178
9179 return res;
9180 }
9181
9182 /* ERRORS */
9183 open_failed:
9184 {
9185 GST_DEBUG_OBJECT (src, "failed to open stream");
9186 goto done;
9187 }
9188 no_control:
9189 {
9190 GST_DEBUG_OBJECT (src, "no control url to send GET_PARAMETER");
9191 res = GST_RTSP_ERROR;
9192 goto done;
9193 }
9194 not_supported:
9195 {
9196 GST_DEBUG_OBJECT (src, "GET_PARAMETER is not supported");
9197 res = GST_RTSP_ERROR;
9198 goto done;
9199 }
9200 create_request_failed:
9201 {
9202 GST_DEBUG_OBJECT (src, "could not create GET_PARAMETER request");
9203 goto done;
9204 }
9205 add_content_hdr_failed:
9206 {
9207 GST_DEBUG_OBJECT (src, "could not add content header");
9208 goto done;
9209 }
9210 set_body_failed:
9211 {
9212 GST_DEBUG_OBJECT (src, "could not set body");
9213 goto done;
9214 }
9215 send_error:
9216 {
9217 gchar *str = gst_rtsp_strresult (res);
9218
9219 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9220 ("Could not send get-parameter. (%s)", str));
9221 g_free (str);
9222 goto done;
9223 }
9224 get_body_failed:
9225 {
9226 GST_DEBUG_OBJECT (src, "could not get body");
9227 goto done;
9228 }
9229 }
9230
9231 /* send SET_PARAMETER */
9232 static GstRTSPResult
gst_rtspsrc_set_parameter(GstRTSPSrc * src,ParameterRequest * req)9233 gst_rtspsrc_set_parameter (GstRTSPSrc * src, ParameterRequest * req)
9234 {
9235 GstRTSPMessage request = { 0 };
9236 GstRTSPMessage response = { 0 };
9237 GstRTSPResult res = GST_RTSP_OK;
9238 GstRTSPStatusCode code = GST_RTSP_STS_OK;
9239 const gchar *control;
9240
9241 GST_DEBUG_OBJECT (src, "creating server set_parameter");
9242
9243 if ((res = gst_rtspsrc_ensure_open (src, FALSE)) < 0)
9244 goto open_failed;
9245
9246 control = get_aggregate_control (src);
9247 if (control == NULL)
9248 goto no_control;
9249
9250 if (!(src->methods & GST_RTSP_SET_PARAMETER))
9251 goto not_supported;
9252
9253 gst_rtspsrc_connection_flush (src, FALSE);
9254
9255 res =
9256 gst_rtsp_message_init_request (&request, GST_RTSP_SET_PARAMETER, control);
9257 if (res < 0)
9258 goto send_error;
9259
9260 res = gst_rtsp_message_add_header (&request, GST_RTSP_HDR_CONTENT_TYPE,
9261 req->content_type == NULL ? "text/parameters" : req->content_type);
9262 if (res < 0)
9263 goto add_content_hdr_failed;
9264
9265 if (req->body && req->body->len) {
9266 res =
9267 gst_rtsp_message_set_body (&request, (guint8 *) req->body->str,
9268 req->body->len);
9269
9270 if (res < 0)
9271 goto set_body_failed;
9272 }
9273
9274 if ((res = gst_rtspsrc_send (src, &src->conninfo,
9275 &request, &response, &code, NULL)) < 0)
9276 goto send_error;
9277
9278 done:
9279 {
9280 gst_promise_reply (req->promise, gst_structure_new ("set-parameter-reply",
9281 "rtsp-result", G_TYPE_INT, res,
9282 "rtsp-code", G_TYPE_INT, code,
9283 "rtsp-reason", G_TYPE_STRING, gst_rtsp_status_as_text (code),
9284 NULL));
9285 free_param_data (req);
9286
9287 gst_rtsp_message_unset (&request);
9288 gst_rtsp_message_unset (&response);
9289
9290 return res;
9291 }
9292
9293 /* ERRORS */
9294 open_failed:
9295 {
9296 GST_DEBUG_OBJECT (src, "failed to open stream");
9297 goto done;
9298 }
9299 no_control:
9300 {
9301 GST_DEBUG_OBJECT (src, "no control url to send SET_PARAMETER");
9302 res = GST_RTSP_ERROR;
9303 goto done;
9304 }
9305 not_supported:
9306 {
9307 GST_DEBUG_OBJECT (src, "SET_PARAMETER is not supported");
9308 res = GST_RTSP_ERROR;
9309 goto done;
9310 }
9311 add_content_hdr_failed:
9312 {
9313 GST_DEBUG_OBJECT (src, "could not add content header");
9314 goto done;
9315 }
9316 set_body_failed:
9317 {
9318 GST_DEBUG_OBJECT (src, "could not set body");
9319 goto done;
9320 }
9321 send_error:
9322 {
9323 gchar *str = gst_rtsp_strresult (res);
9324
9325 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
9326 ("Could not send set-parameter. (%s)", str));
9327 g_free (str);
9328 goto done;
9329 }
9330 }
9331
9332 typedef struct _RTSPKeyValue
9333 {
9334 GstRTSPHeaderField field;
9335 gchar *value;
9336 gchar *custom_key; /* custom header string (field is INVALID then) */
9337 } RTSPKeyValue;
9338
9339 static void
key_value_foreach(GArray * array,GFunc func,gpointer user_data)9340 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
9341 {
9342 guint i;
9343
9344 g_return_if_fail (array != NULL);
9345
9346 for (i = 0; i < array->len; i++) {
9347 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
9348 }
9349 }
9350
9351 static void
dump_key_value(gpointer data,gpointer user_data G_GNUC_UNUSED)9352 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
9353 {
9354 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
9355 GstRTSPSrc *src = GST_RTSPSRC (user_data);
9356 const gchar *key_string;
9357
9358 if (key_value->custom_key != NULL)
9359 key_string = key_value->custom_key;
9360 else
9361 key_string = gst_rtsp_header_as_text (key_value->field);
9362
9363 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
9364 key_value->value);
9365 }
9366
9367 static void
gst_rtspsrc_print_rtsp_message(GstRTSPSrc * src,const GstRTSPMessage * msg)9368 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
9369 {
9370 guint8 *data;
9371 guint size;
9372 GString *body_string = NULL;
9373
9374 g_return_if_fail (src != NULL);
9375 g_return_if_fail (msg != NULL);
9376
9377 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9378 return;
9379
9380 GST_LOG_OBJECT (src, "--------------------------------------------");
9381 switch (msg->type) {
9382 case GST_RTSP_MESSAGE_REQUEST:
9383 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
9384 GST_LOG_OBJECT (src, " request line:");
9385 GST_LOG_OBJECT (src, " method: '%s'",
9386 gst_rtsp_method_as_text (msg->type_data.request.method));
9387 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9388 GST_LOG_OBJECT (src, " version: '%s'",
9389 gst_rtsp_version_as_text (msg->type_data.request.version));
9390 GST_LOG_OBJECT (src, " headers:");
9391 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9392 GST_LOG_OBJECT (src, " body:");
9393 gst_rtsp_message_get_body (msg, &data, &size);
9394 if (size > 0) {
9395 body_string = g_string_new_len ((const gchar *) data, size);
9396 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9397 g_string_free (body_string, TRUE);
9398 body_string = NULL;
9399 }
9400 break;
9401 case GST_RTSP_MESSAGE_RESPONSE:
9402 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
9403 GST_LOG_OBJECT (src, " status line:");
9404 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
9405 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
9406 GST_LOG_OBJECT (src, " version: '%s",
9407 gst_rtsp_version_as_text (msg->type_data.response.version));
9408 GST_LOG_OBJECT (src, " headers:");
9409 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9410 gst_rtsp_message_get_body (msg, &data, &size);
9411 GST_LOG_OBJECT (src, " body: length %d", size);
9412 if (size > 0) {
9413 body_string = g_string_new_len ((const gchar *) data, size);
9414 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9415 g_string_free (body_string, TRUE);
9416 body_string = NULL;
9417 }
9418 break;
9419 case GST_RTSP_MESSAGE_HTTP_REQUEST:
9420 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
9421 GST_LOG_OBJECT (src, " request line:");
9422 GST_LOG_OBJECT (src, " method: '%s'",
9423 gst_rtsp_method_as_text (msg->type_data.request.method));
9424 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
9425 GST_LOG_OBJECT (src, " version: '%s'",
9426 gst_rtsp_version_as_text (msg->type_data.request.version));
9427 GST_LOG_OBJECT (src, " headers:");
9428 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9429 GST_LOG_OBJECT (src, " body:");
9430 gst_rtsp_message_get_body (msg, &data, &size);
9431 if (size > 0) {
9432 body_string = g_string_new_len ((const gchar *) data, size);
9433 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9434 g_string_free (body_string, TRUE);
9435 body_string = NULL;
9436 }
9437 break;
9438 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
9439 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
9440 GST_LOG_OBJECT (src, " status line:");
9441 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
9442 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
9443 GST_LOG_OBJECT (src, " version: '%s'",
9444 gst_rtsp_version_as_text (msg->type_data.response.version));
9445 GST_LOG_OBJECT (src, " headers:");
9446 key_value_foreach (msg->hdr_fields, dump_key_value, src);
9447 gst_rtsp_message_get_body (msg, &data, &size);
9448 GST_LOG_OBJECT (src, " body: length %d", size);
9449 if (size > 0) {
9450 body_string = g_string_new_len ((const gchar *) data, size);
9451 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9452 g_string_free (body_string, TRUE);
9453 body_string = NULL;
9454 }
9455 break;
9456 case GST_RTSP_MESSAGE_DATA:
9457 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
9458 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
9459 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
9460 gst_rtsp_message_get_body (msg, &data, &size);
9461 if (size > 0) {
9462 body_string = g_string_new_len ((const gchar *) data, size);
9463 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
9464 g_string_free (body_string, TRUE);
9465 body_string = NULL;
9466 }
9467 break;
9468 default:
9469 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
9470 break;
9471 }
9472 GST_LOG_OBJECT (src, "--------------------------------------------");
9473 }
9474
9475 static void
gst_rtspsrc_print_sdp_media(GstRTSPSrc * src,GstSDPMedia * media)9476 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
9477 {
9478 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
9479 GST_LOG_OBJECT (src, " port: '%u'", media->port);
9480 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
9481 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
9482 if (media->fmts && media->fmts->len > 0) {
9483 guint i;
9484
9485 GST_LOG_OBJECT (src, " formats:");
9486 for (i = 0; i < media->fmts->len; i++) {
9487 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
9488 gchar *, i));
9489 }
9490 }
9491 GST_LOG_OBJECT (src, " information: '%s'",
9492 GST_STR_NULL (media->information));
9493 if (media->connections && media->connections->len > 0) {
9494 guint i;
9495
9496 GST_LOG_OBJECT (src, " connections:");
9497 for (i = 0; i < media->connections->len; i++) {
9498 GstSDPConnection *conn =
9499 &g_array_index (media->connections, GstSDPConnection, i);
9500
9501 GST_LOG_OBJECT (src, " nettype: '%s'",
9502 GST_STR_NULL (conn->nettype));
9503 GST_LOG_OBJECT (src, " addrtype: '%s'",
9504 GST_STR_NULL (conn->addrtype));
9505 GST_LOG_OBJECT (src, " address: '%s'",
9506 GST_STR_NULL (conn->address));
9507 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
9508 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
9509 }
9510 }
9511 if (media->bandwidths && media->bandwidths->len > 0) {
9512 guint i;
9513
9514 GST_LOG_OBJECT (src, " bandwidths:");
9515 for (i = 0; i < media->bandwidths->len; i++) {
9516 GstSDPBandwidth *bw =
9517 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
9518
9519 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
9520 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
9521 }
9522 }
9523 GST_LOG_OBJECT (src, " key:");
9524 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
9525 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
9526 if (media->attributes && media->attributes->len > 0) {
9527 guint i;
9528
9529 GST_LOG_OBJECT (src, " attributes:");
9530 for (i = 0; i < media->attributes->len; i++) {
9531 GstSDPAttribute *attr =
9532 &g_array_index (media->attributes, GstSDPAttribute, i);
9533
9534 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
9535 }
9536 }
9537 }
9538
9539 void
gst_rtspsrc_print_sdp_message(GstRTSPSrc * src,const GstSDPMessage * msg)9540 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
9541 {
9542 g_return_if_fail (src != NULL);
9543 g_return_if_fail (msg != NULL);
9544
9545 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
9546 return;
9547
9548 GST_LOG_OBJECT (src, "--------------------------------------------");
9549 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
9550 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
9551 GST_LOG_OBJECT (src, " origin:");
9552 GST_LOG_OBJECT (src, " username: '%s'",
9553 GST_STR_NULL (msg->origin.username));
9554 GST_LOG_OBJECT (src, " sess_id: '%s'",
9555 GST_STR_NULL (msg->origin.sess_id));
9556 GST_LOG_OBJECT (src, " sess_version: '%s'",
9557 GST_STR_NULL (msg->origin.sess_version));
9558 GST_LOG_OBJECT (src, " nettype: '%s'",
9559 GST_STR_NULL (msg->origin.nettype));
9560 GST_LOG_OBJECT (src, " addrtype: '%s'",
9561 GST_STR_NULL (msg->origin.addrtype));
9562 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
9563 GST_LOG_OBJECT (src, " session_name: '%s'",
9564 GST_STR_NULL (msg->session_name));
9565 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
9566 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
9567
9568 if (msg->emails && msg->emails->len > 0) {
9569 guint i;
9570
9571 GST_LOG_OBJECT (src, " emails:");
9572 for (i = 0; i < msg->emails->len; i++) {
9573 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
9574 i));
9575 }
9576 }
9577 if (msg->phones && msg->phones->len > 0) {
9578 guint i;
9579
9580 GST_LOG_OBJECT (src, " phones:");
9581 for (i = 0; i < msg->phones->len; i++) {
9582 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
9583 i));
9584 }
9585 }
9586 GST_LOG_OBJECT (src, " connection:");
9587 GST_LOG_OBJECT (src, " nettype: '%s'",
9588 GST_STR_NULL (msg->connection.nettype));
9589 GST_LOG_OBJECT (src, " addrtype: '%s'",
9590 GST_STR_NULL (msg->connection.addrtype));
9591 GST_LOG_OBJECT (src, " address: '%s'",
9592 GST_STR_NULL (msg->connection.address));
9593 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
9594 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
9595 if (msg->bandwidths && msg->bandwidths->len > 0) {
9596 guint i;
9597
9598 GST_LOG_OBJECT (src, " bandwidths:");
9599 for (i = 0; i < msg->bandwidths->len; i++) {
9600 GstSDPBandwidth *bw =
9601 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
9602
9603 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
9604 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
9605 }
9606 }
9607 GST_LOG_OBJECT (src, " key:");
9608 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
9609 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
9610 if (msg->attributes && msg->attributes->len > 0) {
9611 guint i;
9612
9613 GST_LOG_OBJECT (src, " attributes:");
9614 for (i = 0; i < msg->attributes->len; i++) {
9615 GstSDPAttribute *attr =
9616 &g_array_index (msg->attributes, GstSDPAttribute, i);
9617
9618 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
9619 }
9620 }
9621 if (msg->medias && msg->medias->len > 0) {
9622 guint i;
9623
9624 GST_LOG_OBJECT (src, " medias:");
9625 for (i = 0; i < msg->medias->len; i++) {
9626 GST_LOG_OBJECT (src, " media %u:", i);
9627 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
9628 GstSDPMedia, i));
9629 }
9630 }
9631 GST_LOG_OBJECT (src, "--------------------------------------------");
9632 }
9633