preparser.c 9.89 KB
Newer Older
1
/*****************************************************************************
Filip Roséen's avatar
Filip Roséen committed
2
 * preparser.c
3
 *****************************************************************************
Filip Roséen's avatar
Filip Roséen committed
4
 * Copyright © 2017-2017 VLC authors and VideoLAN
5
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
6
7
8
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
9
10
11
12
 * (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
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
13
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
15
 *
Jean-Baptiste Kempf's avatar
LGPL    
Jean-Baptiste Kempf committed
16
17
18
 * You should have received a copy of the GNU Lesser 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.
19
 *****************************************************************************/
Filip Roséen's avatar
Filip Roséen committed
20

21
22
23
24
25
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
26
#include <vlc_atomic.h>
27
28
#include <vlc_executor.h>
#include <vlc_tick.h>
29

30
#include "input/input_interface.h"
Filip Roséen's avatar
Filip Roséen committed
31
32
33
#include "input/input_internal.h"
#include "preparser.h"
#include "fetcher.h"
34

35
struct input_preparser_t
36
{
Filip Roséen's avatar
Filip Roséen committed
37
    vlc_object_t* owner;
38
    input_fetcher_t* fetcher;
39
40
    vlc_executor_t *executor;
    vlc_tick_t default_timeout;
41
    atomic_bool deactivated;
42
43
};

44
struct input_preparser_task
45
{
46
    input_preparser_t *preparser;
47
    input_item_t *item;
48
    input_item_meta_request_option_t options;
49
50
51
    const input_preparser_callbacks_t *cbs;
    void *userdata;

52
    input_item_parser_id_t *parser;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    vlc_mutex_t lock;
    vlc_cond_t cond_ended;
    bool preparse_ended;
    int preparse_status;

    bool fetch_ended;

    atomic_bool interrupted;
};

static struct input_preparser_task *
TaskNew(input_preparser_t *preparser, input_item_t *item,
        input_item_meta_request_option_t options,
        const input_preparser_callbacks_t *cbs, void *userdata)
68
{
69
70
    struct input_preparser_task *task = malloc(sizeof(*task));
    if (!task)
71
72
        return NULL;

73
74
75
76
77
    task->preparser = preparser;
    task->item = item;
    task->options = options;
    task->cbs = cbs;
    task->userdata = userdata;
78
79
80

    input_item_Hold(item);

81
82
83
84
85
86
87
88
89
90
    task->parser = NULL;
    vlc_mutex_init(&task->lock);
    vlc_cond_init(&task->cond_ended);
    task->preparse_ended = false;
    task->preparse_status = ITEM_PREPARSE_SKIPPED;
    task->fetch_ended = false;

    atomic_init(&task->interrupted, false);

    return task;
91
92
}

93
94
static void
TaskDelete(struct input_preparser_task *task)
95
{
96
97
    input_item_Release(task->item);
    free(task);
98
99
}

100
101
static void
OnParserEnded(input_item_t *item, int status, void *task_)
102
{
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    VLC_UNUSED(item);
    struct input_preparser_task *task = task_;

    if (atomic_load(&task->interrupted))
        /*
         * On interruption, the call to input_item_parser_id_Release() may
         * trigger this "parser ended" callback.
         *
         * It must be ignored for two reasons:
         *  - the task lock is already held by this thread;
         *  - the preparse_status, already set, must not be overwritten.
         */
        return;

    vlc_mutex_lock(&task->lock);
    assert(!task->preparse_ended);
    task->preparse_status = status == VLC_SUCCESS ? ITEM_PREPARSE_DONE
                                                  : ITEM_PREPARSE_FAILED;
    task->preparse_ended = true;
    vlc_mutex_unlock(&task->lock);

    vlc_cond_signal(&task->cond_ended);
125
126
}

127
128
129
static void
OnParserSubtreeAdded(input_item_t *item, input_item_node_t *subtree,
                     void *task_)
130
{
131
    VLC_UNUSED(item);
132
    struct input_preparser_task *task = task_;
133

134
135
    if (task->cbs && task->cbs->on_subtree_added)
        task->cbs->on_subtree_added(task->item, subtree, task->userdata);
136
}
137

138
139
static void
OnArtFetchEnded(input_item_t *item, bool fetched, void *userdata)
140
141
{
    VLC_UNUSED(item);
142
143
144
145
146
147
148
149
    VLC_UNUSED(fetched);

    struct input_preparser_task *task = userdata;

    vlc_mutex_lock(&task->lock);
    assert(!task->fetch_ended);
    task->fetch_ended = true;
    vlc_mutex_unlock(&task->lock);
150

151
    vlc_cond_signal(&task->cond_ended);
152
153
}

