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