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"
24 WINE_DEFAULT_DEBUG_CHANNEL(dmime
);
31 IDirectMusicTrack
*pTrack
;
34 static void track_entry_destroy(struct track_entry
*entry
)
38 if (FAILED(hr
= IDirectMusicTrack_Init(entry
->pTrack
, NULL
)))
39 WARN("Failed to de-init track %p, hr %#lx\n", entry
->pTrack
, hr
);
40 IDirectMusicTrack_Release(entry
->pTrack
);
47 IDirectMusicSegment8 IDirectMusicSegment8_iface
;
48 struct dmobject dmobj
;
50 DMUS_IO_SEGMENT_HEADER header
;
51 IDirectMusicGraph
*pGraph
;
54 PCMWAVEFORMAT wave_format
;
59 static struct segment
*segment_create(void);
61 static inline struct segment
*impl_from_IDirectMusicSegment8(IDirectMusicSegment8
*iface
)
63 return CONTAINING_RECORD(iface
, struct segment
, IDirectMusicSegment8_iface
);
66 static HRESULT WINAPI
segment_QueryInterface(IDirectMusicSegment8
*iface
, REFIID riid
, void **ret_iface
)
68 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
70 TRACE("(%p, %s, %p)\n", This
, debugstr_dmguid(riid
), ret_iface
);
74 if (IsEqualIID (riid
, &IID_IUnknown
) || IsEqualIID (riid
, &IID_IDirectMusicSegment
) ||
75 IsEqualIID(riid
, &IID_IDirectMusicSegment2
) ||
76 IsEqualIID (riid
, &IID_IDirectMusicSegment8
))
78 else if (IsEqualIID (riid
, &IID_IDirectMusicObject
))
79 *ret_iface
= &This
->dmobj
.IDirectMusicObject_iface
;
80 else if (IsEqualIID (riid
, &IID_IPersistStream
))
81 *ret_iface
= &This
->dmobj
.IPersistStream_iface
;
83 WARN("(%p, %s, %p): not found\n", This
, debugstr_dmguid(riid
), ret_iface
);
87 IUnknown_AddRef((IUnknown
*)*ret_iface
);
91 static ULONG WINAPI
segment_AddRef(IDirectMusicSegment8
*iface
)
93 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
94 LONG ref
= InterlockedIncrement(&This
->ref
);
96 TRACE("(%p) ref=%ld\n", This
, ref
);
101 static ULONG WINAPI
segment_Release(IDirectMusicSegment8
*iface
)
103 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
104 LONG ref
= InterlockedDecrement(&This
->ref
);
106 TRACE("(%p) ref=%ld\n", This
, ref
);
109 struct track_entry
*entry
, *next
;
111 LIST_FOR_EACH_ENTRY_SAFE(entry
, next
, &This
->tracks
, struct track_entry
, entry
)
113 list_remove(&entry
->entry
);
114 track_entry_destroy(entry
);
118 free(This
->wave_data
);
126 static HRESULT WINAPI
segment_GetLength(IDirectMusicSegment8
*iface
, MUSIC_TIME
*length
)
128 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
130 TRACE("(%p, %p)\n", This
, length
);
132 if (!length
) return E_POINTER
;
133 *length
= This
->header
.mtLength
;
138 static HRESULT WINAPI
segment_SetLength(IDirectMusicSegment8
*iface
, MUSIC_TIME length
)
140 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
142 TRACE("(%p, %ld)\n", This
, length
);
144 This
->header
.mtLength
= length
;
149 static HRESULT WINAPI
segment_GetRepeats(IDirectMusicSegment8
*iface
, DWORD
*repeats
)
151 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
153 TRACE("(%p, %p)\n", This
, repeats
);
155 if (!repeats
) return E_POINTER
;
156 *repeats
= This
->header
.dwRepeats
;
161 static HRESULT WINAPI
segment_SetRepeats(IDirectMusicSegment8
*iface
, DWORD repeats
)
163 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
165 TRACE("(%p, %ld)\n", This
, repeats
);
166 This
->header
.dwRepeats
= repeats
;
171 static HRESULT WINAPI
segment_GetDefaultResolution(IDirectMusicSegment8
*iface
, DWORD
*resolution
)
173 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
175 TRACE("(%p, %p)\n", This
, resolution
);
177 if (!resolution
) return E_POINTER
;
178 *resolution
= This
->header
.dwResolution
;
183 static HRESULT WINAPI
segment_SetDefaultResolution(IDirectMusicSegment8
*iface
, DWORD resolution
)
185 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
187 TRACE("(%p, %ld)\n", This
, resolution
);
188 This
->header
.dwResolution
= resolution
;
193 static HRESULT WINAPI
segment_GetTrack(IDirectMusicSegment8
*iface
, REFGUID type
, DWORD group
,
194 DWORD index
, IDirectMusicTrack
**ret_track
)
196 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
197 struct track_entry
*entry
;
200 TRACE("(%p, %s, %#lx, %#lx, %p)\n", This
, debugstr_dmguid(type
), group
, index
, ret_track
);
202 if (!ret_track
) return E_POINTER
;
204 LIST_FOR_EACH_ENTRY(entry
, &This
->tracks
, struct track_entry
, entry
)
206 if (group
!= -1 && !(entry
->dwGroupBits
& group
)) continue;
208 if (!IsEqualGUID(&GUID_NULL
, type
))
210 CLSID entry_type
= GUID_NULL
;
211 IPersistStream
*persist
;
213 if (SUCCEEDED(hr
= IDirectMusicTrack_QueryInterface(entry
->pTrack
, &IID_IPersistStream
, (void **)&persist
)))
215 hr
= IPersistStream_GetClassID(persist
, &entry_type
);
216 if (SUCCEEDED(hr
)) TRACE(" - %p -> %s\n", entry
, debugstr_dmguid(&entry_type
));
217 IPersistStream_Release(persist
);
220 if (!IsEqualGUID(&entry_type
, type
)) continue;
225 *ret_track
= entry
->pTrack
;
226 IDirectMusicTrack_AddRef(entry
->pTrack
);
231 return DMUS_E_NOT_FOUND
;
234 static HRESULT WINAPI
segment_GetTrackGroup(IDirectMusicSegment8
*iface
, IDirectMusicTrack
*track
, DWORD
*ret_group
)
236 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
237 struct track_entry
*entry
;
239 TRACE("(%p, %p, %p)\n", This
, track
, ret_group
);
241 if (!ret_group
) return E_POINTER
;
243 LIST_FOR_EACH_ENTRY(entry
, &This
->tracks
, struct track_entry
, entry
)
245 if (entry
->pTrack
== track
)
247 *ret_group
= entry
->dwGroupBits
;
252 return DMUS_E_NOT_FOUND
;
255 static HRESULT
segment_append_track(struct segment
*This
, IDirectMusicTrack
*track
, DWORD group
, DWORD flags
)
257 struct track_entry
*entry
;
260 if (!(entry
= calloc(1, sizeof(*entry
)))) return E_OUTOFMEMORY
;
261 entry
->dwGroupBits
= group
;
262 entry
->flags
= flags
;
263 entry
->pTrack
= track
;
264 IDirectMusicTrack_AddRef(track
);
266 hr
= IDirectMusicTrack_Init(track
, (IDirectMusicSegment
*)&This
->IDirectMusicSegment8_iface
);
267 if (FAILED(hr
)) track_entry_destroy(entry
);
268 else list_add_tail(&This
->tracks
, &entry
->entry
);
273 static HRESULT WINAPI
segment_InsertTrack(IDirectMusicSegment8
*iface
, IDirectMusicTrack
*track
, DWORD group
)
275 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
276 struct track_entry
*entry
;
278 TRACE("(%p, %p, %#lx)\n", This
, track
, group
);
280 if (!group
) return E_INVALIDARG
;
282 LIST_FOR_EACH_ENTRY(entry
, &This
->tracks
, struct track_entry
, entry
)
283 if (entry
->pTrack
== track
) return E_FAIL
;
285 return segment_append_track(This
, track
, group
, 0);
288 static HRESULT WINAPI
segment_RemoveTrack(IDirectMusicSegment8
*iface
, IDirectMusicTrack
*track
)
290 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
291 struct track_entry
*entry
;
293 TRACE("(%p, %p)\n", This
, track
);
295 LIST_FOR_EACH_ENTRY(entry
, &This
->tracks
, struct track_entry
, entry
)
297 if (entry
->pTrack
== track
)
299 list_remove(&entry
->entry
);
300 track_entry_destroy(entry
);
308 static HRESULT WINAPI
segment_InitPlay(IDirectMusicSegment8
*iface
,
309 IDirectMusicSegmentState
**state
, IDirectMusicPerformance
*performance
, DWORD flags
)
311 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
314 FIXME("(%p, %p, %p, %ld): semi-stub\n", This
, state
, performance
, flags
);
316 if (!state
) return E_POINTER
;
317 if (FAILED(hr
= create_dmsegmentstate(&IID_IDirectMusicSegmentState
, (void **)state
))) return hr
;
319 /* TODO: DMUS_SEGF_FLAGS */
323 static HRESULT WINAPI
segment_GetGraph(IDirectMusicSegment8
*iface
, IDirectMusicGraph
**graph
)
325 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
327 FIXME("(%p, %p): semi-stub\n", This
, graph
);
329 if (!graph
) return E_POINTER
;
330 if (!(*graph
= This
->pGraph
)) return DMUS_E_NOT_FOUND
;
331 IDirectMusicGraph_AddRef(This
->pGraph
);
336 static HRESULT WINAPI
segment_SetGraph(IDirectMusicSegment8
*iface
, IDirectMusicGraph
*graph
)
338 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
340 FIXME("(%p, %p): to complete\n", This
, graph
);
342 if (This
->pGraph
) IDirectMusicGraph_Release(This
->pGraph
);
343 if ((This
->pGraph
= graph
)) IDirectMusicGraph_AddRef(This
->pGraph
);
348 static HRESULT WINAPI
segment_AddNotificationType(IDirectMusicSegment8
*iface
, REFGUID rguidNotificationType
)
350 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
351 FIXME("(%p, %s): stub\n", This
, debugstr_dmguid(rguidNotificationType
));
355 static HRESULT WINAPI
segment_RemoveNotificationType(IDirectMusicSegment8
*iface
, REFGUID rguidNotificationType
)
357 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
358 FIXME("(%p, %s): stub\n", This
, debugstr_dmguid(rguidNotificationType
));
362 static HRESULT WINAPI
segment_GetParam(IDirectMusicSegment8
*iface
, REFGUID type
, DWORD group
,
363 DWORD index
, MUSIC_TIME time
, MUSIC_TIME
*next
, void *param
)
365 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
366 IDirectMusicTrack
*track
;
367 unsigned int i
, count
;
368 HRESULT hr
= DMUS_E_TRACK_NOT_FOUND
;
370 TRACE("(%p, %s, %#lx, %lu, %ld, %p, %p)\n", This
, debugstr_dmguid(type
), group
, index
, time
,
376 /* Index is relative to the search pattern: group bits and supported param type */
377 for (i
= 0, count
= 0; i
< DMUS_SEG_ANYTRACK
&& count
<= index
; i
++) {
378 if (FAILED(segment_GetTrack(iface
, &GUID_NULL
, group
, i
, &track
))) break;
379 if (FAILED(IDirectMusicTrack_IsParamSupported(track
, type
)))
381 if (index
== count
|| index
== DMUS_SEG_ANYTRACK
)
382 hr
= IDirectMusicTrack_GetParam(track
, type
, time
, next
, param
);
383 IDirectMusicTrack_Release(track
);
390 TRACE("(%p): not found\n", This
);
395 static HRESULT WINAPI
segment_SetParam(IDirectMusicSegment8
*iface
, REFGUID rguidType
,
396 DWORD dwGroupBits
, DWORD dwIndex
, MUSIC_TIME mtTime
, void *pParam
)
398 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
399 FIXME("(%p, %s, %#lx, %ld, %ld, %p): stub\n", This
, debugstr_dmguid(rguidType
), dwGroupBits
,
400 dwIndex
, mtTime
, pParam
);
404 static HRESULT WINAPI
segment_Clone(IDirectMusicSegment8
*iface
, MUSIC_TIME start
, MUSIC_TIME end
,
405 IDirectMusicSegment
**segment
)
407 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
408 struct segment
*clone
;
409 IDirectMusicTrack
*track
;
410 struct track_entry
*entry
;
413 TRACE("(%p, %ld, %ld, %p)\n", This
, start
, end
, segment
);
415 if (!segment
) return E_POINTER
;
417 if (!(clone
= segment_create()))
420 return E_OUTOFMEMORY
;
423 clone
->header
= This
->header
;
424 if ((clone
->pGraph
= This
->pGraph
)) IDirectMusicGraph_AddRef(clone
->pGraph
);
426 LIST_FOR_EACH_ENTRY(entry
, &This
->tracks
, struct track_entry
, entry
)
428 if (FAILED(hr
= IDirectMusicTrack_Clone(entry
->pTrack
, start
, end
, &track
))) break;
429 if (FAILED(hr
= segment_append_track(clone
, track
, entry
->dwGroupBits
, entry
->flags
))) break;
432 *segment
= (IDirectMusicSegment
*)&clone
->IDirectMusicSegment8_iface
;
433 return FAILED(hr
) ? S_FALSE
: S_OK
;
436 static HRESULT WINAPI
segment_SetStartPoint(IDirectMusicSegment8
*iface
, MUSIC_TIME start
)
438 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
440 TRACE("(%p, %ld)\n", This
, start
);
442 if (start
>= This
->header
.mtLength
) return DMUS_E_OUT_OF_RANGE
;
443 This
->header
.mtPlayStart
= start
;
448 static HRESULT WINAPI
segment_GetStartPoint(IDirectMusicSegment8
*iface
, MUSIC_TIME
*start
)
450 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
452 TRACE("(%p, %p)\n", This
, start
);
453 if (!start
) return E_POINTER
;
454 *start
= This
->header
.mtPlayStart
;
459 static HRESULT WINAPI
segment_SetLoopPoints(IDirectMusicSegment8
*iface
, MUSIC_TIME start
, MUSIC_TIME end
)
461 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
463 TRACE("(%p, %ld, %ld)\n", This
, start
, end
);
465 if ((end
|| start
) && (start
>= This
->header
.mtLength
|| end
> This
->header
.mtLength
|| start
> end
))
466 return DMUS_E_OUT_OF_RANGE
;
467 This
->header
.mtLoopStart
= start
;
468 This
->header
.mtLoopEnd
= end
;
473 static HRESULT WINAPI
segment_GetLoopPoints(IDirectMusicSegment8
*iface
, MUSIC_TIME
*start
, MUSIC_TIME
*end
)
475 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
477 TRACE("(%p, %p, %p)\n", This
, start
, end
);
479 if (!start
|| !end
) return E_POINTER
;
480 *start
= This
->header
.mtLoopStart
;
481 *end
= This
->header
.mtLoopEnd
;
486 static HRESULT WINAPI
segment_SetPChannelsUsed(IDirectMusicSegment8
*iface
, DWORD dwNumPChannels
, DWORD
*paPChannels
)
488 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
489 FIXME("(%p, %ld, %p): stub\n", This
, dwNumPChannels
, paPChannels
);
493 static HRESULT WINAPI
segment_SetTrackConfig(IDirectMusicSegment8
*iface
, REFGUID rguidTrackClassID
,
494 DWORD dwGroupBits
, DWORD dwIndex
, DWORD dwFlagsOn
, DWORD dwFlagsOff
)
496 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
497 FIXME("(%p, %s, %#lx, %ld, %ld, %ld): stub\n", This
, debugstr_dmguid(rguidTrackClassID
),
498 dwGroupBits
, dwIndex
, dwFlagsOn
, dwFlagsOff
);
502 static HRESULT WINAPI
segment_GetAudioPathConfig(IDirectMusicSegment8
*iface
, IUnknown
**ppAudioPathConfig
)
504 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
505 FIXME("(%p, %p): stub\n", This
, ppAudioPathConfig
);
509 static HRESULT WINAPI
segment_Compose(IDirectMusicSegment8
*iface
, MUSIC_TIME mtTime
,
510 IDirectMusicSegment
*pFromSegment
, IDirectMusicSegment
*pToSegment
, IDirectMusicSegment
**ppComposedSegment
)
512 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
513 FIXME("(%p, %ld, %p, %p, %p): stub\n", This
, mtTime
, pFromSegment
, pToSegment
, ppComposedSegment
);
517 static HRESULT WINAPI
segment_Download(IDirectMusicSegment8
*iface
, IUnknown
*pAudioPath
)
519 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
520 FIXME("(%p, %p): stub\n", This
, pAudioPath
);
524 static HRESULT WINAPI
segment_Unload(IDirectMusicSegment8
*iface
, IUnknown
*pAudioPath
)
526 struct segment
*This
= impl_from_IDirectMusicSegment8(iface
);
527 FIXME("(%p, %p): stub\n", This
, pAudioPath
);
531 static const IDirectMusicSegment8Vtbl segment_vtbl
=
533 segment_QueryInterface
,
540 segment_GetDefaultResolution
,
541 segment_SetDefaultResolution
,
543 segment_GetTrackGroup
,
549 segment_AddNotificationType
,
550 segment_RemoveNotificationType
,
554 segment_SetStartPoint
,
555 segment_GetStartPoint
,
556 segment_SetLoopPoints
,
557 segment_GetLoopPoints
,
558 segment_SetPChannelsUsed
,
559 segment_SetTrackConfig
,
560 segment_GetAudioPathConfig
,
566 static HRESULT WINAPI
segment_object_ParseDescriptor(IDirectMusicObject
*iface
, IStream
*stream
, DMUS_OBJECTDESC
*desc
)
568 struct chunk_entry riff
= {0};
569 DWORD supported
= DMUS_OBJ_OBJECT
| DMUS_OBJ_VERSION
;
572 TRACE("(%p, %p, %p)\n", iface
, stream
, desc
);
574 if (!stream
|| !desc
)
577 if ((hr
= stream_get_chunk(stream
, &riff
)) != S_OK
)
579 if (riff
.id
!= FOURCC_RIFF
|| !(riff
.type
== DMUS_FOURCC_SEGMENT_FORM
||
580 riff
.type
== mmioFOURCC('W','A','V','E'))) {
581 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff
));
582 stream_skip_chunk(stream
, &riff
);
586 if (riff
.type
== DMUS_FOURCC_SEGMENT_FORM
)
587 supported
|= DMUS_OBJ_NAME
| DMUS_OBJ_CATEGORY
;
589 supported
|= DMUS_OBJ_NAME_INFO
;
590 hr
= dmobj_parsedescriptor(stream
, &riff
, desc
, supported
);
594 desc
->guidClass
= CLSID_DirectMusicSegment
;
595 desc
->dwValidData
|= DMUS_OBJ_CLASS
;
597 dump_DMUS_OBJECTDESC(desc
);
601 static const IDirectMusicObjectVtbl segment_object_vtbl
=
603 dmobj_IDirectMusicObject_QueryInterface
,
604 dmobj_IDirectMusicObject_AddRef
,
605 dmobj_IDirectMusicObject_Release
,
606 dmobj_IDirectMusicObject_GetDescriptor
,
607 dmobj_IDirectMusicObject_SetDescriptor
,
608 segment_object_ParseDescriptor
,
611 static HRESULT
parse_track_form(struct segment
*This
, IStream
*stream
, const struct chunk_entry
*riff
)
613 struct chunk_entry chunk
= {.parent
= riff
};
614 IDirectMusicTrack
*track
= NULL
;
615 IPersistStream
*ps
= NULL
;
617 DMUS_IO_TRACK_HEADER thdr
;
618 DMUS_IO_TRACK_EXTRAS_HEADER txhdr
= {0};
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
)))
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
))))
630 TRACE("Found DMUS_IO_TRACK_HEADER\n");
631 TRACE("\tclass: %s\n", debugstr_guid (&thdr
.guidClassID
));
632 TRACE("\tdwGroup: %#lx\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
))
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: %#lx, dwPriority: %lu\n", txhdr
.dwFlags
, txhdr
.dwPriority
);
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
)))
661 stream_reset_chunk_start(clone
, &chunk
);
664 hr
= CoCreateInstance(&thdr
.guidClassID
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IDirectMusicTrack
,
668 hr
= IDirectMusicTrack_QueryInterface(track
, &IID_IPersistStream
, (void **)&ps
);
671 hr
= IPersistStream_Load(ps
, clone
);
675 hr
= segment_append_track(This
, track
, thdr
.dwGroup
, txhdr
.dwFlags
);
679 IPersistStream_Release(ps
);
681 IDirectMusicTrack_Release(track
);
682 IStream_Release(clone
);
687 static HRESULT
parse_track_list(struct segment
*This
, IStream
*stream
, const struct chunk_entry
*trkl
)
689 struct chunk_entry chunk
= {.parent
= trkl
};
692 TRACE("Parsing track list in %p: %s\n", stream
, debugstr_chunk(trkl
));
694 while ((hr
= stream_next_chunk(stream
, &chunk
)) == S_OK
)
695 if (chunk
.id
== FOURCC_RIFF
&& chunk
.type
== DMUS_FOURCC_TRACK_FORM
)
696 hr
= parse_track_form(This
, stream
, &chunk
);
698 return SUCCEEDED(hr
) ? S_OK
: hr
;
701 static inline void dump_segment_header(DMUS_IO_SEGMENT_HEADER
*h
, DWORD size
)
705 if (size
== offsetof(DMUS_IO_SEGMENT_HEADER
, rtLength
))
707 else if (size
== offsetof(DMUS_IO_SEGMENT_HEADER
, rtLoopStart
))
709 TRACE("Found DirectX%d DMUS_IO_SEGMENT_HEADER\n", dx
);
710 TRACE("\tdwRepeats: %lu\n", h
->dwRepeats
);
711 TRACE("\tmtLength: %lu\n", h
->mtLength
);
712 TRACE("\tmtPlayStart: %lu\n", h
->mtPlayStart
);
713 TRACE("\tmtLoopStart: %lu\n", h
->mtLoopStart
);
714 TRACE("\tmtLoopEnd: %lu\n", h
->mtLoopEnd
);
715 TRACE("\tdwResolution: %lu\n", h
->dwResolution
);
717 TRACE("\trtLength: %s\n", wine_dbgstr_longlong(h
->rtLength
));
718 TRACE("\tdwFlags: %lu\n", h
->dwFlags
);
719 TRACE("\tdwReserved: %lu\n", h
->dwReserved
);
722 TRACE("\trtLoopStart: %s\n", wine_dbgstr_longlong(h
->rtLoopStart
));
723 TRACE("\trtLoopEnd: %s\n", wine_dbgstr_longlong(h
->rtLoopEnd
));
724 if (size
> offsetof(DMUS_IO_SEGMENT_HEADER
, rtPlayStart
))
725 TRACE("\trtPlayStart: %s\n", wine_dbgstr_longlong(h
->rtPlayStart
));
729 static HRESULT
parse_segment_form(struct segment
*This
, IStream
*stream
, const struct chunk_entry
*riff
)
731 struct chunk_entry chunk
= {.parent
= riff
};
734 TRACE("Parsing segment form in %p: %s\n", stream
, debugstr_chunk(riff
));
736 while ((hr
= stream_next_chunk(stream
, &chunk
)) == S_OK
) {
738 case DMUS_FOURCC_SEGMENT_CHUNK
:
739 /* DX9 without rtPlayStart field */
740 if (chunk
.size
== offsetof(DMUS_IO_SEGMENT_HEADER
, rtPlayStart
))
741 WARN("Missing rtPlayStart field in %s\n", debugstr_chunk(&chunk
));
742 /* DX7, DX8 and DX9 structure sizes */
743 else if (chunk
.size
!= offsetof(DMUS_IO_SEGMENT_HEADER
, rtLength
) &&
744 chunk
.size
!= offsetof(DMUS_IO_SEGMENT_HEADER
, rtLoopStart
) &&
745 chunk
.size
!= sizeof(DMUS_IO_SEGMENT_HEADER
)) {
746 WARN("Invalid size of %s\n", debugstr_chunk(&chunk
));
749 if (FAILED(hr
= stream_chunk_get_data(stream
, &chunk
, &This
->header
, chunk
.size
))) {
750 WARN("Failed to read data of %s\n", debugstr_chunk(&chunk
));
753 dump_segment_header(&This
->header
, chunk
.size
);
756 if (chunk
.type
== DMUS_FOURCC_TRACK_LIST
)
757 if (FAILED(hr
= parse_track_list(This
, stream
, &chunk
)))
761 FIXME("Loading of embedded RIFF form %s\n", debugstr_fourcc(chunk
.type
));
766 return SUCCEEDED(hr
) ? S_OK
: hr
;
769 static inline struct segment
*impl_from_IPersistStream(IPersistStream
*iface
)
771 return CONTAINING_RECORD(iface
, struct segment
, dmobj
.IPersistStream_iface
);
774 static HRESULT
parse_wave_form(struct segment
*This
, IStream
*stream
, const struct chunk_entry
*riff
)
777 struct chunk_entry chunk
= {.parent
= riff
};
779 TRACE("Parsing segment wave in %p: %s\n", stream
, debugstr_chunk(riff
));
781 while ((hr
= stream_next_chunk(stream
, &chunk
)) == S_OK
) {
783 case mmioFOURCC('f','m','t',' '): {
784 if (FAILED(hr
= stream_chunk_get_data(stream
, &chunk
, &This
->wave_format
,
785 sizeof(This
->wave_format
))) )
787 TRACE("Wave Format tag %d\n", This
->wave_format
.wf
.wFormatTag
);
790 case mmioFOURCC('d','a','t','a'): {
791 TRACE("Wave Data size %lu\n", chunk
.size
);
793 ERR("Multiple data streams detected\n");
794 This
->wave_data
= malloc(chunk
.size
);
795 This
->data_size
= chunk
.size
;
796 if (!This
->wave_data
)
797 return E_OUTOFMEMORY
;
798 if (FAILED(hr
= stream_chunk_get_data(stream
, &chunk
, This
->wave_data
, chunk
.size
)))
803 FIXME("Skipping LIST tag\n");
806 case mmioFOURCC('I','S','F','T'): {
807 FIXME("Skipping ISFT tag\n");
810 case mmioFOURCC('f','a','c','t'): {
811 FIXME("Skipping fact tag\n");
817 return SUCCEEDED(hr
) ? S_OK
: hr
;
820 static HRESULT WINAPI
segment_persist_stream_Load(IPersistStream
*iface
, IStream
*stream
)
822 struct segment
*This
= impl_from_IPersistStream(iface
);
823 struct chunk_entry riff
= {0};
826 TRACE("(%p, %p): Loading\n", This
, stream
);
831 if (stream_get_chunk(stream
, &riff
) != S_OK
||
832 (riff
.id
!= FOURCC_RIFF
&& riff
.id
!= mmioFOURCC('M','T','h','d')))
833 return DMUS_E_UNSUPPORTED_STREAM
;
834 stream_reset_chunk_start(stream
, &riff
);
836 if (riff
.id
== mmioFOURCC('M','T','h','d')) {
837 FIXME("MIDI file loading not supported\n");
841 hr
= IDirectMusicObject_ParseDescriptor(&This
->dmobj
.IDirectMusicObject_iface
, stream
,
845 stream_reset_chunk_data(stream
, &riff
);
847 if (riff
.type
== DMUS_FOURCC_SEGMENT_FORM
)
848 hr
= parse_segment_form(This
, stream
, &riff
);
849 else if(riff
.type
== mmioFOURCC('W','A','V','E'))
850 hr
= parse_wave_form(This
, stream
, &riff
);
852 FIXME("Unknown type %s\n", debugstr_chunk(&riff
));
859 static const IPersistStreamVtbl segment_persist_stream_vtbl
=
861 dmobj_IPersistStream_QueryInterface
,
862 dmobj_IPersistStream_AddRef
,
863 dmobj_IPersistStream_Release
,
864 dmobj_IPersistStream_GetClassID
,
865 unimpl_IPersistStream_IsDirty
,
866 segment_persist_stream_Load
,
867 unimpl_IPersistStream_Save
,
868 unimpl_IPersistStream_GetSizeMax
,
871 static struct segment
*segment_create(void)
875 if (!(obj
= calloc(1, sizeof(*obj
)))) return NULL
;
876 obj
->IDirectMusicSegment8_iface
.lpVtbl
= &segment_vtbl
;
878 dmobject_init(&obj
->dmobj
, &CLSID_DirectMusicSegment
, (IUnknown
*)&obj
->IDirectMusicSegment8_iface
);
879 obj
->dmobj
.IDirectMusicObject_iface
.lpVtbl
= &segment_object_vtbl
;
880 obj
->dmobj
.IPersistStream_iface
.lpVtbl
= &segment_persist_stream_vtbl
;
881 list_init(&obj
->tracks
);
886 /* for ClassFactory */
887 HRESULT
create_dmsegment(REFIID guid
, void **ret_iface
)
892 if (!(obj
= segment_create()))
895 return E_OUTOFMEMORY
;
898 hr
= IDirectMusicSegment8_QueryInterface(&obj
->IDirectMusicSegment8_iface
, guid
, ret_iface
);
899 IDirectMusicSegment8_Release(&obj
->IDirectMusicSegment8_iface
);