]> git.sesse.net Git - vlc/blob - plugins/beos/aout_beos.cpp
* ALL: the first libvlc commit.
[vlc] / plugins / beos / aout_beos.cpp
1 /*****************************************************************************
2  * aout_beos.cpp: BeOS audio output
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: aout_beos.cpp,v 1.24 2002/06/01 12:31:58 sam Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <kernel/OS.h>
31 #include <View.h>
32 #include <Application.h>
33 #include <Message.h>
34 #include <Locker.h>
35 #include <media/MediaDefs.h>
36 #include <game/PushGameSound.h>
37 #include <malloc.h>
38 #include <string.h>
39
40 extern "C"
41 {
42 #include <vlc/vlc.h>
43 #include <vlc/aout.h>
44 }
45
46 /*****************************************************************************
47  * aout_sys_t: BeOS audio output method descriptor
48  *****************************************************************************
49  * This structure is part of the audio output thread descriptor.
50  * It describes some BeOS specific variables.
51  *****************************************************************************/
52 struct aout_sys_s
53 {
54     BPushGameSound * p_sound;
55     gs_audio_format * p_format;
56     void * p_buffer;
57     int i_buffer_size;
58     int i_buffer_pos;
59 };
60
61 extern "C"
62 {
63
64 /*****************************************************************************
65  * Local prototypes.
66  *****************************************************************************/
67 static int     aout_Open        ( aout_thread_t *p_aout );
68 static int     aout_SetFormat   ( aout_thread_t *p_aout );
69 static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
70 static void    aout_Play        ( aout_thread_t *p_aout,
71                                   byte_t *buffer, int i_size );
72 static void    aout_Close       ( aout_thread_t *p_aout );
73
74 /*****************************************************************************
75  * Functions exported as capabilities. They are declared as static so that
76  * we don't pollute the namespace too much.
77  *****************************************************************************/
78 void _M( aout_getfunctions )( function_list_t * p_function_list )
79 {
80     p_function_list->functions.aout.pf_open = aout_Open;
81     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
82     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
83     p_function_list->functions.aout.pf_play = aout_Play;
84     p_function_list->functions.aout.pf_close = aout_Close;
85 }
86
87 /*****************************************************************************
88  * aout_Open: opens a BPushGameSound
89  *****************************************************************************/
90 static int aout_Open( aout_thread_t *p_aout )
91 {
92     /* Allocate structure */
93     p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
94     if( p_aout->p_sys == NULL )
95     {
96         msg_Err( p_aout, "out of memory" );
97         return( 1 );
98     }
99
100     /* Allocate gs_audio_format */
101     p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
102     if( p_aout->p_sys->p_format == NULL )
103     {
104         free( p_aout->p_sys );
105         msg_Err( p_aout, "out of memory" );
106         return( 1 );
107     }
108
109     /* Initialize some variables */
110     p_aout->p_sys->p_format->frame_rate = 44100.0;
111     p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
112     p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
113     p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
114     p_aout->p_sys->p_format->buffer_size = 4*8192;
115     p_aout->p_sys->i_buffer_pos = 0;
116
117     /* Allocate BPushGameSound */
118     p_aout->p_sys->p_sound = new BPushGameSound( 8192,
119                                                  p_aout->p_sys->p_format,
120                                                  2, NULL );
121     if( p_aout->p_sys->p_sound == NULL )
122     {
123         free( p_aout->p_sys->p_format );
124         free( p_aout->p_sys );
125         msg_Err( p_aout, "cannot allocate BPushGameSound" );
126         return( 1 );
127     }
128
129     if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
130     {
131         free( p_aout->p_sys->p_format );
132         free( p_aout->p_sys );
133         msg_Err( p_aout, "cannot initialize BPushGameSound" );
134         return( 1 );
135     }
136
137     p_aout->p_sys->p_sound->StartPlaying( );
138
139     p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
140                             (size_t *)&p_aout->p_sys->i_buffer_size );
141
142     return( 0 );
143 }
144
145 /*****************************************************************************
146  * aout_SetFormat: sets the dsp output format
147  *****************************************************************************/
148 static int aout_SetFormat( aout_thread_t *p_aout )
149 {
150     return( 0 );
151 }
152
153 /*****************************************************************************
154  * aout_GetBufInfo: buffer status query
155  *****************************************************************************/
156 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
157 {
158     /* Each value is 4 bytes long (stereo signed 16 bits) */
159     int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
160
161     i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
162     if( i_hard_pos < 0 )
163     {
164          i_hard_pos += p_aout->p_sys->i_buffer_size;
165     }
166
167     return( i_hard_pos );
168 }
169
170 /*****************************************************************************
171  * aout_Play: plays a sound samples buffer
172  *****************************************************************************
173  * This function writes a buffer of i_length bytes in the dsp
174  *****************************************************************************/
175 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
176 {
177     int i_newbuf_pos;
178
179     if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
180               > p_aout->p_sys->i_buffer_size )
181     {
182         memcpy( (void *)((int)p_aout->p_sys->p_buffer
183                         + p_aout->p_sys->i_buffer_pos),
184                 buffer,
185                 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
186
187         memcpy( (void *)((int)p_aout->p_sys->p_buffer),
188                 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
189                 i_size - ( p_aout->p_sys->i_buffer_size
190                              - p_aout->p_sys->i_buffer_pos ) );
191         
192         p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
193
194     }
195     else
196     {
197         memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
198                 buffer, i_size );
199
200         p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
201     }
202 }
203
204 /*****************************************************************************
205  * aout_Close: closes the dsp audio device
206  *****************************************************************************/
207 static void aout_Close( aout_thread_t *p_aout )
208 {
209     p_aout->p_sys->p_sound->UnlockCyclic();
210     p_aout->p_sys->p_sound->StopPlaying( );
211     delete p_aout->p_sys->p_sound;
212     free( p_aout->p_sys->p_format );
213     free( p_aout->p_sys );
214 }
215
216 } /* extern "C" */
217