playlist.c 29.2 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * playlist.c : Playlist management functions
 *****************************************************************************
 * Copyright (C) 1999-2001 VideoLAN
5
 * $Id: playlist.c,v 1.52 2003/09/15 00:01:49 fenrir Exp $
6
7
8
9
10
11
12
 *
 * Authors: Samuel Hocevar <sam@zoy.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.
13
 *
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * 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.
 *****************************************************************************/
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
#include <string.h>                                            /* strerror() */

#include <vlc/vlc.h>
28
29
#include <vlc/vout.h>
#include <vlc/sout.h>
30
31
32
33

#include "stream_control.h"
#include "input_ext-intf.h"

34
#include "vlc_playlist.h"
35

36
37
#define PLAYLIST_FILE_HEADER_0_5  "# vlc playlist file version 0.5"

38
39
40
41
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static void RunThread ( playlist_t * );
42
43
44
45
static void SkipItem  ( playlist_t *, int );
static void PlayItem  ( playlist_t * );

static void Poubellize ( playlist_t *, input_thread_t * );
46

sigmunau's avatar
sigmunau committed
47
48
49
/**
 * Create playlist
 *
50
 * Create a playlist structure.
sigmunau's avatar
sigmunau committed
51
52
53
 * \param p_parent the vlc object that is to be the parent of this playlist
 * \return a pointer to the created playlist, or NULL on error
 */
