rtsp.c 30.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
/*****************************************************************************
 * rtsp.c: rtsp VoD server module
 *****************************************************************************
 * Copyright (C) 2003-2004 VideoLAN
 * $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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * 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"
#include "network.h"

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

#define HOST_TEXT N_( "Host address" )
#define HOST_LONGTEXT N_( \
    "You can set the address, port and path the rtsp interface will bind to." )

vlc_module_begin();
    set_description( _("RTSP VoD server") );
    set_capability( "vod server", 1 );
    set_callbacks( Open, Close );
    add_shortcut( "rtsp" );
    add_string ( "rtsp-host", NULL, NULL, HOST_TEXT, HOST_LONGTEXT, VLC_TRUE );
vlc_module_end();

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

62 63 64 65 66 67 68 69 70 71
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;

72 73 74 75 76 77
typedef struct
{
    char *psz_session;
    int64_t i_last; /* for timeout */

    vlc_bool_t b_playing; /* is it in "play" state */
78 79 80 81
    vlc_bool_t b_paused; /* is it in "pause" state */

    int i_es;
    rtsp_client_es_t **es;
82 83 84

} rtsp_client_t;

85
struct media_es_t
86 87 88 89 90
{
    /* VoD server */
    vod_t *p_vod;

    /* RTSP server */
91
    httpd_url_t *p_rtsp_url;
92 93 94

    vod_media_t *p_media;

95 96 97 98 99 100
    es_format_t fmt;
    int         i_port;
    uint8_t     i_payload_type;
    char        *psz_rtpmap;
    char        *psz_fmtp;

101
};
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

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

    /* RTSP server */
    httpd_url_t  *p_rtsp_url;
    char         *psz_rtsp_control;
    char         *psz_rtsp_path;

    char *psz_destination;
    int  i_port;
    int  i_port_audio;
    int  i_port_video;
    int  i_ttl;
118 119 120 121 122
    int  i_payload_type;

    int64_t i_sdp_id;
    int     i_sdp_version;

123 124
    vlc_bool_t b_multicast;

125
    vlc_mutex_t lock;
126 127 128 129 130 131 132 133

    /* ES list */
    int        i_es;
    media_es_t **es;

    /* RTSP client */
    int           i_rtsp;
    rtsp_client_t **rtsp;
134 135 136 137 138 139

    /* Infos */
    char *psz_session_name;
    char *psz_session_description;
    char *psz_session_url;
    char *psz_session_email;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
};

struct vod_sys_t
{
    /* RTSP server */
    httpd_host_t *p_rtsp_host;
    char *psz_host;
    char *psz_path;
    int i_port;

    /* List of media */
    int i_media;
    vod_media_t **media;
};

155
static vod_media_t *MediaNew( vod_t *, char *, input_item_t * );
156 157 158 159 160 161 162 163 164 165
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 * );
166
static int RtspCallbackES( httpd_callback_sys_t *, httpd_client_t *,
167 168 169 170
                           httpd_message_t *, httpd_message_t * );

static char *SDPGenerate( vod_media_t *, char * );

171 172 173 174 175 176 177 178 179 180 181 182 183
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';
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
/*****************************************************************************
 * 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.psz_host || !*url.psz_host )
    {
        if( url.psz_host ) free( url.psz_host );
        url.psz_host = strdup( "localhost" );
    }
    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;

    p_sys->p_rtsp_host =
        httpd_HostNew( VLC_OBJECT(p_vod), url.psz_host, url.i_port );
    if( !p_sys->p_rtsp_host )
    {
        msg_Err( p_vod, "cannot create http server (%s:%i)",
                 url.psz_host, url.i_port );
        goto error;
    }

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

    vlc_UrlClean( &url );
    p_sys->media = 0;
    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;

 error:

    if( p_sys && p_sys->p_rtsp_host ) httpd_HostDelete( p_sys->p_rtsp_host );
    if( p_sys ) free( p_sys );
    vlc_UrlClean( &url );
    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 );

    /* TODO delete medias */

    free( p_sys );
}

/*****************************************************************************
 * Media handling
 *****************************************************************************/
259 260
static vod_media_t *MediaNew( vod_t *p_vod, char *psz_name,
                              input_item_t *p_item )
