]> git.sesse.net Git - vlc/blob - modules/stream_out/bridge.c
Use var_Inherit* instead of var_CreateGet*.
[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     if( unlikely( !p_sys ) )
199         return VLC_ENOMEM;
200     p_sys->b_inited = false;
201
202     var_Create( p_this->p_libvlc, "bridge-lock", VLC_VAR_MUTEX );
203     var_Get( p_this->p_libvlc, "bridge-lock", &val );
204     p_sys->p_lock = val.p_address;
205
206     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
207     p_sys->i_id = val.i_int;
208
209     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
210     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
211     {
212         free( val.psz_string );
213         free( p_sys );
214         return VLC_ENOMEM;
215     }
216     free( val.psz_string );
217
218     p_stream->pf_add    = AddOut;
219     p_stream->pf_del    = DelOut;
220     p_stream->pf_send   = SendOut;
221
222     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
223
224     p_stream->p_sout->i_out_pace_nocontrol++;
225
226     return VLC_SUCCESS;
227 }
228
229 /*****************************************************************************
230  * CloseOut:
231  *****************************************************************************/
232 static void CloseOut( vlc_object_t * p_this )
233 {
234     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
235     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
236
237     p_stream->p_sout->i_out_pace_nocontrol--;
238
239     free( p_sys->psz_name );
240     free( p_sys );
241 }
242
243 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
244 {
245     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
246     bridge_t *p_bridge;
247     bridged_es_t *p_es;
248     int i;
249
250     if ( p_sys->b_inited )
251     {
252         msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
253         return NULL;
254     }
255     p_sys->b_inited = true;
256
257     vlc_mutex_lock( p_sys->p_lock );
258
259     p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
260     if ( p_bridge == NULL )
261     {
262         p_bridge = xmalloc( sizeof( bridge_t ) );
263
264         var_Create( p_stream->p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
265         var_SetAddress( p_stream->p_libvlc, p_sys->psz_name, p_bridge );
266
267         p_bridge->i_es_num = 0;
268         p_bridge->pp_es = NULL;
269     }
270
271     for ( i = 0; i < p_bridge->i_es_num; i++ )
272     {
273         if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
274             break;
275     }
276
277     if ( i == p_bridge->i_es_num )
278     {
279         p_bridge->pp_es = xrealloc( p_bridge->pp_es,
280                           (p_bridge->i_es_num + 1) * sizeof(bridged_es_t *) );
281         p_bridge->i_es_num++;
282         p_bridge->pp_es[i] = xmalloc( 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 = VLC_TS_INVALID;
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     vlc_mutex_t *p_lock;
367     int i_id_offset;
368     mtime_t i_delay;
369
370     char *psz_name;
371
372     bool b_placeholder;
373     bool b_switch_on_iframe;
374     int i_state;
375     mtime_t i_placeholder_delay;
376     sout_stream_id_t *id_video;
377     mtime_t i_last_video;
378     sout_stream_id_t *id_audio;
379     mtime_t i_last_audio;
380 } in_sout_stream_sys_t;
381
382 enum { placeholder_on, placeholder_off };
383
384 /*****************************************************************************
385  * OpenIn:
386  *****************************************************************************/
387 static int OpenIn( vlc_object_t *p_this )
388 {
389     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
390     in_sout_stream_sys_t *p_sys;
391     vlc_value_t val;
392
393     p_sys          = malloc( sizeof( in_sout_stream_sys_t ) );
394     if( unlikely( !p_sys ) )
395         return VLC_ENOMEM;
396
397     if( !p_stream->p_next )
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 = VLC_TS_INVALID;
438     p_sys->i_last_audio = VLC_TS_INVALID;
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     p_stream->p_sout->i_out_pace_nocontrol--;
463
464     free( p_sys->psz_name );
465     free( p_sys );
466 }
467
468 struct sout_stream_id_t
469 {
470     sout_stream_id_t *id;
471     int i_cat; /* es category. Used for placeholder option */
472 };
473
474 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
475 {
476     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
477
478     sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
479     if( !id ) return NULL;
480
481     id->id = p_stream->p_next->pf_add( p_stream->p_next, p_fmt );
482     if( !id->id )
483     {
484         free( id );
485         return NULL;
486     }
487
488     if( p_sys->b_placeholder )
489     {
490         id->i_cat = p_fmt->i_cat;
491         switch( p_fmt->i_cat )
492         {
493             case VIDEO_ES:
494                 if( p_sys->id_video != NULL )
495                     msg_Err( p_stream, "We already had a video es!" );
496                 p_sys->id_video = id->id;
497                 break;
498             case AUDIO_ES:
499                 if( p_sys->id_audio != NULL )
500                     msg_Err( p_stream, "We already had an audio es!" );
501                 p_sys->id_audio = id->id;
502                 break;
503         }
504     }
505
506     return id;
507 }
508
509 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
510 {
511     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
512
513     if( id == p_sys->id_video ) p_sys->id_video = NULL;
514     if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
515
516     int ret = p_stream->p_next->pf_del( p_stream->p_next, id->id );
517
518     free( id );
519     return ret;
520 }
521
522 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
523                    block_t *p_buffer )
524 {
525     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
526     bridge_t *p_bridge;
527     bool b_no_es = true;
528     int i;
529     int i_date = mdate();
530
531     /* First forward the packet for our own ES */
532     if( !p_sys->b_placeholder )
533         p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
534
535     /* Then check all bridged streams */
536     vlc_mutex_lock( p_sys->p_lock );
537
538     p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
539
540     if( p_bridge )
541     {
542     for ( i = 0; i < p_bridge->i_es_num; i++ )
543     {
544         if ( !p_bridge->pp_es[i]->b_empty )
545             b_no_es = false;
546
547         while ( p_bridge->pp_es[i]->p_block != NULL
548                  && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
549                        < i_date
550                       || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
551                           < p_bridge->pp_es[i]->i_last) )
552         {
553             block_t *p_block = p_bridge->pp_es[i]->p_block;
554             msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
555                      i_date - p_block->i_dts - p_sys->i_delay );
556             p_bridge->pp_es[i]->p_block
557                 = p_bridge->pp_es[i]->p_block->p_next;
558             block_Release( p_block );
559         }
560
561         if ( p_bridge->pp_es[i]->p_block == NULL )
562         {
563             p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
564         }
565
566         if ( p_bridge->pp_es[i]->b_changed )
567         {
568             if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
569             {
570                 p_stream->p_next->pf_del( p_stream->p_next, p_bridge->pp_es[i]->id );
571             }
572             else
573             {
574                 /* We need at least two packets to enter the mux. */
575                 if ( p_bridge->pp_es[i]->p_block == NULL
576                       || p_bridge->pp_es[i]->p_block->p_next == NULL )
577                 {
578                     continue;
579                 }
580
581                 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
582                 if( !p_sys->b_placeholder )
583                 {
584                     p_bridge->pp_es[i]->id = p_stream->p_next->pf_add(
585                                 p_stream->p_next, &p_bridge->pp_es[i]->fmt );
586                     if ( p_bridge->pp_es[i]->id == NULL )
587                     {
588                         msg_Warn( p_stream, "couldn't create chain for id %d",
589                                   p_bridge->pp_es[i]->fmt.i_id );
590                     }
591                 }
592                 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
593                          (char*)&p_bridge->pp_es[i]->fmt.i_codec,
594                          p_bridge->pp_es[i]->fmt.i_id, i );
595             }
596         }
597         p_bridge->pp_es[i]->b_changed = false;
598
599         if ( p_bridge->pp_es[i]->b_empty )
600             continue;
601
602         if ( p_bridge->pp_es[i]->p_block == NULL )
603         {
604             if ( p_bridge->pp_es[i]->id != NULL
605                   && p_bridge->pp_es[i]->i_last < i_date )
606             {
607                 if( !p_sys->b_placeholder )
608                     p_stream->p_next->pf_del( p_stream->p_next,
609                                           p_bridge->pp_es[i]->id );
610                 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
611                 p_bridge->pp_es[i]->b_changed = true;
612                 p_bridge->pp_es[i]->id = NULL;
613             }
614             continue;
615         }
616
617         if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
618         {
619             block_t *p_block = p_bridge->pp_es[i]->p_block;
620             while ( p_block != NULL )
621             {
622                 p_bridge->pp_es[i]->i_last = p_block->i_dts;
623                 p_block->i_pts += p_sys->i_delay;
624                 p_block->i_dts += p_sys->i_delay;
625                 p_block = p_block->p_next;
626             }
627             sout_stream_id_t *newid = NULL;
628             if( p_sys->b_placeholder )
629             {
630                 switch( p_bridge->pp_es[i]->fmt.i_cat )
631                 {
632                     case VIDEO_ES:
633                         p_sys->i_last_video = i_date;
634                         newid = p_sys->id_video;
635                         if( !newid )
636                             break;
637                         if( !p_sys->b_switch_on_iframe ||
638                             p_sys->i_state == placeholder_off ||
639                             ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
640                               p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
641                         {
642                             p_stream->p_next->pf_send( p_stream->p_next,
643                                        newid,
644                                        p_bridge->pp_es[i]->p_block );
645                             p_sys->i_state = placeholder_off;
646                         }
647                         break;
648                     case AUDIO_ES:
649                         newid = p_sys->id_audio;
650                         if( !newid )
651                             break;
652                         p_sys->i_last_audio = i_date;
653                     default:
654                         p_stream->p_next->pf_send( p_stream->p_next,
655                                    newid?newid:p_bridge->pp_es[i]->id,
656                                    p_bridge->pp_es[i]->p_block );
657                         break;
658                 }
659             }
660             else /* !b_placeholder */
661                 p_stream->p_next->pf_send( p_stream->p_next,
662                                        p_bridge->pp_es[i]->id,
663                                        p_bridge->pp_es[i]->p_block );
664         }
665         else
666         {
667             block_ChainRelease( p_bridge->pp_es[i]->p_block );
668         }
669
670         p_bridge->pp_es[i]->p_block = NULL;
671         p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
672     }
673
674     if( b_no_es )
675     {
676         for ( i = 0; i < p_bridge->i_es_num; i++ )
677             free( p_bridge->pp_es[i] );
678         free( p_bridge->pp_es );
679         free( p_bridge );
680         var_Destroy( p_stream->p_libvlc, p_sys->psz_name );
681     }
682     }
683
684     if( p_sys->b_placeholder )
685     {
686         switch( id->i_cat )
687         {
688             case VIDEO_ES:
689                 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
690                     && (  !p_sys->b_switch_on_iframe
691                        || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
692                   || p_sys->i_state == placeholder_on )
693                 {
694                     p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
695                     p_sys->i_state = placeholder_on;
696                 }
697                 else
698                     block_Release( p_buffer );
699                 break;
700
701             case AUDIO_ES:
702                 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
703                     p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
704                 else
705                     block_Release( p_buffer );
706                 break;
707
708             default:
709                 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
710                 break;
711         }
712     }
713
714     vlc_mutex_unlock( p_sys->p_lock );
715
716     return VLC_SUCCESS;
717 }