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