media_list_player.c 15.4 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
/*****************************************************************************
 * media_list_player.c: libvlc new API media_list player functions
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#include "libvlc_internal.h"
#include <vlc/libvlc.h>
25
#include "media_list_path.h"
26 27 28 29 30 31 32 33 34 35

/*
 * Private functions
 */

/**************************************************************************
 *       get_next_index (private)
 *
 * Simple next item fetcher.
 **************************************************************************/
36 37
static libvlc_media_list_path_t
get_next_path( libvlc_media_list_player_t * p_mlp )
38 39
{
	/* We are entered with libvlc_media_list_lock( p_mlp->p_list ) */
40 41 42 43 44 45 46 47 48 49 50 51 52
	libvlc_media_list_path_t ret;
	libvlc_media_list_t * p_parent_of_playing_item;
	libvlc_media_list_t * p_sublist_of_playing_item;
    p_sublist_of_playing_item = libvlc_media_list_sublist_at_path(
                            p_mlp->p_mlist,
                            p_mlp->current_playing_item_path );
    
    /* If item just gained a sublist just play it */
    if( p_sublist_of_playing_item )
    {
        libvlc_media_list_release( p_sublist_of_playing_item );
        return libvlc_media_list_path_copy_by_appending( p_mlp->current_playing_item_path, 0 );
    }
53

54 55 56 57
    /* Try to catch next element */
    p_parent_of_playing_item = libvlc_media_list_parentlist_at_path(
                            p_mlp->p_mlist,
                            p_mlp->current_playing_item_path );
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    int deepness = libvlc_media_list_path_deepness( p_mlp->current_playing_item_path );
    if( deepness < 1 || !p_parent_of_playing_item )
        return NULL;

    ret = libvlc_media_list_path_copy( p_mlp->current_playing_item_path );

	while( ret[deepness-1] >= libvlc_media_list_count( p_parent_of_playing_item, NULL ) )
	{
        deepness--;
        if( deepness <= 0 )
        {
            free( ret );
            libvlc_media_list_release( p_parent_of_playing_item );
            return NULL;
        }
        ret[deepness] = -1;
        ret[deepness-1]++;
        p_parent_of_playing_item  = libvlc_media_list_parentlist_at_path(
                                        p_mlp->p_mlist,
                                        ret );
    }
    libvlc_media_list_release( p_parent_of_playing_item );
	return ret;
82 83 84 85 86 87 88 89 90 91 92 93
}

/**************************************************************************
 *       media_instance_reached_end (private) (Event Callback)
 **************************************************************************/
static void 
media_instance_reached_end( const libvlc_event_t * p_event,
                            void * p_user_data )
{
	libvlc_media_list_player_t * p_mlp = p_user_data;
	libvlc_media_instance_t * p_mi = p_event->p_obj;
    libvlc_media_descriptor_t *p_md, * p_current_md;
94

95 96
    p_md = libvlc_media_instance_get_media_descriptor( p_mi, NULL );
    /* XXX: need if p_mlp->p_current_playing_index is beyond */
97
    p_current_md = libvlc_media_list_item_at_path(
98
                        p_mlp->p_mlist,
99
                        p_mlp->current_playing_item_path );
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	if( p_md != p_current_md )
	{
		msg_Warn( p_mlp->p_libvlc_instance->p_libvlc_int,
				  "We are not sync-ed with the media instance" );
        libvlc_media_descriptor_release( p_md );
        libvlc_media_descriptor_release( p_current_md );
		return;
	}
    libvlc_media_descriptor_release( p_md );
    libvlc_media_descriptor_release( p_current_md );
	libvlc_media_list_player_next( p_mlp, NULL );
}

/**************************************************************************
 *       playlist_item_deleted (private) (Event Callback)
 **************************************************************************/
static void 
mlist_item_deleted( const libvlc_event_t * p_event, void * p_user_data )
{
    libvlc_media_descriptor_t * p_current_md;    
	libvlc_media_list_player_t * p_mlp = p_user_data;
	libvlc_media_list_t * p_emitting_mlist = p_event->p_obj;
    /* XXX: need if p_mlp->p_current_playing_index is beyond */
123
    p_current_md = libvlc_media_list_item_at_path(
124
                        p_mlp->p_mlist,
125
                        p_mlp->current_playing_item_path );
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

	if( p_event->u.media_list_item_deleted.item == p_current_md &&
	    p_emitting_mlist == p_mlp->p_mlist )
	{
		/* We are playing this item, we choose to stop */
		libvlc_media_list_player_stop( p_mlp, NULL );
	}
}

/**************************************************************************
 *       install_playlist_observer (private)
 **************************************************************************/
static void
install_playlist_observer( libvlc_media_list_player_t * p_mlp )
{
	libvlc_event_attach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
            libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
}

/**************************************************************************
 *       uninstall_playlist_observer (private)
 **************************************************************************/
static void
uninstall_playlist_observer( libvlc_media_list_player_t * p_mlp )
{
    libvlc_event_detach( libvlc_media_list_event_manager( p_mlp->p_mlist, NULL ),
            libvlc_MediaListItemDeleted, mlist_item_deleted, p_mlp, NULL );
}

