1 /*****************************************************************************
2 * bridge.c: bridge stream output module
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_block.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 #define ID_TEXT N_("ID")
40 #define ID_LONGTEXT N_( \
41 "Integer identifier for this elementary stream. This will be used to " \
42 "\"find\" this stream later." )
44 #define DELAY_TEXT N_("Delay")
45 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
46 "will be delayed according to this value (in milliseconds, should be "\
47 ">= 100 ms). For high values, you will need to raise caching values." )
49 #define ID_OFFSET_TEXT N_("ID Offset")
50 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
51 "bridge_out to obtain the stream IDs bridge_in will register.")
53 static int OpenOut ( vlc_object_t * );
54 static void CloseOut( vlc_object_t * );
55 static int OpenIn ( vlc_object_t * );
56 static void CloseIn ( vlc_object_t * );
58 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
59 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
62 set_shortname( _("Bridge"));
63 set_description( _("Bridge stream output"));
65 set_section( N_("Bridge out"), NULL );
66 set_capability( "sout stream", 50 );
67 add_shortcut( "bridge-out" );
68 /* Only usable with VLM. No category so not in gui preferences
69 set_category( CAT_SOUT );
70 set_subcategory( SUBCAT_SOUT_STREAM );*/
71 add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
73 set_callbacks( OpenOut, CloseOut );
76 set_section( N_("Bridge in"), NULL );
77 set_capability( "sout stream", 50 );
78 add_shortcut( "bridge-in" );
79 /*set_category( CAT_SOUT );
80 set_subcategory( SUBCAT_SOUT_STREAM );*/
81 add_integer( SOUT_CFG_PREFIX_IN "delay", 0, NULL, DELAY_TEXT,
82 DELAY_LONGTEXT, false );
83 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
84 ID_OFFSET_LONGTEXT, false );
85 set_callbacks( OpenIn, CloseIn );
90 /*****************************************************************************
92 *****************************************************************************/
93 static const char *ppsz_sout_options_out[] = {
97 static const char *ppsz_sout_options_in[] = {
98 "delay", "id-offset", NULL
101 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
102 static int DelOut ( sout_stream_t *, sout_stream_id_t * );
103 static int SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
105 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
106 static int DelIn ( sout_stream_t *, sout_stream_id_t * );
107 static int SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
109 typedef struct bridged_es_t
117 sout_stream_id_t *id;
122 typedef struct bridge_t
124 bridged_es_t **pp_es;
128 #define GetBridge(a) __GetBridge( VLC_OBJECT(a) )
129 static bridge_t *__GetBridge( vlc_object_t *p_object )
134 if( var_Get( p_object->p_libvlc, "bridge-struct", &val ) )
140 p_bridge = val.p_address;
151 typedef struct out_sout_stream_sys_t
157 } out_sout_stream_sys_t;
159 /*****************************************************************************
161 *****************************************************************************/
162 static int OpenOut( vlc_object_t *p_this )
164 sout_stream_t *p_stream = (sout_stream_t *)p_this;
165 out_sout_stream_sys_t *p_sys;
168 config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
171 p_sys = malloc( sizeof( out_sout_stream_sys_t ) );
172 p_sys->b_inited = false;
174 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
175 var_Get( p_this->p_libvlc, "bridge-lock", &val );
176 p_sys->p_lock = val.p_address;
178 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
179 p_sys->i_id = val.i_int;
181 p_stream->pf_add = AddOut;
182 p_stream->pf_del = DelOut;
183 p_stream->pf_send = SendOut;
185 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
187 p_stream->p_sout->i_out_pace_nocontrol++;
192 /*****************************************************************************
194 *****************************************************************************/
195 static void CloseOut( vlc_object_t * p_this )
197 sout_stream_t *p_stream = (sout_stream_t*)p_this;
198 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
200 p_stream->p_sout->i_out_pace_nocontrol--;
205 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
207 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
212 if ( p_sys->b_inited )
216 p_sys->b_inited = true;
218 vlc_mutex_lock( p_sys->p_lock );
220 p_bridge = GetBridge( p_stream );
221 if ( p_bridge == NULL )
223 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
226 p_bridge = malloc( sizeof( bridge_t ) );
228 var_Create( p_libvlc, "bridge-struct", VLC_VAR_ADDRESS );
229 val.p_address = p_bridge;
230 var_Set( p_libvlc, "bridge-struct", val );
232 p_bridge->i_es_num = 0;
233 p_bridge->pp_es = NULL;
236 for ( i = 0; i < p_bridge->i_es_num; i++ )
238 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
242 if ( i == p_bridge->i_es_num )
244 p_bridge->pp_es = realloc( p_bridge->pp_es,
245 (p_bridge->i_es_num + 1)
246 * sizeof(bridged_es_t *) );
247 p_bridge->i_es_num++;
248 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
251 p_sys->p_es = p_es = p_bridge->pp_es[i];
254 p_es->fmt.i_id = p_sys->i_id;
255 p_es->p_block = NULL;
256 p_es->pp_last = &p_es->p_block;
257 p_es->b_empty = false;
261 p_es->b_changed = true;
263 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
264 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
266 vlc_mutex_unlock( p_sys->p_lock );
268 return (sout_stream_id_t *)p_sys;
271 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
274 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
277 if ( !p_sys->b_inited )
282 vlc_mutex_lock( p_sys->p_lock );
286 p_es->b_empty = true;
287 block_ChainRelease( p_es->p_block );
288 p_es->p_block = false;
290 p_es->b_changed = true;
291 vlc_mutex_unlock( p_sys->p_lock );
293 p_sys->b_inited = false;
298 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
301 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
304 if ( (out_sout_stream_sys_t *)id != p_sys )
306 block_ChainRelease( p_buffer );
310 vlc_mutex_lock( p_sys->p_lock );
313 *p_es->pp_last = p_buffer;
314 while ( p_buffer != NULL )
316 p_es->pp_last = &p_buffer->p_next;
317 p_buffer = p_buffer->p_next;
320 vlc_mutex_unlock( p_sys->p_lock );
330 typedef struct in_sout_stream_sys_t
332 sout_stream_t *p_out;
336 } in_sout_stream_sys_t;
338 /*****************************************************************************
340 *****************************************************************************/
341 static int OpenIn( vlc_object_t *p_this )
343 sout_stream_t *p_stream = (sout_stream_t*)p_this;
344 in_sout_stream_sys_t *p_sys;
347 p_sys = malloc( sizeof( in_sout_stream_sys_t ) );
349 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
352 msg_Err( p_stream, "cannot create chain" );
357 config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
360 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
361 var_Get( p_this->p_libvlc, "bridge-lock", &val );
362 p_sys->p_lock = val.p_address;
364 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
365 p_sys->i_id_offset = val.i_int;
367 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
368 p_sys->i_delay = (mtime_t)val.i_int * 1000;
370 p_stream->pf_add = AddIn;
371 p_stream->pf_del = DelIn;
372 p_stream->pf_send = SendIn;
374 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
376 /* update p_sout->i_out_pace_nocontrol */
377 p_stream->p_sout->i_out_pace_nocontrol++;
382 /*****************************************************************************
384 *****************************************************************************/
385 static void CloseIn( vlc_object_t * p_this )
387 sout_stream_t *p_stream = (sout_stream_t*)p_this;
388 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
390 sout_StreamDelete( p_sys->p_out );
391 p_stream->p_sout->i_out_pace_nocontrol--;
396 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
398 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
400 return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
403 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
405 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
407 return p_sys->p_out->pf_del( p_sys->p_out, id );
410 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
413 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
418 /* First forward the packet for our own ES */
419 p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );
421 /* Then check all bridged streams */
422 vlc_mutex_lock( p_sys->p_lock );
424 p_bridge = GetBridge( p_stream );
425 if ( p_bridge == NULL )
427 vlc_mutex_unlock( p_sys->p_lock );
431 for ( i = 0; i < p_bridge->i_es_num; i++ )
433 if ( !p_bridge->pp_es[i]->b_empty )
436 while ( p_bridge->pp_es[i]->p_block != NULL
437 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
439 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
440 < p_bridge->pp_es[i]->i_last) )
442 block_t *p_block = p_bridge->pp_es[i]->p_block;
443 msg_Dbg( p_stream, "dropping a packet (" I64Fd ")",
444 mdate() - p_block->i_dts - p_sys->i_delay );
445 p_bridge->pp_es[i]->p_block
446 = p_bridge->pp_es[i]->p_block->p_next;
447 block_Release( p_block );
450 if ( p_bridge->pp_es[i]->p_block == NULL )
452 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
455 if ( p_bridge->pp_es[i]->b_changed )
457 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
459 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
463 /* We need at least two packets to enter the mux. */
464 if ( p_bridge->pp_es[i]->p_block == NULL
465 || p_bridge->pp_es[i]->p_block->p_next == NULL )
470 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
471 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
472 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
473 if ( p_bridge->pp_es[i]->id == NULL )
475 msg_Warn( p_stream, "couldn't create chain for id %d",
476 p_bridge->pp_es[i]->fmt.i_id );
478 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
479 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
480 p_bridge->pp_es[i]->fmt.i_id, i );
483 p_bridge->pp_es[i]->b_changed = false;
485 if ( p_bridge->pp_es[i]->b_empty )
488 if ( p_bridge->pp_es[i]->p_block == NULL )
490 if ( p_bridge->pp_es[i]->id != NULL
491 && p_bridge->pp_es[i]->i_last < mdate() )
493 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
494 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
495 p_bridge->pp_es[i]->b_changed = true;
496 p_bridge->pp_es[i]->id = NULL;
501 if ( p_bridge->pp_es[i]->id != NULL )
503 block_t *p_block = p_bridge->pp_es[i]->p_block;
504 while ( p_block != NULL )
506 p_bridge->pp_es[i]->i_last = p_block->i_dts;
507 p_block->i_pts += p_sys->i_delay;
508 p_block->i_dts += p_sys->i_delay;
509 p_block = p_block->p_next;
511 p_sys->p_out->pf_send( p_sys->p_out, p_bridge->pp_es[i]->id,
512 p_bridge->pp_es[i]->p_block );
516 block_ChainRelease( p_bridge->pp_es[i]->p_block );
519 p_bridge->pp_es[i]->p_block = NULL;
520 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
525 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
526 for ( i = 0; i < p_bridge->i_es_num; i++ )
527 free( p_bridge->pp_es[i] );
528 free( p_bridge->pp_es );
530 var_Destroy( p_libvlc, "bridge-struct" );
533 vlc_mutex_unlock( p_sys->p_lock );