]> git.sesse.net Git - vlc/blob - modules/audio_output/sdl.c
6ab0a1390875120f42af199f3f12f515fff2fd37
[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.3 2002/08/19 21:31:11 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*2
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Open        ( vlc_object_t * );
47 static void Close       ( vlc_object_t * );
48
49 static int  SetFormat   ( aout_instance_t * );
50 static void Play        ( aout_instance_t * );
51
52 static void SDLCallback ( void *, Uint8 *, int );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_description( _("Simple DirectMedia Layer audio module") );
59     set_capability( "audio output", 40 );
60     set_callbacks( Open, Close );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Open: open the audio device
65  *****************************************************************************/
66 static int Open ( vlc_object_t *p_this )
67 {
68     aout_instance_t *p_aout = (aout_instance_t *)p_this;
69
70     Uint32 i_flags = SDL_INIT_AUDIO;
71
72     if( SDL_WasInit( i_flags ) )
73     {
74         return 1;
75     }
76
77     p_aout->output.pf_setformat = SetFormat;
78     p_aout->output.pf_play = Play;
79
80 #ifndef WIN32
81     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
82     i_flags |= SDL_INIT_EVENTTHREAD;
83 #endif
84 #ifdef DEBUG
85     /* In debug mode you may want vlc to dump a core instead of staying
86      * stuck */
87     i_flags |= SDL_INIT_NOPARACHUTE;
88 #endif
89
90     /* Initialize library */
91     if( SDL_Init( i_flags ) < 0 )
92     {
93         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
94         return 1;
95     }
96
97     return 0;
98 }
99
100 /*****************************************************************************
101  * SetFormat: reset the audio device and sets its format
102  *****************************************************************************/
103 static int SetFormat( aout_instance_t *p_aout )
104 {
105     /* TODO: finish and clean this */
106     SDL_AudioSpec desired;
107
108     desired.freq       = p_aout->output.output.i_rate;
109     desired.format     = AUDIO_S16SYS;
110     desired.channels   = p_aout->output.output.i_channels;
111     desired.callback   = SDLCallback;
112     desired.userdata   = p_aout;
113     desired.samples    = FRAME_SIZE;
114
115     /* Open the sound device - FIXME : get the "natural" parameters */
116     if( SDL_OpenAudio( &desired, NULL ) < 0 )
117     {
118         return -1;
119     }
120
121     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
122     p_aout->output.i_nb_samples = FRAME_SIZE;
123
124     SDL_PauseAudio( 0 );
125
126     return 0;
127 }
128
129 /*****************************************************************************
130  * Play: play a sound samples buffer
131  *****************************************************************************/
132 static void Play( aout_instance_t * p_aout )
133 {
134 }
135
136 /*****************************************************************************
137  * Close: close the audio device
138  *****************************************************************************/
139 static void Close ( vlc_object_t *p_this )
140 {
141     SDL_PauseAudio( 1 );
142     SDL_CloseAudio();
143     SDL_QuitSubSystem( SDL_INIT_AUDIO );
144 }
145
146 /*****************************************************************************
147  * SDLCallback: what to do once SDL has played sound samples
148  *****************************************************************************/
149 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
150 {
151     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
152     /* FIXME : take into account SDL latency instead of mdate() */
153     aout_buffer_t * p_buffer = aout_OutputNextBuffer( p_aout, mdate(), 0 );
154
155     if ( i_len != FRAME_SIZE * sizeof(s16)
156                     * p_aout->output.output.i_channels )
157     {
158         msg_Err( p_aout, "SDL doesn't know its buffer size (%d)", i_len );
159     }
160
161     if ( p_buffer != NULL )
162     {
163         p_aout->p_vlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
164         aout_BufferFree( p_buffer );
165     }
166     else
167     {
168         p_aout->p_vlc->pf_memset( p_stream, 0, i_len );
169     }
170 }
171