/**************************************************************************
 *       install_media_instance_observer (private)
 **************************************************************************/
static void
install_media_instance_observer( libvlc_media_list_player_t * p_mlp )
{
	libvlc_event_attach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
                         libvlc_MediaInstanceReachedEnd,
					      media_instance_reached_end, p_mlp, NULL );
}


/**************************************************************************
 *       uninstall_media_instance_observer (private)
 **************************************************************************/
static void
uninstall_media_instance_observer( libvlc_media_list_player_t * p_mlp )
{
	libvlc_event_detach( libvlc_media_instance_event_manager( p_mlp->p_mi, NULL ),
                         libvlc_MediaInstanceReachedEnd,
					     media_instance_reached_end, p_mlp, NULL );
}
177

178 179 180 181 182 183 184
/**************************************************************************
 *       Stop (Public)
 **************************************************************************/
static vlc_bool_t
libvlc_media_list_player_is_playing( libvlc_media_list_player_t * p_mlp,
                                    libvlc_exception_t * p_e )
{
185 186
    //libvlc_exception_raise( p_e, "Unimplemented" );
    return 1;
187 188
}

189
/**************************************************************************
190
 *       set_current_playing_item (private)
191 192 193 194
 *
 * Playlist lock should be held
 **************************************************************************/
static void
195 196 197
set_current_playing_item( libvlc_media_list_player_t * p_mlp,
                          libvlc_media_list_path_t path,
                          libvlc_exception_t * p_e )
198 199 200
{
	libvlc_media_descriptor_t * p_md;
	
201
	p_md = libvlc_media_list_item_at_path( p_mlp->p_mlist, path );
202 203 204 205 206 207
	if( !p_md )
	{
		if( !libvlc_exception_raised( p_e ) )
			libvlc_exception_raise( p_e, "Can't obtain a media" );
		return;
	}
208
    
209 210
    vlc_mutex_lock( &p_mlp->object_lock );
	
211 212
    free( p_mlp->current_playing_item_path );
    p_mlp->current_playing_item_path = path;
213 214 215

	/* We are not interested in getting media_descriptor stop event now */
	uninstall_media_instance_observer( p_mlp );
216 217 218 219 220 221 222 223 224
    if( p_md->p_subitems && libvlc_media_list_count( p_md->p_subitems, NULL ) > 0 )
    {
        libvlc_media_descriptor_t * p_submd;
        p_submd = libvlc_media_list_item_at_index( p_md->p_subitems, 0, NULL );
        libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_submd, NULL );
        libvlc_media_descriptor_release( p_submd );
    }
    else
        libvlc_media_instance_set_media_descriptor( p_mlp->p_mi, p_md, NULL );
225 226 227 228 229 230 231 232 233
//	wait_playing_state(); /* If we want to be synchronous */
	install_media_instance_observer( p_mlp );

    vlc_mutex_unlock( &p_mlp->object_lock );

	libvlc_media_list_unlock( p_mlp->p_mlist );		
	
	libvlc_media_descriptor_release( p_md ); /* for libvlc_media_list_item_at_index */
}
234 235 236 237 238 239

/*
 * Public libvlc functions
 */

/**************************************************************************
240
 *         new (Public)
241 242 243 244 245 246 247 248
 **************************************************************************/
libvlc_media_list_player_t *
libvlc_media_list_player_new( libvlc_instance_t * p_instance,
                              libvlc_exception_t * p_e )
{
    (void)p_e;
    libvlc_media_list_player_t * p_mlp;
    p_mlp = malloc(sizeof(libvlc_media_list_player_t));
249
	p_mlp->current_playing_item_path = NULL;
250 251
    p_mlp->p_mi = NULL;
    p_mlp->p_mlist = NULL;
252
    vlc_mutex_init( p_instance->p_libvlc_int, &p_mlp->object_lock );
253 254 255 256 257 258 259 260
    p_mlp->p_event_manager = libvlc_event_manager_new( p_mlp,
                                                       p_instance,
                                                       p_e );
    libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
            libvlc_MediaListPlayerNextItemSet, p_e );
    libvlc_event_manager_register_event_type( p_mlp->p_event_manager,
            libvlc_MediaListPlayerNextItemSet, p_e );

261 262 263 264
	return p_mlp;
}

/**************************************************************************
265
 *         release (Public)
266 267 268 269 270 271 272
 **************************************************************************/
void libvlc_media_list_player_release( libvlc_media_list_player_t * p_mlp )
{
    free(p_mlp);
}

/**************************************************************************
273
 *        set_media_instance (Public)
274 275 276 277 278 279 280 281 282 283
 **************************************************************************/
void libvlc_media_list_player_set_media_instance(
                                     libvlc_media_list_player_t * p_mlp,
                                     libvlc_media_instance_t * p_mi,
                                     libvlc_exception_t * p_e )
{
    vlc_mutex_lock( &p_mlp->object_lock );

	if( p_mlp->p_mi )
	{
284
		uninstall_media_instance_observer( p_mlp );
285 286 287 288 289 290 291 292 293 294 295
		libvlc_media_instance_release( p_mlp->p_mi );
	}
	libvlc_media_instance_retain( p_mi );
	p_mlp->p_mi = p_mi;

    install_media_instance_observer( p_mlp );

    vlc_mutex_unlock( &p_mlp->object_lock );
}