54
playlist_t * __playlist_Create ( vlc_object_t *p_parent )
55
56
{
    playlist_t *p_playlist;
hartman's avatar
hartman committed
57
    vlc_value_t     val;
58
59
60
61
62
63
64
65
66

    /* Allocate structure */
    p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
    if( !p_playlist )
    {
        msg_Err( p_parent, "out of memory" );
        return NULL;
    }

hartman's avatar
hartman committed
67
68
69
70
    var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
    val.b_bool = VLC_TRUE;
    var_Set( p_playlist, "intf-change", val );

71
    var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
gbazin's avatar
   
gbazin committed
72

73
74
75
76
    var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
    val.b_bool = VLC_TRUE;
    var_Set( p_playlist, "intf-show", val );

77
    p_playlist->p_input = NULL;
78
    p_playlist->i_status = PLAYLIST_STOPPED;
79
80
81
    p_playlist->i_index = -1;
    p_playlist->i_size = 0;
    p_playlist->pp_items = NULL;
Sam Hocevar's avatar
Sam Hocevar committed
82

83
84
    if( vlc_thread_create( p_playlist, "playlist", RunThread,
                           VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
85
86
87
88
89
90
    {
        msg_Err( p_playlist, "cannot spawn playlist thread" );
        vlc_object_destroy( p_playlist );
        return NULL;
    }

91
92
93
    /* The object has been initialized, now attach it */
    vlc_object_attach( p_playlist, p_parent );

94
95
96
    return p_playlist;
}

sigmunau's avatar
sigmunau committed
97
98
99
/**
 * Destroy the playlist.
 *
100
 * Delete all items in the playlist and free the playlist structure.
sigmunau's avatar
sigmunau committed
101
102
 * \param p_playlist the playlist structure to destroy
 */
103
104
105
106
107
108
void playlist_Destroy( playlist_t * p_playlist )
{
    p_playlist->b_die = 1;

    vlc_thread_join( p_playlist );

hartman's avatar
hartman committed
109
110
    var_Destroy( p_playlist, "intf-change" );

111
112
113
    vlc_object_destroy( p_playlist );
}

sigmunau's avatar
sigmunau committed
114
115
116
117
118
119
/**
 * Add an MRL to the playlist. This is a simplified version of
 * playlist_AddExt inculded for convenince. It equals calling playlist_AddExt
 * with psz_name == psz_target and i_duration == -1
 */

gbazin's avatar
   
gbazin committed
120
121
122
int playlist_Add( playlist_t *p_playlist, const char *psz_target,
                  const char **ppsz_options, int i_options,
                  int i_mode, int i_pos )
123
{
sigmunau's avatar
sigmunau committed
124
125
    return playlist_AddExt( p_playlist, psz_target, psz_target, -1,
                            ppsz_options, i_options, i_mode, i_pos );
126
}
127

sigmunau's avatar
sigmunau committed
128
129
130
131
/**
 * Add a MRL into the playlist.
 *
 * \param p_playlist the playlist to add into
sigmunau's avatar
sigmunau committed
132
 * \param psz_uri the mrl to add to the playlist
sigmunau's avatar
sigmunau committed
133
134
135
 * \param psz_name a text giving a name or description of this item
 * \param i_duration a hint about the duration of this item, in miliseconds, or
 *        -1 if unknown.
sigmunau's avatar
sigmunau committed
136
137
 * \param ppsz_options array of options
 * \param i_options number of items in ppsz_options
sigmunau's avatar
sigmunau committed
138
 * \param i_mode the mode used when adding
139
 * \param i_pos the position in the playlist where to add. If this is
sigmunau's avatar
sigmunau committed
140
141
142
143
144
145
146
147
 *        PLAYLIST_END the item will be added at the end of the playlist
 *        regardless of it's size
 * \return always returns 0
*/
int playlist_AddExt( playlist_t *p_playlist, const char * psz_uri,
                     const char * psz_name, mtime_t i_duration,
                     const char **ppsz_options, int i_options, int i_mode,
                     int i_pos )
zorglub's avatar
   
zorglub committed
148
149
150
151
152
153
154
155
156
157
158
{
    playlist_item_t * p_item;

    p_item = malloc( sizeof( playlist_item_t ) );
    if( p_item == NULL )
    {
        msg_Err( p_playlist, "out of memory" );
    }

    p_item->psz_name = strdup( psz_name );
    p_item->psz_uri  = strdup( psz_uri );
sigmunau's avatar
sigmunau committed
159
    p_item->i_duration = i_duration;
zorglub's avatar
   
zorglub committed
160
161
162
163
    p_item->i_type = 0;
    p_item->i_status = 0;
    p_item->b_autodeletion = VLC_FALSE;

gbazin's avatar
   
gbazin committed
164
165
166
167
168
169
170
171
172
173
174
175
176
    p_item->ppsz_options = NULL;
    p_item->i_options = i_options;

    if( i_options )
    {
        int i;

        p_item->ppsz_options = (char **)malloc( i_options * sizeof(char *) );
        for( i = 0; i < i_options; i++ )
            p_item->ppsz_options[i] = strdup( ppsz_options[i] );

    }

zorglub's avatar
   
zorglub committed
177
178
    return playlist_AddItem( p_playlist, p_item, i_mode, i_pos );
}
179

sigmunau's avatar
sigmunau committed
180
181
182
183
184
185
186
187
188
189
190
/**
 * Add a playlist item into a playlist
 *
 * \param p_playlist the playlist to insert into
 * \param p_item the playlist item to insert
 * \param i_mode the mode used when adding
 * \param i_pos the possition in the playlist where to add. If this is
 *        PLAYLIST_END the item will be added at the end of the playlist
 *        regardless of it's size
 * \return always returns 0
*/
191
192
193
int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
                int i_mode, int i_pos)
{
hartman's avatar
hartman committed
194
    vlc_value_t     val;
195

196
197
198
    vlc_mutex_lock( &p_playlist->object_lock );

    /*
199
     * CHECK_INSERT : checks if the item is already enqued before
200
201
202
203
204
     * enqueing it
     */
    if ( i_mode & PLAYLIST_CHECK_INSERT )
    {
         int j;
205

206
207
208
209
         if ( p_playlist->pp_items )
         {
             for ( j = 0; j < p_playlist->i_size; j++ )
             {
210
                 if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
211
                 {
212
                      if( p_item->psz_name )
213
214
215
216
217
218
219
220
                      {
                          free( p_item->psz_name );
                      }
                      if( p_item->psz_uri )
                      {
                          free( p_item->psz_uri );
                      }
                      free( p_item );
221
                      vlc_mutex_unlock( &p_playlist->object_lock );
222
                      return 0;
223
224
225
226
227
228
229
                 }
             }
         }
         i_mode &= ~PLAYLIST_CHECK_INSERT;
         i_mode |= PLAYLIST_APPEND;
    }

230

zorglub's avatar
   
zorglub committed
231
    msg_Dbg( p_playlist, "adding playlist item  %s  ( %s )", p_item->psz_name, p_item->psz_uri);
232

233
234
    /* Create the new playlist item */

235

236
237
238
    /* Do a few boundary checks and allocate space for the item */
    if( i_pos == PLAYLIST_END )
    {
239
240
241
242
243
244
        if( i_mode & PLAYLIST_INSERT )
        {
            i_mode &= ~PLAYLIST_INSERT;
            i_mode |= PLAYLIST_APPEND;
        }

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        i_pos = p_playlist->i_size - 1;
    }

    if( !(i_mode & PLAYLIST_REPLACE)
         || i_pos < 0 || i_pos >= p_playlist->i_size )
    {
        /* Additional boundary checks */
        if( i_mode & PLAYLIST_APPEND )
        {
            i_pos++;
        }

        if( i_pos < 0 )
        {
            i_pos = 0;
        }
261
        else if( i_pos > p_playlist->i_size )
262
        {
263
            i_pos = p_playlist->i_size;
264
265
        }

266
267
268
269
        INSERT_ELEM( p_playlist->pp_items,
                     p_playlist->i_size,
                     i_pos,
                     p_item );
270

271
272
        if( p_playlist->i_index >= i_pos )
        {
273
            p_playlist->i_index++;
274
275
276
277
278
        }
    }
    else
    {
        /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
279
280
281
282
283
284
285
286
        if( p_playlist->pp_items[i_pos]->psz_name )
        {
            free( p_playlist->pp_items[i_pos]->psz_name );
        }
        if( p_playlist->pp_items[i_pos]->psz_uri )
        {
            free( p_playlist->pp_items[i_pos]->psz_uri );
        }
287
        /* XXX: what if the item is still in use? */
288
289
        free( p_playlist->pp_items[i_pos] );
        p_playlist->pp_items[i_pos] = p_item;
290
291
    }

292
293
294
295
296
297
298
299
300
    if( i_mode & PLAYLIST_GO )
    {
        p_playlist->i_index = i_pos;
        if( p_playlist->p_input )
        {
            input_StopThread( p_playlist->p_input );
        }
        p_playlist->i_status = PLAYLIST_RUNNING;
    }
Sam Hocevar's avatar
Sam Hocevar committed
301

302
    vlc_mutex_unlock( &p_playlist->object_lock );
303

hartman's avatar
hartman committed
304
305
306
    val.b_bool = VLC_TRUE;
    var_Set( p_playlist, "intf-change", val );

307
308
309
    return 0;
}

sigmunau's avatar
sigmunau committed
310
311
312
313
314
315
316
/**
 * delete an item from a playlist.
 *
 * \param p_playlist the playlist to remove from.
 * \param i_pos the position of the item to remove
 * \return returns 0
 */
317
318
int playlist_Delete( playlist_t * p_playlist, int i_pos )
{
hartman's avatar
hartman committed
319
    vlc_value_t     val;
320
    vlc_mutex_lock( &p_playlist->object_lock );
Sam Hocevar's avatar
Sam Hocevar committed
321

322
323
    if( i_pos >= 0 && i_pos < p_playlist->i_size )
    {
324
325
        msg_Dbg( p_playlist, "deleting playlist item  %s ",
                             p_playlist->pp_items[i_pos]->psz_name );
326

327
328
329
330
331
332
333
334
        if( p_playlist->pp_items[i_pos]->psz_name )
        {
            free( p_playlist->pp_items[i_pos]->psz_name );
        }
        if( p_playlist->pp_items[i_pos]->psz_uri )
        {
            free( p_playlist->pp_items[i_pos]->psz_uri );
        }
gbazin's avatar
   
gbazin committed
335
336
337
338
339
340
341
342
343
        if( p_playlist->pp_items[i_pos]->i_options )
        {
            int i;

            for( i = 0; i < p_playlist->pp_items[i_pos]->i_options; i++ )
                free( p_playlist->pp_items[i_pos]->ppsz_options[i] );

            free( p_playlist->pp_items[i_pos]->ppsz_options );
        }
344

345
        /* XXX: what if the item is still in use? */
346
        free( p_playlist->pp_items[i_pos] );
347

gbazin's avatar
   
gbazin committed
348
        if( i_pos <= p_playlist->i_index )
349
350
351
352
353
        {
            p_playlist->i_index--;
        }

        /* Renumber the playlist */
354
355
356
        REMOVE_ELEM( p_playlist->pp_items,
                     p_playlist->i_size,
                     i_pos );
357
358
    }

359
    vlc_mutex_unlock( &p_playlist->object_lock );
hartman's avatar
hartman committed
360
361
362
363
364
365
366

    val.b_bool = VLC_TRUE;
    var_Set( p_playlist, "intf-change", val );

    return 0;
}

zorglub's avatar
zorglub committed
367
368
369
370
371
372
373

/**
 * Sort the playlist
 *
 */
int playlist_Sort( playlist_t * p_playlist , int i_type )
{
374
    int i , i_small , i_position;
zorglub's avatar
zorglub committed
375
376
377
    playlist_item_t *p_temp;

    vlc_mutex_lock( &p_playlist->object_lock );
378

zorglub's avatar
zorglub committed
379
380
381
382
383
    for( i_position = 0; i_position < p_playlist->i_size -1 ; i_position ++ )
    {
        i_small  = i_position;
        for( i = i_position + 1 ; i<  p_playlist->i_size ; i++)
        {
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
            int i_test;

            i_test = strcasecmp( p_playlist->pp_items[i]->psz_name,
                                 p_playlist->pp_items[i_small]->psz_name );

            if( i_type == SORT_NORMAL  && i_test < 0 ||
                i_type == SORT_REVERSE && i_test > 0 )
            {
                i_small = i;
            }

            p_temp = p_playlist->pp_items[i_position];
            p_playlist->pp_items[i_position] = p_playlist->pp_items[i_small];
            p_playlist->pp_items[i_small] = p_temp;
        }
    }

    for( i=0; i < p_playlist->i_size; i++ )
    {
        msg_Dbg( p_playlist, "%s", p_playlist->pp_items[i]->psz_name );
    }
zorglub's avatar
zorglub committed
405
406
407

    vlc_mutex_unlock( &p_playlist->object_lock );

408
    return 0;
zorglub's avatar
zorglub committed
409
410
}

sigmunau's avatar
sigmunau committed
411
412
413
/**
 * Move an item in a playlist
 *
hartman's avatar
hartman committed
414
415
 * Move the item in the playlist with position i_pos before the current item
 * at position i_newpos.
sigmunau's avatar
sigmunau committed
416
417
418
419
420
421
 * \param p_playlist the playlist to move items in
 * \param i_pos the position of the item to move
 * \param i_newpos the position of the item that will be behind the moved item
 *        after the move
 * \return returns 0
 */
hartman's avatar
hartman committed
422
423
424
425
426
427
428
429
430
431
432
int playlist_Move( playlist_t * p_playlist, int i_pos, int i_newpos)
{
    vlc_value_t     val;
    vlc_mutex_lock( &p_playlist->object_lock );

    /* take into account that our own row disappears. */
    if ( i_pos < i_newpos ) i_newpos--;

    if( i_pos >= 0 && i_newpos >=0 && i_pos <= p_playlist->i_size 
                     && i_newpos <= p_playlist->i_size )
    {
gbazin's avatar
   
gbazin committed
433
434
        playlist_item_t * temp;

hartman's avatar
hartman committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        msg_Dbg( p_playlist, "moving playlist item  %s ",
                             p_playlist->pp_items[i_pos]->psz_name );

        if( i_pos == p_playlist->i_index )
        {
            p_playlist->i_index = i_newpos;
        }
        else if( i_pos > p_playlist->i_index && i_newpos <= p_playlist->i_index )
        {
            p_playlist->i_index++;
        }
        else if( i_pos < p_playlist->i_index && i_newpos >= p_playlist->i_index )
        {
            p_playlist->i_index--;
        }
gbazin's avatar
   
gbazin committed
450

hartman's avatar
hartman committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
        if ( i_pos < i_newpos )
        {
            temp = p_playlist->pp_items[i_pos];
            while ( i_pos < i_newpos )
            {
                p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos+1];
                i_pos++;
            }
            p_playlist->pp_items[i_newpos] = temp;
        }
        else if ( i_pos > i_newpos )
        {
            temp = p_playlist->pp_items[i_pos];
            while ( i_pos > i_newpos )
            {
                p_playlist->pp_items[i_pos] = p_playlist->pp_items[i_pos-1];
                i_pos--;
            }
            p_playlist->pp_items[i_newpos] = temp;
        }
    }

    vlc_mutex_unlock( &p_playlist->object_lock );
Sam Hocevar's avatar
Sam Hocevar committed
474

hartman's avatar
hartman committed
475
476
477
    val.b_bool = VLC_TRUE;
    var_Set( p_playlist, "intf-change", val );

478
479
480
    return 0;
}

