1 /*****************************************************************************
2 * bridge.c: bridge stream output module
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 /*****************************************************************************
35 *****************************************************************************/
36 #define ID_TEXT N_("ID")
37 #define ID_LONGTEXT N_( \
38 "Specify an identifier integer for this elementary stream" )
40 #define DELAY_TEXT N_("Delay")
41 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
42 "will be delayed accordingly (in milliseconds, >= 100 ms). For high " \
43 "values you will need to raise file-caching and others.")
45 #define ID_OFFSET_TEXT N_("ID Offset")
46 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
47 "bridge_out to obtain the stream IDs bridge_in will register.")
49 static int OpenOut ( vlc_object_t * );
50 static void CloseOut( vlc_object_t * );
51 static int OpenIn ( vlc_object_t * );
52 static void CloseIn ( vlc_object_t * );
54 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
55 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
59 set_shortname( _("Bridge out"));
60 set_description( _("Bridge out stream output") );
61 set_capability( "sout stream", 50 );
62 add_shortcut( "bridge-out" );
63 set_category( CAT_SOUT );
64 set_subcategory( SUBCAT_SOUT_STREAM );
65 add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
67 set_callbacks( OpenOut, CloseOut );
70 set_shortname( _("Bridge in"));
71 set_description( _("Bridge in stream output") );
72 set_capability( "sout stream", 50 );
73 add_shortcut( "bridge-in" );
74 set_category( CAT_SOUT );
75 set_subcategory( SUBCAT_SOUT_STREAM );
76 add_integer( SOUT_CFG_PREFIX_IN "delay", 100, NULL, DELAY_TEXT,
77 DELAY_LONGTEXT, VLC_FALSE );
78 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
79 ID_OFFSET_LONGTEXT, VLC_FALSE );
80 set_callbacks( OpenIn, CloseIn );
82 var_Create( p_module->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
86 /*****************************************************************************
88 *****************************************************************************/
89 static const char *ppsz_sout_options_out[] = {
93 static const char *ppsz_sout_options_in[] = {
94 "delay", "id-offset", NULL
97 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
98 static int DelOut ( sout_stream_t *, sout_stream_id_t * );
99 static int SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
101 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
102 static int DelIn ( sout_stream_t *, sout_stream_id_t * );
103 static int SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
105 typedef struct bridged_es_t
114 sout_stream_id_t *id;
115 vlc_bool_t b_changed;
118 typedef struct bridge_t
124 #define GetBridge(a) __GetBridge( VLC_OBJECT(a) )
125 static bridge_t *__GetBridge( vlc_object_t *p_object )
127 libvlc_t *p_libvlc = p_object->p_libvlc;
131 if( var_Get( p_libvlc, "bridge-struct", &val ) != VLC_SUCCESS )
137 p_bridge = val.p_address;
148 typedef struct out_sout_stream_sys_t
154 } out_sout_stream_sys_t;
156 /*****************************************************************************
158 *****************************************************************************/
159 static int OpenOut( vlc_object_t *p_this )
161 sout_stream_t *p_stream = (sout_stream_t*)p_this;
162 out_sout_stream_sys_t *p_sys;
165 sout_CfgParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
168 p_sys = malloc( sizeof( out_sout_stream_sys_t ) );
169 p_sys->b_inited = VLC_FALSE;
171 var_Get( p_this->p_libvlc, "bridge-lock", &val );
172 p_sys->p_lock = val.p_address;
174 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
175 p_sys->i_id = val.i_int;
177 p_stream->pf_add = AddOut;
178 p_stream->pf_del = DelOut;
179 p_stream->pf_send = SendOut;
181 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
183 /* update p_sout->i_out_pace_nocontrol */
184 p_stream->p_sout->i_out_pace_nocontrol++;
189 /*****************************************************************************
191 *****************************************************************************/
192 static void CloseOut( vlc_object_t * p_this )
194 sout_stream_t *p_stream = (sout_stream_t*)p_this;
195 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
197 /* update p_sout->i_out_pace_nocontrol */
198 p_stream->p_sout->i_out_pace_nocontrol--;
203 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
205 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
210 if ( p_sys->b_inited )
214 p_sys->b_inited = VLC_TRUE;
216 vlc_mutex_lock( p_sys->p_lock );
218 p_bridge = GetBridge( p_stream );
219 if ( p_bridge == NULL )
221 libvlc_t *p_libvlc = p_stream->p_libvlc;
224 p_bridge = malloc( sizeof( bridge_t ) );
226 var_Create( p_libvlc, "bridge-struct", VLC_VAR_ADDRESS );
227 val.p_address = p_bridge;
228 var_Set( p_libvlc, "bridge-struct", val );
230 p_bridge->i_es_num = 0;
231 p_bridge->p_es = NULL;
234 for ( i = 0; i < p_bridge->i_es_num; i++ )
236 if ( p_bridge->p_es[i].b_empty && !p_bridge->p_es[i].b_changed )
240 if ( i == p_bridge->i_es_num )
242 p_bridge->p_es = realloc( p_bridge->p_es,
243 (p_bridge->i_es_num + 1)
244 * sizeof(bridged_es_t) );
245 p_sys->i_position = p_bridge->i_es_num;
246 p_bridge->i_es_num++;
249 p_es = &p_bridge->p_es[ p_sys->i_position ];
251 p_es->fmt.i_id = p_sys->i_id;
252 p_es->p_block = NULL;
253 p_es->pp_last = &p_es->p_block;
254 p_es->b_empty = VLC_FALSE;
257 p_es->b_changed = VLC_TRUE;
259 vlc_mutex_unlock( p_sys->p_lock );
261 return (sout_stream_id_t *)p_sys;
264 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
266 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
270 if ( !p_sys->b_inited )
275 vlc_mutex_lock( p_sys->p_lock );
277 p_bridge = GetBridge( p_stream );
278 p_es = &p_bridge->p_es[ p_sys->i_position ];
280 p_es->b_empty = VLC_TRUE;
281 block_ChainRelease( p_es->p_block );
283 p_es->b_changed = VLC_TRUE;
284 vlc_mutex_unlock( p_sys->p_lock );
289 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
292 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
296 if ( (out_sout_stream_sys_t *)id != p_sys )
298 block_ChainRelease( p_buffer );
302 vlc_mutex_lock( p_sys->p_lock );
304 p_bridge = GetBridge( p_stream );
305 p_es = &p_bridge->p_es[ p_sys->i_position ];
306 *p_es->pp_last = p_buffer;
307 while ( p_buffer != NULL )
309 p_es->pp_last = &p_buffer->p_next;
310 p_buffer = p_buffer->p_next;
313 vlc_mutex_unlock( p_sys->p_lock );
323 typedef struct in_sout_stream_sys_t
325 sout_stream_t *p_out;
329 } in_sout_stream_sys_t;
331 /*****************************************************************************
333 *****************************************************************************/
334 static int OpenIn( vlc_object_t *p_this )
336 sout_stream_t *p_stream = (sout_stream_t*)p_this;
337 in_sout_stream_sys_t *p_sys;
340 p_sys = malloc( sizeof( in_sout_stream_sys_t ) );
342 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
345 msg_Err( p_stream, "cannot create chain" );
350 sout_CfgParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
353 var_Get( p_this->p_libvlc, "bridge-lock", &val );
354 p_sys->p_lock = val.p_address;
356 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
357 p_sys->i_id_offset = val.i_int;
359 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
360 p_sys->i_delay = (mtime_t)val.i_int * 1000;
362 p_stream->pf_add = AddIn;
363 p_stream->pf_del = DelIn;
364 p_stream->pf_send = SendIn;
366 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
368 /* update p_sout->i_out_pace_nocontrol */
369 p_stream->p_sout->i_out_pace_nocontrol++;
374 /*****************************************************************************
376 *****************************************************************************/
377 static void CloseIn( vlc_object_t * p_this )
379 sout_stream_t *p_stream = (sout_stream_t*)p_this;
380 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
382 sout_StreamDelete( p_sys->p_out );
384 /* update p_sout->i_out_pace_nocontrol */
385 p_stream->p_sout->i_out_pace_nocontrol--;
391 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
393 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
395 return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
398 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
400 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
402 return p_sys->p_out->pf_del( p_sys->p_out, id );
405 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
408 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
410 vlc_bool_t b_no_es = VLC_TRUE;
413 /* First forward the packet for our own ES */
414 p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );
416 /* Then check all bridged streams */
417 vlc_mutex_lock( p_sys->p_lock );
419 p_bridge = GetBridge( p_stream );
420 if ( p_bridge == NULL )
422 vlc_mutex_unlock( p_sys->p_lock );
426 for ( i = 0; i < p_bridge->i_es_num; i++ )
428 if ( p_bridge->p_es[i].b_changed )
430 if ( p_bridge->p_es[i].b_empty )
432 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->p_es[i].id );
436 p_bridge->p_es[i].fmt.i_id += p_sys->i_id_offset;
437 p_bridge->p_es[i].id = p_sys->p_out->pf_add(
438 p_sys->p_out, &p_bridge->p_es[i].fmt );
439 if ( p_bridge->p_es[i].id == NULL )
441 msg_Warn( p_stream, "couldn't create chain for id %d",
442 p_bridge->p_es[i].fmt.i_id );
446 p_bridge->p_es[i].b_changed = VLC_FALSE;
448 if ( p_bridge->p_es[i].b_empty )
453 if ( p_bridge->p_es[i].p_block == NULL )
456 if ( p_bridge->p_es[i].id != NULL )
458 block_t *p_block = p_bridge->p_es[i].p_block;
459 while ( p_block != NULL )
461 p_block->i_pts += p_sys->i_delay;
462 p_block->i_dts += p_sys->i_delay;
463 p_block = p_block->p_next;
465 p_sys->p_out->pf_send( p_sys->p_out, p_bridge->p_es[i].id,
466 p_bridge->p_es[i].p_block );
469 block_ChainRelease( p_bridge->p_es[i].p_block );
471 p_bridge->p_es[i].p_block = NULL;
472 p_bridge->p_es[i].pp_last = &p_bridge->p_es[i].p_block;
477 libvlc_t *p_libvlc = p_stream->p_libvlc;
478 free( p_bridge->p_es );
480 var_Destroy( p_libvlc, "bridge-struct" );
483 vlc_mutex_unlock( p_sys->p_lock );