playlist.c
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:20k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * playlist.c : Playlist management functions
  3.  *****************************************************************************
  4.  * Copyright (C) 1999-2004 VideoLAN
  5.  * $Id: playlist.c 8153 2004-07-08 12:25:20Z gbazin $
  6.  *
  7.  * Authors: Samuel Hocevar <sam@zoy.org>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #include <stdlib.h>                                      /* free(), strtol() */
  24. #include <stdio.h>                                              /* sprintf() */
  25. #include <string.h>                                            /* strerror() */
  26. #include <vlc/vlc.h>
  27. #include <vlc/vout.h>
  28. #include <vlc/sout.h>
  29. #include <vlc/input.h>
  30. #include "vlc_playlist.h"
  31. #define PLAYLIST_FILE_HEADER_0_5  "# vlc playlist file version 0.5"
  32. /*****************************************************************************
  33.  * Local prototypes
  34.  *****************************************************************************/
  35. static void RunThread ( playlist_t * );
  36. static void SkipItem  ( playlist_t *, int );
  37. static void PlayItem  ( playlist_t * );
  38. /**
  39.  * Create playlist
  40.  *
  41.  * Create a playlist structure.
  42.  * param p_parent the vlc object that is to be the parent of this playlist
  43.  * return a pointer to the created playlist, or NULL on error
  44.  */
  45. playlist_t * __playlist_Create ( vlc_object_t *p_parent )
  46. {
  47.     playlist_t *p_playlist;
  48.     vlc_value_t     val;
  49.     /* Allocate structure */
  50.     p_playlist = vlc_object_create( p_parent, VLC_OBJECT_PLAYLIST );
  51.     if( !p_playlist )
  52.     {
  53.         msg_Err( p_parent, "out of memory" );
  54.         return NULL;
  55.     }
  56.     var_Create( p_playlist, "intf-change", VLC_VAR_BOOL );
  57.     val.b_bool = VLC_TRUE;
  58.     var_Set( p_playlist, "intf-change", val );
  59.     var_Create( p_playlist, "item-change", VLC_VAR_INTEGER );
  60.     val.i_int = -1;
  61.     var_Set( p_playlist, "item-change", val );
  62.     var_Create( p_playlist, "playlist-current", VLC_VAR_INTEGER );
  63.     val.i_int = -1;
  64.     var_Set( p_playlist, "playlist-current", val );
  65.     var_Create( p_playlist, "intf-popupmenu", VLC_VAR_BOOL );
  66.     var_Create( p_playlist, "intf-show", VLC_VAR_BOOL );
  67.     val.b_bool = VLC_TRUE;
  68.     var_Set( p_playlist, "intf-show", val );
  69.     var_Create( p_playlist, "prevent-skip", VLC_VAR_BOOL );
  70.     val.b_bool = VLC_FALSE;
  71.     var_Set( p_playlist, "prevent-skip", val );
  72.     var_Create( p_playlist, "play-and-stop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  73.     var_Create( p_playlist, "random", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  74.     var_Create( p_playlist, "repeat", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  75.     var_Create( p_playlist, "loop", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  76.     p_playlist->p_input = NULL;
  77.     p_playlist->i_status = PLAYLIST_STOPPED;
  78.     p_playlist->i_index = -1;
  79.     p_playlist->i_size = 0;
  80.     p_playlist->pp_items = NULL;
  81.     p_playlist->i_groups = 0;
  82.     p_playlist->pp_groups = NULL;
  83.     p_playlist->i_last_group = 0;
  84.     p_playlist->i_last_id = 0;
  85.     p_playlist->i_sort = SORT_ID;
  86.     p_playlist->i_order = ORDER_NORMAL;
  87.     playlist_CreateGroup( p_playlist, _("Normal") );
  88.     if( vlc_thread_create( p_playlist, "playlist", RunThread,
  89.                            VLC_THREAD_PRIORITY_LOW, VLC_TRUE ) )
  90.     {
  91.         msg_Err( p_playlist, "cannot spawn playlist thread" );
  92.         vlc_object_destroy( p_playlist );
  93.         return NULL;
  94.     }
  95.     /* The object has been initialized, now attach it */
  96.     vlc_object_attach( p_playlist, p_parent );
  97.     return p_playlist;
  98. }
  99. /**
  100.  * Destroy the playlist.
  101.  *
  102.  * Delete all items in the playlist and free the playlist structure.
  103.  * param p_playlist the playlist structure to destroy
  104.  */
  105. void playlist_Destroy( playlist_t * p_playlist )
  106. {
  107.     p_playlist->b_die = 1;
  108.     vlc_thread_join( p_playlist );
  109.     var_Destroy( p_playlist, "intf-change" );
  110.     var_Destroy( p_playlist, "item-change" );
  111.     var_Destroy( p_playlist, "playlist-current" );
  112.     var_Destroy( p_playlist, "intf-popmenu" );
  113.     var_Destroy( p_playlist, "intf-show" );
  114.     var_Destroy( p_playlist, "prevent-skip" );
  115.     var_Destroy( p_playlist, "play-and-stop" );
  116.     var_Destroy( p_playlist, "random" );
  117.     var_Destroy( p_playlist, "repeat" );
  118.     var_Destroy( p_playlist, "loop" );
  119.     while( p_playlist->i_groups > 0 )
  120.     {
  121.         playlist_DeleteGroup( p_playlist, p_playlist->pp_groups[0]->i_id );
  122.     }
  123.     while( p_playlist->i_size > 0 )
  124.     {
  125.         playlist_Delete( p_playlist, 0 );
  126.     }
  127.     vlc_object_destroy( p_playlist );
  128. }
  129. /**
  130.  * Do a playlist action.
  131.  *
  132.  * If there is something in the playlist then you can do playlist actions.
  133.  * param p_playlist the playlist to do the command on
  134.  * param i_command the command to do
  135.  * param i_arg the argument to the command. See playlist_command_t for details
  136.  */
  137. void playlist_Command( playlist_t * p_playlist, playlist_command_t i_command,
  138.                        int i_arg )
  139. {
  140.     vlc_value_t val;
  141.     vlc_mutex_lock( &p_playlist->object_lock );
  142.     if( p_playlist->i_size <= 0 )
  143.     {
  144.         vlc_mutex_unlock( &p_playlist->object_lock );
  145.         return;
  146.     }
  147.     switch( i_command )
  148.     {
  149.     case PLAYLIST_STOP:
  150.         p_playlist->i_status = PLAYLIST_STOPPED;
  151.         if( p_playlist->p_input )
  152.         {
  153.             input_StopThread( p_playlist->p_input );
  154.             val.i_int = p_playlist->i_index;
  155.             /* Does not matter if we unlock here */
  156.             vlc_mutex_unlock( &p_playlist->object_lock );
  157.             var_Set( p_playlist, "item-change",val );
  158.             vlc_mutex_lock( &p_playlist->object_lock );
  159.         }
  160.         break;
  161.     case PLAYLIST_PLAY:
  162.         p_playlist->i_status = PLAYLIST_RUNNING;
  163.         if( !p_playlist->p_input && p_playlist->i_enabled != 0 )
  164.         {
  165.             PlayItem( p_playlist );
  166.         }
  167.         if( p_playlist->p_input )
  168.         {
  169.             val.i_int = PLAYING_S;
  170.             var_Set( p_playlist->p_input, "state", val );
  171.         }
  172.         break;
  173.     case PLAYLIST_PAUSE:
  174.         val.i_int = 0;
  175.         if( p_playlist->p_input )
  176.             var_Get( p_playlist->p_input, "state", &val );
  177.         if( val.i_int == PAUSE_S )
  178.         {
  179.             p_playlist->i_status = PLAYLIST_RUNNING;
  180.             if( p_playlist->p_input )
  181.             {
  182.                 val.i_int = PLAYING_S;
  183.                 var_Set( p_playlist->p_input, "state", val );
  184.             }
  185.         }
  186.         else
  187.         {
  188.             p_playlist->i_status = PLAYLIST_PAUSED;
  189.             if( p_playlist->p_input )
  190.             {
  191.                 val.i_int = PAUSE_S;
  192.                 var_Set( p_playlist->p_input, "state", val );
  193.             }
  194.         }
  195.         break;
  196.     case PLAYLIST_SKIP:
  197.         p_playlist->i_status = PLAYLIST_STOPPED;
  198.         if( p_playlist->i_enabled == 0)
  199.         {
  200.             break;
  201.         }
  202.         SkipItem( p_playlist, i_arg );
  203.         if( p_playlist->p_input )
  204.         {
  205.             input_StopThread( p_playlist->p_input );
  206.         }
  207.         p_playlist->i_status = PLAYLIST_RUNNING;
  208.         break;
  209.     case PLAYLIST_GOTO:
  210.         if( i_arg >= 0 && i_arg < p_playlist->i_size &&
  211.             p_playlist->i_enabled != 0 )
  212.         {
  213.             p_playlist->i_index = i_arg;
  214.             if( p_playlist->p_input )
  215.             {
  216.                 input_StopThread( p_playlist->p_input );
  217.             }
  218.             val.b_bool = VLC_TRUE;
  219.             var_Set( p_playlist, "prevent-skip", val );
  220.             p_playlist->i_status = PLAYLIST_RUNNING;
  221.         }
  222.         break;
  223.     default:
  224.         msg_Err( p_playlist, "unknown playlist command" );
  225.         break;
  226.     }
  227.     vlc_mutex_unlock( &p_playlist->object_lock );
  228. #if 0
  229.     val.b_bool = VLC_TRUE;
  230.     var_Set( p_playlist, "intf-change", val );
  231. #endif
  232.     return;
  233. }
  234. static mtime_t ObjectGarbageCollector( playlist_t *p_playlist, int i_type,
  235.                                        mtime_t destroy_date )
  236. {
  237.     vlc_object_t *p_obj;
  238.     if( destroy_date > mdate() ) return destroy_date;
  239.     if( destroy_date == 0 )
  240.     {
  241.         /* give a little time */
  242.         return mdate() + I64C(1000000);
  243.     }
  244.     else
  245.     {
  246.         while( ( p_obj = vlc_object_find( p_playlist, i_type, FIND_CHILD ) ) )
  247.         {
  248.             if( p_obj->p_parent != (vlc_object_t*)p_playlist )
  249.             {
  250.                 /* only first child (ie unused) */
  251.                 vlc_object_release( p_obj );
  252.                 break;
  253.             }
  254.             if( i_type == VLC_OBJECT_VOUT )
  255.             {
  256.                 msg_Dbg( p_playlist, "garbage collector destroying 1 vout" );
  257.                 vlc_object_detach( p_obj );
  258.                 vlc_object_release( p_obj );
  259.                 vout_Destroy( (vout_thread_t *)p_obj );
  260.             }
  261.             else if( i_type == VLC_OBJECT_SOUT )
  262.             {
  263.                 vlc_object_release( p_obj );
  264.                 sout_DeleteInstance( (sout_instance_t*)p_obj );
  265.             }
  266.         }
  267.         return 0;
  268.     }
  269. }
  270. /*****************************************************************************
  271.  * RunThread: main playlist thread
  272.  *****************************************************************************/
  273. static void RunThread ( playlist_t *p_playlist )
  274. {
  275.     vlc_object_t *p_obj;
  276.     vlc_value_t val;
  277.     mtime_t    i_vout_destroyed_date = 0;
  278.     mtime_t    i_sout_destroyed_date = 0;
  279.     playlist_item_t *p_autodelete_item = 0;
  280.     /* Tell above that we're ready */
  281.     vlc_thread_ready( p_playlist );
  282.     while( !p_playlist->b_die )
  283.     {
  284.         vlc_mutex_lock( &p_playlist->object_lock );
  285.         /* If there is an input, check that it doesn't need to die. */
  286.         if( p_playlist->p_input )
  287.         {
  288.             /* This input is dead. Remove it ! */
  289.             if( p_playlist->p_input->b_dead )
  290.             {
  291.                 input_thread_t *p_input;
  292.                 p_input = p_playlist->p_input;
  293.                 p_playlist->p_input = NULL;
  294.                 /* Release the playlist lock, because we may get stuck
  295.                  * in input_DestroyThread() for some time. */
  296.                 vlc_mutex_unlock( &p_playlist->object_lock );
  297.                 /* Destroy input */
  298.                 input_DestroyThread( p_input );
  299.                 /* Unlink current input
  300.                  * (_after_ input_DestroyThread for vout garbage collector) */
  301.                 vlc_object_detach( p_input );
  302.                 /* Destroy object */
  303.                 vlc_object_destroy( p_input );
  304.                 i_vout_destroyed_date = 0;
  305.                 i_sout_destroyed_date = 0;
  306.                 /* Check for autodeletion */
  307.                 if( p_autodelete_item )
  308.                 {
  309.                     playlist_ItemDelete( p_autodelete_item );
  310.                     p_autodelete_item = 0;
  311.                 }
  312.                 continue;
  313.             }
  314.             /* This input is dying, let him do */
  315.             else if( p_playlist->p_input->b_die )
  316.             {
  317.                 ;
  318.             }
  319.             /* This input has finished, ask him to die ! */
  320.             else if( p_playlist->p_input->b_error
  321.                       || p_playlist->p_input->b_eof )
  322.             {
  323.                 input_StopThread( p_playlist->p_input );
  324.                 if( p_playlist->pp_items[p_playlist->i_index]->b_autodeletion )
  325.                 {
  326.                     /* This ain't pretty but hey it works */
  327.                     p_autodelete_item =
  328.                         p_playlist->pp_items[p_playlist->i_index];
  329.                     p_playlist->pp_items[p_playlist->i_index] =
  330.                         playlist_ItemNew( p_playlist,
  331.                                           p_autodelete_item->input.psz_uri, 0);
  332.                     vlc_mutex_unlock( &p_playlist->object_lock );
  333.                     p_playlist->i_status = PLAYLIST_STOPPED;
  334.                     playlist_Delete( p_playlist, p_playlist->i_index );
  335.                     p_playlist->i_status = PLAYLIST_RUNNING;
  336.                     vlc_mutex_lock( &p_playlist->object_lock );
  337.                 }
  338.                 SkipItem( p_playlist, 1 );
  339.                 vlc_mutex_unlock( &p_playlist->object_lock );
  340.                 continue;
  341.             }
  342.             else if( p_playlist->p_input->i_state != INIT_S )
  343.             {
  344.                 vlc_mutex_unlock( &p_playlist->object_lock );
  345.                 i_vout_destroyed_date =
  346.                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT,
  347.                                             i_vout_destroyed_date );
  348.                 i_sout_destroyed_date =
  349.                     ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT,
  350.                                             i_sout_destroyed_date );
  351.                 vlc_mutex_lock( &p_playlist->object_lock );
  352.             }
  353.         }
  354.         else if( p_playlist->i_status != PLAYLIST_STOPPED )
  355.         {
  356.             /* Start another input. Let's check if that item has
  357.              * been forced. In that case, we override random (by not skipping)
  358.              * and play-and-stop */
  359.             vlc_bool_t b_forced;
  360.             var_Get( p_playlist, "prevent-skip", &val );
  361.             b_forced = val.b_bool;
  362.             if( val.b_bool == VLC_FALSE )
  363.             {
  364.                 SkipItem( p_playlist, 0 );
  365.             }
  366.             /* Reset forced status */
  367.             val.b_bool = VLC_FALSE;
  368.             var_Set( p_playlist, "prevent-skip", val );
  369.             /* Check for play-and-stop */
  370.             var_Get( p_playlist, "play-and-stop", &val );
  371.             if( val.b_bool == VLC_FALSE || b_forced == VLC_TRUE )
  372.             {
  373.                 PlayItem( p_playlist );
  374.             }
  375.         }
  376.         else if( p_playlist->i_status == PLAYLIST_STOPPED )
  377.         {
  378.             vlc_mutex_unlock( &p_playlist->object_lock );
  379.             i_sout_destroyed_date =
  380.                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_SOUT, mdate() );
  381.             i_vout_destroyed_date =
  382.                 ObjectGarbageCollector( p_playlist, VLC_OBJECT_VOUT, mdate() );
  383.             vlc_mutex_lock( &p_playlist->object_lock );
  384.         }
  385.         vlc_mutex_unlock( &p_playlist->object_lock );
  386.         msleep( INTF_IDLE_SLEEP );
  387.     }
  388.     /* If there is an input, kill it */
  389.     while( 1 )
  390.     {
  391.         vlc_mutex_lock( &p_playlist->object_lock );
  392.         if( p_playlist->p_input == NULL )
  393.         {
  394.             vlc_mutex_unlock( &p_playlist->object_lock );
  395.             break;
  396.         }
  397.         if( p_playlist->p_input->b_dead )
  398.         {
  399.             input_thread_t *p_input;
  400.             /* Unlink current input */
  401.             p_input = p_playlist->p_input;
  402.             p_playlist->p_input = NULL;
  403.             vlc_mutex_unlock( &p_playlist->object_lock );
  404.             /* Destroy input */
  405.             input_DestroyThread( p_input );
  406.             /* Unlink current input (_after_ input_DestroyThread for vout
  407.              * garbage collector)*/
  408.             vlc_object_detach( p_input );
  409.             /* Destroy object */
  410.             vlc_object_destroy( p_input );
  411.             continue;
  412.         }
  413.         else if( p_playlist->p_input->b_die )
  414.         {
  415.             /* This input is dying, leave him alone */
  416.             ;
  417.         }
  418.         else if( p_playlist->p_input->b_error || p_playlist->p_input->b_eof )
  419.         {
  420.             input_StopThread( p_playlist->p_input );
  421.             vlc_mutex_unlock( &p_playlist->object_lock );
  422.             continue;
  423.         }
  424.         else
  425.         {
  426.             p_playlist->p_input->b_eof = 1;
  427.         }
  428.         vlc_mutex_unlock( &p_playlist->object_lock );
  429.         msleep( INTF_IDLE_SLEEP );
  430.     }
  431.     /* close all remaining sout */
  432.     while( ( p_obj = vlc_object_find( p_playlist,
  433.                                       VLC_OBJECT_SOUT, FIND_CHILD ) ) )
  434.     {
  435.         vlc_object_release( p_obj );
  436.         sout_DeleteInstance( (sout_instance_t*)p_obj );
  437.     }
  438.     /* close all remaining vout */
  439.     while( ( p_obj = vlc_object_find( p_playlist,
  440.                                       VLC_OBJECT_VOUT, FIND_CHILD ) ) )
  441.     {
  442.         vlc_object_detach( p_obj );
  443.         vlc_object_release( p_obj );
  444.         vout_Destroy( (vout_thread_t *)p_obj );
  445.     }
  446. }
  447. /*****************************************************************************
  448.  * SkipItem: go to Xth playlist item
  449.  *****************************************************************************
  450.  * This function calculates the position of the next playlist item, depending
  451.  * on the playlist course mode (forward, backward, random...).
  452.  *****************************************************************************/
  453. static void SkipItem( playlist_t *p_playlist, int i_arg )
  454. {
  455.     int i_oldindex = p_playlist->i_index;
  456.     vlc_bool_t b_random, b_repeat, b_loop;
  457.     vlc_value_t val;
  458.     int i_count;
  459.     /* If the playlist is empty, there is no current item */
  460.     if( p_playlist->i_size == 0 )
  461.     {
  462.         p_playlist->i_index = -1;
  463.         return;
  464.     }
  465.     var_Get( p_playlist, "random", &val );
  466.     b_random = val.b_bool;
  467.     var_Get( p_playlist, "repeat", &val );
  468.     b_repeat = val.b_bool;
  469.     var_Get( p_playlist, "loop", &val );
  470.     b_loop = val.b_bool;
  471.     /* Increment */
  472.     if( b_random )
  473.     {
  474.         srand( (unsigned int)mdate() );
  475.         i_count = 0;
  476.         while( i_count < p_playlist->i_size )
  477.         {
  478.             p_playlist->i_index =
  479.                 (int)((float)p_playlist->i_size * rand() / (RAND_MAX+1.0));
  480.             /* Check if the item has not already been played */
  481.             if( p_playlist->pp_items[p_playlist->i_index]->i_nb_played == 0 )
  482.                 break;
  483.             i_count++;
  484.         }
  485.         if( i_count == p_playlist->i_size )
  486.         {
  487.             /* The whole playlist has been played: reset the counters */
  488.             while( i_count > 0 )
  489.             {
  490.                 p_playlist->pp_items[--i_count]->i_nb_played = 0;
  491.             }
  492.             if( !b_loop )
  493.             {
  494.                 p_playlist->i_status = PLAYLIST_STOPPED;
  495.             }
  496.         }
  497.     }
  498.     else if( !b_repeat )
  499.     {
  500.         p_playlist->i_index += i_arg;
  501.     }
  502.     /* Boundary check */
  503.     if( p_playlist->i_index >= p_playlist->i_size )
  504.     {
  505.         if( p_playlist->i_status == PLAYLIST_STOPPED || b_random || b_loop )
  506.         {
  507.             p_playlist->i_index -= p_playlist->i_size
  508.                          * ( p_playlist->i_index / p_playlist->i_size );
  509.         }
  510.         else
  511.         {
  512.             /* Don't loop by default: stop at playlist end */
  513.             p_playlist->i_index = i_oldindex;
  514.             p_playlist->i_status = PLAYLIST_STOPPED;
  515.         }
  516.     }
  517.     else if( p_playlist->i_index < 0 )
  518.     {
  519.         p_playlist->i_index = p_playlist->i_size - 1;
  520.     }
  521.     /* Check that the item is enabled */
  522.     if( p_playlist->pp_items[p_playlist->i_index]->b_enabled == VLC_FALSE &&
  523.         p_playlist->i_enabled != 0)
  524.     {
  525.         SkipItem( p_playlist , 1 );
  526.     }
  527. }
  528. /*****************************************************************************
  529.  * PlayItem: play current playlist item
  530.  *****************************************************************************
  531.  * This function calculates the position of the next playlist item, depending
  532.  * on the playlist course mode (forward, backward, random...).
  533.  *****************************************************************************/
  534. static void PlayItem( playlist_t *p_playlist )
  535. {
  536.     playlist_item_t *p_item;
  537.     vlc_value_t val;
  538.     if( p_playlist->i_index == -1 )
  539.     {
  540.         if( p_playlist->i_size == 0 || p_playlist->i_enabled == 0)
  541.         {
  542.             return;
  543.         }
  544.         SkipItem( p_playlist, 1 );
  545.     }
  546.     if( p_playlist->i_enabled == 0)
  547.     {
  548.         return;
  549.     }
  550.     msg_Dbg( p_playlist, "creating new input thread" );
  551.     p_item = p_playlist->pp_items[p_playlist->i_index];
  552.     p_item->i_nb_played++;
  553.     p_playlist->p_input = input_CreateThread( p_playlist, &p_item->input );
  554.     val.i_int = p_playlist->i_index;
  555.     /* unlock the playlist to set the var...mmm */
  556.     vlc_mutex_unlock( &p_playlist->object_lock);
  557.     var_Set( p_playlist, "playlist-current", val);
  558.     vlc_mutex_lock( &p_playlist->object_lock);
  559. }