]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
playlist: Handle vlc_InputItemErrorWhenReadingChanged events.
[vlc] / modules / audio_output / file.c
1 /*****************************************************************************
2  * file.c : audio output which writes the samples to a file
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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 <errno.h>
34
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_aout.h>
38 #include <vlc_codecs.h> /* WAVEHEADER */
39 #include <vlc_charset.h>
40
41 #define FRAME_SIZE 2048
42 #define A52_FRAME_NB 1536
43
44 /*****************************************************************************
45  * aout_sys_t: audio output method descriptor
46  *****************************************************************************
47  * This structure is part of the audio output thread descriptor.
48  * It describes the direct sound specific properties of an audio device.
49  *****************************************************************************/
50 struct aout_sys_t
51 {
52     FILE     * p_file;
53     bool b_add_wav_header;
54
55     WAVEHEADER waveh;                      /* Wave header of the output file */
56 };
57
58 #define CHANNELS_MAX 6
59 static int pi_channels_maps[CHANNELS_MAX+1] =
60 {
61     0,
62     AOUT_CHAN_CENTER,
63     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
64     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
65     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
66      | AOUT_CHAN_REARRIGHT,
67     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
68      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
69     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
70      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
71 };
72
73 /*****************************************************************************
74  * Local prototypes.
75  *****************************************************************************/
76 static int     Open        ( vlc_object_t * );
77 static void    Close       ( vlc_object_t * );
78 static void    Play        ( aout_instance_t * );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 #define FORMAT_TEXT N_("Output format")
84 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
85     "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
86     "\"float32\" or \"spdif\"")
87 #define CHANNELS_TEXT N_("Number of output channels")
88 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
89     "will be saved but you can restrict the number of channels here.")
90
91 #define WAV_TEXT N_("Add WAVE header")
92 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
93                         "header to the file.")
94
95 static const char *const format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
96                                      "s16_le", "u16_be", "s16_be", "fixed32",
97                                      "float32", "spdif" };
98 static const int format_int[] = { VLC_FOURCC('u','8',' ',' '),
99                                   VLC_FOURCC('s','8',' ',' '),
100                                   AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
101                                   VLC_FOURCC('u','1','6','l'),
102                                   VLC_FOURCC('s','1','6','l'),
103                                   VLC_FOURCC('u','1','6','b'),
104                                   VLC_FOURCC('s','1','6','b'),
105                                   VLC_FOURCC('f','i','3','2'),
106                                   VLC_FOURCC('f','l','3','2'),
107                                   VLC_FOURCC('s','p','i','f') };
108
109 #define FILE_TEXT N_("Output file")
110 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
111
112 vlc_module_begin();
113     set_description( N_("File audio output") );
114     set_shortname( N_("File") );
115     set_category( CAT_AUDIO );
116     set_subcategory( SUBCAT_AUDIO_AOUT );
117
118     add_string( "audiofile-format", "s16", NULL,
119                 FORMAT_TEXT, FORMAT_LONGTEXT, true );
120         change_string_list( format_list, 0, 0 );
121     add_integer( "audiofile-channels", 0, NULL,
122                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true );
123     add_file( "audiofile-file", "audiofile.wav", NULL, FILE_TEXT,
124               FILE_LONGTEXT, false );
125         change_unsafe();
126     add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, true );
127
128     set_capability( "audio output", 0 );
129     add_shortcut( "file" );
130     add_shortcut( "audiofile" );
131     set_callbacks( Open, Close );
132 vlc_module_end();
133
134 /*****************************************************************************
135  * Open: open a dummy audio device
136  *****************************************************************************/
137 static int Open( vlc_object_t * p_this )
138 {
139     aout_instance_t * p_aout = (aout_instance_t *)p_this;
140     char * psz_name, * psz_format;
141     const char ** ppsz_compare = format_list;
142     vlc_value_t val;
143     int i_channels, i = 0;
144
145     var_Create( p_this, "audiofile-file", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
146     var_Get( p_this, "audiofile-file", &val );
147     psz_name = val.psz_string;
148     if( !psz_name || !*psz_name )
149     {
150         msg_Err( p_aout, "you need to specify an output file name" );
151         free( psz_name );
152         return VLC_EGENERIC;
153     }
154
155     /* Allocate structure */
156     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
157     if( p_aout->output.p_sys == NULL )
158         return VLC_ENOMEM;
159
160     if( !strcmp( psz_name, "-" ) )
161         p_aout->output.p_sys->p_file = stdout;
162     else
163         p_aout->output.p_sys->p_file = utf8_fopen( psz_name, "wb" );
164
165     free( psz_name );
166     if ( p_aout->output.p_sys->p_file == NULL )
167     {
168         free( p_aout->output.p_sys );
169         return VLC_EGENERIC;
170     }
171
172     p_aout->output.pf_play = Play;
173
174     /* Audio format */
175     var_Create( p_this, "audiofile-format", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
176     var_Get( p_this, "audiofile-format", &val );
177     psz_format = val.psz_string;
178
179     while ( *ppsz_compare != NULL )
180     {
181         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
182         {
183             break;
184         }
185         ppsz_compare++; i++;
186     }
187
188     if ( *ppsz_compare == NULL )
189     {
190         msg_Err( p_aout, "cannot understand the format string (%s)",
191                  psz_format );
192         if( p_aout->output.p_sys->p_file != stdout )
193             fclose( p_aout->output.p_sys->p_file );
194         free( p_aout->output.p_sys );
195         return VLC_EGENERIC;
196     }
197
198     p_aout->output.output.i_format = format_int[i];
199     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
200     {
201         p_aout->output.i_nb_samples = A52_FRAME_NB;
202         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
203         p_aout->output.output.i_frame_length = A52_FRAME_NB;
204         aout_VolumeNoneInit( p_aout );
205     }
206     else
207     {
208         p_aout->output.i_nb_samples = FRAME_SIZE;
209         aout_VolumeSoftInit( p_aout );
210     }
211
212     /* Channels number */
213     var_Create( p_this, "audiofile-channels",
214                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
215     var_Get( p_this, "audiofile-channels", &val );
216     i_channels = val.i_int;
217
218     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
219     {
220         p_aout->output.output.i_physical_channels =
221             pi_channels_maps[i_channels];
222     }
223
224     /* WAV header */
225     var_Create( p_this, "audiofile-wav", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
226     var_Get( p_this, "audiofile-wav", &val );
227     p_aout->output.p_sys->b_add_wav_header = val.b_bool;
228
229     if( p_aout->output.p_sys->b_add_wav_header )
230     {
231         /* Write wave header */
232         WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
233
234         memset( wh, 0, sizeof(wh) );
235
236         switch( p_aout->output.output.i_format )
237         {
238         case VLC_FOURCC('f','l','3','2'):
239             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
240             wh->BitsPerSample = sizeof(float) * 8;
241             break;
242         case VLC_FOURCC('u','8',' ',' '):
243             wh->Format     = WAVE_FORMAT_PCM;
244             wh->BitsPerSample = 8;
245             break;
246         case VLC_FOURCC('s','1','6','l'):
247         default:
248             wh->Format     = WAVE_FORMAT_PCM;
249             wh->BitsPerSample = 16;
250             break;
251         }
252
253         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
254         wh->Length = 0;                    /* temp, to be filled in as we go */
255         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
256         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
257         wh->SubChunkLength = 16;
258
259         wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
260         wh->SampleFreq = p_aout->output.output.i_rate;
261         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
262         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
263
264         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
265         wh->DataLength = 0;                /* temp, to be filled in as we go */
266
267         /* Header -> little endian format */
268         SetWLE( &wh->Format, wh->Format );
269         SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
270         SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
271         SetWLE( &wh->Modus, wh->Modus );
272         SetDWLE( &wh->SampleFreq, wh->SampleFreq );
273         SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
274         SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
275
276         if( fwrite( wh, sizeof(WAVEHEADER), 1,
277                     p_aout->output.p_sys->p_file ) != 1 )
278         {
279             msg_Err( p_aout, "write error (%m)" );
280         }
281     }
282
283     return 0;
284 }
285
286 /*****************************************************************************
287  * Close: close our file
288  *****************************************************************************/
289 static void Close( vlc_object_t * p_this )
290 {
291     aout_instance_t * p_aout = (aout_instance_t *)p_this;
292
293     msg_Dbg( p_aout, "closing audio file" );
294
295     if( p_aout->output.p_sys->b_add_wav_header )
296     {
297         /* Update Wave Header */
298         p_aout->output.p_sys->waveh.Length =
299             p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
300
301         /* Write Wave Header */
302         if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
303         {
304             msg_Err( p_aout, "seek error (%m)" );
305         }
306
307         /* Header -> little endian format */
308         SetDWLE( &p_aout->output.p_sys->waveh.Length,
309                  p_aout->output.p_sys->waveh.Length );
310         SetDWLE( &p_aout->output.p_sys->waveh.DataLength,
311                  p_aout->output.p_sys->waveh.DataLength );
312
313         if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
314                     p_aout->output.p_sys->p_file ) != 1 )
315         {
316             msg_Err( p_aout, "write error (%m)" );
317         }
318     }
319
320     if( p_aout->output.p_sys->p_file != stdout )
321         fclose( p_aout->output.p_sys->p_file );
322     free( p_aout->output.p_sys );
323 }
324
325 /*****************************************************************************
326  * Play: pretend to play a sound
327  *****************************************************************************/
328 static void Play( aout_instance_t * p_aout )
329 {
330     aout_buffer_t * p_buffer;
331
332     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
333
334     if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
335                 p_aout->output.p_sys->p_file ) != 1 )
336     {
337         msg_Err( p_aout, "write error (%m)" );
338     }
339
340     if( p_aout->output.p_sys->b_add_wav_header )
341     {
342         /* Update Wave Header */
343         p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
344     }
345
346     aout_BufferFree( p_buffer );
347 }