mediacontrol_core.c 13.8 KB
Newer Older
zorglub's avatar
zorglub committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*****************************************************************************
 * core.c: Core functions : init, playlist, stream management
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $Id$
 *
 * Authors: Olivier Aubert <olivier.aubert@liris.univ-lyon1.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
zorglub's avatar
zorglub committed
22 23 24 25 26
 *****************************************************************************/

#include <mediacontrol_internal.h>
#include <vlc/mediacontrol.h>

27
#include <vlc/libvlc.h>
zorglub's avatar
zorglub committed
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
#include <vlc/intf.h>
#include <vlc/vout.h>
#include <vlc/aout.h>
#include <vlc_demux.h>

#include <vlc_osd.h>

#define HAS_SNAPSHOT 1

#ifdef HAS_SNAPSHOT
#include <snapshot.h>
#endif

#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <errno.h>                                                 /* ENOMEM */
#include <stdio.h>
#include <ctype.h>

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#endif

58
mediacontrol_Instance* mediacontrol_new( char** args, mediacontrol_Exception *exception )
zorglub's avatar
zorglub committed
59
{
60 61 62 63 64 65 66 67 68
    mediacontrol_Instance* retval;
    libvlc_exception_t ex;
    char **ppsz_argv;
    int i_count = 0;
    int i_index;
    char **p_tmp;

    libvlc_exception_init( &ex );
    exception=mediacontrol_exception_init( exception );
zorglub's avatar
zorglub committed
69

70 71
    /* Copy args array */
    if( args )
zorglub's avatar
zorglub committed
72
    {
73 74
        for ( p_tmp = args ; *p_tmp != NULL ; p_tmp++ )
            i_count++;
zorglub's avatar
zorglub committed
75 76
    }

77 78
    ppsz_argv = malloc( ( i_count + 2 ) * sizeof( char * ) ) ;
    if( ! ppsz_argv )
zorglub's avatar
zorglub committed
79
    {
80 81 82 83 84 85 86 87 88 89
        RAISE_NULL( mediacontrol_InternalException, "out of memory" );
    }
    ppsz_argv[0] = "vlc";
    for ( i_index = 0; i_index < i_count; i_index++ )
    {
        ppsz_argv[i_index + 1] = strdup( args[i_index] );
        if( ! ppsz_argv[i_index + 1] )
        {
            RAISE_NULL( mediacontrol_InternalException, "out of memory" );
        }
zorglub's avatar
zorglub committed
90 91
    }

92
    ppsz_argv[i_count + 2] = NULL;
zorglub's avatar
zorglub committed
93

94 95 96 97
    retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
    if( ! retval )
    { 
        RAISE_NULL( mediacontrol_InternalException, "out of memory" );
zorglub's avatar
zorglub committed
98
    }
99 100 101 102 103

    retval->p_instance = libvlc_new( i_count + 1, ppsz_argv, &ex );
    retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
    HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
    return retval;  
zorglub's avatar
zorglub committed
104 105
};

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
void
mediacontrol_exit( mediacontrol_Instance *self )
{
    libvlc_exception_t ex;
    libvlc_exception_init( &ex );

    libvlc_destroy( self->p_instance, &ex );
}

libvlc_instance_t*
mediacontrol_get_libvlc_instance( mediacontrol_Instance *self )
{
  return self->p_instance;
}

mediacontrol_Instance *
mediacontrol_new_from_instance( libvlc_instance_t* p_instance,
				mediacontrol_Exception *exception )
{
  mediacontrol_Instance* retval;

  retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
  if( ! retval )
  { 
      RAISE_NULL( mediacontrol_InternalException, "out of memory" );
  }
  retval->p_instance = p_instance;
  retval->p_playlist = retval->p_instance->p_libvlc_int->p_playlist;
  return retval;  
}
zorglub's avatar
zorglub committed
136 137 138 139 140 141 142 143 144 145

/**************************************************************************
 * Playback management
 **************************************************************************/
mediacontrol_Position*
mediacontrol_get_media_position( mediacontrol_Instance *self,
                                 const mediacontrol_PositionOrigin an_origin,
                                 const mediacontrol_PositionKey a_key,
                                 mediacontrol_Exception *exception )
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
146
    mediacontrol_Position* retval = NULL;
147 148 149
    libvlc_exception_t ex;
    vlc_int64_t pos;
    libvlc_input_t * p_input;
zorglub's avatar
zorglub committed
150 151

    exception = mediacontrol_exception_init( exception );
152
    libvlc_exception_init( &ex );
zorglub's avatar
zorglub committed
153 154 155 156 157

    retval = ( mediacontrol_Position* )malloc( sizeof( mediacontrol_Position ) );
    retval->origin = an_origin;
    retval->key = a_key;

158 159
    p_input = libvlc_playlist_get_input( self->p_instance, &ex);
    HANDLE_LIBVLC_EXCEPTION_NULL( &ex );
