]> git.sesse.net Git - vlc/blob - modules/stream_out/autodel.c
More boundary cleanup
[vlc] / modules / stream_out / autodel.c
1 /*****************************************************************************
2  * autodel.c: monitor mux inputs and automatically add/delete streams
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id: autodel.c 12074 2005-08-08 17:18:08Z dionoea $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29 #include <vlc_sout.h>
30 #include <vlc_block.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open    ( vlc_object_t * );
36 static void Close   ( vlc_object_t * );
37
38 #define SOUT_CFG_PREFIX "sout-autodel-"
39
40 vlc_module_begin();
41     set_shortname( _("Autodel"));
42     set_description( _("Automatically add/delete input streams"));
43     set_capability( "sout stream", 50 );
44     add_shortcut( "autodel" );
45     set_callbacks( Open, Close );
46 vlc_module_end();
47
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static sout_stream_id_t *Add   ( sout_stream_t *, es_format_t * );
53 static int               Del   ( sout_stream_t *, sout_stream_id_t * );
54 static int               Send  ( sout_stream_t *, sout_stream_id_t *, block_t * );
55
56 struct sout_stream_id_t
57 {
58     sout_stream_id_t *id;
59     es_format_t fmt;
60     mtime_t i_last;
61     vlc_bool_t b_error;
62 };
63
64 struct sout_stream_sys_t
65 {
66     sout_stream_t   *p_out;
67     sout_stream_id_t **pp_es;
68     int i_es_num;
69 };
70
71 /*****************************************************************************
72  * Open:
73  *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
75 {
76     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
77     sout_stream_sys_t *p_sys;
78
79     p_sys          = malloc( sizeof( sout_stream_sys_t ) );
80
81     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
82     if( !p_sys->p_out )
83     {
84         msg_Err( p_stream, "cannot create chain" );
85         free( p_sys );
86         return VLC_EGENERIC;
87     }
88     p_sys->pp_es = NULL;
89     p_sys->i_es_num = 0;
90
91     p_stream->pf_add    = Add;
92     p_stream->pf_del    = Del;
93     p_stream->pf_send   = Send;
94
95     p_stream->p_sys     = p_sys;
96
97     /* update p_sout->i_out_pace_nocontrol */
98     p_stream->p_sout->i_out_pace_nocontrol++;
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Close:
105  *****************************************************************************/
106 static void Close( vlc_object_t * p_this )
107 {
108     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
109     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
110
111     sout_StreamDelete( p_sys->p_out );
112     p_stream->p_sout->i_out_pace_nocontrol--;
113
114     free( p_sys );
115 }
116
117 static sout_stream_id_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
118 {
119     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
120     sout_stream_id_t *p_es = malloc( sizeof(sout_stream_id_t) );
121
122     p_es->fmt = *p_fmt;
123     p_es->id = NULL;
124     p_es->i_last = 0;
125     p_es->b_error = VLC_FALSE;
126     TAB_APPEND( p_sys->i_es_num, p_sys->pp_es, p_es );
127
128     return p_es;
129 }
130
131 static int Del( sout_stream_t *p_stream, sout_stream_id_t *p_es )
132 {
133     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
134     sout_stream_id_t *id = p_es->id;
135
136     TAB_REMOVE( p_sys->i_es_num, p_sys->pp_es, p_es );
137     free( p_es );
138
139     if ( id != NULL )
140         return p_sys->p_out->pf_del( p_sys->p_out, id );
141     else
142         return VLC_SUCCESS;
143 }
144
145 static int Send( sout_stream_t *p_stream, sout_stream_id_t *p_es,
146                  block_t *p_buffer )
147 {
148     sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
149     mtime_t i_current = mdate();
150     int i;
151
152     p_es->i_last = p_buffer->i_dts;
153     if ( p_es->id == NULL && p_es->b_error != VLC_TRUE )
154     {
155         p_es->id = p_sys->p_out->pf_add( p_sys->p_out, &p_es->fmt );
156         if ( p_es->id == NULL )
157         {
158             p_es->b_error = VLC_TRUE;
159             msg_Err( p_stream, "couldn't create chain for id %d",
160                      p_es->fmt.i_id );
161         }
162     }
163
164     if ( p_es->b_error != VLC_TRUE )
165         p_sys->p_out->pf_send( p_sys->p_out, p_es->id, p_buffer );
166     else
167         block_ChainRelease( p_buffer );
168
169     for ( i = 0; i < p_sys->i_es_num; i++ )
170     {
171         if ( p_sys->pp_es[i]->id != NULL
172               && (p_sys->pp_es[i]->fmt.i_cat == VIDEO_ES
173                    || p_sys->pp_es[i]->fmt.i_cat == AUDIO_ES)
174               && p_sys->pp_es[i]->i_last < i_current )
175         {
176             p_sys->p_out->pf_del( p_sys->p_out, p_sys->pp_es[i]->id );
177             p_sys->pp_es[i]->id = NULL;
178         }
179     }
180
181     return VLC_SUCCESS;
182 }