rtsp.c 51.2 KB
Newer Older
1
2
3
/*****************************************************************************
 * rtsp.c: rtsp VoD server module
 *****************************************************************************
4
 * Copyright (C) 2003-2006 the VideoLAN team
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * $Id$
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
25
26
27
28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29
30
31
32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
35
36
#include <vlc_input.h>
#include <vlc_sout.h>
37
#include <vlc_block.h>
38

39
40
41
#include <vlc_httpd.h>
#include <vlc_vod.h>
#include <vlc_url.h>
zorglub's avatar
zorglub committed
42
43
#include <vlc_network.h>
#include <vlc_charset.h>
Christophe Mutricy's avatar
Christophe Mutricy committed
44
#include <vlc_strings.h>
45

46
47
48
#ifndef WIN32
# include <locale.h>
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
49

50
51
52
53
#ifdef HAVE_XLOCALE_H
# include <xlocale.h>
#endif

54
55
56
57
58
59
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

60
#define HOST_TEXT N_( "RTSP host address" )
61
#define HOST_LONGTEXT N_( \
62
63
    "This defines the address, port and path the RTSP VOD server will listen " \
    "on.\nSyntax is address:port/path. The default is to listen on all "\
Christophe Mutricy's avatar
Christophe Mutricy committed
64
    "interfaces (address 0.0.0.0), on port 554, with no path.\nTo listen " \
65
    "only on the local interface, use \"localhost\" as address." )
66

Pierre Ynard's avatar
Pierre Ynard committed
67
68
#define THROTTLE_TEXT N_( "Maximum number of connections" )
#define THROTTLE_LONGTEXT N_( "This limits the maximum number of clients " \
69
    "that can connect to the RTSP VOD. 0 means no limit."  )
70

71
72
#define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )

73
74
75
76
77
78
79
#define SESSION_TIMEOUT_TEXT N_( "Sets the timeout option in the RTSP " \
    "session string" )
#define SESSION_TIMEOUT_LONGTEXT N_( "Defines what timeout option to add " \
    "to the RTSP session ID string. Setting it to a negative number removes " \
    "the timeout option entirely. This is needed by some IPTV STBs (such as " \
    "those made by HansunTech) which get confused by it. The default is 5." )

80
81
82
83
84
85
86
87
88
vlc_module_begin ()
    set_shortname( N_("RTSP VoD" ) )
    set_description( N_("RTSP VoD server") )
    set_category( CAT_SOUT )
    set_subcategory( SUBCAT_SOUT_VOD )
    set_capability( "vod server", 1 )
    set_callbacks( Open, Close )
    add_shortcut( "rtsp" )
    add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, true )
89
    add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
90
                RAWMUX_TEXT, true )
Pierre Ynard's avatar
Pierre Ynard committed
91
92
    add_integer( "rtsp-throttle-users", 0, NULL, THROTTLE_TEXT,
                 THROTTLE_LONGTEXT, true )
93
    add_integer( "rtsp-session-timeout", 5, NULL, SESSION_TIMEOUT_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
94
                 SESSION_TIMEOUT_LONGTEXT, true )
95
vlc_module_end ()
96
97
98
99
100

/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/

101
102
103
104
105
106
107
108
109
typedef struct media_es_t media_es_t;

typedef struct
{
    media_es_t *p_media_es;
    int i_port;

} rtsp_client_es_t;

110
111
112
113
typedef struct
{
    char *psz_session;

114
115
    bool b_playing; /* is it in "play" state */
    bool b_paused; /* is it in "pause" state */
116
117
118

    int i_es;
    rtsp_client_es_t **es;
119
120
121

} rtsp_client_t;

122
struct media_es_t
123
124
125
126
127
{
    /* VoD server */
    vod_t *p_vod;

    /* RTSP server */
128
    httpd_url_t *p_rtsp_url;
129
130
131

    vod_media_t *p_media;

132
133
    es_format_t fmt;
    uint8_t     i_payload_type;
134
135
136
    const char  *psz_ptname;
    unsigned    i_clock_rate;
    unsigned    i_channels;
137
138
    char        *psz_fmtp;

139
};
140
141
142

struct vod_media_t
{
143
144
    int id;

145
146
147
148
149
    /* VoD server */
    vod_t *p_vod;

    /* RTSP server */
    httpd_url_t  *p_rtsp_url;
150
151
    char         *psz_rtsp_control_v4;
    char         *psz_rtsp_control_v6;
152
153
    char         *psz_rtsp_path;

154
155
156
    int  i_payload_type;

    vlc_mutex_t lock;
157
158
159
160

    /* ES list */
    int        i_es;
    media_es_t **es;
Pierre Ynard's avatar
Pierre Ynard committed
161
    const char *psz_mux;
162
    bool  b_raw;
163
164
165
166

