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