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