sigmunau's avatar
sigmunau committed
481
482
/**
 * Do a playlist action
483
 *
sigmunau's avatar
sigmunau committed
484
485
486
487
488
489
 * \param p_playlist the playlist to do the command on
 * \param i_command the command to do
 * \param i_arg the argument to the command. See playlist_command_t for details
 */
 void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
                       int i_arg )
490
{
491
492
    vlc_value_t val;

493
    vlc_mutex_lock( &p_playlist->object_lock );
Sam Hocevar's avatar
Sam Hocevar committed
494

495
496
497
498
    switch( i_command )
    {
    case PLAYLIST_STOP:
        p_playlist->i_status = PLAYLIST_STOPPED;
499
500
501
502
        if( p_playlist->p_input )
        {
            input_StopThread( p_playlist->p_input );
        }
503
        break;
504

505
506
    case PLAYLIST_PLAY:
        p_playlist->i_status = PLAYLIST_RUNNING;
gbazin's avatar
   
gbazin committed
507
        if( !p_playlist->p_input )
508
        {
509
            PlayItem( p_playlist );
gbazin's avatar
   
gbazin committed
510
511
512
        }
        if( p_playlist->p_input )
        {
513
514
            val.i_int = PLAYING_S;
            var_Set( p_playlist->p_input, "state", val );
515
        }
516
        break;
517

518
519
520
521
    case PLAYLIST_PAUSE:
        p_playlist->i_status = PLAYLIST_PAUSED;
        if( p_playlist->p_input )
        {
522
523
            val.i_int = PAUSE_S;
            var_Set( p_playlist->p_input, "state", val );
524
525
526
        }
        break;

527
    case PLAYLIST_SKIP:
528
529
530
        p_playlist->i_status = PLAYLIST_STOPPED;
        SkipItem( p_playlist, i_arg );
        if( p_playlist->p_input )
531
        {
532
533
534
535
536
537
538
539
540
541
542
543
544
            input_StopThread( p_playlist->p_input );
        }
        p_playlist->i_status = PLAYLIST_RUNNING;
        break;

    case PLAYLIST_GOTO:
        if( i_arg >= 0 && i_arg < p_playlist->i_size )
        {
            p_playlist->i_index = i_arg;
            if( p_playlist->p_input )
            {
                input_StopThread( p_playlist->p_input );
            }
545
546
547
            p_playlist->i_status = PLAYLIST_RUNNING;
        }
        break;
548

549
    default:
550
        msg_Err( p_playlist, "unknown playlist command" );
551
552
553
        break;
    }

554
    vlc_mutex_unlock( &p_playlist->object_lock );
Sam Hocevar's avatar
Sam Hocevar committed
555

556
557
558
559
    return;
}
/* Following functions are local */

