]> git.sesse.net Git - vlc/blob - src/stream_output/stream_output.c
* all: use p_vlc->pf_memcpy instead of memcpy on big data block.
[vlc] / src / stream_output / stream_output.c
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 $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *          Erioc Petit <titer@videolan.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                                /* free() */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <string.h>                                            /* strerror() */
32
33 #include <vlc/vlc.h>
34
35 #include <vlc/sout.h>
36 #undef DEBUG_BUFFER
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int      InitInstance      ( sout_instance_t * );
41
42 /*****************************************************************************
43  * sout_NewInstance: creates a new stream output instance
44  *****************************************************************************/
45 sout_instance_t * __sout_NewInstance ( vlc_object_t *p_parent,
46                                        char * psz_dest )
47 {
48     sout_instance_t * p_sout;
49
50     /* Allocate descriptor */
51     p_sout = vlc_object_create( p_parent, VLC_OBJECT_SOUT );
52     if( p_sout == NULL )
53     {
54         msg_Err( p_parent, "out of memory" );
55         return NULL;
56     }
57
58     p_sout->psz_dest = strdup( psz_dest );
59
60     if ( InitInstance( p_sout ) == -1 )
61     {
62         vlc_object_destroy( p_sout );
63         return NULL;
64     }
65
66     vlc_object_attach( p_sout, p_parent );
67
68     return p_sout;
69 }
70
71 /*****************************************************************************
72  * InitInstance: opens appropriate modules
73  *****************************************************************************/
74 static int InitInstance( sout_instance_t * p_sout )
75 {
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;
79
80     p_sout->psz_access = "";
81     p_sout->psz_mux = "";
82     p_sout->psz_name = "";
83     p_sout->p_access = NULL;
84     p_sout->p_mux = 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 );
90
91     /* Skip the plug-in names */
92     while( *psz_parser && *psz_parser != ':' )
93     {
94         psz_parser++;
95     }
96 #if defined( WIN32 ) || defined( UNDER_CE )
97     if( psz_parser - p_sout->psz_dest == 1 )
98     {
99         msg_Warn( p_sout, "drive letter %c: found in source string",
100                           *p_sout->psz_dest ) ;
101         psz_parser = "";
102     }
103 #endif
104
105     if( !*psz_parser )
106     {
107         p_sout->psz_access = p_sout->psz_mux = "";
108         p_sout->psz_name = p_sout->psz_dest;
109     }
110     else
111     {
112         *psz_parser++ = '\0';
113
114         /* let's skip '//' */
115         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
116         {
117             psz_parser += 2 ;
118         } 
119
120         p_sout->psz_name = psz_parser ;
121
122         /* Come back to parse the access and mux plug-ins */
123         psz_parser = p_sout->psz_dest;
124
125         if( !*psz_parser )
126         {
127             /* No access */
128             p_sout->psz_access = "";
129         }
130         else if( *psz_parser == '/' )
131         {
132             /* No access */
133             p_sout->psz_access = "";
134             psz_parser++;
135         }
136         else
137         {
138             p_sout->psz_access = psz_parser;
139
140             while( *psz_parser && *psz_parser != '/' )
141             {
142                 psz_parser++;
143             }
144
145             if( *psz_parser == '/' )
146             {
147                 *psz_parser++ = '\0';
148             }
149         }
150
151         if( !*psz_parser )
152         {
153             /* No mux */
154             p_sout->psz_mux = "";
155         }
156         else
157         {
158             p_sout->psz_mux = psz_parser;
159         }
160     }
161
162     msg_Dbg( p_sout, "access `%s', mux `%s', name `%s'",
163              p_sout->psz_access, p_sout->psz_mux, p_sout->psz_name );
164
165
166     /* Find and open appropriate access module */
167     p_sout->p_access =
168         module_Need( p_sout, "sout access", p_sout->psz_access );
169
170     if( p_sout->p_access == NULL )
171     {
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 );
174         return -1;
175     }
176
177     /* Find and open appropriate mux module */
178     p_sout->p_mux =
179         module_Need( p_sout, "sout mux", p_sout->psz_mux );
180
181     if( p_sout->p_mux == NULL )
182     {
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 );
186         return -1;
187     }
188
189     p_sout->i_nb_inputs = 0;
190     p_sout->pp_inputs = NULL;
191
192     return 0;
193 }
194
195
196 /*****************************************************************************
197  * sout_DeleteInstance: delete a previously allocated instance
198  *****************************************************************************/
199 void sout_DeleteInstance( sout_instance_t * p_sout )
200 {
201     /* Unlink object */
202     vlc_object_detach( p_sout );
203     if( p_sout->p_mux )
204     {
205         module_Unneed( p_sout, p_sout->p_mux );
206     }
207     if( p_sout->p_access )
208     {
209         module_Unneed( p_sout, p_sout->p_access );
210     }
211
212     vlc_mutex_destroy( &p_sout->lock );
213
214     /* Free structure */
215     vlc_object_destroy( p_sout );
216 }
217
218
219 /*****************************************************************************
220  *
221  *****************************************************************************/
222 sout_input_t *__sout_InputNew( vlc_object_t *p_this,
223                                 sout_packet_format_t *p_format )
224 {
225     sout_instance_t *p_sout = NULL;
226     sout_input_t    *p_input;
227     int             i_try;
228
229     /* search an stream output */
230     for( i_try = 0; i_try < 200; i_try++ )
231     {
232         p_sout = vlc_object_find( p_this, VLC_OBJECT_SOUT, FIND_ANYWHERE );
233         if( !p_sout )
234         {
235             msleep( 100*1000 );
236             msg_Dbg( p_this, "waiting for sout" );
237         }
238         else
239         {
240             break;
241         }
242     }
243
244     if( !p_sout )
245     {
246         msg_Err( p_this, "cannot find any stream ouput" );
247         return( NULL );
248     }
249
250     msg_Dbg( p_sout, "adding a new input" );
251
252     /* create a new sout input */
253     p_input = malloc( sizeof( sout_input_t ) );
254
255     p_input->p_sout = p_sout;
256     vlc_mutex_init( p_sout, &p_input->lock );
257     memcpy( &p_input->input_format,
258             p_format,
259             sizeof( sout_packet_format_t ) );
260     p_input->p_fifo = sout_FifoCreate( p_sout );
261     p_input->p_mux_data = NULL;
262
263     if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
264     {
265         /* add this new one to p_sout */
266         vlc_mutex_lock( &p_sout->lock );
267         if( p_sout->i_nb_inputs == 0 )
268         {
269             p_sout->pp_inputs = malloc( sizeof( sout_input_t * ) );
270         }
271         else
272         {
273             p_sout->pp_inputs = realloc( p_sout->pp_inputs,
274                                         sizeof( sout_input_t * ) *
275                                                 ( p_sout->i_nb_inputs + 1 ) );
276         }
277         p_sout->pp_inputs[p_sout->i_nb_inputs] = p_input;
278         p_sout->i_nb_inputs++;
279
280         if( p_sout->pf_mux_addstream( p_sout, p_input ) < 0 )
281         {
282             msg_Err( p_sout, "cannot add this stream" );
283
284             vlc_mutex_unlock( &p_sout->lock );
285             sout_InputDelete( p_input );
286             vlc_mutex_lock( &p_sout->lock );
287
288             p_input = NULL;
289         }
290         vlc_mutex_unlock( &p_sout->lock );
291     }
292
293     vlc_object_release( p_sout );
294
295     return( p_input );
296 }
297
298
299 int sout_InputDelete( sout_input_t *p_input )
300 {
301     sout_instance_t     *p_sout = p_input->p_sout;
302     int                 i_input;
303
304
305     msg_Dbg( p_sout, "removing an input" );
306
307     vlc_mutex_lock( &p_sout->lock );
308
309     sout_FifoDestroy( p_sout, p_input->p_fifo );
310     vlc_mutex_destroy( &p_input->lock );
311
312     for( i_input = 0; i_input < p_sout->i_nb_inputs; i_input++ )
313     {
314         if( p_sout->pp_inputs[i_input] == p_input )
315         {
316             break;
317         }
318     }
319     if( i_input < p_sout->i_nb_inputs )
320     {
321         if( p_sout->pf_mux_delstream( p_sout, p_input ) < 0 )
322         {
323             msg_Err( p_sout, "cannot del this stream from mux" );
324         }
325
326         /* remove the entry */
327         if( p_sout->i_nb_inputs > 1 )
328         {
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*) );
332         }
333         else
334         {
335             free( p_sout->pp_inputs );
336         }
337         p_sout->i_nb_inputs--;
338
339         if( p_sout->i_nb_inputs == 0 )
340         {
341             msg_Warn( p_sout, "no more input stream" );
342         }
343     }
344     else if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
345     {
346         msg_Err( p_sout, "cannot find the input to be deleted" );
347     }
348
349     free( p_input );
350
351     vlc_mutex_unlock( &p_sout->lock );
352
353     return( 0 );
354 }
355
356
357 int sout_InputSendBuffer( sout_input_t *p_input, sout_buffer_t *p_buffer )
358 {
359 /*    msg_Dbg( p_input->p_sout,
360              "send buffer, size:%d", p_buffer->i_size ); */
361
362     if( p_input->input_format.i_fourcc != VLC_FOURCC( 'n', 'u', 'l', 'l' ) )
363     {
364         sout_FifoPut( p_input->p_fifo, p_buffer );
365
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 );
369
370     }
371     else
372     {
373         sout_BufferDelete( p_input->p_sout, p_buffer );
374     }
375
376     return( 0 );
377 }
378
379 sout_fifo_t *sout_FifoCreate( sout_instance_t *p_sout )
380 {
381     sout_fifo_t *p_fifo;
382
383     if( !( p_fifo = malloc( sizeof( sout_fifo_t ) ) ) )
384     {
385         return( NULL );
386     }
387
388     vlc_mutex_init( p_sout, &p_fifo->lock );
389     vlc_cond_init ( p_sout, &p_fifo->wait );
390     p_fifo->i_depth = 0;
391     p_fifo->p_first = NULL;
392     p_fifo->pp_last = &p_fifo->p_first;
393
394     return( p_fifo );
395 }
396
397 void       sout_FifoFree( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
398 {
399     sout_buffer_t *p_buffer;
400
401     vlc_mutex_lock( &p_fifo->lock );
402     p_buffer = p_fifo->p_first;
403     while( p_buffer )
404     {
405         sout_buffer_t *p_next;
406         p_next = p_buffer->p_next;
407         sout_BufferDelete( p_sout, p_buffer );
408         p_buffer = p_next;
409     }
410     vlc_mutex_unlock( &p_fifo->lock );
411
412     return;
413 }
414 void       sout_FifoDestroy( sout_instance_t *p_sout, sout_fifo_t *p_fifo )
415 {
416     sout_FifoFree( p_sout, p_fifo );
417     vlc_mutex_destroy( &p_fifo->lock );
418     vlc_cond_destroy ( &p_fifo->wait );
419
420     free( p_fifo );
421 }
422
423 void        sout_FifoPut( sout_fifo_t *p_fifo, sout_buffer_t *p_buffer )
424 {
425     vlc_mutex_lock( &p_fifo->lock );
426
427     do
428     {
429         *p_fifo->pp_last = p_buffer;
430         p_fifo->pp_last = &p_buffer->p_next;
431         p_fifo->i_depth++;
432
433         p_buffer = p_buffer->p_next;
434
435     } while( p_buffer );
436
437     /* warm there is data in this fifo */
438     vlc_cond_signal( &p_fifo->wait );
439     vlc_mutex_unlock( &p_fifo->lock );
440 }
441
442 sout_buffer_t *sout_FifoGet( sout_fifo_t *p_fifo )
443 {
444     sout_buffer_t *p_buffer;
445
446     vlc_mutex_lock( &p_fifo->lock );
447
448     if( p_fifo->p_first == NULL )
449     {
450         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
451     }
452
453     p_buffer = p_fifo->p_first;
454
455     p_fifo->p_first = p_buffer->p_next;
456     p_fifo->i_depth--;
457
458     if( p_fifo->p_first == NULL )
459     {
460         p_fifo->pp_last = &p_fifo->p_first;
461     }
462
463     vlc_mutex_unlock( &p_fifo->lock );
464
465     p_buffer->p_next = NULL;
466     return( p_buffer );
467 }
468
469 sout_buffer_t *sout_FifoShow( sout_fifo_t *p_fifo )
470 {
471     sout_buffer_t *p_buffer;
472
473     vlc_mutex_lock( &p_fifo->lock );
474
475     if( p_fifo->p_first == NULL )
476     {
477         vlc_cond_wait( &p_fifo->wait, &p_fifo->lock );
478     }
479
480     p_buffer = p_fifo->p_first;
481
482     vlc_mutex_unlock( &p_fifo->lock );
483
484     return( p_buffer );
485 }
486
487 sout_buffer_t *sout_BufferNew( sout_instance_t *p_sout, size_t i_size )
488 {
489     sout_buffer_t *p_buffer;
490     size_t        i_prehader;
491
492 #ifdef DEBUG_BUFFER
493     msg_Dbg( p_sout, "allocating an new buffer, size:%d", (uint32_t)i_size );
494 #endif
495
496     p_buffer = malloc( sizeof( sout_buffer_t ) );
497     i_prehader = p_sout->i_access_preheader + p_sout->i_mux_preheader;
498
499     if( i_size > 0 )
500     {
501         p_buffer->p_allocated_buffer = malloc( i_size + i_prehader );
502         p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
503     }
504     else
505     {
506         p_buffer->p_allocated_buffer = NULL;
507         p_buffer->p_buffer = NULL;
508     }
509     p_buffer->i_allocated_size = i_size + i_prehader;
510     p_buffer->i_buffer_size = i_size;
511
512     p_buffer->i_size = i_size;
513     p_buffer->i_length = 0;
514     p_buffer->i_dts = 0;
515     p_buffer->i_pts = 0;
516     p_buffer->i_bitrate = 0;
517     p_buffer->p_next = NULL;
518
519     return( p_buffer );
520 }
521 int sout_BufferRealloc( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
522 {
523     size_t          i_prehader;
524
525 #ifdef DEBUG_BUFFER
526     msg_Dbg( p_sout,
527              "realloc buffer old size:%d new size:%d",
528              (uint32_t)p_buffer->i_allocated_size,
529              (uint32_t)i_size );
530 #endif
531
532     i_prehader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
533
534     if( !( p_buffer->p_allocated_buffer = realloc( p_buffer->p_allocated_buffer, i_size + i_prehader ) ) )
535     {
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;
541         return( -1 );
542     }
543     p_buffer->p_buffer = p_buffer->p_allocated_buffer + i_prehader;
544
545     p_buffer->i_allocated_size = i_size + i_prehader;
546     p_buffer->i_buffer_size = i_size;
547
548     return( 0 );
549 }
550
551 int sout_BufferReallocFromPreHeader( sout_instance_t *p_sout, sout_buffer_t *p_buffer, size_t i_size )
552 {
553     size_t  i_preheader;
554
555     i_preheader = p_buffer->p_buffer - p_buffer->p_allocated_buffer;
556
557     if( i_preheader < i_size )
558     {
559         return( -1 );
560     }
561
562     p_buffer->p_buffer -= i_size;
563     p_buffer->i_size += i_size;
564     p_buffer->i_buffer_size += i_size;
565
566     return( 0 );
567 }
568
569 int sout_BufferDelete( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
570 {
571 #ifdef DEBUG_BUFFER
572     msg_Dbg( p_sout, "freeing buffer, size:%d", p_buffer->i_size );
573 #endif
574     if( p_buffer->p_allocated_buffer )
575     {
576         free( p_buffer->p_allocated_buffer );
577     }
578     free( p_buffer );
579     return( 0 );
580 }
581
582 sout_buffer_t *sout_BufferDuplicate( sout_instance_t *p_sout,
583                                      sout_buffer_t *p_buffer )
584 {
585     sout_buffer_t *p_dup;
586
587     p_dup = sout_BufferNew( p_sout, p_buffer->i_size );
588
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 );
594
595     return( p_dup );
596 }
597
598 void sout_BufferChain( sout_buffer_t **pp_chain,
599                        sout_buffer_t *p_buffer )
600 {
601     if( *pp_chain == NULL )
602     {
603         *pp_chain = p_buffer;
604     }
605     else
606     {
607         sout_buffer_t *p = *pp_chain;
608
609         while( p->p_next )
610         {
611             p = p->p_next;
612         }
613
614         p->p_next = p_buffer;
615     }
616 }