summaryrefslogtreecommitdiffstats
path: root/common/playlist.c
blob: 1fd202b98faf7e1d282a07dafedd9257add72610 (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
/*
 * This file is part of mpv.
 *
 * mpv 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 (at your option) any later version.
 *
 * mpv 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include "config.h"
#include "playlist.h"
#include "common/common.h"
#include "common/global.h"
#include "common/msg.h"
#include "mpv_talloc.h"
#include "options/path.h"

#include "demux/demux.h"
#include "stream/stream.h"

struct playlist_entry *playlist_entry_new(const char *filename)
{
    struct playlist_entry *e = talloc_zero(NULL, struct playlist_entry);
    char *local_filename = mp_file_url_to_filename(e, bstr0(filename));
    e->filename = local_filename ? local_filename : talloc_strdup(e, filename);
    e->stream_flags = STREAM_ORIGIN_DIRECT;
    e->original_index = -1;
    return e;
}

void playlist_entry_add_param(struct playlist_entry *e, bstr name, bstr value)
{
    struct playlist_param p = {bstrdup(e, name), bstrdup(e, value)};
    MP_TARRAY_APPEND(e, e->params, e->num_params, p);
}

void playlist_entry_add_params(struct playlist_entry *e,
                               struct playlist_param *params,
                               int num_params)
{
    for (int n = 0; n < num_params; n++)
        playlist_entry_add_param(e, params[n].name, params[n].value);
}

static void playlist_update_indexes(struct playlist *pl, int start, int end)
{
    start = MPMAX(start, 0);
    end = end < 0 ? pl->num_entries : MPMIN(end, pl->num_entries);

    for (int n = start; n < end; n++)
        pl->entries[n]->pl_index = n;
}

void playlist_add(struct playlist *pl, struct playlist_entry *add)
{
    MP_TARRAY_APPEND(pl, pl->entries, pl->num_entries, add);
    add->pl = pl;
    add->pl_index = pl->num_entries - 1;
    talloc_steal(pl, add);
}

void playlist_entry_unref(struct playlist_entry *e)
{
    e->reserved--;
    if (e->reserved < 0) {
        assert(!e->pl);
        talloc_free(e);
    }
}

void playlist_remove(struct playlist *pl, struct playlist_entry *entry)
{
    assert(pl && entry->pl == pl);

    if (pl->current == entry) {
        pl->current = playlist_entry_get_rel(entry, 1);
        pl->current_was_replaced = true;
    }

    MP_TARRAY_REMOVE_AT(pl->entries, pl->num_entries, entry->pl_index);
    playlist_update_indexes(pl, entry->pl_index, -1);

    entry->pl = NULL;
    entry->pl_index = -1;
    ta_set_parent(entry, NULL);

    entry->removed = true;
    playlist_entry_unref(entry);
}

void playlist_clear(struct playlist *pl)
{
    for (int n = pl->num_entries - 1; n >= 0; n--)
        playlist_remove(pl, pl->entries[n]);
    assert(!pl->current);
    pl->current_was_replaced = false;
}

void playlist_clear_except_current(struct playlist *pl)
{
    for (int n = pl->num_entries - 1; n >= 0; n--) {
        if (pl->entries[n] != pl->current)
            playlist_remove(pl, pl->entries[n]);
    }
}

// Moves the entry so that it takes "at"'s place (or move to end, if at==NULL).
void playlist_move(struct playlist *pl, struct playlist_entry *entry,
                   struct playlist_entry *at)
{
    if (entry == at)
        return;

    assert(entry && entry->pl == pl);
    assert(!at || at->pl == pl);

    int index = at ? at->pl_index : pl->num_entries;
    MP_TARRAY_INSERT_AT(pl, pl->entries, pl->num_entries, index, entry);

    int old_index = entry->pl_index;
    if (old_index >= index)
        old_index += 1;
    MP_TARRAY_REMOVE_AT(pl->entries, pl->num_entries, old_index);

    playlist_update_indexes(pl, MPMIN(index - 1, old_index - 1),
                                MPMAX(index + 1, old_index + 1));
}

void playlist_add_file(struct playlist *pl, const char *filename)
{
    playlist_add(pl, playlist_entry_new(filename));
}

void playlist_shuffle(struct playlist *pl)
{
    for (int n = 0; n < pl->num_entries; n++)
        pl->entries[n]->original_index = n;
    for (int n = 0; n < pl->num_entries - 1; n++) {
        int j = (int)((double)(pl->num_entries - n) * rand() / (RAND_MAX + 1.0));
        MPSWAP(struct playlist_entry *, pl->entries[n], pl->entries[n + j]);
    }
    playlist_update_indexes(pl, 0, -1);
}

#define CMP_INT(a, b) ((a) == (b) ? 0 : ((a) > (b) ? 1 : -1))

static int cmp_unshuffle(const void *a, const void *b)
{
    struct playlist_entry *ea = *(struct playlist_entry **)a;
    struct playlist_entry *eb = *(struct playlist_entry **)b;

    if (ea->original_index >= 0 && ea->original_index != eb->original_index)
        return CMP_INT(ea->original_index, eb->original_index);
    return CMP_INT(ea->pl_index, eb->pl_index);
}

void playlist_unshuffle(struct playlist *pl)
{
    if (pl->num_entries)
        qsort(pl->entries, pl->num_entries, sizeof(pl->entries[0]), cmp_unshuffle);
    playlist_update_indexes(pl, 0, -1);
}

// (Explicitly ignores current_was_replaced.)
struct playlist_entry *playlist_get_first(struct playlist *pl)
{
    return pl->num_entries ? pl->entries[0] : NULL;
}

// (Explicitly ignores current_was_replaced.)
struct playlist_entry *playlist_get_last(struct playlist *pl)
{
    return pl->num_entries ? pl->entries[pl->num_entries - 1] : NULL;
}

struct playlist_entry *playlist_get_next(struct playlist *pl, int direction)
{
    assert(direction == -1 || direction == +1);
    if (!pl->current)
        return NULL;
    assert(pl->current->pl == pl);
    if (direction < 0)
        return playlist_entry_get_rel(pl->current, -1);
    return pl->current_was_replaced ? pl->current :
           playlist_entry_get_rel(pl->current, 1);
}

// (Explicitly ignores current_was_replaced.)
struct playlist_entry *playlist_entry_get_rel(struct playlist_entry *e,
                                              int direction)
{
    assert(direction == -1 || direction == +1);
    if (!e->pl)
        return NULL;
    return playlist_entry_from_index(e->pl, e->pl_index + direction);
}

void playlist_add_base_path(struct playlist *pl, bstr base_path)
{
    if (base_path.len == 0 || bstrcmp0(base_path, ".") == 0)
        return;
    for (int n = 0; n < pl->num_entries; n++) {
        struct playlist_entry *e = pl->entries[n];
        if (!mp_is_url(bstr0(e->filename))) {
            char *new_file = mp_path_join_bstr(e, base_path, bstr0(e->filename));
            talloc_free(e->filename);
            e->filename = new_file;
        }
    }
}

// Add redirected_from as new redirect entry to each item in pl.
void playlist_add_redirect(struct playlist *pl, const char *redirected_from)
{
    for (int n = 0; n < pl->num_entries; n++) {
        struct playlist_entry *e = pl->entries[n];
        if (e->num_redirects >= 10) // arbitrary limit for sanity
            continue;
        char *s = talloc_strdup(e, redirected_from);
        if (s)
            MP_TARRAY_APPEND(e, e->redirects, e->num_redirects, s);
    }
}

void playlist_set_stream_flags(struct playlist *pl, int flags)
{
    for (int n = 0; n < pl->num_entries; n++)
        pl->entries[n]->stream_flags = flags;
}

static void playlist_transfer_entries_to(struct playlist *pl, int dst_index,
                                         struct playlist *source_pl)
{
    assert(pl != source_pl);

    int count = source_pl->num_entries;
    MP_TARRAY_INSERT_N_AT(pl, pl->entries, pl->num_entries, dst_index, count);

    for (int n = 0; n < count; n++) {
        struct playlist_entry *e = source_pl->entries[n];
        e->pl = pl;
        e->pl_index = dst_index + n;
        pl->entries[e->pl_index] = e;
        talloc_steal(pl, e);
    }

    playlist_update_indexes(pl, dst_index + count, -1);
    source_pl->num_entries = 0;
}

// Move all entries from source_pl to pl, appending them after the current entry
// of pl. source_pl will be empty, and all entries have changed ownership to pl.
void playlist_transfer_entries(struct playlist *pl, struct playlist *source_pl)
{

    int add_at = pl->num_entries;
    if (pl->current) {
        add_at = pl->current->pl_index + 1;
        if (pl->current_was_replaced)
            add_at += 1;
    }
    assert(add_at >= 0);
    assert(add_at <= pl->num_entries);

    playlist_transfer_entries_to(pl, add_at, source_pl);
}

void playlist_append_entries(struct playlist *pl, struct playlist *source_pl)
{
    playlist_transfer_entries_to(pl, pl->num_entries, source_pl);
}

// Return number of entries between list start and e.
// Return -1 if e is not on the list, or if e is NULL.
int playlist_entry_to_index(struct playlist *pl, struct playlist_entry *e)
{
    if (!e)
        return -1;
    assert(e->pl == pl);
    return e->pl_index;
}

int playlist_entry_count(struct playlist *pl)
{
    return pl->num_entries;
}

// Return entry for which playlist_entry_to_index() would return index.
// Return NULL if not found.
struct playlist_entry *playlist_entry_from_index(struct playlist *pl, int index)
{
    return index >= 0 && index < pl->num_entries ? pl->entries[index] : NULL;
}

struct playlist *playlist_parse_file(const char *file, struct mp_cancel *cancel,
                                     struct mpv_global *global)
{
    struct mp_log *log = mp_log_new(NULL, global->log, "!playlist_parser");
    mp_verbose(log, "Parsing playlist file %s...\n", file);

    struct demuxer_params p = {
        .force_format = "playlist",
        .stream_flags = STREAM_ORIGIN_DIRECT,
    };
    struct demuxer *d = demux_open_url(file, &p, cancel, global);
    if (!d) {
        talloc_free(log);
        return NULL;
    }

    struct playlist *ret = NULL;
    if (d && d->playlist) {
        ret = talloc_zero(NULL, struct playlist);
        playlist_transfer_entries(ret, d->playlist);
        if (d->filetype && strcmp(d->filetype, "hls") == 0) {
            mp_warn(log, "This might be a HLS stream. For correct operation, "
                         "pass it to the player\ndirectly. Don't use --playlist.\n");
        }
    }
    demux_free(d);

    if (ret) {
        mp_verbose(log, "Playlist successfully parsed\n");
    } else {
        mp_err(log, "Error while parsing playlist\n");
    }

    if (ret && !ret->num_entries)
        mp_warn(log, "Warning: empty playlist\n");

    talloc_free(log);
    return ret;
}