154
155
156
157
158
159
static const input_fetcher_callbacks_t input_fetcher_callbacks = {
    .on_art_fetch_ended = OnArtFetchEnded,
};

static void
ParseLocked(struct input_preparser_task *task)
160
{
161
    vlc_mutex_assert(&task->lock);
162

163
164
    if (atomic_load(&task->interrupted))
        return;
165

166
167
168
169
170
    static const input_item_parser_cbs_t cbs = {
        .on_ended = OnParserEnded,
        .on_subtree_added = OnParserSubtreeAdded,
    };

171
172
173
174
175
176
177
    vlc_object_t *obj = task->preparser->owner;
    task->parser = input_item_Parse(task->item, obj, &cbs, task);
    if (!task->parser)
    {
        task->preparse_status = ITEM_PREPARSE_FAILED;
        return;
    }
178

179
180
181
    /* Wait until the end of parsing */
    while (!task->preparse_ended && !atomic_load(&task->interrupted))
        vlc_cond_wait(&task->cond_ended, &task->lock);
182

183
184
    /* This call also interrupts the parsing if it is still running */
    input_item_parser_id_Release(task->parser);
185
186
}

187
188
static void
FetchLocked(struct input_preparser_task *task)
189
{
190
    vlc_mutex_assert(&task->lock);
191

192
193
    if (atomic_load(&task->interrupted))
        return;
194

195
196
197
    input_fetcher_t *fetcher = task->preparser->fetcher;
    if (!fetcher || !(task->options & META_REQUEST_OPTION_FETCH_ANY))
        return;
198

199
200
201
202
203
204
    int ret =
        input_fetcher_Push(fetcher, task->item,
                           task->options & META_REQUEST_OPTION_FETCH_ANY,
                           &input_fetcher_callbacks, task);
    if (ret != VLC_SUCCESS)
        return;
205

206
207
208
    /* Wait until the end of fetching (fetching is not interruptible) */
    while (!task->fetch_ended)
        vlc_cond_wait(&task->cond_ended, &task->lock);
209
210
}

211
212
static void
RunnableRun(void *userdata)
Filip Roséen's avatar
Filip Roséen committed
213
{
214
    struct input_preparser_task *task = userdata;
215

216
217
218
219
    vlc_mutex_lock(&task->lock);
    ParseLocked(task);
    FetchLocked(task);
    vlc_mutex_unlock(&task->lock);
220

221
222
223
    if (!atomic_load(&task->interrupted))
        input_item_SetPreparsed(task->item, true);
}
224

225
226
227
228
static void
RunnableInterrupt(void *userdata)
{
    struct input_preparser_task *task = userdata;
Filip Roséen's avatar
Filip Roséen committed
229

230
231
232
233
    assert(!atomic_load(&task->interrupted));
    atomic_store(&task->interrupted, true);
    vlc_cond_signal(&task->cond_ended);
}
Filip Roséen's avatar
Filip Roséen committed
234

235
236
237
238
static void
RunnableOnFinished(void *userdata)
{
    struct input_preparser_task *task = userdata;
239

240
241
242
    if (task->cbs && task->cbs->on_preparse_ended)
        task->cbs->on_preparse_ended(task->item, task->preparse_status,
                                     task->userdata);
243

244
245
    TaskDelete(task);
}
246

247
input_preparser_t* input_preparser_New( vlc_object_t *parent )
248
{
249
    input_preparser_t* preparser = malloc( sizeof *preparser );
250
251
    if (!preparser)
        return NULL;
252

253
254
255
    int max_threads = var_InheritInteger(parent, "preparse-threads");
    if (max_threads < 1)
        max_threads = 1;
Filip Roséen's avatar
Filip Roséen committed
256

257
258
    preparser->executor = vlc_executor_New(max_threads);
    if (!preparser->executor)
Filip Roséen's avatar
Filip Roséen committed
259
    {
260
        free(preparser);
Filip Roséen's avatar
Filip Roséen committed
261
        return NULL;
262
    }
263

264
265
266
267
268
    preparser->default_timeout =
        VLC_TICK_FROM_MS(var_InheritInteger(parent, "preparse-timeout"));
    if (preparser->default_timeout < 0)
        preparser->default_timeout = 0;

Filip Roséen's avatar
Filip Roséen committed
269
    preparser->owner = parent;
270
    preparser->fetcher = input_fetcher_New( parent );
271
    atomic_init( &preparser->deactivated, false );
Filip Roséen's avatar
Filip Roséen committed
272
273
274
275
276

    if( unlikely( !preparser->fetcher ) )
        msg_Warn( parent, "unable to create art fetcher" );

    return preparser;
277
278
}

