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