]> git.sesse.net Git - vlc/blob - modules/media_library/sql_media_library.h
dbus: simplify input event handling and reduce lock scope
[vlc] / modules / media_library / sql_media_library.h
1 /*****************************************************************************
2  * sql_media_library.h : Media Library Interface
3  *****************************************************************************
4  * Copyright (C) 2008-2010 the VideoLAN team and AUTHORS
5  * $Id$
6  *
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>
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 #ifndef SQL_MEDIA_LIBRARY_H
29 #define SQL_MEDIA_LIBRARY_H
30
31 #include <stdarg.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35
36 #include <vlc_common.h>
37 #include <vlc_sql.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>
46
47 #include "item_list.h"
48
49 /*****************************************************************************
50  * Static parameters
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 */
58
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;
64
65 struct ml_poolobject_t
66 {
67     ml_media_t* p_media;
68     ml_poolobject_t* p_next;
69 };
70
71 struct media_library_sys_t
72 {
73     /* Lock on the ML object */
74     vlc_mutex_t lock;
75
76     /* SQL object */
77     sql_t *p_sql;
78
79     /* Monitoring thread */
80     monitoring_thread_t *p_mon;
81
82     /* Watch thread */
83     watch_thread_t *p_watch;
84
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;
89
90     /* Info on update/collection rebuilding */
91     bool b_updating;
92     bool b_rebuilding;
93 };
94
95 /* Directory Monitoring thread */
96 struct monitoring_thread_t
97 {
98     VLC_COMMON_MEMBERS;
99
100     vlc_cond_t wait;
101     vlc_mutex_t lock;
102     vlc_thread_t thread;
103     media_library_t *p_ml;
104 };
105
106 /* Media status Watching thread */
107 struct watch_thread_t
108 {
109     media_library_t *p_ml;
110     vlc_thread_t thread;
111     vlc_cond_t cond;
112     vlc_mutex_t lock;
113
114     /* Input items watched */
115     struct item_list_t* p_hlist[ ML_ITEMLIST_HASH_LENGTH ];
116     vlc_mutex_t list_mutex;
117
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;
122 };
123
124
125
126 /*****************************************************************************
127  * Function headers
128  *****************************************************************************/
129 /* General functions */
130 int CreateEmptyDatabase( media_library_t *p_ml );
131 int InitDatabase( media_library_t *p_ml );
132
133 /* Module Control */
134 int Control( media_library_t *p_ml,
135              int i_query,
136              va_list args );
137
138 /* Add functions */
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 );
150
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,
155                                      int i_media );
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 );
160
161 /* Get functions */
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 );
167
168 /* Search in the database */
169 int BuildSelectVa( media_library_t *p_ml,
170                    char **ppsz_query,
171                    ml_result_type_e *p_result_type,
172                    va_list criterias );
173 int BuildSelect( media_library_t *p_ml,
174                  char **ppsz_query,
175                  ml_result_type_e *p_result_type,
176                  const char *psz_selected_type_lvalue,
177                  ml_select_e selected_type,
178                  ml_ftree_t *tree );
179 int Find( media_library_t *p_ml,
180           vlc_array_t *results,
181           ... );
182 int FindVa( media_library_t *p_ml,
183             vlc_array_t *results,
184             va_list criterias );
185 int FindAdv( media_library_t *p_ml,
186              vlc_array_t *results,
187              ml_select_e selected_type,
188              const char* psz_lvalue,
189              ml_ftree_t *tree );
190
191 /* Update the database */
192 int Update( media_library_t *p_ml,
193             ml_select_e selected_type,
194             const char* psz_lvalue,
195             ml_ftree_t *where,
196             vlc_array_t *changes );
197 int BuildUpdate( media_library_t *p_ml,
198                  char **ppsz_query,
199                  char **ppsz_id_query,
200                  const char *psz_lvalue,
201                  ml_select_e selected_type,
202                  ml_ftree_t* where,
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,
207                  int i_album_id,
208                  const char *psz_cover );
209
210 /* Delete medias in the database */
211 int Delete( media_library_t *p_ml, vlc_array_t *p_array );
212
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,
217            char ***ppp_res,
218            int *pi_rows,
219            int *pi_cols,
220            const char *psz_fmt,
221            ... );
222 int QueryVa( media_library_t *p_ml,
223              char ***ppp_res,
224              int *pi_rows,
225              int *pi_cols,
226              const char *psz_fmt,
227              va_list args );
228 int QuerySimpleVa( media_library_t *p_ml,
229                    const char *psz_fmt,
230                    va_list argp );
231
232 /* Convert SQL results to ML results */
233 int StringToResult( ml_result_t *res,
234                     const char *psz,
235                     const char *psz_id,
236                     ml_result_type_e result_type );
237 int SQLToMediaArray( media_library_t *p_ml,
238                      vlc_array_t *p_result_array,
239                      char **pp_results,
240                      int i_rows,
241                      int i_cols );
242 int SQLToResultArray( media_library_t *p_ml,
243                       vlc_array_t *p_result_array,
244                       char **pp_results,
245                       int i_rows,
246                       int i_cols,
247                       ml_result_type_e result_type );
248
249 /* Database locking functions */
250
251 /**
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.
257  */
258 static inline int Begin( media_library_t* p_ml )
259 {
260     return sql_BeginTransaction( p_ml->p_sys->p_sql );
261 }
262
263 /**
264  * @brief Commits the transaction
265  * @param p_ml The Media Library object
266  */
267 static inline void Commit( media_library_t* p_ml )
268 {
269     sql_CommitTransaction( p_ml->p_sys->p_sql );
270 }
271
272 /**
273  * @brief Rollback the transaction
274  * @param p_ml The Media Library Object
275  */
276 static inline void Rollback( media_library_t* p_ml )
277 {
278     sql_RollbackTransaction( p_ml->p_sys->p_sql );
279 }
280
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 );
291
292
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 );
299
300 /*****************************************************************************
301  * Items watching system
302  *****************************************************************************/
303 /* Watching thread */
304 #define watch_add_Item( a, b, c ) __watch_add_Item( a, b, c, false )
305
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 );
310
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 );
318
319 /*****************************************************************************
320  * Free result of ml_Query
321  *****************************************************************************/
322 static inline void FreeSQLResult( media_library_t *p_ml, char **ppsz_result )
323 {
324     if( ppsz_result )
325     {
326         sql_Free( p_ml->p_sys->p_sql, ppsz_result );
327     }
328 }
329
330 #endif /* SQL_MEDIA_LIBRARY_H */