rtsp: fix MTU setting
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include "rtsp-client.h"
24 #include "rtsp-sdp.h"
25 #include "rtsp-params.h"
26
27 static GMutex tunnels_lock;
28 static GHashTable *tunnels;
29
30 #define DEFAULT_SESSION_POOL            NULL
31 #define DEFAULT_MEDIA_MAPPING           NULL
32 #define DEFAULT_USE_CLIENT_SETTINGS     FALSE
33
34 enum
35 {
36   PROP_0,
37   PROP_SESSION_POOL,
38   PROP_MEDIA_MAPPING,
39   PROP_USE_CLIENT_SETTINGS,
40   PROP_LAST
41 };
42
43 enum
44 {
45   SIGNAL_CLOSED,
46   SIGNAL_NEW_SESSION,
47   SIGNAL_OPTIONS_REQUEST,
48   SIGNAL_DESCRIBE_REQUEST,
49   SIGNAL_SETUP_REQUEST,
50   SIGNAL_PLAY_REQUEST,
51   SIGNAL_PAUSE_REQUEST,
52   SIGNAL_TEARDOWN_REQUEST,
53   SIGNAL_SET_PARAMETER_REQUEST,
54   SIGNAL_GET_PARAMETER_REQUEST,
55   SIGNAL_LAST
56 };
57
58 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
59 #define GST_CAT_DEFAULT rtsp_client_debug
60
61 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
62
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);
68
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);
74
75 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
76
77 static void
78 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
79 {
80   GObjectClass *gobject_class;
81
82   gobject_class = G_OBJECT_CLASS (klass);
83
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;
87
88   klass->create_sdp = create_sdp;
89
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));
95
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));
101
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));
107
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);
112
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);
117
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,
122       G_TYPE_POINTER);
123
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,
128       G_TYPE_POINTER);
129
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,
134       G_TYPE_POINTER);
135
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,
140       G_TYPE_POINTER);
141
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,
146       G_TYPE_POINTER);
147
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,
152       G_TYPE_POINTER);
153
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);
159
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);
165
166   tunnels =
167       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
168   g_mutex_init (&tunnels_lock);
169
170   GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
171 }
172
173 static void
174 gst_rtsp_client_init (GstRTSPClient * client)
175 {
176   client->use_client_settings = DEFAULT_USE_CLIENT_SETTINGS;
177 }
178
179 static void
180 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
181 {
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;
185
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);
190   }
191 }
192
193 static void
194 client_cleanup_sessions (GstRTSPClient * client)
195 {
196   GList *sessions;
197
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);
204   }
205   g_list_free (client->sessions);
206   client->sessions = NULL;
207 }
208
209 /* A client is finalized when the connection is broken */
210 static void
211 gst_rtsp_client_finalize (GObject * obj)
212 {
213   GstRTSPClient *client = GST_RTSP_CLIENT (obj);
214
215   GST_INFO ("finalize client %p", client);
216
217   if (client->watchid)
218     g_source_destroy ((GSource *) client->watch);
219
220   client_cleanup_sessions (client);
221
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);
227   if (client->auth)
228     g_object_unref (client->auth);
229
230   if (client->uri)
231     gst_rtsp_url_free (client->uri);
232   if (client->media)
233     g_object_unref (client->media);
234
235   g_free (client->server_ip);
236
237   G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
238 }
239
240 static void
241 gst_rtsp_client_get_property (GObject * object, guint propid,
242     GValue * value, GParamSpec * pspec)
243 {
244   GstRTSPClient *client = GST_RTSP_CLIENT (object);
245
246   switch (propid) {
247     case PROP_SESSION_POOL:
248       g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
249       break;
250     case PROP_MEDIA_MAPPING:
251       g_value_take_object (value, gst_rtsp_client_get_media_mapping (client));
252       break;
253     case PROP_USE_CLIENT_SETTINGS:
254       g_value_set_boolean (value,
255           gst_rtsp_client_get_use_client_settings (client));
256       break;
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
259   }
260 }
261
262 static void
263 gst_rtsp_client_set_property (GObject * object, guint propid,
264     const GValue * value, GParamSpec * pspec)
265 {
266   GstRTSPClient *client = GST_RTSP_CLIENT (object);
267
268   switch (propid) {
269     case PROP_SESSION_POOL:
270       gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
271       break;
272     case PROP_MEDIA_MAPPING:
273       gst_rtsp_client_set_media_mapping (client, g_value_get_object (value));
274       break;
275     case PROP_USE_CLIENT_SETTINGS:
276       gst_rtsp_client_set_use_client_settings (client,
277           g_value_get_boolean (value));
278       break;
279     default:
280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
281   }
282 }
283
284 /**
285  * gst_rtsp_client_new:
286  *
287  * Create a new #GstRTSPClient instance.
288  *
289  * Returns: a new #GstRTSPClient
290  */
291 GstRTSPClient *
292 gst_rtsp_client_new (void)
293 {
294   GstRTSPClient *result;
295
296   result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
297
298   return result;
299 }
300
301 static void
302 send_response (GstRTSPClient * client, GstRTSPSession * session,
303     GstRTSPMessage * response)
304 {
305   gst_rtsp_message_add_header (response, GST_RTSP_HDR_SERVER,
306       "GStreamer RTSP server");
307
308   /* remove any previous header */
309   gst_rtsp_message_remove_header (response, GST_RTSP_HDR_SESSION, -1);
310
311   /* add the new session header for new session ids */
312   if (session) {
313     gchar *str;
314
315     if (session->timeout != 60)
316       str =
317           g_strdup_printf ("%s; timeout=%d", session->sessionid,
318           session->timeout);
319     else
320       str = g_strdup (session->sessionid);
321
322     gst_rtsp_message_take_header (response, GST_RTSP_HDR_SESSION, str);
323   }
324
325   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
326     gst_rtsp_message_dump (response);
327   }
328
329   gst_rtsp_watch_send_message (client->watch, response, NULL);
330   gst_rtsp_message_unset (response);
331 }
332
333 static void
334 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
335     GstRTSPClientState * state)
336 {
337   gst_rtsp_message_init_response (state->response, code,
338       gst_rtsp_status_as_text (code), state->request);
339
340   send_response (client, NULL, state->response);
341 }
342
343 static void
344 handle_unauthorized_request (GstRTSPClient * client, GstRTSPAuth * auth,
345     GstRTSPClientState * state)
346 {
347   gst_rtsp_message_init_response (state->response, GST_RTSP_STS_UNAUTHORIZED,
348       gst_rtsp_status_as_text (GST_RTSP_STS_UNAUTHORIZED), state->request);
349
350   if (auth) {
351     /* and let the authentication manager setup the auth tokens */
352     gst_rtsp_auth_setup_auth (auth, client, 0, state);
353   }
354
355   send_response (client, state->session, state->response);
356 }
357
358
359 static gboolean
360 compare_uri (const GstRTSPUrl * uri1, const GstRTSPUrl * uri2)
361 {
362   if (uri1 == NULL || uri2 == NULL)
363     return FALSE;
364
365   if (strcmp (uri1->abspath, uri2->abspath))
366     return FALSE;
367
368   return TRUE;
369 }
370
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)
376 {
377   GstRTSPMediaFactory *factory;
378   GstRTSPMedia *media;
379   GstRTSPAuth *auth;
380
381   if (!compare_uri (client->uri, state->uri)) {
382     /* remove any previously cached values before we try to construct a new
383      * media for uri */
384     if (client->uri)
385       gst_rtsp_url_free (client->uri);
386     client->uri = NULL;
387     if (client->media)
388       g_object_unref (client->media);
389     client->media = NULL;
390
391     if (!client->media_mapping)
392       goto no_mapping;
393
394     /* find the factory for the uri first */
395     if (!(factory =
396             gst_rtsp_media_mapping_find_factory (client->media_mapping,
397                 state->uri)))
398       goto no_factory;
399
400     state->factory = factory;
401
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))
405         goto not_allowed;
406
407       g_object_unref (auth);
408     }
409
410     /* prepare the media and add it to the pipeline */
411     if (!(media = gst_rtsp_media_factory_construct (factory, state->uri)))
412       goto no_media;
413
414     g_object_unref (factory);
415     factory = NULL;
416     state->factory = NULL;
417
418     /* set ipv6 on the media before preparing */
419     media->is_ipv6 = client->is_ipv6;
420     state->media = media;
421
422     /* prepare the media */
423     if (!(gst_rtsp_media_prepare (media)))
424       goto no_prepare;
425
426     /* now keep track of the uri and the media */
427     client->uri = gst_rtsp_url_copy (state->uri);
428     client->media = media;
429   } else {
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);
434   }
435
436   if (media)
437     g_object_ref (media);
438
439   return media;
440
441   /* ERRORS */
442 no_mapping:
443   {
444     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
445     return NULL;
446   }
447 no_factory:
448   {
449     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
450     return NULL;
451   }
452 not_allowed:
453   {
454     handle_unauthorized_request (client, auth, state);
455     g_object_unref (factory);
456     g_object_unref (auth);
457     return NULL;
458   }
459 no_media:
460   {
461     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
462     g_object_unref (factory);
463     return NULL;
464   }
465 no_prepare:
466   {
467     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
468     g_object_unref (media);
469     return NULL;
470   }
471 }
472
473 static gboolean
474 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
475 {
476   GstRTSPMessage message = { 0 };
477   GstMapInfo map_info;
478   guint8 *data;
479   guint usize;
480
481   gst_rtsp_message_init_data (&message, channel);
482
483   /* FIXME, need some sort of iovec RTSPMessage here */
484   if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
485     return FALSE;
486
487   gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
488
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);
492
493   gst_rtsp_message_steal_body (&message, &data, &usize);
494   gst_buffer_unmap (buffer, &map_info);
495
496   gst_rtsp_message_unset (&message);
497
498   return TRUE;
499 }
500
501 static void
502 link_transport (GstRTSPClient * client, GstRTSPSession * session,
503     GstRTSPStreamTransport * trans)
504 {
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);
512 }
513
514 static void
515 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
516     GstRTSPStreamTransport * trans)
517 {
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);
523 }
524
525 static void
526 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
527     GstRTSPSessionMedia * media)
528 {
529   guint n_streams, i;
530
531   n_streams = gst_rtsp_media_n_streams (media->media);
532   for (i = 0; i < n_streams; i++) {
533     GstRTSPStreamTransport *trans;
534     GstRTSPTransport *tr;
535
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))
540       continue;
541
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);
545     }
546   }
547 }
548
549 static void
550 close_connection (GstRTSPClient * client)
551 {
552   const gchar *tunnelid;
553
554   GST_DEBUG ("client %p: closing connection", client);
555
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);
561   }
562
563   gst_rtsp_connection_close (client->connection);
564 }
565
566 static gboolean
567 handle_teardown_request (GstRTSPClient * client, GstRTSPClientState * state)
568 {
569   GstRTSPSession *session;
570   GstRTSPSessionMedia *media;
571   GstRTSPStatusCode code;
572
573   if (!state->session)
574     goto no_session;
575
576   session = state->session;
577
578   /* get a handle to the configuration of the media in the session */
579   media = gst_rtsp_session_get_media (session, state->uri);
580   if (!media)
581     goto not_found;
582
583   state->sessmedia = media;
584
585   /* unlink the all TCP callbacks */
586   unlink_session_transports (client, session, media);
587
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);
592
593   gst_rtsp_session_media_set_state (media, GST_STATE_NULL);
594
595   /* unmanage the media in the session, returns false if all media session
596    * are torn down. */
597   if (!gst_rtsp_session_release_media (session, media)) {
598     /* remove the session */
599     gst_rtsp_session_pool_remove (client->session_pool, session);
600   }
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);
605
606   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONNECTION,
607       "close");
608
609   send_response (client, session, state->response);
610
611   /* we emit the signal before closing the connection */
612   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
613       0, state);
614
615   close_connection (client);
616
617   return TRUE;
618
619   /* ERRORS */
620 no_session:
621   {
622     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
623     return FALSE;
624   }
625 not_found:
626   {
627     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
628     return FALSE;
629   }
630 }
631
632 static gboolean
633 handle_get_param_request (GstRTSPClient * client, GstRTSPClientState * state)
634 {
635   GstRTSPResult res;
636   guint8 *data;
637   guint size;
638
639   res = gst_rtsp_message_get_body (state->request, &data, &size);
640   if (res != GST_RTSP_OK)
641     goto bad_request;
642
643   if (size == 0) {
644     /* no body, keep-alive request */
645     send_generic_response (client, GST_RTSP_STS_OK, state);
646   } else {
647     /* there is a body, handle the params */
648     res = gst_rtsp_params_get (client, state);
649     if (res != GST_RTSP_OK)
650       goto bad_request;
651
652     send_response (client, state->session, state->response);
653   }
654
655   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
656       0, state);
657
658   return TRUE;
659
660   /* ERRORS */
661 bad_request:
662   {
663     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
664     return FALSE;
665   }
666 }
667
668 static gboolean
669 handle_set_param_request (GstRTSPClient * client, GstRTSPClientState * state)
670 {
671   GstRTSPResult res;
672   guint8 *data;
673   guint size;
674
675   res = gst_rtsp_message_get_body (state->request, &data, &size);
676   if (res != GST_RTSP_OK)
677     goto bad_request;
678
679   if (size == 0) {
680     /* no body, keep-alive request */
681     send_generic_response (client, GST_RTSP_STS_OK, state);
682   } else {
683     /* there is a body, handle the params */
684     res = gst_rtsp_params_set (client, state);
685     if (res != GST_RTSP_OK)
686       goto bad_request;
687
688     send_response (client, state->session, state->response);
689   }
690
691   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
692       0, state);
693
694   return TRUE;
695
696   /* ERRORS */
697 bad_request:
698   {
699     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
700     return FALSE;
701   }
702 }
703
704 static gboolean
705 handle_pause_request (GstRTSPClient * client, GstRTSPClientState * state)
706 {
707   GstRTSPSession *session;
708   GstRTSPSessionMedia *media;
709   GstRTSPStatusCode code;
710
711   if (!(session = state->session))
712     goto no_session;
713
714   /* get a handle to the configuration of the media in the session */
715   media = gst_rtsp_session_get_media (session, state->uri);
716   if (!media)
717     goto not_found;
718
719   state->sessmedia = media;
720
721   /* the session state must be playing or recording */
722   if (media->state != GST_RTSP_STATE_PLAYING &&
723       media->state != GST_RTSP_STATE_RECORDING)
724     goto invalid_state;
725
726   /* unlink the all TCP callbacks */
727   unlink_session_transports (client, session, media);
728
729   /* then pause sending */
730   gst_rtsp_session_media_set_state (media, GST_STATE_PAUSED);
731
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);
736
737   send_response (client, session, state->response);
738
739   /* the state is now READY */
740   media->state = GST_RTSP_STATE_READY;
741
742   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST],
743       0, state);
744
745   return TRUE;
746
747   /* ERRORS */
748 no_session:
749   {
750     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
751     return FALSE;
752   }
753 not_found:
754   {
755     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
756     return FALSE;
757   }
758 invalid_state:
759   {
760     send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
761         state);
762     return FALSE;
763   }
764 }
765
766 static gboolean
767 handle_play_request (GstRTSPClient * client, GstRTSPClientState * state)
768 {
769   GstRTSPSession *session;
770   GstRTSPSessionMedia *media;
771   GstRTSPStatusCode code;
772   GString *rtpinfo;
773   guint n_streams, i, infocount;
774   gchar *str;
775   GstRTSPTimeRange *range;
776   GstRTSPResult res;
777
778   if (!(session = state->session))
779     goto no_session;
780
781   /* get a handle to the configuration of the media in the session */
782   media = gst_rtsp_session_get_media (session, state->uri);
783   if (!media)
784     goto not_found;
785
786   state->sessmedia = media;
787
788   /* the session state must be playing or ready */
789   if (media->state != GST_RTSP_STATE_PLAYING &&
790       media->state != GST_RTSP_STATE_READY)
791     goto invalid_state;
792
793   /* parse the range header if we have one */
794   res =
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);
801     }
802   }
803
804   /* grab RTPInfo from the payloaders now */
805   rtpinfo = g_string_new ("");
806
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;
811     gchar *uristr;
812     guint rtptime, seq;
813
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);
819       continue;
820     }
821
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);
825     }
826
827     if (gst_rtsp_stream_get_rtpinfo (trans->stream, &rtptime, &seq)) {
828       if (infocount > 0)
829         g_string_append (rtpinfo, ", ");
830
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);
834       g_free (uristr);
835
836       infocount++;
837     } else {
838       GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
839     }
840   }
841
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);
846
847   /* add the RTP-Info header */
848   if (infocount > 0) {
849     str = g_string_free (rtpinfo, FALSE);
850     gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RTP_INFO, str);
851   } else {
852     g_string_free (rtpinfo, TRUE);
853   }
854
855   /* add the range */
856   str = gst_rtsp_media_get_range_string (media->media, TRUE);
857   gst_rtsp_message_take_header (state->response, GST_RTSP_HDR_RANGE, str);
858
859   send_response (client, session, state->response);
860
861   /* start playing after sending the request */
862   gst_rtsp_session_media_set_state (media, GST_STATE_PLAYING);
863
864   media->state = GST_RTSP_STATE_PLAYING;
865
866   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST],
867       0, state);
868
869   return TRUE;
870
871   /* ERRORS */
872 no_session:
873   {
874     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, state);
875     return FALSE;
876   }
877 not_found:
878   {
879     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
880     return FALSE;
881   }
882 invalid_state:
883   {
884     send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
885         state);
886     return FALSE;
887   }
888 }
889
890 static void
891 do_keepalive (GstRTSPSession * session)
892 {
893   GST_INFO ("keep session %p alive", session);
894   gst_rtsp_session_touch (session);
895 }
896
897 static gboolean
898 handle_blocksize (GstRTSPMedia * media, GstRTSPMessage * request)
899 {
900   gchar *blocksize_str;
901   gboolean ret = TRUE;
902
903   if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
904           &blocksize_str, 0) == GST_RTSP_OK) {
905     guint64 blocksize;
906     gchar *end;
907
908     blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
909     if (end == blocksize_str) {
910       GST_ERROR ("failed to parse blocksize");
911       ret = FALSE;
912     } else {
913       if (blocksize > G_MAXUINT)
914         blocksize = G_MAXUINT;
915       gst_rtsp_media_set_mtu (media, blocksize);
916     }
917   }
918
919   return ret;
920 }
921
922 static gboolean
923 handle_setup_request (GstRTSPClient * client, GstRTSPClientState * state)
924 {
925   GstRTSPResult res;
926   GstRTSPUrl *uri;
927   gchar *transport;
928   gchar **transports;
929   gboolean have_transport;
930   GstRTSPTransport *ct, *st;
931   gint i;
932   GstRTSPLowerTrans supported;
933   GstRTSPStatusCode code;
934   GstRTSPSession *session;
935   GstRTSPStreamTransport *trans;
936   gchar *trans_str, *pos;
937   guint streamid;
938   GstRTSPSessionMedia *media;
939
940   uri = state->uri;
941
942   /* the uri contains the stream number we added in the SDP config, which is
943    * always /stream=%d so we need to strip that off 
944    * parse the stream we need to configure, look for the stream in the abspath
945    * first and then in the query. */
946   if (uri->abspath == NULL || !(pos = strstr (uri->abspath, "/stream="))) {
947     if (uri->query == NULL || !(pos = strstr (uri->query, "/stream=")))
948       goto bad_request;
949   }
950
951   /* we can mofify the parse uri in place */
952   *pos = '\0';
953
954   pos += strlen ("/stream=");
955   if (sscanf (pos, "%u", &streamid) != 1)
956     goto bad_request;
957
958   /* parse the transport */
959   res =
960       gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_TRANSPORT,
961       &transport, 0);
962   if (res != GST_RTSP_OK)
963     goto no_transport;
964
965   transports = g_strsplit (transport, ",", 0);
966   gst_rtsp_transport_new (&ct);
967
968   /* init transports */
969   have_transport = FALSE;
970   gst_rtsp_transport_init (ct);
971
972   /* our supported transports */
973   supported = GST_RTSP_LOWER_TRANS_UDP |
974       GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
975
976   /* loop through the transports, try to parse */
977   for (i = 0; transports[i]; i++) {
978     res = gst_rtsp_transport_parse (transports[i], ct);
979     if (res != GST_RTSP_OK) {
980       /* no valid transport, search some more */
981       GST_WARNING ("could not parse transport %s", transports[i]);
982       goto next;
983     }
984
985     /* we have a transport, see if it's RTP/AVP */
986     if (ct->trans != GST_RTSP_TRANS_RTP || ct->profile != GST_RTSP_PROFILE_AVP) {
987       GST_WARNING ("invalid transport %s", transports[i]);
988       goto next;
989     }
990
991     if (!(ct->lower_transport & supported)) {
992       GST_WARNING ("unsupported transport %s", transports[i]);
993       goto next;
994     }
995
996     /* we have a valid transport */
997     GST_INFO ("found valid transport %s", transports[i]);
998     have_transport = TRUE;
999     break;
1000
1001   next:
1002     gst_rtsp_transport_init (ct);
1003   }
1004   g_strfreev (transports);
1005
1006   /* we have not found anything usable, error out */
1007   if (!have_transport)
1008     goto unsupported_transports;
1009
1010   if (client->session_pool == NULL)
1011     goto no_pool;
1012
1013   session = state->session;
1014
1015   if (session) {
1016     g_object_ref (session);
1017     /* get a handle to the configuration of the media in the session, this can
1018      * return NULL if this is a new url to manage in this session. */
1019     media = gst_rtsp_session_get_media (session, uri);
1020   } else {
1021     /* create a session if this fails we probably reached our session limit or
1022      * something. */
1023     if (!(session = gst_rtsp_session_pool_create (client->session_pool)))
1024       goto service_unavailable;
1025
1026     state->session = session;
1027
1028     /* we need a new media configuration in this session */
1029     media = NULL;
1030   }
1031
1032   /* we have no media, find one and manage it */
1033   if (media == NULL) {
1034     GstRTSPMedia *m;
1035
1036     /* get a handle to the configuration of the media in the session */
1037     if ((m = find_media (client, state))) {
1038       /* manage the media in our session now */
1039       media = gst_rtsp_session_manage_media (session, uri, m);
1040     }
1041   }
1042
1043   /* if we stil have no media, error */
1044   if (media == NULL)
1045     goto not_found;
1046
1047   state->sessmedia = media;
1048
1049   if (!handle_blocksize (media->media, state->request))
1050     goto invalid_blocksize;
1051
1052   /* we have a valid transport now, set the destination of the client. */
1053   if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1054     if (ct->destination == NULL || !client->use_client_settings) {
1055       g_free (ct->destination);
1056       ct->destination = gst_rtsp_media_get_multicast_group (media->media);
1057     }
1058     /* reset ttl if client settings are not allowed */
1059     if (!client->use_client_settings) {
1060       ct->ttl = 0;
1061     }
1062   } else {
1063     GstRTSPUrl *url;
1064
1065     url = gst_rtsp_connection_get_url (client->connection);
1066     g_free (ct->destination);
1067     ct->destination = g_strdup (url->host);
1068
1069     if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1070       /* check if the client selected channels for TCP */
1071       if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1072         gst_rtsp_session_media_alloc_channels (media, &ct->interleaved);
1073       }
1074     }
1075   }
1076
1077   /* get a handle to the transport of the media in this session */
1078   if (!(trans = gst_rtsp_session_media_get_transport (media, streamid)))
1079     goto no_stream_transport;
1080
1081   st = gst_rtsp_stream_transport_set_transport (trans, ct);
1082
1083   /* configure keepalive for this transport */
1084   gst_rtsp_stream_transport_set_keepalive (trans,
1085       (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1086
1087   /* serialize the server transport */
1088   trans_str = gst_rtsp_transport_as_text (st);
1089   gst_rtsp_transport_free (st);
1090
1091   /* construct the response now */
1092   code = GST_RTSP_STS_OK;
1093   gst_rtsp_message_init_response (state->response, code,
1094       gst_rtsp_status_as_text (code), state->request);
1095
1096   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_TRANSPORT,
1097       trans_str);
1098   g_free (trans_str);
1099
1100   send_response (client, session, state->response);
1101
1102   /* update the state */
1103   switch (media->state) {
1104     case GST_RTSP_STATE_PLAYING:
1105     case GST_RTSP_STATE_RECORDING:
1106     case GST_RTSP_STATE_READY:
1107       /* no state change */
1108       break;
1109     default:
1110       media->state = GST_RTSP_STATE_READY;
1111       break;
1112   }
1113   g_object_unref (session);
1114
1115   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST],
1116       0, state);
1117
1118   return TRUE;
1119
1120   /* ERRORS */
1121 bad_request:
1122   {
1123     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1124     return FALSE;
1125   }
1126 not_found:
1127   {
1128     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1129     g_object_unref (session);
1130     gst_rtsp_transport_free (ct);
1131     return FALSE;
1132   }
1133 invalid_blocksize:
1134   {
1135     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, state);
1136     g_object_unref (session);
1137     gst_rtsp_transport_free (ct);
1138     return FALSE;
1139   }
1140 no_stream_transport:
1141   {
1142     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, state);
1143     g_object_unref (session);
1144     gst_rtsp_transport_free (ct);
1145     return FALSE;
1146   }
1147 no_transport:
1148   {
1149     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1150     return FALSE;
1151   }
1152 unsupported_transports:
1153   {
1154     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, state);
1155     gst_rtsp_transport_free (ct);
1156     return FALSE;
1157   }
1158 no_pool:
1159   {
1160     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1161     gst_rtsp_transport_free (ct);
1162     return FALSE;
1163   }
1164 service_unavailable:
1165   {
1166     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1167     gst_rtsp_transport_free (ct);
1168     return FALSE;
1169   }
1170 }
1171
1172 static GstSDPMessage *
1173 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1174 {
1175   GstSDPMessage *sdp;
1176   GstSDPInfo info;
1177   const gchar *proto;
1178   GstRTSPLowerTrans protocols;
1179
1180   gst_sdp_message_new (&sdp);
1181
1182   /* some standard things first */
1183   gst_sdp_message_set_version (sdp, "0");
1184
1185   if (client->is_ipv6)
1186     proto = "IP6";
1187   else
1188     proto = "IP4";
1189
1190   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1191       client->server_ip);
1192
1193   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1194   gst_sdp_message_set_information (sdp, "rtsp-server");
1195   gst_sdp_message_add_time (sdp, "0", "0", NULL);
1196   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1197   gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1198   gst_sdp_message_add_attribute (sdp, "control", "*");
1199
1200   info.server_proto = proto;
1201   protocols = gst_rtsp_media_get_protocols (media);
1202   if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
1203     info.server_ip = gst_rtsp_media_get_multicast_group (media);
1204   else
1205     info.server_ip = g_strdup (client->server_ip);
1206
1207   /* create an SDP for the media object */
1208   if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1209     goto no_sdp;
1210
1211   g_free (info.server_ip);
1212
1213   return sdp;
1214
1215   /* ERRORS */
1216 no_sdp:
1217   {
1218     g_free (info.server_ip);
1219     gst_sdp_message_free (sdp);
1220     return NULL;
1221   }
1222 }
1223
1224 /* for the describe we must generate an SDP */
1225 static gboolean
1226 handle_describe_request (GstRTSPClient * client, GstRTSPClientState * state)
1227 {
1228   GstRTSPResult res;
1229   GstSDPMessage *sdp;
1230   guint i, str_len;
1231   gchar *str, *content_base;
1232   GstRTSPMedia *media;
1233   GstRTSPClientClass *klass;
1234
1235   klass = GST_RTSP_CLIENT_GET_CLASS (client);
1236
1237   /* check what kind of format is accepted, we don't really do anything with it
1238    * and always return SDP for now. */
1239   for (i = 0; i++;) {
1240     gchar *accept;
1241
1242     res =
1243         gst_rtsp_message_get_header (state->request, GST_RTSP_HDR_ACCEPT,
1244         &accept, i);
1245     if (res == GST_RTSP_ENOTIMPL)
1246       break;
1247
1248     if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1249       break;
1250   }
1251
1252   /* find the media object for the uri */
1253   if (!(media = find_media (client, state)))
1254     goto no_media;
1255
1256   /* create an SDP for the media object on this client */
1257   if (!(sdp = klass->create_sdp (client, media)))
1258     goto no_sdp;
1259
1260   g_object_unref (media);
1261
1262   gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1263       gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1264
1265   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_TYPE,
1266       "application/sdp");
1267
1268   /* content base for some clients that might screw up creating the setup uri */
1269   str = gst_rtsp_url_get_request_uri (state->uri);
1270   str_len = strlen (str);
1271
1272   /* check for trailing '/' and append one */
1273   if (str[str_len - 1] != '/') {
1274     content_base = g_malloc (str_len + 2);
1275     memcpy (content_base, str, str_len);
1276     content_base[str_len] = '/';
1277     content_base[str_len + 1] = '\0';
1278     g_free (str);
1279   } else {
1280     content_base = str;
1281   }
1282
1283   GST_INFO ("adding content-base: %s", content_base);
1284
1285   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_CONTENT_BASE,
1286       content_base);
1287   g_free (content_base);
1288
1289   /* add SDP to the response body */
1290   str = gst_sdp_message_as_text (sdp);
1291   gst_rtsp_message_take_body (state->response, (guint8 *) str, strlen (str));
1292   gst_sdp_message_free (sdp);
1293
1294   send_response (client, state->session, state->response);
1295
1296   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1297       0, state);
1298
1299   return TRUE;
1300
1301   /* ERRORS */
1302 no_media:
1303   {
1304     /* error reply is already sent */
1305     return FALSE;
1306   }
1307 no_sdp:
1308   {
1309     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, state);
1310     g_object_unref (media);
1311     return FALSE;
1312   }
1313 }
1314
1315 static gboolean
1316 handle_options_request (GstRTSPClient * client, GstRTSPClientState * state)
1317 {
1318   GstRTSPMethod options;
1319   gchar *str;
1320
1321   options = GST_RTSP_DESCRIBE |
1322       GST_RTSP_OPTIONS |
1323       GST_RTSP_PAUSE |
1324       GST_RTSP_PLAY |
1325       GST_RTSP_SETUP |
1326       GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1327
1328   str = gst_rtsp_options_as_text (options);
1329
1330   gst_rtsp_message_init_response (state->response, GST_RTSP_STS_OK,
1331       gst_rtsp_status_as_text (GST_RTSP_STS_OK), state->request);
1332
1333   gst_rtsp_message_add_header (state->response, GST_RTSP_HDR_PUBLIC, str);
1334   g_free (str);
1335
1336   send_response (client, state->session, state->response);
1337
1338   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1339       0, state);
1340
1341   return TRUE;
1342 }
1343
1344 /* remove duplicate and trailing '/' */
1345 static void
1346 sanitize_uri (GstRTSPUrl * uri)
1347 {
1348   gint i, len;
1349   gchar *s, *d;
1350   gboolean have_slash, prev_slash;
1351
1352   s = d = uri->abspath;
1353   len = strlen (uri->abspath);
1354
1355   prev_slash = FALSE;
1356
1357   for (i = 0; i < len; i++) {
1358     have_slash = s[i] == '/';
1359     *d = s[i];
1360     if (!have_slash || !prev_slash)
1361       d++;
1362     prev_slash = have_slash;
1363   }
1364   len = d - uri->abspath;
1365   /* don't remove the first slash if that's the only thing left */
1366   if (len > 1 && *(d - 1) == '/')
1367     d--;
1368   *d = '\0';
1369 }
1370
1371 static void
1372 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1373 {
1374   GST_INFO ("client %p: session %p finished", client, session);
1375
1376   /* unlink all media managed in this session */
1377   client_unlink_session (client, session);
1378
1379   /* remove the session */
1380   if (!(client->sessions = g_list_remove (client->sessions, session))) {
1381     GST_INFO ("client %p: all sessions finalized, close the connection",
1382         client);
1383     close_connection (client);
1384   }
1385 }
1386
1387 static void
1388 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
1389 {
1390   GList *walk;
1391
1392   for (walk = client->sessions; walk; walk = g_list_next (walk)) {
1393     GstRTSPSession *msession = (GstRTSPSession *) walk->data;
1394
1395     /* we already know about this session */
1396     if (msession == session)
1397       return;
1398   }
1399
1400   GST_INFO ("watching session %p", session);
1401
1402   g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
1403       client);
1404   client->sessions = g_list_prepend (client->sessions, session);
1405
1406   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1407       session);
1408 }
1409
1410 static void
1411 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1412 {
1413   GstRTSPMethod method;
1414   const gchar *uristr;
1415   GstRTSPUrl *uri;
1416   GstRTSPVersion version;
1417   GstRTSPResult res;
1418   GstRTSPSession *session;
1419   GstRTSPClientState state = { NULL };
1420   GstRTSPMessage response = { 0 };
1421   gchar *sessid;
1422
1423   state.request = request;
1424   state.response = &response;
1425
1426   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1427     gst_rtsp_message_dump (request);
1428   }
1429
1430   GST_INFO ("client %p: received a request", client);
1431
1432   gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1433
1434   if (version != GST_RTSP_VERSION_1_0) {
1435     /* we can only handle 1.0 requests */
1436     send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1437         &state);
1438     return;
1439   }
1440   state.method = method;
1441
1442   /* we always try to parse the url first */
1443   if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK) {
1444     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1445     return;
1446   }
1447
1448   /* sanitize the uri */
1449   sanitize_uri (uri);
1450   state.uri = uri;
1451
1452   /* get the session if there is any */
1453   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1454   if (res == GST_RTSP_OK) {
1455     if (client->session_pool == NULL)
1456       goto no_pool;
1457
1458     /* we had a session in the request, find it again */
1459     if (!(session = gst_rtsp_session_pool_find (client->session_pool, sessid)))
1460       goto session_not_found;
1461
1462     /* we add the session to the client list of watched sessions. When a session
1463      * disappears because it times out, we will be notified. If all sessions are
1464      * gone, we will close the connection */
1465     client_watch_session (client, session);
1466   } else
1467     session = NULL;
1468
1469   state.session = session;
1470
1471   if (client->auth) {
1472     if (!gst_rtsp_auth_check (client->auth, client, 0, &state))
1473       goto not_authorized;
1474   }
1475
1476   /* now see what is asked and dispatch to a dedicated handler */
1477   switch (method) {
1478     case GST_RTSP_OPTIONS:
1479       handle_options_request (client, &state);
1480       break;
1481     case GST_RTSP_DESCRIBE:
1482       handle_describe_request (client, &state);
1483       break;
1484     case GST_RTSP_SETUP:
1485       handle_setup_request (client, &state);
1486       break;
1487     case GST_RTSP_PLAY:
1488       handle_play_request (client, &state);
1489       break;
1490     case GST_RTSP_PAUSE:
1491       handle_pause_request (client, &state);
1492       break;
1493     case GST_RTSP_TEARDOWN:
1494       handle_teardown_request (client, &state);
1495       break;
1496     case GST_RTSP_SET_PARAMETER:
1497       handle_set_param_request (client, &state);
1498       break;
1499     case GST_RTSP_GET_PARAMETER:
1500       handle_get_param_request (client, &state);
1501       break;
1502     case GST_RTSP_ANNOUNCE:
1503     case GST_RTSP_RECORD:
1504     case GST_RTSP_REDIRECT:
1505       send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &state);
1506       break;
1507     case GST_RTSP_INVALID:
1508     default:
1509       send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &state);
1510       break;
1511   }
1512   if (session)
1513     g_object_unref (session);
1514
1515   gst_rtsp_url_free (uri);
1516   return;
1517
1518   /* ERRORS */
1519 no_pool:
1520   {
1521     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, &state);
1522     return;
1523   }
1524 session_not_found:
1525   {
1526     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, &state);
1527     return;
1528   }
1529 not_authorized:
1530   {
1531     handle_unauthorized_request (client, client->auth, &state);
1532     return;
1533   }
1534 }
1535
1536 static void
1537 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
1538 {
1539   GstRTSPResult res;
1540   guint8 channel;
1541   GList *walk;
1542   guint8 *data;
1543   guint size;
1544   GstBuffer *buffer;
1545   gboolean handled;
1546
1547   /* find the stream for this message */
1548   res = gst_rtsp_message_parse_data (message, &channel);
1549   if (res != GST_RTSP_OK)
1550     return;
1551
1552   gst_rtsp_message_steal_body (message, &data, &size);
1553
1554   buffer = gst_buffer_new_wrapped (data, size);
1555
1556   handled = FALSE;
1557   for (walk = client->transports; walk; walk = g_list_next (walk)) {
1558     GstRTSPStreamTransport *trans = (GstRTSPStreamTransport *) walk->data;
1559     GstRTSPStream *stream;
1560     GstRTSPTransport *tr;
1561
1562     /* get the transport, if there is no transport configured, skip this stream */
1563     if (!(tr = trans->transport))
1564       continue;
1565
1566     /* we also need a media stream */
1567     if (!(stream = trans->stream))
1568       continue;
1569
1570     /* check for TCP transport */
1571     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1572       /* dispatch to the stream based on the channel number */
1573       if (tr->interleaved.min == channel) {
1574         gst_rtsp_stream_recv_rtp (stream, buffer);
1575         handled = TRUE;
1576         break;
1577       } else if (tr->interleaved.max == channel) {
1578         gst_rtsp_stream_recv_rtcp (stream, buffer);
1579         handled = TRUE;
1580         break;
1581       }
1582     }
1583   }
1584   if (!handled)
1585     gst_buffer_unref (buffer);
1586 }
1587
1588 /**
1589  * gst_rtsp_client_set_session_pool:
1590  * @client: a #GstRTSPClient
1591  * @pool: a #GstRTSPSessionPool
1592  *
1593  * Set @pool as the sessionpool for @client which it will use to find
1594  * or allocate sessions. the sessionpool is usually inherited from the server
1595  * that created the client but can be overridden later.
1596  */
1597 void
1598 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
1599     GstRTSPSessionPool * pool)
1600 {
1601   GstRTSPSessionPool *old;
1602
1603   old = client->session_pool;
1604   if (old != pool) {
1605     if (pool)
1606       g_object_ref (pool);
1607     client->session_pool = pool;
1608     if (old)
1609       g_object_unref (old);
1610   }
1611 }
1612
1613 /**
1614  * gst_rtsp_client_get_session_pool:
1615  * @client: a #GstRTSPClient
1616  *
1617  * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
1618  *
1619  * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
1620  */
1621 GstRTSPSessionPool *
1622 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
1623 {
1624   GstRTSPSessionPool *result;
1625
1626   if ((result = client->session_pool))
1627     g_object_ref (result);
1628
1629   return result;
1630 }
1631
1632 /**
1633  * gst_rtsp_client_set_server:
1634  * @client: a #GstRTSPClient
1635  * @server: a #GstRTSPServer
1636  *
1637  * Set @server as the server that created @client.
1638  */
1639 void
1640 gst_rtsp_client_set_server (GstRTSPClient * client, GstRTSPServer * server)
1641 {
1642   GstRTSPServer *old;
1643
1644   old = client->server;
1645   if (old != server) {
1646     if (server)
1647       g_object_ref (server);
1648     client->server = server;
1649     if (old)
1650       g_object_unref (old);
1651   }
1652 }
1653
1654 /**
1655  * gst_rtsp_client_get_server:
1656  * @client: a #GstRTSPClient
1657  *
1658  * Get the #GstRTSPServer object that @client was created from.
1659  *
1660  * Returns: (transfer full): a #GstRTSPServer, unref after usage.
1661  */
1662 GstRTSPServer *
1663 gst_rtsp_client_get_server (GstRTSPClient * client)
1664 {
1665   GstRTSPServer *result;
1666
1667   if ((result = client->server))
1668     g_object_ref (result);
1669
1670   return result;
1671 }
1672
1673 /**
1674  * gst_rtsp_client_set_media_mapping:
1675  * @client: a #GstRTSPClient
1676  * @mapping: a #GstRTSPMediaMapping
1677  *
1678  * Set @mapping as the media mapping for @client which it will use to map urls
1679  * to media streams. These mapping is usually inherited from the server that
1680  * created the client but can be overriden later.
1681  */
1682 void
1683 gst_rtsp_client_set_media_mapping (GstRTSPClient * client,
1684     GstRTSPMediaMapping * mapping)
1685 {
1686   GstRTSPMediaMapping *old;
1687
1688   old = client->media_mapping;
1689
1690   if (old != mapping) {
1691     if (mapping)
1692       g_object_ref (mapping);
1693     client->media_mapping = mapping;
1694     if (old)
1695       g_object_unref (old);
1696   }
1697 }
1698
1699 /**
1700  * gst_rtsp_client_get_media_mapping:
1701  * @client: a #GstRTSPClient
1702  *
1703  * Get the #GstRTSPMediaMapping object that @client uses to manage its sessions.
1704  *
1705  * Returns: (transfer full): a #GstRTSPMediaMapping, unref after usage.
1706  */
1707 GstRTSPMediaMapping *
1708 gst_rtsp_client_get_media_mapping (GstRTSPClient * client)
1709 {
1710   GstRTSPMediaMapping *result;
1711
1712   if ((result = client->media_mapping))
1713     g_object_ref (result);
1714
1715   return result;
1716 }
1717
1718 /**
1719  * gst_rtsp_client_set_use_client_settings:
1720  * @client: a #GstRTSPClient
1721  * @use_client_settings: whether to use client settings for multicast
1722  *
1723  * Use client transport settings (destination and ttl) for multicast.
1724  * When @use_client_settings is %FALSE, the server settings will be
1725  * used.
1726  */
1727 void
1728 gst_rtsp_client_set_use_client_settings (GstRTSPClient * client,
1729     gboolean use_client_settings)
1730 {
1731   client->use_client_settings = use_client_settings;
1732 }
1733
1734 /**
1735  * gst_rtsp_client_get_use_client_settings:
1736  * @client: a #GstRTSPClient
1737  *
1738  * Check if client transport settings (destination and ttl) for multicast
1739  * will be used.
1740  */
1741 gboolean
1742 gst_rtsp_client_get_use_client_settings (GstRTSPClient * client)
1743 {
1744   return client->use_client_settings;
1745 }
1746
1747 /**
1748  * gst_rtsp_client_set_auth:
1749  * @client: a #GstRTSPClient
1750  * @auth: a #GstRTSPAuth
1751  *
1752  * configure @auth to be used as the authentication manager of @client.
1753  */
1754 void
1755 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
1756 {
1757   GstRTSPAuth *old;
1758
1759   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
1760
1761   old = client->auth;
1762
1763   if (old != auth) {
1764     if (auth)
1765       g_object_ref (auth);
1766     client->auth = auth;
1767     if (old)
1768       g_object_unref (old);
1769   }
1770 }
1771
1772
1773 /**
1774  * gst_rtsp_client_get_auth:
1775  * @client: a #GstRTSPClient
1776  *
1777  * Get the #GstRTSPAuth used as the authentication manager of @client.
1778  *
1779  * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
1780  * usage.
1781  */
1782 GstRTSPAuth *
1783 gst_rtsp_client_get_auth (GstRTSPClient * client)
1784 {
1785   GstRTSPAuth *result;
1786
1787   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
1788
1789   if ((result = client->auth))
1790     g_object_ref (result);
1791
1792   return result;
1793 }
1794
1795 static GstRTSPResult
1796 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
1797     gpointer user_data)
1798 {
1799   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1800
1801   switch (message->type) {
1802     case GST_RTSP_MESSAGE_REQUEST:
1803       handle_request (client, message);
1804       break;
1805     case GST_RTSP_MESSAGE_RESPONSE:
1806       break;
1807     case GST_RTSP_MESSAGE_DATA:
1808       handle_data (client, message);
1809       break;
1810     default:
1811       break;
1812   }
1813   return GST_RTSP_OK;
1814 }
1815
1816 static GstRTSPResult
1817 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
1818 {
1819   /* GstRTSPClient *client; */
1820
1821   /* client = GST_RTSP_CLIENT (user_data); */
1822
1823   /* GST_INFO ("client %p: sent a message with cseq %d", client, cseq); */
1824
1825   return GST_RTSP_OK;
1826 }
1827
1828 static GstRTSPResult
1829 closed (GstRTSPWatch * watch, gpointer user_data)
1830 {
1831   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1832   const gchar *tunnelid;
1833
1834   GST_INFO ("client %p: connection closed", client);
1835
1836   if ((tunnelid = gst_rtsp_connection_get_tunnelid (client->connection))) {
1837     g_mutex_lock (&tunnels_lock);
1838     /* remove from tunnelids */
1839     g_hash_table_remove (tunnels, tunnelid);
1840     g_mutex_unlock (&tunnels_lock);
1841   }
1842
1843   return GST_RTSP_OK;
1844 }
1845
1846 static GstRTSPResult
1847 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
1848 {
1849   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1850   gchar *str;
1851
1852   str = gst_rtsp_strresult (result);
1853   GST_INFO ("client %p: received an error %s", client, str);
1854   g_free (str);
1855
1856   return GST_RTSP_OK;
1857 }
1858
1859 static GstRTSPResult
1860 error_full (GstRTSPWatch * watch, GstRTSPResult result,
1861     GstRTSPMessage * message, guint id, gpointer user_data)
1862 {
1863   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1864   gchar *str;
1865
1866   str = gst_rtsp_strresult (result);
1867   GST_INFO
1868       ("client %p: received an error %s when handling message %p with id %d",
1869       client, str, message, id);
1870   g_free (str);
1871
1872   return GST_RTSP_OK;
1873 }
1874
1875 static gboolean
1876 remember_tunnel (GstRTSPClient * client)
1877 {
1878   const gchar *tunnelid;
1879
1880   /* store client in the pending tunnels */
1881   tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1882   if (tunnelid == NULL)
1883     goto no_tunnelid;
1884
1885   GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
1886
1887   /* we can't have two clients connecting with the same tunnelid */
1888   g_mutex_lock (&tunnels_lock);
1889   if (g_hash_table_lookup (tunnels, tunnelid))
1890     goto tunnel_existed;
1891
1892   g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
1893   g_mutex_unlock (&tunnels_lock);
1894
1895   return TRUE;
1896
1897   /* ERRORS */
1898 no_tunnelid:
1899   {
1900     GST_ERROR ("client %p: no tunnelid provided", client);
1901     return FALSE;
1902   }
1903 tunnel_existed:
1904   {
1905     g_mutex_unlock (&tunnels_lock);
1906     GST_ERROR ("client %p: tunnel session %s already existed", client,
1907         tunnelid);
1908     return FALSE;
1909   }
1910 }
1911
1912 static GstRTSPStatusCode
1913 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
1914 {
1915   GstRTSPClient *client;
1916
1917   client = GST_RTSP_CLIENT (user_data);
1918
1919   GST_INFO ("client %p: tunnel start (connection %p)", client,
1920       client->connection);
1921
1922   if (!remember_tunnel (client))
1923     goto tunnel_error;
1924
1925   return GST_RTSP_STS_OK;
1926
1927   /* ERRORS */
1928 tunnel_error:
1929   {
1930     GST_ERROR ("client %p: error starting tunnel", client);
1931     return GST_RTSP_STS_SERVICE_UNAVAILABLE;
1932   }
1933 }
1934
1935 static GstRTSPResult
1936 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
1937 {
1938   GstRTSPClient *client;
1939
1940   client = GST_RTSP_CLIENT (user_data);
1941
1942   GST_INFO ("client %p: tunnel lost (connection %p)", client,
1943       client->connection);
1944
1945   /* ignore error, it'll only be a problem when the client does a POST again */
1946   remember_tunnel (client);
1947
1948   return GST_RTSP_OK;
1949 }
1950
1951 static GstRTSPResult
1952 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
1953 {
1954   const gchar *tunnelid;
1955   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
1956   GstRTSPClient *oclient;
1957
1958   GST_INFO ("client %p: tunnel complete", client);
1959
1960   /* find previous tunnel */
1961   tunnelid = gst_rtsp_connection_get_tunnelid (client->connection);
1962   if (tunnelid == NULL)
1963     goto no_tunnelid;
1964
1965   g_mutex_lock (&tunnels_lock);
1966   if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
1967     goto no_tunnel;
1968
1969   /* remove the old client from the table. ref before because removing it will
1970    * remove the ref to it. */
1971   g_object_ref (oclient);
1972   g_hash_table_remove (tunnels, tunnelid);
1973
1974   if (oclient->watch == NULL)
1975     goto tunnel_closed;
1976   g_mutex_unlock (&tunnels_lock);
1977
1978   GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
1979       oclient->connection, client->connection);
1980
1981   /* merge the tunnels into the first client */
1982   gst_rtsp_connection_do_tunnel (oclient->connection, client->connection);
1983   gst_rtsp_watch_reset (oclient->watch);
1984   g_object_unref (oclient);
1985
1986   return GST_RTSP_OK;
1987
1988   /* ERRORS */
1989 no_tunnelid:
1990   {
1991     GST_INFO ("client %p: no tunnelid provided", client);
1992     return GST_RTSP_ERROR;
1993   }
1994 no_tunnel:
1995   {
1996     g_mutex_unlock (&tunnels_lock);
1997     GST_INFO ("client %p: tunnel session %s not found", client, tunnelid);
1998     return GST_RTSP_ERROR;
1999   }
2000 tunnel_closed:
2001   {
2002     g_mutex_unlock (&tunnels_lock);
2003     GST_INFO ("client %p: tunnel session %s was closed", client, tunnelid);
2004     g_object_unref (oclient);
2005     return GST_RTSP_ERROR;
2006   }
2007 }
2008
2009 static GstRTSPWatchFuncs watch_funcs = {
2010   message_received,
2011   message_sent,
2012   closed,
2013   error,
2014   tunnel_start,
2015   tunnel_complete,
2016   error_full,
2017   tunnel_lost
2018 };
2019
2020 static void
2021 client_watch_notify (GstRTSPClient * client)
2022 {
2023   GST_INFO ("client %p: watch destroyed", client);
2024   client->watchid = 0;
2025   client->watch = NULL;
2026   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2027   g_object_unref (client);
2028 }
2029
2030 static gboolean
2031 attach_client (GstRTSPClient * client, GSocket * socket,
2032     GstRTSPConnection * conn, GError ** error)
2033 {
2034   GSocket *read_socket;
2035   GSocketAddress *address;
2036   GSource *source;
2037   GMainContext *context;
2038   GstRTSPUrl *url;
2039
2040   read_socket = gst_rtsp_connection_get_read_socket (conn);
2041   client->is_ipv6 = g_socket_get_family (socket) == G_SOCKET_FAMILY_IPV6;
2042
2043   if (!(address = g_socket_get_remote_address (read_socket, error)))
2044     goto no_address;
2045
2046   g_free (client->server_ip);
2047   /* keep the original ip that the client connected to */
2048   if (G_IS_INET_SOCKET_ADDRESS (address)) {
2049     GInetAddress *iaddr;
2050
2051     iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2052
2053     client->server_ip = g_inet_address_to_string (iaddr);
2054     g_object_unref (address);
2055   } else {
2056     client->server_ip = g_strdup ("unknown");
2057   }
2058
2059   GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2060       client->server_ip, client->is_ipv6);
2061
2062   url = gst_rtsp_connection_get_url (conn);
2063   GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2064
2065   client->connection = conn;
2066
2067   /* create watch for the connection and attach */
2068   client->watch = gst_rtsp_watch_new (client->connection, &watch_funcs,
2069       g_object_ref (client), (GDestroyNotify) client_watch_notify);
2070
2071   /* find the context to add the watch */
2072   if ((source = g_main_current_source ()))
2073     context = g_source_get_context (source);
2074   else
2075     context = NULL;
2076
2077   GST_INFO ("attaching to context %p", context);
2078
2079   client->watchid = gst_rtsp_watch_attach (client->watch, context);
2080   gst_rtsp_watch_unref (client->watch);
2081
2082   return TRUE;
2083
2084   /* ERRORS */
2085 no_address:
2086   {
2087     GST_ERROR ("could not get remote address %s", (*error)->message);
2088     return FALSE;
2089   }
2090 }
2091
2092 /**
2093  * gst_rtsp_client_create_from_socket:
2094  * @client: a #GstRTSPClient
2095  * @socket: a #GSocket
2096  * @ip: the IP address of the remote client
2097  * @port: the port used by the other end
2098  * @initial_buffer: any initial data that was already read from the socket
2099  * @error: a #GError
2100  *
2101  * Take an existing network socket and use it for an RTSP connection.
2102  *
2103  * Returns: %TRUE on success.
2104  */
2105 gboolean
2106 gst_rtsp_client_create_from_socket (GstRTSPClient * client, GSocket * socket,
2107     const gchar * ip, gint port, const gchar * initial_buffer, GError ** error)
2108 {
2109   GstRTSPConnection *conn;
2110   GstRTSPResult res;
2111
2112   GST_RTSP_CHECK (gst_rtsp_connection_create_from_socket (socket, ip, port,
2113           initial_buffer, &conn), no_connection);
2114
2115   return attach_client (client, socket, conn, error);
2116
2117   /* ERRORS */
2118 no_connection:
2119   {
2120     gchar *str = gst_rtsp_strresult (res);
2121
2122     GST_ERROR ("could not create connection from socket %p: %s", socket, str);
2123     g_free (str);
2124     return FALSE;
2125   }
2126 }
2127
2128 /**
2129  * gst_rtsp_client_accept:
2130  * @client: a #GstRTSPClient
2131  * @socket: a #GSocket
2132  * @cancellable: a #GCancellable
2133  * @error: a #GError
2134  *
2135  * Accept a new connection for @client on @socket.
2136  *
2137  * This function should be called when the client properties and urls are fully
2138  * configured and the client is ready to start.
2139  *
2140  * Returns: %TRUE if the client could be accepted.
2141  */
2142 gboolean
2143 gst_rtsp_client_accept (GstRTSPClient * client, GSocket * socket,
2144     GCancellable * cancellable, GError ** error)
2145 {
2146   GstRTSPConnection *conn;
2147   GstRTSPResult res;
2148
2149   /* a new client connected. */
2150   GST_RTSP_CHECK (gst_rtsp_connection_accept (socket, &conn, cancellable),
2151       accept_failed);
2152
2153   return attach_client (client, socket, conn, error);
2154
2155   /* ERRORS */
2156 accept_failed:
2157   {
2158     gchar *str = gst_rtsp_strresult (res);
2159
2160     GST_ERROR ("Could not accept client on server socket %p: %s", socket, str);
2161     g_free (str);
2162     return FALSE;
2163   }
2164 }