- CoSetState info should be thread local.
[wine] / dlls / dmime / audiopath.c
1 /* IDirectMusicAudioPath Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
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 Library 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-1307, USA.
18  */
19
20 #include "dmime_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 /*****************************************************************************
26  * IDirectMusicAudioPathImpl implementation
27  */
28 /* IDirectMusicAudioPath IUnknown part: */
29 HRESULT WINAPI IDirectMusicAudioPathImpl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, UnknownVtbl, iface);
31         
32         if (IsEqualIID (riid, &IID_IUnknown)) {
33                 *ppobj = (LPVOID)&This->UnknownVtbl;
34                 IDirectMusicAudioPathImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
35                 return S_OK;    
36         } else if (IsEqualIID (riid, &IID_IDirectMusicAudioPath)) {
37                 *ppobj = (LPVOID)&This->AudioPathVtbl;
38                 IDirectMusicAudioPathImpl_IDirectMusicAudioPath_AddRef ((LPDIRECTMUSICAUDIOPATH)&This->AudioPathVtbl);
39                 return S_OK;
40         } else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
41                 *ppobj = (LPVOID)&This->ObjectVtbl;
42                 IDirectMusicAudioPathImpl_IDirectMusicObject_AddRef ((LPDIRECTMUSICOBJECT)&This->ObjectVtbl);           
43                 return S_OK;
44         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
45                 *ppobj = (LPVOID)&This->PersistStreamVtbl;
46                 IDirectMusicAudioPathImpl_IPersistStream_AddRef ((LPPERSISTSTREAM)&This->PersistStreamVtbl);            
47                 return S_OK;
48         }
49         
50         WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
51         return E_NOINTERFACE;
52 }
53
54 ULONG WINAPI IDirectMusicAudioPathImpl_IUnknown_AddRef (LPUNKNOWN iface) {
55         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, UnknownVtbl, iface);
56         TRACE("(%p) : AddRef from %ld\n", This, This->ref);
57         return ++(This->ref);
58 }
59
60 ULONG WINAPI IDirectMusicAudioPathImpl_IUnknown_Release (LPUNKNOWN iface) {
61         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, UnknownVtbl, iface);
62         ULONG ref = --This->ref;
63         TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
64         if (ref == 0) {
65                 HeapFree(GetProcessHeap(), 0, This);
66         }
67         return ref;
68 }
69
70 ICOM_VTABLE(IUnknown) DirectMusicAudioPath_Unknown_Vtbl = {
71     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
72         IDirectMusicAudioPathImpl_IUnknown_QueryInterface,
73         IDirectMusicAudioPathImpl_IUnknown_AddRef,
74         IDirectMusicAudioPathImpl_IUnknown_Release
75 };
76
77 /* IDirectMusicAudioPathImpl IDirectMusicAudioPath part: */
78 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_QueryInterface (LPDIRECTMUSICAUDIOPATH iface, REFIID riid, LPVOID *ppobj) {
79         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
80         return IDirectMusicAudioPathImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
81 }
82
83 ULONG WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_AddRef (LPDIRECTMUSICAUDIOPATH iface) {
84         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
85         return IDirectMusicAudioPathImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
86 }
87
88 ULONG WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_Release (LPDIRECTMUSICAUDIOPATH iface) {
89         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
90         return IDirectMusicAudioPathImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
91 }
92
93 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_GetObjectInPath (LPDIRECTMUSICAUDIOPATH iface, DWORD dwPChannel, DWORD dwStage, DWORD dwBuffer, REFGUID guidObject, WORD dwIndex, REFGUID iidInterface, void** ppObject) {
94         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
95
96         FIXME("(%p, %ld, %ld, %ld, %s, %d, %s, %p): stub\n", This, dwPChannel, dwStage, dwBuffer, debugstr_guid(guidObject), dwIndex, debugstr_guid(iidInterface), ppObject);
97
98         switch (dwStage) {
99         case DMUS_PATH_BUFFER:
100           {
101             if (IsEqualIID (iidInterface, &IID_IDirectSoundBuffer8)) {
102               IDirectSoundBuffer8_QueryInterface (This->pDSBuffer, &IID_IDirectSoundBuffer8, ppObject);
103               TRACE("returning %p\n",*ppObject);
104               return S_OK;
105             } else if (IsEqualIID (iidInterface, &IID_IDirectSound3DBuffer)) {
106               IDirectSoundBuffer8_QueryInterface (This->pDSBuffer, &IID_IDirectSound3DBuffer, ppObject);
107               TRACE("returning %p\n",*ppObject);
108               return S_OK;
109             } else {
110               FIXME("Bad iid\n");
111             }
112           }
113           break;
114
115         case DMUS_PATH_PRIMARY_BUFFER: {
116           if (IsEqualIID (iidInterface, &IID_IDirectSound3DListener)) {
117             IDirectSoundBuffer8_QueryInterface (This->pPrimary, &IID_IDirectSound3DListener, ppObject);
118             return S_OK;
119           } else {
120             FIXME("bad iid...\n");
121           }
122         }
123         break;
124
125         case DMUS_PATH_AUDIOPATH_GRAPH:
126           {
127             if (IsEqualIID (iidInterface, &IID_IDirectMusicGraph)) {
128               if (NULL == This->pToolGraph) {
129                 IDirectMusicGraphImpl* pGraph;
130                 DMUSIC_CreateDirectMusicGraphImpl (&IID_IDirectMusicGraph, (LPVOID*)&pGraph, NULL);
131                 This->pToolGraph = (IDirectMusicGraph*) pGraph;
132               }
133               *ppObject = (LPDIRECTMUSICGRAPH) This->pToolGraph; 
134               IDirectMusicGraph_AddRef((LPDIRECTMUSICGRAPH) *ppObject);
135               return S_OK;
136             }
137           }
138           break;
139
140         case DMUS_PATH_AUDIOPATH_TOOL:
141           {
142             /* TODO */
143           }
144           break;
145
146         case DMUS_PATH_PERFORMANCE:
147           {
148             /* TODO check wanted GUID */
149             *ppObject = (LPDIRECTMUSICPERFORMANCE8) This->pPerf; 
150             IDirectMusicPerformance8Impl_AddRef((LPDIRECTMUSICPERFORMANCE8) *ppObject);
151             return S_OK;
152           }
153           break;
154
155         case DMUS_PATH_PERFORMANCE_GRAPH:
156           {
157             IDirectMusicGraph* pPerfoGraph = NULL; 
158             IDirectMusicPerformance8Impl_GetGraph((LPDIRECTMUSICPERFORMANCE8) This->pPerf, &pPerfoGraph);
159             if (NULL == pPerfoGraph) {
160               IDirectMusicGraphImpl* pGraph = NULL; 
161               DMUSIC_CreateDirectMusicGraphImpl (&IID_IDirectMusicGraph, (LPVOID*)&pGraph, NULL);                       
162               IDirectMusicPerformance8Impl_SetGraph((LPDIRECTMUSICPERFORMANCE8) This->pPerf, (IDirectMusicGraph*) pGraph);
163               /* we need release as SetGraph do an AddRef */
164               IDirectMusicGraph_Release((LPDIRECTMUSICGRAPH) pGraph);
165               pPerfoGraph = (LPDIRECTMUSICGRAPH) pGraph;
166             }
167             *ppObject = (LPDIRECTMUSICGRAPH) pPerfoGraph; 
168             return S_OK;
169           }
170           break;
171
172         case DMUS_PATH_PERFORMANCE_TOOL:
173         default:
174           break;
175         }
176
177         *ppObject = NULL;
178         return E_INVALIDARG;
179 }
180
181 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_Activate (LPDIRECTMUSICAUDIOPATH iface, BOOL fActivate) {
182         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
183         FIXME("(%p, %d): stub\n", This, fActivate);
184         return S_OK;
185 }
186
187 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_SetVolume (LPDIRECTMUSICAUDIOPATH iface, long lVolume, DWORD dwDuration) {
188         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
189         FIXME("(%p, %li, %ld): stub\n", This, lVolume, dwDuration);
190         return S_OK;
191 }
192
193 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicAudioPath_ConvertPChannel (LPDIRECTMUSICAUDIOPATH iface, DWORD dwPChannelIn, DWORD* pdwPChannelOut) {
194         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, AudioPathVtbl, iface);
195         FIXME("(%p, %ld, %p): stub\n", This, dwPChannelIn, pdwPChannelOut);
196         return S_OK;
197 }
198
199 ICOM_VTABLE(IDirectMusicAudioPath) DirectMusicAudioPath_AudioPath_Vtbl = {
200     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
201         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_QueryInterface,
202         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_AddRef,
203         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_Release,
204         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_GetObjectInPath,
205         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_Activate,
206         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_SetVolume,
207         IDirectMusicAudioPathImpl_IDirectMusicAudioPath_ConvertPChannel
208 };
209
210 /* IDirectMusicAudioPathImpl IDirectMusicObject part: */
211 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
212         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, ObjectVtbl, iface);
213         return IDirectMusicAudioPathImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
214 }
215
216 ULONG WINAPI IDirectMusicAudioPathImpl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
217         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, ObjectVtbl, iface);
218         return IDirectMusicAudioPathImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
219 }
220
221 ULONG WINAPI IDirectMusicAudioPathImpl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
222         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, ObjectVtbl, iface);
223         return IDirectMusicAudioPathImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
224 }
225
226 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
227         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, ObjectVtbl, iface);
228         TRACE("(%p, %p)\n", This, pDesc);
229         /* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
230         memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
231         return S_OK;
232 }
233
234 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
235         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, ObjectVtbl, iface);
236         TRACE("(%p, %p): setting descriptor:\n", This, pDesc);
237         if (TRACE_ON(dmime)) {
238                 DMUSIC_dump_DMUS_OBJECTDESC (pDesc);
239         }
240         
241         /* According to MSDN, we should copy only given values, not whole struct */     
242         if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
243                 memcpy (&This->pDesc->guidObject, &pDesc->guidObject, sizeof (pDesc->guidObject));
244         if (pDesc->dwValidData & DMUS_OBJ_CLASS)
245                 memcpy (&This->pDesc->guidClass, &pDesc->guidClass, sizeof (pDesc->guidClass));         
246         if (pDesc->dwValidData & DMUS_OBJ_NAME)
247                 strncpyW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
248         if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
249                 strncpyW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);             
250         if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
251                 strncpyW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);             
252         if (pDesc->dwValidData & DMUS_OBJ_VERSION)
253                 memcpy (&This->pDesc->vVersion, &pDesc->vVersion, sizeof (pDesc->vVersion));                            
254         if (pDesc->dwValidData & DMUS_OBJ_DATE)
255                 memcpy (&This->pDesc->ftDate, &pDesc->ftDate, sizeof (pDesc->ftDate));                          
256         if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
257                 memcpy (&This->pDesc->llMemLength, &pDesc->llMemLength, sizeof (pDesc->llMemLength));                           
258                 memcpy (This->pDesc->pbMemData, pDesc->pbMemData, sizeof (pDesc->pbMemData));
259         }
260         if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
261                 /* according to MSDN, we copy the stream */
262                 IStream_Clone (pDesc->pStream, &This->pDesc->pStream);  
263         }
264         
265         /* add new flags */
266         This->pDesc->dwValidData |= pDesc->dwValidData;
267
268         return S_OK;
269 }
270
271 HRESULT WINAPI IDirectMusicAudioPathImpl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
272         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, ObjectVtbl, iface);
273         DMUS_PRIVATE_CHUNK Chunk;
274         DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
275         LARGE_INTEGER liMove; /* used when skipping chunks */
276
277         TRACE("(%p, %p, %p)\n", This, pStream, pDesc);
278         
279         /* FIXME: should this be determined from stream? */
280         pDesc->dwValidData |= DMUS_OBJ_CLASS;
281         memcpy (&pDesc->guidClass, &CLSID_DirectMusicAudioPath, sizeof(CLSID));
282         
283         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
284         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
285         switch (Chunk.fccID) {  
286                 case FOURCC_RIFF: {
287                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
288                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
289                         StreamSize = Chunk.dwSize - sizeof(FOURCC);
290                         StreamCount = 0;
291                         if (Chunk.fccID == DMUS_FOURCC_AUDIOPATH_FORM) {
292                                 TRACE_(dmfile)(": audio path form\n");
293                                 do {
294                                         IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
295                                         StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
296                                         TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
297                                         switch (Chunk.fccID) {
298                                                 case DMUS_FOURCC_GUID_CHUNK: {
299                                                         TRACE_(dmfile)(": GUID chunk\n");
300                                                         pDesc->dwValidData |= DMUS_OBJ_OBJECT;
301                                                         IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
302                                                         break;
303                                                 }
304                                                 case DMUS_FOURCC_VERSION_CHUNK: {
305                                                         TRACE_(dmfile)(": version chunk\n");
306                                                         pDesc->dwValidData |= DMUS_OBJ_VERSION;
307                                                         IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
308                                                         break;
309                                                 }
310                                                 case DMUS_FOURCC_CATEGORY_CHUNK: {
311                                                         TRACE_(dmfile)(": category chunk\n");
312                                                         pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
313                                                         IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
314                                                         break;
315                                                 }
316                                                 case FOURCC_LIST: {
317                                                         IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);                             
318                                                         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
319                                                         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
320                                                         ListCount[0] = 0;
321                                                         switch (Chunk.fccID) {
322                                                                 /* evil M$ UNFO list, which can (!?) contain INFO elements */
323                                                                 case DMUS_FOURCC_UNFO_LIST: {
324                                                                         TRACE_(dmfile)(": UNFO list\n");
325                                                                         do {
326                                                                                 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
327                                                                                 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
328                                                                                 TRACE_(dmfile)(": %s chunk (size = 0x%04lx)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
329                                                                                 switch (Chunk.fccID) {
330                                                                                         /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
331                                              (though strings seem to be valid unicode) */
332                                                                                         case mmioFOURCC('I','N','A','M'):
333                                                                                         case DMUS_FOURCC_UNAM_CHUNK: {
334                                                                                                 TRACE_(dmfile)(": name chunk\n");
335                                                                                                 pDesc->dwValidData |= DMUS_OBJ_NAME;
336                                                                                                 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
337                                                                                                 break;
338                                                                                         }
339                                                                                         case mmioFOURCC('I','A','R','T'):
340                                                                                         case DMUS_FOURCC_UART_CHUNK: {
341                                                                                                 TRACE_(dmfile)(": artist chunk (ignored)\n");
342                                                                                                 liMove.QuadPart = Chunk.dwSize;
343                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
344                                                                                                 break;
345                                                                                         }
346                                                                                         case mmioFOURCC('I','C','O','P'):
347                                                                                         case DMUS_FOURCC_UCOP_CHUNK: {
348                                                                                                 TRACE_(dmfile)(": copyright chunk (ignored)\n");
349                                                                                                 liMove.QuadPart = Chunk.dwSize;
350                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
351                                                                                                 break;
352                                                                                         }
353                                                                                         case mmioFOURCC('I','S','B','J'):
354                                                                                         case DMUS_FOURCC_USBJ_CHUNK: {
355                                                                                                 TRACE_(dmfile)(": subject chunk (ignored)\n");
356                                                                                                 liMove.QuadPart = Chunk.dwSize;
357                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
358                                                                                                 break;
359                                                                                         }
360                                                                                         case mmioFOURCC('I','C','M','T'):
361                                                                                         case DMUS_FOURCC_UCMT_CHUNK: {
362                                                                                                 TRACE_(dmfile)(": comment chunk (ignored)\n");
363                                                                                                 liMove.QuadPart = Chunk.dwSize;
364                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
365                                                                                                 break;
366                                                                                         }
367                                                                                         default: {
368                                                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
369                                                                                                 liMove.QuadPart = Chunk.dwSize;
370                                                                                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
371                                                                                                 break;                                          
372                                                                                         }
373                                                                                 }
374                                                                                 TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
375                                                                         } while (ListCount[0] < ListSize[0]);
376                                                                         break;
377                                                                 }
378                                                                 default: {
379                                                                         TRACE_(dmfile)(": unknown (skipping)\n");
380                                                                         liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
381                                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
382                                                                         break;                                          
383                                                                 }
384                                                         }
385                                                         break;
386                                                 }       
387                                                 default: {
388                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
389                                                         liMove.QuadPart = Chunk.dwSize;
390                                                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
391                                                         break;                                          
392                                                 }
393                                         }
394                                         TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
395                                 } while (StreamCount < StreamSize);
396                                 break;
397                         } else {
398                                 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
399                                 liMove.QuadPart = StreamSize;
400                                 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
401                                 return E_FAIL;
402                         }
403                 
404                         TRACE_(dmfile)(": reading finished\n");
405                         break;
406                 }
407                 default: {
408                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
409                         liMove.QuadPart = Chunk.dwSize;
410                         IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
411                         return DMUS_E_INVALIDFILE;
412                 }
413         }       
414         
415         TRACE(": returning descriptor:\n");
416         if (TRACE_ON(dmime)) {
417                 DMUSIC_dump_DMUS_OBJECTDESC (pDesc);
418         }
419         
420         return S_OK;
421 }
422
423 ICOM_VTABLE(IDirectMusicObject) DirectMusicAudioPath_Object_Vtbl = {
424     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
425         IDirectMusicAudioPathImpl_IDirectMusicObject_QueryInterface,
426         IDirectMusicAudioPathImpl_IDirectMusicObject_AddRef,
427         IDirectMusicAudioPathImpl_IDirectMusicObject_Release,
428         IDirectMusicAudioPathImpl_IDirectMusicObject_GetDescriptor,
429         IDirectMusicAudioPathImpl_IDirectMusicObject_SetDescriptor,
430         IDirectMusicAudioPathImpl_IDirectMusicObject_ParseDescriptor
431 };
432
433 /* IDirectMusicAudioPathImpl IPersistStream part: */
434 HRESULT WINAPI IDirectMusicAudioPathImpl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
435         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, PersistStreamVtbl, iface);
436         return IDirectMusicAudioPathImpl_IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
437 }
438
439 ULONG WINAPI IDirectMusicAudioPathImpl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
440         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, PersistStreamVtbl, iface);
441         return IDirectMusicAudioPathImpl_IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
442 }
443
444 ULONG WINAPI IDirectMusicAudioPathImpl_IPersistStream_Release (LPPERSISTSTREAM iface) {
445         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, PersistStreamVtbl, iface);
446         return IDirectMusicAudioPathImpl_IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
447 }
448
449 HRESULT WINAPI IDirectMusicAudioPathImpl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
450         return E_NOTIMPL;
451 }
452
453 HRESULT WINAPI IDirectMusicAudioPathImpl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
454         return E_NOTIMPL;
455 }
456
457 HRESULT WINAPI IDirectMusicAudioPathImpl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
458         ICOM_THIS_MULTI(IDirectMusicAudioPathImpl, PersistStreamVtbl, iface);
459
460         FOURCC chunkID;
461         DWORD chunkSize, StreamSize, StreamCount, ListSize[3], ListCount[3];
462         LARGE_INTEGER liMove; /* used when skipping chunks */
463
464         FIXME("(%p, %p): Loading not implemented yet\n", This, pStm);
465         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
466         IStream_Read (pStm, &chunkSize, sizeof(DWORD), NULL);
467         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
468         switch (chunkID) {      
469                 case FOURCC_RIFF: {
470                         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);                            
471                         TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(chunkID));
472                         StreamSize = chunkSize - sizeof(FOURCC);
473                         StreamCount = 0;
474                         switch (chunkID) {
475                                 case DMUS_FOURCC_AUDIOPATH_FORM: {
476                                         TRACE_(dmfile)(": audio path form\n");
477                                         do {
478                                                 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
479                                                 IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
480                                                 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
481                                                 TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
482                                                 switch (chunkID) {
483                                                         case DMUS_FOURCC_GUID_CHUNK: {
484                                                                 TRACE_(dmfile)(": GUID chunk\n");
485                                                                 This->pDesc->dwValidData |= DMUS_OBJ_OBJECT;
486                                                                 IStream_Read (pStm, &This->pDesc->guidObject, chunkSize, NULL);
487                                                                 break;
488                                                         }
489                                                         case DMUS_FOURCC_VERSION_CHUNK: {
490                                                                 TRACE_(dmfile)(": version chunk\n");
491                                                                 This->pDesc->dwValidData |= DMUS_OBJ_VERSION;
492                                                                 IStream_Read (pStm, &This->pDesc->vVersion, chunkSize, NULL);
493                                                                 break;
494                                                         }
495                                                         case DMUS_FOURCC_CATEGORY_CHUNK: {
496                                                                 TRACE_(dmfile)(": category chunk\n");
497                                                                 This->pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
498                                                                 IStream_Read (pStm, This->pDesc->wszCategory, chunkSize, NULL);
499                                                                 break;
500                                                         }
501                                                         case FOURCC_LIST: {
502                                                                 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);                            
503                                                                 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(chunkID));
504                                                                 ListSize[0] = chunkSize - sizeof(FOURCC);
505                                                                 ListCount[0] = 0;
506                                                                 switch (chunkID) {
507                                                                         case DMUS_FOURCC_UNFO_LIST: {
508                                                                                 TRACE_(dmfile)(": UNFO list\n");
509                                                                                 do {
510                                                                                         IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
511                                                                                         IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
512                                                                                         ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
513                                                                                         TRACE_(dmfile)(": %s chunk (size = %ld)", debugstr_fourcc (chunkID), chunkSize);
514                                                                                         switch (chunkID) {
515                                                                                                 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
516                                               (though strings seem to be valid unicode) */
517                                                                                                 case mmioFOURCC('I','N','A','M'):
518                                                                                                 case DMUS_FOURCC_UNAM_CHUNK: {
519                                                                                                         TRACE_(dmfile)(": name chunk\n");
520                                                                                                         This->pDesc->dwValidData |= DMUS_OBJ_NAME;
521                                                                                                         IStream_Read (pStm, This->pDesc->wszName, chunkSize, NULL);
522                                                                                                         break;
523                                                                                                 }
524                                                                                                 case mmioFOURCC('I','A','R','T'):
525                                                                                                 case DMUS_FOURCC_UART_CHUNK: {
526                                                                                                         TRACE_(dmfile)(": artist chunk (ignored)\n");
527                                                                                                         liMove.QuadPart = chunkSize;
528                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
529                                                                                                         break;
530                                                                                                 }
531                                                                                                 case mmioFOURCC('I','C','O','P'):
532                                                                                                 case DMUS_FOURCC_UCOP_CHUNK: {
533                                                                                                         TRACE_(dmfile)(": copyright chunk (ignored)\n");
534                                                                                                         liMove.QuadPart = chunkSize;
535                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
536                                                                                                         break;
537                                                                                                 }
538                                                                                                 case mmioFOURCC('I','S','B','J'):
539                                                                                                 case DMUS_FOURCC_USBJ_CHUNK: {
540                                                                                                         TRACE_(dmfile)(": subject chunk (ignored)\n");
541                                                                                                         liMove.QuadPart = chunkSize;
542                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
543                                                                                                         break;
544                                                                                                 }
545                                                                                                 case mmioFOURCC('I','C','M','T'):
546                                                                                                 case DMUS_FOURCC_UCMT_CHUNK: {
547                                                                                                         TRACE_(dmfile)(": comment chunk (ignored)\n");
548                                                                                                         liMove.QuadPart = chunkSize;
549                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
550                                                                                                         break;
551                                                                                                 }
552                                                                                                 default: {
553                                                                                                         TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
554                                                                                                         liMove.QuadPart = chunkSize;
555                                                                                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
556                                                                                                         break;                                          
557                                                                                                 }
558                                                                                         }
559                                                                                         TRACE_(dmfile)(": ListCount[0] = %ld < ListSize[0] = %ld\n", ListCount[0], ListSize[0]);
560                                                                                 } while (ListCount[0] < ListSize[0]);
561                                                                                 break;
562                                                                         }
563                                                                         default: {
564                                                                                 TRACE_(dmfile)(": unknown (skipping)\n");
565                                                                                 liMove.QuadPart = chunkSize - sizeof(FOURCC);
566                                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
567                                                                                 break;                                          
568                                                                         }
569                                                                 }
570                                                                 break;
571                                                         }       
572                                                         default: {
573                                                                 TRACE_(dmfile)(": unknown chunk (irrevelant & skipping)\n");
574                                                                 liMove.QuadPart = chunkSize;
575                                                                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
576                                                                 break;                                          
577                                                         }
578                                                 }
579                                                 TRACE_(dmfile)(": StreamCount[0] = %ld < StreamSize[0] = %ld\n", StreamCount, StreamSize);
580                                         } while (StreamCount < StreamSize);
581                                         break;
582                                 }
583                                 default: {
584                                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
585                                         liMove.QuadPart = StreamSize;
586                                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
587                                         return E_FAIL;
588                                 }
589                         }
590                         TRACE_(dmfile)(": reading finished\n");
591                         break;
592                 }
593                 default: {
594                         TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
595                         liMove.QuadPart = chunkSize;
596                         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
597                         return E_FAIL;
598                 }
599         }
600
601         return S_OK;
602 }
603
604 HRESULT WINAPI IDirectMusicAudioPathImpl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
605         return E_NOTIMPL;
606 }
607
608 HRESULT WINAPI IDirectMusicAudioPathImpl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
609         return E_NOTIMPL;
610 }
611
612 ICOM_VTABLE(IPersistStream) DirectMusicAudioPath_PersistStream_Vtbl = {
613     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
614         IDirectMusicAudioPathImpl_IPersistStream_QueryInterface,
615         IDirectMusicAudioPathImpl_IPersistStream_AddRef,
616         IDirectMusicAudioPathImpl_IPersistStream_Release,
617         IDirectMusicAudioPathImpl_IPersistStream_GetClassID,
618         IDirectMusicAudioPathImpl_IPersistStream_IsDirty,
619         IDirectMusicAudioPathImpl_IPersistStream_Load,
620         IDirectMusicAudioPathImpl_IPersistStream_Save,
621         IDirectMusicAudioPathImpl_IPersistStream_GetSizeMax
622 };
623
624 /* for ClassFactory */
625 HRESULT WINAPI DMUSIC_CreateDirectMusicAudioPathImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
626         IDirectMusicAudioPathImpl* obj;
627         
628         obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicAudioPathImpl));
629         if (NULL == obj) {
630                 *ppobj = (LPVOID) NULL;
631                 return E_OUTOFMEMORY;
632         }
633         obj->UnknownVtbl = &DirectMusicAudioPath_Unknown_Vtbl;
634         obj->AudioPathVtbl = &DirectMusicAudioPath_AudioPath_Vtbl;
635         obj->ObjectVtbl = &DirectMusicAudioPath_Object_Vtbl;
636         obj->PersistStreamVtbl = &DirectMusicAudioPath_PersistStream_Vtbl;
637         obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
638         DM_STRUCT_INIT(obj->pDesc);
639         obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
640         memcpy (&obj->pDesc->guidClass, &CLSID_DirectMusicAudioPath, sizeof (CLSID));
641         obj->ref = 0; /* will be inited by QueryInterface */
642         
643         return IDirectMusicAudioPathImpl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
644 }