1 /*****************************************************************************
2 * bridge.c: bridge stream output module
3 *****************************************************************************
4 * Copyright (C) 2005-2008 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Antoine Cellerier <dionoea at videolan dot org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_block.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 #define ID_TEXT N_("ID")
42 #define ID_LONGTEXT N_( \
43 "Integer identifier for this elementary stream. This will be used to " \
44 "\"find\" this stream later." )
46 #define DEST_TEXT N_( "Destination bridge-in name" )
47 #define DEST_LONGTEXT N_( \
48 "Name of the destination bridge-in. If you do not need more " \
49 "than one bridge-in at a time, you can discard this option." )
51 #define DELAY_TEXT N_("Delay")
52 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
53 "will be delayed according to this value (in milliseconds, should be "\
54 ">= 100 ms). For high values, you will need to raise caching values." )
56 #define ID_OFFSET_TEXT N_("ID Offset")
57 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
58 "bridge_out to obtain the stream IDs bridge_in will register.")
60 #define NAME_TEXT N_( "Name of current instance" )
61 #define NAME_LONGTEXT N_( \
62 "Name of this bridge-in instance. If you do not need more " \
63 "than one bridge-in at a time, you can discard this option." )
65 #define PLACEHOLDER_TEXT N_( "Fallback to placeholder stream when out of data" )
66 #define PLACEHOLDER_LONGTEXT N_( \
67 "If set to true, the bridge will discard all input elementary streams " \
68 "except if it doesn't receive data from another bridge-in. This can " \
69 "be used to configure a place holder stream when the real source " \
70 "breaks. Source and placeholder streams should have the same format. " )
72 #define PLACEHOLDER_DELAY_TEXT N_( "Placeholder delay" )
73 #define PLACEHOLDER_DELAY_LONGTEXT N_( \
74 "Delay (in ms) before the placeholder kicks in." )
76 #define PLACEHOLDER_IFRAME_TEXT N_( "Wait for I frame before toggling placeholder" )
77 #define PLACEHOLDER_IFRAME_LONGTEXT N_( \
78 "If enabled, switching between the placeholder and the normal stream " \
79 "will only occur on I frames. This will remove artifacts on stream " \
80 "switching at the expense of a slightly longer delay, depending on " \
81 "the frequence of I frames in the streams." )
83 static int OpenOut ( vlc_object_t * );
84 static void CloseOut( vlc_object_t * );
85 static int OpenIn ( vlc_object_t * );
86 static void CloseIn ( vlc_object_t * );
88 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
89 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
92 set_shortname( N_("Bridge"))
93 set_description( N_("Bridge stream output"))
95 set_section( N_("Bridge out"), NULL )
96 set_capability( "sout stream", 50 )
97 add_shortcut( "bridge-out" )
98 /* Only usable with VLM. No category so not in gui preferences
99 set_category( CAT_SOUT )
100 set_subcategory( SUBCAT_SOUT_STREAM )*/
101 add_integer( SOUT_CFG_PREFIX_OUT "id", 0, ID_TEXT, ID_LONGTEXT,
103 add_string( SOUT_CFG_PREFIX_OUT "in-name", "default",
104 DEST_TEXT, DEST_LONGTEXT, false )
105 set_callbacks( OpenOut, CloseOut )
108 set_section( N_("Bridge in"), NULL )
109 set_capability( "sout stream", 50 )
110 add_shortcut( "bridge-in" )
111 /*set_category( CAT_SOUT )
112 set_subcategory( SUBCAT_SOUT_STREAM )*/
113 add_integer( SOUT_CFG_PREFIX_IN "delay", 0, DELAY_TEXT,
114 DELAY_LONGTEXT, false )
115 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, ID_OFFSET_TEXT,
116 ID_OFFSET_LONGTEXT, false )
117 add_string( SOUT_CFG_PREFIX_IN "name", "default",
118 NAME_TEXT, NAME_LONGTEXT, false )
119 add_bool( SOUT_CFG_PREFIX_IN "placeholder", false,
120 PLACEHOLDER_TEXT, PLACEHOLDER_LONGTEXT, false )
121 add_integer( SOUT_CFG_PREFIX_IN "placeholder-delay", 200,
122 PLACEHOLDER_DELAY_TEXT, PLACEHOLDER_DELAY_LONGTEXT, false )
123 add_bool( SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", true,
124 PLACEHOLDER_IFRAME_TEXT, PLACEHOLDER_IFRAME_LONGTEXT, false )
125 set_callbacks( OpenIn, CloseIn )
130 /*****************************************************************************
132 *****************************************************************************/
133 static const char *const ppsz_sout_options_out[] = {
134 "id", "in-name", NULL
137 static const char *const ppsz_sout_options_in[] = {
138 "delay", "id-offset", "name",
139 "placeholder", "placeholder-delay", "placeholder-switch-on-iframe",
143 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
144 static int DelOut ( sout_stream_t *, sout_stream_id_t * );
145 static int SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
147 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
148 static int DelIn ( sout_stream_t *, sout_stream_id_t * );
149 static int SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
151 typedef struct bridged_es_t
159 sout_stream_id_t *id;
164 typedef struct bridge_t
166 bridged_es_t **pp_es;
170 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
176 typedef struct out_sout_stream_sys_t
183 } out_sout_stream_sys_t;
185 /*****************************************************************************
187 *****************************************************************************/
188 static int OpenOut( vlc_object_t *p_this )
190 sout_stream_t *p_stream = (sout_stream_t *)p_this;
191 out_sout_stream_sys_t *p_sys;
194 config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
197 p_sys = malloc( sizeof( out_sout_stream_sys_t ) );
198 if( unlikely( !p_sys ) )
200 p_sys->b_inited = false;
202 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
203 p_sys->i_id = val.i_int;
205 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
206 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
208 free( val.psz_string );
212 free( val.psz_string );
214 p_stream->pf_add = AddOut;
215 p_stream->pf_del = DelOut;
216 p_stream->pf_send = SendOut;
218 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
220 p_stream->p_sout->i_out_pace_nocontrol++;
225 /*****************************************************************************
227 *****************************************************************************/
228 static void CloseOut( vlc_object_t * p_this )
230 sout_stream_t *p_stream = (sout_stream_t*)p_this;
231 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
233 p_stream->p_sout->i_out_pace_nocontrol--;
235 free( p_sys->psz_name );
239 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
241 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
246 if ( p_sys->b_inited )
248 msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
251 p_sys->b_inited = true;
253 vlc_mutex_lock( &lock );
255 p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
256 if ( p_bridge == NULL )
258 p_bridge = xmalloc( sizeof( bridge_t ) );
260 var_Create( p_stream->p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
261 var_SetAddress( p_stream->p_libvlc, p_sys->psz_name, p_bridge );
263 p_bridge->i_es_num = 0;
264 p_bridge->pp_es = NULL;
267 for ( i = 0; i < p_bridge->i_es_num; i++ )
269 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
273 if ( i == p_bridge->i_es_num )
275 p_bridge->pp_es = xrealloc( p_bridge->pp_es,
276 (p_bridge->i_es_num + 1) * sizeof(bridged_es_t *) );
277 p_bridge->i_es_num++;
278 p_bridge->pp_es[i] = xmalloc( sizeof(bridged_es_t) );
281 p_sys->p_es = p_es = p_bridge->pp_es[i];
284 p_es->fmt.i_id = p_sys->i_id;
285 p_es->p_block = NULL;
286 p_es->pp_last = &p_es->p_block;
287 p_es->b_empty = false;
290 p_es->i_last = VLC_TS_INVALID;
291 p_es->b_changed = true;
293 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
294 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
296 vlc_mutex_unlock( &lock );
298 return (sout_stream_id_t *)p_sys;
301 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
304 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
307 if ( !p_sys->b_inited )
312 vlc_mutex_lock( &lock );
316 p_es->b_empty = true;
317 block_ChainRelease( p_es->p_block );
318 p_es->p_block = false;
320 p_es->b_changed = true;
321 vlc_mutex_unlock( &lock );
323 p_sys->b_inited = false;
328 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
331 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
334 if ( (out_sout_stream_sys_t *)id != p_sys )
336 block_ChainRelease( p_buffer );
340 vlc_mutex_lock( &lock );
343 *p_es->pp_last = p_buffer;
344 while ( p_buffer != NULL )
346 p_es->pp_last = &p_buffer->p_next;
347 p_buffer = p_buffer->p_next;
350 vlc_mutex_unlock( &lock );
360 typedef struct in_sout_stream_sys_t
368 bool b_switch_on_iframe;
370 mtime_t i_placeholder_delay;
371 sout_stream_id_t *id_video;
372 mtime_t i_last_video;
373 sout_stream_id_t *id_audio;
374 mtime_t i_last_audio;
375 } in_sout_stream_sys_t;
377 enum { placeholder_on, placeholder_off };
379 /*****************************************************************************
381 *****************************************************************************/
382 static int OpenIn( vlc_object_t *p_this )
384 sout_stream_t *p_stream = (sout_stream_t*)p_this;
385 in_sout_stream_sys_t *p_sys;
388 p_sys = malloc( sizeof( in_sout_stream_sys_t ) );
389 if( unlikely( !p_sys ) )
392 if( !p_stream->p_next )
394 msg_Err( p_stream, "cannot create chain" );
399 config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
402 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
403 p_sys->i_id_offset = val.i_int;
405 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
406 p_sys->i_delay = (mtime_t)val.i_int * 1000;
408 var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
409 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
411 free( val.psz_string );
415 free( val.psz_string );
417 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
418 p_sys->b_placeholder = val.b_bool;
420 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", &val);
421 p_sys->b_switch_on_iframe = val.b_bool;
423 p_sys->i_state = placeholder_on;
425 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
426 p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
428 p_sys->i_last_video = VLC_TS_INVALID;
429 p_sys->i_last_audio = VLC_TS_INVALID;
430 p_sys->id_video = NULL;
431 p_sys->id_audio = NULL;
433 p_stream->pf_add = AddIn;
434 p_stream->pf_del = DelIn;
435 p_stream->pf_send = SendIn;
437 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
439 /* update p_sout->i_out_pace_nocontrol */
440 p_stream->p_sout->i_out_pace_nocontrol++;
445 /*****************************************************************************
447 *****************************************************************************/
448 static void CloseIn( vlc_object_t * p_this )
450 sout_stream_t *p_stream = (sout_stream_t*)p_this;
451 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
453 p_stream->p_sout->i_out_pace_nocontrol--;
455 free( p_sys->psz_name );
459 struct sout_stream_id_t
461 sout_stream_id_t *id;
462 int i_cat; /* es category. Used for placeholder option */
465 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
467 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
469 sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
470 if( !id ) return NULL;
472 id->id = p_stream->p_next->pf_add( p_stream->p_next, p_fmt );
479 if( p_sys->b_placeholder )
481 id->i_cat = p_fmt->i_cat;
482 switch( p_fmt->i_cat )
485 if( p_sys->id_video != NULL )
486 msg_Err( p_stream, "We already had a video es!" );
487 p_sys->id_video = id->id;
490 if( p_sys->id_audio != NULL )
491 msg_Err( p_stream, "We already had an audio es!" );
492 p_sys->id_audio = id->id;
500 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
502 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
504 if( id == p_sys->id_video ) p_sys->id_video = NULL;
505 if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
507 int ret = p_stream->p_next->pf_del( p_stream->p_next, id->id );
513 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
516 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
520 int i_date = mdate();
522 /* First forward the packet for our own ES */
523 if( !p_sys->b_placeholder )
524 p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
526 /* Then check all bridged streams */
527 vlc_mutex_lock( &lock );
529 p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
533 for ( i = 0; i < p_bridge->i_es_num; i++ )
535 if ( !p_bridge->pp_es[i]->b_empty )
538 while ( p_bridge->pp_es[i]->p_block != NULL
539 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
541 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
542 < p_bridge->pp_es[i]->i_last) )
544 block_t *p_block = p_bridge->pp_es[i]->p_block;
545 msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
546 i_date - p_block->i_dts - p_sys->i_delay );
547 p_bridge->pp_es[i]->p_block
548 = p_bridge->pp_es[i]->p_block->p_next;
549 block_Release( p_block );
552 if ( p_bridge->pp_es[i]->p_block == NULL )
554 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
557 if ( p_bridge->pp_es[i]->b_changed )
559 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
561 p_stream->p_next->pf_del( p_stream->p_next, p_bridge->pp_es[i]->id );
565 /* We need at least two packets to enter the mux. */
566 if ( p_bridge->pp_es[i]->p_block == NULL
567 || p_bridge->pp_es[i]->p_block->p_next == NULL )
572 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
573 if( !p_sys->b_placeholder )
575 p_bridge->pp_es[i]->id = p_stream->p_next->pf_add(
576 p_stream->p_next, &p_bridge->pp_es[i]->fmt );
577 if ( p_bridge->pp_es[i]->id == NULL )
579 msg_Warn( p_stream, "couldn't create chain for id %d",
580 p_bridge->pp_es[i]->fmt.i_id );
583 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
584 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
585 p_bridge->pp_es[i]->fmt.i_id, i );
588 p_bridge->pp_es[i]->b_changed = false;
590 if ( p_bridge->pp_es[i]->b_empty )
593 if ( p_bridge->pp_es[i]->p_block == NULL )
595 if ( p_bridge->pp_es[i]->id != NULL
596 && p_bridge->pp_es[i]->i_last < i_date )
598 if( !p_sys->b_placeholder )
599 p_stream->p_next->pf_del( p_stream->p_next,
600 p_bridge->pp_es[i]->id );
601 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
602 p_bridge->pp_es[i]->b_changed = true;
603 p_bridge->pp_es[i]->id = NULL;
608 if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
610 block_t *p_block = p_bridge->pp_es[i]->p_block;
611 while ( p_block != NULL )
613 p_bridge->pp_es[i]->i_last = p_block->i_dts;
614 p_block->i_pts += p_sys->i_delay;
615 p_block->i_dts += p_sys->i_delay;
616 p_block = p_block->p_next;
618 sout_stream_id_t *newid = NULL;
619 if( p_sys->b_placeholder )
621 switch( p_bridge->pp_es[i]->fmt.i_cat )
624 p_sys->i_last_video = i_date;
625 newid = p_sys->id_video;
628 if( !p_sys->b_switch_on_iframe ||
629 p_sys->i_state == placeholder_off ||
630 ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
631 p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
633 p_stream->p_next->pf_send( p_stream->p_next,
635 p_bridge->pp_es[i]->p_block );
636 p_sys->i_state = placeholder_off;
640 newid = p_sys->id_audio;
643 p_sys->i_last_audio = i_date;
645 p_stream->p_next->pf_send( p_stream->p_next,
646 newid?newid:p_bridge->pp_es[i]->id,
647 p_bridge->pp_es[i]->p_block );
651 else /* !b_placeholder */
652 p_stream->p_next->pf_send( p_stream->p_next,
653 p_bridge->pp_es[i]->id,
654 p_bridge->pp_es[i]->p_block );
658 block_ChainRelease( p_bridge->pp_es[i]->p_block );
661 p_bridge->pp_es[i]->p_block = NULL;
662 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
667 for ( i = 0; i < p_bridge->i_es_num; i++ )
668 free( p_bridge->pp_es[i] );
669 free( p_bridge->pp_es );
671 var_Destroy( p_stream->p_libvlc, p_sys->psz_name );
675 if( p_sys->b_placeholder )
680 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
681 && ( !p_sys->b_switch_on_iframe
682 || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
683 || p_sys->i_state == placeholder_on )
685 p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
686 p_sys->i_state = placeholder_on;
689 block_Release( p_buffer );
693 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
694 p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
696 block_Release( p_buffer );
700 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
705 vlc_mutex_unlock( &lock );