]> git.sesse.net Git - vlc/blob - src/playlist/item.c
Fix deadlocks in playlist
[vlc] / src / playlist / item.c
1 /*****************************************************************************
2  * item.c : Playlist item functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VideoLAN
5  * $Id$
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
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/sout.h>
30 #include <vlc/input.h>
31
32 #include "vlc_playlist.h"
33
34
35
36
37
38 /**
39  * Add a playlist item into a playlist
40  *
41  * \param p_playlist the playlist to insert into
42  * \param p_item the playlist item to insert
43  * \param i_mode the mode used when adding
44  * \param i_pos the possition in the playlist where to add. If this is
45  *        PLAYLIST_END the item will be added at the end of the playlist
46  *        regardless of it's size
47  * \return The id of the playlist item
48 */
49 int playlist_AddItem( playlist_t *p_playlist, playlist_item_t * p_item,
50                 int i_mode, int i_pos)
51 {
52     vlc_value_t     val;
53
54     vlc_mutex_lock( &p_playlist->object_lock );
55
56     /*
57      * CHECK_INSERT : checks if the item is already enqued before
58      * enqueing it
59      */
60     if ( i_mode & PLAYLIST_CHECK_INSERT )
61     {
62          int j;
63
64          if ( p_playlist->pp_items )
65          {
66              for ( j = 0; j < p_playlist->i_size; j++ )
67              {
68                  if ( !strcmp( p_playlist->pp_items[j]->psz_uri, p_item->psz_uri ) )
69                  {
70                       if( p_item->psz_name )
71                       {
72                           free( p_item->psz_name );
73                       }
74                       if( p_item->psz_uri )
75                       {
76                           free( p_item->psz_uri );
77                       }
78                       free( p_item );
79                       vlc_mutex_unlock( &p_playlist->object_lock );
80                       return -1;
81                  }
82              }
83          }
84          i_mode &= ~PLAYLIST_CHECK_INSERT;
85          i_mode |= PLAYLIST_APPEND;
86     }
87
88
89     msg_Dbg( p_playlist, "adding playlist item `%s' ( %s )",
90              p_item->psz_name, p_item->psz_uri );
91
92
93     p_item->i_id = ++p_playlist->i_last_id;
94
95     /* Do a few boundary checks and allocate space for the item */
96     if( i_pos == PLAYLIST_END )
97     {
98         if( i_mode & PLAYLIST_INSERT )
99         {
100             i_mode &= ~PLAYLIST_INSERT;
101             i_mode |= PLAYLIST_APPEND;
102         }
103
104         i_pos = p_playlist->i_size - 1;
105     }
106
107     if( !(i_mode & PLAYLIST_REPLACE)
108          || i_pos < 0 || i_pos >= p_playlist->i_size )
109     {
110         /* Additional boundary checks */
111         if( i_mode & PLAYLIST_APPEND )
112         {
113             i_pos++;
114         }
115
116         if( i_pos < 0 )
117         {
118             i_pos = 0;
119         }
120         else if( i_pos > p_playlist->i_size )
121         {
122             i_pos = p_playlist->i_size;
123         }
124
125         INSERT_ELEM( p_playlist->pp_items,
126                      p_playlist->i_size,
127                      i_pos,
128                      p_item );
129         p_playlist->i_enabled ++;
130
131         if( p_playlist->i_index >= i_pos )
132         {
133             p_playlist->i_index++;
134         }
135     }
136     else
137     {
138         /* i_mode == PLAYLIST_REPLACE and 0 <= i_pos < p_playlist->i_size */
139         if( p_playlist->pp_items[i_pos]->psz_name )
140         {
141             free( p_playlist->pp_items[i_pos]->psz_name );
142         }
143         if( p_playlist->pp_items[i_pos]->psz_uri )
144         {
145             free( p_playlist->pp_items[i_pos]->psz_uri );
146         }
147         /* XXX: what if the item is still in use? */
148         free( p_playlist->pp_items[i_pos] );
149         p_playlist->pp_items[i_pos] = p_item;
150     }
151
152     if( i_mode & PLAYLIST_GO )
153     {
154         p_playlist->i_index = i_pos;
155         if( p_playlist->p_input )
156         {
157             input_StopThread( p_playlist->p_input );
158         }
159         p_playlist->i_status = PLAYLIST_RUNNING;
160     }
161
162     vlc_mutex_unlock( &p_playlist->object_lock );
163
164     val.b_bool = VLC_TRUE;
165     var_Set( p_playlist, "intf-change", val );
166
167     return p_item->i_id;
168 }
169
170 /**
171  *  Add a option to one item ( no need for p_playlist )
172  *
173  * \param p_item the item on which we want the info
174  * \param psz_format the option
175  * \return 0 on success
176 */
177 int playlist_ItemAddOption( playlist_item_t *p_item,
178                             const char *psz_option )
179 {
180     if( !psz_option ) return VLC_EGENERIC;
181
182     INSERT_ELEM( p_item->ppsz_options, p_item->i_options, p_item->i_options,
183                  strdup( psz_option ) );
184
185     return VLC_SUCCESS;
186 }