]> git.sesse.net Git - vlc/blob - modules/stream_out/rtp.c
e03ea10f8e5dfe9cd687f847647ff6026a30d3fb
[vlc] / modules / stream_out / rtp.c
1 /*****************************************************************************
2  * rtp.c: rtp stream output module
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/input.h>
33 #include <vlc/sout.h>
34
35 #include "vlc_httpd.h"
36 #include "network.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define DST_TEXT N_("Destination")
42 #define DST_LONGTEXT N_( \
43     "Allows you to specify the output URL used for the streaming output." )
44 #define SDP_TEXT N_("SDP")
45 #define SDP_LONGTEXT N_( \
46     "Allows you to specify the SDP used for the streaming output. " \
47     "You must use an url: http://location to access the SDP via HTTP, " \
48     "rtsp://location for RTSP access, and sap:// for the SDP to be " \
49     "announced via SAP." )
50 #define MUX_TEXT N_("Muxer")
51 #define MUX_LONGTEXT N_( \
52     "Allows you to specify the muxer used for the streaming output." )
53
54 #define NAME_TEXT N_("Session name")
55 #define NAME_LONGTEXT N_( \
56     "Allows you to specify the session name used for the streaming output." )
57 #define DESC_TEXT N_("Session description")
58 #define DESC_LONGTEXT N_( \
59     "Allows you to give a broader description of the stream." )
60 #define URL_TEXT N_("Session URL")
61 #define URL_LONGTEXT N_( \
62     "Allows you to specify a URL with additional information on the stream." )
63 #define EMAIL_TEXT N_("Session email")
64 #define EMAIL_LONGTEXT N_( \
65     "Allows you to specify contact e-mail address for this session." )
66
67 #define PORT_TEXT N_("Port")
68 #define PORT_LONGTEXT N_( \
69     "Allows you to specify the base port used for the RTP streaming." )
70 #define PORT_AUDIO_TEXT N_("Audio port")
71 #define PORT_AUDIO_LONGTEXT N_( \
72     "Allows you to specify the default audio port used for the RTP streaming." )
73 #define PORT_VIDEO_TEXT N_("Video port")
74 #define PORT_VIDEO_LONGTEXT N_( \
75     "Allows you to specify the default video port used for the RTP streaming." )
76
77 #define TTL_TEXT N_("Time to live")
78 #define TTL_LONGTEXT N_( \
79     "Allows you to specify the time to live for the output stream." )
80
81 static int  Open ( vlc_object_t * );
82 static void Close( vlc_object_t * );
83
84 #define SOUT_CFG_PREFIX "sout-rtp-"
85
86 vlc_module_begin();
87     set_description( _("RTP stream output") );
88     set_capability( "sout stream", 0 );
89     add_shortcut( "rtp" );
90
91     add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
92                 DST_LONGTEXT, VLC_TRUE );
93     add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
94                 SDP_LONGTEXT, VLC_TRUE );
95     add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
96                 MUX_LONGTEXT, VLC_TRUE );
97
98     add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
99                 NAME_LONGTEXT, VLC_TRUE );
100     add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
101                 DESC_LONGTEXT, VLC_TRUE );
102     add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
103                 URL_LONGTEXT, VLC_TRUE );
104     add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
105                 EMAIL_LONGTEXT, VLC_TRUE );
106
107     add_integer( SOUT_CFG_PREFIX "port-audio", 1234, NULL, PORT_AUDIO_TEXT,
108                  PORT_LONGTEXT, VLC_TRUE );
109     add_integer( SOUT_CFG_PREFIX "port-video", 1236, NULL, PORT_VIDEO_TEXT,
110                  PORT_LONGTEXT, VLC_TRUE );
111     add_integer( SOUT_CFG_PREFIX "port", 1238, NULL, PORT_TEXT,
112                  PORT_LONGTEXT, VLC_TRUE );
113
114     add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
115                  TTL_LONGTEXT, VLC_TRUE );
116
117     set_callbacks( Open, Close );
118 vlc_module_end();
119
120 /*****************************************************************************
121  * Exported prototypes
122  *****************************************************************************/
123 static const char *ppsz_sout_options[] = {
124     "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
125     "description", "url","email", NULL
126 };
127
128 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
129 static int               Del ( sout_stream_t *, sout_stream_id_t * );
130 static int               Send( sout_stream_t *, sout_stream_id_t *,
131                                block_t* );
132
133 /* For unicast/interleaved streaming */
134 typedef struct
135 {
136     char    *psz_session;
137     int64_t i_last; /* for timeout */
138
139     /* is it in "play" state */
140     vlc_bool_t b_playing;
141
142     /* output (id-access) */
143     int               i_id;
144     sout_stream_id_t  **id;
145     int               i_access;
146     sout_access_out_t **access;
147 } rtsp_client_t;
148
149 struct sout_stream_sys_t
150 {
151     /* sdp */
152     int64_t i_sdp_id;
153     int     i_sdp_version;
154     char    *psz_sdp;
155     vlc_mutex_t  lock_sdp;
156
157     char        *psz_session_name;
158     char        *psz_session_description;
159     char        *psz_session_url;
160     char        *psz_session_email;
161
162     /* */
163     vlc_bool_t b_export_sdp_file;
164     char *psz_sdp_file;
165     /* sap */
166     vlc_bool_t b_export_sap;
167     session_descriptor_t *p_session;
168
169     httpd_host_t *p_httpd_host;
170     httpd_file_t *p_httpd_file;
171
172     httpd_host_t *p_rtsp_host;
173     httpd_url_t  *p_rtsp_url;
174     char         *psz_rtsp_control;
175     char         *psz_rtsp_path;
176
177     /* */
178     char *psz_destination;
179     int  i_port;
180     int  i_port_audio;
181     int  i_port_video;
182     int  i_ttl;
183
184     /* when need to use a private one or when using muxer */
185     int i_payload_type;
186
187     /* in case we do TS/PS over rtp */
188     sout_mux_t        *p_mux;
189     sout_access_out_t *p_access;
190     int               i_mtu;
191     sout_access_out_t *p_grab;
192     uint16_t          i_sequence;
193     uint32_t          i_timestamp_start;
194     uint8_t           ssrc[4];
195     block_t           *packet;
196
197     /* */
198     vlc_mutex_t      lock_es;
199     int              i_es;
200     sout_stream_id_t **es;
201
202     /* */
203     int              i_rtsp;
204     rtsp_client_t    **rtsp;
205 };
206
207 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
208                                     block_t * );
209
210 struct sout_stream_id_t
211 {
212     sout_stream_t *p_stream;
213     /* rtp field */
214     uint8_t     i_payload_type;
215     uint16_t    i_sequence;
216     uint32_t    i_timestamp_start;
217     uint8_t     ssrc[4];
218
219     /* for sdp */
220     int         i_clock_rate;
221     char        *psz_rtpmap;
222     char        *psz_fmtp;
223     char        *psz_destination;
224     int         i_port;
225     int         i_cat;
226
227     /* Packetizer specific fields */
228     pf_rtp_packetizer_t pf_packetize;
229     int           i_mtu;
230
231     /* for sending the packets */
232     sout_access_out_t *p_access;
233
234     vlc_mutex_t       lock_rtsp;
235     int               i_rtsp_access;
236     sout_access_out_t **rtsp_access;
237
238     /* */
239     sout_input_t      *p_input;
240
241     /* RTSP url control */
242     httpd_url_t  *p_rtsp_url;
243 };
244
245 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
246
247 static void SDPHandleUrl( sout_stream_t *, char * );
248
249 static int SapSetup( sout_stream_t *p_stream );
250 static int FileSetup( sout_stream_t *p_stream );
251 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
252 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
253
254 static int  RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
255                           httpd_message_t *, httpd_message_t * );
256 static int  RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
257                             httpd_message_t *, httpd_message_t * );
258
259
260 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
261 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
262 static void           RtspClientDel( sout_stream_t *, rtsp_client_t * );
263
264 /*****************************************************************************
265  * Open:
266  *****************************************************************************/
267 static int Open( vlc_object_t *p_this )
268 {
269     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
270     sout_instance_t     *p_sout = p_stream->p_sout;
271     sout_stream_sys_t   *p_sys;
272     vlc_value_t         val;
273
274     sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
275
276     p_sys = malloc( sizeof( sout_stream_sys_t ) );
277
278     p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
279     if( *p_sys->psz_destination == '\0' )
280     {
281         free( p_sys->psz_destination );
282         p_sys->psz_destination = NULL;
283     }
284
285     p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
286     p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
287     p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
288     p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
289
290     p_sys->i_port       = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
291     p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
292     p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
293
294     p_sys->psz_sdp_file = NULL;
295
296     if( p_sys->i_port_audio == p_sys->i_port_video )
297     {
298         msg_Err( p_stream, "audio and video port cannot be the same" );
299         p_sys->i_port_audio = 0;
300         p_sys->i_port_video = 0;
301     }
302
303     if( !p_sys->psz_session_name )
304     {
305         if( p_sys->psz_destination )
306             p_sys->psz_session_name = strdup( p_sys->psz_destination );
307         else
308            p_sys->psz_session_name = strdup( "NONE" );
309     }
310
311     if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
312     {
313         sout_cfg_t *p_cfg;
314         vlc_bool_t b_ok = VLC_FALSE;
315
316         for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
317         {
318             if( !strcmp( p_cfg->psz_name, "sdp" ) )
319             {
320                 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
321                 {
322                     b_ok = VLC_TRUE;
323                     break;
324                 }
325             }
326         }
327         if( !b_ok )
328         {
329             vlc_value_t val2;
330             var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
331             if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
332                 b_ok = VLC_TRUE;
333             free( val2.psz_string );
334         }
335
336         if( !b_ok )
337         {
338             msg_Err( p_stream, "missing destination and not in rtsp mode" );
339             free( p_sys );
340             return VLC_EGENERIC;
341         }
342         p_sys->psz_destination = NULL;
343     }
344     else if( p_sys->i_port <= 0 )
345     {
346         msg_Err( p_stream, "invalid port" );
347         free( p_sys );
348         return VLC_EGENERIC;
349     }
350
351     var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
352     p_sys->i_ttl = val.i_int;
353
354     p_sys->i_payload_type = 96;
355     p_sys->i_es = 0;
356     p_sys->es   = NULL;
357     p_sys->i_rtsp = 0;
358     p_sys->rtsp   = NULL;
359     p_sys->psz_sdp = NULL;
360
361     p_sys->i_sdp_id = mdate();
362     p_sys->i_sdp_version = 1;
363     p_sys->psz_sdp = NULL;
364
365     p_sys->b_export_sap = VLC_FALSE;
366     p_sys->b_export_sdp_file = VLC_FALSE;
367     p_sys->p_session = NULL;
368
369     p_sys->p_httpd_host = NULL;
370     p_sys->p_httpd_file = NULL;
371     p_sys->p_rtsp_host  = NULL;
372     p_sys->p_rtsp_url   = NULL;
373     p_sys->psz_rtsp_control = NULL;
374     p_sys->psz_rtsp_path = NULL;
375
376     vlc_mutex_init( p_stream, &p_sys->lock_sdp );
377     vlc_mutex_init( p_stream, &p_sys->lock_es );
378
379     p_stream->pf_add    = Add;
380     p_stream->pf_del    = Del;
381     p_stream->pf_send   = Send;
382
383     p_stream->p_sys     = p_sys;
384
385     var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
386     if( *val.psz_string )
387     {
388         sout_access_out_t *p_grab;
389
390         char *psz_rtpmap;
391         char access[100];
392         char url[p_sys->psz_destination ? strlen( p_sys->psz_destination ) + 1 + 12+1 : 14];
393
394         /* Check muxer type */
395         if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
396         {
397             psz_rtpmap = "MP2P/90000";
398         }
399         else if( !strncasecmp( val.psz_string, "ts", 2 ) )
400         {
401             psz_rtpmap = "MP2T/90000";
402             p_sys->i_payload_type = 33;
403         }
404         else
405         {
406             msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
407             return VLC_EGENERIC;
408         }
409
410         /* create the access out */
411         if( p_sys->i_ttl > 0 )
412         {
413             sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
414         }
415         else
416         {
417             sprintf( access, "udp{raw}" );
418         }
419         sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
420         if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
421         {
422             msg_Err( p_stream, "cannot create the access out for %s://%s",
423                      access, url );
424             free( p_sys );
425             return VLC_EGENERIC;
426         }
427         p_sys->i_mtu = config_GetInt( p_stream, "mtu" );  /* XXX beurk */
428         if( p_sys->i_mtu <= 16 )
429         {
430             /* better than nothing */
431             p_sys->i_mtu = 1500;
432         }
433
434         /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
435         p_grab = p_sys->p_grab =
436             vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
437         p_grab->p_module    = NULL;
438         p_grab->p_sout      = p_sout;
439         p_grab->psz_access  = strdup( "grab" );
440         p_grab->p_cfg       = NULL;
441         p_grab->psz_name    = strdup( "" );
442         p_grab->p_sys       = (sout_access_out_sys_t*)p_stream;
443         p_grab->pf_seek     = NULL;
444         p_grab->pf_write    = AccessOutGrabberWrite;
445
446         /* the muxer */
447         if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
448         {
449             msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
450             sout_AccessOutDelete( p_sys->p_grab );
451             sout_AccessOutDelete( p_sys->p_access );
452             free( p_sys );
453             return VLC_EGENERIC;
454         }
455
456         /* create the SDP for a muxed stream (only once) */
457         /* FIXME  http://www.faqs.org/rfcs/rfc2327.html
458            All text fields should be UTF-8 encoded. Use global a:charset to announce this.
459            o= - should be local username (no spaces allowed)
460            o= time should be hashed with some other value to garantue uniqueness
461            o= we need IP6 support?
462            o= don't use the localhost address. use fully qualified domain name or IP4 address
463            p= international phone number (pass via vars?)
464            c= IP6 support
465            c= /ttl should only be added in case of multicast
466            a= recvonly (missing)
467            a= type:broadcast (missing)
468            a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
469            a= x-plgroup: (missing)
470            RTP packets need to get the correct src IP address  */
471         p_sys->psz_sdp =
472             malloc( 10 + 30 + 10 + strlen( p_sys->psz_session_name ) +
473                     10 + strlen( p_sys->psz_session_description ) + 10 + strlen( p_sys->psz_session_url ) +
474                     10 + strlen( p_sys->psz_session_email ) + 10 + strlen( p_sys->psz_destination ) +
475                     10 + 10 + strlen( PACKAGE_STRING ) +
476                     20 + 10 + 20 + 10 + strlen( psz_rtpmap ) );
477         sprintf( p_sys->psz_sdp,
478                  "v=0\r\n"
479                  "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
480                  "s=%s\r\n"
481                  "i=%s\r\n"
482                  "u=%s\r\n"
483                  "e=%s\r\n"
484                  "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
485                  "a=tool:"PACKAGE_STRING"\r\n"
486                  "c=IN IP4 %s/%d\r\n"
487                  "m=video %d RTP/AVP %d\r\n"
488                  "a=rtpmap:%d %s\r\n",
489                  p_sys->i_sdp_id, p_sys->i_sdp_version,
490                  p_sys->psz_session_name,
491                  p_sys->psz_session_description,
492                  p_sys->psz_session_url,
493                  p_sys->psz_session_email,
494                  p_sys->psz_destination, p_sys->i_ttl,
495                  p_sys->i_port, p_sys->i_payload_type,
496                  p_sys->i_payload_type, psz_rtpmap );
497         fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
498
499         /* create the rtp context */
500         p_sys->ssrc[0] = rand()&0xff;
501         p_sys->ssrc[1] = rand()&0xff;
502         p_sys->ssrc[2] = rand()&0xff;
503         p_sys->ssrc[3] = rand()&0xff;
504         p_sys->i_sequence = rand()&0xffff;
505         p_sys->i_timestamp_start = rand()&0xffffffff;
506         p_sys->packet = NULL;
507     }
508     else
509     {
510         p_sys->p_mux    = NULL;
511         p_sys->p_access = NULL;
512         p_sys->p_grab   = NULL;
513     }
514     free( val.psz_string );
515
516
517     var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
518     if( *val.psz_string )
519     {
520         sout_cfg_t *p_cfg;
521
522         SDPHandleUrl( p_stream, val.psz_string );
523
524         for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
525         {
526             if( !strcmp( p_cfg->psz_name, "sdp" ) )
527             {
528                 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
529                     continue;
530
531                 if( !strcmp( p_cfg->psz_value, val.psz_string ) )   /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
532                     continue;
533
534                 SDPHandleUrl( p_stream, p_cfg->psz_value );
535             }
536         }
537     }
538     free( val.psz_string );
539
540     /* update p_sout->i_out_pace_nocontrol */
541     p_stream->p_sout->i_out_pace_nocontrol++;
542
543     return VLC_SUCCESS;
544 }
545
546 /*****************************************************************************
547  * Close:
548  *****************************************************************************/
549 static void Close( vlc_object_t * p_this )
550 {
551     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
552     sout_stream_sys_t *p_sys = p_stream->p_sys;
553
554     /* update p_sout->i_out_pace_nocontrol */
555     p_stream->p_sout->i_out_pace_nocontrol--;
556
557     if( p_sys->p_mux )
558     {
559         sout_MuxDelete( p_sys->p_mux );
560         sout_AccessOutDelete( p_sys->p_access );
561         sout_AccessOutDelete( p_sys->p_grab );
562         if( p_sys->packet )
563         {
564             block_Release( p_sys->packet );
565         }
566     }
567
568     while( p_sys->i_rtsp > 0 )
569     {
570         RtspClientDel( p_stream, p_sys->rtsp[0] );
571     }
572
573     vlc_mutex_destroy( &p_sys->lock_sdp );
574
575     if( p_sys->p_httpd_file )
576     {
577         httpd_FileDelete( p_sys->p_httpd_file );
578     }
579     if( p_sys->p_httpd_host )
580     {
581         httpd_HostDelete( p_sys->p_httpd_host );
582     }
583     if( p_sys->p_rtsp_url )
584     {
585         httpd_UrlDelete( p_sys->p_rtsp_url );
586     }
587     if( p_sys->p_rtsp_host )
588     {
589         httpd_HostDelete( p_sys->p_rtsp_host );
590     }
591 #if 0
592     /* why? is this disabled? */
593     if( p_sys->psz_session_name )
594     {
595         free( p_sys->psz_session_name );
596         p_sys->psz_session_name = NULL;
597     }
598     if( p_sys->psz_session_description )
599     {
600         free( p_sys->psz_session_description );
601         p_sys->psz_session_description = NULL;
602     }
603     if( p_sys->psz_session_url )
604     {
605         free( p_sys->psz_session_url );
606         p_sys->psz_session_url = NULL;
607     }
608     if( p_sys->psz_session_email )
609     {
610         free( p_sys->psz_session_email );
611         p_sys->psz_session_email = NULL;
612     }
613 #endif
614     if( p_sys->psz_sdp )
615     {
616         free( p_sys->psz_sdp );
617     }
618     free( p_sys );
619 }
620
621 /*****************************************************************************
622  * SDPHandleUrl:
623  *****************************************************************************/
624 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
625 {
626     sout_stream_sys_t *p_sys = p_stream->p_sys;
627     vlc_url_t url;
628
629     vlc_UrlParse( &url, psz_url, 0 );
630     if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
631     {
632         if( p_sys->p_httpd_file )
633         {
634             msg_Err( p_stream, "You can used sdp=http:// only once" );
635             return;
636         }
637
638         if( HttpSetup( p_stream, &url ) )
639         {
640             msg_Err( p_stream, "cannot export sdp as http" );
641         }
642     }
643     else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
644     {
645         if( p_sys->p_rtsp_url )
646         {
647             msg_Err( p_stream, "You can used sdp=rtsp:// only once" );
648             return;
649         }
650
651         /* FIXME test if destination is multicast or no destination at all FIXME */
652         if( RtspSetup( p_stream, &url ) )
653         {
654             msg_Err( p_stream, "cannot export sdp as rtsp" );
655         }
656     }
657     else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) || 
658              ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
659     {
660         p_sys->b_export_sap = VLC_TRUE;
661         SapSetup( p_stream );
662     }
663     else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
664     {
665         if( p_sys->b_export_sdp_file )
666         {
667             msg_Err( p_stream, "You can used sdp=file:// only once" );
668             return;
669         }
670         p_sys->b_export_sdp_file = VLC_TRUE;
671         psz_url = &psz_url[5];
672         if( psz_url[0] == '/' && psz_url[1] == '/' )
673             psz_url += 2;
674         p_sys->psz_sdp_file = strdup( psz_url );
675     }
676     else
677     {
678         msg_Warn( p_stream, "unknown protocol for SDP (%s)",
679                   url.psz_protocol );
680     }
681     vlc_UrlClean( &url );
682 }
683
684 /*****************************************************************************
685  * SDPGenerate
686  *****************************************************************************/
687         /* FIXME  http://www.faqs.org/rfcs/rfc2327.html
688            All text fields should be UTF-8 encoded. Use global a:charset to announce this.
689            o= - should be local username (no spaces allowed)
690            o= time should be hashed with some other value to garantue uniqueness
691            o= we need IP6 support?
692            o= don't use the localhost address. use fully qualified domain name or IP4 address
693            p= international phone number (pass via vars?)
694            c= IP6 support
695            c= /ttl should only be added in case of multicast
696            a= recvonly (missing)
697            a= type:broadcast (missing)
698            a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
699            a= x-plgroup: (missing)
700            RTP packets need to get the correct src IP address  */
701 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
702 {
703     sout_stream_sys_t *p_sys = p_stream->p_sys;
704     int i_size;
705     char *psz_sdp, *p;
706     int i;
707
708     i_size = strlen( "v=0\r\n" ) +
709              strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
710              strlen( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
711              strlen( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
712              strlen( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
713              strlen( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
714              strlen( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
715              strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
716              strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
717              strlen( psz_destination ? psz_destination : "0.0.0.0") ;
718     for( i = 0; i < p_sys->i_es; i++ )
719     {
720         sout_stream_id_t *id = p_sys->es[i];
721
722         i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
723         if( id->psz_rtpmap )
724         {
725             i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
726         }
727         if( id->psz_fmtp )
728         {
729             i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
730         }
731         if( b_rtsp )
732         {
733             i_size += strlen( "a=control:*/trackid=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
734         }
735     }
736
737     p = psz_sdp = malloc( i_size );
738     p += sprintf( p, "v=0\r\n" );
739     p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
740                   p_sys->i_sdp_id, p_sys->i_sdp_version );
741     if( *p_sys->psz_session_name )
742         p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
743     if( *p_sys->psz_session_description )
744         p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
745     if( *p_sys->psz_session_url )
746         p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
747     if( *p_sys->psz_session_email )
748         p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
749
750     p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
751     p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
752
753     p += sprintf( p, "c=IN IP4 %s/%d\r\n", psz_destination ? psz_destination : "0.0.0.0",
754                   p_sys->i_ttl );
755
756     for( i = 0; i < p_sys->i_es; i++ )
757     {
758         sout_stream_id_t *id = p_sys->es[i];
759
760         if( id->i_cat == AUDIO_ES )
761         {
762             p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
763                           id->i_port, id->i_payload_type );
764         }
765         else if( id->i_cat == VIDEO_ES )
766         {
767             p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
768                           id->i_port, id->i_payload_type );
769         }
770         else
771         {
772             continue;
773         }
774         if( id->psz_rtpmap )
775         {
776             p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
777                           id->psz_rtpmap );
778         }
779         if( id->psz_fmtp )
780         {
781             p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
782                           id->psz_fmtp );
783         }
784         if( b_rtsp )
785         {
786             p += sprintf( p, "a=control:%s/trackid=%d\r\n", p_sys->psz_rtsp_control, i );
787         }
788     }
789
790     return psz_sdp;
791 }
792
793 /*****************************************************************************
794  *
795  *****************************************************************************/
796 static int rtp_packetize_l16  ( sout_stream_t *, sout_stream_id_t *, block_t * );
797 static int rtp_packetize_l8   ( sout_stream_t *, sout_stream_id_t *, block_t * );
798 static int rtp_packetize_mpa  ( sout_stream_t *, sout_stream_id_t *, block_t * );
799 static int rtp_packetize_mpv  ( sout_stream_t *, sout_stream_id_t *, block_t * );
800 static int rtp_packetize_ac3  ( sout_stream_t *, sout_stream_id_t *, block_t * );
801 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
802 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
803 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
804
805 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
806 {
807     static const char hex[16] = "0123456789abcdef";
808     int i;
809
810     for( i = 0; i < i_data; i++ )
811     {
812         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
813         s[2*i+1] = hex[(p_data[i]   )&0xf];
814     }
815     s[2*i_data] = '\0';
816 }
817
818 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
819 {
820     sout_instance_t   *p_sout = p_stream->p_sout;
821     sout_stream_sys_t *p_sys = p_stream->p_sys;
822     sout_stream_id_t  *id;
823     sout_access_out_t *p_access = NULL;
824     int               i_port;
825     char              *psz_sdp;
826
827     if( p_sys->p_mux != NULL )
828     {
829         sout_input_t      *p_input  = NULL;
830         if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
831         {
832             msg_Err( p_stream, "cannot add this stream to the muxer" );
833             return NULL;
834         }
835
836         id = malloc( sizeof( sout_stream_id_t ) );
837         memset( id, 0, sizeof( sout_stream_id_t ) );
838         id->p_access    = NULL;
839         id->p_input     = p_input;
840         id->pf_packetize= NULL;
841         id->p_rtsp_url  = NULL;
842         id->i_port      = 0;
843         return id;
844     }
845
846
847     /* Choose the port */
848     i_port = 0;
849     if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
850     {
851         i_port = p_sys->i_port_audio;
852         p_sys->i_port_audio = 0;
853     }
854     else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
855     {
856         i_port = p_sys->i_port_video;
857         p_sys->i_port_video = 0;
858     }
859     while( i_port == 0 )
860     {
861         if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
862         {
863             i_port = p_sys->i_port;
864             p_sys->i_port += 2;
865             break;
866         }
867         p_sys->i_port += 2;
868     }
869
870     if( p_sys->psz_destination )
871     {
872         char access[100];
873         char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
874
875         /* first try to create the access out */
876         if( p_sys->i_ttl > 0 )
877         {
878             sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
879         }
880         else
881         {
882             sprintf( access, "udp{raw}" );
883         }
884         sprintf( url, "%s:%d", p_sys->psz_destination, i_port );
885         if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
886         {
887             msg_Err( p_stream, "cannot create the access out for %s://%s",
888                      access, url );
889             return NULL;
890         }
891         msg_Dbg( p_stream, "access out %s:%s", access, url );
892     }
893
894     /* not create the rtp specific stuff */
895     id = malloc( sizeof( sout_stream_id_t ) );
896     memset( id, 0, sizeof( sout_stream_id_t ) );
897     id->p_stream   = p_stream;
898     id->p_access   = p_access;
899     id->p_input    = NULL;
900     id->psz_rtpmap = NULL;
901     id->psz_fmtp   = NULL;
902     id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
903     id->i_port = i_port;
904     id->p_rtsp_url = NULL;
905     vlc_mutex_init( p_stream, &id->lock_rtsp );
906     id->i_rtsp_access = 0;
907     id->rtsp_access = NULL;
908
909     switch( p_fmt->i_codec )
910     {
911         case VLC_FOURCC( 's', '1', '6', 'b' ):
912             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
913             {
914                 id->i_payload_type = 11;
915             }
916             else if( p_fmt->audio.i_channels == 2 &&
917                      p_fmt->audio.i_rate == 44100 )
918             {
919                 id->i_payload_type = 10;
920             }
921             else
922             {
923                 id->i_payload_type = p_sys->i_payload_type++;
924             }
925             id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
926             sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
927                      p_fmt->audio.i_channels );
928             id->i_clock_rate = p_fmt->audio.i_rate;
929             id->pf_packetize = rtp_packetize_l16;
930             break;
931         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
932             id->i_payload_type = p_sys->i_payload_type++;
933             id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
934             sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
935                      p_fmt->audio.i_channels );
936             id->i_clock_rate = p_fmt->audio.i_rate;
937             id->pf_packetize = rtp_packetize_l8;
938             break;
939         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
940             id->i_payload_type = 14;
941             id->i_clock_rate = 90000;
942             id->psz_rtpmap = strdup( "MPA/90000" );
943             id->pf_packetize = rtp_packetize_mpa;
944             break;
945         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
946             id->i_payload_type = 32;
947             id->i_clock_rate = 90000;
948             id->psz_rtpmap = strdup( "MPV/90000" );
949             id->pf_packetize = rtp_packetize_mpv;
950             break;
951         case VLC_FOURCC( 'a', '5', '2', ' ' ):
952             id->i_payload_type = p_sys->i_payload_type++;
953             id->i_clock_rate = 90000;
954             id->psz_rtpmap = strdup( "ac3/90000" );
955             id->pf_packetize = rtp_packetize_ac3;
956             break;
957         case VLC_FOURCC( 'H', '2', '6', '3' ):
958             id->i_payload_type = p_sys->i_payload_type++;
959             id->i_clock_rate = 90000;
960             id->psz_rtpmap = strdup( "H263-1998/90000" );
961             id->pf_packetize = rtp_packetize_h263;
962             break;
963
964         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
965         {
966             char hexa[2*p_fmt->i_extra +1];
967
968             id->i_payload_type = p_sys->i_payload_type++;
969             id->i_clock_rate = 90000;
970             id->psz_rtpmap = strdup( "MP4V-ES/90000" );
971             id->pf_packetize = rtp_packetize_split;
972             if( p_fmt->i_extra > 0 )
973             {
974                 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
975                 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
976                 sprintf( id->psz_fmtp,
977                          "profile-level-id=3; config=%s;", hexa );
978             }
979             break;
980         }
981         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
982         {
983             char hexa[2*p_fmt->i_extra +1];
984
985             id->i_payload_type = p_sys->i_payload_type++;
986             id->i_clock_rate = p_fmt->audio.i_rate;
987             id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
988             sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
989             id->pf_packetize = rtp_packetize_mp4a;
990             id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
991             sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
992             sprintf( id->psz_fmtp,
993                      "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
994                      "config=%s; SizeLength=13;IndexLength=3; "
995                      "IndexDeltaLength=3; Profile=1;", hexa );
996             break;
997         }
998
999         default:
1000             msg_Err( p_stream, "cannot add this stream (unsupported "
1001                      "codec:%4.4s)", (char*)&p_fmt->i_codec );
1002             if( p_access )
1003             {
1004                 sout_AccessOutDelete( p_access );
1005             }
1006             free( id );
1007             return NULL;
1008     }
1009     id->i_cat = p_fmt->i_cat;
1010
1011     id->ssrc[0] = rand()&0xff;
1012     id->ssrc[1] = rand()&0xff;
1013     id->ssrc[2] = rand()&0xff;
1014     id->ssrc[3] = rand()&0xff;
1015     id->i_sequence = rand()&0xffff;
1016     id->i_timestamp_start = rand()&0xffffffff;
1017
1018     id->i_mtu    = config_GetInt( p_stream, "mtu" );  /* XXX beuk */
1019     if( id->i_mtu <= 16 )
1020     {
1021         /* better than nothing */
1022         id->i_mtu = 1500;
1023     }
1024     msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
1025
1026     if( p_sys->p_rtsp_url )
1027     {
1028         char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1029
1030         sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1031         fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
1032         id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
1033
1034         if( id->p_rtsp_url )
1035         {
1036             httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP,    RtspCallbackId, (void*)id );
1037             //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
1038             //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream );
1039         }
1040     }
1041
1042
1043     /* Update p_sys context */
1044     vlc_mutex_lock( &p_sys->lock_es );
1045     TAB_APPEND( p_sys->i_es, p_sys->es, id );
1046     vlc_mutex_unlock( &p_sys->lock_es );
1047
1048     psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1049
1050     vlc_mutex_lock( &p_sys->lock_sdp );
1051     free( p_sys->psz_sdp );
1052     p_sys->psz_sdp = psz_sdp;
1053     vlc_mutex_unlock( &p_sys->lock_sdp );
1054
1055     p_sys->i_sdp_version++;
1056
1057     fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
1058
1059     /* Update SDP (sap/file) */
1060     if( p_sys->b_export_sap ) SapSetup( p_stream );
1061     if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1062
1063     return id;
1064 }
1065
1066 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1067 {
1068     sout_stream_sys_t *p_sys = p_stream->p_sys;
1069
1070     vlc_mutex_lock( &p_sys->lock_es );
1071     TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1072     vlc_mutex_unlock( &p_sys->lock_es );
1073
1074     /* Release port */
1075     if( id->i_port > 0 )
1076     {
1077         if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1078             p_sys->i_port_audio = id->i_port;
1079         else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1080             p_sys->i_port_video = id->i_port;
1081     }
1082
1083     if( id->p_access )
1084     {
1085         if( id->psz_rtpmap )
1086         {
1087             free( id->psz_rtpmap );
1088         }
1089         if( id->psz_fmtp )
1090         {
1091             free( id->psz_fmtp );
1092         }
1093         if( id->psz_destination )
1094             free( id->psz_destination );
1095         sout_AccessOutDelete( id->p_access );
1096     }
1097     else if( id->p_input )
1098     {
1099         sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1100     }
1101     if( id->p_rtsp_url )
1102     {
1103         httpd_UrlDelete( id->p_rtsp_url );
1104     }
1105     vlc_mutex_destroy( &id->lock_rtsp );
1106     if( id->rtsp_access ) free( id->rtsp_access );
1107
1108     /* Update SDP (sap/file) */
1109     if( p_sys->b_export_sap ) SapSetup( p_stream );
1110     if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1111
1112     free( id );
1113     return VLC_SUCCESS;
1114 }
1115
1116 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1117                  block_t *p_buffer )
1118 {
1119     block_t *p_next;
1120
1121     if( p_stream->p_sys->p_mux )
1122     {
1123         sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1124     }
1125     else
1126     {
1127         while( p_buffer )
1128         {
1129             p_next = p_buffer->p_next;
1130             if( id->pf_packetize( p_stream, id, p_buffer ) )
1131             {
1132                 break;
1133             }
1134             block_Release( p_buffer );
1135             p_buffer = p_next;
1136         }
1137     }
1138     return VLC_SUCCESS;
1139 }
1140
1141
1142 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1143                                         block_t *p_buffer )
1144 {
1145     sout_stream_sys_t *p_sys = p_stream->p_sys;
1146
1147     int64_t  i_dts = p_buffer->i_dts;
1148     uint32_t i_timestamp = i_dts * 9 / 100;
1149
1150     uint8_t         *p_data = p_buffer->p_buffer;
1151     unsigned int    i_data  = p_buffer->i_buffer;
1152     unsigned int    i_max   = p_sys->i_mtu - 12;
1153
1154     int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1155
1156     while( i_data > 0 )
1157     {
1158         unsigned int i_size;
1159
1160         /* output complete packet */
1161         if( p_sys->packet &&
1162             p_sys->packet->i_buffer + i_data > i_max )
1163         {
1164             sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1165             p_sys->packet = NULL;
1166         }
1167
1168         if( p_sys->packet == NULL )
1169         {
1170             /* allocate a new packet */
1171             p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1172             p_sys->packet->p_buffer[ 0] = 0x80;
1173             p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1174             p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1175             p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence     )&0xff;
1176             p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1177             p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1178             p_sys->packet->p_buffer[ 6] = ( i_timestamp >>  8 )&0xff;
1179             p_sys->packet->p_buffer[ 7] = ( i_timestamp       )&0xff;
1180             p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1181             p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1182             p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1183             p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1184             p_sys->packet->i_buffer = 12;
1185
1186             p_sys->packet->i_dts = i_dts;
1187             p_sys->packet->i_length = p_buffer->i_length / i_packet;
1188             i_dts += p_sys->packet->i_length;
1189
1190             p_sys->i_sequence++;
1191         }
1192
1193         i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
1194
1195         memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1196                 p_data, i_size );
1197
1198         p_sys->packet->i_buffer += i_size;
1199         p_data += i_size;
1200         i_data -= i_size;
1201     }
1202
1203     return VLC_SUCCESS;
1204 }
1205
1206 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1207                                   block_t *p_buffer )
1208 {
1209     sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1210
1211     //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1212     //
1213     while( p_buffer )
1214     {
1215         block_t *p_next;
1216
1217         AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1218
1219         p_next = p_buffer->p_next;
1220         block_Release( p_buffer );
1221         p_buffer = p_next;
1222     }
1223
1224     return VLC_SUCCESS;
1225 }
1226
1227 /****************************************************************************
1228  * SAP:
1229  ****************************************************************************/
1230 static int SapSetup( sout_stream_t *p_stream )
1231 {
1232     sout_stream_sys_t *p_sys = p_stream->p_sys;
1233     sout_instance_t   *p_sout = p_stream->p_sout;
1234     announce_method_t *p_method = (announce_method_t *)
1235                                   malloc(sizeof(announce_method_t));
1236
1237     /* Remove the previous session */
1238     if( p_sys->p_session != NULL)
1239     {
1240         sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1241         sout_AnnounceSessionDestroy( p_sys->p_session );
1242         p_sys->p_session = NULL;
1243     }
1244     p_method->i_type = METHOD_TYPE_SAP;
1245     p_method->psz_address = NULL; /* FIXME */
1246     p_method->i_ip_version = 4; /* FIXME ! */
1247
1248     if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
1249     {
1250         p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1251                                                      p_method );
1252     }
1253
1254     free( p_method );
1255     return VLC_SUCCESS;
1256 }
1257
1258 /****************************************************************************
1259 * File:
1260 ****************************************************************************/
1261 static int FileSetup( sout_stream_t *p_stream )
1262 {
1263     sout_stream_sys_t *p_sys = p_stream->p_sys;
1264     FILE            *f;
1265
1266     if( ( f = fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1267     {
1268         msg_Err( p_stream, "cannot open file '%s' (%s)",
1269                  p_sys->psz_sdp_file, strerror(errno) );
1270         return VLC_EGENERIC;
1271     }
1272
1273     fprintf( f, "%s", p_sys->psz_sdp );
1274     fclose( f );
1275
1276     return VLC_SUCCESS;
1277 }
1278
1279 /****************************************************************************
1280  * HTTP:
1281  ****************************************************************************/
1282 static int  HttpCallback( httpd_file_sys_t *p_args,
1283                           httpd_file_t *, uint8_t *p_request,
1284                           uint8_t **pp_data, int *pi_data );
1285
1286 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1287 {
1288     sout_stream_sys_t *p_sys = p_stream->p_sys;
1289
1290     p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1291     if( p_sys->p_httpd_host )
1292     {
1293         p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1294                                              url->psz_path ? url->psz_path : "/",
1295                                              "application/sdp",
1296                                              NULL, NULL,
1297                                              HttpCallback, (void*)p_sys );
1298     }
1299     if( p_sys->p_httpd_file == NULL )
1300     {
1301         return VLC_EGENERIC;
1302     }
1303     return VLC_SUCCESS;
1304 }
1305
1306 static int  HttpCallback( httpd_file_sys_t *p_args,
1307                           httpd_file_t *f, uint8_t *p_request,
1308                           uint8_t **pp_data, int *pi_data )
1309 {
1310     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1311
1312     vlc_mutex_lock( &p_sys->lock_sdp );
1313     if( p_sys->psz_sdp && *p_sys->psz_sdp )
1314     {
1315         *pi_data = strlen( p_sys->psz_sdp );
1316         *pp_data = malloc( *pi_data );
1317         memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1318     }
1319     else
1320     {
1321         *pp_data = NULL;
1322         *pi_data = 0;
1323     }
1324     vlc_mutex_unlock( &p_sys->lock_sdp );
1325
1326     return VLC_SUCCESS;
1327 }
1328
1329 /****************************************************************************
1330  * RTSP:
1331  ****************************************************************************/
1332 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1333 {
1334     rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1335
1336     rtsp->psz_session = psz_session;
1337     rtsp->i_last = 0;
1338     rtsp->b_playing = VLC_FALSE;
1339     rtsp->i_id = 0;
1340     rtsp->id = NULL;
1341     rtsp->i_access = 0;
1342     rtsp->access = NULL;
1343
1344     TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1345
1346     return rtsp;
1347 }
1348 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1349 {
1350     int i;
1351     for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1352     {
1353         if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1354         {
1355             return p_stream->p_sys->rtsp[i];
1356         }
1357     }
1358     return NULL;
1359 }
1360
1361 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1362 {
1363     int i;
1364     TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1365
1366     for( i = 0; i < rtsp->i_access; i++ )
1367     {
1368         sout_AccessOutDelete( rtsp->access[i] );
1369     }
1370     if( rtsp->id )     free( rtsp->id );
1371     if( rtsp->access ) free( rtsp->access );
1372
1373     free( rtsp->psz_session );
1374     free( rtsp );
1375 }
1376
1377 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1378 {
1379     sout_stream_sys_t *p_sys = p_stream->p_sys;
1380
1381     fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1382
1383     p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1384     if( p_sys->p_rtsp_host == NULL )
1385     {
1386         return VLC_EGENERIC;
1387     }
1388
1389     p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1390     p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1391     sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1392              url->psz_host,  url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1393
1394     p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
1395     if( p_sys->p_rtsp_url == 0 )
1396     {
1397         return VLC_EGENERIC;
1398     }
1399     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1400     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
1401     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream );
1402     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1403
1404     return VLC_SUCCESS;
1405 }
1406
1407 static int  RtspCallback( httpd_callback_sys_t *p_args,
1408                           httpd_client_t *cl,
1409                           httpd_message_t *answer, httpd_message_t *query )
1410 {
1411     sout_stream_t *p_stream = (sout_stream_t*)p_args;
1412     sout_stream_sys_t *p_sys = p_stream->p_sys;
1413     char          *psz_destination = p_sys->psz_destination;
1414     char          *psz_session = NULL;
1415
1416     if( answer == NULL || query == NULL )
1417     {
1418         return VLC_SUCCESS;
1419     }
1420     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1421
1422     answer->i_proto = HTTPD_PROTO_RTSP;
1423     answer->i_version= query->i_version;
1424     answer->i_type   = HTTPD_MSG_ANSWER;
1425
1426     switch( query->i_type )
1427     {
1428         case HTTPD_MSG_DESCRIBE:
1429         {
1430             char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1431
1432             answer->i_status = 200;
1433             answer->psz_status = strdup( "OK" );
1434             httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
1435
1436             answer->p_body = psz_sdp;
1437             answer->i_body = strlen( psz_sdp );
1438             break;
1439         }
1440
1441         case HTTPD_MSG_PLAY:
1442         {
1443             rtsp_client_t *rtsp;
1444             /* for now only multicast so easy */
1445             answer->i_status = 200;
1446             answer->psz_status = strdup( "OK" );
1447             answer->i_body = 0;
1448             answer->p_body = NULL;
1449
1450             psz_session = httpd_MsgGet( query, "Session" );
1451             rtsp = RtspClientGet( p_stream, psz_session );
1452             if( rtsp && !rtsp->b_playing )
1453             {
1454                 int i_id;
1455                 /* FIXME */
1456                 rtsp->b_playing = VLC_TRUE;
1457
1458                 vlc_mutex_lock( &p_sys->lock_es );
1459                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1460                 {
1461                     sout_stream_id_t *id = rtsp->id[i_id];
1462                     int i;
1463
1464                     for( i = 0; i < p_sys->i_es; i++ )
1465                     {
1466                         if( id == p_sys->es[i] )
1467                             break;
1468                     }
1469                     if( i >= p_sys->i_es ) continue;
1470
1471                     vlc_mutex_lock( &id->lock_rtsp );
1472                     TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1473                     vlc_mutex_unlock( &id->lock_rtsp );
1474                 }
1475                 vlc_mutex_unlock( &p_sys->lock_es );
1476             }
1477             break;
1478         }
1479         case HTTPD_MSG_PAUSE:
1480             /* FIXME */
1481             return VLC_EGENERIC;
1482         case HTTPD_MSG_TEARDOWN:
1483         {
1484             rtsp_client_t *rtsp;
1485
1486             /* for now only multicast so easy again */
1487             answer->i_status = 200;
1488             answer->psz_status = strdup( "OK" );
1489             answer->i_body = 0;
1490             answer->p_body = NULL;
1491
1492             psz_session = httpd_MsgGet( query, "Session" );
1493             rtsp = RtspClientGet( p_stream, psz_session );
1494             if( rtsp )
1495             {
1496                 int i_id;
1497
1498                 vlc_mutex_lock( &p_sys->lock_es );
1499                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1500                 {
1501                     sout_stream_id_t *id = rtsp->id[i_id];
1502                     int i;
1503
1504                     for( i = 0; i < p_sys->i_es; i++ )
1505                     {
1506                         if( id == p_sys->es[i] )
1507                             break;
1508                     }
1509                     if( i >= p_sys->i_es ) continue;
1510
1511                     vlc_mutex_lock( &id->lock_rtsp );
1512                     TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1513                     vlc_mutex_unlock( &id->lock_rtsp );
1514                 }
1515                 vlc_mutex_unlock( &p_sys->lock_es );
1516
1517                 RtspClientDel( p_stream, rtsp );
1518             }
1519             break;
1520         }
1521
1522         default:
1523             return VLC_EGENERIC;
1524     }
1525     httpd_MsgAdd( answer, "Server", "VLC Server" );
1526     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1527     httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1528     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1529
1530     if( psz_session )
1531     {
1532         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1533     }
1534     return VLC_SUCCESS;
1535 }
1536
1537 static int  RtspCallbackId( httpd_callback_sys_t *p_args,
1538                           httpd_client_t *cl,
1539                           httpd_message_t *answer, httpd_message_t *query )
1540 {
1541     sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1542     sout_stream_t    *p_stream = id->p_stream;
1543     sout_stream_sys_t *p_sys = p_stream->p_sys;
1544     char          *psz_session = NULL;
1545
1546     if( answer == NULL || query == NULL )
1547     {
1548         return VLC_SUCCESS;
1549     }
1550     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1551
1552     answer->i_proto = HTTPD_PROTO_RTSP;
1553     answer->i_version= query->i_version;
1554     answer->i_type   = HTTPD_MSG_ANSWER;
1555
1556     switch( query->i_type )
1557     {
1558         case HTTPD_MSG_SETUP:
1559         {
1560             char *psz_transport = httpd_MsgGet( query, "Transport" );
1561
1562             fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1563
1564             if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1565             {
1566                 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1567                 answer->i_status = 200;
1568                 answer->psz_status = strdup( "OK" );
1569                 answer->i_body = 0;
1570                 answer->p_body = NULL;
1571                 psz_session = httpd_MsgGet( query, "Session" );
1572                 if( *psz_session == 0 )
1573                 {
1574                     psz_session = malloc( 100 );
1575                     sprintf( psz_session, "%d", rand() );
1576                 }
1577                 httpd_MsgAdd( answer, "Transport",
1578                               "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1579                               id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1580             }
1581             else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1582             {
1583                 int  i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1584                 char *ip    = httpd_ClientIP( cl );
1585
1586                 char psz_access[100];
1587                 char psz_url[100];
1588
1589                 sout_access_out_t *p_access;
1590
1591                 rtsp_client_t *rtsp = NULL;
1592
1593                 if( ip == NULL )
1594                 {
1595                     answer->i_status = 400;
1596                     answer->psz_status = strdup( "Internal server error" );
1597                     answer->i_body = 0;
1598                     answer->p_body = NULL;
1599                     break;
1600                 }
1601
1602                 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1603                          ip, i_port );
1604
1605                 psz_session = httpd_MsgGet( query, "Session" );
1606                 if( *psz_session == 0 )
1607                 {
1608                     psz_session = malloc( 100 );
1609                     sprintf( psz_session, "%d", rand() );
1610
1611                     rtsp = RtspClientNew( p_stream, psz_session );
1612                 }
1613                 else
1614                 {
1615                     rtsp = RtspClientGet( p_stream, psz_session );
1616                     if( rtsp == NULL )
1617                     {
1618                         /* FIXME right error code */
1619                         answer->i_status = 400;
1620                         answer->psz_status = strdup( "Unknown session id" );
1621                         answer->i_body = 0;
1622                         answer->p_body = NULL;
1623                         free( ip );
1624                         break;
1625                     }
1626                 }
1627
1628                 /* first try to create the access out */
1629                 if( p_sys->i_ttl > 0 )
1630                     sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1631                 else
1632                     sprintf( psz_access, "udp{raw}" );
1633                 sprintf( psz_url, "%s:%d", ip, i_port );
1634                 free( ip );
1635                 
1636                 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1637                 {
1638                     msg_Err( p_stream, "cannot create the access out for %s://%s",
1639                              psz_access, psz_url );
1640                     answer->i_status = 400;
1641                     answer->psz_status = strdup( "Server internal error" );
1642                     answer->i_body = 0;
1643                     answer->p_body = NULL;
1644                     break;
1645                 }
1646
1647                 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1648                 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1649
1650                 answer->i_status = 200;
1651                 answer->psz_status = strdup( "OK" );
1652                 answer->i_body = 0;
1653                 answer->p_body = NULL;
1654
1655                 httpd_MsgAdd( answer, "Transport",
1656                               "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1657             }
1658             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1659             {
1660                 answer->i_status = 400;
1661                 answer->psz_status = strdup( "Bad Request" );
1662                 answer->i_body = 0;
1663                 answer->p_body = NULL;
1664             }
1665             break;
1666         }
1667
1668         default:
1669             return VLC_EGENERIC;
1670     }
1671     httpd_MsgAdd( answer, "Server", "VLC Server" );
1672     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1673     httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1674     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1675
1676     if( psz_session )
1677     {
1678         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1679     }
1680     return VLC_SUCCESS;
1681 }
1682
1683 /****************************************************************************
1684  * rtp_packetize_*:
1685  ****************************************************************************/
1686 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1687                                   int b_marker, int64_t i_pts )
1688 {
1689     uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1690
1691     out->p_buffer[0] = 0x80;
1692     out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1693     out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1694     out->p_buffer[3] = ( id->i_sequence     )&0xff;
1695     out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1696     out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1697     out->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
1698     out->p_buffer[7] = ( i_timestamp       )&0xff;
1699
1700     out->p_buffer[ 8] = id->ssrc[0];
1701     out->p_buffer[ 9] = id->ssrc[1];
1702     out->p_buffer[10] = id->ssrc[2];
1703     out->p_buffer[11] = id->ssrc[3];
1704
1705     out->i_buffer = 12;
1706     id->i_sequence++;
1707 }
1708
1709 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1710 {
1711     int i;
1712     vlc_mutex_lock( &id->lock_rtsp );
1713     for( i = 0; i < id->i_rtsp_access; i++ )
1714     {
1715         sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1716     }
1717     vlc_mutex_unlock( &id->lock_rtsp );
1718
1719     if( id->p_access )
1720     {
1721         sout_AccessOutWrite( id->p_access, out );
1722     }
1723     else
1724     {
1725         block_Release( out );
1726     }
1727 }
1728
1729 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1730                               block_t *in )
1731 {
1732     int     i_max   = id->i_mtu - 12 - 4; /* payload max in one packet */
1733     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1734
1735     uint8_t *p_data = in->p_buffer;
1736     int     i_data  = in->i_buffer;
1737     int     i;
1738
1739     for( i = 0; i < i_count; i++ )
1740     {
1741         int           i_payload = __MIN( i_max, i_data );
1742         block_t *out = block_New( p_stream, 16 + i_payload );
1743
1744         /* rtp common header */
1745         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1746         /* mbz set to 0 */
1747         out->p_buffer[12] = 0;
1748         out->p_buffer[13] = 0;
1749         /* fragment offset in the current frame */
1750         out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1751         out->p_buffer[15] = ( (i*i_max)      )&0xff;
1752         memcpy( &out->p_buffer[16], p_data, i_payload );
1753
1754         out->i_buffer   = 16 + i_payload;
1755         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1756         out->i_length = in->i_length / i_count;
1757
1758         rtp_packetize_send( id, out );
1759
1760         p_data += i_payload;
1761         i_data -= i_payload;
1762     }
1763
1764     return VLC_SUCCESS;
1765 }
1766
1767 /* rfc2250 */
1768 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1769                               block_t *in )
1770 {
1771     int     i_max   = id->i_mtu - 12 - 4; /* payload max in one packet */
1772     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1773
1774     uint8_t *p_data = in->p_buffer;
1775     int     i_data  = in->i_buffer;
1776     int     i;
1777     int     b_sequence_start = 0;
1778     int     i_temporal_ref = 0;
1779     int     i_picture_coding_type = 0;
1780     int     i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1781     int     b_start_slice = 0;
1782
1783     /* preparse this packet to get some info */
1784     if( in->i_buffer > 4 )
1785     {
1786         uint8_t *p = p_data;
1787         int      i_rest = in->i_buffer;
1788
1789         for( ;; )
1790         {
1791             while( i_rest > 4 &&
1792                    ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1793             {
1794                 p++;
1795                 i_rest--;
1796             }
1797             if( i_rest <= 4 )
1798             {
1799                 break;
1800             }
1801             p += 3;
1802             i_rest -= 4;
1803
1804             if( *p == 0xb3 )
1805             {
1806                 /* sequence start code */
1807                 b_sequence_start = 1;
1808             }
1809             else if( *p == 0x00 && i_rest >= 4 )
1810             {
1811                 /* picture */
1812                 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1813                 i_picture_coding_type = (p[2] >> 3)&0x07;
1814
1815                 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1816                                     i_picture_coding_type == 3 ) )
1817                 {
1818                     i_ffv = (p[3] >> 2)&0x01;
1819                     i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1820                     if( i_rest > 4 && i_picture_coding_type == 3 )
1821                     {
1822                         i_fbv = (p[4]>>6)&0x01;
1823                         i_bfc = (p[4]>>3)&0x07;
1824                     }
1825                 }
1826             }
1827             else if( *p <= 0xaf )
1828             {
1829                 b_start_slice = 1;
1830             }
1831         }
1832     }
1833
1834     for( i = 0; i < i_count; i++ )
1835     {
1836         int           i_payload = __MIN( i_max, i_data );
1837         block_t *out = block_New( p_stream,
1838                                              16 + i_payload );
1839         uint32_t      h = ( i_temporal_ref << 16 )|
1840                           ( b_sequence_start << 13 )|
1841                           ( b_start_slice << 12 )|
1842                           ( i == i_count - 1 ? 1 << 11 : 0 )|
1843                           ( i_picture_coding_type << 8 )|
1844                           ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1845
1846         /* rtp common header */
1847         rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1848                               in->i_pts > 0 ? in->i_pts : in->i_dts );
1849
1850         /* MBZ:5 T:1 TR:10 AN:1 N:1 S:1 B:1 E:1 P:3 FBV:1 BFC:3 FFV:1 FFC:3 */
1851         out->p_buffer[12] = ( h >> 24 )&0xff;
1852         out->p_buffer[13] = ( h >> 16 )&0xff;
1853         out->p_buffer[14] = ( h >>  8 )&0xff;
1854         out->p_buffer[15] = ( h       )&0xff;
1855
1856         memcpy( &out->p_buffer[16], p_data, i_payload );
1857
1858         out->i_buffer   = 16 + i_payload;
1859         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1860         out->i_length = in->i_length / i_count;
1861
1862         rtp_packetize_send( id, out );
1863
1864         p_data += i_payload;
1865         i_data -= i_payload;
1866     }
1867
1868     return VLC_SUCCESS;
1869 }
1870 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1871                               block_t *in )
1872 {
1873     int     i_max   = id->i_mtu - 12 - 2; /* payload max in one packet */
1874     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1875
1876     uint8_t *p_data = in->p_buffer;
1877     int     i_data  = in->i_buffer;
1878     int     i;
1879
1880     for( i = 0; i < i_count; i++ )
1881     {
1882         int           i_payload = __MIN( i_max, i_data );
1883         block_t *out = block_New( p_stream, 14 + i_payload );
1884
1885         /* rtp common header */
1886         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1887         /* unit count */
1888         out->p_buffer[12] = 1;
1889         /* unit header */
1890         out->p_buffer[13] = 0x00;
1891         /* data */
1892         memcpy( &out->p_buffer[14], p_data, i_payload );
1893
1894         out->i_buffer   = 14 + i_payload;
1895         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1896         out->i_length = in->i_length / i_count;
1897
1898         rtp_packetize_send( id, out );
1899
1900         p_data += i_payload;
1901         i_data -= i_payload;
1902     }
1903
1904     return VLC_SUCCESS;
1905 }
1906
1907 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1908                                 block_t *in )
1909 {
1910     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1911     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1912
1913     uint8_t *p_data = in->p_buffer;
1914     int     i_data  = in->i_buffer;
1915     int     i;
1916
1917     for( i = 0; i < i_count; i++ )
1918     {
1919         int           i_payload = __MIN( i_max, i_data );
1920         block_t *out = block_New( p_stream, 12 + i_payload );
1921
1922         /* rtp common header */
1923         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1924                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1925         memcpy( &out->p_buffer[12], p_data, i_payload );
1926
1927         out->i_buffer   = 12 + i_payload;
1928         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1929         out->i_length = in->i_length / i_count;
1930
1931         rtp_packetize_send( id, out );
1932
1933         p_data += i_payload;
1934         i_data -= i_payload;
1935     }
1936
1937     return VLC_SUCCESS;
1938 }
1939
1940 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1941                               block_t *in )
1942 {
1943     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1944     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1945
1946     uint8_t *p_data = in->p_buffer;
1947     int     i_data  = in->i_buffer;
1948     int     i_packet = 0;
1949
1950     while( i_data > 0 )
1951     {
1952         int           i_payload = (__MIN( i_max, i_data )/4)*4;
1953         block_t *out = block_New( p_stream, 12 + i_payload );
1954
1955         /* rtp common header */
1956         rtp_packetize_common( id, out, 0,
1957                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1958         memcpy( &out->p_buffer[12], p_data, i_payload );
1959
1960         out->i_buffer   = 12 + i_payload;
1961         out->i_dts    = in->i_dts + i_packet * in->i_length / i_count;
1962         out->i_length = in->i_length / i_count;
1963
1964         rtp_packetize_send( id, out );
1965
1966         p_data += i_payload;
1967         i_data -= i_payload;
1968         i_packet++;
1969     }
1970
1971     return VLC_SUCCESS;
1972 }
1973
1974 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1975                              block_t *in )
1976 {
1977     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1978     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1979
1980     uint8_t *p_data = in->p_buffer;
1981     int     i_data  = in->i_buffer;
1982     int     i_packet = 0;
1983
1984     while( i_data > 0 )
1985     {
1986         int           i_payload = (__MIN( i_max, i_data )/2)*2;
1987         block_t *out = block_New( p_stream, 12 + i_payload );
1988
1989         /* rtp common header */
1990         rtp_packetize_common( id, out, 0,
1991                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1992         memcpy( &out->p_buffer[12], p_data, i_payload );
1993
1994         out->i_buffer   = 12 + i_payload;
1995         out->i_dts    = in->i_dts + i_packet * in->i_length / i_count;
1996         out->i_length = in->i_length / i_count;
1997
1998         rtp_packetize_send( id, out );
1999
2000         p_data += i_payload;
2001         i_data -= i_payload;
2002         i_packet++;
2003     }
2004
2005     return VLC_SUCCESS;
2006 }
2007 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2008                                block_t *in )
2009 {
2010     int     i_max   = id->i_mtu - 16; /* payload max in one packet */
2011     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2012
2013     uint8_t *p_data = in->p_buffer;
2014     int     i_data  = in->i_buffer;
2015     int     i;
2016
2017     for( i = 0; i < i_count; i++ )
2018     {
2019         int           i_payload = __MIN( i_max, i_data );
2020         block_t *out = block_New( p_stream, 16 + i_payload );
2021
2022         /* rtp common header */
2023         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2024                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2025         /* AU headers */
2026         /* AU headers length (bits) */
2027         out->p_buffer[12] = 0;
2028         out->p_buffer[13] = 2*8;
2029         /* for each AU length 13 bits + idx 3bits, */
2030         out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2031         out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2032
2033         memcpy( &out->p_buffer[16], p_data, i_payload );
2034
2035         out->i_buffer   = 16 + i_payload;
2036         out->i_dts    = in->i_dts + i * in->i_length / i_count;
2037         out->i_length = in->i_length / i_count;
2038
2039         rtp_packetize_send( id, out );
2040
2041         p_data += i_payload;
2042         i_data -= i_payload;
2043     }
2044
2045     return VLC_SUCCESS;
2046 }
2047
2048
2049 /* rfc2429 */
2050 #define RTP_H263_HEADER_SIZE (2)  // plen = 0
2051 #define RTP_H263_PAYLOAD_START (14)  // plen = 0
2052 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2053                                block_t *in )
2054 {
2055     uint8_t *p_data = in->p_buffer;
2056     int     i_data  = in->i_buffer;
2057     int     i;
2058     int     i_max   = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2059     int     i_count;
2060     int     b_p_bit;
2061     int     b_v_bit = 0; // no pesky error resilience
2062     int     i_plen = 0; // normally plen=0 for PSC packet
2063     int     i_pebit = 0; // because plen=0
2064     uint16_t h;
2065
2066     if( i_data < 2 )
2067     {
2068         return VLC_EGENERIC;
2069     }
2070     if( p_data[0] || p_data[1] )
2071     {
2072         return VLC_EGENERIC;
2073     }
2074     /* remove 2 leading 0 bytes */
2075     p_data += 2;
2076     i_data -= 2;
2077     i_count = ( i_data + i_max - 1 ) / i_max;
2078
2079     for( i = 0; i < i_count; i++ )
2080     {
2081         int      i_payload = __MIN( i_max, i_data );
2082         block_t *out = block_New( p_stream,
2083                                   RTP_H263_PAYLOAD_START + i_payload );
2084         b_p_bit = (i == 0) ? 1 : 0;
2085         h = ( b_p_bit << 10 )|
2086             ( b_v_bit << 9  )|
2087             ( i_plen  << 3  )|
2088               i_pebit;
2089
2090         /* rtp common header */
2091         //b_m_bit = 1; // always contains end of frame
2092         rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2093                               in->i_pts > 0 ? in->i_pts : in->i_dts );
2094
2095         /* h263 header */
2096         out->p_buffer[12] = ( h >>  8 )&0xff;
2097         out->p_buffer[13] = ( h       )&0xff;
2098         memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2099
2100         out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2101         out->i_dts    = in->i_dts + i * in->i_length / i_count;
2102         out->i_length = in->i_length / i_count;
2103
2104         rtp_packetize_send( id, out );
2105
2106         p_data += i_payload;
2107         i_data -= i_payload;
2108     }
2109
2110     return VLC_SUCCESS;
2111 }
2112