lib: media: fix libvlc_media_duplicate() behavior
[vlc.git] / include / vlc_stream.h
blob5627545df568b2adb2c31ffed7bb15874d090383
1 /*****************************************************************************
2 * vlc_stream.h: Stream (between access and demux) descriptor and methods
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifndef VLC_STREAM_H
24 #define VLC_STREAM_H 1
26 #include <vlc_block.h>
28 # ifdef __cplusplus
29 extern "C" {
30 # endif
32 /**
33 * \defgroup stream Stream
34 * \ingroup input
35 * Buffered input byte streams
36 * @{
37 * \file
38 * Byte streams and byte stream filter modules interface
41 /**
42 * stream_t definition
45 struct stream_t
47 struct vlc_object_t obj;
49 char *psz_name;
50 char *psz_url; /**< Full URL or MRL (can be NULL) */
51 const char *psz_location; /**< Location (URL with the scheme stripped) */
52 char *psz_filepath; /**< Local file path (if applicable) */
53 bool b_preparsing; /**< True if this access is used to preparse */
54 input_item_t *p_input_item;/**< Input item (can be NULL) */
56 union {
57 /**
58 * Input stream
60 * Depending on the module capability:
61 * - "stream filter" or "demux": input byte stream (not NULL)
62 * - "access": a NULL pointer
63 * - "demux_filter": undefined
65 stream_t *s;
66 /**
67 * Input demuxer
69 * If the module capability is "demux_filter", this is the upstream
70 * demuxer or demux filter. Otherwise, this is undefined.
72 demux_t *p_next;
75 /* es output */
76 es_out_t *out; /* our p_es_out */
78 /**
79 * Read data.
81 * Callback to read data from the stream into a caller-supplied buffer.
83 * This may be NULL if the stream is actually a directory rather than a
84 * byte stream, or if \ref stream_t.pf_block is non-NULL.
86 * \param buf buffer to read data into
87 * \param len buffer length (in bytes)
89 * \retval -1 no data available yet
90 * \retval 0 end of stream (incl. fatal error)
91 * \retval positive number of bytes read (no more than len)
93 ssize_t (*pf_read)(stream_t *, void *buf, size_t len);
95 /**
96 * Read data block.
98 * Callback to read a block of data. The data is read into a block of
99 * memory allocated by the stream. For some streams, data can be read more
100 * efficiently in block of a certain size, and/or using a custom allocator
101 * for buffers. In such case, this callback should be provided instead of
102 * \ref stream_t.pf_read; otherwise, this should be NULL.
104 * \param eof storage space for end-of-stream flag [OUT]
105 * (*eof is always false when invoking pf_block(); pf_block() should set
106 * *eof to true if it detects the end of the stream)
108 * \return a data block,
109 * NULL if no data available yet, on error and at end-of-stream
111 block_t *(*pf_block)(stream_t *, bool *eof);
114 * Read directory.
116 * Callback to fill an item node from a directory
117 * (see doc/browsing.txt for details).
119 * NULL if the stream is not a directory.
121 int (*pf_readdir)(stream_t *, input_item_node_t *);
123 int (*pf_demux)(stream_t *);
126 * Seek.
128 * Callback to set the stream pointer (in bytes from start).
130 * May be NULL if seeking is not supported.
132 int (*pf_seek)(stream_t *, uint64_t);
135 * Stream control.
137 * Cannot be NULL.
139 * \see stream_query_e
141 int (*pf_control)(stream_t *, int i_query, va_list);
144 * Private data pointer
146 void *p_sys;
150 * Possible commands to send to vlc_stream_Control() and vlc_stream_vaControl()
152 enum stream_query_e
154 /* capabilities */
155 STREAM_CAN_SEEK, /**< arg1= bool * res=cannot fail*/
156 STREAM_CAN_FASTSEEK, /**< arg1= bool * res=cannot fail*/
157 STREAM_CAN_PAUSE, /**< arg1= bool * res=cannot fail*/
158 STREAM_CAN_CONTROL_PACE, /**< arg1= bool * res=cannot fail*/
159 /* */
160 STREAM_GET_SIZE=6, /**< arg1= uint64_t * res=can fail */
162 /* */
163 STREAM_GET_PTS_DELAY = 0x101,/**< arg1= vlc_tick_t* res=cannot fail */
164 STREAM_GET_TITLE_INFO, /**< arg1=input_title_t*** arg2=int* res=can fail */
165 STREAM_GET_TITLE, /**< arg1=unsigned * res=can fail */
166 STREAM_GET_SEEKPOINT, /**< arg1=unsigned * res=can fail */
167 STREAM_GET_META, /**< arg1= vlc_meta_t * res=can fail */
168 STREAM_GET_CONTENT_TYPE, /**< arg1= char ** res=can fail */
169 STREAM_GET_SIGNAL, /**< arg1=double *pf_quality, arg2=double *pf_strength res=can fail */
170 STREAM_GET_TAGS, /**< arg1=const block_t ** res=can fail */
171 STREAM_GET_TYPE, /**< arg1=int* res=can fail */
173 STREAM_SET_PAUSE_STATE = 0x200, /**< arg1= bool res=can fail */
174 STREAM_SET_TITLE, /**< arg1= int res=can fail */
175 STREAM_SET_SEEKPOINT, /**< arg1= int res=can fail */
177 /* XXX only data read through vlc_stream_Read/Block will be recorded */
178 STREAM_SET_RECORD_STATE, /**< arg1=bool, arg2=const char *psz_ext (if arg1 is true) res=can fail */
180 STREAM_SET_PRIVATE_ID_STATE = 0x1000, /* arg1= int i_private_data, bool b_selected res=can fail */
181 STREAM_SET_PRIVATE_ID_CA, /* arg1= void * */
182 STREAM_GET_PRIVATE_ID_STATE, /* arg1=int i_private_data arg2=bool * res=can fail */
186 * Reads data from a byte stream.
188 * This function always waits for the requested number of bytes, unless a fatal
189 * error is encountered or the end-of-stream is reached first.
191 * If the buffer is NULL, data is skipped instead of read. This is effectively
192 * a relative forward seek, but it works even on non-seekable streams.
194 * \param buf start of buffer to read data into [OUT]
195 * \param len number of bytes to read
196 * \return the number of bytes read or a negative value on error.
198 VLC_API ssize_t vlc_stream_Read(stream_t *, void *buf, size_t len) VLC_USED;
201 * Reads partial data from a byte stream.
203 * This function waits until some data is available for reading from the
204 * stream, a fatal error is encountered or the end-of-stream is reached.
206 * Unlike vlc_stream_Read(), this function does not wait for the full requested
207 * bytes count. It can return a short count even before the end of the stream
208 * and in the absence of any error.
210 * \param buf start of buffer to read data into [OUT]
211 * \param len buffer size (maximum number of bytes to read)
212 * \return the number of bytes read or a negative value on error.
214 VLC_API ssize_t vlc_stream_ReadPartial(stream_t *, void *buf, size_t len)
215 VLC_USED;
218 * Peeks at data from a byte stream.
220 * This function buffers for the requested number of bytes, waiting if
221 * necessary. Then it stores a pointer to the buffer. Unlike vlc_stream_Read()
222 * or vlc_stream_Block(), this function does not modify the stream read offset.
224 * \note
225 * The buffer remains valid until the next read/peek or seek operation on the
226 * same stream. In case of error, the buffer address is undefined.
228 * \param bufp storage space for the buffer address [OUT]
229 * \param len number of bytes to peek
230 * \return the number of bytes actually available (shorter than requested if
231 * the end-of-stream is reached), or a negative value on error.
233 VLC_API ssize_t vlc_stream_Peek(stream_t *, const uint8_t **, size_t) VLC_USED;
236 * Reads a data block from a byte stream.
238 * This function dequeues the next block of data from the byte stream. The
239 * byte stream back-end decides on the size of the block; the caller cannot
240 * make any assumption about it.
242 * The function might also return NULL spuriously - this does not necessarily
243 * imply that the stream is ended nor that it has encountered a nonrecoverable
244 * error.
246 * This function should be used instead of vlc_stream_Read() or
247 * vlc_stream_Peek() when the caller can handle reads of any size.
249 * \return either a data block or NULL
251 VLC_API block_t *vlc_stream_ReadBlock(stream_t *) VLC_USED;
254 * Tells the current stream position.
256 * This function tells the current read offset (in bytes) from the start of
257 * the start of the stream.
258 * @note The read offset may be larger than the stream size, either because of
259 * a seek past the end, or because the stream shrank asynchronously.
261 * @return the byte offset from the beginning of the stream (cannot fail)
263 VLC_API uint64_t vlc_stream_Tell(const stream_t *) VLC_USED;
266 * Checks for end of stream.
268 * Checks if the last attempt to reads data from the stream encountered the
269 * end of stream before the attempt could be fully satisfied.
270 * The value is initially false, and is reset to false by vlc_stream_Seek().
272 * \note The function can return false even though the current stream position
273 * is equal to the stream size. It will return true after the following attempt
274 * to read more than zero bytes.
276 * \note It might be possible to read after the end of the stream.
277 * It implies the size of the stream increased asynchronously in the mean time.
278 * Streams of most types cannot trigger such a case,
279 * but regular local files notably can.
281 * \note In principles, the stream size should match the stream offset when
282 * the end-of-stream is reached. But that rule is not enforced; it is entirely
283 * dependent on the underlying implementation of the stream.
285 VLC_API bool vlc_stream_Eof(const stream_t *) VLC_USED;
288 * Sets the current stream position.
290 * This function changes the read offset within a stream, if the stream
291 * supports seeking. In case of error, the read offset is not changed.
293 * @note It is possible (but not useful) to seek past the end of a stream.
295 * @param offset byte offset from the beginning of the stream
296 * @return zero on success, a negative value on error
298 VLC_API int vlc_stream_Seek(stream_t *, uint64_t offset) VLC_USED;
300 VLC_API int vlc_stream_vaControl(stream_t *s, int query, va_list args);
302 static inline int vlc_stream_Control(stream_t *s, int query, ...)
304 va_list ap;
305 int ret;
307 va_start(ap, query);
308 ret = vlc_stream_vaControl(s, query, ap);
309 va_end(ap);
310 return ret;
313 VLC_API block_t *vlc_stream_Block(stream_t *s, size_t);
314 VLC_API char *vlc_stream_ReadLine(stream_t *);
317 * Reads a directory.
319 * This function fills an input item node with any and all the items within
320 * a directory. The behaviour is undefined if the stream is not a directory.
322 * \param s directory object to read from
323 * \param node node to store the items into
324 * \return VLC_SUCCESS on success
326 VLC_API int vlc_stream_ReadDir(stream_t *s, input_item_node_t *node);
329 * Closes a byte stream.
330 * \param s byte stream to close
332 VLC_API void vlc_stream_Delete(stream_t *s);
334 VLC_API stream_t *vlc_stream_CommonNew(vlc_object_t *, void (*)(stream_t *));
337 * Get the size of the stream.
339 VLC_USED static inline int vlc_stream_GetSize( stream_t *s, uint64_t *size )
341 return vlc_stream_Control( s, STREAM_GET_SIZE, size );
344 static inline int64_t stream_Size( stream_t *s )
346 uint64_t i_pos;
348 if( vlc_stream_GetSize( s, &i_pos ) )
349 return 0;
350 if( i_pos >> 62 )
351 return (int64_t)1 << 62;
352 return i_pos;
355 VLC_USED
356 static inline bool stream_HasExtension( stream_t *s, const char *extension )
358 const char *name = (s->psz_filepath != NULL) ? s->psz_filepath
359 : s->psz_url;
360 const char *ext = strrchr( name, '.' );
361 return ext != NULL && !strcasecmp( ext, extension );
365 * Get the Content-Type of a stream, or NULL if unknown.
366 * Result must be free()'d.
368 static inline char *stream_ContentType( stream_t *s )
370 char *res;
371 if( vlc_stream_Control( s, STREAM_GET_CONTENT_TYPE, &res ) )
372 return NULL;
373 return res;
377 * Get the mime-type of a stream
379 * \warning the returned resource is to be freed by the caller
380 * \return the mime-type, or `NULL` if unknown
382 VLC_USED
383 static inline char *stream_MimeType( stream_t *s )
385 char* mime_type = stream_ContentType( s );
387 if( mime_type ) /* strip parameters */
388 mime_type[strcspn( mime_type, " ;" )] = '\0';
390 return mime_type;
394 * Checks for a MIME type.
396 * Checks if the stream has a specific MIME type.
398 VLC_USED
399 static inline bool stream_IsMimeType(stream_t *s, const char *type)
401 char *mime = stream_MimeType(s);
402 if (mime == NULL)
403 return false;
405 bool ok = !strcasecmp(mime, type);
406 free(mime);
407 return ok;
411 * Create a stream from a memory buffer.
413 * \param obj parent VLC object
414 * \param base start address of the memory buffer to read from
415 * \param size size in bytes of the memory buffer
416 * \param preserve if false, free(base) will be called when the stream is
417 * destroyed; if true, the memory buffer is preserved
419 VLC_API stream_t *vlc_stream_MemoryNew(vlc_object_t *obj, uint8_t *base,
420 size_t size, bool preserve) VLC_USED;
421 #define vlc_stream_MemoryNew(a, b, c, d) \
422 vlc_stream_MemoryNew(VLC_OBJECT(a), b, c, d)
425 * Create a stream_t reading from a URL.
426 * You must delete it using vlc_stream_Delete.
428 VLC_API stream_t * vlc_stream_NewURL(vlc_object_t *obj, const char *url)
429 VLC_USED;
430 #define vlc_stream_NewURL(a, b) vlc_stream_NewURL(VLC_OBJECT(a), b)
433 * \defgroup stream_fifo FIFO stream
434 * In-memory anonymous pipe
438 typedef struct vlc_stream_fifo vlc_stream_fifo_t;
441 * Creates a FIFO stream.
443 * Creates a non-seekable byte stream object whose byte stream is generated
444 * by another thread in the process. This is the LibVLC equivalent of an
445 * anonymous pipe/FIFO.
447 * On the reader side, the normal stream functions are used,
448 * e.g. vlc_stream_Read() and vlc_stream_Delete().
450 * The created stream object is automatically destroyed when both the reader
451 * and the writer sides have been closed, with vlc_stream_Delete() and
452 * vlc_stream_fifo_Close() respectively.
454 * \param parent parent VLC object for the stream
455 * \param reader location to store read side stream pointer [OUT]
456 * \return a FIFO stream object or NULL on memory error.
458 VLC_API vlc_stream_fifo_t *vlc_stream_fifo_New(vlc_object_t *parent,
459 stream_t **reader);
462 * Writes a block to a FIFO stream.
464 * \param s FIFO stream created by vlc_stream_fifo_New()
465 * \param block data block to write to the stream
466 * \return 0 on success. -1 if the reader end has already been closed
467 * (errno is then set to EPIPE, and the block is deleted).
469 * \bug No congestion control is performed. If the reader end is not keeping
470 * up with the writer end, buffers will accumulate in memory.
472 VLC_API int vlc_stream_fifo_Queue(vlc_stream_fifo_t *s, block_t *block);
475 * Writes data to a FIFO stream.
477 * This is a convenience helper for vlc_stream_fifo_Queue().
478 * \param s FIFO stream created by vlc_stream_fifo_New()
479 * \param buf start address of data to write
480 * \param len length of data to write in bytes
481 * \return len on success, or -1 on error (errno is set accordingly)
483 VLC_API ssize_t vlc_stream_fifo_Write(vlc_stream_fifo_t *s, const void *buf,
484 size_t len);
487 * Terminates a FIFO stream.
489 * Marks the end of the FIFO stream and releases any underlying resources.
490 * \param s FIFO stream created by vlc_stream_fifo_New()
492 VLC_API void vlc_stream_fifo_Close(vlc_stream_fifo_t *s);
495 * @}
499 * Try to add a stream filter to an open stream.
500 * @return New stream to use, or NULL if the filter could not be added.
502 VLC_API stream_t* vlc_stream_FilterNew( stream_t *p_source, const char *psz_stream_filter );
505 * @}
508 # ifdef __cplusplus
510 # endif
512 #endif