560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
static void ObjectGarbageCollector( playlist_t *p_playlist,
                                    int i_type,
                                    vlc_bool_t *pb_obj_destroyed,
                                    mtime_t *pi_obj_destroyed_date )
{
    vlc_object_t *p_obj;
    if( *pb_obj_destroyed || *pi_obj_destroyed_date > mdate() )
    {
        return;
    }

    if( *pi_obj_destroyed_date == 0 )
    {
        /* give a little time */
        *pi_obj_destroyed_date = mdate() + 300000LL;
    }
    else
    {
        while( ( p_obj = vlc_object_find( p_playlist,
                                           i_type,
                                           FIND_CHILD ) ) )
        {
            if( p_obj->p_parent != (vlc_object_t*)p_playlist )
            {
                /* only first chiled (ie unused) */
                vlc_object_release( p_obj );
                break;
            }
            if( i_type == VLC_OBJECT_VOUT )
            {
                msg_Dbg( p_playlist, "vout garbage collector destroying 1 vout" );
                vlc_object_detach( p_obj );
                vlc_object_release( p_obj );
                vout_Destroy( (vout_thread_t *)p_obj );
            }
            else if( i_type == VLC_OBJECT_SOUT )
            {
                vlc_object_release( p_obj );
                sout_DeleteInstance( (sout_instance_t*)p_obj );
            }
        }
        *pb_obj_destroyed = VLC_TRUE;
    }
}