zorglub's avatar
zorglub committed
160 161 162

    if(  an_origin != mediacontrol_AbsolutePosition )
    {
163
        libvlc_input_free( p_input );
zorglub's avatar
zorglub committed
164
        /* Relative or ModuloPosition make no sense */
165 166
        RAISE_NULL( mediacontrol_PositionOriginNotSupported,
                    "Only absolute position is valid." );
zorglub's avatar
zorglub committed
167 168 169
    }

    /* We are asked for an AbsolutePosition. */
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    pos = libvlc_input_get_time( p_input, &ex );

    if( a_key == mediacontrol_MediaTime )
    {
        retval->value = pos / 1000;
    }
    else
    {
        if( ! self->p_playlist->p_input ) 
        {
            libvlc_input_free( p_input );
            RAISE_NULL( mediacontrol_InternalException,
                        "No input" );
        }
        retval->value = mediacontrol_unit_convert( self->p_playlist->p_input,
                                                   mediacontrol_MediaTime,
                                                   a_key,
                                                   pos / 1000 );
    }
    libvlc_input_free( p_input );
zorglub's avatar
zorglub committed
190 191 192 193 194 195 196 197 198
    return retval;
}

/* Sets the media position */
void
mediacontrol_set_media_position( mediacontrol_Instance *self,
                                 const mediacontrol_Position * a_position,
                                 mediacontrol_Exception *exception )
{
199 200 201
    libvlc_input_t * p_input;
    libvlc_exception_t ex;
    vlc_int64_t i_pos;
zorglub's avatar
zorglub committed
202

203 204 205 206 207 208 209 210 211 212
    libvlc_exception_init( &ex );
    mediacontrol_exception_init( exception );

    p_input = libvlc_playlist_get_input( self->p_instance, &ex);
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );

    i_pos = mediacontrol_position2microsecond( self->p_playlist->p_input, a_position );
    libvlc_input_set_time( p_input, i_pos, &ex );
    libvlc_input_free( p_input );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
zorglub's avatar
zorglub committed
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
}

/* Starts playing a stream */
/*
 * Known issues: since moving in the playlist using playlist_Next
 * or playlist_Prev implies starting to play items, the a_position
 * argument will be only honored for the 1st item in the list.
 * 
 * XXX:FIXME split moving in the playlist and playing items two
 * different actions or make playlist_<Next|Prev> accept a time
 * value to start to play from.
 */
void
mediacontrol_start( mediacontrol_Instance *self,
                    const mediacontrol_Position * a_position,
                    mediacontrol_Exception *exception )
{
    playlist_t * p_playlist = self->p_playlist;

    exception = mediacontrol_exception_init( exception );
    if( ! p_playlist )
    {
        RAISE( mediacontrol_PlaylistException, "No available playlist" );
        return;
    }

    vlc_mutex_lock( &p_playlist->object_lock );
    if( p_playlist->i_size )
    {
        int i_from;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
243
        char *psz_from = NULL;
zorglub's avatar
zorglub committed
244

Jean-Paul Saman's avatar
Jean-Paul Saman committed
245
        psz_from = ( char * )malloc( 20 * sizeof( char ) );
246
        if( psz_from && p_playlist->status.p_item )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
247 248 249 250 251
        {
            i_from = mediacontrol_position2microsecond( p_playlist->p_input, a_position ) / 1000000;

            /* Set start time */
            snprintf( psz_from, 20, "start-time=%i", i_from );
252
            vlc_input_item_AddOption( p_playlist->status.p_item->p_input, psz_from );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
253 254
            free( psz_from );
        }
zorglub's avatar
zorglub committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

        vlc_mutex_unlock( &p_playlist->object_lock );
        playlist_Play( p_playlist );
    }
    else
    {
        RAISE( mediacontrol_PlaylistException, "Empty playlist." );
        vlc_mutex_unlock( &p_playlist->object_lock );
    }
}

void
mediacontrol_pause( mediacontrol_Instance *self,
                    const mediacontrol_Position * a_position,
                    mediacontrol_Exception *exception )
{
    input_thread_t *p_input = self->p_playlist->p_input;

    /* FIXME: use the a_position parameter */
    exception=mediacontrol_exception_init( exception );
    if( p_input != NULL )
    {
        var_SetInteger( p_input, "state", PAUSE_S );
    }
    else
    {
        RAISE( mediacontrol_InternalException, "No input" );
    }
}

void
mediacontrol_resume( mediacontrol_Instance *self,
                     const mediacontrol_Position * a_position,
                     mediacontrol_Exception *exception )
{
    input_thread_t *p_input = self->p_playlist->p_input;

    /* FIXME: use the a_position parameter */
    exception=mediacontrol_exception_init( exception );
    if( p_input != NULL )
    {
        var_SetInteger( p_input, "state", PAUSE_S );
    }
    else
    {
        RAISE( mediacontrol_InternalException, "No input" );
    }
}

