1 /*****************************************************************************
2 * bridge.c: bridge stream output module
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_plugin.h>
35 #include <vlc_block.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 #define ID_TEXT N_("ID")
41 #define ID_LONGTEXT N_( \
42 "Integer identifier for this elementary stream. This will be used to " \
43 "\"find\" this stream later." )
45 #define DELAY_TEXT N_("Delay")
46 #define DELAY_LONGTEXT N_("Pictures coming from the picture video outputs " \
47 "will be delayed according to this value (in milliseconds, should be "\
48 ">= 100 ms). For high values, you will need to raise caching values." )
50 #define ID_OFFSET_TEXT N_("ID Offset")
51 #define ID_OFFSET_LONGTEXT N_("Offset to add to the stream IDs specified in " \
52 "bridge_out to obtain the stream IDs bridge_in will register.")
54 static int OpenOut ( vlc_object_t * );
55 static void CloseOut( vlc_object_t * );
56 static int OpenIn ( vlc_object_t * );
57 static void CloseIn ( vlc_object_t * );
59 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
60 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
63 set_shortname( N_("Bridge"));
64 set_description( N_("Bridge stream output"));
66 set_section( N_("Bridge out"), NULL );
67 set_capability( "sout stream", 50 );
68 add_shortcut( "bridge-out" );
69 /* Only usable with VLM. No category so not in gui preferences
70 set_category( CAT_SOUT );
71 set_subcategory( SUBCAT_SOUT_STREAM );*/
72 add_integer( SOUT_CFG_PREFIX_OUT "id", 0, NULL, ID_TEXT, ID_LONGTEXT,
74 set_callbacks( OpenOut, CloseOut );
77 set_section( N_("Bridge in"), NULL );
78 set_capability( "sout stream", 50 );
79 add_shortcut( "bridge-in" );
80 /*set_category( CAT_SOUT );
81 set_subcategory( SUBCAT_SOUT_STREAM );*/
82 add_integer( SOUT_CFG_PREFIX_IN "delay", 0, NULL, DELAY_TEXT,
83 DELAY_LONGTEXT, false );
84 add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, NULL, ID_OFFSET_TEXT,
85 ID_OFFSET_LONGTEXT, false );
86 set_callbacks( OpenIn, CloseIn );
91 /*****************************************************************************
93 *****************************************************************************/
94 static const char *const ppsz_sout_options_out[] = {
98 static const char *const ppsz_sout_options_in[] = {
99 "delay", "id-offset", NULL
102 static sout_stream_id_t *AddOut ( sout_stream_t *, es_format_t * );
103 static int DelOut ( sout_stream_t *, sout_stream_id_t * );
104 static int SendOut( sout_stream_t *, sout_stream_id_t *, block_t * );
106 static sout_stream_id_t *AddIn ( sout_stream_t *, es_format_t * );
107 static int DelIn ( sout_stream_t *, sout_stream_id_t * );
108 static int SendIn( sout_stream_t *, sout_stream_id_t *, block_t * );
110 typedef struct bridged_es_t
118 sout_stream_id_t *id;
123 typedef struct bridge_t
125 bridged_es_t **pp_es;
129 #define GetBridge(a) __GetBridge( VLC_OBJECT(a) )
130 static bridge_t *__GetBridge( vlc_object_t *p_object )
135 if( var_Get( p_object->p_libvlc, "bridge-struct", &val ) )
141 p_bridge = val.p_address;
152 typedef struct out_sout_stream_sys_t
158 } out_sout_stream_sys_t;
160 /*****************************************************************************
162 *****************************************************************************/
163 static int OpenOut( vlc_object_t *p_this )
165 sout_stream_t *p_stream = (sout_stream_t *)p_this;
166 out_sout_stream_sys_t *p_sys;
169 config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
172 p_sys = malloc( sizeof( out_sout_stream_sys_t ) );
173 p_sys->b_inited = false;
175 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
176 var_Get( p_this->p_libvlc, "bridge-lock", &val );
177 p_sys->p_lock = val.p_address;
179 var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
180 p_sys->i_id = val.i_int;
182 p_stream->pf_add = AddOut;
183 p_stream->pf_del = DelOut;
184 p_stream->pf_send = SendOut;
186 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
188 p_stream->p_sout->i_out_pace_nocontrol++;
193 /*****************************************************************************
195 *****************************************************************************/
196 static void CloseOut( vlc_object_t * p_this )
198 sout_stream_t *p_stream = (sout_stream_t*)p_this;
199 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
201 p_stream->p_sout->i_out_pace_nocontrol--;
206 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
208 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
213 if ( p_sys->b_inited )
217 p_sys->b_inited = true;
219 vlc_mutex_lock( p_sys->p_lock );
221 p_bridge = GetBridge( p_stream );
222 if ( p_bridge == NULL )
224 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
227 p_bridge = malloc( sizeof( bridge_t ) );
229 var_Create( p_libvlc, "bridge-struct", VLC_VAR_ADDRESS );
230 val.p_address = p_bridge;
231 var_Set( p_libvlc, "bridge-struct", val );
233 p_bridge->i_es_num = 0;
234 p_bridge->pp_es = NULL;
237 for ( i = 0; i < p_bridge->i_es_num; i++ )
239 if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
243 if ( i == p_bridge->i_es_num )
245 p_bridge->pp_es = realloc( p_bridge->pp_es,
246 (p_bridge->i_es_num + 1)
247 * sizeof(bridged_es_t *) );
248 p_bridge->i_es_num++;
249 p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
252 p_sys->p_es = p_es = p_bridge->pp_es[i];
255 p_es->fmt.i_id = p_sys->i_id;
256 p_es->p_block = NULL;
257 p_es->pp_last = &p_es->p_block;
258 p_es->b_empty = false;
262 p_es->b_changed = true;
264 msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
265 (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
267 vlc_mutex_unlock( p_sys->p_lock );
269 return (sout_stream_id_t *)p_sys;
272 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
275 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
278 if ( !p_sys->b_inited )
283 vlc_mutex_lock( p_sys->p_lock );
287 p_es->b_empty = true;
288 block_ChainRelease( p_es->p_block );
289 p_es->p_block = false;
291 p_es->b_changed = true;
292 vlc_mutex_unlock( p_sys->p_lock );
294 p_sys->b_inited = false;
299 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
302 out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
305 if ( (out_sout_stream_sys_t *)id != p_sys )
307 block_ChainRelease( p_buffer );
311 vlc_mutex_lock( p_sys->p_lock );
314 *p_es->pp_last = p_buffer;
315 while ( p_buffer != NULL )
317 p_es->pp_last = &p_buffer->p_next;
318 p_buffer = p_buffer->p_next;
321 vlc_mutex_unlock( p_sys->p_lock );
331 typedef struct in_sout_stream_sys_t
333 sout_stream_t *p_out;
337 } in_sout_stream_sys_t;
339 /*****************************************************************************
341 *****************************************************************************/
342 static int OpenIn( vlc_object_t *p_this )
344 sout_stream_t *p_stream = (sout_stream_t*)p_this;
345 in_sout_stream_sys_t *p_sys;
348 p_sys = malloc( sizeof( in_sout_stream_sys_t ) );
350 p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
353 msg_Err( p_stream, "cannot create chain" );
358 config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
361 var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
362 var_Get( p_this->p_libvlc, "bridge-lock", &val );
363 p_sys->p_lock = val.p_address;
365 var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
366 p_sys->i_id_offset = val.i_int;
368 var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
369 p_sys->i_delay = (mtime_t)val.i_int * 1000;
371 p_stream->pf_add = AddIn;
372 p_stream->pf_del = DelIn;
373 p_stream->pf_send = SendIn;
375 p_stream->p_sys = (sout_stream_sys_t *)p_sys;
377 /* update p_sout->i_out_pace_nocontrol */
378 p_stream->p_sout->i_out_pace_nocontrol++;
383 /*****************************************************************************
385 *****************************************************************************/
386 static void CloseIn( vlc_object_t * p_this )
388 sout_stream_t *p_stream = (sout_stream_t*)p_this;
389 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
391 sout_StreamDelete( p_sys->p_out );
392 p_stream->p_sout->i_out_pace_nocontrol--;
397 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
399 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
401 return p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
404 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
406 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
408 return p_sys->p_out->pf_del( p_sys->p_out, id );
411 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
414 in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
419 /* First forward the packet for our own ES */
420 p_sys->p_out->pf_send( p_sys->p_out, id, p_buffer );
422 /* Then check all bridged streams */
423 vlc_mutex_lock( p_sys->p_lock );
425 p_bridge = GetBridge( p_stream );
426 if ( p_bridge == NULL )
428 vlc_mutex_unlock( p_sys->p_lock );
432 for ( i = 0; i < p_bridge->i_es_num; i++ )
434 if ( !p_bridge->pp_es[i]->b_empty )
437 while ( p_bridge->pp_es[i]->p_block != NULL
438 && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
440 || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
441 < p_bridge->pp_es[i]->i_last) )
443 block_t *p_block = p_bridge->pp_es[i]->p_block;
444 msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
445 mdate() - p_block->i_dts - p_sys->i_delay );
446 p_bridge->pp_es[i]->p_block
447 = p_bridge->pp_es[i]->p_block->p_next;
448 block_Release( p_block );
451 if ( p_bridge->pp_es[i]->p_block == NULL )
453 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
456 if ( p_bridge->pp_es[i]->b_changed )
458 if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
460 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
464 /* We need at least two packets to enter the mux. */
465 if ( p_bridge->pp_es[i]->p_block == NULL
466 || p_bridge->pp_es[i]->p_block->p_next == NULL )
471 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
472 p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
473 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
474 if ( p_bridge->pp_es[i]->id == NULL )
476 msg_Warn( p_stream, "couldn't create chain for id %d",
477 p_bridge->pp_es[i]->fmt.i_id );
479 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
480 (char*)&p_bridge->pp_es[i]->fmt.i_codec,
481 p_bridge->pp_es[i]->fmt.i_id, i );
484 p_bridge->pp_es[i]->b_changed = false;
486 if ( p_bridge->pp_es[i]->b_empty )
489 if ( p_bridge->pp_es[i]->p_block == NULL )
491 if ( p_bridge->pp_es[i]->id != NULL
492 && p_bridge->pp_es[i]->i_last < mdate() )
494 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
495 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
496 p_bridge->pp_es[i]->b_changed = true;
497 p_bridge->pp_es[i]->id = NULL;
502 if ( p_bridge->pp_es[i]->id != NULL )
504 block_t *p_block = p_bridge->pp_es[i]->p_block;
505 while ( p_block != NULL )
507 p_bridge->pp_es[i]->i_last = p_block->i_dts;
508 p_block->i_pts += p_sys->i_delay;
509 p_block->i_dts += p_sys->i_delay;
510 p_block = p_block->p_next;
512 p_sys->p_out->pf_send( p_sys->p_out, p_bridge->pp_es[i]->id,
513 p_bridge->pp_es[i]->p_block );
517 block_ChainRelease( p_bridge->pp_es[i]->p_block );
520 p_bridge->pp_es[i]->p_block = NULL;
521 p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
526 vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
527 for ( i = 0; i < p_bridge->i_es_num; i++ )
528 free( p_bridge->pp_es[i] );
529 free( p_bridge->pp_es );
531 var_Destroy( p_libvlc, "bridge-struct" );
534 vlc_mutex_unlock( p_sys->p_lock );