File: oss.c

package info (click to toggle)
fusionsound 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 3,584 kB
  • ctags: 1,568
  • sloc: ansic: 16,461; sh: 8,865; perl: 668; makefile: 375
file content (302 lines) | stat: -rw-r--r-- 8,201 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
   (c) Copyright 2000-2002  convergence integrated media GmbH.
   (c) Copyright 2002-2006  convergence GmbH.

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <sven@convergence.de> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <sys/soundcard.h>

#include <direct/messages.h>
#include <direct/util.h>

#include <fusionsound.h>

#include <core/sound_device.h>
#include <core/sound_driver.h>

#include <misc/sound_conf.h>


FS_SOUND_DRIVER( oss )

/******************************************************************************/

typedef struct {
     int fd;
     int bytes_per_frame;
} OSSDeviceData;

/******************************************************************************/

#ifdef WORDS_BIGENDIAN
# ifndef AFMT_S24_BE
#  define AFMT_S24_BE 0x00001000
# endif
# ifndef AFMT_S32_BE
#  define AFMT_S32_BE 0x00010000
# endif
# define AFMT_S16  AFMT_S16_BE
# define AFMT_S24  AFMT_S24_BE
# define AFMT_S32  AFMT_S32_BE
#else
# ifndef AFMT_S24_LE
#  define AFMT_S24_LE 0x00000800
# endif
# ifndef AFMT_S32_LE
#  define AFMT_S32_LE 0x00008000
# endif
# define AFMT_S16  AFMT_S16_LE
# define AFMT_S24  AFMT_S24_LE
# define AFMT_S32  AFMT_S32_LE
#endif

static inline int
fs2oss_format( FSSampleFormat format )
{
     switch (format) {
          case FSSF_U8:
               return AFMT_U8;
          case FSSF_S16:
               return AFMT_S16;
          case FSSF_S24:
               return AFMT_S24;
          case FSSF_S32:
               return AFMT_S32;
          default:
               break;
     }
     
     return -1;
}

static inline FSSampleFormat
oss2fs_format( int format )
{
     switch (format) {
          case AFMT_U8:
               return FSSF_U8;
          case AFMT_S16:
               return FSSF_S16;
          case AFMT_S24:
               return FSSF_S24;
          case AFMT_S32:
               return FSSF_S32;
          default:
               break;
     }
     
     return -1;
}

/******************************************************************************/

static DFBResult
device_probe( void )
{
     int fd, fmt;
     
     if (fs_config->device) {
          fd = open( fs_config->device, O_WRONLY | O_NONBLOCK );
     }
     else {
          fd = direct_try_open( "/dev/dsp", "/dev/sound/dsp", 
                                O_WRONLY | O_NONBLOCK, false );
     }
     
     if (fd < 0)
          return DFB_IO;
     
     /* issue a generic ioctl to test the device */     
     if (ioctl( fd, SNDCTL_DSP_GETFMTS, &fmt ) < 0) {
          close( fd );
          return DFB_UNSUPPORTED;
     }
          
     close( fd );
     
     return DFB_OK;
}

static void
device_get_driver_info( SoundDriverInfo *info )
{
     snprintf( info->name, 
               FS_SOUND_DRIVER_INFO_NAME_LENGTH, 
               "OSS" );
     
     snprintf( info->vendor,
               FS_SOUND_DRIVER_INFO_VENDOR_LENGTH,
               "directfb.org" );
               
     snprintf( info->url, 
               FS_SOUND_DRIVER_INFO_URL_LENGTH,
               "http://www.directfb.org" );
               
     snprintf( info->license, 
               FS_SOUND_DRIVER_INFO_LICENSE_LENGTH,
               "LGPL" );
               
     info->version.major = 0;
     info->version.minor = 1;
     
     info->device_data_size = sizeof(OSSDeviceData);
}

