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