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_( "Place holder delay" )
73 #define PLACEHOLDER_DELAY_LONGTEXT N_( \
74 "Delay (in ms) before the place holder kicks in." )
76 static int OpenOut ( vlc_object_t * );
77 static void CloseOut( vlc_object_t * );
78 static int OpenIn ( vlc_object_t * );
79 static void CloseIn ( vlc_object_t * );
81 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
82 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
85 set_shortname( N_("Bridge"));
86 set_description( N_("Bridge stream output"));
88 set_section( N_("Bridge out"), NULL );
89 set_capability( "sout stream", 50 );
90 add_shortcut( "bridge-out" );
91 /* Only usable with VLM. No category so not in gui preferences
92 set_category( CAT_SOUT );
93 set_subcategory( SUBCAT_SOUT_STREAM );*/
94 add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
96 add_string( SOUT_CFG_PREFIX_OUT "in-name", "default", NULL,
97 DEST_TEXT, DEST_LONGTEXT, false );
98 set_callbacks( OpenOut, CloseOut );
101 set_section( N_("Bridge in"), NULL );
102 set_capability( "sout stream", 50 );
103 add_shortcut( "bridge-in" );
104 /*set_category( CAT_SOUT );
105 set_subcategory( SUBCAT_SOUT_STREAM );*/
106 add_integer( SOUT_CFG_PREFIX_IN "delay", 0, NULL, DELAY_TEXT,
107 DELAY_LONGTEXT, false );
108 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
109 ID_OFFSET_LONGTEXT, false );
110 add_string( SOUT_CFG_PREFIX_IN "name", "default", NULL,
111 NAME_TEXT, NAME_LONGTEXT, false );
112 add_bool( SOUT_CFG_PREFIX_IN "placeholder", false, NULL,
113 PLACEHOLDER_TEXT, PLACEHOLDER_LONGTEXT, false );
114 add_integer( SOUT_CFG_PREFIX_IN "placeholder-delay", 200, NULL,
115 PLACEHOLDER_DELAY_TEXT, PLACEHOLDER_DELAY_LONGTEXT, false );
116 set_callbacks( OpenIn, CloseIn );
121 /*****************************************************************************
123 *****************************************************************************/
124 static const char *const ppsz_sout_options_out[] = {
125 "id", "in-name", NULL
128 static const char *const ppsz_sout_options_in[] = {
129 "delay", "id-offset", "name", "placeholder", "placeholder-delay", NULL
132 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
133 static int DelOut ( sout_stream_t *, sout_stream_id_t * );
134 static int SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
136 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
137 static int DelIn ( sout_stream_t *, sout_stream_id_t * );
138 static int SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
140 typedef struct bridged_es_t
148 sout_stream_id_t *id;
153 typedef struct bridge_t
155 bridged_es_t **pp_es;
159 #define GetBridge(a,b) __GetBridge( VLC_OBJECT(a), b )
160 static bridge_t *__GetBridge( vlc_object_t *p_object, const char *psz_name )
165 if( var_Get( p_object->p_libvlc, psz_name, &val ) )
171 p_bridge = val.p_address;
182 typedef struct out_sout_stream_sys_t
190 } out_sout_stream_sys_t;
192 /*****************************************************************************
194 *****************************************************************************/
195 static int OpenOut( vlc_object_t *p_this )
197 sout_stream_t *p_stream = (sout_stream_t *)p_this;
198 out_sout_stream_sys_t *p_sys;
201 config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
204 p_sys = malloc( sizeof( out_sout_stream_sys_t ) );
205 p_sys->b_inited = false;
207 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
208 var_Get( p_this->p_libvlc, "bridge-lock", &val );
209 p_sys->p_lock = val.p_address;
211 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
212 p_sys->i_id = val.i_int;
214 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
215 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
217 free( val.psz_string );
221 free( val.psz_string );
223 p_stream->pf_add = AddOut;
224 p_stream->pf_del = DelOut;
225 p_stream->pf_send = SendOut;
227 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
229 p_stream->p_sout->i_out_pace_nocontrol++;
234 /*****************************************************************************
236 *****************************************************************************/
237 static void CloseOut( vlc_object_t * p_this )
239 sout_stream_t *p_stream = (sout_stream_t*)p_this;
240 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
242 p_stream->p_sout->i_out_pace_nocontrol--;
244 free( p_sys->psz_name );
248 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
250 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
255 if ( p_sys->b_inited )
257 msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
260 p_sys->b_inited = true;
262 vlc_mutex_lock( p_sys->p_lock );
264 p_bridge = GetBridge( p_stream, p_sys->psz_name );
265 if ( p_bridge == NULL )
267 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
270 p_bridge = malloc( sizeof( bridge_t ) );
272 var_Create( p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
273 val.p_address = p_bridge;
274 var_Set( p_libvlc, p_sys->psz_name, val );
276 p_bridge->i_es_num = 0;
277 p_bridge->pp_es = NULL;
280 for ( i = 0; i < p_bridge->i_es_num; i++ )
282 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
286 if ( i == p_bridge->i_es_num )
288 p_bridge->pp_es = realloc( p_bridge->pp_es,
289 (p_bridge->i_es_num + 1)
290 * sizeof(bridged_es_t *) );
291 p_bridge->i_es_num++;
292 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
295 p_sys->p_es = p_es = p_bridge->pp_es[i];
298 p_es->fmt.i_id = p_sys->i_id;
299 p_es->p_block = NULL;
300 p_es->pp_last = &p_es->p_block;
301 p_es->b_empty = false;
305 p_es->b_changed = true;
307 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
308 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
310 vlc_mutex_unlock( p_sys->p_lock );
312 return (sout_stream_id_t *)p_sys;
315 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
318 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
321 if ( !p_sys->b_inited )
326 vlc_mutex_lock( p_sys->p_lock );
330 p_es->b_empty = true;
331 block_ChainRelease( p_es->p_block );
332 p_es->p_block = false;
334 p_es->b_changed = true;
335 vlc_mutex_unlock( p_sys->p_lock );
337 p_sys->b_inited = false;
342 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
345 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
348 if ( (out_sout_stream_sys_t *)id != p_sys )
350 block_ChainRelease( p_buffer );
354 vlc_mutex_lock( p_sys->p_lock );
357 *p_es->pp_last = p_buffer;
358 while ( p_buffer != NULL )
360 p_es->pp_last = &p_buffer->p_next;
361 p_buffer = p_buffer->p_next;
364 vlc_mutex_unlock( p_sys->p_lock );
374 typedef struct in_sout_stream_sys_t
376 sout_stream_t *p_out;
384 mtime_t i_placeholder_delay;
385 sout_stream_id_t *id_video;
386 mtime_t i_last_video;
387 sout_stream_id_t *id_audio;
388 mtime_t i_last_audio;
389 } in_sout_stream_sys_t;
391 /*****************************************************************************
393 *****************************************************************************/
394 static int OpenIn( vlc_object_t *p_this )
396 sout_stream_t *p_stream = (sout_stream_t*)p_this;
397 in_sout_stream_sys_t *p_sys;
400 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-delay", &val );
436 p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
438 p_sys->i_last_video = 0;
439 p_sys->i_last_audio = 0;
440 p_sys->id_video = NULL;
441 p_sys->id_audio = NULL;
443 p_stream->pf_add = AddIn;
444 p_stream->pf_del = DelIn;
445 p_stream->pf_send = SendIn;
447 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
449 /* update p_sout->i_out_pace_nocontrol */
450 p_stream->p_sout->i_out_pace_nocontrol++;
455 /*****************************************************************************
457 *****************************************************************************/
458 static void CloseIn( vlc_object_t * p_this )
460 sout_stream_t *p_stream = (sout_stream_t*)p_this;
461 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
463 sout_StreamDelete( p_sys->p_out );
464 p_stream->p_sout->i_out_pace_nocontrol--;
466 free( p_sys->psz_name );
470 struct sout_stream_id_t
472 sout_stream_id_t *id;
473 int i_cat; /* es category. Used for placeholder option */
476 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
478 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
480 sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
481 if( !id ) return NULL;
483 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
490 if( p_sys->b_placeholder )
492 id->i_cat = p_fmt->i_cat;
493 switch( p_fmt->i_cat )
496 if( p_sys->id_video != NULL )
497 msg_Err( p_stream, "We already had a video es!" );
498 p_sys->id_video = id->id;
501 if( p_sys->id_audio != NULL )
502 msg_Err( p_stream, "We already had an audio es!" );
503 p_sys->id_audio = id->id;
511 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
513 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
515 if( id == p_sys->id_video ) p_sys->id_video = NULL;
516 if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
518 int ret = p_sys->p_out->pf_del( p_sys->p_out, id->id );
524 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
527 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
531 int i_date = mdate();
533 /* First forward the packet for our own ES */
534 if( !p_sys->b_placeholder )
535 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
537 /* Then check all bridged streams */
538 vlc_mutex_lock( p_sys->p_lock );
540 p_bridge = GetBridge( p_stream, p_sys->psz_name );
544 for ( i = 0; i < p_bridge->i_es_num; i++ )
546 if ( !p_bridge->pp_es[i]->b_empty )
549 while ( p_bridge->pp_es[i]->p_block != NULL
550 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
552 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
553 < p_bridge->pp_es[i]->i_last) )
555 block_t *p_block = p_bridge->pp_es[i]->p_block;
556 msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
557 i_date - p_block->i_dts - p_sys->i_delay );
558 p_bridge->pp_es[i]->p_block
559 = p_bridge->pp_es[i]->p_block->p_next;
560 block_Release( p_block );
563 if ( p_bridge->pp_es[i]->p_block == NULL )
565 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
568 if ( p_bridge->pp_es[i]->b_changed )
570 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
572 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
576 /* We need at least two packets to enter the mux. */
577 if ( p_bridge->pp_es[i]->p_block == NULL
578 || p_bridge->pp_es[i]->p_block->p_next == NULL )
583 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
584 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
585 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
586 if ( p_bridge->pp_es[i]->id == NULL )
588 msg_Warn( p_stream, "couldn't create chain for id %d",
589 p_bridge->pp_es[i]->fmt.i_id );
591 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
592 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
593 p_bridge->pp_es[i]->fmt.i_id, i );
596 p_bridge->pp_es[i]->b_changed = false;
598 if ( p_bridge->pp_es[i]->b_empty )
601 if ( p_bridge->pp_es[i]->p_block == NULL )
603 if ( p_bridge->pp_es[i]->id != NULL
604 && p_bridge->pp_es[i]->i_last < i_date )
606 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
607 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
608 p_bridge->pp_es[i]->b_changed = true;
609 p_bridge->pp_es[i]->id = NULL;
614 if ( p_bridge->pp_es[i]->id != NULL )
616 block_t *p_block = p_bridge->pp_es[i]->p_block;
617 while ( p_block != NULL )
619 p_bridge->pp_es[i]->i_last = p_block->i_dts;
620 p_block->i_pts += p_sys->i_delay;
621 p_block->i_dts += p_sys->i_delay;
622 p_block = p_block->p_next;
624 sout_stream_id_t *newid = NULL;
625 if( p_sys->b_placeholder )
627 switch( p_bridge->pp_es[i]->fmt.i_cat )
630 p_sys->i_last_video = i_date;
631 newid = p_sys->id_video;
634 newid = p_sys->id_audio;
635 p_sys->i_last_audio = i_date;
639 p_sys->p_out->pf_send( p_sys->p_out,
640 newid ? newid : p_bridge->pp_es[i]->id,
641 p_bridge->pp_es[i]->p_block );
645 block_ChainRelease( p_bridge->pp_es[i]->p_block );
648 p_bridge->pp_es[i]->p_block = NULL;
649 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
654 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
655 for ( i = 0; i < p_bridge->i_es_num; i++ )
656 free( p_bridge->pp_es[i] );
657 free( p_bridge->pp_es );
659 var_Destroy( p_libvlc, p_sys->psz_name );
663 if( p_sys->b_placeholder )
668 if( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date )
669 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
671 block_Release( p_buffer );
675 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
676 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
678 block_Release( p_buffer );
682 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
687 vlc_mutex_unlock( p_sys->p_lock );