segment.c 50 KB
Newer Older
1
/* IDirectMusicSegment8 Implementation
2
 *
3
 * Copyright (C) 2003-2004 Rok Mandeljc
4
 * Copyright (C) 2003-2004 Raphael Junqueira
5
 *
6 7 8 9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
10 11 12
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
15
 *
16 17 18
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21
#include "dmime_private.h"
22

23 24
WINE_DEFAULT_DEBUG_CHANNEL(dmime);
WINE_DECLARE_DEBUG_CHANNEL(dmfile);
25

26
/*****************************************************************************
27
 * IDirectMusicSegmentImpl implementation
28
 */
29
/* IDirectMusicSegment IUnknown part: */
30
static HRESULT WINAPI IDirectMusicSegment8Impl_IUnknown_QueryInterface (LPUNKNOWN iface, REFIID riid, LPVOID *ppobj) {
31
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, UnknownVtbl, iface);
32
	TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ppobj);
33 34
	
	if (IsEqualIID (riid, &IID_IUnknown)) {
35
		*ppobj = &This->UnknownVtbl;
36
		IUnknown_AddRef (iface);
37 38 39 40
		return S_OK;	
	} else if (IsEqualIID (riid, &IID_IDirectMusicSegment)
	|| IsEqualIID (riid, &IID_IDirectMusicSegment2)
	|| IsEqualIID (riid, &IID_IDirectMusicSegment8)) {
41
		*ppobj = &This->SegmentVtbl;
42
		IUnknown_AddRef (iface);
43 44
		return S_OK;
	} else if (IsEqualIID (riid, &IID_IDirectMusicObject)) {
45
		*ppobj = &This->ObjectVtbl;
46
		IUnknown_AddRef (iface);
47 48
		return S_OK;
	} else if (IsEqualIID (riid, &IID_IPersistStream)) {
49
		*ppobj = &This->PersistStreamVtbl;
50
		IUnknown_AddRef (iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
51
		return S_OK;
52
	}
53
	
54
	WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ppobj);
55 56 57
	return E_NOINTERFACE;
}

58
static ULONG WINAPI IDirectMusicSegment8Impl_IUnknown_AddRef (LPUNKNOWN iface) {
59
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, UnknownVtbl, iface);
60 61
        ULONG ref = InterlockedIncrement(&This->ref);

62
	TRACE("(%p): AddRef from %d\n", This, ref - 1);
63

64 65
	DMIME_LockModule();

66
	return ref;
67 68
}

Mike McCormack's avatar
Mike McCormack committed
69
static ULONG WINAPI IDirectMusicSegment8Impl_IUnknown_Release (LPUNKNOWN iface) {
70
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, UnknownVtbl, iface);
71
	ULONG ref = InterlockedDecrement(&This->ref);
72
	TRACE("(%p): ReleaseRef to %d\n", This, ref);
73
	
74
	if (ref == 0) {
75 76
		HeapFree(GetProcessHeap(), 0, This);
	}
77 78 79

	DMIME_UnlockModule();
	
80 81 82
	return ref;
}

83
static const IUnknownVtbl DirectMusicSegment8_Unknown_Vtbl = {
84 85 86
  IDirectMusicSegment8Impl_IUnknown_QueryInterface,
  IDirectMusicSegment8Impl_IUnknown_AddRef,
  IDirectMusicSegment8Impl_IUnknown_Release
87
};
Rok Mandeljc's avatar
Rok Mandeljc committed
88

89
/* IDirectMusicSegmentImpl IDirectMusicSegment part: */
90
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_QueryInterface (LPDIRECTMUSICSEGMENT8 iface, REFIID riid, LPVOID *ppobj) {
91
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
92
  return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
93 94
}

95
static ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_AddRef (LPDIRECTMUSICSEGMENT8 iface) {
96
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
97
  return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
98
}
Rok Mandeljc's avatar
Rok Mandeljc committed
99

Mike McCormack's avatar
Mike McCormack committed
100
static ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Release (LPDIRECTMUSICSEGMENT8 iface) {
101
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
102
  return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
103
}
Rok Mandeljc's avatar
Rok Mandeljc committed
104

Mike McCormack's avatar
Mike McCormack committed
105
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLength (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtLength) {
106 107 108 109 110 111 112
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  TRACE("(%p, %p)\n", This, pmtLength);
  if (NULL == pmtLength) {
    return E_POINTER;
  }
  *pmtLength = This->header.mtLength;
  return S_OK;
113 114
}

Mike McCormack's avatar
Mike McCormack committed
115
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLength (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtLength) {
116
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
117
  TRACE("(%p, %d)\n", This, mtLength);
118 119
  This->header.mtLength = mtLength;
  return S_OK;
120 121
}

Mike McCormack's avatar
Mike McCormack committed
122
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetRepeats (LPDIRECTMUSICSEGMENT8 iface, DWORD* pdwRepeats) { 
123 124 125 126 127 128 129
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  TRACE("(%p, %p)\n", This, pdwRepeats);
  if (NULL == pdwRepeats) {
    return E_POINTER;
  }
  *pdwRepeats = This->header.dwRepeats;
  return S_OK;
130 131
}

Mike McCormack's avatar
Mike McCormack committed
132
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetRepeats (LPDIRECTMUSICSEGMENT8 iface, DWORD dwRepeats) {
133
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
134
  TRACE("(%p, %d)\n", This, dwRepeats);
135 136
  This->header.dwRepeats = dwRepeats;
  return S_OK;
137 138
}

Mike McCormack's avatar
Mike McCormack committed
139
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetDefaultResolution (LPDIRECTMUSICSEGMENT8 iface, DWORD* pdwResolution) {
140 141 142 143 144 145 146
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  TRACE("(%p, %p)\n", This, pdwResolution);
  if (NULL == pdwResolution) {
    return E_POINTER;
  }
  *pdwResolution = This->header.dwResolution;
  return S_OK;
147 148
}

Mike McCormack's avatar
Mike McCormack committed
149
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetDefaultResolution (LPDIRECTMUSICSEGMENT8 iface, DWORD dwResolution) {
150
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
151
  TRACE("(%p, %d)\n", This, dwResolution);
152 153
  This->header.dwResolution = dwResolution;
  return S_OK;
154
}
Rok Mandeljc's avatar
Rok Mandeljc committed
155

