sgimb.c 15.7 KB
Newer Older
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
1 2 3
/*****************************************************************************
 * sgimb.c: a meta demux to parse sgimb referrer files
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
5
 * $Id$
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Derk-Jan Hartman <hartman at videolan dot 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
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
22 23 24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * This is a metademux for the Kasenna MediaBase metafile format.
 * Kasenna MediaBase first returns this file when you are trying to access
 * their MPEG streams (MIME: application/x-sgimb). Very few applications
 * understand this format and the format is not really documented on the net.
 * Following a typical MediaBase file. Notice the sgi prefix of all the elements.
30
 * This stems from the fact that the MediaBase servers were first introduced by SGI?????.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
31 32
 *
 * sgiNameServerHost=host.name.tld
33
 *     Obvious: the host hosting this stream
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
34
 * Stream="xdma://host.name.tld/demo/a_very_cool.mpg"
35
 *     Not always present. xdma can be read as RTSP.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
36
 * sgiMovieName=/demo/a_very_cool.mpg
37 38 39 40 41 42 43
 *     The path to the asset
 * sgiAuxState=1|2
 *     AuxState=2 is always Video On Demand (so not Scheduled)
 *     Not present with Live streams
 * sgiLiveFeed=True|False
 *     Denounces if the stream is live or from assets (Canned?)
 *     Live appears as a little sattelite dish in the web interface of Kasenna
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
44
 * sgiFormatName=PARTNER_41_MPEG-4
45 46 47 48 49 50 51 52 53
 *     The type of stream. One of:
 *       PARTNER_41_MPEG-4 (RTSP MPEG-4 fully compliant)
 *       MPEG1-Audio       (MP3 Audio streams in MPEG TS)
 *       MPEG-1            (MPEG 1 A/V in MPEG TS)
 *       MPEG-2            (MPEG 2 A/V in MPEG TS)
 * sgiWidth=720
 *     The width of the to be received stream. Only present if stream is not Live.
 * sgiHeight=576
 *     The height of the to be received stream. Only present if stream is not Live.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
54
 * sgiBitrate=1630208
55
 *     The bitrate of the to be received stream. Only present if stream is not Live.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
56
 * sgiDuration=378345000
57
 *     The duration of the to be received stream. Only present if stream is not Live.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
58 59 60 61
 * sgiQTFileBegin
 * rtsptext
 * rtsp://host.name.tld/demo/a_very_cool.mpg
 * sgiQTFileEnd
62
 *     Sometimes present. QT will recognize this as a RTSP reference file, if present.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
63
 * sgiApplicationName=MediaBaseURL
64
 *     Beats me !! :)
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
65
 * sgiElapsedTime=0
66 67 68 69 70 71 72 73 74
 *     Time passed since the asset was started (resets for repeating non live assets?)
 * sgiMulticastAddress=233.81.233.15
 *     The multicast IP used for the Multicast feed.
 *     Also defines if a stream is multicast or not. (blue dot in kasenna web interface)
 * sgiMulticastPort=1234
 *     The multicast port for the same Multicast feed.
 * sgiPacketSize=16384
 *     The packetsize of the UDP frames that Kasenna sends. They should have used a default
 *     that is a multiple of 188 (TS frame size). Most networks don't support more than 1500 anyways.
75
 *     Also, when you lose a frame of this size, imagecorruption is more likely then with smaller
76
 *     frames.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
77
 * sgiServerVersion=6.1.2
78
 *     Version of the server
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
79
 * sgiRtspPort=554
80
 *     TCP port used for RTSP communication
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
81
 * AutoStart=True
82 83
 *     Start playing automatically
 * DeliveryService=cds
84
 *     Simulcasted (scheduled unicast) content. (Green dot in Kasenna web interface)
85 86 87 88
 * sgiShowingName=A nice name that everyone likes
 *     A human readible descriptive title for this stream.
 * sgiSid=2311
 *     Looks like this is the ID of the scheduled asset?
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
89
 * sgiUserAccount=pid=1724&time=1078527309&displayText=You%20are%20logged%20as%20guest&
90
 *     User Authentication. Above is a default guest entry. Not required for RTSP communication.
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
91
 * sgiUserPassword=
92
 *     Password :)
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
93 94 95 96 97 98 99 100
 *
 *****************************************************************************/


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

101 102 103 104
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

105
#include <vlc_common.h>
Clément Stenac's avatar
Clément Stenac committed
106
#include <vlc_demux.h>
107
#include "playlist.h"
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define MAX_LINE 1024

