rtsp.c 44.9 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>

#include <errno.h>

#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc/sout.h>

#include "vlc_httpd.h"
#include "vlc_vod.h"
38
#include "vlc_url.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
39
#include "network.h"
40
#include "charset.h"
41 42 43 44 45 46 47

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

48
#define HOST_TEXT N_( "RTSP host address" )
Clément Stenac's avatar
Clément Stenac committed
49
/// \bug [String] extra space
50
#define HOST_LONGTEXT N_( \
51 52 53 54
    "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 "\
    "interfaces (address 0.0.0.0), on port 554, with no path.\n To listen " \
    "only on the local interface, use \"localhost\" as address." )
55

56
#define THROTLE_TEXT N_( "Maximum number of connections" )
57 58
#define THROTLE_LONGTEXT N_( "This limits the maximum number of clients " \
    "that can connect to the RTSP VOD. 0 means no limit."  )
59

60 61
#define RAWMUX_TEXT N_( "MUX for RAW RTSP transport" )

62
vlc_module_begin();
Clément Stenac's avatar
 
Clément Stenac committed
63
    set_shortname( _("RTSP VoD" ) );
64
    set_description( _("RTSP VoD server") );
65 66
    set_category( CAT_SOUT );
    set_subcategory( SUBCAT_SOUT_VOD );
67 68 69 70
    set_capability( "vod server", 1 );
    set_callbacks( Open, Close );
    add_shortcut( "rtsp" );
    add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
71
    add_string( "rtsp-raw-mux", "ts", NULL, RAWMUX_TEXT, RAWMUX_TEXT, VLC_TRUE );
72 73
    add_integer( "rtsp-throttle-users", 0, NULL, THROTLE_TEXT,
                                           THROTLE_LONGTEXT, VLC_TRUE );
74 75 76 77 78 79
vlc_module_end();

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

80 81 82 83 84 85 86 87 88 89
typedef struct media_es_t media_es_t;

typedef struct
{
    media_es_t *p_media_es;
    char *psz_ip;
    int i_port;

} rtsp_client_es_t;

90 91 92 93 94 95
typedef struct
{
    char *psz_session;
    int64_t i_last; /* for timeout */

    vlc_bool_t b_playing; /* is it in "play" state */
96 97 98 99
    vlc_bool_t b_paused; /* is it in "pause" state */

    int i_es;
    rtsp_client_es_t **es;
100 101 102

} rtsp_client_t;

103
struct media_es_t
104 105 106 107 108
{
    /* VoD server */
    vod_t *p_vod;

    /* RTSP server */
109
    httpd_url_t *p_rtsp_url;
110 111 112

    vod_media_t *p_media;

113 114 115 116 117 118
    es_format_t fmt;
    int         i_port;
    uint8_t     i_payload_type;
    char        *psz_rtpmap;
    char        *psz_fmtp;

119
};
120 121 122 123 124 125 126 127

struct vod_media_t
{
    /* VoD server */
    vod_t *p_vod;

    /* RTSP server */
    httpd_url_t  *p_rtsp_url;
128 129
    char         *psz_rtsp_control_v4;
    char         *psz_rtsp_control_v6;
130 131 132 133 134 135
    char         *psz_rtsp_path;

    int  i_port;
    int  i_port_audio;
    int  i_port_video;
    int  i_ttl;
136 137 138 139 140
    int  i_payload_type;

    int64_t i_sdp_id;
    int     i_sdp_version;

141 142
    vlc_bool_t b_multicast;

143
    vlc_mutex_t lock;
144 145 146 147

    /* ES list */
    int        i_es;
    media_es_t **es;
148
    char       *psz_mux;
149
    vlc_bool_t  b_raw;
150 151 152 153

    /* RTSP client */
    int           i_rtsp;
    rtsp_client_t **rtsp;
154 155 156 157 158 159

    /* Infos */
    char *psz_session_name;
    char *psz_session_description;
    char *psz_session_url;
    char *psz_session_email;
160
    mtime_t i_length;
161 162 163 164 165 166 167 168
};

struct vod_sys_t
{
    /* RTSP server */
    httpd_host_t *p_rtsp_host;
    char *psz_path;
    int i_port;
169
    int i_throttle_users;
170
    int i_connections;
171

172 173
    char *psz_raw_mux;

174 175 176 177 178
    /* List of media */
    int i_media;
    vod_media_t **media;
};

179
static vod_media_t *MediaNew( vod_t *, const char *, input_item_t * );
180 181 182 183 184 185 186 187 188 189
static void         MediaDel( vod_t *, vod_media_t * );
static int          MediaAddES( vod_t *, vod_media_t *, es_format_t * );
static void         MediaDelES( vod_t *, vod_media_t *, es_format_t * );

static rtsp_client_t *RtspClientNew( vod_media_t *, char * );
static rtsp_client_t *RtspClientGet( vod_media_t *, char * );
static void           RtspClientDel( vod_media_t *, rtsp_client_t * );

static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
                         httpd_message_t *, httpd_message_t * );
190
static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
191 192
                           httpd_message_t *, httpd_message_t * );

193
static char *SDPGenerate( const vod_media_t *, httpd_client_t *cl );
194

