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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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_MOUNT_POINTS 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_MOUNT_POINTS,
97 g_param_spec_object ("mount-points", "Mount Points",
98 "The mount points to use for client session",
99 GST_TYPE_RTSP_MOUNT_POINTS,
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;
177 client->close_response_seq = 0;
181 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
183 /* unlink all media managed in this session */
184 while (session->medias) {
185 GstRTSPSessionMedia *media = session->medias->data;
187 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
188 unlink_session_transports (client, session, media);
189 /* unmanage the media in the session. this will modify session->medias */
190 gst_rtsp_session_release_media (session, media);
195 client_cleanup_sessions (GstRTSPClient * client)
199 /* remove weak-ref from sessions */
200 for (sessions = client->sessions; sessions; sessions = g_list_next (sessions)) {
201 GstRTSPSession *session = (GstRTSPSession *) sessions->data;
202 g_object_weak_unref (G_OBJECT (session),
203 (GWeakNotify) client_session_finalized, client);
204 client_unlink_session (client, session);
206 g_list_free (client->sessions);
207 client->sessions = NULL;
210 /* A client is finalized when the connection is broken */
212 gst_rtsp_client_finalize (GObject * obj)
214 GstRTSPClient *client = GST_RTSP_CLIENT (obj);
216 GST_INFO ("finalize client %p", client);
219 g_source_destroy ((GSource *) client->watch);
221 client_cleanup_sessions (client);
223 gst_rtsp_connection_free (client->connection);
224 if (client->session_pool)
225 g_object_unref (client->session_pool);
226 if (client->mount_points)
227 g_object_unref (client->mount_points);
229 g_object_unref (client->auth);
232 gst_rtsp_url_free (client->uri);
234 gst_rtsp_media_unprepare (client->media);
235 g_object_unref (client->media);
238 g_free (client->server_ip);
240 G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
244 gst_rtsp_client_get_property (GObject * object, guint propid,
245 GValue * value, GParamSpec * pspec)
247 GstRTSPClient *client = GST_RTSP_CLIENT (object);
250 case PROP_SESSION_POOL:
251 g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
253 case PROP_MOUNT_POINTS:
254 g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
256 case PROP_USE_CLIENT_SETTINGS:
257 g_value_set_boolean (value,
258 gst_rtsp_client_get_use_client_settings (client));
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
266 gst_rtsp_client_set_property (GObject * object, guint propid,
267 const GValue * value, GParamSpec * pspec)
269 GstRTSPClient *client = GST_RTSP_CLIENT (object);
272 case PROP_SESSION_POOL:
273 gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
275 case PROP_MOUNT_POINTS:
276 gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
278 case PROP_USE_CLIENT_SETTINGS:
279 gst_rtsp_client_set_use_client_settings (client,
280 g_value_get_boolean (value));
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
288 * gst_rtsp_client_new:
290 * Create a new #GstRTSPClient instance.
292 * Returns: a new #GstRTSPClient
295 gst_rtsp_client_new (void)
297 GstRTSPClient *result;
299 result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
305 send_response (GstRTSPClient * client, GstRTSPSession * session,
306 GstRTSPMessage * response, gboolean close)
308 gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
309 "GStreamer RTSP server");
311 /* remove any previous header */
312 gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
314 /* add the new session header for new session ids */
316 gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION,
317 gst_rtsp_session_get_header (session));
320 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
321 gst_rtsp_message_dump (response);
325 gst_rtsp_message_add_header (response, GST_RTSP_HDR_CONNECTION, "close");
327 /* send the response and store the seq number so we can wait until it's
328 * written to the client to close the connection */
329 gst_rtsp_watch_send_message (client->watch, response, close ?
330 &client->close_response_seq : NULL);
331 gst_rtsp_message_unset (response);
335 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
336 GstRTSPClientState * state)
338 gst_rtsp_message_init_response (state->response, code,
339 gst_rtsp_status_as_text (code), state->request);
341 send_response (client, NULL, state->response, FALSE);
345 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
346 GstRTSPClientState * state)
348 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
349 gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
352 /* and let the authentication manager setup the auth tokens */
353 gst_rtsp_auth_setup_auth (auth, client, 0, state);
356 send_response (client, state->session, state->response, FALSE);
361 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
363 if (uri1 == NULL || uri2 == NULL)
366 if (strcmp (uri1->abspath, uri2->abspath))
372 /* this function is called to initially find the media for the DESCRIBE request
373 * but is cached for when the same client (without breaking the connection) is
374 * doing a setup for the exact same url. */
375 static GstRTSPMedia *
376 find_media (GstRTSPClient * client, GstRTSPClientState * state)
378 GstRTSPMediaFactory *factory;
382 if (!compare_uri (client->uri, state->uri)) {
383 /* remove any previously cached values before we try to construct a new
386 gst_rtsp_url_free (client->uri);
389 gst_rtsp_media_unprepare (client->media);
390 g_object_unref (client->media);
392 client->media = NULL;
394 if (!client->mount_points)
395 goto no_mount_points;
397 /* find the factory for the uri first */
399 gst_rtsp_mount_points_find_factory (client->mount_points,
403 state->factory = factory;
405 /* check if we have access to the factory */
406 if ((auth = gst_rtsp_media_factory_get_auth (factory))) {
407 if (!gst_rtsp_auth_check (auth, client, 0, state))
410 g_object_unref (auth);
413 /* prepare the media and add it to the pipeline */
414 if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
417 g_object_unref (factory);
419 state->factory = NULL;
421 /* set ipv6 on the media before preparing */
422 media->is_ipv6 = client->is_ipv6;
423 state->media = media;
425 /* prepare the media */
426 if (!(gst_rtsp_media_prepare (media)))
429 /* now keep track of the uri and the media */
430 client->uri = gst_rtsp_url_copy (state->uri);
431 client->media = media;
433 /* we have seen this uri before, used cached media */
434 media = client->media;
435 state->media = media;
436 GST_INFO ("reusing cached media %p", media);
440 g_object_ref (media);
447 GST_ERROR ("client %p: no mount points configured", client);
448 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
453 GST_ERROR ("client %p: no factory for uri", client);
454 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
459 GST_ERROR ("client %p: unauthorized request", client);
460 handle_unauthorized_request (client, auth, state);
461 g_object_unref (factory);
462 g_object_unref (auth);
467 GST_ERROR ("client %p: can't create media", client);
468 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
469 g_object_unref (factory);
474 GST_ERROR ("client %p: can't prepare media", client);
475 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
476 g_object_unref (media);
482 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
484 GstRTSPMessage message = { 0 };
489 gst_rtsp_message_init_data (&message, channel);
491 /* FIXME, need some sort of iovec RTSPMessage here */
492 if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
495 gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
497 /* FIXME, client->watch could have been finalized here, we need to keep an
498 * extra refcount to the watch. */
499 gst_rtsp_watch_send_message (client->watch, &message, NULL);
501 gst_rtsp_message_steal_body (&message, &data, &usize);
502 gst_buffer_unmap (buffer, &map_info);
504 gst_rtsp_message_unset (&message);
510 link_transport (GstRTSPClient * client, GstRTSPSession * session,
511 GstRTSPStreamTransport * trans)
513 GST_DEBUG ("client %p: linking transport %p", client, trans);
514 gst_rtsp_stream_transport_set_callbacks (trans,
515 (GstRTSPSendFunc) do_send_data,
516 (GstRTSPSendFunc) do_send_data, client, NULL);
518 client->transports = g_list_prepend (client->transports, trans);
520 /* make sure our session can't expire */
521 gst_rtsp_session_prevent_expire (session);
525 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
526 GstRTSPStreamTransport * trans)
528 GST_DEBUG ("client %p: unlinking transport %p", client, trans);
529 gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
531 client->transports = g_list_remove (client->transports, trans);
533 /* our session can now expire */
534 gst_rtsp_session_allow_expire (session);
538 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
539 GstRTSPSessionMedia * media)
543 n_streams = gst_rtsp_media_n_streams (media->media);
544 for (i = 0; i < n_streams; i++) {
545 GstRTSPStreamTransport *trans;
546 GstRTSPTransport *tr;
548 /* get the transport, if there is no transport configured, skip this stream */
549 trans = gst_rtsp_session_media_get_transport (media, i);
553 tr = trans->transport;
555 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
556 /* for TCP, unlink the stream from the TCP connection of the client */
557 unlink_transport (client, session, trans);
563 close_connection (GstRTSPClient * client)
565 const gchar *tunnelid;
567 GST_DEBUG ("client %p: closing connection", client);
569 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
570 g_mutex_lock (&tunnels_lock);
571 /* remove from tunnelids */
572 g_hash_table_remove (tunnels, tunnelid);
573 g_mutex_unlock (&tunnels_lock);
576 gst_rtsp_connection_close (client->connection);
580 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
582 GstRTSPSession *session;
583 GstRTSPSessionMedia *media;
584 GstRTSPStatusCode code;
589 session = state->session;
591 /* get a handle to the configuration of the media in the session */
592 media = gst_rtsp_session_get_media (session, state->uri);
596 state->sessmedia = media;
598 /* unlink the all TCP callbacks */
599 unlink_session_transports (client, session, media);
601 /* remove the session from the watched sessions */
602 g_object_weak_unref (G_OBJECT (session),
603 (GWeakNotify) client_session_finalized, client);
604 client->sessions = g_list_remove (client->sessions, session);
606 gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
608 /* unmanage the media in the session, returns false if all media session
610 if (!gst_rtsp_session_release_media (session, media)) {
611 /* remove the session */
612 gst_rtsp_session_pool_remove (client->session_pool, session);
614 /* construct the response now */
615 code = GST_RTSP_STS_OK;
616 gst_rtsp_message_init_response (state->response, code,
617 gst_rtsp_status_as_text (code), state->request);
619 send_response (client, session, state->response, TRUE);
621 /* we emit the signal before closing the connection */
622 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
630 GST_ERROR ("client %p: no session", client);
631 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
636 GST_ERROR ("client %p: no media for uri", client);
637 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
643 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
649 res = gst_rtsp_message_get_body (state->request, &data, &size);
650 if (res != GST_RTSP_OK)
654 /* no body, keep-alive request */
655 send_generic_response (client, GST_RTSP_STS_OK, state);
657 /* there is a body, handle the params */
658 res = gst_rtsp_params_get (client, state);
659 if (res != GST_RTSP_OK)
662 send_response (client, state->session, state->response, FALSE);
665 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
673 GST_ERROR ("client %p: bad request", client);
674 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
680 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
686 res = gst_rtsp_message_get_body (state->request, &data, &size);
687 if (res != GST_RTSP_OK)
691 /* no body, keep-alive request */
692 send_generic_response (client, GST_RTSP_STS_OK, state);
694 /* there is a body, handle the params */
695 res = gst_rtsp_params_set (client, state);
696 if (res != GST_RTSP_OK)
699 send_response (client, state->session, state->response, FALSE);
702 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
710 GST_ERROR ("client %p: bad request", client);
711 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
717 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
719 GstRTSPSession *session;
720 GstRTSPSessionMedia *media;
721 GstRTSPStatusCode code;
723 if (!(session = state->session))
726 /* get a handle to the configuration of the media in the session */
727 media = gst_rtsp_session_get_media (session, state->uri);
731 state->sessmedia = media;
733 /* the session state must be playing or recording */
734 if (media->state != GST_RTSP_STATE_PLAYING &&
735 media->state != GST_RTSP_STATE_RECORDING)
738 /* unlink the all TCP callbacks */
739 unlink_session_transports (client, session, media);
741 /* then pause sending */
742 gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
744 /* construct the response now */
745 code = GST_RTSP_STS_OK;
746 gst_rtsp_message_init_response (state->response, code,
747 gst_rtsp_status_as_text (code), state->request);
749 send_response (client, session, state->response, FALSE);
751 /* the state is now READY */
752 media->state = GST_RTSP_STATE_READY;
754 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
762 GST_ERROR ("client %p: no seesion", client);
763 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
768 GST_ERROR ("client %p: no media for uri", client);
769 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
774 GST_ERROR ("client %p: not PLAYING or RECORDING", client);
775 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
782 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
784 GstRTSPSession *session;
785 GstRTSPSessionMedia *media;
786 GstRTSPStatusCode code;
788 guint n_streams, i, infocount;
790 GstRTSPTimeRange *range;
793 if (!(session = state->session))
796 /* get a handle to the configuration of the media in the session */
797 media = gst_rtsp_session_get_media (session, state->uri);
801 state->sessmedia = media;
803 /* the session state must be playing or ready */
804 if (media->state != GST_RTSP_STATE_PLAYING &&
805 media->state != GST_RTSP_STATE_READY)
808 /* parse the range header if we have one */
810 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_RANGE, &str, 0);
811 if (res == GST_RTSP_OK) {
812 if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
813 /* we have a range, seek to the position */
814 gst_rtsp_media_seek (media->media, range);
815 gst_rtsp_range_free (range);
819 /* grab RTPInfo from the payloaders now */
820 rtpinfo = g_string_new ("");
822 n_streams = gst_rtsp_media_n_streams (media->media);
823 for (i = 0, infocount = 0; i < n_streams; i++) {
824 GstRTSPStreamTransport *trans;
825 GstRTSPTransport *tr;
829 /* get the transport, if there is no transport configured, skip this stream */
830 trans = gst_rtsp_session_media_get_transport (media, i);
832 GST_INFO ("stream %d is not configured", i);
835 tr = trans->transport;
837 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
838 /* for TCP, link the stream to the TCP connection of the client */
839 link_transport (client, session, trans);
842 if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
844 g_string_append (rtpinfo, ", ");
846 uristr = gst_rtsp_url_get_request_uri (state->uri);
847 g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
848 uristr, i, seq, rtptime);
853 GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
857 /* construct the response now */
858 code = GST_RTSP_STS_OK;
859 gst_rtsp_message_init_response (state->response, code,
860 gst_rtsp_status_as_text (code), state->request);
862 /* add the RTP-Info header */
864 str = g_string_free (rtpinfo, FALSE);
865 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
867 g_string_free (rtpinfo, TRUE);
871 str = gst_rtsp_media_get_range_string (media->media, TRUE);
872 gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
874 send_response (client, session, state->response, FALSE);
876 /* start playing after sending the request */
877 gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
879 media->state = GST_RTSP_STATE_PLAYING;
881 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
889 GST_ERROR ("client %p: no session", client);
890 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
895 GST_ERROR ("client %p: media not found", client);
896 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
901 GST_ERROR ("client %p: not PLAYING or READY", client);
902 send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
909 do_keepalive (GstRTSPSession * session)
911 GST_INFO ("keep session %p alive", session);
912 gst_rtsp_session_touch (session);
915 /* parse @transport and return a valid transport in @tr. only transports
916 * from @supported are returned. Returns FALSE if no valid transport
919 parse_transport (const char *transport, GstRTSPLowerTrans supported,
920 GstRTSPTransport * tr)
927 gst_rtsp_transport_init (tr);
929 GST_DEBUG ("parsing transports %s", transport);
931 transports = g_strsplit (transport, ",", 0);
933 /* loop through the transports, try to parse */
934 for (i = 0; transports[i]; i++) {
935 res = gst_rtsp_transport_parse (transports[i], tr);
936 if (res != GST_RTSP_OK) {
937 /* no valid transport, search some more */
938 GST_WARNING ("could not parse transport %s", transports[i]);
942 /* we have a transport, see if it's RTP/AVP */
943 if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
944 GST_WARNING ("invalid transport %s", transports[i]);
948 if (!(tr->lower_transport & supported)) {
949 GST_WARNING ("unsupported transport %s", transports[i]);
953 /* we have a valid transport */
954 GST_INFO ("found valid transport %s", transports[i]);
959 gst_rtsp_transport_init (tr);
961 g_strfreev (transports);
967 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
968 GstRTSPMessage * request)
970 gchar *blocksize_str;
973 if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
974 &blocksize_str, 0) == GST_RTSP_OK) {
978 blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
979 if (end == blocksize_str) {
980 GST_ERROR ("failed to parse blocksize");
983 /* we don't want to change the mtu when this media
984 * can be shared because it impacts other clients */
985 if (gst_rtsp_media_is_shared (media))
988 if (blocksize > G_MAXUINT)
989 blocksize = G_MAXUINT;
990 gst_rtsp_stream_set_mtu (stream, blocksize);
997 configure_client_transport (GstRTSPClient * client, GstRTSPClientState * state,
998 GstRTSPTransport * ct)
1000 /* we have a valid transport now, set the destination of the client. */
1001 if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1002 if (ct->destination == NULL || !client->use_client_settings) {
1003 GstRTSPAddress *addr;
1005 addr = gst_rtsp_stream_get_address (state->stream);
1009 g_free (ct->destination);
1010 ct->destination = g_strdup (addr->address);
1011 ct->port.min = addr->port;
1012 ct->port.max = addr->port + addr->n_ports - 1;
1013 ct->ttl = addr->ttl;
1018 url = gst_rtsp_connection_get_url (client->connection);
1019 g_free (ct->destination);
1020 ct->destination = g_strdup (url->host);
1022 if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1023 /* check if the client selected channels for TCP */
1024 if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1025 gst_rtsp_session_media_alloc_channels (state->sessmedia,
1035 GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1040 static GstRTSPTransport *
1041 make_server_transport (GstRTSPClient * client, GstRTSPClientState * state,
1042 GstRTSPTransport * ct)
1044 GstRTSPTransport *st;
1046 /* prepare the server transport */
1047 gst_rtsp_transport_new (&st);
1049 st->trans = ct->trans;
1050 st->profile = ct->profile;
1051 st->lower_transport = ct->lower_transport;
1053 switch (st->lower_transport) {
1054 case GST_RTSP_LOWER_TRANS_UDP:
1055 st->client_port = ct->client_port;
1056 st->server_port = state->stream->server_port;
1058 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1059 st->port = ct->port;
1060 st->destination = g_strdup (ct->destination);
1063 case GST_RTSP_LOWER_TRANS_TCP:
1064 st->interleaved = ct->interleaved;
1069 if (state->stream->session)
1070 g_object_get (state->stream->session, "internal-ssrc", &st->ssrc, NULL);
1076 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
1081 GstRTSPTransport *ct, *st;
1082 GstRTSPLowerTrans supported;
1083 GstRTSPStatusCode code;
1084 GstRTSPSession *session;
1085 GstRTSPStreamTransport *trans;
1086 gchar *trans_str, *pos;
1088 GstRTSPSessionMedia *sessmedia;
1089 GstRTSPMedia *media;
1090 GstRTSPStream *stream;
1094 /* the uri contains the stream number we added in the SDP config, which is
1095 * always /stream=%d so we need to strip that off
1096 * parse the stream we need to configure, look for the stream in the abspath
1097 * first and then in the query. */
1098 if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
1099 if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
1103 /* we can mofify the parsed uri in place */
1106 pos += strlen ("/stream=");
1107 if (sscanf (pos, "%u", &streamid) != 1)
1110 /* parse the transport */
1112 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
1114 if (res != GST_RTSP_OK)
1117 gst_rtsp_transport_new (&ct);
1119 /* our supported transports */
1120 supported = GST_RTSP_LOWER_TRANS_UDP |
1121 GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
1123 /* parse and find a usable supported transport */
1124 if (!parse_transport (transport, supported, ct))
1125 goto unsupported_transports;
1127 /* we create the session after parsing stuff so that we don't make
1128 * a session for malformed requests */
1129 if (client->session_pool == NULL)
1132 session = state->session;
1135 g_object_ref (session);
1136 /* get a handle to the configuration of the media in the session, this can
1137 * return NULL if this is a new url to manage in this session. */
1138 sessmedia = gst_rtsp_session_get_media (session, uri);
1140 /* create a session if this fails we probably reached our session limit or
1142 if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1143 goto service_unavailable;
1145 state->session = session;
1147 /* we need a new media configuration in this session */
1151 /* we have no media, find one and manage it */
1152 if (sessmedia == NULL) {
1153 /* get a handle to the configuration of the media in the session */
1154 if ((media = find_media (client, state))) {
1155 /* manage the media in our session now */
1156 sessmedia = gst_rtsp_session_manage_media (session, uri, media);
1160 /* if we stil have no media, error */
1161 if (sessmedia == NULL)
1164 state->sessmedia = sessmedia;
1165 state->media = media = sessmedia->media;
1167 /* now get the stream */
1168 stream = gst_rtsp_media_get_stream (media, streamid);
1172 state->stream = stream;
1174 /* set blocksize on this stream */
1175 if (!handle_blocksize (media, stream, state->request))
1176 goto invalid_blocksize;
1178 /* update the client transport */
1179 if (!configure_client_transport (client, state, ct))
1180 goto unsupported_client_transport;
1182 /* set in the session media transport */
1183 trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1185 /* configure keepalive for this transport */
1186 gst_rtsp_stream_transport_set_keepalive (trans,
1187 (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1189 /* create and serialize the server transport */
1190 st = make_server_transport (client, state, ct);
1191 trans_str = gst_rtsp_transport_as_text (st);
1192 gst_rtsp_transport_free (st);
1194 /* construct the response now */
1195 code = GST_RTSP_STS_OK;
1196 gst_rtsp_message_init_response (state->response, code,
1197 gst_rtsp_status_as_text (code), state->request);
1199 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1203 send_response (client, session, state->response, FALSE);
1205 /* update the state */
1206 switch (sessmedia->state) {
1207 case GST_RTSP_STATE_PLAYING:
1208 case GST_RTSP_STATE_RECORDING:
1209 case GST_RTSP_STATE_READY:
1210 /* no state change */
1213 sessmedia->state = GST_RTSP_STATE_READY;
1216 g_object_unref (session);
1218 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1226 GST_ERROR ("client %p: bad request", client);
1227 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1232 GST_ERROR ("client %p: media not found", client);
1233 send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1234 g_object_unref (session);
1235 gst_rtsp_transport_free (ct);
1240 GST_ERROR ("client %p: invalid blocksize", client);
1241 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1242 g_object_unref (session);
1243 gst_rtsp_transport_free (ct);
1246 unsupported_client_transport:
1248 GST_ERROR ("client %p: unsupported client transport", client);
1249 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1250 g_object_unref (session);
1251 gst_rtsp_transport_free (ct);
1256 GST_ERROR ("client %p: no transport", client);
1257 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1260 unsupported_transports:
1262 GST_ERROR ("client %p: unsupported transports", client);
1263 send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1264 gst_rtsp_transport_free (ct);
1269 GST_ERROR ("client %p: no session pool configured", client);
1270 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1271 gst_rtsp_transport_free (ct);
1274 service_unavailable:
1276 GST_ERROR ("client %p: can't create session", client);
1277 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1278 gst_rtsp_transport_free (ct);
1283 static GstSDPMessage *
1284 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1290 gst_sdp_message_new (&sdp);
1292 /* some standard things first */
1293 gst_sdp_message_set_version (sdp, "0");
1295 if (client->is_ipv6)
1300 gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1303 gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1304 gst_sdp_message_set_information (sdp, "rtsp-server");
1305 gst_sdp_message_add_time (sdp, "0", "0", NULL);
1306 gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1307 gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1308 gst_sdp_message_add_attribute (sdp, "control", "*");
1310 info.server_proto = proto;
1311 info.server_ip = g_strdup (client->server_ip);
1313 /* create an SDP for the media object */
1314 if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1317 g_free (info.server_ip);
1324 GST_ERROR ("client %p: could not create SDP", client);
1325 g_free (info.server_ip);
1326 gst_sdp_message_free (sdp);
1331 /* for the describe we must generate an SDP */
1333 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1338 gchar *str, *content_base;
1339 GstRTSPMedia *media;
1340 GstRTSPClientClass *klass;
1342 klass = GST_RTSP_CLIENT_GET_CLASS (client);
1344 /* check what kind of format is accepted, we don't really do anything with it
1345 * and always return SDP for now. */
1350 gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1352 if (res == GST_RTSP_ENOTIMPL)
1355 if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1359 /* find the media object for the uri */
1360 if (!(media = find_media (client, state)))
1363 /* create an SDP for the media object on this client */
1364 if (!(sdp = klass->create_sdp (client, media)))
1367 g_object_unref (media);
1369 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1370 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1372 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1375 /* content base for some clients that might screw up creating the setup uri */
1376 str = gst_rtsp_url_get_request_uri (state->uri);
1377 str_len = strlen (str);
1379 /* check for trailing '/' and append one */
1380 if (str[str_len - 1] != '/') {
1381 content_base = g_malloc (str_len + 2);
1382 memcpy (content_base, str, str_len);
1383 content_base[str_len] = '/';
1384 content_base[str_len + 1] = '\0';
1390 GST_INFO ("adding content-base: %s", content_base);
1392 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1394 g_free (content_base);
1396 /* add SDP to the response body */
1397 str = gst_sdp_message_as_text (sdp);
1398 gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1399 gst_sdp_message_free (sdp);
1401 send_response (client, state->session, state->response, FALSE);
1403 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1411 GST_ERROR ("client %p: no media", client);
1412 /* error reply is already sent */
1417 GST_ERROR ("client %p: can't create SDP", client);
1418 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1419 g_object_unref (media);
1425 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1427 GstRTSPMethod options;
1430 options = GST_RTSP_DESCRIBE |
1435 GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1437 str = gst_rtsp_options_as_text (options);
1439 gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1440 gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1442 gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1445 send_response (client, state->session, state->response, FALSE);
1447 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1453 /* remove duplicate and trailing '/' */
1455 sanitize_uri (GstRTSPUrl * uri)
1459 gboolean have_slash, prev_slash;
1461 s = d = uri->abspath;
1462 len = strlen (uri->abspath);
1466 for (i = 0; i < len; i++) {
1467 have_slash = s[i] == '/';
1469 if (!have_slash || !prev_slash)
1471 prev_slash = have_slash;
1473 len = d - uri->abspath;
1474 /* don't remove the first slash if that's the only thing left */
1475 if (len > 1 && *(d - 1) == '/')
1481 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1483 GST_INFO ("client %p: session %p finished", client, session);
1485 /* unlink all media managed in this session */
1486 client_unlink_session (client, session);
1488 /* remove the session */
1489 if (!(client->sessions = g_list_remove (client->sessions, session))) {
1490 GST_INFO ("client %p: all sessions finalized, close the connection",
1492 close_connection (client);
1497 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1501 for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1502 GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1504 /* we already know about this session */
1505 if (msession == session)
1509 GST_INFO ("watching session %p", session);
1511 g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1513 client->sessions = g_list_prepend (client->sessions, session);
1515 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1520 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1522 GstRTSPMethod method;
1523 const gchar *uristr;
1525 GstRTSPVersion version;
1527 GstRTSPSession *session;
1528 GstRTSPClientState state = { NULL };
1529 GstRTSPMessage response = { 0 };
1532 state.request = request;
1533 state.response = &response;
1535 if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1536 gst_rtsp_message_dump (request);
1539 GST_INFO ("client %p: received a request", client);
1541 gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1543 if (version != GST_RTSP_VERSION_1_0) {
1544 /* we can only handle 1.0 requests */
1545 send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1549 state.method = method;
1551 /* we always try to parse the url first */
1552 if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1553 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1557 /* sanitize the uri */
1561 /* get the session if there is any */
1562 res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1563 if (res == GST_RTSP_OK) {
1564 if (client->session_pool == NULL)
1567 /* we had a session in the request, find it again */
1568 if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1569 goto session_not_found;
1571 /* we add the session to the client list of watched sessions. When a session
1572 * disappears because it times out, we will be notified. If all sessions are
1573 * gone, we will close the connection */
1574 client_watch_session (client, session);
1578 state.session = session;
1581 if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1582 goto not_authorized;
1585 /* now see what is asked and dispatch to a dedicated handler */
1587 case GST_RTSP_OPTIONS:
1588 handle_options_request (client, &state);
1590 case GST_RTSP_DESCRIBE:
1591 handle_describe_request (client, &state);
1593 case GST_RTSP_SETUP:
1594 handle_setup_request (client, &state);
1597 handle_play_request (client, &state);
1599 case GST_RTSP_PAUSE:
1600 handle_pause_request (client, &state);
1602 case GST_RTSP_TEARDOWN:
1603 handle_teardown_request (client, &state);
1605 case GST_RTSP_SET_PARAMETER:
1606 handle_set_param_request (client, &state);
1608 case GST_RTSP_GET_PARAMETER:
1609 handle_get_param_request (client, &state);
1611 case GST_RTSP_ANNOUNCE:
1612 case GST_RTSP_RECORD:
1613 case GST_RTSP_REDIRECT:
1614 send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1616 case GST_RTSP_INVALID:
1618 send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1622 g_object_unref (session);
1624 gst_rtsp_url_free (uri);
1630 GST_ERROR ("client %p: no pool configured", client);
1631 send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1636 GST_ERROR ("client %p: session not found", client);
1637 send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1642 GST_ERROR ("client %p: not allowed", client);
1643 handle_unauthorized_request (client, client->auth, &state);
1649 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1659 /* find the stream for this message */
1660 res = gst_rtsp_message_parse_data (message, &channel);
1661 if (res != GST_RTSP_OK)
1664 gst_rtsp_message_steal_body (message, &data, &size);
1666 buffer = gst_buffer_new_wrapped (data, size);
1669 for (walk = client->transports; walk; walk = g_list_next (walk)) {
1670 GstRTSPStreamTransport *trans;
1671 GstRTSPStream *stream;
1672 GstRTSPTransport *tr;
1676 /* we only add clients with a transport to the list */
1677 tr = trans->transport;
1678 stream = trans->stream;
1680 /* check for TCP transport */
1681 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1682 /* dispatch to the stream based on the channel number */
1683 if (tr->interleaved.min == channel) {
1684 gst_rtsp_stream_recv_rtp (stream, buffer);
1687 } else if (tr->interleaved.max == channel) {
1688 gst_rtsp_stream_recv_rtcp (stream, buffer);
1695 gst_buffer_unref (buffer);
1699 * gst_rtsp_client_set_session_pool:
1700 * @client: a #GstRTSPClient
1701 * @pool: a #GstRTSPSessionPool
1703 * Set @pool as the sessionpool for @client which it will use to find
1704 * or allocate sessions. the sessionpool is usually inherited from the server
1705 * that created the client but can be overridden later.
1708 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1709 GstRTSPSessionPool * pool)
1711 GstRTSPSessionPool *old;
1713 old = client->session_pool;
1716 g_object_ref (pool);
1717 client->session_pool = pool;
1719 g_object_unref (old);
1724 * gst_rtsp_client_get_session_pool:
1725 * @client: a #GstRTSPClient
1727 * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1729 * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1731 GstRTSPSessionPool *
1732 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1734 GstRTSPSessionPool *result;
1736 if ((result = client->session_pool))
1737 g_object_ref (result);
1743 * gst_rtsp_client_set_server:
1744 * @client: a #GstRTSPClient
1745 * @server: a #GstRTSPServer
1747 * Set @server as the server that created @client.
1750 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1754 old = client->server;
1755 if (old != server) {
1757 g_object_ref (server);
1758 client->server = server;
1760 g_object_unref (old);
1765 * gst_rtsp_client_get_server:
1766 * @client: a #GstRTSPClient
1768 * Get the #GstRTSPServer object that @client was created from.
1770 * Returns: (transfer full): a #GstRTSPServer, unref after usage.
1773 gst_rtsp_client_get_server (GstRTSPClient * client)
1775 GstRTSPServer *result;
1777 if ((result = client->server))
1778 g_object_ref (result);
1784 * gst_rtsp_client_set_mount_points:
1785 * @client: a #GstRTSPClient
1786 * @mounts: a #GstRTSPMountPoints
1788 * Set @mounts as the mount points for @client which it will use to map urls
1789 * to media streams. These mount points are usually inherited from the server that
1790 * created the client but can be overriden later.
1793 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
1794 GstRTSPMountPoints * mounts)
1796 GstRTSPMountPoints *old;
1798 old = client->mount_points;
1800 if (old != mounts) {
1802 g_object_ref (mounts);
1803 client->mount_points = mounts;
1805 g_object_unref (old);
1810 * gst_rtsp_client_get_mount_points:
1811 * @client: a #GstRTSPClient
1813 * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
1815 * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
1817 GstRTSPMountPoints *
1818 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
1820 GstRTSPMountPoints *result;
1822 if ((result = client->mount_points))
1823 g_object_ref (result);
1829 * gst_rtsp_client_set_use_client_settings:
1830 * @client: a #GstRTSPClient
1831 * @use_client_settings: whether to use client settings for multicast
1833 * Use client transport settings (destination and ttl) for multicast.
1834 * When @use_client_settings is %FALSE, the server settings will be
1838 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1839 gboolean use_client_settings)
1841 client->use_client_settings = use_client_settings;
1845 * gst_rtsp_client_get_use_client_settings:
1846 * @client: a #GstRTSPClient
1848 * Check if client transport settings (destination and ttl) for multicast
1852 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1854 return client->use_client_settings;
1858 * gst_rtsp_client_set_auth:
1859 * @client: a #GstRTSPClient
1860 * @auth: a #GstRTSPAuth
1862 * configure @auth to be used as the authentication manager of @client.
1865 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1869 g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1875 g_object_ref (auth);
1876 client->auth = auth;
1878 g_object_unref (old);
1884 * gst_rtsp_client_get_auth:
1885 * @client: a #GstRTSPClient
1887 * Get the #GstRTSPAuth used as the authentication manager of @client.
1889 * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1893 gst_rtsp_client_get_auth (GstRTSPClient * client)
1895 GstRTSPAuth *result;
1897 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1899 if ((result = client->auth))
1900 g_object_ref (result);
1905 static GstRTSPResult
1906 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1909 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1911 switch (message->type) {
1912 case GST_RTSP_MESSAGE_REQUEST:
1913 handle_request (client, message);
1915 case GST_RTSP_MESSAGE_RESPONSE:
1917 case GST_RTSP_MESSAGE_DATA:
1918 handle_data (client, message);
1926 static GstRTSPResult
1927 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1929 GstRTSPClient *client;
1931 client = GST_RTSP_CLIENT (user_data);
1932 if (client->close_response_seq && client->close_response_seq == cseq) {
1933 client->close_response_seq = 0;
1934 close_connection (client);
1940 static GstRTSPResult
1941 closed (GstRTSPWatch * watch, gpointer user_data)
1943 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1944 const gchar *tunnelid;
1946 GST_INFO ("client %p: connection closed", client);
1948 if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1949 g_mutex_lock (&tunnels_lock);
1950 /* remove from tunnelids */
1951 g_hash_table_remove (tunnels, tunnelid);
1952 g_mutex_unlock (&tunnels_lock);
1958 static GstRTSPResult
1959 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1961 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1964 str = gst_rtsp_strresult (result);
1965 GST_INFO ("client %p: received an error %s", client, str);
1971 static GstRTSPResult
1972 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1973 GstRTSPMessage * message, guint id, gpointer user_data)
1975 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1978 str = gst_rtsp_strresult (result);
1980 ("client %p: received an error %s when handling message %p with id %d",
1981 client, str, message, id);
1988 remember_tunnel (GstRTSPClient * client)
1990 const gchar *tunnelid;
1992 /* store client in the pending tunnels */
1993 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1994 if (tunnelid == NULL)
1997 GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1999 /* we can't have two clients connecting with the same tunnelid */
2000 g_mutex_lock (&tunnels_lock);
2001 if (g_hash_table_lookup (tunnels, tunnelid))
2002 goto tunnel_existed;
2004 g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2005 g_mutex_unlock (&tunnels_lock);
2012 GST_ERROR ("client %p: no tunnelid provided", client);
2017 g_mutex_unlock (&tunnels_lock);
2018 GST_ERROR ("client %p: tunnel session %s already existed", client,
2024 static GstRTSPStatusCode
2025 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2027 GstRTSPClient *client;
2029 client = GST_RTSP_CLIENT (user_data);
2031 GST_INFO ("client %p: tunnel start (connection %p)", client,
2032 client->connection);
2034 if (!remember_tunnel (client))
2037 return GST_RTSP_STS_OK;
2042 GST_ERROR ("client %p: error starting tunnel", client);
2043 return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2047 static GstRTSPResult
2048 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2050 GstRTSPClient *client;
2052 client = GST_RTSP_CLIENT (user_data);
2054 GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2055 client->connection);
2057 /* ignore error, it'll only be a problem when the client does a POST again */
2058 remember_tunnel (client);
2063 static GstRTSPResult
2064 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2066 const gchar *tunnelid;
2067 GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2068 GstRTSPClient *oclient;
2070 GST_INFO ("client %p: tunnel complete", client);
2072 /* find previous tunnel */
2073 tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
2074 if (tunnelid == NULL)
2077 g_mutex_lock (&tunnels_lock);
2078 if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2081 /* remove the old client from the table. ref before because removing it will
2082 * remove the ref to it. */
2083 g_object_ref (oclient);
2084 g_hash_table_remove (tunnels, tunnelid);
2086 if (oclient->watch == NULL)
2088 g_mutex_unlock (&tunnels_lock);
2090 GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2091 oclient->connection, client->connection);
2093 /* merge the tunnels into the first client */
2094 gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
2095 gst_rtsp_watch_reset (oclient->watch);
2096 g_object_unref (oclient);
2103 GST_ERROR ("client %p: no tunnelid provided", client);
2104 return GST_RTSP_ERROR;
2108 g_mutex_unlock (&tunnels_lock);
2109 GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2110 return GST_RTSP_ERROR;
2114 g_mutex_unlock (&tunnels_lock);
2115 GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2116 g_object_unref (oclient);
2117 return GST_RTSP_ERROR;
2121 static GstRTSPWatchFuncs watch_funcs = {
2133 client_watch_notify (GstRTSPClient * client)
2135 GST_INFO ("client %p: watch destroyed", client);
2136 client->watch = NULL;
2137 g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2138 g_object_unref (client);
2142 setup_client (GstRTSPClient * client, GSocket * socket,
2143 GstRTSPConnection * conn, GError ** error)
2145 GSocket *read_socket;
2146 GSocketAddress *address;
2149 read_socket = gst_rtsp_connection_get_read_socket (conn);
2150 client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2152 if (!(address = g_socket_get_remote_address (read_socket, error)))
2155 g_free (client->server_ip);
2156 /* keep the original ip that the client connected to */
2157 if (G_IS_INET_SOCKET_ADDRESS (address)) {
2158 GInetAddress *iaddr;
2160 iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2162 client->server_ip = g_inet_address_to_string (iaddr);
2163 g_object_unref (address);
2165 client->server_ip = g_strdup ("unknown");
2168 GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2169 client->server_ip, client->is_ipv6);
2171 url = gst_rtsp_connection_get_url (conn);
2172 GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2174 client->connection = conn;
2181 GST_ERROR ("could not get remote address %s", (*error)->message);
2187 * gst_rtsp_client_use_socket:
2188 * @client: a #GstRTSPClient
2189 * @socket: a #GSocket
2190 * @ip: the IP address of the remote client
2191 * @port: the port used by the other end
2192 * @initial_buffer: any zero terminated initial data that was already read from
2196 * Take an existing network socket and use it for an RTSP connection.
2198 * Returns: %TRUE on success.
2201 gst_rtsp_client_use_socket (GstRTSPClient * client, GSocket * socket,
2202 const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2204 GstRTSPConnection *conn;
2207 GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2208 initial_buffer, &conn), no_connection);
2210 return setup_client (client, socket, conn, error);
2215 gchar *str = gst_rtsp_strresult (res);
2217 GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2224 * gst_rtsp_client_accept:
2225 * @client: a #GstRTSPClient
2226 * @socket: a #GSocket
2227 * @context: the context to run in
2228 * @cancellable: a #GCancellable
2231 * Accept a new connection for @client on @socket.
2233 * Returns: %TRUE if the client could be accepted.
2236 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2237 GCancellable * cancellable, GError ** error)
2239 GstRTSPConnection *conn;
2242 /* a new client connected. */
2243 GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2246 return setup_client (client, socket, conn, error);
2251 gchar *str = gst_rtsp_strresult (res);
2253 GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2260 * gst_rtsp_client_attach:
2261 * @client: a #GstRTSPClient
2262 * @context: (allow-none): a #GMainContext
2264 * Attaches @client to @context. When the mainloop for @context is run, the
2265 * client will be dispatched. When @context is NULL, the default context will be
2268 * This function should be called when the client properties and urls are fully
2269 * configured and the client is ready to start.
2271 * Returns: the ID (greater than 0) for the source within the GMainContext.
2274 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2278 g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2279 g_return_val_if_fail (client->watch == NULL, 0);
2281 /* create watch for the connection and attach */
2282 client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2283 g_object_ref (client), (GDestroyNotify) client_watch_notify);
2285 GST_INFO ("attaching to context %p", context);
2286 res = gst_rtsp_watch_attach (client->watch, context);
2287 gst_rtsp_watch_unref (client->watch);