struct demux_sys_t
{
    char        *psz_uri;       /* Stream= or sgiQTFileBegin rtsp link */
    char        *psz_server;    /* sgiNameServerHost= */
    char        *psz_location;  /* sgiMovieName= */
    char        *psz_name;      /* sgiShowingName= */
    char        *psz_user;      /* sgiUserAccount= */
    char        *psz_password;  /* sgiUserPassword= */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
122 123 124
    char        *psz_mcast_ip;  /* sgiMulticastAddress= */
    int         i_mcast_port;   /* sgiMulticastPort= */
    int         i_packet_size;  /* sgiPacketSize= */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
125 126 127
    mtime_t     i_duration;     /* sgiDuration= */
    int         i_port;         /* sgiRtspPort= */
    int         i_sid;          /* sgiSid= */
128 129
    bool  b_concert;      /* DeliveryService=cds */
    bool  b_rtsp_kasenna; /* kasenna style RTSP */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
130 131 132 133 134 135 136 137
};

static int Demux ( demux_t *p_demux );
static int Control( demux_t *p_demux, int i_query, va_list args );

/*****************************************************************************
 * Activate: initializes m3u demux structures
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
138
int Import_SGIMB( vlc_object_t * p_this )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
139 140
{
    demux_t *p_demux = (demux_t *)p_this;
141
    const uint8_t *p_peek;
142
    int i_size;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
143 144

    /* Lets check the content to see if this is a sgi mediabase file */
145
    i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
146
    i_size -= sizeof("sgiNameServerHost=") - 1;
147 148
    if ( i_size > 0 )
    {
149 150
        unsigned int i_len = sizeof("sgiNameServerHost=") - 1;
        while ( i_size && strncasecmp( (char *)p_peek, "sgiNameServerHost=", i_len ) )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
151 152 153 154
        {
            p_peek++;
            i_size--;
        }
155
        if ( !strncasecmp( (char *)p_peek, "sgiNameServerHost=", i_len ) )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
156
        {
157 158 159 160 161 162 163 164 165 166 167 168 169
            STANDARD_DEMUX_INIT_MSG( "using SGIMB playlist reader" );
            p_demux->p_sys->psz_uri = NULL;
            p_demux->p_sys->psz_server = NULL;
            p_demux->p_sys->psz_location = NULL;
            p_demux->p_sys->psz_name = NULL;
            p_demux->p_sys->psz_user = NULL;
            p_demux->p_sys->psz_password = NULL;
            p_demux->p_sys->psz_mcast_ip = NULL;
            p_demux->p_sys->i_mcast_port = 0;
            p_demux->p_sys->i_packet_size = 0;
            p_demux->p_sys->i_duration = 0;
            p_demux->p_sys->i_port = 0;
            p_demux->p_sys->i_sid = 0;
170 171
            p_demux->p_sys->b_rtsp_kasenna = false;
            p_demux->p_sys->b_concert = false;
172
 
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
173 174 175 176 177 178 179 180 181
            return VLC_SUCCESS;
        }
    }
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
void Close_SGIMB( vlc_object_t *p_this )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
183 184 185
{
    demux_t *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
186 187 188 189 190 191 192
    free( p_sys->psz_uri );
    free( p_sys->psz_server );
    free( p_sys->psz_location );
    free( p_sys->psz_name );
    free( p_sys->psz_user );
    free( p_sys->psz_password );
    free( p_sys->psz_mcast_ip );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    free( p_demux->p_sys );
    return;
}

