3bfbfac00e8b372e466697f7a6a026251fccd7bb
[vlc.git] / 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 <vlc/vlc.h>
30 #include <vlc/input.h>
31 #include <vlc/sout.h>
32
33 #include "vlc_httpd.h"
34 #include "network.h"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 vlc_module_begin();
43     set_description( _("RTP stream output") );
44     set_capability( "sout stream", 0 );
45     add_shortcut( "rtp" );
46     set_callbacks( Open, Close );
47 vlc_module_end();
48
49 /*****************************************************************************
50  * Exported prototypes
51  *****************************************************************************/
52 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
53 static int               Del ( sout_stream_t *, sout_stream_id_t * );
54 static int               Send( sout_stream_t *, sout_stream_id_t *,
55                                sout_buffer_t* );
56
57 struct sout_stream_sys_t
58 {
59     /* sdp */
60     int64_t i_sdp_id;
61     int     i_sdp_version;
62     char    *psz_sdp;
63
64     vlc_mutex_t  lock_sdp;
65
66     httpd_host_t *p_httpd_host;
67     httpd_file_t *p_httpd_file;
68
69     httpd_host_t *p_rtsp_host;
70     httpd_url_t  *p_rtsp_url;
71     char         *psz_rtsp_control;
72     char         *psz_rtsp_path;
73
74     /* */
75     char *psz_destination;
76     int  i_port;
77     int  i_ttl;
78
79     /* when need to use a private one or when using muxer */
80     int i_payload_type;
81
82     /* in case we do TS/PS over rtp */
83     sout_mux_t        *p_mux;
84     sout_access_out_t *p_access;
85     int               i_mtu;
86     sout_access_out_t *p_grab;
87     uint16_t          i_sequence;
88     uint32_t          i_timestamp_start;
89     uint8_t           ssrc[4];
90     sout_buffer_t     *packet;
91
92     /* */
93     int              i_es;
94     sout_stream_id_t **es;
95 };
96
97 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
98                                     sout_buffer_t * );
99
100 struct sout_stream_id_t
101 {
102     /* rtp field */
103     uint8_t     i_payload_type;
104     uint16_t    i_sequence;
105     uint32_t    i_timestamp_start;
106     uint8_t     ssrc[4];
107
108     /* for sdp */
109     int         i_clock_rate;
110     char        *psz_rtpmap;
111     char        *psz_fmtp;
112     char        *psz_destination;
113     int         i_port;
114     int         i_cat;
115
116     /* Packetizer specific fields */
117     pf_rtp_packetizer_t pf_packetize;
118     int           i_mtu;
119
120     /* for sending the packets */
121     sout_access_out_t *p_access;
122     sout_input_t      *p_input;
123
124     /* RTSP url control */
125     httpd_url_t  *p_rtsp_url;
126 };
127
128 static int AccessOutGrabberWrite( sout_access_out_t *, sout_buffer_t * );
129
130 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
131 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
132
133 typedef struct
134 {
135     char    *psz_session;
136     int64_t i_last;
137
138     int                 i_access;
139     sout_access_out_t **p_access;
140 } rtsp_session_t;
141
142 #if 0
143 static rtsp_session_t *RtspSessionNew   ( sout_stream_t *p_stream );
144 static rtsp_session_t *RtspSessionFind  ( sout_stream_t *p_stream, char *psz_session );
145 static void            RtspSessionDelete( sout_stream_t *p_stream, rtsp_session_t *sess );
146 #endif
147
148 static int  RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
149                           httpd_message_t *, httpd_message_t * );
150
151
152 /*****************************************************************************
153  * Open:
154  *****************************************************************************/
155 static int Open( vlc_object_t *p_this )
156 {
157     sout_stream_t       *p_stream = (sout_stream_t*)p_this;
158     sout_instance_t     *p_sout = p_stream->p_sout;
159     sout_stream_sys_t   *p_sys;
160
161     char *val;
162
163     p_sys = malloc( sizeof( sout_stream_sys_t ) );
164     p_sys->psz_destination = sout_cfg_find_value( p_stream->p_cfg, "dst" );
165     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "port" ) ) )
166     {
167         p_sys->i_port = atoi( val );
168     }
169     if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' ||
170         p_sys->i_port <= 0 )
171     {
172         msg_Err( p_stream, "invalid/missing dst or port" );
173         free( p_sys );
174         return VLC_EGENERIC;
175     }
176     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ttl" ) ) )
177     {
178         p_sys->i_ttl = atoi( val );
179     }
180     else
181     {
182         p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
183     }
184
185     p_sys->i_payload_type = 96;
186     p_sys->i_es = 0;
187     p_sys->es   = NULL;
188     p_sys->psz_sdp = NULL;
189
190     p_sys->i_sdp_id = mdate();
191     p_sys->i_sdp_version = 1;
192     p_sys->psz_sdp = NULL;
193     p_sys->p_httpd_host = NULL;
194     p_sys->p_httpd_file = NULL;
195     p_sys->p_rtsp_host  = NULL;
196     p_sys->p_rtsp_url   = NULL;
197     p_sys->psz_rtsp_control = NULL;
198     p_sys->psz_rtsp_path = NULL;
199
200     vlc_mutex_init( p_stream, &p_sys->lock_sdp );
201
202     p_stream->pf_add    = Add;
203     p_stream->pf_del    = Del;
204     p_stream->pf_send   = Send;
205
206     p_stream->p_sys     = p_sys;
207
208     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mux" ) ) )
209     {
210         sout_access_out_t *p_grab;
211
212         char *psz_rtpmap;
213         char access[100];
214         char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
215
216         /* Check muxer type */
217         if( !strncasecmp( val, "ps", 2 ) || !strncasecmp( val, "mpeg1", 5 ) )
218         {
219             psz_rtpmap = "MP2P/90000";
220         }
221         else if( !strncasecmp( val, "ts", 2 ) )
222         {
223             psz_rtpmap = "MP2T/90000";
224             p_sys->i_payload_type = 33;
225         }
226         else
227         {
228             msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
229             return VLC_EGENERIC;
230         }
231
232         /* create the access out */
233         if( p_sys->i_ttl > 0 )
234         {
235             sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
236         }
237         else
238         {
239             sprintf( access, "udp{raw}" );
240         }
241         sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
242         if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
243         {
244             msg_Err( p_stream, "cannot create the access out for %s://%s",
245                      access, url );
246             free( p_sys );
247             return VLC_EGENERIC;
248         }
249         p_sys->i_mtu = config_GetInt( p_stream, "mtu" );  /* XXX beurk */
250         if( p_sys->i_mtu <= 16 )
251         {
252             /* better than nothing */
253             p_sys->i_mtu = 1500;
254         }
255
256         /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
257         p_grab = p_sys->p_grab =
258             vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
259         p_grab->p_module    = NULL;
260         p_grab->p_sout      = p_sout;
261         p_grab->psz_access  = strdup( "grab" );
262         p_grab->p_cfg       = NULL;
263         p_grab->psz_name    = strdup( "" );
264         p_grab->p_sys       = (sout_access_out_sys_t*)p_stream;
265         p_grab->pf_seek     = NULL;
266         p_grab->pf_write    = AccessOutGrabberWrite;
267
268         /* the muxer */
269         if( !( p_sys->p_mux = sout_MuxNew( p_sout, val, p_sys->p_grab ) ) )
270         {
271             msg_Err( p_stream, "cannot create the muxer (%s)", val );
272             sout_AccessOutDelete( p_sys->p_grab );
273             sout_AccessOutDelete( p_sys->p_access );
274             free( p_sys );
275             return VLC_EGENERIC;
276         }
277         p_sout->i_preheader = __MAX( p_sout->i_preheader,
278                                      p_sys->p_mux->i_preheader );
279
280         /* create the SDP only once */
281         p_sys->psz_sdp =
282             malloc( 200 + 20 + 10 + strlen( p_sys->psz_destination ) +
283                     10 + 10 + 10 + 10 + strlen( psz_rtpmap ) );
284         sprintf( p_sys->psz_sdp,
285                  "v=0\n"
286                  "o=- "I64Fd" %d IN IP4 127.0.0.1\n"
287                  "s=NONE\n"
288                  "c=IN IP4 %s/%d\n"
289                  "m=video %d RTP/AVP %d\n"
290                  "a=rtpmap:%d %s\n",
291                  p_sys->i_sdp_id, p_sys->i_sdp_version,
292                  p_sys->psz_destination, p_sys->i_ttl,
293                  p_sys->i_port, p_sys->i_payload_type,
294                  p_sys->i_payload_type, psz_rtpmap );
295
296         fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
297
298         /* create the rtp context */
299         p_sys->ssrc[0] = rand()&0xff;
300         p_sys->ssrc[1] = rand()&0xff;
301         p_sys->ssrc[2] = rand()&0xff;
302         p_sys->ssrc[3] = rand()&0xff;
303         p_sys->i_sequence = rand()&0xffff;
304         p_sys->i_timestamp_start = rand()&0xffffffff;
305         p_sys->packet = NULL;
306     }
307     else
308     {
309         p_sys->p_mux    = NULL;
310         p_sys->p_access = NULL;
311         p_sys->p_grab   = NULL;
312     }
313
314     if( ( val = sout_cfg_find_value( p_stream->p_cfg, "sdp" ) ) )
315     {
316         vlc_url_t url;
317
318         vlc_UrlParse( &url, val, 0 );
319         if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
320         {
321             if( HttpSetup( p_stream, &url ) )
322             {
323                 msg_Err( p_stream, "cannot export sdp as http" );
324             }
325         }
326         if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
327         {
328             if( RtspSetup( p_stream, &url ) )
329             {
330                 msg_Err( p_stream, "cannot export sdp as rtsp" );
331             }
332         }
333         else
334         {
335             msg_Warn( p_stream, "unknow protocol for SDP (%s)",
336                       url.psz_protocol );
337         }
338         vlc_UrlClean( &url );
339     }
340
341     /* update p_sout->i_out_pace_nocontrol */
342     p_stream->p_sout->i_out_pace_nocontrol++;
343
344     return VLC_SUCCESS;
345 }
346
347 /*****************************************************************************
348  * Close:
349  *****************************************************************************/
350 static void Close( vlc_object_t * p_this )
351 {
352     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
353     sout_stream_sys_t *p_sys = p_stream->p_sys;
354
355     /* update p_sout->i_out_pace_nocontrol */
356     p_stream->p_sout->i_out_pace_nocontrol--;
357
358     if( p_sys->p_mux )
359     {
360         sout_MuxDelete( p_sys->p_mux );
361         sout_AccessOutDelete( p_sys->p_access );
362         sout_AccessOutDelete( p_sys->p_grab );
363         if( p_sys->packet )
364         {
365             sout_BufferDelete( p_stream->p_sout, p_sys->packet );
366         }
367     }
368
369     vlc_mutex_destroy( &p_sys->lock_sdp );
370
371     if( p_sys->p_httpd_file )
372     {
373         httpd_FileDelete( p_sys->p_httpd_file );
374     }
375     if( p_sys->p_httpd_host )
376     {
377         httpd_HostDelete( p_sys->p_httpd_host );
378     }
379     if( p_sys->p_rtsp_url )
380     {
381         httpd_UrlDelete( p_sys->p_rtsp_url );
382     }
383     if( p_sys->p_rtsp_host )
384     {
385         httpd_HostDelete( p_sys->p_rtsp_host );
386     }
387
388     if( p_sys->psz_sdp )
389     {
390         free( p_sys->psz_sdp );
391     }
392     free( p_sys );
393 }
394
395 /*****************************************************************************
396  * SDPGenerate
397  *****************************************************************************/
398 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
399 {
400     sout_stream_sys_t *p_sys = p_stream->p_sys;
401     int i_size;
402     char *psz_sdp, *p;
403     int i;
404
405     i_size = strlen( "v=0\n" ) +
406              strlen( "o=- * * IN IP4 127.0.0.1\n" ) +
407              strlen( "s=NONE\n" ) +
408              strlen( "c=IN IP4 */*\n" ) +
409              strlen( psz_destination ) +
410              20 + 10 + 10 + 1;
411     for( i = 0; i < p_sys->i_es; i++ )
412     {
413         sout_stream_id_t *id = p_sys->es[i];
414
415         i_size += strlen( "m=**d*o * RTP/AVP *\n" ) + 10 + 10;
416         if( id->psz_rtpmap )
417         {
418             i_size += strlen( "a=rtpmap:* *\n" ) + strlen( id->psz_rtpmap )+10;
419         }
420         if( id->psz_fmtp )
421         {
422             i_size += strlen( "a=fmtp:* *\n" ) + strlen( id->psz_fmtp ) + 10;
423         }
424         if( b_rtsp )
425         {
426             i_size += strlen( "a=control:*/trackid=*\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
427         }
428     }
429
430     p = psz_sdp = malloc( i_size );
431     p += sprintf( p, "v=0\n" );
432     p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\n",
433                   p_sys->i_sdp_id, p_sys->i_sdp_version );
434     p += sprintf( p, "s=NONE\n" );
435     p += sprintf( p, "c=IN IP4 %s/%d\n", psz_destination,
436                   p_sys->i_ttl );
437
438     for( i = 0; i < p_sys->i_es; i++ )
439     {
440         sout_stream_id_t *id = p_sys->es[i];
441
442         if( id->i_cat == AUDIO_ES )
443         {
444             p += sprintf( p, "m=audio %d RTP/AVP %d\n",
445                           id->i_port, id->i_payload_type );
446         }
447         else if( id->i_cat == VIDEO_ES )
448         {
449             p += sprintf( p, "m=video %d RTP/AVP %d\n",
450                           id->i_port, id->i_payload_type );
451         }
452         else
453         {
454             continue;
455         }
456         if( id->psz_rtpmap )
457         {
458             p += sprintf( p, "a=rtpmap:%d %s\n", id->i_payload_type,
459                           id->psz_rtpmap );
460         }
461         if( id->psz_fmtp )
462         {
463             p += sprintf( p, "a=fmtp:%d %s\n", id->i_payload_type,
464                           id->psz_fmtp );
465         }
466         if( b_rtsp )
467         {
468             p += sprintf( p, "a=control:%s/trackid=%d\n", p_sys->psz_rtsp_control, i );
469         }
470     }
471
472     return psz_sdp;
473 }
474
475 /*****************************************************************************
476  *
477  *****************************************************************************/
478 static int rtp_packetize_l16  ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
479 static int rtp_packetize_l8   ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
480 static int rtp_packetize_mpa  ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
481 static int rtp_packetize_mpv  ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
482 static int rtp_packetize_ac3  ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
483 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
484 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
485
486 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
487 {
488     static const char hex[16] = "0123456789abcdef";
489     int i;
490
491     for( i = 0; i < i_data; i++ )
492     {
493         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
494         s[2*i+1] = hex[(p_data[i]   )&0xf];
495     }
496     s[2*i_data] = '\0';
497 }
498
499 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
500 {
501     sout_instance_t   *p_sout = p_stream->p_sout;
502     sout_stream_sys_t *p_sys = p_stream->p_sys;
503     sout_stream_id_t  *id;
504     sout_access_out_t *p_access = NULL;
505
506     char access[100];
507     char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
508
509     if( p_sys->p_mux != NULL )
510     {
511         sout_input_t      *p_input  = NULL;
512         if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
513         {
514             msg_Err( p_stream, "cannot add this stream to the muxer" );
515             return NULL;
516         }
517
518         id = malloc( sizeof( sout_stream_id_t ) );
519         id->p_access    = NULL;
520         id->p_input     = p_input;
521         id->pf_packetize= NULL;
522
523         return id;
524     }
525
526     /* first try to create the access out */
527     if( p_sys->i_ttl > 0 )
528     {
529         sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
530     }
531     else
532     {
533         sprintf( access, "udp{raw}" );
534     }
535     sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
536     if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
537     {
538         msg_Err( p_stream, "cannot create the access out for %s://%s",
539                  access, url );
540         return NULL;
541     }
542
543     /* not create the rtp specific stuff */
544     id = malloc( sizeof( sout_stream_id_t ) );
545     id->p_access   = p_access;
546     id->p_input    = NULL;
547     id->psz_rtpmap = NULL;
548     id->psz_fmtp   = NULL;
549     id->psz_destination = strdup( p_sys->psz_destination );
550     id->i_port = p_sys->i_port;
551     id->p_rtsp_url = NULL;
552
553     switch( p_fmt->i_codec )
554     {
555         case VLC_FOURCC( 's', '1', '6', 'b' ):
556             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
557             {
558                 id->i_payload_type = 11;
559             }
560             else if( p_fmt->audio.i_channels == 2 &&
561                      p_fmt->audio.i_rate == 44100 )
562             {
563                 id->i_payload_type = 10;
564             }
565             else
566             {
567                 id->i_payload_type = p_sys->i_payload_type++;
568             }
569             id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
570             sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
571                      p_fmt->audio.i_channels );
572             id->i_clock_rate = p_fmt->audio.i_rate;
573             id->pf_packetize = rtp_packetize_l16;
574             break;
575         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
576             id->i_payload_type = p_sys->i_payload_type++;
577             id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
578             sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
579                      p_fmt->audio.i_channels );
580             id->i_clock_rate = p_fmt->audio.i_rate;
581             id->pf_packetize = rtp_packetize_l8;
582             break;
583         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
584             id->i_payload_type = 14;
585             id->i_clock_rate = 90000;
586             id->psz_rtpmap = strdup( "MPA/90000" );
587             id->pf_packetize = rtp_packetize_mpa;
588             break;
589         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
590             id->i_payload_type = 32;
591             id->i_clock_rate = 90000;
592             id->psz_rtpmap = strdup( "MPV/90000" );
593             id->pf_packetize = rtp_packetize_mpv;
594             break;
595         case VLC_FOURCC( 'a', '5', '2', ' ' ):
596             id->i_payload_type = p_sys->i_payload_type++;
597             id->i_clock_rate = 90000;
598             id->psz_rtpmap = strdup( "ac3/90000" );
599             id->pf_packetize = rtp_packetize_ac3;
600             break;
601         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
602         {
603             char hexa[2*p_fmt->i_extra +1];
604
605             id->i_payload_type = p_sys->i_payload_type++;
606             id->i_clock_rate = 90000;
607             id->psz_rtpmap = strdup( "MP4V-ES/90000" );
608             id->pf_packetize = rtp_packetize_split;
609             if( p_fmt->i_extra > 0 )
610             {
611                 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
612                 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
613                 sprintf( id->psz_fmtp,
614                          "profile-level-id=3; config=%s;", hexa );
615             }
616             break;
617         }
618         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
619         {
620             char hexa[2*p_fmt->i_extra +1];
621
622             id->i_payload_type = p_sys->i_payload_type++;
623             id->i_clock_rate = p_fmt->audio.i_rate;
624             id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
625             sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
626             id->pf_packetize = rtp_packetize_mp4a;
627             id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
628             sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
629             sprintf( id->psz_fmtp,
630                      "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
631                      "config=%s; SizeLength=13;IndexLength=3; "
632                      "IndexDeltaLength=3; Profile=1;", hexa );
633             break;
634         }
635
636         default:
637             msg_Err( p_stream, "cannot add this stream (unsupported "
638                      "codec:%4.4s)", (char*)&p_fmt->i_codec );
639             free( id );
640             return NULL;
641     }
642     id->i_cat = p_fmt->i_cat;
643
644     id->ssrc[0] = rand()&0xff;
645     id->ssrc[1] = rand()&0xff;
646     id->ssrc[2] = rand()&0xff;
647     id->ssrc[3] = rand()&0xff;
648     id->i_sequence = rand()&0xffff;
649     id->i_timestamp_start = rand()&0xffffffff;
650
651     id->i_mtu    = config_GetInt( p_stream, "mtu" );  /* XXX beuk */
652     if( id->i_mtu <= 16 )
653     {
654         /* better than nothing */
655         id->i_mtu = 1500;
656     }
657     msg_Dbg( p_stream, "access out %s:%s mtu=%d", access, url, id->i_mtu );
658
659     if( p_sys->p_rtsp_url )
660     {
661         char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
662
663         sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
664         fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
665         id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
666
667         if( id->p_rtsp_url )
668         {
669             httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP,    RtspCallback, (void*)p_stream );
670             httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
671             httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream );
672             httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
673         }
674     }
675
676
677     /* Update p_sys context */
678     /* update port used (2 -> 1 rtp, 1 rtcp )*/
679     TAB_APPEND( p_sys->i_es, p_sys->es, id );
680     if( p_sys->p_mux == NULL )
681     {
682         char *psz_sdp;
683         p_sys->i_port += 2;
684         psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
685
686         vlc_mutex_lock( &p_sys->lock_sdp );
687         free( p_sys->psz_sdp );
688         p_sys->psz_sdp = psz_sdp;
689         vlc_mutex_unlock( &p_sys->lock_sdp );
690
691         p_sys->i_sdp_version++;
692
693         fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
694     }
695
696     return id;
697 }
698
699 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
700 {
701     sout_stream_sys_t *p_sys = p_stream->p_sys;
702
703     TAB_REMOVE( p_sys->i_es, p_sys->es, id );
704
705     if( id->p_access )
706     {
707         if( id->psz_rtpmap )
708         {
709             free( id->psz_rtpmap );
710         }
711         if( id->psz_fmtp )
712         {
713             free( id->psz_fmtp );
714         }
715         free( id->psz_destination );
716         sout_AccessOutDelete( id->p_access );
717     }
718     else if( id->p_input )
719     {
720         sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
721     }
722     if( id->p_rtsp_url )
723     {
724         httpd_UrlDelete( id->p_rtsp_url );
725     }
726     free( id );
727     return VLC_SUCCESS;
728 }
729
730 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
731                  sout_buffer_t *p_buffer )
732 {
733     sout_buffer_t *p_next;
734
735     if( p_stream->p_sys->p_mux )
736     {
737         sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
738     }
739     else
740     {
741         while( p_buffer )
742         {
743             p_next = p_buffer->p_next;
744             if( id->pf_packetize( p_stream, id, p_buffer ) )
745             {
746                 break;
747             }
748             sout_BufferDelete( p_stream->p_sout, p_buffer );
749             p_buffer = p_next;
750         }
751     }
752     return VLC_SUCCESS;
753 }
754
755
756 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
757                                         sout_buffer_t *p_buffer )
758 {
759     sout_stream_sys_t *p_sys = p_stream->p_sys;
760
761     int64_t  i_dts = p_buffer->i_dts;
762     uint32_t i_timestamp = i_dts * 9 / 100;
763
764     uint8_t         *p_data = p_buffer->p_buffer;
765     unsigned int    i_data  = p_buffer->i_size;
766     unsigned int    i_max   = p_sys->i_mtu - 12;
767
768     int i_packet = ( p_buffer->i_size + i_max - 1 ) / i_max;
769
770     while( i_data > 0 )
771     {
772         unsigned int i_size;
773
774         /* output complete packet */
775         if( p_sys->packet &&
776             p_sys->packet->i_size + i_data > i_max )
777         {
778             sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
779             p_sys->packet = NULL;
780         }
781
782         if( p_sys->packet == NULL )
783         {
784             /* allocate a new packet */
785             p_sys->packet = sout_BufferNew( p_stream->p_sout, p_sys->i_mtu );
786             p_sys->packet->p_buffer[ 0] = 0x80;
787             p_sys->packet->p_buffer[ 1] = p_sys->i_payload_type;
788             p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
789             p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence     )&0xff;
790             p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
791             p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
792             p_sys->packet->p_buffer[ 6] = ( i_timestamp >>  8 )&0xff;
793             p_sys->packet->p_buffer[ 7] = ( i_timestamp       )&0xff;
794             p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
795             p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
796             p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
797             p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
798             p_sys->packet->i_size = 12;
799
800             p_sys->packet->i_dts = i_dts;
801             p_sys->packet->i_length = p_buffer->i_length / i_packet;
802             i_dts += p_sys->packet->i_length;
803
804             p_sys->i_sequence++;
805         }
806
807         i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_size );
808
809         memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_size],
810                 p_data,
811                 i_size );
812
813         p_sys->packet->i_size += i_size;
814         p_data += i_size;
815         i_data -= i_size;
816     }
817
818     return VLC_SUCCESS;
819 }
820
821 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
822                                   sout_buffer_t *p_buffer )
823 {
824     sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
825
826     //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_size );
827     //
828     while( p_buffer )
829     {
830         sout_buffer_t *p_next;
831
832         AccessOutGrabberWriteBuffer( p_stream, p_buffer );
833
834         p_next = p_buffer->p_next;
835         sout_BufferDelete( p_access->p_sout, p_buffer );
836         p_buffer = p_next;
837     }
838
839     return VLC_SUCCESS;
840 }
841
842 /****************************************************************************
843  * HTTP:
844  ****************************************************************************/
845 static int  HttpCallback( httpd_file_sys_t *p_args,
846                           httpd_file_t *, uint8_t *p_request,
847                           uint8_t **pp_data, int *pi_data );
848
849 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
850 {
851     sout_stream_sys_t *p_sys = p_stream->p_sys;
852
853     p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
854     if( p_sys->p_httpd_host )
855     {
856         p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
857                                              url->psz_path ? url->psz_path : "/",
858                                              "application/sdp",
859                                              NULL, NULL,
860                                              HttpCallback, (void*)p_sys );
861     }
862     if( p_sys->p_httpd_file == NULL )
863     {
864         return VLC_EGENERIC;
865     }
866     return VLC_SUCCESS;
867 }
868
869 static int  HttpCallback( httpd_file_sys_t *p_args,
870                           httpd_file_t *f, uint8_t *p_request,
871                           uint8_t **pp_data, int *pi_data )
872 {
873     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
874
875     vlc_mutex_lock( &p_sys->lock_sdp );
876     if( p_sys->psz_sdp && *p_sys->psz_sdp )
877     {
878         *pi_data = strlen( p_sys->psz_sdp );
879         *pp_data = malloc( *pi_data );
880         memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
881     }
882     else
883     {
884         *pp_data = NULL;
885         *pi_data = 0;
886     }
887     vlc_mutex_unlock( &p_sys->lock_sdp );
888
889     return VLC_SUCCESS;
890 }
891
892 /****************************************************************************
893  * RTSP:
894  ****************************************************************************/
895 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
896 {
897     sout_stream_sys_t *p_sys = p_stream->p_sys;
898
899     fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
900
901     p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
902     if( p_sys->p_rtsp_host == NULL )
903     {
904         return VLC_EGENERIC;
905     }
906
907     p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
908     p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
909     sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
910              url->psz_host,  url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
911
912     p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
913     if( p_sys->p_rtsp_url == 0 )
914     {
915         return VLC_EGENERIC;
916     }
917     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
918     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP,    RtspCallback, (void*)p_stream );
919     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
920     /* httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream ); */
921     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
922
923     return VLC_SUCCESS;
924 }
925
926 static int  RtspCallback( httpd_callback_sys_t *p_args,
927                           httpd_client_t *cl,
928                           httpd_message_t *answer, httpd_message_t *query )
929 {
930     sout_stream_t *p_stream = (sout_stream_t*)p_args;
931     char          *psz_destination = p_stream->p_sys->psz_destination;
932     char          *psz_session = NULL;
933
934     if( psz_destination && *psz_destination == 0 )
935     {
936         psz_destination = NULL;
937     }
938
939     if( answer == NULL || query == NULL )
940     {
941         return VLC_SUCCESS;
942     }
943     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
944
945     answer->i_proto = HTTPD_PROTO_RTSP;
946     answer->i_version= query->i_version;
947     answer->i_type   = HTTPD_MSG_ANSWER;
948
949     switch( query->i_type )
950     {
951         case HTTPD_MSG_DESCRIBE:
952         {
953             char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
954
955             answer->i_status = 200;
956             answer->psz_status = strdup( "OK" );
957             httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
958
959             answer->p_body = psz_sdp;
960             answer->i_body = strlen( psz_sdp );
961             break;
962         }
963
964         case HTTPD_MSG_SETUP:
965         {
966             char *psz_transport = httpd_MsgGet( query, "Transport" );
967
968             fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
969             if( strstr( psz_transport, "multicast" ) )
970             {
971                 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
972                 answer->i_status = 200;
973                 answer->psz_status = strdup( "OK" );
974                 answer->i_body = 0;
975                 answer->p_body = NULL;
976                 psz_session = httpd_MsgGet( query, "Session" );
977                 if( *psz_session == 0 )
978                 {
979                     psz_session = malloc( 100 );
980                     sprintf( psz_session, "%d", rand() );
981                 }
982             }
983             else /*if( strstr( psz_transport, "unicast" ) ||
984                        strstr( psz_transport, "interleaved" ) ) */
985             {
986                 answer->i_status = 400;
987                 answer->psz_status = strdup( "Bad Request" );
988                 answer->i_body = 0;
989                 answer->p_body = NULL;
990             }
991             break;
992         }
993         case HTTPD_MSG_PLAY:
994         {
995             /* for now only multicast so easy */
996             answer->i_status = 200;
997             answer->psz_status = strdup( "OK" );
998             answer->i_body = 0;
999             answer->p_body = NULL;
1000
1001             psz_session = httpd_MsgGet( query, "Session" );
1002             break;
1003         }
1004         case HTTPD_MSG_PAUSE:
1005             /* FIXME */
1006             return VLC_EGENERIC;
1007         case HTTPD_MSG_TEARDOWN:
1008             /* for now only multicast so easy again */
1009             answer->i_status = 200;
1010             answer->psz_status = strdup( "OK" );
1011             answer->i_body = 0;
1012             answer->p_body = NULL;
1013
1014             psz_session = httpd_MsgGet( query, "Session" );
1015             break;
1016
1017         default:
1018             return VLC_EGENERIC;
1019     }
1020     httpd_MsgAdd( answer, "Server", "VLC Server" );
1021     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1022     httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1023     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1024
1025     if( psz_session )
1026     {
1027         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1028     }
1029     return VLC_SUCCESS;
1030 }
1031
1032 /****************************************************************************
1033  * rtp_packetize_*:
1034  ****************************************************************************/
1035 static void rtp_packetize_common( sout_stream_id_t *id, sout_buffer_t *out,
1036                                   int b_marker, int64_t i_pts )
1037 {
1038     uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1039
1040     out->p_buffer[0] = 0x80;
1041     out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1042     out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1043     out->p_buffer[3] = ( id->i_sequence     )&0xff;
1044     out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1045     out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1046     out->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
1047     out->p_buffer[7] = ( i_timestamp       )&0xff;
1048
1049     out->p_buffer[ 8] = id->ssrc[0];
1050     out->p_buffer[ 9] = id->ssrc[1];
1051     out->p_buffer[10] = id->ssrc[2];
1052     out->p_buffer[11] = id->ssrc[3];
1053
1054     out->i_size = 12;
1055     id->i_sequence++;
1056 }
1057
1058 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1059                               sout_buffer_t *in )
1060 {
1061     int     i_max   = id->i_mtu - 12 - 4; /* payload max in one packet */
1062     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1063
1064     uint8_t *p_data = in->p_buffer;
1065     int     i_data  = in->i_size;
1066     int     i;
1067
1068     for( i = 0; i < i_count; i++ )
1069     {
1070         int           i_payload = __MIN( i_max, i_data );
1071         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 16 + i_payload );
1072
1073         /* rtp common header */
1074         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1075         /* mbz set to 0 */
1076         out->p_buffer[12] = 0;
1077         out->p_buffer[13] = 0;
1078         /* fragment offset in the current frame */
1079         out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1080         out->p_buffer[15] = ( (i*i_max)      )&0xff;
1081         memcpy( &out->p_buffer[16], p_data, i_payload );
1082
1083         out->i_size   = 16 + i_payload;
1084         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1085         out->i_length = in->i_length / i_count;
1086
1087         sout_AccessOutWrite( id->p_access, out );
1088
1089         p_data += i_payload;
1090         i_data -= i_payload;
1091     }
1092
1093     return VLC_SUCCESS;
1094 }
1095
1096 /* rfc2250 */
1097 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1098                               sout_buffer_t *in )
1099 {
1100     int     i_max   = id->i_mtu - 12 - 4; /* payload max in one packet */
1101     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1102
1103     uint8_t *p_data = in->p_buffer;
1104     int     i_data  = in->i_size;
1105     int     i;
1106     int     b_sequence_start = 0;
1107     int     i_temporal_ref = 0;
1108     int     i_picture_coding_type = 0;
1109     int     i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1110     int     b_start_slice = 0;
1111
1112     /* preparse this packet to get some info */
1113     if( in->i_size > 4 )
1114     {
1115         uint8_t *p = p_data;
1116         int      i_rest = in->i_size;
1117
1118         for( ;; )
1119         {
1120             while( i_rest > 4 &&
1121                    ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1122             {
1123                 p++;
1124                 i_rest--;
1125             }
1126             if( i_rest <= 4 )
1127             {
1128                 break;
1129             }
1130             p += 3;
1131             i_rest -= 4;
1132
1133             if( *p == 0xb3 )
1134             {
1135                 /* sequence start code */
1136                 b_sequence_start = 1;
1137             }
1138             else if( *p == 0x00 && i_rest >= 4 )
1139             {
1140                 /* picture */
1141                 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1142                 i_picture_coding_type = (p[2] >> 3)&0x07;
1143
1144                 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1145                                     i_picture_coding_type == 3 ) )
1146                 {
1147                     i_ffv = (p[3] >> 2)&0x01;
1148                     i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1149                     if( i_rest > 4 && i_picture_coding_type == 3 )
1150                     {
1151                         i_fbv = (p[4]>>6)&0x01;
1152                         i_bfc = (p[4]>>3)&0x07;
1153                     }
1154                 }
1155             }
1156             else if( *p <= 0xaf )
1157             {
1158                 b_start_slice = 1;
1159             }
1160         }
1161     }
1162
1163     for( i = 0; i < i_count; i++ )
1164     {
1165         int           i_payload = __MIN( i_max, i_data );
1166         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout,
1167                                              16 + i_payload );
1168         uint32_t      h = ( i_temporal_ref << 16 )|
1169                           ( b_sequence_start << 13 )|
1170                           ( b_start_slice << 12 )|
1171                           ( i == i_count - 1 ? 1 << 11 : 0 )|
1172                           ( i_picture_coding_type << 8 )|
1173                           ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1174
1175         /* rtp common header */
1176         rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1177                               in->i_pts > 0 ? in->i_pts : in->i_dts );
1178
1179         /* 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 */
1180         out->p_buffer[12] = ( h >> 24 )&0xff;
1181         out->p_buffer[13] = ( h >> 16 )&0xff;
1182         out->p_buffer[14] = ( h >>  8 )&0xff;
1183         out->p_buffer[15] = ( h       )&0xff;
1184
1185         memcpy( &out->p_buffer[16], p_data, i_payload );
1186
1187         out->i_size   = 16 + i_payload;
1188         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1189         out->i_length = in->i_length / i_count;
1190
1191         sout_AccessOutWrite( id->p_access, out );
1192
1193         p_data += i_payload;
1194         i_data -= i_payload;
1195     }
1196
1197     return VLC_SUCCESS;
1198 }
1199 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1200                               sout_buffer_t *in )
1201 {
1202     int     i_max   = id->i_mtu - 12 - 2; /* payload max in one packet */
1203     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1204
1205     uint8_t *p_data = in->p_buffer;
1206     int     i_data  = in->i_size;
1207     int     i;
1208
1209     for( i = 0; i < i_count; i++ )
1210     {
1211         int           i_payload = __MIN( i_max, i_data );
1212         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 14 + i_payload );
1213
1214         /* rtp common header */
1215         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1216         /* unit count */
1217         out->p_buffer[12] = 1;
1218         /* unit header */
1219         out->p_buffer[13] = 0x00;
1220         /* data */
1221         memcpy( &out->p_buffer[14], p_data, i_payload );
1222
1223         out->i_size   = 14 + i_payload;
1224         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1225         out->i_length = in->i_length / i_count;
1226
1227         sout_AccessOutWrite( id->p_access, out );
1228
1229         p_data += i_payload;
1230         i_data -= i_payload;
1231     }
1232
1233     return VLC_SUCCESS;
1234 }
1235
1236 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1237                                 sout_buffer_t *in )
1238 {
1239     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1240     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1241
1242     uint8_t *p_data = in->p_buffer;
1243     int     i_data  = in->i_size;
1244     int     i;
1245
1246     for( i = 0; i < i_count; i++ )
1247     {
1248         int           i_payload = __MIN( i_max, i_data );
1249         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 12 + i_payload );
1250
1251         /* rtp common header */
1252         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1253                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1254         memcpy( &out->p_buffer[12], p_data, i_payload );
1255
1256         out->i_size   = 12 + i_payload;
1257         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1258         out->i_length = in->i_length / i_count;
1259
1260         sout_AccessOutWrite( id->p_access, out );
1261
1262         p_data += i_payload;
1263         i_data -= i_payload;
1264     }
1265
1266     return VLC_SUCCESS;
1267 }
1268
1269 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1270                               sout_buffer_t *in )
1271 {
1272     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1273     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1274
1275     uint8_t *p_data = in->p_buffer;
1276     int     i_data  = in->i_size;
1277     int     i_packet = 0;
1278
1279     while( i_data > 0 )
1280     {
1281         int           i_payload = (__MIN( i_max, i_data )/4)*4;
1282         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 12 + i_payload );
1283
1284         /* rtp common header */
1285         rtp_packetize_common( id, out, 0,
1286                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1287         memcpy( &out->p_buffer[12], p_data, i_payload );
1288
1289         out->i_size   = 12 + i_payload;
1290         out->i_dts    = in->i_dts + i_packet * in->i_length / i_count;
1291         out->i_length = in->i_length / i_count;
1292
1293         sout_AccessOutWrite( id->p_access, out );
1294
1295         p_data += i_payload;
1296         i_data -= i_payload;
1297         i_packet++;
1298     }
1299
1300     return VLC_SUCCESS;
1301 }
1302
1303 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1304                              sout_buffer_t *in )
1305 {
1306     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1307     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1308
1309     uint8_t *p_data = in->p_buffer;
1310     int     i_data  = in->i_size;
1311     int     i_packet = 0;
1312
1313     while( i_data > 0 )
1314     {
1315         int           i_payload = (__MIN( i_max, i_data )/2)*2;
1316         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 12 + i_payload );
1317
1318         /* rtp common header */
1319         rtp_packetize_common( id, out, 0,
1320                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1321         memcpy( &out->p_buffer[12], p_data, i_payload );
1322
1323         out->i_size   = 12 + i_payload;
1324         out->i_dts    = in->i_dts + i_packet * in->i_length / i_count;
1325         out->i_length = in->i_length / i_count;
1326
1327         sout_AccessOutWrite( id->p_access, out );
1328
1329         p_data += i_payload;
1330         i_data -= i_payload;
1331         i_packet++;
1332     }
1333
1334     return VLC_SUCCESS;
1335 }
1336 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1337                                sout_buffer_t *in )
1338 {
1339     int     i_max   = id->i_mtu - 16; /* payload max in one packet */
1340     int     i_count = ( in->i_size + i_max - 1 ) / i_max;
1341
1342     uint8_t *p_data = in->p_buffer;
1343     int     i_data  = in->i_size;
1344     int     i;
1345
1346     for( i = 0; i < i_count; i++ )
1347     {
1348         int           i_payload = __MIN( i_max, i_data );
1349         sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 16 + i_payload );
1350
1351         /* rtp common header */
1352         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1353                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1354         /* AU headers */
1355         /* AU headers length (bits) */
1356         out->p_buffer[12] = 0;
1357         out->p_buffer[13] = 2*8;
1358         /* for each AU length 13 bits + idx 3bits, */
1359         out->p_buffer[14] = ( in->i_size >> 5 )&0xff;
1360         out->p_buffer[15] = ( (in->i_size&0xff)<<3 )|0;
1361
1362         memcpy( &out->p_buffer[16], p_data, i_payload );
1363
1364         out->i_size   = 16 + i_payload;
1365         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1366         out->i_length = in->i_length / i_count;
1367
1368         sout_AccessOutWrite( id->p_access, out );
1369
1370         p_data += i_payload;
1371         i_data -= i_payload;
1372     }
1373
1374     return VLC_SUCCESS;
1375 }