winegstreamer: Merge wm_reader_get_stream_sample with GetNextSample.
[wine.git] / dlls / winegstreamer / wm_reader.c
blob8ac852a99b0dafbd919fa0cda3800ff7cfc4c56b
1 /*
2 * Copyright 2012 Austin English
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "gst_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(wmvcore);
23 struct wm_stream
25 struct wm_reader *reader;
26 struct wg_parser_stream *wg_stream;
27 struct wg_format format;
28 WMT_STREAM_SELECTION selection;
29 WORD index;
30 bool eos;
31 /* Note that we only pretend to read compressed samples, and instead output
32 * uncompressed samples regardless of whether we are configured to read
33 * compressed samples. Rather, the behaviour of the reader objects differs
34 * in nontrivial ways depending on this field. */
35 bool read_compressed;
37 IWMReaderAllocatorEx *output_allocator;
38 IWMReaderAllocatorEx *stream_allocator;
41 struct wm_reader
43 IUnknown IUnknown_inner;
44 IWMSyncReader2 IWMSyncReader2_iface;
45 IWMHeaderInfo3 IWMHeaderInfo3_iface;
46 IWMLanguageList IWMLanguageList_iface;
47 IWMPacketSize2 IWMPacketSize2_iface;
48 IWMProfile3 IWMProfile3_iface;
49 IWMReaderPlaylistBurn IWMReaderPlaylistBurn_iface;
50 IWMReaderTimecode IWMReaderTimecode_iface;
51 IUnknown *outer;
52 LONG refcount;
54 CRITICAL_SECTION cs;
55 QWORD start_time;
57 IStream *source_stream;
58 HANDLE file;
59 HANDLE read_thread;
60 bool read_thread_shutdown;
61 struct wg_parser *wg_parser;
63 struct wm_stream *streams;
64 WORD stream_count;
67 static struct wm_stream *get_stream_by_output_number(struct wm_reader *reader, DWORD output)
69 if (output < reader->stream_count)
70 return &reader->streams[output];
71 WARN("Invalid output number %lu.\n", output);
72 return NULL;
75 struct output_props
77 IWMOutputMediaProps IWMOutputMediaProps_iface;
78 LONG refcount;
80 AM_MEDIA_TYPE mt;
83 static inline struct output_props *impl_from_IWMOutputMediaProps(IWMOutputMediaProps *iface)
85 return CONTAINING_RECORD(iface, struct output_props, IWMOutputMediaProps_iface);
88 static HRESULT WINAPI output_props_QueryInterface(IWMOutputMediaProps *iface, REFIID iid, void **out)
90 struct output_props *props = impl_from_IWMOutputMediaProps(iface);
92 TRACE("props %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
94 if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IWMOutputMediaProps))
95 *out = &props->IWMOutputMediaProps_iface;
96 else
98 *out = NULL;
99 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
100 return E_NOINTERFACE;
103 IUnknown_AddRef((IUnknown *)*out);
104 return S_OK;
107 static ULONG WINAPI output_props_AddRef(IWMOutputMediaProps *iface)
109 struct output_props *props = impl_from_IWMOutputMediaProps(iface);
110 ULONG refcount = InterlockedIncrement(&props->refcount);
112 TRACE("%p increasing refcount to %lu.\n", props, refcount);
114 return refcount;
117 static ULONG WINAPI output_props_Release(IWMOutputMediaProps *iface)
119 struct output_props *props = impl_from_IWMOutputMediaProps(iface);
120 ULONG refcount = InterlockedDecrement(&props->refcount);
122 TRACE("%p decreasing refcount to %lu.\n", props, refcount);
124 if (!refcount)
125 free(props);
127 return refcount;
130 static HRESULT WINAPI output_props_GetType(IWMOutputMediaProps *iface, GUID *major_type)
132 const struct output_props *props = impl_from_IWMOutputMediaProps(iface);
134 TRACE("iface %p, major_type %p.\n", iface, major_type);
136 *major_type = props->mt.majortype;
137 return S_OK;
140 static HRESULT WINAPI output_props_GetMediaType(IWMOutputMediaProps *iface, WM_MEDIA_TYPE *mt, DWORD *size)
142 const struct output_props *props = impl_from_IWMOutputMediaProps(iface);
143 const DWORD req_size = *size;
145 TRACE("iface %p, mt %p, size %p.\n", iface, mt, size);
147 *size = sizeof(*mt) + props->mt.cbFormat;
148 if (!mt)
149 return S_OK;
150 if (req_size < *size)
151 return ASF_E_BUFFERTOOSMALL;
153 strmbase_dump_media_type(&props->mt);
155 memcpy(mt, &props->mt, sizeof(*mt));
156 memcpy(mt + 1, props->mt.pbFormat, props->mt.cbFormat);
157 mt->pbFormat = (BYTE *)(mt + 1);
158 return S_OK;
161 static HRESULT WINAPI output_props_SetMediaType(IWMOutputMediaProps *iface, WM_MEDIA_TYPE *mt)
163 const struct output_props *props = impl_from_IWMOutputMediaProps(iface);
165 TRACE("iface %p, mt %p.\n", iface, mt);
167 if (!mt)
168 return E_POINTER;
170 if (!IsEqualGUID(&props->mt.majortype, &mt->majortype))
171 return E_FAIL;
173 FreeMediaType((AM_MEDIA_TYPE *)&props->mt);
174 return CopyMediaType((AM_MEDIA_TYPE *)&props->mt, (AM_MEDIA_TYPE *)mt);
177 static HRESULT WINAPI output_props_GetStreamGroupName(IWMOutputMediaProps *iface, WCHAR *name, WORD *len)
179 FIXME("iface %p, name %p, len %p, stub!\n", iface, name, len);
180 return E_NOTIMPL;
183 static HRESULT WINAPI output_props_GetConnectionName(IWMOutputMediaProps *iface, WCHAR *name, WORD *len)
185 FIXME("iface %p, name %p, len %p, stub!\n", iface, name, len);
186 return E_NOTIMPL;
189 static const struct IWMOutputMediaPropsVtbl output_props_vtbl =
191 output_props_QueryInterface,
192 output_props_AddRef,
193 output_props_Release,
194 output_props_GetType,
195 output_props_GetMediaType,
196 output_props_SetMediaType,
197 output_props_GetStreamGroupName,
198 output_props_GetConnectionName,
201 static struct output_props *unsafe_impl_from_IWMOutputMediaProps(IWMOutputMediaProps *iface)
203 if (!iface)
204 return NULL;
205 assert(iface->lpVtbl == &output_props_vtbl);
206 return impl_from_IWMOutputMediaProps(iface);
209 static IWMOutputMediaProps *output_props_create(const struct wg_format *format)
211 struct output_props *object;
213 if (!(object = calloc(1, sizeof(*object))))
214 return NULL;
215 object->IWMOutputMediaProps_iface.lpVtbl = &output_props_vtbl;
216 object->refcount = 1;
218 if (!amt_from_wg_format(&object->mt, format, true))
220 free(object);
221 return NULL;
224 TRACE("Created output properties %p.\n", object);
225 return &object->IWMOutputMediaProps_iface;
228 struct buffer
230 INSSBuffer INSSBuffer_iface;
231 LONG refcount;
233 DWORD size, capacity;
234 BYTE data[1];
237 static struct buffer *impl_from_INSSBuffer(INSSBuffer *iface)
239 return CONTAINING_RECORD(iface, struct buffer, INSSBuffer_iface);
242 static HRESULT WINAPI buffer_QueryInterface(INSSBuffer *iface, REFIID iid, void **out)
244 struct buffer *buffer = impl_from_INSSBuffer(iface);
246 TRACE("buffer %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out);
248 if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_INSSBuffer))
249 *out = &buffer->INSSBuffer_iface;
250 else
252 *out = NULL;
253 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
254 return E_NOINTERFACE;
257 IUnknown_AddRef((IUnknown *)*out);
258 return S_OK;
261 static ULONG WINAPI buffer_AddRef(INSSBuffer *iface)
263 struct buffer *buffer = impl_from_INSSBuffer(iface);
264 ULONG refcount = InterlockedIncrement(&buffer->refcount);
266 TRACE("%p increasing refcount to %lu.\n", buffer, refcount);
268 return refcount;
271 static ULONG WINAPI buffer_Release(INSSBuffer *iface)
273 struct buffer *buffer = impl_from_INSSBuffer(iface);
274 ULONG refcount = InterlockedDecrement(&buffer->refcount);
276 TRACE("%p decreasing refcount to %lu.\n", buffer, refcount);
278 if (!refcount)
279 free(buffer);
281 return refcount;
284 static HRESULT WINAPI buffer_GetLength(INSSBuffer *iface, DWORD *size)
286 FIXME("iface %p, size %p, stub!\n", iface, size);
287 return E_NOTIMPL;
290 static HRESULT WINAPI buffer_SetLength(INSSBuffer *iface, DWORD size)
292 struct buffer *buffer = impl_from_INSSBuffer(iface);
294 TRACE("iface %p, size %lu.\n", buffer, size);
296 if (size > buffer->capacity)
297 return E_INVALIDARG;
299 buffer->size = size;
300 return S_OK;
303 static HRESULT WINAPI buffer_GetMaxLength(INSSBuffer *iface, DWORD *size)
305 struct buffer *buffer = impl_from_INSSBuffer(iface);
307 TRACE("buffer %p, size %p.\n", buffer, size);
309 *size = buffer->capacity;
310 return S_OK;
313 static HRESULT WINAPI buffer_GetBuffer(INSSBuffer *iface, BYTE **data)
315 struct buffer *buffer = impl_from_INSSBuffer(iface);
317 TRACE("buffer %p, data %p.\n", buffer, data);
319 *data = buffer->data;
320 return S_OK;
323 static HRESULT WINAPI buffer_GetBufferAndLength(INSSBuffer *iface, BYTE **data, DWORD *size)
325 struct buffer *buffer = impl_from_INSSBuffer(iface);
327 TRACE("buffer %p, data %p, size %p.\n", buffer, data, size);
329 *size = buffer->size;
330 *data = buffer->data;
331 return S_OK;
334 static const INSSBufferVtbl buffer_vtbl =
336 buffer_QueryInterface,
337 buffer_AddRef,
338 buffer_Release,
339 buffer_GetLength,
340 buffer_SetLength,
341 buffer_GetMaxLength,
342 buffer_GetBuffer,
343 buffer_GetBufferAndLength,
346 struct stream_config
348 IWMStreamConfig IWMStreamConfig_iface;
349 IWMMediaProps IWMMediaProps_iface;
350 LONG refcount;
352 const struct wm_stream *stream;
355 static struct stream_config *impl_from_IWMStreamConfig(IWMStreamConfig *iface)
357 return CONTAINING_RECORD(iface, struct stream_config, IWMStreamConfig_iface);
360 static HRESULT WINAPI stream_config_QueryInterface(IWMStreamConfig *iface, REFIID iid, void **out)
362 struct stream_config *config = impl_from_IWMStreamConfig(iface);
364 TRACE("config %p, iid %s, out %p.\n", config, debugstr_guid(iid), out);
366 if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IWMStreamConfig))
367 *out = &config->IWMStreamConfig_iface;
368 else if (IsEqualGUID(iid, &IID_IWMMediaProps))
369 *out = &config->IWMMediaProps_iface;
370 else
372 *out = NULL;
373 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
374 return E_NOINTERFACE;
377 IUnknown_AddRef((IUnknown *)*out);
378 return S_OK;
381 static ULONG WINAPI stream_config_AddRef(IWMStreamConfig *iface)
383 struct stream_config *config = impl_from_IWMStreamConfig(iface);
384 ULONG refcount = InterlockedIncrement(&config->refcount);
386 TRACE("%p increasing refcount to %lu.\n", config, refcount);
388 return refcount;
391 static ULONG WINAPI stream_config_Release(IWMStreamConfig *iface)
393 struct stream_config *config = impl_from_IWMStreamConfig(iface);
394 ULONG refcount = InterlockedDecrement(&config->refcount);
396 TRACE("%p decreasing refcount to %lu.\n", config, refcount);
398 if (!refcount)
400 IWMProfile3_Release(&config->stream->reader->IWMProfile3_iface);
401 free(config);
404 return refcount;
407 static HRESULT WINAPI stream_config_GetStreamType(IWMStreamConfig *iface, GUID *type)
409 struct stream_config *config = impl_from_IWMStreamConfig(iface);
410 struct wm_reader *reader = config->stream->reader;
411 AM_MEDIA_TYPE mt;
413 TRACE("config %p, type %p.\n", config, type);
415 EnterCriticalSection(&reader->cs);
417 if (!amt_from_wg_format(&mt, &config->stream->format, true))
419 LeaveCriticalSection(&reader->cs);
420 return E_OUTOFMEMORY;
423 *type = mt.majortype;
424 FreeMediaType(&mt);
426 LeaveCriticalSection(&reader->cs);
428 return S_OK;
431 static HRESULT WINAPI stream_config_GetStreamNumber(IWMStreamConfig *iface, WORD *number)
433 struct stream_config *config = impl_from_IWMStreamConfig(iface);
435 TRACE("config %p, number %p.\n", config, number);
437 *number = config->stream->index + 1;
438 return S_OK;
441 static HRESULT WINAPI stream_config_SetStreamNumber(IWMStreamConfig *iface, WORD number)
443 FIXME("iface %p, number %u, stub!\n", iface, number);
444 return E_NOTIMPL;
447 static HRESULT WINAPI stream_config_GetStreamName(IWMStreamConfig *iface, WCHAR *name, WORD *len)
449 FIXME("iface %p, name %p, len %p, stub!\n", iface, name, len);
450 return E_NOTIMPL;
453 static HRESULT WINAPI stream_config_SetStreamName(IWMStreamConfig *iface, const WCHAR *name)
455 FIXME("iface %p, name %s, stub!\n", iface, debugstr_w(name));
456 return E_NOTIMPL;
459 static HRESULT WINAPI stream_config_GetConnectionName(IWMStreamConfig *iface, WCHAR *name, WORD *len)
461 FIXME("iface %p, name %p, len %p, stub!\n", iface, name, len);
462 return E_NOTIMPL;
465 static HRESULT WINAPI stream_config_SetConnectionName(IWMStreamConfig *iface, const WCHAR *name)
467 FIXME("iface %p, name %s, stub!\n", iface, debugstr_w(name));
468 return E_NOTIMPL;
471 static HRESULT WINAPI stream_config_GetBitrate(IWMStreamConfig *iface, DWORD *bitrate)
473 FIXME("iface %p, bitrate %p, stub!\n", iface, bitrate);
474 return E_NOTIMPL;
477 static HRESULT WINAPI stream_config_SetBitrate(IWMStreamConfig *iface, DWORD bitrate)
479 FIXME("iface %p, bitrate %lu, stub!\n", iface, bitrate);
480 return E_NOTIMPL;
483 static HRESULT WINAPI stream_config_GetBufferWindow(IWMStreamConfig *iface, DWORD *window)
485 FIXME("iface %p, window %p, stub!\n", iface, window);
486 return E_NOTIMPL;
489 static HRESULT WINAPI stream_config_SetBufferWindow(IWMStreamConfig *iface, DWORD window)
491 FIXME("iface %p, window %lu, stub!\n", iface, window);
492 return E_NOTIMPL;
495 static const IWMStreamConfigVtbl stream_config_vtbl =
497 stream_config_QueryInterface,
498 stream_config_AddRef,
499 stream_config_Release,
500 stream_config_GetStreamType,
501 stream_config_GetStreamNumber,
502 stream_config_SetStreamNumber,
503 stream_config_GetStreamName,
504 stream_config_SetStreamName,
505 stream_config_GetConnectionName,
506 stream_config_SetConnectionName,
507 stream_config_GetBitrate,
508 stream_config_SetBitrate,
509 stream_config_GetBufferWindow,
510 stream_config_SetBufferWindow,
513 static struct stream_config *impl_from_IWMMediaProps(IWMMediaProps *iface)
515 return CONTAINING_RECORD(iface, struct stream_config, IWMMediaProps_iface);
518 static HRESULT WINAPI stream_props_QueryInterface(IWMMediaProps *iface, REFIID iid, void **out)
520 struct stream_config *config = impl_from_IWMMediaProps(iface);
521 return IWMStreamConfig_QueryInterface(&config->IWMStreamConfig_iface, iid, out);
524 static ULONG WINAPI stream_props_AddRef(IWMMediaProps *iface)
526 struct stream_config *config = impl_from_IWMMediaProps(iface);
527 return IWMStreamConfig_AddRef(&config->IWMStreamConfig_iface);
530 static ULONG WINAPI stream_props_Release(IWMMediaProps *iface)
532 struct stream_config *config = impl_from_IWMMediaProps(iface);
533 return IWMStreamConfig_Release(&config->IWMStreamConfig_iface);
536 static HRESULT WINAPI stream_props_GetType(IWMMediaProps *iface, GUID *major_type)
538 FIXME("iface %p, major_type %p, stub!\n", iface, major_type);
539 return E_NOTIMPL;
542 static HRESULT WINAPI stream_props_GetMediaType(IWMMediaProps *iface, WM_MEDIA_TYPE *mt, DWORD *size)
544 struct stream_config *config = impl_from_IWMMediaProps(iface);
545 const DWORD req_size = *size;
546 AM_MEDIA_TYPE stream_mt;
548 TRACE("iface %p, mt %p, size %p.\n", iface, mt, size);
550 if (!amt_from_wg_format(&stream_mt, &config->stream->format, true))
551 return E_OUTOFMEMORY;
553 *size = sizeof(stream_mt) + stream_mt.cbFormat;
554 if (!mt)
555 return S_OK;
556 if (req_size < *size)
557 return ASF_E_BUFFERTOOSMALL;
559 strmbase_dump_media_type(&stream_mt);
561 memcpy(mt, &stream_mt, sizeof(*mt));
562 memcpy(mt + 1, stream_mt.pbFormat, stream_mt.cbFormat);
563 mt->pbFormat = (BYTE *)(mt + 1);
564 return S_OK;
567 static HRESULT WINAPI stream_props_SetMediaType(IWMMediaProps *iface, WM_MEDIA_TYPE *mt)
569 FIXME("iface %p, mt %p, stub!\n", iface, mt);
570 return E_NOTIMPL;
573 static const IWMMediaPropsVtbl stream_props_vtbl =
575 stream_props_QueryInterface,
576 stream_props_AddRef,
577 stream_props_Release,
578 stream_props_GetType,
579 stream_props_GetMediaType,
580 stream_props_SetMediaType,
583 static DWORD CALLBACK read_thread(void *arg)
585 struct wm_reader *reader = arg;
586 IStream *stream = reader->source_stream;
587 HANDLE file = reader->file;
588 size_t buffer_size = 4096;
589 uint64_t file_size;
590 void *data;
592 if (!(data = malloc(buffer_size)))
593 return 0;
595 if (file)
597 LARGE_INTEGER size;
599 GetFileSizeEx(file, &size);
600 file_size = size.QuadPart;
602 else
604 STATSTG stat;
606 IStream_Stat(stream, &stat, STATFLAG_NONAME);
607 file_size = stat.cbSize.QuadPart;
610 TRACE("Starting read thread for reader %p.\n", reader);
612 while (!reader->read_thread_shutdown)
614 LARGE_INTEGER large_offset;
615 uint64_t offset;
616 ULONG ret_size;
617 uint32_t size;
618 HRESULT hr;
620 if (!wg_parser_get_next_read_offset(reader->wg_parser, &offset, &size))
621 continue;
623 if (offset >= file_size)
624 size = 0;
625 else if (offset + size >= file_size)
626 size = file_size - offset;
628 if (!size)
630 wg_parser_push_data(reader->wg_parser, data, 0);
631 continue;
634 if (!array_reserve(&data, &buffer_size, size, 1))
636 free(data);
637 return 0;
640 ret_size = 0;
642 large_offset.QuadPart = offset;
643 if (file)
645 if (!SetFilePointerEx(file, large_offset, NULL, FILE_BEGIN)
646 || !ReadFile(file, data, size, &ret_size, NULL))
648 ERR("Failed to read %u bytes at offset %I64u, error %lu.\n", size, offset, GetLastError());
649 wg_parser_push_data(reader->wg_parser, NULL, 0);
650 continue;
653 else
655 if (SUCCEEDED(hr = IStream_Seek(stream, large_offset, STREAM_SEEK_SET, NULL)))
656 hr = IStream_Read(stream, data, size, &ret_size);
657 if (FAILED(hr))
659 ERR("Failed to read %u bytes at offset %I64u, hr %#lx.\n", size, offset, hr);
660 wg_parser_push_data(reader->wg_parser, NULL, 0);
661 continue;
665 if (ret_size != size)
666 ERR("Unexpected short read: requested %u bytes, got %lu.\n", size, ret_size);
667 wg_parser_push_data(reader->wg_parser, data, ret_size);
670 free(data);
671 TRACE("Reader is shutting down; exiting.\n");
672 return 0;
675 static struct wm_reader *impl_from_IWMProfile3(IWMProfile3 *iface)
677 return CONTAINING_RECORD(iface, struct wm_reader, IWMProfile3_iface);
680 static HRESULT WINAPI profile_QueryInterface(IWMProfile3 *iface, REFIID iid, void **out)
682 struct wm_reader *reader = impl_from_IWMProfile3(iface);
683 return IUnknown_QueryInterface(reader->outer, iid, out);
686 static ULONG WINAPI profile_AddRef(IWMProfile3 *iface)
688 struct wm_reader *reader = impl_from_IWMProfile3(iface);
689 return IUnknown_AddRef(reader->outer);
692 static ULONG WINAPI profile_Release(IWMProfile3 *iface)
694 struct wm_reader *reader = impl_from_IWMProfile3(iface);
695 return IUnknown_Release(reader->outer);
698 static HRESULT WINAPI profile_GetVersion(IWMProfile3 *iface, WMT_VERSION *version)
700 FIXME("iface %p, version %p, stub!\n", iface, version);
701 return E_NOTIMPL;
704 static HRESULT WINAPI profile_GetName(IWMProfile3 *iface, WCHAR *name, DWORD *length)
706 FIXME("iface %p, name %p, length %p, stub!\n", iface, name, length);
707 return E_NOTIMPL;
710 static HRESULT WINAPI profile_SetName(IWMProfile3 *iface, const WCHAR *name)
712 FIXME("iface %p, name %s, stub!\n", iface, debugstr_w(name));
713 return E_NOTIMPL;
716 static HRESULT WINAPI profile_GetDescription(IWMProfile3 *iface, WCHAR *description, DWORD *length)
718 FIXME("iface %p, description %p, length %p, stub!\n", iface, description, length);
719 return E_NOTIMPL;
722 static HRESULT WINAPI profile_SetDescription(IWMProfile3 *iface, const WCHAR *description)
724 FIXME("iface %p, description %s, stub!\n", iface, debugstr_w(description));
725 return E_NOTIMPL;
728 static HRESULT WINAPI profile_GetStreamCount(IWMProfile3 *iface, DWORD *count)
730 struct wm_reader *reader = impl_from_IWMProfile3(iface);
732 TRACE("reader %p, count %p.\n", reader, count);
734 if (!count)
735 return E_INVALIDARG;
737 EnterCriticalSection(&reader->cs);
738 *count = reader->stream_count;
739 LeaveCriticalSection(&reader->cs);
740 return S_OK;
743 static HRESULT WINAPI profile_GetStream(IWMProfile3 *iface, DWORD index, IWMStreamConfig **config)
745 struct wm_reader *reader = impl_from_IWMProfile3(iface);
746 struct stream_config *object;
748 TRACE("reader %p, index %lu, config %p.\n", reader, index, config);
750 EnterCriticalSection(&reader->cs);
752 if (index >= reader->stream_count)
754 LeaveCriticalSection(&reader->cs);
755 WARN("Index %lu exceeds stream count %u; returning E_INVALIDARG.\n", index, reader->stream_count);
756 return E_INVALIDARG;
759 if (!(object = calloc(1, sizeof(*object))))
761 LeaveCriticalSection(&reader->cs);
762 return E_OUTOFMEMORY;
765 object->IWMStreamConfig_iface.lpVtbl = &stream_config_vtbl;
766 object->IWMMediaProps_iface.lpVtbl = &stream_props_vtbl;
767 object->refcount = 1;
768 object->stream = &reader->streams[index];
769 IWMProfile3_AddRef(&reader->IWMProfile3_iface);
771 LeaveCriticalSection(&reader->cs);
773 TRACE("Created stream config %p.\n", object);
774 *config = &object->IWMStreamConfig_iface;
775 return S_OK;
778 static HRESULT WINAPI profile_GetStreamByNumber(IWMProfile3 *iface, WORD stream_number, IWMStreamConfig **config)
780 FIXME("iface %p, stream_number %u, config %p, stub!\n", iface, stream_number, config);
781 return E_NOTIMPL;
784 static HRESULT WINAPI profile_RemoveStream(IWMProfile3 *iface, IWMStreamConfig *config)
786 FIXME("iface %p, config %p, stub!\n", iface, config);
787 return E_NOTIMPL;
790 static HRESULT WINAPI profile_RemoveStreamByNumber(IWMProfile3 *iface, WORD stream_number)
792 FIXME("iface %p, stream_number %u, stub!\n", iface, stream_number);
793 return E_NOTIMPL;
796 static HRESULT WINAPI profile_AddStream(IWMProfile3 *iface, IWMStreamConfig *config)
798 FIXME("iface %p, config %p, stub!\n", iface, config);
799 return E_NOTIMPL;
802 static HRESULT WINAPI profile_ReconfigStream(IWMProfile3 *iface, IWMStreamConfig *config)
804 FIXME("iface %p, config %p, stub!\n", iface, config);
805 return E_NOTIMPL;
808 static HRESULT WINAPI profile_CreateNewStream(IWMProfile3 *iface, REFGUID type, IWMStreamConfig **config)
810 FIXME("iface %p, type %s, config %p, stub!\n", iface, debugstr_guid(type), config);
811 return E_NOTIMPL;
814 static HRESULT WINAPI profile_GetMutualExclusionCount(IWMProfile3 *iface, DWORD *count)
816 FIXME("iface %p, count %p, stub!\n", iface, count);
817 return E_NOTIMPL;
820 static HRESULT WINAPI profile_GetMutualExclusion(IWMProfile3 *iface, DWORD index, IWMMutualExclusion **excl)
822 FIXME("iface %p, index %lu, excl %p, stub!\n", iface, index, excl);
823 return E_NOTIMPL;
826 static HRESULT WINAPI profile_RemoveMutualExclusion(IWMProfile3 *iface, IWMMutualExclusion *excl)
828 FIXME("iface %p, excl %p, stub!\n", iface, excl);
829 return E_NOTIMPL;
832 static HRESULT WINAPI profile_AddMutualExclusion(IWMProfile3 *iface, IWMMutualExclusion *excl)
834 FIXME("iface %p, excl %p, stub!\n", iface, excl);
835 return E_NOTIMPL;
838 static HRESULT WINAPI profile_CreateNewMutualExclusion(IWMProfile3 *iface, IWMMutualExclusion **excl)
840 FIXME("iface %p, excl %p, stub!\n", iface, excl);
841 return E_NOTIMPL;
844 static HRESULT WINAPI profile_GetProfileID(IWMProfile3 *iface, GUID *id)
846 FIXME("iface %p, id %p, stub!\n", iface, id);
847 return E_NOTIMPL;
850 static HRESULT WINAPI profile_GetStorageFormat(IWMProfile3 *iface, WMT_STORAGE_FORMAT *format)
852 FIXME("iface %p, format %p, stub!\n", iface, format);
853 return E_NOTIMPL;
856 static HRESULT WINAPI profile_SetStorageFormat(IWMProfile3 *iface, WMT_STORAGE_FORMAT format)
858 FIXME("iface %p, format %#x, stub!\n", iface, format);
859 return E_NOTIMPL;
862 static HRESULT WINAPI profile_GetBandwidthSharingCount(IWMProfile3 *iface, DWORD *count)
864 FIXME("iface %p, count %p, stub!\n", iface, count);
865 return E_NOTIMPL;
868 static HRESULT WINAPI profile_GetBandwidthSharing(IWMProfile3 *iface, DWORD index, IWMBandwidthSharing **sharing)
870 FIXME("iface %p, index %lu, sharing %p, stub!\n", iface, index, sharing);
871 return E_NOTIMPL;
874 static HRESULT WINAPI profile_RemoveBandwidthSharing( IWMProfile3 *iface, IWMBandwidthSharing *sharing)
876 FIXME("iface %p, sharing %p, stub!\n", iface, sharing);
877 return E_NOTIMPL;
880 static HRESULT WINAPI profile_AddBandwidthSharing(IWMProfile3 *iface, IWMBandwidthSharing *sharing)
882 FIXME("iface %p, sharing %p, stub!\n", iface, sharing);
883 return E_NOTIMPL;
886 static HRESULT WINAPI profile_CreateNewBandwidthSharing( IWMProfile3 *iface, IWMBandwidthSharing **sharing)
888 FIXME("iface %p, sharing %p, stub!\n", iface, sharing);
889 return E_NOTIMPL;
892 static HRESULT WINAPI profile_GetStreamPrioritization(IWMProfile3 *iface, IWMStreamPrioritization **stream)
894 FIXME("iface %p, stream %p, stub!\n", iface, stream);
895 return E_NOTIMPL;
898 static HRESULT WINAPI profile_SetStreamPrioritization(IWMProfile3 *iface, IWMStreamPrioritization *stream)
900 FIXME("iface %p, stream %p, stub!\n", iface, stream);
901 return E_NOTIMPL;
904 static HRESULT WINAPI profile_RemoveStreamPrioritization(IWMProfile3 *iface)
906 FIXME("iface %p, stub!\n", iface);
907 return E_NOTIMPL;
910 static HRESULT WINAPI profile_CreateNewStreamPrioritization(IWMProfile3 *iface, IWMStreamPrioritization **stream)
912 FIXME("iface %p, stream %p, stub!\n", iface, stream);
913 return E_NOTIMPL;
916 static HRESULT WINAPI profile_GetExpectedPacketCount(IWMProfile3 *iface, QWORD duration, QWORD *count)
918 FIXME("iface %p, duration %s, count %p, stub!\n", iface, debugstr_time(duration), count);
919 return E_NOTIMPL;
922 static const IWMProfile3Vtbl profile_vtbl =
924 profile_QueryInterface,
925 profile_AddRef,
926 profile_Release,
927 profile_GetVersion,
928 profile_GetName,
929 profile_SetName,
930 profile_GetDescription,
931 profile_SetDescription,
932 profile_GetStreamCount,
933 profile_GetStream,
934 profile_GetStreamByNumber,
935 profile_RemoveStream,
936 profile_RemoveStreamByNumber,
937 profile_AddStream,
938 profile_ReconfigStream,
939 profile_CreateNewStream,
940 profile_GetMutualExclusionCount,
941 profile_GetMutualExclusion,
942 profile_RemoveMutualExclusion,
943 profile_AddMutualExclusion,
944 profile_CreateNewMutualExclusion,
945 profile_GetProfileID,
946 profile_GetStorageFormat,
947 profile_SetStorageFormat,
948 profile_GetBandwidthSharingCount,
949 profile_GetBandwidthSharing,
950 profile_RemoveBandwidthSharing,
951 profile_AddBandwidthSharing,
952 profile_CreateNewBandwidthSharing,
953 profile_GetStreamPrioritization,
954 profile_SetStreamPrioritization,
955 profile_RemoveStreamPrioritization,
956 profile_CreateNewStreamPrioritization,
957 profile_GetExpectedPacketCount,
960 static struct wm_reader *impl_from_IWMHeaderInfo3(IWMHeaderInfo3 *iface)
962 return CONTAINING_RECORD(iface, struct wm_reader, IWMHeaderInfo3_iface);
965 static HRESULT WINAPI header_info_QueryInterface(IWMHeaderInfo3 *iface, REFIID iid, void **out)
967 struct wm_reader *reader = impl_from_IWMHeaderInfo3(iface);
968 return IUnknown_QueryInterface(reader->outer, iid, out);
971 static ULONG WINAPI header_info_AddRef(IWMHeaderInfo3 *iface)
973 struct wm_reader *reader = impl_from_IWMHeaderInfo3(iface);
974 return IUnknown_AddRef(reader->outer);
977 static ULONG WINAPI header_info_Release(IWMHeaderInfo3 *iface)
979 struct wm_reader *reader = impl_from_IWMHeaderInfo3(iface);
980 return IUnknown_Release(reader->outer);
983 static HRESULT WINAPI header_info_GetAttributeCount(IWMHeaderInfo3 *iface, WORD stream_number, WORD *count)
985 FIXME("iface %p, stream_number %u, count %p, stub!\n", iface, stream_number, count);
986 return E_NOTIMPL;
989 static HRESULT WINAPI header_info_GetAttributeByIndex(IWMHeaderInfo3 *iface, WORD index, WORD *stream_number,
990 WCHAR *name, WORD *name_len, WMT_ATTR_DATATYPE *type, BYTE *value, WORD *size)
992 FIXME("iface %p, index %u, stream_number %p, name %p, name_len %p, type %p, value %p, size %p, stub!\n",
993 iface, index, stream_number, name, name_len, type, value, size);
994 return E_NOTIMPL;
997 static HRESULT WINAPI header_info_GetAttributeByName(IWMHeaderInfo3 *iface, WORD *stream_number,
998 const WCHAR *name, WMT_ATTR_DATATYPE *type, BYTE *value, WORD *size)
1000 struct wm_reader *reader = impl_from_IWMHeaderInfo3(iface);
1001 const WORD req_size = *size;
1003 TRACE("reader %p, stream_number %p, name %s, type %p, value %p, size %u.\n",
1004 reader, stream_number, debugstr_w(name), type, value, *size);
1006 if (!stream_number)
1007 return E_INVALIDARG;
1009 if (!wcscmp(name, L"Duration"))
1011 QWORD duration;
1013 if (*stream_number)
1015 WARN("Requesting duration for stream %u, returning ASF_E_NOTFOUND.\n", *stream_number);
1016 return ASF_E_NOTFOUND;
1019 *size = sizeof(QWORD);
1020 if (!value)
1022 *type = WMT_TYPE_QWORD;
1023 return S_OK;
1025 if (req_size < *size)
1026 return ASF_E_BUFFERTOOSMALL;
1028 *type = WMT_TYPE_QWORD;
1029 EnterCriticalSection(&reader->cs);
1030 duration = wg_parser_stream_get_duration(wg_parser_get_stream(reader->wg_parser, 0));
1031 LeaveCriticalSection(&reader->cs);
1032 TRACE("Returning duration %s.\n", debugstr_time(duration));
1033 memcpy(value, &duration, sizeof(QWORD));
1034 return S_OK;
1036 else if (!wcscmp(name, L"Seekable"))
1038 if (*stream_number)
1040 WARN("Requesting duration for stream %u, returning ASF_E_NOTFOUND.\n", *stream_number);
1041 return ASF_E_NOTFOUND;
1044 *size = sizeof(BOOL);
1045 if (!value)
1047 *type = WMT_TYPE_BOOL;
1048 return S_OK;
1050 if (req_size < *size)
1051 return ASF_E_BUFFERTOOSMALL;
1053 *type = WMT_TYPE_BOOL;
1054 *(BOOL *)value = TRUE;
1055 return S_OK;
1057 else
1059 FIXME("Unknown attribute %s.\n", debugstr_w(name));
1060 return ASF_E_NOTFOUND;
1064 static HRESULT WINAPI header_info_SetAttribute(IWMHeaderInfo3 *iface, WORD stream_number,
1065 const WCHAR *name, WMT_ATTR_DATATYPE type, const BYTE *value, WORD size)
1067 FIXME("iface %p, stream_number %u, name %s, type %#x, value %p, size %u, stub!\n",
1068 iface, stream_number, debugstr_w(name), type, value, size);
1069 return E_NOTIMPL;
1072 static HRESULT WINAPI header_info_GetMarkerCount(IWMHeaderInfo3 *iface, WORD *count)
1074 FIXME("iface %p, count %p, stub!\n", iface, count);
1075 return E_NOTIMPL;
1078 static HRESULT WINAPI header_info_GetMarker(IWMHeaderInfo3 *iface,
1079 WORD index, WCHAR *name, WORD *len, QWORD *time)
1081 FIXME("iface %p, index %u, name %p, len %p, time %p, stub!\n", iface, index, name, len, time);
1082 return E_NOTIMPL;
1085 static HRESULT WINAPI header_info_AddMarker(IWMHeaderInfo3 *iface, const WCHAR *name, QWORD time)
1087 FIXME("iface %p, name %s, time %s, stub!\n", iface, debugstr_w(name), debugstr_time(time));
1088 return E_NOTIMPL;
1091 static HRESULT WINAPI header_info_RemoveMarker(IWMHeaderInfo3 *iface, WORD index)
1093 FIXME("iface %p, index %u, stub!\n", iface, index);
1094 return E_NOTIMPL;
1097 static HRESULT WINAPI header_info_GetScriptCount(IWMHeaderInfo3 *iface, WORD *count)
1099 FIXME("iface %p, count %p, stub!\n", iface, count);
1100 return E_NOTIMPL;
1103 static HRESULT WINAPI header_info_GetScript(IWMHeaderInfo3 *iface, WORD index, WCHAR *type,
1104 WORD *type_len, WCHAR *command, WORD *command_len, QWORD *time)
1106 FIXME("iface %p, index %u, type %p, type_len %p, command %p, command_len %p, time %p, stub!\n",
1107 iface, index, type, type_len, command, command_len, time);
1108 return E_NOTIMPL;
1111 static HRESULT WINAPI header_info_AddScript(IWMHeaderInfo3 *iface,
1112 const WCHAR *type, const WCHAR *command, QWORD time)
1114 FIXME("iface %p, type %s, command %s, time %s, stub!\n",
1115 iface, debugstr_w(type), debugstr_w(command), debugstr_time(time));
1116 return E_NOTIMPL;
1119 static HRESULT WINAPI header_info_RemoveScript(IWMHeaderInfo3 *iface, WORD index)
1121 FIXME("iface %p, index %u, stub!\n", iface, index);
1122 return E_NOTIMPL;
1125 static HRESULT WINAPI header_info_GetCodecInfoCount(IWMHeaderInfo3 *iface, DWORD *count)
1127 FIXME("iface %p, count %p, stub!\n", iface, count);
1128 return E_NOTIMPL;
1131 static HRESULT WINAPI header_info_GetCodecInfo(IWMHeaderInfo3 *iface, DWORD index, WORD *name_len,
1132 WCHAR *name, WORD *desc_len, WCHAR *desc, WMT_CODEC_INFO_TYPE *type, WORD *size, BYTE *info)
1134 FIXME("iface %p, index %lu, name_len %p, name %p, desc_len %p, desc %p, type %p, size %p, info %p, stub!\n",
1135 iface, index, name_len, name, desc_len, desc, type, size, info);
1136 return E_NOTIMPL;
1139 static HRESULT WINAPI header_info_GetAttributeCountEx(IWMHeaderInfo3 *iface, WORD stream_number, WORD *count)
1141 FIXME("iface %p, stream_number %u, count %p, stub!\n", iface, stream_number, count);
1142 return E_NOTIMPL;
1145 static HRESULT WINAPI header_info_GetAttributeIndices(IWMHeaderInfo3 *iface, WORD stream_number,
1146 const WCHAR *name, WORD *lang_index, WORD *indices, WORD *count)
1148 FIXME("iface %p, stream_number %u, name %s, lang_index %p, indices %p, count %p, stub!\n",
1149 iface, stream_number, debugstr_w(name), lang_index, indices, count);
1150 return E_NOTIMPL;
1153 static HRESULT WINAPI header_info_GetAttributeByIndexEx(IWMHeaderInfo3 *iface,
1154 WORD stream_number, WORD index, WCHAR *name, WORD *name_len,
1155 WMT_ATTR_DATATYPE *type, WORD *lang_index, BYTE *value, DWORD *size)
1157 FIXME("iface %p, stream_number %u, index %u, name %p, name_len %p,"
1158 " type %p, lang_index %p, value %p, size %p, stub!\n",
1159 iface, stream_number, index, name, name_len, type, lang_index, value, size);
1160 return E_NOTIMPL;
1163 static HRESULT WINAPI header_info_ModifyAttribute(IWMHeaderInfo3 *iface, WORD stream_number,
1164 WORD index, WMT_ATTR_DATATYPE type, WORD lang_index, const BYTE *value, DWORD size)
1166 FIXME("iface %p, stream_number %u, index %u, type %#x, lang_index %u, value %p, size %lu, stub!\n",
1167 iface, stream_number, index, type, lang_index, value, size);
1168 return E_NOTIMPL;
1171 static HRESULT WINAPI header_info_AddAttribute(IWMHeaderInfo3 *iface,
1172 WORD stream_number, const WCHAR *name, WORD *index,
1173 WMT_ATTR_DATATYPE type, WORD lang_index, const BYTE *value, DWORD size)
1175 FIXME("iface %p, stream_number %u, name %s, index %p, type %#x, lang_index %u, value %p, size %lu, stub!\n",
1176 iface, stream_number, debugstr_w(name), index, type, lang_index, value, size);
1177 return E_NOTIMPL;
1180 static HRESULT WINAPI header_info_DeleteAttribute(IWMHeaderInfo3 *iface, WORD stream_number, WORD index)
1182 FIXME("iface %p, stream_number %u, index %u, stub!\n", iface, stream_number, index);
1183 return E_NOTIMPL;
1186 static HRESULT WINAPI header_info_AddCodecInfo(IWMHeaderInfo3 *iface, const WCHAR *name,
1187 const WCHAR *desc, WMT_CODEC_INFO_TYPE type, WORD size, BYTE *info)
1189 FIXME("iface %p, name %s, desc %s, type %#x, size %u, info %p, stub!\n",
1190 info, debugstr_w(name), debugstr_w(desc), type, size, info);
1191 return E_NOTIMPL;
1194 static const IWMHeaderInfo3Vtbl header_info_vtbl =
1196 header_info_QueryInterface,
1197 header_info_AddRef,
1198 header_info_Release,
1199 header_info_GetAttributeCount,
1200 header_info_GetAttributeByIndex,
1201 header_info_GetAttributeByName,
1202 header_info_SetAttribute,
1203 header_info_GetMarkerCount,
1204 header_info_GetMarker,
1205 header_info_AddMarker,
1206 header_info_RemoveMarker,
1207 header_info_GetScriptCount,
1208 header_info_GetScript,
1209 header_info_AddScript,
1210 header_info_RemoveScript,
1211 header_info_GetCodecInfoCount,
1212 header_info_GetCodecInfo,
1213 header_info_GetAttributeCountEx,
1214 header_info_GetAttributeIndices,
1215 header_info_GetAttributeByIndexEx,
1216 header_info_ModifyAttribute,
1217 header_info_AddAttribute,
1218 header_info_DeleteAttribute,
1219 header_info_AddCodecInfo,
1222 static struct wm_reader *impl_from_IWMLanguageList(IWMLanguageList *iface)
1224 return CONTAINING_RECORD(iface, struct wm_reader, IWMLanguageList_iface);
1227 static HRESULT WINAPI language_list_QueryInterface(IWMLanguageList *iface, REFIID iid, void **out)
1229 struct wm_reader *reader = impl_from_IWMLanguageList(iface);
1230 return IUnknown_QueryInterface(reader->outer, iid, out);
1233 static ULONG WINAPI language_list_AddRef(IWMLanguageList *iface)
1235 struct wm_reader *reader = impl_from_IWMLanguageList(iface);
1236 return IUnknown_AddRef(reader->outer);
1239 static ULONG WINAPI language_list_Release(IWMLanguageList *iface)
1241 struct wm_reader *reader = impl_from_IWMLanguageList(iface);
1242 return IUnknown_Release(reader->outer);
1245 static HRESULT WINAPI language_list_GetLanguageCount(IWMLanguageList *iface, WORD *count)
1247 FIXME("iface %p, count %p, stub!\n", iface, count);
1248 return E_NOTIMPL;
1251 static HRESULT WINAPI language_list_GetLanguageDetails(IWMLanguageList *iface,
1252 WORD index, WCHAR *lang, WORD *len)
1254 FIXME("iface %p, index %u, lang %p, len %p, stub!\n", iface, index, lang, len);
1255 return E_NOTIMPL;
1258 static HRESULT WINAPI language_list_AddLanguageByRFC1766String(IWMLanguageList *iface,
1259 const WCHAR *lang, WORD *index)
1261 FIXME("iface %p, lang %s, index %p, stub!\n", iface, debugstr_w(lang), index);
1262 return E_NOTIMPL;
1265 static const IWMLanguageListVtbl language_list_vtbl =
1267 language_list_QueryInterface,
1268 language_list_AddRef,
1269 language_list_Release,
1270 language_list_GetLanguageCount,
1271 language_list_GetLanguageDetails,
1272 language_list_AddLanguageByRFC1766String,
1275 static struct wm_reader *impl_from_IWMPacketSize2(IWMPacketSize2 *iface)
1277 return CONTAINING_RECORD(iface, struct wm_reader, IWMPacketSize2_iface);
1280 static HRESULT WINAPI packet_size_QueryInterface(IWMPacketSize2 *iface, REFIID iid, void **out)
1282 struct wm_reader *reader = impl_from_IWMPacketSize2(iface);
1283 return IUnknown_QueryInterface(reader->outer, iid, out);
1286 static ULONG WINAPI packet_size_AddRef(IWMPacketSize2 *iface)
1288 struct wm_reader *reader = impl_from_IWMPacketSize2(iface);
1289 return IUnknown_AddRef(reader->outer);
1292 static ULONG WINAPI packet_size_Release(IWMPacketSize2 *iface)
1294 struct wm_reader *reader = impl_from_IWMPacketSize2(iface);
1295 return IUnknown_Release(reader->outer);
1298 static HRESULT WINAPI packet_size_GetMaxPacketSize(IWMPacketSize2 *iface, DWORD *size)
1300 FIXME("iface %p, size %p, stub!\n", iface, size);
1301 return E_NOTIMPL;
1304 static HRESULT WINAPI packet_size_SetMaxPacketSize(IWMPacketSize2 *iface, DWORD size)
1306 FIXME("iface %p, size %lu, stub!\n", iface, size);
1307 return E_NOTIMPL;
1310 static HRESULT WINAPI packet_size_GetMinPacketSize(IWMPacketSize2 *iface, DWORD *size)
1312 FIXME("iface %p, size %p, stub!\n", iface, size);
1313 return E_NOTIMPL;
1316 static HRESULT WINAPI packet_size_SetMinPacketSize(IWMPacketSize2 *iface, DWORD size)
1318 FIXME("iface %p, size %lu, stub!\n", iface, size);
1319 return E_NOTIMPL;
1322 static const IWMPacketSize2Vtbl packet_size_vtbl =
1324 packet_size_QueryInterface,
1325 packet_size_AddRef,
1326 packet_size_Release,
1327 packet_size_GetMaxPacketSize,
1328 packet_size_SetMaxPacketSize,
1329 packet_size_GetMinPacketSize,
1330 packet_size_SetMinPacketSize,
1333 static struct wm_reader *impl_from_IWMReaderPlaylistBurn(IWMReaderPlaylistBurn *iface)
1335 return CONTAINING_RECORD(iface, struct wm_reader, IWMReaderPlaylistBurn_iface);
1338 static HRESULT WINAPI playlist_QueryInterface(IWMReaderPlaylistBurn *iface, REFIID iid, void **out)
1340 struct wm_reader *reader = impl_from_IWMReaderPlaylistBurn(iface);
1341 return IUnknown_QueryInterface(reader->outer, iid, out);
1344 static ULONG WINAPI playlist_AddRef(IWMReaderPlaylistBurn *iface)
1346 struct wm_reader *reader = impl_from_IWMReaderPlaylistBurn(iface);
1347 return IUnknown_AddRef(reader->outer);
1350 static ULONG WINAPI playlist_Release(IWMReaderPlaylistBurn *iface)
1352 struct wm_reader *reader = impl_from_IWMReaderPlaylistBurn(iface);
1353 return IUnknown_Release(reader->outer);
1356 static HRESULT WINAPI playlist_InitPlaylistBurn(IWMReaderPlaylistBurn *iface, DWORD count,
1357 const WCHAR **filenames, IWMStatusCallback *callback, void *context)
1359 FIXME("iface %p, count %lu, filenames %p, callback %p, context %p, stub!\n",
1360 iface, count, filenames, callback, context);
1361 return E_NOTIMPL;
1364 static HRESULT WINAPI playlist_GetInitResults(IWMReaderPlaylistBurn *iface, DWORD count, HRESULT *hrs)
1366 FIXME("iface %p, count %lu, hrs %p, stub!\n", iface, count, hrs);
1367 return E_NOTIMPL;
1370 static HRESULT WINAPI playlist_Cancel(IWMReaderPlaylistBurn *iface)
1372 FIXME("iface %p, stub!\n", iface);
1373 return E_NOTIMPL;
1376 static HRESULT WINAPI playlist_EndPlaylistBurn(IWMReaderPlaylistBurn *iface, HRESULT hr)
1378 FIXME("iface %p, hr %#lx, stub!\n", iface, hr);
1379 return E_NOTIMPL;
1382 static const IWMReaderPlaylistBurnVtbl playlist_vtbl =
1384 playlist_QueryInterface,
1385 playlist_AddRef,
1386 playlist_Release,
1387 playlist_InitPlaylistBurn,
1388 playlist_GetInitResults,
1389 playlist_Cancel,
1390 playlist_EndPlaylistBurn,
1393 static struct wm_reader *impl_from_IWMReaderTimecode(IWMReaderTimecode *iface)
1395 return CONTAINING_RECORD(iface, struct wm_reader, IWMReaderTimecode_iface);
1398 static HRESULT WINAPI timecode_QueryInterface(IWMReaderTimecode *iface, REFIID iid, void **out)
1400 struct wm_reader *reader = impl_from_IWMReaderTimecode(iface);
1401 return IUnknown_QueryInterface(reader->outer, iid, out);
1404 static ULONG WINAPI timecode_AddRef(IWMReaderTimecode *iface)
1406 struct wm_reader *reader = impl_from_IWMReaderTimecode(iface);
1407 return IUnknown_AddRef(reader->outer);
1410 static ULONG WINAPI timecode_Release(IWMReaderTimecode *iface)
1412 struct wm_reader *reader = impl_from_IWMReaderTimecode(iface);
1413 return IUnknown_Release(reader->outer);
1416 static HRESULT WINAPI timecode_GetTimecodeRangeCount(IWMReaderTimecode *iface,
1417 WORD stream_number, WORD *count)
1419 FIXME("iface %p, stream_number %u, count %p, stub!\n", iface, stream_number, count);
1420 return E_NOTIMPL;
1423 static HRESULT WINAPI timecode_GetTimecodeRangeBounds(IWMReaderTimecode *iface,
1424 WORD stream_number, WORD index, DWORD *start, DWORD *end)
1426 FIXME("iface %p, stream_number %u, index %u, start %p, end %p, stub!\n",
1427 iface, stream_number, index, start, end);
1428 return E_NOTIMPL;
1431 static const IWMReaderTimecodeVtbl timecode_vtbl =
1433 timecode_QueryInterface,
1434 timecode_AddRef,
1435 timecode_Release,
1436 timecode_GetTimecodeRangeCount,
1437 timecode_GetTimecodeRangeBounds,
1440 static HRESULT init_stream(struct wm_reader *reader, QWORD file_size)
1442 struct wg_parser *wg_parser;
1443 HRESULT hr;
1444 WORD i;
1446 if (!(wg_parser = wg_parser_create(WG_PARSER_DECODEBIN, false)))
1447 return E_OUTOFMEMORY;
1449 reader->wg_parser = wg_parser;
1450 reader->read_thread_shutdown = false;
1451 if (!(reader->read_thread = CreateThread(NULL, 0, read_thread, reader, 0, NULL)))
1453 hr = E_OUTOFMEMORY;
1454 goto out_destroy_parser;
1457 if (FAILED(hr = wg_parser_connect(reader->wg_parser, file_size)))
1459 ERR("Failed to connect parser, hr %#lx.\n", hr);
1460 goto out_shutdown_thread;
1463 reader->stream_count = wg_parser_get_stream_count(reader->wg_parser);
1465 if (!(reader->streams = calloc(reader->stream_count, sizeof(*reader->streams))))
1467 hr = E_OUTOFMEMORY;
1468 goto out_disconnect_parser;
1471 for (i = 0; i < reader->stream_count; ++i)
1473 struct wm_stream *stream = &reader->streams[i];
1475 stream->wg_stream = wg_parser_get_stream(reader->wg_parser, i);
1476 stream->reader = reader;
1477 stream->index = i;
1478 stream->selection = WMT_ON;
1479 wg_parser_stream_get_preferred_format(stream->wg_stream, &stream->format);
1480 if (stream->format.major_type == WG_MAJOR_TYPE_AUDIO)
1482 /* R.U.S.E enumerates available audio types, picks the first one it
1483 * likes, and then sets the wrong stream to that type. libav might
1484 * give us WG_AUDIO_FORMAT_F32LE by default, which will result in
1485 * the game incorrectly interpreting float data as integer.
1486 * Therefore just match native and always set our default format to
1487 * S16LE. */
1488 stream->format.u.audio.format = WG_AUDIO_FORMAT_S16LE;
1490 else if (stream->format.major_type == WG_MAJOR_TYPE_VIDEO)
1492 /* Call of Juarez: Bound in Blood breaks if I420 is enumerated.
1493 * Some native decoders output I420, but the msmpeg4v3 decoder
1494 * never does.
1496 * Shadowgrounds provides wmv3 video and assumes that the initial
1497 * video type will be BGR. */
1498 stream->format.u.video.format = WG_VIDEO_FORMAT_BGR;
1500 wg_parser_stream_enable(stream->wg_stream, &stream->format);
1503 /* We probably discarded events because streams weren't enabled yet.
1504 * Now that they're all enabled seek back to the start again. */
1505 wg_parser_stream_seek(reader->streams[0].wg_stream, 1.0, 0, 0,
1506 AM_SEEKING_AbsolutePositioning, AM_SEEKING_NoPositioning);
1508 return S_OK;
1510 out_disconnect_parser:
1511 wg_parser_disconnect(reader->wg_parser);
1513 out_shutdown_thread:
1514 reader->read_thread_shutdown = true;
1515 WaitForSingleObject(reader->read_thread, INFINITE);
1516 CloseHandle(reader->read_thread);
1517 reader->read_thread = NULL;
1519 out_destroy_parser:
1520 wg_parser_destroy(reader->wg_parser);
1521 reader->wg_parser = NULL;
1523 return hr;
1526 static struct wm_stream *wm_reader_get_stream_by_stream_number(struct wm_reader *reader, WORD stream_number)
1528 if (stream_number && stream_number <= reader->stream_count)
1529 return &reader->streams[stream_number - 1];
1530 WARN("Invalid stream number %u.\n", stream_number);
1531 return NULL;
1534 static const enum wg_video_format video_formats[] =
1536 /* Try to prefer YUV formats over RGB ones. Most decoders output in the
1537 * YUV color space, and it's generally much less expensive for
1538 * videoconvert to do YUV -> YUV transformations. */
1539 WG_VIDEO_FORMAT_NV12,
1540 WG_VIDEO_FORMAT_YV12,
1541 WG_VIDEO_FORMAT_YUY2,
1542 WG_VIDEO_FORMAT_UYVY,
1543 WG_VIDEO_FORMAT_YVYU,
1544 WG_VIDEO_FORMAT_BGRx,
1545 WG_VIDEO_FORMAT_BGR,
1546 WG_VIDEO_FORMAT_RGB16,
1547 WG_VIDEO_FORMAT_RGB15,
1550 static const char *get_major_type_string(enum wg_major_type type)
1552 switch (type)
1554 case WG_MAJOR_TYPE_AUDIO:
1555 return "audio";
1556 case WG_MAJOR_TYPE_AUDIO_MPEG1:
1557 return "mpeg1-audio";
1558 case WG_MAJOR_TYPE_AUDIO_MPEG4:
1559 return "mpeg4-audio";
1560 case WG_MAJOR_TYPE_AUDIO_WMA:
1561 return "wma";
1562 case WG_MAJOR_TYPE_VIDEO:
1563 return "video";
1564 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
1565 return "cinepak";
1566 case WG_MAJOR_TYPE_VIDEO_H264:
1567 return "h264";
1568 case WG_MAJOR_TYPE_UNKNOWN:
1569 return "unknown";
1571 assert(0);
1572 return NULL;
1575 static HRESULT wm_stream_allocate_sample(struct wm_stream *stream, DWORD size, INSSBuffer **sample)
1577 struct buffer *buffer;
1579 if (!stream->read_compressed && stream->output_allocator)
1580 return IWMReaderAllocatorEx_AllocateForOutputEx(stream->output_allocator, stream->index,
1581 size, sample, 0, 0, 0, NULL);
1583 if (stream->read_compressed && stream->stream_allocator)
1584 return IWMReaderAllocatorEx_AllocateForStreamEx(stream->stream_allocator, stream->index + 1,
1585 size, sample, 0, 0, 0, NULL);
1587 /* FIXME: Should these be pooled? */
1588 if (!(buffer = calloc(1, offsetof(struct buffer, data[size]))))
1589 return E_OUTOFMEMORY;
1590 buffer->INSSBuffer_iface.lpVtbl = &buffer_vtbl;
1591 buffer->refcount = 1;
1592 buffer->capacity = size;
1594 TRACE("Created buffer %p.\n", buffer);
1595 *sample = &buffer->INSSBuffer_iface;
1596 return S_OK;
1599 static HRESULT wm_reader_read_stream_sample(struct wm_reader *reader, struct wg_parser_buffer *buffer,
1600 INSSBuffer **sample, QWORD *pts, QWORD *duration, DWORD *flags)
1602 struct wm_stream *stream;
1603 DWORD size, capacity;
1604 HRESULT hr;
1605 BYTE *data;
1607 if (!(stream = wm_reader_get_stream_by_stream_number(reader, buffer->stream + 1)))
1608 return E_INVALIDARG;
1610 TRACE("Got buffer for '%s' stream %p.\n", get_major_type_string(stream->format.major_type), stream);
1612 if (FAILED(hr = wm_stream_allocate_sample(stream, buffer->size, sample)))
1614 ERR("Failed to allocate sample of %u bytes, hr %#lx.\n", buffer->size, hr);
1615 wg_parser_stream_release_buffer(stream->wg_stream);
1616 return hr;
1619 if (FAILED(hr = INSSBuffer_GetBufferAndLength(*sample, &data, &size)))
1620 ERR("Failed to get data pointer, hr %#lx.\n", hr);
1621 if (FAILED(hr = INSSBuffer_GetMaxLength(*sample, &capacity)))
1622 ERR("Failed to get capacity, hr %#lx.\n", hr);
1623 if (buffer->size > capacity)
1624 ERR("Returned capacity %lu is less than requested capacity %u.\n", capacity, buffer->size);
1626 if (!wg_parser_stream_copy_buffer(stream->wg_stream, data, 0, buffer->size))
1628 /* The GStreamer pin has been flushed. */
1629 INSSBuffer_Release(*sample);
1630 *sample = NULL;
1631 return S_FALSE;
1634 if (FAILED(hr = INSSBuffer_SetLength(*sample, buffer->size)))
1635 ERR("Failed to set size %u, hr %#lx.\n", buffer->size, hr);
1637 wg_parser_stream_release_buffer(stream->wg_stream);
1639 if (!buffer->has_pts)
1640 FIXME("Missing PTS.\n");
1641 if (!buffer->has_duration)
1642 FIXME("Missing duration.\n");
1644 *pts = buffer->pts;
1645 *duration = buffer->duration;
1646 *flags = 0;
1647 if (buffer->discontinuity)
1648 *flags |= WM_SF_DISCONTINUITY;
1649 if (!buffer->delta)
1650 *flags |= WM_SF_CLEANPOINT;
1652 return S_OK;
1655 static struct wm_reader *impl_from_IUnknown(IUnknown *iface)
1657 return CONTAINING_RECORD(iface, struct wm_reader, IUnknown_inner);
1660 static HRESULT WINAPI unknown_inner_QueryInterface(IUnknown *iface, REFIID iid, void **out)
1662 struct wm_reader *reader = impl_from_IUnknown(iface);
1664 TRACE("reader %p, iid %s, out %p.\n", reader, debugstr_guid(iid), out);
1666 if (IsEqualIID(iid, &IID_IUnknown)
1667 || IsEqualIID(iid, &IID_IWMSyncReader)
1668 || IsEqualIID(iid, &IID_IWMSyncReader2))
1669 *out = &reader->IWMSyncReader2_iface;
1670 else if (IsEqualIID(iid, &IID_IWMHeaderInfo)
1671 || IsEqualIID(iid, &IID_IWMHeaderInfo2)
1672 || IsEqualIID(iid, &IID_IWMHeaderInfo3))
1673 *out = &reader->IWMHeaderInfo3_iface;
1674 else if (IsEqualIID(iid, &IID_IWMLanguageList))
1675 *out = &reader->IWMLanguageList_iface;
1676 else if (IsEqualIID(iid, &IID_IWMPacketSize)
1677 || IsEqualIID(iid, &IID_IWMPacketSize2))
1678 *out = &reader->IWMPacketSize2_iface;
1679 else if (IsEqualIID(iid, &IID_IWMProfile)
1680 || IsEqualIID(iid, &IID_IWMProfile2)
1681 || IsEqualIID(iid, &IID_IWMProfile3))
1682 *out = &reader->IWMProfile3_iface;
1683 else if (IsEqualIID(iid, &IID_IWMReaderPlaylistBurn))
1684 *out = &reader->IWMReaderPlaylistBurn_iface;
1685 else if (IsEqualIID(iid, &IID_IWMReaderTimecode))
1686 *out = &reader->IWMReaderTimecode_iface;
1687 else
1689 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(iid));
1690 return E_NOINTERFACE;
1693 IUnknown_AddRef((IUnknown *)*out);
1694 return S_OK;
1697 static ULONG WINAPI unknown_inner_AddRef(IUnknown *iface)
1699 struct wm_reader *reader = impl_from_IUnknown(iface);
1700 ULONG refcount = InterlockedIncrement(&reader->refcount);
1701 TRACE("%p increasing refcount to %lu.\n", reader, refcount);
1702 return refcount;
1705 static ULONG WINAPI unknown_inner_Release(IUnknown *iface)
1707 struct wm_reader *reader = impl_from_IUnknown(iface);
1708 ULONG refcount = InterlockedDecrement(&reader->refcount);
1710 TRACE("%p decreasing refcount to %lu.\n", reader, refcount);
1712 if (!refcount)
1714 IWMSyncReader2_Close(&reader->IWMSyncReader2_iface);
1716 reader->cs.DebugInfo->Spare[0] = 0;
1717 DeleteCriticalSection(&reader->cs);
1719 free(reader);
1722 return refcount;
1725 static const IUnknownVtbl unknown_inner_vtbl =
1727 unknown_inner_QueryInterface,
1728 unknown_inner_AddRef,
1729 unknown_inner_Release,
1732 static struct wm_reader *impl_from_IWMSyncReader2(IWMSyncReader2 *iface)
1734 return CONTAINING_RECORD(iface, struct wm_reader, IWMSyncReader2_iface);
1737 static HRESULT WINAPI reader_QueryInterface(IWMSyncReader2 *iface, REFIID iid, void **out)
1739 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1740 return IUnknown_QueryInterface(reader->outer, iid, out);
1743 static ULONG WINAPI reader_AddRef(IWMSyncReader2 *iface)
1745 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1746 return IUnknown_AddRef(reader->outer);
1749 static ULONG WINAPI reader_Release(IWMSyncReader2 *iface)
1751 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1752 return IUnknown_Release(reader->outer);
1755 static HRESULT WINAPI reader_Close(IWMSyncReader2 *iface)
1757 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1759 TRACE("reader %p.\n", reader);
1761 EnterCriticalSection(&reader->cs);
1763 if (!reader->wg_parser)
1765 LeaveCriticalSection(&reader->cs);
1766 return NS_E_INVALID_REQUEST;
1769 wg_parser_disconnect(reader->wg_parser);
1771 reader->read_thread_shutdown = true;
1772 WaitForSingleObject(reader->read_thread, INFINITE);
1773 CloseHandle(reader->read_thread);
1774 reader->read_thread = NULL;
1776 wg_parser_destroy(reader->wg_parser);
1777 reader->wg_parser = NULL;
1779 if (reader->source_stream)
1780 IStream_Release(reader->source_stream);
1781 reader->source_stream = NULL;
1782 if (reader->file)
1783 CloseHandle(reader->file);
1784 reader->file = NULL;
1786 LeaveCriticalSection(&reader->cs);
1787 return S_OK;
1790 static HRESULT WINAPI reader_GetMaxOutputSampleSize(IWMSyncReader2 *iface, DWORD output, DWORD *max)
1792 struct wm_reader *This = impl_from_IWMSyncReader2(iface);
1793 FIXME("(%p)->(%lu %p): stub!\n", This, output, max);
1794 return E_NOTIMPL;
1797 static HRESULT WINAPI reader_GetMaxStreamSampleSize(IWMSyncReader2 *iface, WORD stream_number, DWORD *size)
1799 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1800 struct wm_stream *stream;
1802 TRACE("reader %p, stream_number %u, size %p.\n", reader, stream_number, size);
1804 EnterCriticalSection(&reader->cs);
1806 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
1808 LeaveCriticalSection(&reader->cs);
1809 return E_INVALIDARG;
1812 *size = wg_format_get_max_size(&stream->format);
1814 LeaveCriticalSection(&reader->cs);
1815 return S_OK;
1818 static HRESULT WINAPI reader_GetNextSample(IWMSyncReader2 *iface,
1819 WORD stream_number, INSSBuffer **sample, QWORD *pts, QWORD *duration,
1820 DWORD *flags, DWORD *output_number, WORD *ret_stream_number)
1822 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1823 struct wm_stream *stream;
1824 HRESULT hr = S_FALSE;
1826 TRACE("reader %p, stream_number %u, sample %p, pts %p, duration %p,"
1827 " flags %p, output_number %p, ret_stream_number %p.\n",
1828 reader, stream_number, sample, pts, duration, flags, output_number, ret_stream_number);
1830 if (!stream_number && !output_number && !ret_stream_number)
1831 return E_INVALIDARG;
1833 EnterCriticalSection(&reader->cs);
1835 if (!stream_number)
1836 stream = NULL;
1837 else if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
1838 hr = E_INVALIDARG;
1839 else if (stream->selection == WMT_OFF)
1840 hr = NS_E_INVALID_REQUEST;
1841 else if (stream->eos)
1842 hr = NS_E_NO_MORE_SAMPLES;
1844 while (hr == S_FALSE)
1846 struct wg_parser_buffer wg_buffer;
1847 if (!wg_parser_stream_get_buffer(reader->wg_parser, stream ? stream->wg_stream : NULL, &wg_buffer))
1848 hr = NS_E_NO_MORE_SAMPLES;
1849 else if (SUCCEEDED(hr = wm_reader_read_stream_sample(reader, &wg_buffer, sample, pts, duration, flags)))
1850 stream_number = wg_buffer.stream + 1;
1853 if (stream && hr == NS_E_NO_MORE_SAMPLES)
1854 stream->eos = true;
1856 if (output_number && hr == S_OK)
1857 *output_number = stream_number - 1;
1858 if (ret_stream_number && (hr == S_OK || stream_number))
1859 *ret_stream_number = stream_number;
1861 LeaveCriticalSection(&reader->cs);
1862 return hr;
1865 static HRESULT WINAPI reader_GetOutputCount(IWMSyncReader2 *iface, DWORD *count)
1867 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1869 TRACE("reader %p, count %p.\n", reader, count);
1871 EnterCriticalSection(&reader->cs);
1872 *count = reader->stream_count;
1873 LeaveCriticalSection(&reader->cs);
1874 return S_OK;
1877 static HRESULT WINAPI reader_GetOutputFormat(IWMSyncReader2 *iface,
1878 DWORD output, DWORD index, IWMOutputMediaProps **props)
1880 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1881 struct wm_stream *stream;
1882 struct wg_format format;
1884 TRACE("reader %p, output %lu, index %lu, props %p.\n", reader, output, index, props);
1886 EnterCriticalSection(&reader->cs);
1888 if (!(stream = get_stream_by_output_number(reader, output)))
1890 LeaveCriticalSection(&reader->cs);
1891 return E_INVALIDARG;
1894 wg_parser_stream_get_preferred_format(stream->wg_stream, &format);
1896 switch (format.major_type)
1898 case WG_MAJOR_TYPE_VIDEO:
1899 if (index >= ARRAY_SIZE(video_formats))
1901 LeaveCriticalSection(&reader->cs);
1902 return NS_E_INVALID_OUTPUT_FORMAT;
1904 format.u.video.format = video_formats[index];
1905 break;
1907 case WG_MAJOR_TYPE_AUDIO:
1908 if (index)
1910 LeaveCriticalSection(&reader->cs);
1911 return NS_E_INVALID_OUTPUT_FORMAT;
1913 format.u.audio.format = WG_AUDIO_FORMAT_S16LE;
1914 break;
1916 case WG_MAJOR_TYPE_AUDIO_MPEG1:
1917 case WG_MAJOR_TYPE_AUDIO_MPEG4:
1918 case WG_MAJOR_TYPE_AUDIO_WMA:
1919 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
1920 case WG_MAJOR_TYPE_VIDEO_H264:
1921 FIXME("Format %u not implemented!\n", format.major_type);
1922 break;
1923 case WG_MAJOR_TYPE_UNKNOWN:
1924 break;
1927 LeaveCriticalSection(&reader->cs);
1929 *props = output_props_create(&format);
1930 return *props ? S_OK : E_OUTOFMEMORY;
1933 static HRESULT WINAPI reader_GetOutputFormatCount(IWMSyncReader2 *iface, DWORD output, DWORD *count)
1935 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1936 struct wm_stream *stream;
1937 struct wg_format format;
1939 TRACE("reader %p, output %lu, count %p.\n", reader, output, count);
1941 EnterCriticalSection(&reader->cs);
1943 if (!(stream = get_stream_by_output_number(reader, output)))
1945 LeaveCriticalSection(&reader->cs);
1946 return E_INVALIDARG;
1949 wg_parser_stream_get_preferred_format(stream->wg_stream, &format);
1950 switch (format.major_type)
1952 case WG_MAJOR_TYPE_VIDEO:
1953 *count = ARRAY_SIZE(video_formats);
1954 break;
1956 case WG_MAJOR_TYPE_AUDIO_MPEG1:
1957 case WG_MAJOR_TYPE_AUDIO_MPEG4:
1958 case WG_MAJOR_TYPE_AUDIO_WMA:
1959 case WG_MAJOR_TYPE_VIDEO_CINEPAK:
1960 case WG_MAJOR_TYPE_VIDEO_H264:
1961 FIXME("Format %u not implemented!\n", format.major_type);
1962 /* fallthrough */
1963 case WG_MAJOR_TYPE_AUDIO:
1964 case WG_MAJOR_TYPE_UNKNOWN:
1965 *count = 1;
1966 break;
1969 LeaveCriticalSection(&reader->cs);
1970 return S_OK;
1973 static HRESULT WINAPI reader_GetOutputNumberForStream(IWMSyncReader2 *iface,
1974 WORD stream_number, DWORD *output)
1976 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1978 TRACE("reader %p, stream_number %u, output %p.\n", reader, stream_number, output);
1980 *output = stream_number - 1;
1981 return S_OK;
1984 static HRESULT WINAPI reader_GetOutputProps(IWMSyncReader2 *iface,
1985 DWORD output, IWMOutputMediaProps **props)
1987 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
1988 struct wm_stream *stream;
1990 TRACE("reader %p, output %lu, props %p.\n", reader, output, props);
1992 EnterCriticalSection(&reader->cs);
1994 if (!(stream = get_stream_by_output_number(reader, output)))
1996 LeaveCriticalSection(&reader->cs);
1997 return E_INVALIDARG;
2000 *props = output_props_create(&stream->format);
2001 LeaveCriticalSection(&reader->cs);
2002 return *props ? S_OK : E_OUTOFMEMORY;
2005 static HRESULT WINAPI reader_GetOutputSetting(IWMSyncReader2 *iface, DWORD output_num, const WCHAR *name,
2006 WMT_ATTR_DATATYPE *type, BYTE *value, WORD *length)
2008 struct wm_reader *This = impl_from_IWMSyncReader2(iface);
2009 FIXME("(%p)->(%lu %s %p %p %p): stub!\n", This, output_num, debugstr_w(name), type, value, length);
2010 return E_NOTIMPL;
2013 static HRESULT WINAPI reader_GetReadStreamSamples(IWMSyncReader2 *iface, WORD stream_number, BOOL *compressed)
2015 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2016 struct wm_stream *stream;
2018 TRACE("reader %p, stream_number %u, compressed %p.\n", reader, stream_number, compressed);
2020 EnterCriticalSection(&reader->cs);
2022 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
2024 LeaveCriticalSection(&reader->cs);
2025 return E_INVALIDARG;
2028 *compressed = stream->read_compressed;
2030 LeaveCriticalSection(&reader->cs);
2031 return S_OK;
2034 static HRESULT WINAPI reader_GetStreamNumberForOutput(IWMSyncReader2 *iface,
2035 DWORD output, WORD *stream_number)
2037 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2039 TRACE("reader %p, output %lu, stream_number %p.\n", reader, output, stream_number);
2041 *stream_number = output + 1;
2042 return S_OK;
2045 static HRESULT WINAPI reader_GetStreamSelected(IWMSyncReader2 *iface,
2046 WORD stream_number, WMT_STREAM_SELECTION *selection)
2048 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2049 struct wm_stream *stream;
2051 TRACE("reader %p, stream_number %u, selection %p.\n", reader, stream_number, selection);
2053 EnterCriticalSection(&reader->cs);
2055 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
2057 LeaveCriticalSection(&reader->cs);
2058 return E_INVALIDARG;
2061 *selection = stream->selection;
2063 LeaveCriticalSection(&reader->cs);
2064 return S_OK;
2067 static HRESULT WINAPI reader_Open(IWMSyncReader2 *iface, const WCHAR *filename)
2069 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2070 LARGE_INTEGER size;
2071 HANDLE file;
2072 HRESULT hr;
2074 TRACE("reader %p, filename %s.\n", reader, debugstr_w(filename));
2076 if ((file = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2077 OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
2079 ERR("Failed to open %s, error %lu.\n", debugstr_w(filename), GetLastError());
2080 return HRESULT_FROM_WIN32(GetLastError());
2083 if (!GetFileSizeEx(file, &size))
2085 ERR("Failed to get the size of %s, error %lu.\n", debugstr_w(filename), GetLastError());
2086 CloseHandle(file);
2087 return HRESULT_FROM_WIN32(GetLastError());
2090 EnterCriticalSection(&reader->cs);
2092 if (reader->wg_parser)
2094 LeaveCriticalSection(&reader->cs);
2095 WARN("Stream is already open; returning E_UNEXPECTED.\n");
2096 CloseHandle(file);
2097 return E_UNEXPECTED;
2100 reader->file = file;
2102 if (FAILED(hr = init_stream(reader, size.QuadPart)))
2103 reader->file = NULL;
2105 LeaveCriticalSection(&reader->cs);
2106 return hr;
2109 static HRESULT WINAPI reader_OpenStream(IWMSyncReader2 *iface, IStream *stream)
2111 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2112 STATSTG stat;
2113 HRESULT hr;
2115 TRACE("reader %p, stream %p.\n", reader, stream);
2117 if (FAILED(hr = IStream_Stat(stream, &stat, STATFLAG_NONAME)))
2119 ERR("Failed to stat stream, hr %#lx.\n", hr);
2120 return hr;
2123 EnterCriticalSection(&reader->cs);
2125 if (reader->wg_parser)
2127 LeaveCriticalSection(&reader->cs);
2128 WARN("Stream is already open; returning E_UNEXPECTED.\n");
2129 return E_UNEXPECTED;
2132 IStream_AddRef(reader->source_stream = stream);
2133 if (FAILED(hr = init_stream(reader, stat.cbSize.QuadPart)))
2135 IStream_Release(stream);
2136 reader->source_stream = NULL;
2139 LeaveCriticalSection(&reader->cs);
2140 return hr;
2143 static HRESULT WINAPI reader_SetOutputProps(IWMSyncReader2 *iface, DWORD output, IWMOutputMediaProps *props_iface)
2145 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2146 struct output_props *props = unsafe_impl_from_IWMOutputMediaProps(props_iface);
2147 struct wg_format format, pref_format;
2148 struct wm_stream *stream;
2149 HRESULT hr = S_OK;
2150 int i;
2152 TRACE("reader %p, output %lu, props_iface %p.\n", reader, output, props_iface);
2154 strmbase_dump_media_type(&props->mt);
2156 if (!amt_to_wg_format(&props->mt, &format))
2158 ERR("Failed to convert media type to winegstreamer format.\n");
2159 return E_FAIL;
2162 EnterCriticalSection(&reader->cs);
2164 if (!(stream = get_stream_by_output_number(reader, output)))
2166 LeaveCriticalSection(&reader->cs);
2167 return E_INVALIDARG;
2170 wg_parser_stream_get_preferred_format(stream->wg_stream, &pref_format);
2171 if (pref_format.major_type != format.major_type)
2173 /* R.U.S.E sets the type of the wrong stream, apparently by accident. */
2174 hr = NS_E_INCOMPATIBLE_FORMAT;
2176 else switch (pref_format.major_type)
2178 case WG_MAJOR_TYPE_AUDIO:
2179 if (format.u.audio.format == WG_AUDIO_FORMAT_UNKNOWN)
2180 hr = NS_E_AUDIO_CODEC_NOT_INSTALLED;
2181 else if (format.u.audio.channels > pref_format.u.audio.channels)
2182 hr = NS_E_AUDIO_CODEC_NOT_INSTALLED;
2183 break;
2185 case WG_MAJOR_TYPE_VIDEO:
2186 for (i = 0; i < ARRAY_SIZE(video_formats); ++i)
2187 if (format.u.video.format == video_formats[i])
2188 break;
2189 if (i == ARRAY_SIZE(video_formats))
2190 hr = NS_E_INVALID_OUTPUT_FORMAT;
2191 else if (pref_format.u.video.width != format.u.video.width)
2192 hr = NS_E_INVALID_OUTPUT_FORMAT;
2193 else if (pref_format.u.video.height != format.u.video.height)
2194 hr = NS_E_INVALID_OUTPUT_FORMAT;
2195 break;
2197 default:
2198 hr = NS_E_INCOMPATIBLE_FORMAT;
2199 break;
2202 if (FAILED(hr))
2204 WARN("Unsupported media type, returning %#lx.\n", hr);
2205 LeaveCriticalSection(&reader->cs);
2206 return hr;
2209 stream->format = format;
2210 wg_parser_stream_enable(stream->wg_stream, &format);
2212 /* Re-decode any buffers that might have been generated with the old format.
2214 * FIXME: Seeking in-place will cause some buffers to be dropped.
2215 * Unfortunately, we can't really store the last received PTS and seek there
2216 * either: since seeks are inexact and we aren't guaranteed to receive
2217 * samples in order, some buffers might be duplicated or dropped anyway.
2218 * In order to really seamlessly allow for format changes, we need
2219 * cooperation from each individual GStreamer stream, to be able to tell
2220 * upstream exactly which buffers they need resent...
2222 * In all likelihood this function is being called not mid-stream but rather
2223 * while setting the stream up, before consuming any events. Accordingly
2224 * let's just seek back to the beginning. */
2225 wg_parser_stream_seek(reader->streams[0].wg_stream, 1.0, reader->start_time, 0,
2226 AM_SEEKING_AbsolutePositioning, AM_SEEKING_NoPositioning);
2228 LeaveCriticalSection(&reader->cs);
2229 return S_OK;
2232 static HRESULT WINAPI reader_SetOutputSetting(IWMSyncReader2 *iface, DWORD output,
2233 const WCHAR *name, WMT_ATTR_DATATYPE type, const BYTE *value, WORD size)
2235 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2237 TRACE("reader %p, output %lu, name %s, type %#x, value %p, size %u.\n",
2238 reader, output, debugstr_w(name), type, value, size);
2240 if (!wcscmp(name, L"VideoSampleDurations"))
2242 FIXME("Ignoring VideoSampleDurations setting.\n");
2243 return S_OK;
2245 if (!wcscmp(name, L"EnableDiscreteOutput"))
2247 FIXME("Ignoring EnableDiscreteOutput setting.\n");
2248 return S_OK;
2250 if (!wcscmp(name, L"SpeakerConfig"))
2252 FIXME("Ignoring SpeakerConfig setting.\n");
2253 return S_OK;
2255 else
2257 FIXME("Unknown setting %s; returning E_NOTIMPL.\n", debugstr_w(name));
2258 return E_NOTIMPL;
2262 static HRESULT WINAPI reader_SetRange(IWMSyncReader2 *iface, QWORD start, LONGLONG duration)
2264 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2265 WORD i;
2267 TRACE("reader %p, start %I64u, duration %I64d.\n", reader, start, duration);
2269 EnterCriticalSection(&reader->cs);
2271 reader->start_time = start;
2273 wg_parser_stream_seek(reader->streams[0].wg_stream, 1.0, start, start + duration,
2274 AM_SEEKING_AbsolutePositioning, duration ? AM_SEEKING_AbsolutePositioning : AM_SEEKING_NoPositioning);
2276 for (i = 0; i < reader->stream_count; ++i)
2277 reader->streams[i].eos = false;
2279 LeaveCriticalSection(&reader->cs);
2280 return S_OK;
2283 static HRESULT WINAPI reader_SetRangeByFrame(IWMSyncReader2 *iface, WORD stream_num, QWORD frame_num,
2284 LONGLONG frames)
2286 struct wm_reader *This = impl_from_IWMSyncReader2(iface);
2287 FIXME("(%p)->(%d %s %s): stub!\n", This, stream_num, wine_dbgstr_longlong(frame_num), wine_dbgstr_longlong(frames));
2288 return E_NOTIMPL;
2291 static HRESULT WINAPI reader_SetReadStreamSamples(IWMSyncReader2 *iface, WORD stream_number, BOOL compressed)
2293 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2294 struct wm_stream *stream;
2296 TRACE("reader %p, stream_index %u, compressed %d.\n", reader, stream_number, compressed);
2298 EnterCriticalSection(&reader->cs);
2300 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
2302 LeaveCriticalSection(&reader->cs);
2303 return E_INVALIDARG;
2306 stream->read_compressed = compressed;
2308 LeaveCriticalSection(&reader->cs);
2309 return S_OK;
2312 static HRESULT WINAPI reader_SetStreamsSelected(IWMSyncReader2 *iface,
2313 WORD count, WORD *stream_numbers, WMT_STREAM_SELECTION *selections)
2315 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2316 struct wm_stream *stream;
2317 WORD i;
2319 TRACE("reader %p, count %u, stream_numbers %p, selections %p.\n",
2320 reader, count, stream_numbers, selections);
2322 if (!count)
2323 return E_INVALIDARG;
2325 EnterCriticalSection(&reader->cs);
2327 for (i = 0; i < count; ++i)
2329 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_numbers[i])))
2331 LeaveCriticalSection(&reader->cs);
2332 WARN("Invalid stream number %u; returning NS_E_INVALID_REQUEST.\n", stream_numbers[i]);
2333 return NS_E_INVALID_REQUEST;
2337 for (i = 0; i < count; ++i)
2339 stream = wm_reader_get_stream_by_stream_number(reader, stream_numbers[i]);
2340 stream->selection = selections[i];
2341 if (selections[i] == WMT_OFF)
2343 TRACE("Disabling stream %u.\n", stream_numbers[i]);
2344 wg_parser_stream_disable(stream->wg_stream);
2346 else if (selections[i] == WMT_ON)
2348 if (selections[i] != WMT_ON)
2349 FIXME("Ignoring selection %#x for stream %u; treating as enabled.\n",
2350 selections[i], stream_numbers[i]);
2351 TRACE("Enabling stream %u.\n", stream_numbers[i]);
2352 wg_parser_stream_enable(stream->wg_stream, &stream->format);
2356 LeaveCriticalSection(&reader->cs);
2357 return S_OK;
2360 static HRESULT WINAPI reader_SetRangeByTimecode(IWMSyncReader2 *iface, WORD stream_num,
2361 WMT_TIMECODE_EXTENSION_DATA *start, WMT_TIMECODE_EXTENSION_DATA *end)
2363 struct wm_reader *This = impl_from_IWMSyncReader2(iface);
2364 FIXME("(%p)->(%u %p %p): stub!\n", This, stream_num, start, end);
2365 return E_NOTIMPL;
2368 static HRESULT WINAPI reader_SetRangeByFrameEx(IWMSyncReader2 *iface, WORD stream_num, QWORD frame_num,
2369 LONGLONG frames_to_read, QWORD *starttime)
2371 struct wm_reader *This = impl_from_IWMSyncReader2(iface);
2372 FIXME("(%p)->(%u %s %s %p): stub!\n", This, stream_num, wine_dbgstr_longlong(frame_num),
2373 wine_dbgstr_longlong(frames_to_read), starttime);
2374 return E_NOTIMPL;
2377 static HRESULT WINAPI reader_SetAllocateForOutput(IWMSyncReader2 *iface, DWORD output, IWMReaderAllocatorEx *allocator)
2379 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2380 struct wm_stream *stream;
2382 TRACE("reader %p, output %lu, allocator %p.\n", reader, output, allocator);
2384 EnterCriticalSection(&reader->cs);
2386 if (!(stream = get_stream_by_output_number(reader, output)))
2388 LeaveCriticalSection(&reader->cs);
2389 return E_INVALIDARG;
2392 if (stream->output_allocator)
2393 IWMReaderAllocatorEx_Release(stream->output_allocator);
2394 if ((stream->output_allocator = allocator))
2395 IWMReaderAllocatorEx_AddRef(stream->output_allocator);
2397 LeaveCriticalSection(&reader->cs);
2398 return S_OK;
2401 static HRESULT WINAPI reader_GetAllocateForOutput(IWMSyncReader2 *iface, DWORD output, IWMReaderAllocatorEx **allocator)
2403 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2404 struct wm_stream *stream;
2406 TRACE("reader %p, output %lu, allocator %p.\n", reader, output, allocator);
2408 if (!allocator)
2409 return E_INVALIDARG;
2411 EnterCriticalSection(&reader->cs);
2413 if (!(stream = get_stream_by_output_number(reader, output)))
2415 LeaveCriticalSection(&reader->cs);
2416 return E_INVALIDARG;
2419 stream = reader->streams + output;
2420 if ((*allocator = stream->output_allocator))
2421 IWMReaderAllocatorEx_AddRef(*allocator);
2423 LeaveCriticalSection(&reader->cs);
2424 return S_OK;
2427 static HRESULT WINAPI reader_SetAllocateForStream(IWMSyncReader2 *iface, DWORD stream_number, IWMReaderAllocatorEx *allocator)
2429 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2430 struct wm_stream *stream;
2432 TRACE("reader %p, stream_number %lu, allocator %p.\n", reader, stream_number, allocator);
2434 EnterCriticalSection(&reader->cs);
2436 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
2438 LeaveCriticalSection(&reader->cs);
2439 return E_INVALIDARG;
2442 if (stream->stream_allocator)
2443 IWMReaderAllocatorEx_Release(stream->stream_allocator);
2444 if ((stream->stream_allocator = allocator))
2445 IWMReaderAllocatorEx_AddRef(stream->stream_allocator);
2447 LeaveCriticalSection(&reader->cs);
2448 return S_OK;
2451 static HRESULT WINAPI reader_GetAllocateForStream(IWMSyncReader2 *iface, DWORD stream_number, IWMReaderAllocatorEx **allocator)
2453 struct wm_reader *reader = impl_from_IWMSyncReader2(iface);
2454 struct wm_stream *stream;
2456 TRACE("reader %p, stream_number %lu, allocator %p.\n", reader, stream_number, allocator);
2458 if (!allocator)
2459 return E_INVALIDARG;
2461 EnterCriticalSection(&reader->cs);
2463 if (!(stream = wm_reader_get_stream_by_stream_number(reader, stream_number)))
2465 LeaveCriticalSection(&reader->cs);
2466 return E_INVALIDARG;
2469 if ((*allocator = stream->stream_allocator))
2470 IWMReaderAllocatorEx_AddRef(*allocator);
2472 LeaveCriticalSection(&reader->cs);
2473 return S_OK;
2476 static const IWMSyncReader2Vtbl reader_vtbl =
2478 reader_QueryInterface,
2479 reader_AddRef,
2480 reader_Release,
2481 reader_Open,
2482 reader_Close,
2483 reader_SetRange,
2484 reader_SetRangeByFrame,
2485 reader_GetNextSample,
2486 reader_SetStreamsSelected,
2487 reader_GetStreamSelected,
2488 reader_SetReadStreamSamples,
2489 reader_GetReadStreamSamples,
2490 reader_GetOutputSetting,
2491 reader_SetOutputSetting,
2492 reader_GetOutputCount,
2493 reader_GetOutputProps,
2494 reader_SetOutputProps,
2495 reader_GetOutputFormatCount,
2496 reader_GetOutputFormat,
2497 reader_GetOutputNumberForStream,
2498 reader_GetStreamNumberForOutput,
2499 reader_GetMaxOutputSampleSize,
2500 reader_GetMaxStreamSampleSize,
2501 reader_OpenStream,
2502 reader_SetRangeByTimecode,
2503 reader_SetRangeByFrameEx,
2504 reader_SetAllocateForOutput,
2505 reader_GetAllocateForOutput,
2506 reader_SetAllocateForStream,
2507 reader_GetAllocateForStream
2510 HRESULT WINAPI winegstreamer_create_wm_sync_reader(IUnknown *outer, void **out)
2512 struct wm_reader *object;
2514 TRACE("out %p.\n", out);
2516 if (!(object = calloc(1, sizeof(*object))))
2517 return E_OUTOFMEMORY;
2519 object->IUnknown_inner.lpVtbl = &unknown_inner_vtbl;
2520 object->IWMSyncReader2_iface.lpVtbl = &reader_vtbl;
2521 object->IWMHeaderInfo3_iface.lpVtbl = &header_info_vtbl;
2522 object->IWMLanguageList_iface.lpVtbl = &language_list_vtbl;
2523 object->IWMPacketSize2_iface.lpVtbl = &packet_size_vtbl;
2524 object->IWMProfile3_iface.lpVtbl = &profile_vtbl;
2525 object->IWMReaderPlaylistBurn_iface.lpVtbl = &playlist_vtbl;
2526 object->IWMReaderTimecode_iface.lpVtbl = &timecode_vtbl;
2527 object->outer = outer ? outer : &object->IUnknown_inner;
2528 object->refcount = 1;
2530 InitializeCriticalSection(&object->cs);
2531 object->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": reader.cs");
2533 TRACE("Created reader %p.\n", object);
2534 *out = outer ? (void *)&object->IUnknown_inner : (void *)&object->IWMSyncReader2_iface;
2535 return S_OK;