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 g_mutex_init (&tunnels_lock);
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)
117 /* unlink all media managed in this session */
118 while (g_list_length (session->medias) > 0) {
119 GstRTSPSessionMedia *media = g_list_first (session->medias)->data;
121 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
122 unlink_session_streams (client, session, media);
123 /* unmanage the media in the session. this will modify session->medias */
124 gst_rtsp_session_release_media (session, media);
129 client_cleanup_sessions (GstRTSPClient * client)
133 /* remove weak-ref from sessions */
134 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
135 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
136 g_object_weak_unref (G_OBJECT (session),
137 (GWeakNotify) client_session_finalized, client);
138 client_unlink_session (client, session);
140 g_list_free (client->sessions);
141 client->sessions = NULL;
144 /* A client is finalized when the connection is broken */
146 gst_rtsp_client_finalize (GObject * obj)
148 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
150 GST_INFO ("finalize client %p", client);
152 client_cleanup_sessions (client);
154 gst_rtsp_connection_free (client->connection);
155 if (client->session_pool)
156 g_object_unref (client->session_pool);
157 if (client->media_mapping)
158 g_object_unref (client->media_mapping);
160 g_object_unref (client->auth);
163 gst_rtsp_url_free (client->uri);
165 g_object_unref (client->media);
167 g_free (client->server_ip);
169 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
173 gst_rtsp_client_get_property (GObject * object, guint propid,
174 GValue * value, GParamSpec * pspec)
176 GstRTSPClient *client = GST_RTSP_CLIENT (object);
179 case PROP_SESSION_POOL:
180 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
182 case PROP_MEDIA_MAPPING:
183 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
191 gst_rtsp_client_set_property (GObject * object, guint propid,
192 const GValue * value, GParamSpec * pspec)
194 GstRTSPClient *client = GST_RTSP_CLIENT (object);
197 case PROP_SESSION_POOL:
198 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
200 case PROP_MEDIA_MAPPING:
201 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
209 * gst_rtsp_client_new:
211 * Create a new #GstRTSPClient instance.
213 * Returns: a new #GstRTSPClient
216 gst_rtsp_client_new (void)
218 GstRTSPClient *result;
220 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
226 send_response (GstRTSPClient * client, GstRTSPSession * session,
227 GstRTSPMessage * response)
229 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
230 "GStreamer RTSP server");
232 /* remove any previous header */
233 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
235 /* add the new session header for new session ids */
239 if (session->timeout != 60)
241 g_strdup_printf ("%s; timeout=%d", session->sessionid,
244 str = g_strdup (session->sessionid);
246 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
249 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
250 gst_rtsp_message_dump (response);
253 gst_rtsp_watch_send_message (client->watch, response, NULL);
254 gst_rtsp_message_unset (response);
258 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
259 GstRTSPClientState * state)
261 gst_rtsp_message_init_response (state->response, code,
262 gst_rtsp_status_as_text (code), state->request);
264 send_response (client, NULL, state->response);
268 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
269 GstRTSPClientState * state)
271 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
272 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
275 /* and let the authentication manager setup the auth tokens */
276 gst_rtsp_auth_setup_auth (auth, client, 0, state);
279 send_response (client, state->session, state->response);
284 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
286 if (uri1 == NULL || uri2 == NULL)
289 if (strcmp (uri1->abspath, uri2->abspath))
295 /* this function is called to initially find the media for the DESCRIBE request
296 * but is cached for when the same client (without breaking the connection) is
297 * doing a setup for the exact same url. */
298 static GstRTSPMedia *
299 find_media (GstRTSPClient * client, GstRTSPClientState * state)
301 GstRTSPMediaFactory *factory;
305 if (!compare_uri (client->uri, state->uri)) {
306 /* remove any previously cached values before we try to construct a new
309 gst_rtsp_url_free (client->uri);
312 g_object_unref (client->media);
313 client->media = NULL;
315 if (!client->media_mapping)
318 /* find the factory for the uri first */
320 gst_rtsp_media_mapping_find_factory (client->media_mapping,
324 state->factory = factory;
326 /* check if we have access to the factory */
327 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
328 if (!gst_rtsp_auth_check (auth, client, 0, state))
331 g_object_unref (auth);
334 /* prepare the media and add it to the pipeline */
335 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
338 g_object_unref (factory);
340 state->factory = NULL;
342 /* set ipv6 on the media before preparing */
343 media->is_ipv6 = client->is_ipv6;
344 state->media = media;
346 /* prepare the media */
347 if (!(gst_rtsp_media_prepare (media)))
350 /* now keep track of the uri and the media */
351 client->uri = gst_rtsp_url_copy (state->uri);
352 client->media = media;
354 /* we have seen this uri before, used cached media */
355 media = client->media;
356 state->media = media;
357 GST_INFO ("reusing cached media %p", media);
361 g_object_ref (media);
368 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
373 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
378 handle_unauthorized_request (client, auth, state);
379 g_object_unref (factory);
380 g_object_unref (auth);
385 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
386 g_object_unref (factory);
391 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
392 g_object_unref (media);
398 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
400 GstRTSPMessage message = { 0 };
405 gst_rtsp_message_init_data (&message, channel);
407 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
410 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
412 /* FIXME, client->watch could have been finalized here, we need to keep an
413 * extra refcount to the watch. */
414 gst_rtsp_watch_send_message (client->watch, &message, NULL);
416 gst_rtsp_message_steal_body (&message, &data, &usize);
417 gst_buffer_unmap (buffer, &map_info);
419 gst_rtsp_message_unset (&message);
425 link_stream (GstRTSPClient * client, GstRTSPSession * session,
426 GstRTSPSessionStream * stream)
428 GST_DEBUG ("client %p: linking stream %p", client, stream);
429 gst_rtsp_session_stream_set_callbacks (stream, (GstRTSPSendFunc) do_send_data,
430 (GstRTSPSendFunc) do_send_data, client, NULL);
431 client->streams = g_list_prepend (client->streams, stream);
432 /* make sure our session can't expire */
433 gst_rtsp_session_prevent_expire (session);
437 unlink_stream (GstRTSPClient * client, GstRTSPSession * session,
438 GstRTSPSessionStream * stream)
440 GST_DEBUG ("client %p: unlinking stream %p", client, stream);
441 gst_rtsp_session_stream_set_callbacks (stream, NULL, NULL, NULL, NULL);
442 client->streams = g_list_remove (client->streams, stream);
443 /* our session can now expire */
444 gst_rtsp_session_allow_expire (session);
448 unlink_session_streams (GstRTSPClient * client, GstRTSPSession * session,
449 GstRTSPSessionMedia * media)
453 n_streams = gst_rtsp_media_n_streams (media->media);
454 for (i = 0; i < n_streams; i++) {
455 GstRTSPSessionStream *sstream;
456 GstRTSPTransport *tr;
458 /* get the stream as configured in the session */
459 sstream = gst_rtsp_session_media_get_stream (media, i);
460 /* get the transport, if there is no transport configured, skip this stream */
461 if (!(tr = sstream->trans.transport))
464 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
465 /* for TCP, unlink the stream from the TCP connection of the client */
466 unlink_stream (client, session, sstream);
472 close_connection (GstRTSPClient * client)
474 const gchar *tunnelid;
476 GST_DEBUG ("client %p: closing connection", client);
478 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
479 g_mutex_lock (&tunnels_lock);
480 /* remove from tunnelids */
481 g_hash_table_remove (tunnels, tunnelid);
482 g_mutex_unlock (&tunnels_lock);
485 gst_rtsp_connection_close (client->connection);
486 if (client->watchid) {
487 g_source_destroy ((GSource *) client->watch);
489 client->watch = NULL;
494 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
496 GstRTSPSession *session;
497 GstRTSPSessionMedia *media;
498 GstRTSPStatusCode code;
503 session = state->session;
505 /* get a handle to the configuration of the media in the session */
506 media = gst_rtsp_session_get_media (session, state->uri);
510 state->sessmedia = media;
512 /* unlink the all TCP callbacks */
513 unlink_session_streams (client, session, media);
515 /* remove the session from the watched sessions */
516 g_object_weak_unref (G_OBJECT (session),
517 (GWeakNotify) client_session_finalized, client);
518 client->sessions = g_list_remove (client->sessions, session);
520 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
522 /* unmanage the media in the session, returns false if all media session
524 if (!gst_rtsp_session_release_media (session, media)) {
525 /* remove the session */
526 gst_rtsp_session_pool_remove (client->session_pool, session);
528 /* construct the response now */
529 code = GST_RTSP_STS_OK;
530 gst_rtsp_message_init_response (state->response, code,
531 gst_rtsp_status_as_text (code), state->request);
533 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
536 send_response (client, session, state->response);
538 close_connection (client);
545 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
550 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
556 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
562 res = gst_rtsp_message_get_body (state->request, &data, &size);
563 if (res != GST_RTSP_OK)
567 /* no body, keep-alive request */
568 send_generic_response (client, GST_RTSP_STS_OK, state);
570 /* there is a body, handle the params */
571 res = gst_rtsp_params_get (client, state);
572 if (res != GST_RTSP_OK)
575 send_response (client, state->session, state->response);
582 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
588 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
594 res = gst_rtsp_message_get_body (state->request, &data, &size);
595 if (res != GST_RTSP_OK)
599 /* no body, keep-alive request */
600 send_generic_response (client, GST_RTSP_STS_OK, state);
602 /* there is a body, handle the params */
603 res = gst_rtsp_params_set (client, state);
604 if (res != GST_RTSP_OK)
607 send_response (client, state->session, state->response);
614 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
620 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
622 GstRTSPSession *session;
623 GstRTSPSessionMedia *media;
624 GstRTSPStatusCode code;
626 if (!(session = state->session))
629 /* get a handle to the configuration of the media in the session */
630 media = gst_rtsp_session_get_media (session, state->uri);
634 state->sessmedia = media;
636 /* the session state must be playing or recording */
637 if (media->state != GST_RTSP_STATE_PLAYING &&
638 media->state != GST_RTSP_STATE_RECORDING)
641 /* unlink the all TCP callbacks */
642 unlink_session_streams (client, session, media);
644 /* then pause sending */
645 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
647 /* construct the response now */
648 code = GST_RTSP_STS_OK;
649 gst_rtsp_message_init_response (state->response, code,
650 gst_rtsp_status_as_text (code), state->request);
652 send_response (client, session, state->response);
654 /* the state is now READY */
655 media->state = GST_RTSP_STATE_READY;
662 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
667 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
672 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
679 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
681 GstRTSPSession *session;
682 GstRTSPSessionMedia *media;
683 GstRTSPStatusCode code;
685 guint n_streams, i, infocount;
686 guint timestamp, seqnum;
688 GstRTSPTimeRange *range;
691 if (!(session = state->session))
694 /* get a handle to the configuration of the media in the session */
695 media = gst_rtsp_session_get_media (session, state->uri);
699 state->sessmedia = media;
701 /* the session state must be playing or ready */
702 if (media->state != GST_RTSP_STATE_PLAYING &&
703 media->state != GST_RTSP_STATE_READY)
706 /* parse the range header if we have one */
708 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
709 if (res == GST_RTSP_OK) {
710 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
711 /* we have a range, seek to the position */
712 gst_rtsp_media_seek (media->media, range);
713 gst_rtsp_range_free (range);
717 /* grab RTPInfo from the payloaders now */
718 rtpinfo = g_string_new ("");
720 n_streams = gst_rtsp_media_n_streams (media->media);
721 for (i = 0, infocount = 0; i < n_streams; i++) {
722 GstRTSPSessionStream *sstream;
723 GstRTSPMediaStream *stream;
724 GstRTSPTransport *tr;
725 GObjectClass *payobjclass;
728 /* get the stream as configured in the session */
729 sstream = gst_rtsp_session_media_get_stream (media, i);
730 /* get the transport, if there is no transport configured, skip this stream */
731 if (!(tr = sstream->trans.transport)) {
732 GST_INFO ("stream %d is not configured", i);
736 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
737 /* for TCP, link the stream to the TCP connection of the client */
738 link_stream (client, session, sstream);
741 stream = sstream->media_stream;
743 payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
745 if (g_object_class_find_property (payobjclass, "seqnum") &&
746 g_object_class_find_property (payobjclass, "timestamp")) {
749 payobj = G_OBJECT (stream->payloader);
751 /* only add RTP-Info for streams with seqnum and timestamp */
752 g_object_get (payobj, "seqnum", &seqnum, "timestamp", ×tamp, NULL);
755 g_string_append (rtpinfo, ", ");
757 uristr = gst_rtsp_url_get_request_uri (state->uri);
758 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
759 uristr, i, seqnum, timestamp);
764 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
768 /* construct the response now */
769 code = GST_RTSP_STS_OK;
770 gst_rtsp_message_init_response (state->response, code,
771 gst_rtsp_status_as_text (code), state->request);
773 /* add the RTP-Info header */
775 str = g_string_free (rtpinfo, FALSE);
776 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
778 g_string_free (rtpinfo, TRUE);
782 str = gst_rtsp_media_get_range_string (media->media, TRUE);
783 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
785 send_response (client, session, state->response);
787 /* start playing after sending the request */
788 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
790 media->state = GST_RTSP_STATE_PLAYING;
797 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
802 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
807 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
814 do_keepalive (GstRTSPSession * session)
816 GST_INFO ("keep session %p alive", session);
817 gst_rtsp_session_touch (session);
821 handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
823 gchar *blocksize_str;
826 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
827 &blocksize_str, 0) == GST_RTSP_OK) {
831 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
832 if (end == blocksize_str) {
833 GST_ERROR ("failed to parse blocksize");
836 if (blocksize > G_MAXUINT)
837 blocksize = G_MAXUINT;
838 gst_rtsp_media_handle_mtu (media, (guint)blocksize);
846 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
852 gboolean have_transport;
853 GstRTSPTransport *ct, *st;
855 GstRTSPLowerTrans supported;
856 GstRTSPStatusCode code;
857 GstRTSPSession *session;
858 GstRTSPSessionStream *stream;
859 gchar *trans_str, *pos;
861 GstRTSPSessionMedia *media;
865 /* the uri contains the stream number we added in the SDP config, which is
866 * always /stream=%d so we need to strip that off
867 * parse the stream we need to configure, look for the stream in the abspath
868 * first and then in the query. */
869 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
870 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
874 /* we can mofify the parse uri in place */
877 pos += strlen ("/stream=");
878 if (sscanf (pos, "%u", &streamid) != 1)
881 /* parse the transport */
883 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
885 if (res != GST_RTSP_OK)
888 transports = g_strsplit (transport, ",", 0);
889 gst_rtsp_transport_new (&ct);
891 /* init transports */
892 have_transport = FALSE;
893 gst_rtsp_transport_init (ct);
895 /* our supported transports */
896 supported = GST_RTSP_LOWER_TRANS_UDP |
897 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
899 /* loop through the transports, try to parse */
900 for (i = 0; transports[i]; i++) {
901 res = gst_rtsp_transport_parse (transports[i], ct);
902 if (res != GST_RTSP_OK) {
903 /* no valid transport, search some more */
904 GST_WARNING ("could not parse transport %s", transports[i]);
908 /* we have a transport, see if it's RTP/AVP */
909 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
910 GST_WARNING ("invalid transport %s", transports[i]);
914 if (!(ct->lower_transport & supported)) {
915 GST_WARNING ("unsupported transport %s", transports[i]);
919 /* we have a valid transport */
920 GST_INFO ("found valid transport %s", transports[i]);
921 have_transport = TRUE;
925 gst_rtsp_transport_init (ct);
927 g_strfreev (transports);
929 /* we have not found anything usable, error out */
931 goto unsupported_transports;
933 if (client->session_pool == NULL)
936 session = state->session;
939 g_object_ref (session);
940 /* get a handle to the configuration of the media in the session, this can
941 * return NULL if this is a new url to manage in this session. */
942 media = gst_rtsp_session_get_media (session, uri);
944 /* create a session if this fails we probably reached our session limit or
946 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
947 goto service_unavailable;
949 state->session = session;
951 /* we need a new media configuration in this session */
955 /* we have no media, find one and manage it */
959 /* get a handle to the configuration of the media in the session */
960 if ((m = find_media (client, state))) {
961 /* manage the media in our session now */
962 media = gst_rtsp_session_manage_media (session, uri, m);
966 /* if we stil have no media, error */
970 state->sessmedia = media;
972 if (!handle_blocksize (media->media, state->request))
973 goto invalid_blocksize;
975 /* we have a valid transport now, set the destination of the client. */
976 g_free (ct->destination);
977 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
978 ct->destination = gst_rtsp_media_get_multicast_group (media->media);
982 url = gst_rtsp_connection_get_url (client->connection);
983 ct->destination = g_strdup (url->host);
985 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
986 /* check if the client selected channels for TCP */
987 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
988 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
993 /* get a handle to the stream in the media */
994 if (!(stream = gst_rtsp_session_media_get_stream (media, streamid)))
997 st = gst_rtsp_session_stream_set_transport (stream, ct);
999 /* configure keepalive for this transport */
1000 gst_rtsp_session_stream_set_keepalive (stream,
1001 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1003 /* serialize the server transport */
1004 trans_str = gst_rtsp_transport_as_text (st);
1005 gst_rtsp_transport_free (st);
1007 /* construct the response now */
1008 code = GST_RTSP_STS_OK;
1009 gst_rtsp_message_init_response (state->response, code,
1010 gst_rtsp_status_as_text (code), state->request);
1012 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1016 send_response (client, session, state->response);
1018 /* update the state */
1019 switch (media->state) {
1020 case GST_RTSP_STATE_PLAYING:
1021 case GST_RTSP_STATE_RECORDING:
1022 case GST_RTSP_STATE_READY:
1023 /* no state change */
1026 media->state = GST_RTSP_STATE_READY;
1029 g_object_unref (session);
1036 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1041 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1042 g_object_unref (session);
1043 gst_rtsp_transport_free (ct);
1048 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1049 g_object_unref (session);
1050 gst_rtsp_transport_free (ct);
1055 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1056 g_object_unref (session);
1057 gst_rtsp_transport_free (ct);
1062 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1065 unsupported_transports:
1067 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1068 gst_rtsp_transport_free (ct);
1073 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1074 gst_rtsp_transport_free (ct);
1077 service_unavailable:
1079 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1080 gst_rtsp_transport_free (ct);
1085 static GstSDPMessage *
1086 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1091 GstRTSPLowerTrans protocols;
1093 gst_sdp_message_new (&sdp);
1095 /* some standard things first */
1096 gst_sdp_message_set_version (sdp, "0");
1098 if (client->is_ipv6)
1103 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1106 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1107 gst_sdp_message_set_information (sdp, "rtsp-server");
1108 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1109 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1110 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1111 gst_sdp_message_add_attribute (sdp, "control", "*");
1113 info.server_proto = proto;
1114 protocols = gst_rtsp_media_get_protocols (media);
1115 if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1116 info.server_ip = gst_rtsp_media_get_multicast_group (media);
1118 info.server_ip = g_strdup (client->server_ip);
1120 /* create an SDP for the media object */
1121 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1124 g_free (info.server_ip);
1131 g_free (info.server_ip);
1132 gst_sdp_message_free (sdp);
1137 /* for the describe we must generate an SDP */
1139 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1144 gchar *str, *content_base;
1145 GstRTSPMedia *media;
1147 /* check what kind of format is accepted, we don't really do anything with it
1148 * and always return SDP for now. */
1153 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1155 if (res == GST_RTSP_ENOTIMPL)
1158 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1162 /* find the media object for the uri */
1163 if (!(media = find_media (client, state)))
1166 /* create an SDP for the media object on this client */
1167 if (!(sdp = create_sdp (client, media)))
1170 g_object_unref (media);
1172 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1173 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1175 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1178 /* content base for some clients that might screw up creating the setup uri */
1179 str = gst_rtsp_url_get_request_uri (state->uri);
1180 str_len = strlen (str);
1182 /* check for trailing '/' and append one */
1183 if (str[str_len - 1] != '/') {
1184 content_base = g_malloc (str_len + 2);
1185 memcpy (content_base, str, str_len);
1186 content_base[str_len] = '/';
1187 content_base[str_len + 1] = '\0';
1193 GST_INFO ("adding content-base: %s", content_base);
1195 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1197 g_free (content_base);
1199 /* add SDP to the response body */
1200 str = gst_sdp_message_as_text (sdp);
1201 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1202 gst_sdp_message_free (sdp);
1204 send_response (client, state->session, state->response);
1211 /* error reply is already sent */
1216 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1217 g_object_unref (media);
1223 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1225 GstRTSPMethod options;
1228 options = GST_RTSP_DESCRIBE |
1233 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1235 str = gst_rtsp_options_as_text (options);
1237 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1238 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1240 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1243 send_response (client, state->session, state->response);
1248 /* remove duplicate and trailing '/' */
1250 sanitize_uri (GstRTSPUrl * uri)
1254 gboolean have_slash, prev_slash;
1256 s = d = uri->abspath;
1257 len = strlen (uri->abspath);
1261 for (i = 0; i < len; i++) {
1262 have_slash = s[i] == '/';
1264 if (!have_slash || !prev_slash)
1266 prev_slash = have_slash;
1268 len = d - uri->abspath;
1269 /* don't remove the first slash if that's the only thing left */
1270 if (len > 1 && *(d - 1) == '/')
1276 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1278 GST_INFO ("client %p: session %p finished", client, session);
1280 /* unlink all media managed in this session */
1281 client_unlink_session (client, session);
1283 /* remove the session */
1284 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1285 GST_INFO ("client %p: all sessions finalized, close the connection",
1287 close_connection (client);
1292 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1296 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1297 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1299 /* we already know about this session */
1300 if (msession == session)
1304 GST_INFO ("watching session %p", session);
1306 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1308 client->sessions = g_list_prepend (client->sessions, session);
1312 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1314 GstRTSPMethod method;
1315 const gchar *uristr;
1317 GstRTSPVersion version;
1319 GstRTSPSession *session;
1320 GstRTSPClientState state = { NULL };
1321 GstRTSPMessage response = { 0 };
1324 state.request = request;
1325 state.response = &response;
1327 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1328 gst_rtsp_message_dump (request);
1331 GST_INFO ("client %p: received a request", client);
1333 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1335 if (version != GST_RTSP_VERSION_1_0) {
1336 /* we can only handle 1.0 requests */
1337 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1341 state.method = method;
1343 /* we always try to parse the url first */
1344 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1345 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1349 /* sanitize the uri */
1353 /* get the session if there is any */
1354 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1355 if (res == GST_RTSP_OK) {
1356 if (client->session_pool == NULL)
1359 /* we had a session in the request, find it again */
1360 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1361 goto session_not_found;
1363 /* we add the session to the client list of watched sessions. When a session
1364 * disappears because it times out, we will be notified. If all sessions are
1365 * gone, we will close the connection */
1366 client_watch_session (client, session);
1370 state.session = session;
1373 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1374 goto not_authorized;
1377 /* now see what is asked and dispatch to a dedicated handler */
1379 case GST_RTSP_OPTIONS:
1380 handle_options_request (client, &state);
1382 case GST_RTSP_DESCRIBE:
1383 handle_describe_request (client, &state);
1385 case GST_RTSP_SETUP:
1386 handle_setup_request (client, &state);
1389 handle_play_request (client, &state);
1391 case GST_RTSP_PAUSE:
1392 handle_pause_request (client, &state);
1394 case GST_RTSP_TEARDOWN:
1395 handle_teardown_request (client, &state);
1397 case GST_RTSP_SET_PARAMETER:
1398 handle_set_param_request (client, &state);
1400 case GST_RTSP_GET_PARAMETER:
1401 handle_get_param_request (client, &state);
1403 case GST_RTSP_ANNOUNCE:
1404 case GST_RTSP_RECORD:
1405 case GST_RTSP_REDIRECT:
1406 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1408 case GST_RTSP_INVALID:
1410 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1414 g_object_unref (session);
1416 gst_rtsp_url_free (uri);
1422 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1427 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1432 handle_unauthorized_request (client, client->auth, &state);
1438 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1448 /* find the stream for this message */
1449 res = gst_rtsp_message_parse_data (message, &channel);
1450 if (res != GST_RTSP_OK)
1453 gst_rtsp_message_steal_body (message, &data, &size);
1455 buffer = gst_buffer_new_wrapped (data, size);
1458 for (walk = client->streams; walk; walk = g_list_next (walk)) {
1459 GstRTSPSessionStream *stream = (GstRTSPSessionStream *) walk->data;
1460 GstRTSPMediaStream *mstream;
1461 GstRTSPTransport *tr;
1463 /* get the transport, if there is no transport configured, skip this stream */
1464 if (!(tr = stream->trans.transport))
1467 /* we also need a media stream */
1468 if (!(mstream = stream->media_stream))
1471 /* check for TCP transport */
1472 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1473 /* dispatch to the stream based on the channel number */
1474 if (tr->interleaved.min == channel) {
1475 gst_rtsp_media_stream_rtp (mstream, buffer);
1478 } else if (tr->interleaved.max == channel) {
1479 gst_rtsp_media_stream_rtcp (mstream, buffer);
1486 gst_buffer_unref (buffer);
1490 * gst_rtsp_client_set_session_pool:
1491 * @client: a #GstRTSPClient
1492 * @pool: a #GstRTSPSessionPool
1494 * Set @pool as the sessionpool for @client which it will use to find
1495 * or allocate sessions. the sessionpool is usually inherited from the server
1496 * that created the client but can be overridden later.
1499 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1500 GstRTSPSessionPool * pool)
1502 GstRTSPSessionPool *old;
1504 old = client->session_pool;
1507 g_object_ref (pool);
1508 client->session_pool = pool;
1510 g_object_unref (old);
1515 * gst_rtsp_client_get_session_pool:
1516 * @client: a #GstRTSPClient
1518 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1520 * Returns: a #GstRTSPSessionPool, unref after usage.
1522 GstRTSPSessionPool *
1523 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1525 GstRTSPSessionPool *result;
1527 if ((result = client->session_pool))
1528 g_object_ref (result);
1534 * gst_rtsp_client_set_server:
1535 * @client: a #GstRTSPClient
1536 * @server: a #GstRTSPServer
1538 * Set @server as the server that created @client.
1541 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1545 old = client->server;
1546 if (old != server) {
1548 g_object_ref (server);
1549 client->server = server;
1551 g_object_unref (old);
1556 * gst_rtsp_client_get_server:
1557 * @client: a #GstRTSPClient
1559 * Get the #GstRTSPServer object that @client was created from.
1561 * Returns: a #GstRTSPServer, unref after usage.
1564 gst_rtsp_client_get_server (GstRTSPClient * client)
1566 GstRTSPServer *result;
1568 if ((result = client->server))
1569 g_object_ref (result);
1575 * gst_rtsp_client_set_media_mapping:
1576 * @client: a #GstRTSPClient
1577 * @mapping: a #GstRTSPMediaMapping
1579 * Set @mapping as the media mapping for @client which it will use to map urls
1580 * to media streams. These mapping is usually inherited from the server that
1581 * created the client but can be overriden later.
1584 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1585 GstRTSPMediaMapping * mapping)
1587 GstRTSPMediaMapping *old;
1589 old = client->media_mapping;
1591 if (old != mapping) {
1593 g_object_ref (mapping);
1594 client->media_mapping = mapping;
1596 g_object_unref (old);
1601 * gst_rtsp_client_get_media_mapping:
1602 * @client: a #GstRTSPClient
1604 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1606 * Returns: a #GstRTSPMediaMapping, unref after usage.
1608 GstRTSPMediaMapping *
1609 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1611 GstRTSPMediaMapping *result;
1613 if ((result = client->media_mapping))
1614 g_object_ref (result);
1620 * gst_rtsp_client_set_auth:
1621 * @client: a #GstRTSPClient
1622 * @auth: a #GstRTSPAuth
1624 * configure @auth to be used as the authentication manager of @client.
1627 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1631 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1637 g_object_ref (auth);
1638 client->auth = auth;
1640 g_object_unref (old);
1646 * gst_rtsp_client_get_auth:
1647 * @client: a #GstRTSPClient
1649 * Get the #GstRTSPAuth used as the authentication manager of @client.
1651 * Returns: the #GstRTSPAuth of @client. g_object_unref() after
1655 gst_rtsp_client_get_auth (GstRTSPClient * client)
1657 GstRTSPAuth *result;
1659 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1661 if ((result = client->auth))
1662 g_object_ref (result);
1667 static GstRTSPResult
1668 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1671 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1673 switch (message->type) {
1674 case GST_RTSP_MESSAGE_REQUEST:
1675 handle_request (client, message);
1677 case GST_RTSP_MESSAGE_RESPONSE:
1679 case GST_RTSP_MESSAGE_DATA:
1680 handle_data (client, message);
1688 static GstRTSPResult
1689 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1691 /* GstRTSPClient *client; */
1693 /* client = GST_RTSP_CLIENT (user_data); */
1695 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1700 static GstRTSPResult
1701 closed (GstRTSPWatch * watch, gpointer user_data)
1703 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1704 const gchar *tunnelid;
1706 GST_INFO ("client %p: connection closed", client);
1708 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1709 g_mutex_lock (&tunnels_lock);
1710 /* remove from tunnelids */
1711 g_hash_table_remove (tunnels, tunnelid);
1712 g_mutex_unlock (&tunnels_lock);
1718 static GstRTSPResult
1719 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1721 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1724 str = gst_rtsp_strresult (result);
1725 GST_INFO ("client %p: received an error %s", client, str);
1731 static GstRTSPResult
1732 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1733 GstRTSPMessage * message, guint id, gpointer user_data)
1735 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1738 str = gst_rtsp_strresult (result);
1740 ("client %p: received an error %s when handling message %p with id %d",
1741 client, str, message, id);
1748 remember_tunnel (GstRTSPClient * client)
1750 const gchar *tunnelid;
1752 /* store client in the pending tunnels */
1753 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1754 if (tunnelid == NULL)
1757 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1759 /* we can't have two clients connecting with the same tunnelid */
1760 g_mutex_lock (&tunnels_lock);
1761 if (g_hash_table_lookup (tunnels, tunnelid))
1762 goto tunnel_existed;
1764 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1765 g_mutex_unlock (&tunnels_lock);
1772 GST_ERROR ("client %p: no tunnelid provided", client);
1777 g_mutex_unlock (&tunnels_lock);
1778 GST_ERROR ("client %p: tunnel session %s already existed", client,
1784 static GstRTSPStatusCode
1785 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1787 GstRTSPClient *client;
1789 client = GST_RTSP_CLIENT (user_data);
1791 GST_INFO ("client %p: tunnel start (connection %p)", client,
1792 client->connection);
1794 if (!remember_tunnel (client))
1797 return GST_RTSP_STS_OK;
1802 GST_ERROR ("client %p: error starting tunnel", client);
1803 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1807 static GstRTSPResult
1808 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1810 GstRTSPClient *client;
1812 client = GST_RTSP_CLIENT (user_data);
1814 GST_INFO ("client %p: tunnel lost (connection %p)", client,
1815 client->connection);
1817 /* ignore error, it'll only be a problem when the client does a POST again */
1818 remember_tunnel (client);
1823 static GstRTSPResult
1824 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1826 const gchar *tunnelid;
1827 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1828 GstRTSPClient *oclient;
1830 GST_INFO ("client %p: tunnel complete", client);
1832 /* find previous tunnel */
1833 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1834 if (tunnelid == NULL)
1837 g_mutex_lock (&tunnels_lock);
1838 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1841 /* remove the old client from the table. ref before because removing it will
1842 * remove the ref to it. */
1843 g_object_ref (oclient);
1844 g_hash_table_remove (tunnels, tunnelid);
1846 if (oclient->watch == NULL)
1848 g_mutex_unlock (&tunnels_lock);
1850 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1851 oclient->connection, client->connection);
1853 /* merge the tunnels into the first client */
1854 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1855 gst_rtsp_watch_reset (oclient->watch);
1856 g_object_unref (oclient);
1858 /* we don't need this watch anymore */
1859 g_source_destroy ((GSource *) client->watch);
1860 client->watchid = 0;
1861 client->watch = NULL;
1868 GST_INFO ("client %p: no tunnelid provided", client);
1869 return GST_RTSP_ERROR;
1873 g_mutex_unlock (&tunnels_lock);
1874 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
1875 return GST_RTSP_ERROR;
1879 g_mutex_unlock (&tunnels_lock);
1880 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
1881 g_object_unref (oclient);
1882 return GST_RTSP_ERROR;
1886 static GstRTSPWatchFuncs watch_funcs = {
1898 client_watch_notify (GstRTSPClient * client)
1900 GST_INFO ("client %p: watch destroyed", client);
1901 client->watchid = 0;
1902 client->watch = NULL;
1903 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
1904 g_object_unref (client);
1908 * gst_rtsp_client_attach:
1909 * @client: a #GstRTSPClient
1910 * @socket: a #GSocket
1911 * @cancellable: a #GCancellable
1914 * Accept a new connection for @client on @socket.
1916 * This function should be called when the client properties and urls are fully
1917 * configured and the client is ready to start.
1919 * Returns: %TRUE if the client could be accepted.
1922 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
1923 GCancellable * cancellable, GError ** error)
1925 GstRTSPConnection *conn;
1927 GSocket *read_socket;
1928 GSocketAddress *addres;
1930 GMainContext *context;
1932 struct sockaddr_storage addr;
1934 gchar ip[INET6_ADDRSTRLEN];
1936 /* a new client connected. */
1937 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
1940 read_socket = gst_rtsp_connection_get_read_socket (conn);
1941 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
1943 if (!(addres = g_socket_get_remote_address (read_socket, error)))
1946 addrlen = sizeof (addr);
1947 if (!g_socket_address_to_native (addres, &addr, addrlen, error))
1949 g_object_unref (addres);
1951 if (getnameinfo ((struct sockaddr *) &addr, addrlen, ip, sizeof (ip), NULL, 0,
1952 NI_NUMERICHOST) != 0)
1953 goto getnameinfo_failed;
1955 /* keep the original ip that the client connected to */
1956 g_free (client->server_ip);
1957 client->server_ip = g_strndup (ip, sizeof (ip));
1959 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
1960 client->server_ip, client->is_ipv6);
1962 url = gst_rtsp_connection_get_url (conn);
1963 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
1965 client->connection = conn;
1967 /* create watch for the connection and attach */
1968 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
1969 g_object_ref (client), (GDestroyNotify) client_watch_notify);
1971 /* find the context to add the watch */
1972 if ((source = g_main_current_source ()))
1973 context = g_source_get_context (source);
1977 GST_INFO ("attaching to context %p", context);
1979 client->watchid = gst_rtsp_watch_attach (client->watch, context);
1980 gst_rtsp_watch_unref (client->watch);
1987 gchar *str = gst_rtsp_strresult (res);
1989 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
1995 GST_ERROR ("could not get remote address %s", (*error)->message);
2000 g_object_unref (addres);
2001 GST_ERROR ("could not get native address %s", (*error)->message);
2006 GST_ERROR ("getnameinfo failed: %s", g_strerror (errno));