1 /*****************************************************************************
2 * stream_filter.c
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
5 * $Id: eeae19faee91f1eed50b3a47ffe2f705d917627e $
6 *
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_stream.h>
30 #include <vlc_modules.h>
31 #include <libvlc.h>
32
33 #include <assert.h>
34
35 #include "stream.h"
36
37 static void StreamDelete( stream_t * );
38
vlc_stream_FilterNew(stream_t * p_source,const char * psz_stream_filter)39 stream_t *vlc_stream_FilterNew( stream_t *p_source,
40 const char *psz_stream_filter )
41 {
42 stream_t *s;
43 assert( p_source != NULL );
44
45 s = vlc_stream_CommonNew( p_source->obj.parent, StreamDelete );
46 if( s == NULL )
47 return NULL;
48
49 s->p_input = p_source->p_input;
50
51 if( p_source->psz_url != NULL )
52 {
53 s->psz_url = strdup( p_source->psz_url );
54 if( unlikely(s->psz_url == NULL) )
55 goto error;
56 }
57 s->p_source = p_source;
58
59 /* */
60 s->p_module = module_need( s, "stream_filter", psz_stream_filter, true );
61 if( s->p_module == NULL )
62 goto error;
63
64 return s;
65 error:
66 stream_CommonDelete( s );
67 return NULL;
68 }
69
70 /* Add automatic stream filter */
stream_FilterAutoNew(stream_t * p_source)71 stream_t *stream_FilterAutoNew( stream_t *p_source )
72 {
73 /* Limit number of entries to avoid infinite recursion. */
74 for( unsigned i = 0; i < 16; i++ )
75 {
76 stream_t *p_filter = vlc_stream_FilterNew( p_source, NULL );
77 if( p_filter == NULL )
78 break;
79
80 msg_Dbg( p_filter, "stream filter added to %p", (void *)p_source );
81 p_source = p_filter;
82 }
83 return p_source;
84 }
85
86 /* Add specified stream filter(s) */
stream_FilterChainNew(stream_t * p_source,const char * psz_chain)87 stream_t *stream_FilterChainNew( stream_t *p_source, const char *psz_chain )
88 {
89 /* Add user stream filter */
90 char *chain = strdup( psz_chain );
91 if( unlikely(chain == NULL) )
92 return p_source;
93
94 char *buf;
95 for( const char *name = strtok_r( chain, ":", &buf );
96 name != NULL;
97 name = strtok_r( NULL, ":", &buf ) )
98 {
99 stream_t *p_filter = vlc_stream_FilterNew( p_source, name );
100 if( p_filter != NULL )
101 p_source = p_filter;
102 else
103 msg_Warn( p_source, "cannot insert stream filter %s", name );
104 }
105 free( chain );
106
107 return p_source;
108 }
109
StreamDelete(stream_t * s)110 static void StreamDelete( stream_t *s )
111 {
112 module_unneed( s, s->p_module );
113
114 if( s->p_source )
115 vlc_stream_Delete( s->p_source );
116 }
117
vlc_stream_FilterDefaultReadDir(stream_t * s,input_item_node_t * p_node)118 int vlc_stream_FilterDefaultReadDir( stream_t *s, input_item_node_t *p_node )
119 {
120 assert( s->p_source != NULL );
121 return vlc_stream_ReadDir( s->p_source, p_node );
122 }
123