1 /*****************************************************************************
2 * vlc_stream.h: Stream (between access and demux) descriptor and methods
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 #define VLC_STREAM_H 1
27 #include <vlc_block.h>
34 * \defgroup stream Stream
36 * Buffered input byte streams
39 * Byte streams and byte stream filter modules interface
48 struct vlc_common_members obj
;
50 /* Module properties for stream filter */
54 char *psz_url
; /**< Full URL or MRL (can be NULL) */
55 const char *psz_location
; /**< Location (URL with the scheme stripped) */
56 char *psz_filepath
; /**< Local file path (if applicable) */
57 bool b_preparsing
; /**< True if this access is used to preparse */
63 * Depending on the module capability:
64 * - "stream filter" or "demux": input byte stream (not NULL)
65 * - "access" or "access_demux": a NULL pointer
66 * - "demux_filter": undefined
72 * If the module capability is "demux_filter", this is the upstream
73 * demuxer or demux filter. Otherwise, this is undefined.
79 es_out_t
*out
; /* our p_es_out */
84 * Callback to read data from the stream into a caller-supplied buffer.
86 * This may be NULL if the stream is actually a directory rather than a
87 * byte stream, or if \ref stream_t.pf_block is non-NULL.
89 * \param buf buffer to read data into
90 * \param len buffer length (in bytes)
92 * \retval -1 no data available yet
93 * \retval 0 end of stream (incl. fatal error)
94 * \retval positive number of bytes read (no more than len)
96 ssize_t (*pf_read
)(stream_t
*, void *buf
, size_t len
);
101 * Callback to read a block of data. The data is read into a block of
102 * memory allocated by the stream. For some streams, data can be read more
103 * efficiently in block of a certain size, and/or using a custom allocator
104 * for buffers. In such case, this callback should be provided instead of
105 * \ref stream_t.pf_read; otherwise, this should be NULL.
107 * \param eof storage space for end-of-stream flag [OUT]
108 * (*eof is always false when invoking pf_block(); pf_block() should set
109 * *eof to true if it detects the end of the stream)
111 * \return a data block,
112 * NULL if no data available yet, on error and at end-of-stream
114 block_t
*(*pf_block
)(stream_t
*, bool *eof
);
119 * Callback to fill an item node from a directory
120 * (see doc/browsing.txt for details).
122 * NULL if the stream is not a directory.
124 int (*pf_readdir
)(stream_t
*, input_item_node_t
*);
126 int (*pf_demux
)(stream_t
*);
131 * Callback to set the stream pointer (in bytes from start).
133 * May be NULL if seeking is not supported.
135 int (*pf_seek
)(stream_t
*, uint64_t);
142 * \see stream_query_e
144 int (*pf_control
)(stream_t
*, int i_query
, va_list);
147 * Private data pointer
151 /* Weak link to parent input */
152 input_thread_t
*p_input
;
156 * Possible commands to send to vlc_stream_Control() and vlc_stream_vaControl()
161 STREAM_CAN_SEEK
, /**< arg1= bool * res=cannot fail*/
162 STREAM_CAN_FASTSEEK
, /**< arg1= bool * res=cannot fail*/
163 STREAM_CAN_PAUSE
, /**< arg1= bool * res=cannot fail*/
164 STREAM_CAN_CONTROL_PACE
, /**< arg1= bool * res=cannot fail*/
166 STREAM_GET_SIZE
=6, /**< arg1= uint64_t * res=can fail */
167 STREAM_IS_DIRECTORY
, /**< res=can fail */
170 STREAM_GET_PTS_DELAY
= 0x101,/**< arg1= int64_t* res=cannot fail */
171 STREAM_GET_TITLE_INFO
, /**< arg1=input_title_t*** arg2=int* res=can fail */
172 STREAM_GET_TITLE
, /**< arg1=unsigned * res=can fail */
173 STREAM_GET_SEEKPOINT
, /**< arg1=unsigned * res=can fail */
174 STREAM_GET_META
, /**< arg1= vlc_meta_t * res=can fail */
175 STREAM_GET_CONTENT_TYPE
, /**< arg1= char ** res=can fail */
176 STREAM_GET_SIGNAL
, /**< arg1=double *pf_quality, arg2=double *pf_strength res=can fail */
177 STREAM_GET_TAGS
, /**< arg1=const block_t ** res=can fail */
179 STREAM_SET_PAUSE_STATE
= 0x200, /**< arg1= bool res=can fail */
180 STREAM_SET_TITLE
, /**< arg1= int res=can fail */
181 STREAM_SET_SEEKPOINT
, /**< arg1= int res=can fail */
183 /* XXX only data read through vlc_stream_Read/Block will be recorded */
184 STREAM_SET_RECORD_STATE
, /**< arg1=bool, arg2=const char *psz_ext (if arg1 is true) res=can fail */
186 STREAM_SET_PRIVATE_ID_STATE
= 0x1000, /* arg1= int i_private_data, bool b_selected res=can fail */
187 STREAM_SET_PRIVATE_ID_CA
, /* arg1= int i_program_number, uint16_t i_vpid, uint16_t i_apid1, uint16_t i_apid2, uint16_t i_apid3, uint8_t i_length, uint8_t *p_data */
188 STREAM_GET_PRIVATE_ID_STATE
, /* arg1=int i_private_data arg2=bool * res=can fail */
192 * Reads data from a byte stream.
194 * This function always waits for the requested number of bytes, unless a fatal
195 * error is encountered or the end-of-stream is reached first.
197 * If the buffer is NULL, data is skipped instead of read. This is effectively
198 * a relative forward seek, but it works even on non-seekable streams.
200 * \param buf start of buffer to read data into [OUT]
201 * \param len number of bytes to read
202 * \return the number of bytes read or a negative value on error.
204 VLC_API ssize_t
vlc_stream_Read(stream_t
*, void *buf
, size_t len
) VLC_USED
;
207 * Reads partial data from a byte stream.
209 * This function waits until some data is available for reading from the
210 * stream, a fatal error is encountered or the end-of-stream is reached.
212 * Unlike vlc_stream_Read(), this function does not wait for the full requested
213 * bytes count. It can return a short count even before the end of the stream
214 * and in the absence of any error.
216 * \param buf start of buffer to read data into [OUT]
217 * \param len buffer size (maximum number of bytes to read)
218 * \return the number of bytes read or a negative value on error.
220 VLC_API ssize_t
vlc_stream_ReadPartial(stream_t
*, void *buf
, size_t len
)
224 * Peeks at data from a byte stream.
226 * This function buffers for the requested number of bytes, waiting if
227 * necessary. Then it stores a pointer to the buffer. Unlike vlc_stream_Read()
228 * or vlc_stream_Block(), this function does not modify the stream read offset.
231 * The buffer remains valid until the next read/peek or seek operation on the
232 * same stream. In case of error, the buffer address is undefined.
234 * \param bufp storage space for the buffer address [OUT]
235 * \param len number of bytes to peek
236 * \return the number of bytes actually available (shorter than requested if
237 * the end-of-stream is reached), or a negative value on error.
239 VLC_API ssize_t
vlc_stream_Peek(stream_t
*, const uint8_t **, size_t) VLC_USED
;
242 * Reads a data block from a byte stream.
244 * This function dequeues the next block of data from the byte stream. The
245 * byte stream back-end decides on the size of the block; the caller cannot
246 * make any assumption about it.
248 * The function might also return NULL spuriously - this does not necessarily
249 * imply that the stream is ended nor that it has encountered a nonrecoverable
252 * This function should be used instead of vlc_stream_Read() or
253 * vlc_stream_Peek() when the caller can handle reads of any size.
255 * \return either a data block or NULL
257 VLC_API block_t
*vlc_stream_ReadBlock(stream_t
*) VLC_USED
;
260 * Tells the current stream position.
262 * This function tells the current read offset (in bytes) from the start of
263 * the start of the stream.
264 * @note The read offset may be larger than the stream size, either because of
265 * a seek past the end, or because the stream shrank asynchronously.
267 * @return the byte offset from the beginning of the stream (cannot fail)
269 VLC_API
uint64_t vlc_stream_Tell(const stream_t
*) VLC_USED
;
272 * Checks for end of stream.
274 * Checks if the last attempt to reads data from the stream encountered the
275 * end of stream before the attempt could be fully satisfied.
276 * The value is initially false, and is reset to false by vlc_stream_Seek().
278 * \note The function can return false even though the current stream position
279 * is equal to the stream size. It will return true after the following attempt
280 * to read more than zero bytes.
282 * \note It might be possible to read after the end of the stream.
283 * It implies the size of the stream increased asynchronously in the mean time.
284 * Streams of most types cannot trigger such a case,
285 * but regular local files notably can.
287 * \note In principles, the stream size should match the stream offset when
288 * the end-of-stream is reached. But that rule is not enforced; it is entirely
289 * dependent on the underlying implementation of the stream.
291 VLC_API
bool vlc_stream_Eof(const stream_t
*) VLC_USED
;
294 * Sets the current stream position.
296 * This function changes the read offset within a stream, if the stream
297 * supports seeking. In case of error, the read offset is not changed.
299 * @note It is possible (but not useful) to seek past the end of a stream.
301 * @param offset byte offset from the beginning of the stream
302 * @return zero on success, a negative value on error
304 VLC_API
int vlc_stream_Seek(stream_t
*, uint64_t offset
) VLC_USED
;
306 VLC_API
int vlc_stream_vaControl(stream_t
*s
, int query
, va_list args
);
308 static inline int vlc_stream_Control(stream_t
*s
, int query
, ...)
314 ret
= vlc_stream_vaControl(s
, query
, ap
);
319 VLC_API block_t
*vlc_stream_Block(stream_t
*s
, size_t);
320 VLC_API
char *vlc_stream_ReadLine(stream_t
*);
321 VLC_API
int vlc_stream_ReadDir(stream_t
*, input_item_node_t
*);
324 * Closes a byte stream.
325 * \param s byte stream to close
327 VLC_API
void vlc_stream_Delete(stream_t
*s
);
329 VLC_API stream_t
*vlc_stream_CommonNew(vlc_object_t
*, void (*)(stream_t
*));
332 * Get the size of the stream.
334 VLC_USED
static inline int vlc_stream_GetSize( stream_t
*s
, uint64_t *size
)
336 return vlc_stream_Control( s
, STREAM_GET_SIZE
, size
);
339 static inline int64_t stream_Size( stream_t
*s
)
343 if( vlc_stream_GetSize( s
, &i_pos
) )
346 return (int64_t)1 << 62;
351 static inline bool stream_HasExtension( stream_t
*s
, const char *extension
)
353 const char *name
= (s
->psz_filepath
!= NULL
) ? s
->psz_filepath
355 const char *ext
= strrchr( name
, '.' );
356 return ext
!= NULL
&& !strcasecmp( ext
, extension
);
360 * Get the Content-Type of a stream, or NULL if unknown.
361 * Result must be free()'d.
363 static inline char *stream_ContentType( stream_t
*s
)
366 if( vlc_stream_Control( s
, STREAM_GET_CONTENT_TYPE
, &res
) )
372 * Get the mime-type of a stream
374 * \warning the returned resource is to be freed by the caller
375 * \return the mime-type, or `NULL` if unknown
378 static inline char *stream_MimeType( stream_t
*s
)
380 char* mime_type
= stream_ContentType( s
);
382 if( mime_type
) /* strip parameters */
383 mime_type
[strcspn( mime_type
, " ;" )] = '\0';
389 * Checks for a MIME type.
391 * Checks if the stream has a specific MIME type.
394 static inline bool stream_IsMimeType(stream_t
*s
, const char *type
)
396 char *mime
= stream_MimeType(s
);
400 bool ok
= !strcasecmp(mime
, type
);
406 * Create a stream from a memory buffer.
408 * \param obj parent VLC object
409 * \param base start address of the memory buffer to read from
410 * \param size size in bytes of the memory buffer
411 * \param preserve if false, free(base) will be called when the stream is
412 * destroyed; if true, the memory buffer is preserved
414 VLC_API stream_t
*vlc_stream_MemoryNew(vlc_object_t
*obj
, uint8_t *base
,
415 size_t size
, bool preserve
) VLC_USED
;
416 #define vlc_stream_MemoryNew(a, b, c, d) \
417 vlc_stream_MemoryNew(VLC_OBJECT(a), b, c, d)
420 * Create a stream_t reading from a URL.
421 * You must delete it using vlc_stream_Delete.
423 VLC_API stream_t
* vlc_stream_NewURL(vlc_object_t
*obj
, const char *url
)
425 #define vlc_stream_NewURL(a, b) vlc_stream_NewURL(VLC_OBJECT(a), b)
428 * \defgroup stream_fifo FIFO stream
429 * In-memory anonymous pipe
434 * Creates a FIFO stream.
436 * Creates a non-seekable byte stream object whose byte stream is generated
437 * by another thread in the process. This is the LibVLC equivalent of an
438 * anonymous pipe/FIFO.
440 * On the reader side, the normal stream functions are used,
441 * e.g. vlc_stream_Read() and vlc_stream_Delete().
443 * The created stream object is automatically destroyed when both the reader
444 * and the writer sides have been closed, with vlc_stream_Delete() and
445 * vlc_stream_fifo_Close() respectively.
447 * \param parent parent VLC object for the stream
448 * \return a stream object or NULL on memory error.
450 VLC_API stream_t
*vlc_stream_fifo_New(vlc_object_t
*parent
);
453 * Writes a block to a FIFO stream.
455 * \param s FIFO stream created by vlc_stream_fifo_New()
456 * \param block data block to write to the stream
457 * \return 0 on success. -1 if the reader end has already been closed
458 * (errno is then set to EPIPE, and the block is deleted).
460 * \bug No congestion control is performed. If the reader end is not keeping
461 * up with the writer end, buffers will accumulate in memory.
463 VLC_API
int vlc_stream_fifo_Queue(stream_t
*s
, block_t
*block
);
466 * Writes data to a FIFO stream.
468 * This is a convenience helper for vlc_stream_fifo_Queue().
469 * \param s FIFO stream created by vlc_stream_fifo_New()
470 * \param buf start address of data to write
471 * \param len length of data to write in bytes
472 * \return len on success, or -1 on error (errno is set accordingly)
474 VLC_API ssize_t
vlc_stream_fifo_Write(stream_t
*s
, const void *buf
,
478 * Terminates a FIFO stream.
480 * Marks the end of the FIFO stream and releases any underlying resources.
481 * \param s FIFO stream created by vlc_stream_fifo_New()
483 VLC_API
void vlc_stream_fifo_Close(stream_t
*s
);
490 * Try to add a stream filter to an open stream.
491 * @return New stream to use, or NULL if the filter could not be added.
493 VLC_API stream_t
* vlc_stream_FilterNew( stream_t
*p_source
, const char *psz_stream_filter
);
496 * Default ReadDir implementation for stream Filter. This implementation just
497 * forward the pf_readdir call to the p_source stream.
499 VLC_API
int vlc_stream_FilterDefaultReadDir(stream_t
*s
,
500 input_item_node_t
*p_node
);
503 * Sets vlc_stream_FilterDefaultReadDir as the pf_readdir callback for this
506 #define stream_FilterSetDefaultReadDir(stream) \
508 (stream)->pf_readdir = vlc_stream_FilterDefaultReadDir; \