static DFBResult
device_open( void                  *device_data, 
             SoundDeviceInfo       *device_info,
             CoreSoundDeviceConfig *config )
{
     OSSDeviceData *data       = device_data;
     int            fmt        = fs2oss_format( config->format );
     int            channels   = config->channels;
     int            rate       = config->rate;
     int            buffersize = 0;
#if defined(SNDCTL_DSP_PROFILE) && defined(APF_NORMAL)
     int            prof       = APF_NORMAL;
#endif
     
     if (fmt == -1)
          return DFB_UNSUPPORTED;
     
     /* open sound device in non-blocking mode */
     if (fs_config->device) {
          data->fd = open( fs_config->device, O_WRONLY | O_NONBLOCK );
     }
     else {
          data->fd = direct_try_open( "/dev/dsp", "/dev/sound/dsp", 
                                      O_WRONLY | O_NONBLOCK, false );
     }
     
     if (data->fd < 0) {
          D_ERROR( "FusionSound/Device/OSS: "
                   "Couldn't open output device!\n" );
          return DFB_IO;
     }
     
     /* reset to blocking mode */
     fcntl( data->fd, F_SETFL, fcntl( data->fd, F_GETFL ) & ~O_NONBLOCK );
      
     /* TODO: get device name */
     
     /* device capabilities */
     device_info->caps = DCF_WRITEBLOCKS;

     
     /* set application profile */
#if defined(SNDCTL_DSP_PROFILE) && defined(APF_NORMAL)
     if (ioctl( data->fd, SNDCTL_DSP_PROFILE, &prof ) < 0) {
          D_ERROR( "FusionSound/Device/OSS: "
                   "Unable to set application profile!\n" );
          close( data->fd );
          return DFB_FAILURE;
     }
#endif
         
     /* set output format */
     if (ioctl( data->fd, SNDCTL_DSP_SETFMT, &fmt ) < 0 || 
         oss2fs_format( fmt ) != config->format) {
          D_ERROR( "FusionSound/Device/OSS: unsupported format!\n" );
          close( data->fd );
          return DFB_UNSUPPORTED;
     }

     /* set number of channels */
     if (ioctl( data->fd, SNDCTL_DSP_CHANNELS, &channels ) < 0 || 
         channels != config->channels) {
          D_ERROR( "FusionSound/Device/OSS: unsupported channels mode!\n" );
          close( data->fd );
          return DFB_UNSUPPORTED;
     }
     
     data->bytes_per_frame = config->channels * 
                             FS_BITS_PER_SAMPLE(config->format) >> 3;
     
     /* set sample rate */
     if (ioctl( data->fd, SNDCTL_DSP_SPEED, &rate ) < 0) {
          D_ERROR( "FusionSound/Device/OSS: "
                   "Unable to set rate to '%d'!\n", config->rate );
          close( data->fd );
          return DFB_UNSUPPORTED;
     }
     
     /* query block size */
     ioctl( data->fd, SNDCTL_DSP_GETBLKSIZE, &buffersize );
     buffersize /= data->bytes_per_frame;
     if (buffersize < 1) {
          D_ERROR( "FusionSound/Device/OSS: Unable to query block size!\n" );
          close( data->fd );
          return DFB_UNSUPPORTED;
     }

     config->rate = rate;
     config->buffersize = buffersize;

     return DFB_OK;
}

static void
device_write( void *device_data, void *samples, unsigned count )
{
     OSSDeviceData *data = device_data;
     
     if (write( data->fd, samples, count*data->bytes_per_frame ) < 0)
          D_PERROR( "FusionSound/Device/OSS: couldn't write %d frames!\n", count );
}

static void
device_get_output_delay( void *device_data, int *delay )
{
     OSSDeviceData  *data = device_data;
     audio_buf_info  info;
     
     if (ioctl( data->fd, SNDCTL_DSP_GETOSPACE, &info ) < 0) {
          D_ONCE( "ioctl SNDCTL_DSP_GETOSPACE failed" );
          *delay = 0;
          return;
     }
     
     *delay = (info.fragsize * info.fragstotal - info.bytes) / data->bytes_per_frame;
}

static void
device_close( void *device_data )
{
     OSSDeviceData *data = device_data;
     
     ioctl( data->fd, SNDCTL_DSP_RESET, 0 );
     close( data->fd );
}