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 )
259 p_sys->b_inited = true;
261 vlc_mutex_lock( p_sys->p_lock );
263 p_bridge = GetBridge( p_stream, p_sys->psz_name );
264 if ( p_bridge == NULL )
266 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
269 p_bridge = malloc( sizeof( bridge_t ) );
271 var_Create( p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
272 val.p_address = p_bridge;
273 var_Set( p_libvlc, p_sys->psz_name, val );
275 p_bridge->i_es_num = 0;
276 p_bridge->pp_es = NULL;
279 for ( i = 0; i < p_bridge->i_es_num; i++ )
281 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
285 if ( i == p_bridge->i_es_num )
287 p_bridge->pp_es = realloc( p_bridge->pp_es,
288 (p_bridge->i_es_num + 1)
289 * sizeof(bridged_es_t *) );
290 p_bridge->i_es_num++;
291 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
294 p_sys->p_es = p_es = p_bridge->pp_es[i];
297 p_es->fmt.i_id = p_sys->i_id;
298 p_es->p_block = NULL;
299 p_es->pp_last = &p_es->p_block;
300 p_es->b_empty = false;
304 p_es->b_changed = true;
306 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
307 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
309 vlc_mutex_unlock( p_sys->p_lock );
311 return (sout_stream_id_t *)p_sys;
314 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
317 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
320 if ( !p_sys->b_inited )
325 vlc_mutex_lock( p_sys->p_lock );
329 p_es->b_empty = true;
330 block_ChainRelease( p_es->p_block );
331 p_es->p_block = false;
333 p_es->b_changed = true;
334 vlc_mutex_unlock( p_sys->p_lock );
336 p_sys->b_inited = false;
341 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
344 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
347 if ( (out_sout_stream_sys_t *)id != p_sys )
349 block_ChainRelease( p_buffer );
353 vlc_mutex_lock( p_sys->p_lock );
356 *p_es->pp_last = p_buffer;
357 while ( p_buffer != NULL )
359 p_es->pp_last = &p_buffer->p_next;
360 p_buffer = p_buffer->p_next;
363 vlc_mutex_unlock( p_sys->p_lock );
373 typedef struct in_sout_stream_sys_t
375 sout_stream_t *p_out;
383 mtime_t i_placeholder_delay;
384 sout_stream_id_t *id_video;
385 mtime_t i_last_video;
386 sout_stream_id_t *id_audio;
387 mtime_t i_last_audio;
388 } in_sout_stream_sys_t;
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 ) );
401 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
404 msg_Err( p_stream, "cannot create chain" );
409 config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
412 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
413 var_Get( p_this->p_libvlc, "bridge-lock", &val );
414 p_sys->p_lock = val.p_address;
416 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
417 p_sys->i_id_offset = val.i_int;
419 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
420 p_sys->i_delay = (mtime_t)val.i_int * 1000;
422 var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
423 if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
425 free( val.psz_string );
429 free( val.psz_string );
431 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
432 p_sys->b_placeholder = val.b_bool;
434 var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
435 p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
437 p_sys->i_last_video = 0;
438 p_sys->i_last_audio = 0;
439 p_sys->id_video = NULL;
440 p_sys->id_audio = NULL;
442 p_stream->pf_add = AddIn;
443 p_stream->pf_del = DelIn;
444 p_stream->pf_send = SendIn;
446 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
448 /* update p_sout->i_out_pace_nocontrol */
449 p_stream->p_sout->i_out_pace_nocontrol++;
454 /*****************************************************************************
456 *****************************************************************************/
457 static void CloseIn( vlc_object_t * p_this )
459 sout_stream_t *p_stream = (sout_stream_t*)p_this;
460 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
462 sout_StreamDelete( p_sys->p_out );
463 p_stream->p_sout->i_out_pace_nocontrol--;
465 free( p_sys->psz_name );
469 struct sout_stream_id_t
471 sout_stream_id_t *id;
472 int i_cat; /* es category. Used for placeholder option */
475 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
477 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
479 sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
480 if( !id ) return NULL;
482 id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
489 if( p_sys->b_placeholder )
491 id->i_cat = p_fmt->i_cat;
492 switch( p_fmt->i_cat )
495 if( p_sys->id_video != NULL )
496 msg_Err( p_stream, "We already had a video es!" );
497 p_sys->id_video = id->id;
500 if( p_sys->id_audio != NULL )
501 msg_Err( p_stream, "We already had an audio es!" );
502 p_sys->id_audio = id->id;
510 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
512 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
514 if( id == p_sys->id_video ) p_sys->id_video = NULL;
515 if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
517 int ret = p_sys->p_out->pf_del( p_sys->p_out, id->id );
523 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
526 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
530 int i_date = mdate();
532 /* First forward the packet for our own ES */
533 if( !p_sys->b_placeholder )
534 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
536 /* Then check all bridged streams */
537 vlc_mutex_lock( p_sys->p_lock );
539 p_bridge = GetBridge( p_stream, p_sys->psz_name );
543 for ( i = 0; i < p_bridge->i_es_num; i++ )
545 if ( !p_bridge->pp_es[i]->b_empty )
548 while ( p_bridge->pp_es[i]->p_block != NULL
549 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
551 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
552 < p_bridge->pp_es[i]->i_last) )
554 block_t *p_block = p_bridge->pp_es[i]->p_block;
555 msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
556 i_date - p_block->i_dts - p_sys->i_delay );
557 p_bridge->pp_es[i]->p_block
558 = p_bridge->pp_es[i]->p_block->p_next;
559 block_Release( p_block );
562 if ( p_bridge->pp_es[i]->p_block == NULL )
564 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
567 if ( p_bridge->pp_es[i]->b_changed )
569 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
571 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
575 /* We need at least two packets to enter the mux. */
576 if ( p_bridge->pp_es[i]->p_block == NULL
577 || p_bridge->pp_es[i]->p_block->p_next == NULL )
582 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
583 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
584 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
585 if ( p_bridge->pp_es[i]->id == NULL )
587 msg_Warn( p_stream, "couldn't create chain for id %d",
588 p_bridge->pp_es[i]->fmt.i_id );
590 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
591 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
592 p_bridge->pp_es[i]->fmt.i_id, i );
595 p_bridge->pp_es[i]->b_changed = false;
597 if ( p_bridge->pp_es[i]->b_empty )
600 if ( p_bridge->pp_es[i]->p_block == NULL )
602 if ( p_bridge->pp_es[i]->id != NULL
603 && p_bridge->pp_es[i]->i_last < i_date )
605 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
606 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
607 p_bridge->pp_es[i]->b_changed = true;
608 p_bridge->pp_es[i]->id = NULL;
613 if ( p_bridge->pp_es[i]->id != NULL )
615 block_t *p_block = p_bridge->pp_es[i]->p_block;
616 while ( p_block != NULL )
618 p_bridge->pp_es[i]->i_last = p_block->i_dts;
619 p_block->i_pts += p_sys->i_delay;
620 p_block->i_dts += p_sys->i_delay;
621 p_block = p_block->p_next;
623 sout_stream_id_t *newid = NULL;
624 if( p_sys->b_placeholder )
626 switch( p_bridge->pp_es[i]->fmt.i_cat )
629 p_sys->i_last_video = i_date;
630 newid = p_sys->id_video;
633 newid = p_sys->id_audio;
634 p_sys->i_last_audio = i_date;
638 p_sys->p_out->pf_send( p_sys->p_out,
639 newid ? newid : p_bridge->pp_es[i]->id,
640 p_bridge->pp_es[i]->p_block );
644 block_ChainRelease( p_bridge->pp_es[i]->p_block );
647 p_bridge->pp_es[i]->p_block = NULL;
648 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
653 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
654 for ( i = 0; i < p_bridge->i_es_num; i++ )
655 free( p_bridge->pp_es[i] );
656 free( p_bridge->pp_es );
658 var_Destroy( p_libvlc, p_sys->psz_name );
662 if( p_sys->b_placeholder )
667 if( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date )
668 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
670 block_Release( p_buffer );
674 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
675 p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
677 block_Release( p_buffer );
681 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
686 vlc_mutex_unlock( p_sys->p_lock );