From 68d2903cb1f3095070636ebb9e696594cc71f6c4 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 24 Sep 2016 17:27:19 +0200 Subject: 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. --- player/command.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'player/command.c') diff --git a/player/command.c b/player/command.c index c52aa7394a..6239aa5a52 100644 --- a/player/command.c +++ b/player/command.c @@ -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 (!mpctx->demuxer) + return M_PROPERTY_UNAVAILABLE; + if (given_chapters->format != MPV_FORMAT_NODE_ARRAY) - return M_PROPERTY_OK; + return M_PROPERTY_ERROR; - int num_chapters_given = given_chapters->u.list->num; double len = get_time_length(mpctx); - if (len < 0) - return M_PROPERTY_UNAVAILABLE; - 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); -- cgit v1.2.3