]> git.sesse.net Git - vlc/blob - src/input/stream_demux.c
aout: fix mutex leak
[vlc] / src / input / stream_demux.c
1 /*****************************************************************************
2  * stream_demux.c
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
5  * $Id$
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 #include <limits.h>
28
29 #include "demux.h"
30 #include <libvlc.h>
31 #include <vlc_codec.h>
32
33 /****************************************************************************
34  * stream_Demux*: create a demuxer for an outpout stream (allow demuxer chain)
35  ****************************************************************************/
36 struct stream_sys_t
37 {
38     /* Data buffer */
39     block_fifo_t *p_fifo;
40     block_t      *p_block;
41
42     uint64_t    i_pos;
43
44     /* Demuxer */
45     char        *psz_name;
46     es_out_t    *out;
47
48     vlc_thread_t thread;
49     vlc_mutex_t  lock;
50     struct
51     {
52         double  position;
53         int64_t length;
54         int64_t time;
55     } stats;
56 };
57
58 static int  DStreamRead   ( stream_t *, void *p_read, unsigned int i_read );
59 static int  DStreamPeek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
60 static int  DStreamControl( stream_t *, int i_query, va_list );
61 static void DStreamDelete ( stream_t * );
62 static void* DStreamThread ( void * );
63
64
65 stream_t *stream_DemuxNew( demux_t *p_demux, const char *psz_demux, es_out_t *out )
66 {
67     vlc_object_t *p_obj = VLC_OBJECT(p_demux);
68     /* We create a stream reader, and launch a thread */
69     stream_t     *s;
70     stream_sys_t *p_sys;
71
72     s = stream_CommonNew( p_obj );
73     if( s == NULL )
74         return NULL;
75     s->p_input = p_demux->p_input;
76     s->psz_path  = strdup(""); /* N/A */
77     s->pf_read   = DStreamRead;
78     s->pf_peek   = DStreamPeek;
79     s->pf_control= DStreamControl;
80     s->pf_destroy= DStreamDelete;
81
82     s->p_sys = p_sys = malloc( sizeof( *p_sys) );
83     if( !s->psz_path || !s->p_sys )
84     {
85         stream_CommonDelete( s );
86         return NULL;
87     }
88
89     p_sys->i_pos = 0;
90     p_sys->out = out;
91     p_sys->p_block = NULL;
92     p_sys->psz_name = strdup( psz_demux );
93     p_sys->stats.position = 0.;
94     p_sys->stats.length = 0;
95     p_sys->stats.time = 0;
96
97     /* decoder fifo */
98     if( ( p_sys->p_fifo = block_FifoNew() ) == NULL )
99     {
100         stream_CommonDelete( s );
101         free( p_sys->psz_name );
102         free( p_sys );
103         return NULL;
104     }
105
106     vlc_mutex_init( &p_sys->lock );
107
108     if( vlc_clone( &p_sys->thread, DStreamThread, s, VLC_THREAD_PRIORITY_INPUT ) )
109     {
110         vlc_mutex_destroy( &p_sys->lock );
111         block_FifoRelease( p_sys->p_fifo );
112         stream_CommonDelete( s );
113         free( p_sys->psz_name );
114         free( p_sys );
115         return NULL;
116     }
117
118     return s;
119 }
120
121 void stream_DemuxSend( stream_t *s, block_t *p_block )
122 {
123     stream_sys_t *p_sys = s->p_sys;
124     block_FifoPut( p_sys->p_fifo, p_block );
125 }
126
127 int stream_DemuxControlVa( stream_t *s, int query, va_list args )
128 {
129     stream_sys_t *sys = s->p_sys;
130
131     switch( query )
132     {
133         case DEMUX_GET_POSITION:
134             vlc_mutex_lock( &sys->lock );
135             *va_arg( args, double * ) = sys->stats.position;
136             vlc_mutex_unlock( &sys->lock );
137             break;
138         case DEMUX_GET_LENGTH:
139             vlc_mutex_lock( &sys->lock );
140             *va_arg( args, int64_t * ) = sys->stats.length;
141             vlc_mutex_unlock( &sys->lock );
142             break;
143         case DEMUX_GET_TIME:
144             vlc_mutex_lock( &sys->lock );
145             *va_arg( args, int64_t * ) = sys->stats.time;
146             vlc_mutex_unlock( &sys->lock );
147             break;
148         default:
149             return VLC_EGENERIC;
150     }
151     return VLC_SUCCESS;
152 }
153
154 static void DStreamDelete( stream_t *s )
155 {
156     stream_sys_t *p_sys = s->p_sys;
157     block_t *p_empty;
158
159     vlc_object_kill( s );
160     p_empty = block_Alloc( 0 );
161     block_FifoPut( p_sys->p_fifo, p_empty );
162     vlc_join( p_sys->thread, NULL );
163     vlc_mutex_destroy( &p_sys->lock );
164
165     if( p_sys->p_block )
166         block_Release( p_sys->p_block );
167
168     block_FifoRelease( p_sys->p_fifo );
169     free( p_sys->psz_name );
170     free( p_sys );
171     stream_CommonDelete( s );
172 }
173
174
175 static int DStreamRead( stream_t *s, void *p_read, unsigned int i_read )
176 {
177     stream_sys_t *p_sys = s->p_sys;
178     uint8_t *p_out = p_read;
179     int i_out = 0;
180
181     //msg_Dbg( s, "DStreamRead: wanted %d bytes", i_read );
182
183     while( vlc_object_alive( s ) && !s->b_error && i_read )
184     {
185         block_t *p_block = p_sys->p_block;
186         int i_copy;
187
188         if( !p_block )
189         {
190             p_block = block_FifoGet( p_sys->p_fifo );
191             if( !p_block ) s->b_error = 1;
192             p_sys->p_block = p_block;
193         }
194
195         if( p_block && i_read )
196         {
197             i_copy = __MIN( i_read, p_block->i_buffer );
198             if( p_out && i_copy ) memcpy( p_out, p_block->p_buffer, i_copy );
199             i_read -= i_copy;
200             p_out += i_copy;
201             i_out += i_copy;
202             p_block->i_buffer -= i_copy;
203             p_block->p_buffer += i_copy;
204
205             if( !p_block->i_buffer )
206             {
207                 block_Release( p_block );
208                 p_sys->p_block = NULL;
209             }
210         }
211     }
212
213     p_sys->i_pos += i_out;
214     return i_out;
215 }
216
217 static int DStreamPeek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
218 {
219     stream_sys_t *p_sys = s->p_sys;
220     block_t **pp_block = &p_sys->p_block;
221     int i_out = 0;
222     *pp_peek = 0;
223
224     //msg_Dbg( s, "DStreamPeek: wanted %d bytes", i_peek );
225
226     while( vlc_object_alive( s ) && !s->b_error && i_peek )
227     {
228         int i_copy;
229
230         if( !*pp_block )
231         {
232             *pp_block = block_FifoGet( p_sys->p_fifo );
233             if( !*pp_block ) s->b_error = 1;
234         }
235
236         if( *pp_block && i_peek )
237         {
238             i_copy = __MIN( i_peek, (*pp_block)->i_buffer );
239             i_peek -= i_copy;
240             i_out += i_copy;
241
242             if( i_peek ) pp_block = &(*pp_block)->p_next;
243         }
244     }
245
246     if( p_sys->p_block )
247     {
248         p_sys->p_block = block_ChainGather( p_sys->p_block );
249         *pp_peek = p_sys->p_block->p_buffer;
250     }
251
252     return i_out;
253 }
254
255 static int DStreamControl( stream_t *s, int i_query, va_list args )
256 {
257     stream_sys_t *p_sys = s->p_sys;
258     uint64_t    *p_i64;
259     bool *p_b;
260
261     switch( i_query )
262     {
263         case STREAM_GET_SIZE:
264             p_i64 = va_arg( args, uint64_t * );
265             *p_i64 = 0;
266             return VLC_SUCCESS;
267
268         case STREAM_CAN_SEEK:
269             p_b = (bool*) va_arg( args, bool * );
270             *p_b = false;
271             return VLC_SUCCESS;
272
273         case STREAM_CAN_FASTSEEK:
274             p_b = (bool*) va_arg( args, bool * );
275             *p_b = false;
276             return VLC_SUCCESS;
277
278         case STREAM_GET_POSITION:
279             p_i64 = va_arg( args, uint64_t * );
280             *p_i64 = p_sys->i_pos;
281             return VLC_SUCCESS;
282
283         case STREAM_SET_POSITION:
284         {
285             uint64_t i64 = va_arg( args, uint64_t );
286             if( i64 < p_sys->i_pos )
287                 return VLC_EGENERIC;
288
289             uint64_t i_skip = i64 - p_sys->i_pos;
290             while( i_skip > 0 )
291             {
292                 int i_read = DStreamRead( s, NULL, __MIN(i_skip, INT_MAX) );
293                 if( i_read <= 0 )
294                     return VLC_EGENERIC;
295                 i_skip -= i_read;
296             }
297             return VLC_SUCCESS;
298         }
299
300         case STREAM_CONTROL_ACCESS:
301         case STREAM_GET_CONTENT_TYPE:
302         case STREAM_SET_RECORD_STATE:
303             return VLC_EGENERIC;
304
305         default:
306             msg_Err( s, "invalid DStreamControl query=0x%x", i_query );
307             return VLC_EGENERIC;
308     }
309 }
310
311 static void* DStreamThread( void *obj )
312 {
313     stream_t *s = (stream_t *)obj;
314     stream_sys_t *p_sys = s->p_sys;
315     demux_t *p_demux;
316
317     /* Create the demuxer */
318     p_demux = demux_New( s, s->p_input, "", p_sys->psz_name, "", s, p_sys->out,
319                          false );
320     if( p_demux == NULL )
321         return NULL;
322
323     /* stream_Demux cannot apply DVB filters.
324      * Get all programs and let the E/S output sort them out. */
325     demux_Control( p_demux, DEMUX_SET_GROUP, -1, NULL );
326
327     /* Main loop */
328     mtime_t next_update = 0;
329     while( vlc_object_alive( s ) )
330     {
331         if( p_demux->info.i_update || mdate() >= next_update )
332         {
333             double newpos;
334             int64_t newlen, newtime;
335
336             if( demux_Control( p_demux, DEMUX_GET_POSITION, &newpos ) )
337                 newpos = 0.;
338             if( demux_Control( p_demux, DEMUX_GET_LENGTH, &newlen ) )
339                 newlen = 0;
340             if( demux_Control( p_demux, DEMUX_GET_TIME, &newtime ) )
341                 newtime = 0;
342
343             vlc_mutex_lock( &p_sys->lock );
344             p_sys->stats.position = newpos;
345             p_sys->stats.length = newlen;
346             p_sys->stats.time = newtime;
347             vlc_mutex_unlock( &p_sys->lock );
348
349             p_demux->info.i_update = 0;
350             next_update = mdate() + (CLOCK_FREQ / 4);
351         }
352
353         if( demux_Demux( p_demux ) <= 0 )
354             break;
355     }
356
357     demux_Delete( p_demux );
358
359     return NULL;
360 }