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.
23 #include "rtsp-client.h"
25 #include "rtsp-params.h"
27 static GMutex tunnels_lock;
28 static GHashTable *tunnels;
30 #define DEFAULT_SESSION_POOL NULL
31 #define DEFAULT_MEDIA_MAPPING NULL
32 #define DEFAULT_USE_CLIENT_SETTINGS FALSE
39 PROP_USE_CLIENT_SETTINGS,
47 SIGNAL_OPTIONS_REQUEST,
48 SIGNAL_DESCRIBE_REQUEST,
52 SIGNAL_TEARDOWN_REQUEST,
53 SIGNAL_SET_PARAMETER_REQUEST,
54 SIGNAL_GET_PARAMETER_REQUEST,
58 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
59 #define GST_CAT_DEFAULT rtsp_client_debug
61 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
63 static void gst_rtsp_client_get_property (GObject * object, guint propid,
64 GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_client_set_property (GObject * object, guint propid,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_client_finalize (GObject * obj);
69 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
70 static void client_session_finalized (GstRTSPClient * client,
71 GstRTSPSession * session);
72 static void unlink_session_transports (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 klass->create_sdp = create_sdp;
90 g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
91 g_param_spec_object ("session-pool", "Session Pool",
92 "The session pool to use for client session",
93 GST_TYPE_RTSP_SESSION_POOL,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 g_object_class_install_property (gobject_class, PROP_MEDIA_MAPPING,
97 g_param_spec_object ("media-mapping", "Media Mapping",
98 "The media mapping to use for client session",
99 GST_TYPE_RTSP_MEDIA_MAPPING,
100 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102 g_object_class_install_property (gobject_class, PROP_USE_CLIENT_SETTINGS,
103 g_param_spec_boolean ("use-client-settings", "Use Client Settings",
104 "Use client settings for ttl and destination in multicast",
105 DEFAULT_USE_CLIENT_SETTINGS,
106 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 gst_rtsp_client_signals[SIGNAL_CLOSED] =
109 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
110 G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
111 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
113 gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
114 g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
115 G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
116 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
118 gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
119 g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
120 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
121 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
124 gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
125 g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
126 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
127 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
130 gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
131 g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
132 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
133 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
136 gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
137 g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
138 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
139 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
142 gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
143 g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
144 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
145 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
148 gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
149 g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
150 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
151 NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
154 gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
155 g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
156 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
157 set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
158 G_TYPE_NONE, 1, G_TYPE_POINTER);
160 gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
161 g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
162 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
163 get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
164 G_TYPE_NONE, 1, G_TYPE_POINTER);
167 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
168 g_mutex_init (&tunnels_lock);
170 GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
174 gst_rtsp_client_init (GstRTSPClient * client)
176 client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
180 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
182 /* unlink all media managed in this session */
183 while (g_list_length (session->medias) > 0) {
184 GstRTSPSessionMedia *media = g_list_first (session->medias)->data;
186 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
187 unlink_session_transports (client, session, media);
188 /* unmanage the media in the session. this will modify session->medias */
189 gst_rtsp_session_release_media (session, media);
194 client_cleanup_sessions (GstRTSPClient * client)
198 /* remove weak-ref from sessions */
199 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
200 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
201 g_object_weak_unref (G_OBJECT (session),
202 (GWeakNotify) client_session_finalized, client);
203 client_unlink_session (client, session);
205 g_list_free (client->sessions);
206 client->sessions = NULL;
209 /* A client is finalized when the connection is broken */
211 gst_rtsp_client_finalize (GObject * obj)
213 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
215 GST_INFO ("finalize client %p", client);
218 g_source_destroy ((GSource *) client->watch);
220 client_cleanup_sessions (client);
222 gst_rtsp_connection_free (client->connection);
223 if (client->session_pool)
224 g_object_unref (client->session_pool);
225 if (client->media_mapping)
226 g_object_unref (client->media_mapping);
228 g_object_unref (client->auth);
231 gst_rtsp_url_free (client->uri);
233 g_object_unref (client->media);
235 g_free (client->server_ip);
237 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
241 gst_rtsp_client_get_property (GObject * object, guint propid,
242 GValue * value, GParamSpec * pspec)
244 GstRTSPClient *client = GST_RTSP_CLIENT (object);
247 case PROP_SESSION_POOL:
248 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
250 case PROP_MEDIA_MAPPING:
251 g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
253 case PROP_USE_CLIENT_SETTINGS:
254 g_value_set_boolean (value,
255 gst_rtsp_client_get_use_client_settings (client));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
263 gst_rtsp_client_set_property (GObject * object, guint propid,
264 const GValue * value, GParamSpec * pspec)
266 GstRTSPClient *client = GST_RTSP_CLIENT (object);
269 case PROP_SESSION_POOL:
270 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
272 case PROP_MEDIA_MAPPING:
273 gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
275 case PROP_USE_CLIENT_SETTINGS:
276 gst_rtsp_client_set_use_client_settings (client,
277 g_value_get_boolean (value));
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
285 * gst_rtsp_client_new:
287 * Create a new #GstRTSPClient instance.
289 * Returns: a new #GstRTSPClient
292 gst_rtsp_client_new (void)
294 GstRTSPClient *result;
296 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
302 send_response (GstRTSPClient * client, GstRTSPSession * session,
303 GstRTSPMessage * response)
305 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
306 "GStreamer RTSP server");
308 /* remove any previous header */
309 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
311 /* add the new session header for new session ids */
315 if (session->timeout != 60)
317 g_strdup_printf ("%s; timeout=%d", session->sessionid,
320 str = g_strdup (session->sessionid);
322 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
325 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
326 gst_rtsp_message_dump (response);
329 gst_rtsp_watch_send_message (client->watch, response, NULL);
330 gst_rtsp_message_unset (response);
334 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
335 GstRTSPClientState * state)
337 gst_rtsp_message_init_response (state->response, code,
338 gst_rtsp_status_as_text (code), state->request);
340 send_response (client, NULL, state->response);
344 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
345 GstRTSPClientState * state)
347 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
348 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
351 /* and let the authentication manager setup the auth tokens */
352 gst_rtsp_auth_setup_auth (auth, client, 0, state);
355 send_response (client, state->session, state->response);
360 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
362 if (uri1 == NULL || uri2 == NULL)
365 if (strcmp (uri1->abspath, uri2->abspath))
371 /* this function is called to initially find the media for the DESCRIBE request
372 * but is cached for when the same client (without breaking the connection) is
373 * doing a setup for the exact same url. */
374 static GstRTSPMedia *
375 find_media (GstRTSPClient * client, GstRTSPClientState * state)
377 GstRTSPMediaFactory *factory;
381 if (!compare_uri (client->uri, state->uri)) {
382 /* remove any previously cached values before we try to construct a new
385 gst_rtsp_url_free (client->uri);
388 g_object_unref (client->media);
389 client->media = NULL;
391 if (!client->media_mapping)
394 /* find the factory for the uri first */
396 gst_rtsp_media_mapping_find_factory (client->media_mapping,
400 state->factory = factory;
402 /* check if we have access to the factory */
403 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
404 if (!gst_rtsp_auth_check (auth, client, 0, state))
407 g_object_unref (auth);
410 /* prepare the media and add it to the pipeline */
411 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
414 g_object_unref (factory);
416 state->factory = NULL;
418 /* set ipv6 on the media before preparing */
419 media->is_ipv6 = client->is_ipv6;
420 state->media = media;
422 /* prepare the media */
423 if (!(gst_rtsp_media_prepare (media)))
426 /* now keep track of the uri and the media */
427 client->uri = gst_rtsp_url_copy (state->uri);
428 client->media = media;
430 /* we have seen this uri before, used cached media */
431 media = client->media;
432 state->media = media;
433 GST_INFO ("reusing cached media %p", media);
437 g_object_ref (media);
444 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
449 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
454 handle_unauthorized_request (client, auth, state);
455 g_object_unref (factory);
456 g_object_unref (auth);
461 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
462 g_object_unref (factory);
467 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
468 g_object_unref (media);
474 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
476 GstRTSPMessage message = { 0 };
481 gst_rtsp_message_init_data (&message, channel);
483 /* FIXME, need some sort of iovec RTSPMessage here */
484 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
487 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
489 /* FIXME, client->watch could have been finalized here, we need to keep an
490 * extra refcount to the watch. */
491 gst_rtsp_watch_send_message (client->watch, &message, NULL);
493 gst_rtsp_message_steal_body (&message, &data, &usize);
494 gst_buffer_unmap (buffer, &map_info);
496 gst_rtsp_message_unset (&message);
502 link_transport (GstRTSPClient * client, GstRTSPSession * session,
503 GstRTSPStreamTransport * trans)
505 GST_DEBUG ("client %p: linking transport %p", client, trans);
506 gst_rtsp_stream_transport_set_callbacks (trans,
507 (GstRTSPSendFunc) do_send_data,
508 (GstRTSPSendFunc) do_send_data, client, NULL);
509 client->transports = g_list_prepend (client->transports, trans);
510 /* make sure our session can't expire */
511 gst_rtsp_session_prevent_expire (session);
515 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
516 GstRTSPStreamTransport * trans)
518 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
519 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
520 client->transports = g_list_remove (client->transports, trans);
521 /* our session can now expire */
522 gst_rtsp_session_allow_expire (session);
526 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
527 GstRTSPSessionMedia * media)
531 n_streams = gst_rtsp_media_n_streams (media->media);
532 for (i = 0; i < n_streams; i++) {
533 GstRTSPStreamTransport *trans;
534 GstRTSPTransport *tr;
536 /* get the stream as configured in the session */
537 trans = gst_rtsp_session_media_get_transport (media, i);
538 /* get the transport, if there is no transport configured, skip this stream */
539 if (!(tr = trans->transport))
542 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
543 /* for TCP, unlink the stream from the TCP connection of the client */
544 unlink_transport (client, session, trans);
550 close_connection (GstRTSPClient * client)
552 const gchar *tunnelid;
554 GST_DEBUG ("client %p: closing connection", client);
556 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
557 g_mutex_lock (&tunnels_lock);
558 /* remove from tunnelids */
559 g_hash_table_remove (tunnels, tunnelid);
560 g_mutex_unlock (&tunnels_lock);
563 gst_rtsp_connection_close (client->connection);
567 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
569 GstRTSPSession *session;
570 GstRTSPSessionMedia *media;
571 GstRTSPStatusCode code;
576 session = state->session;
578 /* get a handle to the configuration of the media in the session */
579 media = gst_rtsp_session_get_media (session, state->uri);
583 state->sessmedia = media;
585 /* unlink the all TCP callbacks */
586 unlink_session_transports (client, session, media);
588 /* remove the session from the watched sessions */
589 g_object_weak_unref (G_OBJECT (session),
590 (GWeakNotify) client_session_finalized, client);
591 client->sessions = g_list_remove (client->sessions, session);
593 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
595 /* unmanage the media in the session, returns false if all media session
597 if (!gst_rtsp_session_release_media (session, media)) {
598 /* remove the session */
599 gst_rtsp_session_pool_remove (client->session_pool, session);
601 /* construct the response now */
602 code = GST_RTSP_STS_OK;
603 gst_rtsp_message_init_response (state->response, code,
604 gst_rtsp_status_as_text (code), state->request);
606 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
609 send_response (client, session, state->response);
611 /* we emit the signal before closing the connection */
612 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
615 close_connection (client);
622 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
627 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
633 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
639 res = gst_rtsp_message_get_body (state->request, &data, &size);
640 if (res != GST_RTSP_OK)
644 /* no body, keep-alive request */
645 send_generic_response (client, GST_RTSP_STS_OK, state);
647 /* there is a body, handle the params */
648 res = gst_rtsp_params_get (client, state);
649 if (res != GST_RTSP_OK)
652 send_response (client, state->session, state->response);
655 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
663 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
669 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
675 res = gst_rtsp_message_get_body (state->request, &data, &size);
676 if (res != GST_RTSP_OK)
680 /* no body, keep-alive request */
681 send_generic_response (client, GST_RTSP_STS_OK, state);
683 /* there is a body, handle the params */
684 res = gst_rtsp_params_set (client, state);
685 if (res != GST_RTSP_OK)
688 send_response (client, state->session, state->response);
691 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
699 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
705 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
707 GstRTSPSession *session;
708 GstRTSPSessionMedia *media;
709 GstRTSPStatusCode code;
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 recording */
722 if (media->state != GST_RTSP_STATE_PLAYING &&
723 media->state != GST_RTSP_STATE_RECORDING)
726 /* unlink the all TCP callbacks */
727 unlink_session_transports (client, session, media);
729 /* then pause sending */
730 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
732 /* construct the response now */
733 code = GST_RTSP_STS_OK;
734 gst_rtsp_message_init_response (state->response, code,
735 gst_rtsp_status_as_text (code), state->request);
737 send_response (client, session, state->response);
739 /* the state is now READY */
740 media->state = GST_RTSP_STATE_READY;
742 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
750 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
755 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
760 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
767 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
769 GstRTSPSession *session;
770 GstRTSPSessionMedia *media;
771 GstRTSPStatusCode code;
773 guint n_streams, i, infocount;
775 GstRTSPTimeRange *range;
778 if (!(session = state->session))
781 /* get a handle to the configuration of the media in the session */
782 media = gst_rtsp_session_get_media (session, state->uri);
786 state->sessmedia = media;
788 /* the session state must be playing or ready */
789 if (media->state != GST_RTSP_STATE_PLAYING &&
790 media->state != GST_RTSP_STATE_READY)
793 /* parse the range header if we have one */
795 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
796 if (res == GST_RTSP_OK) {
797 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
798 /* we have a range, seek to the position */
799 gst_rtsp_media_seek (media->media, range);
800 gst_rtsp_range_free (range);
804 /* grab RTPInfo from the payloaders now */
805 rtpinfo = g_string_new ("");
807 n_streams = gst_rtsp_media_n_streams (media->media);
808 for (i = 0, infocount = 0; i < n_streams; i++) {
809 GstRTSPStreamTransport *trans;
810 GstRTSPTransport *tr;
814 /* get the stream as configured in the session */
815 trans = gst_rtsp_session_media_get_transport (media, i);
816 /* get the transport, if there is no transport configured, skip this stream */
817 if (!(tr = trans->transport)) {
818 GST_INFO ("stream %d is not configured", i);
822 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
823 /* for TCP, link the stream to the TCP connection of the client */
824 link_transport (client, session, trans);
827 if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
829 g_string_append (rtpinfo, ", ");
831 uristr = gst_rtsp_url_get_request_uri (state->uri);
832 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
833 uristr, i, seq, rtptime);
838 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
842 /* construct the response now */
843 code = GST_RTSP_STS_OK;
844 gst_rtsp_message_init_response (state->response, code,
845 gst_rtsp_status_as_text (code), state->request);
847 /* add the RTP-Info header */
849 str = g_string_free (rtpinfo, FALSE);
850 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
852 g_string_free (rtpinfo, TRUE);
856 str = gst_rtsp_media_get_range_string (media->media, TRUE);
857 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
859 send_response (client, session, state->response);
861 /* start playing after sending the request */
862 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
864 media->state = GST_RTSP_STATE_PLAYING;
866 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
874 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
879 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
884 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
891 do_keepalive (GstRTSPSession * session)
893 GST_INFO ("keep session %p alive", session);
894 gst_rtsp_session_touch (session);
898 handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
900 gchar *blocksize_str;
903 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
904 &blocksize_str, 0) == GST_RTSP_OK) {
908 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
909 if (end == blocksize_str) {
910 GST_ERROR ("failed to parse blocksize");
913 /* we don't want to change the mtu when this media
914 * can be shared because it impacts other clients */
915 if (gst_rtsp_media_is_shared (media))
918 if (blocksize > G_MAXUINT)
919 blocksize = G_MAXUINT;
920 gst_rtsp_media_set_mtu (media, blocksize);
928 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
934 gboolean have_transport;
935 GstRTSPTransport *ct, *st;
937 GstRTSPLowerTrans supported;
938 GstRTSPStatusCode code;
939 GstRTSPSession *session;
940 GstRTSPStreamTransport *trans;
941 gchar *trans_str, *pos;
943 GstRTSPSessionMedia *media;
947 /* the uri contains the stream number we added in the SDP config, which is
948 * always /stream=%d so we need to strip that off
949 * parse the stream we need to configure, look for the stream in the abspath
950 * first and then in the query. */
951 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
952 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
956 /* we can mofify the parse uri in place */
959 pos += strlen ("/stream=");
960 if (sscanf (pos, "%u", &streamid) != 1)
963 /* parse the transport */
965 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
967 if (res != GST_RTSP_OK)
970 transports = g_strsplit (transport, ",", 0);
971 gst_rtsp_transport_new (&ct);
973 /* init transports */
974 have_transport = FALSE;
975 gst_rtsp_transport_init (ct);
977 /* our supported transports */
978 supported = GST_RTSP_LOWER_TRANS_UDP |
979 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
981 /* loop through the transports, try to parse */
982 for (i = 0; transports[i]; i++) {
983 res = gst_rtsp_transport_parse (transports[i], ct);
984 if (res != GST_RTSP_OK) {
985 /* no valid transport, search some more */
986 GST_WARNING ("could not parse transport %s", transports[i]);
990 /* we have a transport, see if it's RTP/AVP */
991 if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
992 GST_WARNING ("invalid transport %s", transports[i]);
996 if (!(ct->lower_transport & supported)) {
997 GST_WARNING ("unsupported transport %s", transports[i]);
1001 /* we have a valid transport */
1002 GST_INFO ("found valid transport %s", transports[i]);
1003 have_transport = TRUE;
1007 gst_rtsp_transport_init (ct);
1009 g_strfreev (transports);
1011 /* we have not found anything usable, error out */
1012 if (!have_transport)
1013 goto unsupported_transports;
1015 if (client->session_pool == NULL)
1018 session = state->session;
1021 g_object_ref (session);
1022 /* get a handle to the configuration of the media in the session, this can
1023 * return NULL if this is a new url to manage in this session. */
1024 media = gst_rtsp_session_get_media (session, uri);
1026 /* create a session if this fails we probably reached our session limit or
1028 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1029 goto service_unavailable;
1031 state->session = session;
1033 /* we need a new media configuration in this session */
1037 /* we have no media, find one and manage it */
1038 if (media == NULL) {
1041 /* get a handle to the configuration of the media in the session */
1042 if ((m = find_media (client, state))) {
1043 /* manage the media in our session now */
1044 media = gst_rtsp_session_manage_media (session, uri, m);
1048 /* if we stil have no media, error */
1052 state->sessmedia = media;
1054 if (!handle_blocksize (media->media, state->request))
1055 goto invalid_blocksize;
1057 /* we have a valid transport now, set the destination of the client. */
1058 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1059 if (ct->destination == NULL || !client->use_client_settings) {
1060 g_free (ct->destination);
1061 ct->destination = gst_rtsp_media_get_multicast_group (media->media);
1063 /* reset ttl if client settings are not allowed */
1064 if (!client->use_client_settings) {
1070 url = gst_rtsp_connection_get_url (client->connection);
1071 g_free (ct->destination);
1072 ct->destination = g_strdup (url->host);
1074 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1075 /* check if the client selected channels for TCP */
1076 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1077 gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
1082 /* get a handle to the transport of the media in this session */
1083 if (!(trans = gst_rtsp_session_media_get_transport (media, streamid)))
1084 goto no_stream_transport;
1086 st = gst_rtsp_stream_transport_set_transport (trans, ct);
1088 /* configure keepalive for this transport */
1089 gst_rtsp_stream_transport_set_keepalive (trans,
1090 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1092 /* serialize the server transport */
1093 trans_str = gst_rtsp_transport_as_text (st);
1094 gst_rtsp_transport_free (st);
1096 /* construct the response now */
1097 code = GST_RTSP_STS_OK;
1098 gst_rtsp_message_init_response (state->response, code,
1099 gst_rtsp_status_as_text (code), state->request);
1101 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1105 send_response (client, session, state->response);
1107 /* update the state */
1108 switch (media->state) {
1109 case GST_RTSP_STATE_PLAYING:
1110 case GST_RTSP_STATE_RECORDING:
1111 case GST_RTSP_STATE_READY:
1112 /* no state change */
1115 media->state = GST_RTSP_STATE_READY;
1118 g_object_unref (session);
1120 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1128 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1133 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1134 g_object_unref (session);
1135 gst_rtsp_transport_free (ct);
1140 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1141 g_object_unref (session);
1142 gst_rtsp_transport_free (ct);
1145 no_stream_transport:
1147 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1148 g_object_unref (session);
1149 gst_rtsp_transport_free (ct);
1154 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1157 unsupported_transports:
1159 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1160 gst_rtsp_transport_free (ct);
1165 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1166 gst_rtsp_transport_free (ct);
1169 service_unavailable:
1171 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1172 gst_rtsp_transport_free (ct);
1177 static GstSDPMessage *
1178 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1183 GstRTSPLowerTrans protocols;
1185 gst_sdp_message_new (&sdp);
1187 /* some standard things first */
1188 gst_sdp_message_set_version (sdp, "0");
1190 if (client->is_ipv6)
1195 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1198 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1199 gst_sdp_message_set_information (sdp, "rtsp-server");
1200 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1201 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1202 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1203 gst_sdp_message_add_attribute (sdp, "control", "*");
1205 info.server_proto = proto;
1206 protocols = gst_rtsp_media_get_protocols (media);
1207 if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1208 info.server_ip = gst_rtsp_media_get_multicast_group (media);
1210 info.server_ip = g_strdup (client->server_ip);
1212 /* create an SDP for the media object */
1213 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1216 g_free (info.server_ip);
1223 g_free (info.server_ip);
1224 gst_sdp_message_free (sdp);
1229 /* for the describe we must generate an SDP */
1231 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1236 gchar *str, *content_base;
1237 GstRTSPMedia *media;
1238 GstRTSPClientClass *klass;
1240 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1242 /* check what kind of format is accepted, we don't really do anything with it
1243 * and always return SDP for now. */
1248 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1250 if (res == GST_RTSP_ENOTIMPL)
1253 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1257 /* find the media object for the uri */
1258 if (!(media = find_media (client, state)))
1261 /* create an SDP for the media object on this client */
1262 if (!(sdp = klass->create_sdp (client, media)))
1265 g_object_unref (media);
1267 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1268 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1270 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1273 /* content base for some clients that might screw up creating the setup uri */
1274 str = gst_rtsp_url_get_request_uri (state->uri);
1275 str_len = strlen (str);
1277 /* check for trailing '/' and append one */
1278 if (str[str_len - 1] != '/') {
1279 content_base = g_malloc (str_len + 2);
1280 memcpy (content_base, str, str_len);
1281 content_base[str_len] = '/';
1282 content_base[str_len + 1] = '\0';
1288 GST_INFO ("adding content-base: %s", content_base);
1290 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1292 g_free (content_base);
1294 /* add SDP to the response body */
1295 str = gst_sdp_message_as_text (sdp);
1296 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1297 gst_sdp_message_free (sdp);
1299 send_response (client, state->session, state->response);
1301 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1309 /* error reply is already sent */
1314 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1315 g_object_unref (media);
1321 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1323 GstRTSPMethod options;
1326 options = GST_RTSP_DESCRIBE |
1331 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1333 str = gst_rtsp_options_as_text (options);
1335 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1336 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1338 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1341 send_response (client, state->session, state->response);
1343 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1349 /* remove duplicate and trailing '/' */
1351 sanitize_uri (GstRTSPUrl * uri)
1355 gboolean have_slash, prev_slash;
1357 s = d = uri->abspath;
1358 len = strlen (uri->abspath);
1362 for (i = 0; i < len; i++) {
1363 have_slash = s[i] == '/';
1365 if (!have_slash || !prev_slash)
1367 prev_slash = have_slash;
1369 len = d - uri->abspath;
1370 /* don't remove the first slash if that's the only thing left */
1371 if (len > 1 && *(d - 1) == '/')
1377 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1379 GST_INFO ("client %p: session %p finished", client, session);
1381 /* unlink all media managed in this session */
1382 client_unlink_session (client, session);
1384 /* remove the session */
1385 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1386 GST_INFO ("client %p: all sessions finalized, close the connection",
1388 close_connection (client);
1393 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1397 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1398 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1400 /* we already know about this session */
1401 if (msession == session)
1405 GST_INFO ("watching session %p", session);
1407 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1409 client->sessions = g_list_prepend (client->sessions, session);
1411 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1416 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1418 GstRTSPMethod method;
1419 const gchar *uristr;
1421 GstRTSPVersion version;
1423 GstRTSPSession *session;
1424 GstRTSPClientState state = { NULL };
1425 GstRTSPMessage response = { 0 };
1428 state.request = request;
1429 state.response = &response;
1431 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1432 gst_rtsp_message_dump (request);
1435 GST_INFO ("client %p: received a request", client);
1437 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1439 if (version != GST_RTSP_VERSION_1_0) {
1440 /* we can only handle 1.0 requests */
1441 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1445 state.method = method;
1447 /* we always try to parse the url first */
1448 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1449 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1453 /* sanitize the uri */
1457 /* get the session if there is any */
1458 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1459 if (res == GST_RTSP_OK) {
1460 if (client->session_pool == NULL)
1463 /* we had a session in the request, find it again */
1464 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1465 goto session_not_found;
1467 /* we add the session to the client list of watched sessions. When a session
1468 * disappears because it times out, we will be notified. If all sessions are
1469 * gone, we will close the connection */
1470 client_watch_session (client, session);
1474 state.session = session;
1477 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1478 goto not_authorized;
1481 /* now see what is asked and dispatch to a dedicated handler */
1483 case GST_RTSP_OPTIONS:
1484 handle_options_request (client, &state);
1486 case GST_RTSP_DESCRIBE:
1487 handle_describe_request (client, &state);
1489 case GST_RTSP_SETUP:
1490 handle_setup_request (client, &state);
1493 handle_play_request (client, &state);
1495 case GST_RTSP_PAUSE:
1496 handle_pause_request (client, &state);
1498 case GST_RTSP_TEARDOWN:
1499 handle_teardown_request (client, &state);
1501 case GST_RTSP_SET_PARAMETER:
1502 handle_set_param_request (client, &state);
1504 case GST_RTSP_GET_PARAMETER:
1505 handle_get_param_request (client, &state);
1507 case GST_RTSP_ANNOUNCE:
1508 case GST_RTSP_RECORD:
1509 case GST_RTSP_REDIRECT:
1510 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1512 case GST_RTSP_INVALID:
1514 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1518 g_object_unref (session);
1520 gst_rtsp_url_free (uri);
1526 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1531 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1536 handle_unauthorized_request (client, client->auth, &state);
1542 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1552 /* find the stream for this message */
1553 res = gst_rtsp_message_parse_data (message, &channel);
1554 if (res != GST_RTSP_OK)
1557 gst_rtsp_message_steal_body (message, &data, &size);
1559 buffer = gst_buffer_new_wrapped (data, size);
1562 for (walk = client->transports; walk; walk = g_list_next (walk)) {
1563 GstRTSPStreamTransport *trans = (GstRTSPStreamTransport *) walk->data;
1564 GstRTSPStream *stream;
1565 GstRTSPTransport *tr;
1567 /* get the transport, if there is no transport configured, skip this stream */
1568 if (!(tr = trans->transport))
1571 /* we also need a media stream */
1572 if (!(stream = trans->stream))
1575 /* check for TCP transport */
1576 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1577 /* dispatch to the stream based on the channel number */
1578 if (tr->interleaved.min == channel) {
1579 gst_rtsp_stream_recv_rtp (stream, buffer);
1582 } else if (tr->interleaved.max == channel) {
1583 gst_rtsp_stream_recv_rtcp (stream, buffer);
1590 gst_buffer_unref (buffer);
1594 * gst_rtsp_client_set_session_pool:
1595 * @client: a #GstRTSPClient
1596 * @pool: a #GstRTSPSessionPool
1598 * Set @pool as the sessionpool for @client which it will use to find
1599 * or allocate sessions. the sessionpool is usually inherited from the server
1600 * that created the client but can be overridden later.
1603 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1604 GstRTSPSessionPool * pool)
1606 GstRTSPSessionPool *old;
1608 old = client->session_pool;
1611 g_object_ref (pool);
1612 client->session_pool = pool;
1614 g_object_unref (old);
1619 * gst_rtsp_client_get_session_pool:
1620 * @client: a #GstRTSPClient
1622 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1624 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1626 GstRTSPSessionPool *
1627 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1629 GstRTSPSessionPool *result;
1631 if ((result = client->session_pool))
1632 g_object_ref (result);
1638 * gst_rtsp_client_set_server:
1639 * @client: a #GstRTSPClient
1640 * @server: a #GstRTSPServer
1642 * Set @server as the server that created @client.
1645 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1649 old = client->server;
1650 if (old != server) {
1652 g_object_ref (server);
1653 client->server = server;
1655 g_object_unref (old);
1660 * gst_rtsp_client_get_server:
1661 * @client: a #GstRTSPClient
1663 * Get the #GstRTSPServer object that @client was created from.
1665 * Returns: (transfer full): a #GstRTSPServer, unref after usage.
1668 gst_rtsp_client_get_server (GstRTSPClient * client)
1670 GstRTSPServer *result;
1672 if ((result = client->server))
1673 g_object_ref (result);
1679 * gst_rtsp_client_set_media_mapping:
1680 * @client: a #GstRTSPClient
1681 * @mapping: a #GstRTSPMediaMapping
1683 * Set @mapping as the media mapping for @client which it will use to map urls
1684 * to media streams. These mapping is usually inherited from the server that
1685 * created the client but can be overriden later.
1688 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1689 GstRTSPMediaMapping * mapping)
1691 GstRTSPMediaMapping *old;
1693 old = client->media_mapping;
1695 if (old != mapping) {
1697 g_object_ref (mapping);
1698 client->media_mapping = mapping;
1700 g_object_unref (old);
1705 * gst_rtsp_client_get_media_mapping:
1706 * @client: a #GstRTSPClient
1708 * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1710 * Returns: (transfer full): a #GstRTSPMediaMapping, unref after usage.
1712 GstRTSPMediaMapping *
1713 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1715 GstRTSPMediaMapping *result;
1717 if ((result = client->media_mapping))
1718 g_object_ref (result);
1724 * gst_rtsp_client_set_use_client_settings:
1725 * @client: a #GstRTSPClient
1726 * @use_client_settings: whether to use client settings for multicast
1728 * Use client transport settings (destination and ttl) for multicast.
1729 * When @use_client_settings is %FALSE, the server settings will be
1733 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1734 gboolean use_client_settings)
1736 client->use_client_settings = use_client_settings;
1740 * gst_rtsp_client_get_use_client_settings:
1741 * @client: a #GstRTSPClient
1743 * Check if client transport settings (destination and ttl) for multicast
1747 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1749 return client->use_client_settings;
1753 * gst_rtsp_client_set_auth:
1754 * @client: a #GstRTSPClient
1755 * @auth: a #GstRTSPAuth
1757 * configure @auth to be used as the authentication manager of @client.
1760 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1764 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1770 g_object_ref (auth);
1771 client->auth = auth;
1773 g_object_unref (old);
1779 * gst_rtsp_client_get_auth:
1780 * @client: a #GstRTSPClient
1782 * Get the #GstRTSPAuth used as the authentication manager of @client.
1784 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1788 gst_rtsp_client_get_auth (GstRTSPClient * client)
1790 GstRTSPAuth *result;
1792 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1794 if ((result = client->auth))
1795 g_object_ref (result);
1800 static GstRTSPResult
1801 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1804 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1806 switch (message->type) {
1807 case GST_RTSP_MESSAGE_REQUEST:
1808 handle_request (client, message);
1810 case GST_RTSP_MESSAGE_RESPONSE:
1812 case GST_RTSP_MESSAGE_DATA:
1813 handle_data (client, message);
1821 static GstRTSPResult
1822 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1824 /* GstRTSPClient *client; */
1826 /* client = GST_RTSP_CLIENT (user_data); */
1828 /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1833 static GstRTSPResult
1834 closed (GstRTSPWatch * watch, gpointer user_data)
1836 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1837 const gchar *tunnelid;
1839 GST_INFO ("client %p: connection closed", client);
1841 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1842 g_mutex_lock (&tunnels_lock);
1843 /* remove from tunnelids */
1844 g_hash_table_remove (tunnels, tunnelid);
1845 g_mutex_unlock (&tunnels_lock);
1851 static GstRTSPResult
1852 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1854 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1857 str = gst_rtsp_strresult (result);
1858 GST_INFO ("client %p: received an error %s", client, str);
1864 static GstRTSPResult
1865 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1866 GstRTSPMessage * message, guint id, gpointer user_data)
1868 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1871 str = gst_rtsp_strresult (result);
1873 ("client %p: received an error %s when handling message %p with id %d",
1874 client, str, message, id);
1881 remember_tunnel (GstRTSPClient * client)
1883 const gchar *tunnelid;
1885 /* store client in the pending tunnels */
1886 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1887 if (tunnelid == NULL)
1890 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1892 /* we can't have two clients connecting with the same tunnelid */
1893 g_mutex_lock (&tunnels_lock);
1894 if (g_hash_table_lookup (tunnels, tunnelid))
1895 goto tunnel_existed;
1897 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1898 g_mutex_unlock (&tunnels_lock);
1905 GST_ERROR ("client %p: no tunnelid provided", client);
1910 g_mutex_unlock (&tunnels_lock);
1911 GST_ERROR ("client %p: tunnel session %s already existed", client,
1917 static GstRTSPStatusCode
1918 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1920 GstRTSPClient *client;
1922 client = GST_RTSP_CLIENT (user_data);
1924 GST_INFO ("client %p: tunnel start (connection %p)", client,
1925 client->connection);
1927 if (!remember_tunnel (client))
1930 return GST_RTSP_STS_OK;
1935 GST_ERROR ("client %p: error starting tunnel", client);
1936 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1940 static GstRTSPResult
1941 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1943 GstRTSPClient *client;
1945 client = GST_RTSP_CLIENT (user_data);
1947 GST_INFO ("client %p: tunnel lost (connection %p)", client,
1948 client->connection);
1950 /* ignore error, it'll only be a problem when the client does a POST again */
1951 remember_tunnel (client);
1956 static GstRTSPResult
1957 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1959 const gchar *tunnelid;
1960 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1961 GstRTSPClient *oclient;
1963 GST_INFO ("client %p: tunnel complete", client);
1965 /* find previous tunnel */
1966 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1967 if (tunnelid == NULL)
1970 g_mutex_lock (&tunnels_lock);
1971 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1974 /* remove the old client from the table. ref before because removing it will
1975 * remove the ref to it. */
1976 g_object_ref (oclient);
1977 g_hash_table_remove (tunnels, tunnelid);
1979 if (oclient->watch == NULL)
1981 g_mutex_unlock (&tunnels_lock);
1983 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1984 oclient->connection, client->connection);
1986 /* merge the tunnels into the first client */
1987 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1988 gst_rtsp_watch_reset (oclient->watch);
1989 g_object_unref (oclient);
1996 GST_INFO ("client %p: no tunnelid provided", client);
1997 return GST_RTSP_ERROR;
2001 g_mutex_unlock (&tunnels_lock);
2002 GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
2003 return GST_RTSP_ERROR;
2007 g_mutex_unlock (&tunnels_lock);
2008 GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
2009 g_object_unref (oclient);
2010 return GST_RTSP_ERROR;
2014 static GstRTSPWatchFuncs watch_funcs = {
2026 client_watch_notify (GstRTSPClient * client)
2028 GST_INFO ("client %p: watch destroyed", client);
2029 client->watchid = 0;
2030 client->watch = NULL;
2031 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2032 g_object_unref (client);
2036 attach_client (GstRTSPClient * client, GSocket * socket,
2037 GstRTSPConnection * conn, GError ** error)
2039 GSocket *read_socket;
2040 GSocketAddress *address;
2042 GMainContext *context;
2045 read_socket = gst_rtsp_connection_get_read_socket (conn);
2046 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2048 if (!(address = g_socket_get_remote_address (read_socket, error)))
2051 g_free (client->server_ip);
2052 /* keep the original ip that the client connected to */
2053 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2054 GInetAddress *iaddr;
2056 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2058 client->server_ip = g_inet_address_to_string (iaddr);
2059 g_object_unref (address);
2061 client->server_ip = g_strdup ("unknown");
2064 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2065 client->server_ip, client->is_ipv6);
2067 url = gst_rtsp_connection_get_url (conn);
2068 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2070 client->connection = conn;
2072 /* create watch for the connection and attach */
2073 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2074 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2076 /* find the context to add the watch */
2077 if ((source = g_main_current_source ()))
2078 context = g_source_get_context (source);
2082 GST_INFO ("attaching to context %p", context);
2084 client->watchid = gst_rtsp_watch_attach (client->watch, context);
2085 gst_rtsp_watch_unref (client->watch);
2092 GST_ERROR ("could not get remote address %s", (*error)->message);
2098 * gst_rtsp_client_create_from_socket:
2099 * @client: a #GstRTSPClient
2100 * @socket: a #GSocket
2101 * @ip: the IP address of the remote client
2102 * @port: the port used by the other end
2103 * @initial_buffer: any initial data that was already read from the socket
2106 * Take an existing network socket and use it for an RTSP connection.
2108 * Returns: %TRUE on success.
2111 gst_rtsp_client_create_from_socket (GstRTSPClient * client, GSocket * socket,
2112 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2114 GstRTSPConnection *conn;
2117 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2118 initial_buffer, &conn), no_connection);
2120 return attach_client (client, socket, conn, error);
2125 gchar *str = gst_rtsp_strresult (res);
2127 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2134 * gst_rtsp_client_accept:
2135 * @client: a #GstRTSPClient
2136 * @socket: a #GSocket
2137 * @cancellable: a #GCancellable
2140 * Accept a new connection for @client on @socket.
2142 * This function should be called when the client properties and urls are fully
2143 * configured and the client is ready to start.
2145 * Returns: %TRUE if the client could be accepted.
2148 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2149 GCancellable * cancellable, GError ** error)
2151 GstRTSPConnection *conn;
2154 /* a new client connected. */
2155 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2158 return attach_client (client, socket, conn, error);
2163 gchar *str = gst_rtsp_strresult (res);
2165 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);