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