261 262 263
{
    vod_sys_t *p_sys = p_vod->p_sys;
    vod_media_t *p_media = malloc( sizeof(vod_media_t) );
264
    int i;
265

266
    memset( p_media, 0, sizeof(vod_media_t) );
267 268
    p_media->es = 0;
    p_media->rtsp = 0;
269

270
    asprintf( &p_media->psz_rtsp_path, "%s%s", p_sys->psz_path, psz_name );
271 272 273 274 275
    p_media->p_rtsp_url =
        httpd_UrlNewUnique( p_sys->p_rtsp_host, p_media->psz_rtsp_path, 0, 0 );

    if( !p_media->p_rtsp_url )
    {
276
        msg_Err( p_vod, "cannot create http url (%s)", p_media->psz_rtsp_path);
277 278
        free( p_media->psz_rtsp_path );
        free( p_media );
279
        return 0;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    }

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

    asprintf( &p_media->psz_rtsp_control, "rtsp://%s:%d%s",
              p_sys->psz_host, p_sys->i_port, p_media->psz_rtsp_path );

    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 );
    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 );

300 301 302 303 304 305 306 307 308 309 310 311 312 313
    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;

314 315 316
    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++ )
317 318 319
    {
        MediaAddES( p_vod, p_media, p_item->es[i] );
    }
320 321
    vlc_mutex_unlock( &p_item->lock );

322 323 324 325 326 327 328
    return p_media;
}

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

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

331 332 333 334 335 336
    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 );
    if( p_media->psz_rtsp_control ) free( p_media->psz_rtsp_control );

    TAB_REMOVE( p_sys->i_media, p_sys->media, p_media );
337 338 339 340

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

    vlc_mutex_destroy( &p_media->lock );
341 342 343 344 345 346
    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) );
347
    char *psz_urlc;
348

349
    memset( p_es, 0, sizeof(media_es_t) );
350 351

    /* TODO: update SDP, etc... */
352 353 354
    asprintf( &psz_urlc, "%s/trackid=%d",
              p_media->psz_rtsp_path, p_media->i_es );
    msg_Dbg( p_vod, "  - ES %4.4s (%s)", (char *)&p_fmt->i_codec, psz_urlc );
355

356
    switch( p_fmt->i_codec )
357
    {
358 359 360 361 362 363 364 365 366 367 368 369 370
    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++;
        }
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
        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', ' ', ' ' ):
        p_es->i_payload_type = p_media->i_payload_type++;
        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;
    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 )
402
        {
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
            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 );
425
        }
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        break;

    default:
        msg_Err( p_vod, "cannot add this stream (unsupported "
                 "codec: %4.4s)", (char*)&p_fmt->i_codec );
        free( p_es );
        return VLC_EGENERIC;
    }

    p_es->p_rtsp_url =
        httpd_UrlNewUnique( p_vod->p_sys->p_rtsp_host, psz_urlc, 0, 0 );

    if( !p_es->p_rtsp_url )
    {
        msg_Err( p_vod, "cannot create http url (%s)", psz_urlc );
        free( psz_urlc );
        free( p_es );
        return VLC_EGENERIC;
444
    }
445 446 447 448
    free( psz_urlc );

    httpd_UrlCatch( p_es->p_rtsp_url, HTTPD_MSG_SETUP,
                    RtspCallbackES, (void*)p_es );
449 450 451 452 453 454
    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 );
455

456
    es_format_Copy( &p_es->fmt, p_fmt );
457 458 459
    p_es->p_vod = p_vod;
    p_es->p_media = p_media;

460
#if 0
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    /* 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;
    }
483 484 485 486
#else

    p_es->i_port = 0;
#endif
487 488 489 490 491 492 493

    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++;

494 495 496 497 498 499
    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;
500 501 502 503 504 505 506 507 508 509 510 511 512
    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;
513

514 515 516
    msg_Dbg( p_vod, "  - Removing ES %4.4s", (char *)&p_fmt->i_codec );

    vlc_mutex_lock( &p_media->lock );
517
    TAB_REMOVE( p_media->i_es, p_media->es, p_es );
518
    vlc_mutex_unlock( &p_media->lock );
519

520 521 522
    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++;
523 524

    if( p_es->p_rtsp_url ) httpd_UrlDelete( p_es->p_rtsp_url );
525
    es_format_Clean( &p_es->fmt );
526 527 528 529 530 531 532
}

/****************************************************************************
 * RTSP server implementation
 ****************************************************************************/
static rtsp_client_t *RtspClientNew( vod_media_t *p_media, char *psz_session )
{
533
    rtsp_client_t *p_rtsp = malloc( sizeof(rtsp_client_t) );
534 535
    memset( p_rtsp, 0, sizeof(rtsp_client_t) );
    p_rtsp->es = 0;
536

537 538
    p_rtsp->psz_session = psz_session;
    TAB_APPEND( p_media->i_rtsp, p_media->rtsp, p_rtsp );
539 540 541

    msg_Dbg( p_media->p_vod, "new session: %s", psz_session );

542
    return p_rtsp;
543 544 545 546 547 548
}

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

549
    for( i = 0; psz_session && i < p_media->i_rtsp; i++ )