Mike McCormack's avatar
Mike McCormack committed
156
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, IDirectMusicTrack** ppTrack) {
157
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
158 159 160 161 162 163 164
  
  CLSID pIt_clsid;
  struct list* pEntry = NULL;
  LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
  IPersistStream* pCLSIDStream = NULL;
  HRESULT hr = S_OK;

165
  TRACE("(%p, %s, %d, 0x%x, %p)\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, ppTrack);
166 167 168 169 170 171 172

  if (NULL == ppTrack) {
    return E_POINTER;
  }

  LIST_FOR_EACH (pEntry, &This->Tracks) {
    pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
173
    TRACE(" - %p -> 0x%x,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
174 175 176 177
    if (0xFFFFFFFF != dwGroupBits && 0 == (pIt->dwGroupBits & dwGroupBits)) continue ;
    if (FALSE == IsEqualGUID(&GUID_NULL, rguidType)) {
      /**
       * it rguidType is not null we must check if CLSID are equals
Austin English's avatar
Austin English committed
178
       * and the unique way to get it is using IPersistStream Interface
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
       */
      hr = IDirectMusicTrack_QueryInterface(pIt->pTrack, &IID_IPersistStream, (void**) &pCLSIDStream);
      if (FAILED(hr)) {
	ERR("(%p): object %p don't implement IPersistStream Interface. Expect a crash (critical problem)\n", This, pIt->pTrack);
	continue ;
      }
      hr = IPersistStream_GetClassID(pCLSIDStream, &pIt_clsid);
      IPersistStream_Release(pCLSIDStream); pCLSIDStream = NULL;
      if (FAILED(hr)) {
	ERR("(%p): non-implemented GetClassID for object %p\n", This, pIt->pTrack);
	continue ;
      }
      TRACE(" - %p -> %s\n", pIt, debugstr_dmguid(&pIt_clsid));
      if (FALSE == IsEqualGUID(&pIt_clsid, rguidType)) continue ;
    }
    if (0 == dwIndex) {
      *ppTrack = pIt->pTrack;
      IDirectMusicTrack_AddRef(*ppTrack);
      return S_OK;
    } 
    --dwIndex;
  }  
  return DMUS_E_NOT_FOUND;
202 203
}

Mike McCormack's avatar
Mike McCormack committed
204
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrackGroup (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicTrack* pTrack, DWORD* pdwGroupBits) {
205
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
206 207 208 209 210 211 212 213 214 215 216 217

  struct list* pEntry = NULL;
  LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;

  TRACE("(%p, %p, %p)\n", This, pTrack, pdwGroupBits);

  if (NULL == pdwGroupBits) {
    return E_POINTER;
  }

  LIST_FOR_EACH (pEntry, &This->Tracks) {
    pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
218
    TRACE(" - %p -> %d,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
219 220 221 222 223 224 225
    if (NULL != pIt && pIt->pTrack == pTrack) {
      *pdwGroupBits = pIt->dwGroupBits;
      return S_OK;
    }
  }

  return DMUS_E_NOT_FOUND;
226 227
}

Mike McCormack's avatar
Mike McCormack committed
228
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicTrack* pTrack, DWORD dwGroupBits) {
229
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
230 231

  DWORD i = 0;
232
  struct list* pEntry = NULL;
233
  LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
234
  LPDMUS_PRIVATE_SEGMENT_TRACK pNewSegTrack = NULL;
235

236
  TRACE("(%p, %p, %d)\n", This, pTrack, dwGroupBits);
237 238 239 240

  LIST_FOR_EACH (pEntry, &This->Tracks) {
    i++;
    pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
241
    TRACE(" - #%u: %p -> %d,%p\n", i, pIt, pIt->dwGroupBits, pIt->pTrack);
242 243 244 245 246 247 248 249 250 251 252 253 254
    if (NULL != pIt && pIt->pTrack == pTrack) {
      ERR("(%p, %p): track is already in list\n", This, pTrack);
      return E_FAIL;
    }
  }

  pNewSegTrack = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_SEGMENT_TRACK));
  if (NULL == pNewSegTrack) {
    ERR(": no more memory\n");
    return  E_OUTOFMEMORY;
  }
  pNewSegTrack->dwGroupBits = dwGroupBits;
  pNewSegTrack->pTrack = pTrack;
255
  IDirectMusicTrack_Init(pTrack, (IDirectMusicSegment *)iface);
256 257 258
  IDirectMusicTrack_AddRef(pTrack);
  list_add_tail (&This->Tracks, &pNewSegTrack->entry);

259
  return S_OK;
260 261
}

Mike McCormack's avatar
Mike McCormack committed
262
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveTrack (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicTrack* pTrack) {
263
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
264

265
  struct list* pEntry = NULL;
266 267
  LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;

268
  TRACE("(%p, %p)\n", This, pTrack);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

  LIST_FOR_EACH (pEntry, &This->Tracks) {
    pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
    if (pIt->pTrack == pTrack) {
      TRACE("(%p, %p): track in list\n", This, pTrack);
      
      list_remove(&pIt->entry);
      IDirectMusicTrack_Init(pIt->pTrack, NULL);
      IDirectMusicTrack_Release(pIt->pTrack);
      HeapFree(GetProcessHeap(), 0, pIt);   

      return S_OK;
    }
  }
  
  return S_FALSE;
285 286
}

Mike McCormack's avatar
Mike McCormack committed
287
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_InitPlay (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicSegmentState** ppSegState, IDirectMusicPerformance* pPerformance, DWORD dwFlags) {
288 289
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  HRESULT hr;
290
  FIXME("(%p, %p, %p, %d): semi-stub\n", This, ppSegState, pPerformance, dwFlags);
291 292 293 294 295 296 297 298 299
  if (NULL == ppSegState) {
    return E_POINTER;
  }
  hr = DMUSIC_CreateDirectMusicSegmentStateImpl(&IID_IDirectMusicSegmentState, (void**) ppSegState, NULL);
  if (FAILED(hr)) {
    return hr;
  }
  /* TODO: DMUS_SEGF_FLAGS */
  return S_OK;
300 301
}

Mike McCormack's avatar
Mike McCormack committed
302
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetGraph (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicGraph** ppGraph) {
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  FIXME("(%p, %p): semi-stub\n", This, ppGraph);
  if (NULL == ppGraph) {
    return E_POINTER;
  }
  if (NULL == This->pGraph) {
    return DMUS_E_NOT_FOUND;
  }
  /** 
   * should return This, as seen in msdn 
   * "...The segment object implements IDirectMusicGraph directly..."
   */
  *ppGraph = This->pGraph;
  IDirectMusicGraph_AddRef(This->pGraph);
  return S_OK;
318 319
}

Mike McCormack's avatar
Mike McCormack committed
320
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetGraph (LPDIRECTMUSICSEGMENT8 iface, IDirectMusicGraph* pGraph) {
321 322 323 324 325 326 327 328 329 330
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  FIXME("(%p, %p): to complete\n", This, pGraph);
  if (NULL != This->pGraph) {
    IDirectMusicGraph_Release(This->pGraph);
  }
  This->pGraph = pGraph;
  if (NULL != This->pGraph) {
    IDirectMusicGraph_AddRef(This->pGraph);
  }
  return S_OK;
331 332
}

