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 $
7 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Eric Petit <titer@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
34 #include <SoundPlayer.h>
35 #include <media/MediaDefs.h>
42 #include <aout_internal.h>
45 /*****************************************************************************
46 * aout_sys_t: BeOS audio output method descriptor
47 *****************************************************************************/
49 typedef struct aout_sys_t
51 BSoundPlayer *p_player;
57 /*****************************************************************************
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 );
65 /*****************************************************************************
67 *****************************************************************************/
68 int E_(OpenAudio) ( vlc_object_t * p_this )
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 )
74 msg_Err( p_aout, "Not enough memory" );
77 aout_sys_t *p_sys = p_aout->output.p_sys;
79 aout_VolumeSoftInit( p_aout );
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 )
86 p_aout->output.output.i_physical_channels
87 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
90 media_raw_audio_format *p_format;
91 p_format = (media_raw_audio_format*)
92 malloc( sizeof( media_raw_audio_format ) );
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;
100 p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
102 p_format->buffer_size = 8192;
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;
108 p_sys->p_player = new BSoundPlayer( p_format, "player", Play, NULL, p_aout );
109 if( p_sys->p_player->InitCheck() != B_OK )
111 msg_Err( p_aout, "BSoundPlayer InitCheck failed" );
112 delete p_sys->p_player;
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 ) )
131 msg_Err( p_aout, "cannot create Latency thread" );
134 p_sys->p_player->Start();
135 p_sys->p_player->SetHasData( true );
140 /*****************************************************************************
142 *****************************************************************************/
143 void E_(CloseAudio) ( vlc_object_t *p_this )
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;
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 );
153 p_sys->p_player->Stop();
154 delete p_sys->p_player;
158 /*****************************************************************************
160 *****************************************************************************/
161 static void Play( void *aout, void *p_buffer, size_t i_size,
162 const media_raw_audio_format &format )
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;
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 );
174 p_aout_buffer = aout_OutputNextBuffer( p_aout, play_time, VLC_FALSE );
176 if( p_aout_buffer != NULL )
178 p_aout->p_vlc->pf_memcpy( (float*)p_buffer,
179 p_aout_buffer->p_buffer, i_size );
180 aout_BufferFree( p_aout_buffer );
184 p_aout->p_vlc->pf_memset( (float*)p_buffer, 0, i_size );
188 /*****************************************************************************
190 *****************************************************************************/
191 static void DoNothing( aout_instance_t *p_aout )
196 /*****************************************************************************
198 *****************************************************************************/
199 static int CheckLatency( aout_instance_t *p_aout )
201 aout_sys_t *p_sys = (aout_sys_t*)p_aout->output.p_sys;
202 mtime_t last_check = 0;
205 while( !p_aout->b_die )
207 /* check every 0.1 second */
208 if( mdate() > last_check + 100000 )
210 latency = p_sys->p_player->Latency();
213 vlc_mutex_lock( &p_sys->lock );
214 p_sys->latency = latency;
215 vlc_mutex_unlock( &p_sys->lock );
217 last_check = mdate();