server: Make FD_WINE_NONBLOCKING into a separate field.
[wine.git] / dlls / dmime / segment.c
blobf5dbf4b9959940dd78f9da20ca30195e807170c0
1 /* IDirectMusicSegment8 Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
4 * Copyright (C) 2003-2004 Raphael Junqueira
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "dmime_private.h"
22 #include "dmobject.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
26 /*****************************************************************************
27 * IDirectMusicSegmentImpl implementation
29 typedef struct IDirectMusicSegment8Impl {
30 IDirectMusicSegment8 IDirectMusicSegment8_iface;
31 struct dmobject dmobj;
32 LONG ref;
33 DMUS_IO_SEGMENT_HEADER header;
34 IDirectMusicGraph *pGraph;
35 struct list Tracks;
36 } IDirectMusicSegment8Impl;
38 static inline IDirectMusicSegment8Impl *impl_from_IDirectMusicSegment8(IDirectMusicSegment8 *iface)
40 return CONTAINING_RECORD(iface, IDirectMusicSegment8Impl, IDirectMusicSegment8_iface);
43 static HRESULT WINAPI IDirectMusicSegment8Impl_QueryInterface(IDirectMusicSegment8 *iface,
44 REFIID riid, void **ret_iface)
46 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
48 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
50 *ret_iface = NULL;
52 if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicSegment) ||
53 IsEqualIID(riid, &IID_IDirectMusicSegment2) ||
54 IsEqualIID (riid, &IID_IDirectMusicSegment8))
55 *ret_iface = iface;
56 else if (IsEqualIID (riid, &IID_IDirectMusicObject))
57 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
58 else if (IsEqualIID (riid, &IID_IPersistStream))
59 *ret_iface = &This->dmobj.IPersistStream_iface;
60 else {
61 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
62 return E_NOINTERFACE;
65 IUnknown_AddRef((IUnknown*)*ret_iface);
66 return S_OK;
69 static ULONG WINAPI IDirectMusicSegment8Impl_AddRef(IDirectMusicSegment8 *iface)
71 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
72 LONG ref = InterlockedIncrement(&This->ref);
74 TRACE("(%p) ref=%d\n", This, ref);
76 return ref;
79 static ULONG WINAPI IDirectMusicSegment8Impl_Release(IDirectMusicSegment8 *iface)
81 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
82 LONG ref = InterlockedDecrement(&This->ref);
84 TRACE("(%p) ref=%d\n", This, ref);
86 if (!ref) {
87 HeapFree(GetProcessHeap(), 0, This);
88 DMIME_UnlockModule();
91 return ref;
94 static HRESULT WINAPI IDirectMusicSegment8Impl_GetLength(IDirectMusicSegment8 *iface,
95 MUSIC_TIME *pmtLength)
97 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
99 TRACE("(%p, %p)\n", This, pmtLength);
100 if (NULL == pmtLength) {
101 return E_POINTER;
103 *pmtLength = This->header.mtLength;
104 return S_OK;
107 static HRESULT WINAPI IDirectMusicSegment8Impl_SetLength(IDirectMusicSegment8 *iface,
108 MUSIC_TIME mtLength)
110 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
112 TRACE("(%p, %d)\n", This, mtLength);
113 This->header.mtLength = mtLength;
114 return S_OK;
117 static HRESULT WINAPI IDirectMusicSegment8Impl_GetRepeats(IDirectMusicSegment8 *iface,
118 DWORD *pdwRepeats)
120 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
122 TRACE("(%p, %p)\n", This, pdwRepeats);
123 if (NULL == pdwRepeats) {
124 return E_POINTER;
126 *pdwRepeats = This->header.dwRepeats;
127 return S_OK;
130 static HRESULT WINAPI IDirectMusicSegment8Impl_SetRepeats(IDirectMusicSegment8 *iface,
131 DWORD dwRepeats)
133 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
135 TRACE("(%p, %d)\n", This, dwRepeats);
136 This->header.dwRepeats = dwRepeats;
137 return S_OK;
140 static HRESULT WINAPI IDirectMusicSegment8Impl_GetDefaultResolution(IDirectMusicSegment8 *iface,
141 DWORD *pdwResolution)
143 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
145 TRACE("(%p, %p)\n", This, pdwResolution);
146 if (NULL == pdwResolution) {
147 return E_POINTER;
149 *pdwResolution = This->header.dwResolution;
150 return S_OK;
153 static HRESULT WINAPI IDirectMusicSegment8Impl_SetDefaultResolution(IDirectMusicSegment8 *iface,
154 DWORD dwResolution)
156 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
158 TRACE("(%p, %d)\n", This, dwResolution);
159 This->header.dwResolution = dwResolution;
160 return S_OK;
163 static HRESULT WINAPI IDirectMusicSegment8Impl_GetTrack(IDirectMusicSegment8 *iface,
164 REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, IDirectMusicTrack **ppTrack)
166 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
167 CLSID pIt_clsid;
168 struct list* pEntry = NULL;
169 LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
170 IPersistStream* pCLSIDStream = NULL;
171 HRESULT hr = S_OK;
173 TRACE("(%p, %s, %#x, %#x, %p)\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, ppTrack);
175 if (NULL == ppTrack) {
176 return E_POINTER;
179 LIST_FOR_EACH (pEntry, &This->Tracks) {
180 pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
181 TRACE(" - %p -> 0x%x,%p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
182 if (0xFFFFFFFF != dwGroupBits && 0 == (pIt->dwGroupBits & dwGroupBits)) continue ;
183 if (FALSE == IsEqualGUID(&GUID_NULL, rguidType)) {
185 * it rguidType is not null we must check if CLSIDs are equal
186 * and the unique way to get it is using IPersistStream Interface
188 hr = IDirectMusicTrack_QueryInterface(pIt->pTrack, &IID_IPersistStream, (void**) &pCLSIDStream);
189 if (FAILED(hr)) {
190 ERR("(%p): object %p don't implement IPersistStream Interface. Expect a crash (critical problem)\n", This, pIt->pTrack);
191 continue ;
193 hr = IPersistStream_GetClassID(pCLSIDStream, &pIt_clsid);
194 IPersistStream_Release(pCLSIDStream); pCLSIDStream = NULL;
195 if (FAILED(hr)) {
196 ERR("(%p): non-implemented GetClassID for object %p\n", This, pIt->pTrack);
197 continue ;
199 TRACE(" - %p -> %s\n", pIt, debugstr_dmguid(&pIt_clsid));
200 if (FALSE == IsEqualGUID(&pIt_clsid, rguidType)) continue ;
202 if (0 == dwIndex) {
203 *ppTrack = pIt->pTrack;
204 IDirectMusicTrack_AddRef(*ppTrack);
205 return S_OK;
207 --dwIndex;
209 return DMUS_E_NOT_FOUND;
212 static HRESULT WINAPI IDirectMusicSegment8Impl_GetTrackGroup(IDirectMusicSegment8 *iface,
213 IDirectMusicTrack *pTrack, DWORD *pdwGroupBits)
215 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
216 struct list* pEntry = NULL;
217 LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
219 TRACE("(%p, %p, %p)\n", This, pTrack, pdwGroupBits);
221 if (NULL == pdwGroupBits) {
222 return E_POINTER;
225 LIST_FOR_EACH (pEntry, &This->Tracks) {
226 pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
227 TRACE(" - %p -> %#x, %p\n", pIt, pIt->dwGroupBits, pIt->pTrack);
228 if (NULL != pIt && pIt->pTrack == pTrack) {
229 *pdwGroupBits = pIt->dwGroupBits;
230 return S_OK;
234 return DMUS_E_NOT_FOUND;
237 static HRESULT WINAPI IDirectMusicSegment8Impl_InsertTrack(IDirectMusicSegment8 *iface,
238 IDirectMusicTrack *pTrack, DWORD group)
240 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
241 DWORD i = 0;
242 struct list* pEntry = NULL;
243 LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
244 LPDMUS_PRIVATE_SEGMENT_TRACK pNewSegTrack = NULL;
246 TRACE("(%p, %p, %#x)\n", This, pTrack, group);
248 if (!group)
249 return E_INVALIDARG;
251 LIST_FOR_EACH (pEntry, &This->Tracks) {
252 i++;
253 pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
254 TRACE(" - #%u: %p -> %#x, %p\n", i, pIt, pIt->dwGroupBits, pIt->pTrack);
255 if (NULL != pIt && pIt->pTrack == pTrack) {
256 ERR("(%p, %p): track is already in list\n", This, pTrack);
257 return E_FAIL;
261 pNewSegTrack = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(DMUS_PRIVATE_SEGMENT_TRACK));
262 if (NULL == pNewSegTrack)
263 return E_OUTOFMEMORY;
265 pNewSegTrack->dwGroupBits = group;
266 pNewSegTrack->pTrack = pTrack;
267 IDirectMusicTrack_Init(pTrack, (IDirectMusicSegment *)iface);
268 IDirectMusicTrack_AddRef(pTrack);
269 list_add_tail (&This->Tracks, &pNewSegTrack->entry);
271 return S_OK;
274 static HRESULT WINAPI IDirectMusicSegment8Impl_RemoveTrack(IDirectMusicSegment8 *iface,
275 IDirectMusicTrack *pTrack)
277 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
278 struct list* pEntry = NULL;
279 LPDMUS_PRIVATE_SEGMENT_TRACK pIt = NULL;
281 TRACE("(%p, %p)\n", This, pTrack);
283 LIST_FOR_EACH (pEntry, &This->Tracks) {
284 pIt = LIST_ENTRY(pEntry, DMUS_PRIVATE_SEGMENT_TRACK, entry);
285 if (pIt->pTrack == pTrack) {
286 TRACE("(%p, %p): track in list\n", This, pTrack);
288 list_remove(&pIt->entry);
289 IDirectMusicTrack_Init(pIt->pTrack, NULL);
290 IDirectMusicTrack_Release(pIt->pTrack);
291 HeapFree(GetProcessHeap(), 0, pIt);
293 return S_OK;
297 return S_FALSE;
300 static HRESULT WINAPI IDirectMusicSegment8Impl_InitPlay(IDirectMusicSegment8 *iface,
301 IDirectMusicSegmentState **ppSegState, IDirectMusicPerformance *pPerformance, DWORD dwFlags)
303 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
304 HRESULT hr;
306 FIXME("(%p, %p, %p, %d): semi-stub\n", This, ppSegState, pPerformance, dwFlags);
307 if (NULL == ppSegState) {
308 return E_POINTER;
310 hr = create_dmsegmentstate(&IID_IDirectMusicSegmentState,(void**) ppSegState);
311 if (FAILED(hr)) {
312 return hr;
314 /* TODO: DMUS_SEGF_FLAGS */
315 return S_OK;
318 static HRESULT WINAPI IDirectMusicSegment8Impl_GetGraph(IDirectMusicSegment8 *iface,
319 IDirectMusicGraph **ppGraph)
321 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
323 FIXME("(%p, %p): semi-stub\n", This, ppGraph);
324 if (NULL == ppGraph) {
325 return E_POINTER;
327 if (NULL == This->pGraph) {
328 return DMUS_E_NOT_FOUND;
330 /**
331 * should return This, as seen in msdn
332 * "...The segment object implements IDirectMusicGraph directly..."
334 *ppGraph = This->pGraph;
335 IDirectMusicGraph_AddRef(This->pGraph);
336 return S_OK;
339 static HRESULT WINAPI IDirectMusicSegment8Impl_SetGraph(IDirectMusicSegment8 *iface,
340 IDirectMusicGraph *pGraph)
342 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
344 FIXME("(%p, %p): to complete\n", This, pGraph);
345 if (NULL != This->pGraph) {
346 IDirectMusicGraph_Release(This->pGraph);
348 This->pGraph = pGraph;
349 if (NULL != This->pGraph) {
350 IDirectMusicGraph_AddRef(This->pGraph);
352 return S_OK;
355 static HRESULT WINAPI IDirectMusicSegment8Impl_AddNotificationType(IDirectMusicSegment8 *iface,
356 REFGUID rguidNotificationType)
358 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
359 FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
360 return S_OK;
363 static HRESULT WINAPI IDirectMusicSegment8Impl_RemoveNotificationType(IDirectMusicSegment8 *iface,
364 REFGUID rguidNotificationType)
366 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
367 FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
368 return S_OK;
371 static HRESULT WINAPI IDirectMusicSegment8Impl_GetParam(IDirectMusicSegment8 *iface, REFGUID type,
372 DWORD group, DWORD index, MUSIC_TIME time, MUSIC_TIME *next, void *param)
374 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
375 IDirectMusicTrack *track;
376 unsigned int i, count;
377 HRESULT hr = DMUS_E_TRACK_NOT_FOUND;
379 TRACE("(%p, %s, %#x, %u, %d, %p, %p)\n", This, debugstr_dmguid(type), group, index, time,
380 next, param);
382 if (!type)
383 return E_POINTER;
385 /* Index is relative to the search pattern: group bits and supported param type */
386 for (i = 0, count = 0; i < DMUS_SEG_ANYTRACK && count <= index; i++) {
387 if (FAILED(IDirectMusicSegment8Impl_GetTrack(iface, &GUID_NULL, group, i, &track)))
388 break;
389 if (FAILED(IDirectMusicTrack_IsParamSupported(track, type)))
390 continue;
391 if (index == count || index == DMUS_SEG_ANYTRACK)
392 hr = IDirectMusicTrack_GetParam(track, type, time, next, param);
393 IDirectMusicTrack_Release(track);
395 if (SUCCEEDED(hr))
396 return hr;
397 count++;
400 TRACE("(%p): not found\n", This);
402 return hr;
405 static HRESULT WINAPI IDirectMusicSegment8Impl_SetParam(IDirectMusicSegment8 *iface,
406 REFGUID rguidType, DWORD dwGroupBits, DWORD dwIndex, MUSIC_TIME mtTime, void *pParam)
408 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
409 FIXME("(%p, %s, %#x, %d, %d, %p): stub\n", This, debugstr_dmguid(rguidType), dwGroupBits, dwIndex, mtTime, pParam);
410 return S_OK;
413 static HRESULT WINAPI IDirectMusicSegment8Impl_Clone(IDirectMusicSegment8 *iface,
414 MUSIC_TIME mtStart, MUSIC_TIME mtEnd, IDirectMusicSegment **ppSegment)
416 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
417 FIXME("(%p, %d, %d, %p): stub\n", This, mtStart, mtEnd, ppSegment);
418 return S_OK;
421 static HRESULT WINAPI IDirectMusicSegment8Impl_SetStartPoint(IDirectMusicSegment8 *iface,
422 MUSIC_TIME mtStart)
424 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
426 TRACE("(%p, %d)\n", This, mtStart);
427 if (mtStart >= This->header.mtLength) {
428 return DMUS_E_OUT_OF_RANGE;
430 This->header.mtPlayStart = mtStart;
431 return S_OK;
434 static HRESULT WINAPI IDirectMusicSegment8Impl_GetStartPoint(IDirectMusicSegment8 *iface,
435 MUSIC_TIME *pmtStart)
437 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
439 TRACE("(%p, %p)\n", This, pmtStart);
440 if (NULL == pmtStart) {
441 return E_POINTER;
443 *pmtStart = This->header.mtPlayStart;
444 return S_OK;
447 static HRESULT WINAPI IDirectMusicSegment8Impl_SetLoopPoints(IDirectMusicSegment8 *iface,
448 MUSIC_TIME start, MUSIC_TIME end)
450 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
452 TRACE("(%p, %d, %d)\n", This, start, end);
454 if ((end || start) &&
455 (start >= This->header.mtLength || end > This->header.mtLength || start > end))
456 return DMUS_E_OUT_OF_RANGE;
458 This->header.mtLoopStart = start;
459 This->header.mtLoopEnd = end;
461 return S_OK;
464 static HRESULT WINAPI IDirectMusicSegment8Impl_GetLoopPoints(IDirectMusicSegment8 *iface,
465 MUSIC_TIME *pmtStart, MUSIC_TIME *pmtEnd)
467 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
469 TRACE("(%p, %p, %p)\n", This, pmtStart, pmtEnd);
470 if (NULL == pmtStart || NULL == pmtEnd) {
471 return E_POINTER;
473 *pmtStart = This->header.mtLoopStart;
474 *pmtEnd = This->header.mtLoopEnd;
475 return S_OK;
478 static HRESULT WINAPI IDirectMusicSegment8Impl_SetPChannelsUsed(IDirectMusicSegment8 *iface,
479 DWORD dwNumPChannels, DWORD *paPChannels)
481 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
482 FIXME("(%p, %d, %p): stub\n", This, dwNumPChannels, paPChannels);
483 return S_OK;
486 static HRESULT WINAPI IDirectMusicSegment8Impl_SetTrackConfig(IDirectMusicSegment8 *iface,
487 REFGUID rguidTrackClassID, DWORD dwGroupBits, DWORD dwIndex, DWORD dwFlagsOn,
488 DWORD dwFlagsOff)
490 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
491 FIXME("(%p, %s, %#x, %d, %d, %d): stub\n", This, debugstr_dmguid(rguidTrackClassID), dwGroupBits, dwIndex, dwFlagsOn, dwFlagsOff);
492 return S_OK;
495 static HRESULT WINAPI IDirectMusicSegment8Impl_GetAudioPathConfig(IDirectMusicSegment8 *iface,
496 IUnknown **ppAudioPathConfig)
498 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
499 FIXME("(%p, %p): stub\n", This, ppAudioPathConfig);
500 return S_OK;
503 static HRESULT WINAPI IDirectMusicSegment8Impl_Compose(IDirectMusicSegment8 *iface,
504 MUSIC_TIME mtTime, IDirectMusicSegment *pFromSegment, IDirectMusicSegment *pToSegment,
505 IDirectMusicSegment **ppComposedSegment)
507 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
508 FIXME("(%p, %d, %p, %p, %p): stub\n", This, mtTime, pFromSegment, pToSegment, ppComposedSegment);
509 return S_OK;
512 static HRESULT WINAPI IDirectMusicSegment8Impl_Download(IDirectMusicSegment8 *iface,
513 IUnknown *pAudioPath)
515 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
516 FIXME("(%p, %p): stub\n", This, pAudioPath);
517 return S_OK;
520 static HRESULT WINAPI IDirectMusicSegment8Impl_Unload(IDirectMusicSegment8 *iface,
521 IUnknown *pAudioPath)
523 IDirectMusicSegment8Impl *This = impl_from_IDirectMusicSegment8(iface);
524 FIXME("(%p, %p): stub\n", This, pAudioPath);
525 return S_OK;
528 static const IDirectMusicSegment8Vtbl dmsegment8_vtbl = {
529 IDirectMusicSegment8Impl_QueryInterface,
530 IDirectMusicSegment8Impl_AddRef,
531 IDirectMusicSegment8Impl_Release,
532 IDirectMusicSegment8Impl_GetLength,
533 IDirectMusicSegment8Impl_SetLength,
534 IDirectMusicSegment8Impl_GetRepeats,
535 IDirectMusicSegment8Impl_SetRepeats,
536 IDirectMusicSegment8Impl_GetDefaultResolution,
537 IDirectMusicSegment8Impl_SetDefaultResolution,
538 IDirectMusicSegment8Impl_GetTrack,
539 IDirectMusicSegment8Impl_GetTrackGroup,
540 IDirectMusicSegment8Impl_InsertTrack,
541 IDirectMusicSegment8Impl_RemoveTrack,
542 IDirectMusicSegment8Impl_InitPlay,
543 IDirectMusicSegment8Impl_GetGraph,
544 IDirectMusicSegment8Impl_SetGraph,
545 IDirectMusicSegment8Impl_AddNotificationType,
546 IDirectMusicSegment8Impl_RemoveNotificationType,
547 IDirectMusicSegment8Impl_GetParam,
548 IDirectMusicSegment8Impl_SetParam,
549 IDirectMusicSegment8Impl_Clone,
550 IDirectMusicSegment8Impl_SetStartPoint,
551 IDirectMusicSegment8Impl_GetStartPoint,
552 IDirectMusicSegment8Impl_SetLoopPoints,
553 IDirectMusicSegment8Impl_GetLoopPoints,
554 IDirectMusicSegment8Impl_SetPChannelsUsed,
555 IDirectMusicSegment8Impl_SetTrackConfig,
556 IDirectMusicSegment8Impl_GetAudioPathConfig,
557 IDirectMusicSegment8Impl_Compose,
558 IDirectMusicSegment8Impl_Download,
559 IDirectMusicSegment8Impl_Unload
562 /* IDirectMusicSegment8Impl IDirectMusicObject part: */
563 static HRESULT WINAPI seg_IDirectMusicObject_ParseDescriptor(IDirectMusicObject *iface,
564 IStream *stream, DMUS_OBJECTDESC *desc)
566 struct chunk_entry riff = {0};
567 DWORD supported = DMUS_OBJ_OBJECT | DMUS_OBJ_VERSION;
568 HRESULT hr;
570 TRACE("(%p, %p, %p)\n", iface, stream, desc);
572 if (!stream || !desc)
573 return E_POINTER;
575 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
576 return hr;
577 if (riff.id != FOURCC_RIFF || !(riff.type == DMUS_FOURCC_SEGMENT_FORM ||
578 riff.type == mmioFOURCC('W','A','V','E'))) {
579 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
580 stream_skip_chunk(stream, &riff);
581 return E_FAIL;
584 if (riff.type == DMUS_FOURCC_SEGMENT_FORM)
585 supported |= DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY;
586 else
587 supported |= DMUS_OBJ_NAME_INFO;
588 hr = dmobj_parsedescriptor(stream, &riff, desc, supported);
589 if (FAILED(hr))
590 return hr;
592 desc->guidClass = CLSID_DirectMusicSegment;
593 desc->dwValidData |= DMUS_OBJ_CLASS;
595 dump_DMUS_OBJECTDESC(desc);
596 return S_OK;
599 static const IDirectMusicObjectVtbl dmobject_vtbl = {
600 dmobj_IDirectMusicObject_QueryInterface,
601 dmobj_IDirectMusicObject_AddRef,
602 dmobj_IDirectMusicObject_Release,
603 dmobj_IDirectMusicObject_GetDescriptor,
604 dmobj_IDirectMusicObject_SetDescriptor,
605 seg_IDirectMusicObject_ParseDescriptor
608 /* IDirectMusicSegment8Impl IPersistStream part: */
609 static HRESULT parse_track_form(IDirectMusicSegment8Impl *This, IStream *stream,
610 const struct chunk_entry *riff)
612 struct chunk_entry chunk = {.parent = riff};
613 IDirectMusicTrack *track = NULL;
614 IPersistStream *ps = NULL;
615 IStream *clone;
616 DMUS_IO_TRACK_HEADER thdr;
617 DMUS_IO_TRACK_EXTRAS_HEADER txhdr = {0};
618 HRESULT hr;
619 DMUS_PRIVATE_SEGMENT_TRACK *item;
621 TRACE("Parsing track form in %p: %s\n", stream, debugstr_chunk(riff));
623 /* First chunk must be the track header */
624 if (FAILED(hr = stream_get_chunk(stream, &chunk)))
625 return hr;
626 if (chunk.id != DMUS_FOURCC_TRACK_CHUNK)
627 return DMUS_E_TRACK_HDR_NOT_FIRST_CK;
628 if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &thdr, sizeof(thdr))))
629 return hr;
630 TRACE("Found DMUS_IO_TRACK_HEADER\n");
631 TRACE("\tclass: %s\n", debugstr_guid (&thdr.guidClassID));
632 TRACE("\tdwGroup: %#x\n", thdr.dwGroup);
633 TRACE("\tckid: %s\n", debugstr_fourcc (thdr.ckid));
634 TRACE("\tfccType: %s\n", debugstr_fourcc (thdr.fccType));
636 if (!!thdr.ckid == !!thdr.fccType) {
637 WARN("One and only one of the ckid (%s) and fccType (%s) need to be set\n",
638 debugstr_fourcc(thdr.ckid), debugstr_fourcc(thdr.fccType));
639 return DMUS_E_INVALID_TRACK_HDR;
642 /* Optional chunks */
643 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
644 if ((thdr.ckid && chunk.id == thdr.ckid) ||
645 (!thdr.ckid && (chunk.id == FOURCC_LIST || chunk.id == FOURCC_RIFF) &&
646 chunk.type == thdr.fccType))
647 break;
649 if (chunk.id == DMUS_FOURCC_TRACK_EXTRAS_CHUNK &&
650 SUCCEEDED(stream_chunk_get_data(stream, &chunk, &txhdr, sizeof(txhdr)))) {
651 FIXME("DMUS_IO_TRACK_EXTRAS_HEADER chunk not fully handled\n");
652 TRACE("dwFlags: %#x, dwPriority: %u\n", txhdr.dwFlags, txhdr.dwPriority);
655 if (hr != S_OK)
656 return hr == S_FALSE ? DMUS_E_TRACK_NOT_FOUND : hr;
658 /* Some DirectMusicTrack implementation expect the stream to start with their data chunk */
659 if (FAILED(hr = IStream_Clone(stream, &clone)))
660 return hr;
661 stream_reset_chunk_start(clone, &chunk);
663 /* Load the track */
664 hr = CoCreateInstance(&thdr.guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack,
665 (void **)&track);
666 if (FAILED(hr))
667 goto done;
668 hr = IDirectMusicTrack_QueryInterface(track, &IID_IPersistStream, (void **)&ps);
669 if (FAILED(hr))
670 goto done;
671 hr = IPersistStream_Load(ps, clone);
672 if (FAILED(hr))
673 goto done;
675 hr = IDirectMusicSegment8_InsertTrack(&This->IDirectMusicSegment8_iface, track, thdr.dwGroup);
676 if (FAILED(hr))
677 goto done;
679 item = LIST_ENTRY(list_tail(&This->Tracks), DMUS_PRIVATE_SEGMENT_TRACK, entry);
680 item->flags = txhdr.dwFlags;
682 done:
683 if (ps)
684 IPersistStream_Release(ps);
685 if (track)
686 IDirectMusicTrack_Release(track);
687 IStream_Release(clone);
689 return hr;
692 static HRESULT parse_track_list(IDirectMusicSegment8Impl *This, IStream *stream,
693 const struct chunk_entry *trkl)
695 struct chunk_entry chunk = {.parent = trkl};
696 HRESULT hr;
698 TRACE("Parsing track list in %p: %s\n", stream, debugstr_chunk(trkl));
700 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK)
701 if (chunk.id == FOURCC_RIFF && chunk.type == DMUS_FOURCC_TRACK_FORM)
702 hr = parse_track_form(This, stream, &chunk);
704 return SUCCEEDED(hr) ? S_OK : hr;
707 static inline void dump_segment_header(DMUS_IO_SEGMENT_HEADER *h, DWORD size)
709 unsigned int dx = 9;
711 if (size == offsetof(DMUS_IO_SEGMENT_HEADER, rtLength))
712 dx = 7;
713 else if (size == offsetof(DMUS_IO_SEGMENT_HEADER, rtLoopStart))
714 dx = 8;
715 TRACE("Found DirectX%d DMUS_IO_SEGMENT_HEADER\n", dx);
716 TRACE("\tdwRepeats: %u\n", h->dwRepeats);
717 TRACE("\tmtLength: %u\n", h->mtLength);
718 TRACE("\tmtPlayStart: %u\n", h->mtPlayStart);
719 TRACE("\tmtLoopStart: %u\n", h->mtLoopStart);
720 TRACE("\tmtLoopEnd: %u\n", h->mtLoopEnd);
721 TRACE("\tdwResolution: %u\n", h->dwResolution);
722 if (dx >= 8) {
723 TRACE("\trtLength: %s\n", wine_dbgstr_longlong(h->rtLength));
724 TRACE("\tdwFlags: %u\n", h->dwFlags);
725 TRACE("\tdwReserved: %u\n", h->dwReserved);
727 if (dx == 9) {
728 TRACE("\trtLoopStart: %s\n", wine_dbgstr_longlong(h->rtLoopStart));
729 TRACE("\trtLoopEnd: %s\n", wine_dbgstr_longlong(h->rtLoopEnd));
730 TRACE("\trtPlayStart: %s\n", wine_dbgstr_longlong(h->rtPlayStart));
734 static HRESULT parse_segment_form(IDirectMusicSegment8Impl *This, IStream *stream,
735 const struct chunk_entry *riff)
737 struct chunk_entry chunk = {.parent = riff};
738 HRESULT hr;
740 TRACE("Parsing segment form in %p: %s\n", stream, debugstr_chunk(riff));
742 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
743 switch (chunk.id) {
744 case DMUS_FOURCC_SEGMENT_CHUNK:
745 /* DX7, DX8 and DX9 structure sizes */
746 if (chunk.size != offsetof(DMUS_IO_SEGMENT_HEADER, rtLength) &&
747 chunk.size != offsetof(DMUS_IO_SEGMENT_HEADER, rtLoopStart) &&
748 chunk.size != sizeof(DMUS_IO_SEGMENT_HEADER)) {
749 WARN("Invalid size of %s\n", debugstr_chunk(&chunk));
750 break;
752 if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &This->header, chunk.size))) {
753 WARN("Failed to read data of %s\n", debugstr_chunk(&chunk));
754 return hr;
756 dump_segment_header(&This->header, chunk.size);
757 break;
758 case FOURCC_LIST:
759 if (chunk.type == DMUS_FOURCC_TRACK_LIST)
760 if (FAILED(hr = parse_track_list(This, stream, &chunk)))
761 return hr;
762 break;
763 case FOURCC_RIFF:
764 FIXME("Loading of embedded RIFF form %s\n", debugstr_fourcc(chunk.type));
765 break;
769 return SUCCEEDED(hr) ? S_OK : hr;
772 static inline IDirectMusicSegment8Impl *impl_from_IPersistStream(IPersistStream *iface)
774 return CONTAINING_RECORD(iface, IDirectMusicSegment8Impl, dmobj.IPersistStream_iface);
777 static HRESULT WINAPI seg_IPersistStream_Load(IPersistStream *iface, IStream *stream)
779 IDirectMusicSegment8Impl *This = impl_from_IPersistStream(iface);
780 struct chunk_entry riff = {0};
781 HRESULT hr;
783 TRACE("(%p, %p): Loading\n", This, stream);
785 if (!stream)
786 return E_POINTER;
788 if (stream_get_chunk(stream, &riff) != S_OK ||
789 (riff.id != FOURCC_RIFF && riff.id != mmioFOURCC('M','T','h','d')))
790 return DMUS_E_UNSUPPORTED_STREAM;
791 stream_reset_chunk_start(stream, &riff);
793 if (riff.id == mmioFOURCC('M','T','h','d')) {
794 FIXME("MIDI file loading not supported\n");
795 return S_OK;
798 hr = IDirectMusicObject_ParseDescriptor(&This->dmobj.IDirectMusicObject_iface, stream,
799 &This->dmobj.desc);
800 if (FAILED(hr))
801 return hr;
802 stream_reset_chunk_data(stream, &riff);
804 if (riff.type == DMUS_FOURCC_SEGMENT_FORM)
805 hr = parse_segment_form(This, stream, &riff);
806 else {
807 FIXME("WAVE form loading not implemented\n");
808 hr = S_OK;
811 return hr;
814 static const IPersistStreamVtbl persiststream_vtbl = {
815 dmobj_IPersistStream_QueryInterface,
816 dmobj_IPersistStream_AddRef,
817 dmobj_IPersistStream_Release,
818 dmobj_IPersistStream_GetClassID,
819 unimpl_IPersistStream_IsDirty,
820 seg_IPersistStream_Load,
821 unimpl_IPersistStream_Save,
822 unimpl_IPersistStream_GetSizeMax
825 /* for ClassFactory */
826 HRESULT WINAPI create_dmsegment(REFIID lpcGUID, void **ppobj)
828 IDirectMusicSegment8Impl* obj;
829 HRESULT hr;
831 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicSegment8Impl));
832 if (NULL == obj) {
833 *ppobj = NULL;
834 return E_OUTOFMEMORY;
836 obj->IDirectMusicSegment8_iface.lpVtbl = &dmsegment8_vtbl;
837 obj->ref = 1;
838 dmobject_init(&obj->dmobj, &CLSID_DirectMusicSegment,
839 (IUnknown *)&obj->IDirectMusicSegment8_iface);
840 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
841 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
842 list_init (&obj->Tracks);
844 DMIME_LockModule();
845 hr = IDirectMusicSegment8_QueryInterface(&obj->IDirectMusicSegment8_iface, lpcGUID, ppobj);
846 IDirectMusicSegment8_Release(&obj->IDirectMusicSegment8_iface);
848 return hr;