Mike McCormack's avatar
Mike McCormack committed
333
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_AddNotificationType (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidNotificationType) {
334 335 336
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
  return S_OK;
337 338
}

Mike McCormack's avatar
Mike McCormack committed
339
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveNotificationType (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidNotificationType) {
340 341 342
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
  return S_OK;
343 344
}

Mike McCormack's avatar
Mike McCormack committed
345
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetParam (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, MUSIC_TIME* pmtNext, void* pParam) {
346 347 348 349 350 351 352 353 354
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);

  CLSID pIt_clsid;
  struct list* pEntry = NULL;
  IDirectMusicTrack* pTrack = NULL;
  IPersistStream* pCLSIDStream = NULL;
  LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
  HRESULT hr = S_OK;

355
  FIXME("(%p, %s, 0x%x, %d, %d, %p, %p)\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pmtNext, pParam);
356 357 358 359 360 361
  
  if (DMUS_SEG_ANYTRACK == dwIndex) {
    
    LIST_FOR_EACH (pEntry, &This->Tracks) {
      pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);

362 363 364 365 366 367
      hr = IDirectMusicTrack_QueryInterface(pIt->pTrack, &IID_IPersistStream, (void**) &pCLSIDStream);
      if (FAILED(hr)) {
	ERR("(%p): object %p don't implement IPersistStream Interface. Expect a crash (critical problem)\n", This, pIt->pTrack);
	continue ;
      }

368
      TRACE(" - %p -> 0x%x,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

      if (0xFFFFFFFF != dwGroupBits && 0 == (pIt->dwGroupBits & dwGroupBits)) continue ;
      hr = IPersistStream_GetClassID(pCLSIDStream, &pIt_clsid);
      IPersistStream_Release(pCLSIDStream); pCLSIDStream = NULL;
      if (FAILED(hr)) {
	ERR("(%p): non-implemented GetClassID for object %p\n", This, pIt->pTrack);
	continue ;
      }
      if (FALSE == IsEqualGUID(&pIt_clsid, rguidType)) continue ;
      if (FAILED(IDirectMusicTrack_IsParamSupported(pIt->pTrack, rguidType))) continue ;
      hr = IDirectMusicTrack_GetParam(pIt->pTrack, rguidType, mtTime, pmtNext, pParam);
      if (SUCCEEDED(hr)) return hr;
    }
    ERR("(%p): not found\n", This);
    return DMUS_E_TRACK_NOT_FOUND;
  } 

  hr = IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack(iface, &GUID_NULL, dwGroupBits, dwIndex, &pTrack);
  if (FAILED(hr)) {
    ERR("(%p): not found\n", This);
    return DMUS_E_TRACK_NOT_FOUND;
  }

  hr = IDirectMusicTrack_GetParam(pTrack, rguidType, mtTime, pmtNext, pParam);
  IDirectMusicTrack_Release(pTrack); pTrack = NULL;

  return hr;
396 397
}

Mike McCormack's avatar
Mike McCormack committed
398
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetParam (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void* pParam) {
399
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
400
  FIXME("(%p, %s, %d, %d, %d, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
401
  return S_OK;
402 403
}

Mike McCormack's avatar
Mike McCormack committed
404
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Clone (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicSegment** ppSegment) {
405
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
406
  FIXME("(%p, %d, %d, %p): stub\n", This, mtStart, mtEnd, ppSegment);
407
  return S_OK;
408 409
}

Mike McCormack's avatar
Mike McCormack committed
410
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetStartPoint (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart) {
411
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
412
  TRACE("(%p, %d)\n", This, mtStart);
413 414 415 416 417
  if (mtStart >= This->header.mtLength) {
    return DMUS_E_OUT_OF_RANGE;
  }
  This->header.mtPlayStart = mtStart;
  return S_OK;
418 419
}

Mike McCormack's avatar
Mike McCormack committed
420
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetStartPoint (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtStart) {
421 422 423 424 425 426 427
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  TRACE("(%p, %p)\n", This, pmtStart);
  if (NULL == pmtStart) {
    return E_POINTER;
  }
  *pmtStart = This->header.mtPlayStart;
  return S_OK;
428 429
}

Mike McCormack's avatar
Mike McCormack committed
430
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLoopPoints (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtStart, MUSIC_TIME mtEnd) {
431
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
432
  TRACE("(%p, %d, %d)\n", This, mtStart, mtEnd);
433 434 435 436 437 438
  if (mtStart >= This->header.mtLength || mtEnd > This->header.mtLength || mtStart > mtEnd) {
    return DMUS_E_OUT_OF_RANGE;
  }
  This->header.mtLoopStart = mtStart;
  This->header.mtLoopEnd   = mtEnd;
  return S_OK;
439 440
}

Mike McCormack's avatar
Mike McCormack committed
441
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLoopPoints (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME* pmtStart, MUSIC_TIME* pmtEnd) {
442 443 444 445 446 447 448 449
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
  TRACE("(%p, %p, %p)\n", This, pmtStart, pmtEnd);
  if (NULL == pmtStart || NULL == pmtEnd) {
    return E_POINTER;
  }
  *pmtStart = This->header.mtLoopStart;
  *pmtEnd   = This->header.mtLoopEnd;
  return S_OK;
450 451
}

Mike McCormack's avatar
Mike McCormack committed
452
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetPChannelsUsed (LPDIRECTMUSICSEGMENT8 iface, DWORD dwNumPChannels, DWORD* paPChannels) {
453
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
454
	FIXME("(%p, %d, %p): stub\n", This, dwNumPChannels, paPChannels);
Rok Mandeljc's avatar
Rok Mandeljc committed
455
	return S_OK;
456 457
}

Mike McCormack's avatar
Mike McCormack committed
458
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_SetTrackConfig (LPDIRECTMUSICSEGMENT8 iface, REFGUID rguidTrackClassID, DWORD dwGroupBits, DWORD dwIndex, DWORD dwFlagsOn, DWORD dwFlagsOff) {
459
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
460
	FIXME("(%p, %s, %d, %d, %d, %d): stub\n", This, debugstr_dmguid(rguidTrackClassID), dwGroupBits, dwIndex, dwFlagsOn, dwFlagsOff);
Rok Mandeljc's avatar
Rok Mandeljc committed
461
	return S_OK;
462 463
}

Mike McCormack's avatar
Mike McCormack committed
464
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_GetAudioPathConfig (LPDIRECTMUSICSEGMENT8 iface, IUnknown** ppAudioPathConfig){
465
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
466 467
	FIXME("(%p, %p): stub\n", This, ppAudioPathConfig);
	return S_OK;
468 469
}

Mike McCormack's avatar
Mike McCormack committed
470
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Compose (LPDIRECTMUSICSEGMENT8 iface, MUSIC_TIME mtTime, IDirectMusicSegment* pFromSegment, IDirectMusicSegment* pToSegment, IDirectMusicSegment** ppComposedSegment) {
471
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
472
	FIXME("(%p, %d, %p, %p, %p): stub\n", This, mtTime, pFromSegment, pToSegment, ppComposedSegment);
Rok Mandeljc's avatar
Rok Mandeljc committed
473
	return S_OK;
474 475
}

Mike McCormack's avatar
Mike McCormack committed
476
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Download (LPDIRECTMUSICSEGMENT8 iface, IUnknown *pAudioPath) {
477
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
478 479
	FIXME("(%p, %p): stub\n", This, pAudioPath);
	return S_OK;
480 481
}

Mike McCormack's avatar
Mike McCormack committed
482
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicSegment8_Unload (LPDIRECTMUSICSEGMENT8 iface, IUnknown *pAudioPath) {
483
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, SegmentVtbl, iface);
Rok Mandeljc's avatar
Rok Mandeljc committed
484 485
	FIXME("(%p, %p): stub\n", This, pAudioPath);
	return S_OK;
486 487
}