605
606
607
608
609
/*****************************************************************************
 * RunThread: main playlist thread
 *****************************************************************************/
static void RunThread ( playlist_t *p_playlist )
{
610
611
612
613
614
615
616
    vlc_object_t *p_obj;
    vlc_bool_t b_vout_destroyed = VLC_FALSE; /*we do vout garbage collector */
    mtime_t    i_vout_destroyed_date = 0;

    vlc_bool_t b_sout_destroyed = VLC_FALSE; /*we do vout garbage collector */
    mtime_t    i_sout_destroyed_date = 0;

617
618
619
    /* Tell above that we're ready */
    vlc_thread_ready( p_playlist );

620
621
    while( !p_playlist->b_die )
    {
622
623
        vlc_mutex_lock( &p_playlist->object_lock );

624
625
626
        /* If there is an input, check that it doesn't need to die. */
        if( p_playlist->p_input )
        {
Sam Hocevar's avatar
Sam Hocevar committed
627
            /* This input is dead. Remove it ! */
Sam Hocevar's avatar
Sam Hocevar committed
628
            if( p_playlist->p_input->b_dead )
629
630
631
632
633
            {
                input_thread_t *p_input;

                p_input = p_playlist->p_input;
                p_playlist->p_input = NULL;
634
635
636
637

                /* Release the playlist lock, because we may get stuck
                 * in input_DestroyThread() for some time. */
                vlc_mutex_unlock( &p_playlist->object_lock );
638
639
640

                /* Destroy input */
                input_DestroyThread( p_input );
641
642
643
644
645

                /* Unlink current input (_after_ input_DestroyThread for vout garbage collector)*/
                vlc_object_detach( p_input );

                /* Destroy object */
Sam Hocevar's avatar
Sam Hocevar committed
646
                vlc_object_destroy( p_input );
647
648
649
650
651

                b_vout_destroyed = VLC_FALSE;
                i_vout_destroyed_date = 0;
                b_sout_destroyed = VLC_FALSE;
                i_sout_destroyed_date = 0;
652
                continue;
653
            }
Sam Hocevar's avatar
Sam Hocevar committed
654
655
656
657
658
659
            /* This input is dying, let him do */
            else if( p_playlist->p_input->b_die )
            {
                ;
            }
            /* This input has finished, ask him to die ! */
Sam Hocevar's avatar
Sam Hocevar committed
660
661
            else if( p_playlist->p_input->b_error
                      || p_playlist->p_input->b_eof )
662
            {
gbazin's avatar
   
gbazin committed
663
664
665
                /* Check for autodeletion */
                if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
                {
gbazin's avatar
   
gbazin committed
666
                    vlc_mutex_unlock( &p_playlist->object_lock );
gbazin's avatar
   
gbazin committed
667
                    playlist_Delete( p_playlist, p_playlist->i_index );
gbazin's avatar
   
gbazin committed
668
                    vlc_mutex_lock( &p_playlist->object_lock );
gbazin's avatar
   
gbazin committed
669
670
671
672
673
                }

                /* Select the next playlist item */
                SkipItem( p_playlist, 1 );

Sam Hocevar's avatar
Sam Hocevar committed
674
                input_StopThread( p_playlist->p_input );
675
                vlc_mutex_unlock( &p_playlist->object_lock );
676
                continue;
677
            }
678
679
680
681
682
683
684
            else if( p_playlist->p_input->stream.control.i_status != INIT_S )
            {
                ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
                                        &b_vout_destroyed, &i_vout_destroyed_date );
                ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
                                        &b_sout_destroyed, &i_sout_destroyed_date );
            }
685
686
687
        }
        else if( p_playlist->i_status != PLAYLIST_STOPPED )
        {
688
            SkipItem( p_playlist, 0 );
689
            PlayItem( p_playlist );
690
        }
