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