195 196 197 198 199 200 201 202 203 204 205 206 207
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';
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/*****************************************************************************
 * Open: Starts the RTSP server module
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    vod_t *p_vod = (vod_t *)p_this;
    vod_sys_t *p_sys = 0;
    char *psz_url = 0;
    vlc_url_t url;

    psz_url = config_GetPsz( p_vod, "rtsp-host" );
    vlc_UrlParse( &url, psz_url, 0 );
    if( psz_url ) free( psz_url );

    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;

228 229 230
    var_Create( p_this, "rtsp-throttle-users", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    p_sys->i_throttle_users = var_GetInteger( p_this, "rtsp-throtle-users" );
    msg_Dbg( p_this, "allowing up to %d connections", p_sys->i_throttle_users );
231 232
    p_sys->i_connections = 0;

233 234
    var_Create( p_this, "rtsp-raw-mux", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
    p_sys->psz_raw_mux = var_GetString( p_this, "rtsp-raw-mux" );
235

236 237 238 239
    p_sys->p_rtsp_host =
        httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
    if( !p_sys->p_rtsp_host )
    {
240
        msg_Err( p_vod, "cannot create RTSP server (%s:%i)",
241 242 243 244 245 246 247 248
                 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 );
249
    p_sys->media = NULL;
250 251 252 253 254 255 256 257 258
    p_sys->i_media = 0;

    p_vod->pf_media_new = MediaNew;
    p_vod->pf_media_del = MediaDel;
    p_vod->pf_media_add_es = MediaAddES;
    p_vod->pf_media_del_es = MediaDelES;

    return VLC_SUCCESS;

259
error:
260
    if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
261
    if( p_sys && p_sys->psz_raw_mux ) free( p_sys->psz_raw_mux );
262 263
    if( p_sys ) free( p_sys );
    vlc_UrlClean( &url );
264

265 266 267 268 269 270 271 272 273 274 275 276
    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;

    httpd_HostDelete( p_sys->p_rtsp_host );
277
    var_Destroy( p_this, "rtsp-throttle-users" );
278
    var_Destroy( p_this, "rtsp-raw-mux" );
279 280

    /* TODO delete medias */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
281
    free( p_sys->psz_path );
282
    free( p_sys->psz_raw_mux );
283 284 285 286 287 288
    free( p_sys );
}

/*****************************************************************************
 * Media handling
 *****************************************************************************/
289
static vod_media_t *MediaNew( vod_t *p_vod, const char *psz_name,
290
                              input_item_t *p_item )
291 292 293
{
    vod_sys_t *p_sys = p_vod->p_sys;
    vod_media_t *p_media = malloc( sizeof(vod_media_t) );
294
    int i;
295

Jean-Paul Saman's avatar
Jean-Paul Saman committed
296 297 298 299 300 301
    if( !p_media )
    {
        msg_Err( p_vod, "not enough memory" );
        return NULL;
    }

302
    memset( p_media, 0, sizeof(vod_media_t) );
303
    p_media->es = 0;
304
    p_media->psz_mux = 0;
305
    p_media->rtsp = 0;
306
    p_media->b_raw = VLC_FALSE;
307

308
    asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
309
    p_media->p_rtsp_url =
310 311
        httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, NULL,
                            NULL, NULL );
312 313 314

    if( !p_media->p_rtsp_url )
    {
315
        msg_Err( p_vod, "cannot create RTSP url (%s)", p_media->psz_rtsp_path);
316 317
        free( p_media->psz_rtsp_path );
        free( p_media );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
318
        return NULL;
319 320
    }

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

323
    asprintf( &p_media->psz_rtsp_control_v4,
324
               "a=control:rtsp://%%s:%d%s/trackID=%%d\r\n",
325 326
               p_sys->i_port, p_media->psz_rtsp_path );
    asprintf( &p_media->psz_rtsp_control_v6,
327
               "a=control:rtsp://[%%s]:%d%s/trackID=%%d\r\n",
328
              p_sys->i_port, p_media->psz_rtsp_path );
329

330 331
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_SETUP,
                    RtspCallback, (void*)p_media );
332 333 334 335 336 337
    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 );
338 339
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_GETPARAMETER,
                    RtspCallback, (void*)p_media );
340 341 342 343 344 345 346
    httpd_UrlCatch( p_media->p_rtsp_url, HTTPD_MSG_TEARDOWN,
                    RtspCallback, (void*)p_media );

    p_media->p_vod = p_vod;

    TAB_APPEND( p_sys->i_media, p_sys->media, p_media );

347 348 349 350 351 352 353 354 355 356 357 358 359
    vlc_mutex_init( p_vod, &p_media->lock );
    p_media->psz_session_name = strdup("");
    p_media->psz_session_description = strdup("");
    p_media->psz_session_url = strdup("");
    p_media->psz_session_email = strdup("");

    p_media->i_port_audio = 1234;
    p_media->i_port_video = 1236;
    p_media->i_port       = 1238;
    p_media->i_payload_type = 96;

    p_media->i_sdp_id = mdate();
    p_media->i_sdp_version = 1;
360
    p_media->i_length = p_item->i_duration;
361

362 363 364
    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++ )
365 366 367
    {
        MediaAddES( p_vod, p_media, p_item->es[i] );
    }
368 369
    vlc_mutex_unlock( &p_item->lock );

370 371 372 373 374 375 376
    return p_media;
}

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

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

379 380 381
    while( p_media->i_rtsp > 0 ) RtspClientDel( p_media, p_media->rtsp[0] );
    httpd_UrlDelete( p_media->p_rtsp_url );
    if( p_media->psz_rtsp_path ) free( p_media->psz_rtsp_path );
