1 /*****************************************************************************
2 * sql_media_library.h : Media Library Interface
3 *****************************************************************************
4 * Copyright (C) 2008-2010 the VideoLAN team and AUTHORS
7 * Authors: Antoine Lejeune <phytos@videolan.org>
8 * Jean-Philippe André <jpeg@videolan.org>
9 * Rémi Duraffort <ivoire@videolan.org>
10 * Adrien Maglo <magsoft@videolan.org>
11 * Srikanth Raju <srikiraju at gmail dot com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #ifndef SQL_MEDIA_LIBRARY_H
29 #define SQL_MEDIA_LIBRARY_H
36 #include <vlc_common.h>
38 #include <vlc_media_library.h>
39 #include <vlc_playlist.h>
40 #include <vlc_input.h>
41 #include <vlc_arrays.h>
42 #include <vlc_charset.h>
43 #include <vlc_plugin.h>
44 #include <vlc_interface.h>
45 #include <vlc_modules.h>
47 #include "item_list.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 #define THREAD_SLEEP_DELAY 2 /* Time between two calls to item_list_loop */
53 #define MONITORING_DELAY 30 /* Media library updates interval */
54 #define ITEM_LOOP_UPDATE 1 /* An item is updated after 1 loop */
55 #define ITEM_LOOP_MAX_AGE 10 /* An item is deleted after 10 loops */
56 #define ML_DBVERSION 1 /* The current version of the database */
57 #define ML_MEDIAPOOL_HASH_LENGTH 100 /* The length of the media pool hash */
59 /*****************************************************************************
60 * Structures and types definitions
61 *****************************************************************************/
62 typedef struct monitoring_thread_t monitoring_thread_t;
63 typedef struct ml_poolobject_t ml_poolobject_t;
65 struct ml_poolobject_t
68 ml_poolobject_t* p_next;
71 struct media_library_sys_t
73 /* Lock on the ML object */
79 /* Monitoring thread */
80 monitoring_thread_t *p_mon;
83 watch_thread_t *p_watch;
85 /* Holds all medias */
86 DECL_ARRAY( ml_media_t* ) mediapool;
87 ml_poolobject_t* p_mediapool[ ML_MEDIAPOOL_HASH_LENGTH ];
88 vlc_mutex_t pool_mutex;
90 /* Info on update/collection rebuilding */
95 /* Directory Monitoring thread */
96 struct monitoring_thread_t
103 media_library_t *p_ml;
106 /* Media status Watching thread */
107 struct watch_thread_t
109 media_library_t *p_ml;
114 /* Input items watched */
115 struct item_list_t* p_hlist[ ML_ITEMLIST_HASH_LENGTH ];
116 vlc_mutex_t list_mutex;
118 /* List of items to check */
119 input_item_t** item_append_queue;
120 vlc_mutex_t item_append_queue_lock;
121 int item_append_queue_count;
126 /*****************************************************************************
128 *****************************************************************************/
129 /* General functions */
130 int CreateEmptyDatabase( media_library_t *p_ml );
131 int InitDatabase( media_library_t *p_ml );
134 int Control( media_library_t *p_ml,
139 int AddMedia( media_library_t *p_ml,
140 ml_media_t *p_media );
141 int AddAlbum( media_library_t *p_ml, const char *psz_title,
142 const char *psz_cover, const int i_album_artist );
143 int AddPeople( media_library_t *p_ml,
144 const char *psz_name,
145 const char *psz_role );
146 int AddPlaylistItem( media_library_t *p_ml,
147 playlist_item_t *p_playlist_item );
148 int AddInputItem( media_library_t *p_ml,
149 input_item_t *p_input );
151 /* Create and Copy functions */
152 ml_media_t* GetMedia( media_library_t* p_ml, int id,
153 ml_select_e select, bool reload );
154 input_item_t* GetInputItemFromMedia( media_library_t *p_ml,
156 void CopyInputItemToMedia( ml_media_t *p_media,
157 input_item_t *p_item );
158 void CopyMediaToInputItem( input_item_t *p_item,
159 ml_media_t *p_media );
162 int GetDatabaseVersion( media_library_t *p_ml );
163 int GetMediaIdOfInputItem( media_library_t *p_ml,
164 input_item_t *p_item );
165 int GetMediaIdOfURI( media_library_t *p_ml,
166 const char *psz_uri );
168 /* Search in the database */
169 int BuildSelectVa( media_library_t *p_ml,
171 ml_result_type_e *p_result_type,
173 int BuildSelect( media_library_t *p_ml,
175 ml_result_type_e *p_result_type,
176 const char *psz_selected_type_lvalue,
177 ml_select_e selected_type,
179 int Find( media_library_t *p_ml,
180 vlc_array_t *results,
182 int FindVa( media_library_t *p_ml,
183 vlc_array_t *results,
185 int FindAdv( media_library_t *p_ml,
186 vlc_array_t *results,
187 ml_select_e selected_type,
188 const char* psz_lvalue,
191 /* Update the database */
192 int Update( media_library_t *p_ml,
193 ml_select_e selected_type,
194 const char* psz_lvalue,
196 vlc_array_t *changes );
197 int BuildUpdate( media_library_t *p_ml,
199 char **ppsz_id_query,
200 const char *psz_lvalue,
201 ml_select_e selected_type,
203 vlc_array_t *changes );
204 int UpdateMedia( media_library_t *p_ml,
205 ml_media_t *p_media );
206 int SetArtCover( media_library_t *p_ml,
208 const char *psz_cover );
210 /* Delete medias in the database */
211 int Delete( media_library_t *p_ml, vlc_array_t *p_array );
213 /* Do some query on the database */
214 int QuerySimple( media_library_t *p_ml,
215 const char *psz_fmt, ... );
216 int Query( media_library_t *p_ml,
222 int QueryVa( media_library_t *p_ml,
228 int QuerySimpleVa( media_library_t *p_ml,
232 /* Convert SQL results to ML results */
233 int StringToResult( ml_result_t *res,
236 ml_result_type_e result_type );
237 int SQLToMediaArray( media_library_t *p_ml,
238 vlc_array_t *p_result_array,
242 int SQLToResultArray( media_library_t *p_ml,
243 vlc_array_t *p_result_array,
247 ml_result_type_e result_type );
249 /* Database locking functions */
252 * @brief Begin a transaction
253 * @param p_ml The Media Library object
254 * @return VLC_SUCCESS and VLC_EGENERIC
255 * @note This creates a SHARED lock in SQLITE. All queries made between
256 * a Begin and Commit/Rollback will be transactional.
258 static inline int Begin( media_library_t* p_ml )
260 return sql_BeginTransaction( p_ml->p_sys->p_sql );
264 * @brief Commits the transaction
265 * @param p_ml The Media Library object
267 static inline void Commit( media_library_t* p_ml )
269 sql_CommitTransaction( p_ml->p_sys->p_sql );
273 * @brief Rollback the transaction
274 * @param p_ml The Media Library Object
276 static inline void Rollback( media_library_t* p_ml )
278 sql_RollbackTransaction( p_ml->p_sys->p_sql );
281 /****************************************************************************
282 * Scanning/monitoring functions
283 *****************************************************************************/
284 void *RunMonitoringThread( void *p_mon );
285 int AddDirToMonitor( media_library_t *p_ml,
286 const char *psz_dir );
287 int ListMonitoredDirs( media_library_t *p_ml,
288 vlc_array_t *p_array );
289 int RemoveDirToMonitor( media_library_t *p_ml,
290 const char *psz_dir );
293 /*****************************************************************************
294 * Media pool functions
295 *****************************************************************************/
296 ml_media_t* pool_GetMedia( media_library_t* p_ml, int media_id );
297 int pool_InsertMedia( media_library_t* p_ml, ml_media_t* media, bool locked );
298 void pool_GC( media_library_t* p_ml );
300 /*****************************************************************************
301 * Items watching system
302 *****************************************************************************/
303 /* Watching thread */
304 #define watch_add_Item( a, b, c ) __watch_add_Item( a, b, c, false )
306 int watch_Init( media_library_t *p_ml );
307 void watch_Close( media_library_t *p_ml );
308 int __watch_add_Item( media_library_t *p_ml, input_item_t *p_item,
309 ml_media_t* p_media, bool locked );
311 #define watch_del_Item( a, b ) __watch_del_Item( a, b, false )
312 int __watch_del_Item( media_library_t *p_ml, input_item_t *p_item, bool locked );
313 int watch_del_MediaById( media_library_t* p_ml, int i_media_id );
314 input_item_t* watch_get_itemOfMediaId( media_library_t *p_ml, int i_media_id );
315 ml_media_t* watch_get_mediaOfMediaId( media_library_t* p_ml, int i_media_id );
316 int watch_get_mediaIdOfItem( media_library_t *p_ml, input_item_t *p_item );
317 void watch_Force_Update( media_library_t* p_ml );
319 /*****************************************************************************
320 * Free result of ml_Query
321 *****************************************************************************/
322 static inline void FreeSQLResult( media_library_t *p_ml, char **ppsz_result )
326 sql_Free( p_ml->p_sys->p_sql, ppsz_result );
330 #endif /* SQL_MEDIA_LIBRARY_H */