    /* RTSP client */
    int           i_rtsp;
    rtsp_client_t **rtsp;
167
168

    /* Infos */
169
    mtime_t i_length;
170
171
172
173
174
175
176
177
};

struct vod_sys_t
{
    /* RTSP server */
    httpd_host_t *p_rtsp_host;
    char *psz_path;
    int i_port;
178
    int i_throttle_users;
179
    int i_connections;
180

181
182
    char *psz_raw_mux;

183
184
    int i_session_timeout;

185
    /* List of media */
186
187
    vlc_mutex_t lock_media;
    int i_media_id;
188
189
    int i_media;
    vod_media_t **media;
190
191
192

    /* */
    block_fifo_t *p_fifo_cmd;
193
194
};

195
196
197
198
199
200
201
202
203
204
205
/* rtsp delayed command (to avoid deadlock between vlm/httpd) */
typedef enum
{
    RTSP_CMD_TYPE_NONE,  /* Exit requested */

    RTSP_CMD_TYPE_PLAY,
    RTSP_CMD_TYPE_PAUSE,
    RTSP_CMD_TYPE_STOP,
    RTSP_CMD_TYPE_SEEK,
    RTSP_CMD_TYPE_REWIND,
    RTSP_CMD_TYPE_FORWARD,
sebastien's avatar
sebastien committed
206
207
208

    RTSP_CMD_TYPE_ADD,
    RTSP_CMD_TYPE_DEL,
209
210
} rtsp_cmd_type_t;

sebastien's avatar
sebastien committed
211
212
213
214
215
/* */
typedef struct
{
    int i_type;
    int i_media_id;
sebastien's avatar
sebastien committed
216
    vod_media_t *p_media;
sebastien's avatar
sebastien committed
217
218
219
220
221
    char *psz_session;
    char *psz_arg;
    double f_arg;
} rtsp_cmd_t;

222
static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
223
static void         MediaDel( vod_t *, vod_media_t * );
sebastien's avatar
sebastien committed
224
static void         MediaAskDel ( vod_t *, vod_media_t * );
225
226
227
static int          MediaAddES( vod_t *, vod_media_t *, es_format_t * );
static void         MediaDelES( vod_t *, vod_media_t *, es_format_t * );

228
229
230
static void* CommandThread( vlc_object_t *p_this );
static void  CommandPush( vod_t *, rtsp_cmd_type_t, vod_media_t *, const char *psz_session,
                          double f_arg, const char *psz_arg );
231

232
static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
233
static rtsp_client_t *RtspClientGet( vod_media_t *, const char * );
234
235
236
static void           RtspClientDel( vod_media_t *, rtsp_client_t * );

static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
237
                         httpd_message_t *, const httpd_message_t * );
238
static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
239
                           httpd_message_t *, const httpd_message_t * );
240

241
static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
242

243
244
245
246
247
248
249
250
251
252
253
254
255
static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
{
    static const char hex[16] = "0123456789abcdef";
    int i;

    for( i = 0; i < i_data; i++ )
    {
        s[2*i+0] = hex[(p_data[i]>>4)&0xf];
        s[2*i+1] = hex[(p_data[i]   )&0xf];
    }
    s[2*i_data] = '\0';
}