488
static const IDirectMusicSegment8Vtbl DirectMusicSegment8_Segment_Vtbl = {
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	IDirectMusicSegment8Impl_IDirectMusicSegment8_QueryInterface,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_AddRef,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_Release,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLength,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLength,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetRepeats,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetRepeats,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetDefaultResolution,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetDefaultResolution,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrack,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetTrackGroup,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveTrack,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_InitPlay,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetGraph,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetGraph,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_AddNotificationType,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_RemoveNotificationType,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetParam,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetParam,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_Clone,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetStartPoint,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetStartPoint,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetLoopPoints,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetLoopPoints,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetPChannelsUsed,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_SetTrackConfig,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_GetAudioPathConfig,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_Compose,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_Download,
	IDirectMusicSegment8Impl_IDirectMusicSegment8_Unload
520 521
};

522
/* IDirectMusicSegment8Impl IDirectMusicObject part: */
523
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_QueryInterface (LPDIRECTMUSICOBJECT iface, REFIID riid, LPVOID *ppobj) {
524
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
525
	return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
526 527
}

528
static ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_AddRef (LPDIRECTMUSICOBJECT iface) {
529
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
530
	return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
531 532
}

Mike McCormack's avatar
Mike McCormack committed
533
static ULONG WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_Release (LPDIRECTMUSICOBJECT iface) {
534
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
535
	return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
536 537
}

Mike McCormack's avatar
Mike McCormack committed
538
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_GetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
539
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
540
	TRACE("(%p, %p)\n", This, pDesc);
541 542
	/* I think we shouldn't return pointer here since then values can be changed; it'd be a mess */
	memcpy (pDesc, This->pDesc, This->pDesc->dwSize);
543 544 545
	return S_OK;
}

Mike McCormack's avatar
Mike McCormack committed
546
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_SetDescriptor (LPDIRECTMUSICOBJECT iface, LPDMUS_OBJECTDESC pDesc) {
547
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
548
	TRACE("(%p, %p): setting descriptor:\n%s\n", This, pDesc, debugstr_DMUS_OBJECTDESC (pDesc));
549

550 551
	/* According to MSDN, we should copy only given values, not whole struct */	
	if (pDesc->dwValidData & DMUS_OBJ_OBJECT)
552
		This->pDesc->guidObject = pDesc->guidObject;
553
	if (pDesc->dwValidData & DMUS_OBJ_CLASS)
554
		This->pDesc->guidClass = pDesc->guidClass;
555
	if (pDesc->dwValidData & DMUS_OBJ_NAME)
556
		lstrcpynW (This->pDesc->wszName, pDesc->wszName, DMUS_MAX_NAME);
557
	if (pDesc->dwValidData & DMUS_OBJ_CATEGORY)
558
		lstrcpynW (This->pDesc->wszCategory, pDesc->wszCategory, DMUS_MAX_CATEGORY);
559
	if (pDesc->dwValidData & DMUS_OBJ_FILENAME)
560
		lstrcpynW (This->pDesc->wszFileName, pDesc->wszFileName, DMUS_MAX_FILENAME);
561
	if (pDesc->dwValidData & DMUS_OBJ_VERSION)
562
		This->pDesc->vVersion = pDesc->vVersion;
563
	if (pDesc->dwValidData & DMUS_OBJ_DATE)
564
		This->pDesc->ftDate = pDesc->ftDate;
565
	if (pDesc->dwValidData & DMUS_OBJ_MEMORY) {
566 567
		This->pDesc->llMemLength = pDesc->llMemLength;
		memcpy (This->pDesc->pbMemData, pDesc->pbMemData, pDesc->llMemLength);
568 569 570 571 572 573 574 575
	}
	if (pDesc->dwValidData & DMUS_OBJ_STREAM) {
		/* according to MSDN, we copy the stream */
		IStream_Clone (pDesc->pStream, &This->pDesc->pStream);	
	}
	
	/* add new flags */
	This->pDesc->dwValidData |= pDesc->dwValidData;
576 577 578 579

	return S_OK;
}

