summaryrefslogtreecommitdiff
path: root/plugins/gtkui/coverart.c
blob: f7d78696834b0c5d0eafca9cd0a2c5a23e27e08f (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
    DeaDBeeF - ultimate music player for GNU/Linux systems with X11
    Copyright (C) 2009-2013 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 <assert.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#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;


GdkPixbuf *pixbuf_default;

#define MAX_ID 256
#define CACHE_SIZE 20

typedef struct {
    struct timeval tm;
    char *fname;
    int width;
    GdkPixbuf *pixbuf;
} cached_pixbuf_t;

#define MAX_CALLBACKS 200

typedef struct cover_callback_s {
    void (*cb) (void*ud);
    void *ud;
} cover_callback_t;

typedef struct load_query_s {
    char *fname;
    int width;
    cover_callback_t callbacks[MAX_CALLBACKS];
    int numcb;
    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, void (*callback) (void *user_data), void *user_data) {
    deadbeef->mutex_lock (mutex);
    load_query_t *q;
    if (fname) {
        for (q = queue; q; q = q->next) {
            if (q->fname && !strcmp (q->fname, fname) && width == q->width) {
                if (q->numcb < MAX_CALLBACKS && callback) {
                    q->callbacks[q->numcb].cb = callback;
                    q->callbacks[q->numcb].ud = user_data;
                    q->numcb++;
                }
                deadbeef->mutex_unlock (mutex);
                return;
            }
        }
    }
    q = malloc (sizeof (load_query_t));
    memset (q, 0, sizeof (load_query_t));
    if (fname) {
        q->fname = strdup (fname);
    }
    q->width = width;
    q->callbacks[q->numcb].cb = callback;
    q->callbacks[q->numcb].ud = user_data;
    q->numcb++;
    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);
}

void
loading_thread (void *none) {
#ifdef __linux__
    prctl (PR_SET_NAME, "deadbeef-gtkui-artwork", 0, 0, 0, 0);
#endif
    for (;;) {
        trace ("covercache: waiting for signal\n");
        deadbeef->cond_wait (cond, mutex);
        trace ("covercache: signal received (terminate=%d, queue=%p)\n", terminate, queue);
        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 (!queue->fname) {
                for (int i = 0; i < queue->numcb; i++) {
                    if (queue->callbacks[i].cb) {
                        queue->callbacks[i].cb (queue->callbacks[i].ud);
                    }
                }
                queue_pop ();
                continue;
            }

            if (cache_min == -1) {
                trace ("coverart pixbuf cache overflow, waiting...\n");
                usleep (500000);
                continue;
            }
            GdkPixbuf *pixbuf = NULL;
            GError *error = NULL;
            pixbuf = gdk_pixbuf_new_from_file_at_scale (queue->fname, queue->width, queue->width, TRUE, &error);
            if (error) {
                //fprintf (stderr, "gdk_pixbuf_new_from_file_at_scale %s %d failed, error: %s\n", queue->fname, queue->width, error ? error->message : "n/a");
                g_error_free (error);
                error = NULL;
            }
            if (!pixbuf) {
                pixbuf = pixbuf_default;
                g_object_ref (pixbuf);
            }
            if (cache_min != -1) {
                deadbeef->mutex_lock (mutex);
                cache[cache_min].pixbuf = pixbuf;
                cache[cache_min].fname = strdup (queue->fname);
                gettimeofday (&cache[cache_min].tm, NULL);
                cache[cache_min].width = queue->width;
                deadbeef->mutex_unlock (mutex);
            }

            for (int i = 0; i < queue->numcb; i++) {
                if (queue->callbacks[i].cb) {
                    queue->callbacks[i].cb (queue->callbacks[i].ud);
                }
            }
            queue_pop ();
        }
        if (terminate) {
            break;
        }
    }
}

typedef struct {
    int width;
    void (*callback)(void *user_data);
    void *user_data;
} cover_avail_info_t;

static void
cover_avail_callback (const char *fname, const char *artist, const char *album, void *user_data) {
    if (!fname) {
        free (user_data);
        return;
    }
    cover_avail_info_t *dt = user_data;
    // means requested image is now in disk cache
    // load it into main memory
    GdkPixbuf *pb = get_cover_art_callb (fname, artist, album, dt->width, dt->callback, dt->user_data);
    if (pb) {
        g_object_unref (pb);
    }
    free (dt);
}

static GdkPixbuf *
get_pixbuf (const char *fname, int width, void (*callback)(void *user_data), void *user_data) {
    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) {
                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, callback, user_data);
    return NULL;
}

void
queue_cover_callback (void (*callback)(void *user_data), void *user_data) {
    queue_add (NULL, -1, callback, user_data);
}


GdkPixbuf *
get_cover_art_callb (const char *fname, const char *artist, const char *album, int width, void (*callback) (void *user_data), void *user_data) {
    if (!coverart_plugin) {
        return NULL;
    }

    if (width == -1) {
        char path[2048];
        coverart_plugin->make_cache_path2 (path, sizeof (path), fname, album, artist, -1);
        deadbeef->mutex_lock (mutex);
        int i_largest = -1;
        int size_largest = -1;
        for (int i = 0; i < CACHE_SIZE; i++) {
            if (!cache[i].pixbuf) {
                continue;
            }
            if (!strcmp (cache[i].fname, path)) {
                gettimeofday (&cache[i].tm, NULL);
                if (cache[i].width > size_largest) {
                    size_largest = cache[i].width;
                    i_largest = i;
                }
            }
        }
        if (i_largest != -1) {
            GdkPixbuf *pb = cache[i_largest].pixbuf;
            g_object_ref (pb);
            deadbeef->mutex_unlock (mutex);
            return pb;
        }
        deadbeef->mutex_unlock (mutex);
        return NULL;
    }

    cover_avail_info_t *dt = malloc (sizeof (cover_avail_info_t));
    dt->width = width;
    dt->callback = callback;
    dt->user_data = user_data;
    char *image_fname = coverart_plugin->get_album_art (fname, artist, album, -1, cover_avail_callback, dt);
    if (image_fname) {
        GdkPixbuf *pb = get_pixbuf (image_fname, width, callback, user_data);
        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));
    if (pixbuf_default) {
        g_object_unref (pixbuf_default);
        pixbuf_default = NULL;
    }
    deadbeef->cond_free (cond);
    cond = 0;
    deadbeef->mutex_free (mutex);
    mutex = 0;
}

GdkPixbuf *
cover_get_default_pixbuf (void) {
    if (!coverart_plugin) {
        return NULL;
    }
    if (!pixbuf_default) {
        GError *error = NULL;
        const char *defpath = coverart_plugin->get_default_cover ();
        pixbuf_default = gdk_pixbuf_new_from_file (defpath, &error);
        if (!pixbuf_default) {
            fprintf (stderr, "default cover: gdk_pixbuf_new_from_file %s failed, error: %s\n", defpath, error->message);
        }
        if (error) {
            g_error_free (error);
            error = NULL;
        }
        if (!pixbuf_default) {
            pixbuf_default = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, 2, 2);
        }
        assert (pixbuf_default);
    }

    g_object_ref (pixbuf_default);
    return pixbuf_default;
}

int
gtkui_is_default_pixbuf (GdkPixbuf *pb) {
    return pb == pixbuf_default;
}