/src/vlc/src/playlist/preparse.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * playlist/preparse.c |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2018 VLC authors and VideoLAN |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation; either version 2.1 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with this program; if not, write to the Free Software Foundation, |
18 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
19 | | *****************************************************************************/ |
20 | | |
21 | | #ifdef HAVE_CONFIG_H |
22 | | # include "config.h" |
23 | | #endif |
24 | | |
25 | | #include "preparse.h" |
26 | | |
27 | | #include "content.h" |
28 | | #include "item.h" |
29 | | #include "playlist.h" |
30 | | #include "notify.h" |
31 | | |
32 | | typedef struct VLC_VECTOR(input_item_t *) media_vector_t; |
33 | | |
34 | | static void |
35 | | vlc_playlist_CollectChildren(vlc_playlist_t *playlist, |
36 | | media_vector_t *dest, |
37 | | const input_item_node_t *node) |
38 | 0 | { |
39 | 0 | vlc_playlist_AssertLocked(playlist); |
40 | 0 | for (int i = 0; i < node->i_children; ++i) |
41 | 0 | { |
42 | 0 | input_item_node_t *child = node->pp_children[i]; |
43 | 0 | input_item_t *media = child->p_item; |
44 | 0 | vlc_vector_push(dest, media); |
45 | 0 | vlc_playlist_CollectChildren(playlist, dest, child); |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | | int |
50 | | vlc_playlist_ExpandItem(vlc_playlist_t *playlist, size_t index, |
51 | | const input_item_node_t *node) |
52 | 0 | { |
53 | 0 | vlc_playlist_AssertLocked(playlist); |
54 | |
|
55 | 0 | media_vector_t flatten = VLC_VECTOR_INITIALIZER; |
56 | 0 | vlc_playlist_CollectChildren(playlist, &flatten, node); |
57 | |
|
58 | 0 | int ret = vlc_playlist_Expand(playlist, index, flatten.data, flatten.size); |
59 | 0 | vlc_vector_destroy(&flatten); |
60 | |
|
61 | 0 | return ret; |
62 | 0 | } |
63 | | |
64 | | int |
65 | | vlc_playlist_ExpandItemFromNode(vlc_playlist_t *playlist, |
66 | | const input_item_node_t *subitems) |
67 | 0 | { |
68 | 0 | vlc_playlist_AssertLocked(playlist); |
69 | 0 | input_item_t *media = subitems->p_item; |
70 | 0 | ssize_t index = vlc_playlist_IndexOfMedia(playlist, media); |
71 | 0 | if (index == -1) |
72 | 0 | return VLC_ENOENT; |
73 | | |
74 | | /* replace the item by its flatten subtree */ |
75 | 0 | return vlc_playlist_ExpandItem(playlist, index, subitems); |
76 | 0 | } |
77 | | |
78 | | static void |
79 | | on_subtree_added(vlc_preparser_req *req, input_item_node_t *subtree, |
80 | | void *userdata) |
81 | 0 | { |
82 | 0 | VLC_UNUSED(req); /* retrieved by subtree->p_item */ |
83 | 0 | vlc_playlist_t *playlist = userdata; |
84 | |
|
85 | 0 | vlc_playlist_Lock(playlist); |
86 | 0 | vlc_playlist_ExpandItemFromNode(playlist, subtree); |
87 | 0 | vlc_playlist_Unlock(playlist); |
88 | 0 | input_item_node_Delete(subtree); |
89 | 0 | } |
90 | | |
91 | | static void |
92 | | on_preparse_ended(vlc_preparser_req *req, int status, void *userdata) |
93 | 0 | { |
94 | | /* This function may be called synchronously on the calling thread if it was |
95 | | cancelled when the request had not started yet. The playlist cancels with |
96 | | the playlist lock held, so nothing here may take that lock before the status |
97 | | has been checked. */ |
98 | 0 | if (status != VLC_SUCCESS) |
99 | 0 | return; |
100 | | |
101 | 0 | input_item_t *media = vlc_preparser_req_GetItem(req); |
102 | 0 | vlc_playlist_t *playlist = userdata; |
103 | |
|
104 | 0 | vlc_playlist_Lock(playlist); |
105 | 0 | ssize_t index = vlc_playlist_IndexOfMedia(playlist, media); |
106 | 0 | if (index != -1) |
107 | 0 | vlc_playlist_Notify(playlist, on_items_updated, index, |
108 | 0 | &playlist->items.data[index], 1); |
109 | 0 | vlc_playlist_Unlock(playlist); |
110 | 0 | } |
111 | | |
112 | | static const struct vlc_preparser_cbs preparser_callbacks = { |
113 | | .on_ended = on_preparse_ended, |
114 | | .on_subtree_added = on_subtree_added, |
115 | | }; |
116 | | |
117 | | void |
118 | | vlc_playlist_AutoPreparse(vlc_playlist_t *playlist, vlc_playlist_item_t *item, |
119 | | bool parse_subitems) |
120 | 0 | { |
121 | | #ifdef TEST_PLAYLIST |
122 | | VLC_UNUSED(preparser_callbacks); |
123 | | #endif |
124 | |
|
125 | 0 | input_item_t *input = item->media; |
126 | 0 | assert(item->preparser_req == NULL); |
127 | |
|
128 | 0 | if (playlist->parser != NULL && !input_item_IsPreparsed(input)) |
129 | 0 | { |
130 | 0 | switch (playlist->recursive) |
131 | 0 | { |
132 | 0 | case VLC_PLAYLIST_PREPARSING_ENABLED: |
133 | 0 | parse_subitems = false; |
134 | 0 | break; |
135 | 0 | case VLC_PLAYLIST_PREPARSING_COLLAPSE: |
136 | 0 | break; |
137 | 0 | case VLC_PLAYLIST_PREPARSING_RECURSIVE: |
138 | 0 | parse_subitems = true; |
139 | 0 | break; |
140 | 0 | default: vlc_assert_unreachable(); |
141 | 0 | } |
142 | | |
143 | 0 | bool input_net; |
144 | 0 | enum input_item_type_e input_type = input_item_GetType(input, &input_net); |
145 | |
|
146 | 0 | if (input_net) |
147 | 0 | return; |
148 | | |
149 | 0 | switch (input_type) |
150 | 0 | { |
151 | 0 | case ITEM_TYPE_NODE: |
152 | 0 | case ITEM_TYPE_FILE: |
153 | 0 | case ITEM_TYPE_DIRECTORY: |
154 | 0 | case ITEM_TYPE_PLAYLIST: |
155 | 0 | break; |
156 | 0 | default: |
157 | 0 | return; |
158 | 0 | } |
159 | | |
160 | 0 | int options = VLC_PREPARSER_TYPE_PARSE | VLC_PREPARSER_TYPE_FETCHMETA_LOCAL; |
161 | 0 | if (parse_subitems) |
162 | 0 | options |= VLC_PREPARSER_OPTION_SUBITEMS; |
163 | |
|
164 | 0 | vlc_preparser_req *req = |
165 | 0 | vlc_preparser_req_NewParse(playlist->parser, input, options, |
166 | 0 | &preparser_callbacks, playlist); |
167 | 0 | if (req == NULL) |
168 | 0 | return; |
169 | | |
170 | | /* Store the request before submitting it. The callbacks may run |
171 | | even before vlc_preparser_Submit returns. */ |
172 | 0 | item->preparser_req = req; |
173 | |
|
174 | 0 | if (vlc_preparser_Submit(playlist->parser, req) != VLC_SUCCESS) |
175 | 0 | { |
176 | | item->preparser_req = NULL; |
177 | 0 | vlc_preparser_req_Release(req); |
178 | 0 | } |
179 | 0 | } |
180 | 0 | } |