Mike McCormack's avatar
Mike McCormack committed
580
static HRESULT WINAPI IDirectMusicSegment8Impl_IDirectMusicObject_ParseDescriptor (LPDIRECTMUSICOBJECT iface, LPSTREAM pStream, LPDMUS_OBJECTDESC pDesc) {
581 582 583 584
	ICOM_THIS_MULTI(IDirectMusicSegment8Impl, ObjectVtbl, iface);
	DMUS_PRIVATE_CHUNK Chunk;
	DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
	LARGE_INTEGER liMove; /* used when skipping chunks */
585

586
	TRACE("(%p,%p, %p)\n", This, pStream, pDesc);
587

588 589
	/* FIXME: should this be determined from stream? */
	pDesc->dwValidData |= DMUS_OBJ_CLASS;
590 591
	pDesc->guidClass = CLSID_DirectMusicSegment;

592
	IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
593
	TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
594 595 596 597 598 599 600 601 602 603 604
	switch (Chunk.fccID) {	
		case FOURCC_RIFF: {
			IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);				
			TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
			StreamSize = Chunk.dwSize - sizeof(FOURCC);
			StreamCount = 0;
			if (Chunk.fccID == DMUS_FOURCC_SEGMENT_FORM) {
				TRACE_(dmfile)(": segment form\n");
				do {
					IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
					StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
605
					TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
					switch (Chunk.fccID) {
						case DMUS_FOURCC_GUID_CHUNK: {
							TRACE_(dmfile)(": GUID chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_OBJECT;
							IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
							break;
						}
						case DMUS_FOURCC_VERSION_CHUNK: {
							TRACE_(dmfile)(": version chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_VERSION;
							IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
							break;
						}
						case DMUS_FOURCC_CATEGORY_CHUNK: {
							TRACE_(dmfile)(": category chunk\n");
							pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
							IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
							break;
						}
						case FOURCC_LIST: {
							IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);				
							TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
							ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
							ListCount[0] = 0;
							switch (Chunk.fccID) {
								/* evil M$ UNFO list, which can (!?) contain INFO elements */
								case DMUS_FOURCC_UNFO_LIST: {
									TRACE_(dmfile)(": UNFO list\n");
									do {
										IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
										ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
637
										TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
638 639
										switch (Chunk.fccID) {
											/* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
640
											   (though strings seem to be valid unicode) */
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
											case mmioFOURCC('I','N','A','M'):
											case DMUS_FOURCC_UNAM_CHUNK: {
												TRACE_(dmfile)(": name chunk\n");
												pDesc->dwValidData |= DMUS_OBJ_NAME;
												IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
												break;
											}
											case mmioFOURCC('I','A','R','T'):
											case DMUS_FOURCC_UART_CHUNK: {
												TRACE_(dmfile)(": artist chunk (ignored)\n");
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											case mmioFOURCC('I','C','O','P'):
											case DMUS_FOURCC_UCOP_CHUNK: {
												TRACE_(dmfile)(": copyright chunk (ignored)\n");
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											case mmioFOURCC('I','S','B','J'):
											case DMUS_FOURCC_USBJ_CHUNK: {
												TRACE_(dmfile)(": subject chunk (ignored)\n");
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											case mmioFOURCC('I','C','M','T'):
											case DMUS_FOURCC_UCMT_CHUNK: {
												TRACE_(dmfile)(": comment chunk (ignored)\n");
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;
											}
											default: {
677
												TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
678 679 680 681 682
												liMove.QuadPart = Chunk.dwSize;
												IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
												break;						
											}
										}
683
										TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
684 685 686
									} while (ListCount[0] < ListSize[0]);
									break;
								}
687 688 689 690 691
								case DMUS_FOURCC_TRACK_LIST: {
								  TRACE_(dmfile)(": TRACK list\n");
								  do {
								    IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
								    ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
692
                                                                    TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
693 694
								    switch (Chunk.fccID) {
								    default: {
695
								      TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
696 697 698 699 700
								      liMove.QuadPart = Chunk.dwSize;
								      IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
								      break;						
								    }
								    }
701
                                                                    TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
702 703 704 705 706 707 708 709
								  } while (ListCount[0] < ListSize[0]);
								  break;
								}
							        default: {
								  TRACE_(dmfile)(": unknown (skipping)\n");
								  liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
								  IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
								  break;						
710 711 712 713 714
								}
							}
							break;
						}	
						default: {
715
							TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
716 717 718 719 720
							liMove.QuadPart = Chunk.dwSize;
							IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
							break;						
						}
					}
721
					TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
722 723
				} while (StreamCount < StreamSize);
				break;
724
			} else if (Chunk.fccID == mmioFOURCC('W','A','V','E')) {
725
				TRACE_(dmfile)(": wave form (loading not yet implemented)\n");
726 727
				liMove.QuadPart = StreamSize;
				IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
728
			} else {
729
				TRACE_(dmfile)(": unexpected chunk (loading failed)\n");
730 731 732 733 734 735 736
				liMove.QuadPart = StreamSize;
				IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
				return E_FAIL;
			}
		
			TRACE_(dmfile)(": reading finished\n");
			break;
737
		}
738 739 740 741 742 743 744 745
		default: {
			TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
			liMove.QuadPart = Chunk.dwSize;
			IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
			return DMUS_E_INVALIDFILE;
		}
	}	
	
746
	TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
747
	
748
	return S_OK;
749 750
}

751
static const IDirectMusicObjectVtbl DirectMusicSegment8_Object_Vtbl = {
752 753 754 755 756 757
  IDirectMusicSegment8Impl_IDirectMusicObject_QueryInterface,
  IDirectMusicSegment8Impl_IDirectMusicObject_AddRef,
  IDirectMusicSegment8Impl_IDirectMusicObject_Release,
  IDirectMusicSegment8Impl_IDirectMusicObject_GetDescriptor,
  IDirectMusicSegment8Impl_IDirectMusicObject_SetDescriptor,
  IDirectMusicSegment8Impl_IDirectMusicObject_ParseDescriptor
758
};
759

760
/* IDirectMusicSegment8Impl IPersistStream part: */
761
static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_QueryInterface (LPPERSISTSTREAM iface, REFIID riid, LPVOID *ppobj) {
762
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
763
  return IUnknown_QueryInterface ((LPUNKNOWN)&This->UnknownVtbl, riid, ppobj);
764
}
765

766
static ULONG WINAPI IDirectMusicSegment8Impl_IPersistStream_AddRef (LPPERSISTSTREAM iface) {
767
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
768
  return IUnknown_AddRef ((LPUNKNOWN)&This->UnknownVtbl);
769 770
}

Mike McCormack's avatar
Mike McCormack committed
771
static ULONG WINAPI IDirectMusicSegment8Impl_IPersistStream_Release (LPPERSISTSTREAM iface) {
772
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
773
  return IUnknown_Release ((LPUNKNOWN)&This->UnknownVtbl);
774 775
}

Mike McCormack's avatar
Mike McCormack committed
776
static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_GetClassID (LPPERSISTSTREAM iface, CLSID* pClassID) {
777 778
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
  TRACE("(%p, %p)\n", This, pClassID);
779
  *pClassID = CLSID_DirectMusicSegment;
780
  return S_OK;
781 782
}

Mike McCormack's avatar
Mike McCormack committed
783
static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_IsDirty (LPPERSISTSTREAM iface) {
784 785 786
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
  FIXME("(%p): stub, always S_FALSE\n", This);
  return S_FALSE;
787 788
}

789 790 791
static HRESULT IDirectMusicSegment8Impl_IPersistStream_LoadTrack (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicTrack** ppTrack, 
								  DMUS_IO_TRACK_HEADER* pTrack_hdr) {

792
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
  HRESULT hr = E_FAIL;
  IPersistStream* pPersistStream = NULL;
  
  hr = CoCreateInstance (&pTrack_hdr->guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack, (LPVOID*) ppTrack);
  if (FAILED(hr)) {
    ERR(": could not create object\n");
    return hr;
  }
  /* acquire PersistStream interface */
  hr = IDirectMusicTrack_QueryInterface (*ppTrack, &IID_IPersistStream, (LPVOID*) &pPersistStream);
  if (FAILED(hr)) {
    ERR(": could not acquire IPersistStream\n");
    return hr;
  }
  /* load */
  hr = IPersistStream_Load (pPersistStream, pClonedStream);
  if (FAILED(hr)) {
    ERR(": failed to load object\n");
    return hr;
  }
  
  /* release all loading-related stuff */
  IPersistStream_Release (pPersistStream);

817
  hr = IDirectMusicSegment8Impl_IDirectMusicSegment8_InsertTrack ((LPDIRECTMUSICSEGMENT8)&This->SegmentVtbl, *ppTrack, pTrack_hdr->dwGroup); /* at dsPosition */
818 819 820 821 822
  if (FAILED(hr)) {
    ERR(": could not insert track\n");
    return hr;
  }

823 824 825
  return S_OK;
}

826
static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseTrackForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm) {
827

828
  /*ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);*/
829 830
  HRESULT hr = E_FAIL;
  DMUS_PRIVATE_CHUNK Chunk;
831
  DWORD StreamSize, StreamCount, ListSize[3];
832
  LARGE_INTEGER liMove; /* used when skipping chunks */
833

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
  DMUS_IO_TRACK_HEADER        track_hdr;
  DMUS_IO_TRACK_EXTRAS_HEADER track_xhdr;
  IDirectMusicTrack*          pTrack = NULL;

  if (pChunk->fccID != DMUS_FOURCC_TRACK_FORM) {
    ERR_(dmfile)(": %s chunk should be a TRACK form\n", debugstr_fourcc (pChunk->fccID));
    return E_FAIL;
  }  

  StreamSize = pChunk->dwSize - sizeof(FOURCC);
  StreamCount = 0;

  do {
    IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
    StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
849
    TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
850 851 852
    
    switch (Chunk.fccID) {
    case DMUS_FOURCC_TRACK_CHUNK: {
853
      TRACE_(dmfile)(": track chunk\n");
854
      IStream_Read (pStm, &track_hdr, sizeof(DMUS_IO_TRACK_HEADER), NULL);
855
      TRACE_(dmfile)(" - class: %s\n", debugstr_guid (&track_hdr.guidClassID));
856
      TRACE_(dmfile)(" - dwGroup: %d\n", track_hdr.dwGroup);
857 858
      TRACE_(dmfile)(" - ckid: %s\n", debugstr_fourcc (track_hdr.ckid));
      TRACE_(dmfile)(" - fccType: %s\n", debugstr_fourcc (track_hdr.fccType));
859 860 861
      break;
    }
    case DMUS_FOURCC_TRACK_EXTRAS_CHUNK: {
862
      TRACE_(dmfile)(": track extras chunk\n");
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
      IStream_Read (pStm, &track_xhdr, sizeof(DMUS_IO_TRACK_EXTRAS_HEADER), NULL);
      break;
    }

    case DMUS_FOURCC_COMMANDTRACK_CHUNK: {
      TRACE_(dmfile)(": COMMANDTRACK track\n");
      liMove.QuadPart = Chunk.dwSize;
      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
      break;
    }

    case FOURCC_LIST: {
      IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
      TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
      ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
      if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {
	LPSTREAM pClonedStream = NULL;

	TRACE_(dmfile)(": TRACK list\n");

	IStream_Clone (pStm, &pClonedStream);
884

885
	liMove.QuadPart = 0;
886
	liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
887
	IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
888
	
889 890 891 892
	hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
	if (FAILED(hr)) {
	  ERR(": could not load track\n");
	  return hr;
893
	}
894
	IStream_Release (pClonedStream);
895 896 897
	
	IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */

898 899 900
	liMove.QuadPart = ListSize[0];
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
	
901 902 903 904 905 906 907
      } else {
	TRACE_(dmfile)(": unknown (skipping)\n");
	liMove.QuadPart = Chunk.dwSize;
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
      }
      break;
    }
908

909 910
    case FOURCC_RIFF: {
      IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
Raphael Junqueira's avatar
Raphael Junqueira committed
911
      TRACE_(dmfile)(": RIFF chunk of type %s\n", debugstr_fourcc(Chunk.fccID));
912

913
      ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
914

915
      if (Chunk.fccID == track_hdr.fccType && 0 == track_hdr.ckid) {	
916 917
	LPSTREAM pClonedStream = NULL;

918
	TRACE_(dmfile)(": TRACK RIFF\n");
919 920 921 922

	IStream_Clone (pStm, &pClonedStream);
	
	liMove.QuadPart = 0;
923
	liMove.QuadPart -= sizeof(FOURCC) + (sizeof(FOURCC)+sizeof(DWORD));
924
	IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
925
	
926 927 928 929 930 931 932 933 934 935 936 937 938
	hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
	if (FAILED(hr)) {
	  ERR(": could not load track\n");
	  return hr;
	}
	IStream_Release (pClonedStream);
	
	IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */

	/** now safe move the cursor */
	liMove.QuadPart = ListSize[0];
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
	 
939
      } else {
940 941
	TRACE_(dmfile)(": unknown RIFF fmt (skipping)\n");
	liMove.QuadPart = ListSize[0];
942 943 944 945
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
      }
      break;
    }
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
    default: {
      if (0 == track_hdr.fccType && Chunk.fccID == track_hdr.ckid) {
	LPSTREAM pClonedStream = NULL;

	TRACE_(dmfile)(": TRACK solo\n");

	IStream_Clone (pStm, &pClonedStream);
	
	liMove.QuadPart = 0;
	liMove.QuadPart -= (sizeof(FOURCC) + sizeof(DWORD));
	IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);

	hr = IDirectMusicSegment8Impl_IPersistStream_LoadTrack (iface, pClonedStream, &pTrack, &track_hdr);
	if (FAILED(hr)) {
	  ERR(": could not load track\n");
	  return hr;
	}
	IStream_Release (pClonedStream);
	
	IDirectMusicTrack_Release(pTrack); pTrack = NULL; /* now we can release at as it inserted */

	liMove.QuadPart = Chunk.dwSize;
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);

	break;
      }

974
      TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
975 976 977 978 979
      liMove.QuadPart = Chunk.dwSize;
      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
      break;						
    }
    }
980
    TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
981 982 983 984 985 986 987 988 989
  } while (StreamCount < StreamSize);  

  return S_OK;
}