256
257
258
259
260
261
/*****************************************************************************
 * Open: Starts the RTSP server module
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    vod_t *p_vod = (vod_t *)p_this;
ivoire's avatar
ivoire committed
262
263
    vod_sys_t *p_sys = NULL;
    char *psz_url = NULL;
264
265
    vlc_url_t url;

266
    psz_url = var_InheritString( p_vod, "rtsp-host" );
267
    vlc_UrlParse( &url, psz_url, 0 );
ivoire's avatar
ivoire committed
268
    free( psz_url );
269
270
271
272
273
274
275

    if( url.i_port <= 0 ) url.i_port = 554;

    p_vod->p_sys = p_sys = malloc( sizeof( vod_sys_t ) );
    if( !p_sys ) goto error;
    p_sys->p_rtsp_host = 0;

276
    p_sys->i_session_timeout = var_CreateGetInteger( p_this, "rtsp-session-timeout" );
277

278
    p_sys->i_throttle_users = var_CreateGetInteger( p_this, "rtsp-throttle-users" );
279
    msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
280
281
    p_sys->i_connections = 0;

282
    p_sys->psz_raw_mux = var_CreateGetString( p_this, "rtsp-raw-mux" );
283

284
285
286
287
    p_sys->p_rtsp_host =
        httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
    if( !p_sys->p_rtsp_host )
    {
288
        msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
289
290
291
292
293
294
295
296
                 url.psz_host, url.i_port );
        goto error;
    }

    p_sys->psz_path = strdup( url.psz_path ? url.psz_path : "/" );
    p_sys->i_port = url.i_port;

    vlc_UrlClean( &url );
297

298
    vlc_mutex_init( &p_sys->lock_media );
299
300
301

    TAB_INIT( p_sys->i_media, p_sys->media );
    p_sys->i_media_id = 0;
302
303

    p_vod->pf_media_new = MediaNew;
sebastien's avatar
sebastien committed
304
    p_vod->pf_media_del = MediaAskDel;
305
306
307
    p_vod->pf_media_add_es = MediaAddES;
    p_vod->pf_media_del_es = MediaDelES;

308
    p_sys->p_fifo_cmd = block_FifoNew();
309
    if( vlc_thread_create( p_vod, "rtsp vod thread", CommandThread,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
310
                           VLC_THREAD_PRIORITY_LOW ) )
311
312
313
314
315
316
317
    {
        msg_Err( p_vod, "cannot spawn rtsp vod thread" );
        block_FifoRelease( p_sys->p_fifo_cmd );
        free( p_sys->psz_path );
        goto error;
    }

318
319
    return VLC_SUCCESS;

320
error:
ivoire's avatar
ivoire committed
321
322
323
324
325
326
    if( p_sys )
    {
        if( p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
        free( p_sys->psz_raw_mux );
        free( p_sys );
    }
327
    vlc_UrlClean( &url );
328

329
330
331
332
333
334
335
336
337
338
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    vod_t *p_vod = (vod_t *)p_this;
    vod_sys_t *p_sys = p_vod->p_sys;
sebastien's avatar
sebastien committed
339
340
    block_t *p_block_cmd;
    rtsp_cmd_t cmd;
341

342
    /* Stop command thread */
343
    vlc_object_kill( p_vod );
344
345
346
    CommandPush( p_vod, RTSP_CMD_TYPE_NONE, NULL, NULL, 0.0, NULL );
    vlc_thread_join( p_vod );

sebastien's avatar
sebastien committed
347
348
349
350
351
    while( block_FifoCount( p_sys->p_fifo_cmd ) > 0 )
    {
         p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
         memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
         block_Release( p_block_cmd );
sebastien's avatar
sebastien committed
352
353
         if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
             MediaDel(p_vod, cmd.p_media);
sebastien's avatar
sebastien committed
354
355
356
         free( cmd.psz_session );
         free( cmd.psz_arg );
    }
357
358
    block_FifoRelease( p_sys->p_fifo_cmd );

359
    httpd_HostDelete( p_sys->p_rtsp_host );
360
    var_Destroy( p_this, "rtsp-session-timeout" );
361
    var_Destroy( p_this, "rtsp-throttle-users" );
362
    var_Destroy( p_this, "rtsp-raw-mux" );
363

364
365
366
367
368
369
370
    /* Check VLM is not buggy */
    if( p_sys->i_media > 0 )
        msg_Err( p_vod, "rtsp vod leaking %d medias", p_sys->i_media );
    TAB_CLEAN( p_sys->i_media, p_sys->media );

    vlc_mutex_destroy( &p_sys->lock_media );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
371
    free( p_sys->psz_path );
372
    free( p_sys->psz_raw_mux );
373
374
375
376
377
378
    free( p_sys );
}

/*****************************************************************************
 * Media handling
 *****************************************************************************/
379
static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
380
                              input_item_t *p_item )
381
{
382
    int i;
ivoire's avatar
ivoire committed
383
    vod_sys_t *p_sys = p_vod->p_sys;
384

ivoire's avatar
ivoire committed
385
    vod_media_t *p_media = calloc( 1, sizeof(vod_media_t) );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
386
387
388
    if( !p_media )
        return NULL;

389
390
    p_media->id = p_sys->i_media_id++;
    TAB_INIT( p_media->i_es, p_media->es );
ivoire's avatar
ivoire committed
391
    p_media->psz_mux = NULL;
392
    TAB_INIT( p_media->i_rtsp, p_media->rtsp );
393
    p_media->b_raw = false;
394

395
396
397
    if( asprintf( &p_media->psz_rtsp_path, "%s%s",
                  p_sys->psz_path, psz_name ) <0 )
        return NULL;
398
    p_media->p_rtsp_url =
399
400
        httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
                            NULL, NULL );
401
402
403

    if( !p_media->p_rtsp_url )
    {
404
        msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
405
406
        free( p_media->psz_rtsp_path );
        free( p_media );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
407
        return NULL;
408
409
    }

410
    msg_Dbg( p_vod, "created RTSP url: %s", p_media->psz_rtsp_path );
411

