client: map url to path only in describe
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /**
20  * SECTION:rtsp-client
21  * @short_description: A client connection state
22  * @see_also: #GstRTSPServer, #GstRTSPThreadPool
23  *
24  * The client object handles the connection with a client for as long as a TCP
25  * connection is open.
26  *
27  * A #GstRTSPClient is created by #GstRTSPServer when a new connection is
28  * accepted and it inherits the #GstRTSPMountPoints, #GstRTSPSessionPool,
29  * #GstRTSPAuth and #GstRTSPThreadPool from the server.
30  *
31  * The client connection should be configured with the #GstRTSPConnection using
32  * gst_rtsp_client_set_connection() before it can be attached to a #GMainContext
33  * using gst_rtsp_client_attach(). From then on the client will handle requests
34  * on the connection.
35  *
36  * Use gst_rtsp_client_session_filter() to iterate or modify all the
37  * #GstRTSPSession objects managed by the client object.
38  *
39  * Last reviewed on 2013-07-11 (1.0.0)
40  */
41
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "rtsp-client.h"
46 #include "rtsp-sdp.h"
47 #include "rtsp-params.h"
48
49 #define GST_RTSP_CLIENT_GET_PRIVATE(obj)  \
50    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_CLIENT, GstRTSPClientPrivate))
51
52 /* locking order:
53  * send_lock, lock, tunnels_lock
54  */
55
56 struct _GstRTSPClientPrivate
57 {
58   GMutex lock;                  /* protects everything else */
59   GMutex send_lock;
60   GstRTSPConnection *connection;
61   GstRTSPWatch *watch;
62   guint close_seq;
63   gchar *server_ip;
64   gboolean is_ipv6;
65
66   GstRTSPClientSendFunc send_func;      /* protected by send_lock */
67   gpointer send_data;           /* protected by send_lock */
68   GDestroyNotify send_notify;   /* protected by send_lock */
69
70   GstRTSPSessionPool *session_pool;
71   GstRTSPMountPoints *mount_points;
72   GstRTSPAuth *auth;
73   GstRTSPThreadPool *thread_pool;
74
75   /* used to cache the media in the last requested DESCRIBE so that
76    * we can pick it up in the next SETUP immediately */
77   gchar *path;
78   GstRTSPMedia *media;
79
80   GList *transports;
81   GList *sessions;
82 };
83
84 static GMutex tunnels_lock;
85 static GHashTable *tunnels;     /* protected by tunnels_lock */
86
87 #define DEFAULT_SESSION_POOL            NULL
88 #define DEFAULT_MOUNT_POINTS            NULL
89
90 enum
91 {
92   PROP_0,
93   PROP_SESSION_POOL,
94   PROP_MOUNT_POINTS,
95   PROP_LAST
96 };
97
98 enum
99 {
100   SIGNAL_CLOSED,
101   SIGNAL_NEW_SESSION,
102   SIGNAL_OPTIONS_REQUEST,
103   SIGNAL_DESCRIBE_REQUEST,
104   SIGNAL_SETUP_REQUEST,
105   SIGNAL_PLAY_REQUEST,
106   SIGNAL_PAUSE_REQUEST,
107   SIGNAL_TEARDOWN_REQUEST,
108   SIGNAL_SET_PARAMETER_REQUEST,
109   SIGNAL_GET_PARAMETER_REQUEST,
110   SIGNAL_HANDLE_RESPONSE,
111   SIGNAL_LAST
112 };
113
114 GST_DEBUG_CATEGORY_STATIC (rtsp_client_debug);
115 #define GST_CAT_DEFAULT rtsp_client_debug
116
117 static guint gst_rtsp_client_signals[SIGNAL_LAST] = { 0 };
118
119 static void gst_rtsp_client_get_property (GObject * object, guint propid,
120     GValue * value, GParamSpec * pspec);
121 static void gst_rtsp_client_set_property (GObject * object, guint propid,
122     const GValue * value, GParamSpec * pspec);
123 static void gst_rtsp_client_finalize (GObject * obj);
124
125 static GstSDPMessage *create_sdp (GstRTSPClient * client, GstRTSPMedia * media);
126 static void client_session_finalized (GstRTSPClient * client,
127     GstRTSPSession * session);
128 static void unlink_session_transports (GstRTSPClient * client,
129     GstRTSPSession * session, GstRTSPSessionMedia * sessmedia);
130 static gboolean default_configure_client_transport (GstRTSPClient * client,
131     GstRTSPContext * ctx, GstRTSPTransport * ct);
132 static GstRTSPResult default_params_set (GstRTSPClient * client,
133     GstRTSPContext * ctx);
134 static GstRTSPResult default_params_get (GstRTSPClient * client,
135     GstRTSPContext * ctx);
136
137 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
138
139 static void
140 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
141 {
142   GObjectClass *gobject_class;
143
144   g_type_class_add_private (klass, sizeof (GstRTSPClientPrivate));
145
146   gobject_class = G_OBJECT_CLASS (klass);
147
148   gobject_class->get_property = gst_rtsp_client_get_property;
149   gobject_class->set_property = gst_rtsp_client_set_property;
150   gobject_class->finalize = gst_rtsp_client_finalize;
151
152   klass->create_sdp = create_sdp;
153   klass->configure_client_transport = default_configure_client_transport;
154   klass->params_set = default_params_set;
155   klass->params_get = default_params_get;
156
157   g_object_class_install_property (gobject_class, PROP_SESSION_POOL,
158       g_param_spec_object ("session-pool", "Session Pool",
159           "The session pool to use for client session",
160           GST_TYPE_RTSP_SESSION_POOL,
161           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
162
163   g_object_class_install_property (gobject_class, PROP_MOUNT_POINTS,
164       g_param_spec_object ("mount-points", "Mount Points",
165           "The mount points to use for client session",
166           GST_TYPE_RTSP_MOUNT_POINTS,
167           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168
169   gst_rtsp_client_signals[SIGNAL_CLOSED] =
170       g_signal_new ("closed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
171       G_STRUCT_OFFSET (GstRTSPClientClass, closed), NULL, NULL,
172       g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
173
174   gst_rtsp_client_signals[SIGNAL_NEW_SESSION] =
175       g_signal_new ("new-session", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
176       G_STRUCT_OFFSET (GstRTSPClientClass, new_session), NULL, NULL,
177       g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_RTSP_SESSION);
178
179   gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST] =
180       g_signal_new ("options-request", G_TYPE_FROM_CLASS (klass),
181       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, options_request),
182       NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
183       G_TYPE_POINTER);
184
185   gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST] =
186       g_signal_new ("describe-request", G_TYPE_FROM_CLASS (klass),
187       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, describe_request),
188       NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
189       G_TYPE_POINTER);
190
191   gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST] =
192       g_signal_new ("setup-request", G_TYPE_FROM_CLASS (klass),
193       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, setup_request),
194       NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
195       G_TYPE_POINTER);
196
197   gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST] =
198       g_signal_new ("play-request", G_TYPE_FROM_CLASS (klass),
199       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, play_request),
200       NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
201       G_TYPE_POINTER);
202
203   gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST] =
204       g_signal_new ("pause-request", G_TYPE_FROM_CLASS (klass),
205       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, pause_request),
206       NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
207       G_TYPE_POINTER);
208
209   gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST] =
210       g_signal_new ("teardown-request", G_TYPE_FROM_CLASS (klass),
211       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass, teardown_request),
212       NULL, NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
213       G_TYPE_POINTER);
214
215   gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST] =
216       g_signal_new ("set-parameter-request", G_TYPE_FROM_CLASS (klass),
217       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
218           set_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
219       G_TYPE_NONE, 1, G_TYPE_POINTER);
220
221   gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST] =
222       g_signal_new ("get-parameter-request", G_TYPE_FROM_CLASS (klass),
223       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
224           get_parameter_request), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
225       G_TYPE_NONE, 1, G_TYPE_POINTER);
226
227   gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE] =
228       g_signal_new ("handle-response", G_TYPE_FROM_CLASS (klass),
229       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPClientClass,
230           handle_response), NULL, NULL, g_cclosure_marshal_VOID__POINTER,
231       G_TYPE_NONE, 1, G_TYPE_POINTER);
232
233   tunnels =
234       g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
235   g_mutex_init (&tunnels_lock);
236
237   GST_DEBUG_CATEGORY_INIT (rtsp_client_debug, "rtspclient", 0, "GstRTSPClient");
238 }
239
240 static void
241 gst_rtsp_client_init (GstRTSPClient * client)
242 {
243   GstRTSPClientPrivate *priv = GST_RTSP_CLIENT_GET_PRIVATE (client);
244
245   client->priv = priv;
246
247   g_mutex_init (&priv->lock);
248   g_mutex_init (&priv->send_lock);
249   priv->close_seq = 0;
250 }
251
252 static GstRTSPFilterResult
253 filter_session (GstRTSPSession * sess, GstRTSPSessionMedia * sessmedia,
254     gpointer user_data)
255 {
256   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
257
258   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
259   unlink_session_transports (client, sess, sessmedia);
260
261   /* unmanage the media in the session */
262   return GST_RTSP_FILTER_REMOVE;
263 }
264
265 static void
266 client_unlink_session (GstRTSPClient * client, GstRTSPSession * session)
267 {
268   /* unlink all media managed in this session */
269   gst_rtsp_session_filter (session, filter_session, client);
270 }
271
272 static void
273 client_watch_session (GstRTSPClient * client, GstRTSPSession * session)
274 {
275   GstRTSPClientPrivate *priv = client->priv;
276   GList *walk;
277
278   for (walk = priv->sessions; walk; walk = g_list_next (walk)) {
279     GstRTSPSession *msession = (GstRTSPSession *) walk->data;
280
281     /* we already know about this session */
282     if (msession == session)
283       return;
284   }
285
286   GST_INFO ("watching session %p", session);
287
288   g_object_weak_ref (G_OBJECT (session), (GWeakNotify) client_session_finalized,
289       client);
290   priv->sessions = g_list_prepend (priv->sessions, session);
291 }
292
293 static void
294 client_unwatch_session (GstRTSPClient * client, GstRTSPSession * session)
295 {
296   GstRTSPClientPrivate *priv = client->priv;
297
298   GST_INFO ("unwatching session %p", session);
299
300   g_object_weak_unref (G_OBJECT (session),
301       (GWeakNotify) client_session_finalized, client);
302   priv->sessions = g_list_remove (priv->sessions, session);
303 }
304
305 static void
306 client_cleanup_session (GstRTSPClient * client, GstRTSPSession * session)
307 {
308   g_object_weak_unref (G_OBJECT (session),
309       (GWeakNotify) client_session_finalized, client);
310   client_unlink_session (client, session);
311 }
312
313 static void
314 client_cleanup_sessions (GstRTSPClient * client)
315 {
316   GstRTSPClientPrivate *priv = client->priv;
317   GList *sessions;
318
319   /* remove weak-ref from sessions */
320   for (sessions = priv->sessions; sessions; sessions = g_list_next (sessions)) {
321     client_cleanup_session (client, (GstRTSPSession *) sessions->data);
322   }
323   g_list_free (priv->sessions);
324   priv->sessions = NULL;
325 }
326
327 /* A client is finalized when the connection is broken */
328 static void
329 gst_rtsp_client_finalize (GObject * obj)
330 {
331   GstRTSPClient *client = GST_RTSP_CLIENT (obj);
332   GstRTSPClientPrivate *priv = client->priv;
333
334   GST_INFO ("finalize client %p", client);
335
336   gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
337
338   if (priv->watch)
339     g_source_destroy ((GSource *) priv->watch);
340
341   client_cleanup_sessions (client);
342
343   if (priv->connection)
344     gst_rtsp_connection_free (priv->connection);
345   if (priv->session_pool)
346     g_object_unref (priv->session_pool);
347   if (priv->mount_points)
348     g_object_unref (priv->mount_points);
349   if (priv->auth)
350     g_object_unref (priv->auth);
351   if (priv->thread_pool)
352     g_object_unref (priv->thread_pool);
353
354   if (priv->path)
355     g_free (priv->path);
356   if (priv->media) {
357     gst_rtsp_media_unprepare (priv->media);
358     g_object_unref (priv->media);
359   }
360
361   g_free (priv->server_ip);
362   g_mutex_clear (&priv->lock);
363   g_mutex_clear (&priv->send_lock);
364
365   G_OBJECT_CLASS (gst_rtsp_client_parent_class)->finalize (obj);
366 }
367
368 static void
369 gst_rtsp_client_get_property (GObject * object, guint propid,
370     GValue * value, GParamSpec * pspec)
371 {
372   GstRTSPClient *client = GST_RTSP_CLIENT (object);
373
374   switch (propid) {
375     case PROP_SESSION_POOL:
376       g_value_take_object (value, gst_rtsp_client_get_session_pool (client));
377       break;
378     case PROP_MOUNT_POINTS:
379       g_value_take_object (value, gst_rtsp_client_get_mount_points (client));
380       break;
381     default:
382       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
383   }
384 }
385
386 static void
387 gst_rtsp_client_set_property (GObject * object, guint propid,
388     const GValue * value, GParamSpec * pspec)
389 {
390   GstRTSPClient *client = GST_RTSP_CLIENT (object);
391
392   switch (propid) {
393     case PROP_SESSION_POOL:
394       gst_rtsp_client_set_session_pool (client, g_value_get_object (value));
395       break;
396     case PROP_MOUNT_POINTS:
397       gst_rtsp_client_set_mount_points (client, g_value_get_object (value));
398       break;
399     default:
400       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
401   }
402 }
403
404 /**
405  * gst_rtsp_client_new:
406  *
407  * Create a new #GstRTSPClient instance.
408  *
409  * Returns: a new #GstRTSPClient
410  */
411 GstRTSPClient *
412 gst_rtsp_client_new (void)
413 {
414   GstRTSPClient *result;
415
416   result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
417
418   return result;
419 }
420
421 static void
422 send_message (GstRTSPClient * client, GstRTSPSession * session,
423     GstRTSPMessage * message, gboolean close)
424 {
425   GstRTSPClientPrivate *priv = client->priv;
426
427   gst_rtsp_message_add_header (message, GST_RTSP_HDR_SERVER,
428       "GStreamer RTSP server");
429
430   /* remove any previous header */
431   gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
432
433   /* add the new session header for new session ids */
434   if (session) {
435     gst_rtsp_message_take_header (message, GST_RTSP_HDR_SESSION,
436         gst_rtsp_session_get_header (session));
437   }
438
439   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
440     gst_rtsp_message_dump (message);
441   }
442
443   if (close)
444     gst_rtsp_message_add_header (message, GST_RTSP_HDR_CONNECTION, "close");
445
446   g_mutex_lock (&priv->send_lock);
447   if (priv->send_func)
448     priv->send_func (client, message, close, priv->send_data);
449   g_mutex_unlock (&priv->send_lock);
450
451   gst_rtsp_message_unset (message);
452 }
453
454 static void
455 send_generic_response (GstRTSPClient * client, GstRTSPStatusCode code,
456     GstRTSPContext * ctx)
457 {
458   gst_rtsp_message_init_response (ctx->response, code,
459       gst_rtsp_status_as_text (code), ctx->request);
460
461   send_message (client, NULL, ctx->response, FALSE);
462 }
463
464 static gboolean
465 paths_are_equal (const gchar * path1, const gchar * path2, gint len2)
466 {
467   if (path1 == NULL || path2 == NULL)
468     return FALSE;
469
470   if (strlen (path1) != len2)
471     return FALSE;
472
473   if (strncmp (path1, path2, len2))
474     return FALSE;
475
476   return TRUE;
477 }
478
479 /* this function is called to initially find the media for the DESCRIBE request
480  * but is cached for when the same client (without breaking the connection) is
481  * doing a setup for the exact same url. */
482 static GstRTSPMedia *
483 find_media (GstRTSPClient * client, GstRTSPContext * ctx, gchar * path,
484     gint * matched)
485 {
486   GstRTSPClientPrivate *priv = client->priv;
487   GstRTSPMediaFactory *factory;
488   GstRTSPMedia *media;
489   gint path_len;
490
491   /* find the longest matching factory for the uri first */
492   if (!(factory = gst_rtsp_mount_points_match (priv->mount_points,
493               path, matched)))
494     goto no_factory;
495
496   ctx->factory = factory;
497
498   if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_ACCESS))
499     goto no_factory_access;
500
501   if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_MEDIA_FACTORY_CONSTRUCT))
502     goto not_authorized;
503
504   if (matched)
505     path_len = *matched;
506   else
507     path_len = strlen (path);
508
509   if (!paths_are_equal (priv->path, path, path_len)) {
510     GstRTSPThread *thread;
511
512     /* remove any previously cached values before we try to construct a new
513      * media for uri */
514     if (priv->path)
515       g_free (priv->path);
516     priv->path = NULL;
517     if (priv->media) {
518       gst_rtsp_media_unprepare (priv->media);
519       g_object_unref (priv->media);
520     }
521     priv->media = NULL;
522
523     /* prepare the media and add it to the pipeline */
524     if (!(media = gst_rtsp_media_factory_construct (factory, ctx->uri)))
525       goto no_media;
526
527     ctx->media = media;
528
529     thread = gst_rtsp_thread_pool_get_thread (priv->thread_pool,
530         GST_RTSP_THREAD_TYPE_MEDIA, ctx);
531     if (thread == NULL)
532       goto no_thread;
533
534     /* prepare the media */
535     if (!(gst_rtsp_media_prepare (media, thread)))
536       goto no_prepare;
537
538     /* now keep track of the uri and the media */
539     priv->path = g_strndup (path, path_len);
540     priv->media = media;
541   } else {
542     /* we have seen this path before, used cached media */
543     media = priv->media;
544     ctx->media = media;
545     GST_INFO ("reusing cached media %p for path %s", media, priv->path);
546   }
547
548   g_object_unref (factory);
549   ctx->factory = NULL;
550
551   if (media)
552     g_object_ref (media);
553
554   return media;
555
556   /* ERRORS */
557 no_factory:
558   {
559     GST_ERROR ("client %p: no factory for path %s", client, path);
560     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
561     return NULL;
562   }
563 no_factory_access:
564   {
565     GST_ERROR ("client %p: not authorized to see factory path %s", client,
566         path);
567     return NULL;
568   }
569 not_authorized:
570   {
571     GST_ERROR ("client %p: not authorized for factory path %s", client, path);
572     return NULL;
573   }
574 no_media:
575   {
576     GST_ERROR ("client %p: can't create media", client);
577     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
578     g_object_unref (factory);
579     ctx->factory = NULL;
580     return NULL;
581   }
582 no_thread:
583   {
584     GST_ERROR ("client %p: can't create thread", client);
585     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
586     g_object_unref (media);
587     ctx->media = NULL;
588     g_object_unref (factory);
589     ctx->factory = NULL;
590     return NULL;
591   }
592 no_prepare:
593   {
594     GST_ERROR ("client %p: can't prepare media", client);
595     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
596     g_object_unref (media);
597     ctx->media = NULL;
598     g_object_unref (factory);
599     ctx->factory = NULL;
600     return NULL;
601   }
602 }
603
604 static gboolean
605 do_send_data (GstBuffer * buffer, guint8 channel, GstRTSPClient * client)
606 {
607   GstRTSPClientPrivate *priv = client->priv;
608   GstRTSPMessage message = { 0 };
609   GstMapInfo map_info;
610   guint8 *data;
611   guint usize;
612
613   gst_rtsp_message_init_data (&message, channel);
614
615   /* FIXME, need some sort of iovec RTSPMessage here */
616   if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ))
617     return FALSE;
618
619   gst_rtsp_message_take_body (&message, map_info.data, map_info.size);
620
621   g_mutex_lock (&priv->send_lock);
622   if (priv->send_func)
623     priv->send_func (client, &message, FALSE, priv->send_data);
624   g_mutex_unlock (&priv->send_lock);
625
626   gst_rtsp_message_steal_body (&message, &data, &usize);
627   gst_buffer_unmap (buffer, &map_info);
628
629   gst_rtsp_message_unset (&message);
630
631   return TRUE;
632 }
633
634 static void
635 link_transport (GstRTSPClient * client, GstRTSPSession * session,
636     GstRTSPStreamTransport * trans)
637 {
638   GstRTSPClientPrivate *priv = client->priv;
639
640   GST_DEBUG ("client %p: linking transport %p", client, trans);
641
642   gst_rtsp_stream_transport_set_callbacks (trans,
643       (GstRTSPSendFunc) do_send_data,
644       (GstRTSPSendFunc) do_send_data, client, NULL);
645
646   priv->transports = g_list_prepend (priv->transports, trans);
647
648   /* make sure our session can't expire */
649   gst_rtsp_session_prevent_expire (session);
650 }
651
652 static void
653 unlink_transport (GstRTSPClient * client, GstRTSPSession * session,
654     GstRTSPStreamTransport * trans)
655 {
656   GstRTSPClientPrivate *priv = client->priv;
657
658   GST_DEBUG ("client %p: unlinking transport %p", client, trans);
659
660   gst_rtsp_stream_transport_set_callbacks (trans, NULL, NULL, NULL, NULL);
661
662   priv->transports = g_list_remove (priv->transports, trans);
663
664   /* our session can now expire */
665   gst_rtsp_session_allow_expire (session);
666 }
667
668 static void
669 unlink_session_transports (GstRTSPClient * client, GstRTSPSession * session,
670     GstRTSPSessionMedia * sessmedia)
671 {
672   guint n_streams, i;
673
674   n_streams =
675       gst_rtsp_media_n_streams (gst_rtsp_session_media_get_media (sessmedia));
676   for (i = 0; i < n_streams; i++) {
677     GstRTSPStreamTransport *trans;
678     const GstRTSPTransport *tr;
679
680     /* get the transport, if there is no transport configured, skip this stream */
681     trans = gst_rtsp_session_media_get_transport (sessmedia, i);
682     if (trans == NULL)
683       continue;
684
685     tr = gst_rtsp_stream_transport_get_transport (trans);
686
687     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
688       /* for TCP, unlink the stream from the TCP connection of the client */
689       unlink_transport (client, session, trans);
690     }
691   }
692 }
693
694 static void
695 close_connection (GstRTSPClient * client)
696 {
697   GstRTSPClientPrivate *priv = client->priv;
698   const gchar *tunnelid;
699
700   GST_DEBUG ("client %p: closing connection", client);
701
702   if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
703     g_mutex_lock (&tunnels_lock);
704     /* remove from tunnelids */
705     g_hash_table_remove (tunnels, tunnelid);
706     g_mutex_unlock (&tunnels_lock);
707   }
708
709   gst_rtsp_connection_close (priv->connection);
710 }
711
712 static gboolean
713 handle_teardown_request (GstRTSPClient * client, GstRTSPContext * ctx)
714 {
715   GstRTSPClientPrivate *priv = client->priv;
716   GstRTSPSession *session;
717   GstRTSPSessionMedia *sessmedia;
718   GstRTSPStatusCode code;
719   const gchar *path;
720   gint matched;
721
722   if (!ctx->session)
723     goto no_session;
724
725   session = ctx->session;
726
727   if (!ctx->uri)
728     goto no_uri;
729
730   path = ctx->uri->abspath;
731
732   /* get a handle to the configuration of the media in the session */
733   sessmedia = gst_rtsp_session_get_media (session, path, &matched);
734   if (!sessmedia)
735     goto not_found;
736
737   /* only aggregate control for now.. */
738   if (path[matched] != '\0')
739     goto no_aggregate;
740
741   ctx->sessmedia = sessmedia;
742
743   /* we emit the signal before closing the connection */
744   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_TEARDOWN_REQUEST],
745       0, ctx);
746
747   /* unlink the all TCP callbacks */
748   unlink_session_transports (client, session, sessmedia);
749
750   /* remove the session from the watched sessions */
751   client_unwatch_session (client, session);
752
753   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_NULL);
754
755   /* unmanage the media in the session, returns false if all media session
756    * are torn down. */
757   if (!gst_rtsp_session_release_media (session, sessmedia)) {
758     /* remove the session */
759     gst_rtsp_session_pool_remove (priv->session_pool, session);
760   }
761   /* construct the response now */
762   code = GST_RTSP_STS_OK;
763   gst_rtsp_message_init_response (ctx->response, code,
764       gst_rtsp_status_as_text (code), ctx->request);
765
766   send_message (client, session, ctx->response, TRUE);
767
768   return TRUE;
769
770   /* ERRORS */
771 no_session:
772   {
773     GST_ERROR ("client %p: no session", client);
774     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
775     return FALSE;
776   }
777 no_uri:
778   {
779     GST_ERROR ("client %p: no uri supplied", client);
780     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
781     return FALSE;
782   }
783 not_found:
784   {
785     GST_ERROR ("client %p: no media for uri", client);
786     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
787     return FALSE;
788   }
789 no_aggregate:
790   {
791     GST_ERROR ("client %p: no aggregate path %s", client, path);
792     send_generic_response (client,
793         GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
794     return FALSE;
795   }
796 }
797
798 static GstRTSPResult
799 default_params_set (GstRTSPClient * client, GstRTSPContext * ctx)
800 {
801   GstRTSPResult res;
802
803   res = gst_rtsp_params_set (client, ctx);
804
805   return res;
806 }
807
808 static GstRTSPResult
809 default_params_get (GstRTSPClient * client, GstRTSPContext * ctx)
810 {
811   GstRTSPResult res;
812
813   res = gst_rtsp_params_get (client, ctx);
814
815   return res;
816 }
817
818 static gboolean
819 handle_get_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
820 {
821   GstRTSPResult res;
822   guint8 *data;
823   guint size;
824
825   res = gst_rtsp_message_get_body (ctx->request, &data, &size);
826   if (res != GST_RTSP_OK)
827     goto bad_request;
828
829   if (size == 0) {
830     /* no body, keep-alive request */
831     send_generic_response (client, GST_RTSP_STS_OK, ctx);
832   } else {
833     /* there is a body, handle the params */
834     res = GST_RTSP_CLIENT_GET_CLASS (client)->params_get (client, ctx);
835     if (res != GST_RTSP_OK)
836       goto bad_request;
837
838     send_message (client, ctx->session, ctx->response, FALSE);
839   }
840
841   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_GET_PARAMETER_REQUEST],
842       0, ctx);
843
844   return TRUE;
845
846   /* ERRORS */
847 bad_request:
848   {
849     GST_ERROR ("client %p: bad request", client);
850     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
851     return FALSE;
852   }
853 }
854
855 static gboolean
856 handle_set_param_request (GstRTSPClient * client, GstRTSPContext * ctx)
857 {
858   GstRTSPResult res;
859   guint8 *data;
860   guint size;
861
862   res = gst_rtsp_message_get_body (ctx->request, &data, &size);
863   if (res != GST_RTSP_OK)
864     goto bad_request;
865
866   if (size == 0) {
867     /* no body, keep-alive request */
868     send_generic_response (client, GST_RTSP_STS_OK, ctx);
869   } else {
870     /* there is a body, handle the params */
871     res = GST_RTSP_CLIENT_GET_CLASS (client)->params_set (client, ctx);
872     if (res != GST_RTSP_OK)
873       goto bad_request;
874
875     send_message (client, ctx->session, ctx->response, FALSE);
876   }
877
878   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SET_PARAMETER_REQUEST],
879       0, ctx);
880
881   return TRUE;
882
883   /* ERRORS */
884 bad_request:
885   {
886     GST_ERROR ("client %p: bad request", client);
887     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
888     return FALSE;
889   }
890 }
891
892 static gboolean
893 handle_pause_request (GstRTSPClient * client, GstRTSPContext * ctx)
894 {
895   GstRTSPSession *session;
896   GstRTSPSessionMedia *sessmedia;
897   GstRTSPStatusCode code;
898   GstRTSPState rtspstate;
899   const gchar *path;
900   gint matched;
901
902   if (!(session = ctx->session))
903     goto no_session;
904
905   if (!ctx->uri)
906     goto no_uri;
907
908   path = ctx->uri->abspath;
909
910   /* get a handle to the configuration of the media in the session */
911   sessmedia = gst_rtsp_session_get_media (session, path, &matched);
912   if (!sessmedia)
913     goto not_found;
914
915   if (path[matched] != '\0')
916     goto no_aggregate;
917
918   ctx->sessmedia = sessmedia;
919
920   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
921   /* the session state must be playing or recording */
922   if (rtspstate != GST_RTSP_STATE_PLAYING &&
923       rtspstate != GST_RTSP_STATE_RECORDING)
924     goto invalid_state;
925
926   /* unlink the all TCP callbacks */
927   unlink_session_transports (client, session, sessmedia);
928
929   /* then pause sending */
930   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PAUSED);
931
932   /* construct the response now */
933   code = GST_RTSP_STS_OK;
934   gst_rtsp_message_init_response (ctx->response, code,
935       gst_rtsp_status_as_text (code), ctx->request);
936
937   send_message (client, session, ctx->response, FALSE);
938
939   /* the state is now READY */
940   gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
941
942   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PAUSE_REQUEST], 0, ctx);
943
944   return TRUE;
945
946   /* ERRORS */
947 no_session:
948   {
949     GST_ERROR ("client %p: no seesion", client);
950     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
951     return FALSE;
952   }
953 no_uri:
954   {
955     GST_ERROR ("client %p: no uri supplied", client);
956     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
957     return FALSE;
958   }
959 not_found:
960   {
961     GST_ERROR ("client %p: no media for uri", client);
962     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
963     return FALSE;
964   }
965 no_aggregate:
966   {
967     GST_ERROR ("client %p: no aggregate path %s", client, path);
968     send_generic_response (client,
969         GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
970     return FALSE;
971   }
972 invalid_state:
973   {
974     GST_ERROR ("client %p: not PLAYING or RECORDING", client);
975     send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
976         ctx);
977     return FALSE;
978   }
979 }
980
981 static gboolean
982 handle_play_request (GstRTSPClient * client, GstRTSPContext * ctx)
983 {
984   GstRTSPSession *session;
985   GstRTSPSessionMedia *sessmedia;
986   GstRTSPMedia *media;
987   GstRTSPStatusCode code;
988   GString *rtpinfo;
989   guint n_streams, i, infocount;
990   gchar *str;
991   GstRTSPTimeRange *range;
992   GstRTSPResult res;
993   GstRTSPState rtspstate;
994   GstRTSPRangeUnit unit = GST_RTSP_RANGE_NPT;
995   const gchar *path;
996   gint matched;
997
998   if (!(session = ctx->session))
999     goto no_session;
1000
1001   if (!ctx->uri)
1002     goto no_uri;
1003
1004   path = ctx->uri->abspath;
1005
1006   /* get a handle to the configuration of the media in the session */
1007   sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1008   if (!sessmedia)
1009     goto not_found;
1010
1011   if (path[matched] != '\0')
1012     goto no_aggregate;
1013
1014   ctx->sessmedia = sessmedia;
1015   ctx->media = media = gst_rtsp_session_media_get_media (sessmedia);
1016
1017   /* the session state must be playing or ready */
1018   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1019   if (rtspstate != GST_RTSP_STATE_PLAYING && rtspstate != GST_RTSP_STATE_READY)
1020     goto invalid_state;
1021
1022   /* parse the range header if we have one */
1023   res = gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_RANGE, &str, 0);
1024   if (res == GST_RTSP_OK) {
1025     if (gst_rtsp_range_parse (str, &range) == GST_RTSP_OK) {
1026       /* we have a range, seek to the position */
1027       unit = range->unit;
1028       gst_rtsp_media_seek (media, range);
1029       gst_rtsp_range_free (range);
1030     }
1031   }
1032
1033   /* grab RTPInfo from the payloaders now */
1034   rtpinfo = g_string_new ("");
1035
1036   n_streams = gst_rtsp_media_n_streams (media);
1037   for (i = 0, infocount = 0; i < n_streams; i++) {
1038     GstRTSPStreamTransport *trans;
1039     GstRTSPStream *stream;
1040     const GstRTSPTransport *tr;
1041     gchar *uristr;
1042     guint rtptime, seq;
1043
1044     /* get the transport, if there is no transport configured, skip this stream */
1045     trans = gst_rtsp_session_media_get_transport (sessmedia, i);
1046     if (trans == NULL) {
1047       GST_INFO ("stream %d is not configured", i);
1048       continue;
1049     }
1050     tr = gst_rtsp_stream_transport_get_transport (trans);
1051
1052     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
1053       /* for TCP, link the stream to the TCP connection of the client */
1054       link_transport (client, session, trans);
1055     }
1056
1057     stream = gst_rtsp_stream_transport_get_stream (trans);
1058     if (gst_rtsp_stream_get_rtpinfo (stream, &rtptime, &seq)) {
1059       if (infocount > 0)
1060         g_string_append (rtpinfo, ", ");
1061
1062       uristr = gst_rtsp_url_get_request_uri (ctx->uri);
1063       g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u",
1064           uristr, i, seq, rtptime);
1065       g_free (uristr);
1066
1067       infocount++;
1068     } else {
1069       GST_WARNING ("RTP-Info cannot be determined for stream %d", i);
1070     }
1071   }
1072
1073   /* construct the response now */
1074   code = GST_RTSP_STS_OK;
1075   gst_rtsp_message_init_response (ctx->response, code,
1076       gst_rtsp_status_as_text (code), ctx->request);
1077
1078   /* add the RTP-Info header */
1079   if (infocount > 0) {
1080     str = g_string_free (rtpinfo, FALSE);
1081     gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RTP_INFO, str);
1082   } else {
1083     g_string_free (rtpinfo, TRUE);
1084   }
1085
1086   /* add the range */
1087   str = gst_rtsp_media_get_range_string (media, TRUE, unit);
1088   if (str)
1089     gst_rtsp_message_take_header (ctx->response, GST_RTSP_HDR_RANGE, str);
1090
1091   send_message (client, session, ctx->response, FALSE);
1092
1093   /* start playing after sending the request */
1094   gst_rtsp_session_media_set_state (sessmedia, GST_STATE_PLAYING);
1095
1096   gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_PLAYING);
1097
1098   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_PLAY_REQUEST], 0, ctx);
1099
1100   return TRUE;
1101
1102   /* ERRORS */
1103 no_session:
1104   {
1105     GST_ERROR ("client %p: no session", client);
1106     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1107     return FALSE;
1108   }
1109 no_uri:
1110   {
1111     GST_ERROR ("client %p: no uri supplied", client);
1112     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1113     return FALSE;
1114   }
1115 not_found:
1116   {
1117     GST_ERROR ("client %p: media not found", client);
1118     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1119     return FALSE;
1120   }
1121 no_aggregate:
1122   {
1123     GST_ERROR ("client %p: no aggregate path %s", client, path);
1124     send_generic_response (client,
1125         GST_RTSP_STS_ONLY_AGGREGATE_OPERATION_ALLOWED, ctx);
1126     return FALSE;
1127   }
1128 invalid_state:
1129   {
1130     GST_ERROR ("client %p: not PLAYING or READY", client);
1131     send_generic_response (client, GST_RTSP_STS_METHOD_NOT_VALID_IN_THIS_STATE,
1132         ctx);
1133     return FALSE;
1134   }
1135 }
1136
1137 static void
1138 do_keepalive (GstRTSPSession * session)
1139 {
1140   GST_INFO ("keep session %p alive", session);
1141   gst_rtsp_session_touch (session);
1142 }
1143
1144 /* parse @transport and return a valid transport in @tr. only transports
1145  * from @supported are returned. Returns FALSE if no valid transport
1146  * was found. */
1147 static gboolean
1148 parse_transport (const char *transport, GstRTSPLowerTrans supported,
1149     GstRTSPTransport * tr)
1150 {
1151   gint i;
1152   gboolean res;
1153   gchar **transports;
1154
1155   res = FALSE;
1156   gst_rtsp_transport_init (tr);
1157
1158   GST_DEBUG ("parsing transports %s", transport);
1159
1160   transports = g_strsplit (transport, ",", 0);
1161
1162   /* loop through the transports, try to parse */
1163   for (i = 0; transports[i]; i++) {
1164     res = gst_rtsp_transport_parse (transports[i], tr);
1165     if (res != GST_RTSP_OK) {
1166       /* no valid transport, search some more */
1167       GST_WARNING ("could not parse transport %s", transports[i]);
1168       goto next;
1169     }
1170
1171     /* we have a transport, see if it's RTP/AVP */
1172     if (tr->trans != GST_RTSP_TRANS_RTP || tr->profile != GST_RTSP_PROFILE_AVP) {
1173       GST_WARNING ("invalid transport %s", transports[i]);
1174       goto next;
1175     }
1176
1177     if (!(tr->lower_transport & supported)) {
1178       GST_WARNING ("unsupported transport %s", transports[i]);
1179       goto next;
1180     }
1181
1182     /* we have a valid transport */
1183     GST_INFO ("found valid transport %s", transports[i]);
1184     res = TRUE;
1185     break;
1186
1187   next:
1188     gst_rtsp_transport_init (tr);
1189   }
1190   g_strfreev (transports);
1191
1192   return res;
1193 }
1194
1195 static gboolean
1196 handle_blocksize (GstRTSPMedia * media, GstRTSPStream * stream,
1197     GstRTSPMessage * request)
1198 {
1199   gchar *blocksize_str;
1200   gboolean ret = TRUE;
1201
1202   if (gst_rtsp_message_get_header (request, GST_RTSP_HDR_BLOCKSIZE,
1203           &blocksize_str, 0) == GST_RTSP_OK) {
1204     guint64 blocksize;
1205     gchar *end;
1206
1207     blocksize = g_ascii_strtoull (blocksize_str, &end, 10);
1208     if (end == blocksize_str) {
1209       GST_ERROR ("failed to parse blocksize");
1210       ret = FALSE;
1211     } else {
1212       /* we don't want to change the mtu when this media
1213        * can be shared because it impacts other clients */
1214       if (gst_rtsp_media_is_shared (media))
1215         return TRUE;
1216
1217       if (blocksize > G_MAXUINT)
1218         blocksize = G_MAXUINT;
1219       gst_rtsp_stream_set_mtu (stream, blocksize);
1220     }
1221   }
1222   return ret;
1223 }
1224
1225 static gboolean
1226 default_configure_client_transport (GstRTSPClient * client,
1227     GstRTSPContext * ctx, GstRTSPTransport * ct)
1228 {
1229   GstRTSPClientPrivate *priv = client->priv;
1230
1231   /* we have a valid transport now, set the destination of the client. */
1232   if (ct->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1233     gboolean use_client_settings;
1234
1235     use_client_settings =
1236         gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_TRANSPORT_CLIENT_SETTINGS);
1237
1238     if (ct->destination && use_client_settings) {
1239       GstRTSPAddress *addr;
1240
1241       addr = gst_rtsp_stream_reserve_address (ctx->stream, ct->destination,
1242           ct->port.min, ct->port.max - ct->port.min + 1, ct->ttl);
1243
1244       if (addr == NULL)
1245         goto no_address;
1246
1247       gst_rtsp_address_free (addr);
1248     } else {
1249       GstRTSPAddress *addr;
1250       GSocketFamily family;
1251
1252       family = priv->is_ipv6 ? G_SOCKET_FAMILY_IPV6 : G_SOCKET_FAMILY_IPV4;
1253
1254       addr = gst_rtsp_stream_get_multicast_address (ctx->stream, family);
1255       if (addr == NULL)
1256         goto no_address;
1257
1258       g_free (ct->destination);
1259       ct->destination = g_strdup (addr->address);
1260       ct->port.min = addr->port;
1261       ct->port.max = addr->port + addr->n_ports - 1;
1262       ct->ttl = addr->ttl;
1263
1264       gst_rtsp_address_free (addr);
1265     }
1266   } else {
1267     GstRTSPUrl *url;
1268
1269     url = gst_rtsp_connection_get_url (priv->connection);
1270     g_free (ct->destination);
1271     ct->destination = g_strdup (url->host);
1272
1273     if (ct->lower_transport & GST_RTSP_LOWER_TRANS_TCP) {
1274       /* check if the client selected channels for TCP */
1275       if (ct->interleaved.min == -1 || ct->interleaved.max == -1) {
1276         gst_rtsp_session_media_alloc_channels (ctx->sessmedia,
1277             &ct->interleaved);
1278       }
1279     }
1280   }
1281   return TRUE;
1282
1283   /* ERRORS */
1284 no_address:
1285   {
1286     GST_ERROR_OBJECT (client, "failed to acquire address for stream");
1287     return FALSE;
1288   }
1289 }
1290
1291 static GstRTSPTransport *
1292 make_server_transport (GstRTSPClient * client, GstRTSPContext * ctx,
1293     GstRTSPTransport * ct)
1294 {
1295   GstRTSPTransport *st;
1296   GInetAddress *addr;
1297   GSocketFamily family;
1298
1299   /* prepare the server transport */
1300   gst_rtsp_transport_new (&st);
1301
1302   st->trans = ct->trans;
1303   st->profile = ct->profile;
1304   st->lower_transport = ct->lower_transport;
1305
1306   addr = g_inet_address_new_from_string (ct->destination);
1307
1308   if (!addr) {
1309     GST_ERROR ("failed to get inet addr from client destination");
1310     family = G_SOCKET_FAMILY_IPV4;
1311   } else {
1312     family = g_inet_address_get_family (addr);
1313     g_object_unref (addr);
1314     addr = NULL;
1315   }
1316
1317   switch (st->lower_transport) {
1318     case GST_RTSP_LOWER_TRANS_UDP:
1319       st->client_port = ct->client_port;
1320       gst_rtsp_stream_get_server_port (ctx->stream, &st->server_port, family);
1321       break;
1322     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1323       st->port = ct->port;
1324       st->destination = g_strdup (ct->destination);
1325       st->ttl = ct->ttl;
1326       break;
1327     case GST_RTSP_LOWER_TRANS_TCP:
1328       st->interleaved = ct->interleaved;
1329     default:
1330       break;
1331   }
1332
1333   gst_rtsp_stream_get_ssrc (ctx->stream, &st->ssrc);
1334
1335   return st;
1336 }
1337
1338 static gboolean
1339 handle_setup_request (GstRTSPClient * client, GstRTSPContext * ctx)
1340 {
1341   GstRTSPClientPrivate *priv = client->priv;
1342   GstRTSPResult res;
1343   GstRTSPUrl *uri;
1344   gchar *transport;
1345   GstRTSPTransport *ct, *st;
1346   GstRTSPLowerTrans supported;
1347   GstRTSPStatusCode code;
1348   GstRTSPSession *session;
1349   GstRTSPStreamTransport *trans;
1350   gchar *trans_str;
1351   GstRTSPSessionMedia *sessmedia;
1352   GstRTSPMedia *media;
1353   GstRTSPStream *stream;
1354   GstRTSPState rtspstate;
1355   GstRTSPClientClass *klass;
1356   gchar *path, *control;
1357   gint matched;
1358
1359   if (!ctx->uri)
1360     goto no_uri;
1361
1362   uri = ctx->uri;
1363   path = uri->abspath;
1364
1365   /* parse the transport */
1366   res =
1367       gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_TRANSPORT,
1368       &transport, 0);
1369   if (res != GST_RTSP_OK)
1370     goto no_transport;
1371
1372   /* we create the session after parsing stuff so that we don't make
1373    * a session for malformed requests */
1374   if (priv->session_pool == NULL)
1375     goto no_pool;
1376
1377   session = ctx->session;
1378
1379   if (session) {
1380     g_object_ref (session);
1381     /* get a handle to the configuration of the media in the session, this can
1382      * return NULL if this is a new url to manage in this session. */
1383     sessmedia = gst_rtsp_session_get_media (session, path, &matched);
1384   } else {
1385     /* we need a new media configuration in this session */
1386     sessmedia = NULL;
1387   }
1388
1389   /* we have no session media, find one and manage it */
1390   if (sessmedia == NULL) {
1391     /* get a handle to the configuration of the media in the session */
1392     media = find_media (client, ctx, path, &matched);
1393   } else {
1394     if ((media = gst_rtsp_session_media_get_media (sessmedia)))
1395       g_object_ref (media);
1396   }
1397   /* no media, not found then */
1398   if (media == NULL)
1399     goto media_not_found;
1400
1401   /* path is what matched. We can modify the parsed uri in place */
1402   path[matched] = '\0';
1403   /* control is remainder */
1404   control = &path[matched + 1];
1405
1406   /* find the stream now using the control part */
1407   stream = gst_rtsp_media_find_stream (media, control);
1408   if (stream == NULL)
1409     goto stream_not_found;
1410
1411   /* now we have a uri identifying a valid media and stream */
1412   ctx->stream = stream;
1413   ctx->media = media;
1414
1415   if (session == NULL) {
1416     /* create a session if this fails we probably reached our session limit or
1417      * something. */
1418     if (!(session = gst_rtsp_session_pool_create (priv->session_pool)))
1419       goto service_unavailable;
1420
1421     /* make sure this client is closed when the session is closed */
1422     client_watch_session (client, session);
1423
1424     /* signal new session */
1425     g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_NEW_SESSION], 0,
1426         session);
1427
1428     ctx->session = session;
1429   }
1430
1431   if (sessmedia == NULL) {
1432     /* manage the media in our session now, if not done already  */
1433     sessmedia = gst_rtsp_session_manage_media (session, path, media);
1434     /* if we stil have no media, error */
1435     if (sessmedia == NULL)
1436       goto sessmedia_unavailable;
1437   } else {
1438     g_object_unref (media);
1439   }
1440
1441   ctx->sessmedia = sessmedia;
1442
1443   /* set blocksize on this stream */
1444   if (!handle_blocksize (media, stream, ctx->request))
1445     goto invalid_blocksize;
1446
1447   gst_rtsp_transport_new (&ct);
1448
1449   /* our supported transports */
1450   supported = gst_rtsp_stream_get_protocols (stream);
1451
1452   /* parse and find a usable supported transport */
1453   if (!parse_transport (transport, supported, ct))
1454     goto unsupported_transports;
1455
1456   /* update the client transport */
1457   klass = GST_RTSP_CLIENT_GET_CLASS (client);
1458   if (!klass->configure_client_transport (client, ctx, ct))
1459     goto unsupported_client_transport;
1460
1461   /* set in the session media transport */
1462   trans = gst_rtsp_session_media_set_transport (sessmedia, stream, ct);
1463
1464   /* configure keepalive for this transport */
1465   gst_rtsp_stream_transport_set_keepalive (trans,
1466       (GstRTSPKeepAliveFunc) do_keepalive, session, NULL);
1467
1468   /* create and serialize the server transport */
1469   st = make_server_transport (client, ctx, ct);
1470   trans_str = gst_rtsp_transport_as_text (st);
1471   gst_rtsp_transport_free (st);
1472
1473   /* construct the response now */
1474   code = GST_RTSP_STS_OK;
1475   gst_rtsp_message_init_response (ctx->response, code,
1476       gst_rtsp_status_as_text (code), ctx->request);
1477
1478   gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_TRANSPORT,
1479       trans_str);
1480   g_free (trans_str);
1481
1482   send_message (client, session, ctx->response, FALSE);
1483
1484   /* update the state */
1485   rtspstate = gst_rtsp_session_media_get_rtsp_state (sessmedia);
1486   switch (rtspstate) {
1487     case GST_RTSP_STATE_PLAYING:
1488     case GST_RTSP_STATE_RECORDING:
1489     case GST_RTSP_STATE_READY:
1490       /* no state change */
1491       break;
1492     default:
1493       gst_rtsp_session_media_set_rtsp_state (sessmedia, GST_RTSP_STATE_READY);
1494       break;
1495   }
1496   g_object_unref (session);
1497
1498   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_SETUP_REQUEST], 0, ctx);
1499
1500   return TRUE;
1501
1502   /* ERRORS */
1503 no_uri:
1504   {
1505     GST_ERROR ("client %p: no uri", client);
1506     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1507     return FALSE;
1508   }
1509 no_transport:
1510   {
1511     GST_ERROR ("client %p: no transport", client);
1512     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1513     return FALSE;
1514   }
1515 no_pool:
1516   {
1517     GST_ERROR ("client %p: no session pool configured", client);
1518     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1519     return FALSE;
1520   }
1521 media_not_found:
1522   {
1523     GST_ERROR ("client %p: media '%s' not found", client, path);
1524     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1525     return FALSE;
1526   }
1527 stream_not_found:
1528   {
1529     GST_ERROR ("client %p: stream '%s' not found", client, control);
1530     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1531     g_object_unref (media);
1532     return FALSE;
1533   }
1534 service_unavailable:
1535   {
1536     GST_ERROR ("client %p: can't create session", client);
1537     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1538     g_object_unref (media);
1539     return FALSE;
1540   }
1541 sessmedia_unavailable:
1542   {
1543     GST_ERROR ("client %p: can't create session media", client);
1544     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1545     g_object_unref (media);
1546     g_object_unref (session);
1547     return FALSE;
1548   }
1549 invalid_blocksize:
1550   {
1551     GST_ERROR ("client %p: invalid blocksize", client);
1552     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1553     g_object_unref (session);
1554     return FALSE;
1555   }
1556 unsupported_transports:
1557   {
1558     GST_ERROR ("client %p: unsupported transports", client);
1559     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1560     gst_rtsp_transport_free (ct);
1561     g_object_unref (session);
1562     return FALSE;
1563   }
1564 unsupported_client_transport:
1565   {
1566     GST_ERROR ("client %p: unsupported client transport", client);
1567     send_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, ctx);
1568     gst_rtsp_transport_free (ct);
1569     g_object_unref (session);
1570     return FALSE;
1571   }
1572 }
1573
1574 static GstSDPMessage *
1575 create_sdp (GstRTSPClient * client, GstRTSPMedia * media)
1576 {
1577   GstRTSPClientPrivate *priv = client->priv;
1578   GstSDPMessage *sdp;
1579   GstSDPInfo info;
1580   const gchar *proto;
1581
1582   gst_sdp_message_new (&sdp);
1583
1584   /* some standard things first */
1585   gst_sdp_message_set_version (sdp, "0");
1586
1587   if (priv->is_ipv6)
1588     proto = "IP6";
1589   else
1590     proto = "IP4";
1591
1592   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", proto,
1593       priv->server_ip);
1594
1595   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
1596   gst_sdp_message_set_information (sdp, "rtsp-server");
1597   gst_sdp_message_add_time (sdp, "0", "0", NULL);
1598   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
1599   gst_sdp_message_add_attribute (sdp, "type", "broadcast");
1600   gst_sdp_message_add_attribute (sdp, "control", "*");
1601
1602   info.is_ipv6 = priv->is_ipv6;
1603   info.server_ip = priv->server_ip;
1604
1605   /* create an SDP for the media object */
1606   if (!gst_rtsp_sdp_from_media (sdp, &info, media))
1607     goto no_sdp;
1608
1609   return sdp;
1610
1611   /* ERRORS */
1612 no_sdp:
1613   {
1614     GST_ERROR ("client %p: could not create SDP", client);
1615     gst_sdp_message_free (sdp);
1616     return NULL;
1617   }
1618 }
1619
1620 /* for the describe we must generate an SDP */
1621 static gboolean
1622 handle_describe_request (GstRTSPClient * client, GstRTSPContext * ctx)
1623 {
1624   GstRTSPClientPrivate *priv = client->priv;
1625   GstRTSPResult res;
1626   GstSDPMessage *sdp;
1627   guint i, str_len;
1628   gchar *path, *str, *str_query, *content_base;
1629   GstRTSPMedia *media;
1630   GstRTSPClientClass *klass;
1631
1632   klass = GST_RTSP_CLIENT_GET_CLASS (client);
1633
1634   if (!ctx->uri)
1635     goto no_uri;
1636
1637   /* check what kind of format is accepted, we don't really do anything with it
1638    * and always return SDP for now. */
1639   for (i = 0; i++;) {
1640     gchar *accept;
1641
1642     res =
1643         gst_rtsp_message_get_header (ctx->request, GST_RTSP_HDR_ACCEPT,
1644         &accept, i);
1645     if (res == GST_RTSP_ENOTIMPL)
1646       break;
1647
1648     if (g_ascii_strcasecmp (accept, "application/sdp") == 0)
1649       break;
1650   }
1651
1652   if (!priv->mount_points)
1653     goto no_mount_points;
1654
1655   if (!(path = gst_rtsp_mount_points_make_path (priv->mount_points, ctx->uri)))
1656     goto no_path;
1657
1658   /* find the media object for the uri */
1659   if (!(media = find_media (client, ctx, path, NULL)))
1660     goto no_media;
1661
1662   g_free (path);
1663
1664   /* create an SDP for the media object on this client */
1665   if (!(sdp = klass->create_sdp (client, media)))
1666     goto no_sdp;
1667
1668   g_object_unref (media);
1669
1670   gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1671       gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1672
1673   gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_TYPE,
1674       "application/sdp");
1675
1676   /* content base for some clients that might screw up creating the setup uri */
1677   str = gst_rtsp_url_get_request_uri (ctx->uri);
1678   str_len = strlen (str);
1679
1680   /* check for query part */
1681   if (ctx->uri->query != NULL) {
1682     str_query = g_strrstr (str, "?");
1683     *str_query = '\0';
1684     str_len = strlen (str);
1685   }
1686
1687   /* check for trailing '/' and append one */
1688   if (str[str_len - 1] != '/') {
1689     content_base = g_malloc (str_len + 2);
1690     memcpy (content_base, str, str_len);
1691     content_base[str_len] = '/';
1692     content_base[str_len + 1] = '\0';
1693     g_free (str);
1694   } else {
1695     content_base = str;
1696   }
1697
1698   GST_INFO ("adding content-base: %s", content_base);
1699
1700   gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_CONTENT_BASE,
1701       content_base);
1702   g_free (content_base);
1703
1704   /* add SDP to the response body */
1705   str = gst_sdp_message_as_text (sdp);
1706   gst_rtsp_message_take_body (ctx->response, (guint8 *) str, strlen (str));
1707   gst_sdp_message_free (sdp);
1708
1709   send_message (client, ctx->session, ctx->response, FALSE);
1710
1711   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_DESCRIBE_REQUEST],
1712       0, ctx);
1713
1714   return TRUE;
1715
1716   /* ERRORS */
1717 no_uri:
1718   {
1719     GST_ERROR ("client %p: no uri", client);
1720     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1721     return FALSE;
1722   }
1723 no_mount_points:
1724   {
1725     GST_ERROR ("client %p: no mount points configured", client);
1726     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1727     return FALSE;
1728   }
1729 no_path:
1730   {
1731     GST_ERROR ("client %p: can't find path for url", client);
1732     send_generic_response (client, GST_RTSP_STS_NOT_FOUND, ctx);
1733     return FALSE;
1734   }
1735 no_media:
1736   {
1737     GST_ERROR ("client %p: no media", client);
1738     g_free (path);
1739     /* error reply is already sent */
1740     return FALSE;
1741   }
1742 no_sdp:
1743   {
1744     GST_ERROR ("client %p: can't create SDP", client);
1745     send_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, ctx);
1746     g_object_unref (media);
1747     return FALSE;
1748   }
1749 }
1750
1751 static gboolean
1752 handle_options_request (GstRTSPClient * client, GstRTSPContext * ctx)
1753 {
1754   GstRTSPMethod options;
1755   gchar *str;
1756
1757   options = GST_RTSP_DESCRIBE |
1758       GST_RTSP_OPTIONS |
1759       GST_RTSP_PAUSE |
1760       GST_RTSP_PLAY |
1761       GST_RTSP_SETUP |
1762       GST_RTSP_GET_PARAMETER | GST_RTSP_SET_PARAMETER | GST_RTSP_TEARDOWN;
1763
1764   str = gst_rtsp_options_as_text (options);
1765
1766   gst_rtsp_message_init_response (ctx->response, GST_RTSP_STS_OK,
1767       gst_rtsp_status_as_text (GST_RTSP_STS_OK), ctx->request);
1768
1769   gst_rtsp_message_add_header (ctx->response, GST_RTSP_HDR_PUBLIC, str);
1770   g_free (str);
1771
1772   send_message (client, ctx->session, ctx->response, FALSE);
1773
1774   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_OPTIONS_REQUEST],
1775       0, ctx);
1776
1777   return TRUE;
1778 }
1779
1780 /* remove duplicate and trailing '/' */
1781 static void
1782 sanitize_uri (GstRTSPUrl * uri)
1783 {
1784   gint i, len;
1785   gchar *s, *d;
1786   gboolean have_slash, prev_slash;
1787
1788   s = d = uri->abspath;
1789   len = strlen (uri->abspath);
1790
1791   prev_slash = FALSE;
1792
1793   for (i = 0; i < len; i++) {
1794     have_slash = s[i] == '/';
1795     *d = s[i];
1796     if (!have_slash || !prev_slash)
1797       d++;
1798     prev_slash = have_slash;
1799   }
1800   len = d - uri->abspath;
1801   /* don't remove the first slash if that's the only thing left */
1802   if (len > 1 && *(d - 1) == '/')
1803     d--;
1804   *d = '\0';
1805 }
1806
1807 static void
1808 client_session_finalized (GstRTSPClient * client, GstRTSPSession * session)
1809 {
1810   GstRTSPClientPrivate *priv = client->priv;
1811
1812   GST_INFO ("client %p: session %p finished", client, session);
1813
1814   /* unlink all media managed in this session */
1815   client_unlink_session (client, session);
1816
1817   /* remove the session */
1818   if (!(priv->sessions = g_list_remove (priv->sessions, session))) {
1819     GST_INFO ("client %p: all sessions finalized, close the connection",
1820         client);
1821     close_connection (client);
1822   }
1823 }
1824
1825 static void
1826 handle_request (GstRTSPClient * client, GstRTSPMessage * request)
1827 {
1828   GstRTSPClientPrivate *priv = client->priv;
1829   GstRTSPMethod method;
1830   const gchar *uristr;
1831   GstRTSPUrl *uri = NULL;
1832   GstRTSPVersion version;
1833   GstRTSPResult res;
1834   GstRTSPSession *session = NULL;
1835   GstRTSPContext sctx = { NULL }, *ctx;
1836   GstRTSPMessage response = { 0 };
1837   gchar *sessid;
1838
1839   if (!(ctx = gst_rtsp_context_get_current ())) {
1840     ctx = &sctx;
1841     ctx->auth = priv->auth;
1842     gst_rtsp_context_push_current (ctx);
1843   }
1844
1845   ctx->conn = priv->connection;
1846   ctx->client = client;
1847   ctx->request = request;
1848   ctx->response = &response;
1849
1850   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
1851     gst_rtsp_message_dump (request);
1852   }
1853
1854   GST_INFO ("client %p: received a request", client);
1855
1856   gst_rtsp_message_parse_request (request, &method, &uristr, &version);
1857
1858   /* we can only handle 1.0 requests */
1859   if (version != GST_RTSP_VERSION_1_0)
1860     goto not_supported;
1861
1862   ctx->method = method;
1863
1864   /* we always try to parse the url first */
1865   if (strcmp (uristr, "*") == 0) {
1866     /* special case where we have * as uri, keep uri = NULL */
1867   } else if (gst_rtsp_url_parse (uristr, &uri) != GST_RTSP_OK)
1868     goto bad_request;
1869
1870   /* get the session if there is any */
1871   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
1872   if (res == GST_RTSP_OK) {
1873     if (priv->session_pool == NULL)
1874       goto no_pool;
1875
1876     /* we had a session in the request, find it again */
1877     if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
1878       goto session_not_found;
1879
1880     /* we add the session to the client list of watched sessions. When a session
1881      * disappears because it times out, we will be notified. If all sessions are
1882      * gone, we will close the connection */
1883     client_watch_session (client, session);
1884   }
1885
1886   /* sanitize the uri */
1887   if (uri)
1888     sanitize_uri (uri);
1889   ctx->uri = uri;
1890   ctx->session = session;
1891
1892   if (!gst_rtsp_auth_check (GST_RTSP_AUTH_CHECK_URL))
1893     goto not_authorized;
1894
1895   /* now see what is asked and dispatch to a dedicated handler */
1896   switch (method) {
1897     case GST_RTSP_OPTIONS:
1898       handle_options_request (client, ctx);
1899       break;
1900     case GST_RTSP_DESCRIBE:
1901       handle_describe_request (client, ctx);
1902       break;
1903     case GST_RTSP_SETUP:
1904       handle_setup_request (client, ctx);
1905       break;
1906     case GST_RTSP_PLAY:
1907       handle_play_request (client, ctx);
1908       break;
1909     case GST_RTSP_PAUSE:
1910       handle_pause_request (client, ctx);
1911       break;
1912     case GST_RTSP_TEARDOWN:
1913       handle_teardown_request (client, ctx);
1914       break;
1915     case GST_RTSP_SET_PARAMETER:
1916       handle_set_param_request (client, ctx);
1917       break;
1918     case GST_RTSP_GET_PARAMETER:
1919       handle_get_param_request (client, ctx);
1920       break;
1921     case GST_RTSP_ANNOUNCE:
1922     case GST_RTSP_RECORD:
1923     case GST_RTSP_REDIRECT:
1924       goto not_implemented;
1925     case GST_RTSP_INVALID:
1926     default:
1927       goto bad_request;
1928   }
1929
1930 done:
1931   if (ctx == &sctx)
1932     gst_rtsp_context_pop_current (ctx);
1933   if (session)
1934     g_object_unref (session);
1935   if (uri)
1936     gst_rtsp_url_free (uri);
1937   return;
1938
1939   /* ERRORS */
1940 not_supported:
1941   {
1942     GST_ERROR ("client %p: version %d not supported", client, version);
1943     send_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED,
1944         ctx);
1945     goto done;
1946   }
1947 bad_request:
1948   {
1949     GST_ERROR ("client %p: bad request", client);
1950     send_generic_response (client, GST_RTSP_STS_BAD_REQUEST, ctx);
1951     goto done;
1952   }
1953 no_pool:
1954   {
1955     GST_ERROR ("client %p: no pool configured", client);
1956     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1957     goto done;
1958   }
1959 session_not_found:
1960   {
1961     GST_ERROR ("client %p: session not found", client);
1962     send_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, ctx);
1963     goto done;
1964   }
1965 not_authorized:
1966   {
1967     GST_ERROR ("client %p: not allowed", client);
1968     goto done;
1969   }
1970 not_implemented:
1971   {
1972     GST_ERROR ("client %p: method %d not implemented", client, method);
1973     send_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, ctx);
1974     goto done;
1975   }
1976 }
1977
1978
1979 static void
1980 handle_response (GstRTSPClient * client, GstRTSPMessage * response)
1981 {
1982   GstRTSPClientPrivate *priv = client->priv;
1983   GstRTSPResult res;
1984   GstRTSPSession *session = NULL;
1985   GstRTSPContext sctx = { NULL }, *ctx;
1986   gchar *sessid;
1987
1988   if (!(ctx = gst_rtsp_context_get_current ())) {
1989     ctx = &sctx;
1990     ctx->auth = priv->auth;
1991     gst_rtsp_context_push_current (ctx);
1992   }
1993
1994   ctx->conn = priv->connection;
1995   ctx->client = client;
1996   ctx->request = NULL;
1997   ctx->uri = NULL;
1998   ctx->method = GST_RTSP_INVALID;
1999   ctx->response = response;
2000
2001   if (gst_debug_category_get_threshold (rtsp_client_debug) >= GST_LEVEL_LOG) {
2002     gst_rtsp_message_dump (response);
2003   }
2004
2005   GST_INFO ("client %p: received a response", client);
2006
2007   /* get the session if there is any */
2008   res =
2009       gst_rtsp_message_get_header (response, GST_RTSP_HDR_SESSION, &sessid, 0);
2010   if (res == GST_RTSP_OK) {
2011     if (priv->session_pool == NULL)
2012       goto no_pool;
2013
2014     /* we had a session in the request, find it again */
2015     if (!(session = gst_rtsp_session_pool_find (priv->session_pool, sessid)))
2016       goto session_not_found;
2017
2018     /* we add the session to the client list of watched sessions. When a session
2019      * disappears because it times out, we will be notified. If all sessions are
2020      * gone, we will close the connection */
2021     client_watch_session (client, session);
2022   }
2023
2024   ctx->session = session;
2025
2026   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_HANDLE_RESPONSE],
2027       0, ctx);
2028
2029 done:
2030   if (ctx == &sctx)
2031     gst_rtsp_context_pop_current (ctx);
2032   if (session)
2033     g_object_unref (session);
2034   return;
2035
2036 no_pool:
2037   {
2038     GST_ERROR ("client %p: no pool configured", client);
2039     goto done;
2040   }
2041 session_not_found:
2042   {
2043     GST_ERROR ("client %p: session not found", client);
2044     goto done;
2045   }
2046 }
2047
2048 static void
2049 handle_data (GstRTSPClient * client, GstRTSPMessage * message)
2050 {
2051   GstRTSPClientPrivate *priv = client->priv;
2052   GstRTSPResult res;
2053   guint8 channel;
2054   GList *walk;
2055   guint8 *data;
2056   guint size;
2057   GstBuffer *buffer;
2058   gboolean handled;
2059
2060   /* find the stream for this message */
2061   res = gst_rtsp_message_parse_data (message, &channel);
2062   if (res != GST_RTSP_OK)
2063     return;
2064
2065   gst_rtsp_message_steal_body (message, &data, &size);
2066
2067   buffer = gst_buffer_new_wrapped (data, size);
2068
2069   handled = FALSE;
2070   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2071     GstRTSPStreamTransport *trans;
2072     GstRTSPStream *stream;
2073     const GstRTSPTransport *tr;
2074
2075     trans = walk->data;
2076
2077     tr = gst_rtsp_stream_transport_get_transport (trans);
2078     stream = gst_rtsp_stream_transport_get_stream (trans);
2079
2080     /* check for TCP transport */
2081     if (tr->lower_transport == GST_RTSP_LOWER_TRANS_TCP) {
2082       /* dispatch to the stream based on the channel number */
2083       if (tr->interleaved.min == channel) {
2084         gst_rtsp_stream_recv_rtp (stream, buffer);
2085         handled = TRUE;
2086         break;
2087       } else if (tr->interleaved.max == channel) {
2088         gst_rtsp_stream_recv_rtcp (stream, buffer);
2089         handled = TRUE;
2090         break;
2091       }
2092     }
2093   }
2094   if (!handled)
2095     gst_buffer_unref (buffer);
2096 }
2097
2098 /**
2099  * gst_rtsp_client_set_session_pool:
2100  * @client: a #GstRTSPClient
2101  * @pool: a #GstRTSPSessionPool
2102  *
2103  * Set @pool as the sessionpool for @client which it will use to find
2104  * or allocate sessions. the sessionpool is usually inherited from the server
2105  * that created the client but can be overridden later.
2106  */
2107 void
2108 gst_rtsp_client_set_session_pool (GstRTSPClient * client,
2109     GstRTSPSessionPool * pool)
2110 {
2111   GstRTSPSessionPool *old;
2112   GstRTSPClientPrivate *priv;
2113
2114   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2115
2116   priv = client->priv;
2117
2118   if (pool)
2119     g_object_ref (pool);
2120
2121   g_mutex_lock (&priv->lock);
2122   old = priv->session_pool;
2123   priv->session_pool = pool;
2124   g_mutex_unlock (&priv->lock);
2125
2126   if (old)
2127     g_object_unref (old);
2128 }
2129
2130 /**
2131  * gst_rtsp_client_get_session_pool:
2132  * @client: a #GstRTSPClient
2133  *
2134  * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
2135  *
2136  * Returns: (transfer full): a #GstRTSPSessionPool, unref after usage.
2137  */
2138 GstRTSPSessionPool *
2139 gst_rtsp_client_get_session_pool (GstRTSPClient * client)
2140 {
2141   GstRTSPClientPrivate *priv;
2142   GstRTSPSessionPool *result;
2143
2144   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2145
2146   priv = client->priv;
2147
2148   g_mutex_lock (&priv->lock);
2149   if ((result = priv->session_pool))
2150     g_object_ref (result);
2151   g_mutex_unlock (&priv->lock);
2152
2153   return result;
2154 }
2155
2156 /**
2157  * gst_rtsp_client_set_mount_points:
2158  * @client: a #GstRTSPClient
2159  * @mounts: a #GstRTSPMountPoints
2160  *
2161  * Set @mounts as the mount points for @client which it will use to map urls
2162  * to media streams. These mount points are usually inherited from the server that
2163  * created the client but can be overriden later.
2164  */
2165 void
2166 gst_rtsp_client_set_mount_points (GstRTSPClient * client,
2167     GstRTSPMountPoints * mounts)
2168 {
2169   GstRTSPClientPrivate *priv;
2170   GstRTSPMountPoints *old;
2171
2172   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2173
2174   priv = client->priv;
2175
2176   if (mounts)
2177     g_object_ref (mounts);
2178
2179   g_mutex_lock (&priv->lock);
2180   old = priv->mount_points;
2181   priv->mount_points = mounts;
2182   g_mutex_unlock (&priv->lock);
2183
2184   if (old)
2185     g_object_unref (old);
2186 }
2187
2188 /**
2189  * gst_rtsp_client_get_mount_points:
2190  * @client: a #GstRTSPClient
2191  *
2192  * Get the #GstRTSPMountPoints object that @client uses to manage its sessions.
2193  *
2194  * Returns: (transfer full): a #GstRTSPMountPoints, unref after usage.
2195  */
2196 GstRTSPMountPoints *
2197 gst_rtsp_client_get_mount_points (GstRTSPClient * client)
2198 {
2199   GstRTSPClientPrivate *priv;
2200   GstRTSPMountPoints *result;
2201
2202   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2203
2204   priv = client->priv;
2205
2206   g_mutex_lock (&priv->lock);
2207   if ((result = priv->mount_points))
2208     g_object_ref (result);
2209   g_mutex_unlock (&priv->lock);
2210
2211   return result;
2212 }
2213
2214 /**
2215  * gst_rtsp_client_set_auth:
2216  * @client: a #GstRTSPClient
2217  * @auth: a #GstRTSPAuth
2218  *
2219  * configure @auth to be used as the authentication manager of @client.
2220  */
2221 void
2222 gst_rtsp_client_set_auth (GstRTSPClient * client, GstRTSPAuth * auth)
2223 {
2224   GstRTSPClientPrivate *priv;
2225   GstRTSPAuth *old;
2226
2227   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2228
2229   priv = client->priv;
2230
2231   if (auth)
2232     g_object_ref (auth);
2233
2234   g_mutex_lock (&priv->lock);
2235   old = priv->auth;
2236   priv->auth = auth;
2237   g_mutex_unlock (&priv->lock);
2238
2239   if (old)
2240     g_object_unref (old);
2241 }
2242
2243
2244 /**
2245  * gst_rtsp_client_get_auth:
2246  * @client: a #GstRTSPClient
2247  *
2248  * Get the #GstRTSPAuth used as the authentication manager of @client.
2249  *
2250  * Returns: (transfer full): the #GstRTSPAuth of @client. g_object_unref() after
2251  * usage.
2252  */
2253 GstRTSPAuth *
2254 gst_rtsp_client_get_auth (GstRTSPClient * client)
2255 {
2256   GstRTSPClientPrivate *priv;
2257   GstRTSPAuth *result;
2258
2259   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2260
2261   priv = client->priv;
2262
2263   g_mutex_lock (&priv->lock);
2264   if ((result = priv->auth))
2265     g_object_ref (result);
2266   g_mutex_unlock (&priv->lock);
2267
2268   return result;
2269 }
2270
2271 /**
2272  * gst_rtsp_client_set_thread_pool:
2273  * @client: a #GstRTSPClient
2274  * @pool: a #GstRTSPThreadPool
2275  *
2276  * configure @pool to be used as the thread pool of @client.
2277  */
2278 void
2279 gst_rtsp_client_set_thread_pool (GstRTSPClient * client,
2280     GstRTSPThreadPool * pool)
2281 {
2282   GstRTSPClientPrivate *priv;
2283   GstRTSPThreadPool *old;
2284
2285   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2286
2287   priv = client->priv;
2288
2289   if (pool)
2290     g_object_ref (pool);
2291
2292   g_mutex_lock (&priv->lock);
2293   old = priv->thread_pool;
2294   priv->thread_pool = pool;
2295   g_mutex_unlock (&priv->lock);
2296
2297   if (old)
2298     g_object_unref (old);
2299 }
2300
2301 /**
2302  * gst_rtsp_client_get_thread_pool:
2303  * @client: a #GstRTSPClient
2304  *
2305  * Get the #GstRTSPThreadPool used as the thread pool of @client.
2306  *
2307  * Returns: (transfer full): the #GstRTSPThreadPool of @client. g_object_unref() after
2308  * usage.
2309  */
2310 GstRTSPThreadPool *
2311 gst_rtsp_client_get_thread_pool (GstRTSPClient * client)
2312 {
2313   GstRTSPClientPrivate *priv;
2314   GstRTSPThreadPool *result;
2315
2316   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2317
2318   priv = client->priv;
2319
2320   g_mutex_lock (&priv->lock);
2321   if ((result = priv->thread_pool))
2322     g_object_ref (result);
2323   g_mutex_unlock (&priv->lock);
2324
2325   return result;
2326 }
2327
2328 /**
2329  * gst_rtsp_client_set_connection:
2330  * @client: a #GstRTSPClient
2331  * @conn: (transfer full): a #GstRTSPConnection
2332  *
2333  * Set the #GstRTSPConnection of @client. This function takes ownership of
2334  * @conn.
2335  *
2336  * Returns: %TRUE on success.
2337  */
2338 gboolean
2339 gst_rtsp_client_set_connection (GstRTSPClient * client,
2340     GstRTSPConnection * conn)
2341 {
2342   GstRTSPClientPrivate *priv;
2343   GSocket *read_socket;
2344   GSocketAddress *address;
2345   GstRTSPUrl *url;
2346   GError *error = NULL;
2347
2348   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), FALSE);
2349   g_return_val_if_fail (conn != NULL, FALSE);
2350
2351   priv = client->priv;
2352
2353   read_socket = gst_rtsp_connection_get_read_socket (conn);
2354
2355   if (!(address = g_socket_get_local_address (read_socket, &error)))
2356     goto no_address;
2357
2358   g_free (priv->server_ip);
2359   /* keep the original ip that the client connected to */
2360   if (G_IS_INET_SOCKET_ADDRESS (address)) {
2361     GInetAddress *iaddr;
2362
2363     iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
2364
2365     /* socket might be ipv6 but adress still ipv4 */
2366     priv->is_ipv6 = g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6;
2367     priv->server_ip = g_inet_address_to_string (iaddr);
2368     g_object_unref (address);
2369   } else {
2370     priv->is_ipv6 = g_socket_get_family (read_socket) == G_SOCKET_FAMILY_IPV6;
2371     priv->server_ip = g_strdup ("unknown");
2372   }
2373
2374   GST_INFO ("client %p connected to server ip %s, ipv6 = %d", client,
2375       priv->server_ip, priv->is_ipv6);
2376
2377   url = gst_rtsp_connection_get_url (conn);
2378   GST_INFO ("added new client %p ip %s:%d", client, url->host, url->port);
2379
2380   priv->connection = conn;
2381
2382   return TRUE;
2383
2384   /* ERRORS */
2385 no_address:
2386   {
2387     GST_ERROR ("could not get local address %s", error->message);
2388     g_error_free (error);
2389     return FALSE;
2390   }
2391 }
2392
2393 /**
2394  * gst_rtsp_client_get_connection:
2395  * @client: a #GstRTSPClient
2396  *
2397  * Get the #GstRTSPConnection of @client.
2398  *
2399  * Returns: (transfer none): the #GstRTSPConnection of @client.
2400  * The connection object returned remains valid until the client is freed.
2401  */
2402 GstRTSPConnection *
2403 gst_rtsp_client_get_connection (GstRTSPClient * client)
2404 {
2405   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2406
2407   return client->priv->connection;
2408 }
2409
2410 /**
2411  * gst_rtsp_client_set_send_func:
2412  * @client: a #GstRTSPClient
2413  * @func: a #GstRTSPClientSendFunc
2414  * @user_data: user data passed to @func
2415  * @notify: called when @user_data is no longer in use
2416  *
2417  * Set @func as the callback that will be called when a new message needs to be
2418  * sent to the client. @user_data is passed to @func and @notify is called when
2419  * @user_data is no longer in use.
2420  *
2421  * By default, the client will send the messages on the #GstRTSPConnection that
2422  * was configured with gst_rtsp_client_attach() was called.
2423  */
2424 void
2425 gst_rtsp_client_set_send_func (GstRTSPClient * client,
2426     GstRTSPClientSendFunc func, gpointer user_data, GDestroyNotify notify)
2427 {
2428   GstRTSPClientPrivate *priv;
2429   GDestroyNotify old_notify;
2430   gpointer old_data;
2431
2432   g_return_if_fail (GST_IS_RTSP_CLIENT (client));
2433
2434   priv = client->priv;
2435
2436   g_mutex_lock (&priv->send_lock);
2437   priv->send_func = func;
2438   old_notify = priv->send_notify;
2439   old_data = priv->send_data;
2440   priv->send_notify = notify;
2441   priv->send_data = user_data;
2442   g_mutex_unlock (&priv->send_lock);
2443
2444   if (old_notify)
2445     old_notify (old_data);
2446 }
2447
2448 /**
2449  * gst_rtsp_client_handle_message:
2450  * @client: a #GstRTSPClient
2451  * @message: an #GstRTSPMessage
2452  *
2453  * Let the client handle @message.
2454  *
2455  * Returns: a #GstRTSPResult.
2456  */
2457 GstRTSPResult
2458 gst_rtsp_client_handle_message (GstRTSPClient * client,
2459     GstRTSPMessage * message)
2460 {
2461   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2462   g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2463
2464   switch (message->type) {
2465     case GST_RTSP_MESSAGE_REQUEST:
2466       handle_request (client, message);
2467       break;
2468     case GST_RTSP_MESSAGE_RESPONSE:
2469       handle_response (client, message);
2470       break;
2471     case GST_RTSP_MESSAGE_DATA:
2472       handle_data (client, message);
2473       break;
2474     default:
2475       break;
2476   }
2477   return GST_RTSP_OK;
2478 }
2479
2480 /**
2481  * gst_rtsp_client_send_message:
2482  * @client: a #GstRTSPClient
2483  * @session: a #GstRTSPSession to send the message to or %NULL
2484  * @message: The #GstRTSPMessage to send
2485  *
2486  * Send a message message to the remote end. @message must be a
2487  * #GST_RTSP_MESSAGE_REQUEST or a #GST_RTSP_MESSAGE_RESPONSE.
2488  */
2489 GstRTSPResult
2490 gst_rtsp_client_send_message (GstRTSPClient * client, GstRTSPSession * session,
2491     GstRTSPMessage * message)
2492 {
2493   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), GST_RTSP_EINVAL);
2494   g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2495   g_return_val_if_fail (message->type == GST_RTSP_MESSAGE_REQUEST ||
2496       message->type == GST_RTSP_MESSAGE_RESPONSE, GST_RTSP_EINVAL);
2497
2498   send_message (client, session, message, FALSE);
2499
2500   return GST_RTSP_OK;
2501 }
2502
2503 static GstRTSPResult
2504 do_send_message (GstRTSPClient * client, GstRTSPMessage * message,
2505     gboolean close, gpointer user_data)
2506 {
2507   GstRTSPClientPrivate *priv = client->priv;
2508
2509   /* send the response and store the seq number so we can wait until it's
2510    * written to the client to close the connection */
2511   return gst_rtsp_watch_send_message (priv->watch, message, close ?
2512       &priv->close_seq : NULL);
2513 }
2514
2515 static GstRTSPResult
2516 message_received (GstRTSPWatch * watch, GstRTSPMessage * message,
2517     gpointer user_data)
2518 {
2519   return gst_rtsp_client_handle_message (GST_RTSP_CLIENT (user_data), message);
2520 }
2521
2522 static GstRTSPResult
2523 message_sent (GstRTSPWatch * watch, guint cseq, gpointer user_data)
2524 {
2525   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2526   GstRTSPClientPrivate *priv = client->priv;
2527
2528   if (priv->close_seq && priv->close_seq == cseq) {
2529     priv->close_seq = 0;
2530     close_connection (client);
2531   }
2532
2533   return GST_RTSP_OK;
2534 }
2535
2536 static GstRTSPResult
2537 closed (GstRTSPWatch * watch, gpointer user_data)
2538 {
2539   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2540   GstRTSPClientPrivate *priv = client->priv;
2541   const gchar *tunnelid;
2542
2543   GST_INFO ("client %p: connection closed", client);
2544
2545   if ((tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection))) {
2546     g_mutex_lock (&tunnels_lock);
2547     /* remove from tunnelids */
2548     g_hash_table_remove (tunnels, tunnelid);
2549     g_mutex_unlock (&tunnels_lock);
2550   }
2551
2552   gst_rtsp_client_set_send_func (client, NULL, NULL, NULL);
2553
2554   return GST_RTSP_OK;
2555 }
2556
2557 static GstRTSPResult
2558 error (GstRTSPWatch * watch, GstRTSPResult result, gpointer user_data)
2559 {
2560   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2561   gchar *str;
2562
2563   str = gst_rtsp_strresult (result);
2564   GST_INFO ("client %p: received an error %s", client, str);
2565   g_free (str);
2566
2567   return GST_RTSP_OK;
2568 }
2569
2570 static GstRTSPResult
2571 error_full (GstRTSPWatch * watch, GstRTSPResult result,
2572     GstRTSPMessage * message, guint id, gpointer user_data)
2573 {
2574   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2575   gchar *str;
2576
2577   str = gst_rtsp_strresult (result);
2578   GST_INFO
2579       ("client %p: error when handling message %p with id %d: %s",
2580       client, message, id, str);
2581   g_free (str);
2582
2583   return GST_RTSP_OK;
2584 }
2585
2586 static gboolean
2587 remember_tunnel (GstRTSPClient * client)
2588 {
2589   GstRTSPClientPrivate *priv = client->priv;
2590   const gchar *tunnelid;
2591
2592   /* store client in the pending tunnels */
2593   tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2594   if (tunnelid == NULL)
2595     goto no_tunnelid;
2596
2597   GST_INFO ("client %p: inserting tunnel session %s", client, tunnelid);
2598
2599   /* we can't have two clients connecting with the same tunnelid */
2600   g_mutex_lock (&tunnels_lock);
2601   if (g_hash_table_lookup (tunnels, tunnelid))
2602     goto tunnel_existed;
2603
2604   g_hash_table_insert (tunnels, g_strdup (tunnelid), g_object_ref (client));
2605   g_mutex_unlock (&tunnels_lock);
2606
2607   return TRUE;
2608
2609   /* ERRORS */
2610 no_tunnelid:
2611   {
2612     GST_ERROR ("client %p: no tunnelid provided", client);
2613     return FALSE;
2614   }
2615 tunnel_existed:
2616   {
2617     g_mutex_unlock (&tunnels_lock);
2618     GST_ERROR ("client %p: tunnel session %s already existed", client,
2619         tunnelid);
2620     return FALSE;
2621   }
2622 }
2623
2624 static GstRTSPStatusCode
2625 tunnel_start (GstRTSPWatch * watch, gpointer user_data)
2626 {
2627   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2628   GstRTSPClientPrivate *priv = client->priv;
2629
2630   GST_INFO ("client %p: tunnel start (connection %p)", client,
2631       priv->connection);
2632
2633   if (!remember_tunnel (client))
2634     goto tunnel_error;
2635
2636   return GST_RTSP_STS_OK;
2637
2638   /* ERRORS */
2639 tunnel_error:
2640   {
2641     GST_ERROR ("client %p: error starting tunnel", client);
2642     return GST_RTSP_STS_SERVICE_UNAVAILABLE;
2643   }
2644 }
2645
2646 static GstRTSPResult
2647 tunnel_lost (GstRTSPWatch * watch, gpointer user_data)
2648 {
2649   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2650   GstRTSPClientPrivate *priv = client->priv;
2651
2652   GST_WARNING ("client %p: tunnel lost (connection %p)", client,
2653       priv->connection);
2654
2655   /* ignore error, it'll only be a problem when the client does a POST again */
2656   remember_tunnel (client);
2657
2658   return GST_RTSP_OK;
2659 }
2660
2661 static GstRTSPResult
2662 tunnel_complete (GstRTSPWatch * watch, gpointer user_data)
2663 {
2664   const gchar *tunnelid;
2665   GstRTSPClient *client = GST_RTSP_CLIENT (user_data);
2666   GstRTSPClientPrivate *priv = client->priv;
2667   GstRTSPClient *oclient;
2668   GstRTSPClientPrivate *opriv;
2669
2670   GST_INFO ("client %p: tunnel complete", client);
2671
2672   /* find previous tunnel */
2673   tunnelid = gst_rtsp_connection_get_tunnelid (priv->connection);
2674   if (tunnelid == NULL)
2675     goto no_tunnelid;
2676
2677   g_mutex_lock (&tunnels_lock);
2678   if (!(oclient = g_hash_table_lookup (tunnels, tunnelid)))
2679     goto no_tunnel;
2680
2681   /* remove the old client from the table. ref before because removing it will
2682    * remove the ref to it. */
2683   g_object_ref (oclient);
2684   g_hash_table_remove (tunnels, tunnelid);
2685
2686   opriv = oclient->priv;
2687
2688   if (opriv->watch == NULL)
2689     goto tunnel_closed;
2690   g_mutex_unlock (&tunnels_lock);
2691
2692   GST_INFO ("client %p: found tunnel %p (old %p, new %p)", client, oclient,
2693       opriv->connection, priv->connection);
2694
2695   /* merge the tunnels into the first client */
2696   gst_rtsp_connection_do_tunnel (opriv->connection, priv->connection);
2697   gst_rtsp_watch_reset (opriv->watch);
2698   g_object_unref (oclient);
2699
2700   return GST_RTSP_OK;
2701
2702   /* ERRORS */
2703 no_tunnelid:
2704   {
2705     GST_ERROR ("client %p: no tunnelid provided", client);
2706     return GST_RTSP_ERROR;
2707   }
2708 no_tunnel:
2709   {
2710     g_mutex_unlock (&tunnels_lock);
2711     GST_ERROR ("client %p: tunnel session %s not found", client, tunnelid);
2712     return GST_RTSP_ERROR;
2713   }
2714 tunnel_closed:
2715   {
2716     g_mutex_unlock (&tunnels_lock);
2717     GST_ERROR ("client %p: tunnel session %s was closed", client, tunnelid);
2718     g_object_unref (oclient);
2719     return GST_RTSP_ERROR;
2720   }
2721 }
2722
2723 static GstRTSPWatchFuncs watch_funcs = {
2724   message_received,
2725   message_sent,
2726   closed,
2727   error,
2728   tunnel_start,
2729   tunnel_complete,
2730   error_full,
2731   tunnel_lost
2732 };
2733
2734 static void
2735 client_watch_notify (GstRTSPClient * client)
2736 {
2737   GstRTSPClientPrivate *priv = client->priv;
2738
2739   GST_INFO ("client %p: watch destroyed", client);
2740   priv->watch = NULL;
2741   g_signal_emit (client, gst_rtsp_client_signals[SIGNAL_CLOSED], 0, NULL);
2742   g_object_unref (client);
2743 }
2744
2745 /**
2746  * gst_rtsp_client_attach:
2747  * @client: a #GstRTSPClient
2748  * @context: (allow-none): a #GMainContext
2749  *
2750  * Attaches @client to @context. When the mainloop for @context is run, the
2751  * client will be dispatched. When @context is NULL, the default context will be
2752  * used).
2753  *
2754  * This function should be called when the client properties and urls are fully
2755  * configured and the client is ready to start.
2756  *
2757  * Returns: the ID (greater than 0) for the source within the GMainContext.
2758  */
2759 guint
2760 gst_rtsp_client_attach (GstRTSPClient * client, GMainContext * context)
2761 {
2762   GstRTSPClientPrivate *priv;
2763   guint res;
2764
2765   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), 0);
2766   priv = client->priv;
2767   g_return_val_if_fail (priv->connection != NULL, 0);
2768   g_return_val_if_fail (priv->watch == NULL, 0);
2769
2770   /* create watch for the connection and attach */
2771   priv->watch = gst_rtsp_watch_new (priv->connection, &watch_funcs,
2772       g_object_ref (client), (GDestroyNotify) client_watch_notify);
2773   gst_rtsp_client_set_send_func (client, do_send_message, priv->watch,
2774       (GDestroyNotify) gst_rtsp_watch_unref);
2775
2776   /* FIXME make this configurable. We don't want to do this yet because it will
2777    * be superceeded by a cache object later */
2778   gst_rtsp_watch_set_send_backlog (priv->watch, 0, 100);
2779
2780   GST_INFO ("attaching to context %p", context);
2781   res = gst_rtsp_watch_attach (priv->watch, context);
2782
2783   return res;
2784 }
2785
2786 /**
2787  * gst_rtsp_client_session_filter:
2788  * @client: a #GstRTSPClient
2789  * @func: (scope call): a callback
2790  * @user_data: user data passed to @func
2791  *
2792  * Call @func for each session managed by @client. The result value of @func
2793  * determines what happens to the session. @func will be called with @client
2794  * locked so no further actions on @client can be performed from @func.
2795  *
2796  * If @func returns #GST_RTSP_FILTER_REMOVE, the session will be removed from
2797  * @client.
2798  *
2799  * If @func returns #GST_RTSP_FILTER_KEEP, the session will remain in @client.
2800  *
2801  * If @func returns #GST_RTSP_FILTER_REF, the session will remain in @client but
2802  * will also be added with an additional ref to the result #GList of this
2803  * function..
2804  *
2805  * Returns: (element-type GstRTSPSession) (transfer full): a #GList with all
2806  * sessions for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2807  * element in the #GList should be unreffed before the list is freed.
2808  */
2809 GList *
2810 gst_rtsp_client_session_filter (GstRTSPClient * client,
2811     GstRTSPClientSessionFilterFunc func, gpointer user_data)
2812 {
2813   GstRTSPClientPrivate *priv;
2814   GList *result, *walk, *next;
2815
2816   g_return_val_if_fail (GST_IS_RTSP_CLIENT (client), NULL);
2817   g_return_val_if_fail (func != NULL, NULL);
2818
2819   priv = client->priv;
2820
2821   result = NULL;
2822
2823   g_mutex_lock (&priv->lock);
2824   for (walk = priv->sessions; walk; walk = next) {
2825     GstRTSPSession *sess = walk->data;
2826
2827     next = g_list_next (walk);
2828
2829     switch (func (client, sess, user_data)) {
2830       case GST_RTSP_FILTER_REMOVE:
2831         /* stop watching the session and pretent it went away */
2832         client_cleanup_session (client, sess);
2833         break;
2834       case GST_RTSP_FILTER_REF:
2835         result = g_list_prepend (result, g_object_ref (sess));
2836         break;
2837       case GST_RTSP_FILTER_KEEP:
2838       default:
2839         break;
2840     }
2841   }
2842   g_mutex_unlock (&priv->lock);
2843
2844   return result;
2845 }