]> git.sesse.net Git - vlc/blob - modules/gui/beos/AudioOutput.cpp
* added a sanity lock
[vlc] / modules / gui / beos / AudioOutput.cpp
1 /*****************************************************************************
2  * AudioOutput.cpp: BeOS audio output
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: AudioOutput.cpp,v 1.27 2003/01/14 15:31:12 titer Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Eric Petit <titer@videolan.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 <stdio.h>
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <malloc.h>
32 #include <string.h>
33
34 #include <SoundPlayer.h>
35 #include <media/MediaDefs.h>
36
37
38 #include <vlc/vlc.h>
39 #include <vlc/aout.h>
40 extern "C"
41 {
42     #include <aout_internal.h>
43 }
44
45 /*****************************************************************************
46  * aout_sys_t: BeOS audio output method descriptor
47  *****************************************************************************/
48
49 typedef struct aout_sys_t
50 {
51     BSoundPlayer *p_player;
52     vlc_mutex_t   lock;
53     mtime_t       latency;
54     
55 } aout_sys_t;
56
57 /*****************************************************************************
58  * Local prototypes.
59  *****************************************************************************/
60 static void Play         ( void *p_aout, void *p_buffer, size_t size,
61                            const media_raw_audio_format &format );
62 static void DoNothing    ( aout_instance_t *p_aout );
63 static int  CheckLatency ( aout_instance_t *p_aout );
64
65 /*****************************************************************************
66  * OpenAudio
67  *****************************************************************************/
68 int E_(OpenAudio) ( vlc_object_t * p_this )
69 {
70     aout_instance_t *p_aout = (aout_instance_t*) p_this;
71     p_aout->output.p_sys = (aout_sys_t *) malloc( sizeof( aout_sys_t ) );
72     if( p_aout->output.p_sys == NULL )
73     {
74         msg_Err( p_aout, "Not enough memory" );
75         return -1;
76     }
77     aout_sys_t *p_sys = p_aout->output.p_sys;
78
79     aout_VolumeSoftInit( p_aout );
80
81     int i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
82     /* BSoundPlayer does not support more than 2 channels AFAIK */
83     if ( i_nb_channels > 2 )
84     {
85         i_nb_channels = 2;
86         p_aout->output.output.i_physical_channels
87             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
88     }
89  
90     media_raw_audio_format *p_format;
91     p_format = (media_raw_audio_format*)
92         malloc( sizeof( media_raw_audio_format ) );
93     
94     p_format->channel_count = i_nb_channels;
95     p_format->frame_rate = p_aout->output.output.i_rate;    
96     p_format->format = media_raw_audio_format::B_AUDIO_FLOAT;
97 #ifdef WORDS_BIGENDIAN
98     p_format->byte_order = B_MEDIA_BIG_ENDIAN;
99 #else
100     p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
101 #endif
102     p_format->buffer_size = 8192;
103
104     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
105     p_aout->output.i_nb_samples = 2048 / i_nb_channels;
106     p_aout->output.pf_play = DoNothing;
107
108     p_sys->p_player = new BSoundPlayer( p_format, "player", Play, NULL, p_aout );
109     if( p_sys->p_player->InitCheck() != B_OK )
110     {
111         msg_Err( p_aout, "BSoundPlayer InitCheck failed" );
112         delete p_sys->p_player;
113         free( p_sys );
114         return -1;
115     }
116
117     /* FIXME FIXME FIXME
118      * We should check BSoundPlayer Latency() everytime we call
119      * aout_OutputNextBuffer(). Unfortunatly, it does not seem to work
120      * correctly: on my computer, it hangs for about 5 seconds at the
121      * beginning of the file. This is not acceptable, so we will start
122      * playing the file with a default latency (my computer's one ;p )
123      * and we create a thread who's going to update it ASAP. According
124      * to what I've seen, the latency is almost constant most of the
125      * time anyway -- titer */
126     p_sys->latency = 16209;
127     vlc_mutex_init( p_aout, &p_sys->lock );
128     if( vlc_thread_create( p_aout, "latency", CheckLatency,
129                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
130     {
131         msg_Err( p_aout, "cannot create Latency thread" );
132     }
133
134     p_sys->p_player->Start();
135     p_sys->p_player->SetHasData( true );
136
137     return 0;
138 }
139
140 /*****************************************************************************
141  * CloseAudio
142  *****************************************************************************/
143 void E_(CloseAudio) ( vlc_object_t *p_this )
144 {
145     aout_instance_t * p_aout = (aout_instance_t *) p_this;
146     aout_sys_t * p_sys = (aout_sys_t *) p_aout->output.p_sys;
147     
148     p_aout->b_die = VLC_TRUE;
149     vlc_thread_join( p_aout );
150     p_aout->b_die = VLC_FALSE;
151     vlc_mutex_destroy( &p_sys->lock );
152     
153     p_sys->p_player->Stop();
154     delete p_sys->p_player;
155     free( p_sys );
156 }
157
158 /*****************************************************************************
159  * Play
160  *****************************************************************************/
161 static void Play( void *aout, void *p_buffer, size_t i_size,
162                   const media_raw_audio_format &format )
163 {
164     aout_instance_t *p_aout = (aout_instance_t*)aout;
165     aout_sys_t *p_sys = (aout_sys_t*)p_aout->output.p_sys;
166     aout_buffer_t * p_aout_buffer;
167     mtime_t play_time = 0;
168     
169     /* FIXME (see above) */
170     vlc_mutex_lock( &p_sys->lock );
171     play_time = mdate() + p_sys->latency;
172     vlc_mutex_unlock( &p_sys->lock );
173     
174     p_aout_buffer = aout_OutputNextBuffer( p_aout, play_time, VLC_FALSE );
175
176     if( p_aout_buffer != NULL )
177     {
178         p_aout->p_vlc->pf_memcpy( (float*)p_buffer,
179                                   p_aout_buffer->p_buffer, i_size );
180         aout_BufferFree( p_aout_buffer );
181     }
182     else
183     {
184         p_aout->p_vlc->pf_memset( (float*)p_buffer, 0, i_size );
185     }
186 }
187
188 /*****************************************************************************
189  * DoNothing 
190  *****************************************************************************/
191 static void DoNothing( aout_instance_t *p_aout )
192 {
193     return;
194 }
195
196 /*****************************************************************************
197  * CheckLatency
198  *****************************************************************************/
199 static int CheckLatency( aout_instance_t *p_aout )
200 {
201     aout_sys_t *p_sys = (aout_sys_t*)p_aout->output.p_sys;
202     mtime_t last_check = 0;
203     mtime_t latency;
204     
205     while( !p_aout->b_die )
206     {
207         /* check every 0.1 second */
208         if( mdate() > last_check + 100000 )
209         {
210             latency = p_sys->p_player->Latency();
211             if( latency > 0 )
212             {
213                 vlc_mutex_lock( &p_sys->lock );
214                 p_sys->latency = latency;
215                 vlc_mutex_unlock( &p_sys->lock );
216             }
217             last_check = mdate();
218         }
219         snooze( 5000 );
220     }
221     return VLC_SUCCESS;
222 }