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 created by libvlc_downloader_task_new() and started with
50 * libvlc_downloader_submit().
51 * It can be passed to libvlc_downloader_cancel() to cancel that request,
52 * or to libvlc_downloader_set_pause() to pause/resume that request.
53 *
54 * \note Validity starts when libvlc_downloader_task_new() returns a non-NULL
55 * handle and ends with libvlc_downloader_task_release().
56 */
58
59/**
60 * Downloader status
61 */
63{
64 /** download pending */
66 /** active download in progress (not paused) */
68 /** download paused */
70 /** download finished */
72 /** download cancelled */
74 /** download error occurred */
77
78/** Sentinel return value to signal an error in the download (to be returned by on_buffer callback) */
79#define LIBVLC_DOWNLOADER_CB_ERROR ((ptrdiff_t)-1)
80
81/** Sentinel return value to cancel the download (to be returned by on_buffer callback) */
82#define LIBVLC_DOWNLOADER_CB_CANCEL ((ptrdiff_t)-2)
83
84/**
85 * Downloader callbacks
86 */
88{
89 /**
90 * Version of struct libvlc_downloader_cbs
91 */
92 uint32_t version;
93
94 /**
95 * Called when a buffer of data is read.
96 *
97 * \note Mandatory (can't be NULL),
98 * available since version 0
99 *
100 * \warning Do not call any libvlc_downloader_* API from within this callback.
101 * Only libvlc_downloader_task_* APIs may be called on the provided task handle.
102 * And avoid blocking operations in this callback as it is invoked with the internal lock held.
103 *
104 * \param opaque user data
105 * \param task opaque handle returned by libvlc_downloader_task_new()
106 * \param buf pointer to buffer (owned by downloader, only valid during callback)
107 * \param len size of buffer
108 * \param position total number of bytes read by the downloader so far
109 * \param total total size of the media in bytes (only medias with finite size are allowed to download)
110 *
111 * \return The number of bytes consumed by the user, or \ref LIBVLC_DOWNLOADER_CB_ERROR to
112 * terminate the download with an error, or \ref LIBVLC_DOWNLOADER_CB_CANCEL to cancel the download.
113 *
114 * If the returned number of bytes consumed is less than \p len (partial read), the downloader
115 * auto-pauses as a form of backpressure. The user must call `libvlc_downloader_set_pause(dl, task, false)`
116 * from the main thread to resume when ready to consume data again.
117 *
118 * \note User must copy the data if it needs to be accessed later.
119 * The maximum buffer size is limited to around 64 KB.
120 */
121 ptrdiff_t (*on_buffer)(void *opaque, libvlc_downloader_task *task,
122 const uint8_t *buf, size_t len,
123 uint64_t position, uint64_t total);
124
125 /**
126 * Called when the downloader state changes.
127 *
128 * \note Mandatory (can't be NULL),
129 * available since version 0
130 *
131 * Invoked when download starts, pauses, resumes, cancels, finishes
132 * or errors out. \ref libvlc_downloader_status_t
133 *
134 * \warning Do not call any libvlc_downloader_* API from within this callback.
135 * Only libvlc_downloader_task_* APIs may be called on the provided task handle.
136 * And avoid blocking operations in this callback as it is invoked with the internal lock held.
137 *
138 * \param opaque user data
139 * \param task opaque handle returned by libvlc_downloader_task_new()
140 * \param status download status
141 */
144
145 /**
146 * Called when subitems of the media are available.
147 *
148 * \note Optional (can be NULL),
149 * available since version 0
150 *
151 * \warning Do not call any libvlc_downloader_* API from within this callback.
152 * Only libvlc_downloader_task_* APIs may be called on the provided task handle.
153 *
154 * \param opaque user data
155 * \param task opaque handle returned by libvlc_downloader_task_new()
156 * \param subitems media list of subitems (owned by LibVLC)
157 */
159 libvlc_media_list_t *subitems);
160
161 /**
162 * Called when the parsed media has slaves.
163 *
164 * \note Optional (can be NULL),
165 * available since version 0
166 *
167 * \warning Do not call any libvlc_downloader_* API from within this callback.
168 * Only libvlc_downloader_task_* APIs may be called on the provided task handle.
169 *
170 * \param opaque user data
171 * \param task opaque handle returned by libvlc_downloader_task_new()
172 * \param slaves array of libvlc_media_slave_t* (owned by LibVLC)
173 * \param count number of slaves
174 */
175 void (*on_slaves)(void *opaque, libvlc_downloader_task *task,
176 libvlc_media_slave_t **slaves, size_t count);
177};
178
179/**
180 * struct defining a downloader request
181 */
183{
184 /**
185 * Version of libvlc_downloader_request_t
186 */
187 uint32_t version;
188
189 /**
190 * Media to download
191 *
192 * \note Mandatory (can't be NULL),
193 * available since version 0
194 *
195 * - Only finite-size media are allowed to download.
196 *
197 * - If the media is a playlist or directory, the download will not proceed
198 * and the task terminates with the error state. If it has subitems, the
199 * user is notified of them via the on_subitems callback before that.
200 *
201 * - If the media is a livestream or unknown type, the download will error out.
202 */
204};
205
206/**
207 * struct defining downloader configuration
208 */
210{
211 /**
212 * Version of struct libvlc_downloader_cfg
213 */
214 uint32_t version;
215
216 /**
217 * The maximum number of threads used by the parser internally, 0 for default
218 * (1 thread)
219 *
220 * \note Optional (can be 0),
221 * available since version 0
222 */
224};
225
226/**
227 * Create a downloader instance.
228 *
229 * Supports downloading files over a limited set of protocols:
230 * http(s), ftp, file, nfs, smb, sftp
231 *
232 * The downloader must be released by calling libvlc_downloader_destroy()
233 * when it is no longer needed.
234 *
235 * \param inst LibVLC instance
236 * \param cfg a pointer to a valid downloader configuration struct
237 * \return downloader instance or NULL on error
238 *
239 * \version LibVLC 4.0.0 or later
240 */
243
244/**
245 * Create a media download task.
246 *
247 * This prepares a task handle for downloading a media. Nothing runs and no
248 * callback can fire until the handle is passed to
249 * libvlc_downloader_submit().
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 task handle owned by the caller. It must
258 * be released with libvlc_downloader_task_release(), whether or not it is
259 * submitted.
260 *
261 * \version LibVLC 4.0.0 or later
262 */
265 const struct libvlc_downloader_cbs *cbs, void *cbs_opaque);
266
267/**
268 * Start a task created by libvlc_downloader_task_new()
269 *
270 * - The downloader first parses the media.
271 *
272 * - If the media has subitems, the user will be notified via the on_subitems callback.
273 *
274 * - If the media has slaves, the user will be notified via the on_slaves callback.
275 *
276 * - If the media is not a file type,
277 * the download will not proceed. \see libvlc_media_type_t,
278 * and the user will be notified via the on_state_update callback.
279 *
280 * - If the media is a file type with finite size, the download starts in a separate thread.
281 *
282 * On success the task is scheduled and the callbacks are guaranteed to be called
283 * and the task will eventually report a terminal state (finished/cancelled/error).
284 * That callback may run even before this function returns.
285 *
286 * \note On failure no callback is invoked. The caller keeps its reference and
287 * may submit the task again.
288 *
289 * \note A task may be submitted at most once, and may only be re-submitted if
290 * the previous attempt failed. Submitting does not transfer the caller's
291 * reference. It keeps owning the handle and must release it.
292 *
293 * \param downloader the downloader the task was created from
294 * \param task a task that has not yet been successfully submitted
295 * \return 0 on success, -1 on error
296 *
297 * \version LibVLC 4.0.0 or later
298 */
299LIBVLC_API int
301
302/**
303 * Cancel an ongoing download.
304 *
305 * \param downloader downloader instance
306 * \param task a downloader task returned by libvlc_downloader_task_new(),
307 * or NULL to cancel all submitted requests.
308 *
309 * \return the number of requests cancelled
310 *
311 * \note
312 * - This function is valid only if the request is in one of the following states:
313 * pending, running, or paused.
314 *
315 * - When a request is cancelled, the `on_state_update` callback will be triggered
316 * with the cancelled state.
317 *
318 * - If the request is already in a terminated state (finished, cancelled, or error),
319 * or if it was never submitted, the call is a no-op and no callback will be
320 * invoked.
321 *
322 * \version LibVLC 4.0.0 or later
323 */
325
326/**
327 * Toggle pause/resume for the download.
328 *
329 * \param downloader downloader instance
330 * \param task a valid downloader task returned by libvlc_downloader_task_new()
331 * \param paused true to pause, false to resume
332 *
333 * \note This API is valid only when the download is in pending/running/paused state.
334 * And the on_state_update callback with paused/running state will be called only during these
335 * state changes. Else, for finished/cancelled/error states, it's a no-op and
336 * no callback will be called.
337 *
338 * Pausing a pending task (including one not submitted yet) does not
339 * pause parsing. The on_subitems and on_slaves callbacks are still invoked,
340 * if available. If the download starts, the paused state is reported before
341 * any data is read (without a prior running state), and nothing is downloaded
342 * until the task is resumed.
343 *
344 * \version LibVLC 4.0.0 or later
345 */
348 bool paused);
349
350/**
351 * Destroy a downloader and free resources.
352 * All pending, running and paused downloads are cancelled.
353 * Waits for all download threads to join.
354 *
355 * \param downloader downloader instance
356 *
357 * \version LibVLC 4.0.0 or later
358 */
360
361/**
362 * Get the media associated with the downloader request handle.
363 *
364 * \param task opaque handle returned by libvlc_downloader_task_new()
365 * \return the media associated with the request handle.
366 *
367 * \note The returned media is held by the task, it must not be
368 * released by the caller.
369 *
370 * \version LibVLC 4.0.0 or later
371 */
374
375/**
376 * Release a downloader task handle.
377 *
378 * \param task the downloader task handle
379 *
380 * \note
381 * - The libvlc_downloader_task_new() call transfers the ownership of the task
382 * handle to the caller. It must be released whether or not it was submitted.
383 *
384 * - Mandatory to call to avoid memory leaks.
385 *
386 * - It is safe to call this API from within the on_state_update callback, when it
387 * reports a terminal state (finished, cancelled, error) \see libvlc_downloader_status_t.
388 *
389 * - It is safe to call this API at any time, including after
390 * libvlc_downloader_destroy() has been called on the downloader that
391 * created it. The task handle should not be used after calling this function.
392 *
393 * - If called on an active task, it doesn't cancel the task,
394 * use \ref libvlc_downloader_cancel() for that.
395 *
396 * \version LibVLC 4.0.0 or later
397 */
399
400/** @}*/
401
402# ifdef __cplusplus
403}
404# endif
405
406#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:63
struct libvlc_downloader_task libvlc_downloader_task
Opaque handle of a downloader task.
Definition libvlc_downloader.h:57
libvlc_downloader_task * libvlc_downloader_task_new(libvlc_downloader_t *downloader, const libvlc_downloader_request_t *req, const struct libvlc_downloader_cbs *cbs, void *cbs_opaque)
Create a media download task.
int libvlc_downloader_submit(libvlc_downloader_t *downloader, libvlc_downloader_task *task)
Start a task created by libvlc_downloader_task_new().
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:73
@ libvlc_downloader_status_paused
download paused
Definition libvlc_downloader.h:69
@ libvlc_downloader_status_error
download error occurred
Definition libvlc_downloader.h:75
@ libvlc_downloader_status_finished
download finished
Definition libvlc_downloader.h:71
@ libvlc_downloader_status_running
active download in progress (not paused)
Definition libvlc_downloader.h:67
@ libvlc_downloader_status_pending
download pending
Definition libvlc_downloader.h:65
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:88
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:121
uint32_t version
Version of struct libvlc_downloader_cbs.
Definition libvlc_downloader.h:92
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:142
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:175
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:158
struct defining downloader configuration
Definition libvlc_downloader.h:210
uint32_t version
Version of struct libvlc_downloader_cfg.
Definition libvlc_downloader.h:214
uint32_t max_parser_threads
The maximum number of threads used by the parser internally, 0 for default (1 thread).
Definition libvlc_downloader.h:223
struct defining a downloader request
Definition libvlc_downloader.h:183
libvlc_media_t * media
Media to download.
Definition libvlc_downloader.h:203
uint32_t version
Version of libvlc_downloader_request_t.
Definition libvlc_downloader.h:187
A slave of a libvlc_media_t.
Definition libvlc_media.h:156
Definition fetcher.c:54