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