d13787a6b4b5a78c8ac95f6719056d41f4a4e51c
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-client.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <sys/ioctl.h>
21
22 #include <gst/sdp/gstsdpmessage.h>
23
24 #include "rtsp-client.h"
25
26 #undef DEBUG
27
28 G_DEFINE_TYPE (GstRTSPClient, gst_rtsp_client, G_TYPE_OBJECT);
29
30 static void
31 gst_rtsp_client_class_init (GstRTSPClientClass * klass)
32 {
33   GObjectClass *gobject_class;
34
35   gobject_class = G_OBJECT_CLASS (klass);
36 }
37
38 static void
39 gst_rtsp_client_init (GstRTSPClient * client)
40 {
41 }
42
43 /**
44  * gst_rtsp_client_new:
45  *
46  * Create a new #GstRTSPClient instance.
47  */
48 GstRTSPClient *
49 gst_rtsp_client_new (void)
50 {
51   GstRTSPClient *result;
52
53   result = g_object_new (GST_TYPE_RTSP_CLIENT, NULL);
54
55   return result;
56 }
57
58 static void
59 handle_generic_response (GstRTSPClient *client, GstRTSPStatusCode code, 
60     GstRTSPMessage *request)
61 {
62   GstRTSPMessage response = { 0 };
63
64   gst_rtsp_message_init_response (&response, code, 
65         gst_rtsp_status_as_text (code), request);
66
67   gst_rtsp_connection_send (client->connection, &response, NULL);
68 }
69
70 static gboolean
71 handle_teardown_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
72 {
73   GstRTSPResult res;
74   GstRTSPSessionMedia *media;
75   GstRTSPSession *session;
76   gchar *sessid;
77   GstRTSPMessage response = { 0 };
78   GstRTSPStatusCode code;
79
80   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
81   if (res == GST_RTSP_OK) {
82     /* we had a session in the request, find it again */
83     if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
84       goto session_not_found;
85   }
86   else
87     goto service_unavailable;
88
89   /* get a handle to the configuration of the media in the session */
90   media = gst_rtsp_session_get_media (session, client->media);
91   if (!media)
92     goto not_found;
93
94   gst_rtsp_session_media_stop (media);
95
96   gst_rtsp_session_pool_remove (client->pool, session);
97   g_object_unref (session);
98
99   /* remove the session id from the request, which will also remove it from the
100    * response */
101   gst_rtsp_message_remove_header (request, GST_RTSP_HDR_SESSION, -1);
102
103   /* construct the response now */
104   code = GST_RTSP_STS_OK;
105   gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
106
107   gst_rtsp_connection_send (client->connection, &response, NULL);
108
109   return FALSE;
110
111   /* ERRORS */
112 session_not_found:
113   {
114     handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
115     return FALSE;
116   }
117 service_unavailable:
118   {
119     return FALSE;
120   }
121 not_found:
122   {
123     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
124     return FALSE;
125   }
126 }
127
128 static gboolean
129 handle_pause_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
130 {
131   GstRTSPResult res;
132   GstRTSPSessionMedia *media;
133   GstRTSPSession *session;
134   gchar *sessid;
135   GstRTSPMessage response = { 0 };
136   GstRTSPStatusCode code;
137
138   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
139   if (res == GST_RTSP_OK) {
140     /* we had a session in the request, find it again */
141     if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
142       goto session_not_found;
143   }
144   else
145     goto service_unavailable;
146
147   /* get a handle to the configuration of the media in the session */
148   media = gst_rtsp_session_get_media (session, client->media);
149   if (!media)
150     goto not_found;
151
152   gst_rtsp_session_media_pause (media);
153   g_object_unref (session);
154
155   /* construct the response now */
156   code = GST_RTSP_STS_OK;
157   gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
158
159   gst_rtsp_connection_send (client->connection, &response, NULL);
160
161   return FALSE;
162
163   /* ERRORS */
164 session_not_found:
165   {
166     handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
167     return FALSE;
168   }
169 service_unavailable:
170   {
171     return FALSE;
172   }
173 not_found:
174   {
175     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
176     return FALSE;
177   }
178 }
179
180 static gboolean
181 handle_play_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
182 {
183   GstRTSPResult res;
184   GstRTSPSessionMedia *media;
185   GstRTSPSession *session;
186   gchar *sessid;
187   GstRTSPMessage response = { 0 };
188   GstRTSPStatusCode code;
189   GstStateChangeReturn ret;
190   GString *rtpinfo;
191   guint n_streams, i;
192   guint timestamp, seqnum;
193
194   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
195   if (res == GST_RTSP_OK) {
196     /* we had a session in the request, find it again */
197     if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
198       goto session_not_found;
199   }
200   else
201     goto service_unavailable;
202
203   /* get a handle to the configuration of the media in the session */
204   media = gst_rtsp_session_get_media (session, client->media);
205   if (!media)
206     goto not_found;
207
208   /* wait for paused to get the caps */
209   ret = gst_rtsp_session_media_pause (media);
210   switch (ret) {
211     case GST_STATE_CHANGE_NO_PREROLL:
212       break;
213     case GST_STATE_CHANGE_SUCCESS:
214       break;
215     case GST_STATE_CHANGE_FAILURE:
216       goto service_unavailable;
217     case GST_STATE_CHANGE_ASYNC:
218       ret = gst_element_get_state (media->pipeline, NULL, NULL, -1);
219       break;
220   }
221
222   /* grab RTPInfo from the payloaders now */
223   rtpinfo = g_string_new ("");
224   n_streams = gst_rtsp_media_n_streams (client->media);
225   for (i = 0; i < n_streams; i++) {
226     GstRTSPMediaStream *stream;
227
228     stream = gst_rtsp_media_get_stream (client->media, i);
229
230     g_object_get (G_OBJECT (stream->payloader), "seqnum", &seqnum, NULL);
231     g_object_get (G_OBJECT (stream->payloader), "timestamp", &timestamp, NULL);
232
233     if (i > 0)
234       g_string_append (rtpinfo, ", ");
235     g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u", uri, i, seqnum, timestamp);
236   }
237
238   /* construct the response now */
239   code = GST_RTSP_STS_OK;
240   gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
241
242   /* add the RTP-Info header */
243   gst_rtsp_message_add_header (&response, GST_RTSP_HDR_RTP_INFO, rtpinfo->str);
244   g_string_free (rtpinfo, TRUE);
245
246   gst_rtsp_connection_send (client->connection, &response, NULL);
247
248   /* start playing after sending the request */
249   gst_rtsp_session_media_play (media);
250   g_object_unref (session);
251
252   return FALSE;
253
254   /* ERRORS */
255 session_not_found:
256   {
257     handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
258     return FALSE;
259   }
260 service_unavailable:
261   {
262     handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
263     return FALSE;
264   }
265 not_found:
266   {
267     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
268     return FALSE;
269   }
270 }
271
272 static gboolean
273 handle_setup_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
274 {
275   GstRTSPResult res;
276   gchar *sessid;
277   gchar *transport;
278   gchar **transports;
279   gboolean have_transport;
280   GstRTSPTransport *ct, *st;
281   GstRTSPSession *session;
282   gint i;
283   GstRTSPLowerTrans supported;
284   GstRTSPMessage response = { 0 };
285   GstRTSPStatusCode code;
286   GstRTSPSessionStream *stream;
287   gchar *trans_str, *pos;
288   guint streamid;
289   GstRTSPSessionMedia *media;
290   gboolean need_session;
291
292   /* find the media associated with the uri */
293   if (client->media == NULL) {
294     if ((client->media = gst_rtsp_media_new (uri)) == NULL)
295       goto not_found;
296   }
297
298   /* parse the transport */
299   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_TRANSPORT, &transport, 0);
300   if (res != GST_RTSP_OK)
301     goto unsupported_transports;
302
303   transports = g_strsplit (transport, ",", 0);
304   gst_rtsp_transport_new (&ct);  
305
306   /* loop through the transports, try to parse */
307   have_transport = FALSE;
308   for (i = 0; transports[i]; i++) {
309
310     gst_rtsp_transport_init (ct);  
311     res = gst_rtsp_transport_parse (transports[i], ct);
312     if (res == GST_RTSP_OK) {
313       have_transport = TRUE;
314       break;
315     }
316   }
317   g_strfreev (transports);
318
319   /* we have not found anything usable, error out */
320   if (!have_transport) {
321     gst_rtsp_transport_free (ct);  
322     goto unsupported_transports;
323   }
324
325   /* we have a valid transport, check if we can handle it */
326   if (ct->trans != GST_RTSP_TRANS_RTP)
327     goto unsupported_transports;
328   if (ct->profile != GST_RTSP_PROFILE_AVP)
329     goto unsupported_transports;
330   supported = GST_RTSP_LOWER_TRANS_UDP |
331         GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
332   if (!(ct->lower_transport & supported))
333     goto unsupported_transports;
334
335   /* a setup request creates a session for a client, check if the client already
336    * sent a session id to us */
337   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
338   if (res == GST_RTSP_OK) {
339     /* we had a session in the request, find it again */
340     if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
341       goto session_not_found;
342     need_session = FALSE;
343   }
344   else {
345     /* create a session if this fails we probably reached our session limit or
346      * something. */
347     if (!(session = gst_rtsp_session_pool_create (client->pool)))
348       goto service_unavailable;
349     need_session = TRUE;
350   }
351
352   /* get a handle to the configuration of the media in the session */
353   media = gst_rtsp_session_get_media (session, client->media);
354   if (!media)
355     goto not_found;
356
357   /* parse the stream we need to configure */
358   if (!(pos = strstr (uri, "stream=")))
359     goto bad_request;
360
361   pos += strlen ("stream=");
362   if (sscanf (pos, "%u", &streamid) != 1)
363     goto bad_request;
364
365   /* get a handle to the stream in the media */
366   stream = gst_rtsp_session_get_stream (media, streamid);
367
368   /* setup the server transport from the client transport */
369   st = gst_rtsp_session_stream_set_transport (stream, inet_ntoa (client->address.sin_addr), ct);
370
371   /* serialize the server transport */
372   trans_str = gst_rtsp_transport_as_text (st);
373
374   /* construct the response now */
375   code = GST_RTSP_STS_OK;
376   gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
377
378   if (need_session)
379     gst_rtsp_message_add_header (&response, GST_RTSP_HDR_SESSION, session->sessionid);
380   gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str);
381   g_free (trans_str);
382   g_object_unref (session);
383
384   gst_rtsp_connection_send (client->connection, &response, NULL);
385
386   return TRUE;
387
388   /* ERRORS */
389 not_found:
390   {
391     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
392     return FALSE;
393   }
394 bad_request:
395   {
396     handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
397     return FALSE;
398   }
399 session_not_found:
400   {
401     handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
402     return FALSE;
403   }
404 unsupported_transports:
405   {
406     handle_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
407     return FALSE;
408   }
409 service_unavailable:
410   {
411     handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
412     return FALSE;
413   }
414 }
415
416 static gboolean
417 handle_describe_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
418 {
419   GstRTSPMessage response = { 0 };
420   GstSDPMessage *sdp;
421   guint n_streams, i;
422   gchar *sdptext;
423   GstRTSPMedia *media;
424   GstElement *pipeline = NULL;
425
426   /* check what kind of format is accepted */
427
428
429   /* for the describe we must generate an SDP */
430   if (!(media = gst_rtsp_media_new (uri)))
431     goto no_media;
432
433   /* create a pipeline if we have to */
434   if (pipeline == NULL) {
435     pipeline = gst_pipeline_new ("client-pipeline");
436   }
437
438   /* prepare the media into the pipeline */
439   if (!gst_rtsp_media_prepare (media, GST_BIN (pipeline)))
440     goto no_media;
441
442   /* link fakesink to all stream pads and set the pipeline to PLAYING */
443   n_streams = gst_rtsp_media_n_streams (media);
444   for (i = 0; i < n_streams; i++) {
445     GstRTSPMediaStream *stream;
446     GstElement *sink;
447     GstPad *sinkpad;
448
449     stream = gst_rtsp_media_get_stream (media, i);
450
451     sink = gst_element_factory_make ("fakesink", NULL);
452     gst_bin_add (GST_BIN (pipeline), sink);
453
454     sinkpad = gst_element_get_static_pad (sink, "sink");
455     gst_pad_link (stream->srcpad, sinkpad);
456     gst_object_unref (sinkpad);
457   }
458
459   /* now play and wait till we get the pads blocked. At that time the pipeline
460    * is prerolled and we have the caps on the streams too. */
461   gst_element_set_state (pipeline, GST_STATE_PLAYING);
462
463   /* wait for state change to complete */
464   gst_element_get_state (pipeline, NULL, NULL, -1);
465
466   /* we should now be able to construct the SDP message */
467   gst_sdp_message_new (&sdp);
468
469   /* some standard things first */
470   gst_sdp_message_set_version (sdp, "0");
471   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", "IP4", "127.0.0.1");
472   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
473   gst_sdp_message_set_information (sdp, "rtsp-server");
474   gst_sdp_message_add_time (sdp, "0", "0", NULL);
475   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
476   gst_sdp_message_add_attribute (sdp, "type", "broadcast");
477
478   for (i = 0; i < n_streams; i++) {
479     GstRTSPMediaStream *stream;
480     GstSDPMedia *smedia;
481     GstStructure *s;
482     const gchar *caps_str, *caps_enc, *caps_params;
483     gchar *tmp;
484     gint caps_pt, caps_rate;
485     guint n_fields, j;
486     gboolean first;
487     GString *fmtp;
488
489     stream = gst_rtsp_media_get_stream (media, i);
490     gst_sdp_media_new (&smedia);
491
492     s = gst_caps_get_structure (stream->caps, 0);
493
494     /* get media type and payload for the m= line */
495     caps_str = gst_structure_get_string (s, "media");
496     gst_sdp_media_set_media (smedia, caps_str);
497
498     gst_structure_get_int (s, "payload", &caps_pt);
499     tmp = g_strdup_printf ("%d", caps_pt);
500     gst_sdp_media_add_format (smedia, tmp);
501     g_free (tmp);
502
503     gst_sdp_media_set_port_info (smedia, 0, 1);
504     gst_sdp_media_set_proto (smedia, "RTP/AVP");
505
506     /* for the c= line */
507     gst_sdp_media_add_connection (smedia, "IN", "IP4", "127.0.0.1", 0, 0);
508
509     /* get clock-rate, media type and params for the rtpmap attribute */
510     gst_structure_get_int (s, "clock-rate", &caps_rate);
511     caps_enc = gst_structure_get_string (s, "encoding-name");
512     caps_params = gst_structure_get_string (s, "encoding-params");
513
514     if (caps_params)
515       tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
516                       caps_params);
517     else
518       tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
519
520     gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
521     g_free (tmp);
522
523     /* the config uri */
524     tmp = g_strdup_printf ("stream=%d", i);
525     gst_sdp_media_add_attribute (smedia, "control", tmp);
526     g_free (tmp);
527
528     /* collect all other properties and add them to fmtp */
529     fmtp = g_string_new ("");
530     g_string_append_printf (fmtp, "%d ", caps_pt);
531     first = TRUE;
532     n_fields = gst_structure_n_fields (s);
533     for (j = 0; j < n_fields; j++) {
534       const gchar *fname, *fval;
535
536       fname = gst_structure_nth_field_name (s, j);
537
538       /* filter out standard properties */
539       if (!strcmp (fname, "media")) 
540         continue;
541       if (!strcmp (fname, "payload")) 
542         continue;
543       if (!strcmp (fname, "clock-rate")) 
544         continue;
545       if (!strcmp (fname, "encoding-name")) 
546         continue;
547       if (!strcmp (fname, "encoding-params")) 
548         continue;
549       if (!strcmp (fname, "ssrc")) 
550         continue;
551       if (!strcmp (fname, "clock-base")) 
552         continue;
553       if (!strcmp (fname, "seqnum-base")) 
554         continue;
555
556       if ((fval = gst_structure_get_string (s, fname))) {
557         g_string_append_printf (fmtp, "%s%s=%s", first ? "":";", fname, fval);
558         first = FALSE;
559       }
560     }
561     if (!first) {
562       tmp = g_string_free (fmtp, FALSE);
563       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
564       g_free (tmp);
565     }
566     else {
567       g_string_free (fmtp, TRUE);
568     }
569     gst_sdp_message_add_media (sdp, smedia);
570   }
571   /* go back to NULL */
572   gst_element_set_state (pipeline, GST_STATE_NULL);
573
574   g_object_unref (media);
575
576   gst_object_unref (pipeline);
577   pipeline = NULL;
578
579   gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, 
580         gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
581
582   /* add SDP to the response body */
583   sdptext = gst_sdp_message_as_text (sdp);
584   gst_rtsp_message_take_body (&response, (guint8 *)sdptext, strlen (sdptext));
585   gst_sdp_message_free (sdp);
586
587   gst_rtsp_connection_send (client->connection, &response, NULL);
588
589   return TRUE;
590
591   /* ERRORS */
592 no_media:
593   {
594     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
595     return FALSE;
596   }
597 }
598
599 static void
600 handle_options_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
601 {
602   GstRTSPMessage response = { 0 };
603   GstRTSPMethod options;
604   GString *str;
605
606   gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, 
607         gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
608
609   options = GST_RTSP_DESCRIBE |
610             GST_RTSP_OPTIONS |
611     //        GST_RTSP_PAUSE |
612             GST_RTSP_PLAY |
613             GST_RTSP_SETUP |
614             GST_RTSP_TEARDOWN;
615
616   /* always return options.. */
617   str = g_string_new ("OPTIONS");
618
619   if (options & GST_RTSP_DESCRIBE)
620     g_string_append (str, ", DESCRIBE");
621   if (options & GST_RTSP_ANNOUNCE)
622     g_string_append (str, ", ANNOUNCE");
623   if (options & GST_RTSP_GET_PARAMETER)
624     g_string_append (str, ", GET_PARAMETER");
625   if (options & GST_RTSP_PAUSE)
626     g_string_append (str, ", PAUSE");
627   if (options & GST_RTSP_PLAY)
628     g_string_append (str, ", PLAY");
629   if (options & GST_RTSP_RECORD)
630     g_string_append (str, ", RECORD");
631   if (options & GST_RTSP_REDIRECT)
632     g_string_append (str, ", REDIRECT");
633   if (options & GST_RTSP_SETUP)
634     g_string_append (str, ", SETUP");
635   if (options & GST_RTSP_SET_PARAMETER)
636     g_string_append (str, ", SET_PARAMETER");
637   if (options & GST_RTSP_TEARDOWN)
638     g_string_append (str, ", TEARDOWN");
639
640   gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str->str);
641
642   g_string_free (str, TRUE);
643
644   gst_rtsp_connection_send (client->connection, &response, NULL);
645 }
646
647 /* this function runs in a client specific thread and handles all rtsp messages
648  * with the client */
649 static gpointer
650 handle_client (GstRTSPClient *client)
651 {
652   GstRTSPMessage request = { 0 };
653   GstRTSPResult res;
654   GstRTSPMethod method;
655   const gchar *uri;
656   GstRTSPVersion version;
657
658   while (TRUE) {
659     /* start by waiting for a message from the client */
660     res = gst_rtsp_connection_receive (client->connection, &request, NULL);
661     if (res < 0)
662       goto receive_failed;
663
664 #ifdef DEBUG
665     gst_rtsp_message_dump (&request);
666 #endif
667
668     gst_rtsp_message_parse_request (&request, &method, &uri, &version);
669
670     if (version != GST_RTSP_VERSION_1_0) {
671       /* we can only handle 1.0 requests */
672       handle_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED, &request);
673       continue;
674     }
675
676     /* now see what is asked and dispatch to a dedicated handler */
677     switch (method) {
678       case GST_RTSP_OPTIONS:
679         handle_options_response (client, uri, &request);
680         break;
681       case GST_RTSP_DESCRIBE:
682         handle_describe_response (client, uri, &request);
683         break;
684       case GST_RTSP_SETUP:
685         handle_setup_response (client, uri, &request);
686         break;
687       case GST_RTSP_PLAY:
688         handle_play_response (client, uri, &request);
689         break;
690       case GST_RTSP_PAUSE:
691         handle_pause_response (client, uri, &request);
692         break;
693       case GST_RTSP_TEARDOWN:
694         handle_teardown_response (client, uri, &request);
695         break;
696       case GST_RTSP_ANNOUNCE:
697       case GST_RTSP_GET_PARAMETER:
698       case GST_RTSP_RECORD:
699       case GST_RTSP_REDIRECT:
700       case GST_RTSP_SET_PARAMETER:
701         handle_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &request);
702         break;
703       case GST_RTSP_INVALID:
704       default:
705         handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &request);
706         break;
707     }
708   }
709   g_object_unref (client);
710   return NULL;
711
712   /* ERRORS */
713 receive_failed:
714   {
715     g_print ("receive failed, disconnect client %p\n", client);
716     gst_rtsp_connection_close (client->connection);
717     g_object_unref (client);
718     return NULL;
719   }
720 }
721
722 /* called when we need to accept a new request from a client */
723 static gboolean
724 client_accept (GstRTSPClient *client, GIOChannel *channel)
725 {
726   /* a new client connected. */
727   int server_sock_fd;
728   unsigned int address_len;
729   GstRTSPConnection *conn;
730
731   conn = client->connection;
732
733   server_sock_fd = g_io_channel_unix_get_fd (channel);
734
735   address_len = sizeof (client->address);
736   memset (&client->address, 0, address_len);
737
738   conn->fd.fd = accept (server_sock_fd, (struct sockaddr *) &client->address,
739       &address_len);
740   if (conn->fd.fd == -1)
741     goto accept_failed;
742
743   g_print ("added new client %p ip %s with fd %d\n", client,
744                 inet_ntoa (client->address.sin_addr), conn->fd.fd);
745
746   /* FIXME some hackery, we need to have a connection method to accept server
747    * connections */
748   gst_poll_add_fd (conn->fdset, &conn->fd);
749
750   return TRUE;
751
752   /* ERRORS */
753 accept_failed:
754   {
755     g_error ("Could not accept client on server socket %d: %s (%d)",
756             server_sock_fd, g_strerror (errno), errno);
757     return FALSE;
758   }
759 }
760
761 /**
762  * gst_rtsp_client_set_session_pool:
763  * @client: a #GstRTSPClient
764  * @pool: a #GstRTSPSessionPool
765  *
766  * Set @pool as the sessionpool for @client which it will use to find
767  * or allocate sessions.
768  */
769 void
770 gst_rtsp_client_set_session_pool (GstRTSPClient *client, GstRTSPSessionPool *pool)
771 {
772   GstRTSPSessionPool *old;
773
774   old = client->pool;
775   if (pool)
776     g_object_ref (pool);
777   client->pool = pool;
778   if (old)
779     g_object_unref (old);
780 }
781
782 /**
783  * gst_rtsp_client_get_session_pool:
784  * @client: a #GstRTSPClient
785  *
786  * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
787  *
788  * Returns: a #GstRTSPSessionPool, unref after usage.
789  */
790 GstRTSPSessionPool *
791 gst_rtsp_client_get_session_pool (GstRTSPClient *client)
792 {
793   GstRTSPSessionPool *result;
794
795   if ((result = client->pool))
796     g_object_ref (result);
797
798   return result;
799 }
800
801
802 /**
803  * gst_rtsp_client_attach:
804  * @client: a #GstRTSPClient
805  * @channel: a #GIOChannel
806  *
807  * Accept a new connection for @client on the socket in @source. 
808  *
809  * This function should be called when the client properties and urls are fully
810  * configured and the client is ready to start.
811  *
812  * Returns: %TRUE if the client could be accepted.
813  */
814 gboolean
815 gst_rtsp_client_accept (GstRTSPClient *client, GIOChannel *channel)
816 {
817   gst_rtsp_connection_create (NULL, &client->connection);
818
819   if (!client_accept (client, channel))
820     goto accept_failed;
821
822   /* client accepted, spawn a thread for the client */
823   g_object_ref (client);
824   client->thread = g_thread_create ((GThreadFunc)handle_client, client, TRUE, NULL);
825
826   return TRUE;
827
828   /* ERRORS */
829 accept_failed:
830   {
831     gst_rtsp_connection_close (client->connection);
832     return FALSE;
833   }
834 }