static int ParseLine ( demux_t *p_demux, char *psz_line )
{
    char        *psz_bol;
    demux_sys_t *p_sys = p_demux->p_sys;

    psz_bol = psz_line;

    /* Remove unnecessary tabs or spaces at the beginning of line */
    while( *psz_bol == ' ' || *psz_bol == '\t' ||
           *psz_bol == '\n' || *psz_bol == '\r' )
    {
        psz_bol++;
    }

    if( !strncasecmp( psz_bol, "rtsp://", sizeof("rtsp://") - 1 ) )
    {
        /* We found the link, it was inside a sgiQTFileBegin */
Rémi Duraffort's avatar
Rémi Duraffort committed
214
        free( p_sys->psz_uri );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
215 216 217 218 219 220 221
        p_sys->psz_uri = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "Stream=\"", sizeof("Stream=\"") - 1 ) )
    {
        psz_bol += sizeof("Stream=\"") - 1;
        if ( !psz_bol )
            return 0;
222 223 224 225
        char* psz_tmp = strrchr( psz_bol, '"' );
        if( !psz_tmp )
            return 0;
        psz_tmp[0] = '\0';
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
226 227 228 229 230 231 232 233
        /* We cheat around xdma. for some reason xdma links work different then rtsp */
        if( !strncasecmp( psz_bol, "xdma://", sizeof("xdma://") - 1 ) )
        {
            psz_bol[0] = 'r';
            psz_bol[1] = 't';
            psz_bol[2] = 's';
            psz_bol[3] = 'p';
        }
Rémi Duraffort's avatar
Rémi Duraffort committed
234
        free( p_sys->psz_uri );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
235 236 237 238 239
        p_sys->psz_uri = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "sgiNameServerHost=", sizeof("sgiNameServerHost=") - 1 ) )
    {
        psz_bol += sizeof("sgiNameServerHost=") - 1;
Rémi Duraffort's avatar
Rémi Duraffort committed
240
        free( p_sys->psz_server );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
241 242 243 244 245
        p_sys->psz_server = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "sgiMovieName=", sizeof("sgiMovieName=") - 1 ) )
    {
        psz_bol += sizeof("sgiMovieName=") - 1;
Rémi Duraffort's avatar
Rémi Duraffort committed
246
        free( p_sys->psz_location );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
247 248 249 250 251
        p_sys->psz_location = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "sgiUserAccount=", sizeof("sgiUserAccount=") - 1 ) )
    {
        psz_bol += sizeof("sgiUserAccount=") - 1;
Rémi Duraffort's avatar
Rémi Duraffort committed
252
        free( p_sys->psz_user );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
253 254 255 256 257
        p_sys->psz_user = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "sgiUserPassword=", sizeof("sgiUserPassword=") - 1 ) )
    {
        psz_bol += sizeof("sgiUserPassword=") - 1;
Rémi Duraffort's avatar
Rémi Duraffort committed
258
        free( p_sys->psz_password );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
259 260 261 262 263
        p_sys->psz_password = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "sgiShowingName=", sizeof("sgiShowingName=") - 1 ) )
    {
        psz_bol += sizeof("sgiShowingName=") - 1;
Rémi Duraffort's avatar
Rémi Duraffort committed
264
        free( p_sys->psz_name );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
265 266
        p_sys->psz_name = strdup( psz_bol );
    }
