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