691
692
693
694
        else if( p_playlist->i_status == PLAYLIST_STOPPED )
        {
            ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
                                    &b_sout_destroyed, &i_sout_destroyed_date );
695
696
            ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
                                    &b_vout_destroyed, &i_vout_destroyed_date );
697
        }
698
699
        vlc_mutex_unlock( &p_playlist->object_lock );

700
701
702
703
        msleep( INTF_IDLE_SLEEP );
    }

    /* If there is an input, kill it */
704
    while( 1 )
705
    {
706
707
708
709
710
711
712
713
        vlc_mutex_lock( &p_playlist->object_lock );

        if( p_playlist->p_input == NULL )
        {
            vlc_mutex_unlock( &p_playlist->object_lock );
            break;
        }

Sam Hocevar's avatar
Sam Hocevar committed
714
        if( p_playlist->p_input->b_dead )
715
716
717
718
719
720
        {
            input_thread_t *p_input;

            /* Unlink current input */
            p_input = p_playlist->p_input;
            p_playlist->p_input = NULL;
721
            vlc_mutex_unlock( &p_playlist->object_lock );
722
723
724

            /* Destroy input */
            input_DestroyThread( p_input );
725
726
727
728
729
            /* Unlink current input (_after_ input_DestroyThread for vout
             * garbage collector)*/
            vlc_object_detach( p_input );

            /* Destroy object */
Sam Hocevar's avatar
Sam Hocevar committed
730
            vlc_object_destroy( p_input );
731
            continue;
732
        }
Sam Hocevar's avatar
Sam Hocevar committed
733
734
        else if( p_playlist->p_input->b_die )
        {
735
            /* This input is dying, leave him alone */
Sam Hocevar's avatar
Sam Hocevar committed
736
737
            ;
        }
Sam Hocevar's avatar
Sam Hocevar committed
738
        else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
739
        {
Sam Hocevar's avatar
Sam Hocevar committed
740
            input_StopThread( p_playlist->p_input );
741
            vlc_mutex_unlock( &p_playlist->object_lock );
742
            continue;
743
744
745
746
747
748
        }
        else
        {
            p_playlist->p_input->b_eof = 1;
        }

749
750
        vlc_mutex_unlock( &p_playlist->object_lock );

751
752
        msleep( INTF_IDLE_SLEEP );
    }