412
    if( asprintf( &p_media->psz_rtsp_control_v4,
413
               "rtsp://%%s:%d%s/trackID=%%d",
414
               p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
ivoire's avatar
ivoire committed
415
416
417
418
    {
        httpd_UrlDelete( p_media->p_rtsp_url );
        free( p_media->psz_rtsp_path );
        free( p_media );
419
        return NULL;
ivoire's avatar
ivoire committed
420
    }
421
    if( asprintf( &p_media->psz_rtsp_control_v6,
422
               "rtsp://[%%s]:%d%s/trackID=%%d",
423
              p_sys->i_port, p_media->psz_rtsp_path ) < 0 )
ivoire's avatar
ivoire committed
424
425
426
427
    {
        httpd_UrlDelete( p_media->p_rtsp_url );
        free( p_media->psz_rtsp_path );
        free( p_media );
428
        return NULL;
ivoire's avatar
ivoire committed
429
    }
430

431
432
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
                    RtspCallback, (void*)p_media );
433
434
435
436
437
438
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_DESCRIBE,
                    RtspCallback, (void*)p_media );
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PLAY,
                    RtspCallback, (void*)p_media );
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_PAUSE,
                    RtspCallback, (void*)p_media );
439
440
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
                    RtspCallback, (void*)p_media );
441
442
443
444
445
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
                    RtspCallback, (void*)p_media );

    p_media->p_vod = p_vod;

446
    vlc_mutex_init( &p_media->lock );
Pierre Ynard's avatar
Pierre Ynard committed
447

448
449
    p_media->i_payload_type = 96;

450
    p_media->i_length = input_item_GetDuration( p_item );
451

452
453
454
    vlc_mutex_lock( &p_item->lock );
    msg_Dbg( p_vod, "media has %i declared ES", p_item->i_es );
    for( i = 0; i < p_item->i_es; i++ )
455
456
457
    {
        MediaAddES( p_vod, p_media, p_item->es[i] );
    }
458
459
    vlc_mutex_unlock( &p_item->lock );

sebastien's avatar
sebastien committed
460
    CommandPush( p_vod, RTSP_CMD_TYPE_ADD, p_media, NULL, 0.0, NULL );
461
462
463
    return p_media;
}

sebastien's avatar
sebastien committed
464
465
466
467
468
static void MediaAskDel ( vod_t *p_vod, vod_media_t *p_media )
{
    CommandPush( p_vod, RTSP_CMD_TYPE_DEL, p_media, NULL, 0.0, NULL );
}

469
470
471
472
static void MediaDel( vod_t *p_vod, vod_media_t *p_media )
{
    vod_sys_t *p_sys = p_vod->p_sys;

473
474
    msg_Dbg( p_vod, "deleting media: %s", p_media->psz_rtsp_path );

475
476
477
478
    vlc_mutex_lock( &p_sys->lock_media );
    TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
    vlc_mutex_unlock( &p_sys->lock_media );

479
480
    httpd_UrlDelete( p_media->p_rtsp_url );

481
482
483
    while( p_media->i_rtsp > 0 )
        RtspClientDel( p_media, p_media->rtsp[0] );
    TAB_CLEAN( p_media->i_rtsp, p_media->rtsp );
Laurent Aimar's avatar
Laurent Aimar committed
484

ivoire's avatar
ivoire committed
485
486
487
    free( p_media->psz_rtsp_path );
    free( p_media->psz_rtsp_control_v6 );
    free( p_media->psz_rtsp_control_v4 );
488

489
490
491
    while( p_media->i_es )
        MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );
    TAB_CLEAN( p_media->i_es, p_media->es );
492
493

    vlc_mutex_destroy( &p_media->lock );
494

495
496
497
498
499
    free( p_media );
}

