VLC 4.0.0-dev
Loading...
Searching...
No Matches
libvlc_downloader.h
Go to the documentation of this file.
1/*****************************************************************************
2 * libvlc_downloader.h: LibVLC Downloader API
3 *****************************************************************************
4 * Copyright (C) 2026 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#ifndef VLC_LIBVLC_DOWNLOADER_H
22#define VLC_LIBVLC_DOWNLOADER_H 1
23
24#include <vlc/libvlc.h>
25#include <vlc/libvlc_media.h>
27
28# ifdef __cplusplus
29extern "C" {
30# endif
31
32/** \defgroup libvlc_downloader LibVLC downloader
33 * \ingroup libvlc
34 * @ref libvlc_downloader_t is an abstract representation of a downloader
35 * @{
36 * \file
37 * LibVLC downloader API
38 */
40
41/**
42 * A downloader request object
43 */
45
46/**
47 * Opaque handle of a downloader task.
48 *
49 * Identifies a task request submitted via libvlc_downloader_queue().
50 * It can be passed to libvlc_downloader_cancel() to cancel that request,
51 * or to libvlc_downloader_set_pause() to pause/resume that request.
52 *
53 * \note Validity starts when libvlc_downloader_queue() returns a non-NULL handle
54 * and ends with libvlc_downloader_task_release().
55 */
57
58/**
59 * Downloader status
60 */
62{
63 /** download pending */
65 /** active download in progress (not paused) */
67 /** download paused */
69 /** download finished */
71 /** download cancelled */
73 /** download error occurred */
76
77/** Sentinel return value to signal an error in the download (to be returned by on_buffer callback) */
78#define LIBVLC_DOWNLOADER_CB_ERROR ((ptrdiff_t)-1)
79
80/** Sentinel return value to cancel the download (to be returned by on_buffer callback) */
81#define LIBVLC_DOWNLOADER_CB_CANCEL ((ptrdiff_t)-2)
82
83/**
84 * Downloader callbacks
85 */
87{
88 /**
89 * Version of struct libvlc_downloader_cbs
90 */
91 uint32_t version;
92
93 /**
94 * Called when a buffer of data is read.
95 *
96 * \note Mandatory (can't be NULL),
97 * available since version 0
98 *
99 * \warning Do not call any libvlc_downloader_* API from within this callback.
100 * Only libvlc_downloader_task_* APIs may be called on the provided task handle.
101 * And avoid blocking operations in this callback as it is invoked with the internal lock held.
102 *
103 * \param opaque user data
104 * \param task opaque handle returned by libvlc_downloader_queue()
105 * \param buf pointer to buffer (owned by downloader, only valid during callback)
106 * \param len size of buffer
107 * \param position total number of bytes read by the downloader so far
108 * \param total total size of the media in bytes (only medias with finite size are allowed to download)
109 *
110 * \return The number of bytes consumed by the user, or \ref LIBVLC_DOWNLOADER_CB_ERROR to
111 * terminate the download with an error, or \ref LIBVLC_DOWNLOADER_CB_CANCEL to cancel the download.
112 *
113 * If the returned number of bytes consumed is less than \p len (partial read), the downloader
114 * auto-pauses as a form of backpressure. The user must call `libvlc_downloader_set_pause(dl, task, false)`
115 * from the main thread to resume when ready to consume data again.
116 *
117 * \note User must copy the data if it needs to be accessed later.
118 * The maximum buffer size is limited to around 64 KB.
119 */
120 ptrdiff_t (*on_buffer)(void *opaque, libvlc_downloader_task *task,
121 const uint8_t *buf, size_t len,
122 uint64_t position, uint64_t total);
123
124 /**
125 * Called when the downloader state changes.
126 *
127 * \note Mandatory (can't be NULL),
128 * available since version 0
129 *
130 * Invoked when download starts, pauses, resumes, cancels, finishes
131 * or errors out. \ref libvlc_downloader_status_t
132 *
133 * \warning Do not call any libvlc_downloader_* API from within this callback.
134 * Only libvlc_downloader_task_* APIs may be called on the provided task handle.
135 * And avoid blocking operations in this callback as it is invoked with the internal lock held.
136 *
137 * \param opaque user data
138 * \param task opaque handle returned by libvlc_downloader_queue()
139 * \param status download status
140 */
143
144 /**
145 * Called when subitems of the media are available.
146 *
147 * \note Optional (can be NULL),
148 * available since version 0
149 *
150 * \param opaque user data
151 * \param task opaque handle returned by libvlc_downloader_queue()
152 * \param subitems media list of subitems (owned by LibVLC)
153 */
155 libvlc_media_list_t *subitems);
156
157 /**
158 * Called when the parsed media has slaves.
159 *
160 * \note Optional (can be NULL),
161 * available since version 0
162 *
163 * \param opaque user data
164 * \param task opaque handle returned by libvlc_downloader_queue()
165 * \param slaves array of libvlc_media_slave_t* (owned by LibVLC)
166 * \param count number of slaves
167 */
168 void (*on_slaves)(void *opaque, libvlc_downloader_task *task,
169 libvlc_media_slave_t **slaves, size_t count);
170};
171
172/**
173 * struct defining a downloader request
174 */
176{
177 /**
178 * Version of libvlc_downloader_request_t
179 */
180 uint32_t version;
181
182 /**
183 * Media to download
184 *
185 * \note Mandatory (can't be NULL),
186 * available since version 0
187 *
188 * - Only finite-size media are allowed to download.
189 *
190 * - If the media is a playlist or directory, the user will be notified of the
191 * subitems via the on_subitems callback and the download will not proceed.
192 *
193 * - If the media is a livestream or unknown type, the download will error out.
194 */
196};
197
198/**
199 * struct defining downloader configuration
200 */
202{
203 /**
204 * Version of struct libvlc_downloader_cfg
205 */
206 uint32_t version;
207
208 /**
209 * The maximum number of threads used by the parser internally, 0 for default
210 * (1 thread)
211 *
212 * \note Optional (can be 0),
213 * available since version 0
214 */
216};
217
218/**
219 * Create a downloader instance.
220 *
221 * Supports downloading files over a limited set of protocols:
222 * http(s), ftp, file, nfs, smb, sftp
223 *
224 * The downloader must be released by calling libvlc_downloader_destroy()
225 * when it is no longer needed.
226 *
227 * \param inst LibVLC instance
228 * \param cfg a pointer to a valid downloader configuration struct
229 * \return downloader instance or NULL on error
230 *
231 * \version LibVLC 4.0.0 or later
232 */
235
236/**
237 * Download a media asynchronously.
238 *
239 * - The downloader first parses the media.
240 *
241 * - If the media has subitems, the user will be notified via the on_subitems callback.
242 *
243 * - If the media has slaves, the user will be notified via the on_slaves callback.
244 *
245 * - If the media is not a file type,
246 * the download will not proceed. \see libvlc_media_type_t,
247 * and the user will be notified via the on_state_update callback.
248 *
249 * - If the media is a file type with finite size, the download starts in a separate thread.
250 *
251 * \param downloader downloader instance
252 * \param req a pointer to a valid request struct
253 * \param cbs a pointer to a valid callbacks struct. The pointed struct
254 * must be kept alive (and not modified) by the caller until libvlc_downloader_cbs.on_state_update()
255 * is called for the returned task handle with a terminal state (finished/cancelled/error).
256 * \param cbs_opaque opaque pointer for callbacks
257 * \return NULL in case of error, or a valid handle if the request was
258 * scheduled for downloading.
259 *
260 * \note No callbacks will be invoked if the return value is NULL.
261 *
262 * \version LibVLC 4.0.0 or later
263 */
266 const struct libvlc_downloader_cbs *cbs, void *cbs_opaque);
267
268/**
269 * Cancel an ongoing download.
270 *
271 * \param downloader downloader instance
272 * \param task a downloader task returned by libvlc_downloader_queue(),
273 * or NULL to cancel all requests.
274 *
275 * \return the number of requests cancelled
276 *
277 * \note
278 * - This function is valid only if the request is in one of the following states:
279 * pending, running, or paused.
280 *
281 * - When a request is cancelled, the `on_state_update` callback will be triggered
282 * with the cancelled state.
283 *
284 * - If the request is already in a terminated state (finished, cancelled, or error),
285 * the call is a no-op and no callback will be invoked.
286 *
287 * \version LibVLC 4.0.0 or later
288 */
290
291/**
292 * Toggle pause/resume for the download.
293 *
294 * \param downloader downloader instance
295 * \param task a valid downloader task returned by libvlc_downloader_queue()
296 * \param paused true to pause, false to resume
297 *
298 * \note This API is valid only when the download is in pending/running/paused state.
299 * And the on_state_update callback with paused/running state will be called only during these
300 * state changes. Else, for finished/cancelled/error states, it's a no-op and
301 * no callback will be called.
302 *
303 * \version LibVLC 4.0.0 or later
304 */
307 bool paused);
308
309/**
310 * Destroy a downloader and free resources.
311 * All pending, running and paused downloads are cancelled.
312 * Waits for all download threads to join.
313 *
314 * \param downloader downloader instance
315 *
316 * \version LibVLC 4.0.0 or later
317 */
319
320/**
321 * Get the media associated with the downloader request handle.
322 *
323 * \param task opaque handle returned by libvlc_downloader_queue()
324 * \return the media associated with the request handle.
325 *
326 * \note The returned media is held by the task, it must not be
327 * released by the caller.
328 *
329 * \version LibVLC 4.0.0 or later
330 */
333
334/**
335 * Release a downloader task handle.
336 *
337 * \param task the downloader task handle
338 *
339 * \note
340 * - The task handle is retained when returned by libvlc_downloader_queue().
341 *
342 * - Mandatory to call to avoid memory leaks.
343 *
344 * - It is safe to call this API from within the on_state_update callback, when it
345 * reports a terminal state (finished, cancelled, error) \see libvlc_downloader_status_t.
346 *
347 * - The task handle should not be used after calling this function.
348 *
349 * - If called on an active task, it doesn't cancel the task,
350 * use \ref libvlc_downloader_cancel() for that.
351 *
352 * \version LibVLC 4.0.0 or later
353 */
355
356/** @}*/
357
358# ifdef __cplusplus
359}
360# endif
361
362#endif /* VLC_LIBVLC_DOWNLOADER_H */
size_t count
Definition core.c:403
struct libvlc_instance_t libvlc_instance_t
This structure is opaque.
Definition libvlc.h:76
void libvlc_downloader_set_pause(libvlc_downloader_t *downloader, libvlc_downloader_task *task, bool paused)
Toggle pause/resume for the download.
size_t libvlc_downloader_cancel(libvlc_downloader_t *downloader, libvlc_downloader_task *task)
Cancel an ongoing download.
void libvlc_downloader_task_release(libvlc_downloader_task *task)
Release a downloader task handle.
libvlc_downloader_t * libvlc_downloader_new(libvlc_instance_t *inst, const struct libvlc_downloader_cfg *cfg)
Create a downloader instance.
struct libvlc_downloader_t libvlc_downloader_t
Definition libvlc_downloader.h:39
libvlc_downloader_status_t
Downloader status.
Definition libvlc_downloader.h:62
struct libvlc_downloader_task libvlc_downloader_task
Opaque handle of a downloader task.
Definition libvlc_downloader.h:56
libvlc_downloader_task * libvlc_downloader_queue(libvlc_downloader_t *downloader, const libvlc_downloader_request_t *req, const struct libvlc_downloader_cbs *cbs, void *cbs_opaque)
Download a media asynchronously.
void libvlc_downloader_destroy(libvlc_downloader_t *downloader)
Destroy a downloader and free resources.
libvlc_media_t * libvlc_downloader_task_get_media(libvlc_downloader_task *task)
Get the media associated with the downloader request handle.
@ libvlc_downloader_status_cancelled
download cancelled
Definition libvlc_downloader.h:72
@ libvlc_downloader_status_paused
download paused
Definition libvlc_downloader.h:68
@ libvlc_downloader_status_error
download error occurred
Definition libvlc_downloader.h:74
@ libvlc_downloader_status_finished
download finished
Definition libvlc_downloader.h:70
@ libvlc_downloader_status_running
active download in progress (not paused)
Definition libvlc_downloader.h:66
@ libvlc_downloader_status_pending
download pending
Definition libvlc_downloader.h:64
struct libvlc_media_list_t libvlc_media_list_t
Definition libvlc_media_list.h:40
struct libvlc_media_t libvlc_media_t
Definition libvlc_media.h:47
#define LIBVLC_API
Definition libvlc.h:42
LibVLC core external API.
LibVLC media item/descriptor external API.
LibVLC media list (playlist) external API.
Downloader callbacks.
Definition libvlc_downloader.h:87
ptrdiff_t(* on_buffer)(void *opaque, libvlc_downloader_task *task, const uint8_t *buf, size_t len, uint64_t position, uint64_t total)
Called when a buffer of data is read.
Definition libvlc_downloader.h:120
uint32_t version
Version of struct libvlc_downloader_cbs.
Definition libvlc_downloader.h:91
void(* on_state_update)(void *opaque, libvlc_downloader_task *task, libvlc_downloader_status_t status)
Called when the downloader state changes.
Definition libvlc_downloader.h:141
void(* on_slaves)(void *opaque, libvlc_downloader_task *task, libvlc_media_slave_t **slaves, size_t count)
Called when the parsed media has slaves.
Definition libvlc_downloader.h:168
void(* on_subitems)(void *opaque, libvlc_downloader_task *task, libvlc_media_list_t *subitems)
Called when subitems of the media are available.
Definition libvlc_downloader.h:154
struct defining downloader configuration
Definition libvlc_downloader.h:202
uint32_t version
Version of struct libvlc_downloader_cfg.
Definition libvlc_downloader.h:206
uint32_t max_parser_threads
The maximum number of threads used by the parser internally, 0 for default (1 thread).
Definition libvlc_downloader.h:215
struct defining a downloader request
Definition libvlc_downloader.h:176
libvlc_media_t * media
Media to download.
Definition libvlc_downloader.h:195
uint32_t version
Version of libvlc_downloader_request_t.
Definition libvlc_downloader.h:180
A slave of a libvlc_media_t.
Definition libvlc_media.h:156
Definition fetcher.c:54