1 /*****************************************************************************
2 * stream_output.c : stream output module
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: stream_output.c,v 1.8 2003/01/08 14:59:23 sam 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_nb_inputs = 0;
86 p_sout->pp_inputs = NULL;
87 vlc_mutex_init( p_sout, &p_sout->lock );
89 /* Skip the plug-in names */
90 while( *psz_parser && *psz_parser != ':' )
94 #if defined( WIN32 ) || defined( UNDER_CE )
95 if( psz_parser - p_sout->psz_dest == 1 )
97 msg_Warn( p_sout, "drive letter %c: found in source string",
105 p_sout->psz_access = p_sout->psz_mux = "";
106 p_sout->psz_name = p_sout->psz_dest;
110 *psz_parser++ = '\0';
112 /* let's skip '//' */
113 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
118 p_sout->psz_name = psz_parser ;
120 /* Come back to parse the access and mux plug-ins */
121 psz_parser = p_sout->psz_dest;
126 p_sout->psz_access = "";
128 else if( *psz_parser == '/' )
131 p_sout->psz_access = "";
136 p_sout->psz_access = psz_parser;
138 while( *psz_parser && *psz_parser != '/' )
143 if( *psz_parser == '/' )
145 *psz_parser++ = '\0';
152 p_sout->psz_mux = "";
156 p_sout->psz_mux = psz_parser;
160 msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
161 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
164 /* Find and open appropriate access module */
166 module_Need( p_sout, "sout access", p_sout->psz_access );
168 if( p_sout->p_access == NULL )
170 msg_Err( p_sout, "no suitable sout access module for `%s/%s://%s'",
171 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
175 /* Find and open appropriate mux module */
177 module_Need( p_sout, "sout mux", p_sout->psz_mux );
179 if( p_sout->p_mux == NULL )
181 msg_Err( p_sout, "no suitable mux module for `%s/%s://%s'",
182 p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
183 module_Unneed( p_sout, p_sout->p_access );
187 p_sout->i_nb_inputs = 0;
188 p_sout->pp_inputs = NULL;
194 /*****************************************************************************
195 * sout_DeleteInstance: delete a previously allocated instance
196 *****************************************************************************/
197 void sout_DeleteInstance( sout_instance_t * p_sout )
200 vlc_object_detach( p_sout );
203 module_Unneed( p_sout, p_sout->p_mux );
205 if( p_sout->p_access )
207 module_Unneed( p_sout, p_sout->p_access );
210 vlc_mutex_destroy( &p_sout->lock );
213 vlc_object_destroy( p_sout );
217 /*****************************************************************************
219 *****************************************************************************/
220 sout_input_t *__sout_InputNew( vlc_object_t *p_this,
221 sout_packet_format_t *p_format )
223 sout_instance_t *p_sout = NULL;
224 sout_input_t *p_input;
227 /* search an stream output */
228 for( i_try = 0; i_try < 200; i_try++ )
230 p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
234 msg_Dbg( p_this, "waiting for sout" );
244 msg_Err( p_this, "cannot find any stream ouput" );
248 msg_Dbg( p_sout, "adding a new input" );
250 /* create a new sout input */
251 p_input = malloc( sizeof( sout_input_t ) );
253 p_input->p_sout = p_sout;
254 vlc_mutex_init( p_sout, &p_input->lock );
255 memcpy( &p_input->input_format,
257 sizeof( sout_packet_format_t ) );
258 p_input->p_fifo = sout_FifoCreate( p_sout );
259 p_input->p_mux_data = NULL;
261 /* add this new one to p_sout */
262 vlc_mutex_lock( &p_sout->lock );
263 if( p_sout->i_nb_inputs == 0 )
265 p_sout->pp_inputs = malloc( sizeof( sout_input_t * ) );
269 p_sout->pp_inputs = realloc( p_sout->pp_inputs,
270 sizeof( sout_input_t * ) *
271 ( p_sout->i_nb_inputs + 1 ) );
273 p_sout->pp_inputs[p_sout->i_nb_inputs] = p_input;
274 p_sout->i_nb_inputs++;
276 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) &&
277 p_sout->pf_mux_addstream( p_sout, p_input ) < 0 )
279 /* vlc_mutex_unlock( &p_sout->lock ); */
280 msg_Err( p_sout, "cannot add this stream" );
283 vlc_mutex_unlock( &p_sout->lock );
285 vlc_object_release( p_sout );
291 int sout_InputDelete( sout_input_t *p_input )
293 sout_instance_t *p_sout = p_input->p_sout;
297 vlc_mutex_lock( &p_sout->lock );
298 for( i_input = 0; i_input < p_sout->i_nb_inputs; i_input++ )
300 if( p_sout->pp_inputs[i_input] == p_input )
306 if( i_input >= p_sout->i_nb_inputs )
308 msg_Err( p_sout, "cannot find input to delete" );
312 msg_Dbg( p_sout, "removing an input" );
314 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) &&
315 p_sout->pf_mux_delstream( p_sout, p_input ) < 0 )
317 msg_Err( p_sout, "cannot del this stream" );
321 /* remove the entry */
322 if( p_sout->i_nb_inputs > 1 )
324 memmove( &p_sout->pp_inputs[i_input],
325 &p_sout->pp_inputs[i_input+1],
326 (p_sout->i_nb_inputs - i_input - 1) * sizeof( sout_input_t*) );
330 free( p_sout->pp_inputs );
332 p_sout->i_nb_inputs--;
334 sout_FifoDestroy( p_sout, p_input->p_fifo );
335 vlc_mutex_destroy( &p_input->lock );
338 if( p_sout->i_nb_inputs == 0 )
340 msg_Warn( p_sout, "no more input stream" );
342 vlc_mutex_unlock( &p_sout->lock );
348 int sout_InputSendBuffer( sout_input_t *p_input, sout_buffer_t *p_buffer )
350 /* msg_Dbg( p_input->p_sout,
351 "send buffer, size:%d", p_buffer->i_size ); */
353 if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
355 sout_FifoPut( p_input->p_fifo, p_buffer );
357 vlc_mutex_lock( &p_input->p_sout->lock );
358 p_input->p_sout->pf_mux( p_input->p_sout );
359 vlc_mutex_unlock( &p_input->p_sout->lock );
364 sout_BufferDelete( p_input->p_sout, p_buffer );
370 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
374 if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
379 vlc_mutex_init( p_sout, &p_fifo->lock );
380 vlc_cond_init ( p_sout, &p_fifo->wait );
382 p_fifo->p_first = NULL;
383 p_fifo->pp_last = &p_fifo->p_first;
388 void sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
390 sout_buffer_t *p_buffer;
392 vlc_mutex_lock( &p_fifo->lock );
393 p_buffer = p_fifo->p_first;
396 sout_buffer_t *p_next;
397 p_next = p_buffer->p_next;
398 sout_BufferDelete( p_sout, p_buffer );
401 vlc_mutex_unlock( &p_fifo->lock );
405 void sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
407 sout_FifoFree( p_sout, p_fifo );
408 vlc_mutex_destroy( &p_fifo->lock );
409 vlc_cond_destroy ( &p_fifo->wait );
414 void sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
416 vlc_mutex_lock( &p_fifo->lock );
420 *p_fifo->pp_last = p_buffer;
421 p_fifo->pp_last = &p_buffer->p_next;
424 p_buffer = p_buffer->p_next;
428 /* warm there is data in this fifo */
429 vlc_cond_signal( &p_fifo->wait );
430 vlc_mutex_unlock( &p_fifo->lock );
433 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
435 sout_buffer_t *p_buffer;
437 vlc_mutex_lock( &p_fifo->lock );
439 if( p_fifo->p_first == NULL )
441 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
444 p_buffer = p_fifo->p_first;
446 p_fifo->p_first = p_buffer->p_next;
449 if( p_fifo->p_first == NULL )
451 p_fifo->pp_last = &p_fifo->p_first;
454 vlc_mutex_unlock( &p_fifo->lock );
456 p_buffer->p_next = NULL;
460 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
462 sout_buffer_t *p_buffer;
464 vlc_mutex_lock( &p_fifo->lock );
466 if( p_fifo->p_first == NULL )
468 vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
471 p_buffer = p_fifo->p_first;
473 vlc_mutex_unlock( &p_fifo->lock );
478 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
480 sout_buffer_t *p_buffer;
483 msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
486 p_buffer = malloc( sizeof( sout_buffer_t ) );
489 p_buffer->p_buffer = malloc( i_size );
493 p_buffer->p_buffer = NULL;
495 p_buffer->i_allocated_size = i_size;
496 p_buffer->i_size = i_size;
497 p_buffer->p_next = NULL;
501 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
505 "realloc buffer old size:%d new size:%d",
506 (uint32_t)p_buffer->i_allocated_size,
509 if( !( p_buffer->p_buffer = realloc( p_buffer->p_buffer, i_size ) ) )
511 msg_Err( p_sout, "realloc failed" );
512 p_buffer->i_allocated_size = 0;
513 p_buffer->i_size = 0;
518 p_buffer->i_allocated_size = i_size;
522 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
525 msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
527 if( p_buffer->p_buffer )
529 free( p_buffer->p_buffer );
535 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
536 sout_buffer_t *p_buffer )
538 sout_buffer_t *p_dup;
540 p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
542 p_dup->i_bitrate= p_buffer->i_bitrate;
543 p_dup->i_dts = p_buffer->i_dts;
544 p_dup->i_pts = p_buffer->i_pts;
545 p_dup->i_length = p_buffer->i_length;
546 memcpy( p_dup->p_buffer, p_buffer->p_buffer, p_buffer->i_size );
551 void sout_BufferChain( sout_buffer_t **pp_chain,
552 sout_buffer_t *p_buffer )
554 if( *pp_chain == NULL )
556 *pp_chain = p_buffer;
560 sout_buffer_t *p = *pp_chain;
567 p->p_next = p_buffer;