]> git.sesse.net Git - vlc/blob - modules/audio_output/sdl.c
2b0d569373aa811a0981018044ffb8a5a51d969c
[vlc] / modules / audio_output / sdl.c
1 /*****************************************************************************
2  * sdl.c : SDL audio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 VideoLAN
5  * $Id: sdl.c,v 1.8 2002/08/30 22:22:24 massiot Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Pierre Baillet <oct@zoy.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 <errno.h>                                                 /* ENOMEM */
30 #include <fcntl.h>                                       /* open(), O_WRONLY */
31 #include <string.h>                                            /* strerror() */
32 #include <unistd.h>                                      /* write(), close() */
33 #include <stdlib.h>                            /* calloc(), malloc(), free() */
34
35 #include <vlc/vlc.h>
36 #include <vlc/aout.h>
37 #include "aout_internal.h"
38
39 #include SDL_INCLUDE_FILE
40
41 #define FRAME_SIZE 2048
42
43 /*****************************************************************************
44  * aout_sys_t: SDL audio output method descriptor
45  *****************************************************************************
46  * This structure is part of the audio output thread descriptor.
47  * It describes the specific properties of an audio device.
48  *****************************************************************************/
49 struct aout_sys_t
50 {   
51     mtime_t next_date;
52     mtime_t buffer_time;
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open        ( vlc_object_t * );
59 static void Close       ( vlc_object_t * );
60
61 static int  SetFormat   ( aout_instance_t * );
62 static void Play        ( aout_instance_t * );
63
64 static void SDLCallback ( void *, Uint8 *, int );
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin();
70     set_description( _("Simple DirectMedia Layer audio module") );
71     set_capability( "audio output", 40 );
72     set_callbacks( Open, Close );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * Open: open the audio device
77  *****************************************************************************/
78 static int Open ( vlc_object_t *p_this )
79 {
80     aout_instance_t *p_aout = (aout_instance_t *)p_this;
81
82     Uint32 i_flags = SDL_INIT_AUDIO;
83
84     if( SDL_WasInit( i_flags ) )
85     {
86         return VLC_EGENERIC;
87     }
88
89     p_aout->output.pf_setformat = SetFormat;
90     p_aout->output.pf_play = Play;
91
92 #ifndef WIN32
93     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
94     i_flags |= SDL_INIT_EVENTTHREAD;
95 #endif
96 #ifdef DEBUG
97     /* In debug mode you may want vlc to dump a core instead of staying
98      * stuck */
99     i_flags |= SDL_INIT_NOPARACHUTE;
100 #endif
101
102     /* Initialize library */
103     if( SDL_Init( i_flags ) < 0 )
104     {
105         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
106         return VLC_EGENERIC;
107     }
108
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * SetFormat: reset the audio device and sets its format
114  *****************************************************************************/
115 static int SetFormat( aout_instance_t *p_aout )
116 {
117     /* TODO: finish and clean this */
118     SDL_AudioSpec desired;
119
120     desired.freq       = p_aout->output.output.i_rate;
121     desired.format     = AUDIO_S16SYS;
122     desired.channels   = p_aout->output.output.i_channels;
123     desired.callback   = SDLCallback;
124     desired.userdata   = p_aout;
125     desired.samples    = FRAME_SIZE;
126
127     /* Open the sound device - FIXME : get the "natural" parameters */
128     if( SDL_OpenAudio( &desired, NULL ) < 0 )
129     {
130         return VLC_EGENERIC;
131     }
132
133     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
134     p_aout->output.i_nb_samples = FRAME_SIZE;
135
136     SDL_PauseAudio( 0 );
137
138     return VLC_SUCCESS;
139 }
140
141 /*****************************************************************************
142  * Play: play a sound samples buffer
143  *****************************************************************************/
144 static void Play( aout_instance_t * p_aout )
145 {
146 }
147
148 /*****************************************************************************
149  * Close: close the audio device
150  *****************************************************************************/
151 static void Close ( vlc_object_t *p_this )
152 {
153     SDL_PauseAudio( 1 );
154     SDL_CloseAudio();
155     SDL_QuitSubSystem( SDL_INIT_AUDIO );
156 }
157
158 /*****************************************************************************
159  * SDLCallback: what to do once SDL has played sound samples
160  *****************************************************************************/
161 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
162 {
163     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
164     aout_buffer_t *   p_buffer;
165
166     /* SDL is unable to call us at regular times, or tell us its current
167      * hardware latency, or the buffer state. So we just pop data and throw
168      * it at SDL's face. Nah. */
169
170     vlc_mutex_lock( &p_aout->output_fifo_lock );
171     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
172     vlc_mutex_unlock( &p_aout->output_fifo_lock );
173
174     if ( p_buffer != NULL )
175     {
176         p_aout->p_vlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
177         aout_BufferFree( p_buffer );
178     }
179     else
180     {
181         p_aout->p_vlc->pf_memset( p_stream, 0, i_len );
182     }
183 }
184