279
void input_preparser_Push( input_preparser_t *preparser,
Filip Roséen's avatar
Filip Roséen committed
280
    input_item_t *item, input_item_meta_request_option_t i_options,
281
    const input_preparser_callbacks_t *cbs, void *cbs_userdata,
282
    int timeout_ms, void *id )
283
{
284
285
286
    if( atomic_load( &preparser->deactivated ) )
        return;

Filip Roséen's avatar
Filip Roséen committed
287
    vlc_mutex_lock( &item->lock );
288
    enum input_item_type_e i_type = item->i_type;
Filip Roséen's avatar
Filip Roséen committed
289
    int b_net = item->b_net;
Thomas Guillem's avatar
Thomas Guillem committed
290
291
    if( i_options & META_REQUEST_OPTION_DO_INTERACT )
        item->b_preparse_interact = true;
Filip Roséen's avatar
Filip Roséen committed
292
    vlc_mutex_unlock( &item->lock );
293

Filip Roséen's avatar
Filip Roséen committed
294
    switch( i_type )
295
    {
Filip Roséen's avatar
Filip Roséen committed
296
297
298
299
300
301
        case ITEM_TYPE_NODE:
        case ITEM_TYPE_FILE:
        case ITEM_TYPE_DIRECTORY:
        case ITEM_TYPE_PLAYLIST:
            if( !b_net || i_options & META_REQUEST_OPTION_SCOPE_NETWORK )
                break;
Steve Lhomme's avatar
Steve Lhomme committed
302
            /* fallthrough */
Filip Roséen's avatar
Filip Roséen committed
303
        default:
304
305
            if (cbs && cbs->on_preparse_ended)
                cbs->on_preparse_ended(item, ITEM_PREPARSE_SKIPPED, cbs_userdata);
306
            return;
307
    }
Filip Roséen's avatar
Filip Roséen committed
308

309
310
311
312
313
314
315
    struct input_preparser_task *task =
        TaskNew(preparser, item, i_options, cbs, cbs_userdata);
    static const struct vlc_runnable runnable = {
        .run = RunnableRun,
        .interrupt = RunnableInterrupt,
        .on_finished = RunnableOnFinished,
    };
316

317
318
319
320
321
    vlc_tick_t timeout = timeout_ms == -1 ? preparser->default_timeout
                                          : VLC_TICK_FROM_MS(timeout_ms);
    if (vlc_executor_Submit(preparser->executor, &runnable, task, id, timeout))
    {
        if (cbs && cbs->on_preparse_ended)
322
            cbs->on_preparse_ended(item, ITEM_PREPARSE_FAILED, cbs_userdata);
323
324
        TaskDelete(task);
    }
325
326
}

327
void input_preparser_fetcher_Push( input_preparser_t *preparser,
328
329
    input_item_t *item, input_item_meta_request_option_t options,
    const input_fetcher_callbacks_t *cbs, void *cbs_userdata )
330
{
Filip Roséen's avatar
Filip Roséen committed
331
    if( preparser->fetcher )
332
        input_fetcher_Push( preparser->fetcher, item, options,
333
                            cbs, cbs_userdata );
Filip Roséen's avatar
Filip Roséen committed
334
}
335

336
void input_preparser_Cancel( input_preparser_t *preparser, void *id )
Filip Roséen's avatar
Filip Roséen committed
337
{
338
    vlc_executor_Cancel(preparser->executor, id);
339
340
}

341
void input_preparser_Deactivate( input_preparser_t* preparser )
342
343
{
    atomic_store( &preparser->deactivated, true );
344
    input_preparser_Cancel(preparser, NULL);
345
346
}

347
void input_preparser_Delete( input_preparser_t *preparser )
348
{
349
    vlc_executor_Delete(preparser->executor);
350

Filip Roséen's avatar
Filip Roséen committed
351
    if( preparser->fetcher )
352
        input_fetcher_Delete( preparser->fetcher );
353

Filip Roséen's avatar
Filip Roséen committed
354
355
    free( preparser );
}