static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseTrackList (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, IDirectMusicSegment8Impl* This) {

  HRESULT hr = E_FAIL;
  DMUS_PRIVATE_CHUNK Chunk;
990
  DWORD StreamSize, ListSize[3], ListCount[3];
991 992 993 994 995 996 997
  LARGE_INTEGER liMove; /* used when skipping chunks */

  if (pChunk->fccID != DMUS_FOURCC_TRACK_LIST) {
    ERR_(dmfile)(": %s chunk should be a TRACK list\n", debugstr_fourcc (pChunk->fccID));
    return E_FAIL;
  }  

998
  ListSize[0] = pChunk->dwSize - sizeof(FOURCC);
999 1000 1001 1002 1003
  ListCount[0] = 0;

  do {
    IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
    ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1004
    TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1005 1006 1007 1008 1009 1010 1011 1012
    switch (Chunk.fccID) { 
    case FOURCC_RIFF: {
      IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
      TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
      StreamSize = Chunk.dwSize - sizeof(FOURCC);
      switch (Chunk.fccID) {
      case  DMUS_FOURCC_TRACK_FORM: {
	TRACE_(dmfile)(": TRACK form\n");
1013
	hr = IDirectMusicSegment8Impl_IPersistStream_ParseTrackForm (iface, &Chunk, pStm);
1014 1015 1016 1017
	if (FAILED(hr)) return hr;	
	break;
      }
      default: {
1018
	TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1019 1020 1021 1022 1023 1024 1025 1026
	liMove.QuadPart = StreamSize;
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
	break;
      }
      }
      break;
    }
    default: {
1027
      TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1028 1029 1030 1031 1032
      liMove.QuadPart = Chunk.dwSize;
      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
      break;						
    }
    }
1033
    TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  } while (ListCount[0] < ListSize[0]);

  return S_OK;
}

