]> git.sesse.net Git - vlc/blob - include/vlc_playlist.h
* Remove some unneeded complexity in playlist and directory
[vlc] / include / vlc_playlist.h
1 /*****************************************************************************
2  * vlc_playlist.h : Playlist functions
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifndef _VLC_PLAYLIST_H_
25 #define _VLC_PLAYLIST_H_
26
27 #include <assert.h>
28 #include <vlc_input.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 TYPEDEF_ARRAY(playlist_item_t*, playlist_item_array_t);
33 TYPEDEF_ARRAY(input_item_t*, input_item_array_t);
34 /**
35  * \file
36  * This file contain structures and function prototypes related
37  * to the playlist in vlc
38  *
39  * \defgroup vlc_playlist Playlist
40  * @{
41  */
42
43 /**
44  * playlist export helper structure
45  */
46 struct playlist_export_t
47 {
48     char *psz_filename;
49     FILE *p_file;
50     playlist_item_t *p_root;
51 };
52
53 /**
54  * playlist item / node
55  * \see playlist_t
56  */
57 struct playlist_item_t
58 {
59     input_item_t           *p_input;    /**< input item descriptor */
60
61     /* Tree specific fields */
62     int                    i_children;  /**< Number of children
63                                              -1 if not a node */
64     playlist_item_t      **pp_children; /**< Children nodes/items */
65     playlist_item_t       *p_parent;    /**< Item parent */
66
67     int                    i_id;        /**< Playlist item specific id */
68
69     uint8_t                i_flags;     /**< Flags */
70 };
71
72 #define PLAYLIST_SAVE_FLAG      0x0001    /**< Must it be saved */
73 #define PLAYLIST_SKIP_FLAG      0x0002    /**< Must playlist skip after it ? */
74 #define PLAYLIST_DBL_FLAG       0x0004    /**< Is it disabled ? */
75 #define PLAYLIST_RO_FLAG        0x0008    /**< Write-enabled ? */
76 #define PLAYLIST_REMOVE_FLAG    0x0010    /**< Remove this item at the end */
77 #define PLAYLIST_EXPANDED_FLAG  0x0020    /**< Expanded node */
78
79 /** Playlist status */
80 typedef enum
81 { PLAYLIST_STOPPED,PLAYLIST_RUNNING,PLAYLIST_PAUSED } playlist_status_t;
82
83
84 struct services_discovery_t
85 {
86     VLC_COMMON_MEMBERS
87     char *psz_module;
88
89     module_t *p_module;
90
91     services_discovery_sys_t *p_sys;
92     void (*pf_run) ( services_discovery_t *);
93 };
94
95 /** Structure containing information about the playlist */
96 struct playlist_t
97 {
98     VLC_COMMON_MEMBERS
99     int                   i_enabled; /**< How many items are enabled ? */
100
101     playlist_item_array_t items; /**< Arrays of items */
102     playlist_item_array_t all_items; /**< Array of items and nodes */
103
104     input_item_array_t    input_items; /**< Array of input items */
105
106     playlist_item_array_t current; /**< Items currently being played */
107     int                   i_current_index; /**< Index in current array */
108     /** Reset current item ? */
109     vlc_bool_t            b_reset_currently_playing;
110     mtime_t               last_rebuild_date;
111
112     int                   i_last_playlist_id; /**< Last id to an item */
113     int                   i_last_input_id ; /**< Last id on an input */
114
115     services_discovery_t **pp_sds;
116     int                   i_sds;
117
118     /* Predefined items */
119     playlist_item_t *     p_root_category;
120     playlist_item_t *     p_root_onelevel;
121     playlist_item_t *     p_local_category; /** < "Playlist" in CATEGORY view */
122     playlist_item_t *     p_ml_category; /** < "Library" in CATEGORY view */
123     playlist_item_t *     p_local_onelevel; /** < "Playlist" in ONELEVEL view */
124     playlist_item_t *     p_ml_onelevel; /** < "Library" in ONELEVEL iew */
125
126     vlc_bool_t            b_always_tree;/**< Always display as tree */
127     vlc_bool_t            b_never_tree;/**< Never display as tree */
128
129     vlc_bool_t            b_doing_ml; /**< Doing media library stuff, */
130                                       /*get quicker */
131
132     /* Runtime */
133     input_thread_t *      p_input;  /**< the input thread associated
134                                      * with the current item */
135     int                   i_sort; /**< Last sorting applied to the playlist */
136     int                   i_order; /**< Last ordering applied to the playlist */
137     mtime_t               gc_date;
138     vlc_bool_t            b_cant_sleep;
139     playlist_preparse_t  *p_preparse; /**< Preparser object */
140     playlist_fetcher_t   *p_fetcher;/**< Meta and art fetcher object */
141
142     vlc_mutex_t gc_lock;         /**< Lock to protect the garbage collection */
143
144     struct {
145         /* Current status. These fields are readonly, only the playlist
146          * main loop can touch it*/
147         playlist_status_t   i_status;  /**< Current status of playlist */
148         playlist_item_t *   p_item; /**< Currently playing/active item */
149         playlist_item_t *   p_node; /**< Current node to play from */
150     } status;
151
152     struct {
153         /* Request. Use this to give orders to the playlist main loop  */
154         int                 i_status; /**< requested playlist status */
155         playlist_item_t *   p_node;   /**< requested node to play from */
156         playlist_item_t *   p_item;   /**< requested item to play in the node */
157
158         int                 i_skip;   /**< Number of items to skip */
159
160         vlc_bool_t          b_request;/**< Set to true by the requester
161                                            The playlist sets it back to false
162                                            when processing the request */
163         vlc_mutex_t         lock;     /**< Lock to protect request */
164     } request;
165
166     // Playlist-unrelated fields
167     interaction_t       *p_interaction;       /**< Interaction manager */
168     /** The input thread computing stats */
169     input_thread_t      *p_stats_computer;
170     global_stats_t      *p_stats;             /**< Global statistics */
171 };
172
173 /** Helper to add an item */
174 struct playlist_add_t
175 {
176     int i_node;
177     int i_item;
178     int i_position;
179 };
180
181 #define SORT_ID 0
182 #define SORT_TITLE 1
183 #define SORT_TITLE_NODES_FIRST 2
184 #define SORT_ARTIST 3
185 #define SORT_GENRE 4
186 #define SORT_RANDOM 5
187 #define SORT_DURATION 6
188 #define SORT_TITLE_NUMERIC 7
189 #define SORT_ALBUM 8
190
191 #define ORDER_NORMAL 0
192 #define ORDER_REVERSE 1
193
194 /*****************************************************************************
195  * Prototypes
196  *****************************************************************************/
197
198 /* Global thread */
199 #define playlist_ThreadCreate(a) __playlist_ThreadCreate(VLC_OBJECT(a))
200 void        __playlist_ThreadCreate   ( vlc_object_t * );
201 int           playlist_ThreadDestroy  ( playlist_t * );
202
203 /* Helpers */
204 #define PL_LOCK vlc_mutex_lock( &p_playlist->object_lock );
205 #define PL_UNLOCK vlc_mutex_unlock( &p_playlist->object_lock );
206
207 #define pl_Get( a ) a->p_libvlc->p_playlist
208 #define pl_Yield( a ) __pl_Yield( VLC_OBJECT(a) )
209 static inline playlist_t *__pl_Yield( vlc_object_t *p_this )
210 {
211     assert( p_this->p_libvlc->p_playlist );
212     vlc_object_yield( p_this->p_libvlc->p_playlist );
213     return p_this->p_libvlc->p_playlist;
214 }
215 #define pl_Release(a) vlc_object_release( a->p_libvlc->p_playlist );
216
217 /* Playlist control */
218 #define playlist_Play(p) playlist_LockControl(p,PLAYLIST_PLAY )
219 #define playlist_Pause(p) playlist_LockControl(p,PLAYLIST_PAUSE )
220 #define playlist_Stop(p) playlist_LockControl(p,PLAYLIST_STOP )
221 #define playlist_Next(p) playlist_LockControl(p,PLAYLIST_SKIP, 1)
222 #define playlist_Prev(p) playlist_LockControl(p,PLAYLIST_SKIP, -1)
223 #define playlist_Skip(p,i) playlist_LockControl(p,PLAYLIST_SKIP, i)
224
225 VLC_EXPORT( int, playlist_Control, ( playlist_t *, int, ...  ) );
226 VLC_EXPORT( int, playlist_LockControl, ( playlist_t *, int, ...  ) );
227
228 VLC_EXPORT( void,  playlist_Clear, ( playlist_t * ) );
229 VLC_EXPORT( void,  playlist_LockClear, ( playlist_t * ) );
230
231 VLC_EXPORT( int, playlist_PreparseEnqueue, (playlist_t *, input_item_t *) );
232 VLC_EXPORT( int, playlist_PreparseEnqueueItem, (playlist_t *, playlist_item_t *) );
233 VLC_EXPORT( int, playlist_AskForArtEnqueue, (playlist_t *, input_item_t *) );
234
235 /* Services discovery */
236
237 VLC_EXPORT( int, playlist_ServicesDiscoveryAdd, (playlist_t *, const char *));
238 VLC_EXPORT( int, playlist_ServicesDiscoveryRemove, (playlist_t *, const char *));
239 VLC_EXPORT( int, playlist_AddSDModules, (playlist_t *, char *));
240 VLC_EXPORT( vlc_bool_t, playlist_IsServicesDiscoveryLoaded, ( playlist_t *,const char *));
241
242 /* Playlist sorting */
243 VLC_EXPORT( int,  playlist_TreeMove, ( playlist_t *, playlist_item_t *, playlist_item_t *, int ) );
244 VLC_EXPORT( int,  playlist_NodeSort, ( playlist_t *, playlist_item_t *,int, int ) );
245 VLC_EXPORT( int,  playlist_RecursiveNodeSort, ( playlist_t *, playlist_item_t *,int, int ) );
246
247 /* Load/Save */
248 VLC_EXPORT( int,  playlist_Export, ( playlist_t *, const char *, playlist_item_t *, const char * ) );
249
250 /********************************************************
251  * Item management
252  ********************************************************/
253
254 /*************************** Item creation **************************/
255
256 VLC_EXPORT( playlist_item_t* , playlist_ItemNewWithType, ( vlc_object_t *,const char *,const char *, int , const char *const *, int, int) );
257
258 #define playlist_ItemNew( a , b, c ) __playlist_ItemNew(VLC_OBJECT(a) , b , c )
259 /** Create a new item, without adding it to the playlist
260  * \param p_obj a vlc object (anyone will do)
261  * \param psz_uri the mrl of the item
262  * \param psz_name a text giving a name or description of the item
263  * \return the new item or NULL on failure
264  */
265 static inline playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
266                                      const char *psz_uri, const char *psz_name )
267 {
268     /* 0 = ITEM_TYPE_UNKNOWN */
269     return playlist_ItemNewWithType( p_obj, psz_uri,  psz_name, 0, NULL, -1,0);
270 }
271
272 #define playlist_ItemNewFromInput(a,b) __playlist_ItemNewFromInput(VLC_OBJECT(a),b)
273 VLC_EXPORT( playlist_item_t *, __playlist_ItemNewFromInput, ( vlc_object_t *p_obj,input_item_t *p_input ) );
274
275 /*************************** Item deletion **************************/
276 VLC_EXPORT( int, playlist_ItemDelete, ( playlist_item_t * ) );
277 VLC_EXPORT( int,  playlist_DeleteAllFromInput, ( playlist_t *, int ) );
278 VLC_EXPORT( int,  playlist_DeleteFromInput, ( playlist_t *, int, playlist_item_t *, vlc_bool_t ) );
279 VLC_EXPORT( int,  playlist_DeleteFromItemId, ( playlist_t *, int ) );
280 VLC_EXPORT( int,  playlist_LockDelete, ( playlist_t *, int ) );
281 VLC_EXPORT( int,  playlist_LockDeleteAllFromInput, ( playlist_t *, int ) );
282
283 /*************************** Item fields accessors **************************/
284 VLC_EXPORT( int, playlist_ItemSetName, (playlist_item_t *, const char * ) );
285
286 /******************** Item addition ********************/
287 VLC_EXPORT( int,  playlist_Add,    ( playlist_t *, const char *, const char *, int, int, vlc_bool_t ) );
288 VLC_EXPORT( int,  playlist_AddExt, ( playlist_t *, const char *, const char *, int, int, mtime_t, const char *const *,int, vlc_bool_t ) );
289 VLC_EXPORT( int, playlist_AddInput, ( playlist_t *, input_item_t *,int , int, vlc_bool_t ) );
290 VLC_EXPORT( playlist_item_t *, playlist_NodeAddInput, ( playlist_t *, input_item_t *,playlist_item_t *,int , int ) );
291 VLC_EXPORT( void, playlist_NodeAddItem, ( playlist_t *, playlist_item_t *, playlist_item_t *,int , int ) );
292 VLC_EXPORT( int, playlist_BothAddInput, ( playlist_t *, input_item_t *,playlist_item_t *,int , int ) );
293
294 /********************** Misc item operations **********************/
295 VLC_EXPORT( playlist_item_t*, playlist_ItemToNode, (playlist_t *,playlist_item_t *) );
296 VLC_EXPORT( playlist_item_t*, playlist_LockItemToNode, (playlist_t *,playlist_item_t *) );
297
298 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
299                                    int i_input_id, playlist_item_t *p_root,
300                                    vlc_bool_t );
301
302 /********************************** Item search *************************/
303 VLC_EXPORT( playlist_item_t *, playlist_ItemGetById, (playlist_t *, int) );
304 VLC_EXPORT( playlist_item_t *, playlist_ItemGetByInput, (playlist_t *,input_item_t * ) );
305
306 static inline playlist_item_t *playlist_LockItemGetById( playlist_t *p_playlist,
307                                                          int i_id)
308 {
309     playlist_item_t *p_ret;
310     vlc_mutex_lock( &p_playlist->object_lock );
311     p_ret = playlist_ItemGetById( p_playlist, i_id );
312     vlc_mutex_unlock( &p_playlist->object_lock );
313     return p_ret;
314 }
315
316 static inline playlist_item_t *playlist_LockItemGetByInput(
317                                 playlist_t *p_playlist, input_item_t *p_item )
318 {
319     playlist_item_t *p_ret;
320     vlc_mutex_lock( &p_playlist->object_lock );
321     p_ret = playlist_ItemGetByInput( p_playlist, p_item );
322     vlc_mutex_unlock( &p_playlist->object_lock );
323     return p_ret;
324 }
325
326 VLC_EXPORT( int, playlist_LiveSearchUpdate, (playlist_t *, playlist_item_t *, const char *) );
327
328 /********************************************************
329  * Tree management
330  ********************************************************/
331 VLC_EXPORT(void, playlist_NodeDump, ( playlist_t *p_playlist, playlist_item_t *p_item, int i_level ) );
332 VLC_EXPORT( int, playlist_NodeChildrenCount, (playlist_t *,playlist_item_t* ) );
333
334 /* Node management */
335 VLC_EXPORT( playlist_item_t *, playlist_NodeCreate, ( playlist_t *, const char *, playlist_item_t * p_parent ) );
336 VLC_EXPORT( int, playlist_NodeAppend, (playlist_t *,playlist_item_t*,playlist_item_t *) );
337 VLC_EXPORT( int, playlist_NodeInsert, (playlist_t *,playlist_item_t*,playlist_item_t *, int) );
338 VLC_EXPORT( int, playlist_NodeRemoveItem, (playlist_t *,playlist_item_t*,playlist_item_t *) );
339 VLC_EXPORT( playlist_item_t *, playlist_ChildSearchName, (playlist_item_t*, const char* ) );
340 VLC_EXPORT( int, playlist_NodeDelete, ( playlist_t *, playlist_item_t *, vlc_bool_t , vlc_bool_t ) );
341 VLC_EXPORT( int, playlist_NodeEmpty, ( playlist_t *, playlist_item_t *, vlc_bool_t ) );
342 VLC_EXPORT( void, playlist_NodesPairCreate, (playlist_t *, const char *, playlist_item_t **, playlist_item_t **, vlc_bool_t ) );
343 VLC_EXPORT( playlist_item_t *, playlist_GetPreferredNode, ( playlist_t *p_playlist, playlist_item_t *p_node ) );
344
345 /***********************************************************************
346  * Inline functions
347  ***********************************************************************/
348 /** Open a playlist file, add its content to the current playlist */
349 static inline int playlist_Import( playlist_t *p_playlist, const char *psz_file){
350     char psz_uri[256+10];
351     input_item_t *p_input;
352     snprintf( psz_uri, 256+9, "file/://%s", psz_file );
353     p_input = input_ItemNewExt( p_playlist, psz_uri, psz_file, 0, NULL, -1 );
354     playlist_AddInput( p_playlist, p_input, PLAYLIST_APPEND, PLAYLIST_END,
355                        VLC_TRUE );
356     input_Read( p_playlist, p_input, VLC_TRUE );
357     return VLC_SUCCESS;
358 }
359
360 /** Tell if the playlist is currently running */
361 #define playlist_IsPlaying( pl ) ( pl->status.i_status == PLAYLIST_RUNNING )
362
363 /** Tell if the playlist is empty */
364 #define playlist_IsEmpty( pl ) ( pl->items.i_size == 0 )
365
366 /** Tell the number of items in the current playing context */
367 #define playlist_CurrentSize( obj ) obj->p_libvlc->p_playlist->current.i_size
368
369
370 /** Ask the playlist to do some work */
371 static inline void playlist_Signal( playlist_t *p_playlist )
372 {
373     PL_LOCK;
374     vlc_cond_signal( &p_playlist->object_wait );
375     PL_UNLOCK;
376 }
377
378 /**
379  * @}
380  */
381
382 #endif