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