1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/*
DeaDBeeF - ultimate music player for GNU/Linux systems with X11
Copyright (C) 2009-2010 Alexey Yakovenko <waker@users.sourceforge.net>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <gtk/gtk.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "coverart.h"
#include "../artwork/artwork.h"
#include "gtkui.h"
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(...)
extern DB_artwork_plugin_t *coverart_plugin;
#define MAX_ID 256
#define CACHE_SIZE 20
typedef struct {
struct timeval tm;
char *fname;
time_t filetime;
int width;
GdkPixbuf *pixbuf;
} cached_pixbuf_t;
typedef struct load_query_s {
char *fname;
int width;
struct load_query_s *next;
} load_query_t;
static cached_pixbuf_t cache[CACHE_SIZE];
static int terminate = 0;
static uintptr_t mutex;
static uintptr_t cond;
static uintptr_t tid;
load_query_t *queue;
load_query_t *tail;
static void
queue_add (const char *fname, int width) {
deadbeef->mutex_lock (mutex);
load_query_t *q;
for (q = queue; q; q = q->next) {
if (!strcmp (q->fname, fname) && width == q->width) {
deadbeef->mutex_unlock (mutex);
return; // dupe
}
}
q = malloc (sizeof (load_query_t));
memset (q, 0, sizeof (load_query_t));
q->fname = strdup (fname);
q->width = width;
if (tail) {
tail->next = q;
tail = q;
}
else {
queue = tail = q;
}
deadbeef->mutex_unlock (mutex);
deadbeef->cond_signal (cond);
}
static void
queue_pop (void) {
deadbeef->mutex_lock (mutex);
load_query_t *next = queue->next;
if (queue->fname) {
free (queue->fname);
}
free (queue);
queue = next;
if (!queue) {
tail = NULL;
}
deadbeef->mutex_unlock (mutex);
}
gboolean
redraw_playlist_cb (gpointer dt) {
void main_refresh (void);
main_refresh ();
return FALSE;
}
void
loading_thread (void *none) {
for (;;) {
trace ("covercache: waiting for signal\n");
deadbeef->cond_wait (cond, mutex);
trace ("covercache: signal received\n");
deadbeef->mutex_unlock (mutex);
while (!terminate && queue) {
int cache_min = 0;
deadbeef->mutex_lock (mutex);
for (int i = 0; i < CACHE_SIZE; i++) {
if (!cache[i].pixbuf) {
cache_min = i;
break;
}
if (cache[cache_min].pixbuf && cache[i].pixbuf) {
if (cache[cache_min].tm.tv_sec > cache[i].tm.tv_sec) {
cache_min = i;
}
}
}
if (cache_min != -1) {
if (cache[cache_min].pixbuf) {
g_object_unref (cache[cache_min].pixbuf);
cache[cache_min].pixbuf = NULL;
}
if (cache[cache_min].fname) {
free (cache[cache_min].fname);
cache[cache_min].fname = NULL;
}
}
deadbeef->mutex_unlock (mutex);
if (cache_min == -1) {
trace ("coverart pixbuf cache overflow, waiting...\n");
usleep (500000);
continue;
}
struct stat stat_buf;
if (stat (queue->fname, &stat_buf) < 0) {
trace ("failed to stat file %s\n", queue->fname);
}
GdkPixbuf *pixbuf = NULL;
GError *error = NULL;
pixbuf = gdk_pixbuf_new_from_file_at_scale (queue->fname, queue->width, queue->width, TRUE, &error);
if (!pixbuf) {
unlink (queue->fname);
fprintf (stderr, "gdk_pixbuf_new_from_file_at_scale %s %d failed, error: %s\n", queue->fname, queue->width, error->message);
if (error) {
g_error_free (error);
error = NULL;
}
const char *defpath = coverart_plugin->get_default_cover ();
if (stat (defpath, &stat_buf) < 0) {
trace ("failed to stat file %s\n", queue->fname);
}
pixbuf = gdk_pixbuf_new_from_file_at_scale (defpath, queue->width, queue->width, TRUE, &error);
if (!pixbuf) {
fprintf (stderr, "gdk_pixbuf_new_from_file_at_scale %s %d failed, error: %s\n", defpath, queue->width, error->message);
}
}
if (error) {
g_error_free (error);
error = NULL;
}
if (!pixbuf) {
// make default empty image
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 2, 2);
stat_buf.st_mtime = 0;
}
if (cache_min != -1) {
deadbeef->mutex_lock (mutex);
cache[cache_min].filetime = stat_buf.st_mtime;
cache[cache_min].pixbuf = pixbuf;
cache[cache_min].fname = strdup (queue->fname);
gettimeofday (&cache[cache_min].tm, NULL);
cache[cache_min].width = queue->width;
struct stat stat_buf;
deadbeef->mutex_unlock (mutex);
}
queue_pop ();
g_idle_add (redraw_playlist_cb, NULL);
}
if (terminate) {
break;
}
}
}
void
cover_avail_callback (const char *fname, const char *artist, const char *album, void *user_data) {
// means requested image is now in disk cache
// load it into main memory
GdkPixbuf *pb = get_cover_art (fname, artist, album, (intptr_t)user_data);
if (pb) {
g_object_unref (pb);
// already in cache, redraw
g_idle_add (redraw_playlist_cb, NULL);
}
}
static GdkPixbuf *
get_pixbuf (const char *fname, int width) {
int requested_width = width;
// find in cache
deadbeef->mutex_lock (mutex);
for (int i = 0; i < CACHE_SIZE; i++) {
if (cache[i].pixbuf) {
if (!strcmp (fname, cache[i].fname) && cache[i].width == width) {
// check if cached filetime hasn't changed
struct stat stat_buf;
if (!stat (fname, &stat_buf) && stat_buf.st_mtime == cache[i].filetime) {
gettimeofday (&cache[i].tm, NULL);
GdkPixbuf *pb = cache[i].pixbuf;
g_object_ref (pb);
deadbeef->mutex_unlock (mutex);
return pb;
}
}
}
}
#if 0
printf ("cache miss: %s/%d\n", fname, width);
for (int i = 0; i < CACHE_SIZE; i++) {
if (cache[i].pixbuf) {
printf (" cache line %d: %s/%d\n", i, cache[i].fname, cache[i].width);
}
}
#endif
deadbeef->mutex_unlock (mutex);
queue_add (fname, width);
return NULL;
}
GdkPixbuf *
get_cover_art (const char *fname, const char *artist, const char *album, int width) {
if (!coverart_plugin) {
return NULL;
}
char *image_fname = coverart_plugin->get_album_art (fname, artist, album, cover_avail_callback, (void *)(intptr_t)width);
if (image_fname) {
GdkPixbuf *pb = get_pixbuf (image_fname, width);
free (image_fname);
return pb;
}
return NULL;
}
void
coverart_reset_queue (void) {
deadbeef->mutex_lock (mutex);
if (queue) {
load_query_t *q = queue->next;
while (q) {
load_query_t *next = q->next;
if (q->fname) {
free (q->fname);
}
free (q);
q = next;
}
queue->next = NULL;
tail = queue;
}
deadbeef->mutex_unlock (mutex);
if (coverart_plugin) {
coverart_plugin->reset (1);
}
}
void
cover_art_init (void) {
terminate = 0;
mutex = deadbeef->mutex_create_nonrecursive ();
cond = deadbeef->cond_create ();
tid = deadbeef->thread_start_low_priority (loading_thread, NULL);
}
void
cover_art_free (void) {
trace ("terminating cover art loader...\n");
if (coverart_plugin) {
trace ("resetting artwork plugin...\n");
coverart_plugin->reset (0);
}
if (tid) {
terminate = 1;
trace ("sending terminate signal to art loader thread...\n");
deadbeef->cond_signal (cond);
deadbeef->thread_join (tid);
tid = 0;
}
while (queue) {
queue_pop ();
}
for (int i = 0; i < CACHE_SIZE; i++) {
if (cache[i].pixbuf) {
g_object_unref (cache[i].pixbuf);
}
}
memset (cache, 0, sizeof (cache));
deadbeef->cond_free (cond);
deadbeef->mutex_free (mutex);
}
|