summaryrefslogtreecommitdiff
path: root/src/libaudcore/scanner.cc
blob: 75cb04a5c97ba640d6724e5b88eef81230783081 (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
/*
 * scanner.c
 * Copyright 2012-2016 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "scanner.h"

#include <glib.h>  /* for GThreadPool */

#include "audstrings.h"
#include "cue-cache.h"
#include "i18n.h"
#include "internal.h"
#include "plugins.h"
#include "probe.h"
#include "tuple.h"
#include "vfs.h"

static GThreadPool * pool;

ScanRequest::ScanRequest (const String & filename, int flags, Callback callback,
 PluginHandle * decoder, Tuple && tuple) :
    filename (filename),
    flags (flags),
    callback (callback),
    decoder (decoder),
    tuple (std::move (tuple)),
    ip (nullptr)
{
    /* If this is a cuesheet entry (and it has not already been loaded), capture
     * a reference to the cache immediately.  During a playlist scan, requests
     * have overlapping lifecycles--each new ScanRequest is created by the
     * callback of the previous request--so the cached cuesheet persists as long
     * as consecutive playlist entries reference it. */
    if (! this->tuple.valid () && is_cuesheet_entry (filename))
        cue_cache.capture (new CueCacheRef (strip_subtune (filename)));
}

void ScanRequest::read_cuesheet_entry ()
{
    for (auto & item : cue_cache->load ())
    {
        if (item.filename == filename)
        {
            decoder = item.decoder;
            tuple = item.tuple.ref ();
            break;
        }
    }
}

void ScanRequest::run ()
{
    /* load cuesheet entry (possibly cached) */
    if (cue_cache)
        read_cuesheet_entry ();

    /* for a cuesheet entry, determine the source filename */
    String audio_file = tuple.get_str (Tuple::AudioFile);
    if (! audio_file)
        audio_file = filename;

    bool need_tuple = (flags & SCAN_TUPLE) && ! tuple.valid ();
    bool need_image = (flags & SCAN_IMAGE);

    if (! decoder)
        decoder = aud_file_find_decoder (audio_file, false, file, & error);
    if (! decoder)
        goto err;

    if (need_tuple || need_image)
    {
        if (! (ip = load_input_plugin (decoder, & error)))
            goto err;

        Index<char> * pimage = need_image ? & image_data : nullptr;
        if (! aud_file_read_tag (audio_file, decoder, file, tuple, pimage, & error))
            goto err;

        if ((flags & SCAN_IMAGE) && ! image_data.len ())
            image_file = art_search (audio_file);
    }

    /* rewind/reopen the input file */
    if ((flags & SCAN_FILE))
        open_input_file (audio_file, "r", ip, file, & error);
    else
    {
    err:
        /* close file if not needed or if an error occurred */
        file = VFSFile ();
    }

    callback (this);
}

static void scan_worker (void * data, void *)
{
    ((ScanRequest *) data)->run ();
    delete (ScanRequest *) data;
}

void scanner_init ()
{
    pool = g_thread_pool_new (scan_worker, nullptr, SCAN_THREADS, false, nullptr);
}

void scanner_request (ScanRequest * request)
{
    g_thread_pool_push (pool, request, nullptr);
}

void scanner_cleanup ()
{
    g_thread_pool_free (pool, false, true);
}