]> git.sesse.net Git - vlc/blob - modules/stream_out/rtp.c
* Massive spelling corrections.
[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
604 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
605 {
606     static const char hex[16] = "0123456789abcdef";
607     int i;
608
609     for( i = 0; i < i_data; i++ )
610     {
611         s[2*i+0] = hex[(p_data[i]>>4)&0xf];
612         s[2*i+1] = hex[(p_data[i]   )&0xf];
613     }
614     s[2*i_data] = '\0';
615 }
616
617 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
618 {
619     sout_instance_t   *p_sout = p_stream->p_sout;
620     sout_stream_sys_t *p_sys = p_stream->p_sys;
621     sout_stream_id_t  *id;
622     sout_access_out_t *p_access = NULL;
623
624     if( p_sys->p_mux != NULL )
625     {
626         sout_input_t      *p_input  = NULL;
627         if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
628         {
629             msg_Err( p_stream, "cannot add this stream to the muxer" );
630             return NULL;
631         }
632
633         id = malloc( sizeof( sout_stream_id_t ) );
634         memset( id, 0, sizeof( sout_stream_id_t ) );
635         id->p_access    = NULL;
636         id->p_input     = p_input;
637         id->pf_packetize= NULL;
638         id->p_rtsp_url  = NULL;
639
640         return id;
641     }
642
643     if( p_sys->psz_destination )
644     {
645         char access[100];
646         char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
647
648         /* first try to create the access out */
649         if( p_sys->i_ttl > 0 )
650         {
651             sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
652         }
653         else
654         {
655             sprintf( access, "udp{raw}" );
656         }
657         sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
658         if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
659         {
660             msg_Err( p_stream, "cannot create the access out for %s://%s",
661                      access, url );
662             return NULL;
663         }
664         msg_Dbg( p_stream, "access out %s:%s", access, url );
665     }
666
667     /* not create the rtp specific stuff */
668     id = malloc( sizeof( sout_stream_id_t ) );
669     memset( id, 0, sizeof( sout_stream_id_t ) );
670     id->p_stream   = p_stream;
671     id->p_access   = p_access;
672     id->p_input    = NULL;
673     id->psz_rtpmap = NULL;
674     id->psz_fmtp   = NULL;
675     id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
676     id->i_port = p_sys->i_port;
677     id->p_rtsp_url = NULL;
678     vlc_mutex_init( p_stream, &id->lock_rtsp );
679     id->i_rtsp_access = 0;
680     id->rtsp_access = NULL;
681
682     switch( p_fmt->i_codec )
683     {
684         case VLC_FOURCC( 's', '1', '6', 'b' ):
685             if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
686             {
687                 id->i_payload_type = 11;
688             }
689             else if( p_fmt->audio.i_channels == 2 &&
690                      p_fmt->audio.i_rate == 44100 )
691             {
692                 id->i_payload_type = 10;
693             }
694             else
695             {
696                 id->i_payload_type = p_sys->i_payload_type++;
697             }
698             id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
699             sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
700                      p_fmt->audio.i_channels );
701             id->i_clock_rate = p_fmt->audio.i_rate;
702             id->pf_packetize = rtp_packetize_l16;
703             break;
704         case VLC_FOURCC( 'u', '8', ' ', ' ' ):
705             id->i_payload_type = p_sys->i_payload_type++;
706             id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
707             sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
708                      p_fmt->audio.i_channels );
709             id->i_clock_rate = p_fmt->audio.i_rate;
710             id->pf_packetize = rtp_packetize_l8;
711             break;
712         case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
713             id->i_payload_type = 14;
714             id->i_clock_rate = 90000;
715             id->psz_rtpmap = strdup( "MPA/90000" );
716             id->pf_packetize = rtp_packetize_mpa;
717             break;
718         case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
719             id->i_payload_type = 32;
720             id->i_clock_rate = 90000;
721             id->psz_rtpmap = strdup( "MPV/90000" );
722             id->pf_packetize = rtp_packetize_mpv;
723             break;
724         case VLC_FOURCC( 'a', '5', '2', ' ' ):
725             id->i_payload_type = p_sys->i_payload_type++;
726             id->i_clock_rate = 90000;
727             id->psz_rtpmap = strdup( "ac3/90000" );
728             id->pf_packetize = rtp_packetize_ac3;
729             break;
730         case VLC_FOURCC( 'm', 'p', '4', 'v' ):
731         {
732             char hexa[2*p_fmt->i_extra +1];
733
734             id->i_payload_type = p_sys->i_payload_type++;
735             id->i_clock_rate = 90000;
736             id->psz_rtpmap = strdup( "MP4V-ES/90000" );
737             id->pf_packetize = rtp_packetize_split;
738             if( p_fmt->i_extra > 0 )
739             {
740                 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
741                 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
742                 sprintf( id->psz_fmtp,
743                          "profile-level-id=3; config=%s;", hexa );
744             }
745             break;
746         }
747         case VLC_FOURCC( 'm', 'p', '4', 'a' ):
748         {
749             char hexa[2*p_fmt->i_extra +1];
750
751             id->i_payload_type = p_sys->i_payload_type++;
752             id->i_clock_rate = p_fmt->audio.i_rate;
753             id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
754             sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
755             id->pf_packetize = rtp_packetize_mp4a;
756             id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
757             sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
758             sprintf( id->psz_fmtp,
759                      "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
760                      "config=%s; SizeLength=13;IndexLength=3; "
761                      "IndexDeltaLength=3; Profile=1;", hexa );
762             break;
763         }
764
765         default:
766             msg_Err( p_stream, "cannot add this stream (unsupported "
767                      "codec:%4.4s)", (char*)&p_fmt->i_codec );
768             free( id );
769             return NULL;
770     }
771     id->i_cat = p_fmt->i_cat;
772
773     id->ssrc[0] = rand()&0xff;
774     id->ssrc[1] = rand()&0xff;
775     id->ssrc[2] = rand()&0xff;
776     id->ssrc[3] = rand()&0xff;
777     id->i_sequence = rand()&0xffff;
778     id->i_timestamp_start = rand()&0xffffffff;
779
780     id->i_mtu    = config_GetInt( p_stream, "mtu" );  /* XXX beuk */
781     if( id->i_mtu <= 16 )
782     {
783         /* better than nothing */
784         id->i_mtu = 1500;
785     }
786     msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
787
788     if( p_sys->p_rtsp_url )
789     {
790         char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
791
792         sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
793         fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
794         id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
795
796         if( id->p_rtsp_url )
797         {
798             httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP,    RtspCallbackId, (void*)id );
799             //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
800             //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream );
801         }
802     }
803
804
805     /* Update p_sys context */
806     /* update port used (2 -> 1 rtp, 1 rtcp )*/
807     vlc_mutex_lock( &p_sys->lock_es );
808     TAB_APPEND( p_sys->i_es, p_sys->es, id );
809     vlc_mutex_unlock( &p_sys->lock_es );
810
811     if( p_sys->p_mux == NULL )
812     {
813         char *psz_sdp;
814         p_sys->i_port += 2;
815         psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
816
817         vlc_mutex_lock( &p_sys->lock_sdp );
818         free( p_sys->psz_sdp );
819         p_sys->psz_sdp = psz_sdp;
820         vlc_mutex_unlock( &p_sys->lock_sdp );
821
822         p_sys->i_sdp_version++;
823
824         fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
825
826         /* Update the SAP announce */
827         if( p_sys->b_export_sap )
828         {
829             SapSetup( p_stream );
830         }
831     }
832
833     return id;
834 }
835
836 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
837 {
838     sout_stream_sys_t *p_sys = p_stream->p_sys;
839
840     vlc_mutex_lock( &p_sys->lock_es );
841     TAB_REMOVE( p_sys->i_es, p_sys->es, id );
842     vlc_mutex_unlock( &p_sys->lock_es );
843
844     if( id->p_access )
845     {
846         if( id->psz_rtpmap )
847         {
848             free( id->psz_rtpmap );
849         }
850         if( id->psz_fmtp )
851         {
852             free( id->psz_fmtp );
853         }
854         if( id->psz_destination )
855             free( id->psz_destination );
856         sout_AccessOutDelete( id->p_access );
857     }
858     else if( id->p_input )
859     {
860         sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
861     }
862     if( id->p_rtsp_url )
863     {
864         httpd_UrlDelete( id->p_rtsp_url );
865     }
866     vlc_mutex_destroy( &id->lock_rtsp );
867     if( id->rtsp_access ) free( id->rtsp_access );
868
869     /* Update the SAP announce */
870     if( p_sys->b_export_sap )
871     {
872         SapSetup( p_stream );
873     }
874
875     free( id );
876     return VLC_SUCCESS;
877 }
878
879 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
880                  block_t *p_buffer )
881 {
882     block_t *p_next;
883
884     if( p_stream->p_sys->p_mux )
885     {
886         sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
887     }
888     else
889     {
890         while( p_buffer )
891         {
892             p_next = p_buffer->p_next;
893             if( id->pf_packetize( p_stream, id, p_buffer ) )
894             {
895                 break;
896             }
897             block_Release( p_buffer );
898             p_buffer = p_next;
899         }
900     }
901     return VLC_SUCCESS;
902 }
903
904
905 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
906                                         block_t *p_buffer )
907 {
908     sout_stream_sys_t *p_sys = p_stream->p_sys;
909
910     int64_t  i_dts = p_buffer->i_dts;
911     uint32_t i_timestamp = i_dts * 9 / 100;
912
913     uint8_t         *p_data = p_buffer->p_buffer;
914     unsigned int    i_data  = p_buffer->i_buffer;
915     unsigned int    i_max   = p_sys->i_mtu - 12;
916
917     int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
918
919     while( i_data > 0 )
920     {
921         unsigned int i_size;
922
923         /* output complete packet */
924         if( p_sys->packet &&
925             p_sys->packet->i_buffer + i_data > i_max )
926         {
927             sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
928             p_sys->packet = NULL;
929         }
930
931         if( p_sys->packet == NULL )
932         {
933             /* allocate a new packet */
934             p_sys->packet = block_New( p_stream, p_sys->i_mtu );
935             p_sys->packet->p_buffer[ 0] = 0x80;
936             p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
937             p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
938             p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence     )&0xff;
939             p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
940             p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
941             p_sys->packet->p_buffer[ 6] = ( i_timestamp >>  8 )&0xff;
942             p_sys->packet->p_buffer[ 7] = ( i_timestamp       )&0xff;
943             p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
944             p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
945             p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
946             p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
947             p_sys->packet->i_buffer = 12;
948
949             p_sys->packet->i_dts = i_dts;
950             p_sys->packet->i_length = p_buffer->i_length / i_packet;
951             i_dts += p_sys->packet->i_length;
952
953             p_sys->i_sequence++;
954         }
955
956         i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
957
958         memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
959                 p_data, i_size );
960
961         p_sys->packet->i_buffer += i_size;
962         p_data += i_size;
963         i_data -= i_size;
964     }
965
966     return VLC_SUCCESS;
967 }
968
969 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
970                                   block_t *p_buffer )
971 {
972     sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
973
974     //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
975     //
976     while( p_buffer )
977     {
978         block_t *p_next;
979
980         AccessOutGrabberWriteBuffer( p_stream, p_buffer );
981
982         p_next = p_buffer->p_next;
983         block_Release( p_buffer );
984         p_buffer = p_next;
985     }
986
987     return VLC_SUCCESS;
988 }
989
990 /****************************************************************************
991  * SAP:
992  ****************************************************************************/
993 static int SapSetup( sout_stream_t *p_stream )
994 {
995     sout_stream_sys_t *p_sys = p_stream->p_sys;
996     sout_instance_t   *p_sout = p_stream->p_sout;
997     announce_method_t *p_method = (announce_method_t *)
998                                   malloc(sizeof(announce_method_t));
999
1000     /* Remove the previous session */
1001     if( p_sys->p_session != NULL)
1002     {
1003         sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1004         sout_AnnounceSessionDestroy( p_sys->p_session );
1005         p_sys->p_session = NULL;
1006     }
1007     p_method->i_type = METHOD_TYPE_SAP;
1008     p_method->psz_address = NULL; /* FIXME */
1009     p_method->i_ip_version = 4; /* FIXME ! */
1010
1011     if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
1012     {
1013         p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1014                                                      p_method );
1015     }
1016
1017     free( p_method );
1018     return VLC_SUCCESS;
1019 }
1020
1021 /****************************************************************************
1022  * HTTP:
1023  ****************************************************************************/
1024 static int  HttpCallback( httpd_file_sys_t *p_args,
1025                           httpd_file_t *, uint8_t *p_request,
1026                           uint8_t **pp_data, int *pi_data );
1027
1028 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1029 {
1030     sout_stream_sys_t *p_sys = p_stream->p_sys;
1031
1032     p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1033     if( p_sys->p_httpd_host )
1034     {
1035         p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1036                                              url->psz_path ? url->psz_path : "/",
1037                                              "application/sdp",
1038                                              NULL, NULL,
1039                                              HttpCallback, (void*)p_sys );
1040     }
1041     if( p_sys->p_httpd_file == NULL )
1042     {
1043         return VLC_EGENERIC;
1044     }
1045     return VLC_SUCCESS;
1046 }
1047
1048 static int  HttpCallback( httpd_file_sys_t *p_args,
1049                           httpd_file_t *f, uint8_t *p_request,
1050                           uint8_t **pp_data, int *pi_data )
1051 {
1052     sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1053
1054     vlc_mutex_lock( &p_sys->lock_sdp );
1055     if( p_sys->psz_sdp && *p_sys->psz_sdp )
1056     {
1057         *pi_data = strlen( p_sys->psz_sdp );
1058         *pp_data = malloc( *pi_data );
1059         memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1060     }
1061     else
1062     {
1063         *pp_data = NULL;
1064         *pi_data = 0;
1065     }
1066     vlc_mutex_unlock( &p_sys->lock_sdp );
1067
1068     return VLC_SUCCESS;
1069 }
1070
1071 /****************************************************************************
1072  * RTSP:
1073  ****************************************************************************/
1074 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1075 {
1076     rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1077
1078     rtsp->psz_session = psz_session;
1079     rtsp->i_last = 0;
1080     rtsp->b_playing = VLC_FALSE;
1081     rtsp->i_id = 0;
1082     rtsp->id = NULL;
1083     rtsp->i_access = 0;
1084     rtsp->access = NULL;
1085
1086     TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1087
1088     return rtsp;
1089 }
1090 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1091 {
1092     int i;
1093     for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1094     {
1095         if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1096         {
1097             return p_stream->p_sys->rtsp[i];
1098         }
1099     }
1100     return NULL;
1101 }
1102
1103 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1104 {
1105     int i;
1106     TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1107
1108     for( i = 0; i < rtsp->i_access; i++ )
1109     {
1110         sout_AccessOutDelete( rtsp->access[i] );
1111     }
1112     if( rtsp->id )     free( rtsp->id );
1113     if( rtsp->access ) free( rtsp->access );
1114
1115     free( rtsp->psz_session );
1116     free( rtsp );
1117 }
1118
1119 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1120 {
1121     sout_stream_sys_t *p_sys = p_stream->p_sys;
1122
1123     fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1124
1125     p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1126     if( p_sys->p_rtsp_host == NULL )
1127     {
1128         return VLC_EGENERIC;
1129     }
1130
1131     p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1132     p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1133     sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1134              url->psz_host,  url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1135
1136     p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
1137     if( p_sys->p_rtsp_url == 0 )
1138     {
1139         return VLC_EGENERIC;
1140     }
1141     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1142     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY,     RtspCallback, (void*)p_stream );
1143     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE,    RtspCallback, (void*)p_stream );
1144     httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1145
1146     return VLC_SUCCESS;
1147 }
1148
1149 static int  RtspCallback( httpd_callback_sys_t *p_args,
1150                           httpd_client_t *cl,
1151                           httpd_message_t *answer, httpd_message_t *query )
1152 {
1153     sout_stream_t *p_stream = (sout_stream_t*)p_args;
1154     sout_stream_sys_t *p_sys = p_stream->p_sys;
1155     char          *psz_destination = p_sys->psz_destination;
1156     char          *psz_session = NULL;
1157
1158     if( answer == NULL || query == NULL )
1159     {
1160         return VLC_SUCCESS;
1161     }
1162     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1163
1164     answer->i_proto = HTTPD_PROTO_RTSP;
1165     answer->i_version= query->i_version;
1166     answer->i_type   = HTTPD_MSG_ANSWER;
1167
1168     switch( query->i_type )
1169     {
1170         case HTTPD_MSG_DESCRIBE:
1171         {
1172             char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1173
1174             answer->i_status = 200;
1175             answer->psz_status = strdup( "OK" );
1176             httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );
1177
1178             answer->p_body = psz_sdp;
1179             answer->i_body = strlen( psz_sdp );
1180             break;
1181         }
1182
1183         case HTTPD_MSG_PLAY:
1184         {
1185             rtsp_client_t *rtsp;
1186             /* for now only multicast so easy */
1187             answer->i_status = 200;
1188             answer->psz_status = strdup( "OK" );
1189             answer->i_body = 0;
1190             answer->p_body = NULL;
1191
1192             psz_session = httpd_MsgGet( query, "Session" );
1193             rtsp = RtspClientGet( p_stream, psz_session );
1194             if( rtsp && !rtsp->b_playing )
1195             {
1196                 int i_id;
1197                 /* FIXME */
1198                 rtsp->b_playing = VLC_TRUE;
1199
1200                 vlc_mutex_lock( &p_sys->lock_es );
1201                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1202                 {
1203                     sout_stream_id_t *id = rtsp->id[i_id];
1204                     int i;
1205
1206                     for( i = 0; i < p_sys->i_es; i++ )
1207                     {
1208                         if( id == p_sys->es[i] )
1209                             break;
1210                     }
1211                     if( i >= p_sys->i_es ) continue;
1212
1213                     vlc_mutex_lock( &id->lock_rtsp );
1214                     TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1215                     vlc_mutex_unlock( &id->lock_rtsp );
1216                 }
1217                 vlc_mutex_unlock( &p_sys->lock_es );
1218             }
1219             break;
1220         }
1221         case HTTPD_MSG_PAUSE:
1222             /* FIXME */
1223             return VLC_EGENERIC;
1224         case HTTPD_MSG_TEARDOWN:
1225         {
1226             rtsp_client_t *rtsp;
1227
1228             /* for now only multicast so easy again */
1229             answer->i_status = 200;
1230             answer->psz_status = strdup( "OK" );
1231             answer->i_body = 0;
1232             answer->p_body = NULL;
1233
1234             psz_session = httpd_MsgGet( query, "Session" );
1235             rtsp = RtspClientGet( p_stream, psz_session );
1236             if( rtsp )
1237             {
1238                 int i_id;
1239
1240                 vlc_mutex_lock( &p_sys->lock_es );
1241                 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1242                 {
1243                     sout_stream_id_t *id = rtsp->id[i_id];
1244                     int i;
1245
1246                     for( i = 0; i < p_sys->i_es; i++ )
1247                     {
1248                         if( id == p_sys->es[i] )
1249                             break;
1250                     }
1251                     if( i >= p_sys->i_es ) continue;
1252
1253                     vlc_mutex_lock( &id->lock_rtsp );
1254                     TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1255                     vlc_mutex_unlock( &id->lock_rtsp );
1256                 }
1257                 vlc_mutex_unlock( &p_sys->lock_es );
1258
1259                 RtspClientDel( p_stream, rtsp );
1260             }
1261             break;
1262         }
1263
1264         default:
1265             return VLC_EGENERIC;
1266     }
1267     httpd_MsgAdd( answer, "Server", "VLC Server" );
1268     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1269     httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1270     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1271
1272     if( psz_session )
1273     {
1274         httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1275     }
1276     return VLC_SUCCESS;
1277 }
1278
1279 static int  RtspCallbackId( httpd_callback_sys_t *p_args,
1280                           httpd_client_t *cl,
1281                           httpd_message_t *answer, httpd_message_t *query )
1282 {
1283     sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1284     sout_stream_t    *p_stream = id->p_stream;
1285     sout_stream_sys_t *p_sys = p_stream->p_sys;
1286     char          *psz_session = NULL;
1287
1288     if( answer == NULL || query == NULL )
1289     {
1290         return VLC_SUCCESS;
1291     }
1292     fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1293
1294     answer->i_proto = HTTPD_PROTO_RTSP;
1295     answer->i_version= query->i_version;
1296     answer->i_type   = HTTPD_MSG_ANSWER;
1297
1298     switch( query->i_type )
1299     {
1300         case HTTPD_MSG_SETUP:
1301         {
1302             char *psz_transport = httpd_MsgGet( query, "Transport" );
1303
1304             fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1305
1306             if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1307             {
1308                 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1309                 answer->i_status = 200;
1310                 answer->psz_status = strdup( "OK" );
1311                 answer->i_body = 0;
1312                 answer->p_body = NULL;
1313                 psz_session = httpd_MsgGet( query, "Session" );
1314                 if( *psz_session == 0 )
1315                 {
1316                     psz_session = malloc( 100 );
1317                     sprintf( psz_session, "%d", rand() );
1318                 }
1319                 httpd_MsgAdd( answer, "Transport",
1320                               "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1321                               id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1322             }
1323             else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1324             {
1325                 int  i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1326                 char *ip    = httpd_ClientIP( cl );
1327
1328                 char psz_access[100];
1329                 char psz_url[100];
1330
1331                 sout_access_out_t *p_access;
1332
1333                 rtsp_client_t *rtsp = NULL;
1334
1335                 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1336                          ip, i_port );
1337
1338                 psz_session = httpd_MsgGet( query, "Session" );
1339                 if( *psz_session == 0 )
1340                 {
1341                     psz_session = malloc( 100 );
1342                     sprintf( psz_session, "%d", rand() );
1343
1344                     rtsp = RtspClientNew( p_stream, psz_session );
1345                 }
1346                 else
1347                 {
1348                     rtsp = RtspClientGet( p_stream, psz_session );
1349                     if( rtsp == NULL )
1350                     {
1351                         /* FIXME right error code */
1352                         answer->i_status = 400;
1353                         answer->psz_status = strdup( "Unknown session id" );
1354                         answer->i_body = 0;
1355                         answer->p_body = NULL;
1356                         break;
1357                     }
1358                 }
1359
1360                 /* first try to create the access out */
1361                 if( p_sys->i_ttl > 0 )
1362                     sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1363                 else
1364                     sprintf( psz_access, "udp{raw}" );
1365                 sprintf( psz_url, "%s:%d", ip, i_port );
1366                 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1367                 {
1368                     msg_Err( p_stream, "cannot create the access out for %s://%s",
1369                              psz_access, psz_url );
1370                     answer->i_status = 400;
1371                     answer->psz_status = strdup( "Server internal error" );
1372                     answer->i_body = 0;
1373                     answer->p_body = NULL;
1374                     break;
1375                 }
1376
1377                 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1378                 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1379
1380                 answer->i_status = 200;
1381                 answer->psz_status = strdup( "OK" );
1382                 answer->i_body = 0;
1383                 answer->p_body = NULL;
1384
1385                 httpd_MsgAdd( answer, "Transport",
1386                               "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1387             }
1388             else /* TODO  strstr( psz_transport, "interleaved" ) ) */
1389             {
1390                 answer->i_status = 400;
1391                 answer->psz_status = strdup( "Bad Request" );
1392                 answer->i_body = 0;
1393                 answer->p_body = NULL;
1394             }
1395             break;
1396         }
1397
1398         default:
1399             return VLC_EGENERIC;
1400     }
1401     httpd_MsgAdd( answer, "Server", "VLC Server" );
1402     httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1403     httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1404     httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1405
1406     if( psz_session )
1407     {
1408         httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1409     }
1410     return VLC_SUCCESS;
1411 }
1412
1413 /****************************************************************************
1414  * rtp_packetize_*:
1415  ****************************************************************************/
1416 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1417                                   int b_marker, int64_t i_pts )
1418 {
1419     uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1420
1421     out->p_buffer[0] = 0x80;
1422     out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1423     out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1424     out->p_buffer[3] = ( id->i_sequence     )&0xff;
1425     out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1426     out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1427     out->p_buffer[6] = ( i_timestamp >>  8 )&0xff;
1428     out->p_buffer[7] = ( i_timestamp       )&0xff;
1429
1430     out->p_buffer[ 8] = id->ssrc[0];
1431     out->p_buffer[ 9] = id->ssrc[1];
1432     out->p_buffer[10] = id->ssrc[2];
1433     out->p_buffer[11] = id->ssrc[3];
1434
1435     out->i_buffer = 12;
1436     id->i_sequence++;
1437 }
1438
1439 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1440 {
1441     int i;
1442     vlc_mutex_lock( &id->lock_rtsp );
1443     for( i = 0; i < id->i_rtsp_access; i++ )
1444     {
1445         sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1446     }
1447     vlc_mutex_unlock( &id->lock_rtsp );
1448
1449     if( id->p_access )
1450     {
1451         sout_AccessOutWrite( id->p_access, out );
1452     }
1453     else
1454     {
1455         block_Release( out );
1456     }
1457 }
1458
1459 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1460                               block_t *in )
1461 {
1462     int     i_max   = id->i_mtu - 12 - 4; /* payload max in one packet */
1463     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1464
1465     uint8_t *p_data = in->p_buffer;
1466     int     i_data  = in->i_buffer;
1467     int     i;
1468
1469     for( i = 0; i < i_count; i++ )
1470     {
1471         int           i_payload = __MIN( i_max, i_data );
1472         block_t *out = block_New( p_stream, 16 + i_payload );
1473
1474         /* rtp common header */
1475         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1476         /* mbz set to 0 */
1477         out->p_buffer[12] = 0;
1478         out->p_buffer[13] = 0;
1479         /* fragment offset in the current frame */
1480         out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1481         out->p_buffer[15] = ( (i*i_max)      )&0xff;
1482         memcpy( &out->p_buffer[16], p_data, i_payload );
1483
1484         out->i_buffer   = 16 + i_payload;
1485         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1486         out->i_length = in->i_length / i_count;
1487
1488         rtp_packetize_send( id, out );
1489
1490         p_data += i_payload;
1491         i_data -= i_payload;
1492     }
1493
1494     return VLC_SUCCESS;
1495 }
1496
1497 /* rfc2250 */
1498 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1499                               block_t *in )
1500 {
1501     int     i_max   = id->i_mtu - 12 - 4; /* payload max in one packet */
1502     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1503
1504     uint8_t *p_data = in->p_buffer;
1505     int     i_data  = in->i_buffer;
1506     int     i;
1507     int     b_sequence_start = 0;
1508     int     i_temporal_ref = 0;
1509     int     i_picture_coding_type = 0;
1510     int     i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1511     int     b_start_slice = 0;
1512
1513     /* preparse this packet to get some info */
1514     if( in->i_buffer > 4 )
1515     {
1516         uint8_t *p = p_data;
1517         int      i_rest = in->i_buffer;
1518
1519         for( ;; )
1520         {
1521             while( i_rest > 4 &&
1522                    ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1523             {
1524                 p++;
1525                 i_rest--;
1526             }
1527             if( i_rest <= 4 )
1528             {
1529                 break;
1530             }
1531             p += 3;
1532             i_rest -= 4;
1533
1534             if( *p == 0xb3 )
1535             {
1536                 /* sequence start code */
1537                 b_sequence_start = 1;
1538             }
1539             else if( *p == 0x00 && i_rest >= 4 )
1540             {
1541                 /* picture */
1542                 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1543                 i_picture_coding_type = (p[2] >> 3)&0x07;
1544
1545                 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1546                                     i_picture_coding_type == 3 ) )
1547                 {
1548                     i_ffv = (p[3] >> 2)&0x01;
1549                     i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1550                     if( i_rest > 4 && i_picture_coding_type == 3 )
1551                     {
1552                         i_fbv = (p[4]>>6)&0x01;
1553                         i_bfc = (p[4]>>3)&0x07;
1554                     }
1555                 }
1556             }
1557             else if( *p <= 0xaf )
1558             {
1559                 b_start_slice = 1;
1560             }
1561         }
1562     }
1563
1564     for( i = 0; i < i_count; i++ )
1565     {
1566         int           i_payload = __MIN( i_max, i_data );
1567         block_t *out = block_New( p_stream,
1568                                              16 + i_payload );
1569         uint32_t      h = ( i_temporal_ref << 16 )|
1570                           ( b_sequence_start << 13 )|
1571                           ( b_start_slice << 12 )|
1572                           ( i == i_count - 1 ? 1 << 11 : 0 )|
1573                           ( i_picture_coding_type << 8 )|
1574                           ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1575
1576         /* rtp common header */
1577         rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1578                               in->i_pts > 0 ? in->i_pts : in->i_dts );
1579
1580         /* 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 */
1581         out->p_buffer[12] = ( h >> 24 )&0xff;
1582         out->p_buffer[13] = ( h >> 16 )&0xff;
1583         out->p_buffer[14] = ( h >>  8 )&0xff;
1584         out->p_buffer[15] = ( h       )&0xff;
1585
1586         memcpy( &out->p_buffer[16], p_data, i_payload );
1587
1588         out->i_buffer   = 16 + i_payload;
1589         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1590         out->i_length = in->i_length / i_count;
1591
1592         rtp_packetize_send( id, out );
1593
1594         p_data += i_payload;
1595         i_data -= i_payload;
1596     }
1597
1598     return VLC_SUCCESS;
1599 }
1600 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1601                               block_t *in )
1602 {
1603     int     i_max   = id->i_mtu - 12 - 2; /* payload max in one packet */
1604     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1605
1606     uint8_t *p_data = in->p_buffer;
1607     int     i_data  = in->i_buffer;
1608     int     i;
1609
1610     for( i = 0; i < i_count; i++ )
1611     {
1612         int           i_payload = __MIN( i_max, i_data );
1613         block_t *out = block_New( p_stream, 14 + i_payload );
1614
1615         /* rtp common header */
1616         rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1617         /* unit count */
1618         out->p_buffer[12] = 1;
1619         /* unit header */
1620         out->p_buffer[13] = 0x00;
1621         /* data */
1622         memcpy( &out->p_buffer[14], p_data, i_payload );
1623
1624         out->i_buffer   = 14 + i_payload;
1625         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1626         out->i_length = in->i_length / i_count;
1627
1628         rtp_packetize_send( id, out );
1629
1630         p_data += i_payload;
1631         i_data -= i_payload;
1632     }
1633
1634     return VLC_SUCCESS;
1635 }
1636
1637 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1638                                 block_t *in )
1639 {
1640     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1641     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1642
1643     uint8_t *p_data = in->p_buffer;
1644     int     i_data  = in->i_buffer;
1645     int     i;
1646
1647     for( i = 0; i < i_count; i++ )
1648     {
1649         int           i_payload = __MIN( i_max, i_data );
1650         block_t *out = block_New( p_stream, 12 + i_payload );
1651
1652         /* rtp common header */
1653         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1654                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1655         memcpy( &out->p_buffer[12], p_data, i_payload );
1656
1657         out->i_buffer   = 12 + i_payload;
1658         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1659         out->i_length = in->i_length / i_count;
1660
1661         rtp_packetize_send( id, out );
1662
1663         p_data += i_payload;
1664         i_data -= i_payload;
1665     }
1666
1667     return VLC_SUCCESS;
1668 }
1669
1670 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1671                               block_t *in )
1672 {
1673     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1674     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1675
1676     uint8_t *p_data = in->p_buffer;
1677     int     i_data  = in->i_buffer;
1678     int     i_packet = 0;
1679
1680     while( i_data > 0 )
1681     {
1682         int           i_payload = (__MIN( i_max, i_data )/4)*4;
1683         block_t *out = block_New( p_stream, 12 + i_payload );
1684
1685         /* rtp common header */
1686         rtp_packetize_common( id, out, 0,
1687                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1688         memcpy( &out->p_buffer[12], p_data, i_payload );
1689
1690         out->i_buffer   = 12 + i_payload;
1691         out->i_dts    = in->i_dts + i_packet * in->i_length / i_count;
1692         out->i_length = in->i_length / i_count;
1693
1694         rtp_packetize_send( id, out );
1695
1696         p_data += i_payload;
1697         i_data -= i_payload;
1698         i_packet++;
1699     }
1700
1701     return VLC_SUCCESS;
1702 }
1703
1704 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1705                              block_t *in )
1706 {
1707     int     i_max   = id->i_mtu - 12; /* payload max in one packet */
1708     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1709
1710     uint8_t *p_data = in->p_buffer;
1711     int     i_data  = in->i_buffer;
1712     int     i_packet = 0;
1713
1714     while( i_data > 0 )
1715     {
1716         int           i_payload = (__MIN( i_max, i_data )/2)*2;
1717         block_t *out = block_New( p_stream, 12 + i_payload );
1718
1719         /* rtp common header */
1720         rtp_packetize_common( id, out, 0,
1721                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1722         memcpy( &out->p_buffer[12], p_data, i_payload );
1723
1724         out->i_buffer   = 12 + i_payload;
1725         out->i_dts    = in->i_dts + i_packet * in->i_length / i_count;
1726         out->i_length = in->i_length / i_count;
1727
1728         rtp_packetize_send( id, out );
1729
1730         p_data += i_payload;
1731         i_data -= i_payload;
1732         i_packet++;
1733     }
1734
1735     return VLC_SUCCESS;
1736 }
1737 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1738                                block_t *in )
1739 {
1740     int     i_max   = id->i_mtu - 16; /* payload max in one packet */
1741     int     i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1742
1743     uint8_t *p_data = in->p_buffer;
1744     int     i_data  = in->i_buffer;
1745     int     i;
1746
1747     for( i = 0; i < i_count; i++ )
1748     {
1749         int           i_payload = __MIN( i_max, i_data );
1750         block_t *out = block_New( p_stream, 16 + i_payload );
1751
1752         /* rtp common header */
1753         rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1754                               (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1755         /* AU headers */
1756         /* AU headers length (bits) */
1757         out->p_buffer[12] = 0;
1758         out->p_buffer[13] = 2*8;
1759         /* for each AU length 13 bits + idx 3bits, */
1760         out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1761         out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1762
1763         memcpy( &out->p_buffer[16], p_data, i_payload );
1764
1765         out->i_buffer   = 16 + i_payload;
1766         out->i_dts    = in->i_dts + i * in->i_length / i_count;
1767         out->i_length = in->i_length / i_count;
1768
1769         rtp_packetize_send( id, out );
1770
1771         p_data += i_payload;
1772         i_data -= i_payload;
1773     }
1774
1775     return VLC_SUCCESS;
1776 }