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);
52 static void unlink_streams (GstRTSPClient * client);
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)
91 /* A client is finalized when the connection is broken */
93 gst_rtsp_client_finalize (GObject * obj)
95 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
98 GST_INFO ("finalize client %p", client);
100 /* remove weak-ref from sessions */
101 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
102 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
103 g_object_weak_unref (G_OBJECT (msession),
104 (GWeakNotify) client_session_finalized, client);
107 unlink_streams (client);
109 g_list_free (client->sessions);
111 gst_rtsp_connection_free (client->connection);
112 if (client->session_pool)
113 g_object_unref (client->session_pool);
114 if (client->media_mapping)
115 g_object_unref (client->media_mapping);
118 gst_rtsp_url_free (client->uri);
120 g_object_unref (client->media);
122 g_free (client->server_ip);
124 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
128 gst_rtsp_client_get_property (GObject * object, guint propid,
129 GValue * value, GParamSpec * pspec)
131 GstRTSPClient *client = GST_RTSP_CLIENT (object);
134 case PROP_SESSION_POOL:
135 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
137 case PROP_MEDIA_MAPPING:
138 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
146 gst_rtsp_client_set_property (GObject * object, guint propid,
147 const GValue * value, GParamSpec * pspec)
149 GstRTSPClient *client = GST_RTSP_CLIENT (object);
152 case PROP_SESSION_POOL:
153 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
155 case PROP_MEDIA_MAPPING:
156 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
164 * gst_rtsp_client_new:
166 * Create a new #GstRTSPClient instance.
169 gst_rtsp_client_new (void)
171 GstRTSPClient *result;
173 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
179 send_response (GstRTSPClient * client, GstRTSPSession * session,
180 GstRTSPMessage * response)
182 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
183 "GStreamer RTSP server");
185 /* remove any previous header */
186 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
188 /* add the new session header for new session ids */
192 if (session->timeout != 60)
194 g_strdup_printf ("%s; timeout=%d", session->sessionid,
197 str = g_strdup (session->sessionid);
199 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
202 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
203 gst_rtsp_message_dump (response);
206 gst_rtsp_watch_send_message (client->watch, response, NULL);
207 gst_rtsp_message_unset (response);
211 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
212 GstRTSPMessage * request)
214 GstRTSPMessage response = { 0 };
216 gst_rtsp_message_init_response (&response, code,
217 gst_rtsp_status_as_text (code), request);
219 send_response (client, NULL, &response);
223 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
225 if (uri1 == NULL || uri2 == NULL)
228 if (strcmp (uri1->abspath, uri2->abspath))
234 /* this function is called to initially find the media for the DESCRIBE request
235 * but is cached for when the same client (without breaking the connection) is
236 * doing a setup for the exact same url. */
237 static GstRTSPMedia *
238 find_media (GstRTSPClient * client, GstRTSPUrl * uri, GstRTSPMessage * request)
240 GstRTSPMediaFactory *factory;
243 if (!compare_uri (client->uri, uri)) {
244 /* remove any previously cached values before we try to construct a new
247 gst_rtsp_url_free (client->uri);
250 g_object_unref (client->media);
251 client->media = NULL;
253 if (!client->media_mapping)
256 /* find the factory for the uri first */
258 gst_rtsp_media_mapping_find_factory (client->media_mapping, uri)))
261 /* prepare the media and add it to the pipeline */
262 if (!(media = gst_rtsp_media_factory_construct (factory, uri)))
265 /* set ipv6 on the media before preparing */
266 media->is_ipv6 = client->is_ipv6;
268 /* prepare the media */
269 if (!(gst_rtsp_media_prepare (media)))
272 /* now keep track of the uri and the media */
273 client->uri = gst_rtsp_url_copy (uri);
274 client->media = media;
276 /* we have seen this uri before, used cached media */
277 media = client->media;
278 GST_INFO ("reusing cached media %p", media);
282 g_object_ref (media);
289 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
294 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
299 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
300 g_object_unref (factory);
305 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
306 g_object_unref (media);
307 g_object_unref (factory);
313 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
315 GstRTSPMessage message = { 0 };
319 gst_rtsp_message_init_data (&message, channel);
321 data = GST_BUFFER_DATA (buffer);
322 size = GST_BUFFER_SIZE (buffer);
323 gst_rtsp_message_take_body (&message, data, size);
325 /* FIXME, client->watch could have been finalized here, we need to keep an
326 * extra refcount to the watch. */
327 gst_rtsp_watch_send_message (client->watch, &message, NULL);
329 gst_rtsp_message_steal_body (&message, &data, &size);
330 gst_rtsp_message_unset (&message);
336 link_stream (GstRTSPClient * client, GstRTSPSessionStream * stream)
338 GST_DEBUG ("client %p: linking stream %p", client, stream);
339 gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data,
340 (GstRTSPSendFunc) do_send_data, client, NULL);
341 client->streams = g_list_prepend (client->streams, stream);
345 unlink_stream (GstRTSPClient * client, GstRTSPSessionStream * stream)
347 GST_DEBUG ("client %p: unlinking stream %p", client, stream);
348 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL);
349 client->streams = g_list_remove (client->streams, stream);
353 unlink_streams (GstRTSPClient * client)
357 GST_DEBUG ("client %p: unlinking streams", client);
358 for (walk = client->streams; walk; walk = g_list_next (walk)) {
359 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
361 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL);
363 g_list_free (client->streams);
364 client->streams = NULL;
368 unlink_session_streams (GstRTSPClient * client, GstRTSPSessionMedia * media)
372 n_streams = gst_rtsp_media_n_streams (media->media);
373 for (i = 0; i < n_streams; i++) {
374 GstRTSPSessionStream *sstream;
375 GstRTSPTransport *tr;
377 /* get the stream as configured in the session */
378 sstream = gst_rtsp_session_media_get_stream (media, i);
379 /* get the transport, if there is no transport configured, skip this stream */
380 if (!(tr = sstream->trans.transport))
383 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
384 /* for TCP, unlink the stream from the TCP connection of the client */
385 unlink_stream (client, sstream);
391 handle_teardown_request (GstRTSPClient * client, GstRTSPUrl * uri,
392 GstRTSPSession * session, GstRTSPMessage * request)
394 GstRTSPSessionMedia *media;
395 GstRTSPMessage response = { 0 };
396 GstRTSPStatusCode code;
401 /* get a handle to the configuration of the media in the session */
402 media = gst_rtsp_session_get_media (session, uri);
406 /* unlink the all TCP callbacks */
407 unlink_session_streams (client, media);
409 /* remove the session from the watched sessions */
410 g_object_weak_unref (G_OBJECT (session),
411 (GWeakNotify) client_session_finalized, client);
412 client->sessions = g_list_remove (client->sessions, session);
414 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
416 /* unmanage the media in the session, returns false if all media session
418 if (!gst_rtsp_session_release_media (session, media)) {
419 /* remove the session */
420 gst_rtsp_session_pool_remove (client->session_pool, session);
422 /* construct the response now */
423 code = GST_RTSP_STS_OK;
424 gst_rtsp_message_init_response (&response, code,
425 gst_rtsp_status_as_text (code), request);
427 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONNECTION, "close");
429 send_response (client, session, &response);
431 GST_DEBUG ("client %p: closing connection", client);
432 if (client->watchid) {
433 g_source_destroy ((GSource *) client->watch);
436 gst_rtsp_connection_close (client->connection);
443 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
448 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
454 handle_get_param_request (GstRTSPClient * client, GstRTSPUrl * uri,
455 GstRTSPSession * session, GstRTSPMessage * request)
461 res = gst_rtsp_message_get_body (request, &data, &size);
462 if (res != GST_RTSP_OK)
466 /* no body, keep-alive request */
467 send_generic_response (client, GST_RTSP_STS_OK, request);
469 /* there is a body */
470 GstRTSPMessage response = { 0 };
472 /* there is a body, handle the params */
473 res = gst_rtsp_params_get (client, uri, session, request, &response);
474 if (res != GST_RTSP_OK)
477 send_response (client, session, &response);
484 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
490 handle_set_param_request (GstRTSPClient * client, GstRTSPUrl * uri,
491 GstRTSPSession * session, GstRTSPMessage * request)
497 res = gst_rtsp_message_get_body (request, &data, &size);
498 if (res != GST_RTSP_OK)
502 /* no body, keep-alive request */
503 send_generic_response (client, GST_RTSP_STS_OK, request);
505 GstRTSPMessage response = { 0 };
507 /* there is a body, handle the params */
508 res = gst_rtsp_params_set (client, uri, session, request, &response);
509 if (res != GST_RTSP_OK)
512 send_response (client, session, &response);
519 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
525 handle_pause_request (GstRTSPClient * client, GstRTSPUrl * uri,
526 GstRTSPSession * session, GstRTSPMessage * request)
528 GstRTSPSessionMedia *media;
529 GstRTSPMessage response = { 0 };
530 GstRTSPStatusCode code;
535 /* get a handle to the configuration of the media in the session */
536 media = gst_rtsp_session_get_media (session, uri);
540 /* the session state must be playing or recording */
541 if (media->state != GST_RTSP_STATE_PLAYING &&
542 media->state != GST_RTSP_STATE_RECORDING)
545 /* unlink the all TCP callbacks */
546 unlink_session_streams (client, media);
548 /* then pause sending */
549 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
551 /* construct the response now */
552 code = GST_RTSP_STS_OK;
553 gst_rtsp_message_init_response (&response, code,
554 gst_rtsp_status_as_text (code), request);
556 send_response (client, session, &response);
558 /* the state is now READY */
559 media->state = GST_RTSP_STATE_READY;
566 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
571 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
576 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
583 handle_play_request (GstRTSPClient * client, GstRTSPUrl * uri,
584 GstRTSPSession * session, GstRTSPMessage * request)
586 GstRTSPSessionMedia *media;
587 GstRTSPMessage response = { 0 };
588 GstRTSPStatusCode code;
590 guint n_streams, i, infocount;
591 guint timestamp, seqnum;
593 GstRTSPTimeRange *range;
599 /* get a handle to the configuration of the media in the session */
600 media = gst_rtsp_session_get_media (session, uri);
604 /* the session state must be playing or ready */
605 if (media->state != GST_RTSP_STATE_PLAYING &&
606 media->state != GST_RTSP_STATE_READY)
609 /* parse the range header if we have one */
610 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_RANGE, &str, 0);
611 if (res == GST_RTSP_OK) {
612 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
613 /* we have a range, seek to the position */
614 gst_rtsp_media_seek (media->media, range);
615 gst_rtsp_range_free (range);
619 /* grab RTPInfo from the payloaders now */
620 rtpinfo = g_string_new ("");
622 n_streams = gst_rtsp_media_n_streams (media->media);
623 for (i = 0, infocount = 0; i < n_streams; i++) {
624 GstRTSPSessionStream *sstream;
625 GstRTSPMediaStream *stream;
626 GstRTSPTransport *tr;
627 GObjectClass *payobjclass;
630 /* get the stream as configured in the session */
631 sstream = gst_rtsp_session_media_get_stream (media, i);
632 /* get the transport, if there is no transport configured, skip this stream */
633 if (!(tr = sstream->trans.transport)) {
634 GST_INFO ("stream %d is not configured", i);
638 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
639 /* for TCP, link the stream to the TCP connection of the client */
640 link_stream (client, sstream);
643 stream = sstream->media_stream;
645 payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
647 if (g_object_class_find_property (payobjclass, "seqnum") &&
648 g_object_class_find_property (payobjclass, "timestamp")) {
651 payobj = G_OBJECT (stream->payloader);
653 /* only add RTP-Info for streams with seqnum and timestamp */
654 g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL);
657 g_string_append (rtpinfo, ", ");
659 uristr = gst_rtsp_url_get_request_uri (uri);
660 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
661 uristr, i, seqnum, timestamp);
666 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
670 /* construct the response now */
671 code = GST_RTSP_STS_OK;
672 gst_rtsp_message_init_response (&response, code,
673 gst_rtsp_status_as_text (code), request);
675 /* add the RTP-Info header */
677 str = g_string_free (rtpinfo, FALSE);
678 gst_rtsp_message_take_header (&response, GST_RTSP_HDR_RTP_INFO, str);
680 g_string_free (rtpinfo, TRUE);
684 str = gst_rtsp_range_to_string (&media->media->range);
685 gst_rtsp_message_take_header (&response, GST_RTSP_HDR_RANGE, str);
687 send_response (client, session, &response);
689 /* start playing after sending the request */
690 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
692 media->state = GST_RTSP_STATE_PLAYING;
699 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
704 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
709 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
716 do_keepalive (GstRTSPSession * session)
718 GST_INFO ("keep session %p alive", session);
719 gst_rtsp_session_touch (session);
723 handle_setup_request (GstRTSPClient * client, GstRTSPUrl * uri,
724 GstRTSPSession * session, GstRTSPMessage * request)
729 gboolean have_transport;
730 GstRTSPTransport *ct, *st;
732 GstRTSPLowerTrans supported;
733 GstRTSPMessage response = { 0 };
734 GstRTSPStatusCode code;
735 GstRTSPSessionStream *stream;
736 gchar *trans_str, *pos;
738 GstRTSPSessionMedia *media;
739 gboolean need_session;
742 /* the uri contains the stream number we added in the SDP config, which is
743 * always /stream=%d so we need to strip that off
744 * parse the stream we need to configure, look for the stream in the abspath
745 * first and then in the query. */
746 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
747 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
751 /* we can mofify the parse uri in place */
754 pos += strlen ("/stream=");
755 if (sscanf (pos, "%u", &streamid) != 1)
758 /* parse the transport */
760 gst_rtsp_message_get_header (request, GST_RTSP_HDR_TRANSPORT, &transport,
762 if (res != GST_RTSP_OK)
765 transports = g_strsplit (transport, ",", 0);
766 gst_rtsp_transport_new (&ct);
768 /* init transports */
769 have_transport = FALSE;
770 gst_rtsp_transport_init (ct);
772 /* our supported transports */
773 supported = GST_RTSP_LOWER_TRANS_UDP |
774 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
776 /* loop through the transports, try to parse */
777 for (i = 0; transports[i]; i++) {
778 res = gst_rtsp_transport_parse (transports[i], ct);
779 if (res != GST_RTSP_OK) {
780 /* no valid transport, search some more */
781 GST_WARNING ("could not parse transport %s", transports[i]);
785 /* we have a transport, see if it's RTP/AVP */
786 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
787 GST_WARNING ("invalid transport %s", transports[i]);
791 if (!(ct->lower_transport & supported)) {
792 GST_WARNING ("unsupported transport %s", transports[i]);
796 /* we have a valid transport */
797 GST_INFO ("found valid transport %s", transports[i]);
798 have_transport = TRUE;
802 gst_rtsp_transport_init (ct);
804 g_strfreev (transports);
806 /* we have not found anything usable, error out */
808 goto unsupported_transports;
810 if (client->session_pool == NULL)
813 /* we have a valid transport now, set the destination of the client. */
814 g_free (ct->destination);
815 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
816 ct->destination = g_strdup (MCAST_ADDRESS);
818 url = gst_rtsp_connection_get_url (client->connection);
819 ct->destination = g_strdup (url->host);
823 g_object_ref (session);
824 /* get a handle to the configuration of the media in the session, this can
825 * return NULL if this is a new url to manage in this session. */
826 media = gst_rtsp_session_get_media (session, uri);
828 need_session = FALSE;
830 /* create a session if this fails we probably reached our session limit or
832 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
833 goto service_unavailable;
835 /* we need a new media configuration in this session */
841 /* we have no media, find one and manage it */
845 /* get a handle to the configuration of the media in the session */
846 if ((m = find_media (client, uri, request))) {
847 /* manage the media in our session now */
848 media = gst_rtsp_session_manage_media (session, uri, m);
852 /* if we stil have no media, error */
856 /* fix the transports */
857 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
858 /* check if the client selected channels for TCP */
859 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
860 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
864 /* get a handle to the stream in the media */
865 if (!(stream = gst_rtsp_session_media_get_stream (media, streamid)))
868 st = gst_rtsp_session_stream_set_transport (stream, ct);
870 /* configure keepalive for this transport */
871 gst_rtsp_session_stream_set_keepalive (stream,
872 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
874 /* serialize the server transport */
875 trans_str = gst_rtsp_transport_as_text (st);
876 gst_rtsp_transport_free (st);
878 /* construct the response now */
879 code = GST_RTSP_STS_OK;
880 gst_rtsp_message_init_response (&response, code,
881 gst_rtsp_status_as_text (code), request);
883 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str);
886 send_response (client, session, &response);
888 /* update the state */
889 switch (media->state) {
890 case GST_RTSP_STATE_PLAYING:
891 case GST_RTSP_STATE_RECORDING:
892 case GST_RTSP_STATE_READY:
893 /* no state change */
896 media->state = GST_RTSP_STATE_READY;
899 g_object_unref (session);
906 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
911 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
912 g_object_unref (session);
917 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
918 g_object_unref (media);
919 g_object_unref (session);
924 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
927 unsupported_transports:
929 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
930 gst_rtsp_transport_free (ct);
935 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
940 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
945 static GstSDPMessage *
946 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
952 gst_sdp_message_new (&sdp);
954 /* some standard things first */
955 gst_sdp_message_set_version (sdp, "0");
962 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
965 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
966 gst_sdp_message_set_information (sdp, "rtsp-server");
967 gst_sdp_message_add_time (sdp, "0", "0", NULL);
968 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
969 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
970 gst_sdp_message_add_attribute (sdp, "control", "*");
972 info.server_proto = proto;
973 if (media->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
974 info.server_ip = MCAST_ADDRESS;
976 info.server_ip = client->server_ip;
978 /* create an SDP for the media object */
979 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
987 gst_sdp_message_free (sdp);
992 /* for the describe we must generate an SDP */
994 handle_describe_request (GstRTSPClient * client, GstRTSPUrl * uri,
995 GstRTSPSession * session, GstRTSPMessage * request)
997 GstRTSPMessage response = { 0 };
1001 gchar *str, *content_base;
1002 GstRTSPMedia *media;
1004 /* check what kind of format is accepted, we don't really do anything with it
1005 * and always return SDP for now. */
1010 gst_rtsp_message_get_header (request, GST_RTSP_HDR_ACCEPT, &accept, i);
1011 if (res == GST_RTSP_ENOTIMPL)
1014 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1018 /* find the media object for the uri */
1019 if (!(media = find_media (client, uri, request)))
1022 /* create an SDP for the media object on this client */
1023 if (!(sdp = create_sdp (client, media)))
1026 g_object_unref (media);
1028 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
1029 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
1031 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONTENT_TYPE,
1034 /* content base for some clients that might screw up creating the setup uri */
1035 str = gst_rtsp_url_get_request_uri (uri);
1036 str_len = strlen (str);
1038 /* check for trailing '/' and append one */
1039 if (str[str_len - 1] != '/') {
1040 content_base = g_malloc (str_len + 2);
1041 memcpy (content_base, str, str_len);
1042 content_base[str_len] = '/';
1043 content_base[str_len + 1] = '\0';
1049 GST_INFO ("adding content-base: %s", content_base);
1051 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_CONTENT_BASE,
1053 g_free (content_base);
1055 /* add SDP to the response body */
1056 str = gst_sdp_message_as_text (sdp);
1057 gst_rtsp_message_take_body (&response, (guint8 *) str, strlen (str));
1058 gst_sdp_message_free (sdp);
1060 send_response (client, session, &response);
1067 /* error reply is already sent */
1072 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
1073 g_object_unref (media);
1079 handle_options_request (GstRTSPClient * client, GstRTSPUrl * uri,
1080 GstRTSPSession * session, GstRTSPMessage * request)
1082 GstRTSPMessage response = { 0 };
1083 GstRTSPMethod options;
1086 options = GST_RTSP_DESCRIBE |
1091 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1093 str = gst_rtsp_options_as_text (options);
1095 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK,
1096 gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
1098 gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str);
1101 send_response (client, session, &response);
1106 /* remove duplicate and trailing '/' */
1108 santize_uri (GstRTSPUrl * uri)
1112 gboolean have_slash, prev_slash;
1114 s = d = uri->abspath;
1115 len = strlen (uri->abspath);
1119 for (i = 0; i < len; i++) {
1120 have_slash = s[i] == '/';
1122 if (!have_slash || !prev_slash)
1124 prev_slash = have_slash;
1126 len = d - uri->abspath;
1127 /* don't remove the first slash if that's the only thing left */
1128 if (len > 1 && *(d - 1) == '/')
1134 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1136 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1137 GST_INFO ("all sessions finalized, close the connection");
1138 g_source_destroy ((GSource *) client->watch);
1139 client->watchid = 0;
1144 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1148 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1149 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1151 /* we already know about this session */
1152 if (msession == session)
1156 GST_INFO ("watching session %p", session);
1158 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1160 client->sessions = g_list_prepend (client->sessions, session);
1164 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1166 GstRTSPMethod method;
1167 const gchar *uristr;
1169 GstRTSPVersion version;
1171 GstRTSPSession *session;
1174 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1175 gst_rtsp_message_dump (request);
1178 GST_INFO ("client %p: received a request", client);
1180 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1182 if (version != GST_RTSP_VERSION_1_0) {
1183 /* we can only handle 1.0 requests */
1184 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1189 /* we always try to parse the url first */
1190 if ((res = gst_rtsp_url_parse (uristr, &uri)) != GST_RTSP_OK) {
1191 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
1195 /* sanitize the uri */
1198 /* get the session if there is any */
1199 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1200 if (res == GST_RTSP_OK) {
1201 if (client->session_pool == NULL)
1204 /* we had a session in the request, find it again */
1205 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1206 goto session_not_found;
1208 /* we add the session to the client list of watched sessions. When a session
1209 * disappears because it times out, we will be notified. If all sessions are
1210 * gone, we will close the connection */
1211 client_watch_session (client, session);
1215 /* now see what is asked and dispatch to a dedicated handler */
1217 case GST_RTSP_OPTIONS:
1218 handle_options_request (client, uri, session, request);
1220 case GST_RTSP_DESCRIBE:
1221 handle_describe_request (client, uri, session, request);
1223 case GST_RTSP_SETUP:
1224 handle_setup_request (client, uri, session, request);
1227 handle_play_request (client, uri, session, request);
1229 case GST_RTSP_PAUSE:
1230 handle_pause_request (client, uri, session, request);
1232 case GST_RTSP_TEARDOWN:
1233 handle_teardown_request (client, uri, session, request);
1235 case GST_RTSP_SET_PARAMETER:
1236 handle_set_param_request (client, uri, session, request);
1238 case GST_RTSP_GET_PARAMETER:
1239 handle_get_param_request (client, uri, session, request);
1241 case GST_RTSP_ANNOUNCE:
1242 case GST_RTSP_RECORD:
1243 case GST_RTSP_REDIRECT:
1244 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, request);
1246 case GST_RTSP_INVALID:
1248 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
1252 g_object_unref (session);
1254 gst_rtsp_url_free (uri);
1260 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
1265 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
1271 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1281 /* find the stream for this message */
1282 res = gst_rtsp_message_parse_data (message, &channel);
1283 if (res != GST_RTSP_OK)
1286 gst_rtsp_message_steal_body (message, &data, &size);
1288 buffer = gst_buffer_new ();
1289 GST_BUFFER_DATA (buffer) = data;
1290 GST_BUFFER_MALLOCDATA (buffer) = data;
1291 GST_BUFFER_SIZE (buffer) = size;
1294 for (walk = client->streams; walk; walk = g_list_next (walk)) {
1295 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
1296 GstRTSPMediaStream *mstream;
1297 GstRTSPTransport *tr;
1299 /* get the transport, if there is no transport configured, skip this stream */
1300 if (!(tr = stream->trans.transport))
1303 /* we also need a media stream */
1304 if (!(mstream = stream->media_stream))
1307 /* check for TCP transport */
1308 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1309 /* dispatch to the stream based on the channel number */
1310 if (tr->interleaved.min == channel) {
1311 gst_rtsp_media_stream_rtp (mstream, buffer);
1314 } else if (tr->interleaved.max == channel) {
1315 gst_rtsp_media_stream_rtcp (mstream, buffer);
1322 gst_buffer_unref (buffer);
1326 * gst_rtsp_client_set_session_pool:
1327 * @client: a #GstRTSPClient
1328 * @pool: a #GstRTSPSessionPool
1330 * Set @pool as the sessionpool for @client which it will use to find
1331 * or allocate sessions. the sessionpool is usually inherited from the server
1332 * that created the client but can be overridden later.
1335 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1336 GstRTSPSessionPool * pool)
1338 GstRTSPSessionPool *old;
1340 old = client->session_pool;
1343 g_object_ref (pool);
1344 client->session_pool = pool;
1346 g_object_unref (old);
1351 * gst_rtsp_client_get_session_pool:
1352 * @client: a #GstRTSPClient
1354 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1356 * Returns: a #GstRTSPSessionPool, unref after usage.
1358 GstRTSPSessionPool *
1359 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1361 GstRTSPSessionPool *result;
1363 if ((result = client->session_pool))
1364 g_object_ref (result);
1370 * gst_rtsp_client_set_media_mapping:
1371 * @client: a #GstRTSPClient
1372 * @mapping: a #GstRTSPMediaMapping
1374 * Set @mapping as the media mapping for @client which it will use to map urls
1375 * to media streams. These mapping is usually inherited from the server that
1376 * created the client but can be overriden later.
1379 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1380 GstRTSPMediaMapping * mapping)
1382 GstRTSPMediaMapping *old;
1384 old = client->media_mapping;
1386 if (old != mapping) {
1388 g_object_ref (mapping);
1389 client->media_mapping = mapping;
1391 g_object_unref (old);
1396 * gst_rtsp_client_get_media_mapping:
1397 * @client: a #GstRTSPClient
1399 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1401 * Returns: a #GstRTSPMediaMapping, unref after usage.
1403 GstRTSPMediaMapping *
1404 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1406 GstRTSPMediaMapping *result;
1408 if ((result = client->media_mapping))
1409 g_object_ref (result);
1414 static GstRTSPResult
1415 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1418 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1420 switch (message->type) {
1421 case GST_RTSP_MESSAGE_REQUEST:
1422 handle_request (client, message);
1424 case GST_RTSP_MESSAGE_RESPONSE:
1426 case GST_RTSP_MESSAGE_DATA:
1427 handle_data (client, message);
1435 static GstRTSPResult
1436 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1438 GstRTSPClient *client;
1440 client = GST_RTSP_CLIENT (user_data);
1442 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1447 static GstRTSPResult
1448 closed (GstRTSPWatch * watch, gpointer user_data)
1450 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1451 const gchar *tunnelid;
1453 GST_INFO ("client %p: connection closed", client);
1455 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1456 g_mutex_lock (tunnels_lock);
1457 /* remove from tunnelids */
1458 g_hash_table_remove (tunnels, tunnelid);
1459 g_mutex_unlock (tunnels_lock);
1462 /* remove all streams that are streaming over this client connection */
1463 unlink_streams (client);
1468 static GstRTSPResult
1469 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1471 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1474 str = gst_rtsp_strresult (result);
1475 GST_INFO ("client %p: received an error %s", client, str);
1481 static GstRTSPResult
1482 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1483 GstRTSPMessage * message, guint id, gpointer user_data)
1485 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1488 str = gst_rtsp_strresult (result);
1490 ("client %p: received an error %s when handling message %p with id %d",
1491 client, str, message, id);
1498 remember_tunnel (GstRTSPClient * client)
1500 const gchar *tunnelid;
1502 /* store client in the pending tunnels */
1503 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1504 if (tunnelid == NULL)
1507 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1509 /* we can't have two clients connecting with the same tunnelid */
1510 g_mutex_lock (tunnels_lock);
1511 if (g_hash_table_lookup (tunnels, tunnelid))
1512 goto tunnel_existed;
1514 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1515 g_mutex_unlock (tunnels_lock);
1522 GST_ERROR ("client %p: no tunnelid provided", client);
1527 g_mutex_unlock (tunnels_lock);
1528 GST_ERROR ("client %p: tunnel session %s already existed", client, tunnelid);
1533 static GstRTSPStatusCode
1534 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1536 GstRTSPClient *client;
1538 client = GST_RTSP_CLIENT (user_data);
1540 GST_INFO ("client %p: tunnel start (connection %p)", client, client->connection);
1542 if (!remember_tunnel (client))
1545 return GST_RTSP_STS_OK;
1550 GST_ERROR ("client %p: error starting tunnel", client);
1551 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1555 static GstRTSPResult
1556 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1558 GstRTSPClient *client;
1560 client = GST_RTSP_CLIENT (user_data);
1562 GST_INFO ("client %p: tunnel lost (connection %p)", client, client->connection);
1564 /* ignore error, it'll only be a problem when the client does a POST again */
1565 remember_tunnel (client);
1570 static GstRTSPResult
1571 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1573 const gchar *tunnelid;
1574 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1575 GstRTSPClient *oclient;
1577 GST_INFO ("client %p: tunnel complete", client);
1579 /* find previous tunnel */
1580 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1581 if (tunnelid == NULL)
1584 g_mutex_lock (tunnels_lock);
1585 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1588 /* remove the old client from the table. ref before because removing it will
1589 * remove the ref to it. */
1590 g_object_ref (oclient);
1591 g_hash_table_remove (tunnels, tunnelid);
1592 g_mutex_unlock (tunnels_lock);
1594 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1595 oclient->connection, client->connection);
1597 /* merge the tunnels into the first client */
1598 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1599 gst_rtsp_watch_reset (oclient->watch);
1600 g_object_unref (oclient);
1602 /* we don't need this watch anymore */
1603 g_source_destroy ((GSource *) client->watch);
1604 client->watchid = 0;
1611 GST_INFO ("client %p: no tunnelid provided", client);
1612 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1616 g_mutex_unlock (tunnels_lock);
1617 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
1618 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1622 static GstRTSPWatchFuncs watch_funcs = {
1634 client_watch_notify (GstRTSPClient * client)
1636 GST_INFO ("client %p: watch destroyed", client);
1637 client->watchid = 0;
1638 g_object_unref (client);
1642 * gst_rtsp_client_attach:
1643 * @client: a #GstRTSPClient
1644 * @channel: a #GIOChannel
1646 * Accept a new connection for @client on the socket in @channel.
1648 * This function should be called when the client properties and urls are fully
1649 * configured and the client is ready to start.
1651 * Returns: %TRUE if the client could be accepted.
1654 gst_rtsp_client_accept (GstRTSPClient * client, GIOChannel * channel)
1657 GstRTSPConnection *conn;
1660 GMainContext *context;
1662 struct sockaddr_storage addr;
1664 gchar ip[INET6_ADDRSTRLEN];
1666 /* a new client connected. */
1667 sock = g_io_channel_unix_get_fd (channel);
1669 GST_RTSP_CHECK (gst_rtsp_connection_accept (sock, &conn), accept_failed);
1671 fd = gst_rtsp_connection_get_readfd (conn);
1673 addrlen = sizeof (addr);
1674 if (getsockname (fd, (struct sockaddr *) &addr, &addrlen) < 0)
1675 goto getpeername_failed;
1677 client->is_ipv6 = addr.ss_family == AF_INET6;
1679 addrlen = sizeof (addr);
1680 if (getnameinfo ((struct sockaddr *) &addr, addrlen, ip, sizeof (ip), NULL, 0,
1681 NI_NUMERICHOST) != 0)
1682 goto getnameinfo_failed;
1684 /* keep the original ip that the client connected to */
1685 g_free (client->server_ip);
1686 client->server_ip = g_strndup (ip, sizeof (ip));
1688 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
1689 client->server_ip, client->is_ipv6);
1691 url = gst_rtsp_connection_get_url (conn);
1692 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
1694 client->connection = conn;
1696 /* create watch for the connection and attach */
1697 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
1698 g_object_ref (client), (GDestroyNotify) client_watch_notify);
1700 /* find the context to add the watch */
1701 if ((source = g_main_current_source ()))
1702 context = g_source_get_context (source);
1706 GST_INFO ("attaching to context %p", context);
1708 client->watchid = gst_rtsp_watch_attach (client->watch, context);
1709 gst_rtsp_watch_unref (client->watch);
1716 gchar *str = gst_rtsp_strresult (res);
1718 GST_ERROR ("Could not accept client on server socket %d: %s", sock, str);
1724 GST_ERROR ("getpeername failed: %s", g_strerror (errno));
1729 GST_ERROR ("getnameinfo failed: %s", g_strerror (errno));