]> git.sesse.net Git - vlc/blob - modules/stream_out/bridge.c
macosx: remove debug
[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 placeholder" )
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, ID_TEXT, ID_LONGTEXT,
102                  false )
103     add_string( SOUT_CFG_PREFIX_OUT "in-name", "default",
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, DELAY_TEXT,
114                  DELAY_LONGTEXT, false )
115     add_integer( SOUT_CFG_PREFIX_IN "id-offset", 8192, ID_OFFSET_TEXT,
116                  ID_OFFSET_LONGTEXT, false )
117     add_string( SOUT_CFG_PREFIX_IN "name", "default",
118                 NAME_TEXT, NAME_LONGTEXT, false )
119     add_bool( SOUT_CFG_PREFIX_IN "placeholder", false,
120               PLACEHOLDER_TEXT, PLACEHOLDER_LONGTEXT, false )
121     add_integer( SOUT_CFG_PREFIX_IN "placeholder-delay", 200,
122                  PLACEHOLDER_DELAY_TEXT, PLACEHOLDER_DELAY_LONGTEXT, false )
123     add_bool( SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", true,
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 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
171
172 /*
173  * Bridge out
174  */
175
176 typedef struct out_sout_stream_sys_t
177 {
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_Get( p_stream, SOUT_CFG_PREFIX_OUT "id", &val );
203     p_sys->i_id = val.i_int;
204
205     var_Get( p_stream, SOUT_CFG_PREFIX_OUT "in-name", &val );
206     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
207     {
208         free( val.psz_string );
209         free( p_sys );
210         return VLC_ENOMEM;
211     }
212     free( val.psz_string );
213
214     p_stream->pf_add    = AddOut;
215     p_stream->pf_del    = DelOut;
216     p_stream->pf_send   = SendOut;
217
218     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
219
220     p_stream->p_sout->i_out_pace_nocontrol++;
221
222     return VLC_SUCCESS;
223 }
224
225 /*****************************************************************************
226  * CloseOut:
227  *****************************************************************************/
228 static void CloseOut( vlc_object_t * p_this )
229 {
230     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
231     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
232
233     p_stream->p_sout->i_out_pace_nocontrol--;
234
235     free( p_sys->psz_name );
236     free( p_sys );
237 }
238
239 static sout_stream_id_t * AddOut( sout_stream_t *p_stream, es_format_t *p_fmt )
240 {
241     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
242     bridge_t *p_bridge;
243     bridged_es_t *p_es;
244     int i;
245
246     if ( p_sys->b_inited )
247     {
248         msg_Err( p_stream, "bridge-out can only handle 1 es at a time." );
249         return NULL;
250     }
251     p_sys->b_inited = true;
252
253     vlc_mutex_lock( &lock );
254
255     p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
256     if ( p_bridge == NULL )
257     {
258         p_bridge = xmalloc( sizeof( bridge_t ) );
259
260         var_Create( p_stream->p_libvlc, p_sys->psz_name, VLC_VAR_ADDRESS );
261         var_SetAddress( p_stream->p_libvlc, p_sys->psz_name, p_bridge );
262
263         p_bridge->i_es_num = 0;
264         p_bridge->pp_es = NULL;
265     }
266
267     for ( i = 0; i < p_bridge->i_es_num; i++ )
268     {
269         if ( p_bridge->pp_es[i]->b_empty && !p_bridge->pp_es[i]->b_changed )
270             break;
271     }
272
273     if ( i == p_bridge->i_es_num )
274     {
275         p_bridge->pp_es = xrealloc( p_bridge->pp_es,
276                           (p_bridge->i_es_num + 1) * sizeof(bridged_es_t *) );
277         p_bridge->i_es_num++;
278         p_bridge->pp_es[i] = xmalloc( sizeof(bridged_es_t) );
279     }
280
281     p_sys->p_es = p_es = p_bridge->pp_es[i];
282
283     p_es->fmt = *p_fmt;
284     p_es->fmt.i_id = p_sys->i_id;
285     p_es->p_block = NULL;
286     p_es->pp_last = &p_es->p_block;
287     p_es->b_empty = false;
288
289     p_es->id = NULL;
290     p_es->i_last = VLC_TS_INVALID;
291     p_es->b_changed = true;
292
293     msg_Dbg( p_stream, "bridging out input codec=%4.4s id=%d pos=%d",
294              (char*)&p_es->fmt.i_codec, p_es->fmt.i_id, i );
295
296     vlc_mutex_unlock( &lock );
297
298     return (sout_stream_id_t *)p_sys;
299 }
300
301 static int DelOut( sout_stream_t *p_stream, sout_stream_id_t *id )
302 {
303     VLC_UNUSED(id);
304     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
305     bridged_es_t *p_es;
306
307     if ( !p_sys->b_inited )
308     {
309         return VLC_SUCCESS;
310     }
311
312     vlc_mutex_lock( &lock );
313
314     p_es = p_sys->p_es;
315
316     p_es->b_empty = true;
317     block_ChainRelease( p_es->p_block );
318     p_es->p_block = false;
319
320     p_es->b_changed = true;
321     vlc_mutex_unlock( &lock );
322
323     p_sys->b_inited = false;
324
325     return VLC_SUCCESS;
326 }
327
328 static int SendOut( sout_stream_t *p_stream, sout_stream_id_t *id,
329                     block_t *p_buffer )
330 {
331     out_sout_stream_sys_t *p_sys = (out_sout_stream_sys_t *)p_stream->p_sys;
332     bridged_es_t *p_es;
333
334     if ( (out_sout_stream_sys_t *)id != p_sys )
335     {
336         block_ChainRelease( p_buffer );
337         return VLC_SUCCESS;
338     }
339
340     vlc_mutex_lock( &lock );
341
342     p_es = p_sys->p_es;
343     *p_es->pp_last = p_buffer;
344     while ( p_buffer != NULL )
345     {
346         p_es->pp_last = &p_buffer->p_next;
347         p_buffer = p_buffer->p_next;
348     }
349
350     vlc_mutex_unlock( &lock );
351
352     return VLC_SUCCESS;
353 }
354
355
356 /*
357  * Bridge in
358  */
359
360 typedef struct in_sout_stream_sys_t
361 {
362     int i_id_offset;
363     mtime_t i_delay;
364
365     char *psz_name;
366
367     bool b_placeholder;
368     bool b_switch_on_iframe;
369     int i_state;
370     mtime_t i_placeholder_delay;
371     sout_stream_id_t *id_video;
372     mtime_t i_last_video;
373     sout_stream_id_t *id_audio;
374     mtime_t i_last_audio;
375 } in_sout_stream_sys_t;
376
377 enum { placeholder_on, placeholder_off };
378
379 /*****************************************************************************
380  * OpenIn:
381  *****************************************************************************/
382 static int OpenIn( vlc_object_t *p_this )
383 {
384     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
385     in_sout_stream_sys_t *p_sys;
386     vlc_value_t val;
387
388     p_sys          = malloc( sizeof( in_sout_stream_sys_t ) );
389     if( unlikely( !p_sys ) )
390         return VLC_ENOMEM;
391
392     if( !p_stream->p_next )
393     {
394         msg_Err( p_stream, "cannot create chain" );
395         free( p_sys );
396         return VLC_EGENERIC;
397     }
398
399     config_ChainParse( p_stream, SOUT_CFG_PREFIX_IN, ppsz_sout_options_in,
400                    p_stream->p_cfg );
401
402     var_Get( p_stream, SOUT_CFG_PREFIX_IN "id-offset", &val );
403     p_sys->i_id_offset = val.i_int;
404
405     var_Get( p_stream, SOUT_CFG_PREFIX_IN "delay", &val );
406     p_sys->i_delay = (mtime_t)val.i_int * 1000;
407
408     var_Get( p_stream, SOUT_CFG_PREFIX_IN "name", &val );
409     if( asprintf( &p_sys->psz_name, "bridge-struct-%s", val.psz_string )<0 )
410     {
411         free( val.psz_string );
412         free( p_sys );
413         return VLC_ENOMEM;
414     }
415     free( val.psz_string );
416
417     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder", &val );
418     p_sys->b_placeholder = val.b_bool;
419
420     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-switch-on-iframe", &val);
421     p_sys->b_switch_on_iframe = val.b_bool;
422
423     p_sys->i_state = placeholder_on;
424
425     var_Get( p_stream, SOUT_CFG_PREFIX_IN "placeholder-delay", &val );
426     p_sys->i_placeholder_delay = (mtime_t)val.i_int * 1000;
427
428     p_sys->i_last_video = VLC_TS_INVALID;
429     p_sys->i_last_audio = VLC_TS_INVALID;
430     p_sys->id_video = NULL;
431     p_sys->id_audio = NULL;
432
433     p_stream->pf_add    = AddIn;
434     p_stream->pf_del    = DelIn;
435     p_stream->pf_send   = SendIn;
436
437     p_stream->p_sys     = (sout_stream_sys_t *)p_sys;
438
439     /* update p_sout->i_out_pace_nocontrol */
440     p_stream->p_sout->i_out_pace_nocontrol++;
441
442     return VLC_SUCCESS;
443 }
444
445 /*****************************************************************************
446  * CloseIn:
447  *****************************************************************************/
448 static void CloseIn( vlc_object_t * p_this )
449 {
450     sout_stream_t     *p_stream = (sout_stream_t*)p_this;
451     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
452
453     p_stream->p_sout->i_out_pace_nocontrol--;
454
455     free( p_sys->psz_name );
456     free( p_sys );
457 }
458
459 struct sout_stream_id_t
460 {
461     sout_stream_id_t *id;
462     int i_cat; /* es category. Used for placeholder option */
463 };
464
465 static sout_stream_id_t * AddIn( sout_stream_t *p_stream, es_format_t *p_fmt )
466 {
467     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
468
469     sout_stream_id_t *id = malloc( sizeof( sout_stream_id_t ) );
470     if( !id ) return NULL;
471
472     id->id = p_stream->p_next->pf_add( p_stream->p_next, p_fmt );
473     if( !id->id )
474     {
475         free( id );
476         return NULL;
477     }
478
479     if( p_sys->b_placeholder )
480     {
481         id->i_cat = p_fmt->i_cat;
482         switch( p_fmt->i_cat )
483         {
484             case VIDEO_ES:
485                 if( p_sys->id_video != NULL )
486                     msg_Err( p_stream, "We already had a video es!" );
487                 p_sys->id_video = id->id;
488                 break;
489             case AUDIO_ES:
490                 if( p_sys->id_audio != NULL )
491                     msg_Err( p_stream, "We already had an audio es!" );
492                 p_sys->id_audio = id->id;
493                 break;
494         }
495     }
496
497     return id;
498 }
499
500 static int DelIn( sout_stream_t *p_stream, sout_stream_id_t *id )
501 {
502     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
503
504     if( id == p_sys->id_video ) p_sys->id_video = NULL;
505     if( id == p_sys->id_audio ) p_sys->id_audio = NULL;
506
507     int ret = p_stream->p_next->pf_del( p_stream->p_next, id->id );
508
509     free( id );
510     return ret;
511 }
512
513 static int SendIn( sout_stream_t *p_stream, sout_stream_id_t *id,
514                    block_t *p_buffer )
515 {
516     in_sout_stream_sys_t *p_sys = (in_sout_stream_sys_t *)p_stream->p_sys;
517     bridge_t *p_bridge;
518     bool b_no_es = true;
519     int i;
520     int i_date = mdate();
521
522     /* First forward the packet for our own ES */
523     if( !p_sys->b_placeholder )
524         p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
525
526     /* Then check all bridged streams */
527     vlc_mutex_lock( &lock );
528
529     p_bridge = var_GetAddress( p_stream->p_libvlc, p_sys->psz_name );
530
531     if( p_bridge )
532     {
533     for ( i = 0; i < p_bridge->i_es_num; i++ )
534     {
535         if ( !p_bridge->pp_es[i]->b_empty )
536             b_no_es = false;
537
538         while ( p_bridge->pp_es[i]->p_block != NULL
539                  && (p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
540                        < i_date
541                       || p_bridge->pp_es[i]->p_block->i_dts + p_sys->i_delay
542                           < p_bridge->pp_es[i]->i_last) )
543         {
544             block_t *p_block = p_bridge->pp_es[i]->p_block;
545             msg_Dbg( p_stream, "dropping a packet (%"PRId64 ")",
546                      i_date - p_block->i_dts - p_sys->i_delay );
547             p_bridge->pp_es[i]->p_block
548                 = p_bridge->pp_es[i]->p_block->p_next;
549             block_Release( p_block );
550         }
551
552         if ( p_bridge->pp_es[i]->p_block == NULL )
553         {
554             p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
555         }
556
557         if ( p_bridge->pp_es[i]->b_changed )
558         {
559             if ( p_bridge->pp_es[i]->b_empty && p_bridge->pp_es[i]->id != NULL )
560             {
561                 p_stream->p_next->pf_del( p_stream->p_next, p_bridge->pp_es[i]->id );
562             }
563             else
564             {
565                 /* We need at least two packets to enter the mux. */
566                 if ( p_bridge->pp_es[i]->p_block == NULL
567                       || p_bridge->pp_es[i]->p_block->p_next == NULL )
568                 {
569                     continue;
570                 }
571
572                 p_bridge->pp_es[i]->fmt.i_id += p_sys->i_id_offset;
573                 if( !p_sys->b_placeholder )
574                 {
575                     p_bridge->pp_es[i]->id = p_stream->p_next->pf_add(
576                                 p_stream->p_next, &p_bridge->pp_es[i]->fmt );
577                     if ( p_bridge->pp_es[i]->id == NULL )
578                     {
579                         msg_Warn( p_stream, "couldn't create chain for id %d",
580                                   p_bridge->pp_es[i]->fmt.i_id );
581                     }
582                 }
583                 msg_Dbg( p_stream, "bridging in input codec=%4.4s id=%d pos=%d",
584                          (char*)&p_bridge->pp_es[i]->fmt.i_codec,
585                          p_bridge->pp_es[i]->fmt.i_id, i );
586             }
587         }
588         p_bridge->pp_es[i]->b_changed = false;
589
590         if ( p_bridge->pp_es[i]->b_empty )
591             continue;
592
593         if ( p_bridge->pp_es[i]->p_block == NULL )
594         {
595             if ( p_bridge->pp_es[i]->id != NULL
596                   && p_bridge->pp_es[i]->i_last < i_date )
597             {
598                 if( !p_sys->b_placeholder )
599                     p_stream->p_next->pf_del( p_stream->p_next,
600                                           p_bridge->pp_es[i]->id );
601                 p_bridge->pp_es[i]->fmt.i_id -= p_sys->i_id_offset;
602                 p_bridge->pp_es[i]->b_changed = true;
603                 p_bridge->pp_es[i]->id = NULL;
604             }
605             continue;
606         }
607
608         if ( p_bridge->pp_es[i]->id != NULL || p_sys->b_placeholder)
609         {
610             block_t *p_block = p_bridge->pp_es[i]->p_block;
611             while ( p_block != NULL )
612             {
613                 p_bridge->pp_es[i]->i_last = p_block->i_dts;
614                 p_block->i_pts += p_sys->i_delay;
615                 p_block->i_dts += p_sys->i_delay;
616                 p_block = p_block->p_next;
617             }
618             sout_stream_id_t *newid = NULL;
619             if( p_sys->b_placeholder )
620             {
621                 switch( p_bridge->pp_es[i]->fmt.i_cat )
622                 {
623                     case VIDEO_ES:
624                         p_sys->i_last_video = i_date;
625                         newid = p_sys->id_video;
626                         if( !newid )
627                             break;
628                         if( !p_sys->b_switch_on_iframe ||
629                             p_sys->i_state == placeholder_off ||
630                             ( p_bridge->pp_es[i]->fmt.i_cat == VIDEO_ES &&
631                               p_bridge->pp_es[i]->p_block->i_flags & BLOCK_FLAG_TYPE_I ) )
632                         {
633                             p_stream->p_next->pf_send( p_stream->p_next,
634                                        newid,
635                                        p_bridge->pp_es[i]->p_block );
636                             p_sys->i_state = placeholder_off;
637                         }
638                         break;
639                     case AUDIO_ES:
640                         newid = p_sys->id_audio;
641                         if( !newid )
642                             break;
643                         p_sys->i_last_audio = i_date;
644                     default:
645                         p_stream->p_next->pf_send( p_stream->p_next,
646                                    newid?newid:p_bridge->pp_es[i]->id,
647                                    p_bridge->pp_es[i]->p_block );
648                         break;
649                 }
650             }
651             else /* !b_placeholder */
652                 p_stream->p_next->pf_send( p_stream->p_next,
653                                        p_bridge->pp_es[i]->id,
654                                        p_bridge->pp_es[i]->p_block );
655         }
656         else
657         {
658             block_ChainRelease( p_bridge->pp_es[i]->p_block );
659         }
660
661         p_bridge->pp_es[i]->p_block = NULL;
662         p_bridge->pp_es[i]->pp_last = &p_bridge->pp_es[i]->p_block;
663     }
664
665     if( b_no_es )
666     {
667         for ( i = 0; i < p_bridge->i_es_num; i++ )
668             free( p_bridge->pp_es[i] );
669         free( p_bridge->pp_es );
670         free( p_bridge );
671         var_Destroy( p_stream->p_libvlc, p_sys->psz_name );
672     }
673     }
674
675     if( p_sys->b_placeholder )
676     {
677         switch( id->i_cat )
678         {
679             case VIDEO_ES:
680                 if( ( p_sys->i_last_video + p_sys->i_placeholder_delay < i_date
681                     && (  !p_sys->b_switch_on_iframe
682                        || p_buffer->i_flags & BLOCK_FLAG_TYPE_I ) )
683                   || p_sys->i_state == placeholder_on )
684                 {
685                     p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
686                     p_sys->i_state = placeholder_on;
687                 }
688                 else
689                     block_Release( p_buffer );
690                 break;
691
692             case AUDIO_ES:
693                 if( p_sys->i_last_audio + p_sys->i_placeholder_delay < i_date )
694                     p_stream->p_next->pf_send( p_stream->p_next, id->id, p_buffer );
695                 else
696                     block_Release( p_buffer );
697                 break;
698
699             default:
700                 block_Release( p_buffer ); /* FIXME: placeholder subs anyone? */
701                 break;
702         }
703     }
704
705     vlc_mutex_unlock( &lock );
706
707     return VLC_SUCCESS;
708 }