stream.c 15.2 KB
Newer Older
1 2
/* -*- tab-width: 8; c-basic-offset: 4 -*- */

3 4 5 6
/*
 *      MSACM32 library
 *
 *      Copyright 1998  Patrik Stridvall
7
 *		  1999	Eric Pouech
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24 25 26 27 28
 */

/* TODO
 * 	+ asynchronous conversion is not implemented
 *	+ callback/notification
 *	* acmStreamMessage
 *	+ properly close ACM streams
29 30
 */

31
#include <stdarg.h>
32
#include <string.h>
33
#include "windef.h"
34
#include "winbase.h"
35
#include "winerror.h"
36
#include "wine/debug.h"
37
#include "mmsystem.h"
38
#define NOBITMAP
39
#include "mmreg.h"
40 41
#include "msacm.h"
#include "msacmdrv.h"
42
#include "wineacm.h"
43

44
WINE_DEFAULT_DEBUG_CHANNEL(msacm);
45

46 47
static PWINE_ACMSTREAM	ACM_GetStream(HACMSTREAM has)
{
48 49
    TRACE("(%p)\n", has);

50 51
    return (PWINE_ACMSTREAM)has;
}
52

53
/***********************************************************************
54
 *           acmStreamClose (MSACM32.@)
55
 */
56
MMRESULT WINAPI acmStreamClose(HACMSTREAM has, DWORD fdwClose)
57
{
58 59
    PWINE_ACMSTREAM	was;
    MMRESULT		ret;
60

61
    TRACE("(%p, %d)\n", has, fdwClose);
62

63
    if ((was = ACM_GetStream(has)) == NULL) {
64
        WARN("invalid handle\n");
65 66
	return MMSYSERR_INVALHANDLE;
    }
67
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_CLOSE, (LPARAM)&was->drvInst, 0);
68
    if (ret == MMSYSERR_NOERROR) {
69
	if (was->hAcmDriver)
70
	    acmDriverClose(was->hAcmDriver, 0L);
71 72 73 74
	HeapFree(MSACM_hHeap, 0, was);
    }
    TRACE("=> (%d)\n", ret);
    return ret;
75 76 77
}

/***********************************************************************
78
 *           acmStreamConvert (MSACM32.@)
79
 */
80
MMRESULT WINAPI acmStreamConvert(HACMSTREAM has, PACMSTREAMHEADER pash,
81
				 DWORD fdwConvert)
82
{
83 84 85 86
    PWINE_ACMSTREAM	was;
    MMRESULT		ret = MMSYSERR_NOERROR;
    PACMDRVSTREAMHEADER	padsh;

87
    TRACE("(%p, %p, %d)\n", has, pash, fdwConvert);
88

89 90
    if ((was = ACM_GetStream(has)) == NULL) {
        WARN("invalid handle\n");
91
	return MMSYSERR_INVALHANDLE;
92 93 94
    }
    if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
        WARN("invalid parameter\n");
95
	return MMSYSERR_INVALPARAM;
96 97 98
    }
    if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
        WARN("unprepared header\n");
99
	return ACMERR_UNPREPARED;
100
    }
101

102 103 104
    pash->cbSrcLengthUsed = 0;
    pash->cbDstLengthUsed = 0;

105 106 107 108 109 110 111 112 113 114 115
    /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
     * size. some fields are private to msacm internals, and are exposed
     * in ACMSTREAMHEADER in the dwReservedDriver array
     */
    padsh = (PACMDRVSTREAMHEADER)pash;

    /* check that pointers have not been modified */
    if (padsh->pbPreparedSrc != padsh->pbSrc ||
	padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
	padsh->pbPreparedDst != padsh->pbDst ||
	padsh->cbPreparedDstLength < padsh->cbDstLength) {
116
        WARN("invalid parameter\n");
117
	return MMSYSERR_INVALPARAM;
118
    }
119 120 121

    padsh->fdwConvert = fdwConvert;

122
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_CONVERT, (LPARAM)&was->drvInst, (LPARAM)padsh);
123 124 125 126 127
    if (ret == MMSYSERR_NOERROR) {
	padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_DONE;
    }
    TRACE("=> (%d)\n", ret);
    return ret;
128 129 130
}

/***********************************************************************
131
 *           acmStreamMessage (MSACM32.@)
132
 */
