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 placholder" )
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, NULL, ID_TEXT, ID_LONGTEXT,
103 add_string( SOUT_CFG_PREFIX_OUT "in-name", "default", NULL,
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, NULL, DELAY_TEXT,
114 DELAY_LONGTEXT, false )
115 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
116 ID_OFFSET_LONGTEXT, false )
117 add_string( SOUT_CFG_PREFIX_IN "name", "default", NULL,
118 NAME_TEXT, NAME_LONGTEXT, false )
119 add_bool( SOUT_CFG_PREFIX_IN "placeholder", false, NULL,
120 PLACEHOLDER_TEXT, PLACEHOLDER_LONGTEXT, false )
121 add_integer( SOUT_CFG_PREFIX_IN "placeholder-delay", 200, NULL,
122 PLACEHOLDER_DELAY_TEXT, PLACEHOLDER_DELAY_LONGTEXT, false )
123 add_bool( SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", true, NULL,
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;
175 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_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
203 var_Get( p_this->p_libvlc, "bridge-lock", &val );
204 p_sys->p_lock = val.p_address;
206 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
207 p_sys->i_id = val.i_int;
209 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
210 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
212 free( val.psz_string );
216 free( val.psz_string );
218 p_stream->pf_add = AddOut;
219 p_stream->pf_del = DelOut;
220 p_stream->pf_send = SendOut;
222 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
224 p_stream->p_sout->i_out_pace_nocontrol++;
229 /*****************************************************************************
231 *****************************************************************************/
232 static void CloseOut( vlc_object_t * p_this )
234 sout_stream_t *p_stream = (sout_stream_t*)p_this;
235 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
237 p_stream->p_sout->i_out_pace_nocontrol--;
239 free( p_sys->psz_name );
243 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
245 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
250 if ( p_sys->b_inited )
252 msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
255 p_sys->b_inited = true;
257 vlc_mutex_lock( p_sys->p_lock );
259 p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
260 if ( p_bridge == NULL )
262 p_bridge = xmalloc( sizeof( bridge_t ) );
264 var_Create( p_stream->p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
265 var_SetAddress( p_stream->p_libvlc, p_sys->psz_name, p_bridge );
267 p_bridge->i_es_num = 0;
268 p_bridge->pp_es = NULL;
271 for ( i = 0; i < p_bridge->i_es_num; i++ )
273 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
277 if ( i == p_bridge->i_es_num )
279 p_bridge->pp_es = xrealloc( p_bridge->pp_es,
280 (p_bridge->i_es_num + 1) * sizeof(bridged_es_t *) );
281 p_bridge->i_es_num++;
282 p_bridge->pp_es[i] = xmalloc( sizeof(bridged_es_t) );
285 p_sys->p_es = p_es = p_bridge->pp_es[i];
288 p_es->fmt.i_id = p_sys->i_id;
289 p_es->p_block = NULL;
290 p_es->pp_last = &p_es->p_block;
291 p_es->b_empty = false;
295 p_es->b_changed = true;
297 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
298 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
300 vlc_mutex_unlock( p_sys->p_lock );
302 return (sout_stream_id_t *)p_sys;
305 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
308 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
311 if ( !p_sys->b_inited )
316 vlc_mutex_lock( p_sys->p_lock );
320 p_es->b_empty = true;
321 block_ChainRelease( p_es->p_block );
322 p_es->p_block = false;
324 p_es->b_changed = true;
325 vlc_mutex_unlock( p_sys->p_lock );
327 p_sys->b_inited = false;
332 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
335 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
338 if ( (out_sout_stream_sys_t *)id != p_sys )
340 block_ChainRelease( p_buffer );
344 vlc_mutex_lock( p_sys->p_lock );
347 *p_es->pp_last = p_buffer;
348 while ( p_buffer != NULL )
350 p_es->pp_last = &p_buffer->p_next;
351 p_buffer = p_buffer->p_next;
354 vlc_mutex_unlock( p_sys->p_lock );
364 typedef struct in_sout_stream_sys_t
366 sout_stream_t *p_out;
374 bool b_switch_on_iframe;
376 mtime_t i_placeholder_delay;
377 sout_stream_id_t *id_video;
378 mtime_t i_last_video;
379 sout_stream_id_t *id_audio;
380 mtime_t i_last_audio;
381 } in_sout_stream_sys_t;
383 enum { placeholder_on, placeholder_off };
385 /*****************************************************************************
387 *****************************************************************************/
388 static int OpenIn( vlc_object_t *p_this )
390 sout_stream_t *p_stream = (sout_stream_t*)p_this;
391 in_sout_stream_sys_t *p_sys;
394 p_sys = malloc( sizeof( in_sout_stream_sys_t ) );
395 if( unlikely( !p_sys ) )
398 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
401 msg_Err( p_stream, "cannot create chain" );
406 config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
409 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
410 var_Get( p_this->p_libvlc, "bridge-lock", &val );
411 p_sys->p_lock = val.p_address;
413 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
414 p_sys->i_id_offset = val.i_int;
416 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
417 p_sys->i_delay = (mtime_t)val.i_int * 1000;
419 var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
420 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
422 free( val.psz_string );
426 free( val.psz_string );
428 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
429 p_sys->b_placeholder = val.b_bool;
431 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", &val);
432 p_sys->b_switch_on_iframe = val.b_bool;
434 p_sys->i_state = placeholder_on;
436 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
437 p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
439 p_sys->i_last_video = 0;
440 p_sys->i_last_audio = 0;
441 p_sys->id_video = NULL;
442 p_sys->id_audio = NULL;
444 p_stream->pf_add = AddIn;
445 p_stream->pf_del = DelIn;
446 p_stream->pf_send = SendIn;
448 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
450 /* update p_sout->i_out_pace_nocontrol */
451 p_stream->p_sout->i_out_pace_nocontrol++;
456 /*****************************************************************************
458 *****************************************************************************/
459 static void CloseIn( vlc_object_t * p_this )
461 sout_stream_t *p_stream = (sout_stream_t*)p_this;
462 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
464 sout_StreamDelete( p_sys->p_out );
465 p_stream->p_sout->i_out_pace_nocontrol--;
467 free( p_sys->psz_name );
471 struct sout_stream_id_t
473 sout_stream_id_t *id;
474 int i_cat; /* es category. Used for placeholder option */
477 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
479 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
481 sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
482 if( !id ) return NULL;
484 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
491 if( p_sys->b_placeholder )
493 id->i_cat = p_fmt->i_cat;
494 switch( p_fmt->i_cat )
497 if( p_sys->id_video != NULL )
498 msg_Err( p_stream, "We already had a video es!" );
499 p_sys->id_video = id->id;
502 if( p_sys->id_audio != NULL )
503 msg_Err( p_stream, "We already had an audio es!" );
504 p_sys->id_audio = id->id;
512 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
514 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
516 if( id == p_sys->id_video ) p_sys->id_video = NULL;
517 if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
519 int ret = p_sys->p_out->pf_del( p_sys->p_out, id->id );
525 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
528 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
532 int i_date = mdate();
534 /* First forward the packet for our own ES */
535 if( !p_sys->b_placeholder )
536 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
538 /* Then check all bridged streams */
539 vlc_mutex_lock( p_sys->p_lock );
541 p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
545 for ( i = 0; i < p_bridge->i_es_num; i++ )
547 if ( !p_bridge->pp_es[i]->b_empty )
550 while ( p_bridge->pp_es[i]->p_block != NULL
551 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
553 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
554 < p_bridge->pp_es[i]->i_last) )
556 block_t *p_block = p_bridge->pp_es[i]->p_block;
557 msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
558 i_date - p_block->i_dts - p_sys->i_delay );
559 p_bridge->pp_es[i]->p_block
560 = p_bridge->pp_es[i]->p_block->p_next;
561 block_Release( p_block );
564 if ( p_bridge->pp_es[i]->p_block == NULL )
566 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
569 if ( p_bridge->pp_es[i]->b_changed )
571 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
573 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
577 /* We need at least two packets to enter the mux. */
578 if ( p_bridge->pp_es[i]->p_block == NULL
579 || p_bridge->pp_es[i]->p_block->p_next == NULL )
584 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
585 if( !p_sys->b_placeholder )
587 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
588 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
589 if ( p_bridge->pp_es[i]->id == NULL )
591 msg_Warn( p_stream, "couldn't create chain for id %d",
592 p_bridge->pp_es[i]->fmt.i_id );
595 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
596 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
597 p_bridge->pp_es[i]->fmt.i_id, i );
600 p_bridge->pp_es[i]->b_changed = false;
602 if ( p_bridge->pp_es[i]->b_empty )
605 if ( p_bridge->pp_es[i]->p_block == NULL )
607 if ( p_bridge->pp_es[i]->id != NULL
608 && p_bridge->pp_es[i]->i_last < i_date )
610 if( !p_sys->b_placeholder )
611 p_sys->p_out->pf_del( p_sys->p_out,
612 p_bridge->pp_es[i]->id );
613 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
614 p_bridge->pp_es[i]->b_changed = true;
615 p_bridge->pp_es[i]->id = NULL;
620 if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
622 block_t *p_block = p_bridge->pp_es[i]->p_block;
623 while ( p_block != NULL )
625 p_bridge->pp_es[i]->i_last = p_block->i_dts;
626 p_block->i_pts += p_sys->i_delay;
627 p_block->i_dts += p_sys->i_delay;
628 p_block = p_block->p_next;
630 sout_stream_id_t *newid = NULL;
631 if( p_sys->b_placeholder )
633 switch( p_bridge->pp_es[i]->fmt.i_cat )
636 p_sys->i_last_video = i_date;
637 newid = p_sys->id_video;
640 if( !p_sys->b_switch_on_iframe ||
641 p_sys->i_state == placeholder_off ||
642 ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
643 p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
645 p_sys->p_out->pf_send( p_sys->p_out,
647 p_bridge->pp_es[i]->p_block );
648 p_sys->i_state = placeholder_off;
652 newid = p_sys->id_audio;
655 p_sys->i_last_audio = i_date;
657 p_sys->p_out->pf_send( p_sys->p_out,
658 newid?newid:p_bridge->pp_es[i]->id,
659 p_bridge->pp_es[i]->p_block );
663 else /* !b_placeholder */
664 p_sys->p_out->pf_send( p_sys->p_out,
665 p_bridge->pp_es[i]->id,
666 p_bridge->pp_es[i]->p_block );
670 block_ChainRelease( p_bridge->pp_es[i]->p_block );
673 p_bridge->pp_es[i]->p_block = NULL;
674 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
679 for ( i = 0; i < p_bridge->i_es_num; i++ )
680 free( p_bridge->pp_es[i] );
681 free( p_bridge->pp_es );
683 var_Destroy( p_stream->p_libvlc, p_sys->psz_name );
687 if( p_sys->b_placeholder )
692 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
693 && ( !p_sys->b_switch_on_iframe
694 || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
695 || p_sys->i_state == placeholder_on )
697 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
698 p_sys->i_state = placeholder_on;
701 block_Release( p_buffer );
705 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
706 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
708 block_Release( p_buffer );
712 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
717 vlc_mutex_unlock( p_sys->p_lock );