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