]> git.sesse.net Git - vlc/blob - modules/stream_out/description.c
Fixed a small memleak.
[vlc] / modules / stream_out / description.c
1 /*****************************************************************************
2  * description.c: description stream output module (gathers ES info)
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_input.h>
32 #include <vlc_block.h>
33 #include <vlc_sout.h>
34
35 /*****************************************************************************
36  * Exported prototypes
37  *****************************************************************************/
38 static int      Open    ( vlc_object_t * );
39 static void     Close   ( vlc_object_t * );
40
41 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
42 static int               Del ( sout_stream_t *, sout_stream_id_t * );
43 static int               Send( sout_stream_t *, sout_stream_id_t *, block_t* );
44
45 /*****************************************************************************
46  * Module descriptor
47  *****************************************************************************/
48 vlc_module_begin();
49     set_description( _("Description stream output") );
50     set_capability( "sout stream", 50 );
51     add_shortcut( "description" );
52     set_callbacks( Open, Close );
53 vlc_module_end();
54
55 struct sout_stream_sys_t
56 {
57     int            i_id;
58     input_thread_t *p_input;
59
60     mtime_t i_stream_start;
61 };
62
63 struct sout_stream_id_t
64 {
65     int i_d_u_m_m_y;
66 };
67
68 /*****************************************************************************
69  * Open:
70  *****************************************************************************/
71 static int Open( vlc_object_t *p_this )
72 {
73     sout_stream_t *p_stream = (sout_stream_t*)p_this;
74     sout_stream_sys_t *p_sys;
75
76     p_stream->pf_add  = Add;
77     p_stream->pf_del  = Del;
78     p_stream->pf_send = Send;
79     p_sys = p_stream->p_sys = malloc(sizeof(sout_stream_sys_t));
80
81     p_sys->i_id = 0;
82     p_sys->p_input = NULL;
83
84     p_sys->i_stream_start = 0;
85
86     return VLC_SUCCESS;
87 }
88
89 /*****************************************************************************
90  * Close:
91  *****************************************************************************/
92 static void Close( vlc_object_t *p_this )
93 {
94     sout_stream_t *p_stream = (sout_stream_t *)p_this;
95     sout_stream_sys_t *p_sys = p_stream->p_sys;
96
97     /* It can happen only if buggy */
98     if( p_sys->p_input )
99         vlc_object_release( p_sys->p_input );
100     free( p_sys );
101 }
102
103 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
104 {
105     sout_stream_sys_t *p_sys = p_stream->p_sys;
106     sout_stream_id_t *id;
107     es_format_t *p_fmt_copy;
108     input_item_t *p_item;
109
110     if( !p_sys->p_input )
111         p_sys->p_input = vlc_object_find( p_stream, VLC_OBJECT_INPUT, FIND_PARENT );
112     if( !p_sys->p_input )
113         return NULL;
114     
115     p_item = input_GetItem(p_sys->p_input);
116
117     p_fmt_copy = malloc(sizeof(es_format_t));
118     es_format_Copy( p_fmt_copy, p_fmt );
119
120     vlc_mutex_lock( &p_item->lock );
121     TAB_APPEND( p_item->i_es, p_item->es, p_fmt_copy );
122     vlc_mutex_unlock( &p_item->lock );
123
124     p_sys->i_id++;
125     if( p_sys->i_stream_start <= 0 )
126         p_sys->i_stream_start = mdate();
127
128     id = malloc( sizeof( sout_stream_id_t ) );
129     id->i_d_u_m_m_y = 0;
130     return id;
131 }
132
133 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
134 {
135     sout_stream_sys_t *p_sys = p_stream->p_sys;
136
137     p_sys->i_id--;
138     if( p_sys->i_id <= 0 )
139     {
140         vlc_object_release( p_sys->p_input );
141         p_sys->p_input = NULL;
142     }
143
144     free( id );
145     return VLC_SUCCESS;
146 }
147
148 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
149                  block_t *p_buffer )
150 {
151     sout_stream_sys_t *p_sys = p_stream->p_sys;
152
153     block_ChainRelease( p_buffer );
154
155     if( p_sys->p_input && p_sys->i_stream_start + 1500000 < mdate() )
156         p_sys->p_input->b_eof = VLC_TRUE;
157
158     return VLC_SUCCESS;
159 }