static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
{
500
    char *psz_urlc;
ivoire's avatar
ivoire committed
501
502
503
    media_es_t *p_es = calloc( 1, sizeof(media_es_t) );
    if( !p_es )
        return VLC_ENOMEM;
504

505
    p_media->psz_mux = NULL;
506
507

    /* TODO: update SDP, etc... */
508
509
    if( asprintf( &psz_urlc, "%s/trackID=%d",
              p_media->psz_rtsp_path, p_media->i_es ) < 0 )
ivoire's avatar
ivoire committed
510
511
    {
        free( p_es );
512
        return VLC_ENOMEM;
ivoire's avatar
ivoire committed
513
    }
514
    msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
515

516
517
518
    p_es->i_clock_rate = 90000;
    p_es->i_channels = 1;

519
    switch( p_fmt->i_codec )
520
    {
521
        case VLC_CODEC_S16B:
522
523
524
525
            if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
            {
                p_es->i_payload_type = 11;
            }
526
            else if( p_fmt->audio.i_channels == 2 &&
527
                     p_fmt->audio.i_rate == 44100 )
528
529
530
531
532
533
534
            {
                p_es->i_payload_type = 10;
            }
            else
            {
                p_es->i_payload_type = p_media->i_payload_type++;
            }
535
536
537
            p_es->psz_ptname = "L16";
            p_es->i_clock_rate = p_fmt->audio.i_rate;
            p_es->i_channels = p_fmt->audio.i_channels;
538
            break;
539
        case VLC_CODEC_U8:
540
            p_es->i_payload_type = p_media->i_payload_type++;
541
542
543
            p_es->psz_ptname = "L8";
            p_es->i_clock_rate = p_fmt->audio.i_rate;
            p_es->i_channels = p_fmt->audio.i_channels;
544
            break;
545
        case VLC_CODEC_MPGA:
546
            p_es->i_payload_type = 14;
547
            p_es->psz_ptname = "MPA";
548
            break;
549
        case VLC_CODEC_MPGV:
550
            p_es->i_payload_type = 32;
551
            p_es->psz_ptname = "MPV";
552
            break;
553
        case VLC_CODEC_A52:
554
            p_es->i_payload_type = p_media->i_payload_type++;
555
556
            p_es->psz_ptname = "ac3";
            p_es->i_clock_rate = p_fmt->audio.i_rate;
557
            break;
558
        case VLC_CODEC_H263:
559
            p_es->i_payload_type = p_media->i_payload_type++;
560
            p_es->psz_ptname = "H263-1998";
561
            break;
562
        case VLC_CODEC_H264:
563
            p_es->i_payload_type = p_media->i_payload_type++;
564
            p_es->psz_ptname = "H264";
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
            p_es->psz_fmtp = NULL;
            /* FIXME AAAAAAAAAAAARRRRRRRRGGGG copied from stream_out/rtp.c */
            if( p_fmt->i_extra > 0 )
            {
                uint8_t *p_buffer = p_fmt->p_extra;
                int     i_buffer = p_fmt->i_extra;
                char    *p_64_sps = NULL;
                char    *p_64_pps = NULL;
                char    hexa[6+1];

                while( i_buffer > 4 &&
                       p_buffer[0] == 0 && p_buffer[1] == 0 &&
                       p_buffer[2] == 0 && p_buffer[3] == 1 )
                {
                    const int i_nal_type = p_buffer[4]&0x1f;
                    int i_offset;
                    int i_size      = 0;

                    i_size = i_buffer;
                    for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
                    {
                        if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
                        {
                            /* we found another startcode */
                            i_size = i_offset;
                            break;
                        }
                    }
                    if( i_nal_type == 7 )
                    {
ivoire's avatar
ivoire committed
595
                        free( p_64_sps );
596
597
598
599
600
                        p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
                        sprintf_hexa( hexa, &p_buffer[5], 3 );
                    }
                    else if( i_nal_type == 8 )
                    {
ivoire's avatar
ivoire committed
601
                        free( p_64_pps );
602
603
604
605
606
607
608
                        p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
                    }
                    i_buffer -= i_size;
                    p_buffer += i_size;
                }
                /* */
                if( p_64_sps && p_64_pps )
ivoire's avatar
ivoire committed
609
                {
610
                    if( asprintf( &p_es->psz_fmtp,
611
612
613
                                  "packetization-mode=1;profile-level-id=%s;"
                                  "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
                                  p_64_pps ) < 0 )
ivoire's avatar
ivoire committed
614
615
616
617
618
                    {
                        free( p_64_sps );
                        free( p_64_pps );
                        free( psz_urlc );
                        free( p_es );
619
                        return VLC_ENOMEM;
ivoire's avatar
ivoire committed
620
621
                    }
                }
ivoire's avatar
ivoire committed
622
623
                free( p_64_sps );
                free( p_64_pps );
624
625
626
            }
            if( !p_es->psz_fmtp )
                p_es->psz_fmtp = strdup( "packetization-mode=1" );
627
            break;
628
        case VLC_CODEC_MP4V:
629
            p_es->i_payload_type = p_media->i_payload_type++;
630
            p_es->psz_ptname = "MP4V-ES";
631
632
633
634
            if( p_fmt->i_extra > 0 )
            {
                char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
                sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
ivoire's avatar
ivoire committed
635
636
637
                if( asprintf( &p_es->psz_fmtp,
                              "profile-level-id=3; config=%s;", p_hexa ) == -1 )
                    p_es->psz_fmtp = NULL;
638
639
640
                free( p_hexa );
            }
            break;
641
        case VLC_CODEC_MP4A:
642
            p_es->i_payload_type = p_media->i_payload_type++;
643
644
            p_es->psz_ptname = "mpeg4-generic";
            p_es->i_clock_rate = p_fmt->audio.i_rate;
645
646
647
648
            if( p_fmt->i_extra > 0 )
            {
                char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
                sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
ivoire's avatar
ivoire committed
649
650
651
652
653
                if( asprintf( &p_es->psz_fmtp,
                              "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
                              "config=%s; SizeLength=13;IndexLength=3; "
                              "IndexDeltaLength=3; Profile=1;", p_hexa ) == -1 )
                    p_es->psz_fmtp = NULL;
654
655
656
657
                free( p_hexa );
            }
            break;
        case VLC_FOURCC( 'm', 'p', '2', 't' ):
Pierre Ynard's avatar
Pierre Ynard committed
658
            p_media->psz_mux = "ts";
659
            p_es->i_payload_type = 33;
660
            p_es->psz_ptname = "MP2T";
661
662
            break;
        case VLC_FOURCC( 'm', 'p', '2', 'p' ):
Pierre Ynard's avatar
Pierre Ynard committed
663
            p_media->psz_mux = "ps";
664
            p_es->i_payload_type = p_media->i_payload_type++;
665
            p_es->psz_ptname = "MP2P";
666
            break;
667
        case VLC_CODEC_AMR_NB:
668
            p_es->i_payload_type = p_media->i_payload_type++;
669
670
671
672
            p_es->psz_ptname = "AMR";
            p_es->i_clock_rate = 8000;
            if(p_fmt->audio.i_channels == 2 )
                p_es->i_channels = 2;
673
674
            p_es->psz_fmtp = strdup( "octet-align=1" );
            break;
675
        case VLC_CODEC_AMR_WB:
676
            p_es->i_payload_type = p_media->i_payload_type++;
677
678
679
680
            p_es->psz_ptname = "AMR-WB";
            p_es->i_clock_rate = 16000;
            if(p_fmt->audio.i_channels == 2 )
                p_es->i_channels = 2;
681
682
            p_es->psz_fmtp = strdup( "octet-align=1" );
            break;
683

684
685
686
        default:
            msg_Err( p_vod, "cannot add this stream (unsupported "
                    "codec: %4.4s)", (char*)&p_fmt->i_codec );
ivoire's avatar
ivoire committed
687
            free( psz_urlc );
688
689
            free( p_es );
            return VLC_EGENERIC;
690
691
692
    }

    p_es->p_rtsp_url =
693
694
        httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
                            NULL );
