]> git.sesse.net Git - vlc/blob - modules/gui/beos/AudioOutput.cpp
Merge branch 1.0-bugfix
[vlc] / modules / gui / beos / AudioOutput.cpp
1 /*****************************************************************************
2  * AudioOutput.cpp: BeOS audio output
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <malloc.h>
30
31 #include <SoundPlayer.h>
32 #include <media/MediaDefs.h>
33
34
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include <vlc_aout.h>
41 extern "C"
42 {
43     #include <aout_internal.h>
44 }
45
46 /*****************************************************************************
47  * aout_sys_t: BeOS audio output method descriptor
48  *****************************************************************************/
49
50 typedef struct aout_sys_t
51 {
52     BSoundPlayer * p_player;
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
64 /*****************************************************************************
65  * OpenAudio
66  *****************************************************************************/
67 int OpenAudio ( vlc_object_t * p_this )
68 {
69     aout_instance_t * p_aout = (aout_instance_t*) p_this;
70     p_aout->output.p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
71     if( p_aout->output.p_sys == NULL )
72         return -1;
73     aout_sys_t * p_sys = p_aout->output.p_sys;
74
75     aout_VolumeSoftInit( p_aout );
76
77     int i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
78     /* BSoundPlayer does not support more than 2 channels AFAIK */
79     if( i_nb_channels > 2 )
80     {
81         i_nb_channels = 2;
82         p_aout->output.output.i_physical_channels
83             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
84     }
85
86     media_raw_audio_format * p_format;
87     p_format = (media_raw_audio_format*)
88         malloc( sizeof( media_raw_audio_format ) );
89
90     p_format->channel_count = i_nb_channels;
91     p_format->frame_rate = p_aout->output.output.i_rate;
92     p_format->format = media_raw_audio_format::B_AUDIO_FLOAT;
93 #ifdef WORDS_BIGENDIAN
94     p_format->byte_order = B_MEDIA_BIG_ENDIAN;
95 #else
96     p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
97 #endif
98     p_format->buffer_size = 8192;
99
100     p_aout->output.output.i_format = VLC_CODEC_FL32;
101     p_aout->output.i_nb_samples = 2048 / i_nb_channels;
102     p_aout->output.pf_play = DoNothing;
103
104     p_sys->p_player = new BSoundPlayer( p_format, "player", Play, NULL, p_aout );
105     if( p_sys->p_player->InitCheck() != B_OK )
106     {
107         msg_Err( p_aout, "BSoundPlayer InitCheck failed" );
108         delete p_sys->p_player;
109         free( p_sys );
110         return -1;
111     }
112
113     /* Start playing */
114     p_sys->latency = p_sys->p_player->Latency();
115     p_sys->p_player->Start();
116     p_sys->p_player->SetHasData( true );
117
118     return 0;
119 }
120
121 /*****************************************************************************
122  * CloseAudio
123  *****************************************************************************/
124 void CloseAudio ( vlc_object_t * p_this )
125 {
126     aout_instance_t * p_aout = (aout_instance_t *) p_this;
127     aout_sys_t * p_sys = (aout_sys_t *) p_aout->output.p_sys;
128
129     /* Clean up */
130     p_sys->p_player->Stop();
131     delete p_sys->p_player;
132     free( p_sys );
133 }
134
135 /*****************************************************************************
136  * Play
137  *****************************************************************************/
138 static void Play( void * _p_aout, void * _p_buffer, size_t i_size,
139                   const media_raw_audio_format &format )
140 {
141     aout_instance_t * p_aout = (aout_instance_t*) _p_aout;
142     float * p_buffer = (float*) _p_buffer;
143     aout_sys_t * p_sys = (aout_sys_t*) p_aout->output.p_sys;
144     aout_buffer_t * p_aout_buffer;
145
146     p_aout_buffer = aout_OutputNextBuffer( p_aout,
147                                            mdate() + p_sys->latency,
148                                            false );
149
150     if( p_aout_buffer != NULL )
151     {
152         vlc_memcpy( p_buffer, p_aout_buffer->p_buffer,
153                     MIN( i_size, p_aout_buffer->i_nb_bytes ) );
154         if( p_aout_buffer->i_nb_bytes < i_size )
155         {
156             vlc_memset(  p_buffer + p_aout_buffer->i_nb_bytes,
157                          0, i_size - p_aout_buffer->i_nb_bytes );
158         }
159         aout_BufferFree( p_aout_buffer );
160     }
161     else
162     {
163         vlc_memset( p_buffer, 0, i_size );
164     }
165 }
166
167 /*****************************************************************************
168  * DoNothing
169  *****************************************************************************/
170 static void DoNothing( aout_instance_t *p_aout )
171 {
172     return;
173 }