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