1 /*****************************************************************************
2  * vlc_stream.h: Stream (between access and demux) descriptor and methods
3  *****************************************************************************
4  * Copyright (C) 1999-2004 VLC authors and VideoLAN
5  * $Id: 789545e16e59dd99f215d7b2bafb655f49dfbc88 $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 
24 #ifndef VLC_STREAM_H
25 #define VLC_STREAM_H 1
26 
27 #include <vlc_block.h>
28 
29 # ifdef __cplusplus
30 extern "C" {
31 # endif
32 
33 /**
34  * \defgroup stream Stream
35  * \ingroup input
36  * Buffered input byte streams
37  * @{
38  * \file
39  * Byte streams and byte stream filter modules interface
40  */
41 
42 /**
43  * stream_t definition
44  */
45 
46 struct stream_t
47 {
48     VLC_COMMON_MEMBERS
49 
50     /* Module properties for stream filter */
51     module_t    *p_module;
52 
53     char        *psz_name;
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 */
58 
59     /* Stream source for stream filter */
60     stream_t *p_source;
61 
62     /**
63      * Read data.
64      *
65      * Callback to read data from the stream into a caller-supplied buffer.
66      *
67      * This may be NULL if the stream is actually a directory rather than a
68      * byte stream, or if \ref stream_t.pf_block is non-NULL.
69      *
70      * \param buf buffer to read data into
71      * \param len buffer length (in bytes)
72      *
73      * \retval -1 no data available yet
74      * \retval 0 end of stream (incl. fatal error)
75      * \retval positive number of bytes read (no more than len)
76      */
77     ssize_t     (*pf_read)(stream_t *, void *buf, size_t len);
78 
79     /**
80      * Read data block.
81      *
82      * Callback to read a block of data. The data is read into a block of
83      * memory allocated by the stream. For some streams, data can be read more
84      * efficiently in block of a certain size, and/or using a custom allocator
85      * for buffers. In such case, this callback should be provided instead of
86      * \ref stream_t.pf_read; otherwise, this should be NULL.
87      *
88      * \param eof storage space for end-of-stream flag [OUT]
89      * (*eof is always false when invoking pf_block(); pf_block() should set
90      *  *eof to true if it detects the end of the stream)
91      *
92      * \return a data block,
93      * NULL if no data available yet, on error and at end-of-stream
94      */
95     block_t    *(*pf_block)(stream_t *, bool *eof);
96 
97     /**
98      * Read directory.
99      *
100      * Callback to fill an item node from a directory
101      * (see doc/browsing.txt for details).
102      *
103      * NULL if the stream is not a directory.
104      */
105     int         (*pf_readdir)(stream_t *, input_item_node_t *);
106 
107     /**
108      * Seek.
109      *
110      * Callback to set the stream pointer (in bytes from start).
111      *
112      * May be NULL if seeking is not supported.
113      */
114     int         (*pf_seek)(stream_t *, uint64_t);
115 
116     /**
117      * Stream control.
118      *
119      * Cannot be NULL.
120      *
121      * \see stream_query_e
122      */
123     int         (*pf_control)(stream_t *, int i_query, va_list);
124 
125     /**
126      * Private data pointer
127      */
128     void *p_sys;
129 
130     /* Weak link to parent input */
131     input_thread_t *p_input;
132 };
133 
134 /**
135  * Possible commands to send to vlc_stream_Control() and vlc_stream_vaControl()
136  */
137 enum stream_query_e
138 {
139     /* capabilities */
140     STREAM_CAN_SEEK,            /**< arg1= bool *   res=cannot fail*/
141     STREAM_CAN_FASTSEEK,        /**< arg1= bool *   res=cannot fail*/
142     STREAM_CAN_PAUSE,           /**< arg1= bool *   res=cannot fail*/
143     STREAM_CAN_CONTROL_PACE,    /**< arg1= bool *   res=cannot fail*/
144     /* */
145     STREAM_GET_SIZE=6,          /**< arg1= uint64_t *     res=can fail */
146     STREAM_IS_DIRECTORY,        /**< res=can fail */
147 
148     /* */
149     STREAM_GET_PTS_DELAY = 0x101,/**< arg1= int64_t* res=cannot fail */
150     STREAM_GET_TITLE_INFO, /**< arg1=input_title_t*** arg2=int* res=can fail */
151     STREAM_GET_TITLE,       /**< arg1=unsigned * res=can fail */
152     STREAM_GET_SEEKPOINT,   /**< arg1=unsigned * res=can fail */
153     STREAM_GET_META,        /**< arg1= vlc_meta_t *       res=can fail */
154     STREAM_GET_CONTENT_TYPE,    /**< arg1= char **         res=can fail */
155     STREAM_GET_SIGNAL,      /**< arg1=double *pf_quality, arg2=double *pf_strength   res=can fail */
156     STREAM_GET_TAGS,        /**< arg1=const block_t ** res=can fail */
157 
158     STREAM_SET_PAUSE_STATE = 0x200, /**< arg1= bool        res=can fail */
159     STREAM_SET_TITLE,       /**< arg1= int          res=can fail */
160     STREAM_SET_SEEKPOINT,   /**< arg1= int          res=can fail */
161 
162     /* XXX only data read through vlc_stream_Read/Block will be recorded */
163     STREAM_SET_RECORD_STATE,     /**< arg1=bool, arg2=const char *psz_ext (if arg1 is true)  res=can fail */
164 
165     STREAM_SET_PRIVATE_ID_STATE = 0x1000, /* arg1= int i_private_data, bool b_selected    res=can fail */
166     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 */
167     STREAM_GET_PRIVATE_ID_STATE,          /* arg1=int i_private_data arg2=bool *          res=can fail */
168 };
169 
170 /**
171  * Reads data from a byte stream.
172  *
173  * This function always waits for the requested number of bytes, unless a fatal
174  * error is encountered or the end-of-stream is reached first.
175  *
176  * If the buffer is NULL, data is skipped instead of read. This is effectively
177  * a relative forward seek, but it works even on non-seekable streams.
178  *
179  * \param buf start of buffer to read data into [OUT]
180  * \param len number of bytes to read
181  * \return the number of bytes read or a negative value on error.
182  */
183 VLC_API ssize_t vlc_stream_Read(stream_t *, void *buf, size_t len) VLC_USED;
184 
185 /**
186  * Reads partial data from a byte stream.
187  *
188  * This function waits until some data is available for reading from the
189  * stream, a fatal error is encountered or the end-of-stream is reached.
190  *
191  * Unlike vlc_stream_Read(), this function does not wait for the full requested
192  * bytes count. It can return a short count even before the end of the stream
193  * and in the absence of any error.
194  *
195  * \param buf start of buffer to read data into [OUT]
196  * \param len buffer size (maximum number of bytes to read)
197  * \return the number of bytes read or a negative value on error.
198  */
199 VLC_API ssize_t vlc_stream_ReadPartial(stream_t *, void *buf, size_t len)
200 VLC_USED;
201 
202 /**
203  * Peeks at data from a byte stream.
204  *
205  * This function buffers for the requested number of bytes, waiting if
206  * necessary. Then it stores a pointer to the buffer. Unlike vlc_stream_Read()
207  * or vlc_stream_Block(), this function does not modify the stream read offset.
208  *
209  * \note
210  * The buffer remains valid until the next read/peek or seek operation on the
211  * same stream. In case of error, the buffer address is undefined.
212  *
213  * \param bufp storage space for the buffer address [OUT]
214  * \param len number of bytes to peek
215  * \return the number of bytes actually available (shorter than requested if
216  * the end-of-stream is reached), or a negative value on error.
217  */
218 VLC_API ssize_t vlc_stream_Peek(stream_t *, const uint8_t **, size_t) VLC_USED;
219 
220 /**
221  * Reads a data block from a byte stream.
222  *
223  * This function dequeues the next block of data from the byte stream. The
224  * byte stream back-end decides on the size of the block; the caller cannot
225  * make any assumption about it.
226  *
227  * The function might also return NULL spuriously - this does not necessarily
228  * imply that the stream is ended nor that it has encountered a nonrecoverable
229  * error.
230  *
231  * This function should be used instead of vlc_stream_Read() or
232  * vlc_stream_Peek() when the caller can handle reads of any size.
233  *
234  * \return either a data block or NULL
235  */
236 VLC_API block_t *vlc_stream_ReadBlock(stream_t *) VLC_USED;
237 
238 /**
239  * Tells the current stream position.
240  *
241  * This function tells the current read offset (in bytes) from the start of
242  * the start of the stream.
243  * @note The read offset may be larger than the stream size, either because of
244  * a seek past the end, or because the stream shrank asynchronously.
245  *
246  * @return the byte offset from the beginning of the stream (cannot fail)
247  */
248 VLC_API uint64_t vlc_stream_Tell(const stream_t *) VLC_USED;
249 
250 /**
251  * Checks for end of stream.
252  *
253  * Checks if the last attempt to reads data from the stream encountered the
254  * end of stream before the attempt could be fully satisfied.
255  * The value is initially false, and is reset to false by vlc_stream_Seek().
256  *
257  * \note The function can return false even though the current stream position
258  * is equal to the stream size. It will return true after the following attempt
259  * to read more than zero bytes.
260  *
261  * \note It might be possible to read after the end of the stream.
262  * It implies the size of the stream increased asynchronously in the mean time.
263  * Streams of most types cannot trigger such a case,
264  * but regular local files notably can.
265  *
266  * \note In principles, the stream size should match the stream offset when
267  * the end-of-stream is reached. But that rule is not enforced; it is entirely
268  * dependent on the underlying implementation of the stream.
269  */
270 VLC_API bool vlc_stream_Eof(const stream_t *) VLC_USED;
271 
272 /**
273  * Sets the current stream position.
274  *
275  * This function changes the read offset within a stream, if the stream
276  * supports seeking. In case of error, the read offset is not changed.
277  *
278  * @note It is possible (but not useful) to seek past the end of a stream.
279  *
280  * @param offset byte offset from the beginning of the stream
281  * @return zero on success, a negative value on error
282  */
283 VLC_API int vlc_stream_Seek(stream_t *, uint64_t offset) VLC_USED;
284 
285 VLC_API int vlc_stream_vaControl(stream_t *s, int query, va_list args);
286 
vlc_stream_Control(stream_t * s,int query,...)287 static inline int vlc_stream_Control(stream_t *s, int query, ...)
288 {
289     va_list ap;
290     int ret;
291 
292     va_start(ap, query);
293     ret = vlc_stream_vaControl(s, query, ap);
294     va_end(ap);
295     return ret;
296 }
297 
298 VLC_API block_t *vlc_stream_Block(stream_t *s, size_t);
299 VLC_API char *vlc_stream_ReadLine(stream_t *);
300 VLC_API int vlc_stream_ReadDir(stream_t *, input_item_node_t *);
301 
302 /**
303  * Closes a byte stream.
304  * \param s byte stream to close
305  */
306 VLC_API void vlc_stream_Delete(stream_t *s);
307 
308 VLC_API stream_t *vlc_stream_CommonNew(vlc_object_t *, void (*)(stream_t *));
309 
310 /**
311  * Get the size of the stream.
312  */
vlc_stream_GetSize(stream_t * s,uint64_t * size)313 VLC_USED static inline int vlc_stream_GetSize( stream_t *s, uint64_t *size )
314 {
315     return vlc_stream_Control( s, STREAM_GET_SIZE, size );
316 }
317 
stream_Size(stream_t * s)318 static inline int64_t stream_Size( stream_t *s )
319 {
320     uint64_t i_pos;
321 
322     if( vlc_stream_GetSize( s, &i_pos ) )
323         return 0;
324     if( i_pos >> 62 )
325         return (int64_t)1 << 62;
326     return i_pos;
327 }
328 
329 VLC_USED
stream_HasExtension(stream_t * s,const char * extension)330 static inline bool stream_HasExtension( stream_t *s, const char *extension )
331 {
332     const char *name = (s->psz_filepath != NULL) ? s->psz_filepath
333                                                  : s->psz_url;
334     const char *ext = strrchr( name, '.' );
335     return ext != NULL && !strcasecmp( ext, extension );
336 }
337 
338 /**
339  * Get the Content-Type of a stream, or NULL if unknown.
340  * Result must be free()'d.
341  */
stream_ContentType(stream_t * s)342 static inline char *stream_ContentType( stream_t *s )
343 {
344     char *res;
345     if( vlc_stream_Control( s, STREAM_GET_CONTENT_TYPE, &res ) )
346         return NULL;
347     return res;
348 }
349 
350 /**
351  * Get the mime-type of a stream
352  *
353  * \warning the returned resource is to be freed by the caller
354  * \return the mime-type, or `NULL` if unknown
355  **/
356 VLC_USED
stream_MimeType(stream_t * s)357 static inline char *stream_MimeType( stream_t *s )
358 {
359     char* mime_type = stream_ContentType( s );
360 
361     if( mime_type ) /* strip parameters */
362         mime_type[strcspn( mime_type, " ;" )] = '\0';
363 
364     return mime_type;
365 }
366 
367 /**
368  * Checks for a MIME type.
369  *
370  * Checks if the stream has a specific MIME type.
371  */
372 VLC_USED
stream_IsMimeType(stream_t * s,const char * type)373 static inline bool stream_IsMimeType(stream_t *s, const char *type)
374 {
375     char *mime = stream_MimeType(s);
376     if (mime == NULL)
377         return false;
378 
379     bool ok = !strcasecmp(mime, type);
380     free(mime);
381     return ok;
382 }
383 
384 /**
385  * Create a stream from a memory buffer.
386  *
387  * \param obj parent VLC object
388  * \param base start address of the memory buffer to read from
389  * \param size size in bytes of the memory buffer
390  * \param preserve if false, free(base) will be called when the stream is
391  *                 destroyed; if true, the memory buffer is preserved
392  */
393 VLC_API stream_t *vlc_stream_MemoryNew(vlc_object_t *obj, uint8_t *base,
394                                        size_t size, bool preserve) VLC_USED;
395 #define vlc_stream_MemoryNew(a, b, c, d) \
396         vlc_stream_MemoryNew(VLC_OBJECT(a), b, c, d)
397 
398 /**
399  * Create a stream_t reading from a URL.
400  * You must delete it using vlc_stream_Delete.
401  */
402 VLC_API stream_t * vlc_stream_NewURL(vlc_object_t *obj, const char *url)
403 VLC_USED;
404 #define vlc_stream_NewURL(a, b) vlc_stream_NewURL(VLC_OBJECT(a), b)
405 
406 /**
407  * \defgroup stream_fifo FIFO stream
408  * In-memory anonymous pipe
409   @{
410  */
411 
412 /**
413  * Creates a FIFO stream.
414  *
415  * Creates a non-seekable byte stream object whose byte stream is generated
416  * by another thread in the process. This is the LibVLC equivalent of an
417  * anonymous pipe/FIFO.
418  *
419  * On the reader side, the normal stream functions are used,
420  * e.g. vlc_stream_Read() and vlc_stream_Delete().
421  *
422  * The created stream object is automatically destroyed when both the reader
423  * and the writer sides have been closed, with vlc_stream_Delete() and
424  * vlc_stream_fifo_Close() respectively.
425  *
426  * \param parent parent VLC object for the stream
427  * \return a stream object or NULL on memory error.
428  */
429 VLC_API stream_t *vlc_stream_fifo_New(vlc_object_t *parent);
430 
431 /**
432  * Writes a block to a FIFO stream.
433  *
434  * \param s FIFO stream created by vlc_stream_fifo_New()
435  * \param block data block to write to the stream
436  * \return 0 on success. -1 if the reader end has already been closed
437  * (errno is then set to EPIPE, and the block is deleted).
438  *
439  * \bug No congestion control is performed. If the reader end is not keeping
440  * up with the writer end, buffers will accumulate in memory.
441  */
442 VLC_API int vlc_stream_fifo_Queue(stream_t *s, block_t *block);
443 
444 /**
445  * Writes data to a FIFO stream.
446  *
447  * This is a convenience helper for vlc_stream_fifo_Queue().
448  * \param s FIFO stream created by vlc_stream_fifo_New()
449  * \param buf start address of data to write
450  * \param len length of data to write in bytes
451  * \return len on success, or -1 on error (errno is set accordingly)
452  */
453 VLC_API ssize_t vlc_stream_fifo_Write(stream_t *s, const void *buf,
454                                       size_t len);
455 
456 /**
457  * Terminates a FIFO stream.
458  *
459  * Marks the end of the FIFO stream and releases any underlying resources.
460  * \param s FIFO stream created by vlc_stream_fifo_New()
461  */
462 VLC_API void vlc_stream_fifo_Close(stream_t *s);
463 
464 /**
465  * @}
466  */
467 
468 /**
469  * Try to add a stream filter to an open stream.
470  * @return New stream to use, or NULL if the filter could not be added.
471  **/
472 VLC_API stream_t* vlc_stream_FilterNew( stream_t *p_source, const char *psz_stream_filter );
473 
474 /**
475  * Default ReadDir implementation for stream Filter. This implementation just
476  * forward the pf_readdir call to the p_source stream.
477  */
478 VLC_API int vlc_stream_FilterDefaultReadDir(stream_t *s,
479                                             input_item_node_t *p_node);
480 
481 /**
482  * Sets vlc_stream_FilterDefaultReadDir as the pf_readdir callback for this
483  * stream filter.
484  */
485 #define stream_FilterSetDefaultReadDir(stream) \
486 do { \
487     (stream)->pf_readdir = vlc_stream_FilterDefaultReadDir; \
488 } while (0)
489 
490 /**
491  * @}
492  */
493 
494 # ifdef __cplusplus
495 }
496 # endif
497 
498 #endif
499