2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <sys/ioctl.h>
22 #include "rtsp-client.h"
24 #include "rtsp-params.h"
26 /* temporary multicast address until it's configurable somewhere */
27 #define MCAST_ADDRESS "224.2.0.1"
29 static GMutex *tunnels_lock;
30 static GHashTable *tunnels;
40 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
41 #define GST_CAT_DEFAULT rtsp_client_debug
43 static void gst_rtsp_client_get_property (GObject * object, guint propid,
44 GValue * value, GParamSpec * pspec);
45 static void gst_rtsp_client_set_property (GObject * object, guint propid,
46 const GValue * value, GParamSpec * pspec);
47 static void gst_rtsp_client_finalize (GObject * obj);
49 static void client_session_finalized (GstRTSPClient * client,
50 GstRTSPSession * session);
51 static void unlink_session_streams (GstRTSPClient * client,
52 GstRTSPSession * session, GstRTSPSessionMedia * media);
54 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
57 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
59 GObjectClass *gobject_class;
61 gobject_class = G_OBJECT_CLASS (klass);
63 gobject_class->get_property = gst_rtsp_client_get_property;
64 gobject_class->set_property = gst_rtsp_client_set_property;
65 gobject_class->finalize = gst_rtsp_client_finalize;
67 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
68 g_param_spec_object ("session-pool", "Session Pool",
69 "The session pool to use for client session",
70 GST_TYPE_RTSP_SESSION_POOL,
71 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
73 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
74 g_param_spec_object ("media-mapping", "Media Mapping",
75 "The media mapping to use for client session",
76 GST_TYPE_RTSP_MEDIA_MAPPING,
77 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
80 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
81 tunnels_lock = g_mutex_new ();
83 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
87 gst_rtsp_client_init (GstRTSPClient * client)
92 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
96 /* unlink all media managed in this session */
97 for (medias = session->medias; medias; medias = g_list_next (medias)) {
98 unlink_session_streams (client, session,
99 (GstRTSPSessionMedia *) medias->data);
104 client_cleanup_sessions (GstRTSPClient * client)
108 /* remove weak-ref from sessions */
109 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
110 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
111 g_object_weak_unref (G_OBJECT (session),
112 (GWeakNotify) client_session_finalized, client);
113 client_unlink_session (client, session);
115 g_list_free (client->sessions);
116 client->sessions = NULL;
119 /* A client is finalized when the connection is broken */
121 gst_rtsp_client_finalize (GObject * obj)
123 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
125 GST_INFO ("finalize client %p", client);
127 client_cleanup_sessions (client);
129 gst_rtsp_connection_free (client->connection);
130 if (client->session_pool)
131 g_object_unref (client->session_pool);
132 if (client->media_mapping)
133 g_object_unref (client->media_mapping);
136 gst_rtsp_url_free (client->uri);
138 g_object_unref (client->media);
140 g_free (client->server_ip);
142 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
146 gst_rtsp_client_get_property (GObject * object, guint propid,
147 GValue * value, GParamSpec * pspec)
149 GstRTSPClient *client = GST_RTSP_CLIENT (object);
152 case PROP_SESSION_POOL:
153 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
155 case PROP_MEDIA_MAPPING:
156 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
164 gst_rtsp_client_set_property (GObject * object, guint propid,
165 const GValue * value, GParamSpec * pspec)
167 GstRTSPClient *client = GST_RTSP_CLIENT (object);
170 case PROP_SESSION_POOL:
171 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
173 case PROP_MEDIA_MAPPING:
174 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
182 * gst_rtsp_client_new:
184 * Create a new #GstRTSPClient instance.
187 gst_rtsp_client_new (void)
189 GstRTSPClient *result;
191 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
197 send_response (GstRTSPClient * client, GstRTSPSession * session,
198 GstRTSPMessage * response)
200 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
201 "GStreamer RTSP server");
203 /* remove any previous header */
204 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
206 /* add the new session header for new session ids */
210 if (session->timeout != 60)
212 g_strdup_printf ("%s; timeout=%d", session->sessionid,
215 str = g_strdup (session->sessionid);
217 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
220 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
221 gst_rtsp_message_dump (response);
224 gst_rtsp_watch_send_message (client->watch, response, NULL);
225 gst_rtsp_message_unset (response);
229 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
230 GstRTSPMessage * request)
232 GstRTSPMessage response = { 0 };
234 gst_rtsp_message_init_response (&response, code,
235 gst_rtsp_status_as_text (code), request);
237 send_response (client, NULL, &response);
241 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
243 if (uri1 == NULL || uri2 == NULL)
246 if (strcmp (uri1->abspath, uri2->abspath))
252 /* this function is called to initially find the media for the DESCRIBE request
253 * but is cached for when the same client (without breaking the connection) is
254 * doing a setup for the exact same url. */
255 static GstRTSPMedia *
256 find_media (GstRTSPClient * client, GstRTSPUrl * uri, GstRTSPMessage * request)
258 GstRTSPMediaFactory *factory;
261 if (!compare_uri (client->uri, uri)) {
262 /* remove any previously cached values before we try to construct a new
265 gst_rtsp_url_free (client->uri);
268 g_object_unref (client->media);
269 client->media = NULL;
271 if (!client->media_mapping)
274 /* find the factory for the uri first */
276 gst_rtsp_media_mapping_find_factory (client->media_mapping, uri)))
279 /* prepare the media and add it to the pipeline */
280 if (!(media = gst_rtsp_media_factory_construct (factory, uri)))
283 /* set ipv6 on the media before preparing */
284 media->is_ipv6 = client->is_ipv6;
286 /* prepare the media */
287 if (!(gst_rtsp_media_prepare (media)))
290 /* now keep track of the uri and the media */
291 client->uri = gst_rtsp_url_copy (uri);
292 client->media = media;
294 /* we have seen this uri before, used cached media */
295 media = client->media;
296 GST_INFO ("reusing cached media %p", media);
300 g_object_ref (media);
307 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
312 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
317 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
318 g_object_unref (factory);
323 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
324 g_object_unref (media);
325 g_object_unref (factory);
331 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
333 GstRTSPMessage message = { 0 };
337 gst_rtsp_message_init_data (&message, channel);
339 data = GST_BUFFER_DATA (buffer);
340 size = GST_BUFFER_SIZE (buffer);
341 gst_rtsp_message_take_body (&message, data, size);
343 /* FIXME, client->watch could have been finalized here, we need to keep an
344 * extra refcount to the watch. */
345 gst_rtsp_watch_send_message (client->watch, &message, NULL);
347 gst_rtsp_message_steal_body (&message, &data, &size);
348 gst_rtsp_message_unset (&message);
354 link_stream (GstRTSPClient * client, GstRTSPSession * session,
355 GstRTSPSessionStream * stream)
357 GST_DEBUG ("client %p: linking stream %p", client, stream);
358 gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data,
359 (GstRTSPSendFunc) do_send_data, client, NULL);
360 client->streams = g_list_prepend (client->streams, stream);
361 /* make sure our session can't expire */
362 gst_rtsp_session_prevent_expire (session);
366 unlink_stream (GstRTSPClient * client, GstRTSPSession * session,
367 GstRTSPSessionStream * stream)
369 GST_DEBUG ("client %p: unlinking stream %p", client, stream);
370 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL);
371 client->streams = g_list_remove (client->streams, stream);
372 /* our session can now expire */
373 gst_rtsp_session_allow_expire (session);
377 unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session,
378 GstRTSPSessionMedia * media)
382 n_streams = gst_rtsp_media_n_streams (media->media);
383 for (i = 0; i < n_streams; i++) {
384 GstRTSPSessionStream *sstream;
385 GstRTSPTransport *tr;
387 /* get the stream as configured in the session */
388 sstream = gst_rtsp_session_media_get_stream (media, i);
389 /* get the transport, if there is no transport configured, skip this stream */
390 if (!(tr = sstream->trans.transport))
393 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
394 /* for TCP, unlink the stream from the TCP connection of the client */
395 unlink_stream (client, session, sstream);
401 close_connection (GstRTSPClient * client)
403 const gchar *tunnelid;
405 GST_DEBUG ("client %p: closing connection", client);
407 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
408 g_mutex_lock (tunnels_lock);
409 /* remove from tunnelids */
410 g_hash_table_remove (tunnels, tunnelid);
411 g_mutex_unlock (tunnels_lock);
414 gst_rtsp_connection_close (client->connection);
415 if (client->watchid) {
416 g_source_destroy ((GSource *) client->watch);
418 client->watch = NULL;
423 handle_teardown_request (GstRTSPClient * client, GstRTSPUrl * uri,
424 GstRTSPSession * session, GstRTSPMessage * request)
426 GstRTSPSessionMedia *media;
427 GstRTSPMessage response = { 0 };
428 GstRTSPStatusCode code;
433 /* get a handle to the configuration of the media in the session */
434 media = gst_rtsp_session_get_media (session, uri);
438 /* unlink the all TCP callbacks */
439 unlink_session_streams (client, session, media);
441 /* remove the session from the watched sessions */
442 g_object_weak_unref (G_OBJECT (session),
443 (GWeakNotify) client_session_finalized, client);
444 client->sessions = g_list_remove (client->sessions, session);
446 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
448 /* unmanage the media in the session, returns false if all media session
450 if (!gst_rtsp_session_release_media (session, media)) {
451 /* remove the session */
452 gst_rtsp_session_pool_remove (client->session_pool, session);
454 /* construct the response now */
455 code = GST_RTSP_STS_OK;
456 gst_rtsp_message_init_response (&response, code,
457 gst_rtsp_status_as_text (code), request);
459 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONNECTION, "close");
461 send_response (client, session, &response);
463 close_connection (client);
470 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
475 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
481 handle_get_param_request (GstRTSPClient * client, GstRTSPUrl * uri,
482 GstRTSPSession * session, GstRTSPMessage * request)
488 res = gst_rtsp_message_get_body (request, &data, &size);
489 if (res != GST_RTSP_OK)
493 /* no body, keep-alive request */
494 send_generic_response (client, GST_RTSP_STS_OK, request);
496 /* there is a body */
497 GstRTSPMessage response = { 0 };
499 /* there is a body, handle the params */
500 res = gst_rtsp_params_get (client, uri, session, request, &response);
501 if (res != GST_RTSP_OK)
504 send_response (client, session, &response);
511 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
517 handle_set_param_request (GstRTSPClient * client, GstRTSPUrl * uri,
518 GstRTSPSession * session, GstRTSPMessage * request)
524 res = gst_rtsp_message_get_body (request, &data, &size);
525 if (res != GST_RTSP_OK)
529 /* no body, keep-alive request */
530 send_generic_response (client, GST_RTSP_STS_OK, request);
532 GstRTSPMessage response = { 0 };
534 /* there is a body, handle the params */
535 res = gst_rtsp_params_set (client, uri, session, request, &response);
536 if (res != GST_RTSP_OK)
539 send_response (client, session, &response);
546 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
552 handle_pause_request (GstRTSPClient * client, GstRTSPUrl * uri,
553 GstRTSPSession * session, GstRTSPMessage * request)
555 GstRTSPSessionMedia *media;
556 GstRTSPMessage response = { 0 };
557 GstRTSPStatusCode code;
562 /* get a handle to the configuration of the media in the session */
563 media = gst_rtsp_session_get_media (session, uri);
567 /* the session state must be playing or recording */
568 if (media->state != GST_RTSP_STATE_PLAYING &&
569 media->state != GST_RTSP_STATE_RECORDING)
572 /* unlink the all TCP callbacks */
573 unlink_session_streams (client, session, media);
575 /* then pause sending */
576 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
578 /* construct the response now */
579 code = GST_RTSP_STS_OK;
580 gst_rtsp_message_init_response (&response, code,
581 gst_rtsp_status_as_text (code), request);
583 send_response (client, session, &response);
585 /* the state is now READY */
586 media->state = GST_RTSP_STATE_READY;
593 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
598 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
603 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
610 handle_play_request (GstRTSPClient * client, GstRTSPUrl * uri,
611 GstRTSPSession * session, GstRTSPMessage * request)
613 GstRTSPSessionMedia *media;
614 GstRTSPMessage response = { 0 };
615 GstRTSPStatusCode code;
617 guint n_streams, i, infocount;
618 guint timestamp, seqnum;
620 GstRTSPTimeRange *range;
626 /* get a handle to the configuration of the media in the session */
627 media = gst_rtsp_session_get_media (session, uri);
631 /* the session state must be playing or ready */
632 if (media->state != GST_RTSP_STATE_PLAYING &&
633 media->state != GST_RTSP_STATE_READY)
636 /* parse the range header if we have one */
637 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_RANGE, &str, 0);
638 if (res == GST_RTSP_OK) {
639 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
640 /* we have a range, seek to the position */
641 gst_rtsp_media_seek (media->media, range);
642 gst_rtsp_range_free (range);
646 /* grab RTPInfo from the payloaders now */
647 rtpinfo = g_string_new ("");
649 n_streams = gst_rtsp_media_n_streams (media->media);
650 for (i = 0, infocount = 0; i < n_streams; i++) {
651 GstRTSPSessionStream *sstream;
652 GstRTSPMediaStream *stream;
653 GstRTSPTransport *tr;
654 GObjectClass *payobjclass;
657 /* get the stream as configured in the session */
658 sstream = gst_rtsp_session_media_get_stream (media, i);
659 /* get the transport, if there is no transport configured, skip this stream */
660 if (!(tr = sstream->trans.transport)) {
661 GST_INFO ("stream %d is not configured", i);
665 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
666 /* for TCP, link the stream to the TCP connection of the client */
667 link_stream (client, session, sstream);
670 stream = sstream->media_stream;
672 payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
674 if (g_object_class_find_property (payobjclass, "seqnum") &&
675 g_object_class_find_property (payobjclass, "timestamp")) {
678 payobj = G_OBJECT (stream->payloader);
680 /* only add RTP-Info for streams with seqnum and timestamp */
681 g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL);
684 g_string_append (rtpinfo, ", ");
686 uristr = gst_rtsp_url_get_request_uri (uri);
687 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
688 uristr, i, seqnum, timestamp);
693 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
697 /* construct the response now */
698 code = GST_RTSP_STS_OK;
699 gst_rtsp_message_init_response (&response, code,
700 gst_rtsp_status_as_text (code), request);
702 /* add the RTP-Info header */
704 str = g_string_free (rtpinfo, FALSE);
705 gst_rtsp_message_take_header (&response, GST_RTSP_HDR_RTP_INFO, str);
707 g_string_free (rtpinfo, TRUE);
711 str = gst_rtsp_range_to_string (&media->media->range);
712 gst_rtsp_message_take_header (&response, GST_RTSP_HDR_RANGE, str);
714 send_response (client, session, &response);
716 /* start playing after sending the request */
717 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
719 media->state = GST_RTSP_STATE_PLAYING;
726 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
731 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
736 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
743 do_keepalive (GstRTSPSession * session)
745 GST_INFO ("keep session %p alive", session);
746 gst_rtsp_session_touch (session);
750 handle_setup_request (GstRTSPClient * client, GstRTSPUrl * uri,
751 GstRTSPSession * session, GstRTSPMessage * request)
756 gboolean have_transport;
757 GstRTSPTransport *ct, *st;
759 GstRTSPLowerTrans supported;
760 GstRTSPMessage response = { 0 };
761 GstRTSPStatusCode code;
762 GstRTSPSessionStream *stream;
763 gchar *trans_str, *pos;
765 GstRTSPSessionMedia *media;
766 gboolean need_session;
769 /* the uri contains the stream number we added in the SDP config, which is
770 * always /stream=%d so we need to strip that off
771 * parse the stream we need to configure, look for the stream in the abspath
772 * first and then in the query. */
773 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
774 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
778 /* we can mofify the parse uri in place */
781 pos += strlen ("/stream=");
782 if (sscanf (pos, "%u", &streamid) != 1)
785 /* parse the transport */
787 gst_rtsp_message_get_header (request, GST_RTSP_HDR_TRANSPORT, &transport,
789 if (res != GST_RTSP_OK)
792 transports = g_strsplit (transport, ",", 0);
793 gst_rtsp_transport_new (&ct);
795 /* init transports */
796 have_transport = FALSE;
797 gst_rtsp_transport_init (ct);
799 /* our supported transports */
800 supported = GST_RTSP_LOWER_TRANS_UDP |
801 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
803 /* loop through the transports, try to parse */
804 for (i = 0; transports[i]; i++) {
805 res = gst_rtsp_transport_parse (transports[i], ct);
806 if (res != GST_RTSP_OK) {
807 /* no valid transport, search some more */
808 GST_WARNING ("could not parse transport %s", transports[i]);
812 /* we have a transport, see if it's RTP/AVP */
813 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
814 GST_WARNING ("invalid transport %s", transports[i]);
818 if (!(ct->lower_transport & supported)) {
819 GST_WARNING ("unsupported transport %s", transports[i]);
823 /* we have a valid transport */
824 GST_INFO ("found valid transport %s", transports[i]);
825 have_transport = TRUE;
829 gst_rtsp_transport_init (ct);
831 g_strfreev (transports);
833 /* we have not found anything usable, error out */
835 goto unsupported_transports;
837 if (client->session_pool == NULL)
840 /* we have a valid transport now, set the destination of the client. */
841 g_free (ct->destination);
842 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
843 ct->destination = g_strdup (MCAST_ADDRESS);
845 url = gst_rtsp_connection_get_url (client->connection);
846 ct->destination = g_strdup (url->host);
850 g_object_ref (session);
851 /* get a handle to the configuration of the media in the session, this can
852 * return NULL if this is a new url to manage in this session. */
853 media = gst_rtsp_session_get_media (session, uri);
855 need_session = FALSE;
857 /* create a session if this fails we probably reached our session limit or
859 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
860 goto service_unavailable;
862 /* we need a new media configuration in this session */
868 /* we have no media, find one and manage it */
872 /* get a handle to the configuration of the media in the session */
873 if ((m = find_media (client, uri, request))) {
874 /* manage the media in our session now */
875 media = gst_rtsp_session_manage_media (session, uri, m);
879 /* if we stil have no media, error */
883 /* fix the transports */
884 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
885 /* check if the client selected channels for TCP */
886 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
887 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
891 /* get a handle to the stream in the media */
892 if (!(stream = gst_rtsp_session_media_get_stream (media, streamid)))
895 st = gst_rtsp_session_stream_set_transport (stream, ct);
897 /* configure keepalive for this transport */
898 gst_rtsp_session_stream_set_keepalive (stream,
899 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
901 /* serialize the server transport */
902 trans_str = gst_rtsp_transport_as_text (st);
903 gst_rtsp_transport_free (st);
905 /* construct the response now */
906 code = GST_RTSP_STS_OK;
907 gst_rtsp_message_init_response (&response, code,
908 gst_rtsp_status_as_text (code), request);
910 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str);
913 send_response (client, session, &response);
915 /* update the state */
916 switch (media->state) {
917 case GST_RTSP_STATE_PLAYING:
918 case GST_RTSP_STATE_RECORDING:
919 case GST_RTSP_STATE_READY:
920 /* no state change */
923 media->state = GST_RTSP_STATE_READY;
926 g_object_unref (session);
933 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
938 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
939 g_object_unref (session);
944 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
945 g_object_unref (media);
946 g_object_unref (session);
951 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
954 unsupported_transports:
956 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
957 gst_rtsp_transport_free (ct);
962 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
967 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
972 static GstSDPMessage *
973 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
979 gst_sdp_message_new (&sdp);
981 /* some standard things first */
982 gst_sdp_message_set_version (sdp, "0");
989 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
992 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
993 gst_sdp_message_set_information (sdp, "rtsp-server");
994 gst_sdp_message_add_time (sdp, "0", "0", NULL);
995 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
996 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
997 gst_sdp_message_add_attribute (sdp, "control", "*");
999 info.server_proto = proto;
1000 if (media->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1001 info.server_ip = MCAST_ADDRESS;
1003 info.server_ip = client->server_ip;
1005 /* create an SDP for the media object */
1006 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1014 gst_sdp_message_free (sdp);
1019 /* for the describe we must generate an SDP */
1021 handle_describe_request (GstRTSPClient * client, GstRTSPUrl * uri,
1022 GstRTSPSession * session, GstRTSPMessage * request)
1024 GstRTSPMessage response = { 0 };
1028 gchar *str, *content_base;
1029 GstRTSPMedia *media;
1031 /* check what kind of format is accepted, we don't really do anything with it
1032 * and always return SDP for now. */
1037 gst_rtsp_message_get_header (request, GST_RTSP_HDR_ACCEPT, &accept, i);
1038 if (res == GST_RTSP_ENOTIMPL)
1041 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1045 /* find the media object for the uri */
1046 if (!(media = find_media (client, uri, request)))
1049 /* create an SDP for the media object on this client */
1050 if (!(sdp = create_sdp (client, media)))
1053 g_object_unref (media);
1055 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
1056 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
1058 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONTENT_TYPE,
1061 /* content base for some clients that might screw up creating the setup uri */
1062 str = gst_rtsp_url_get_request_uri (uri);
1063 str_len = strlen (str);
1065 /* check for trailing '/' and append one */
1066 if (str[str_len - 1] != '/') {
1067 content_base = g_malloc (str_len + 2);
1068 memcpy (content_base, str, str_len);
1069 content_base[str_len] = '/';
1070 content_base[str_len + 1] = '\0';
1076 GST_INFO ("adding content-base: %s", content_base);
1078 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONTENT_BASE,
1080 g_free (content_base);
1082 /* add SDP to the response body */
1083 str = gst_sdp_message_as_text (sdp);
1084 gst_rtsp_message_take_body (&response, (guint8 *) str, strlen (str));
1085 gst_sdp_message_free (sdp);
1087 send_response (client, session, &response);
1094 /* error reply is already sent */
1099 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
1100 g_object_unref (media);
1106 handle_options_request (GstRTSPClient * client, GstRTSPUrl * uri,
1107 GstRTSPSession * session, GstRTSPMessage * request)
1109 GstRTSPMessage response = { 0 };
1110 GstRTSPMethod options;
1113 options = GST_RTSP_DESCRIBE |
1118 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1120 str = gst_rtsp_options_as_text (options);
1122 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
1123 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
1125 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str);
1128 send_response (client, session, &response);
1133 /* remove duplicate and trailing '/' */
1135 santize_uri (GstRTSPUrl * uri)
1139 gboolean have_slash, prev_slash;
1141 s = d = uri->abspath;
1142 len = strlen (uri->abspath);
1146 for (i = 0; i < len; i++) {
1147 have_slash = s[i] == '/';
1149 if (!have_slash || !prev_slash)
1151 prev_slash = have_slash;
1153 len = d - uri->abspath;
1154 /* don't remove the first slash if that's the only thing left */
1155 if (len > 1 && *(d - 1) == '/')
1161 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1163 GST_INFO ("client %p: session %p finished", client, session);
1165 /* unlink all media managed in this session */
1166 client_unlink_session (client, session);
1168 /* remove the session */
1169 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1170 GST_INFO ("client %p: all sessions finalized, close the connection",
1172 close_connection (client);
1177 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1181 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1182 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1184 /* we already know about this session */
1185 if (msession == session)
1189 GST_INFO ("watching session %p", session);
1191 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1193 client->sessions = g_list_prepend (client->sessions, session);
1197 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1199 GstRTSPMethod method;
1200 const gchar *uristr;
1202 GstRTSPVersion version;
1204 GstRTSPSession *session;
1207 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1208 gst_rtsp_message_dump (request);
1211 GST_INFO ("client %p: received a request", client);
1213 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1215 if (version != GST_RTSP_VERSION_1_0) {
1216 /* we can only handle 1.0 requests */
1217 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1222 /* we always try to parse the url first */
1223 if ((res = gst_rtsp_url_parse (uristr, &uri)) != GST_RTSP_OK) {
1224 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
1228 /* sanitize the uri */
1231 /* get the session if there is any */
1232 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1233 if (res == GST_RTSP_OK) {
1234 if (client->session_pool == NULL)
1237 /* we had a session in the request, find it again */
1238 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1239 goto session_not_found;
1241 /* we add the session to the client list of watched sessions. When a session
1242 * disappears because it times out, we will be notified. If all sessions are
1243 * gone, we will close the connection */
1244 client_watch_session (client, session);
1248 /* now see what is asked and dispatch to a dedicated handler */
1250 case GST_RTSP_OPTIONS:
1251 handle_options_request (client, uri, session, request);
1253 case GST_RTSP_DESCRIBE:
1254 handle_describe_request (client, uri, session, request);
1256 case GST_RTSP_SETUP:
1257 handle_setup_request (client, uri, session, request);
1260 handle_play_request (client, uri, session, request);
1262 case GST_RTSP_PAUSE:
1263 handle_pause_request (client, uri, session, request);
1265 case GST_RTSP_TEARDOWN:
1266 handle_teardown_request (client, uri, session, request);
1268 case GST_RTSP_SET_PARAMETER:
1269 handle_set_param_request (client, uri, session, request);
1271 case GST_RTSP_GET_PARAMETER:
1272 handle_get_param_request (client, uri, session, request);
1274 case GST_RTSP_ANNOUNCE:
1275 case GST_RTSP_RECORD:
1276 case GST_RTSP_REDIRECT:
1277 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, request);
1279 case GST_RTSP_INVALID:
1281 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
1285 g_object_unref (session);
1287 gst_rtsp_url_free (uri);
1293 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
1298 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
1304 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1314 /* find the stream for this message */
1315 res = gst_rtsp_message_parse_data (message, &channel);
1316 if (res != GST_RTSP_OK)
1319 gst_rtsp_message_steal_body (message, &data, &size);
1321 buffer = gst_buffer_new ();
1322 GST_BUFFER_DATA (buffer) = data;
1323 GST_BUFFER_MALLOCDATA (buffer) = data;
1324 GST_BUFFER_SIZE (buffer) = size;
1327 for (walk = client->streams; walk; walk = g_list_next (walk)) {
1328 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
1329 GstRTSPMediaStream *mstream;
1330 GstRTSPTransport *tr;
1332 /* get the transport, if there is no transport configured, skip this stream */
1333 if (!(tr = stream->trans.transport))
1336 /* we also need a media stream */
1337 if (!(mstream = stream->media_stream))
1340 /* check for TCP transport */
1341 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1342 /* dispatch to the stream based on the channel number */
1343 if (tr->interleaved.min == channel) {
1344 gst_rtsp_media_stream_rtp (mstream, buffer);
1347 } else if (tr->interleaved.max == channel) {
1348 gst_rtsp_media_stream_rtcp (mstream, buffer);
1355 gst_buffer_unref (buffer);
1359 * gst_rtsp_client_set_session_pool:
1360 * @client: a #GstRTSPClient
1361 * @pool: a #GstRTSPSessionPool
1363 * Set @pool as the sessionpool for @client which it will use to find
1364 * or allocate sessions. the sessionpool is usually inherited from the server
1365 * that created the client but can be overridden later.
1368 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1369 GstRTSPSessionPool * pool)
1371 GstRTSPSessionPool *old;
1373 old = client->session_pool;
1376 g_object_ref (pool);
1377 client->session_pool = pool;
1379 g_object_unref (old);
1384 * gst_rtsp_client_get_session_pool:
1385 * @client: a #GstRTSPClient
1387 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1389 * Returns: a #GstRTSPSessionPool, unref after usage.
1391 GstRTSPSessionPool *
1392 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1394 GstRTSPSessionPool *result;
1396 if ((result = client->session_pool))
1397 g_object_ref (result);
1403 * gst_rtsp_client_set_media_mapping:
1404 * @client: a #GstRTSPClient
1405 * @mapping: a #GstRTSPMediaMapping
1407 * Set @mapping as the media mapping for @client which it will use to map urls
1408 * to media streams. These mapping is usually inherited from the server that
1409 * created the client but can be overriden later.
1412 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1413 GstRTSPMediaMapping * mapping)
1415 GstRTSPMediaMapping *old;
1417 old = client->media_mapping;
1419 if (old != mapping) {
1421 g_object_ref (mapping);
1422 client->media_mapping = mapping;
1424 g_object_unref (old);
1429 * gst_rtsp_client_get_media_mapping:
1430 * @client: a #GstRTSPClient
1432 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1434 * Returns: a #GstRTSPMediaMapping, unref after usage.
1436 GstRTSPMediaMapping *
1437 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1439 GstRTSPMediaMapping *result;
1441 if ((result = client->media_mapping))
1442 g_object_ref (result);
1447 static GstRTSPResult
1448 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1451 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1453 switch (message->type) {
1454 case GST_RTSP_MESSAGE_REQUEST:
1455 handle_request (client, message);
1457 case GST_RTSP_MESSAGE_RESPONSE:
1459 case GST_RTSP_MESSAGE_DATA:
1460 handle_data (client, message);
1468 static GstRTSPResult
1469 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1471 GstRTSPClient *client;
1473 client = GST_RTSP_CLIENT (user_data);
1475 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1480 static GstRTSPResult
1481 closed (GstRTSPWatch * watch, gpointer user_data)
1483 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1484 const gchar *tunnelid;
1486 GST_INFO ("client %p: connection closed", client);
1488 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1489 g_mutex_lock (tunnels_lock);
1490 /* remove from tunnelids */
1491 g_hash_table_remove (tunnels, tunnelid);
1492 g_mutex_unlock (tunnels_lock);
1498 static GstRTSPResult
1499 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1501 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1504 str = gst_rtsp_strresult (result);
1505 GST_INFO ("client %p: received an error %s", client, str);
1511 static GstRTSPResult
1512 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1513 GstRTSPMessage * message, guint id, gpointer user_data)
1515 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1518 str = gst_rtsp_strresult (result);
1520 ("client %p: received an error %s when handling message %p with id %d",
1521 client, str, message, id);
1528 remember_tunnel (GstRTSPClient * client)
1530 const gchar *tunnelid;
1532 /* store client in the pending tunnels */
1533 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1534 if (tunnelid == NULL)
1537 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1539 /* we can't have two clients connecting with the same tunnelid */
1540 g_mutex_lock (tunnels_lock);
1541 if (g_hash_table_lookup (tunnels, tunnelid))
1542 goto tunnel_existed;
1544 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1545 g_mutex_unlock (tunnels_lock);
1552 GST_ERROR ("client %p: no tunnelid provided", client);
1557 g_mutex_unlock (tunnels_lock);
1558 GST_ERROR ("client %p: tunnel session %s already existed", client,
1564 static GstRTSPStatusCode
1565 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1567 GstRTSPClient *client;
1569 client = GST_RTSP_CLIENT (user_data);
1571 GST_INFO ("client %p: tunnel start (connection %p)", client,
1572 client->connection);
1574 if (!remember_tunnel (client))
1577 return GST_RTSP_STS_OK;
1582 GST_ERROR ("client %p: error starting tunnel", client);
1583 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1587 static GstRTSPResult
1588 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1590 GstRTSPClient *client;
1592 client = GST_RTSP_CLIENT (user_data);
1594 GST_INFO ("client %p: tunnel lost (connection %p)", client,
1595 client->connection);
1597 /* ignore error, it'll only be a problem when the client does a POST again */
1598 remember_tunnel (client);
1603 static GstRTSPResult
1604 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1606 const gchar *tunnelid;
1607 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1608 GstRTSPClient *oclient;
1610 GST_INFO ("client %p: tunnel complete", client);
1612 /* find previous tunnel */
1613 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1614 if (tunnelid == NULL)
1617 g_mutex_lock (tunnels_lock);
1618 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1621 /* remove the old client from the table. ref before because removing it will
1622 * remove the ref to it. */
1623 g_object_ref (oclient);
1624 g_hash_table_remove (tunnels, tunnelid);
1626 if (oclient->watch == NULL)
1628 g_mutex_unlock (tunnels_lock);
1630 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1631 oclient->connection, client->connection);
1633 /* merge the tunnels into the first client */
1634 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1635 gst_rtsp_watch_reset (oclient->watch);
1636 g_object_unref (oclient);
1638 /* we don't need this watch anymore */
1639 g_source_destroy ((GSource *) client->watch);
1640 client->watchid = 0;
1641 client->watch = NULL;
1648 GST_INFO ("client %p: no tunnelid provided", client);
1649 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1653 g_mutex_unlock (tunnels_lock);
1654 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
1655 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1659 g_mutex_unlock (tunnels_lock);
1660 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
1661 g_object_unref (oclient);
1662 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1666 static GstRTSPWatchFuncs watch_funcs = {
1678 client_watch_notify (GstRTSPClient * client)
1680 GST_INFO ("client %p: watch destroyed", client);
1681 client->watchid = 0;
1682 client->watch = NULL;
1683 g_object_unref (client);
1687 * gst_rtsp_client_attach:
1688 * @client: a #GstRTSPClient
1689 * @channel: a #GIOChannel
1691 * Accept a new connection for @client on the socket in @channel.
1693 * This function should be called when the client properties and urls are fully
1694 * configured and the client is ready to start.
1696 * Returns: %TRUE if the client could be accepted.
1699 gst_rtsp_client_accept (GstRTSPClient * client, GIOChannel * channel)
1702 GstRTSPConnection *conn;
1705 GMainContext *context;
1707 struct sockaddr_storage addr;
1709 gchar ip[INET6_ADDRSTRLEN];
1711 /* a new client connected. */
1712 sock = g_io_channel_unix_get_fd (channel);
1714 GST_RTSP_CHECK (gst_rtsp_connection_accept (sock, &conn), accept_failed);
1716 fd = gst_rtsp_connection_get_readfd (conn);
1718 addrlen = sizeof (addr);
1719 if (getsockname (fd, (struct sockaddr *) &addr, &addrlen) < 0)
1720 goto getpeername_failed;
1722 client->is_ipv6 = addr.ss_family == AF_INET6;
1724 addrlen = sizeof (addr);
1725 if (getnameinfo ((struct sockaddr *) &addr, addrlen, ip, sizeof (ip), NULL, 0,
1726 NI_NUMERICHOST) != 0)
1727 goto getnameinfo_failed;
1729 /* keep the original ip that the client connected to */
1730 g_free (client->server_ip);
1731 client->server_ip = g_strndup (ip, sizeof (ip));
1733 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
1734 client->server_ip, client->is_ipv6);
1736 url = gst_rtsp_connection_get_url (conn);
1737 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
1739 client->connection = conn;
1741 /* create watch for the connection and attach */
1742 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
1743 g_object_ref (client), (GDestroyNotify) client_watch_notify);
1745 /* find the context to add the watch */
1746 if ((source = g_main_current_source ()))
1747 context = g_source_get_context (source);
1751 GST_INFO ("attaching to context %p", context);
1753 client->watchid = gst_rtsp_watch_attach (client->watch, context);
1754 gst_rtsp_watch_unref (client->watch);
1761 gchar *str = gst_rtsp_strresult (res);
1763 GST_ERROR ("Could not accept client on server socket %d: %s", sock, str);
1769 GST_ERROR ("getpeername failed: %s", g_strerror (errno));
1774 GST_ERROR ("getnameinfo failed: %s", g_strerror (errno));