382 383
    if( p_media->psz_rtsp_control_v6 ) free( p_media->psz_rtsp_control_v6 );
    if( p_media->psz_rtsp_control_v4 ) free( p_media->psz_rtsp_control_v4 );
384 385

    TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
386 387 388 389

    while( p_media->i_es ) MediaDelES( p_vod, p_media, &p_media->es[0]->fmt );

    vlc_mutex_destroy( &p_media->lock );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
390 391 392 393
    free( p_media->psz_session_name );
    free( p_media->psz_session_description );
    free( p_media->psz_session_url );
    free( p_media->psz_session_email );
394 395 396 397 398 399
    free( p_media );
}

static int MediaAddES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt )
{
    media_es_t *p_es = malloc( sizeof(media_es_t) );
400
    char *psz_urlc;
401

402
    memset( p_es, 0, sizeof(media_es_t) );
403
    p_media->psz_mux = NULL;
404 405

    /* TODO: update SDP, etc... */
406
    asprintf( &psz_urlc, "%s/trackID=%d",
407 408
              p_media->psz_rtsp_path, p_media->i_es );
    msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
409

410
    switch( p_fmt->i_codec )
411
    {
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
        case VLC_FOURCC( 's', '1', '6', 'b' ):
            if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
            {
                p_es->i_payload_type = 11;
            }
            else if( p_fmt->audio.i_channels == 2 && p_fmt->audio.i_rate == 44100 )
            {
                p_es->i_payload_type = 10;
            }
            else
            {
                p_es->i_payload_type = p_media->i_payload_type++;
            }
            p_es->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
            sprintf( p_es->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
                    p_fmt->audio.i_channels );
            break;
        case VLC_FOURCC( 'u', '8', ' ', ' ' ):
430
            p_es->i_payload_type = p_media->i_payload_type++;
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            p_es->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
            sprintf( p_es->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
                    p_fmt->audio.i_channels );
            break;
        case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
            p_es->i_payload_type = 14;
            p_es->psz_rtpmap = strdup( "MPA/90000" );
            break;
        case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
            p_es->i_payload_type = 32;
            p_es->psz_rtpmap = strdup( "MPV/90000" );
            break;
        case VLC_FOURCC( 'a', '5', '2', ' ' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( "ac3/90000" );
            break;
        case VLC_FOURCC( 'H', '2', '6', '3' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( "H263-1998/90000" );
            break;
451 452 453 454 455
        case VLC_FOURCC( 'h', '2', '6', '4' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( "H264/90000" );
            p_es->psz_fmtp = strdup( "packetization-mode=1" );
            break;
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        case VLC_FOURCC( 'm', 'p', '4', 'v' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( "MP4V-ES/90000" );
            if( p_fmt->i_extra > 0 )
            {
                char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
                p_es->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
                sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
                sprintf( p_es->psz_fmtp,
                        "profile-level-id=3; config=%s;", p_hexa );
                free( p_hexa );
            }
            break;
        case VLC_FOURCC( 'm', 'p', '4', 'a' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
            sprintf( p_es->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
            if( p_fmt->i_extra > 0 )
            {
                char *p_hexa = malloc( 2 * p_fmt->i_extra + 1 );
                p_es->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
                sprintf_hexa( p_hexa, p_fmt->p_extra, p_fmt->i_extra );
                sprintf( 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 );
                free( p_hexa );
            }
            break;
        case VLC_FOURCC( 'm', 'p', '2', 't' ):
            p_media->psz_mux = "ts";
            p_es->i_payload_type = 33;
            p_es->psz_rtpmap = strdup( "MP2T/90000" );
            break;
        case VLC_FOURCC( 'm', 'p', '2', 'p' ):
            p_media->psz_mux = "ps";
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( "MP2P/90000" );
            break;
        case VLC_FOURCC( 's', 'a', 'm', 'r' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
                                    "AMR/8000/2" : "AMR/8000" );
            p_es->psz_fmtp = strdup( "octet-align=1" );
            break;
        case VLC_FOURCC( 's', 'a', 'w', 'b' ):
            p_es->i_payload_type = p_media->i_payload_type++;
            p_es->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
                                    "AMR-WB/16000/2" : "AMR-WB/16000" );
            p_es->psz_fmtp = strdup( "octet-align=1" );
            break;
507

508 509 510 511 512
        default:
            msg_Err( p_vod, "cannot add this stream (unsupported "
                    "codec: %4.4s)", (char*)&p_fmt->i_codec );
            free( p_es );
            return VLC_EGENERIC;
513 514 515
    }

    p_es->p_rtsp_url =
516 517
        httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, NULL, NULL,
                            NULL );
518 519 520

    if( !p_es->p_rtsp_url )
    {
521
        msg_Err( p_vod, "cannot create RTSP url (%s)", psz_urlc );
522 523 524
        free( psz_urlc );
        free( p_es );
        return VLC_EGENERIC;
525
    }
526 527 528 529
    free( psz_urlc );

    httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
                    RtspCallbackES, (void*)p_es );
530 531 532 533 534 535
    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 );
536

537
    es_format_Copy( &p_es->fmt, p_fmt );
538 539 540
    p_es->p_vod = p_vod;
    p_es->p_media = p_media;

541
#if 0
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    /* Choose the port */
    if( p_fmt->i_cat == AUDIO_ES && p_media->i_port_audio > 0 )
    {
        p_es->i_port = p_media->i_port_audio;
        p_media->i_port_audio = 0;
    }
    else if( p_fmt->i_cat == VIDEO_ES && p_media->i_port_video > 0 )
    {
        p_es->i_port = p_media->i_port_video;
        p_media->i_port_video = 0;
    }
    while( !p_es->i_port )
    {
        if( p_media->i_port != p_media->i_port_audio &&
            p_media->i_port != p_media->i_port_video )
        {
            p_es->i_port = p_media->i_port;
            p_media->i_port += 2;
            break;
        }
        p_media->i_port += 2;
    }
564 565 566 567
#else

    p_es->i_port = 0;
#endif
568 569 570 571 572 573 574

    vlc_mutex_lock( &p_media->lock );
    TAB_APPEND( p_media->i_es, p_media->es, p_es );
    vlc_mutex_unlock( &p_media->lock );

    p_media->i_sdp_version++;

575 576 577 578 579 580
    return VLC_SUCCESS;
}

static void MediaDelES( vod_t *p_vod, vod_media_t *p_media, es_format_t *p_fmt)
{
    media_es_t *p_es = 0;
581 582 583 584 585 586 587 588 589 590 591 592 593
    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;
594

595 596 597
    msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );

    vlc_mutex_lock( &p_media->lock );
598
    TAB_REMOVE( p_media->i_es, p_media->es, p_es );
599
    vlc_mutex_unlock( &p_media->lock );
600

601 602 603
    if( p_es->psz_rtpmap ) free( p_es->psz_rtpmap );
    if( p_es->psz_fmtp ) free( p_es->psz_fmtp );
    p_media->i_sdp_version++;
604 605

    if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
606
    es_format_Clean( &p_es->fmt );
607 608 609 610 611 612 613
}

/****************************************************************************
 * RTSP server implementation
 ****************************************************************************/
static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
{
614
    rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
615 616

    if( !p_rtsp ) return NULL;
617 618
    memset( p_rtsp, 0, sizeof(rtsp_client_t) );
    p_rtsp->es = 0;
619

620 621
    p_rtsp->psz_session = psz_session;
    TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
622

623 624
    p_media->p_vod->p_sys->i_connections++;
    msg_Dbg( p_media->p_vod, "new session: %s, connections: %d",
625
             psz_session, p_media->p_vod->p_sys->i_throttle_users );
626

627
    return p_rtsp;
628 629 630 631 632 633
}

static rtsp_client_t *RtspClientGet( vod_media_t *p_media, char *psz_session )
{
    int i;

634
    for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
635 636 637 638 639 640 641 642 643 644
    {
        if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
        {
            return p_media->rtsp[i];
        }
    }

    return NULL;
}

645
static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
646
{
647 648
    p_media->p_vod->p_sys->i_connections--;
    msg_Dbg( p_media->p_vod, "closing session: %s, connections: %d",
649
             p_rtsp->psz_session, p_media->p_vod->p_sys->i_throttle_users );
650

651 652
    while( p_rtsp->i_es-- )
    {
653 654 655 656
        if( p_rtsp->es[p_rtsp->i_es]->psz_ip )
            free( p_rtsp->es[p_rtsp->i_es]->psz_ip );
        free( p_rtsp->es[p_rtsp->i_es] );
        if( !p_rtsp->i_es ) free( p_rtsp->es );
657 658
    }

659
    TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
660

661 662
    free( p_rtsp->psz_session );
    free( p_rtsp );
663 664 665 666 667 668 669
}

static int RtspCallback( httpd_callback_sys_t *p_args, httpd_client_t *cl,
                         httpd_message_t *answer, httpd_message_t *query )
{
    vod_media_t *p_media = (vod_media_t*)p_args;
    vod_t *p_vod = p_media->p_vod;
670
    char *psz_transport = NULL;
671
    char *psz_playnow = NULL; /* support option: x-playNow */
672
    char *psz_session = NULL;
673
    char *psz_cseq = NULL;
674
    rtsp_client_t *p_rtsp;
675
    int i_port = 0;
676
    int i_cseq = 0;
677 678 679

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

680
    msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
681 682 683 684

    answer->i_proto   = HTTPD_PROTO_RTSP;
    answer->i_version = query->i_version;
    answer->i_type    = HTTPD_MSG_ANSWER;
685 686
    answer->i_body    = 0;
    answer->p_body      = NULL;
687 688 689

    switch( query->i_type )
    {
690 691
        case HTTPD_MSG_SETUP:
        {
692
            psz_playnow = httpd_MsgGet( query, "x-playNow" );
693 694 695 696 697 698 699 700
            psz_transport = httpd_MsgGet( query, "Transport" );
            msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );

            if( strstr( psz_transport, "unicast" ) &&
                strstr( psz_transport, "client_port=" ) )
            {
                rtsp_client_t *p_rtsp;
                char ip[NI_MAXNUMERICHOST];
701
                i_port = atoi( strstr( psz_transport, "client_port=" ) +
702 703
                                strlen("client_port=") );

704 705 706
                if( strstr( psz_transport, "MP2T/H2221/UDP" ) ||
                    strstr( psz_transport, "RAW/RAW/UDP" ) )
                {
707
                    p_media->psz_mux = p_vod->p_sys->psz_raw_mux;
708
                    p_media->b_raw = VLC_TRUE;
709
                }
710

711 712 713 714 715 716 717 718 719 720
                if( httpd_ClientIP( cl, ip ) == NULL )
                {
                    answer->i_status = 500;
                    answer->psz_status = strdup( "Internal server error" );
                    answer->i_body = 0;
                    answer->p_body = NULL;
                    break;
                }

                msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
721
                         ip, i_port );
722 723 724 725

                psz_session = httpd_MsgGet( query, "Session" );
                if( !psz_session || !*psz_session )
                {
726 727
                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
728
                    {
729
                        answer->i_status = 503;
730 731 732 733 734
                        answer->psz_status = strdup( "Too many connections" );
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
                    asprintf( &psz_session, "%d", rand() );
                    p_rtsp = RtspClientNew( p_media, psz_session );
                }
                else
                {
                    p_rtsp = RtspClientGet( p_media, psz_session );
                    if( !p_rtsp )
                    {
                        answer->i_status = 454;
                        answer->psz_status = strdup( "Unknown session id" );
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
                }

                answer->i_status = 200;
                answer->psz_status = strdup( "OK" );
                answer->i_body = 0;
                answer->p_body = NULL;

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
                if( p_media->b_raw )
                {
                    if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
                    {
                        httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
                                      i_port, i_port + 1 );
                    }
                    else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
                    {
                        httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
                                      i_port, i_port + 1 );
                    }
                }
                else
                    httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
                                  i_port, i_port + 1 );
772 773 774 775 776 777 778 779
            }
            else /* TODO  strstr( psz_transport, "interleaved" ) ) */
            {
                answer->i_status = 461;
                answer->psz_status = strdup( "Unsupported Transport" );
                answer->i_body = 0;
                answer->p_body = NULL;
            }
780

781 782
            /* Intentional fall-through on x-playNow option in RTSP request */
            if( !psz_playnow )
783
                break;
784 785 786 787
        }

        case HTTPD_MSG_PLAY:
        {
788
            char *psz_output, ip[NI_MAXNUMERICHOST];
789
            int i, i_port_audio = 0, i_port_video = 0;
790 791

            /* for now only multicast so easy */
792 793 794 795 796 797 798
            if( !psz_playnow )
            {
                answer->i_status = 200;
                answer->psz_status = strdup( "OK" );
                answer->i_body = 0;
                answer->p_body = NULL;
            }
799

800 801
            if( !psz_session )
                psz_session = httpd_MsgGet( query, "Session" );
802 803
            msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );

