]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
Revert [14683]
[vlc] / modules / misc / rtsp.c
1 /*****************************************************************************
2  * rtsp.c: rtsp VoD server module
3  *****************************************************************************
4  * Copyright (C) 2003-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <errno.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/input.h>
34 #include <vlc/sout.h>
35
36 #include "vlc_httpd.h"
37 #include "vlc_vod.h"
38 #include "vlc_url.h"
39 #include "network.h"
40 #include "charset.h"
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45 static int  Open ( vlc_object_t * );
46 static void Close( vlc_object_t * );
47
48 #define HOST_TEXT N_( "Host address" )
49 #define HOST_LONGTEXT N_( \
50     "You can set the address, port and path the rtsp interface will bind to." \
51     "\nSyntax is address:port/path. Default is to bind to any address "\
52     "on port 554, with no path." )
53
54 #define THROTLE_TEXT N_( "Maximum number of connections" )
55 #define THROTLE_LONGTEXT N_( "Limit the number of connections " \
56     "to a maximum. (0 = unlimited, N = maximum clients)" )
57
58 vlc_module_begin();
59     set_shortname( _("RTSP VoD" ) );
60     set_description( _("RTSP VoD server") );
61     set_category( CAT_SOUT );
62     set_subcategory( SUBCAT_SOUT_VOD );
63     set_capability( "vod server", 1 );
64     set_callbacks( Open, Close );
65     add_shortcut( "rtsp" );
66     add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
67     add_integer( "rtsp-throtle-users", 0, NULL, THROTLE_TEXT, THROTLE_LONGTEXT, VLC_TRUE );
68 vlc_module_end();
69
70 /*****************************************************************************
71  * Exported prototypes
72  *****************************************************************************/
73
74 typedef struct media_es_t media_es_t;
75
76 typedef struct
77 {
78     media_es_t *p_media_es;
79     char *psz_ip;
80     int i_port;
81
82 } rtsp_client_es_t;
83
84 typedef struct
85 {
86     char *psz_session;
87     int64_t i_last; /* for timeout */
88
89     vlc_bool_t b_playing; /* is it in "play" state */
90     vlc_bool_t b_paused; /* is it in "pause" state */
91
92     int i_es;
93     rtsp_client_es_t **es;
94
95 } rtsp_client_t;
96
97 struct media_es_t
98 {
99     /* VoD server */
100     vod_t *p_vod;
101
102     /* RTSP server */
103     httpd_url_t *p_rtsp_url;
104
105     vod_media_t *p_media;
106
107     es_format_t fmt;
108     int         i_port;
109     uint8_t     i_payload_type;
110     char        *psz_rtpmap;
111     char        *psz_fmtp;
112
113 };
114
115 struct vod_media_t
116 {
117     /* VoD server */
118     vod_t *p_vod;
119
120     /* RTSP server */
121     httpd_url_t  *p_rtsp_url;
122     char         *psz_rtsp_control_v4;
123     char         *psz_rtsp_control_v6;
124     char         *psz_rtsp_path;
125
126     int  i_port;
127     int  i_port_audio;
128     int  i_port_video;
129     int  i_ttl;
130     int  i_payload_type;
131
132     int64_t i_sdp_id;
133     int     i_sdp_version;
134
135     vlc_bool_t b_multicast;
136
137     vlc_mutex_t lock;
138
139     /* ES list */
140     int        i_es;
141     media_es_t **es;
142     char       *psz_mux;
143     vlc_bool_t  b_raw;
144
145     /* RTSP client */
146     int           i_rtsp;
147     rtsp_client_t **rtsp;
148
149     /* Infos */
150     char *psz_session_name;
151     char *psz_session_description;
152     char *psz_session_url;
153     char *psz_session_email;
154     mtime_t i_length;
155 };
156
157 struct vod_sys_t
158 {
159     /* RTSP server */
160     httpd_host_t *p_rtsp_host;
161     char *psz_path;
162     int i_port;
163     int i_throtle_users;
164     int i_connections;
165
166     /* List of media */
167     int i_media;
168     vod_media_t **media;
169 };
170
171 static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
172 static void         MediaDel( vod_t *, vod_media_t * );
173 static int          MediaAddES( vod_t *, vod_media_t *, es_format_t * );
174 static void         MediaDelES( vod_t *, vod_media_t *, es_format_t * );
175
176 static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
177 static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
178 static void           RtspClientDel( vod_media_t *, rtsp_client_t * );
179
180 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
181                          httpd_message_t *, httpd_message_t * );
182 static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
183                            httpd_message_t *, httpd_message_t * );
184
185 static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
186
187 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
188 {
189     static const char hex[16] = "0123456789abcdef";
190     int i;
191
192     for( i = 0; i < i_data; i++ )
193     {
194         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
195         s[2*i+1] = hex[(p_data[i]   )&0xf];
196     }
197     s[2*i_data] = '\0';
198 }
199
200 /*****************************************************************************
201  * Open: Starts the RTSP server module
202  *****************************************************************************/
203 static int Open( vlc_object_t *p_this )
204 {
205     vod_t *p_vod = (vod_t *)p_this;
206     vod_sys_t *p_sys = 0;
207     char *psz_url = 0;
208     vlc_url_t url;
209
210     psz_url = config_GetPsz( p_vod, "rtsp-host" );
211     vlc_UrlParse( &url, psz_url, 0 );
212     if( psz_url ) free( psz_url );
213
214     if( url.i_port <= 0 ) url.i_port = 554;
215
216     p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
217     if( !p_sys ) goto error;
218     p_sys->p_rtsp_host = 0;
219
220     var_Create( p_this, "rtsp-throtle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
221     p_sys->i_throtle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
222     msg_Dbg( p_this, "Allowing up to %d connections", p_sys->i_throtle_users );
223     p_sys->i_connections = 0;
224
225     p_sys->p_rtsp_host =
226         httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
227     if( !p_sys->p_rtsp_host )
228     {
229         msg_Err( p_vod, "cannot create http server (%s:%i)",
230                  url.psz_host, url.i_port );
231         goto error;
232     }
233
234     p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
235     p_sys->i_port = url.i_port;
236
237     vlc_UrlClean( &url );
238     p_sys->media = NULL;
239     p_sys->i_media = 0;
240
241     p_vod->pf_media_new = MediaNew;
242     p_vod->pf_media_del = MediaDel;
243     p_vod->pf_media_add_es = MediaAddES;
244     p_vod->pf_media_del_es = MediaDelES;
245
246     return VLC_SUCCESS;
247
248 error:
249     if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
250     if( p_sys ) free( p_sys );
251     vlc_UrlClean( &url );
252
253     return VLC_EGENERIC;
254 }
255
256 /*****************************************************************************
257  * Close:
258  *****************************************************************************/
259 static void Close( vlc_object_t * p_this )
260 {
261     vod_t *p_vod = (vod_t *)p_this;
262     vod_sys_t *p_sys = p_vod->p_sys;
263
264     httpd_HostDelete( p_sys->p_rtsp_host );
265     var_Destroy( p_this, "rtsp-throtle-users" );
266
267     /* TODO delete medias */
268     free( p_sys->psz_path );
269     free( p_sys );
270 }
271
272 /*****************************************************************************
273  * Media handling
274  *****************************************************************************/
275 static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
276                               input_item_t *p_item )
277 {
278     vod_sys_t *p_sys = p_vod->p_sys;
279     vod_media_t *p_media = malloc( sizeof(vod_media_t) );
280     int i;
281
282     if( !p_media )
283     {
284         msg_Err( p_vod, "not enough memory" );
285         return NULL;
286     }
287
288     memset( p_media, 0, sizeof(vod_media_t) );
289     p_media->es = 0;
290     p_media->psz_mux = 0;
291     p_media->rtsp = 0;
292     p_media->b_raw = VLC_FALSE;
293
294     asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
295     p_media->p_rtsp_url =
296         httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
297                             NULL, NULL );
298
299     if( !p_media->p_rtsp_url )
300     {
301         msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
302         free( p_media->psz_rtsp_path );
303         free( p_media );
304         return NULL;
305     }
306
307     msg_Dbg( p_vod, "created rtsp url: %s", p_media->psz_rtsp_path );
308
309     asprintf( &p_media->psz_rtsp_control_v4,
310                "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
311                p_sys->i_port, p_media->psz_rtsp_path );
312     asprintf( &p_media->psz_rtsp_control_v6,
313                "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
314               p_sys->i_port, p_media->psz_rtsp_path );
315
316     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
317                     RtspCallback, (void*)p_media );
318     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
319                     RtspCallback, (void*)p_media );
320     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
321                     RtspCallback, (void*)p_media );
322     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
323                     RtspCallback, (void*)p_media );
324     httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
325                     RtspCallback, (void*)p_media );
326
327     p_media->p_vod = p_vod;
328
329     TAB_APPEND( p_sys->i_media, p_sys->media, p_media );
330
331     vlc_mutex_init( p_vod, &p_media->lock );
332     p_media->psz_session_name = strdup("");
333     p_media->psz_session_description = strdup("");
334     p_media->psz_session_url = strdup("");
335     p_media->psz_session_email = strdup("");
336
337     p_media->i_port_audio = 1234;
338     p_media->i_port_video = 1236;
339     p_media->i_port       = 1238;
340     p_media->i_payload_type = 96;
341
342     p_media->i_sdp_id = mdate();
343     p_media->i_sdp_version = 1;
344     p_media->i_length = p_item->i_duration;
345
346     vlc_mutex_lock( &p_item->lock );
347     msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
348     for( i = 0; i < p_item->i_es; i++ )
349     {
350         MediaAddES( p_vod, p_media, p_item->es[i] );
351     }
352     vlc_mutex_unlock( &p_item->lock );
353
354     return p_media;
355 }
356
357 static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
358 {
359     vod_sys_t *p_sys = p_vod->p_sys;
360
361     msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );
362
363     while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
364     httpd_UrlDelete( p_media->p_rtsp_url );
365     if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
366     if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
367     if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
368
369     TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
370
371     while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
372
373     vlc_mutex_destroy( &p_media->lock );
374     free( p_media->psz_session_name );
375     free( p_media->psz_session_description );
376     free( p_media->psz_session_url );
377     free( p_media->psz_session_email );
378     free( p_media );
379 }
380
381 static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
382 {
383     media_es_t *p_es = malloc( sizeof(media_es_t) );
384     char *psz_urlc;
385
386     memset( p_es, 0, sizeof(media_es_t) );
387     p_media->psz_mux = NULL;
388
389     /* TODO: update SDP, etc... */
390     asprintf( &psz_urlc, "%s/trackID=%d",
391               p_media->psz_rtsp_path, p_media->i_es );
392     msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
393
394     switch( p_fmt->i_codec )
395     {
396         case VLC_FOURCC( 's', '1', '6', 'b' ):
397             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
398             {
399                 p_es->i_payload_type = 11;
400             }
401             else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
402             {
403                 p_es->i_payload_type = 10;
404             }
405             else
406             {
407                 p_es->i_payload_type = p_media->i_payload_type++;
408             }
409             p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
410             sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
411                     p_fmt->audio.i_channels );
412             break;
413         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
414             p_es->i_payload_type = p_media->i_payload_type++;
415             p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
416             sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
417                     p_fmt->audio.i_channels );
418             break;
419         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
420             p_es->i_payload_type = 14;
421             p_es->psz_rtpmap = strdup( "MPA/90000" );
422             break;
423         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
424             p_es->i_payload_type = 32;
425             p_es->psz_rtpmap = strdup( "MPV/90000" );
426             break;
427         case VLC_FOURCC( 'a', '5', '2', ' ' ):
428             p_es->i_payload_type = p_media->i_payload_type++;
429             p_es->psz_rtpmap = strdup( "ac3/90000" );
430             break;
431         case VLC_FOURCC( 'H', '2', '6', '3' ):
432             p_es->i_payload_type = p_media->i_payload_type++;
433             p_es->psz_rtpmap = strdup( "H263-1998/90000" );
434             break;
435         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
436             p_es->i_payload_type = p_media->i_payload_type++;
437             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
438             if( p_fmt->i_extra > 0 )
439             {
440                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
441                 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
442                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
443                 sprintf( p_es->psz_fmtp,
444                         "profile-level-id=3; config=%s;", p_hexa );
445                 free( p_hexa );
446             }
447             break;
448         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
449             p_es->i_payload_type = p_media->i_payload_type++;
450             p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
451             sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
452             if( p_fmt->i_extra > 0 )
453             {
454                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
455                 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
456                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
457                 sprintf( p_es->psz_fmtp,
458                         "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
459                         "config=%s; SizeLength=13;IndexLength=3; "
460                         "IndexDeltaLength=3; Profile=1;", p_hexa );
461                 free( p_hexa );
462             }
463             break;
464         case VLC_FOURCC( 'm', 'p', '2', 't' ):
465             p_media->psz_mux = "ts";
466             p_es->i_payload_type = 33;
467             p_es->psz_rtpmap = strdup( "MP2T/90000" );
468             break;
469         case VLC_FOURCC( 'm', 'p', '2', 'p' ):
470             p_media->psz_mux = "ps";
471             p_es->i_payload_type = p_media->i_payload_type++;
472             p_es->psz_rtpmap = strdup( "MP2P/90000" );
473             break;
474         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
475             p_es->i_payload_type = p_media->i_payload_type++;
476             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
477                                     "AMR/8000/2" : "AMR/8000" );
478             p_es->psz_fmtp = strdup( "octet-align=1" );
479             break;
480         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
481             p_es->i_payload_type = p_media->i_payload_type++;
482             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
483                                     "AMR-WB/16000/2" : "AMR-WB/16000" );
484             p_es->psz_fmtp = strdup( "octet-align=1" );
485             break;
486
487         default:
488             msg_Err( p_vod, "cannot add this stream (unsupported "
489                     "codec: %4.4s)", (char*)&p_fmt->i_codec );
490             free( p_es );
491             return VLC_EGENERIC;
492     }
493
494     p_es->p_rtsp_url =
495         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
496                             NULL );
497
498     if( !p_es->p_rtsp_url )
499     {
500         msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
501         free( psz_urlc );
502         free( p_es );
503         return VLC_EGENERIC;
504     }
505     free( psz_urlc );
506
507     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
508                     RtspCallbackES, (void*)p_es );
509     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
510                     RtspCallbackES, (void*)p_es );
511     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
512                     RtspCallbackES, (void*)p_es );
513     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
514                     RtspCallbackES, (void*)p_es );
515
516     es_format_Copy( &p_es->fmt, p_fmt );
517     p_es->p_vod = p_vod;
518     p_es->p_media = p_media;
519
520 #if 0
521     /* Choose the port */
522     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
523     {
524         p_es->i_port = p_media->i_port_audio;
525         p_media->i_port_audio = 0;
526     }
527     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
528     {
529         p_es->i_port = p_media->i_port_video;
530         p_media->i_port_video = 0;
531     }
532     while( !p_es->i_port )
533     {
534         if( p_media->i_port != p_media->i_port_audio &&
535             p_media->i_port != p_media->i_port_video )
536         {
537             p_es->i_port = p_media->i_port;
538             p_media->i_port += 2;
539             break;
540         }
541         p_media->i_port += 2;
542     }
543 #else
544
545     p_es->i_port = 0;
546 #endif
547
548     vlc_mutex_lock( &p_media->lock );
549     TAB_APPEND( p_media->i_es, p_media->es, p_es );
550     vlc_mutex_unlock( &p_media->lock );
551
552     p_media->i_sdp_version++;
553
554     return VLC_SUCCESS;
555 }
556
557 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
558 {
559     media_es_t *p_es = 0;
560     int i;
561
562     /* Find the ES */
563     for( i = 0; i < p_media->i_es; i++ )
564     {
565         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
566             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
567             p_media->es[i]->fmt.i_id == p_fmt->i_id )
568         {
569             p_es = p_media->es[i];
570         }
571     }
572     if( !p_es ) return;
573
574     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
575
576     vlc_mutex_lock( &p_media->lock );
577     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
578     vlc_mutex_unlock( &p_media->lock );
579
580     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
581     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
582     p_media->i_sdp_version++;
583
584     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
585     es_format_Clean( &p_es->fmt );
586 }
587
588 /****************************************************************************
589  * RTSP server implementation
590  ****************************************************************************/
591 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
592 {
593     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
594
595     if( !p_rtsp ) return NULL;
596     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
597     p_rtsp->es = 0;
598
599     p_rtsp->psz_session = psz_session;
600     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
601
602     p_media->p_vod->p_sys->i_connections++;
603     msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
604              psz_session, p_media->p_vod->p_sys->i_throtle_users );
605
606     return p_rtsp;
607 }
608
609 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
610 {
611     int i;
612
613     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
614     {
615         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
616         {
617             return p_media->rtsp[i];
618         }
619     }
620
621     return NULL;
622 }
623
624 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
625 {
626     p_media->p_vod->p_sys->i_connections--;
627     msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
628              p_rtsp->psz_session, p_media->p_vod->p_sys->i_throtle_users );
629
630     while( p_rtsp->i_es-- )
631     {
632         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
633             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
634         free( p_rtsp->es[p_rtsp->i_es] );
635         if( !p_rtsp->i_es ) free( p_rtsp->es );
636     }
637
638     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
639
640     free( p_rtsp->psz_session );
641     free( p_rtsp );
642 }
643
644 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
645                          httpd_message_t *answer, httpd_message_t *query )
646 {
647     vod_media_t *p_media = (vod_media_t*)p_args;
648     vod_t *p_vod = p_media->p_vod;
649     char *psz_transport = NULL;
650     char *psz_playnow = NULL; /* support option: x-playNow */
651     char *psz_session = NULL;
652     rtsp_client_t *p_rtsp;
653     int i_port = 0;
654
655     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
656
657     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
658
659     answer->i_proto   = HTTPD_PROTO_RTSP;
660     answer->i_version = query->i_version;
661     answer->i_type    = HTTPD_MSG_ANSWER;
662     answer->i_body    = 0;
663     answer->p_body      = NULL;
664
665     switch( query->i_type )
666     {
667         case HTTPD_MSG_SETUP:
668         {
669             psz_playnow = httpd_MsgGet( query, "x-playNow" );
670             psz_transport = httpd_MsgGet( query, "Transport" );
671             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
672
673             if( strstr( psz_transport, "unicast" ) &&
674                 strstr( psz_transport, "client_port=" ) )
675             {
676                 rtsp_client_t *p_rtsp;
677                 char ip[NI_MAXNUMERICHOST];
678                 i_port = atoi( strstr( psz_transport, "client_port=" ) +
679                                 strlen("client_port=") );
680
681                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
682                     strstr( psz_transport, "RAW/RAW/UDP" ) )
683                 {
684                     p_media->b_raw = VLC_TRUE;
685                 }
686
687                 if( httpd_ClientIP( cl, ip ) == NULL )
688                 {
689                     answer->i_status = 500;
690                     answer->psz_status = strdup( "Internal server error" );
691                     answer->i_body = 0;
692                     answer->p_body = NULL;
693                     break;
694                 }
695
696                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
697                          ip, i_port );
698
699                 psz_session = httpd_MsgGet( query, "Session" );
700                 if( !psz_session || !*psz_session )
701                 {
702                     if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
703                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
704                     {
705                         answer->i_status = 503;
706                         answer->psz_status = strdup( "Too many connections" );
707                         answer->i_body = 0;
708                         answer->p_body = NULL;
709                         break;
710                     }
711                     asprintf( &psz_session, "%d", rand() );
712                     p_rtsp = RtspClientNew( p_media, psz_session );
713                 }
714                 else
715                 {
716                     p_rtsp = RtspClientGet( p_media, psz_session );
717                     if( !p_rtsp )
718                     {
719                         answer->i_status = 454;
720                         answer->psz_status = strdup( "Unknown session id" );
721                         answer->i_body = 0;
722                         answer->p_body = NULL;
723                         break;
724                     }
725                 }
726
727                 answer->i_status = 200;
728                 answer->psz_status = strdup( "OK" );
729                 answer->i_body = 0;
730                 answer->p_body = NULL;
731
732                 if( p_media->b_raw )
733                 {
734                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
735                     {
736                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
737                                       i_port, i_port + 1 );
738                     }
739                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
740                     {
741                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
742                                       i_port, i_port + 1 );
743                     }
744                 }
745                 else
746                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
747                                   i_port, i_port + 1 );
748             }
749             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
750             {
751                 answer->i_status = 461;
752                 answer->psz_status = strdup( "Unsupported Transport" );
753                 answer->i_body = 0;
754                 answer->p_body = NULL;
755             }
756
757             if( !psz_playnow || !*psz_playnow )
758                 break;
759         }
760
761         case HTTPD_MSG_PLAY:
762         {
763             char *psz_output, ip[NI_MAXNUMERICHOST];
764             int i, i_port_audio = 0, i_port_video = 0;
765
766             /* for now only multicast so easy */
767             if( !psz_playnow )
768             {
769                 answer->i_status = 200;
770                 answer->psz_status = strdup( "OK" );
771                 answer->i_body = 0;
772                 answer->p_body = NULL;
773             }
774
775             if( !psz_session )
776                 psz_session = httpd_MsgGet( query, "Session" );
777             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
778
779             p_rtsp = RtspClientGet( p_media, psz_session );
780             if( !p_rtsp ) break;
781
782             if( p_rtsp->b_playing && p_rtsp->b_paused )
783             {
784                 vod_MediaControl( p_vod, p_media, psz_session,
785                                   VOD_MEDIA_PAUSE );
786                 p_rtsp->b_paused = VLC_FALSE;
787                 break;
788             }
789             else if( p_rtsp->b_playing ) break;
790
791             if( httpd_ClientIP( cl, ip ) == NULL ) break;
792
793             p_rtsp->b_playing = VLC_TRUE;
794
795             /* FIXME for != 1 video and 1 audio */
796             for( i = 0; i < p_rtsp->i_es; i++ )
797             {
798                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
799                     i_port_audio = p_rtsp->es[i]->i_port;
800                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
801                     i_port_video = p_rtsp->es[i]->i_port;
802             }
803
804             if( p_media->psz_mux )
805             {
806                 if( p_media->b_raw )
807                 {
808                     asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
809                               ip, i_port, p_media->psz_mux );
810                 }
811                 else
812                 {
813                     asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
814                               ip, i_port_video, p_media->psz_mux );
815                 }
816             }
817             else
818             {
819                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
820                           "port-audio=%i}", ip, i_port_video, i_port_audio );
821             }
822
823             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
824                               psz_output );
825             free( psz_output );
826             break;
827         }
828
829         case HTTPD_MSG_DESCRIBE:
830         {
831             char *psz_sdp =
832                 SDPGenerate( p_media, cl );
833
834             if( psz_sdp != NULL )
835             {
836                 answer->i_status = 200;
837                 answer->psz_status = strdup( "OK" );
838                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
839
840                 answer->p_body = (uint8_t *)psz_sdp;
841                 answer->i_body = strlen( psz_sdp );
842             }
843             else
844             {
845                 answer->i_status = 500;
846                 answer->psz_status = strdup( "Internal server error" );
847                 answer->p_body = NULL;
848                 answer->i_body = 0;
849             }
850             break;
851         }
852
853         case HTTPD_MSG_PAUSE:
854             psz_session = httpd_MsgGet( query, "Session" );
855             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
856
857             p_rtsp = RtspClientGet( p_media, psz_session );
858             if( !p_rtsp ) break;
859
860             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
861             p_rtsp->b_paused = VLC_TRUE;
862
863             answer->i_status = 200;
864             answer->psz_status = strdup( "OK" );
865             answer->i_body = 0;
866             answer->p_body = NULL;
867             break;
868
869         case HTTPD_MSG_TEARDOWN:
870             /* for now only multicast so easy again */
871             answer->i_status = 200;
872             answer->psz_status = strdup( "OK" );
873             answer->i_body = 0;
874             answer->p_body = NULL;
875
876             psz_session = httpd_MsgGet( query, "Session" );
877             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
878
879             p_rtsp = RtspClientGet( p_media, psz_session );
880             if( !p_rtsp ) break;
881
882             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
883             RtspClientDel( p_media, p_rtsp );
884             break;
885
886         default:
887             return VLC_EGENERIC;
888     }
889
890     httpd_MsgAdd( answer, "Server", "VLC Server" );
891     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
892     httpd_MsgAdd( answer, "Cseq", "%d",
893                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
894     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
895
896     if( psz_session )
897     {
898         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
899     }
900
901     return VLC_SUCCESS;
902 }
903
904 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
905                            httpd_message_t *answer, httpd_message_t *query )
906 {
907     media_es_t *p_es = (media_es_t*)p_args;
908     vod_media_t *p_media = p_es->p_media;
909     vod_t *p_vod = p_media->p_vod;
910     rtsp_client_t *p_rtsp = NULL;
911     char *psz_transport = NULL;
912     char *psz_playnow = NULL; /* support option: x-playNow */
913     char *psz_session = NULL;
914     char *psz_position = NULL;
915     int i;
916
917     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
918
919     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
920
921     answer->i_proto   = HTTPD_PROTO_RTSP;
922     answer->i_version = query->i_version;
923     answer->i_type    = HTTPD_MSG_ANSWER;
924     answer->i_body    = 0;
925     answer->p_body      = NULL;
926
927     switch( query->i_type )
928     {
929         case HTTPD_MSG_SETUP:
930             psz_playnow = httpd_MsgGet( query, "x-playNow" );
931             psz_transport = httpd_MsgGet( query, "Transport" );
932
933             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
934
935             if( strstr( psz_transport, "unicast" ) &&
936                 strstr( psz_transport, "client_port=" ) )
937             {
938                 rtsp_client_t *p_rtsp;
939                 rtsp_client_es_t *p_rtsp_es;
940                 char ip[NI_MAXNUMERICHOST];
941                 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
942                                 strlen("client_port=") );
943
944                 if( httpd_ClientIP( cl, ip ) == NULL )
945                 {
946                     answer->i_status = 500;
947                     answer->psz_status = strdup( "Internal server error" );
948                     answer->i_body = 0;
949                     answer->p_body = NULL;
950                     break;
951                 }
952
953                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
954                         ip, i_port );
955
956                 psz_session = httpd_MsgGet( query, "Session" );
957                 if( !psz_session || !*psz_session )
958                 {
959                     if( ( p_vod->p_sys->i_throtle_users > 0 ) &&
960                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throtle_users ) )
961                     {
962                         answer->i_status = 503;
963                         answer->psz_status = strdup( "Too many connections" );
964                         answer->i_body = 0;
965                         answer->p_body = NULL;
966                         break;
967                     }
968                     asprintf( &psz_session, "%d", rand() );
969                     p_rtsp = RtspClientNew( p_media, psz_session );
970                 }
971                 else
972                 {
973                     p_rtsp = RtspClientGet( p_media, psz_session );
974                     if( !p_rtsp )
975                     {
976                         answer->i_status = 454;
977                         answer->psz_status = strdup( "Unknown session id" );
978                         answer->i_body = 0;
979                         answer->p_body = NULL;
980                         break;
981                     }
982                 }
983
984                 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
985                 p_rtsp_es->i_port = i_port;
986                 p_rtsp_es->psz_ip = strdup( ip );
987                 p_rtsp_es->p_media_es = p_es;
988                 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
989
990                 answer->i_status = 200;
991                 answer->psz_status = strdup( "OK" );
992                 answer->i_body = 0;
993                 answer->p_body = NULL;
994
995                 if( p_media->b_raw )
996                 {
997                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
998                     {
999                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1000                                       i_port, i_port + 1 );
1001                     }
1002                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1003                     {
1004                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1005                                       i_port, i_port + 1 );
1006                     }
1007                 }
1008                 else
1009                 {
1010                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1011                                   i_port, i_port + 1 );
1012                 }
1013             }
1014             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1015             {
1016                 answer->i_status = 461;
1017                 answer->psz_status = strdup( "Unsupported Transport" );
1018                 answer->i_body = 0;
1019                 answer->p_body = NULL;
1020             }
1021
1022             if( !psz_playnow || !*psz_playnow )
1023                 break;
1024
1025         case HTTPD_MSG_PLAY:
1026             /* This is kind of a kludge. Should we only support Aggregate
1027              * Operations ? */
1028             psz_session = httpd_MsgGet( query, "Session" );
1029             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1030
1031             p_rtsp = RtspClientGet( p_media, psz_session );
1032
1033             psz_position = httpd_MsgGet( query, "Range" );
1034             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1035             if( psz_position )
1036             {
1037                 double f_pos;
1038                 char *end;
1039
1040                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1041
1042                 psz_position += 4;
1043                 /* FIXME: npt= is not necessarily formatted as a float */
1044                 f_pos = us_strtod( psz_position, &end );
1045                 if( end > psz_position )
1046                 {
1047                     f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1048                     vod_MediaControl( p_vod, p_media, psz_session,
1049                                       VOD_MEDIA_SEEK, f_pos );
1050                 }
1051             }
1052
1053             if( !psz_playnow )
1054             {
1055                 answer->i_status = 200;
1056                 answer->psz_status = strdup( "OK" );
1057                 answer->i_body = 0;
1058                 answer->p_body = NULL;
1059             }
1060             break;
1061
1062         case HTTPD_MSG_TEARDOWN:
1063             answer->i_status = 200;
1064             answer->psz_status = strdup( "OK" );
1065             answer->i_body = 0;
1066             answer->p_body = NULL;
1067
1068             psz_session = httpd_MsgGet( query, "Session" );
1069             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1070
1071             p_rtsp = RtspClientGet( p_media, psz_session );
1072             if( !p_rtsp ) break;
1073
1074             for( i = 0; i < p_rtsp->i_es; i++ )
1075             {
1076                 if( p_rtsp->es[i]->p_media_es == p_es )
1077                 {
1078                     if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1079                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1080                     break;
1081                 }
1082             }
1083
1084             if( !p_rtsp->i_es )
1085             {
1086                 vod_MediaControl( p_vod, p_media, psz_session,
1087                                   VOD_MEDIA_STOP );
1088                 RtspClientDel( p_media, p_rtsp );
1089             }
1090             break;
1091
1092         case HTTPD_MSG_PAUSE:
1093             /* This is kind of a kludge. Should we only support Aggregate
1094              * Operations ? */
1095             psz_session = httpd_MsgGet( query, "Session" );
1096             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1097
1098             p_rtsp = RtspClientGet( p_media, psz_session );
1099             if( !p_rtsp ) break;
1100
1101             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1102             p_rtsp->b_paused = VLC_TRUE;
1103
1104             answer->i_status = 200;
1105             answer->psz_status = strdup( "OK" );
1106             answer->i_body = 0;
1107             answer->p_body = NULL;
1108             break;
1109
1110         default:
1111             return VLC_EGENERIC;
1112             break;
1113     }
1114
1115     httpd_MsgAdd( answer, "Server", "VLC Server" );
1116     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1117     httpd_MsgAdd( answer, "Cseq", "%d",
1118                   atoi( httpd_MsgGet( query, "Cseq" ) ) );
1119     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1120
1121     if( psz_session )
1122         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1123
1124     return VLC_SUCCESS;
1125 }
1126
1127 /*****************************************************************************
1128  * SDPGenerate: TODO
1129  * FIXME: need to be moved to a common place ?
1130  *****************************************************************************/
1131 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1132 {
1133     int i, i_size;
1134     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1135     const char *psz_control;
1136
1137     if( httpd_ServerIP( cl, ip ) == NULL )
1138         return NULL;
1139
1140     p = strchr( ip, '%' );
1141     if( p != NULL )
1142         *p = '\0'; /* remove scope if present */
1143
1144     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1145
1146     /* Calculate size */
1147     i_size = sizeof( "v=0\r\n" ) +
1148         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1149         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1150         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1151         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1152         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1153         sizeof( "t=0 0\r\n" ) + /* FIXME */
1154         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1155         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1156         sizeof( "a=range:npt=0-1000000000.000\r\n" );
1157
1158     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1159                                : p_media->psz_rtsp_control_v4;
1160     for( i = 0; i < p_media->i_es; i++ )
1161     {
1162         media_es_t *p_es = p_media->es[i];
1163
1164         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1165         if( p_es->psz_rtpmap )
1166         {
1167             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1168                 strlen( p_es->psz_rtpmap ) + 9;
1169         }
1170         if( p_es->psz_fmtp )
1171         {
1172             i_size += sizeof( "a=fmtp:* *\r\n" ) +
1173                 strlen( p_es->psz_fmtp ) + 9;
1174         }
1175     }
1176     i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1177
1178     p = psz_sdp = malloc( i_size );
1179     p += sprintf( p, "v=0\r\n" );
1180     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1181                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1182     if( *p_media->psz_session_name )
1183         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1184     if( *p_media->psz_session_description )
1185         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1186     if( *p_media->psz_session_url )
1187         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1188     if( *p_media->psz_session_email )
1189         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1190
1191     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1192     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1193
1194     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1195
1196     if( p_media->i_length > 0 )
1197     {
1198         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1199         p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
1200                       (unsigned)d.rem );
1201     }
1202
1203     for( i = 0; i < p_media->i_es; i++ )
1204     {
1205         media_es_t *p_es = p_media->es[i];
1206
1207         if( p_es->fmt.i_cat == AUDIO_ES )
1208         {
1209             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1210                           p_es->i_port, p_es->i_payload_type );
1211         }
1212         else if( p_es->fmt.i_cat == VIDEO_ES )
1213         {
1214             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1215                           p_es->i_port, p_es->i_payload_type );
1216         }
1217         else
1218         {
1219             continue;
1220         }
1221
1222         if( p_es->psz_rtpmap )
1223         {
1224             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1225                           p_es->psz_rtpmap );
1226         }
1227         if( p_es->psz_fmtp )
1228         {
1229             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1230                           p_es->psz_fmtp );
1231         }
1232
1233         p += sprintf( p, psz_control, ip, i );
1234     }
1235
1236     return psz_sdp;
1237 }