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 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_block.h>
39 #include <vlc_memory.h>
41 /*****************************************************************************
43 *****************************************************************************/
44 #define ID_TEXT N_("ID")
45 #define ID_LONGTEXT N_( \
46 "Integer identifier for this elementary stream. This will be used to " \
47 "\"find\" this stream later." )
49 #define DEST_TEXT N_( "Destination bridge-in name" )
50 #define DEST_LONGTEXT N_( \
51 "Name of the destination bridge-in. If you do not need more " \
52 "than one bridge-in at a time, you can discard this option." )
54 #define DELAY_TEXT N_("Delay")
55 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
56 "will be delayed according to this value (in milliseconds, should be "\
57 ">= 100 ms). For high values, you will need to raise caching values." )
59 #define ID_OFFSET_TEXT N_("ID Offset")
60 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
61 "bridge_out to obtain the stream IDs bridge_in will register.")
63 #define NAME_TEXT N_( "Name of current instance" )
64 #define NAME_LONGTEXT N_( \
65 "Name of this bridge-in instance. If you do not need more " \
66 "than one bridge-in at a time, you can discard this option." )
68 #define PLACEHOLDER_TEXT N_( "Fallback to placeholder stream when out of data" )
69 #define PLACEHOLDER_LONGTEXT N_( \
70 "If set to true, the bridge will discard all input elementary streams " \
71 "except if it doesn't receive data from another bridge-in. This can " \
72 "be used to configure a place holder stream when the real source " \
73 "breaks. Source and placeholder streams should have the same format. " )
75 #define PLACEHOLDER_DELAY_TEXT N_( "Placeholder delay" )
76 #define PLACEHOLDER_DELAY_LONGTEXT N_( \
77 "Delay (in ms) before the placeholder kicks in." )
79 #define PLACEHOLDER_IFRAME_TEXT N_( "Wait for I frame before toggling placholder" )
80 #define PLACEHOLDER_IFRAME_LONGTEXT N_( \
81 "If enabled, switching between the placeholder and the normal stream " \
82 "will only occur on I frames. This will remove artifacts on stream " \
83 "switching at the expense of a slightly longer delay, depending on " \
84 "the frequence of I frames in the streams." )
86 static int OpenOut ( vlc_object_t * );
87 static void CloseOut( vlc_object_t * );
88 static int OpenIn ( vlc_object_t * );
89 static void CloseIn ( vlc_object_t * );
91 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
92 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
95 set_shortname( N_("Bridge"))
96 set_description( N_("Bridge stream output"))
98 set_section( N_("Bridge out"), NULL )
99 set_capability( "sout stream", 50 )
100 add_shortcut( "bridge-out" )
101 /* Only usable with VLM. No category so not in gui preferences
102 set_category( CAT_SOUT )
103 set_subcategory( SUBCAT_SOUT_STREAM )*/
104 add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
106 add_string( SOUT_CFG_PREFIX_OUT "in-name", "default", NULL,
107 DEST_TEXT, DEST_LONGTEXT, false )
108 set_callbacks( OpenOut, CloseOut )
111 set_section( N_("Bridge in"), NULL )
112 set_capability( "sout stream", 50 )
113 add_shortcut( "bridge-in" )
114 /*set_category( CAT_SOUT )
115 set_subcategory( SUBCAT_SOUT_STREAM )*/
116 add_integer( SOUT_CFG_PREFIX_IN "delay", 0, NULL, DELAY_TEXT,
117 DELAY_LONGTEXT, false )
118 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
119 ID_OFFSET_LONGTEXT, false )
120 add_string( SOUT_CFG_PREFIX_IN "name", "default", NULL,
121 NAME_TEXT, NAME_LONGTEXT, false )
122 add_bool( SOUT_CFG_PREFIX_IN "placeholder", false, NULL,
123 PLACEHOLDER_TEXT, PLACEHOLDER_LONGTEXT, false )
124 add_integer( SOUT_CFG_PREFIX_IN "placeholder-delay", 200, NULL,
125 PLACEHOLDER_DELAY_TEXT, PLACEHOLDER_DELAY_LONGTEXT, false )
126 add_bool( SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", true, NULL,
127 PLACEHOLDER_IFRAME_TEXT, PLACEHOLDER_IFRAME_LONGTEXT, false )
128 set_callbacks( OpenIn, CloseIn )
133 /*****************************************************************************
135 *****************************************************************************/
136 static const char *const ppsz_sout_options_out[] = {
137 "id", "in-name", NULL
140 static const char *const ppsz_sout_options_in[] = {
141 "delay", "id-offset", "name",
142 "placeholder", "placeholder-delay", "placeholder-switch-on-iframe",
146 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
147 static int DelOut ( sout_stream_t *, sout_stream_id_t * );
148 static int SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
150 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
151 static int DelIn ( sout_stream_t *, sout_stream_id_t * );
152 static int SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
154 typedef struct bridged_es_t
162 sout_stream_id_t *id;
167 typedef struct bridge_t
169 bridged_es_t **pp_es;
178 typedef struct out_sout_stream_sys_t
186 } out_sout_stream_sys_t;
188 /*****************************************************************************
190 *****************************************************************************/
191 static int OpenOut( vlc_object_t *p_this )
193 sout_stream_t *p_stream = (sout_stream_t *)p_this;
194 out_sout_stream_sys_t *p_sys;
197 config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
200 p_sys = malloc( sizeof( out_sout_stream_sys_t ) );
202 p_sys->b_inited = false;
204 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
205 var_Get( p_this->p_libvlc, "bridge-lock", &val );
206 p_sys->p_lock = val.p_address;
208 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
209 p_sys->i_id = val.i_int;
211 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
212 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
214 free( val.psz_string );
218 free( val.psz_string );
220 p_stream->pf_add = AddOut;
221 p_stream->pf_del = DelOut;
222 p_stream->pf_send = SendOut;
224 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
226 p_stream->p_sout->i_out_pace_nocontrol++;
231 /*****************************************************************************
233 *****************************************************************************/
234 static void CloseOut( vlc_object_t * p_this )
236 sout_stream_t *p_stream = (sout_stream_t*)p_this;
237 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
239 p_stream->p_sout->i_out_pace_nocontrol--;
241 free( p_sys->psz_name );
245 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
247 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
252 if ( p_sys->b_inited )
254 msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
257 p_sys->b_inited = true;
259 vlc_mutex_lock( p_sys->p_lock );
261 p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
262 if ( p_bridge == NULL )
264 p_bridge = malloc( sizeof( bridge_t ) );
267 var_Create( p_stream->p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
268 var_SetAddress( p_stream->p_libvlc, p_sys->psz_name, p_bridge );
270 p_bridge->i_es_num = 0;
271 p_bridge->pp_es = NULL;
274 for ( i = 0; i < p_bridge->i_es_num; i++ )
276 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
280 if ( i == p_bridge->i_es_num )
282 p_bridge->pp_es = realloc_or_free( p_bridge->pp_es,
283 (p_bridge->i_es_num + 1) * sizeof(bridged_es_t *) );
284 assert( p_bridge->pp_es );
285 p_bridge->i_es_num++;
286 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
287 assert( p_bridge->pp_es[i] );
290 p_sys->p_es = p_es = p_bridge->pp_es[i];
293 p_es->fmt.i_id = p_sys->i_id;
294 p_es->p_block = NULL;
295 p_es->pp_last = &p_es->p_block;
296 p_es->b_empty = false;
300 p_es->b_changed = true;
302 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
303 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
305 vlc_mutex_unlock( p_sys->p_lock );
307 return (sout_stream_id_t *)p_sys;
310 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
313 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
316 if ( !p_sys->b_inited )
321 vlc_mutex_lock( p_sys->p_lock );
325 p_es->b_empty = true;
326 block_ChainRelease( p_es->p_block );
327 p_es->p_block = false;
329 p_es->b_changed = true;
330 vlc_mutex_unlock( p_sys->p_lock );
332 p_sys->b_inited = false;
337 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
340 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
343 if ( (out_sout_stream_sys_t *)id != p_sys )
345 block_ChainRelease( p_buffer );
349 vlc_mutex_lock( p_sys->p_lock );
352 *p_es->pp_last = p_buffer;
353 while ( p_buffer != NULL )
355 p_es->pp_last = &p_buffer->p_next;
356 p_buffer = p_buffer->p_next;
359 vlc_mutex_unlock( p_sys->p_lock );
369 typedef struct in_sout_stream_sys_t
371 sout_stream_t *p_out;
379 bool b_switch_on_iframe;
381 mtime_t i_placeholder_delay;
382 sout_stream_id_t *id_video;
383 mtime_t i_last_video;
384 sout_stream_id_t *id_audio;
385 mtime_t i_last_audio;
386 } in_sout_stream_sys_t;
388 enum { placeholder_on, placeholder_off };
390 /*****************************************************************************
392 *****************************************************************************/
393 static int OpenIn( vlc_object_t *p_this )
395 sout_stream_t *p_stream = (sout_stream_t*)p_this;
396 in_sout_stream_sys_t *p_sys;
399 p_sys = malloc( sizeof( in_sout_stream_sys_t ) );
402 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
405 msg_Err( p_stream, "cannot create chain" );
410 config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
413 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
414 var_Get( p_this->p_libvlc, "bridge-lock", &val );
415 p_sys->p_lock = val.p_address;
417 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
418 p_sys->i_id_offset = val.i_int;
420 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
421 p_sys->i_delay = (mtime_t)val.i_int * 1000;
423 var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
424 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
426 free( val.psz_string );
430 free( val.psz_string );
432 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
433 p_sys->b_placeholder = val.b_bool;
435 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", &val);
436 p_sys->b_switch_on_iframe = val.b_bool;
438 p_sys->i_state = placeholder_on;
440 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
441 p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
443 p_sys->i_last_video = 0;
444 p_sys->i_last_audio = 0;
445 p_sys->id_video = NULL;
446 p_sys->id_audio = NULL;
448 p_stream->pf_add = AddIn;
449 p_stream->pf_del = DelIn;
450 p_stream->pf_send = SendIn;
452 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
454 /* update p_sout->i_out_pace_nocontrol */
455 p_stream->p_sout->i_out_pace_nocontrol++;
460 /*****************************************************************************
462 *****************************************************************************/
463 static void CloseIn( vlc_object_t * p_this )
465 sout_stream_t *p_stream = (sout_stream_t*)p_this;
466 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
468 sout_StreamDelete( p_sys->p_out );
469 p_stream->p_sout->i_out_pace_nocontrol--;
471 free( p_sys->psz_name );
475 struct sout_stream_id_t
477 sout_stream_id_t *id;
478 int i_cat; /* es category. Used for placeholder option */
481 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
483 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
485 sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
486 if( !id ) return NULL;
488 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
495 if( p_sys->b_placeholder )
497 id->i_cat = p_fmt->i_cat;
498 switch( p_fmt->i_cat )
501 if( p_sys->id_video != NULL )
502 msg_Err( p_stream, "We already had a video es!" );
503 p_sys->id_video = id->id;
506 if( p_sys->id_audio != NULL )
507 msg_Err( p_stream, "We already had an audio es!" );
508 p_sys->id_audio = id->id;
516 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
518 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
520 if( id == p_sys->id_video ) p_sys->id_video = NULL;
521 if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
523 int ret = p_sys->p_out->pf_del( p_sys->p_out, id->id );
529 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
532 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
536 int i_date = mdate();
538 /* First forward the packet for our own ES */
539 if( !p_sys->b_placeholder )
540 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
542 /* Then check all bridged streams */
543 vlc_mutex_lock( p_sys->p_lock );
545 p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
549 for ( i = 0; i < p_bridge->i_es_num; i++ )
551 if ( !p_bridge->pp_es[i]->b_empty )
554 while ( p_bridge->pp_es[i]->p_block != NULL
555 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
557 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
558 < p_bridge->pp_es[i]->i_last) )
560 block_t *p_block = p_bridge->pp_es[i]->p_block;
561 msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
562 i_date - p_block->i_dts - p_sys->i_delay );
563 p_bridge->pp_es[i]->p_block
564 = p_bridge->pp_es[i]->p_block->p_next;
565 block_Release( p_block );
568 if ( p_bridge->pp_es[i]->p_block == NULL )
570 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
573 if ( p_bridge->pp_es[i]->b_changed )
575 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
577 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
581 /* We need at least two packets to enter the mux. */
582 if ( p_bridge->pp_es[i]->p_block == NULL
583 || p_bridge->pp_es[i]->p_block->p_next == NULL )
588 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
589 if( !p_sys->b_placeholder )
591 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
592 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
593 if ( p_bridge->pp_es[i]->id == NULL )
595 msg_Warn( p_stream, "couldn't create chain for id %d",
596 p_bridge->pp_es[i]->fmt.i_id );
599 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
600 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
601 p_bridge->pp_es[i]->fmt.i_id, i );
604 p_bridge->pp_es[i]->b_changed = false;
606 if ( p_bridge->pp_es[i]->b_empty )
609 if ( p_bridge->pp_es[i]->p_block == NULL )
611 if ( p_bridge->pp_es[i]->id != NULL
612 && p_bridge->pp_es[i]->i_last < i_date )
614 if( !p_sys->b_placeholder )
615 p_sys->p_out->pf_del( p_sys->p_out,
616 p_bridge->pp_es[i]->id );
617 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
618 p_bridge->pp_es[i]->b_changed = true;
619 p_bridge->pp_es[i]->id = NULL;
624 if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
626 block_t *p_block = p_bridge->pp_es[i]->p_block;
627 while ( p_block != NULL )
629 p_bridge->pp_es[i]->i_last = p_block->i_dts;
630 p_block->i_pts += p_sys->i_delay;
631 p_block->i_dts += p_sys->i_delay;
632 p_block = p_block->p_next;
634 sout_stream_id_t *newid = NULL;
635 if( p_sys->b_placeholder )
637 switch( p_bridge->pp_es[i]->fmt.i_cat )
640 p_sys->i_last_video = i_date;
641 newid = p_sys->id_video;
644 if( !p_sys->b_switch_on_iframe ||
645 p_sys->i_state == placeholder_off ||
646 ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
647 p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
649 p_sys->p_out->pf_send( p_sys->p_out,
651 p_bridge->pp_es[i]->p_block );
652 p_sys->i_state = placeholder_off;
656 newid = p_sys->id_audio;
659 p_sys->i_last_audio = i_date;
661 p_sys->p_out->pf_send( p_sys->p_out,
662 newid?newid:p_bridge->pp_es[i]->id,
663 p_bridge->pp_es[i]->p_block );
667 else /* !b_placeholder */
668 p_sys->p_out->pf_send( p_sys->p_out,
669 p_bridge->pp_es[i]->id,
670 p_bridge->pp_es[i]->p_block );
674 block_ChainRelease( p_bridge->pp_es[i]->p_block );
677 p_bridge->pp_es[i]->p_block = NULL;
678 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
683 for ( i = 0; i < p_bridge->i_es_num; i++ )
684 free( p_bridge->pp_es[i] );
685 free( p_bridge->pp_es );
687 var_Destroy( p_stream->p_libvlc, p_sys->psz_name );
691 if( p_sys->b_placeholder )
696 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
697 && ( !p_sys->b_switch_on_iframe
698 || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
699 || p_sys->i_state == placeholder_on )
701 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
702 p_sys->i_state = placeholder_on;
705 block_Release( p_buffer );
709 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
710 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
712 block_Release( p_buffer );
716 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
721 vlc_mutex_unlock( p_sys->p_lock );