804 805 806
            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;

807 808 809
            if( p_rtsp->b_playing )
            {
                char *psz_position = httpd_MsgGet( query, "Range" );
810
                char *psz_scale = httpd_MsgGet( query, "Scale" );
811
                if( psz_position ) psz_position = strstr( psz_position, "npt=" );
812
                if( psz_position && !psz_scale )
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
                {
                    double f_pos;
                    char *end;

                    msg_Dbg( p_vod, "seeking request: %s", psz_position );
                    psz_position += 4;
                    /* FIXME: npt= is not necessarily formatted as a float */
                    f_pos = us_strtod( psz_position, &end );
                    if( end > psz_position )
                    {
                        f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
                        vod_MediaControl( p_vod, p_media, psz_session,
                                      VOD_MEDIA_SEEK, f_pos );
                    }
                    break;
                }
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
                if( psz_scale )
                {
                    double f_scale = 0.0;
                    char *end;

                    f_scale = us_strtod( psz_scale, &end );
                    if( end > psz_scale )
                    {
                        f_scale = (f_scale * 30.0);
                        if( psz_scale[0] == '-' ) /* rewind */
                        {
                            msg_Dbg( p_vod, "rewind request: %s", psz_scale );
                            vod_MediaControl( p_vod, p_media, psz_session,
                                                VOD_MEDIA_REWIND, f_scale );
                        }
                        else if(psz_scale[0] != '1' ) /* fast-forward */
                        {
                            msg_Dbg( p_vod, "fastforward request: %s", psz_scale );
                            vod_MediaControl( p_vod, p_media, psz_session,
                                                VOD_MEDIA_FORWARD, f_scale );
                        }

                        if( p_rtsp->b_paused == VLC_TRUE )
                        {
                            p_rtsp->b_paused = VLC_FALSE;
                            vod_MediaControl( p_vod, p_media, psz_session,
                                                VOD_MEDIA_PAUSE, psz_output );
                        }
                    }
                    break;
                }
860 861
            }