/**************************************************************************
296
 *       set_media_list (Public)
297 298 299 300 301 302 303 304 305
 **************************************************************************/
void libvlc_media_list_player_set_media_list(
                                     libvlc_media_list_player_t * p_mlp,
                                     libvlc_media_list_t * p_mlist,
                                     libvlc_exception_t * p_e )
{
    vlc_mutex_lock( &p_mlp->object_lock );
    
	if( libvlc_media_list_player_is_playing( p_mlp, p_e ) )
306 307 308 309 310
    {
        libvlc_media_instance_stop( p_mlp->p_mi, p_e );
        /* Don't bother if there was an error. */
        libvlc_exception_clear( p_e );
    }
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	if( p_mlp->p_mlist )
	{
		uninstall_playlist_observer( p_mlp );
		libvlc_media_list_release( p_mlp->p_mlist );
	}
	libvlc_media_list_retain( p_mlist );
	p_mlp->p_mlist = p_mlist;
    
	install_playlist_observer( p_mlp );

    vlc_mutex_unlock( &p_mlp->object_lock );
}

/**************************************************************************
 *        Play (Public)
 **************************************************************************/
void libvlc_media_list_player_play( libvlc_media_list_player_t * p_mlp,
                                  libvlc_exception_t * p_e )
{
331
    if( !p_mlp->current_playing_item_path )
332 333
    {
        libvlc_media_list_player_next( p_mlp, p_e );
334
        return; /* Will set to play */
335
    }
336 337 338 339

	libvlc_media_instance_play( p_mlp->p_mi, p_e );
}

340 341 342 343 344 345 346 347
/**************************************************************************
 *        Play item at index (Public)
 **************************************************************************/
void libvlc_media_list_player_play_item_at_index(
                        libvlc_media_list_player_t * p_mlp,
                        int i_index,
                        libvlc_exception_t * p_e )
{
348
	set_current_playing_item( p_mlp, libvlc_media_list_path_with_root_index(i_index), p_e );
349 350 351 352

	if( libvlc_exception_raised( p_e ) )
		return;

353 354 355 356 357
    /* Send the next item event */
    libvlc_event_t event;
    event.type = libvlc_MediaListPlayerNextItemSet;
    libvlc_event_send( p_mlp->p_event_manager, &event );

358 359 360
	libvlc_media_instance_play( p_mlp->p_mi, p_e );
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
/**************************************************************************
 *        Play item (Public)
 **************************************************************************/
void libvlc_media_list_player_play_item(
                        libvlc_media_list_player_t * p_mlp,
                        libvlc_media_descriptor_t * p_md,
                        libvlc_exception_t * p_e )
{
	libvlc_media_list_path_t path = libvlc_media_list_path_of_item( p_mlp->p_mlist, p_md );
    if( !path )
    {
        libvlc_exception_raise( p_e, "No such item in media list" );
        return;
    }
	set_current_playing_item( p_mlp, path, p_e );

	if( libvlc_exception_raised( p_e ) )
		return;

	libvlc_media_instance_play( p_mlp->p_mi, p_e );
}
382

383 384 385 386 387 388 389 390 391
/**************************************************************************
 *       Stop (Public)
 **************************************************************************/
void libvlc_media_list_player_stop( libvlc_media_list_player_t * p_mlp,
                                    libvlc_exception_t * p_e )
{
	libvlc_media_instance_stop( p_mlp->p_mi, p_e );

    vlc_mutex_lock( &p_mlp->object_lock );
392
	p_mlp->current_playing_item_path = NULL;
393 394 395 396 397 398 399 400
    vlc_mutex_unlock( &p_mlp->object_lock );
}

/**************************************************************************
 *       Next (Public)
 **************************************************************************/
void libvlc_media_list_player_next( libvlc_media_list_player_t * p_mlp,
                                    libvlc_exception_t * p_e )
401
{	
402
	libvlc_media_list_path_t path;
403 404 405
	
	libvlc_media_list_lock( p_mlp->p_mlist );

406
	path = get_next_path( p_mlp );
407

408
	if( !path )
409 410 411 412 413 414 415
	{
		libvlc_media_list_unlock( p_mlp->p_mlist );
		libvlc_exception_raise( p_e, "No more element to play" );
		libvlc_media_list_player_stop( p_mlp, p_e );
		return;
	}

416
	set_current_playing_item( p_mlp, path, p_e );
417
	
418 419
    libvlc_media_instance_play( p_mlp->p_mi, p_e );

420
    libvlc_media_list_unlock( p_mlp->p_mlist );
421 422 423 424 425

    /* Send the next item event */
    libvlc_event_t event;
    event.type = libvlc_MediaListPlayerNextItemSet;
    libvlc_event_send( p_mlp->p_event_manager, &event);
426
}