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