862 863 864 865 866 867 868 869 870
            if( p_rtsp->b_playing && p_rtsp->b_paused )
            {
                vod_MediaControl( p_vod, p_media, psz_session,
                                  VOD_MEDIA_PAUSE );
                p_rtsp->b_paused = VLC_FALSE;
                break;
            }
            else if( p_rtsp->b_playing ) break;

871
            if( httpd_ClientIP( cl, ip ) == NULL ) break;
872

873
            p_rtsp->b_playing = VLC_TRUE;
874 875

            /* FIXME for != 1 video and 1 audio */
876
            for( i = 0; i < p_rtsp->i_es; i++ )
877
            {
878 879 880 881
                if( p_rtsp->es[i]->p_media_es->fmt.i_cat == AUDIO_ES )
                    i_port_audio = p_rtsp->es[i]->i_port;
                if( p_rtsp->es[i]->p_media_es->fmt.i_cat == VIDEO_ES )
                    i_port_video = p_rtsp->es[i]->i_port;
882
            }
883

884 885
            if( p_media->psz_mux )
            {
886 887 888 889 890 891 892 893 894 895
                if( p_media->b_raw )
                {
                    asprintf( &psz_output, "std{access=udp,dst=%s:%i,mux=%s}",
                              ip, i_port, p_media->psz_mux );
                }
                else
                {
                    asprintf( &psz_output, "rtp{dst=%s,port=%i,mux=%s}",
                              ip, i_port_video, p_media->psz_mux );
                }
896 897 898 899 900 901 902
            }
            else
            {
                asprintf( &psz_output, "rtp{dst=%s,port-video=%i,"
                          "port-audio=%i}", ip, i_port_video, i_port_audio );
            }

