1 /*****************************************************************************
2 * autodel.c: monitor mux inputs and automatically add/delete streams
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_block.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t
* );
41 static void Close ( vlc_object_t
* );
43 #define SOUT_CFG_PREFIX "sout-autodel-"
46 set_shortname( N_("Autodel"))
47 set_description( N_("Automatically add/delete input streams"))
48 set_capability( "sout stream", 50 )
49 add_shortcut( "autodel" )
50 set_callbacks( Open
, Close
)
54 /*****************************************************************************
56 *****************************************************************************/
57 static sout_stream_id_sys_t
*Add( sout_stream_t
*, const es_format_t
* );
58 static void Del ( sout_stream_t
*, sout_stream_id_sys_t
* );
59 static int Send ( sout_stream_t
*, sout_stream_id_sys_t
*, block_t
* );
61 struct sout_stream_id_sys_t
63 sout_stream_id_sys_t
*id
;
69 struct sout_stream_sys_t
71 sout_stream_id_sys_t
**pp_es
;
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open( vlc_object_t
*p_this
)
80 sout_stream_t
*p_stream
= (sout_stream_t
*)p_this
;
81 sout_stream_sys_t
*p_sys
;
83 p_sys
= malloc( sizeof( sout_stream_sys_t
) );
85 if( !p_stream
->p_next
)
87 msg_Err( p_stream
, "cannot create chain" );
94 p_stream
->pf_add
= Add
;
95 p_stream
->pf_del
= Del
;
96 p_stream
->pf_send
= Send
;
98 p_stream
->p_sys
= p_sys
;
103 /*****************************************************************************
105 *****************************************************************************/
106 static void Close( vlc_object_t
* p_this
)
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
;
114 static sout_stream_id_sys_t
* Add( sout_stream_t
*p_stream
,
115 const es_format_t
*p_fmt
)
117 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
118 sout_stream_id_sys_t
*p_es
= malloc( sizeof(sout_stream_id_sys_t
) );
119 if( unlikely(p_es
== NULL
) )
122 es_format_Copy( &p_es
->fmt
, p_fmt
);
125 p_es
->i_last
= VLC_TS_INVALID
;
126 p_es
->b_error
= false;
127 TAB_APPEND( p_sys
->i_es_num
, p_sys
->pp_es
, p_es
);
132 static void Del( sout_stream_t
*p_stream
, sout_stream_id_sys_t
*p_es
)
134 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
136 if( p_es
->id
!= NULL
)
137 sout_StreamIdDel( p_stream
->p_next
, p_es
->id
);
139 TAB_REMOVE( p_sys
->i_es_num
, p_sys
->pp_es
, p_es
);
140 es_format_Clean( &p_es
->fmt
);
144 static int Send( sout_stream_t
*p_stream
, sout_stream_id_sys_t
*p_es
,
147 sout_stream_sys_t
*p_sys
= (sout_stream_sys_t
*)p_stream
->p_sys
;
148 mtime_t i_current
= mdate();
151 p_es
->i_last
= p_buffer
->i_dts
;
152 if ( !p_es
->id
&& !p_es
->b_error
)
154 p_es
->id
= p_stream
->p_next
->pf_add( p_stream
->p_next
, &p_es
->fmt
);
155 if ( p_es
->id
== NULL
)
157 p_es
->b_error
= true;
158 msg_Err( p_stream
, "couldn't create chain for id %d",
163 if ( !p_es
->b_error
)
164 p_stream
->p_next
->pf_send( p_stream
->p_next
, p_es
->id
, p_buffer
);
166 block_ChainRelease( p_buffer
);
168 for ( i
= 0; i
< p_sys
->i_es_num
; i
++ )
170 if ( p_sys
->pp_es
[i
]->id
!= NULL
171 && (p_sys
->pp_es
[i
]->fmt
.i_cat
== VIDEO_ES
172 || p_sys
->pp_es
[i
]->fmt
.i_cat
== AUDIO_ES
)
173 && p_sys
->pp_es
[i
]->i_last
< i_current
)
175 p_stream
->p_next
->pf_del( p_stream
->p_next
, p_sys
->pp_es
[i
]->id
);
176 p_sys
->pp_es
[i
]->id
= NULL
;