command: some minor corrections to previous commit

The last commit was fine - just making some enhancements.

Rename the function to parse_node_chapters(), since it really has not
much to do with Lua.

Don't use len<0 as check whether it makes sense to set chapters, and
instead check for mpctx->demuxer (that includes the possibility to set
chapters e.g. within a preload hook, but after chapters are initialized
from the demuxer).

Return M_PROPERTY_ERROR instead of M_PROPERTY_OK if the mpv_node has the
wrong type.

It's ok if a chapter has no title, so change the checks accordingly.

Remove a Yoda condition.

Notify that chapter metadata might have changed with mp_notify() (the
chapter list itself is already taken care by generic code).

Fix leaking the metadata allocations of the new chapter list.
This commit is contained in:
wm4 2016-09-24 17:27:19 +02:00
parent e2e9a7faa8
commit 68d2903cb1
1 changed files with 21 additions and 22 deletions

View File

@ -972,39 +972,35 @@ static int get_chapter_entry(int item, int action, void *arg, void *ctx)
return r;
}
static int parse_lua_chapters(struct MPContext *mpctx,
struct mpv_node *given_chapters)
static int parse_node_chapters(struct MPContext *mpctx,
struct mpv_node *given_chapters)
{
if (given_chapters->format != MPV_FORMAT_NODE_ARRAY)
return M_PROPERTY_OK;
int num_chapters_given = given_chapters->u.list->num;
double len = get_time_length(mpctx);
if (len < 0)
if (!mpctx->demuxer)
return M_PROPERTY_UNAVAILABLE;
if (given_chapters->format != MPV_FORMAT_NODE_ARRAY)
return M_PROPERTY_ERROR;
double len = get_time_length(mpctx);
talloc_free(mpctx->chapters);
mpctx->num_chapters = 0;
mpctx->chapters = NULL;
mpctx->chapters = talloc_array(NULL, struct demux_chapter, 0);
for (int n = 0; n < num_chapters_given; n++) {
for (int n = 0; n < given_chapters->u.list->num; n++) {
struct mpv_node *chapter_data = &given_chapters->u.list->values[n];
if (chapter_data->format != MPV_FORMAT_NODE_MAP)
continue;
mpv_node_list *chapter_data_elements = chapter_data->u.list;
// there should be at least 2 elements: time, title
int elements_count = chapter_data_elements->num;
if (elements_count < 2)
continue;
double time = -1;
char *title = 0;
for (int e = 0; e < elements_count; e++) {
struct mpv_node *chapter_data_element = &chapter_data_elements->values[e];
for (int e = 0; e < chapter_data_elements->num; e++) {
struct mpv_node *chapter_data_element =
&chapter_data_elements->values[e];
char *key = chapter_data_elements->keys[e];
switch (chapter_data_element->format) {
case MPV_FORMAT_INT64:
@ -1022,16 +1018,19 @@ static int parse_lua_chapters(struct MPContext *mpctx,
}
}
if (0 <= time && time < len && title) {
if (time >= 0 && time < len) {
struct demux_chapter new = {
.pts = time,
.metadata = talloc_zero(NULL, struct mp_tags),
.metadata = talloc_zero(mpctx->chapters, struct mp_tags),
};
mp_tags_set_str(new.metadata, "title", title);
if (title)
mp_tags_set_str(new.metadata, "title", title);
MP_TARRAY_APPEND(NULL, mpctx->chapters, mpctx->num_chapters, new);
}
}
mp_notify(mpctx, MPV_EVENT_CHAPTER_CHANGE, NULL);
return M_PROPERTY_OK;
}
@ -1066,7 +1065,7 @@ static int mp_property_list_chapters(void *ctx, struct m_property *prop,
}
case M_PROPERTY_SET: {
struct mpv_node *given_chapters = arg;
return parse_lua_chapters(mpctx, given_chapters);
return parse_node_chapters(mpctx, given_chapters);
}
}
return m_property_read_list(action, arg, count, get_chapter_entry, mpctx);