133
MMRESULT WINAPI acmStreamMessage(HACMSTREAM has, UINT uMsg, LPARAM lParam1,
134
				 LPARAM lParam2)
135
{
136
    FIXME("(%p, %u, %ld, %ld): stub\n", has, uMsg, lParam1, lParam2);
137 138
    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
    return MMSYSERR_ERROR;
139 140 141
}

/***********************************************************************
142
 *           acmStreamOpen (MSACM32.@)
143
 */
144 145 146 147
MMRESULT WINAPI acmStreamOpen(PHACMSTREAM phas, HACMDRIVER had,
                              PWAVEFORMATEX pwfxSrc, PWAVEFORMATEX pwfxDst,
                              PWAVEFILTER pwfltr, DWORD_PTR dwCallback,
                              DWORD_PTR dwInstance, DWORD fdwOpen)
148
{
149 150 151 152 153
    PWINE_ACMSTREAM	was;
    PWINE_ACMDRIVER	wad;
    MMRESULT		ret;
    int			wfxSrcSize;
    int			wfxDstSize;
154
    WAVEFORMATEX	wfxSrc, wfxDst;
155

156
    TRACE("(%p, %p, %p, %p, %p, %ld, %ld, %d)\n",
157 158
	  phas, had, pwfxSrc, pwfxDst, pwfltr, dwCallback, dwInstance, fdwOpen);

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    /* NOTE: pwfxSrc and/or pwfxDst can point to a structure smaller than
     * WAVEFORMATEX so don't use them directly when not sure */
    if (pwfxSrc->wFormatTag == WAVE_FORMAT_PCM) {
        memcpy(&wfxSrc, pwfxSrc, sizeof(PCMWAVEFORMAT));
        wfxSrc.wBitsPerSample = pwfxSrc->wBitsPerSample;
        wfxSrc.cbSize = 0;
        pwfxSrc = &wfxSrc;
    }

    if (pwfxDst->wFormatTag == WAVE_FORMAT_PCM) {
        memcpy(&wfxDst, pwfxDst, sizeof(PCMWAVEFORMAT));
        wfxDst.wBitsPerSample = pwfxDst->wBitsPerSample;
        wfxDst.cbSize = 0;
        pwfxDst = &wfxDst;
    }

175
    TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
176
	  pwfxSrc->wFormatTag, pwfxSrc->nChannels, pwfxSrc->nSamplesPerSec, pwfxSrc->nAvgBytesPerSec,
177 178
	  pwfxSrc->nBlockAlign, pwfxSrc->wBitsPerSample, pwfxSrc->cbSize);

179
    TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
180
	  pwfxDst->wFormatTag, pwfxDst->nChannels, pwfxDst->nSamplesPerSec, pwfxDst->nAvgBytesPerSec,
181 182
	  pwfxDst->nBlockAlign, pwfxDst->wBitsPerSample, pwfxDst->cbSize);

183 184 185 186 187 188
    /* (WS) In query mode, phas should be NULL. If it is not, then instead
     * of returning an error we are making sure it is NULL, preventing some
     * applications that pass garbage for phas from crashing.
     */
    if (fdwOpen & ACM_STREAMOPENF_QUERY) phas = NULL;

189 190 191 192
    if (pwfltr && (pwfxSrc->wFormatTag != pwfxDst->wFormatTag)) {
        WARN("invalid parameter\n");
        return MMSYSERR_INVALPARAM;
    }
Eric Pouech's avatar
Eric Pouech committed
193 194 195 196

    wfxSrcSize = wfxDstSize = sizeof(WAVEFORMATEX);
    if (pwfxSrc->wFormatTag != WAVE_FORMAT_PCM) wfxSrcSize += pwfxSrc->cbSize;
    if (pwfxDst->wFormatTag != WAVE_FORMAT_PCM) wfxDstSize += pwfxDst->cbSize;
197

198
    was = HeapAlloc(MSACM_hHeap, 0, sizeof(*was) + wfxSrcSize + wfxDstSize +
199
		    ((pwfltr) ? sizeof(WAVEFILTER) : 0));
200 201
    if (was == NULL) {
        WARN("no memory\n");
202
	return MMSYSERR_NOMEM;
203
    }
204