903 904 905
            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
                              psz_output );
            free( psz_output );
906 907 908
            break;
        }

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
        case HTTPD_MSG_DESCRIBE:
        {
            char *psz_sdp =
                SDPGenerate( p_media, cl );

            if( psz_sdp != NULL )
            {
                answer->i_status = 200;
                answer->psz_status = strdup( "OK" );
                httpd_MsgAdd( answer, "Content-type",  "%s", "application/sdp" );

                answer->p_body = (uint8_t *)psz_sdp;
                answer->i_body = strlen( psz_sdp );
            }
            else
            {
                answer->i_status = 500;
                answer->psz_status = strdup( "Internal server error" );
                answer->p_body = NULL;
                answer->i_body = 0;
            }
            break;
        }

933 934 935
        case HTTPD_MSG_PAUSE:
            psz_session = httpd_MsgGet( query, "Session" );
            msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );
936

937 938 939
            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;

940
            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
941
            p_rtsp->b_paused = VLC_TRUE;
942 943 944 945 946 947

            answer->i_status = 200;
            answer->psz_status = strdup( "OK" );
            answer->i_body = 0;
            answer->p_body = NULL;
            break;
948 949 950 951 952 953 954 955 956 957 958

        case HTTPD_MSG_TEARDOWN:
            /* for now only multicast so easy again */
            answer->i_status = 200;
            answer->psz_status = strdup( "OK" );
            answer->i_body = 0;
            answer->p_body = NULL;

            psz_session = httpd_MsgGet( query, "Session" );
            msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);

959 960
            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;
961 962

            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
963
            RtspClientDel( p_media, p_rtsp );
964 965
            break;

966 967 968 969 970 971 972
        case HTTPD_MSG_GETPARAMETER:
            answer->i_status = 200;
            answer->psz_status = strdup( "OK" );
            answer->i_body = 0;
            answer->p_body = NULL;
            break;

973 974 975 976 977 978
        default:
            return VLC_EGENERIC;
    }

    httpd_MsgAdd( answer, "Server", "VLC Server" );
    httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
979 980
    psz_cseq = httpd_MsgGet( query, "Cseq" );
    psz_cseq ? i_cseq = atoi( psz_cseq ) : 0;
981
    httpd_MsgAdd( answer, "CSeq", "%d", i_cseq );
982 983 984 985 986 987 988 989 990 991
    httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );

    if( psz_session )
    {
        httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
    }

    return VLC_SUCCESS;
}