695
696
697

    if( !p_es->p_rtsp_url )
    {
698
        msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
699
700
701
        free( psz_urlc );
        free( p_es );
        return VLC_EGENERIC;
702
    }
703
704
705
706
    free( psz_urlc );

    httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
                    RtspCallbackES, (void*)p_es );
707
708
709
710
711
712
    httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_TEARDOWN,
                    RtspCallbackES, (void*)p_es );
    httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PLAY,
                    RtspCallbackES, (void*)p_es );
    httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_PAUSE,
                    RtspCallbackES, (void*)p_es );
713

714
    es_format_Copy( &p_es->fmt, p_fmt );
715
716
717
    p_es->p_vod = p_vod;
    p_es->p_media = p_media;

718
719
720
721
    vlc_mutex_lock( &p_media->lock );
    TAB_APPEND( p_media->i_es, p_media->es, p_es );
    vlc_mutex_unlock( &p_media->lock );

722
723
724
725
726
    return VLC_SUCCESS;
}

static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
{
727
    media_es_t *p_es = NULL;
728
729
730
731
732
733
734
735
736
737
738
739
740
    int i;

    /* Find the ES */
    for( i = 0; i < p_media->i_es; i++ )
    {
        if( p_media->es[i]->fmt.i_cat == p_fmt->i_cat &&
            p_media->es[i]->fmt.i_codec == p_fmt->i_codec &&
            p_media->es[i]->fmt.i_id == p_fmt->i_id )
        {
            p_es = p_media->es[i];
        }
    }
    if( !p_es ) return;
741

742
743
744
    msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );

    vlc_mutex_lock( &p_media->lock );
745
    TAB_REMOVE( p_media->i_es, p_media->es, p_es );
746
    vlc_mutex_unlock( &p_media->lock );
747

ivoire's avatar
ivoire committed
748
    free( p_es->psz_fmtp );
749
750

    if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
751
    es_format_Clean( &p_es->fmt );
Laurent Aimar's avatar
Laurent Aimar committed
752
    free( p_es );
753
754
}

