]> git.sesse.net Git - vlc/blob - include/vlc_playlist.h
Re-enable random.
[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 /**
28  *  \file
29  *  This file contain structures and function prototypes related
30  *  to the playlist in vlc
31  */
32
33 /**
34  * \defgroup vlc_playlist Playlist
35  * @{
36  */
37
38 /**
39  * playlist export helper structure
40  */
41 struct playlist_export_t
42 {
43     char *psz_filename;
44     FILE *p_file;
45     playlist_item_t *p_root;
46 };
47
48 /**
49  * playlist item / node
50  * \see playlist_t
51  */
52 struct playlist_item_t
53 {
54     input_item_t           *p_input;    /**< input item descriptor */
55
56     /* Tree specific fields */
57     int                    i_children;  /**< Number of children
58                                              -1 if not a node */
59     playlist_item_t      **pp_children; /**< Children nodes/items */
60     playlist_item_t       *p_parent;    /**< Item parent */
61
62     int                    i_id;        /**< Playlist item specific id */
63
64     uint8_t                i_flags;     /**< Flags */
65 };
66
67 #define PLAYLIST_SAVE_FLAG      0x0001    /**< Must it be saved */
68 #define PLAYLIST_SKIP_FLAG      0x0002    /**< Must playlist skip after it ? */
69 #define PLAYLIST_DBL_FLAG       0x0004    /**< Is it disabled ? */
70 #define PLAYLIST_RO_FLAG        0x0008    /**< Write-enabled ? */
71 #define PLAYLIST_REMOVE_FLAG    0x0010    /**< Remove this item at the end */
72 #define PLAYLIST_EXPANDED_FLAG  0x0020    /**< Expanded node */
73 #define PLAYLIST_PREFCAT_FLAG   0x0040    /**< Prefer category */
74
75 /**
76  * Playlist status
77  */
78 typedef enum
79 { PLAYLIST_STOPPED,PLAYLIST_RUNNING,PLAYLIST_PAUSED } playlist_status_t;
80
81
82 struct services_discovery_t
83 {
84     VLC_COMMON_MEMBERS
85     char *psz_module;
86
87     module_t *p_module;
88
89     services_discovery_sys_t *p_sys;
90     void (*pf_run) ( services_discovery_t *);
91 };
92
93 struct playlist_preparse_t
94 {
95     VLC_COMMON_MEMBERS
96     vlc_mutex_t     lock;
97     int             i_waiting;
98     input_item_t  **pp_waiting;
99 };
100
101
102 /** Structure containing information about the playlist */
103 struct playlist_t
104 {
105     VLC_COMMON_MEMBERS
106 /**
107    \name playlist_t
108    These members are uniq to playlist_t
109 */
110 /*@{*/
111     int                   i_enabled; /**< How many items are enabled ? */
112
113     /* Arrays of items */
114     int                   i_size;   /**< total size of the list */
115     playlist_item_t **    pp_items; /**< array of pointers to the
116                                      * playlist items */
117     int                   i_all_size; /**< size of list of items and nodes */
118     playlist_item_t **    pp_all_items; /**< array of pointers to the
119                                          * playlist items and nodes */
120     int                   i_input_items;
121     input_item_t **       pp_input_items;
122
123     int                   i_random;     /**< Number of candidates for random */
124     playlist_item_t **    pp_random;    /**< Random candidate items */
125     int                   i_random_index; /**< Current random item */
126     vlc_bool_t            b_reset_random; /**< Recreate random array ?*/
127
128     int                   i_last_playlist_id; /**< Last id to an item */
129     int                   i_last_input_id ; /**< Last id on an input */
130
131     services_discovery_t **pp_sds;
132     int                   i_sds;
133
134     /* Predefined items */
135     playlist_item_t *     p_root_category;
136     playlist_item_t *     p_root_onelevel;
137     playlist_item_t *     p_local_category; /** < "Playlist" in CATEGORY view */
138     playlist_item_t *     p_ml_category; /** < "Library" in CATEGORY view */
139     playlist_item_t *     p_local_onelevel; /** < "Playlist" in ONELEVEL view */
140     playlist_item_t *     p_ml_onelevel; /** < "Library" in ONELEVEL iew */
141
142     vlc_bool_t            b_always_tree;/**< Always display as tree */
143     vlc_bool_t            b_never_tree;/**< Never display as tree */
144
145     /* Runtime */
146     input_thread_t *      p_input;  /**< the input thread associated
147                                      * with the current item */
148     int                   i_sort; /**< Last sorting applied to the playlist */
149     int                   i_order; /**< Last ordering applied to the playlist */
150     mtime_t               i_vout_destroyed_date;
151     mtime_t               i_sout_destroyed_date;
152     playlist_preparse_t  *p_preparse; /**< Preparser object */
153
154     vlc_mutex_t gc_lock;         /**< Lock to protect the garbage collection */
155
156     struct {
157         /* Current status. These fields are readonly, only the playlist
158          * main loop can touch it*/
159         playlist_status_t   i_status;  /**< Current status of playlist */
160         playlist_item_t *   p_item; /**< Currently playing/active item */
161         playlist_item_t *   p_node; /**< Current node to play from */
162     } status;
163
164     struct {
165         /* Request. Use this to give orders to the playlist main loop  */
166         int                 i_status; /**< requested playlist status */
167         playlist_item_t *   p_node;   /**< requested node to play from */
168         playlist_item_t *   p_item;   /**< requested item to play in the node */
169
170         int                 i_skip;   /**< Number of items to skip */
171
172         vlc_bool_t          b_request;/**< Set to true by the requester
173                                            The playlist sets it back to false
174                                            when processing the request */
175         vlc_mutex_t         lock;     /**< Lock to protect request */
176     } request;
177
178     // Playlist-unrelated fields
179     interaction_t       *p_interaction;       /**< Interaction manager */
180     global_stats_t      *p_stats;             /**< Global statistics */
181     /*@}*/
182 };
183
184 /* Helper to add an item */
185 struct playlist_add_t
186 {
187     int i_node;
188     int i_item;
189     int i_position;
190 };
191
192 #define SORT_ID 0
193 #define SORT_TITLE 1
194 #define SORT_TITLE_NODES_FIRST 2
195 #define SORT_ARTIST 3
196 #define SORT_GENRE 4
197 #define SORT_RANDOM 5
198 #define SORT_DURATION 6
199 #define SORT_TITLE_NUMERIC 7
200 #define SORT_ALBUM 8
201
202 #define ORDER_NORMAL 0
203 #define ORDER_REVERSE 1
204
205 /*****************************************************************************
206  * Prototypes
207  *****************************************************************************/
208
209 /* Global thread */
210 #define playlist_ThreadCreate(a) __playlist_ThreadCreate(VLC_OBJECT(a))
211 playlist_t *__playlist_ThreadCreate   ( vlc_object_t * );
212 int           playlist_ThreadDestroy  ( playlist_t * );
213
214 /* Helpers */
215 #define PL_LOCK vlc_mutex_lock( &p_playlist->object_lock );
216 #define PL_UNLOCK vlc_mutex_unlock( &p_playlist->object_lock );
217
218 /* Creation/Deletion */
219 playlist_t *playlist_Create   ( vlc_object_t * );
220 void        playlist_Destroy  ( playlist_t * );
221
222 /* Engine */
223 void playlist_MainLoop( playlist_t * );
224 void playlist_LastLoop( playlist_t * );
225 void playlist_PreparseLoop( playlist_preparse_t * );
226
227 /* Control */
228 playlist_item_t * playlist_NextItem  ( playlist_t * );
229 int playlist_PlayItem  ( playlist_t *, playlist_item_t * );
230
231 /* Playlist control */
232 #define playlist_Play(p) playlist_LockControl(p,PLAYLIST_PLAY )
233 #define playlist_Pause(p) playlist_LockControl(p,PLAYLIST_PAUSE )
234 #define playlist_Stop(p) playlist_LockControl(p,PLAYLIST_STOP )
235 #define playlist_Next(p) playlist_LockControl(p,PLAYLIST_SKIP, 1)
236 #define playlist_Prev(p) playlist_LockControl(p,PLAYLIST_SKIP, -1)
237 #define playlist_Skip(p,i) playlist_LockControl(p,PLAYLIST_SKIP, i)
238
239 VLC_EXPORT( int, playlist_Control, ( playlist_t *, int, ...  ) );
240 VLC_EXPORT( int, playlist_LockControl, ( playlist_t *, int, ...  ) );
241
242 VLC_EXPORT( void,  playlist_Clear, ( playlist_t * ) );
243 VLC_EXPORT( void,  playlist_LockClear, ( playlist_t * ) );
244
245 VLC_EXPORT( int, playlist_PreparseEnqueue, (playlist_t *, input_item_t *) );
246 VLC_EXPORT( int, playlist_PreparseEnqueueItem, (playlist_t *, playlist_item_t *) );
247
248 /* Services discovery */
249
250 VLC_EXPORT( int, playlist_ServicesDiscoveryAdd, (playlist_t *, const char *));
251 VLC_EXPORT( int, playlist_ServicesDiscoveryRemove, (playlist_t *, const char *));
252 VLC_EXPORT( int, playlist_AddSDModules, (playlist_t *, char *));
253 VLC_EXPORT( vlc_bool_t, playlist_IsServicesDiscoveryLoaded, ( playlist_t *,const char *));
254
255 /* Playlist sorting */
256 VLC_EXPORT( int,  playlist_TreeMove, ( playlist_t *, playlist_item_t *, playlist_item_t *, int ) );
257 VLC_EXPORT( int,  playlist_NodeSort, ( playlist_t *, playlist_item_t *,int, int ) );
258 VLC_EXPORT( int,  playlist_RecursiveNodeSort, ( playlist_t *, playlist_item_t *,int, int ) );
259
260 /* Load/Save */
261 VLC_EXPORT( int,  playlist_Import, ( playlist_t *, const char *, playlist_item_t *, vlc_bool_t ) );
262 VLC_EXPORT( int,  playlist_Export, ( playlist_t *, const char *, playlist_item_t *, const char * ) );
263
264 /********************************************************
265  * Item management
266  ********************************************************/
267
268 /*************************** Item creation **************************/
269
270 VLC_EXPORT( playlist_item_t* , playlist_ItemNewWithType, ( vlc_object_t *,const char *,const char *, int , const char **, int, int) );
271
272 #define playlist_ItemNew( a , b, c ) __playlist_ItemNew(VLC_OBJECT(a) , b , c )
273 /** Create a new item, without adding it to the playlist
274  * \param p_obj a vlc object (anyone will do)
275  * \param psz_uri the mrl of the item
276  * \param psz_name a text giving a name or description of the item
277  * \return the new item or NULL on failure
278  */
279 static inline playlist_item_t * __playlist_ItemNew( vlc_object_t *p_obj,
280                                      const char *psz_uri, const char *psz_name )
281 {
282     /* 0 = ITEM_TYPE_UNKNOWN */
283     return playlist_ItemNewWithType( p_obj, psz_uri,  psz_name, 0, NULL, -1,0);
284 }
285
286 #define playlist_ItemNewFromInput(a,b) __playlist_ItemNewFromInput(VLC_OBJECT(a),b)
287 VLC_EXPORT( playlist_item_t *, __playlist_ItemNewFromInput, ( vlc_object_t *p_obj,input_item_t *p_input ) );
288
289 /*************************** Item deletion **************************/
290 VLC_EXPORT( int, playlist_ItemDelete, ( playlist_item_t * ) );
291 VLC_EXPORT( int,  playlist_DeleteAllFromInput, ( playlist_t *, int ) );
292 VLC_EXPORT( int,  playlist_DeleteFromInput, ( playlist_t *, int, playlist_item_t *, vlc_bool_t ) );
293 VLC_EXPORT( int,  playlist_DeleteFromItemId, ( playlist_t *, int ) );
294 VLC_EXPORT( int,  playlist_LockDelete, ( playlist_t *, int ) );
295 VLC_EXPORT( int,  playlist_LockDeleteAllFromInput, ( playlist_t *, int ) );
296
297 /*************************** Item fields accessors **************************/
298 VLC_EXPORT( int, playlist_ItemSetName, (playlist_item_t *,  char * ) );
299
300 /******************** Item addition ********************/
301 VLC_EXPORT( int,  playlist_Add,    ( playlist_t *, const char *, const char *, int, int, vlc_bool_t ) );
302 VLC_EXPORT( int,  playlist_AddExt, ( playlist_t *, const char *, const char *, int, int, mtime_t, const char **,int, vlc_bool_t ) );
303 VLC_EXPORT( int, playlist_AddInput, ( playlist_t *, input_item_t *,int , int, vlc_bool_t ) );
304 VLC_EXPORT( playlist_item_t *, playlist_NodeAddInput, ( playlist_t *, input_item_t *,playlist_item_t *,int , int ) );
305 VLC_EXPORT( void, playlist_NodeAddItem, ( playlist_t *, playlist_item_t *, playlist_item_t *,int , int ) );
306 VLC_EXPORT( int, playlist_BothAddInput, ( playlist_t *, input_item_t *,playlist_item_t *,int , int ) );
307 VLC_EXPORT( void, playlist_AddWhereverNeeded, (playlist_t* , input_item_t*, playlist_item_t*,playlist_item_t*,vlc_bool_t, int ) );
308
309 /** Add a MRL into the playlist.
310  * \see playlist_Add
311  */
312 static inline int playlist_PlaylistAdd( playlist_t *p_playlist,
313                           const char *psz_uri, const char *psz_name,
314                           int i_mode, int i_pos )
315 {
316     return playlist_Add( p_playlist, psz_uri, psz_name, i_mode, i_pos,
317                          VLC_TRUE);
318 }
319
320 /** Add a MRL to the media library
321  * \see playlist_Add
322  */
323 static inline int playlist_MLAdd( playlist_t *p_playlist, const char *psz_uri,
324                                   const char *psz_name, int i_mode, int i_pos )
325 {
326     return playlist_Add( p_playlist, psz_uri, psz_name, i_mode, i_pos,
327                          VLC_FALSE );
328 }
329
330 /** Add a MRL to the playlist, with duration and options given
331  * \see playlist_AddExt
332  */
333 static inline int playlist_PlaylistAddExt( playlist_t *p_playlist,
334             const char * psz_uri, const char *psz_name, int i_mode, int i_pos,
335             mtime_t i_duration, const char **ppsz_options, int i_options ) 
336 {
337     return playlist_AddExt( p_playlist, psz_uri, psz_name, i_mode, i_pos,
338                             i_duration, ppsz_options, i_options, VLC_TRUE );
339 }
340
341 /** Add a MRL to the media library, with duration and options given
342  * \see playlist_AddExt
343  */
344 static inline int playlist_MLAddExt( playlist_t *p_playlist,
345             const char * psz_uri, const char *psz_name, int i_mode, int i_pos,
346             mtime_t i_duration, const char **ppsz_options, int i_options ) 
347 {
348     return playlist_AddExt( p_playlist, psz_uri, psz_name, i_mode, i_pos,
349                             i_duration, ppsz_options, i_options, VLC_FALSE );
350 }
351
352 /** Add an input item to the playlist node 
353  * \see playlist_AddInput
354  */
355 static inline int playlist_PlaylistAddInput( playlist_t* p_playlist,
356                                 input_item_t *p_input, int i_mode, int i_pos )
357 {
358     return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, VLC_TRUE );
359 }
360
361 /** Add an input item to the media library 
362  * \see playlist_AddInput
363  */
364 static inline int playlist_MLAddInput( playlist_t* p_playlist,
365                                 input_item_t *p_input, int i_mode, int i_pos )
366 {
367     return playlist_AddInput( p_playlist, p_input, i_mode, i_pos, VLC_FALSE );
368 }
369
370 void playlist_SendAddNotify( playlist_t *p_playlist, int i_item_id, int i_node_id );
371
372 /********************** Misc item operations **********************/
373 VLC_EXPORT( playlist_item_t*, playlist_ItemToNode, (playlist_t *,playlist_item_t *) );
374 VLC_EXPORT( playlist_item_t*, playlist_LockItemToNode, (playlist_t *,playlist_item_t *) );
375
376 playlist_item_t *playlist_ItemFindFromInputAndRoot( playlist_t *p_playlist,
377                                    int i_input_id, playlist_item_t *p_root );
378
379 /********************************** Item search *************************/
380 VLC_EXPORT( playlist_item_t *, playlist_ItemGetById, (playlist_t *, int) );
381 VLC_EXPORT( playlist_item_t *, playlist_ItemGetByInput, (playlist_t *,input_item_t * ) );
382
383 static inline playlist_item_t *playlist_LockItemGetById( playlist_t *p_playlist,
384                                                          int i_id)
385 {
386     playlist_item_t *p_ret;
387     vlc_mutex_lock( &p_playlist->object_lock );
388     p_ret = playlist_ItemGetById( p_playlist, i_id );
389     vlc_mutex_unlock( &p_playlist->object_lock );
390     return p_ret;
391 }
392
393 static inline playlist_item_t *playlist_LockItemGetByInput(
394                                 playlist_t *p_playlist, input_item_t *p_item )
395 {
396     playlist_item_t *p_ret;
397     vlc_mutex_lock( &p_playlist->object_lock );
398     p_ret = playlist_ItemGetByInput( p_playlist, p_item );
399     vlc_mutex_unlock( &p_playlist->object_lock );
400     return p_ret;
401 }
402
403 VLC_EXPORT( int, playlist_LiveSearchUpdate, (playlist_t *, playlist_item_t *, const char *) );
404
405 /********************************************************
406  * Tree management
407  ********************************************************/
408 VLC_EXPORT(void, playlist_NodeDump, ( playlist_t *p_playlist, playlist_item_t *p_item, int i_level ) );
409 VLC_EXPORT( int, playlist_NodeChildrenCount, (playlist_t *,playlist_item_t* ) );
410
411 /* Node management */
412 VLC_EXPORT( playlist_item_t *, playlist_NodeCreate, ( playlist_t *, char *, playlist_item_t * p_parent ) );
413 VLC_EXPORT( int, playlist_NodeAppend, (playlist_t *,playlist_item_t*,playlist_item_t *) );
414 VLC_EXPORT( int, playlist_NodeInsert, (playlist_t *,playlist_item_t*,playlist_item_t *, int) );
415 VLC_EXPORT( int, playlist_NodeRemoveItem, (playlist_t *,playlist_item_t*,playlist_item_t *) );
416 VLC_EXPORT( playlist_item_t *, playlist_ChildSearchName, (playlist_item_t*, const char* ) );
417 VLC_EXPORT( int, playlist_NodeDelete, ( playlist_t *, playlist_item_t *, vlc_bool_t , vlc_bool_t ) );
418 VLC_EXPORT( int, playlist_NodeEmpty, ( playlist_t *, playlist_item_t *, vlc_bool_t ) );
419 VLC_EXPORT( void, playlist_NodesCreateForSD, (playlist_t *, char *, playlist_item_t **, playlist_item_t ** ) );
420 VLC_EXPORT( playlist_item_t *, playlist_GetPreferredNode, ( playlist_t *p_playlist, playlist_item_t *p_node ) );
421
422 /* Tree walking - These functions are only for playlist, not plugins */
423 int playlist_GetAllEnabledChildren( playlist_t *p_playlist,
424                                     playlist_item_t *p_node,
425                                     playlist_item_t ***ppp_items );
426 playlist_item_t *playlist_GetNextLeaf( playlist_t *p_playlist,
427                                     playlist_item_t *p_root,
428                                     playlist_item_t *, vlc_bool_t, vlc_bool_t );
429 playlist_item_t *playlist_GetPrevLeaf( playlist_t *p_playlist,
430                                     playlist_item_t *p_root,
431                                     playlist_item_t *, vlc_bool_t, vlc_bool_t );
432 playlist_item_t *playlist_GetLastLeaf( playlist_t *p_playlist,
433                                     playlist_item_t *p_root );
434
435 /***********************************************************************
436  * Inline functions
437  ***********************************************************************/
438
439 /** Tell if the playlist is currently running */
440 static inline vlc_bool_t playlist_IsPlaying( playlist_t * p_playlist )
441 {
442     vlc_bool_t b_playing;
443     vlc_mutex_lock( &p_playlist->object_lock );
444     b_playing = p_playlist->status.i_status == PLAYLIST_RUNNING;
445     vlc_mutex_unlock( &p_playlist->object_lock );
446     return( b_playing );
447 }
448
449 /** Tell if the playlist is empty */
450 static inline vlc_bool_t playlist_IsEmpty( playlist_t * p_playlist )
451 {
452     vlc_bool_t b_empty;
453     vlc_mutex_lock( &p_playlist->object_lock );
454     b_empty = p_playlist->i_size == 0;
455     vlc_mutex_unlock( &p_playlist->object_lock );
456     return( b_empty );
457 }
458
459 /**
460  * @}
461  */
462
463 #define PLAYLIST_DEBUG 1
464
465 #ifdef PLAYLIST_DEBUG
466 #define PL_DEBUG( msg, args... ) msg_Dbg( p_playlist, msg, ## args )
467 #else
468 #define PL_DEBUG( msg, args ... ) {}
469 #endif
470
471 #define PLI_NAME( p ) p ? p->p_input->psz_name : "null"
472
473 #endif