550 551 552 553 554 555 556 557 558 559
    {
        if( !strcmp( p_media->rtsp[i]->psz_session, psz_session ) )
        {
            return p_media->rtsp[i];
        }
    }

    return NULL;
}

560
static void RtspClientDel( vod_media_t *p_media, rtsp_client_t *p_rtsp )
561
{
562 563
    int i;

564
    msg_Dbg( p_media->p_vod, "closing session: %s", p_rtsp->psz_session );
565

566 567 568 569 570 571 572
    while( p_rtsp->i_es-- )
    {
        rtsp_client_es_t *p_es = p_rtsp->es[p_rtsp->i_es];
        TAB_REMOVE( p_rtsp->i_es, p_rtsp->es, p_es );
        if( p_es[i].psz_ip ) free( p_es[i].psz_ip );
    }

573
    TAB_REMOVE( p_media->i_rtsp, p_media->rtsp, p_rtsp );
574

575 576
    free( p_rtsp->psz_session );
    free( p_rtsp );
577 578 579 580 581 582 583 584 585
}

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;
    char *psz_destination = p_media->psz_destination;
    char *psz_session = NULL;
586
    rtsp_client_t *p_rtsp;
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614

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

    fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );

    answer->i_proto   = HTTPD_PROTO_RTSP;
    answer->i_version = query->i_version;
    answer->i_type    = HTTPD_MSG_ANSWER;

    switch( query->i_type )
    {
        case HTTPD_MSG_DESCRIBE:
        {
            char *psz_sdp =
                SDPGenerate( p_media, psz_destination ?
                             psz_destination : "0.0.0.0" );

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

            answer->p_body = psz_sdp;
            answer->i_body = strlen( psz_sdp );
            break;
        }

        case HTTPD_MSG_PLAY:
        {
615 616
            char *psz_output, *ip;
            int i, i_port_audio = 0, i_port_video = 0;
617 618 619 620 621 622 623 624 625 626

            /* for now only multicast so easy */
            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_PLAY for session: %s", psz_session );

627 628 629 630 631 632 633 634 635 636 637 638
            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;

            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;

639 640
            if( !(ip = httpd_ClientIP( cl )) ) break;

641
            p_rtsp->b_playing = VLC_TRUE;
642 643

            /* FIXME for != 1 video and 1 audio */
644
            for( i = 0; i < p_rtsp->i_es; i++ )
645
            {
646 647 648 649
                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;
650
            }
651 652 653 654 655 656 657

            asprintf( &psz_output, "rtp{dst=%s,port-video=%i,port-audio=%i}",
                      ip, i_port_video, i_port_audio );
            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PLAY,
                              psz_output );
            free( psz_output );
            free( ip );
658 659 660 661 662 663
            break;
        }

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

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

668
            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_PAUSE );
669
            p_rtsp->b_paused = VLC_TRUE;
670 671 672 673 674 675 676 677 678 679 680 681
            return VLC_EGENERIC;

        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);

682 683
            p_rtsp = RtspClientGet( p_media, psz_session );
            if( !p_rtsp ) break;
684 685

            vod_MediaControl( p_vod, p_media, psz_session, VOD_MEDIA_STOP );
686
            RtspClientDel( p_media, p_rtsp );
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
            break;

        default:
            return VLC_EGENERIC;
    }

    httpd_MsgAdd( answer, "Server", "VLC Server" );
    httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
    httpd_MsgAdd( answer, "Cseq", "%d",
                  atoi( httpd_MsgGet( query, "Cseq" ) ) );
    httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );

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

    return VLC_SUCCESS;
}