static HRESULT IDirectMusicSegment8Impl_IPersistStream_ParseSegmentForm (LPPERSISTSTREAM iface, DMUS_PRIVATE_CHUNK* pChunk, IStream* pStm, IDirectMusicSegment8Impl* This) {

  HRESULT hr = E_FAIL;
  DMUS_PRIVATE_CHUNK Chunk;
  DWORD StreamSize, StreamCount, ListSize[3], ListCount[3];
  LARGE_INTEGER liMove; /* used when skipping chunks */

  if (pChunk->fccID != DMUS_FOURCC_SEGMENT_FORM) {
    ERR_(dmfile)(": %s chunk should be a segment form\n", debugstr_fourcc (pChunk->fccID));
    return E_FAIL;
  }

  StreamSize = pChunk->dwSize - sizeof(FOURCC);
  StreamCount = 0;

  do {
    IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
    StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1057
    TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1058
    
1059
    hr = IDirectMusicUtils_IPersistStream_ParseDescGeneric(&Chunk, pStm, This->pDesc);
1060 1061 1062 1063 1064
    if (FAILED(hr)) return hr;
    
    if (hr == S_FALSE) {
      switch (Chunk.fccID) {
      case DMUS_FOURCC_SEGMENT_CHUNK: {
1065
	DWORD checkSz = sizeof(FOURCC);
1066
	TRACE_(dmfile)(": segment chunk\n");
1067
#if 1
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	/** DX 7 */
	IStream_Read (pStm, &This->header.dwRepeats,    sizeof(This->header.dwRepeats), NULL);
	checkSz += sizeof(This->header.dwRepeats);
	IStream_Read (pStm, &This->header.mtLength,     sizeof(This->header.mtLength), NULL);
	checkSz += sizeof(This->header.mtLength);
	IStream_Read (pStm, &This->header.mtPlayStart,  sizeof(This->header.mtPlayStart), NULL);
	checkSz += sizeof(This->header.mtPlayStart);
	IStream_Read (pStm, &This->header.mtLoopStart,  sizeof(This->header.mtLoopStart), NULL);
	checkSz += sizeof(This->header.mtLoopStart);
	IStream_Read (pStm, &This->header.mtLoopEnd,    sizeof(This->header.mtLoopEnd), NULL);
	checkSz += sizeof(This->header.mtLoopEnd);
	IStream_Read (pStm, &This->header.dwResolution, sizeof(This->header.dwResolution), NULL);
	checkSz += sizeof(This->header.dwResolution);
1081
	TRACE_(dmfile)("dwRepeats: %u\n", This->header.dwRepeats);
1082 1083 1084 1085
	TRACE_(dmfile)("mtLength: %u\n",  This->header.mtLength);
	TRACE_(dmfile)("mtPlayStart: %u\n",  This->header.mtPlayStart);
	TRACE_(dmfile)("mtLoopStart: %u\n",  This->header.mtLoopStart);
	TRACE_(dmfile)("mtLoopEnd: %u\n",  This->header.mtLoopEnd);
1086
	TRACE_(dmfile)("dwResolution: %u\n", This->header.dwResolution);
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	/** DX 8 */
	if (Chunk.dwSize > checkSz) {
	  IStream_Read (pStm, &This->header.rtLength,    sizeof(This->header.rtLength), NULL);
	  checkSz += sizeof(This->header.rtLength);
	  IStream_Read (pStm, &This->header.dwFlags,     sizeof(This->header.dwFlags), NULL);
	  checkSz += sizeof(This->header.dwFlags);
	}
	/** DX 9 */
	if (Chunk.dwSize > checkSz) {
	  IStream_Read (pStm, &This->header.rtLoopStart,  sizeof(This->header.rtLoopStart), NULL);
	  checkSz += sizeof(This->header.rtLoopStart);
	  IStream_Read (pStm, &This->header.rtLoopEnd,    sizeof(This->header.rtLoopEnd), NULL);
	  checkSz += sizeof(This->header.rtLoopEnd);
	  IStream_Read (pStm, &This->header.rtPlayStart,  sizeof(This->header.rtPlayStart), NULL);
	  checkSz += sizeof(This->header.rtPlayStart);
	}
1103 1104
#endif
	liMove.QuadPart = Chunk.dwSize - checkSz + sizeof(FOURCC);
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
	break;
      }
      case FOURCC_LIST: {
	IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
	TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
	ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
	ListCount[0] = 0;
	switch (Chunk.fccID) {
	case DMUS_FOURCC_UNFO_LIST: {
	  TRACE_(dmfile)(": UNFO list\n");
	  do {
	    IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
	    ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
1119
            TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1120

1121
	    hr = IDirectMusicUtils_IPersistStream_ParseUNFOGeneric(&Chunk, pStm, This->pDesc);
1122 1123 1124 1125 1126
	    if (FAILED(hr)) return hr;
	    
	    if (hr == S_FALSE) {
	      switch (Chunk.fccID) {
	      default: {
1127
		TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1128 1129 1130 1131 1132 1133 1134
		liMove.QuadPart = Chunk.dwSize;
		IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
		break;						
	      }
	      }
	    }

1135
            TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	  } while (ListCount[0] < ListSize[0]);
	  break;
	}
	case DMUS_FOURCC_TRACK_LIST: {
	  TRACE_(dmfile)(": TRACK list\n");
	  hr = IDirectMusicSegment8Impl_IPersistStream_ParseTrackList (iface, &Chunk, pStm, This);
	  if (FAILED(hr)) return hr;
	  break;
	}
	default: {
	  TRACE_(dmfile)(": unknown (skipping)\n");
	  liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
	  IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
	  break;						
	}
	}
	break;
      }
      default: {
1155
	TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
1156 1157 1158 1159 1160 1161
	liMove.QuadPart = Chunk.dwSize;
	IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
	break;						
      }
      }
    }
1162
    TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
1163 1164 1165 1166 1167
  } while (StreamCount < StreamSize);

  return S_OK;
}

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
static HRESULT IDirectMusicSegment8Impl_IPersistStream_LoadWave (LPPERSISTSTREAM iface, IStream* pClonedStream, IDirectMusicObject** ppWaveObject) {

  HRESULT hr = E_FAIL;
  IPersistStream* pPersistStream = NULL;
  
  hr = CoCreateInstance (&CLSID_DirectSoundWave, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicObject, (LPVOID*) ppWaveObject);
  if (FAILED(hr)) {
    ERR(": could not create object\n");
    return hr;
  }
  /* acquire PersistStream interface */
1179
  hr = IDirectMusicObject_QueryInterface (*ppWaveObject, &IID_IPersistStream, (LPVOID*) &pPersistStream);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
  if (FAILED(hr)) {
    ERR(": could not acquire IPersistStream\n");
    return hr;
  }
  /* load */
  hr = IPersistStream_Load (pPersistStream, pClonedStream);
  if (FAILED(hr)) {
    ERR(": failed to load object\n");
    return hr;
  }
  
  /* release all loading-related stuff */
  IPersistStream_Release (pPersistStream);

  return S_OK;
}

