Mercurial > mplayer.hg
annotate libao2/ao_win32.c @ 20966:dc1a02e98cb6
clarify syncpoint placement recommanditions
(note i would also like to add a "there MUST be 1 syncpoint per second" rule)
author | michael |
---|---|
date | Fri, 17 Nov 2006 13:32:45 +0000 |
parents | 7cfd3a04d537 |
children | a124f3abc1ec |
rev | line source |
---|---|
7913 | 1 /****************************************************************************** |
2 * ao_win32.c: Windows waveOut interface for MPlayer | |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12146
diff
changeset
|
3 * Copyright (c) 2002 - 2004 Sascha Sommer <saschasommer@freenet.de>. |
7913 | 4 * |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
19614 | 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
7913 | 18 * |
19 *****************************************************************************/ | |
20 | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <windows.h> | |
24 #include <mmsystem.h> | |
25 | |
14479 | 26 #include "config.h" |
14245 | 27 #include "libaf/af_format.h" |
7913 | 28 #include "audio_out.h" |
29 #include "audio_out_internal.h" | |
14123 | 30 #include "mp_msg.h" |
31 #include "libvo/fastmemcpy.h" | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
32 #include "osdep/timer.h" |
7913 | 33 |
12699
02efc0619f61
10l WAVE_FORMAT_DOLBY_AC3_SPDIF needs to be defined first, patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12684
diff
changeset
|
34 #define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092 |
13017 | 35 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE |
36 | |
37 static const GUID KSDATAFORMAT_SUBTYPE_PCM = { | |
38 0x1,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71} | |
39 }; | |
40 | |
41 typedef struct { | |
42 WAVEFORMATEX Format; | |
43 union { | |
44 WORD wValidBitsPerSample; | |
45 WORD wSamplesPerBlock; | |
46 WORD wReserved; | |
47 } Samples; | |
48 DWORD dwChannelMask; | |
49 GUID SubFormat; | |
50 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE; | |
51 | |
52 #define SPEAKER_FRONT_LEFT 0x1 | |
53 #define SPEAKER_FRONT_RIGHT 0x2 | |
54 #define SPEAKER_FRONT_CENTER 0x4 | |
55 #define SPEAKER_LOW_FREQUENCY 0x8 | |
56 #define SPEAKER_BACK_LEFT 0x10 | |
57 #define SPEAKER_BACK_RIGHT 0x20 | |
58 #define SPEAKER_FRONT_LEFT_OF_CENTER 0x40 | |
59 #define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80 | |
60 #define SPEAKER_BACK_CENTER 0x100 | |
61 #define SPEAKER_SIDE_LEFT 0x200 | |
62 #define SPEAKER_SIDE_RIGHT 0x400 | |
63 #define SPEAKER_TOP_CENTER 0x800 | |
64 #define SPEAKER_TOP_FRONT_LEFT 0x1000 | |
65 #define SPEAKER_TOP_FRONT_CENTER 0x2000 | |
66 #define SPEAKER_TOP_FRONT_RIGHT 0x4000 | |
67 #define SPEAKER_TOP_BACK_LEFT 0x8000 | |
68 #define SPEAKER_TOP_BACK_CENTER 0x10000 | |
69 #define SPEAKER_TOP_BACK_RIGHT 0x20000 | |
70 | |
71 static const int channel_mask[] = { | |
72 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY, | |
73 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY, | |
74 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_CENTER | SPEAKER_LOW_FREQUENCY, | |
75 SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_LOW_FREQUENCY | |
76 }; | |
77 | |
78 | |
12699
02efc0619f61
10l WAVE_FORMAT_DOLBY_AC3_SPDIF needs to be defined first, patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12684
diff
changeset
|
79 |
12155
deb95ac21f14
restore original ringbuffer constants as the current values are causing heavy stutter here, especially noticable after moving the video window
faust3
parents:
12151
diff
changeset
|
80 #define SAMPLESIZE 1024 |
7913 | 81 #define BUFFER_SIZE 4096 |
12155
deb95ac21f14
restore original ringbuffer constants as the current values are causing heavy stutter here, especially noticable after moving the video window
faust3
parents:
12151
diff
changeset
|
82 #define BUFFER_COUNT 16 |
7913 | 83 |
84 | |
85 static WAVEHDR* waveBlocks; //pointer to our ringbuffer memory | |
86 static HWAVEOUT hWaveOut; //handle to the waveout device | |
87 static unsigned int buf_write=0; | |
88 static unsigned int buf_write_pos=0; | |
89 static int full_buffers=0; | |
90 static int buffered_bytes=0; | |
91 | |
92 | |
93 static ao_info_t info = | |
94 { | |
95 "Windows waveOut audio output", | |
96 "win32", | |
97 "Sascha Sommer <saschasommer@freenet.de>", | |
98 "" | |
99 }; | |
100 | |
101 LIBAO_EXTERN(win32) | |
102 | |
103 static void CALLBACK waveOutProc(HWAVEOUT hWaveOut,UINT uMsg,DWORD dwInstance, | |
104 DWORD dwParam1,DWORD dwParam2) | |
105 { | |
106 if(uMsg != WOM_DONE) | |
107 return; | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
108 if (full_buffers) { |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
109 buffered_bytes-=BUFFER_SIZE; |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
110 --full_buffers; |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
111 } else { |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
112 buffered_bytes=0; |
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
113 } |
7913 | 114 } |
115 | |
116 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
9589
diff
changeset
|
117 static int control(int cmd,void *arg) |
7913 | 118 { |
119 DWORD volume; | |
120 switch (cmd) | |
121 { | |
122 case AOCONTROL_GET_VOLUME: | |
123 { | |
124 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
125 waveOutGetVolume(hWaveOut,&volume); | |
126 vol->left = (float)(LOWORD(volume)/655.35); | |
127 vol->right = (float)(HIWORD(volume)/655.35); | |
128 mp_msg(MSGT_AO, MSGL_DBG2,"ao_win32: volume left:%f volume right:%f\n",vol->left,vol->right); | |
129 return CONTROL_OK; | |
130 } | |
131 case AOCONTROL_SET_VOLUME: | |
132 { | |
133 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
134 volume = MAKELONG(vol->left*655.35,vol->right*655.35); | |
135 waveOutSetVolume(hWaveOut,volume); | |
136 return CONTROL_OK; | |
137 } | |
138 } | |
139 return -1; | |
140 } | |
141 | |
142 // open & setup audio device | |
143 // return: 1=success 0=fail | |
144 static int init(int rate,int channels,int format,int flags) | |
145 { | |
13017 | 146 WAVEFORMATEXTENSIBLE wformat; |
7913 | 147 DWORD totalBufferSize = (BUFFER_SIZE + sizeof(WAVEHDR)) * BUFFER_COUNT; |
148 MMRESULT result; | |
149 unsigned char* buffer; | |
150 int i; | |
151 | |
14135 | 152 switch(format){ |
14245 | 153 case AF_FORMAT_AC3: |
154 case AF_FORMAT_S24_LE: | |
155 case AF_FORMAT_S16_LE: | |
156 case AF_FORMAT_S8: | |
14135 | 157 break; |
158 default: | |
14264 | 159 mp_msg(MSGT_AO, MSGL_V,"ao_win32: format %s not supported defaulting to Signed 16-bit Little-Endian\n",af_fmt2str_short(format)); |
14245 | 160 format=AF_FORMAT_S16_LE; |
14135 | 161 } |
19835
807cdfafa0ce
disable multichannel mode, it never worked reliable
faust3
parents:
19614
diff
changeset
|
162 |
807cdfafa0ce
disable multichannel mode, it never worked reliable
faust3
parents:
19614
diff
changeset
|
163 // FIXME multichannel mode is buggy |
807cdfafa0ce
disable multichannel mode, it never worked reliable
faust3
parents:
19614
diff
changeset
|
164 if(channels > 2) |
807cdfafa0ce
disable multichannel mode, it never worked reliable
faust3
parents:
19614
diff
changeset
|
165 channels = 2; |
807cdfafa0ce
disable multichannel mode, it never worked reliable
faust3
parents:
19614
diff
changeset
|
166 |
7913 | 167 //fill global ao_data |
168 ao_data.channels=channels; | |
169 ao_data.samplerate=rate; | |
170 ao_data.format=format; | |
171 ao_data.bps=channels*rate; | |
14245 | 172 if(format != AF_FORMAT_U8 && format != AF_FORMAT_S8) |
7913 | 173 ao_data.bps*=2; |
174 if(ao_data.buffersize==-1) | |
175 { | |
14264 | 176 ao_data.buffersize=af_fmt2bits(format)/8; |
7913 | 177 ao_data.buffersize*= channels; |
178 ao_data.buffersize*= SAMPLESIZE; | |
179 } | |
14264 | 180 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Samplerate:%iHz Channels:%i Format:%s\n",rate, channels, af_fmt2str_short(format)); |
7913 | 181 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Buffersize:%d\n",ao_data.buffersize); |
182 | |
183 //fill waveformatex | |
13017 | 184 ZeroMemory( &wformat, sizeof(WAVEFORMATEXTENSIBLE)); |
13735
83c5f9888576
ringbuffer variable intialization fix for multifile playback patch by Rune Petersen <rune.mail-list at mail.tele.dk>
faust3
parents:
13017
diff
changeset
|
185 wformat.Format.cbSize = (channels>2)?sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX):0; |
13017 | 186 wformat.Format.nChannels = channels; |
187 wformat.Format.nSamplesPerSec = rate; | |
14245 | 188 if(format == AF_FORMAT_AC3) |
12684
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
189 { |
13017 | 190 wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF; |
191 wformat.Format.wBitsPerSample = 16; | |
192 wformat.Format.nBlockAlign = 4; | |
12684
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
193 } |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
194 else |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
195 { |
13017 | 196 wformat.Format.wFormatTag = (channels>2)?WAVE_FORMAT_EXTENSIBLE:WAVE_FORMAT_PCM; |
14264 | 197 wformat.Format.wBitsPerSample = af_fmt2bits(format); |
13017 | 198 wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3); |
199 } | |
200 if(channels>2) | |
201 { | |
202 wformat.dwChannelMask = channel_mask[channels-3]; | |
203 wformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; | |
14264 | 204 wformat.Samples.wValidBitsPerSample=af_fmt2bits(format); |
13017 | 205 } |
206 | |
207 wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign; | |
7913 | 208 |
209 //open sound device | |
210 //WAVE_MAPPER always points to the default wave device on the system | |
14529
cc9c088305d9
WAVE_FORMAT_DIRECT seems to cause problems with certain os/driver combinations and seems to be useless anyway
faust3
parents:
14503
diff
changeset
|
211 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); |
7913 | 212 if(result == WAVERR_BADFORMAT) |
213 { | |
214 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: format not supported switching to default\n"); | |
13017 | 215 ao_data.channels = wformat.Format.nChannels = 2; |
216 ao_data.samplerate = wformat.Format.nSamplesPerSec = 44100; | |
14245 | 217 ao_data.format = AF_FORMAT_S16_LE; |
13017 | 218 ao_data.bps=ao_data.channels * ao_data.samplerate*2; |
219 wformat.Format.wBitsPerSample=16; | |
220 wformat.Format.wFormatTag=WAVE_FORMAT_PCM; | |
221 wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3); | |
222 wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign; | |
223 ao_data.buffersize=(wformat.Format.wBitsPerSample>>3)*wformat.Format.nChannels*SAMPLESIZE; | |
224 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); | |
7913 | 225 } |
226 if(result != MMSYSERR_NOERROR) | |
227 { | |
14503 | 228 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: unable to open wave mapper device (result=%i)\n",result); |
7913 | 229 return 0; |
230 } | |
231 //allocate buffer memory as one big block | |
232 buffer = malloc(totalBufferSize); | |
9589 | 233 memset(buffer,0x0,totalBufferSize); |
7913 | 234 //and setup pointers to each buffer |
235 waveBlocks = (WAVEHDR*)buffer; | |
236 buffer += sizeof(WAVEHDR) * BUFFER_COUNT; | |
237 for(i = 0; i < BUFFER_COUNT; i++) { | |
238 waveBlocks[i].lpData = buffer; | |
239 buffer += BUFFER_SIZE; | |
240 } | |
13735
83c5f9888576
ringbuffer variable intialization fix for multifile playback patch by Rune Petersen <rune.mail-list at mail.tele.dk>
faust3
parents:
13017
diff
changeset
|
241 buf_write=0; |
83c5f9888576
ringbuffer variable intialization fix for multifile playback patch by Rune Petersen <rune.mail-list at mail.tele.dk>
faust3
parents:
13017
diff
changeset
|
242 buf_write_pos=0; |
83c5f9888576
ringbuffer variable intialization fix for multifile playback patch by Rune Petersen <rune.mail-list at mail.tele.dk>
faust3
parents:
13017
diff
changeset
|
243 full_buffers=0; |
83c5f9888576
ringbuffer variable intialization fix for multifile playback patch by Rune Petersen <rune.mail-list at mail.tele.dk>
faust3
parents:
13017
diff
changeset
|
244 buffered_bytes=0; |
7913 | 245 |
246 return 1; | |
247 } | |
248 | |
249 // close audio device | |
12145 | 250 static void uninit(int immed) |
7913 | 251 { |
12146 | 252 if(!immed)while(buffered_bytes > 0)usec_sleep(50000); |
253 else buffered_bytes=0; | |
7913 | 254 waveOutReset(hWaveOut); |
255 waveOutClose(hWaveOut); | |
256 mp_msg(MSGT_AO, MSGL_V,"waveOut device closed\n"); | |
257 free(waveBlocks); | |
258 mp_msg(MSGT_AO, MSGL_V,"buffer memory freed\n"); | |
259 } | |
260 | |
261 // stop playing and empty buffers (for seeking/pause) | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
14529
diff
changeset
|
262 static void reset(void) |
7913 | 263 { |
264 waveOutReset(hWaveOut); | |
265 buf_write=0; | |
266 buf_write_pos=0; | |
267 full_buffers=0; | |
268 buffered_bytes=0; | |
269 } | |
270 | |
271 // stop playing, keep buffers (for pause) | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
14529
diff
changeset
|
272 static void audio_pause(void) |
7913 | 273 { |
274 waveOutPause(hWaveOut); | |
275 } | |
276 | |
277 // resume playing, after audio_pause() | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
14529
diff
changeset
|
278 static void audio_resume(void) |
7913 | 279 { |
280 waveOutRestart(hWaveOut); | |
281 } | |
282 | |
283 // return: how many bytes can be played without blocking | |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
14529
diff
changeset
|
284 static int get_space(void) |
7913 | 285 { |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
286 return BUFFER_COUNT*BUFFER_SIZE - buffered_bytes; |
7913 | 287 } |
288 | |
289 //writes data into buffer, based on ringbuffer code in ao_sdl.c | |
290 static int write_waveOutBuffer(unsigned char* data,int len){ | |
291 WAVEHDR* current; | |
292 int len2=0; | |
293 int x; | |
294 while(len>0){ | |
295 current = &waveBlocks[buf_write]; | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
296 if(buffered_bytes==BUFFER_COUNT*BUFFER_SIZE) break; |
7913 | 297 //unprepare the header if it is prepared |
298 if(current->dwFlags & WHDR_PREPARED) | |
299 waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR)); | |
300 x=BUFFER_SIZE-buf_write_pos; | |
301 if(x>len) x=len; | |
302 memcpy(current->lpData+buf_write_pos,data+len2,x); | |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12146
diff
changeset
|
303 if(buf_write_pos==0)full_buffers++; |
7913 | 304 len2+=x; len-=x; |
305 buffered_bytes+=x; buf_write_pos+=x; | |
306 //prepare header and write data to device | |
12093
f54d02f6ddbf
let uninit wait until sound is completely played, don't restore volume at exit, fixed ringbuffer bug, patch by Nehal <nehalmistry at gmx.net>\n
faust3
parents:
11511
diff
changeset
|
307 current->dwBufferLength = buf_write_pos; |
7913 | 308 waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR)); |
309 waveOutWrite(hWaveOut, current, sizeof(WAVEHDR)); | |
310 | |
311 if(buf_write_pos>=BUFFER_SIZE){ //buffer is full find next | |
312 // block is full, find next! | |
313 buf_write=(buf_write+1)%BUFFER_COUNT; | |
314 buf_write_pos=0; | |
315 } | |
316 } | |
317 return len2; | |
318 } | |
319 | |
320 // plays 'len' bytes of 'data' | |
321 // it should round it down to outburst*n | |
322 // return: number of bytes played | |
323 static int play(void* data,int len,int flags) | |
324 { | |
20250
7cfd3a04d537
Implement AOPLAY_FINAL_CHUNK support for dshow and win32 out.
reimar
parents:
19835
diff
changeset
|
325 if (!(flags & AOPLAY_FINAL_CHUNK)) |
12151
75fdb659f5bf
round len to outburst and increment full_buffers at the correct time, patch by Nehal <nehalmistry at gmx.net>
faust3
parents:
12146
diff
changeset
|
326 len = (len/ao_data.outburst)*ao_data.outburst; |
7913 | 327 return write_waveOutBuffer(data,len); |
328 } | |
10106 | 329 |
7913 | 330 // return: delay in seconds between first and last sample in buffer |
18915
99e20a22d5d0
modifies function declarations without parameters from ()
reynaldo
parents:
14529
diff
changeset
|
331 static float get_delay(void) |
7913 | 332 { |
333 return (float)(buffered_bytes + ao_data.buffersize)/(float)ao_data.bps; | |
334 } |