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