void
mediacontrol_stop( mediacontrol_Instance *self,
                   const mediacontrol_Position * a_position,
                   mediacontrol_Exception *exception )
{
    /* FIXME: use the a_position parameter */
    exception=mediacontrol_exception_init( exception );
    if( !self->p_playlist )
    {
        RAISE( mediacontrol_PlaylistException, "No playlist" );
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
315 316
    else
        playlist_Stop( self->p_playlist );
zorglub's avatar
zorglub committed
317 318 319 320 321 322 323 324 325 326 327
}

/**************************************************************************
 * Playlist management
 **************************************************************************/

void
mediacontrol_playlist_add_item( mediacontrol_Instance *self,
                                const char * psz_file,
                                mediacontrol_Exception *exception )
{
328 329 330 331 332 333 334
    libvlc_exception_t ex;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    libvlc_playlist_add( self->p_instance, psz_file, psz_file, &ex );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
zorglub's avatar
zorglub committed
335 336 337 338
}

void
mediacontrol_playlist_next_item( mediacontrol_Instance *self,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
339
                                 mediacontrol_Exception *exception )
zorglub's avatar
zorglub committed
340
{
341 342 343 344 345 346 347
    libvlc_exception_t ex;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    libvlc_playlist_next( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
zorglub's avatar
zorglub committed
348 349 350 351 352 353
}

void
mediacontrol_playlist_clear( mediacontrol_Instance *self,
                             mediacontrol_Exception *exception )
{
354 355 356 357 358 359 360
    libvlc_exception_t ex;

    mediacontrol_exception_init( exception );
    libvlc_exception_init( &ex );

    libvlc_playlist_clear( self->p_instance, &ex );
    HANDLE_LIBVLC_EXCEPTION_VOID( &ex );
zorglub's avatar
zorglub committed
361 362 363 364 365 366
}

mediacontrol_PlaylistSeq *
mediacontrol_playlist_get_list( mediacontrol_Instance *self,
                                mediacontrol_Exception *exception )
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
367
    mediacontrol_PlaylistSeq *retval = NULL;
zorglub's avatar
zorglub committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    int i_index;
    playlist_t * p_playlist = self->p_playlist;
    int i_playlist_size;

    exception=mediacontrol_exception_init( exception );
    if( !p_playlist )
    {
        RAISE( mediacontrol_PlaylistException, "No playlist" );
        return NULL;
    }

    vlc_mutex_lock( &p_playlist->object_lock );
    i_playlist_size = p_playlist->i_size;

    retval = mediacontrol_PlaylistSeq__alloc( i_playlist_size );

    for( i_index = 0 ; i_index < i_playlist_size ; i_index++ )
    {
386
        retval->data[i_index] = strdup( p_playlist->pp_items[i_index]->p_input->psz_uri );
zorglub's avatar
zorglub committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    }
    vlc_mutex_unlock( &p_playlist->object_lock );

    return retval;
}

/***************************************************************************
 * Status feedback
 ***************************************************************************/

mediacontrol_StreamInformation *
mediacontrol_get_stream_information( mediacontrol_Instance *self,
                                     mediacontrol_PositionKey a_key,
                                     mediacontrol_Exception *exception )
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
402
    mediacontrol_StreamInformation *retval = NULL;
zorglub's avatar
zorglub committed
403 404 405
    input_thread_t *p_input = self->p_playlist->p_input;
    vlc_value_t val;

zorglub's avatar
zorglub committed
406 407
    retval = ( mediacontrol_StreamInformation* )
                            malloc( sizeof( mediacontrol_StreamInformation ) );
zorglub's avatar
zorglub committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    if( ! retval )
    {
        RAISE( mediacontrol_InternalException, "Out of memory" );
        return NULL;
    }

    if( ! p_input )
    {
        /* No p_input defined */
        retval->streamstatus = mediacontrol_UndefinedStatus;
        retval->url          = strdup( "None" );
        retval->position     = 0;
        retval->length       = 0;
    }
    else
    {
        switch( var_GetInteger( p_input, "state" ) )
        {
        case PLAYING_S     :
            retval->streamstatus = mediacontrol_PlayingStatus;
            break;
        case PAUSE_S       :
            retval->streamstatus = mediacontrol_PauseStatus;
            break;
        case INIT_S        :
            retval->streamstatus = mediacontrol_InitStatus;
            break;
        case END_S         :
            retval->streamstatus = mediacontrol_EndStatus;
            break;
        default :
            retval->streamstatus = mediacontrol_UndefinedStatus;
            break;
        }

        retval->url = strdup( p_input->input.p_item->psz_uri );

        /* TIME and LENGTH are in microseconds. We want them in ms */
        var_Get( p_input, "time", &val);
        retval->position = val.i_time / 1000;

        var_Get( p_input, "length", &val);
        retval->length = val.i_time / 1000;

        retval->position = mediacontrol_unit_convert( p_input,
zorglub's avatar
zorglub committed
453 454
                                         mediacontrol_MediaTime, a_key,
                                         retval->position );
zorglub's avatar
zorglub committed
455
        retval->length   = mediacontrol_unit_convert( p_input,
zorglub's avatar
zorglub committed
456 457
                                         mediacontrol_MediaTime, a_key,
                                         retval->length );
zorglub's avatar
zorglub committed
458 459 460
    }
    return retval;
}