mshtml: Use fregment-less URI in super_navigate.
[wine] / dlls / dmband / bandtrack.c
1 /* IDirectMusicBandTrack Implementation
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License 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 "dmband_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(dmband);
23 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
24
25 /*****************************************************************************
26  * IDirectMusicBandTrack implementation
27  */
28 /* IDirectMusicBandTrack IUnknown part: */
29 static HRESULT WINAPI IDirectMusicBandTrack_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
30         ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
31         TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
32
33         if (IsEqualIID (riid, &IID_IUnknown)) {
34                 *ppobj = &This->UnknownVtbl;
35                 IUnknown_AddRef (iface);
36                 return S_OK;
37         } else if (IsEqualIID (riid, &IID_IDirectMusicTrack)
38           || IsEqualIID (riid, &IID_IDirectMusicTrack8)) {
39                 *ppobj = (LPDIRECTMUSICTRACK8)&This->TrackVtbl;
40                 IUnknown_AddRef (iface);
41                 return S_OK;
42         } else if (IsEqualIID (riid, &IID_IPersistStream)) {
43                 *ppobj = &This->PersistStreamVtbl;
44                 IUnknown_AddRef (iface);
45                 return S_OK;
46         }
47         
48         WARN("(%p, %s,%p): not found\n", This, debugstr_dmguid(riid), ppobj);
49         return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI IDirectMusicBandTrack_IUnknown_AddRef (LPUNKNOWN iface) {
53   ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
54   ULONG ref = InterlockedIncrement(&This->ref);
55
56   TRACE("(%p) : AddRef from %d\n", This, ref - 1);
57
58   DMBAND_LockModule();
59
60   return ref;
61 }
62
63 static ULONG WINAPI IDirectMusicBandTrack_IUnknown_Release (LPUNKNOWN iface) {
64   ICOM_THIS_MULTI(IDirectMusicBandTrack, UnknownVtbl, iface);
65   ULONG ref = InterlockedDecrement(&This->ref);
66
67   TRACE("(%p) : ReleaseRef to %d\n", This, ref);
68
69   if (ref == 0) {
70     HeapFree(GetProcessHeap(), 0, This);
71   }
72
73   DMBAND_UnlockModule();
74   
75   return ref;
76 }
77
78 static const IUnknownVtbl DirectMusicBandTrack_Unknown_Vtbl = {
79   IDirectMusicBandTrack_IUnknown_QueryInterface,
80   IDirectMusicBandTrack_IUnknown_AddRef,
81   IDirectMusicBandTrack_IUnknown_Release
82 };
83
84 /* IDirectMusicBandTrack IDirectMusicTrack8 part: */
85 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_QueryInterface (LPDIRECTMUSICTRACK8 iface, REFIID riid, LPVOID *ppobj) {
86   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
87   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
88 }
89
90 static ULONG WINAPI IDirectMusicBandTrack_IDirectMusicTrack_AddRef (LPDIRECTMUSICTRACK8 iface) {
91   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
92   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
93 }
94
95 static ULONG WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Release (LPDIRECTMUSICTRACK8 iface) {
96   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
97   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
98 }
99
100 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Init (LPDIRECTMUSICTRACK8 iface, IDirectMusicSegment* pSegment) {
101   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
102   FIXME("(%p, %p): stub\n", This, pSegment);
103   return S_OK;
104 }
105
106 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_InitPlay(LPDIRECTMUSICTRACK8 iface, IDirectMusicSegmentState* segment_state, IDirectMusicPerformance* performance, void** state_data, DWORD virtual_track8id, DWORD flags)
107 {
108     ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
109
110     FIXME("(%p, %p, %p, %p, %d, %x): stub\n", This, segment_state, performance, state_data, virtual_track8id, flags);
111
112     return S_OK;
113 }
114
115 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_EndPlay (LPDIRECTMUSICTRACK8 iface, void* pStateData) {
116   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
117   FIXME("(%p, %p): stub\n", This, pStateData);
118   return S_OK;
119 }
120
121 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Play(LPDIRECTMUSICTRACK8 iface, void* state_data, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, MUSIC_TIME mtOffset, DWORD flags, IDirectMusicPerformance* performance, IDirectMusicSegmentState* segment_state, DWORD virtual_id)
122 {
123     ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
124
125     FIXME("(%p, %p, %d, %d, %d, %x, %p, %p, %d): semi-stub\n", This, state_data, mtStart, mtEnd, mtOffset, flags, performance, segment_state, virtual_id);
126
127     /* Sends following pMSG:
128        - DMUS_PATCH_PMSG
129        - DMUS_TRANSPOSE_PMSG
130        - DMUS_CHANNEL_PRIORITY_PMSG
131        - DMUS_MIDI_PMSG
132     */
133
134     return S_OK;
135 }
136
137 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_GetParam (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
138   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
139   FIXME("(%p, %s, %d, %p, %p): stub\n", This, debugstr_dmguid(rguidType), mtTime, pmtNext, pParam);
140   return S_OK;
141 }
142
143 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_SetParam (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, MUSIC_TIME mtTime, void* pParam) {
144   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
145   FIXME("(%p, %s, %d, %p): stub\n", This, debugstr_dmguid(rguidType), mtTime, pParam);
146   return S_OK;
147 }
148
149 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_IsParamSupported (LPDIRECTMUSICTRACK8 iface, REFGUID rguidType) {
150   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
151   
152   TRACE("(%p, %s)\n", This, debugstr_dmguid(rguidType));
153   if (IsEqualGUID (rguidType, &GUID_BandParam)
154       || IsEqualGUID (rguidType, &GUID_Clear_All_Bands)
155       || IsEqualGUID (rguidType, &GUID_ConnectToDLSCollection)
156       || IsEqualGUID (rguidType, &GUID_Disable_Auto_Download)
157       || IsEqualGUID (rguidType, &GUID_Download)
158       || IsEqualGUID (rguidType, &GUID_DownloadToAudioPath)
159       || IsEqualGUID (rguidType, &GUID_Enable_Auto_Download)
160       || IsEqualGUID (rguidType, &GUID_IDirectMusicBand)
161       || IsEqualGUID (rguidType, &GUID_StandardMIDIFile)
162       || IsEqualGUID (rguidType, &GUID_Unload)
163       || IsEqualGUID (rguidType, &GUID_UnloadFromAudioPath)) {
164     TRACE("param supported\n");
165     return S_OK;
166   }
167   
168   TRACE("param unsupported\n");
169   return DMUS_E_TYPE_UNSUPPORTED;
170 }
171
172 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_AddNotificationType (LPDIRECTMUSICTRACK8 iface, REFGUID rguidNotificationType) {
173   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
174   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
175   return S_OK;
176 }
177
178 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_RemoveNotificationType (LPDIRECTMUSICTRACK8 iface, REFGUID rguidNotificationType) {
179   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
180   FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
181   return S_OK;
182 }
183
184 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Clone (LPDIRECTMUSICTRACK8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicTrack** ppTrack) {
185   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
186   FIXME("(%p, %d, %d, %p): stub\n", This, mtStart, mtEnd, ppTrack);
187   return S_OK;
188 }
189
190 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_PlayEx(LPDIRECTMUSICTRACK8 iface, void* state_data, REFERENCE_TIME rtStart, REFERENCE_TIME rtEnd, REFERENCE_TIME rtOffset, DWORD flags, IDirectMusicPerformance* performance, IDirectMusicSegmentState* segment_state, DWORD virtual_id)
191 {
192     ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
193
194     FIXME("(%p, %p, 0x%s, 0x%s, 0x%s, %x, %p, %p, %d): stub\n", This, state_data, wine_dbgstr_longlong(rtStart),
195         wine_dbgstr_longlong(rtEnd), wine_dbgstr_longlong(rtOffset), flags, performance, segment_state, virtual_id);
196
197     return S_OK;
198 }
199
200 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_GetParamEx(LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, REFERENCE_TIME rtTime, REFERENCE_TIME* rtNext, void* param, void* state_data, DWORD flags)
201 {
202     ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
203
204     FIXME("(%p, %s, 0x%s, %p, %p, %p, %x): stub\n", This, debugstr_dmguid(rguidType),
205         wine_dbgstr_longlong(rtTime), rtNext, param, state_data, flags);
206
207     return S_OK;
208 }
209
210 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_SetParamEx(LPDIRECTMUSICTRACK8 iface, REFGUID rguidType, REFERENCE_TIME rtTime, void* param, void* state_data, DWORD flags)
211 {
212     ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
213
214     FIXME("(%p, %s, 0x%s, %p, %p, %x): stub\n", This, debugstr_dmguid(rguidType),
215         wine_dbgstr_longlong(rtTime), param, state_data, flags);
216
217     return S_OK;
218 }
219
220 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Compose (LPDIRECTMUSICTRACK8 iface, IUnknown* pContext, DWORD dwTrackGroup, IDirectMusicTrack** ppResultTrack) {
221   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
222   FIXME("(%p, %p, %d, %p): stub\n", This, pContext, dwTrackGroup, ppResultTrack);
223   return S_OK;
224 }
225
226 static HRESULT WINAPI IDirectMusicBandTrack_IDirectMusicTrack_Join (LPDIRECTMUSICTRACK8 iface, IDirectMusicTrack* pNewTrack, MUSIC_TIME mtJoin, IUnknown* pContext, DWORD dwTrackGroup, IDirectMusicTrack** ppResultTrack) {
227   ICOM_THIS_MULTI(IDirectMusicBandTrack, TrackVtbl, iface);
228   FIXME("(%p, %p, %d, %p, %d, %p): stub\n", This, pNewTrack, mtJoin, pContext, dwTrackGroup, ppResultTrack);
229   return S_OK;
230 }
231
232 static const IDirectMusicTrack8Vtbl DirectMusicBandTrack_DirectMusicTrack_Vtbl = {
233   IDirectMusicBandTrack_IDirectMusicTrack_QueryInterface,
234   IDirectMusicBandTrack_IDirectMusicTrack_AddRef,
235   IDirectMusicBandTrack_IDirectMusicTrack_Release,
236   IDirectMusicBandTrack_IDirectMusicTrack_Init,
237   IDirectMusicBandTrack_IDirectMusicTrack_InitPlay,
238   IDirectMusicBandTrack_IDirectMusicTrack_EndPlay,
239   IDirectMusicBandTrack_IDirectMusicTrack_Play,
240   IDirectMusicBandTrack_IDirectMusicTrack_GetParam,
241   IDirectMusicBandTrack_IDirectMusicTrack_SetParam,
242   IDirectMusicBandTrack_IDirectMusicTrack_IsParamSupported,
243   IDirectMusicBandTrack_IDirectMusicTrack_AddNotificationType,
244   IDirectMusicBandTrack_IDirectMusicTrack_RemoveNotificationType,
245   IDirectMusicBandTrack_IDirectMusicTrack_Clone,
246   IDirectMusicBandTrack_IDirectMusicTrack_PlayEx,
247   IDirectMusicBandTrack_IDirectMusicTrack_GetParamEx,
248   IDirectMusicBandTrack_IDirectMusicTrack_SetParamEx,
249   IDirectMusicBandTrack_IDirectMusicTrack_Compose,
250   IDirectMusicBandTrack_IDirectMusicTrack_Join
251 };
252
253 /* IDirectMusicBandTrack IPersistStream part: */
254 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
255   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
256   return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
257 }
258
259 static ULONG WINAPI IDirectMusicBandTrack_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
260   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
261   return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
262 }
263
264 static ULONG WINAPI IDirectMusicBandTrack_IPersistStream_Release (LPPERSISTSTREAM iface) {
265   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
266   return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
267 }
268
269 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
270   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
271   TRACE("(%p, %p)\n", This, pClassID);
272   *pClassID = CLSID_DirectMusicBandTrack;
273   return S_OK;
274 }
275
276 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
277   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
278   FIXME("(%p): stub, always S_FALSE\n", This);
279   return S_FALSE;
280 }
281
282 static HRESULT IDirectMusicBandTrack_IPersistStream_LoadBand (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicBand** ppBand,
283                                                               DMUS_PRIVATE_BAND_ITEM_HEADER* pHeader) {
284
285   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
286   HRESULT hr = E_FAIL;
287   IPersistStream* pPersistStream = NULL;
288   
289   hr = CoCreateInstance (&CLSID_DirectMusicBand, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicBand, (LPVOID*) ppBand);
290   if (FAILED(hr)) {
291     ERR(": could not create object\n");
292     return hr;
293   }
294   /* acquire PersistStream interface */
295   hr = IDirectMusicBand_QueryInterface (*ppBand, &IID_IPersistStream, (LPVOID*) &pPersistStream);
296   if (FAILED(hr)) {
297     ERR(": could not acquire IPersistStream\n");
298     return hr;
299   }
300   /* load */
301   hr = IPersistStream_Load (pPersistStream, pClonedStream);
302   if (FAILED(hr)) {
303     ERR(": failed to load object\n");
304     return hr;
305   }
306   
307   /* release all loading-related stuff */
308   IPersistStream_Release (pPersistStream);
309
310   /*
311    * @TODO insert pBand into This
312    */
313   if (SUCCEEDED(hr)) {
314     LPDMUS_PRIVATE_BAND pNewBand = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_BAND));
315     if (NULL == pNewBand) {
316       ERR(": no more memory\n");
317       return  E_OUTOFMEMORY;
318     }
319     pNewBand->BandHeader = *pHeader;
320     pNewBand->pBand = (IDirectMusicBandImpl*)((char*)(*ppBand) - offsetof(IDirectMusicBandImpl,BandVtbl));
321     IDirectMusicBand_AddRef(*ppBand);
322     list_add_tail (&This->Bands, &pNewBand->entry);
323   }
324
325   return S_OK;
326 }
327
328 static HRESULT IDirectMusicBandTrack_IPersistStream_ParseBandsList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
329
330   /*ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);*/
331   HRESULT hr = E_FAIL;
332   DMUS_PRIVATE_CHUNK Chunk;
333   DWORD StreamSize, ListSize[3], ListCount[3];
334   LARGE_INTEGER liMove; /* used when skipping chunks */
335
336   IDirectMusicBand* pBand = NULL;
337   DMUS_PRIVATE_BAND_ITEM_HEADER header;
338
339   memset(&header, 0, sizeof header);
340
341   if (pChunk->fccID != DMUS_FOURCC_BANDS_LIST) {
342     ERR_(dmfile)(": %s chunk should be a BANDS list\n", debugstr_fourcc (pChunk->fccID));
343     return E_FAIL;
344   }  
345
346   ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
347   ListCount[0] = 0;
348
349   do {
350     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
351     ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
352     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
353     switch (Chunk.fccID) {
354     case FOURCC_LIST: {
355       IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
356       TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
357       ListSize[1] = Chunk.dwSize - sizeof(FOURCC);
358       ListCount[1] = 0;
359       do {
360         IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
361         ListCount[1] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
362         TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
363         switch (Chunk.fccID) { 
364         case DMUS_FOURCC_BANDITEM_CHUNK: {
365           DMUS_IO_BAND_ITEM_HEADER tmp_header;
366           TRACE_(dmfile)(": Band Item chunk v1\n");
367           
368           IStream_Read (pStm, &tmp_header, sizeof(DMUS_IO_BAND_ITEM_HEADER), NULL);
369           TRACE_(dmfile)(" - lBandTime: %u\n", tmp_header.lBandTime);
370
371           header.dwVersion = 1;
372           header.lBandTime = tmp_header.lBandTime;
373           break;
374         }
375         case DMUS_FOURCC_BANDITEM_CHUNK2: { 
376           DMUS_IO_BAND_ITEM_HEADER2 tmp_header2;
377           TRACE_(dmfile)(": Band Item chunk v2\n");
378           
379           IStream_Read (pStm, &tmp_header2, sizeof(DMUS_IO_BAND_ITEM_HEADER2), NULL);
380           TRACE_(dmfile)(" - lBandTimeLogical: %u\n", tmp_header2.lBandTimeLogical);
381           TRACE_(dmfile)(" - lBandTimePhysical: %u\n", tmp_header2.lBandTimePhysical);
382
383           header.dwVersion = 2;
384           header.lBandTimeLogical = tmp_header2.lBandTimeLogical;
385           header.lBandTimePhysical = tmp_header2.lBandTimePhysical;
386           break;
387         }
388         case FOURCC_RIFF: { 
389           IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
390           TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
391           StreamSize = Chunk.dwSize - sizeof(FOURCC);
392           switch (Chunk.fccID) {
393           case DMUS_FOURCC_BAND_FORM: {
394             LPSTREAM pClonedStream = NULL;
395             TRACE_(dmfile)(": BAND RIFF\n");
396             
397             IStream_Clone (pStm, &pClonedStream);
398             
399             liMove.QuadPart = 0;
400             liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
401             IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
402             
403             hr = IDirectMusicBandTrack_IPersistStream_LoadBand (iface, pClonedStream, &pBand, &header);
404             if (FAILED(hr)) {
405               ERR(": could not load track\n");
406               return hr;
407             }
408             IStream_Release (pClonedStream);
409             
410             IDirectMusicTrack_Release(pBand); pBand = NULL; /* now we can release at as it inserted */
411             
412             /** now safe move the cursor */
413             liMove.QuadPart = StreamSize;
414             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
415             break;
416           }
417           default: {
418             TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
419             liMove.QuadPart = StreamSize;
420             IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
421             break;                                              
422           }
423           }
424           break;
425         }
426         default: {
427           TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
428           liMove.QuadPart = Chunk.dwSize;
429           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
430           break;                                                
431          }
432         }
433         TRACE_(dmfile)(": ListCount[1] = %d < ListSize[1] = %d\n", ListCount[1], ListSize[1]);
434       } while (ListCount[1] < ListSize[1]);
435       break;
436     }
437     default: {
438       TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
439       liMove.QuadPart = Chunk.dwSize;
440       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
441       break;                                            
442     }
443     }
444     TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
445   } while (ListCount[0] < ListSize[0]);
446
447   return S_OK;
448 }
449
450 static HRESULT IDirectMusicBandTrack_IPersistStream_ParseBandTrackForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
451
452   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
453   HRESULT hr = E_FAIL;
454   DMUS_PRIVATE_CHUNK Chunk;
455   DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
456   LARGE_INTEGER liMove; /* used when skipping chunks */
457
458   if (pChunk->fccID != DMUS_FOURCC_BANDTRACK_FORM) {
459     ERR_(dmfile)(": %s chunk should be a BANDTRACK form\n", debugstr_fourcc (pChunk->fccID));
460     return E_FAIL;
461   }  
462
463   StreamSize = pChunk->dwSize - sizeof(FOURCC);
464   StreamCount = 0;
465
466   do {
467     IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
468     StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
469     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
470     
471     hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
472     if (FAILED(hr)) return hr;
473
474     if (hr == S_FALSE) {
475       switch (Chunk.fccID) {
476       case DMUS_FOURCC_BANDTRACK_CHUNK: {
477         TRACE_(dmfile)(": BandTrack chunk\n");
478         IStream_Read (pStm, &This->header, sizeof(DMUS_IO_BAND_TRACK_HEADER), NULL);
479         TRACE_(dmfile)(" - bAutoDownload: %u\n", This->header.bAutoDownload);
480         break;
481       }   
482       case FOURCC_LIST: {
483         IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
484         TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
485         ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
486         ListCount[0] = 0;
487         switch (Chunk.fccID) {
488         case DMUS_FOURCC_UNFO_LIST: { 
489           TRACE_(dmfile)(": UNFO list\n");
490           do {
491             IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
492             ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
493             TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
494             
495             hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
496             if (FAILED(hr)) return hr;
497             
498             if (hr == S_FALSE) {
499               switch (Chunk.fccID) {
500               default: {
501                 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
502                 liMove.QuadPart = Chunk.dwSize;
503                 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
504                 break;                          
505               }
506               }
507             }  
508             TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
509           } while (ListCount[0] < ListSize[0]);
510           break;
511         }
512         case DMUS_FOURCC_BANDS_LIST: {
513           TRACE_(dmfile)(": TRACK list\n");
514           hr = IDirectMusicBandTrack_IPersistStream_ParseBandsList (iface, &Chunk, pStm);
515           if (FAILED(hr)) return hr;
516           break;
517         }
518         default: {
519           TRACE_(dmfile)(": unknown (skipping)\n");
520           liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
521           IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
522           break;                                                
523         }
524         }
525         break;
526       }
527       default: {
528         TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
529         liMove.QuadPart = Chunk.dwSize;
530         IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
531         break;                                          
532       }
533       }
534     }
535     TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
536   } while (StreamCount < StreamSize);  
537
538   return S_OK;
539 }
540
541
542 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
543   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
544
545   DMUS_PRIVATE_CHUNK Chunk;
546   LARGE_INTEGER liMove;
547   HRESULT hr;
548
549   TRACE("(%p, %p): Loading\n", This, pStm);
550
551   IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
552   TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
553   switch (Chunk.fccID) {
554   case FOURCC_RIFF: {
555     IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
556     TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
557     switch (Chunk.fccID) {
558     case DMUS_FOURCC_BANDTRACK_FORM: {
559       TRACE_(dmfile)(": Band track form\n");
560       hr = IDirectMusicBandTrack_IPersistStream_ParseBandTrackForm (iface, &Chunk, pStm);
561       if (FAILED(hr)) return hr;
562       break;    
563     }
564     default: {
565       TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
566       liMove.QuadPart = Chunk.dwSize;
567       IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
568       return E_FAIL;
569     }
570     }
571     TRACE_(dmfile)(": reading finished\n");
572     break;
573   }
574   default: {
575     TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
576     liMove.QuadPart = Chunk.dwSize;
577     IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
578     return E_FAIL;
579   }
580   }
581
582   return S_OK;
583 }
584
585 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
586   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
587   FIXME("(%p): Saving not implemented yet\n", This);
588   return E_NOTIMPL;
589 }
590
591 static HRESULT WINAPI IDirectMusicBandTrack_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
592   ICOM_THIS_MULTI(IDirectMusicBandTrack, PersistStreamVtbl, iface);
593   FIXME("(%p, %p): stub\n", This, pcbSize);
594   return E_NOTIMPL;
595 }
596
597 static const IPersistStreamVtbl DirectMusicBandTrack_PerststStream_Vtbl = {
598   IDirectMusicBandTrack_IPersistStream_QueryInterface,
599   IDirectMusicBandTrack_IPersistStream_AddRef,
600   IDirectMusicBandTrack_IPersistStream_Release,
601   IDirectMusicBandTrack_IPersistStream_GetClassID,
602   IDirectMusicBandTrack_IPersistStream_IsDirty,
603   IDirectMusicBandTrack_IPersistStream_Load,
604   IDirectMusicBandTrack_IPersistStream_Save,
605   IDirectMusicBandTrack_IPersistStream_GetSizeMax
606 };
607
608 /* for ClassFactory */
609 HRESULT WINAPI DMUSIC_CreateDirectMusicBandTrack (LPCGUID lpcGUID, LPVOID *ppobj, LPUNKNOWN pUnkOuter) {
610   IDirectMusicBandTrack* track;
611         
612   track = HeapAlloc (GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBandTrack));
613   if (NULL == track) {
614     *ppobj = NULL;
615     return E_OUTOFMEMORY;
616   }
617   track->UnknownVtbl = &DirectMusicBandTrack_Unknown_Vtbl;
618   track->TrackVtbl = &DirectMusicBandTrack_DirectMusicTrack_Vtbl;
619   track->PersistStreamVtbl = &DirectMusicBandTrack_PerststStream_Vtbl;
620   track->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
621   DM_STRUCT_INIT(track->pDesc);
622   track->pDesc->dwValidData |= DMUS_OBJ_CLASS;
623   track->pDesc->guidClass = CLSID_DirectMusicBandTrack;
624   track->ref = 0; /* will be inited by QueryInterface */
625   list_init (&track->Bands);
626
627   return IDirectMusicBandTrack_IUnknown_QueryInterface ((LPUNKNOWN)&track->UnknownVtbl, lpcGUID, ppobj);
628 }