992
static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
993 994
                           httpd_message_t *answer, httpd_message_t *query )
{
995 996
    media_es_t *p_es = (media_es_t*)p_args;
    vod_media_t *p_media = p_es->p_media;
997
    vod_t *p_vod = p_media->p_vod;
998
    rtsp_client_t *p_rtsp = NULL;
999
    char *psz_transport = NULL;
1000 1001
    char *psz_playnow = NULL; /* support option: x-playNow */
    char *psz_session = NULL;
1002
    char *psz_position = NULL;
1003 1004
    char *psz_cseq = NULL;
    int i_cseq = 0;
1005
    int i;
1006 1007 1008

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

1009
    msg_Dbg( p_vod, "RtspCallback query: type=%d", query->i_type );
1010 1011 1012 1013

    answer->i_proto   = HTTPD_PROTO_RTSP;
    answer->i_version = query->i_version;
    answer->i_type    = HTTPD_MSG_ANSWER;
1014 1015
    answer->i_body    = 0;
    answer->p_body      = NULL;
1016 1017 1018

    switch( query->i_type )
    {
1019 1020 1021
        case HTTPD_MSG_SETUP:
            psz_playnow = httpd_MsgGet( query, "x-playNow" );
            psz_transport = httpd_MsgGet( query, "Transport" );
1022

1023
            msg_Dbg( p_vod, "HTTPD_MSG_SETUP: transport=%s", psz_transport );
1024

1025 1026
            if( strstr( psz_transport, "unicast" ) &&
                strstr( psz_transport, "client_port=" ) )
1027
            {
1028 1029 1030 1031 1032
                rtsp_client_t *p_rtsp;
                rtsp_client_es_t *p_rtsp_es;
                char ip[NI_MAXNUMERICHOST];
                int i_port = atoi( strstr( psz_transport, "client_port=" ) +
                                strlen("client_port=") );
1033

1034
                if( httpd_ClientIP( cl, ip ) == NULL )
1035
                {
1036 1037
                    answer->i_status = 500;
                    answer->psz_status = strdup( "Internal server error" );
1038 1039 1040 1041 1042
                    answer->i_body = 0;
                    answer->p_body = NULL;
                    break;
                }

1043 1044
                msg_Dbg( p_vod, "HTTPD_MSG_SETUP: unicast ip=%s port=%d",
                        ip, i_port );
1045

1046 1047 1048
                psz_session = httpd_MsgGet( query, "Session" );
                if( !psz_session || !*psz_session )
                {
1049 1050
                    if( ( p_vod->p_sys->i_throttle_users > 0 ) &&
                        ( p_vod->p_sys->i_connections >= p_vod->p_sys->i_throttle_users ) )
1051
                    {
1052
                        answer->i_status = 503;
1053 1054 1055 1056 1057
                        answer->psz_status = strdup( "Too many connections" );
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
                    asprintf( &psz_session, "%d", rand() );
                    p_rtsp = RtspClientNew( p_media, psz_session );
                }
                else
                {
                    p_rtsp = RtspClientGet( p_media, psz_session );
                    if( !p_rtsp )
                    {
                        answer->i_status = 454;
                        answer->psz_status = strdup( "Unknown session id" );
                        answer->i_body = 0;
                        answer->p_body = NULL;
                        break;
                    }
                }
1073

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
                p_rtsp_es = malloc( sizeof(rtsp_client_es_t) );
                p_rtsp_es->i_port = i_port;
                p_rtsp_es->psz_ip = strdup( ip );
                p_rtsp_es->p_media_es = p_es;
                TAB_APPEND( p_rtsp->i_es, p_rtsp->es, p_rtsp_es );

                answer->i_status = 200;
                answer->psz_status = strdup( "OK" );
                answer->i_body = 0;
                answer->p_body = NULL;

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
                if( p_media->b_raw )
                {
                    if( strstr( psz_transport, "MP2T/H2221/UDP" ) )
                    {
                        httpd_MsgAdd( answer, "Transport", "MP2T/H2221/UDP;client_port=%d-%d",
                                      i_port, i_port + 1 );
                    }
                    else if( strstr( psz_transport, "RAW/RAW/UDP" ) )
                    {
                        httpd_MsgAdd( answer, "Transport", "RAW/RAW/UDP;client_port=%d-%d",
                                      i_port, i_port + 1 );
                    }
                }
                else
                {
                    httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
                                  i_port, i_port + 1 );
                }
1103 1104 1105 1106 1107 1108 1109 1110
            }
            else /* TODO  strstr( psz_transport, "interleaved" ) ) */
            {
                answer->i_status = 461;
                answer->psz_status = strdup( "Unsupported Transport" );
                answer->i_body = 0;
                answer->p_body = NULL;
            }
1111

1112 1113
            /* Intentional fall-through on x-playNow option in RTSP request */
            if( !psz_playnow )
1114
                break;
1115 1116

        case HTTPD_MSG_PLAY:
1117 1118
            /* This is kind of a kludge. Should we only support Aggregate
             * Operations ? */
1119 1120 1121 1122 1123 1124 1125 1126 1127
            psz_session = httpd_MsgGet( query, "Session" );
            msg_Dbg( p_vod, "HTTPD_MSG_PLAY for session: %s", psz_session );

            p_rtsp = RtspClientGet( p_media, psz_session );

            psz_position = httpd_MsgGet( query, "Range" );
            if( psz_position ) psz_position = strstr( psz_position, "npt=" );
            if( psz_position )
            {
1128 1129
                double f_pos;
                char *end;
1130 1131 1132 1133

                msg_Dbg( p_vod, "seeking request: %s", psz_position );

                psz_position += 4;
1134 1135 1136
                /* FIXME: npt= is not necessarily formatted as a float */
                f_pos = us_strtod( psz_position, &end );
                if( end > psz_position )
1137
                {
1138
                    f_pos /= ((double)(p_media->i_length))/1000 /1000 / 100;
1139
                    vod_MediaControl( p_vod, p_media, psz_session,
1140
                                      VOD_MEDIA_SEEK, f_pos );
1141 1142 1143
                }
            }

1144 1145 1146 1147 1148 1149 1150
            if( !psz_playnow )
            {
                answer->i_status = 200;
                answer->psz_status = strdup( "OK" );
                answer->i_body = 0;
                answer->p_body = NULL;
            }
1151 1152
            break;

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
        case HTTPD_MSG_TEARDOWN:
            answer->i_status = 200;
            answer->psz_status = strdup( "OK" );
            answer->i_body = 0;
            answer->p_body = NULL;

            psz_session = httpd_MsgGet( query, "Session" );
            msg_Dbg( p_vod, "HTTPD_MSG_TEARDOWN for session: %s", psz_session);

            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;

            for( i = 0; i < p_rtsp->i_es; i++ )
            {
                if( p_rtsp->es[i]->p_media_es == p_es )
                {
                    if( p_rtsp->es[i]->psz_ip ) free( p_rtsp->es[i]->psz_ip );
                    TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_rtsp->es[i] );
                    break;
                }
            }

            if( !p_rtsp->i_es )
            {
                vod_MediaControl( p_vod, p_media, psz_session,
                                  VOD_MEDIA_STOP );
                RtspClientDel( p_media, p_rtsp );
            }
            break;

1183
        case HTTPD_MSG_PAUSE:
1184 1185
            /* This is kind of a kludge. Should we only support Aggregate
             * Operations ? */
1186 1187 1188
            psz_session = httpd_MsgGet( query, "Session" );
            msg_Dbg( p_vod, "HTTPD_MSG_PAUSE for session: %s", psz_session );

1189 1190 1191 1192 1193 1194 1195 1196
            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;

            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
            p_rtsp->b_paused = VLC_TRUE;

            answer->i_status = 200;
            answer->psz_status = strdup( "OK" );
1197 1198 1199 1200
            answer->i_body = 0;
            answer->p_body = NULL;
            break;

1201 1202 1203 1204 1205 1206 1207
        default:
            return VLC_EGENERIC;
            break;
    }

    httpd_MsgAdd( answer, "Server", "VLC Server" );
    httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1208 1209 1210 1211 1212 1213
    psz_cseq = httpd_MsgGet( query, "Cseq" );
    if (psz_cseq)
        i_cseq = atoi( psz_cseq );
    else
        i_cseq = 0;
    httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );

    if( psz_session )
        httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * SDPGenerate: TODO
 * FIXME: need to be moved to a common place ?
 *****************************************************************************/
