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 /* temporary multicast address until it's configurable somewhere */
40 #define MCAST_ADDRESS "224.2.0.1"
42 static GMutex *tunnels_lock;
43 static GHashTable *tunnels;
59 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
60 #define GST_CAT_DEFAULT rtsp_client_debug
62 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
64 static void gst_rtsp_client_get_property (GObject * object, guint propid,
65 GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_client_set_property (GObject * object, guint propid,
67 const GValue * value, GParamSpec * pspec);
68 static void gst_rtsp_client_finalize (GObject * obj);
70 static void client_session_finalized (GstRTSPClient * client,
71 GstRTSPSession * session);
72 static void unlink_session_streams (GstRTSPClient * client,
73 GstRTSPSession * session, GstRTSPSessionMedia * media);
75 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
78 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
80 GObjectClass *gobject_class;
82 gobject_class = G_OBJECT_CLASS (klass);
84 gobject_class->get_property = gst_rtsp_client_get_property;
85 gobject_class->set_property = gst_rtsp_client_set_property;
86 gobject_class->finalize = gst_rtsp_client_finalize;
88 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
89 g_param_spec_object ("session-pool", "Session Pool",
90 "The session pool to use for client session",
91 GST_TYPE_RTSP_SESSION_POOL,
92 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
94 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
95 g_param_spec_object ("media-mapping", "Media Mapping",
96 "The media mapping to use for client session",
97 GST_TYPE_RTSP_MEDIA_MAPPING,
98 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
100 gst_rtsp_client_signals[SIGNAL_CLOSED] =
101 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
102 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
103 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
106 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
107 tunnels_lock = g_mutex_new ();
109 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
113 gst_rtsp_client_init (GstRTSPClient * client)
118 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
122 /* unlink all media managed in this session */
123 for (medias = session->medias; medias; medias = g_list_next (medias)) {
124 unlink_session_streams (client, session,
125 (GstRTSPSessionMedia *) medias->data);
130 client_cleanup_sessions (GstRTSPClient * client)
134 /* remove weak-ref from sessions */
135 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
136 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
137 g_object_weak_unref (G_OBJECT (session),
138 (GWeakNotify) client_session_finalized, client);
139 client_unlink_session (client, session);
141 g_list_free (client->sessions);
142 client->sessions = NULL;
145 /* A client is finalized when the connection is broken */
147 gst_rtsp_client_finalize (GObject * obj)
149 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
151 GST_INFO ("finalize client %p", client);
153 client_cleanup_sessions (client);
155 gst_rtsp_connection_free (client->connection);
156 if (client->session_pool)
157 g_object_unref (client->session_pool);
158 if (client->media_mapping)
159 g_object_unref (client->media_mapping);
161 g_object_unref (client->auth);
164 gst_rtsp_url_free (client->uri);
166 g_object_unref (client->media);
168 g_free (client->server_ip);
170 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
174 gst_rtsp_client_get_property (GObject * object, guint propid,
175 GValue * value, GParamSpec * pspec)
177 GstRTSPClient *client = GST_RTSP_CLIENT (object);
180 case PROP_SESSION_POOL:
181 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
183 case PROP_MEDIA_MAPPING:
184 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
192 gst_rtsp_client_set_property (GObject * object, guint propid,
193 const GValue * value, GParamSpec * pspec)
195 GstRTSPClient *client = GST_RTSP_CLIENT (object);
198 case PROP_SESSION_POOL:
199 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
201 case PROP_MEDIA_MAPPING:
202 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
210 * gst_rtsp_client_new:
212 * Create a new #GstRTSPClient instance.
214 * Returns: a new #GstRTSPClient
217 gst_rtsp_client_new (void)
219 GstRTSPClient *result;
221 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
227 send_response (GstRTSPClient * client, GstRTSPSession * session,
228 GstRTSPMessage * response)
230 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
231 "GStreamer RTSP server");
233 /* remove any previous header */
234 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
236 /* add the new session header for new session ids */
240 if (session->timeout != 60)
242 g_strdup_printf ("%s; timeout=%d", session->sessionid,
245 str = g_strdup (session->sessionid);
247 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
250 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
251 gst_rtsp_message_dump (response);
254 gst_rtsp_watch_send_message (client->watch, response, NULL);
255 gst_rtsp_message_unset (response);
259 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
260 GstRTSPClientState * state)
262 gst_rtsp_message_init_response (state->response, code,
263 gst_rtsp_status_as_text (code), state->request);
265 send_response (client, NULL, state->response);
269 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
270 GstRTSPClientState * state)
272 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
273 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
276 /* and let the authentication manager setup the auth tokens */
277 gst_rtsp_auth_setup_auth (auth, client, 0, state);
280 send_response (client, state->session, state->response);
285 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
287 if (uri1 == NULL || uri2 == NULL)
290 if (strcmp (uri1->abspath, uri2->abspath))
296 /* this function is called to initially find the media for the DESCRIBE request
297 * but is cached for when the same client (without breaking the connection) is
298 * doing a setup for the exact same url. */
299 static GstRTSPMedia *
300 find_media (GstRTSPClient * client, GstRTSPClientState * state)
302 GstRTSPMediaFactory *factory;
306 if (!compare_uri (client->uri, state->uri)) {
307 /* remove any previously cached values before we try to construct a new
310 gst_rtsp_url_free (client->uri);
313 g_object_unref (client->media);
314 client->media = NULL;
316 if (!client->media_mapping)
319 /* find the factory for the uri first */
321 gst_rtsp_media_mapping_find_factory (client->media_mapping,
325 state->factory = factory;
327 /* check if we have access to the factory */
328 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
329 if (!gst_rtsp_auth_check (auth, client, 0, state))
332 g_object_unref (auth);
335 /* prepare the media and add it to the pipeline */
336 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
339 g_object_unref (factory);
341 /* set ipv6 on the media before preparing */
342 media->is_ipv6 = client->is_ipv6;
343 state->media = media;
345 /* prepare the media */
346 if (!(gst_rtsp_media_prepare (media)))
349 /* now keep track of the uri and the media */
350 client->uri = gst_rtsp_url_copy (state->uri);
351 client->media = media;
353 /* we have seen this uri before, used cached media */
354 media = client->media;
355 state->media = media;
356 GST_INFO ("reusing cached media %p", media);
360 g_object_ref (media);
367 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
372 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
377 handle_unauthorized_request (client, auth, state);
378 g_object_unref (factory);
379 g_object_unref (auth);
384 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
385 g_object_unref (factory);
390 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
391 g_object_unref (media);
392 g_object_unref (factory);
398 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
400 GstRTSPMessage message = { 0 };
405 gst_rtsp_message_init_data (&message, channel);
407 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
409 gst_rtsp_message_take_body (&message, data, usize);
411 /* FIXME, client->watch could have been finalized here, we need to keep an
412 * extra refcount to the watch. */
413 gst_rtsp_watch_send_message (client->watch, &message, NULL);
415 gst_rtsp_message_steal_body (&message, &data, &usize);
416 gst_buffer_unmap (buffer, data, size);
418 gst_rtsp_message_unset (&message);
424 do_send_data_list (GstBufferList * blist, guint8 channel,
425 GstRTSPClient * client)
429 len = gst_buffer_list_len (blist);
431 for (i = 0; i < len; i++) {
432 GstBuffer *group = gst_buffer_list_get (blist, i);
434 do_send_data (group, channel, client);
441 link_stream (GstRTSPClient * client, GstRTSPSession * session,
442 GstRTSPSessionStream * stream)
444 GST_DEBUG ("client %p: linking stream %p", client, stream);
445 gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data,
446 (GstRTSPSendFunc) do_send_data, (GstRTSPSendListFunc) do_send_data_list,
447 (GstRTSPSendListFunc) do_send_data_list, client, NULL);
448 client->streams = g_list_prepend (client->streams, stream);
449 /* make sure our session can't expire */
450 gst_rtsp_session_prevent_expire (session);
454 unlink_stream (GstRTSPClient * client, GstRTSPSession * session,
455 GstRTSPSessionStream * stream)
457 GST_DEBUG ("client %p: unlinking stream %p", client, stream);
458 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL, NULL,
460 client->streams = g_list_remove (client->streams, stream);
461 /* our session can now expire */
462 gst_rtsp_session_allow_expire (session);
466 unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session,
467 GstRTSPSessionMedia * media)
471 n_streams = gst_rtsp_media_n_streams (media->media);
472 for (i = 0; i < n_streams; i++) {
473 GstRTSPSessionStream *sstream;
474 GstRTSPTransport *tr;
476 /* get the stream as configured in the session */
477 sstream = gst_rtsp_session_media_get_stream (media, i);
478 /* get the transport, if there is no transport configured, skip this stream */
479 if (!(tr = sstream->trans.transport))
482 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
483 /* for TCP, unlink the stream from the TCP connection of the client */
484 unlink_stream (client, session, sstream);
490 close_connection (GstRTSPClient * client)
492 const gchar *tunnelid;
494 GST_DEBUG ("client %p: closing connection", client);
496 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
497 g_mutex_lock (tunnels_lock);
498 /* remove from tunnelids */
499 g_hash_table_remove (tunnels, tunnelid);
500 g_mutex_unlock (tunnels_lock);
503 gst_rtsp_connection_close (client->connection);
504 if (client->watchid) {
505 g_source_destroy ((GSource *) client->watch);
507 client->watch = NULL;
512 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
514 GstRTSPSession *session;
515 GstRTSPSessionMedia *media;
516 GstRTSPStatusCode code;
521 session = state->session;
523 /* get a handle to the configuration of the media in the session */
524 media = gst_rtsp_session_get_media (session, state->uri);
528 state->sessmedia = media;
530 /* unlink the all TCP callbacks */
531 unlink_session_streams (client, session, media);
533 /* remove the session from the watched sessions */
534 g_object_weak_unref (G_OBJECT (session),
535 (GWeakNotify) client_session_finalized, client);
536 client->sessions = g_list_remove (client->sessions, session);
538 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
540 /* unmanage the media in the session, returns false if all media session
542 if (!gst_rtsp_session_release_media (session, media)) {
543 /* remove the session */
544 gst_rtsp_session_pool_remove (client->session_pool, session);
546 /* construct the response now */
547 code = GST_RTSP_STS_OK;
548 gst_rtsp_message_init_response (state->response, code,
549 gst_rtsp_status_as_text (code), state->request);
551 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
554 send_response (client, session, state->response);
556 close_connection (client);
563 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
568 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
574 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
580 res = gst_rtsp_message_get_body (state->request, &data, &size);
581 if (res != GST_RTSP_OK)
585 /* no body, keep-alive request */
586 send_generic_response (client, GST_RTSP_STS_OK, state);
588 /* there is a body, handle the params */
589 res = gst_rtsp_params_get (client, state);
590 if (res != GST_RTSP_OK)
593 send_response (client, state->session, state->response);
600 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
606 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
612 res = gst_rtsp_message_get_body (state->request, &data, &size);
613 if (res != GST_RTSP_OK)
617 /* no body, keep-alive request */
618 send_generic_response (client, GST_RTSP_STS_OK, state);
620 /* there is a body, handle the params */
621 res = gst_rtsp_params_set (client, state);
622 if (res != GST_RTSP_OK)
625 send_response (client, state->session, state->response);
632 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
638 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
640 GstRTSPSession *session;
641 GstRTSPSessionMedia *media;
642 GstRTSPStatusCode code;
644 if (!(session = state->session))
647 /* get a handle to the configuration of the media in the session */
648 media = gst_rtsp_session_get_media (session, state->uri);
652 state->sessmedia = media;
654 /* the session state must be playing or recording */
655 if (media->state != GST_RTSP_STATE_PLAYING &&
656 media->state != GST_RTSP_STATE_RECORDING)
659 /* unlink the all TCP callbacks */
660 unlink_session_streams (client, session, media);
662 /* then pause sending */
663 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
665 /* construct the response now */
666 code = GST_RTSP_STS_OK;
667 gst_rtsp_message_init_response (state->response, code,
668 gst_rtsp_status_as_text (code), state->request);
670 send_response (client, session, state->response);
672 /* the state is now READY */
673 media->state = GST_RTSP_STATE_READY;
680 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
685 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
690 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
697 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
699 GstRTSPSession *session;
700 GstRTSPSessionMedia *media;
701 GstRTSPStatusCode code;
703 guint n_streams, i, infocount;
704 guint timestamp, seqnum;
706 GstRTSPTimeRange *range;
709 if (!(session = state->session))
712 /* get a handle to the configuration of the media in the session */
713 media = gst_rtsp_session_get_media (session, state->uri);
717 state->sessmedia = media;
719 /* the session state must be playing or ready */
720 if (media->state != GST_RTSP_STATE_PLAYING &&
721 media->state != GST_RTSP_STATE_READY)
724 /* parse the range header if we have one */
726 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
727 if (res == GST_RTSP_OK) {
728 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
729 /* we have a range, seek to the position */
730 gst_rtsp_media_seek (media->media, range);
731 gst_rtsp_range_free (range);
735 /* grab RTPInfo from the payloaders now */
736 rtpinfo = g_string_new ("");
738 n_streams = gst_rtsp_media_n_streams (media->media);
739 for (i = 0, infocount = 0; i < n_streams; i++) {
740 GstRTSPSessionStream *sstream;
741 GstRTSPMediaStream *stream;
742 GstRTSPTransport *tr;
743 GObjectClass *payobjclass;
746 /* get the stream as configured in the session */
747 sstream = gst_rtsp_session_media_get_stream (media, i);
748 /* get the transport, if there is no transport configured, skip this stream */
749 if (!(tr = sstream->trans.transport)) {
750 GST_INFO ("stream %d is not configured", i);
754 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
755 /* for TCP, link the stream to the TCP connection of the client */
756 link_stream (client, session, sstream);
759 stream = sstream->media_stream;
761 payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
763 if (g_object_class_find_property (payobjclass, "seqnum") &&
764 g_object_class_find_property (payobjclass, "timestamp")) {
767 payobj = G_OBJECT (stream->payloader);
769 /* only add RTP-Info for streams with seqnum and timestamp */
770 g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL);
773 g_string_append (rtpinfo, ", ");
775 uristr = gst_rtsp_url_get_request_uri (state->uri);
776 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
777 uristr, i, seqnum, timestamp);
782 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
786 /* construct the response now */
787 code = GST_RTSP_STS_OK;
788 gst_rtsp_message_init_response (state->response, code,
789 gst_rtsp_status_as_text (code), state->request);
791 /* add the RTP-Info header */
793 str = g_string_free (rtpinfo, FALSE);
794 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
796 g_string_free (rtpinfo, TRUE);
800 str = gst_rtsp_media_get_range_string (media->media, TRUE);
801 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
803 send_response (client, session, state->response);
805 /* start playing after sending the request */
806 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
808 media->state = GST_RTSP_STATE_PLAYING;
815 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
820 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
825 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
832 do_keepalive (GstRTSPSession * session)
834 GST_INFO ("keep session %p alive", session);
835 gst_rtsp_session_touch (session);
839 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
845 gboolean have_transport;
846 GstRTSPTransport *ct, *st;
848 GstRTSPLowerTrans supported;
849 GstRTSPStatusCode code;
850 GstRTSPSession *session;
851 GstRTSPSessionStream *stream;
852 gchar *trans_str, *pos;
854 GstRTSPSessionMedia *media;
859 /* the uri contains the stream number we added in the SDP config, which is
860 * always /stream=%d so we need to strip that off
861 * parse the stream we need to configure, look for the stream in the abspath
862 * first and then in the query. */
863 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
864 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
868 /* we can mofify the parse uri in place */
871 pos += strlen ("/stream=");
872 if (sscanf (pos, "%u", &streamid) != 1)
875 /* parse the transport */
877 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
879 if (res != GST_RTSP_OK)
882 transports = g_strsplit (transport, ",", 0);
883 gst_rtsp_transport_new (&ct);
885 /* init transports */
886 have_transport = FALSE;
887 gst_rtsp_transport_init (ct);
889 /* our supported transports */
890 supported = GST_RTSP_LOWER_TRANS_UDP |
891 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
893 /* loop through the transports, try to parse */
894 for (i = 0; transports[i]; i++) {
895 res = gst_rtsp_transport_parse (transports[i], ct);
896 if (res != GST_RTSP_OK) {
897 /* no valid transport, search some more */
898 GST_WARNING ("could not parse transport %s", transports[i]);
902 /* we have a transport, see if it's RTP/AVP */
903 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
904 GST_WARNING ("invalid transport %s", transports[i]);
908 if (!(ct->lower_transport & supported)) {
909 GST_WARNING ("unsupported transport %s", transports[i]);
913 /* we have a valid transport */
914 GST_INFO ("found valid transport %s", transports[i]);
915 have_transport = TRUE;
919 gst_rtsp_transport_init (ct);
921 g_strfreev (transports);
923 /* we have not found anything usable, error out */
925 goto unsupported_transports;
927 if (client->session_pool == NULL)
930 /* we have a valid transport now, set the destination of the client. */
931 g_free (ct->destination);
932 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
933 ct->destination = g_strdup (MCAST_ADDRESS);
935 url = gst_rtsp_connection_get_url (client->connection);
936 ct->destination = g_strdup (url->host);
939 session = state->session;
942 g_object_ref (session);
943 /* get a handle to the configuration of the media in the session, this can
944 * return NULL if this is a new url to manage in this session. */
945 media = gst_rtsp_session_get_media (session, uri);
947 /* create a session if this fails we probably reached our session limit or
949 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
950 goto service_unavailable;
952 state->session = session;
954 /* we need a new media configuration in this session */
958 /* we have no media, find one and manage it */
962 /* get a handle to the configuration of the media in the session */
963 if ((m = find_media (client, state))) {
964 /* manage the media in our session now */
965 media = gst_rtsp_session_manage_media (session, uri, m);
969 /* if we stil have no media, error */
973 state->sessmedia = media;
975 /* fix the transports */
976 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
977 /* check if the client selected channels for TCP */
978 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
979 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
983 /* get a handle to the stream in the media */
984 if (!(stream = gst_rtsp_session_media_get_stream (media, streamid)))
987 st = gst_rtsp_session_stream_set_transport (stream, ct);
989 /* configure keepalive for this transport */
990 gst_rtsp_session_stream_set_keepalive (stream,
991 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
993 /* serialize the server transport */
994 trans_str = gst_rtsp_transport_as_text (st);
995 gst_rtsp_transport_free (st);
997 /* construct the response now */
998 code = GST_RTSP_STS_OK;
999 gst_rtsp_message_init_response (state->response, code,
1000 gst_rtsp_status_as_text (code), state->request);
1002 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1006 send_response (client, session, state->response);
1008 /* update the state */
1009 switch (media->state) {
1010 case GST_RTSP_STATE_PLAYING:
1011 case GST_RTSP_STATE_RECORDING:
1012 case GST_RTSP_STATE_READY:
1013 /* no state change */
1016 media->state = GST_RTSP_STATE_READY;
1019 g_object_unref (session);
1026 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1031 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1032 g_object_unref (session);
1037 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1038 g_object_unref (media);
1039 g_object_unref (session);
1044 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1047 unsupported_transports:
1049 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1050 gst_rtsp_transport_free (ct);
1055 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1058 service_unavailable:
1060 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1065 static GstSDPMessage *
1066 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1072 gst_sdp_message_new (&sdp);
1074 /* some standard things first */
1075 gst_sdp_message_set_version (sdp, "0");
1077 if (client->is_ipv6)
1082 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1085 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1086 gst_sdp_message_set_information (sdp, "rtsp-server");
1087 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1088 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1089 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1090 gst_sdp_message_add_attribute (sdp, "control", "*");
1092 info.server_proto = proto;
1093 if (media->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1094 info.server_ip = MCAST_ADDRESS;
1096 info.server_ip = client->server_ip;
1098 /* create an SDP for the media object */
1099 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1107 gst_sdp_message_free (sdp);
1112 /* for the describe we must generate an SDP */
1114 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1119 gchar *str, *content_base;
1120 GstRTSPMedia *media;
1122 /* check what kind of format is accepted, we don't really do anything with it
1123 * and always return SDP for now. */
1128 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1130 if (res == GST_RTSP_ENOTIMPL)
1133 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1137 /* find the media object for the uri */
1138 if (!(media = find_media (client, state)))
1141 /* create an SDP for the media object on this client */
1142 if (!(sdp = create_sdp (client, media)))
1145 g_object_unref (media);
1147 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1148 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1150 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1153 /* content base for some clients that might screw up creating the setup uri */
1154 str = gst_rtsp_url_get_request_uri (state->uri);
1155 str_len = strlen (str);
1157 /* check for trailing '/' and append one */
1158 if (str[str_len - 1] != '/') {
1159 content_base = g_malloc (str_len + 2);
1160 memcpy (content_base, str, str_len);
1161 content_base[str_len] = '/';
1162 content_base[str_len + 1] = '\0';
1168 GST_INFO ("adding content-base: %s", content_base);
1170 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1172 g_free (content_base);
1174 /* add SDP to the response body */
1175 str = gst_sdp_message_as_text (sdp);
1176 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1177 gst_sdp_message_free (sdp);
1179 send_response (client, state->session, state->response);
1186 /* error reply is already sent */
1191 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1192 g_object_unref (media);
1198 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1200 GstRTSPMethod options;
1203 options = GST_RTSP_DESCRIBE |
1208 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1210 str = gst_rtsp_options_as_text (options);
1212 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1213 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1215 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1218 send_response (client, state->session, state->response);
1223 /* remove duplicate and trailing '/' */
1225 sanitize_uri (GstRTSPUrl * uri)
1229 gboolean have_slash, prev_slash;
1231 s = d = uri->abspath;
1232 len = strlen (uri->abspath);
1236 for (i = 0; i < len; i++) {
1237 have_slash = s[i] == '/';
1239 if (!have_slash || !prev_slash)
1241 prev_slash = have_slash;
1243 len = d - uri->abspath;
1244 /* don't remove the first slash if that's the only thing left */
1245 if (len > 1 && *(d - 1) == '/')
1251 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1253 GST_INFO ("client %p: session %p finished", client, session);
1255 /* unlink all media managed in this session */
1256 client_unlink_session (client, session);
1258 /* remove the session */
1259 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1260 GST_INFO ("client %p: all sessions finalized, close the connection",
1262 close_connection (client);
1267 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1271 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1272 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1274 /* we already know about this session */
1275 if (msession == session)
1279 GST_INFO ("watching session %p", session);
1281 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1283 client->sessions = g_list_prepend (client->sessions, session);
1287 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1289 GstRTSPMethod method;
1290 const gchar *uristr;
1292 GstRTSPVersion version;
1294 GstRTSPSession *session;
1295 GstRTSPClientState state = { NULL };
1296 GstRTSPMessage response = { 0 };
1299 state.request = request;
1300 state.response = &response;
1302 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1303 gst_rtsp_message_dump (request);
1306 GST_INFO ("client %p: received a request", client);
1308 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1310 if (version != GST_RTSP_VERSION_1_0) {
1311 /* we can only handle 1.0 requests */
1312 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1316 state.method = method;
1318 /* we always try to parse the url first */
1319 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1320 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1324 /* sanitize the uri */
1328 /* get the session if there is any */
1329 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1330 if (res == GST_RTSP_OK) {
1331 if (client->session_pool == NULL)
1334 /* we had a session in the request, find it again */
1335 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1336 goto session_not_found;
1338 /* we add the session to the client list of watched sessions. When a session
1339 * disappears because it times out, we will be notified. If all sessions are
1340 * gone, we will close the connection */
1341 client_watch_session (client, session);
1345 state.session = session;
1348 if (!gst_rtsp_auth_check (client->auth, client, &state))
1349 goto not_authorized;
1352 /* now see what is asked and dispatch to a dedicated handler */
1354 case GST_RTSP_OPTIONS:
1355 handle_options_request (client, &state);
1357 case GST_RTSP_DESCRIBE:
1358 handle_describe_request (client, &state);
1360 case GST_RTSP_SETUP:
1361 handle_setup_request (client, &state);
1364 handle_play_request (client, &state);
1366 case GST_RTSP_PAUSE:
1367 handle_pause_request (client, &state);
1369 case GST_RTSP_TEARDOWN:
1370 handle_teardown_request (client, &state);
1372 case GST_RTSP_SET_PARAMETER:
1373 handle_set_param_request (client, &state);
1375 case GST_RTSP_GET_PARAMETER:
1376 handle_get_param_request (client, &state);
1378 case GST_RTSP_ANNOUNCE:
1379 case GST_RTSP_RECORD:
1380 case GST_RTSP_REDIRECT:
1381 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1383 case GST_RTSP_INVALID:
1385 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1389 g_object_unref (session);
1391 gst_rtsp_url_free (uri);
1397 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1402 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1407 handle_unauthorized_request (client, client->auth, &state);
1413 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1423 /* find the stream for this message */
1424 res = gst_rtsp_message_parse_data (message, &channel);
1425 if (res != GST_RTSP_OK)
1428 gst_rtsp_message_steal_body (message, &data, &size);
1430 buffer = gst_buffer_new ();
1431 gst_buffer_take_memory (buffer, -1,
1432 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
1435 for (walk = client->streams; walk; walk = g_list_next (walk)) {
1436 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
1437 GstRTSPMediaStream *mstream;
1438 GstRTSPTransport *tr;
1440 /* get the transport, if there is no transport configured, skip this stream */
1441 if (!(tr = stream->trans.transport))
1444 /* we also need a media stream */
1445 if (!(mstream = stream->media_stream))
1448 /* check for TCP transport */
1449 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1450 /* dispatch to the stream based on the channel number */
1451 if (tr->interleaved.min == channel) {
1452 gst_rtsp_media_stream_rtp (mstream, buffer);
1455 } else if (tr->interleaved.max == channel) {
1456 gst_rtsp_media_stream_rtcp (mstream, buffer);
1463 gst_buffer_unref (buffer);
1467 * gst_rtsp_client_set_session_pool:
1468 * @client: a #GstRTSPClient
1469 * @pool: a #GstRTSPSessionPool
1471 * Set @pool as the sessionpool for @client which it will use to find
1472 * or allocate sessions. the sessionpool is usually inherited from the server
1473 * that created the client but can be overridden later.
1476 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1477 GstRTSPSessionPool * pool)
1479 GstRTSPSessionPool *old;
1481 old = client->session_pool;
1484 g_object_ref (pool);
1485 client->session_pool = pool;
1487 g_object_unref (old);
1492 * gst_rtsp_client_get_session_pool:
1493 * @client: a #GstRTSPClient
1495 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1497 * Returns: a #GstRTSPSessionPool, unref after usage.
1499 GstRTSPSessionPool *
1500 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1502 GstRTSPSessionPool *result;
1504 if ((result = client->session_pool))
1505 g_object_ref (result);
1511 * gst_rtsp_client_set_server:
1512 * @client: a #GstRTSPClient
1513 * @server: a #GstRTSPServer
1515 * Set @server as the server that created @client.
1518 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1522 old = client->server;
1523 if (old != server) {
1525 g_object_ref (server);
1526 client->server = server;
1528 g_object_unref (old);
1533 * gst_rtsp_client_get_server:
1534 * @client: a #GstRTSPClient
1536 * Get the #GstRTSPServer object that @client was created from.
1538 * Returns: a #GstRTSPServer, unref after usage.
1541 gst_rtsp_client_get_server (GstRTSPClient * client)
1543 GstRTSPServer *result;
1545 if ((result = client->server))
1546 g_object_ref (result);
1552 * gst_rtsp_client_set_media_mapping:
1553 * @client: a #GstRTSPClient
1554 * @mapping: a #GstRTSPMediaMapping
1556 * Set @mapping as the media mapping for @client which it will use to map urls
1557 * to media streams. These mapping is usually inherited from the server that
1558 * created the client but can be overriden later.
1561 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1562 GstRTSPMediaMapping * mapping)
1564 GstRTSPMediaMapping *old;
1566 old = client->media_mapping;
1568 if (old != mapping) {
1570 g_object_ref (mapping);
1571 client->media_mapping = mapping;
1573 g_object_unref (old);
1578 * gst_rtsp_client_get_media_mapping:
1579 * @client: a #GstRTSPClient
1581 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1583 * Returns: a #GstRTSPMediaMapping, unref after usage.
1585 GstRTSPMediaMapping *
1586 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1588 GstRTSPMediaMapping *result;
1590 if ((result = client->media_mapping))
1591 g_object_ref (result);
1597 * gst_rtsp_client_set_auth:
1598 * @client: a #GstRTSPClient
1599 * @auth: a #GstRTSPAuth
1601 * configure @auth to be used as the authentication manager of @client.
1604 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1608 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1614 g_object_ref (auth);
1615 client->auth = auth;
1617 g_object_unref (old);
1623 * gst_rtsp_client_get_auth:
1624 * @client: a #GstRTSPClient
1626 * Get the #GstRTSPAuth used as the authentication manager of @client.
1628 * Returns: the #GstRTSPAuth of @client. g_object_unref() after
1632 gst_rtsp_client_get_auth (GstRTSPClient * client)
1634 GstRTSPAuth *result;
1636 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1638 if ((result = client->auth))
1639 g_object_ref (result);
1644 static GstRTSPResult
1645 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1648 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1650 switch (message->type) {
1651 case GST_RTSP_MESSAGE_REQUEST:
1652 handle_request (client, message);
1654 case GST_RTSP_MESSAGE_RESPONSE:
1656 case GST_RTSP_MESSAGE_DATA:
1657 handle_data (client, message);
1665 static GstRTSPResult
1666 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1668 GstRTSPClient *client;
1670 client = GST_RTSP_CLIENT (user_data);
1672 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1677 static GstRTSPResult
1678 closed (GstRTSPWatch * watch, gpointer user_data)
1680 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1681 const gchar *tunnelid;
1683 GST_INFO ("client %p: connection closed", client);
1685 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1686 g_mutex_lock (tunnels_lock);
1687 /* remove from tunnelids */
1688 g_hash_table_remove (tunnels, tunnelid);
1689 g_mutex_unlock (tunnels_lock);
1695 static GstRTSPResult
1696 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1698 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1701 str = gst_rtsp_strresult (result);
1702 GST_INFO ("client %p: received an error %s", client, str);
1708 static GstRTSPResult
1709 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1710 GstRTSPMessage * message, guint id, gpointer user_data)
1712 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1715 str = gst_rtsp_strresult (result);
1717 ("client %p: received an error %s when handling message %p with id %d",
1718 client, str, message, id);
1725 remember_tunnel (GstRTSPClient * client)
1727 const gchar *tunnelid;
1729 /* store client in the pending tunnels */
1730 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1731 if (tunnelid == NULL)
1734 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1736 /* we can't have two clients connecting with the same tunnelid */
1737 g_mutex_lock (tunnels_lock);
1738 if (g_hash_table_lookup (tunnels, tunnelid))
1739 goto tunnel_existed;
1741 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1742 g_mutex_unlock (tunnels_lock);
1749 GST_ERROR ("client %p: no tunnelid provided", client);
1754 g_mutex_unlock (tunnels_lock);
1755 GST_ERROR ("client %p: tunnel session %s already existed", client,
1761 static GstRTSPStatusCode
1762 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1764 GstRTSPClient *client;
1766 client = GST_RTSP_CLIENT (user_data);
1768 GST_INFO ("client %p: tunnel start (connection %p)", client,
1769 client->connection);
1771 if (!remember_tunnel (client))
1774 return GST_RTSP_STS_OK;
1779 GST_ERROR ("client %p: error starting tunnel", client);
1780 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1784 static GstRTSPResult
1785 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1787 GstRTSPClient *client;
1789 client = GST_RTSP_CLIENT (user_data);
1791 GST_INFO ("client %p: tunnel lost (connection %p)", client,
1792 client->connection);
1794 /* ignore error, it'll only be a problem when the client does a POST again */
1795 remember_tunnel (client);
1800 static GstRTSPResult
1801 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1803 const gchar *tunnelid;
1804 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1805 GstRTSPClient *oclient;
1807 GST_INFO ("client %p: tunnel complete", client);
1809 /* find previous tunnel */
1810 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1811 if (tunnelid == NULL)
1814 g_mutex_lock (tunnels_lock);
1815 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1818 /* remove the old client from the table. ref before because removing it will
1819 * remove the ref to it. */
1820 g_object_ref (oclient);
1821 g_hash_table_remove (tunnels, tunnelid);
1823 if (oclient->watch == NULL)
1825 g_mutex_unlock (tunnels_lock);
1827 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1828 oclient->connection, client->connection);
1830 /* merge the tunnels into the first client */
1831 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1832 gst_rtsp_watch_reset (oclient->watch);
1833 g_object_unref (oclient);
1835 /* we don't need this watch anymore */
1836 g_source_destroy ((GSource *) client->watch);
1837 client->watchid = 0;
1838 client->watch = NULL;
1845 GST_INFO ("client %p: no tunnelid provided", client);
1846 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1850 g_mutex_unlock (tunnels_lock);
1851 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
1852 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1856 g_mutex_unlock (tunnels_lock);
1857 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
1858 g_object_unref (oclient);
1859 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1863 static GstRTSPWatchFuncs watch_funcs = {
1875 client_watch_notify (GstRTSPClient * client)
1877 GST_INFO ("client %p: watch destroyed", client);
1878 client->watchid = 0;
1879 client->watch = NULL;
1880 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
1881 g_object_unref (client);
1885 * gst_rtsp_client_attach:
1886 * @client: a #GstRTSPClient
1887 * @channel: a #GIOChannel
1889 * Accept a new connection for @client on the socket in @channel.
1891 * This function should be called when the client properties and urls are fully
1892 * configured and the client is ready to start.
1894 * Returns: %TRUE if the client could be accepted.
1897 gst_rtsp_client_accept (GstRTSPClient * client, GIOChannel * channel)
1900 GstRTSPConnection *conn;
1903 GMainContext *context;
1905 struct sockaddr_storage addr;
1907 gchar ip[INET6_ADDRSTRLEN];
1909 /* a new client connected. */
1910 sock = g_io_channel_unix_get_fd (channel);
1912 GST_RTSP_CHECK (gst_rtsp_connection_accept (sock, &conn), accept_failed);
1914 fd = gst_rtsp_connection_get_readfd (conn);
1916 addrlen = sizeof (addr);
1917 if (getsockname (fd, (struct sockaddr *) &addr, &addrlen) < 0)
1918 goto getpeername_failed;
1920 client->is_ipv6 = addr.ss_family == AF_INET6;
1922 if (getnameinfo ((struct sockaddr *) &addr, addrlen, ip, sizeof (ip), NULL, 0,
1923 NI_NUMERICHOST) != 0)
1924 goto getnameinfo_failed;
1926 /* keep the original ip that the client connected to */
1927 g_free (client->server_ip);
1928 client->server_ip = g_strndup (ip, sizeof (ip));
1930 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
1931 client->server_ip, client->is_ipv6);
1933 url = gst_rtsp_connection_get_url (conn);
1934 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
1936 client->connection = conn;
1938 /* create watch for the connection and attach */
1939 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
1940 g_object_ref (client), (GDestroyNotify) client_watch_notify);
1942 /* find the context to add the watch */
1943 if ((source = g_main_current_source ()))
1944 context = g_source_get_context (source);
1948 GST_INFO ("attaching to context %p", context);
1950 client->watchid = gst_rtsp_watch_attach (client->watch, context);
1951 gst_rtsp_watch_unref (client->watch);
1958 gchar *str = gst_rtsp_strresult (res);
1960 GST_ERROR ("Could not accept client on server socket %d: %s", sock, str);
1966 GST_ERROR ("getpeername failed: %s", g_strerror (errno));
1971 GST_ERROR ("getnameinfo failed: %s", g_strerror (errno));