267 268 269
    else if( !strncasecmp( psz_bol, "sgiFormatName=", sizeof("sgiFormatName=") - 1 ) )
    {
        psz_bol += sizeof("sgiFormatName=") - 1;
270
        if( strcasestr( psz_bol, "MPEG-4") == NULL ) /*not mpeg4 found in string */
271
            p_sys->b_rtsp_kasenna = true;
272
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
273 274 275
    else if( !strncasecmp( psz_bol, "sgiMulticastAddress=", sizeof("sgiMulticastAddress=") - 1 ) )
    {
        psz_bol += sizeof("sgiMulticastAddress=") - 1;
Rémi Duraffort's avatar
Rémi Duraffort committed
276
        free( p_sys->psz_mcast_ip );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
277 278 279 280 281 282 283 284 285 286 287 288
        p_sys->psz_mcast_ip = strdup( psz_bol );
    }
    else if( !strncasecmp( psz_bol, "sgiMulticastPort=", sizeof("sgiMulticastPort=") - 1 ) )
    {
        psz_bol += sizeof("sgiMulticastPort=") - 1;
        p_sys->i_mcast_port = (int) strtol( psz_bol, NULL, 0 );
    }
    else if( !strncasecmp( psz_bol, "sgiPacketSize=", sizeof("sgiPacketSize=") - 1 ) )
    {
        psz_bol += sizeof("sgiPacketSize=") - 1;
        p_sys->i_packet_size = (int) strtol( psz_bol, NULL, 0 );
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
289 290 291 292 293 294 295 296 297
    else if( !strncasecmp( psz_bol, "sgiDuration=", sizeof("sgiDuration=") - 1 ) )
    {
        psz_bol += sizeof("sgiDuration=") - 1;
        p_sys->i_duration = (mtime_t) strtol( psz_bol, NULL, 0 );
    }
    else if( !strncasecmp( psz_bol, "sgiRtspPort=", sizeof("sgiRtspPort=") - 1 ) )
    {
        psz_bol += sizeof("sgiRtspPort=") - 1;
        p_sys->i_port = (int) strtol( psz_bol, NULL, 0 );
298 299 300 301 302 303 304 305
    }
    else if( !strncasecmp( psz_bol, "sgiSid=", sizeof("sgiSid=") - 1 ) )
    {
        psz_bol += sizeof("sgiSid=") - 1;
        p_sys->i_sid = (int) strtol( psz_bol, NULL, 0 );
    }
    else if( !strncasecmp( psz_bol, "DeliveryService=cds", sizeof("DeliveryService=cds") - 1 ) )
    {
306
        p_sys->b_concert = true;
307 308
    }
    else
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
    {
        /* This line isn't really important */
        return 0;
    }
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux ( demux_t *p_demux )
{
    demux_sys_t     *p_sys = p_demux->p_sys;
324
    input_item_t    *p_child = NULL;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
325 326
    char            *psz_line;

327
    INIT_PLAYLIST_STUFF;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
328 329 330 331

    while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
    {
        ParseLine( p_demux, psz_line );
332
        free( psz_line );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
333
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
334 335 336

    if( p_sys->psz_mcast_ip )
    {
337 338
        /* Definetly schedules multicast session */
        /* We don't care if it's live or not */
339
        free( p_sys->psz_uri );
340 341 342 343 344
        if( asprintf( &p_sys->psz_uri, "udp://@" "%s:%i", p_sys->psz_mcast_ip, p_sys->i_mcast_port ) == -1 )
        {
            p_sys->psz_uri = NULL;
            return -1;
        }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
345
    }
346 347

    if( p_sys->psz_uri == NULL )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
348 349 350
    {
        if( p_sys->psz_server && p_sys->psz_location )
        {
351 352 353 354 355 356
            if( asprintf( &p_sys->psz_uri, "rtsp://" "%s:%i%s",
                     p_sys->psz_server, p_sys->i_port > 0 ? p_sys->i_port : 554, p_sys->psz_location ) == -1 )
            {
                p_sys->psz_uri = NULL;
                return -1;
            }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
357 358
        }
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
359

360 361 362 363 364 365 366 367 368
    if( p_sys->b_concert )
    {
        /* It's definetly a simulcasted scheduled stream */
        /* We don't care if it's live or not */
        if( p_sys->psz_uri == NULL )
        {
            msg_Err( p_demux, "no URI was found" );
            return -1;
        }
369

370
        free( p_sys->psz_uri );
371 372 373 374 375 376
        if( asprintf( &p_sys->psz_uri, "%s%%3FMeDiAbAsEshowingId=%d%%26MeDiAbAsEconcert%%3FMeDiAbAsE",
                p_sys->psz_uri, p_sys->i_sid ) == -1 )
        {
            p_sys->psz_uri = NULL;
            return -1;
        }
377 378
    }

379
    p_child = input_item_NewWithType( VLC_OBJECT(p_demux), p_sys->psz_uri,
380 381
                      p_sys->psz_name ? p_sys->psz_name : p_sys->psz_uri,
                      0, NULL, p_sys->i_duration, ITEM_TYPE_NET );
382
 
383
    if( !p_child )
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
384 385
    {
        msg_Err( p_demux, "A valid playlistitem could not be created" );
386
        return -1;
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
387 388
    }

389
    input_item_CopyOptions( p_current_input, p_child );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
390 391
    if( p_sys->i_packet_size && p_sys->psz_mcast_ip )
    {
392
        char *psz_option;
393
        p_sys->i_packet_size += 1000;
394 395
        if( asprintf( &psz_option, "mtu=%i", p_sys->i_packet_size ) != -1 )
        {
396
            input_item_AddOption( p_child, psz_option, VLC_INPUT_OPTION_TRUSTED );
397 398
            free( psz_option );
        }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
399
    }
400 401 402
    if( !p_sys->psz_mcast_ip )
    {
        char *psz_option;
403 404
        if( asprintf( &psz_option, "rtsp-caching=5000" ) != -1 )
        {
405
            input_item_AddOption( p_child, psz_option, VLC_INPUT_OPTION_TRUSTED );
406 407
            free( psz_option );
        }
408 409
    }
    if( !p_sys->psz_mcast_ip && p_sys->b_rtsp_kasenna )
410 411
    {
        char *psz_option;
412 413
        if( asprintf( &psz_option, "rtsp-kasenna" ) != -1 )
        {
414
            input_item_AddOption( p_child, psz_option, VLC_INPUT_OPTION_TRUSTED );
415 416
            free( psz_option );
        }
417
    }
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
418

419
    input_item_AddSubItem( p_current_input, p_child );
420
    vlc_gc_decref( p_child );
421
    HANDLE_PLAY_AND_RELEASE
422
    return 0; /* Needed for correct operation of go back */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
423 424 425 426
}

static int Control( demux_t *p_demux, int i_query, va_list args )
{
427
    VLC_UNUSED(p_demux); VLC_UNUSED(i_query); VLC_UNUSED(args);
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
428 429 430
    return VLC_EGENERIC;
}