753

754
    /* close all remaining sout */
755
    while( ( p_obj = vlc_object_find( p_playlist,
756
                                      VLC_OBJECT_SOUT, FIND_CHILD ) ) )
757
758
    {
        vlc_object_release( p_obj );
759
        sout_DeleteInstance( (sout_instance_t*)p_obj );
760
    }
761
762

    /* close all remaining vout */
763
    while( ( p_obj = vlc_object_find( p_playlist,
764
                                      VLC_OBJECT_VOUT, FIND_CHILD ) ) )
765
    {
766
        vlc_object_detach( p_obj );
767
        vlc_object_release( p_obj );
768
        vout_Destroy( (vout_thread_t *)p_obj );
769
    }
770
771
}

772
773
774
775
776
777
778
779
780
/*****************************************************************************
 * SkipItem: go to Xth playlist item
 *****************************************************************************
 * This function calculates the position of the next playlist item, depending
 * on the playlist course mode (forward, backward, random...).
 *****************************************************************************/
static void SkipItem( playlist_t *p_playlist, int i_arg )
{
    int i_oldindex = p_playlist->i_index;
781
    vlc_bool_t b_random;
782
    vlc_value_t val;
783
784
785
786
787
788
789
790

    /* If the playlist is empty, there is no current item */
    if( p_playlist->i_size == 0 )
    {
        p_playlist->i_index = -1;
        return;
    }

791
792
    b_random = config_GetInt( p_playlist, "random" );

793
    /* Increment */
794
    if( b_random )
795
    {
796
        srand( (unsigned int)mdate() );
797
798
799
800
801
802
803
804

        /* Simple random stuff - we cheat a bit to minimize the chances to
         * get the same index again. */
        i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
        if( i_arg == 0 )
        {
            i_arg = (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
        }
805
    }
806

807
808
    p_playlist->i_index += i_arg;

809
810
811
812
    /* Boundary check */
    if( p_playlist->i_index >= p_playlist->i_size )
    {
        if( p_playlist->i_status == PLAYLIST_STOPPED
813
             || b_random
814
815
             || config_GetInt( p_playlist, "loop" ) )
        {
816
817
            p_playlist->i_index -= p_playlist->i_size
                         * ( p_playlist->i_index / p_playlist->i_size );
818
819
820
821
822
823
824
825
826
827
828
829
        }
        else
        {
            /* Don't loop by default: stop at playlist end */
            p_playlist->i_index = i_oldindex;
            p_playlist->i_status = PLAYLIST_STOPPED;
        }
    }
    else if( p_playlist->i_index < 0 )
    {
        p_playlist->i_index = p_playlist->i_size - 1;
    }
830
831
832

    val.b_bool = VLC_TRUE;
    var_Set( p_playlist, "intf-change", val );
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
}

/*****************************************************************************
 * PlayItem: play current playlist item
 *****************************************************************************
 * This function calculates the position of the next playlist item, depending
 * on the playlist course mode (forward, backward, random...).
 *****************************************************************************/
static void PlayItem( playlist_t *p_playlist )
{
    if( p_playlist->i_index == -1 )
    {
        if( p_playlist->i_size == 0 )
        {
            return;
        }

        SkipItem( p_playlist, 1 );
    }

    msg_Dbg( p_playlist, "creating new input thread" );
    p_playlist->p_input = input_CreateThread( p_playlist,
855
                                  p_playlist->pp_items[p_playlist->i_index] );
856
857
858
859
860
861
862
863
864
}

/*****************************************************************************
 * Poubellize: put an input thread in the trashcan
 *****************************************************************************
 * XXX: unused
 *****************************************************************************/
static void Poubellize ( playlist_t *p_playlist, input_thread_t *p_input )
{
865
    msg_Dbg( p_playlist, "poubellizing input %i\n", p_input->i_object_id );
866
867
}

868
869
870
871
872
873
874
875
876
877
878
879
880
881
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
/*****************************************************************************
 * playlist_LoadFile: load a playlist file.
 ****************************************************************************/
int playlist_LoadFile( playlist_t * p_playlist, const char *psz_filename )
{
    FILE *file;
    char line[1024];
    int i_current_status;
    int i;

    msg_Dbg( p_playlist, "opening playlist file %s", psz_filename );

    file = fopen( psz_filename, "rt" );
    if( !file )
    {
        msg_Err( p_playlist, "playlist file %s does not exist", psz_filename );
        return -1;
    }
    fseek( file, 0L, SEEK_SET );

    /* check the file is not empty */
    if ( ! fgets( line, 1024, file ) )
    {
        msg_Err( p_playlist, "playlist file %s is empty", psz_filename );
        fclose( file );
        return -1;
    }

    /* get rid of line feed */
    if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
    {
       line[strlen(line)-1] = (char)0;
       if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
    }
    /* check the file format is valid */
    if ( strcmp ( line , PLAYLIST_FILE_HEADER_0_5 ) )
    {
        msg_Err( p_playlist, "playlist file %s format is unsupported"
                , psz_filename );
        fclose( file );
        return -1;
    }

    /* stop playing */
    i_current_status = p_playlist->i_status;
    if ( p_playlist->i_status != PLAYLIST_STOPPED )
    {
        playlist_Stop ( p_playlist );
    }

    /* delete current content of the playlist */
    for( i = p_playlist->i_size - 1; i >= 0; i-- )
    {
        playlist_Delete ( p_playlist , i );
    }

    /* simply add each line */
    while( fgets( line, 1024, file ) )
    {
       /* ignore comments or empty lines */
       if( (line[0] == '#') || (line[0] == '\r') || (line[0] == '\n')
               || (line[0] == (char)0) )
           continue;

       /* get rid of line feed */
       if( line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r' )
       {
           line[strlen(line)-1] = (char)0;
           if( line[strlen(line)-1] == '\r' ) line[strlen(line)-1] = (char)0;
       }

gbazin's avatar
   
gbazin committed
939
940
       playlist_Add ( p_playlist , (char *)&line ,
                      0, 0, PLAYLIST_APPEND , PLAYLIST_END );
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
972
973
    }

    /* start playing */
    if ( i_current_status != PLAYLIST_STOPPED )
    {
        playlist_Play ( p_playlist );
    }

    fclose( file );

    return 0;
}

/*****************************************************************************
 * playlist_SaveFile: Save a playlist in a file.
 *****************************************************************************/
int playlist_SaveFile( playlist_t * p_playlist, const char * psz_filename )
{
    FILE *file;
    int i;

    vlc_mutex_lock( &p_playlist->object_lock );

    msg_Dbg( p_playlist, "saving playlist file %s", psz_filename );

    file = fopen( psz_filename, "wt" );
    if( !file )
    {
        msg_Err( p_playlist , "could not create playlist file %s"
                , psz_filename );
        return -1;
    }

gbazin's avatar
   
gbazin committed
974
    fprintf( file , PLAYLIST_FILE_HEADER_0_5 "\n" );
975
976
977
978

    for ( i = 0 ; i < p_playlist->i_size ; i++ )
    {
        fprintf( file , p_playlist->pp_items[i]->psz_uri );
gbazin's avatar
   
gbazin committed
979
        fprintf( file , "\n" );
980
981
982
983
984
985
986
987
    }

    fclose( file );

    vlc_mutex_unlock( &p_playlist->object_lock );

    return 0;
}