1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: stream_output.c,v 1.12 2003/01/17 15:26:24 fenrir Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Erioc Petit <titer@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <stdio.h> /* sprintf() */
31 #include <string.h> /* strerror() */
37 /*****************************************************************************
39 *****************************************************************************/
40 static int InitInstance ( sout_instance_t * );
42 /*****************************************************************************
43 * sout_NewInstance: creates a new stream output instance
44 *****************************************************************************/
45 sout_instance_t * __sout_NewInstance ( vlc_object_t *p_parent,
48 sout_instance_t * p_sout;
50 /* Allocate descriptor */
51 p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
54 msg_Err( p_parent, "out of memory" );
58 p_sout->psz_dest = strdup( psz_dest );
60 if ( InitInstance( p_sout ) == -1 )
62 vlc_object_destroy( p_sout );
66 vlc_object_attach( p_sout, p_parent );
71 /*****************************************************************************
72 * InitInstance: opens appropriate modules
73 *****************************************************************************/
74 static int InitInstance( sout_instance_t * p_sout )
76 /* Parse dest string. Syntax : [[<access>][/<mux>]:][<dest>] */
77 /* This code is identical to input.c:InitThread. FIXME : factorize it ? */
78 char * psz_parser = p_sout->psz_dest;
80 p_sout->psz_access = "";
82 p_sout->psz_name = "";
83 p_sout->p_access = NULL;
85 p_sout->i_access_preheader = 0;
86 p_sout->i_mux_preheader = 0;
87 p_sout->i_nb_inputs = 0;
88 p_sout->pp_inputs = NULL;
89 vlc_mutex_init( p_sout, &p_sout->lock );
91 /* Skip the plug-in names */
92 while( *psz_parser && *psz_parser != ':' )
96 #if defined( WIN32 ) || defined( UNDER_CE )
97 if( psz_parser - p_sout->psz_dest == 1 )
99 msg_Warn( p_sout, "drive letter %c: found in source string",
100 *p_sout->psz_dest ) ;
107 p_sout->psz_access = p_sout->psz_mux = "";
108 p_sout->psz_name = p_sout->psz_dest;
112 *psz_parser++ = '\0';
114 /* let's skip '//' */
115 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
120 p_sout->psz_name = psz_parser ;
122 /* Come back to parse the access and mux plug-ins */
123 psz_parser = p_sout->psz_dest;
128 p_sout->psz_access = "";
130 else if( *psz_parser == '/' )
133 p_sout->psz_access = "";
138 p_sout->psz_access = psz_parser;
140 while( *psz_parser && *psz_parser != '/' )
145 if( *psz_parser == '/' )
147 *psz_parser++ = '\0';
154 p_sout->psz_mux = "";
158 p_sout->psz_mux = psz_parser;
162 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
163 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
166 /* Find and open appropriate access module */
168 module_Need( p_sout, "sout access", p_sout->psz_access );
170 if( p_sout->p_access == NULL )
172 msg_Err( p_sout, "no suitable sout access module for `%s/%s://%s'",
173 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
177 /* Find and open appropriate mux module */
179 module_Need( p_sout, "sout mux", p_sout->psz_mux );
181 if( p_sout->p_mux == NULL )
183 msg_Err( p_sout, "no suitable mux module for `%s/%s://%s'",
184 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
185 module_Unneed( p_sout, p_sout->p_access );
189 p_sout->i_nb_inputs = 0;
190 p_sout->pp_inputs = NULL;
196 /*****************************************************************************
197 * sout_DeleteInstance: delete a previously allocated instance
198 *****************************************************************************/
199 void sout_DeleteInstance( sout_instance_t * p_sout )
202 vlc_object_detach( p_sout );
205 module_Unneed( p_sout, p_sout->p_mux );
207 if( p_sout->p_access )
209 module_Unneed( p_sout, p_sout->p_access );
212 vlc_mutex_destroy( &p_sout->lock );
215 vlc_object_destroy( p_sout );
219 /*****************************************************************************
221 *****************************************************************************/
222 sout_input_t *__sout_InputNew( vlc_object_t *p_this,
223 sout_packet_format_t *p_format )
225 sout_instance_t *p_sout = NULL;
226 sout_input_t *p_input;
229 /* search an stream output */
230 for( i_try = 0; i_try < 200; i_try++ )
232 p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
236 msg_Dbg( p_this, "waiting for sout" );
246 msg_Err( p_this, "cannot find any stream ouput" );
250 msg_Dbg( p_sout, "adding a new input" );
252 /* create a new sout input */
253 p_input = malloc( sizeof( sout_input_t ) );
255 p_input->p_sout = p_sout;
256 vlc_mutex_init( p_sout, &p_input->lock );
257 memcpy( &p_input->input_format,
259 sizeof( sout_packet_format_t ) );
260 p_input->p_fifo = sout_FifoCreate( p_sout );
261 p_input->p_mux_data = NULL;
263 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
265 /* add this new one to p_sout */
266 vlc_mutex_lock( &p_sout->lock );
267 if( p_sout->i_nb_inputs == 0 )
269 p_sout->pp_inputs = malloc( sizeof( sout_input_t * ) );
273 p_sout->pp_inputs = realloc( p_sout->pp_inputs,
274 sizeof( sout_input_t * ) *
275 ( p_sout->i_nb_inputs + 1 ) );
277 p_sout->pp_inputs[p_sout->i_nb_inputs] = p_input;
278 p_sout->i_nb_inputs++;
280 if( p_sout->pf_mux_addstream( p_sout, p_input ) < 0 )
282 msg_Err( p_sout, "cannot add this stream" );
284 vlc_mutex_unlock( &p_sout->lock );
285 sout_InputDelete( p_input );
286 vlc_mutex_lock( &p_sout->lock );
290 vlc_mutex_unlock( &p_sout->lock );
293 vlc_object_release( p_sout );
299 int sout_InputDelete( sout_input_t *p_input )
301 sout_instance_t *p_sout = p_input->p_sout;
305 msg_Dbg( p_sout, "removing an input" );
307 vlc_mutex_lock( &p_sout->lock );
309 sout_FifoDestroy( p_sout, p_input->p_fifo );
310 vlc_mutex_destroy( &p_input->lock );
312 for( i_input = 0; i_input < p_sout->i_nb_inputs; i_input++ )
314 if( p_sout->pp_inputs[i_input] == p_input )
319 if( i_input < p_sout->i_nb_inputs )
321 if( p_sout->pf_mux_delstream( p_sout, p_input ) < 0 )
323 msg_Err( p_sout, "cannot del this stream from mux" );
326 /* remove the entry */
327 if( p_sout->i_nb_inputs > 1 )
329 memmove( &p_sout->pp_inputs[i_input],
330 &p_sout->pp_inputs[i_input+1],
331 (p_sout->i_nb_inputs - i_input - 1) * sizeof( sout_input_t*) );
335 free( p_sout->pp_inputs );
337 p_sout->i_nb_inputs--;
339 if( p_sout->i_nb_inputs == 0 )
341 msg_Warn( p_sout, "no more input stream" );
344 else if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
346 msg_Err( p_sout, "cannot find the input to be deleted" );
351 vlc_mutex_unlock( &p_sout->lock );
357 int sout_InputSendBuffer( sout_input_t *p_input, sout_buffer_t *p_buffer )
359 /* msg_Dbg( p_input->p_sout,
360 "send buffer, size:%d", p_buffer->i_size ); */
362 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
364 sout_FifoPut( p_input->p_fifo, p_buffer );
366 vlc_mutex_lock( &p_input->p_sout->lock );
367 p_input->p_sout->pf_mux( p_input->p_sout );
368 vlc_mutex_unlock( &p_input->p_sout->lock );
373 sout_BufferDelete( p_input->p_sout, p_buffer );
379 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
383 if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
388 vlc_mutex_init( p_sout, &p_fifo->lock );
389 vlc_cond_init ( p_sout, &p_fifo->wait );
391 p_fifo->p_first = NULL;
392 p_fifo->pp_last = &p_fifo->p_first;
397 void sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
399 sout_buffer_t *p_buffer;
401 vlc_mutex_lock( &p_fifo->lock );
402 p_buffer = p_fifo->p_first;
405 sout_buffer_t *p_next;
406 p_next = p_buffer->p_next;
407 sout_BufferDelete( p_sout, p_buffer );
410 vlc_mutex_unlock( &p_fifo->lock );
414 void sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
416 sout_FifoFree( p_sout, p_fifo );
417 vlc_mutex_destroy( &p_fifo->lock );
418 vlc_cond_destroy ( &p_fifo->wait );
423 void sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
425 vlc_mutex_lock( &p_fifo->lock );
429 *p_fifo->pp_last = p_buffer;
430 p_fifo->pp_last = &p_buffer->p_next;
433 p_buffer = p_buffer->p_next;
437 /* warm there is data in this fifo */
438 vlc_cond_signal( &p_fifo->wait );
439 vlc_mutex_unlock( &p_fifo->lock );
442 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
444 sout_buffer_t *p_buffer;
446 vlc_mutex_lock( &p_fifo->lock );
448 if( p_fifo->p_first == NULL )
450 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
453 p_buffer = p_fifo->p_first;
455 p_fifo->p_first = p_buffer->p_next;
458 if( p_fifo->p_first == NULL )
460 p_fifo->pp_last = &p_fifo->p_first;
463 vlc_mutex_unlock( &p_fifo->lock );
465 p_buffer->p_next = NULL;
469 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
471 sout_buffer_t *p_buffer;
473 vlc_mutex_lock( &p_fifo->lock );
475 if( p_fifo->p_first == NULL )
477 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
480 p_buffer = p_fifo->p_first;
482 vlc_mutex_unlock( &p_fifo->lock );
487 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
489 sout_buffer_t *p_buffer;
493 msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
496 p_buffer = malloc( sizeof( sout_buffer_t ) );
497 i_prehader = p_sout->i_access_preheader + p_sout->i_mux_preheader;
501 p_buffer->p_allocated_buffer = malloc( i_size + i_prehader );
502 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
506 p_buffer->p_allocated_buffer = NULL;
507 p_buffer->p_buffer = NULL;
509 p_buffer->i_allocated_size = i_size + i_prehader;
510 p_buffer->i_buffer_size = i_size;
512 p_buffer->i_size = i_size;
513 p_buffer->i_length = 0;
516 p_buffer->i_bitrate = 0;
517 p_buffer->p_next = NULL;
521 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
527 "realloc buffer old size:%d new size:%d",
528 (uint32_t)p_buffer->i_allocated_size,
532 i_prehader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
534 if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_prehader ) ) )
536 msg_Err( p_sout, "realloc failed" );
537 p_buffer->i_allocated_size = 0;
538 p_buffer->i_buffer_size = 0;
539 p_buffer->i_size = 0;
540 p_buffer->p_buffer = NULL;
543 p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
545 p_buffer->i_allocated_size = i_size + i_prehader;
546 p_buffer->i_buffer_size = i_size;
551 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
555 i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
557 if( i_preheader < i_size )
562 p_buffer->p_buffer -= i_size;
563 p_buffer->i_size += i_size;
564 p_buffer->i_buffer_size += i_size;
569 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
572 msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
574 if( p_buffer->p_allocated_buffer )
576 free( p_buffer->p_allocated_buffer );
582 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
583 sout_buffer_t *p_buffer )
585 sout_buffer_t *p_dup;
587 p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
589 p_dup->i_bitrate= p_buffer->i_bitrate;
590 p_dup->i_dts = p_buffer->i_dts;
591 p_dup->i_pts = p_buffer->i_pts;
592 p_dup->i_length = p_buffer->i_length;
593 p_sout->p_vlc->pf_memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
598 void sout_BufferChain( sout_buffer_t **pp_chain,
599 sout_buffer_t *p_buffer )
601 if( *pp_chain == NULL )
603 *pp_chain = p_buffer;
607 sout_buffer_t *p = *pp_chain;
614 p->p_next = p_buffer;