2 * OS/2 DART audio output driver
4 * Copyright (c) 2007-2009 by KO Myung-Hun (komh@chollian.net)
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #define INCL_DOSERRORS
35 #include "libaf/af_format.h"
36 #include "audio_out.h"
37 #include "audio_out_internal.h"
39 #include "libvo/fastmemcpy.h"
40 #include "subopt-helper.h"
42 static const ao_info_t info
= {
45 "KO Myung-Hun <komh@chollian.net>",
51 #define OUTBURST_SAMPLES 512
52 #define DEFAULT_DART_SAMPLES (OUTBURST_SAMPLES << 2)
54 #define CHUNK_SIZE ao_data.outburst
56 static uint8_t *m_audioBuf
= NULL
;
58 static int m_nBufSize
= 0;
60 static volatile int m_fQuit
= FALSE
;
61 // may only be modified by DART's playback thread or while it is stopped
62 static volatile int m_iBufReadPos
= 0;
63 // may only be modified by MPlayer's thread
64 static volatile int m_iBufWritePos
= 0;
66 // may only be called by MPlayer's thread
67 // return value may change between immediately following two calls,
68 // and the real number of free bytes might be larger!
69 static int buf_free(void)
71 int nFree
= m_iBufReadPos
- m_iBufWritePos
- CHUNK_SIZE
;
79 // may only be called by DART's playback thread
80 // return value may change between immediately following two calls,
81 // and the real number of buffered bytes might be larger!
82 static int buf_used(void)
84 int nUsed
= m_iBufWritePos
- m_iBufReadPos
;
92 static int write_buffer(unsigned char *data
, int len
)
94 int nFirstLen
= m_nBufSize
- m_iBufWritePos
;
95 int nFree
= buf_free();
103 // till end of buffer
104 fast_memcpy(m_audioBuf
+ m_iBufWritePos
, data
, nFirstLen
);
105 if (len
> nFirstLen
) { // we have to wrap around
106 // remaining part from beginning of buffer
107 fast_memcpy(m_audioBuf
, data
+ nFirstLen
, len
- nFirstLen
);
110 m_iBufWritePos
= (m_iBufWritePos
+ len
) % m_nBufSize
;
115 static int read_buffer(unsigned char *data
, int len
)
117 int nFirstLen
= m_nBufSize
- m_iBufReadPos
;
118 int nBuffered
= buf_used();
126 // till end of buffer
127 fast_memcpy(data
, m_audioBuf
+ m_iBufReadPos
, nFirstLen
);
128 if (len
> nFirstLen
) { // we have to wrap around
129 // remaining part from beginning of buffer
130 fast_memcpy(data
+ nFirstLen
, m_audioBuf
, len
- nFirstLen
);
133 m_iBufReadPos
= (m_iBufReadPos
+ len
) % m_nBufSize
;
138 // end ring buffer stuff
140 static ULONG APIENTRY
dart_audio_callback(PVOID pCBData
, PVOID pBuffer
,
145 nReadLen
= read_buffer(pBuffer
, ulSize
);
146 if (nReadLen
< ulSize
&& !m_fQuit
) {
147 memset((uint8_t *)pBuffer
+ nReadLen
, DART
.bSilence
, ulSize
- nReadLen
);
154 // to set/get/query special features/parameters
155 static int control(int cmd
, void *arg
)
158 case AOCONTROL_GET_VOLUME
:
160 ao_control_vol_t
*vol
= arg
;
162 vol
->left
= vol
->right
= LOUSHORT(dartGetVolume());
167 case AOCONTROL_SET_VOLUME
:
170 ao_control_vol_t
*vol
= arg
;
172 mid
= (vol
->left
+ vol
->right
) / 2;
173 dartSetVolume(MCI_SET_AUDIO_ALL
, mid
);
179 return CONTROL_UNKNOWN
;
182 static void print_help(void)
184 mp_msg(MSGT_AO
, MSGL_FATAL
,
185 "\n-ao dart commandline help:\n"
186 "Example: mplayer -ao dart:noshare\n"
187 " open DART in exclusive mode\n"
190 " Open DART in shareable or exclusive mode\n"
192 " Set buffer size to <size> in samples(default: 2048)\n");
195 // open & set up audio device
196 // return: 1=success 0=fail
197 static int init(int rate
, int channels
, int format
, int flags
)
200 int nDartSamples
= DEFAULT_DART_SAMPLES
;
204 {"share", OPT_ARG_BOOL
, &fShare
, NULL
},
205 {"bufsize", OPT_ARG_INT
, &nDartSamples
, (opt_test_f
)int_non_neg
},
209 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
215 nDartSamples
= DEFAULT_DART_SAMPLES
;
217 mp_msg(MSGT_AO
, MSGL_V
, "DART: opened in %s mode, buffer size = %d sample(s)\n",
218 fShare
? "shareable" : "exclusive", nDartSamples
);
221 case AF_FORMAT_S16_LE
:
226 format
= AF_FORMAT_S16_LE
;
227 mp_msg(MSGT_AO
, MSGL_V
, "DART: format %s not supported defaulting to Signed 16-bit Little-Endian\n",
228 af_fmt2str_short(format
));
232 nBytesPerSample
= (af_fmt2bits(format
) >> 3) * channels
;
234 if (dartInit(0, af_fmt2bits(format
), rate
, MCI_WAVE_FORMAT_PCM
, channels
,
235 2, nBytesPerSample
* nDartSamples
, fShare
,
236 dart_audio_callback
, NULL
))
239 mp_msg(MSGT_AO
, MSGL_V
, "DART: obtained buffer size = %lu bytes\n",
244 ao_data
.channels
= channels
;
245 ao_data
.samplerate
= rate
;
246 ao_data
.format
= format
;
247 ao_data
.bps
= nBytesPerSample
* rate
;
248 ao_data
.outburst
= nBytesPerSample
* OUTBURST_SAMPLES
;
249 ao_data
.buffersize
= DART
.ulBufferSize
;
251 // multiple of CHUNK_SIZE
252 m_nBufSize
= ((DART
.ulBufferSize
<< 2) / CHUNK_SIZE
) * CHUNK_SIZE
;
253 // and one more chunk plus round up
254 m_nBufSize
+= 2 * CHUNK_SIZE
;
256 m_audioBuf
= malloc(m_nBufSize
);
263 // might cause PM DLLs to be loaded which incorrectly enable SIG_FPE,
264 // which AAC decoding might trigger.
265 // so, mask off all floating-point exceptions.
266 _control87(MCW_EM
, MCW_EM
);
271 // close audio device
272 static void uninit(int immed
)
277 while (DART
.fPlaying
)
286 // stop playing and empty buffers (for seeking/pause)
287 static void reset(void)
291 // Reset ring-buffer state
298 // stop playing, keep buffers (for pause)
299 static void audio_pause(void)
304 // resume playing, after audio_pause()
305 static void audio_resume(void)
310 // return: how many bytes can be played without blocking
311 static int get_space(void)
316 // plays 'len' bytes of 'data'
317 // it should round it down to outburst*n
318 // return: number of bytes played
319 static int play(void *data
, int len
, int flags
)
322 if (!(flags
& AOPLAY_FINAL_CHUNK
))
323 len
= (len
/ ao_data
.outburst
) * ao_data
.outburst
;
325 return write_buffer(data
, len
);
328 // return: delay in seconds between first and last sample in buffer
329 static float get_delay(void)
331 int nBuffered
= m_nBufSize
- CHUNK_SIZE
- buf_free(); // could be less
333 return (float)nBuffered
/ (float)ao_data
.bps
;