stream.c 15.1 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
MMRESULT WINAPI acmStreamOpen(PHACMSTREAM phas, HACMDRIVER had, PWAVEFORMATEX pwfxSrc,
			      PWAVEFORMATEX pwfxDst, PWAVEFILTER pwfltr, DWORD dwCallback,
			      DWORD dwInstance, DWORD fdwOpen)
147
{
148 149 150 151 152
    PWINE_ACMSTREAM	was;
    PWINE_ACMDRIVER	wad;
    MMRESULT		ret;
    int			wfxSrcSize;
    int			wfxDstSize;
153
    WAVEFORMATEX	wfxSrc, wfxDst;
154

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

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    /* 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;
    }

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

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

182 183 184 185 186 187
    /* (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;

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

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

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

204 205 206 207 208 209 210 211 212 213 214
    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;
    }
215
    was->drvInst.dwCallback = dwCallback;
216 217
    was->drvInst.dwInstance = dwInstance;
    was->drvInst.fdwOpen = fdwOpen;
218 219
    was->drvInst.fdwDriver = 0L;
    was->drvInst.dwDriver = 0L;
220 221
    /* real value will be stored once ACMDM_STREAM_OPEN succeeds */
    was->drvInst.has = 0L;
222

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

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

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

240 241
	ret = ACMERR_NOTPOSSIBLE;
	for (wadi = MSACM_pFirstACMDriverID; wadi; wadi = wadi->pNextACMDriverID) {
242
	    if ((wadi->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ||
243 244 245
		!MSACM_FindFormatTagInCache(wadi, pwfxSrc->wFormatTag, NULL) ||
		!MSACM_FindFormatTagInCache(wadi, pwfxDst->wFormatTag, NULL))
		continue;
246
	    ret = acmDriverOpen(&had, (HACMDRIVERID)wadi, 0L);
247 248 249 250 251 252 253
	    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;
254

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


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

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

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

332
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_PREPARE, (LPARAM)&was->drvInst, (DWORD)padsh);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    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;
353 354 355
}

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

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

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

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

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

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

    *pdwOutputBytes = 0L;
400

401 402 403 404 405 406 407 408 409
    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;
410
    default:
411
        WARN("invalid flag\n");
412 413
	return MMSYSERR_INVALFLAG;
    }
414

415 416
    adss.cbStruct = sizeof(adss);
    adss.fdwSize = fdwSize;
417
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_SIZE,
418
                            (LPARAM)&was->drvInst, (LPARAM)&adss);
419 420 421 422 423 424 425 426 427 428
    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;
	}
    }
429
    TRACE("=> (%d) [%u]\n", ret, *pdwOutputBytes);
430
    return ret;
431 432 433
}

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

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

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

    /* 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) {
469
        WARN("invalid parameter\n");
470
	return MMSYSERR_INVALPARAM;
471
    }
472 473 474

    padsh->fdwConvert = fdwUnprepare;

475
    ret = MSACM_Message((HACMDRIVER)was->pDrv, ACMDM_STREAM_UNPREPARE, (LPARAM)&was->drvInst, (LPARAM)padsh);
476 477 478 479 480 481
    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;
482
}