]> git.sesse.net Git - vlc/blob - modules/stream_out/bridge.c
sout_bridge: use var_(Get|Set)Address.
[vlc] / modules / stream_out / bridge.c
1 /*****************************************************************************
2  * bridge.c: bridge stream output module
3  *****************************************************************************
4  * Copyright (C) 2005-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Antoine Cellerier <dionoea at videolan dot org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_sout.h>
36 #include <vlc_block.h>
37
38 /*****************************************************************************
39  * Module descriptor
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." )
45
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." )
50
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." )
55
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.")
59
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." )
64
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. " )
71
72 #define PLACEHOLDER_DELAY_TEXT N_( "Placeholder delay" )
73 #define PLACEHOLDER_DELAY_LONGTEXT N_( \
74     "Delay (in ms) before the placeholder kicks in." )
75
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." )
82
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 * );
87
88 #define SOUT_CFG_PREFIX_OUT "sout-bridge-out-"
89 #define SOUT_CFG_PREFIX_IN "sout-bridge-in-"
90
91 vlc_module_begin ()
92     set_shortname( N_("Bridge"))
93     set_description( N_("Bridge stream output"))
94     add_submodule ()
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,
102                  false )
103     add_string( SOUT_CFG_PREFIX_OUT "in-name", "default", NULL,
104                 DEST_TEXT, DEST_LONGTEXT, false )
105     set_callbacks( OpenOut, CloseOut )
106
107     add_submodule ()
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 )
126
127 vlc_module_end ()
128
129
130 /*****************************************************************************
131  * Local prototypes
132  *****************************************************************************/
133 static const char *const ppsz_sout_options_out[] = {
134     "id", "in-name", NULL
135 };
136
137 static const char *const ppsz_sout_options_in[] = {
138     "delay", "id-offset", "name",
139     "placeholder", "placeholder-delay", "placeholder-switch-on-iframe",
140     NULL
141 };
142
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 * );
146
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 * );
150
151 typedef struct bridged_es_t
152 {
153     es_format_t fmt;
154     block_t *p_block;
155     block_t **pp_last;
156     bool b_empty;
157
158     /* bridge in part */
159     sout_stream_id_t *id;
160     mtime_t i_last;
161     bool b_changed;
162 } bridged_es_t;
163
164 typedef struct bridge_t
165 {
166     bridged_es_t **pp_es;
167     int i_es_num;
168 } bridge_t;
169
170
171 /*
172  * Bridge out
173  */
174
175 typedef struct out_sout_stream_sys_t
176 {
177     vlc_mutex_t *p_lock;
178     bridged_es_t *p_es;
179     int i_id;
180     bool b_inited;
181
182     char *psz_name;
183 } out_sout_stream_sys_t;
184
185 /*****************************************************************************
186  * OpenOut:
187  *****************************************************************************/
188 static int OpenOut( vlc_object_t *p_this )
189 {
190     sout_stream_t     *p_stream = (sout_stream_t *)p_this;
191     out_sout_stream_sys_t *p_sys;
192     vlc_value_t val;
193
194     config_ChainParse( p_stream, SOUT_CFG_PREFIX_OUT, ppsz_sout_options_out,
195                    p_stream->p_cfg );
196
197     p_sys          = malloc( sizeof( out_sout_stream_sys_t ) );
198     p_sys->b_inited = false;
199
200     var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
201     var_Get( p_this->p_libvlc, "bridge-lock", &val );
202     p_sys->p_lock = val.p_address;
203
204     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
205     p_sys->i_id = val.i_int;
206
207     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
208     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
209     {
210         free( val.psz_string );
211         free( p_sys );
212         return VLC_ENOMEM;
213     }
214     free( val.psz_string );
215
216     p_stream->pf_add    = AddOut;
217     p_stream->pf_del    = DelOut;
218     p_stream->pf_send   = SendOut;
219
220     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
221
222     p_stream->p_sout->i_out_pace_nocontrol++;
223
224     return VLC_SUCCESS;
225 }
226
227 /*****************************************************************************
228  * CloseOut:
229  *****************************************************************************/
230 static void CloseOut( vlc_object_t * p_this )
231 {
232     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
233     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
234
235     p_stream->p_sout->i_out_pace_nocontrol--;
236
237     free( p_sys->psz_name );
238     free( p_sys );
239 }
240
241 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
242 {
243     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
244     bridge_t *p_bridge;
245     bridged_es_t *p_es;
246     int i;
247
248     if ( p_sys->b_inited )
249     {
250         msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
251         return NULL;
252     }
253     p_sys->b_inited = true;
254
255     vlc_mutex_lock( p_sys->p_lock );
256
257     p_bridge = var_GetAddress( p_stream, p_sys->psz_name );
258     if ( p_bridge == NULL )
259     {
260         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
261         p_bridge = malloc( sizeof( bridge_t ) );
262
263         var_Create( p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
264         var_SetAddress( p_libvlc, p_sys->psz_name, p_bridge );
265
266         p_bridge->i_es_num = 0;
267         p_bridge->pp_es = NULL;
268     }
269
270     for ( i = 0; i < p_bridge->i_es_num; i++ )
271     {
272         if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
273             break;
274     }
275
276     if ( i == p_bridge->i_es_num )
277     {
278         p_bridge->pp_es = realloc( p_bridge->pp_es,
279                                    (p_bridge->i_es_num + 1)
280                                      * sizeof(bridged_es_t *) );
281         p_bridge->i_es_num++;
282         p_bridge->pp_es[i] = malloc( sizeof(bridged_es_t) );
283     }
284
285     p_sys->p_es = p_es = p_bridge->pp_es[i];
286
287     p_es->fmt = *p_fmt;
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;
292
293     p_es->id = NULL;
294     p_es->i_last = 0;
295     p_es->b_changed = true;
296
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 );
299
300     vlc_mutex_unlock( p_sys->p_lock );
301
302     return (sout_stream_id_t *)p_sys;
303 }
304
305 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
306 {
307     VLC_UNUSED(id);
308     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
309     bridged_es_t *p_es;
310
311     if ( !p_sys->b_inited )
312     {
313         return VLC_SUCCESS;
314     }
315
316     vlc_mutex_lock( p_sys->p_lock );
317
318     p_es = p_sys->p_es;
319
320     p_es->b_empty = true;
321     block_ChainRelease( p_es->p_block );
322     p_es->p_block = false;
323
324     p_es->b_changed = true;
325     vlc_mutex_unlock( p_sys->p_lock );
326
327     p_sys->b_inited = false;
328
329     return VLC_SUCCESS;
330 }
331
332 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
333                     block_t *p_buffer )
334 {
335     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
336     bridged_es_t *p_es;
337
338     if ( (out_sout_stream_sys_t *)id != p_sys )
339     {
340         block_ChainRelease( p_buffer );
341         return VLC_SUCCESS;
342     }
343
344     vlc_mutex_lock( p_sys->p_lock );
345
346     p_es = p_sys->p_es;
347     *p_es->pp_last = p_buffer;
348     while ( p_buffer != NULL )
349     {
350         p_es->pp_last = &p_buffer->p_next;
351         p_buffer = p_buffer->p_next;
352     }
353
354     vlc_mutex_unlock( p_sys->p_lock );
355
356     return VLC_SUCCESS;
357 }
358
359
360 /*
361  * Bridge in
362  */
363
364 typedef struct in_sout_stream_sys_t
365 {
366     sout_stream_t *p_out;
367     vlc_mutex_t *p_lock;
368     int i_id_offset;
369     mtime_t i_delay;
370
371     char *psz_name;
372
373     bool b_placeholder;
374     bool b_switch_on_iframe;
375     int i_state;
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;
382
383 enum { placeholder_on, placeholder_off };
384
385 /*****************************************************************************
386  * OpenIn:
387  *****************************************************************************/
388 static int OpenIn( vlc_object_t *p_this )
389 {
390     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
391     in_sout_stream_sys_t *p_sys;
392     vlc_value_t val;
393
394     p_sys          = malloc( sizeof( in_sout_stream_sys_t ) );
395
396     p_sys->p_out = sout_StreamNew( p_stream->p_sout, p_stream->psz_next );
397     if( !p_sys->p_out )
398     {
399         msg_Err( p_stream, "cannot create chain" );
400         free( p_sys );
401         return VLC_EGENERIC;
402     }
403
404     config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
405                    p_stream->p_cfg );
406
407     var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
408     var_Get( p_this->p_libvlc, "bridge-lock", &val );
409     p_sys->p_lock = val.p_address;
410
411     var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
412     p_sys->i_id_offset = val.i_int;
413
414     var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
415     p_sys->i_delay = (mtime_t)val.i_int * 1000;
416
417     var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
418     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
419     {
420         free( val.psz_string );
421         free( p_sys );
422         return VLC_ENOMEM;
423     }
424     free( val.psz_string );
425
426     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
427     p_sys->b_placeholder = val.b_bool;
428
429     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", &val);
430     p_sys->b_switch_on_iframe = val.b_bool;
431
432     p_sys->i_state = placeholder_on;
433
434     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
435     p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
436
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;
441
442     p_stream->pf_add    = AddIn;
443     p_stream->pf_del    = DelIn;
444     p_stream->pf_send   = SendIn;
445
446     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
447
448     /* update p_sout->i_out_pace_nocontrol */
449     p_stream->p_sout->i_out_pace_nocontrol++;
450
451     return VLC_SUCCESS;
452 }
453
454 /*****************************************************************************
455  * CloseIn:
456  *****************************************************************************/
457 static void CloseIn( vlc_object_t * p_this )
458 {
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;
461
462     sout_StreamDelete( p_sys->p_out );
463     p_stream->p_sout->i_out_pace_nocontrol--;
464
465     free( p_sys->psz_name );
466     free( p_sys );
467 }
468
469 struct sout_stream_id_t
470 {
471     sout_stream_id_t *id;
472     int i_cat; /* es category. Used for placeholder option */
473 };
474
475 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
476 {
477     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
478
479     sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
480     if( !id ) return NULL;
481
482     id->id = p_sys->p_out->pf_add( p_sys->p_out, p_fmt );
483     if( !id->id )
484     {
485         free( id );
486         return NULL;
487     }
488
489     if( p_sys->b_placeholder )
490     {
491         id->i_cat = p_fmt->i_cat;
492         switch( p_fmt->i_cat )
493         {
494             case VIDEO_ES:
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;
498                 break;
499             case AUDIO_ES:
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;
503                 break;
504         }
505     }
506
507     return id;
508 }
509
510 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
511 {
512     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
513
514     if( id == p_sys->id_video ) p_sys->id_video = NULL;
515     if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
516
517     int ret = p_sys->p_out->pf_del( p_sys->p_out, id->id );
518
519     free( id );
520     return ret;
521 }
522
523 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
524                    block_t *p_buffer )
525 {
526     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
527     bridge_t *p_bridge;
528     bool b_no_es = true;
529     int i;
530     int i_date = mdate();
531
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 );
535
536     /* Then check all bridged streams */
537     vlc_mutex_lock( p_sys->p_lock );
538
539     p_bridge = var_GetAddress( p_stream, p_sys->psz_name );
540
541     if( p_bridge )
542     {
543     for ( i = 0; i < p_bridge->i_es_num; i++ )
544     {
545         if ( !p_bridge->pp_es[i]->b_empty )
546             b_no_es = false;
547
548         while ( p_bridge->pp_es[i]->p_block != NULL
549                  && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
550                        < i_date
551                       || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
552                           < p_bridge->pp_es[i]->i_last) )
553         {
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 );
560         }
561
562         if ( p_bridge->pp_es[i]->p_block == NULL )
563         {
564             p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
565         }
566
567         if ( p_bridge->pp_es[i]->b_changed )
568         {
569             if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
570             {
571                 p_sys->p_out->pf_del( p_sys->p_out, p_bridge->pp_es[i]->id );
572             }
573             else
574             {
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 )
578                 {
579                     continue;
580                 }
581
582                 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
583                 if( !p_sys->b_placeholder )
584                 {
585                     p_bridge->pp_es[i]->id = p_sys->p_out->pf_add(
586                                 p_sys->p_out, &p_bridge->pp_es[i]->fmt );
587                     if ( p_bridge->pp_es[i]->id == NULL )
588                     {
589                         msg_Warn( p_stream, "couldn't create chain for id %d",
590                                   p_bridge->pp_es[i]->fmt.i_id );
591                     }
592                 }
593                 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
594                          (char*)&p_bridge->pp_es[i]->fmt.i_codec,
595                          p_bridge->pp_es[i]->fmt.i_id, i );
596             }
597         }
598         p_bridge->pp_es[i]->b_changed = false;
599
600         if ( p_bridge->pp_es[i]->b_empty )
601             continue;
602
603         if ( p_bridge->pp_es[i]->p_block == NULL )
604         {
605             if ( p_bridge->pp_es[i]->id != NULL
606                   && p_bridge->pp_es[i]->i_last < i_date )
607             {
608                 if( !p_sys->b_placeholder )
609                     p_sys->p_out->pf_del( p_sys->p_out,
610                                           p_bridge->pp_es[i]->id );
611                 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
612                 p_bridge->pp_es[i]->b_changed = true;
613                 p_bridge->pp_es[i]->id = NULL;
614             }
615             continue;
616         }
617
618         if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
619         {
620             block_t *p_block = p_bridge->pp_es[i]->p_block;
621             while ( p_block != NULL )
622             {
623                 p_bridge->pp_es[i]->i_last = p_block->i_dts;
624                 p_block->i_pts += p_sys->i_delay;
625                 p_block->i_dts += p_sys->i_delay;
626                 p_block = p_block->p_next;
627             }
628             sout_stream_id_t *newid = NULL;
629             if( p_sys->b_placeholder )
630             {
631                 switch( p_bridge->pp_es[i]->fmt.i_cat )
632                 {
633                     case VIDEO_ES:
634                         p_sys->i_last_video = i_date;
635                         newid = p_sys->id_video;
636                         if( !newid )
637                             break;
638                         if( !p_sys->b_switch_on_iframe ||
639                             p_sys->i_state == placeholder_off ||
640                             ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
641                               p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
642                         {
643                             p_sys->p_out->pf_send( p_sys->p_out,
644                                        newid,
645                                        p_bridge->pp_es[i]->p_block );
646                             p_sys->i_state = placeholder_off;
647                         }
648                         break;
649                     case AUDIO_ES:
650                         newid = p_sys->id_audio;
651                         if( !newid )
652                             break;
653                         p_sys->i_last_audio = i_date;
654                     default:
655                         p_sys->p_out->pf_send( p_sys->p_out,
656                                    newid?newid:p_bridge->pp_es[i]->id,
657                                    p_bridge->pp_es[i]->p_block );
658                         break;
659                 }
660             }
661             else /* !b_placeholder */
662                 p_sys->p_out->pf_send( p_sys->p_out,
663                                        p_bridge->pp_es[i]->id,
664                                        p_bridge->pp_es[i]->p_block );
665         }
666         else
667         {
668             block_ChainRelease( p_bridge->pp_es[i]->p_block );
669         }
670
671         p_bridge->pp_es[i]->p_block = NULL;
672         p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
673     }
674
675     if( b_no_es )
676     {
677         vlc_object_t *p_libvlc = VLC_OBJECT( p_stream->p_libvlc );
678         for ( i = 0; i < p_bridge->i_es_num; i++ )
679             free( p_bridge->pp_es[i] );
680         free( p_bridge->pp_es );
681         free( p_bridge );
682         var_Destroy( p_libvlc, p_sys->psz_name );
683     }
684     }
685
686     if( p_sys->b_placeholder )
687     {
688         switch( id->i_cat )
689         {
690             case VIDEO_ES:
691                 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
692                     && (  !p_sys->b_switch_on_iframe
693                        || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
694                   || p_sys->i_state == placeholder_on )
695                 {
696                     p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
697                     p_sys->i_state = placeholder_on;
698                 }
699                 else
700                     block_Release( p_buffer );
701                 break;
702
703             case AUDIO_ES:
704                 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
705                     p_sys->p_out->pf_send( p_sys->p_out, id->id, p_buffer );
706                 else
707                     block_Release( p_buffer );
708                 break;
709
710             default:
711                 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
712                 break;
713         }
714     }
715
716     vlc_mutex_unlock( p_sys->p_lock );
717
718     return VLC_SUCCESS;
719 }