755
756
757
758
759
760
761
762
static void CommandPush( vod_t *p_vod, rtsp_cmd_type_t i_type, vod_media_t *p_media, const char *psz_session,
                         double f_arg, const char *psz_arg )
{
    rtsp_cmd_t cmd;
    block_t *p_cmd;

    memset( &cmd, 0, sizeof(cmd) );
    cmd.i_type = i_type;
sebastien's avatar
sebastien committed
763
    cmd.p_media = p_media;
764
765
766
767
768
769
770
771
772
773
774
775
776
777
    if( p_media )
        cmd.i_media_id = p_media->id;
    if( psz_session )
        cmd.psz_session = strdup(psz_session);
    cmd.f_arg = f_arg;
    if( psz_arg )
        cmd.psz_arg = strdup(psz_arg);

    p_cmd = block_New( p_vod, sizeof(rtsp_cmd_t) );
    memcpy( p_cmd->p_buffer, &cmd, sizeof(cmd) );

    block_FifoPut( p_vod->p_sys->p_fifo_cmd, p_cmd );
}

778
static void* CommandThread( vlc_object_t *p_this )
779
780
781
{
    vod_t *p_vod = (vod_t*)p_this;
    vod_sys_t *p_sys = p_vod->p_sys;
782
    int canc = vlc_savecancel ();
783

784
    while( vlc_object_alive (p_vod) )
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
    {
        block_t *p_block_cmd = block_FifoGet( p_sys->p_fifo_cmd );
        rtsp_cmd_t cmd;
        vod_media_t *p_media = NULL;
        int i;

        if( !p_block_cmd )
            break;

        memcpy( &cmd, p_block_cmd->p_buffer, sizeof(cmd) );
        block_Release( p_block_cmd );

        if( cmd.i_type == RTSP_CMD_TYPE_NONE )
            break;

sebastien's avatar
sebastien committed
800
801
802
803
804
805
806
807
808
809
810
811
812
813
        if ( cmd.i_type == RTSP_CMD_TYPE_ADD )
        {
            vlc_mutex_lock( &p_sys->lock_media );
            TAB_APPEND( p_sys->i_media, p_sys->media, cmd.p_media );
            vlc_mutex_unlock( &p_sys->lock_media );
            goto next;
        }

        if ( cmd.i_type == RTSP_CMD_TYPE_DEL )
        {
            MediaDel(p_vod, cmd.p_media);
            goto next;
        }

814
815
816
817
818
819
820
821
        /* */
        vlc_mutex_lock( &p_sys->lock_media );
        for( i = 0; i < p_sys->i_media; i++ )
        {
            if( p_sys->media[i]->id == cmd.i_media_id )
                break;
        }
        if( i >= p_sys->i_media )
sebastien's avatar
sebastien committed
822
823
        {
            vlc_mutex_unlock( &p_sys->lock_media );
824
            goto next;
sebastien's avatar
sebastien committed
825
        }
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
        p_media = p_sys->media[i];

        switch( cmd.i_type )
        {
        case RTSP_CMD_TYPE_PLAY:
            vod_MediaControl( p_vod, p_media, cmd.psz_session,
                              VOD_MEDIA_PLAY, cmd.psz_arg );
            break;
        case RTSP_CMD_TYPE_PAUSE:
            vod_MediaControl( p_vod, p_media, cmd.psz_session,
                              VOD_MEDIA_PAUSE );
            break;

        case RTSP_CMD_TYPE_STOP:
            vod_MediaControl( p_vod, p_media, cmd.psz_session, VOD_MEDIA_STOP );
            break;

        case RTSP_CMD_TYPE_SEEK:
            vod_MediaControl( p_vod, p_media, cmd.psz_session,
                              VOD_MEDIA_SEEK, cmd.f_arg );
            break;

        case RTSP_CMD_TYPE_REWIND:
            vod_MediaControl( p_vod, p_media, cmd.psz_session,
                              VOD_MEDIA_REWIND, cmd.f_arg );
            break;

        case RTSP_CMD_TYPE_FORWARD:
            vod_MediaControl( p_vod, p_media, cmd.psz_session,
                              VOD_MEDIA_FORWARD, cmd.f_arg );
            break;

        default:
            break;
        }
sebastien's avatar
sebastien committed
861
        vlc_mutex_unlock( &p_sys->lock_media );
862
863

    next:
ivoire's avatar
ivoire committed
864
865
        free( cmd.psz_session );
        free( cmd.psz_arg );
866
    }
867
868

    vlc_restorecancel (canc);
869
    return NULL;
870
871
}

872
873
874
875
876
/****************************************************************************
 * RTSP server implementation
 ****************************************************************************/
static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
{
ivoire's avatar
ivoire committed
877
    rtsp_client_t *p_rtsp = calloc( 1, sizeof(rtsp_client_t) );
878

ivoire's avatar
ivoire committed
879
880
    if( !p_rtsp )
        return NULL;
881
    p_rtsp->es = 0;
882

883
884
    p_rtsp->psz_session = psz_session;
    TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
885

886
887
    p_media->p_vod->p_sys->i_connections++;
    msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
888
             psz_session, p_media->p_vod->p_sys->i_throttle_users );
