comctl32/tests: Use CRT allocation functions.
[wine.git] / dlls / dmime / segment.c
blobe806a845d37dbb6608c1245fa44a09603f346a04
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"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmime);
25 struct track_entry
27 struct list entry;
28 DWORD dwGroupBits;
29 DWORD flags;
30 IDirectMusicTrack *pTrack;
33 static void track_entry_destroy(struct track_entry *entry)
35 HRESULT hr;
37 if (FAILED(hr = IDirectMusicTrack_Init(entry->pTrack, NULL)))
38 WARN("Failed to de-init track %p, hr %#lx\n", entry->pTrack, hr);
39 IDirectMusicTrack_Release(entry->pTrack);
41 free(entry);
44 struct segment
46 IDirectMusicSegment8 IDirectMusicSegment8_iface;
47 struct dmobject dmobj;
48 LONG ref;
49 DMUS_IO_SEGMENT_HEADER header;
50 IDirectMusicGraph *pGraph;
51 struct list tracks;
53 PCMWAVEFORMAT wave_format;
54 void *wave_data;
55 int data_size;
58 static struct segment *segment_create(void);
60 static inline struct segment *impl_from_IDirectMusicSegment8(IDirectMusicSegment8 *iface)
62 return CONTAINING_RECORD(iface, struct segment, IDirectMusicSegment8_iface);
65 static HRESULT WINAPI segment_QueryInterface(IDirectMusicSegment8 *iface, REFIID riid, void **ret_iface)
67 struct segment *This = impl_from_IDirectMusicSegment8(iface);
69 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
71 *ret_iface = NULL;
73 if (IsEqualIID (riid, &IID_IUnknown) || IsEqualIID (riid, &IID_IDirectMusicSegment) ||
74 IsEqualIID(riid, &IID_IDirectMusicSegment2) ||
75 IsEqualIID (riid, &IID_IDirectMusicSegment8))
76 *ret_iface = iface;
77 else if (IsEqualIID (riid, &IID_IDirectMusicObject))
78 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
79 else if (IsEqualIID (riid, &IID_IPersistStream))
80 *ret_iface = &This->dmobj.IPersistStream_iface;
81 else {
82 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
83 return E_NOINTERFACE;
86 IUnknown_AddRef((IUnknown*)*ret_iface);
87 return S_OK;
90 static ULONG WINAPI segment_AddRef(IDirectMusicSegment8 *iface)
92 struct segment *This = impl_from_IDirectMusicSegment8(iface);
93 LONG ref = InterlockedIncrement(&This->ref);
95 TRACE("(%p) ref=%ld\n", This, ref);
97 return ref;
100 static ULONG WINAPI segment_Release(IDirectMusicSegment8 *iface)
102 struct segment *This = impl_from_IDirectMusicSegment8(iface);
103 LONG ref = InterlockedDecrement(&This->ref);
105 TRACE("(%p) ref=%ld\n", This, ref);
107 if (!ref) {
108 struct track_entry *entry, *next;
110 LIST_FOR_EACH_ENTRY_SAFE(entry, next, &This->tracks, struct track_entry, entry)
112 list_remove(&entry->entry);
113 track_entry_destroy(entry);
115 free(This->wave_data);
116 free(This);
119 return ref;
122 static HRESULT WINAPI segment_GetLength(IDirectMusicSegment8 *iface, MUSIC_TIME *length)
124 struct segment *This = impl_from_IDirectMusicSegment8(iface);
126 TRACE("(%p, %p)\n", This, length);
128 if (!length) return E_POINTER;
129 *length = This->header.mtLength;
131 return S_OK;
134 static HRESULT WINAPI segment_SetLength(IDirectMusicSegment8 *iface, MUSIC_TIME length)
136 struct segment *This = impl_from_IDirectMusicSegment8(iface);
138 TRACE("(%p, %ld)\n", This, length);
140 This->header.mtLength = length;
142 return S_OK;
145 static HRESULT WINAPI segment_GetRepeats(IDirectMusicSegment8 *iface, DWORD *repeats)
147 struct segment *This = impl_from_IDirectMusicSegment8(iface);
149 TRACE("(%p, %p)\n", This, repeats);
151 if (!repeats) return E_POINTER;
152 *repeats = This->header.dwRepeats;
154 return S_OK;
157 static HRESULT WINAPI segment_SetRepeats(IDirectMusicSegment8 *iface, DWORD repeats)
159 struct segment *This = impl_from_IDirectMusicSegment8(iface);
161 TRACE("(%p, %ld)\n", This, repeats);
162 This->header.dwRepeats = repeats;
164 return S_OK;
167 static HRESULT WINAPI segment_GetDefaultResolution(IDirectMusicSegment8 *iface, DWORD *resolution)
169 struct segment *This = impl_from_IDirectMusicSegment8(iface);
171 TRACE("(%p, %p)\n", This, resolution);
173 if (!resolution) return E_POINTER;
174 *resolution = This->header.dwResolution;
176 return S_OK;
179 static HRESULT WINAPI segment_SetDefaultResolution(IDirectMusicSegment8 *iface, DWORD resolution)
181 struct segment *This = impl_from_IDirectMusicSegment8(iface);
183 TRACE("(%p, %ld)\n", This, resolution);
184 This->header.dwResolution = resolution;
186 return S_OK;
189 static HRESULT WINAPI segment_GetTrack(IDirectMusicSegment8 *iface, REFGUID type, DWORD group,
190 DWORD index, IDirectMusicTrack **ret_track)
192 struct segment *This = impl_from_IDirectMusicSegment8(iface);
193 struct track_entry *entry;
194 HRESULT hr = S_OK;
196 TRACE("(%p, %s, %#lx, %#lx, %p)\n", This, debugstr_dmguid(type), group, index, ret_track);
198 if (!ret_track) return E_POINTER;
200 LIST_FOR_EACH_ENTRY(entry, &This->tracks, struct track_entry, entry)
202 if (group != -1 && !(entry->dwGroupBits & group)) continue;
204 if (!IsEqualGUID(&GUID_NULL, type))
206 CLSID entry_type = GUID_NULL;
207 IPersistStream *persist;
209 if (SUCCEEDED(hr = IDirectMusicTrack_QueryInterface(entry->pTrack, &IID_IPersistStream, (void **)&persist)))
211 hr = IPersistStream_GetClassID(persist, &entry_type);
212 if (SUCCEEDED(hr)) TRACE(" - %p -> %s\n", entry, debugstr_dmguid(&entry_type));
213 IPersistStream_Release(persist);
216 if (!IsEqualGUID(&entry_type, type)) continue;
219 if (!index--)
221 *ret_track = entry->pTrack;
222 IDirectMusicTrack_AddRef(entry->pTrack);
223 return S_OK;
227 return DMUS_E_NOT_FOUND;
230 static HRESULT WINAPI segment_GetTrackGroup(IDirectMusicSegment8 *iface, IDirectMusicTrack *track, DWORD *ret_group)
232 struct segment *This = impl_from_IDirectMusicSegment8(iface);
233 struct track_entry *entry;
235 TRACE("(%p, %p, %p)\n", This, track, ret_group);
237 if (!ret_group) return E_POINTER;
239 LIST_FOR_EACH_ENTRY(entry, &This->tracks, struct track_entry, entry)
241 if (entry->pTrack == track)
243 *ret_group = entry->dwGroupBits;
244 return S_OK;
248 return DMUS_E_NOT_FOUND;
251 static HRESULT segment_append_track(struct segment *This, IDirectMusicTrack *track, DWORD group, DWORD flags)
253 struct track_entry *entry;
254 HRESULT hr;
256 if (!(entry = calloc(1, sizeof(*entry)))) return E_OUTOFMEMORY;
257 entry->dwGroupBits = group;
258 entry->flags = flags;
259 entry->pTrack = track;
260 IDirectMusicTrack_AddRef(track);
262 hr = IDirectMusicTrack_Init(track, (IDirectMusicSegment *)&This->IDirectMusicSegment8_iface);
263 if (FAILED(hr)) track_entry_destroy(entry);
264 else list_add_tail(&This->tracks, &entry->entry);
266 return hr;
269 static HRESULT WINAPI segment_InsertTrack(IDirectMusicSegment8 *iface, IDirectMusicTrack *track, DWORD group)
271 struct segment *This = impl_from_IDirectMusicSegment8(iface);
272 struct track_entry *entry;
274 TRACE("(%p, %p, %#lx)\n", This, track, group);
276 if (!group) return E_INVALIDARG;
278 LIST_FOR_EACH_ENTRY(entry, &This->tracks, struct track_entry, entry)
279 if (entry->pTrack == track) return E_FAIL;
281 return segment_append_track(This, track, group, 0);
284 static HRESULT WINAPI segment_RemoveTrack(IDirectMusicSegment8 *iface, IDirectMusicTrack *track)
286 struct segment *This = impl_from_IDirectMusicSegment8(iface);
287 struct track_entry *entry;
289 TRACE("(%p, %p)\n", This, track);
291 LIST_FOR_EACH_ENTRY(entry, &This->tracks, struct track_entry, entry)
293 if (entry->pTrack == track)
295 list_remove(&entry->entry);
296 track_entry_destroy(entry);
297 return S_OK;
301 return S_FALSE;
304 static HRESULT WINAPI segment_InitPlay(IDirectMusicSegment8 *iface,
305 IDirectMusicSegmentState **state, IDirectMusicPerformance *performance, DWORD flags)
307 struct segment *This = impl_from_IDirectMusicSegment8(iface);
308 HRESULT hr;
310 FIXME("(%p, %p, %p, %ld): semi-stub\n", This, state, performance, flags);
312 if (!state) return E_POINTER;
313 if (FAILED(hr = create_dmsegmentstate(&IID_IDirectMusicSegmentState, (void **)state))) return hr;
315 /* TODO: DMUS_SEGF_FLAGS */
316 return S_OK;
319 static HRESULT WINAPI segment_GetGraph(IDirectMusicSegment8 *iface, IDirectMusicGraph **graph)
321 struct segment *This = impl_from_IDirectMusicSegment8(iface);
323 FIXME("(%p, %p): semi-stub\n", This, graph);
325 if (!graph) return E_POINTER;
326 if (!(*graph = This->pGraph)) return DMUS_E_NOT_FOUND;
327 IDirectMusicGraph_AddRef(This->pGraph);
329 return S_OK;
332 static HRESULT WINAPI segment_SetGraph(IDirectMusicSegment8 *iface, IDirectMusicGraph *graph)
334 struct segment *This = impl_from_IDirectMusicSegment8(iface);
336 FIXME("(%p, %p): to complete\n", This, graph);
338 if (This->pGraph) IDirectMusicGraph_Release(This->pGraph);
339 if ((This->pGraph = graph)) IDirectMusicGraph_AddRef(This->pGraph);
341 return S_OK;
344 static HRESULT WINAPI segment_AddNotificationType(IDirectMusicSegment8 *iface, REFGUID rguidNotificationType)
346 struct segment *This = impl_from_IDirectMusicSegment8(iface);
347 FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
348 return S_OK;
351 static HRESULT WINAPI segment_RemoveNotificationType(IDirectMusicSegment8 *iface, REFGUID rguidNotificationType)
353 struct segment *This = impl_from_IDirectMusicSegment8(iface);
354 FIXME("(%p, %s): stub\n", This, debugstr_dmguid(rguidNotificationType));
355 return S_OK;
358 static HRESULT WINAPI segment_GetParam(IDirectMusicSegment8 *iface, REFGUID type, DWORD group,
359 DWORD index, MUSIC_TIME time, MUSIC_TIME *next, void *param)
361 struct segment *This = impl_from_IDirectMusicSegment8(iface);
362 IDirectMusicTrack *track;
363 unsigned int i, count;
364 HRESULT hr = DMUS_E_TRACK_NOT_FOUND;
366 TRACE("(%p, %s, %#lx, %lu, %ld, %p, %p)\n", This, debugstr_dmguid(type), group, index, time,
367 next, param);
369 if (!type)
370 return E_POINTER;
372 /* Index is relative to the search pattern: group bits and supported param type */
373 for (i = 0, count = 0; i < DMUS_SEG_ANYTRACK && count <= index; i++) {
374 if (FAILED(segment_GetTrack(iface, &GUID_NULL, group, i, &track))) break;
375 if (FAILED(IDirectMusicTrack_IsParamSupported(track, type)))
376 continue;
377 if (index == count || index == DMUS_SEG_ANYTRACK)
378 hr = IDirectMusicTrack_GetParam(track, type, time, next, param);
379 IDirectMusicTrack_Release(track);
381 if (SUCCEEDED(hr))
382 return hr;
383 count++;
386 TRACE("(%p): not found\n", This);
388 return hr;
391 static HRESULT WINAPI segment_SetParam(IDirectMusicSegment8 *iface, REFGUID type,
392 DWORD group, DWORD index, MUSIC_TIME music_time, void *param)
394 struct segment *This = impl_from_IDirectMusicSegment8(iface);
395 struct track_entry *entry;
396 HRESULT hr;
398 TRACE("(%p, %s, %#lx, %ld, %ld, %p)\n", This, debugstr_dmguid(type), group,
399 index, music_time, param);
401 LIST_FOR_EACH_ENTRY(entry, &This->tracks, struct track_entry, entry)
403 if (group != -1)
405 if (!(group & entry->dwGroupBits)) continue;
406 if (index != DMUS_SEG_ALLTRACKS && index--) continue;
409 if (SUCCEEDED(hr = IDirectMusicTrack_IsParamSupported(entry->pTrack, type))
410 && FAILED(hr = IDirectMusicTrack_SetParam(entry->pTrack, type, music_time, param)))
411 WARN("SetParam for track %p failed, hr %#lx\n", entry->pTrack, hr);
414 return S_OK;
417 static HRESULT WINAPI segment_Clone(IDirectMusicSegment8 *iface, MUSIC_TIME start, MUSIC_TIME end,
418 IDirectMusicSegment **segment)
420 struct segment *This = impl_from_IDirectMusicSegment8(iface);
421 struct segment *clone;
422 IDirectMusicTrack *track;
423 struct track_entry *entry;
424 HRESULT hr = S_OK;
426 TRACE("(%p, %ld, %ld, %p)\n", This, start, end, segment);
428 if (!segment) return E_POINTER;
430 if (!(clone = segment_create()))
432 *segment = NULL;
433 return E_OUTOFMEMORY;
436 clone->header = This->header;
437 if ((clone->pGraph = This->pGraph)) IDirectMusicGraph_AddRef(clone->pGraph);
439 LIST_FOR_EACH_ENTRY(entry, &This->tracks, struct track_entry, entry)
441 if (FAILED(hr = IDirectMusicTrack_Clone(entry->pTrack, start, end, &track))) break;
442 if (FAILED(hr = segment_append_track(clone, track, entry->dwGroupBits, entry->flags))) break;
445 *segment = (IDirectMusicSegment *)&clone->IDirectMusicSegment8_iface;
446 return FAILED(hr) ? S_FALSE : S_OK;
449 static HRESULT WINAPI segment_SetStartPoint(IDirectMusicSegment8 *iface, MUSIC_TIME start)
451 struct segment *This = impl_from_IDirectMusicSegment8(iface);
453 TRACE("(%p, %ld)\n", This, start);
455 if (start >= This->header.mtLength) return DMUS_E_OUT_OF_RANGE;
456 This->header.mtPlayStart = start;
458 return S_OK;
461 static HRESULT WINAPI segment_GetStartPoint(IDirectMusicSegment8 *iface, MUSIC_TIME *start)
463 struct segment *This = impl_from_IDirectMusicSegment8(iface);
465 TRACE("(%p, %p)\n", This, start);
466 if (!start) return E_POINTER;
467 *start = This->header.mtPlayStart;
469 return S_OK;
472 static HRESULT WINAPI segment_SetLoopPoints(IDirectMusicSegment8 *iface, MUSIC_TIME start, MUSIC_TIME end)
474 struct segment *This = impl_from_IDirectMusicSegment8(iface);
476 TRACE("(%p, %ld, %ld)\n", This, start, end);
478 if ((end || start) && (start >= This->header.mtLength || end > This->header.mtLength || start > end))
479 return DMUS_E_OUT_OF_RANGE;
480 This->header.mtLoopStart = start;
481 This->header.mtLoopEnd = end;
483 return S_OK;
486 static HRESULT WINAPI segment_GetLoopPoints(IDirectMusicSegment8 *iface, MUSIC_TIME *start, MUSIC_TIME *end)
488 struct segment *This = impl_from_IDirectMusicSegment8(iface);
490 TRACE("(%p, %p, %p)\n", This, start, end);
492 if (!start || !end) return E_POINTER;
493 *start = This->header.mtLoopStart;
494 *end = This->header.mtLoopEnd;
496 return S_OK;
499 static HRESULT WINAPI segment_SetPChannelsUsed(IDirectMusicSegment8 *iface, DWORD dwNumPChannels, DWORD *paPChannels)
501 struct segment *This = impl_from_IDirectMusicSegment8(iface);
502 FIXME("(%p, %ld, %p): stub\n", This, dwNumPChannels, paPChannels);
503 return S_OK;
506 static HRESULT WINAPI segment_SetTrackConfig(IDirectMusicSegment8 *iface, REFGUID rguidTrackClassID,
507 DWORD dwGroupBits, DWORD dwIndex, DWORD dwFlagsOn, DWORD dwFlagsOff)
509 struct segment *This = impl_from_IDirectMusicSegment8(iface);
510 FIXME("(%p, %s, %#lx, %ld, %ld, %ld): stub\n", This, debugstr_dmguid(rguidTrackClassID),
511 dwGroupBits, dwIndex, dwFlagsOn, dwFlagsOff);
512 return S_OK;
515 static HRESULT WINAPI segment_GetAudioPathConfig(IDirectMusicSegment8 *iface, IUnknown **ppAudioPathConfig)
517 struct segment *This = impl_from_IDirectMusicSegment8(iface);
518 FIXME("(%p, %p): stub\n", This, ppAudioPathConfig);
519 return S_OK;
522 static HRESULT WINAPI segment_Compose(IDirectMusicSegment8 *iface, MUSIC_TIME mtTime,
523 IDirectMusicSegment *pFromSegment, IDirectMusicSegment *pToSegment, IDirectMusicSegment **ppComposedSegment)
525 struct segment *This = impl_from_IDirectMusicSegment8(iface);
526 FIXME("(%p, %ld, %p, %p, %p): stub\n", This, mtTime, pFromSegment, pToSegment, ppComposedSegment);
527 return S_OK;
530 static HRESULT WINAPI segment_Download(IDirectMusicSegment8 *iface, IUnknown *audio_path)
532 struct segment *This = impl_from_IDirectMusicSegment8(iface);
533 TRACE("(%p, %p)\n", This, audio_path);
534 return IDirectMusicSegment8_SetParam(iface, &GUID_DownloadToAudioPath, -1, DMUS_SEG_ALLTRACKS, 0, audio_path);
537 static HRESULT WINAPI segment_Unload(IDirectMusicSegment8 *iface, IUnknown *audio_path)
539 struct segment *This = impl_from_IDirectMusicSegment8(iface);
540 TRACE("(%p, %p)\n", This, audio_path);
541 return IDirectMusicSegment8_SetParam(iface, &GUID_UnloadFromAudioPath, -1, DMUS_SEG_ALLTRACKS, 0, audio_path);
544 static const IDirectMusicSegment8Vtbl segment_vtbl =
546 segment_QueryInterface,
547 segment_AddRef,
548 segment_Release,
549 segment_GetLength,
550 segment_SetLength,
551 segment_GetRepeats,
552 segment_SetRepeats,
553 segment_GetDefaultResolution,
554 segment_SetDefaultResolution,
555 segment_GetTrack,
556 segment_GetTrackGroup,
557 segment_InsertTrack,
558 segment_RemoveTrack,
559 segment_InitPlay,
560 segment_GetGraph,
561 segment_SetGraph,
562 segment_AddNotificationType,
563 segment_RemoveNotificationType,
564 segment_GetParam,
565 segment_SetParam,
566 segment_Clone,
567 segment_SetStartPoint,
568 segment_GetStartPoint,
569 segment_SetLoopPoints,
570 segment_GetLoopPoints,
571 segment_SetPChannelsUsed,
572 segment_SetTrackConfig,
573 segment_GetAudioPathConfig,
574 segment_Compose,
575 segment_Download,
576 segment_Unload,
579 static HRESULT WINAPI segment_object_ParseDescriptor(IDirectMusicObject *iface, IStream *stream, DMUS_OBJECTDESC *desc)
581 struct chunk_entry riff = {0};
582 DWORD supported = DMUS_OBJ_OBJECT | DMUS_OBJ_VERSION;
583 HRESULT hr;
585 TRACE("(%p, %p, %p)\n", iface, stream, desc);
587 if (!stream || !desc)
588 return E_POINTER;
590 if ((hr = stream_get_chunk(stream, &riff)) != S_OK)
591 return hr;
592 if (riff.id != FOURCC_RIFF || !(riff.type == DMUS_FOURCC_SEGMENT_FORM ||
593 riff.type == mmioFOURCC('W','A','V','E'))) {
594 TRACE("loading failed: unexpected %s\n", debugstr_chunk(&riff));
595 stream_skip_chunk(stream, &riff);
596 return E_FAIL;
599 if (riff.type == DMUS_FOURCC_SEGMENT_FORM)
600 supported |= DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY;
601 else
602 supported |= DMUS_OBJ_NAME_INFO;
603 hr = dmobj_parsedescriptor(stream, &riff, desc, supported);
604 if (FAILED(hr))
605 return hr;
607 desc->guidClass = CLSID_DirectMusicSegment;
608 desc->dwValidData |= DMUS_OBJ_CLASS;
610 dump_DMUS_OBJECTDESC(desc);
611 return S_OK;
614 static const IDirectMusicObjectVtbl segment_object_vtbl =
616 dmobj_IDirectMusicObject_QueryInterface,
617 dmobj_IDirectMusicObject_AddRef,
618 dmobj_IDirectMusicObject_Release,
619 dmobj_IDirectMusicObject_GetDescriptor,
620 dmobj_IDirectMusicObject_SetDescriptor,
621 segment_object_ParseDescriptor,
624 static HRESULT parse_track_form(struct segment *This, IStream *stream, const struct chunk_entry *riff)
626 struct chunk_entry chunk = {.parent = riff};
627 IDirectMusicTrack *track = NULL;
628 IPersistStream *ps = NULL;
629 IStream *clone;
630 DMUS_IO_TRACK_HEADER thdr;
631 DMUS_IO_TRACK_EXTRAS_HEADER txhdr = {0};
632 HRESULT hr;
634 TRACE("Parsing track form in %p: %s\n", stream, debugstr_chunk(riff));
636 /* First chunk must be the track header */
637 if (FAILED(hr = stream_get_chunk(stream, &chunk)))
638 return hr;
639 if (chunk.id != DMUS_FOURCC_TRACK_CHUNK)
640 return DMUS_E_TRACK_HDR_NOT_FIRST_CK;
641 if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &thdr, sizeof(thdr))))
642 return hr;
643 TRACE("Found DMUS_IO_TRACK_HEADER\n");
644 TRACE("\tclass: %s\n", debugstr_guid (&thdr.guidClassID));
645 TRACE("\tdwGroup: %#lx\n", thdr.dwGroup);
646 TRACE("\tckid: %s\n", debugstr_fourcc (thdr.ckid));
647 TRACE("\tfccType: %s\n", debugstr_fourcc (thdr.fccType));
649 if (!!thdr.ckid == !!thdr.fccType) {
650 WARN("One and only one of the ckid (%s) and fccType (%s) need to be set\n",
651 debugstr_fourcc(thdr.ckid), debugstr_fourcc(thdr.fccType));
652 return DMUS_E_INVALID_TRACK_HDR;
655 /* Optional chunks */
656 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
657 if ((thdr.ckid && chunk.id == thdr.ckid) ||
658 (!thdr.ckid && (chunk.id == FOURCC_LIST || chunk.id == FOURCC_RIFF) &&
659 chunk.type == thdr.fccType))
660 break;
662 if (chunk.id == DMUS_FOURCC_TRACK_EXTRAS_CHUNK &&
663 SUCCEEDED(stream_chunk_get_data(stream, &chunk, &txhdr, sizeof(txhdr)))) {
664 FIXME("DMUS_IO_TRACK_EXTRAS_HEADER chunk not fully handled\n");
665 TRACE("dwFlags: %#lx, dwPriority: %lu\n", txhdr.dwFlags, txhdr.dwPriority);
668 if (hr != S_OK)
669 return hr == S_FALSE ? DMUS_E_TRACK_NOT_FOUND : hr;
671 /* Some DirectMusicTrack implementation expect the stream to start with their data chunk */
672 if (FAILED(hr = IStream_Clone(stream, &clone)))
673 return hr;
674 stream_reset_chunk_start(clone, &chunk);
676 /* Load the track */
677 hr = CoCreateInstance(&thdr.guidClassID, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectMusicTrack,
678 (void **)&track);
679 if (FAILED(hr))
680 goto done;
681 hr = IDirectMusicTrack_QueryInterface(track, &IID_IPersistStream, (void **)&ps);
682 if (FAILED(hr))
683 goto done;
684 hr = IPersistStream_Load(ps, clone);
685 if (FAILED(hr))
686 goto done;
688 hr = segment_append_track(This, track, thdr.dwGroup, txhdr.dwFlags);
690 done:
691 if (ps)
692 IPersistStream_Release(ps);
693 if (track)
694 IDirectMusicTrack_Release(track);
695 IStream_Release(clone);
697 return hr;
700 static HRESULT parse_track_list(struct segment *This, IStream *stream, const struct chunk_entry *trkl)
702 struct chunk_entry chunk = {.parent = trkl};
703 HRESULT hr;
705 TRACE("Parsing track list in %p: %s\n", stream, debugstr_chunk(trkl));
707 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK)
708 if (chunk.id == FOURCC_RIFF && chunk.type == DMUS_FOURCC_TRACK_FORM)
709 hr = parse_track_form(This, stream, &chunk);
711 return SUCCEEDED(hr) ? S_OK : hr;
714 static inline void dump_segment_header(DMUS_IO_SEGMENT_HEADER *h, DWORD size)
716 unsigned int dx = 9;
718 if (size == offsetof(DMUS_IO_SEGMENT_HEADER, rtLength))
719 dx = 7;
720 else if (size == offsetof(DMUS_IO_SEGMENT_HEADER, rtLoopStart))
721 dx = 8;
722 TRACE("Found DirectX%d DMUS_IO_SEGMENT_HEADER\n", dx);
723 TRACE("\tdwRepeats: %lu\n", h->dwRepeats);
724 TRACE("\tmtLength: %lu\n", h->mtLength);
725 TRACE("\tmtPlayStart: %lu\n", h->mtPlayStart);
726 TRACE("\tmtLoopStart: %lu\n", h->mtLoopStart);
727 TRACE("\tmtLoopEnd: %lu\n", h->mtLoopEnd);
728 TRACE("\tdwResolution: %lu\n", h->dwResolution);
729 if (dx >= 8) {
730 TRACE("\trtLength: %s\n", wine_dbgstr_longlong(h->rtLength));
731 TRACE("\tdwFlags: %lu\n", h->dwFlags);
732 TRACE("\tdwReserved: %lu\n", h->dwReserved);
734 if (dx == 9) {
735 TRACE("\trtLoopStart: %s\n", wine_dbgstr_longlong(h->rtLoopStart));
736 TRACE("\trtLoopEnd: %s\n", wine_dbgstr_longlong(h->rtLoopEnd));
737 if (size > offsetof(DMUS_IO_SEGMENT_HEADER, rtPlayStart))
738 TRACE("\trtPlayStart: %s\n", wine_dbgstr_longlong(h->rtPlayStart));
742 static HRESULT parse_dmsg_chunk(struct segment *This, IStream *stream, const struct chunk_entry *riff)
744 struct chunk_entry chunk = {.parent = riff};
745 HRESULT hr;
747 TRACE("Parsing segment form in %p: %s\n", stream, debugstr_chunk(riff));
749 if (FAILED(hr = dmobj_parsedescriptor(stream, riff, &This->dmobj.desc, DMUS_OBJ_NAME | DMUS_OBJ_CATEGORY))
750 || FAILED(hr = stream_reset_chunk_data(stream, riff)))
751 return hr;
753 while ((hr = stream_next_chunk(stream, &chunk)) == S_OK) {
754 switch (chunk.id) {
755 case DMUS_FOURCC_SEGMENT_CHUNK:
756 /* DX9 without rtPlayStart field */
757 if (chunk.size == offsetof(DMUS_IO_SEGMENT_HEADER, rtPlayStart))
758 WARN("Missing rtPlayStart field in %s\n", debugstr_chunk(&chunk));
759 /* DX7, DX8 and DX9 structure sizes */
760 else if (chunk.size != offsetof(DMUS_IO_SEGMENT_HEADER, rtLength) &&
761 chunk.size != offsetof(DMUS_IO_SEGMENT_HEADER, rtLoopStart) &&
762 chunk.size != sizeof(DMUS_IO_SEGMENT_HEADER)) {
763 WARN("Invalid size of %s\n", debugstr_chunk(&chunk));
764 break;
766 if (FAILED(hr = stream_chunk_get_data(stream, &chunk, &This->header, chunk.size))) {
767 WARN("Failed to read data of %s\n", debugstr_chunk(&chunk));
768 return hr;
770 dump_segment_header(&This->header, chunk.size);
771 break;
772 case FOURCC_LIST:
773 if (chunk.type == DMUS_FOURCC_TRACK_LIST)
774 if (FAILED(hr = parse_track_list(This, stream, &chunk)))
775 return hr;
776 break;
777 case FOURCC_RIFF:
778 FIXME("Loading of embedded RIFF form %s\n", debugstr_fourcc(chunk.type));
779 break;
783 return SUCCEEDED(hr) ? S_OK : hr;
786 static inline struct segment *impl_from_IPersistStream(IPersistStream *iface)
788 return CONTAINING_RECORD(iface, struct segment, dmobj.IPersistStream_iface);
791 static HRESULT WINAPI segment_persist_stream_Load(IPersistStream *iface, IStream *stream)
793 struct segment *This = impl_from_IPersistStream(iface);
794 struct chunk_entry chunk = {0};
795 HRESULT hr;
797 TRACE("(%p, %p): Loading\n", This, stream);
799 if (!stream) return E_POINTER;
801 if ((hr = stream_get_chunk(stream, &chunk)) == S_OK)
803 switch (MAKE_IDTYPE(chunk.id, chunk.type))
805 case MAKE_IDTYPE(FOURCC_RIFF, DMUS_FOURCC_SEGMENT_FORM):
806 hr = parse_dmsg_chunk(This, stream, &chunk);
807 break;
809 case mmioFOURCC('M','T','h','d'):
810 FIXME("MIDI file loading not supported\n");
811 break;
813 case MAKE_IDTYPE(FOURCC_RIFF, mmioFOURCC('W','A','V','E')):
815 IDirectMusicTrack8 *track;
817 TRACE("Loading segment %p from wave file\n", This);
819 This->header.mtLength = 1;
820 if (FAILED(hr = wave_track_create_from_chunk(stream, &chunk, &track))) break;
821 hr = segment_append_track(This, (IDirectMusicTrack *)track, 1, 0);
822 break;
825 default:
826 WARN("Invalid segment chunk %s %s\n", debugstr_fourcc(chunk.id), debugstr_fourcc(chunk.type));
827 hr = DMUS_E_UNSUPPORTED_STREAM;
828 break;
832 stream_skip_chunk(stream, &chunk);
833 if (FAILED(hr))
835 WARN("Failed to load segment from stream %p, hr %#lx\n", stream, hr);
836 return DMUS_E_UNSUPPORTED_STREAM;
839 This->dmobj.desc.guidClass = CLSID_DirectMusicSegment;
840 This->dmobj.desc.dwValidData |= DMUS_OBJ_CLASS;
842 return S_OK;
845 static const IPersistStreamVtbl segment_persist_stream_vtbl =
847 dmobj_IPersistStream_QueryInterface,
848 dmobj_IPersistStream_AddRef,
849 dmobj_IPersistStream_Release,
850 dmobj_IPersistStream_GetClassID,
851 unimpl_IPersistStream_IsDirty,
852 segment_persist_stream_Load,
853 unimpl_IPersistStream_Save,
854 unimpl_IPersistStream_GetSizeMax,
857 static struct segment *segment_create(void)
859 struct segment *obj;
861 if (!(obj = calloc(1, sizeof(*obj)))) return NULL;
862 obj->IDirectMusicSegment8_iface.lpVtbl = &segment_vtbl;
863 obj->ref = 1;
864 dmobject_init(&obj->dmobj, &CLSID_DirectMusicSegment, (IUnknown *)&obj->IDirectMusicSegment8_iface);
865 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &segment_object_vtbl;
866 obj->dmobj.IPersistStream_iface.lpVtbl = &segment_persist_stream_vtbl;
867 list_init(&obj->tracks);
869 return obj;
872 /* for ClassFactory */
873 HRESULT create_dmsegment(REFIID guid, void **ret_iface)
875 struct segment *obj;
876 HRESULT hr;
878 if (!(obj = segment_create()))
880 *ret_iface = NULL;
881 return E_OUTOFMEMORY;
884 hr = IDirectMusicSegment8_QueryInterface(&obj->IDirectMusicSegment8_iface, guid, ret_iface);
885 IDirectMusicSegment8_Release(&obj->IDirectMusicSegment8_iface);
887 return hr;