remove trailing whitespaces
[mplayer/greg.git] / libao2 / ao_dart.c
blobe9bff8df1bb9832daf459a37b0e65fe2463eaed1
1 /*
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.
23 #define INCL_DOS
24 #define INCL_DOSERRORS
25 #include <os2.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30 #include <float.h>
32 #include <dart.h>
34 #include "config.h"
35 #include "libaf/af_format.h"
36 #include "audio_out.h"
37 #include "audio_out_internal.h"
38 #include "mp_msg.h"
39 #include "libvo/fastmemcpy.h"
40 #include "subopt-helper.h"
42 static const ao_info_t info = {
43 "DART audio output",
44 "dart",
45 "KO Myung-Hun <komh@chollian.net>",
49 LIBAO_EXTERN(dart)
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;
73 if (nFree < 0)
74 nFree += m_nBufSize;
76 return nFree;
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;
86 if (nUsed < 0)
87 nUsed += m_nBufSize;
89 return nUsed;
92 static int write_buffer(unsigned char *data, int len)
94 int nFirstLen = m_nBufSize - m_iBufWritePos;
95 int nFree = buf_free();
97 if (len > nFree)
98 len = nFree;
100 if (nFirstLen > len)
101 nFirstLen = len;
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;
112 return len;
115 static int read_buffer(unsigned char *data, int len)
117 int nFirstLen = m_nBufSize - m_iBufReadPos;
118 int nBuffered = buf_used();
120 if (len > nBuffered)
121 len = nBuffered;
123 if (nFirstLen > len)
124 nFirstLen = len;
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;
135 return len;
138 // end ring buffer stuff
140 static ULONG APIENTRY dart_audio_callback(PVOID pCBData, PVOID pBuffer,
141 ULONG ulSize)
143 int nReadLen;
145 nReadLen = read_buffer(pBuffer, ulSize);
146 if (nReadLen < ulSize && !m_fQuit) {
147 memset((uint8_t *)pBuffer + nReadLen, DART.bSilence, ulSize - nReadLen);
148 nReadLen = ulSize;
151 return nReadLen;
154 // to set/get/query special features/parameters
155 static int control(int cmd, void *arg)
157 switch (cmd) {
158 case AOCONTROL_GET_VOLUME:
160 ao_control_vol_t *vol = arg;
162 vol->left = vol->right = LOUSHORT(dartGetVolume());
164 return CONTROL_OK;
167 case AOCONTROL_SET_VOLUME:
169 int mid;
170 ao_control_vol_t *vol = arg;
172 mid = (vol->left + vol->right) / 2;
173 dartSetVolume(MCI_SET_AUDIO_ALL, mid);
175 return CONTROL_OK;
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"
188 "\nOptions:\n"
189 " (no)share\n"
190 " Open DART in shareable or exclusive mode\n"
191 " bufsize=<size>\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)
199 int fShare = 1;
200 int nDartSamples = DEFAULT_DART_SAMPLES;
201 int nBytesPerSample;
203 opt_t subopts[] = {
204 {"share", OPT_ARG_BOOL, &fShare, NULL},
205 {"bufsize", OPT_ARG_INT, &nDartSamples, (opt_test_f)int_non_neg},
206 {NULL}
209 if (subopt_parse(ao_subdevice, subopts) != 0) {
210 print_help();
211 return 0;
214 if (!nDartSamples)
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);
220 switch (format) {
221 case AF_FORMAT_S16_LE:
222 case AF_FORMAT_S8:
223 break;
225 default:
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));
229 break;
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))
237 return 0;
239 mp_msg(MSGT_AO, MSGL_V, "DART: obtained buffer size = %lu bytes\n",
240 DART.ulBufferSize);
242 m_fQuit = FALSE;
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);
258 m_iBufReadPos = 0;
259 m_iBufWritePos = 0;
261 dartPlay();
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);
268 return 1;
271 // close audio device
272 static void uninit(int immed)
274 m_fQuit = TRUE;
276 if (!immed) {
277 while (DART.fPlaying)
278 DosSleep(1);
281 dartClose();
283 free(m_audioBuf);
286 // stop playing and empty buffers (for seeking/pause)
287 static void reset(void)
289 dartPause();
291 // Reset ring-buffer state
292 m_iBufReadPos = 0;
293 m_iBufWritePos = 0;
295 dartResume();
298 // stop playing, keep buffers (for pause)
299 static void audio_pause(void)
301 dartPause();
304 // resume playing, after audio_pause()
305 static void audio_resume(void)
307 dartResume();
310 // return: how many bytes can be played without blocking
311 static int get_space(void)
313 return buf_free();
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;