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"
41 #include "libavutil/fifo.h"
43 static const ao_info_t info
= {
46 "KO Myung-Hun <komh@chollian.net>",
52 #define OUTBURST_SAMPLES 512
53 #define DEFAULT_DART_SAMPLES (OUTBURST_SAMPLES << 2)
55 #define CHUNK_SIZE ao_data.outburst
57 static AVFifoBuffer
*m_audioBuf
;
59 static int m_nBufSize
= 0;
61 static volatile int m_fQuit
= FALSE
;
63 static int write_buffer(unsigned char *data
, int len
)
65 int nFree
= av_fifo_space(m_audioBuf
);
70 return av_fifo_generic_write(m_audioBuf
, data
, len
, NULL
);
73 static int read_buffer(unsigned char *data
, int len
)
75 int nBuffered
= av_fifo_size(m_audioBuf
);
80 av_fifo_generic_read(m_audioBuf
, data
, len
, NULL
);
84 // end ring buffer stuff
86 static ULONG APIENTRY
dart_audio_callback(PVOID pCBData
, PVOID pBuffer
,
91 nReadLen
= read_buffer(pBuffer
, ulSize
);
92 if (nReadLen
< ulSize
&& !m_fQuit
) {
93 memset((uint8_t *)pBuffer
+ nReadLen
, DART
.bSilence
, ulSize
- nReadLen
);
100 // to set/get/query special features/parameters
101 static int control(int cmd
, void *arg
)
104 case AOCONTROL_GET_VOLUME
:
106 ao_control_vol_t
*vol
= arg
;
108 vol
->left
= vol
->right
= LOUSHORT(dartGetVolume());
113 case AOCONTROL_SET_VOLUME
:
116 ao_control_vol_t
*vol
= arg
;
118 mid
= (vol
->left
+ vol
->right
) / 2;
119 dartSetVolume(MCI_SET_AUDIO_ALL
, mid
);
125 return CONTROL_UNKNOWN
;
128 static void print_help(void)
130 mp_msg(MSGT_AO
, MSGL_FATAL
,
131 "\n-ao dart commandline help:\n"
132 "Example: mplayer -ao dart:noshare\n"
133 " open DART in exclusive mode\n"
136 " Open DART in shareable or exclusive mode\n"
138 " Set buffer size to <size> in samples(default: 2048)\n");
141 // open & set up audio device
142 // return: 1=success 0=fail
143 static int init(int rate
, int channels
, int format
, int flags
)
146 int nDartSamples
= DEFAULT_DART_SAMPLES
;
149 const opt_t subopts
[] = {
150 {"share", OPT_ARG_BOOL
, &fShare
, NULL
},
151 {"bufsize", OPT_ARG_INT
, &nDartSamples
, int_non_neg
},
155 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
161 nDartSamples
= DEFAULT_DART_SAMPLES
;
163 mp_msg(MSGT_AO
, MSGL_V
, "DART: opened in %s mode, buffer size = %d sample(s)\n",
164 fShare
? "shareable" : "exclusive", nDartSamples
);
167 case AF_FORMAT_S16_LE
:
172 format
= AF_FORMAT_S16_LE
;
173 mp_msg(MSGT_AO
, MSGL_V
, "DART: format %s not supported defaulting to Signed 16-bit Little-Endian\n",
174 af_fmt2str_short(format
));
178 nBytesPerSample
= (af_fmt2bits(format
) >> 3) * channels
;
180 if (dartInit(0, af_fmt2bits(format
), rate
, MCI_WAVE_FORMAT_PCM
, channels
,
181 2, nBytesPerSample
* nDartSamples
, fShare
,
182 dart_audio_callback
, NULL
))
185 mp_msg(MSGT_AO
, MSGL_V
, "DART: obtained buffer size = %lu bytes\n",
190 ao_data
.channels
= channels
;
191 ao_data
.samplerate
= rate
;
192 ao_data
.format
= format
;
193 ao_data
.bps
= nBytesPerSample
* rate
;
194 ao_data
.outburst
= nBytesPerSample
* OUTBURST_SAMPLES
;
195 ao_data
.buffersize
= DART
.ulBufferSize
;
197 // multiple of CHUNK_SIZE
198 m_nBufSize
= ((DART
.ulBufferSize
<< 2) / CHUNK_SIZE
) * CHUNK_SIZE
;
199 // and one more chunk plus round up
200 m_nBufSize
+= 2 * CHUNK_SIZE
;
202 m_audioBuf
= av_fifo_alloc(m_nBufSize
);
206 // might cause PM DLLs to be loaded which incorrectly enable SIG_FPE,
207 // which AAC decoding might trigger.
208 // so, mask off all floating-point exceptions.
209 _control87(MCW_EM
, MCW_EM
);
214 // close audio device
215 static void uninit(int immed
)
220 while (DART
.fPlaying
)
226 av_fifo_free(m_audioBuf
);
229 // stop playing and empty buffers (for seeking/pause)
230 static void reset(void)
234 // Reset ring-buffer state
235 av_fifo_reset(m_audioBuf
);
240 // stop playing, keep buffers (for pause)
241 static void audio_pause(void)
246 // resume playing, after audio_pause()
247 static void audio_resume(void)
252 // return: how many bytes can be played without blocking
253 static int get_space(void)
255 return av_fifo_space(m_audioBuf
);
258 // plays 'len' bytes of 'data'
259 // it should round it down to outburst*n
260 // return: number of bytes played
261 static int play(void *data
, int len
, int flags
)
264 if (!(flags
& AOPLAY_FINAL_CHUNK
))
265 len
= (len
/ ao_data
.outburst
) * ao_data
.outburst
;
267 return write_buffer(data
, len
);
270 // return: delay in seconds between first and last sample in buffer
271 static float get_delay(void)
273 int nBuffered
= av_fifo_size(m_audioBuf
); // could be less
275 return (float)nBuffered
/ (float)ao_data
.bps
;