707
static int RtspCallbackES( httpd_callback_sys_t *p_args, httpd_client_t *cl,
708 709
                           httpd_message_t *answer, httpd_message_t *query )
{
710 711
    media_es_t *p_es = (media_es_t*)p_args;
    vod_media_t *p_media = p_es->p_media;
712 713 714
    vod_t *p_vod = p_media->p_vod;
    char *psz_session = NULL;
    char *psz_transport = NULL;
715
    int i;
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

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

    fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );

    answer->i_proto   = HTTPD_PROTO_RTSP;
    answer->i_version = query->i_version;
    answer->i_type    = HTTPD_MSG_ANSWER;

    switch( query->i_type )
    {
    case HTTPD_MSG_SETUP:
        psz_transport = httpd_MsgGet( query, "Transport" );
        fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );

        if( strstr( psz_transport, "multicast" ) && p_media->psz_destination )
        {
            fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
            answer->i_status = 200;
            answer->psz_status = strdup( "OK" );
            answer->i_body = 0;
            answer->p_body = NULL;

            psz_session = httpd_MsgGet( query, "Session" );
            if( !psz_session || !*psz_session )
            {
                asprintf( &psz_session, "%d", rand() );
            }

            httpd_MsgAdd( answer, "Transport",
                          "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
                          p_media->psz_destination, p_media->i_port,
                          p_media->i_port+1, p_media->i_ttl );
        }
        else if( strstr( psz_transport, "unicast" ) &&
                 strstr( psz_transport, "client_port=" ) )
        {
753 754
            rtsp_client_t *p_rtsp;
            rtsp_client_es_t *p_rtsp_es;
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
            char *ip = httpd_ClientIP( cl );
            int i_port = atoi( strstr( psz_transport, "client_port=" ) +
                               strlen("client_port=") );

            if( !ip )
            {
                answer->i_status = 400;
                answer->psz_status = strdup( "Internal server error" );
                answer->i_body = 0;
                answer->p_body = NULL;
                break;
            }

            fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
                     ip, i_port );

            psz_session = httpd_MsgGet( query, "Session" );
            if( !psz_session || !*psz_session )
            {
                asprintf( &psz_session, "%d", rand() );
775
                p_rtsp = RtspClientNew( p_media, psz_session );
776 777 778
            }
            else
            {
779 780
                p_rtsp = RtspClientGet( p_media, psz_session );
                if( !p_rtsp )
781 782 783 784 785 786 787 788 789 790 791
                {
                    /* FIXME right error code */
                    answer->i_status = 400;
                    answer->psz_status = strdup( "Unknown session id" );
                    answer->i_body = 0;
                    answer->p_body = NULL;
                    free( ip );
                    break;
                }
            }

792 793 794 795 796
            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 );
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814

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

            httpd_MsgAdd( answer, "Transport", "RTP/AVP/UDP;client_port=%d-%d",
                          i_port, i_port + 1 );
        }
        else /* TODO  strstr( psz_transport, "interleaved" ) ) */
        {
            answer->i_status = 400;
            answer->psz_status = strdup( "Bad Request" );
            answer->i_body = 0;
            answer->p_body = NULL;
        }
        break;

815 816 817 818 819 820 821 822 823 824 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
        case HTTPD_MSG_TEARDOWN:
        {
            rtsp_client_t *p_rtsp;

            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;
        }

        case HTTPD_MSG_PLAY:
        case HTTPD_MSG_PAUSE:
            answer->i_status = 460;
            answer->psz_status = strdup( "Only Aggregate Operation Allowed" );
            answer->i_body = 0;
            answer->p_body = NULL;
            break;

857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
        default:
            return VLC_EGENERIC;
            break;
    }

    httpd_MsgAdd( answer, "Server", "VLC Server" );
    httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
    httpd_MsgAdd( answer, "Cseq", "%d",
                  atoi( httpd_MsgGet( query, "Cseq" ) ) );
    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 ?
 *****************************************************************************/
static char *SDPGenerate( vod_media_t *p_media, char *psz_destination )
{
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 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 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    int i, i_size;
    char *p, *psz_sdp;

    /* Calculate size */
    i_size = strlen( "v=0\r\n" ) +
        strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
        strlen( "s=*\r\n" ) + strlen( p_media->psz_session_name ) +
        strlen( "i=*\r\n" ) + strlen( p_media->psz_session_description ) +
        strlen( "u=*\r\n" ) + strlen( p_media->psz_session_url ) +
        strlen( "e=*\r\n" ) + strlen( p_media->psz_session_email ) +
        strlen( "t=0 0\r\n" ) + /* FIXME */
        strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
        strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
        strlen( psz_destination ? psz_destination : "0.0.0.0" ) ;

    for( i = 0; i < p_media->i_es; i++ )
    {
        media_es_t *p_es = p_media->es[i];

        i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
        if( p_es->psz_rtpmap )
        {
            i_size += strlen( "a=rtpmap:* *\r\n" ) +
                strlen( p_es->psz_rtpmap ) + 10;
        }
        if( p_es->psz_fmtp )
        {
            i_size += strlen( "a=fmtp:* *\r\n" ) +
                strlen( p_es->psz_fmtp ) + 10;
        }

        i_size += strlen( "a=control:*/trackid=*\r\n" ) +
            strlen( p_media->psz_rtsp_control ) + 10;
    }

    p = psz_sdp = malloc( i_size );
    p += sprintf( p, "v=0\r\n" );
    p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
                  p_media->i_sdp_id, p_media->i_sdp_version );
    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 );

    p += sprintf( p, "t=0 0\r\n" ); /* FIXME */
    p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );

    p += sprintf( p, "c=IN IP4 %s/%d\r\n", psz_destination ?
                  psz_destination : "0.0.0.0", p_media->i_ttl );

    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 );
        }

        p += sprintf( p, "a=control:%s/trackid=%d\r\n",
                      p_media->psz_rtsp_control, i );
    }

    fprintf( stderr, psz_sdp );
    return psz_sdp;
972
}