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.
26 #include <sys/types.h>
27 #include <netinet/in.h>
29 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <sys/ioctl.h>
35 #include "rtsp-client.h"
37 #include "rtsp-params.h"
39 static GMutex *tunnels_lock;
40 static GHashTable *tunnels;
56 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
57 #define GST_CAT_DEFAULT rtsp_client_debug
59 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
61 static void gst_rtsp_client_get_property (GObject * object, guint propid,
62 GValue * value, GParamSpec * pspec);
63 static void gst_rtsp_client_set_property (GObject * object, guint propid,
64 const GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_client_finalize (GObject * obj);
67 static void client_session_finalized (GstRTSPClient * client,
68 GstRTSPSession * session);
69 static void unlink_session_streams (GstRTSPClient * client,
70 GstRTSPSession * session, GstRTSPSessionMedia * media);
72 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
75 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
77 GObjectClass *gobject_class;
79 gobject_class = G_OBJECT_CLASS (klass);
81 gobject_class->get_property = gst_rtsp_client_get_property;
82 gobject_class->set_property = gst_rtsp_client_set_property;
83 gobject_class->finalize = gst_rtsp_client_finalize;
85 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
86 g_param_spec_object ("session-pool", "Session Pool",
87 "The session pool to use for client session",
88 GST_TYPE_RTSP_SESSION_POOL,
89 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
91 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
92 g_param_spec_object ("media-mapping", "Media Mapping",
93 "The media mapping to use for client session",
94 GST_TYPE_RTSP_MEDIA_MAPPING,
95 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97 gst_rtsp_client_signals[SIGNAL_CLOSED] =
98 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
99 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
100 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
103 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
104 tunnels_lock = g_mutex_new ();
106 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
110 gst_rtsp_client_init (GstRTSPClient * client)
115 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
119 /* unlink all media managed in this session */
120 for (medias = session->medias; medias; medias = g_list_next (medias)) {
121 GstRTSPSessionMedia *media = medias->data;
123 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
124 unlink_session_streams (client, session, media);
125 /* unmanage the media in the session. */
126 gst_rtsp_session_release_media (session, media);
131 client_cleanup_sessions (GstRTSPClient * client)
135 /* remove weak-ref from sessions */
136 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
137 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
138 g_object_weak_unref (G_OBJECT (session),
139 (GWeakNotify) client_session_finalized, client);
140 client_unlink_session (client, session);
142 g_list_free (client->sessions);
143 client->sessions = NULL;
146 /* A client is finalized when the connection is broken */
148 gst_rtsp_client_finalize (GObject * obj)
150 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
152 GST_INFO ("finalize client %p", client);
154 client_cleanup_sessions (client);
156 gst_rtsp_connection_free (client->connection);
157 if (client->session_pool)
158 g_object_unref (client->session_pool);
159 if (client->media_mapping)
160 g_object_unref (client->media_mapping);
162 g_object_unref (client->auth);
165 gst_rtsp_url_free (client->uri);
167 g_object_unref (client->media);
169 g_free (client->server_ip);
171 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
175 gst_rtsp_client_get_property (GObject * object, guint propid,
176 GValue * value, GParamSpec * pspec)
178 GstRTSPClient *client = GST_RTSP_CLIENT (object);
181 case PROP_SESSION_POOL:
182 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
184 case PROP_MEDIA_MAPPING:
185 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
193 gst_rtsp_client_set_property (GObject * object, guint propid,
194 const GValue * value, GParamSpec * pspec)
196 GstRTSPClient *client = GST_RTSP_CLIENT (object);
199 case PROP_SESSION_POOL:
200 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
202 case PROP_MEDIA_MAPPING:
203 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
211 * gst_rtsp_client_new:
213 * Create a new #GstRTSPClient instance.
215 * Returns: a new #GstRTSPClient
218 gst_rtsp_client_new (void)
220 GstRTSPClient *result;
222 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
228 send_response (GstRTSPClient * client, GstRTSPSession * session,
229 GstRTSPMessage * response)
231 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
232 "GStreamer RTSP server");
234 /* remove any previous header */
235 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
237 /* add the new session header for new session ids */
241 if (session->timeout != 60)
243 g_strdup_printf ("%s; timeout=%d", session->sessionid,
246 str = g_strdup (session->sessionid);
248 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
251 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
252 gst_rtsp_message_dump (response);
255 gst_rtsp_watch_send_message (client->watch, response, NULL);
256 gst_rtsp_message_unset (response);
260 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
261 GstRTSPClientState * state)
263 gst_rtsp_message_init_response (state->response, code,
264 gst_rtsp_status_as_text (code), state->request);
266 send_response (client, NULL, state->response);
270 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
271 GstRTSPClientState * state)
273 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
274 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
277 /* and let the authentication manager setup the auth tokens */
278 gst_rtsp_auth_setup_auth (auth, client, 0, state);
281 send_response (client, state->session, state->response);
286 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
288 if (uri1 == NULL || uri2 == NULL)
291 if (strcmp (uri1->abspath, uri2->abspath))
297 /* this function is called to initially find the media for the DESCRIBE request
298 * but is cached for when the same client (without breaking the connection) is
299 * doing a setup for the exact same url. */
300 static GstRTSPMedia *
301 find_media (GstRTSPClient * client, GstRTSPClientState * state)
303 GstRTSPMediaFactory *factory;
307 if (!compare_uri (client->uri, state->uri)) {
308 /* remove any previously cached values before we try to construct a new
311 gst_rtsp_url_free (client->uri);
314 g_object_unref (client->media);
315 client->media = NULL;
317 if (!client->media_mapping)
320 /* find the factory for the uri first */
322 gst_rtsp_media_mapping_find_factory (client->media_mapping,
326 state->factory = factory;
328 /* check if we have access to the factory */
329 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
330 if (!gst_rtsp_auth_check (auth, client, 0, state))
333 g_object_unref (auth);
336 /* prepare the media and add it to the pipeline */
337 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
340 g_object_unref (factory);
342 state->factory = NULL;
344 /* set ipv6 on the media before preparing */
345 media->is_ipv6 = client->is_ipv6;
346 state->media = media;
348 /* prepare the media */
349 if (!(gst_rtsp_media_prepare (media)))
352 /* now keep track of the uri and the media */
353 client->uri = gst_rtsp_url_copy (state->uri);
354 client->media = media;
356 /* we have seen this uri before, used cached media */
357 media = client->media;
358 state->media = media;
359 GST_INFO ("reusing cached media %p", media);
363 g_object_ref (media);
370 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
375 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
380 handle_unauthorized_request (client, auth, state);
381 g_object_unref (factory);
382 g_object_unref (auth);
387 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
388 g_object_unref (factory);
393 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
394 g_object_unref (media);
400 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
402 GstRTSPMessage message = { 0 };
407 gst_rtsp_message_init_data (&message, channel);
409 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
411 gst_rtsp_message_take_body (&message, data, usize);
413 /* FIXME, client->watch could have been finalized here, we need to keep an
414 * extra refcount to the watch. */
415 gst_rtsp_watch_send_message (client->watch, &message, NULL);
417 gst_rtsp_message_steal_body (&message, &data, &usize);
418 gst_buffer_unmap (buffer, data, size);
420 gst_rtsp_message_unset (&message);
426 do_send_data_list (GstBufferList * blist, guint8 channel,
427 GstRTSPClient * client)
431 len = gst_buffer_list_len (blist);
433 for (i = 0; i < len; i++) {
434 GstBuffer *group = gst_buffer_list_get (blist, i);
436 do_send_data (group, channel, client);
443 link_stream (GstRTSPClient * client, GstRTSPSession * session,
444 GstRTSPSessionStream * stream)
446 GST_DEBUG ("client %p: linking stream %p", client, stream);
447 gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data,
448 (GstRTSPSendFunc) do_send_data, (GstRTSPSendListFunc) do_send_data_list,
449 (GstRTSPSendListFunc) do_send_data_list, client, NULL);
450 client->streams = g_list_prepend (client->streams, stream);
451 /* make sure our session can't expire */
452 gst_rtsp_session_prevent_expire (session);
456 unlink_stream (GstRTSPClient * client, GstRTSPSession * session,
457 GstRTSPSessionStream * stream)
459 GST_DEBUG ("client %p: unlinking stream %p", client, stream);
460 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL, NULL,
462 client->streams = g_list_remove (client->streams, stream);
463 /* our session can now expire */
464 gst_rtsp_session_allow_expire (session);
468 unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session,
469 GstRTSPSessionMedia * media)
473 n_streams = gst_rtsp_media_n_streams (media->media);
474 for (i = 0; i < n_streams; i++) {
475 GstRTSPSessionStream *sstream;
476 GstRTSPTransport *tr;
478 /* get the stream as configured in the session */
479 sstream = gst_rtsp_session_media_get_stream (media, i);
480 /* get the transport, if there is no transport configured, skip this stream */
481 if (!(tr = sstream->trans.transport))
484 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
485 /* for TCP, unlink the stream from the TCP connection of the client */
486 unlink_stream (client, session, sstream);
492 close_connection (GstRTSPClient * client)
494 const gchar *tunnelid;
496 GST_DEBUG ("client %p: closing connection", client);
498 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
499 g_mutex_lock (tunnels_lock);
500 /* remove from tunnelids */
501 g_hash_table_remove (tunnels, tunnelid);
502 g_mutex_unlock (tunnels_lock);
505 gst_rtsp_connection_close (client->connection);
506 if (client->watchid) {
507 g_source_destroy ((GSource *) client->watch);
509 client->watch = NULL;
514 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
516 GstRTSPSession *session;
517 GstRTSPSessionMedia *media;
518 GstRTSPStatusCode code;
523 session = state->session;
525 /* get a handle to the configuration of the media in the session */
526 media = gst_rtsp_session_get_media (session, state->uri);
530 state->sessmedia = media;
532 /* unlink the all TCP callbacks */
533 unlink_session_streams (client, session, media);
535 /* remove the session from the watched sessions */
536 g_object_weak_unref (G_OBJECT (session),
537 (GWeakNotify) client_session_finalized, client);
538 client->sessions = g_list_remove (client->sessions, session);
540 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
542 /* unmanage the media in the session, returns false if all media session
544 if (!gst_rtsp_session_release_media (session, media)) {
545 /* remove the session */
546 gst_rtsp_session_pool_remove (client->session_pool, session);
548 /* construct the response now */
549 code = GST_RTSP_STS_OK;
550 gst_rtsp_message_init_response (state->response, code,
551 gst_rtsp_status_as_text (code), state->request);
553 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
556 send_response (client, session, state->response);
558 close_connection (client);
565 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
570 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
576 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
582 res = gst_rtsp_message_get_body (state->request, &data, &size);
583 if (res != GST_RTSP_OK)
587 /* no body, keep-alive request */
588 send_generic_response (client, GST_RTSP_STS_OK, state);
590 /* there is a body, handle the params */
591 res = gst_rtsp_params_get (client, state);
592 if (res != GST_RTSP_OK)
595 send_response (client, state->session, state->response);
602 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
608 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
614 res = gst_rtsp_message_get_body (state->request, &data, &size);
615 if (res != GST_RTSP_OK)
619 /* no body, keep-alive request */
620 send_generic_response (client, GST_RTSP_STS_OK, state);
622 /* there is a body, handle the params */
623 res = gst_rtsp_params_set (client, state);
624 if (res != GST_RTSP_OK)
627 send_response (client, state->session, state->response);
634 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
640 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
642 GstRTSPSession *session;
643 GstRTSPSessionMedia *media;
644 GstRTSPStatusCode code;
646 if (!(session = state->session))
649 /* get a handle to the configuration of the media in the session */
650 media = gst_rtsp_session_get_media (session, state->uri);
654 state->sessmedia = media;
656 /* the session state must be playing or recording */
657 if (media->state != GST_RTSP_STATE_PLAYING &&
658 media->state != GST_RTSP_STATE_RECORDING)
661 /* unlink the all TCP callbacks */
662 unlink_session_streams (client, session, media);
664 /* then pause sending */
665 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
667 /* construct the response now */
668 code = GST_RTSP_STS_OK;
669 gst_rtsp_message_init_response (state->response, code,
670 gst_rtsp_status_as_text (code), state->request);
672 send_response (client, session, state->response);
674 /* the state is now READY */
675 media->state = GST_RTSP_STATE_READY;
682 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
687 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
692 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
699 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
701 GstRTSPSession *session;
702 GstRTSPSessionMedia *media;
703 GstRTSPStatusCode code;
705 guint n_streams, i, infocount;
706 guint timestamp, seqnum;
708 GstRTSPTimeRange *range;
711 if (!(session = state->session))
714 /* get a handle to the configuration of the media in the session */
715 media = gst_rtsp_session_get_media (session, state->uri);
719 state->sessmedia = media;
721 /* the session state must be playing or ready */
722 if (media->state != GST_RTSP_STATE_PLAYING &&
723 media->state != GST_RTSP_STATE_READY)
726 /* parse the range header if we have one */
728 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
729 if (res == GST_RTSP_OK) {
730 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
731 /* we have a range, seek to the position */
732 gst_rtsp_media_seek (media->media, range);
733 gst_rtsp_range_free (range);
737 /* grab RTPInfo from the payloaders now */
738 rtpinfo = g_string_new ("");
740 n_streams = gst_rtsp_media_n_streams (media->media);
741 for (i = 0, infocount = 0; i < n_streams; i++) {
742 GstRTSPSessionStream *sstream;
743 GstRTSPMediaStream *stream;
744 GstRTSPTransport *tr;
745 GObjectClass *payobjclass;
748 /* get the stream as configured in the session */
749 sstream = gst_rtsp_session_media_get_stream (media, i);
750 /* get the transport, if there is no transport configured, skip this stream */
751 if (!(tr = sstream->trans.transport)) {
752 GST_INFO ("stream %d is not configured", i);
756 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
757 /* for TCP, link the stream to the TCP connection of the client */
758 link_stream (client, session, sstream);
761 stream = sstream->media_stream;
763 payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
765 if (g_object_class_find_property (payobjclass, "seqnum") &&
766 g_object_class_find_property (payobjclass, "timestamp")) {
769 payobj = G_OBJECT (stream->payloader);
771 /* only add RTP-Info for streams with seqnum and timestamp */
772 g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL);
775 g_string_append (rtpinfo, ", ");
777 uristr = gst_rtsp_url_get_request_uri (state->uri);
778 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
779 uristr, i, seqnum, timestamp);
784 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
788 /* construct the response now */
789 code = GST_RTSP_STS_OK;
790 gst_rtsp_message_init_response (state->response, code,
791 gst_rtsp_status_as_text (code), state->request);
793 /* add the RTP-Info header */
795 str = g_string_free (rtpinfo, FALSE);
796 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
798 g_string_free (rtpinfo, TRUE);
802 str = gst_rtsp_media_get_range_string (media->media, TRUE);
803 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
805 send_response (client, session, state->response);
807 /* start playing after sending the request */
808 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
810 media->state = GST_RTSP_STATE_PLAYING;
817 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
822 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
827 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
834 do_keepalive (GstRTSPSession * session)
836 GST_INFO ("keep session %p alive", session);
837 gst_rtsp_session_touch (session);
841 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
847 gboolean have_transport;
848 GstRTSPTransport *ct, *st;
850 GstRTSPLowerTrans supported;
851 GstRTSPStatusCode code;
852 GstRTSPSession *session;
853 GstRTSPSessionStream *stream;
854 gchar *trans_str, *pos;
856 GstRTSPSessionMedia *media;
860 /* the uri contains the stream number we added in the SDP config, which is
861 * always /stream=%d so we need to strip that off
862 * parse the stream we need to configure, look for the stream in the abspath
863 * first and then in the query. */
864 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
865 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
869 /* we can mofify the parse uri in place */
872 pos += strlen ("/stream=");
873 if (sscanf (pos, "%u", &streamid) != 1)
876 /* parse the transport */
878 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
880 if (res != GST_RTSP_OK)
883 transports = g_strsplit (transport, ",", 0);
884 gst_rtsp_transport_new (&ct);
886 /* init transports */
887 have_transport = FALSE;
888 gst_rtsp_transport_init (ct);
890 /* our supported transports */
891 supported = GST_RTSP_LOWER_TRANS_UDP |
892 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
894 /* loop through the transports, try to parse */
895 for (i = 0; transports[i]; i++) {
896 res = gst_rtsp_transport_parse (transports[i], ct);
897 if (res != GST_RTSP_OK) {
898 /* no valid transport, search some more */
899 GST_WARNING ("could not parse transport %s", transports[i]);
903 /* we have a transport, see if it's RTP/AVP */
904 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
905 GST_WARNING ("invalid transport %s", transports[i]);
909 if (!(ct->lower_transport & supported)) {
910 GST_WARNING ("unsupported transport %s", transports[i]);
914 /* we have a valid transport */
915 GST_INFO ("found valid transport %s", transports[i]);
916 have_transport = TRUE;
920 gst_rtsp_transport_init (ct);
922 g_strfreev (transports);
924 /* we have not found anything usable, error out */
926 goto unsupported_transports;
928 if (client->session_pool == NULL)
931 session = state->session;
934 g_object_ref (session);
935 /* get a handle to the configuration of the media in the session, this can
936 * return NULL if this is a new url to manage in this session. */
937 media = gst_rtsp_session_get_media (session, uri);
939 /* create a session if this fails we probably reached our session limit or
941 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
942 goto service_unavailable;
944 state->session = session;
946 /* we need a new media configuration in this session */
950 /* we have no media, find one and manage it */
954 /* get a handle to the configuration of the media in the session */
955 if ((m = find_media (client, state))) {
956 /* manage the media in our session now */
957 media = gst_rtsp_session_manage_media (session, uri, m);
961 /* if we stil have no media, error */
965 state->sessmedia = media;
967 /* we have a valid transport now, set the destination of the client. */
968 g_free (ct->destination);
969 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
970 ct->destination = gst_rtsp_media_get_multicast_group (media->media);
974 url = gst_rtsp_connection_get_url (client->connection);
975 ct->destination = g_strdup (url->host);
977 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
978 /* check if the client selected channels for TCP */
979 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
980 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
985 /* get a handle to the stream in the media */
986 if (!(stream = gst_rtsp_session_media_get_stream (media, streamid)))
989 st = gst_rtsp_session_stream_set_transport (stream, ct);
991 /* configure keepalive for this transport */
992 gst_rtsp_session_stream_set_keepalive (stream,
993 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
995 /* serialize the server transport */
996 trans_str = gst_rtsp_transport_as_text (st);
997 gst_rtsp_transport_free (st);
999 /* construct the response now */
1000 code = GST_RTSP_STS_OK;
1001 gst_rtsp_message_init_response (state->response, code,
1002 gst_rtsp_status_as_text (code), state->request);
1004 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1008 send_response (client, session, state->response);
1010 /* update the state */
1011 switch (media->state) {
1012 case GST_RTSP_STATE_PLAYING:
1013 case GST_RTSP_STATE_RECORDING:
1014 case GST_RTSP_STATE_READY:
1015 /* no state change */
1018 media->state = GST_RTSP_STATE_READY;
1021 g_object_unref (session);
1028 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1033 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1034 g_object_unref (session);
1039 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1040 g_object_unref (media);
1041 g_object_unref (session);
1046 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1049 unsupported_transports:
1051 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1052 gst_rtsp_transport_free (ct);
1057 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1060 service_unavailable:
1062 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1067 static GstSDPMessage *
1068 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1074 gst_sdp_message_new (&sdp);
1076 /* some standard things first */
1077 gst_sdp_message_set_version (sdp, "0");
1079 if (client->is_ipv6)
1084 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1087 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1088 gst_sdp_message_set_information (sdp, "rtsp-server");
1089 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1090 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1091 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1092 gst_sdp_message_add_attribute (sdp, "control", "*");
1094 info.server_proto = proto;
1095 if (media->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1096 info.server_ip = gst_rtsp_media_get_multicast_group (media);
1098 info.server_ip = g_strdup (client->server_ip);
1100 /* create an SDP for the media object */
1101 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1104 g_free (info.server_ip);
1111 g_free (info.server_ip);
1112 gst_sdp_message_free (sdp);
1117 /* for the describe we must generate an SDP */
1119 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1124 gchar *str, *content_base;
1125 GstRTSPMedia *media;
1127 /* check what kind of format is accepted, we don't really do anything with it
1128 * and always return SDP for now. */
1133 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1135 if (res == GST_RTSP_ENOTIMPL)
1138 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1142 /* find the media object for the uri */
1143 if (!(media = find_media (client, state)))
1146 /* create an SDP for the media object on this client */
1147 if (!(sdp = create_sdp (client, media)))
1150 g_object_unref (media);
1152 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1153 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1155 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1158 /* content base for some clients that might screw up creating the setup uri */
1159 str = gst_rtsp_url_get_request_uri (state->uri);
1160 str_len = strlen (str);
1162 /* check for trailing '/' and append one */
1163 if (str[str_len - 1] != '/') {
1164 content_base = g_malloc (str_len + 2);
1165 memcpy (content_base, str, str_len);
1166 content_base[str_len] = '/';
1167 content_base[str_len + 1] = '\0';
1173 GST_INFO ("adding content-base: %s", content_base);
1175 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1177 g_free (content_base);
1179 /* add SDP to the response body */
1180 str = gst_sdp_message_as_text (sdp);
1181 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1182 gst_sdp_message_free (sdp);
1184 send_response (client, state->session, state->response);
1191 /* error reply is already sent */
1196 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1197 g_object_unref (media);
1203 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1205 GstRTSPMethod options;
1208 options = GST_RTSP_DESCRIBE |
1213 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1215 str = gst_rtsp_options_as_text (options);
1217 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1218 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1220 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1223 send_response (client, state->session, state->response);
1228 /* remove duplicate and trailing '/' */
1230 sanitize_uri (GstRTSPUrl * uri)
1234 gboolean have_slash, prev_slash;
1236 s = d = uri->abspath;
1237 len = strlen (uri->abspath);
1241 for (i = 0; i < len; i++) {
1242 have_slash = s[i] == '/';
1244 if (!have_slash || !prev_slash)
1246 prev_slash = have_slash;
1248 len = d - uri->abspath;
1249 /* don't remove the first slash if that's the only thing left */
1250 if (len > 1 && *(d - 1) == '/')
1256 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1258 GST_INFO ("client %p: session %p finished", client, session);
1260 /* unlink all media managed in this session */
1261 client_unlink_session (client, session);
1263 /* remove the session */
1264 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1265 GST_INFO ("client %p: all sessions finalized, close the connection",
1267 close_connection (client);
1272 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1276 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1277 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1279 /* we already know about this session */
1280 if (msession == session)
1284 GST_INFO ("watching session %p", session);
1286 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1288 client->sessions = g_list_prepend (client->sessions, session);
1292 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1294 GstRTSPMethod method;
1295 const gchar *uristr;
1297 GstRTSPVersion version;
1299 GstRTSPSession *session;
1300 GstRTSPClientState state = { NULL };
1301 GstRTSPMessage response = { 0 };
1304 state.request = request;
1305 state.response = &response;
1307 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1308 gst_rtsp_message_dump (request);
1311 GST_INFO ("client %p: received a request", client);
1313 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1315 if (version != GST_RTSP_VERSION_1_0) {
1316 /* we can only handle 1.0 requests */
1317 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1321 state.method = method;
1323 /* we always try to parse the url first */
1324 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1325 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1329 /* sanitize the uri */
1333 /* get the session if there is any */
1334 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1335 if (res == GST_RTSP_OK) {
1336 if (client->session_pool == NULL)
1339 /* we had a session in the request, find it again */
1340 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1341 goto session_not_found;
1343 /* we add the session to the client list of watched sessions. When a session
1344 * disappears because it times out, we will be notified. If all sessions are
1345 * gone, we will close the connection */
1346 client_watch_session (client, session);
1350 state.session = session;
1353 if (!gst_rtsp_auth_check (client->auth, client, &state))
1354 goto not_authorized;
1357 /* now see what is asked and dispatch to a dedicated handler */
1359 case GST_RTSP_OPTIONS:
1360 handle_options_request (client, &state);
1362 case GST_RTSP_DESCRIBE:
1363 handle_describe_request (client, &state);
1365 case GST_RTSP_SETUP:
1366 handle_setup_request (client, &state);
1369 handle_play_request (client, &state);
1371 case GST_RTSP_PAUSE:
1372 handle_pause_request (client, &state);
1374 case GST_RTSP_TEARDOWN:
1375 handle_teardown_request (client, &state);
1377 case GST_RTSP_SET_PARAMETER:
1378 handle_set_param_request (client, &state);
1380 case GST_RTSP_GET_PARAMETER:
1381 handle_get_param_request (client, &state);
1383 case GST_RTSP_ANNOUNCE:
1384 case GST_RTSP_RECORD:
1385 case GST_RTSP_REDIRECT:
1386 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1388 case GST_RTSP_INVALID:
1390 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1394 g_object_unref (session);
1396 gst_rtsp_url_free (uri);
1402 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1407 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1412 handle_unauthorized_request (client, client->auth, &state);
1418 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1428 /* find the stream for this message */
1429 res = gst_rtsp_message_parse_data (message, &channel);
1430 if (res != GST_RTSP_OK)
1433 gst_rtsp_message_steal_body (message, &data, &size);
1435 buffer = gst_buffer_new ();
1436 gst_buffer_take_memory (buffer, -1,
1437 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
1440 for (walk = client->streams; walk; walk = g_list_next (walk)) {
1441 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
1442 GstRTSPMediaStream *mstream;
1443 GstRTSPTransport *tr;
1445 /* get the transport, if there is no transport configured, skip this stream */
1446 if (!(tr = stream->trans.transport))
1449 /* we also need a media stream */
1450 if (!(mstream = stream->media_stream))
1453 /* check for TCP transport */
1454 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1455 /* dispatch to the stream based on the channel number */
1456 if (tr->interleaved.min == channel) {
1457 gst_rtsp_media_stream_rtp (mstream, buffer);
1460 } else if (tr->interleaved.max == channel) {
1461 gst_rtsp_media_stream_rtcp (mstream, buffer);
1468 gst_buffer_unref (buffer);
1472 * gst_rtsp_client_set_session_pool:
1473 * @client: a #GstRTSPClient
1474 * @pool: a #GstRTSPSessionPool
1476 * Set @pool as the sessionpool for @client which it will use to find
1477 * or allocate sessions. the sessionpool is usually inherited from the server
1478 * that created the client but can be overridden later.
1481 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1482 GstRTSPSessionPool * pool)
1484 GstRTSPSessionPool *old;
1486 old = client->session_pool;
1489 g_object_ref (pool);
1490 client->session_pool = pool;
1492 g_object_unref (old);
1497 * gst_rtsp_client_get_session_pool:
1498 * @client: a #GstRTSPClient
1500 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1502 * Returns: a #GstRTSPSessionPool, unref after usage.
1504 GstRTSPSessionPool *
1505 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1507 GstRTSPSessionPool *result;
1509 if ((result = client->session_pool))
1510 g_object_ref (result);
1516 * gst_rtsp_client_set_server:
1517 * @client: a #GstRTSPClient
1518 * @server: a #GstRTSPServer
1520 * Set @server as the server that created @client.
1523 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1527 old = client->server;
1528 if (old != server) {
1530 g_object_ref (server);
1531 client->server = server;
1533 g_object_unref (old);
1538 * gst_rtsp_client_get_server:
1539 * @client: a #GstRTSPClient
1541 * Get the #GstRTSPServer object that @client was created from.
1543 * Returns: a #GstRTSPServer, unref after usage.
1546 gst_rtsp_client_get_server (GstRTSPClient * client)
1548 GstRTSPServer *result;
1550 if ((result = client->server))
1551 g_object_ref (result);
1557 * gst_rtsp_client_set_media_mapping:
1558 * @client: a #GstRTSPClient
1559 * @mapping: a #GstRTSPMediaMapping
1561 * Set @mapping as the media mapping for @client which it will use to map urls
1562 * to media streams. These mapping is usually inherited from the server that
1563 * created the client but can be overriden later.
1566 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1567 GstRTSPMediaMapping * mapping)
1569 GstRTSPMediaMapping *old;
1571 old = client->media_mapping;
1573 if (old != mapping) {
1575 g_object_ref (mapping);
1576 client->media_mapping = mapping;
1578 g_object_unref (old);
1583 * gst_rtsp_client_get_media_mapping:
1584 * @client: a #GstRTSPClient
1586 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1588 * Returns: a #GstRTSPMediaMapping, unref after usage.
1590 GstRTSPMediaMapping *
1591 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1593 GstRTSPMediaMapping *result;
1595 if ((result = client->media_mapping))
1596 g_object_ref (result);
1602 * gst_rtsp_client_set_auth:
1603 * @client: a #GstRTSPClient
1604 * @auth: a #GstRTSPAuth
1606 * configure @auth to be used as the authentication manager of @client.
1609 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1613 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1619 g_object_ref (auth);
1620 client->auth = auth;
1622 g_object_unref (old);
1628 * gst_rtsp_client_get_auth:
1629 * @client: a #GstRTSPClient
1631 * Get the #GstRTSPAuth used as the authentication manager of @client.
1633 * Returns: the #GstRTSPAuth of @client. g_object_unref() after
1637 gst_rtsp_client_get_auth (GstRTSPClient * client)
1639 GstRTSPAuth *result;
1641 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1643 if ((result = client->auth))
1644 g_object_ref (result);
1649 static GstRTSPResult
1650 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1653 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1655 switch (message->type) {
1656 case GST_RTSP_MESSAGE_REQUEST:
1657 handle_request (client, message);
1659 case GST_RTSP_MESSAGE_RESPONSE:
1661 case GST_RTSP_MESSAGE_DATA:
1662 handle_data (client, message);
1670 static GstRTSPResult
1671 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1673 /* GstRTSPClient *client; */
1675 /* client = GST_RTSP_CLIENT (user_data); */
1677 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1682 static GstRTSPResult
1683 closed (GstRTSPWatch * watch, gpointer user_data)
1685 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1686 const gchar *tunnelid;
1688 GST_INFO ("client %p: connection closed", client);
1690 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1691 g_mutex_lock (tunnels_lock);
1692 /* remove from tunnelids */
1693 g_hash_table_remove (tunnels, tunnelid);
1694 g_mutex_unlock (tunnels_lock);
1700 static GstRTSPResult
1701 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1703 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1706 str = gst_rtsp_strresult (result);
1707 GST_INFO ("client %p: received an error %s", client, str);
1713 static GstRTSPResult
1714 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1715 GstRTSPMessage * message, guint id, gpointer user_data)
1717 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1720 str = gst_rtsp_strresult (result);
1722 ("client %p: received an error %s when handling message %p with id %d",
1723 client, str, message, id);
1730 remember_tunnel (GstRTSPClient * client)
1732 const gchar *tunnelid;
1734 /* store client in the pending tunnels */
1735 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1736 if (tunnelid == NULL)
1739 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1741 /* we can't have two clients connecting with the same tunnelid */
1742 g_mutex_lock (tunnels_lock);
1743 if (g_hash_table_lookup (tunnels, tunnelid))
1744 goto tunnel_existed;
1746 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1747 g_mutex_unlock (tunnels_lock);
1754 GST_ERROR ("client %p: no tunnelid provided", client);
1759 g_mutex_unlock (tunnels_lock);
1760 GST_ERROR ("client %p: tunnel session %s already existed", client,
1766 static GstRTSPStatusCode
1767 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1769 GstRTSPClient *client;
1771 client = GST_RTSP_CLIENT (user_data);
1773 GST_INFO ("client %p: tunnel start (connection %p)", client,
1774 client->connection);
1776 if (!remember_tunnel (client))
1779 return GST_RTSP_STS_OK;
1784 GST_ERROR ("client %p: error starting tunnel", client);
1785 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1789 static GstRTSPResult
1790 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1792 GstRTSPClient *client;
1794 client = GST_RTSP_CLIENT (user_data);
1796 GST_INFO ("client %p: tunnel lost (connection %p)", client,
1797 client->connection);
1799 /* ignore error, it'll only be a problem when the client does a POST again */
1800 remember_tunnel (client);
1805 static GstRTSPResult
1806 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1808 const gchar *tunnelid;
1809 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1810 GstRTSPClient *oclient;
1812 GST_INFO ("client %p: tunnel complete", client);
1814 /* find previous tunnel */
1815 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1816 if (tunnelid == NULL)
1819 g_mutex_lock (tunnels_lock);
1820 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1823 /* remove the old client from the table. ref before because removing it will
1824 * remove the ref to it. */
1825 g_object_ref (oclient);
1826 g_hash_table_remove (tunnels, tunnelid);
1828 if (oclient->watch == NULL)
1830 g_mutex_unlock (tunnels_lock);
1832 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1833 oclient->connection, client->connection);
1835 /* merge the tunnels into the first client */
1836 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1837 gst_rtsp_watch_reset (oclient->watch);
1838 g_object_unref (oclient);
1840 /* we don't need this watch anymore */
1841 g_source_destroy ((GSource *) client->watch);
1842 client->watchid = 0;
1843 client->watch = NULL;
1850 GST_INFO ("client %p: no tunnelid provided", client);
1851 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1855 g_mutex_unlock (tunnels_lock);
1856 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
1857 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1861 g_mutex_unlock (tunnels_lock);
1862 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
1863 g_object_unref (oclient);
1864 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1868 static GstRTSPWatchFuncs watch_funcs = {
1880 client_watch_notify (GstRTSPClient * client)
1882 GST_INFO ("client %p: watch destroyed", client);
1883 client->watchid = 0;
1884 client->watch = NULL;
1885 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
1886 g_object_unref (client);
1890 * gst_rtsp_client_attach:
1891 * @client: a #GstRTSPClient
1892 * @channel: a #GIOChannel
1894 * Accept a new connection for @client on the socket in @channel.
1896 * This function should be called when the client properties and urls are fully
1897 * configured and the client is ready to start.
1899 * Returns: %TRUE if the client could be accepted.
1902 gst_rtsp_client_accept (GstRTSPClient * client, GIOChannel * channel)
1905 GstRTSPConnection *conn;
1908 GMainContext *context;
1910 struct sockaddr_storage addr;
1912 gchar ip[INET6_ADDRSTRLEN];
1914 /* a new client connected. */
1915 sock = g_io_channel_unix_get_fd (channel);
1917 GST_RTSP_CHECK (gst_rtsp_connection_accept (sock, &conn), accept_failed);
1919 fd = gst_rtsp_connection_get_readfd (conn);
1921 addrlen = sizeof (addr);
1922 if (getsockname (fd, (struct sockaddr *) &addr, &addrlen) < 0)
1923 goto getpeername_failed;
1925 client->is_ipv6 = addr.ss_family == AF_INET6;
1927 if (getnameinfo ((struct sockaddr *) &addr, addrlen, ip, sizeof (ip), NULL, 0,
1928 NI_NUMERICHOST) != 0)
1929 goto getnameinfo_failed;
1931 /* keep the original ip that the client connected to */
1932 g_free (client->server_ip);
1933 client->server_ip = g_strndup (ip, sizeof (ip));
1935 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
1936 client->server_ip, client->is_ipv6);
1938 url = gst_rtsp_connection_get_url (conn);
1939 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
1941 client->connection = conn;
1943 /* create watch for the connection and attach */
1944 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
1945 g_object_ref (client), (GDestroyNotify) client_watch_notify);
1947 /* find the context to add the watch */
1948 if ((source = g_main_current_source ()))
1949 context = g_source_get_context (source);
1953 GST_INFO ("attaching to context %p", context);
1955 client->watchid = gst_rtsp_watch_attach (client->watch, context);
1956 gst_rtsp_watch_unref (client->watch);
1963 gchar *str = gst_rtsp_strresult (res);
1965 GST_ERROR ("Could not accept client on server socket %d: %s", sock, str);
1971 GST_ERROR ("getpeername failed: %s", g_strerror (errno));
1976 GST_ERROR ("getnameinfo failed: %s", g_strerror (errno));