1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1998 Patrik Stridvall
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * + asynchronous conversion is not implemented
26 * + callback/notification
28 * + properly close ACM streams
36 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
46 static PWINE_ACMSTREAM ACM_GetStream(HACMSTREAM has)
50 return (PWINE_ACMSTREAM)has;
53 /***********************************************************************
54 * acmStreamClose (MSACM32.@)
56 MMRESULT WINAPI acmStreamClose(HACMSTREAM has, DWORD fdwClose)
61 TRACE("(%p, %ld)\n", has, fdwClose);
63 if ((was = ACM_GetStream(has)) == NULL) {
64 WARN("invalid handle\n");
65 return MMSYSERR_INVALHANDLE;
67 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_CLOSE, (LPARAM)&was->drvInst, 0);
68 if (ret == MMSYSERR_NOERROR) {
70 acmDriverClose(was->hAcmDriver, 0L);
71 HeapFree(MSACM_hHeap, 0, was);
73 TRACE("=> (%d)\n", ret);
77 /***********************************************************************
78 * acmStreamConvert (MSACM32.@)
80 MMRESULT WINAPI acmStreamConvert(HACMSTREAM has, PACMSTREAMHEADER pash,
84 MMRESULT ret = MMSYSERR_NOERROR;
85 PACMDRVSTREAMHEADER padsh;
87 TRACE("(%p, %p, %ld)\n", has, pash, fdwConvert);
89 if ((was = ACM_GetStream(has)) == NULL) {
90 WARN("invalid handle\n");
91 return MMSYSERR_INVALHANDLE;
93 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
94 WARN("invalid parameter\n");
95 return MMSYSERR_INVALPARAM;
97 if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
98 WARN("unprepared header\n");
99 return ACMERR_UNPREPARED;
102 pash->cbSrcLengthUsed = 0;
103 pash->cbDstLengthUsed = 0;
105 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
106 * size. some fields are private to msacm internals, and are exposed
107 * in ACMSTREAMHEADER in the dwReservedDriver array
109 padsh = (PACMDRVSTREAMHEADER)pash;
111 /* check that pointers have not been modified */
112 if (padsh->pbPreparedSrc != padsh->pbSrc ||
113 padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
114 padsh->pbPreparedDst != padsh->pbDst ||
115 padsh->cbPreparedDstLength < padsh->cbDstLength) {
116 WARN("invalid parameter\n");
117 return MMSYSERR_INVALPARAM;
120 padsh->fdwConvert = fdwConvert;
122 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_CONVERT, (LPARAM)&was->drvInst, (LPARAM)padsh);
123 if (ret == MMSYSERR_NOERROR) {
124 padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_DONE;
126 TRACE("=> (%d)\n", ret);
130 /***********************************************************************
131 * acmStreamMessage (MSACM32.@)
133 MMRESULT WINAPI acmStreamMessage(HACMSTREAM has, UINT uMsg, LPARAM lParam1,
136 FIXME("(%p, %u, %ld, %ld): stub\n", has, uMsg, lParam1, lParam2);
137 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
138 return MMSYSERR_ERROR;
141 /***********************************************************************
142 * acmStreamOpen (MSACM32.@)
144 MMRESULT WINAPI acmStreamOpen(PHACMSTREAM phas, HACMDRIVER had, PWAVEFORMATEX pwfxSrc,
145 PWAVEFORMATEX pwfxDst, PWAVEFILTER pwfltr, DWORD dwCallback,
146 DWORD dwInstance, DWORD fdwOpen)
153 WAVEFORMATEX wfxSrc, wfxDst;
155 TRACE("(%p, %p, %p, %p, %p, %ld, %ld, %ld)\n",
156 phas, had, pwfxSrc, pwfxDst, pwfltr, dwCallback, dwInstance, fdwOpen);
158 /* NOTE: pwfxSrc and/or pwfxDst can point to a structure smaller than
159 * WAVEFORMATEX so don't use them directly when not sure */
160 if (pwfxSrc->wFormatTag == WAVE_FORMAT_PCM) {
161 memcpy(&wfxSrc, pwfxSrc, sizeof(PCMWAVEFORMAT));
162 wfxSrc.wBitsPerSample = pwfxSrc->wBitsPerSample;
167 if (pwfxDst->wFormatTag == WAVE_FORMAT_PCM) {
168 memcpy(&wfxDst, pwfxDst, sizeof(PCMWAVEFORMAT));
169 wfxDst.wBitsPerSample = pwfxDst->wBitsPerSample;
174 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%lu, nAvgBytesPerSec=%lu, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
175 pwfxSrc->wFormatTag, pwfxSrc->nChannels, pwfxSrc->nSamplesPerSec, pwfxSrc->nAvgBytesPerSec,
176 pwfxSrc->nBlockAlign, pwfxSrc->wBitsPerSample, pwfxSrc->cbSize);
178 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%lu, nAvgBytesPerSec=%lu, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
179 pwfxDst->wFormatTag, pwfxDst->nChannels, pwfxDst->nSamplesPerSec, pwfxDst->nAvgBytesPerSec,
180 pwfxDst->nBlockAlign, pwfxDst->wBitsPerSample, pwfxDst->cbSize);
182 /* (WS) In query mode, phas should be NULL. If it is not, then instead
183 * of returning an error we are making sure it is NULL, preventing some
184 * applications that pass garbage for phas from crashing.
186 if (fdwOpen & ACM_STREAMOPENF_QUERY) phas = NULL;
188 if (pwfltr && (pwfxSrc->wFormatTag != pwfxDst->wFormatTag)) {
189 WARN("invalid parameter\n");
190 return MMSYSERR_INVALPARAM;
193 wfxSrcSize = wfxDstSize = sizeof(WAVEFORMATEX);
194 if (pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) wfxSrcSize += pwfxSrc->cbSize;
195 if (pwfxDst->wFormatTag != WAVE_FORMAT_PCM) wfxDstSize += pwfxDst->cbSize;
197 was = HeapAlloc(MSACM_hHeap, 0, sizeof(*was) + wfxSrcSize + wfxDstSize +
198 ((pwfltr) ? sizeof(WAVEFILTER) : 0));
201 return MMSYSERR_NOMEM;
204 was->drvInst.cbStruct = sizeof(was->drvInst);
205 was->drvInst.pwfxSrc = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was));
206 memcpy(was->drvInst.pwfxSrc, pwfxSrc, wfxSrcSize);
207 was->drvInst.pwfxDst = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was) + wfxSrcSize);
208 memcpy(was->drvInst.pwfxDst, pwfxDst, wfxDstSize);
210 was->drvInst.pwfltr = (PWAVEFILTER)((LPSTR)was + sizeof(*was) + wfxSrcSize + wfxDstSize);
211 memcpy(was->drvInst.pwfltr, pwfltr, sizeof(WAVEFILTER));
213 was->drvInst.pwfltr = NULL;
215 was->drvInst.dwCallback = dwCallback;
216 was->drvInst.dwInstance = dwInstance;
217 was->drvInst.fdwOpen = fdwOpen;
218 was->drvInst.fdwDriver = 0L;
219 was->drvInst.dwDriver = 0L;
220 /* real value will be stored once ACMDM_STREAM_OPEN succeeds */
221 was->drvInst.has = 0L;
224 if (!(wad = MSACM_GetDriver(had))) {
225 ret = MMSYSERR_INVALPARAM;
229 was->obj.dwType = WINE_ACMOBJ_STREAM;
230 was->obj.pACMDriverID = wad->obj.pACMDriverID;
232 was->hAcmDriver = 0; /* not to close it in acmStreamClose */
234 ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
235 if (ret != MMSYSERR_NOERROR)
238 PWINE_ACMDRIVERID wadi;
240 ret = ACMERR_NOTPOSSIBLE;
241 for (wadi = MSACM_pFirstACMDriverID; wadi; wadi = wadi->pNextACMDriverID) {
242 if ((wadi->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ||
243 !MSACM_FindFormatTagInCache(wadi, pwfxSrc->wFormatTag, NULL) ||
244 !MSACM_FindFormatTagInCache(wadi, pwfxDst->wFormatTag, NULL))
246 ret = acmDriverOpen(&had, (HACMDRIVERID)wadi, 0L);
247 if (ret != MMSYSERR_NOERROR)
249 if ((wad = MSACM_GetDriver(had)) != 0) {
250 was->obj.dwType = WINE_ACMOBJ_STREAM;
251 was->obj.pACMDriverID = wad->obj.pACMDriverID;
253 was->hAcmDriver = had;
255 ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
256 TRACE("%s => %08x\n", debugstr_w(wadi->pszDriverAlias), ret);
257 if (ret == MMSYSERR_NOERROR) {
258 if (fdwOpen & ACM_STREAMOPENF_QUERY) {
259 acmDriverClose(had, 0L);
264 /* no match, close this acm driver and try next one */
265 acmDriverClose(had, 0L);
267 if (ret != MMSYSERR_NOERROR) {
268 ret = ACMERR_NOTPOSSIBLE;
272 ret = MMSYSERR_NOERROR;
273 was->drvInst.has = (HACMSTREAM)was;
274 if (!(fdwOpen & ACM_STREAMOPENF_QUERY)) {
276 *phas = (HACMSTREAM)was;
277 TRACE("=> (%d)\n", ret);
283 HeapFree(MSACM_hHeap, 0, was);
284 TRACE("=> (%d)\n", ret);
289 /***********************************************************************
290 * acmStreamPrepareHeader (MSACM32.@)
292 MMRESULT WINAPI acmStreamPrepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
296 MMRESULT ret = MMSYSERR_NOERROR;
297 PACMDRVSTREAMHEADER padsh;
299 TRACE("(%p, %p, %ld)\n", has, pash, fdwPrepare);
301 if ((was = ACM_GetStream(has)) == NULL) {
302 WARN("invalid handle\n");
303 return MMSYSERR_INVALHANDLE;
305 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
306 WARN("invalid parameter\n");
307 return MMSYSERR_INVALPARAM;
310 ret = MMSYSERR_INVALFLAG;
312 if (pash->fdwStatus & ACMSTREAMHEADER_STATUSF_DONE)
313 return MMSYSERR_NOERROR;
315 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
316 * size. some fields are private to msacm internals, and are exposed
317 * in ACMSTREAMHEADER in the dwReservedDriver array
319 padsh = (PACMDRVSTREAMHEADER)pash;
321 padsh->fdwConvert = fdwPrepare;
322 padsh->padshNext = NULL;
323 padsh->fdwDriver = padsh->dwDriver = 0L;
325 padsh->fdwPrepared = 0;
326 padsh->dwPrepared = 0;
327 padsh->pbPreparedSrc = 0;
328 padsh->cbPreparedSrcLength = 0;
329 padsh->pbPreparedDst = 0;
330 padsh->cbPreparedDstLength = 0;
332 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_PREPARE, (LPARAM)&was->drvInst, (DWORD)padsh);
333 if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
334 ret = MMSYSERR_NOERROR;
335 padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_DONE|ACMSTREAMHEADER_STATUSF_INQUEUE);
336 padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_PREPARED;
337 padsh->fdwPrepared = padsh->fdwStatus;
338 padsh->dwPrepared = 0;
339 padsh->pbPreparedSrc = padsh->pbSrc;
340 padsh->cbPreparedSrcLength = padsh->cbSrcLength;
341 padsh->pbPreparedDst = padsh->pbDst;
342 padsh->cbPreparedDstLength = padsh->cbDstLength;
344 padsh->fdwPrepared = 0;
345 padsh->dwPrepared = 0;
346 padsh->pbPreparedSrc = 0;
347 padsh->cbPreparedSrcLength = 0;
348 padsh->pbPreparedDst = 0;
349 padsh->cbPreparedDstLength = 0;
351 TRACE("=> (%d)\n", ret);
355 /***********************************************************************
356 * acmStreamReset (MSACM32.@)
358 MMRESULT WINAPI acmStreamReset(HACMSTREAM has, DWORD fdwReset)
361 MMRESULT ret = MMSYSERR_NOERROR;
363 TRACE("(%p, %ld)\n", has, fdwReset);
366 WARN("invalid flag\n");
367 ret = MMSYSERR_INVALFLAG;
368 } else if ((was = ACM_GetStream(has)) == NULL) {
369 WARN("invalid handle\n");
370 return MMSYSERR_INVALHANDLE;
371 } else if (was->drvInst.fdwOpen & ACM_STREAMOPENF_ASYNC) {
372 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_RESET, (LPARAM)&was->drvInst, 0);
374 TRACE("=> (%d)\n", ret);
378 /***********************************************************************
379 * acmStreamSize (MSACM32.@)
381 MMRESULT WINAPI acmStreamSize(HACMSTREAM has, DWORD cbInput,
382 LPDWORD pdwOutputBytes, DWORD fdwSize)
385 ACMDRVSTREAMSIZE adss;
388 TRACE("(%p, %ld, %p, %ld)\n", has, cbInput, pdwOutputBytes, fdwSize);
390 if ((was = ACM_GetStream(has)) == NULL) {
391 WARN("invalid handle\n");
392 return MMSYSERR_INVALHANDLE;
394 if ((fdwSize & ~ACM_STREAMSIZEF_QUERYMASK) != 0) {
395 WARN("invalid flag\n");
396 return MMSYSERR_INVALFLAG;
399 *pdwOutputBytes = 0L;
401 switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
402 case ACM_STREAMSIZEF_DESTINATION:
403 adss.cbDstLength = cbInput;
404 adss.cbSrcLength = 0;
406 case ACM_STREAMSIZEF_SOURCE:
407 adss.cbSrcLength = cbInput;
408 adss.cbDstLength = 0;
411 WARN("invalid flag\n");
412 return MMSYSERR_INVALFLAG;
415 adss.cbStruct = sizeof(adss);
416 adss.fdwSize = fdwSize;
417 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_SIZE,
418 (LPARAM)&was->drvInst, (LPARAM)&adss);
419 if (ret == MMSYSERR_NOERROR) {
420 switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
421 case ACM_STREAMSIZEF_DESTINATION:
422 *pdwOutputBytes = adss.cbSrcLength;
424 case ACM_STREAMSIZEF_SOURCE:
425 *pdwOutputBytes = adss.cbDstLength;
429 TRACE("=> (%d) [%lu]\n", ret, *pdwOutputBytes);
433 /***********************************************************************
434 * acmStreamUnprepareHeader (MSACM32.@)
436 MMRESULT WINAPI acmStreamUnprepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
440 MMRESULT ret = MMSYSERR_NOERROR;
441 PACMDRVSTREAMHEADER padsh;
443 TRACE("(%p, %p, %ld)\n", has, pash, fdwUnprepare);
445 if ((was = ACM_GetStream(has)) == NULL) {
446 WARN("invalid handle\n");
447 return MMSYSERR_INVALHANDLE;
449 if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
450 WARN("invalid parameter\n");
451 return MMSYSERR_INVALPARAM;
453 if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
454 WARN("unprepared header\n");
455 return ACMERR_UNPREPARED;
458 /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
459 * size. some fields are private to msacm internals, and are exposed
460 * in ACMSTREAMHEADER in the dwReservedDriver array
462 padsh = (PACMDRVSTREAMHEADER)pash;
464 /* check that pointers have not been modified */
465 if (padsh->pbPreparedSrc != padsh->pbSrc ||
466 padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
467 padsh->pbPreparedDst != padsh->pbDst ||
468 padsh->cbPreparedDstLength < padsh->cbDstLength) {
469 WARN("invalid parameter\n");
470 return MMSYSERR_INVALPARAM;
473 padsh->fdwConvert = fdwUnprepare;
475 ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_UNPREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
476 if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
477 ret = MMSYSERR_NOERROR;
478 padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_DONE|ACMSTREAMHEADER_STATUSF_INQUEUE|ACMSTREAMHEADER_STATUSF_PREPARED);
480 TRACE("=> (%d)\n", ret);