Mercurial > mplayer.hg
annotate libao2/ao_win32.c @ 12737:ac56419ba6db
We still need to make sure the upper 16 bits of dwFlags are cleared
author | ranma |
---|---|
date | Wed, 30 Jun 2004 22:50:56 +0000 |
parents | 02efc0619f61 |
children | ab369fe35121 |
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 | |
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. | |
18 * | |
19 *****************************************************************************/ | |
20 | |
21 #include <stdio.h> | |
22 #include <stdlib.h> | |
23 #include <windows.h> | |
24 #include <mmsystem.h> | |
25 | |
26 #include "afmt.h" | |
27 #include "audio_out.h" | |
28 #include "audio_out_internal.h" | |
29 #include "../mp_msg.h" | |
30 #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
|
31 #include "osdep/timer.h" |
7913 | 32 |
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
|
33 #define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092 |
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 |
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
|
35 #define SAMPLESIZE 1024 |
7913 | 36 #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
|
37 #define BUFFER_COUNT 16 |
7913 | 38 |
39 | |
40 static WAVEHDR* waveBlocks; //pointer to our ringbuffer memory | |
41 static HWAVEOUT hWaveOut; //handle to the waveout device | |
42 static unsigned int buf_write=0; | |
43 static unsigned int buf_write_pos=0; | |
44 static int full_buffers=0; | |
45 static int buffered_bytes=0; | |
46 | |
47 | |
48 static ao_info_t info = | |
49 { | |
50 "Windows waveOut audio output", | |
51 "win32", | |
52 "Sascha Sommer <saschasommer@freenet.de>", | |
53 "" | |
54 }; | |
55 | |
56 LIBAO_EXTERN(win32) | |
57 | |
58 static void CALLBACK waveOutProc(HWAVEOUT hWaveOut,UINT uMsg,DWORD dwInstance, | |
59 DWORD dwParam1,DWORD dwParam2) | |
60 { | |
61 if(uMsg != WOM_DONE) | |
62 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
|
63 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
|
64 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
|
65 --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
|
66 } 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
|
67 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
|
68 } |
7913 | 69 } |
70 | |
71 // to set/get/query special features/parameters | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
9589
diff
changeset
|
72 static int control(int cmd,void *arg) |
7913 | 73 { |
74 DWORD volume; | |
75 switch (cmd) | |
76 { | |
77 case AOCONTROL_GET_VOLUME: | |
78 { | |
79 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
80 waveOutGetVolume(hWaveOut,&volume); | |
81 vol->left = (float)(LOWORD(volume)/655.35); | |
82 vol->right = (float)(HIWORD(volume)/655.35); | |
83 mp_msg(MSGT_AO, MSGL_DBG2,"ao_win32: volume left:%f volume right:%f\n",vol->left,vol->right); | |
84 return CONTROL_OK; | |
85 } | |
86 case AOCONTROL_SET_VOLUME: | |
87 { | |
88 ao_control_vol_t* vol = (ao_control_vol_t*)arg; | |
89 volume = MAKELONG(vol->left*655.35,vol->right*655.35); | |
90 waveOutSetVolume(hWaveOut,volume); | |
91 return CONTROL_OK; | |
92 } | |
93 } | |
94 return -1; | |
95 } | |
96 | |
97 // open & setup audio device | |
98 // return: 1=success 0=fail | |
99 static int init(int rate,int channels,int format,int flags) | |
100 { | |
101 WAVEFORMATEX wformat; | |
102 DWORD totalBufferSize = (BUFFER_SIZE + sizeof(WAVEHDR)) * BUFFER_COUNT; | |
103 MMRESULT result; | |
104 unsigned char* buffer; | |
105 int i; | |
106 | |
107 //fill global ao_data | |
108 ao_data.channels=channels; | |
109 ao_data.samplerate=rate; | |
110 ao_data.format=format; | |
111 ao_data.bps=channels*rate; | |
112 if(format != AFMT_U8 && format != AFMT_S8) | |
113 ao_data.bps*=2; | |
114 if(ao_data.buffersize==-1) | |
115 { | |
116 ao_data.buffersize=audio_out_format_bits(format)/8; | |
117 ao_data.buffersize*= channels; | |
118 ao_data.buffersize*= SAMPLESIZE; | |
119 } | |
120 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Samplerate:%iHz Channels:%i Format:%s\n",rate, channels, audio_out_format_name(format)); | |
121 mp_msg(MSGT_AO, MSGL_V,"ao_win32: Buffersize:%d\n",ao_data.buffersize); | |
122 | |
123 //fill waveformatex | |
124 ZeroMemory( &wformat, sizeof(WAVEFORMATEX)); | |
125 wformat.cbSize = 0; /* size of _extra_ info */ | |
126 wformat.nChannels = channels; | |
127 wformat.nSamplesPerSec = rate; | |
12684
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
128 if(format == AFMT_AC3) |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
129 { |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
130 wformat.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF; |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
131 wformat.wBitsPerSample = 16; |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
132 wformat.nBlockAlign = 4; |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
133 } |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
134 else |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
135 { |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
136 wformat.wFormatTag = WAVE_FORMAT_PCM; |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
137 wformat.wBitsPerSample = audio_out_format_bits(format); |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
138 wformat.nBlockAlign = wformat.nChannels * (wformat.wBitsPerSample >> 3); |
47598beff93a
ac3 passthrough, initial patch by Gianluigi Tiesi <sherpya at netfarm.it>
faust3
parents:
12155
diff
changeset
|
139 } |
7913 | 140 wformat.nAvgBytesPerSec = wformat.nSamplesPerSec * wformat.nBlockAlign; |
141 | |
142 //open sound device | |
143 //WAVE_MAPPER always points to the default wave device on the system | |
144 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); | |
145 if(result == WAVERR_BADFORMAT) | |
146 { | |
147 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: format not supported switching to default\n"); | |
148 ao_data.channels = wformat.nChannels = 2; | |
149 ao_data.samplerate = wformat.nSamplesPerSec = 44100; | |
150 ao_data.format = AFMT_S16_LE; | |
11511 | 151 ao_data.bps=ao_data.channels * ao_data.samplerate*2; |
7913 | 152 ao_data.buffersize=wformat.wBitsPerSample=16; |
153 wformat.nBlockAlign = wformat.nChannels * (wformat.wBitsPerSample >> 3); | |
154 wformat.nAvgBytesPerSec = wformat.nSamplesPerSec * wformat.nBlockAlign; | |
155 ao_data.buffersize/=8; | |
156 ao_data.buffersize*= ao_data.channels; | |
157 ao_data.buffersize*= SAMPLESIZE; | |
158 result = waveOutOpen(&hWaveOut,WAVE_MAPPER,&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); | |
159 } | |
160 if(result != MMSYSERR_NOERROR) | |
161 { | |
162 mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: unable to open wave mapper device\n"); | |
163 return 0; | |
164 } | |
165 //save volume | |
166 //allocate buffer memory as one big block | |
167 buffer = malloc(totalBufferSize); | |
9589 | 168 memset(buffer,0x0,totalBufferSize); |
7913 | 169 //and setup pointers to each buffer |
170 waveBlocks = (WAVEHDR*)buffer; | |
171 buffer += sizeof(WAVEHDR) * BUFFER_COUNT; | |
172 for(i = 0; i < BUFFER_COUNT; i++) { | |
173 waveBlocks[i].lpData = buffer; | |
174 buffer += BUFFER_SIZE; | |
175 } | |
176 | |
177 return 1; | |
178 } | |
179 | |
180 // close audio device | |
12145 | 181 static void uninit(int immed) |
7913 | 182 { |
12146 | 183 if(!immed)while(buffered_bytes > 0)usec_sleep(50000); |
184 else buffered_bytes=0; | |
7913 | 185 waveOutReset(hWaveOut); |
186 waveOutClose(hWaveOut); | |
187 mp_msg(MSGT_AO, MSGL_V,"waveOut device closed\n"); | |
188 free(waveBlocks); | |
189 mp_msg(MSGT_AO, MSGL_V,"buffer memory freed\n"); | |
190 } | |
191 | |
192 // stop playing and empty buffers (for seeking/pause) | |
193 static void reset() | |
194 { | |
195 waveOutReset(hWaveOut); | |
196 buf_write=0; | |
197 buf_write_pos=0; | |
198 full_buffers=0; | |
199 buffered_bytes=0; | |
200 } | |
201 | |
202 // stop playing, keep buffers (for pause) | |
203 static void audio_pause() | |
204 { | |
205 waveOutPause(hWaveOut); | |
206 } | |
207 | |
208 // resume playing, after audio_pause() | |
209 static void audio_resume() | |
210 { | |
211 waveOutRestart(hWaveOut); | |
212 } | |
213 | |
214 // return: how many bytes can be played without blocking | |
215 static int get_space() | |
216 { | |
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
|
217 return BUFFER_COUNT*BUFFER_SIZE - buffered_bytes; |
7913 | 218 } |
219 | |
220 //writes data into buffer, based on ringbuffer code in ao_sdl.c | |
221 static int write_waveOutBuffer(unsigned char* data,int len){ | |
222 WAVEHDR* current; | |
223 int len2=0; | |
224 int x; | |
225 while(len>0){ | |
226 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
|
227 if(buffered_bytes==BUFFER_COUNT*BUFFER_SIZE) break; |
7913 | 228 //unprepare the header if it is prepared |
229 if(current->dwFlags & WHDR_PREPARED) | |
230 waveOutUnprepareHeader(hWaveOut, current, sizeof(WAVEHDR)); | |
231 x=BUFFER_SIZE-buf_write_pos; | |
232 if(x>len) x=len; | |
233 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
|
234 if(buf_write_pos==0)full_buffers++; |
7913 | 235 len2+=x; len-=x; |
236 buffered_bytes+=x; buf_write_pos+=x; | |
237 //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
|
238 current->dwBufferLength = buf_write_pos; |
7913 | 239 waveOutPrepareHeader(hWaveOut, current, sizeof(WAVEHDR)); |
240 waveOutWrite(hWaveOut, current, sizeof(WAVEHDR)); | |
241 | |
242 if(buf_write_pos>=BUFFER_SIZE){ //buffer is full find next | |
243 // block is full, find next! | |
244 buf_write=(buf_write+1)%BUFFER_COUNT; | |
245 buf_write_pos=0; | |
246 } | |
247 } | |
248 return len2; | |
249 } | |
250 | |
251 // plays 'len' bytes of 'data' | |
252 // it should round it down to outburst*n | |
253 // return: number of bytes played | |
254 static int play(void* data,int len,int flags) | |
255 { | |
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
|
256 len = (len/ao_data.outburst)*ao_data.outburst; |
7913 | 257 return write_waveOutBuffer(data,len); |
258 } | |
10106 | 259 |
7913 | 260 // return: delay in seconds between first and last sample in buffer |
261 static float get_delay() | |
262 { | |
263 return (float)(buffered_bytes + ao_data.buffersize)/(float)ao_data.bps; | |
264 } |