Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

command: make playlist writable #14990

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DOCS/interface-changes/writable-playlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- make `playlist` property writable
35 changes: 34 additions & 1 deletion DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3029,7 +3029,7 @@ Property list
the entries. Unavailable if the file was not originally associated with a
playlist in some way.

``playlist``
``playlist`` (RW)
Playlist, current entry marked. Currently, the raw property value is
useless.

Expand Down Expand Up @@ -3081,6 +3081,39 @@ Property list
"title" MPV_FORMAT_STRING (optional)
"id" MPV_FORMAT_INT64

Writing this property is also possible using the following format:

::

MPV_FORMAT_NODE_ARRAY (the new playlist)
MPV_FORMAT_STRING (entry from filename)

MPV_FORMAT_INT64 (take existing entry at index, counts from 0)

MPV_FORMAT_NODE_MAP (take existing entry from id)
"id" MPV_FORMAT_INT64
"current" MPV_FORMAT_FLAG (optional)
"title" MPV_FORMAT_STRING|MPV_FORMAT_NONE (optional)

MPV_FORMAT_NODE_MAP (entry from filename)
"filename" MPV_FORMAT_STRING
"current" MPV_FORMAT_FLAG (optional)
"title" MPV_FORMAT_STRING|MPV_FORMAT_NONE (optional)

.. admonition:: Lua examples

``mp.set_property_native("playlist", {1, 0})``

Swap the first two entries and remove the rest.

``mp.set_property_native("playlist", {{filename="foo", current=true}})``

Replace playlist with a single entry and start playing.

``mp.set_property_native("playlist", {1, {id=2}, "bar"})``

Various ways to specify an entry.

``track-list``
List of audio/video/sub tracks, current entry marked. Currently, the raw
property value is useless.
Expand Down
49 changes: 43 additions & 6 deletions common/playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
#include "misc/random.h"
#include "mpv_talloc.h"
#include "options/path.h"
#include "misc/khash.h"

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

KHASH_MAP_INIT_INT64(playlist_id_map, int)

typedef khash_t(playlist_id_map) playlist_id_map_t;

struct playlist_entry *playlist_entry_new(const char *filename)
{
struct playlist_entry *e = talloc_zero(NULL, struct playlist_entry);
Expand Down Expand Up @@ -89,6 +94,16 @@ void playlist_entry_unref(struct playlist_entry *e)
}
}

void playlist_entry_remove(struct playlist_entry *entry)
{
entry->pl = NULL;
entry->pl_index = -1;
ta_set_parent(entry, NULL);

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

void playlist_remove(struct playlist *pl, struct playlist_entry *entry)
{
assert(pl && entry->pl == pl);
Expand All @@ -101,12 +116,7 @@ void playlist_remove(struct playlist *pl, struct playlist_entry *entry)
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);
playlist_entry_remove(entry);
}

void playlist_clear(struct playlist *pl)
Expand Down Expand Up @@ -456,3 +466,30 @@ void playlist_set_current(struct playlist *pl)
}
}
}

playlist_id_map_t *playlist_build_id_map(const struct playlist *pl)
{
khash_t(playlist_id_map) *h = kh_init(playlist_id_map);
for (int i = 0; i < pl->num_entries; ++i) {
struct playlist_entry *e = pl->entries[i];
int absent;
khint_t k = kh_put(playlist_id_map, h, e->id, &absent);
kh_val(h, k) = i;
}
return h;
}

void playlist_destroy_id_map(playlist_id_map_t *h)
{
kh_destroy(playlist_id_map, h);
}

struct playlist_entry *playlist_entry_from_id(struct playlist *pl, const playlist_id_map_t *h, uint64_t id)
{
khint_t k = kh_get(playlist_id_map, h, id);
if (k == kh_end(h))
return NULL;

int index = kh_val(h, k);
return pl->entries[index];
}
9 changes: 9 additions & 0 deletions common/playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
#define MPLAYER_PLAYLIST_H

#include <stdbool.h>

#include "misc/bstr.h"

typedef struct kh_playlist_id_map_s playlist_id_map_t;

struct playlist_param {
bstr name, value;
};
Expand Down Expand Up @@ -127,4 +130,10 @@ void playlist_entry_unref(struct playlist_entry *e);

void playlist_set_current(struct playlist *pl);

playlist_id_map_t *playlist_build_id_map(const struct playlist *pl);
void playlist_destroy_id_map(playlist_id_map_t *h);
struct playlist_entry *playlist_entry_from_id(struct playlist *pl, const playlist_id_map_t *h, uint64_t id);

void playlist_entry_remove(struct playlist_entry *entry);

#endif
Loading
Loading