include: Add ID3D12VideoDecoderHeap in d3d12video.idl.
[wine.git] / dlls / dmime / segment.c
bloba72ec786623feec0e35034c658229053578aa20d
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 struct track_entry
28 struct list entry;
29 DWORD dwGroupBits;
30 DWORD flags;
31 IDirectMusicTrack *pTrack;
34 static void track_entry_destroy(struct track_entry *entry)
36 HRESULT hr;
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);
42 free(entry);
45 struct segment
47 IDirectMusicSegment8 IDirectMusicSegment8_iface;
48 struct dmobject dmobj;
49 LONG ref;
50 DMUS_IO_SEGMENT_HEADER header;
51 IDirectMusicGraph *pGraph;
52 struct list tracks;
54 PCMWAVEFORMAT wave_format;
55 void *wave_data;
56 int data_size;
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);
72 *ret_iface = NULL;
74 if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicSegment) ||
75 IsEqualIID(riid, &IID_IDirectMusicSegment2) ||
76 IsEqualIID (riid, &IID_IDirectMusicSegment8))
77 *ret_iface = iface;
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;
82 else {
83 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
84 return E_NOINTERFACE;
87 IUnknown_AddRef((IUnknown*)*ret_iface);
88 return S_OK;
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);
98 return 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);
108 if (!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);
117 if (This->wave_data)
118 free(This->wave_data);
120 free(This);
123 return ref;
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;
135 return S_OK;
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;
146 return S_OK;
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;
158 return S_OK;
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;
168 return S_OK;
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;
180 return S_OK;
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;
190 return S_OK;
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;
198 HRESULT hr = S_OK;
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;
223 if (!index--)
225 *ret_track = entry->pTrack;
226 IDirectMusicTrack_AddRef(entry->pTrack);
227 return S_OK;
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;
248 return S_OK;
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;
258 HRESULT hr;
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);
270 return hr;
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);
301 return S_OK;
305 return S_FALSE;
308 static HRESULT WINAPI segment_InitPlay(IDirectMusicSegment8 *iface,
309 IDirectMusicSegmentState **state, IDirectMusicPerformance *performance, DWORD flags)
311 struct segment *This = impl_from_IDirectMusicSegment8(iface);
312 HRESULT hr;
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 */
320 return S_OK;
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);
333 return S_OK;
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);
345 return S_OK;
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));
352 return S_OK;
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));
359 return S_OK;
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,
371 next, param);
373 if (!type)
374 return E_POINTER;
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)))
380 continue;
381 if (index == count || index == DMUS_SEG_ANYTRACK)
382 hr = IDirectMusicTrack_GetParam(track, type, time, next, param);
383 IDirectMusicTrack_Release(track);
385 if (SUCCEEDED(hr))
386 return hr;
387 count++;
390 TRACE("(%p): not found\n", This);
392 return hr;
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);
401 return S_OK;
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;
411 HRESULT hr = S_OK;
413 TRACE("(%p, %ld, %ld, %p)\n", This, start, end, segment);
415 if (!segment) return E_POINTER;
417 if (!(clone = segment_create()))
419 *segment = NULL;
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;
445 return S_OK;
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;
456 return S_OK;
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;
470 return S_OK;
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;
483 return S_OK;
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);
490 return S_OK;
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);
499 return S_OK;
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);
506 return S_OK;
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);
514 return S_OK;
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);
521 return S_OK;
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);
528 return S_OK;
531 static const IDirectMusicSegment8Vtbl segment_vtbl =
533 segment_QueryInterface,
534 segment_AddRef,
535 segment_Release,
536 segment_GetLength,
537 segment_SetLength,
538 segment_GetRepeats,
539 segment_SetRepeats,
540 segment_GetDefaultResolution,
541 segment_SetDefaultResolution,
542 segment_GetTrack,
543 segment_GetTrackGroup,
544 segment_InsertTrack,
545 segment_RemoveTrack,
546 segment_InitPlay,
547 segment_GetGraph,
548 segment_SetGraph,
549 segment_AddNotificationType,
550 segment_RemoveNotificationType,
551 segment_GetParam,
552 segment_SetParam,
553 segment_Clone,
554 segment_SetStartPoint,
555 segment_GetStartPoint,
556 segment_SetLoopPoints,
557 segment_GetLoopPoints,
558 segment_SetPChannelsUsed,
559 segment_SetTrackConfig,
560 segment_GetAudioPathConfig,
561 segment_Compose,
562 segment_Download,
563 segment_Unload,
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;
570 HRESULT hr;
572 TRACE("(%p, %p, %p)\n", iface, stream, desc);
574 if (!stream || !desc)
575 return E_POINTER;
577 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
578 return hr;
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);
583 return E_FAIL;
586 if (riff.type == DMUS_FOURCC_SEGMENT_FORM)
587 supported |= DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY;
588 else
589 supported |= DMUS_OBJ_NAME_INFO;
590 hr = dmobj_parsedescriptor(stream, &riff, desc, supported);
591 if (FAILED(hr))
592 return hr;
594 desc->guidClass = CLSID_DirectMusicSegment;
595 desc->dwValidData |= DMUS_OBJ_CLASS;
597 dump_DMUS_OBJECTDESC(desc);
598 return S_OK;
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;
616 IStream *clone;
617 DMUS_IO_TRACK_HEADER thdr;
618 DMUS_IO_TRACK_EXTRAS_HEADER txhdr = {0};
619 HRESULT hr;
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: %#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))
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: %#lx, dwPriority: %lu\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 = segment_append_track(This, track, thdr.dwGroup, txhdr.dwFlags);
677 done:
678 if (ps)
679 IPersistStream_Release(ps);
680 if (track)
681 IDirectMusicTrack_Release(track);
682 IStream_Release(clone);
684 return hr;
687 static HRESULT parse_track_list(struct segment *This, IStream *stream, const struct chunk_entry *trkl)
689 struct chunk_entry chunk = {.parent = trkl};
690 HRESULT hr;
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)
703 unsigned int dx = 9;
705 if (size == offsetof(DMUS_IO_SEGMENT_HEADER, rtLength))
706 dx = 7;
707 else if (size == offsetof(DMUS_IO_SEGMENT_HEADER, rtLoopStart))
708 dx = 8;
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);
716 if (dx >= 8) {
717 TRACE("\trtLength: %s\n", wine_dbgstr_longlong(h->rtLength));
718 TRACE("\tdwFlags: %lu\n", h->dwFlags);
719 TRACE("\tdwReserved: %lu\n", h->dwReserved);
721 if (dx == 9) {
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};
732 HRESULT hr;
734 TRACE("Parsing segment form in %p: %s\n", stream, debugstr_chunk(riff));
736 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
737 switch (chunk.id) {
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));
747 break;
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));
751 return hr;
753 dump_segment_header(&This->header, chunk.size);
754 break;
755 case FOURCC_LIST:
756 if (chunk.type == DMUS_FOURCC_TRACK_LIST)
757 if (FAILED(hr = parse_track_list(This, stream, &chunk)))
758 return hr;
759 break;
760 case FOURCC_RIFF:
761 FIXME("Loading of embedded RIFF form %s\n", debugstr_fourcc(chunk.type));
762 break;
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)
776 HRESULT hr;
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) {
782 switch (chunk.id) {
783 case mmioFOURCC('f','m','t',' '): {
784 if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &This->wave_format,
785 sizeof(This->wave_format))) )
786 return hr;
787 TRACE("Wave Format tag %d\n", This->wave_format.wf.wFormatTag);
788 break;
790 case mmioFOURCC('d','a','t','a'): {
791 TRACE("Wave Data size %lu\n", chunk.size);
792 if (This->wave_data)
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)))
799 return hr;
800 break;
802 case FOURCC_LIST: {
803 FIXME("Skipping LIST tag\n");
804 break;
806 case mmioFOURCC('I','S','F','T'): {
807 FIXME("Skipping ISFT tag\n");
808 break;
810 case mmioFOURCC('f','a','c','t'): {
811 FIXME("Skipping fact tag\n");
812 break;
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};
824 HRESULT hr;
826 TRACE("(%p, %p): Loading\n", This, stream);
828 if (!stream)
829 return E_POINTER;
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");
838 return S_OK;
841 hr = IDirectMusicObject_ParseDescriptor(&This->dmobj.IDirectMusicObject_iface, stream,
842 &This->dmobj.desc);
843 if (FAILED(hr))
844 return hr;
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);
851 else {
852 FIXME("Unknown type %s\n", debugstr_chunk(&riff));
853 hr = S_OK;
856 return hr;
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)
873 struct segment *obj;
875 if (!(obj = calloc(1, sizeof(*obj)))) return NULL;
876 obj->IDirectMusicSegment8_iface.lpVtbl = &segment_vtbl;
877 obj->ref = 1;
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);
883 return obj;
886 /* for ClassFactory */
887 HRESULT create_dmsegment(REFIID guid, void **ret_iface)
889 struct segment *obj;
890 HRESULT hr;
892 if (!(obj = segment_create()))
894 *ret_iface = NULL;
895 return E_OUTOFMEMORY;
898 hr = IDirectMusicSegment8_QueryInterface(&obj->IDirectMusicSegment8_iface, guid, ret_iface);
899 IDirectMusicSegment8_Release(&obj->IDirectMusicSegment8_iface);
901 return hr;