]> git.sesse.net Git - vlc/blob - modules/misc/rtsp.c
* Fix searching in aggregate mode for the RTSP VoD module. (fixes #655)
[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( 'm', 'p', '4', 'v' ):
452             p_es->i_payload_type = p_media->i_payload_type++;
453             p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
454             if( p_fmt->i_extra > 0 )
455             {
456                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
457                 p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
458                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
459                 sprintf( p_es->psz_fmtp,
460                         "profile-level-id=3; config=%s;", p_hexa );
461                 free( p_hexa );
462             }
463             break;
464         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
465             p_es->i_payload_type = p_media->i_payload_type++;
466             p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
467             sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
468             if( p_fmt->i_extra > 0 )
469             {
470                 char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
471                 p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
472                 sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
473                 sprintf( p_es->psz_fmtp,
474                         "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
475                         "config=%s; SizeLength=13;IndexLength=3; "
476                         "IndexDeltaLength=3; Profile=1;", p_hexa );
477                 free( p_hexa );
478             }
479             break;
480         case VLC_FOURCC( 'm', 'p', '2', 't' ):
481             p_media->psz_mux = "ts";
482             p_es->i_payload_type = 33;
483             p_es->psz_rtpmap = strdup( "MP2T/90000" );
484             break;
485         case VLC_FOURCC( 'm', 'p', '2', 'p' ):
486             p_media->psz_mux = "ps";
487             p_es->i_payload_type = p_media->i_payload_type++;
488             p_es->psz_rtpmap = strdup( "MP2P/90000" );
489             break;
490         case VLC_FOURCC( 's', 'a', 'm', 'r' ):
491             p_es->i_payload_type = p_media->i_payload_type++;
492             p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
493                                     "AMR/8000/2" : "AMR/8000" );
494             p_es->psz_fmtp = strdup( "octet-align=1" );
495             break;
496         case VLC_FOURCC( 's', 'a', 'w', 'b' ):
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-WB/16000/2" : "AMR-WB/16000" );
500             p_es->psz_fmtp = strdup( "octet-align=1" );
501             break;
502
503         default:
504             msg_Err( p_vod, "cannot add this stream (unsupported "
505                     "codec: %4.4s)", (char*)&p_fmt->i_codec );
506             free( p_es );
507             return VLC_EGENERIC;
508     }
509
510     p_es->p_rtsp_url =
511         httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
512                             NULL );
513
514     if( !p_es->p_rtsp_url )
515     {
516         msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
517         free( psz_urlc );
518         free( p_es );
519         return VLC_EGENERIC;
520     }
521     free( psz_urlc );
522
523     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
524                     RtspCallbackES, (void*)p_es );
525     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
526                     RtspCallbackES, (void*)p_es );
527     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
528                     RtspCallbackES, (void*)p_es );
529     httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
530                     RtspCallbackES, (void*)p_es );
531
532     es_format_Copy( &p_es->fmt, p_fmt );
533     p_es->p_vod = p_vod;
534     p_es->p_media = p_media;
535
536 #if 0
537     /* Choose the port */
538     if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
539     {
540         p_es->i_port = p_media->i_port_audio;
541         p_media->i_port_audio = 0;
542     }
543     else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
544     {
545         p_es->i_port = p_media->i_port_video;
546         p_media->i_port_video = 0;
547     }
548     while( !p_es->i_port )
549     {
550         if( p_media->i_port != p_media->i_port_audio &&
551             p_media->i_port != p_media->i_port_video )
552         {
553             p_es->i_port = p_media->i_port;
554             p_media->i_port += 2;
555             break;
556         }
557         p_media->i_port += 2;
558     }
559 #else
560
561     p_es->i_port = 0;
562 #endif
563
564     vlc_mutex_lock( &p_media->lock );
565     TAB_APPEND( p_media->i_es, p_media->es, p_es );
566     vlc_mutex_unlock( &p_media->lock );
567
568     p_media->i_sdp_version++;
569
570     return VLC_SUCCESS;
571 }
572
573 static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
574 {
575     media_es_t *p_es = 0;
576     int i;
577
578     /* Find the ES */
579     for( i = 0; i < p_media->i_es; i++ )
580     {
581         if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
582             p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
583             p_media->es[i]->fmt.i_id == p_fmt->i_id )
584         {
585             p_es = p_media->es[i];
586         }
587     }
588     if( !p_es ) return;
589
590     msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );
591
592     vlc_mutex_lock( &p_media->lock );
593     TAB_REMOVE( p_media->i_es, p_media->es, p_es );
594     vlc_mutex_unlock( &p_media->lock );
595
596     if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
597     if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
598     p_media->i_sdp_version++;
599
600     if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
601     es_format_Clean( &p_es->fmt );
602 }
603
604 /****************************************************************************
605  * RTSP server implementation
606  ****************************************************************************/
607 static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
608 {
609     rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
610
611     if( !p_rtsp ) return NULL;
612     memset( p_rtsp, 0, sizeof(rtsp_client_t) );
613     p_rtsp->es = 0;
614
615     p_rtsp->psz_session = psz_session;
616     TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
617
618     p_media->p_vod->p_sys->i_connections++;
619     msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
620              psz_session, p_media->p_vod->p_sys->i_throttle_users );
621
622     return p_rtsp;
623 }
624
625 static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
626 {
627     int i;
628
629     for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
630     {
631         if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
632         {
633             return p_media->rtsp[i];
634         }
635     }
636
637     return NULL;
638 }
639
640 static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
641 {
642     p_media->p_vod->p_sys->i_connections--;
643     msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
644              p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
645
646     while( p_rtsp->i_es-- )
647     {
648         if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
649             free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
650         free( p_rtsp->es[p_rtsp->i_es] );
651         if( !p_rtsp->i_es ) free( p_rtsp->es );
652     }
653
654     TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
655
656     free( p_rtsp->psz_session );
657     free( p_rtsp );
658 }
659
660 static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
661                          httpd_message_t *answer, httpd_message_t *query )
662 {
663     vod_media_t *p_media = (vod_media_t*)p_args;
664     vod_t *p_vod = p_media->p_vod;
665     char *psz_transport = NULL;
666     char *psz_playnow = NULL; /* support option: x-playNow */
667     char *psz_session = NULL;
668     char *psz_cseq = NULL;
669     rtsp_client_t *p_rtsp;
670     int i_port = 0;
671     int i_cseq = 0;
672
673     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
674
675     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
676
677     answer->i_proto   = HTTPD_PROTO_RTSP;
678     answer->i_version = query->i_version;
679     answer->i_type    = HTTPD_MSG_ANSWER;
680     answer->i_body    = 0;
681     answer->p_body      = NULL;
682
683     switch( query->i_type )
684     {
685         case HTTPD_MSG_SETUP:
686         {
687             psz_playnow = httpd_MsgGet( query, "x-playNow" );
688             psz_transport = httpd_MsgGet( query, "Transport" );
689             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
690
691             if( strstr( psz_transport, "unicast" ) &&
692                 strstr( psz_transport, "client_port=" ) )
693             {
694                 rtsp_client_t *p_rtsp;
695                 char ip[NI_MAXNUMERICHOST];
696                 i_port = atoi( strstr( psz_transport, "client_port=" ) +
697                                 strlen("client_port=") );
698
699                 if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
700                     strstr( psz_transport, "RAW/RAW/UDP" ) )
701                 {
702                     p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
703                     p_media->b_raw = VLC_TRUE;
704                 }
705
706                 if( httpd_ClientIP( cl, ip ) == NULL )
707                 {
708                     answer->i_status = 500;
709                     answer->psz_status = strdup( "Internal server error" );
710                     answer->i_body = 0;
711                     answer->p_body = NULL;
712                     break;
713                 }
714
715                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
716                          ip, i_port );
717
718                 psz_session = httpd_MsgGet( query, "Session" );
719                 if( !psz_session || !*psz_session )
720                 {
721                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
722                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
723                     {
724                         answer->i_status = 503;
725                         answer->psz_status = strdup( "Too many connections" );
726                         answer->i_body = 0;
727                         answer->p_body = NULL;
728                         break;
729                     }
730                     asprintf( &psz_session, "%d", rand() );
731                     p_rtsp = RtspClientNew( p_media, psz_session );
732                 }
733                 else
734                 {
735                     p_rtsp = RtspClientGet( p_media, psz_session );
736                     if( !p_rtsp )
737                     {
738                         answer->i_status = 454;
739                         answer->psz_status = strdup( "Unknown session id" );
740                         answer->i_body = 0;
741                         answer->p_body = NULL;
742                         break;
743                     }
744                 }
745
746                 answer->i_status = 200;
747                 answer->psz_status = strdup( "OK" );
748                 answer->i_body = 0;
749                 answer->p_body = NULL;
750
751                 if( p_media->b_raw )
752                 {
753                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
754                     {
755                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
756                                       i_port, i_port + 1 );
757                     }
758                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
759                     {
760                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
761                                       i_port, i_port + 1 );
762                     }
763                 }
764                 else
765                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
766                                   i_port, i_port + 1 );
767             }
768             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
769             {
770                 answer->i_status = 461;
771                 answer->psz_status = strdup( "Unsupported Transport" );
772                 answer->i_body = 0;
773                 answer->p_body = NULL;
774             }
775
776             /* Intentional fall-through on x-playNow option in RTSP request */
777             if( !psz_playnow )
778                 break;
779         }
780
781         case HTTPD_MSG_PLAY:
782         {
783             char *psz_output, *psz_position, ip[NI_MAXNUMERICHOST];
784             int i, i_port_audio = 0, i_port_video = 0;
785
786             /* for now only multicast so easy */
787             if( !psz_playnow )
788             {
789                 answer->i_status = 200;
790                 answer->psz_status = strdup( "OK" );
791                 answer->i_body = 0;
792                 answer->p_body = NULL;
793             }
794
795             if( !psz_session )
796                 psz_session = httpd_MsgGet( query, "Session" );
797             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
798
799             p_rtsp = RtspClientGet( p_media, psz_session );
800             if( !p_rtsp ) break;
801
802             if( p_rtsp->b_playing )
803             {
804                 char *psz_position = httpd_MsgGet( query, "Range" );
805                 if( psz_position ) psz_position = strstr( psz_position, "npt=" );
806                 if( psz_position )
807                 {
808                     double f_pos;
809                     char *end;
810
811                     msg_Dbg( p_vod, "seeking request: %s", psz_position );
812
813                     psz_position += 4;
814                     /* FIXME: npt= is not necessarily formatted as a float */
815                     f_pos = us_strtod( psz_position, &end );
816                     if( end > psz_position )
817                     {
818                         f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
819                         vod_MediaControl( p_vod, p_media, psz_session,
820                                       VOD_MEDIA_SEEK, f_pos );
821                     }
822                     break;
823                 }
824             }
825
826             if( p_rtsp->b_playing && p_rtsp->b_paused )
827             {
828                 vod_MediaControl( p_vod, p_media, psz_session,
829                                   VOD_MEDIA_PAUSE );
830                 p_rtsp->b_paused = VLC_FALSE;
831                 break;
832             }
833             else if( p_rtsp->b_playing ) break;
834
835             if( httpd_ClientIP( cl, ip ) == NULL ) break;
836
837             p_rtsp->b_playing = VLC_TRUE;
838
839             /* FIXME for != 1 video and 1 audio */
840             for( i = 0; i < p_rtsp->i_es; i++ )
841             {
842                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
843                     i_port_audio = p_rtsp->es[i]->i_port;
844                 if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
845                     i_port_video = p_rtsp->es[i]->i_port;
846             }
847
848             if( p_media->psz_mux )
849             {
850                 if( p_media->b_raw )
851                 {
852                     asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
853                               ip, i_port, p_media->psz_mux );
854                 }
855                 else
856                 {
857                     asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
858                               ip, i_port_video, p_media->psz_mux );
859                 }
860             }
861             else
862             {
863                 asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
864                           "port-audio=%i}", ip, i_port_video, i_port_audio );
865             }
866
867             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
868                               psz_output );
869             free( psz_output );
870             break;
871         }
872
873         case HTTPD_MSG_DESCRIBE:
874         {
875             char *psz_sdp =
876                 SDPGenerate( p_media, cl );
877
878             if( psz_sdp != NULL )
879             {
880                 answer->i_status = 200;
881                 answer->psz_status = strdup( "OK" );
882                 httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
883
884                 answer->p_body = (uint8_t *)psz_sdp;
885                 answer->i_body = strlen( psz_sdp );
886             }
887             else
888             {
889                 answer->i_status = 500;
890                 answer->psz_status = strdup( "Internal server error" );
891                 answer->p_body = NULL;
892                 answer->i_body = 0;
893             }
894             break;
895         }
896
897         case HTTPD_MSG_PAUSE:
898             psz_session = httpd_MsgGet( query, "Session" );
899             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
900
901             p_rtsp = RtspClientGet( p_media, psz_session );
902             if( !p_rtsp ) break;
903
904             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
905             p_rtsp->b_paused = VLC_TRUE;
906
907             answer->i_status = 200;
908             answer->psz_status = strdup( "OK" );
909             answer->i_body = 0;
910             answer->p_body = NULL;
911             break;
912
913         case HTTPD_MSG_TEARDOWN:
914             /* for now only multicast so easy again */
915             answer->i_status = 200;
916             answer->psz_status = strdup( "OK" );
917             answer->i_body = 0;
918             answer->p_body = NULL;
919
920             psz_session = httpd_MsgGet( query, "Session" );
921             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
922
923             p_rtsp = RtspClientGet( p_media, psz_session );
924             if( !p_rtsp ) break;
925
926             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
927             RtspClientDel( p_media, p_rtsp );
928             break;
929
930         case HTTPD_MSG_GETPARAMETER:
931             answer->i_status = 200;
932             answer->psz_status = strdup( "OK" );
933             answer->i_body = 0;
934             answer->p_body = NULL;
935             break;
936
937         default:
938             return VLC_EGENERIC;
939     }
940
941     httpd_MsgAdd( answer, "Server", "VLC Server" );
942     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
943     psz_cseq = httpd_MsgGet( query, "Cseq" );
944     psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
945     httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
946     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
947
948     if( psz_session )
949     {
950         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
951     }
952
953     return VLC_SUCCESS;
954 }
955
956 static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
957                            httpd_message_t *answer, httpd_message_t *query )
958 {
959     media_es_t *p_es = (media_es_t*)p_args;
960     vod_media_t *p_media = p_es->p_media;
961     vod_t *p_vod = p_media->p_vod;
962     rtsp_client_t *p_rtsp = NULL;
963     char *psz_transport = NULL;
964     char *psz_playnow = NULL; /* support option: x-playNow */
965     char *psz_session = NULL;
966     char *psz_position = NULL;
967     char *psz_cseq = NULL;
968     int i_cseq = 0;
969     int i;
970
971     if( answer == NULL || query == NULL ) return VLC_SUCCESS;
972
973     msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
974
975     answer->i_proto   = HTTPD_PROTO_RTSP;
976     answer->i_version = query->i_version;
977     answer->i_type    = HTTPD_MSG_ANSWER;
978     answer->i_body    = 0;
979     answer->p_body      = NULL;
980
981     switch( query->i_type )
982     {
983         case HTTPD_MSG_SETUP:
984             psz_playnow = httpd_MsgGet( query, "x-playNow" );
985             psz_transport = httpd_MsgGet( query, "Transport" );
986
987             msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
988
989             if( strstr( psz_transport, "unicast" ) &&
990                 strstr( psz_transport, "client_port=" ) )
991             {
992                 rtsp_client_t *p_rtsp;
993                 rtsp_client_es_t *p_rtsp_es;
994                 char ip[NI_MAXNUMERICHOST];
995                 int i_port = atoi( strstr( psz_transport, "client_port=" ) +
996                                 strlen("client_port=") );
997
998                 if( httpd_ClientIP( cl, ip ) == NULL )
999                 {
1000                     answer->i_status = 500;
1001                     answer->psz_status = strdup( "Internal server error" );
1002                     answer->i_body = 0;
1003                     answer->p_body = NULL;
1004                     break;
1005                 }
1006
1007                 msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
1008                         ip, i_port );
1009
1010                 psz_session = httpd_MsgGet( query, "Session" );
1011                 if( !psz_session || !*psz_session )
1012                 {
1013                     if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
1014                         ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1015                     {
1016                         answer->i_status = 503;
1017                         answer->psz_status = strdup( "Too many connections" );
1018                         answer->i_body = 0;
1019                         answer->p_body = NULL;
1020                         break;
1021                     }
1022                     asprintf( &psz_session, "%d", rand() );
1023                     p_rtsp = RtspClientNew( p_media, psz_session );
1024                 }
1025                 else
1026                 {
1027                     p_rtsp = RtspClientGet( p_media, psz_session );
1028                     if( !p_rtsp )
1029                     {
1030                         answer->i_status = 454;
1031                         answer->psz_status = strdup( "Unknown session id" );
1032                         answer->i_body = 0;
1033                         answer->p_body = NULL;
1034                         break;
1035                     }
1036                 }
1037
1038                 p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
1039                 p_rtsp_es->i_port = i_port;
1040                 p_rtsp_es->psz_ip = strdup( ip );
1041                 p_rtsp_es->p_media_es = p_es;
1042                 TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );
1043
1044                 answer->i_status = 200;
1045                 answer->psz_status = strdup( "OK" );
1046                 answer->i_body = 0;
1047                 answer->p_body = NULL;
1048
1049                 if( p_media->b_raw )
1050                 {
1051                     if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
1052                     {
1053                         httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
1054                                       i_port, i_port + 1 );
1055                     }
1056                     else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
1057                     {
1058                         httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
1059                                       i_port, i_port + 1 );
1060                     }
1061                 }
1062                 else
1063                 {
1064                     httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
1065                                   i_port, i_port + 1 );
1066                 }
1067             }
1068             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1069             {
1070                 answer->i_status = 461;
1071                 answer->psz_status = strdup( "Unsupported Transport" );
1072                 answer->i_body = 0;
1073                 answer->p_body = NULL;
1074             }
1075
1076             /* Intentional fall-through on x-playNow option in RTSP request */
1077             if( !psz_playnow )
1078                 break;
1079
1080         case HTTPD_MSG_PLAY:
1081             /* This is kind of a kludge. Should we only support Aggregate
1082              * Operations ? */
1083             psz_session = httpd_MsgGet( query, "Session" );
1084             msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );
1085
1086             p_rtsp = RtspClientGet( p_media, psz_session );
1087
1088             psz_position = httpd_MsgGet( query, "Range" );
1089             if( psz_position ) psz_position = strstr( psz_position, "npt=" );
1090             if( psz_position )
1091             {
1092                 double f_pos;
1093                 char *end;
1094
1095                 msg_Dbg( p_vod, "seeking request: %s", psz_position );
1096
1097                 psz_position += 4;
1098                 /* FIXME: npt= is not necessarily formatted as a float */
1099                 f_pos = us_strtod( psz_position, &end );
1100                 if( end > psz_position )
1101                 {
1102                     f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1103                     vod_MediaControl( p_vod, p_media, psz_session,
1104                                       VOD_MEDIA_SEEK, f_pos );
1105                 }
1106             }
1107
1108             if( !psz_playnow )
1109             {
1110                 answer->i_status = 200;
1111                 answer->psz_status = strdup( "OK" );
1112                 answer->i_body = 0;
1113                 answer->p_body = NULL;
1114             }
1115             break;
1116
1117         case HTTPD_MSG_TEARDOWN:
1118             answer->i_status = 200;
1119             answer->psz_status = strdup( "OK" );
1120             answer->i_body = 0;
1121             answer->p_body = NULL;
1122
1123             psz_session = httpd_MsgGet( query, "Session" );
1124             msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);
1125
1126             p_rtsp = RtspClientGet( p_media, psz_session );
1127             if( !p_rtsp ) break;
1128
1129             for( i = 0; i < p_rtsp->i_es; i++ )
1130             {
1131                 if( p_rtsp->es[i]->p_media_es == p_es )
1132                 {
1133                     if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
1134                     TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
1135                     break;
1136                 }
1137             }
1138
1139             if( !p_rtsp->i_es )
1140             {
1141                 vod_MediaControl( p_vod, p_media, psz_session,
1142                                   VOD_MEDIA_STOP );
1143                 RtspClientDel( p_media, p_rtsp );
1144             }
1145             break;
1146
1147         case HTTPD_MSG_PAUSE:
1148             /* This is kind of a kludge. Should we only support Aggregate
1149              * Operations ? */
1150             psz_session = httpd_MsgGet( query, "Session" );
1151             msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
1152
1153             p_rtsp = RtspClientGet( p_media, psz_session );
1154             if( !p_rtsp ) break;
1155
1156             vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
1157             p_rtsp->b_paused = VLC_TRUE;
1158
1159             answer->i_status = 200;
1160             answer->psz_status = strdup( "OK" );
1161             answer->i_body = 0;
1162             answer->p_body = NULL;
1163             break;
1164
1165         default:
1166             return VLC_EGENERIC;
1167             break;
1168     }
1169
1170     httpd_MsgAdd( answer, "Server", "VLC Server" );
1171     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1172     psz_cseq = httpd_MsgGet( query, "Cseq" );
1173     if (psz_cseq)
1174         i_cseq = atoi( psz_cseq );
1175     else
1176         i_cseq = 0;
1177     httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1178     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1179
1180     if( psz_session )
1181         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1182
1183     return VLC_SUCCESS;
1184 }
1185
1186 /*****************************************************************************
1187  * SDPGenerate: TODO
1188  * FIXME: need to be moved to a common place ?
1189  *****************************************************************************/
1190 static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1191 {
1192     int i, i_size;
1193     char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1194     const char *psz_control;
1195
1196     if( httpd_ServerIP( cl, ip ) == NULL )
1197         return NULL;
1198
1199     p = strchr( ip, '%' );
1200     if( p != NULL )
1201         *p = '\0'; /* remove scope if present */
1202
1203     ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1204
1205     /* Calculate size */
1206     i_size = sizeof( "v=0\r\n" ) +
1207         sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
1208         sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
1209         sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
1210         sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
1211         sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1212         sizeof( "t=0 0\r\n" ) + /* FIXME */
1213         sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
1214         sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1215         sizeof( "a=range:npt=0-1000000000.000\r\n" );
1216
1217     psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
1218                                : p_media->psz_rtsp_control_v4;
1219     for( i = 0; i < p_media->i_es; i++ )
1220     {
1221         media_es_t *p_es = p_media->es[i];
1222
1223         i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1224         if( p_es->psz_rtpmap )
1225         {
1226             i_size += sizeof( "a=rtpmap:* *\r\n" ) +
1227                 strlen( p_es->psz_rtpmap ) + 9;
1228         }
1229         if( p_es->psz_fmtp )
1230         {
1231             i_size += sizeof( "a=fmtp:* *\r\n" ) +
1232                 strlen( p_es->psz_fmtp ) + 9;
1233         }
1234     }
1235     i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1236
1237     p = psz_sdp = malloc( i_size );
1238     p += sprintf( p, "v=0\r\n" );
1239     p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
1240                   p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1241     if( *p_media->psz_session_name )
1242         p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
1243     if( *p_media->psz_session_description )
1244         p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
1245     if( *p_media->psz_session_url )
1246         p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
1247     if( *p_media->psz_session_email )
1248         p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );
1249
1250     p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1251     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1252
1253     p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
1254
1255     if( p_media->i_length > 0 )
1256     {
1257         lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1258         p += sprintf( p, "a=range:npt=0-"I64Fd".%03u\r\n", d.quot,
1259                       (unsigned)d.rem );
1260     }
1261
1262     for( i = 0; i < p_media->i_es; i++ )
1263     {
1264         media_es_t *p_es = p_media->es[i];
1265
1266         if( p_es->fmt.i_cat == AUDIO_ES )
1267         {
1268             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
1269                           p_es->i_port, p_es->i_payload_type );
1270         }
1271         else if( p_es->fmt.i_cat == VIDEO_ES )
1272         {
1273             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
1274                           p_es->i_port, p_es->i_payload_type );
1275         }
1276         else
1277         {
1278             continue;
1279         }
1280
1281         if( p_es->psz_rtpmap )
1282         {
1283             p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
1284                           p_es->psz_rtpmap );
1285         }
1286         if( p_es->psz_fmtp )
1287         {
1288             p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
1289                           p_es->psz_fmtp );
1290         }
1291
1292         p += sprintf( p, psz_control, ip, i );
1293     }
1294
1295     return psz_sdp;
1296 }