205 206 207 208 209 210 211 212 213 214 215
    was->drvInst.cbStruct = sizeof(was->drvInst);
    was->drvInst.pwfxSrc = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was));
    memcpy(was->drvInst.pwfxSrc, pwfxSrc, wfxSrcSize);
    was->drvInst.pwfxDst = (PWAVEFORMATEX)((LPSTR)was + sizeof(*was) + wfxSrcSize);
    memcpy(was->drvInst.pwfxDst, pwfxDst, wfxDstSize);
    if (pwfltr) {
	was->drvInst.pwfltr = (PWAVEFILTER)((LPSTR)was + sizeof(*was) + wfxSrcSize + wfxDstSize);
	memcpy(was->drvInst.pwfltr, pwfltr, sizeof(WAVEFILTER));
    } else {
	was->drvInst.pwfltr = NULL;
    }
216
    was->drvInst.dwCallback = dwCallback;
217 218
    was->drvInst.dwInstance = dwInstance;
    was->drvInst.fdwOpen = fdwOpen;
219 220
    was->drvInst.fdwDriver = 0L;
    was->drvInst.dwDriver = 0L;
221 222
    /* real value will be stored once ACMDM_STREAM_OPEN succeeds */
    was->drvInst.has = 0L;
223

224 225 226 227 228
    if (had) {
	if (!(wad = MSACM_GetDriver(had))) {
	    ret = MMSYSERR_INVALPARAM;
	    goto errCleanUp;
	}
229

230
	was->obj.dwType = WINE_ACMOBJ_STREAM;
231 232
	was->obj.pACMDriverID = wad->obj.pACMDriverID;
	was->pDrv = wad;
233 234
	was->hAcmDriver = 0; /* not to close it in acmStreamClose */

235
	ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
236 237 238 239
	if (ret != MMSYSERR_NOERROR)
	    goto errCleanUp;
    } else {
	PWINE_ACMDRIVERID wadi;
240

241 242
	ret = ACMERR_NOTPOSSIBLE;
	for (wadi = MSACM_pFirstACMDriverID; wadi; wadi = wadi->pNextACMDriverID) {
243
	    if ((wadi->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ||
244 245 246
		!MSACM_FindFormatTagInCache(wadi, pwfxSrc->wFormatTag, NULL) ||
		!MSACM_FindFormatTagInCache(wadi, pwfxDst->wFormatTag, NULL))
		continue;
247
	    ret = acmDriverOpen(&had, (HACMDRIVERID)wadi, 0L);
248 249 250 251 252 253 254
	    if (ret != MMSYSERR_NOERROR)
		continue;
	    if ((wad = MSACM_GetDriver(had)) != 0) {
		was->obj.dwType = WINE_ACMOBJ_STREAM;
		was->obj.pACMDriverID = wad->obj.pACMDriverID;
		was->pDrv = wad;
		was->hAcmDriver = had;
255

256
		ret = MSACM_Message((HACMDRIVER)wad, ACMDM_STREAM_OPEN, (LPARAM)&was->drvInst, 0L);
257
		TRACE("%s => %08x\n", debugstr_w(wadi->pszDriverAlias), ret);
258 259 260
		if (ret == MMSYSERR_NOERROR) {
		    if (fdwOpen & ACM_STREAMOPENF_QUERY) {
			acmDriverClose(had, 0L);
261
		    }
262
		    break;
263 264
		}
	    }
265 266
	    /* no match, close this acm driver and try next one */
	    acmDriverClose(had, 0L);
267 268 269 270 271 272 273
	}
	if (ret != MMSYSERR_NOERROR) {
	    ret = ACMERR_NOTPOSSIBLE;
	    goto errCleanUp;
	}
    }
    ret = MMSYSERR_NOERROR;
274
    was->drvInst.has = (HACMSTREAM)was;
275 276 277 278
    if (!(fdwOpen & ACM_STREAMOPENF_QUERY)) {
	if (phas)
	    *phas = (HACMSTREAM)was;
	TRACE("=> (%d)\n", ret);
279
	return ret;
280
    }
281
errCleanUp:
282
    if (phas)
283
	*phas = NULL;
284 285 286
    HeapFree(MSACM_hHeap, 0, was);
    TRACE("=> (%d)\n", ret);
    return ret;
287 288 289 290
}


