Move the connection code in one place
[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       /* wait for paused state change to complete */
219       ret = gst_element_get_state (media->pipeline, NULL, NULL, -1);
220       break;
221   }
222
223   /* grab RTPInfo from the payloaders now */
224   rtpinfo = g_string_new ("");
225   n_streams = gst_rtsp_media_n_streams (client->media);
226   for (i = 0; i < n_streams; i++) {
227     GstRTSPMediaStream *stream;
228
229     stream = gst_rtsp_media_get_stream (client->media, i);
230
231     g_object_get (G_OBJECT (stream->payloader), "seqnum", &seqnum, NULL);
232     g_object_get (G_OBJECT (stream->payloader), "timestamp", &timestamp, NULL);
233
234     if (i > 0)
235       g_string_append (rtpinfo, ", ");
236     g_string_append_printf (rtpinfo, "url=%s/stream=%d;seq=%u;rtptime=%u", uri, i, seqnum, timestamp);
237   }
238
239   /* construct the response now */
240   code = GST_RTSP_STS_OK;
241   gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
242
243   /* add the RTP-Info header */
244   gst_rtsp_message_add_header (&response, GST_RTSP_HDR_RTP_INFO, rtpinfo->str);
245   g_string_free (rtpinfo, TRUE);
246
247   gst_rtsp_connection_send (client->connection, &response, NULL);
248
249   /* start playing after sending the request */
250   gst_rtsp_session_media_play (media);
251   g_object_unref (session);
252
253   return FALSE;
254
255   /* ERRORS */
256 session_not_found:
257   {
258     handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
259     return FALSE;
260   }
261 service_unavailable:
262   {
263     handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
264     return FALSE;
265   }
266 not_found:
267   {
268     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
269     return FALSE;
270   }
271 }
272
273 static gboolean
274 handle_setup_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
275 {
276   GstRTSPResult res;
277   gchar *sessid;
278   gchar *transport;
279   gchar **transports;
280   gboolean have_transport;
281   GstRTSPTransport *ct, *st;
282   GstRTSPSession *session;
283   gint i;
284   GstRTSPLowerTrans supported;
285   GstRTSPMessage response = { 0 };
286   GstRTSPStatusCode code;
287   GstRTSPSessionStream *stream;
288   gchar *trans_str, *pos;
289   guint streamid;
290   GstRTSPSessionMedia *media;
291   gboolean need_session;
292
293   /* find the media associated with the uri */
294   if (client->media == NULL) {
295     if ((client->media = gst_rtsp_media_new (uri)) == NULL)
296       goto not_found;
297   }
298
299   /* parse the transport */
300   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_TRANSPORT, &transport, 0);
301   if (res != GST_RTSP_OK)
302     goto unsupported_transports;
303
304   transports = g_strsplit (transport, ",", 0);
305   gst_rtsp_transport_new (&ct);  
306
307   /* loop through the transports, try to parse */
308   have_transport = FALSE;
309   for (i = 0; transports[i]; i++) {
310
311     gst_rtsp_transport_init (ct);  
312     res = gst_rtsp_transport_parse (transports[i], ct);
313     if (res == GST_RTSP_OK) {
314       have_transport = TRUE;
315       break;
316     }
317   }
318   g_strfreev (transports);
319
320   /* we have not found anything usable, error out */
321   if (!have_transport) {
322     gst_rtsp_transport_free (ct);  
323     goto unsupported_transports;
324   }
325
326   /* we have a valid transport, check if we can handle it */
327   if (ct->trans != GST_RTSP_TRANS_RTP)
328     goto unsupported_transports;
329   if (ct->profile != GST_RTSP_PROFILE_AVP)
330     goto unsupported_transports;
331   supported = GST_RTSP_LOWER_TRANS_UDP |
332         GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP;
333   if (!(ct->lower_transport & supported))
334     goto unsupported_transports;
335
336   /* a setup request creates a session for a client, check if the client already
337    * sent a session id to us */
338   res = gst_rtsp_message_get_header (request, GST_RTSP_HDR_SESSION, &sessid, 0);
339   if (res == GST_RTSP_OK) {
340     /* we had a session in the request, find it again */
341     if (!(session = gst_rtsp_session_pool_find (client->pool, sessid)))
342       goto session_not_found;
343     need_session = FALSE;
344   }
345   else {
346     /* create a session if this fails we probably reached our session limit or
347      * something. */
348     if (!(session = gst_rtsp_session_pool_create (client->pool)))
349       goto service_unavailable;
350     need_session = TRUE;
351   }
352
353   /* get a handle to the configuration of the media in the session */
354   media = gst_rtsp_session_get_media (session, client->media);
355   if (!media)
356     goto not_found;
357
358   /* parse the stream we need to configure */
359   if (!(pos = strstr (uri, "stream=")))
360     goto bad_request;
361
362   pos += strlen ("stream=");
363   if (sscanf (pos, "%u", &streamid) != 1)
364     goto bad_request;
365
366   /* get a handle to the stream in the media */
367   stream = gst_rtsp_session_get_stream (media, streamid);
368
369   /* setup the server transport from the client transport */
370   st = gst_rtsp_session_stream_set_transport (stream, inet_ntoa (client->address.sin_addr), ct);
371
372   /* serialize the server transport */
373   trans_str = gst_rtsp_transport_as_text (st);
374
375   /* construct the response now */
376   code = GST_RTSP_STS_OK;
377   gst_rtsp_message_init_response (&response, code, gst_rtsp_status_as_text (code), request);
378
379   if (need_session)
380     gst_rtsp_message_add_header (&response, GST_RTSP_HDR_SESSION, session->sessionid);
381   gst_rtsp_message_add_header (&response, GST_RTSP_HDR_TRANSPORT, trans_str);
382   g_free (trans_str);
383   g_object_unref (session);
384
385   gst_rtsp_connection_send (client->connection, &response, NULL);
386
387   return TRUE;
388
389   /* ERRORS */
390 not_found:
391   {
392     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
393     return FALSE;
394   }
395 bad_request:
396   {
397     handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, request);
398     return FALSE;
399   }
400 session_not_found:
401   {
402     handle_generic_response (client, GST_RTSP_STS_SESSION_NOT_FOUND, request);
403     return FALSE;
404   }
405 unsupported_transports:
406   {
407     handle_generic_response (client, GST_RTSP_STS_UNSUPPORTED_TRANSPORT, request);
408     return FALSE;
409   }
410 service_unavailable:
411   {
412     handle_generic_response (client, GST_RTSP_STS_SERVICE_UNAVAILABLE, request);
413     return FALSE;
414   }
415 }
416
417 static gboolean
418 handle_describe_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
419 {
420   GstRTSPMessage response = { 0 };
421   GstSDPMessage *sdp;
422   guint n_streams, i;
423   gchar *sdptext;
424   GstRTSPMedia *media;
425   GstElement *pipeline = NULL;
426
427   /* check what kind of format is accepted */
428
429
430   /* for the describe we must generate an SDP */
431   if (!(media = gst_rtsp_media_new (uri)))
432     goto no_media;
433
434   /* create a pipeline if we have to */
435   if (pipeline == NULL) {
436     pipeline = gst_pipeline_new ("client-pipeline");
437   }
438
439   /* prepare the media into the pipeline */
440   if (!gst_rtsp_media_prepare (media, GST_BIN (pipeline)))
441     goto no_media;
442
443   /* link fakesink to all stream pads and set the pipeline to PLAYING */
444   n_streams = gst_rtsp_media_n_streams (media);
445   for (i = 0; i < n_streams; i++) {
446     GstRTSPMediaStream *stream;
447     GstElement *sink;
448     GstPad *sinkpad;
449
450     stream = gst_rtsp_media_get_stream (media, i);
451
452     sink = gst_element_factory_make ("fakesink", NULL);
453     gst_bin_add (GST_BIN (pipeline), sink);
454
455     sinkpad = gst_element_get_static_pad (sink, "sink");
456     gst_pad_link (stream->srcpad, sinkpad);
457     gst_object_unref (sinkpad);
458   }
459
460   /* now play and wait till we get the pads blocked. At that time the pipeline
461    * is prerolled and we have the caps on the streams too. */
462   gst_element_set_state (pipeline, GST_STATE_PLAYING);
463
464   /* wait for state change to complete */
465   gst_element_get_state (pipeline, NULL, NULL, -1);
466
467   /* we should now be able to construct the SDP message */
468   gst_sdp_message_new (&sdp);
469
470   /* some standard things first */
471   gst_sdp_message_set_version (sdp, "0");
472   gst_sdp_message_set_origin (sdp, "-", "1188340656180883", "1", "IN", "IP4", "127.0.0.1");
473   gst_sdp_message_set_session_name (sdp, "Session streamed with GStreamer");
474   gst_sdp_message_set_information (sdp, "rtsp-server");
475   gst_sdp_message_add_time (sdp, "0", "0", NULL);
476   gst_sdp_message_add_attribute (sdp, "tool", "GStreamer");
477   gst_sdp_message_add_attribute (sdp, "type", "broadcast");
478
479   for (i = 0; i < n_streams; i++) {
480     GstRTSPMediaStream *stream;
481     GstSDPMedia *smedia;
482     GstStructure *s;
483     const gchar *caps_str, *caps_enc, *caps_params;
484     gchar *tmp;
485     gint caps_pt, caps_rate;
486     guint n_fields, j;
487     gboolean first;
488     GString *fmtp;
489
490     stream = gst_rtsp_media_get_stream (media, i);
491     gst_sdp_media_new (&smedia);
492
493     s = gst_caps_get_structure (stream->caps, 0);
494
495     /* get media type and payload for the m= line */
496     caps_str = gst_structure_get_string (s, "media");
497     gst_sdp_media_set_media (smedia, caps_str);
498
499     gst_structure_get_int (s, "payload", &caps_pt);
500     tmp = g_strdup_printf ("%d", caps_pt);
501     gst_sdp_media_add_format (smedia, tmp);
502     g_free (tmp);
503
504     gst_sdp_media_set_port_info (smedia, 0, 1);
505     gst_sdp_media_set_proto (smedia, "RTP/AVP");
506
507     /* for the c= line */
508     gst_sdp_media_add_connection (smedia, "IN", "IP4", "127.0.0.1", 0, 0);
509
510     /* get clock-rate, media type and params for the rtpmap attribute */
511     gst_structure_get_int (s, "clock-rate", &caps_rate);
512     caps_enc = gst_structure_get_string (s, "encoding-name");
513     caps_params = gst_structure_get_string (s, "encoding-params");
514
515     if (caps_params)
516       tmp = g_strdup_printf ("%d %s/%d/%s", caps_pt, caps_enc, caps_rate,
517                       caps_params);
518     else
519       tmp = g_strdup_printf ("%d %s/%d", caps_pt, caps_enc, caps_rate);
520
521     gst_sdp_media_add_attribute (smedia, "rtpmap", tmp);
522     g_free (tmp);
523
524     /* the config uri */
525     tmp = g_strdup_printf ("stream=%d", i);
526     gst_sdp_media_add_attribute (smedia, "control", tmp);
527     g_free (tmp);
528
529     /* collect all other properties and add them to fmtp */
530     fmtp = g_string_new ("");
531     g_string_append_printf (fmtp, "%d ", caps_pt);
532     first = TRUE;
533     n_fields = gst_structure_n_fields (s);
534     for (j = 0; j < n_fields; j++) {
535       const gchar *fname, *fval;
536
537       fname = gst_structure_nth_field_name (s, j);
538
539       /* filter out standard properties */
540       if (!strcmp (fname, "media")) 
541         continue;
542       if (!strcmp (fname, "payload")) 
543         continue;
544       if (!strcmp (fname, "clock-rate")) 
545         continue;
546       if (!strcmp (fname, "encoding-name")) 
547         continue;
548       if (!strcmp (fname, "encoding-params")) 
549         continue;
550       if (!strcmp (fname, "ssrc")) 
551         continue;
552       if (!strcmp (fname, "clock-base")) 
553         continue;
554       if (!strcmp (fname, "seqnum-base")) 
555         continue;
556
557       if ((fval = gst_structure_get_string (s, fname))) {
558         g_string_append_printf (fmtp, "%s%s=%s", first ? "":";", fname, fval);
559         first = FALSE;
560       }
561     }
562     if (!first) {
563       tmp = g_string_free (fmtp, FALSE);
564       gst_sdp_media_add_attribute (smedia, "fmtp", tmp);
565       g_free (tmp);
566     }
567     else {
568       g_string_free (fmtp, TRUE);
569     }
570     gst_sdp_message_add_media (sdp, smedia);
571   }
572   /* go back to NULL */
573   gst_element_set_state (pipeline, GST_STATE_NULL);
574
575   g_object_unref (media);
576
577   gst_object_unref (pipeline);
578   pipeline = NULL;
579
580   gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, 
581         gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
582
583   /* add SDP to the response body */
584   sdptext = gst_sdp_message_as_text (sdp);
585   gst_rtsp_message_take_body (&response, (guint8 *)sdptext, strlen (sdptext));
586   gst_sdp_message_free (sdp);
587
588   gst_rtsp_connection_send (client->connection, &response, NULL);
589
590   return TRUE;
591
592   /* ERRORS */
593 no_media:
594   {
595     handle_generic_response (client, GST_RTSP_STS_NOT_FOUND, request);
596     return FALSE;
597   }
598 }
599
600 static void
601 handle_options_response (GstRTSPClient *client, const gchar *uri, GstRTSPMessage *request)
602 {
603   GstRTSPMessage response = { 0 };
604   GstRTSPMethod options;
605   GString *str;
606
607   gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, 
608         gst_rtsp_status_as_text (GST_RTSP_STS_OK), request);
609
610   options = GST_RTSP_DESCRIBE |
611             GST_RTSP_OPTIONS |
612     //        GST_RTSP_PAUSE |
613             GST_RTSP_PLAY |
614             GST_RTSP_SETUP |
615             GST_RTSP_TEARDOWN;
616
617   /* always return options.. */
618   str = g_string_new ("OPTIONS");
619
620   if (options & GST_RTSP_DESCRIBE)
621     g_string_append (str, ", DESCRIBE");
622   if (options & GST_RTSP_ANNOUNCE)
623     g_string_append (str, ", ANNOUNCE");
624   if (options & GST_RTSP_GET_PARAMETER)
625     g_string_append (str, ", GET_PARAMETER");
626   if (options & GST_RTSP_PAUSE)
627     g_string_append (str, ", PAUSE");
628   if (options & GST_RTSP_PLAY)
629     g_string_append (str, ", PLAY");
630   if (options & GST_RTSP_RECORD)
631     g_string_append (str, ", RECORD");
632   if (options & GST_RTSP_REDIRECT)
633     g_string_append (str, ", REDIRECT");
634   if (options & GST_RTSP_SETUP)
635     g_string_append (str, ", SETUP");
636   if (options & GST_RTSP_SET_PARAMETER)
637     g_string_append (str, ", SET_PARAMETER");
638   if (options & GST_RTSP_TEARDOWN)
639     g_string_append (str, ", TEARDOWN");
640
641   gst_rtsp_message_add_header (&response, GST_RTSP_HDR_PUBLIC, str->str);
642
643   g_string_free (str, TRUE);
644
645   gst_rtsp_connection_send (client->connection, &response, NULL);
646 }
647
648 /* this function runs in a client specific thread and handles all rtsp messages
649  * with the client */
650 static gpointer
651 handle_client (GstRTSPClient *client)
652 {
653   GstRTSPMessage request = { 0 };
654   GstRTSPResult res;
655   GstRTSPMethod method;
656   const gchar *uri;
657   GstRTSPVersion version;
658
659   while (TRUE) {
660     /* start by waiting for a message from the client */
661     res = gst_rtsp_connection_receive (client->connection, &request, NULL);
662     if (res < 0)
663       goto receive_failed;
664
665 #ifdef DEBUG
666     gst_rtsp_message_dump (&request);
667 #endif
668
669     gst_rtsp_message_parse_request (&request, &method, &uri, &version);
670
671     if (version != GST_RTSP_VERSION_1_0) {
672       /* we can only handle 1.0 requests */
673       handle_generic_response (client, GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED, &request);
674       continue;
675     }
676
677     /* now see what is asked and dispatch to a dedicated handler */
678     switch (method) {
679       case GST_RTSP_OPTIONS:
680         handle_options_response (client, uri, &request);
681         break;
682       case GST_RTSP_DESCRIBE:
683         handle_describe_response (client, uri, &request);
684         break;
685       case GST_RTSP_SETUP:
686         handle_setup_response (client, uri, &request);
687         break;
688       case GST_RTSP_PLAY:
689         handle_play_response (client, uri, &request);
690         break;
691       case GST_RTSP_PAUSE:
692         handle_pause_response (client, uri, &request);
693         break;
694       case GST_RTSP_TEARDOWN:
695         handle_teardown_response (client, uri, &request);
696         break;
697       case GST_RTSP_ANNOUNCE:
698       case GST_RTSP_GET_PARAMETER:
699       case GST_RTSP_RECORD:
700       case GST_RTSP_REDIRECT:
701       case GST_RTSP_SET_PARAMETER:
702         handle_generic_response (client, GST_RTSP_STS_NOT_IMPLEMENTED, &request);
703         break;
704       case GST_RTSP_INVALID:
705       default:
706         handle_generic_response (client, GST_RTSP_STS_BAD_REQUEST, &request);
707         break;
708     }
709   }
710   g_object_unref (client);
711   return NULL;
712
713   /* ERRORS */
714 receive_failed:
715   {
716     g_print ("receive failed, disconnect client %p\n", client);
717     gst_rtsp_connection_close (client->connection);
718     g_object_unref (client);
719     return NULL;
720   }
721 }
722
723 /* called when we need to accept a new request from a client */
724 static gboolean
725 client_accept (GstRTSPClient *client, GIOChannel *channel)
726 {
727   /* a new client connected. */
728   int server_sock_fd, fd;
729   unsigned int address_len;
730   GstRTSPConnection *conn;
731
732   server_sock_fd = g_io_channel_unix_get_fd (channel);
733
734   address_len = sizeof (client->address);
735   memset (&client->address, 0, address_len);
736
737   fd = accept (server_sock_fd, (struct sockaddr *) &client->address,
738       &address_len);
739   if (fd == -1)
740     goto accept_failed;
741
742   /* now create the connection object */
743   gst_rtsp_connection_create (NULL, &conn);
744   conn->fd.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   g_print ("added new client %p ip %s with fd %d\n", client,
751                 inet_ntoa (client->address.sin_addr), conn->fd.fd);
752
753   client->connection = conn;
754
755   return TRUE;
756
757   /* ERRORS */
758 accept_failed:
759   {
760     g_error ("Could not accept client on server socket %d: %s (%d)",
761             server_sock_fd, g_strerror (errno), errno);
762     return FALSE;
763   }
764 }
765
766 /**
767  * gst_rtsp_client_set_session_pool:
768  * @client: a #GstRTSPClient
769  * @pool: a #GstRTSPSessionPool
770  *
771  * Set @pool as the sessionpool for @client which it will use to find
772  * or allocate sessions.
773  */
774 void
775 gst_rtsp_client_set_session_pool (GstRTSPClient *client, GstRTSPSessionPool *pool)
776 {
777   GstRTSPSessionPool *old;
778
779   old = client->pool;
780   if (pool)
781     g_object_ref (pool);
782   client->pool = pool;
783   if (old)
784     g_object_unref (old);
785 }
786
787 /**
788  * gst_rtsp_client_get_session_pool:
789  * @client: a #GstRTSPClient
790  *
791  * Get the #GstRTSPSessionPool object that @client uses to manage its sessions.
792  *
793  * Returns: a #GstRTSPSessionPool, unref after usage.
794  */
795 GstRTSPSessionPool *
796 gst_rtsp_client_get_session_pool (GstRTSPClient *client)
797 {
798   GstRTSPSessionPool *result;
799
800   if ((result = client->pool))
801     g_object_ref (result);
802
803   return result;
804 }
805
806
807 /**
808  * gst_rtsp_client_attach:
809  * @client: a #GstRTSPClient
810  * @channel: a #GIOChannel
811  *
812  * Accept a new connection for @client on the socket in @source. 
813  *
814  * This function should be called when the client properties and urls are fully
815  * configured and the client is ready to start.
816  *
817  * Returns: %TRUE if the client could be accepted.
818  */
819 gboolean
820 gst_rtsp_client_accept (GstRTSPClient *client, GIOChannel *channel)
821 {
822   if (!client_accept (client, channel))
823     goto accept_failed;
824
825   /* client accepted, spawn a thread for the client */
826   g_object_ref (client);
827   client->thread = g_thread_create ((GThreadFunc)handle_client, client, TRUE, NULL);
828
829   return TRUE;
830
831   /* ERRORS */
832 accept_failed:
833   {
834     return FALSE;
835   }
836 }