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