/***********************************************************************
291
 *           acmStreamPrepareHeader (MSACM32.@)
292
 */
293
MMRESULT WINAPI acmStreamPrepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
294
				       DWORD fdwPrepare)
295
{
296 297 298 299
    PWINE_ACMSTREAM	was;
    MMRESULT		ret = MMSYSERR_NOERROR;
    PACMDRVSTREAMHEADER	padsh;

300
    TRACE("(%p, %p, %d)\n", has, pash, fdwPrepare);
301

302 303
    if ((was = ACM_GetStream(has)) == NULL) {
        WARN("invalid handle\n");
304
	return MMSYSERR_INVALHANDLE;
305 306 307
    }
    if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
        WARN("invalid parameter\n");
308
	return MMSYSERR_INVALPARAM;
309
    }
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    if (fdwPrepare)
	ret = MMSYSERR_INVALFLAG;

    if (pash->fdwStatus & ACMSTREAMHEADER_STATUSF_DONE)
	return MMSYSERR_NOERROR;

    /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
     * size. some fields are private to msacm internals, and are exposed
     * in ACMSTREAMHEADER in the dwReservedDriver array
     */
    padsh = (PACMDRVSTREAMHEADER)pash;

    padsh->fdwConvert = fdwPrepare;
    padsh->padshNext = NULL;
    padsh->fdwDriver = padsh->dwDriver = 0L;

    padsh->fdwPrepared = 0;
    padsh->dwPrepared = 0;
    padsh->pbPreparedSrc = 0;
    padsh->cbPreparedSrcLength = 0;
    padsh->pbPreparedDst = 0;
    padsh->cbPreparedDstLength = 0;

333
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_PREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
	ret = MMSYSERR_NOERROR;
	padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_DONE|ACMSTREAMHEADER_STATUSF_INQUEUE);
	padsh->fdwStatus |= ACMSTREAMHEADER_STATUSF_PREPARED;
	padsh->fdwPrepared = padsh->fdwStatus;
	padsh->dwPrepared = 0;
	padsh->pbPreparedSrc = padsh->pbSrc;
	padsh->cbPreparedSrcLength = padsh->cbSrcLength;
	padsh->pbPreparedDst = padsh->pbDst;
	padsh->cbPreparedDstLength = padsh->cbDstLength;
    } else {
	padsh->fdwPrepared = 0;
	padsh->dwPrepared = 0;
	padsh->pbPreparedSrc = 0;
	padsh->cbPreparedSrcLength = 0;
	padsh->pbPreparedDst = 0;
	padsh->cbPreparedDstLength = 0;
    }
    TRACE("=> (%d)\n", ret);
    return ret;
354 355 356
}

/***********************************************************************
357
 *           acmStreamReset (MSACM32.@)
358
 */
359
MMRESULT WINAPI acmStreamReset(HACMSTREAM has, DWORD fdwReset)
360
{
361 362 363
    PWINE_ACMSTREAM	was;
    MMRESULT		ret = MMSYSERR_NOERROR;

364
    TRACE("(%p, %d)\n", has, fdwReset);
365 366

    if (fdwReset) {
367
        WARN("invalid flag\n");
368 369
	ret = MMSYSERR_INVALFLAG;
    } else if ((was = ACM_GetStream(has)) == NULL) {
370
        WARN("invalid handle\n");
371 372
	return MMSYSERR_INVALHANDLE;
    } else if (was->drvInst.fdwOpen & ACM_STREAMOPENF_ASYNC) {
373
	ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_RESET, (LPARAM)&was->drvInst, 0);
374 375 376
    }
    TRACE("=> (%d)\n", ret);
    return ret;
377 378 379
}

/***********************************************************************
380
 *           acmStreamSize (MSACM32.@)
381
 */
382
MMRESULT WINAPI acmStreamSize(HACMSTREAM has, DWORD cbInput,
383
			      LPDWORD pdwOutputBytes, DWORD fdwSize)