Mike McCormack's avatar
Mike McCormack committed
1197
static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_Load (LPPERSISTSTREAM iface, IStream* pStm) {
1198
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
1199

1200 1201
  HRESULT hr;
  DMUS_PRIVATE_CHUNK Chunk;
1202
  DWORD StreamSize;
1203 1204
  /*DWORD ListSize[3], ListCount[3];*/
  LARGE_INTEGER liMove; /* used when skipping chunks */
1205

Raphael Junqueira's avatar
Raphael Junqueira committed
1206
  TRACE("(%p, %p): Loading\n", This, pStm);
1207
  IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
1208
  TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  switch (Chunk.fccID) {	
  case FOURCC_RIFF: {
    IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);				
    TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
    StreamSize = Chunk.dwSize - sizeof(FOURCC);
    switch (Chunk.fccID) {
    case DMUS_FOURCC_SEGMENT_FORM: {
      TRACE_(dmfile)(": segment form\n");
      hr = IDirectMusicSegment8Impl_IPersistStream_ParseSegmentForm (iface, &Chunk, pStm, This);
      if (FAILED(hr)) return hr;
      break;
    }
    case mmioFOURCC('W','A','V','E'): {
1222 1223 1224 1225 1226 1227 1228
      LPSTREAM pClonedStream = NULL;	
      IDirectMusicObject* pWave = NULL;

      FIXME_(dmfile)(": WAVE form (loading to be checked)\n");

      IStream_Clone (pStm, &pClonedStream);
	
1229
      liMove.QuadPart = - (LONGLONG)(sizeof(FOURCC) * 2 + sizeof(DWORD));
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
      IStream_Seek (pClonedStream, liMove, STREAM_SEEK_CUR, NULL);
	
      hr = IDirectMusicSegment8Impl_IPersistStream_LoadWave (iface, pClonedStream, &pWave);
      if (FAILED(hr)) {
	ERR(": could not load track\n");
	return hr;
      }
      IStream_Release (pClonedStream);
      
      IDirectMusicTrack_Release(pWave); pWave = NULL; /* now we can release at as it inserted */

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
      liMove.QuadPart = StreamSize;
      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
      break;      
    }
    default: {
      TRACE_(dmfile)(": unexpected chunk (loading failed)\n");
      liMove.QuadPart = StreamSize;
      IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
      return E_FAIL;
    }
    }
    TRACE_(dmfile)(": reading finished\n");
    break;
  }
  default: {
    TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
    liMove.QuadPart = Chunk.dwSize;
    IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
    return E_FAIL;
  }
  }
  
  return S_OK;
1264 1265
}

Mike McCormack's avatar
Mike McCormack committed
1266
static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_Save (LPPERSISTSTREAM iface, IStream* pStm, BOOL fClearDirty) {
1267 1268
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
  FIXME("(%p): Saving not implemented yet\n", This);
1269
  return E_NOTIMPL;
1270 1271
}

Mike McCormack's avatar
Mike McCormack committed
1272
static HRESULT WINAPI IDirectMusicSegment8Impl_IPersistStream_GetSizeMax (LPPERSISTSTREAM iface, ULARGE_INTEGER* pcbSize) {
1273 1274
  ICOM_THIS_MULTI(IDirectMusicSegment8Impl, PersistStreamVtbl, iface);
  FIXME("(%p, %p): stub\n", This, pcbSize);
1275
  return E_NOTIMPL;
1276 1277
}

1278
static const IPersistStreamVtbl DirectMusicSegment8_PersistStream_Vtbl = {
1279 1280 1281 1282 1283 1284 1285 1286
  IDirectMusicSegment8Impl_IPersistStream_QueryInterface,
  IDirectMusicSegment8Impl_IPersistStream_AddRef,
  IDirectMusicSegment8Impl_IPersistStream_Release,
  IDirectMusicSegment8Impl_IPersistStream_GetClassID,
  IDirectMusicSegment8Impl_IPersistStream_IsDirty,
  IDirectMusicSegment8Impl_IPersistStream_Load,
  IDirectMusicSegment8Impl_IPersistStream_Save,
  IDirectMusicSegment8Impl_IPersistStream_GetSizeMax
1287
};
1288 1289 1290

/* for ClassFactory */
HRESULT WINAPI DMUSIC_CreateDirectMusicSegmentImpl (LPCGUID lpcGUID, LPVOID* ppobj, LPUNKNOWN pUnkOuter) {
1291 1292 1293 1294
  IDirectMusicSegment8Impl* obj;
  
  obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicSegment8Impl));
  if (NULL == obj) {
1295
    *ppobj = NULL;
1296 1297 1298 1299 1300 1301 1302 1303 1304
    return E_OUTOFMEMORY;
  }
  obj->UnknownVtbl = &DirectMusicSegment8_Unknown_Vtbl;
  obj->SegmentVtbl = &DirectMusicSegment8_Segment_Vtbl;
  obj->ObjectVtbl = &DirectMusicSegment8_Object_Vtbl;
  obj->PersistStreamVtbl = &DirectMusicSegment8_PersistStream_Vtbl;
  obj->pDesc = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DMUS_OBJECTDESC));
  DM_STRUCT_INIT(obj->pDesc);
  obj->pDesc->dwValidData |= DMUS_OBJ_CLASS;
1305
  obj->pDesc->guidClass = CLSID_DirectMusicSegment;
1306
  obj->ref = 0; /* will be inited by QueryInterface */
1307
  list_init (&obj->Tracks);
1308

1309
  return IDirectMusicSegment8Impl_IUnknown_QueryInterface ((LPUNKNOWN)&obj->UnknownVtbl, lpcGUID, ppobj);
1310
}