1226
static char *SDPGenerate( const vod_media_t *p_media, httpd_client_t *cl )
1227
{
1228
    int i, i_size;
1229
    char *p, *psz_sdp, ip[NI_MAXNUMERICHOST], ipv;
1230
    const char *psz_control;
1231 1232 1233 1234 1235 1236 1237 1238 1239

    if( httpd_ServerIP( cl, ip ) == NULL )
        return NULL;

    p = strchr( ip, '%' );
    if( p != NULL )
        *p = '\0'; /* remove scope if present */

    ipv = ( strchr( ip, ':' ) != NULL ) ? '6' : '4';
1240 1241

    /* Calculate size */
1242 1243 1244 1245 1246 1247
    i_size = sizeof( "v=0\r\n" ) +
        sizeof( "o=- * * IN IP4 \r\n" ) + 10 + NI_MAXNUMERICHOST +
        sizeof( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
        sizeof( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
        sizeof( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
        sizeof( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
1248
        sizeof( "c=IN IP4 0.0.0.0\r\n" ) + 20 + 10 +
1249 1250 1251
        sizeof( "t=0 0\r\n" ) + /* FIXME */
        sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
        sizeof( "a=range:npt=0-1000000000.000\r\n" );
1252

1253 1254
    psz_control = (ipv == '6') ? p_media->psz_rtsp_control_v6
                               : p_media->psz_rtsp_control_v4;
1255 1256 1257 1258
    for( i = 0; i < p_media->i_es; i++ )
    {
        media_es_t *p_es = p_media->es[i];

1259
        i_size += sizeof( "m=**d*o * RTP/AVP *\r\n" ) + 19;
1260 1261
        if( p_es->psz_rtpmap )
        {
1262 1263
            i_size += sizeof( "a=rtpmap:* *\r\n" ) +
                strlen( p_es->psz_rtpmap ) + 9;
1264 1265 1266
        }
        if( p_es->psz_fmtp )
        {
1267 1268
            i_size += sizeof( "a=fmtp:* *\r\n" ) +
                strlen( p_es->psz_fmtp ) + 9;
1269 1270
        }
    }
1271
    i_size += (strlen( psz_control ) + strlen( ip ) + 9) * p_media->i_es;
1272 1273 1274

    p = psz_sdp = malloc( i_size );
    p += sprintf( p, "v=0\r\n" );
1275 1276
    p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
                  p_media->i_sdp_id, p_media->i_sdp_version, ipv, ip );
1277 1278 1279 1280 1281 1282 1283 1284 1285
    if( *p_media->psz_session_name )
        p += sprintf( p, "s=%s\r\n", p_media->psz_session_name );
    if( *p_media->psz_session_description )
        p += sprintf( p, "i=%s\r\n", p_media->psz_session_description );
    if( *p_media->psz_session_url )
        p += sprintf( p, "u=%s\r\n", p_media->psz_session_url );
    if( *p_media->psz_session_email )
        p += sprintf( p, "e=%s\r\n", p_media->psz_session_email );

1286 1287
    p += sprintf( p, "c=IN IP%c %s\r\n", ipv, ipv == '6' ? "::" : "0.0.0.0" );
    p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
1288
    p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
1289

1290
    if( p_media->i_length > 0 )
1291
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
1292
        lldiv_t d = lldiv( p_media->i_length / 1000, 1000 );
1293
        p += sprintf( p, "a=range:npt=0-%lld.%03u\r\n", d.quot,
1294 1295
                      (unsigned)d.rem );
    }
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
    for( i = 0; i < p_media->i_es; i++ )
    {
        media_es_t *p_es = p_media->es[i];

        if( p_es->fmt.i_cat == AUDIO_ES )
        {
            p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
                          p_es->i_port, p_es->i_payload_type );
        }
        else if( p_es->fmt.i_cat == VIDEO_ES )
        {
            p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
                          p_es->i_port, p_es->i_payload_type );
        }
        else
        {
            continue;
        }

        if( p_es->psz_rtpmap )
        {
            p += sprintf( p, "a=rtpmap:%d %s\r\n", p_es->i_payload_type,
                          p_es->psz_rtpmap );
        }
        if( p_es->psz_fmtp )
        {
            p += sprintf( p, "a=fmtp:%d %s\r\n", p_es->i_payload_type,
                          p_es->psz_fmtp );
        }

1327
        p += sprintf( p, psz_control, ip, i );