384
{
385 386 387
    PWINE_ACMSTREAM	was;
    ACMDRVSTREAMSIZE	adss;
    MMRESULT		ret;
388

389
    TRACE("(%p, %d, %p, %d)\n", has, cbInput, pdwOutputBytes, fdwSize);
390

391
    if ((was = ACM_GetStream(has)) == NULL) {
392
        WARN("invalid handle\n");
393 394 395
	return MMSYSERR_INVALHANDLE;
    }
    if ((fdwSize & ~ACM_STREAMSIZEF_QUERYMASK) != 0) {
396
        WARN("invalid flag\n");
397 398 399 400
	return MMSYSERR_INVALFLAG;
    }

    *pdwOutputBytes = 0L;
401

402 403 404 405 406 407 408 409 410
    switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
    case ACM_STREAMSIZEF_DESTINATION:
	adss.cbDstLength = cbInput;
	adss.cbSrcLength = 0;
	break;
    case ACM_STREAMSIZEF_SOURCE:
	adss.cbSrcLength = cbInput;
	adss.cbDstLength = 0;
	break;
411
    default:
412
        WARN("invalid flag\n");
413 414
	return MMSYSERR_INVALFLAG;
    }
415

416 417
    adss.cbStruct = sizeof(adss);
    adss.fdwSize = fdwSize;
418
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_SIZE,
419
                            (LPARAM)&was->drvInst, (LPARAM)&adss);
420 421 422 423 424 425 426 427 428 429
    if (ret == MMSYSERR_NOERROR) {
	switch (fdwSize & ACM_STREAMSIZEF_QUERYMASK) {
	case ACM_STREAMSIZEF_DESTINATION:
	    *pdwOutputBytes = adss.cbSrcLength;
	    break;
	case ACM_STREAMSIZEF_SOURCE:
	    *pdwOutputBytes = adss.cbDstLength;
	    break;
	}
    }
430
    TRACE("=> (%d) [%u]\n", ret, *pdwOutputBytes);
431
    return ret;
432 433 434
}

/***********************************************************************
435
 *           acmStreamUnprepareHeader (MSACM32.@)
436
 */
437
MMRESULT WINAPI acmStreamUnprepareHeader(HACMSTREAM has, PACMSTREAMHEADER pash,
438
					 DWORD fdwUnprepare)
439
{
440 441 442 443
    PWINE_ACMSTREAM	was;
    MMRESULT		ret = MMSYSERR_NOERROR;
    PACMDRVSTREAMHEADER	padsh;

444
    TRACE("(%p, %p, %d)\n", has, pash, fdwUnprepare);
445

446 447
    if ((was = ACM_GetStream(has)) == NULL) {
        WARN("invalid handle\n");
448
	return MMSYSERR_INVALHANDLE;
449 450 451
    }
    if (!pash || pash->cbStruct < sizeof(ACMSTREAMHEADER)) {
        WARN("invalid parameter\n");
452
	return MMSYSERR_INVALPARAM;
453 454 455
    }
    if (!(pash->fdwStatus & ACMSTREAMHEADER_STATUSF_PREPARED)) {
        WARN("unprepared header\n");
456
	return ACMERR_UNPREPARED;
457
    }
458 459 460 461 462 463 464 465 466 467 468 469

    /* Note: the ACMSTREAMHEADER and ACMDRVSTREAMHEADER structs are of same
     * size. some fields are private to msacm internals, and are exposed
     * in ACMSTREAMHEADER in the dwReservedDriver array
     */
    padsh = (PACMDRVSTREAMHEADER)pash;

    /* check that pointers have not been modified */
    if (padsh->pbPreparedSrc != padsh->pbSrc ||
	padsh->cbPreparedSrcLength < padsh->cbSrcLength ||
	padsh->pbPreparedDst != padsh->pbDst ||
	padsh->cbPreparedDstLength < padsh->cbDstLength) {
470
        WARN("invalid parameter\n");
471
	return MMSYSERR_INVALPARAM;
472
    }
473 474 475

    padsh->fdwConvert = fdwUnprepare;

476
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_UNPREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
477 478 479 480 481 482
    if (ret == MMSYSERR_NOERROR || ret == MMSYSERR_NOTSUPPORTED) {
	ret = MMSYSERR_NOERROR;
	padsh->fdwStatus &= ~(ACMSTREAMHEADER_STATUSF_DONE|ACMSTREAMHEADER_STATUSF_INQUEUE|ACMSTREAMHEADER_STATUSF_PREPARED);
    }
    TRACE("=> (%d)\n", ret);
    return ret;
483
}