889

890
    return p_rtsp;
891
892
}

893
static rtsp_client_t *RtspClientGet( vod_media_t *p_media, const char *psz_session )
894
895
896
{
    int i;

897
    for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
898
899
900
901
902
903
904
905
    {
        if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
            return p_media->rtsp[i];
    }

    return NULL;
}

906
static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
907
{
908
909
    p_media->p_vod->p_sys->i_connections--;
    msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
910
             p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
911

912
    while( p_rtsp->i_es )
913
    {
914
        p_rtsp->i_es--;
915
        free( p_rtsp->es[p_rtsp->i_es] );
916
    }
917
    free( p_rtsp->es );
918

919
    TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
920

921
922
    free( p_rtsp->psz_session );
    free( p_rtsp );
923
924
}

925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947

static float ParseNPT (const char *str)
{
     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
     locale_t oldloc = uselocale (loc);
     unsigned hour, min;
     float sec;

     if (sscanf (str, "%u:%u:%f", &hour, &min, &sec) == 3)
         sec += ((hour * 60) + min) * 60;
     else
     if (sscanf (str, "%f", &sec) != 1)
         sec = 0.;

     if (loc != (locale_t)0)
     {
         uselocale (oldloc);
         freelocale (loc);
     }
     return sec;
}


948
static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
949
                         httpd_message_t *answer, const httpd_message_t *query )
950
951
952
{
    vod_media_t *p_media = (vod_media_t*)p_args;
    vod_t *p_vod = p_media->p_vod;
953
954
955
956
    const char *psz_transport = NULL;
    const char *psz_playnow = NULL; /* support option: x-playNow */
    const char *psz_session = NULL;
    const char *psz_cseq = NULL;
957
    rtsp_client_t *p_rtsp;
958
    int i_port = 0;
959
    int i_cseq = 0;
960
961
962

    if( answer == NULL || query == NULL ) return VLC_SUCCESS;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
963
    msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
964
965
966
967

    answer->i_proto   = HTTPD_PROTO_RTSP;
    answer->i_version = query->i_version;
    answer->i_type    = HTTPD_MSG_ANSWER;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
968
    answer->i_body    = 0;
969
    answer->p_body    = NULL;
970
971
972

    switch( query->i_type )
    {
973
974
        case HTTPD_MSG_SETUP:
        {
975
            psz_playnow = httpd_MsgGet( query, "x-playNow" );
976
            psz_transport = httpd_MsgGet( query, "Transport" );
977
978
979
980
981
            if( psz_transport == NULL )
            {
                answer->i_status = 400;
                break;
            }
982
983
984
985
986
            msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );

            if( strstr( psz_transport, "unicast" ) &&
                strstr( psz_transport, "client_port=" ) )
            {
987
                rtsp_client_t *p_rtsp = NULL;
988
                char ip[NI_MAXNUMERICHOST];
989
                i_port = atoi( strstr( psz_transport, "client_port=" ) +
990
991
                                strlen("client_port=") );

992
993
994
                if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
                    strstr( psz_transport, "RAW/RAW/UDP" ) )
                {
Pierre Ynard's avatar
Pierre Ynard committed
995
                    p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
996
                    p_media->b_raw = true;
997
                }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
998

999
1000
1001
1002
1003
1004
1005
1006
1007
                if( httpd_ClientIP( cl, ip ) == NULL )
                {
                    answer->i_status = 500;
                    answer->i_body = 0;
                    answer->p_body = NULL;
                    break;
                }

                msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
Jean-Paul Saman's avatar
Jean-Paul Saman committed
1008
                         ip, i_port );
1009
1010
1011
1012

                psz_session = httpd_MsgGet( query, "Session" );
                if( !psz_session || !*psz_session )
                {
1013
                    char *psz_new;
1014
1015
                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1016
                    {
1017
                        answer->i_status = 503;
1018
1019
1020
1021
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
1022
1023
                    if( asprintf( &psz_new, "%d", rand() ) < 0 )
                        return VLC_ENOMEM;
1024
1025
1026
                    psz_session = psz_new;

                    p_rtsp = RtspClientNew( p_media, psz_new );
1027
1028
1029
1030
1031
1032
1033
                    if( !p_rtsp )
                    {
                        answer->i_status = 454;
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
                }
                else
                {
                    p_rtsp = RtspClientGet( p_media, psz_session );
                    if( !p_rtsp )
                    {
                        answer->i_status = 454;
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
                }

                answer->i_status = 200;
                answer->i_body = 0;
                answer->p_body = NULL;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
1051
1052
1053
1054
                if( p_media->b_raw )
                {
                    if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
                    {
Christophe Mutricy's avatar