winegstreamer: Wait on error conditions when initializing GStreamer.
[wine.git] / dlls / winegstreamer / gstdemux.c
blob4a0a95c68f39f9efbf5880c4a5a85b6c156a7750
1 /*
2 * GStreamer splitter + decoder, adapted from parser.c
4 * Copyright 2010 Maarten Lankhorst for CodeWeavers
5 * Copyright 2010 Aric Stewart for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "gst_private.h"
24 #include "gst_guids.h"
25 #include "gst_cbs.h"
27 #include "vfwmsgs.h"
28 #include "amvideo.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 #include <assert.h>
35 #include "dvdmedia.h"
36 #include "mmreg.h"
37 #include "ks.h"
38 #include "initguid.h"
39 #include "wmcodecdsp.h"
40 #include "ksmedia.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
44 GST_DEBUG_CATEGORY_STATIC(wine);
45 #define GST_CAT_DEFAULT wine
47 static const GUID MEDIASUBTYPE_CVID = {mmioFOURCC('c','v','i','d'), 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
49 struct parser
51 struct strmbase_filter filter;
52 IAMStreamSelect IAMStreamSelect_iface;
54 struct strmbase_sink sink;
55 IAsyncReader *reader;
57 struct parser_source **sources;
58 unsigned int source_count;
59 BOOL enum_sink_first;
61 LONGLONG filesize;
63 BOOL initial, ignore_flush;
64 GstElement *container;
65 GstPad *my_src, *their_sink;
66 GstBus *bus;
67 guint64 start, nextofs, nextpullofs, stop;
68 HANDLE no_more_pads_event, duration_event, error_event;
70 HANDLE push_thread;
72 BOOL (*init_gst)(struct parser *filter);
73 HRESULT (*source_query_accept)(struct parser_source *pin, const AM_MEDIA_TYPE *mt);
74 HRESULT (*source_get_media_type)(struct parser_source *pin, unsigned int index, AM_MEDIA_TYPE *mt);
77 struct parser_source
79 struct strmbase_source pin;
80 IQualityControl IQualityControl_iface;
82 GstPad *their_src, *post_sink, *post_src, *my_sink;
83 GstElement *flip;
84 HANDLE caps_event, eos_event;
85 GstSegment *segment;
86 GstCaps *caps;
87 SourceSeeking seek;
90 static inline struct parser *impl_from_strmbase_filter(struct strmbase_filter *iface)
92 return CONTAINING_RECORD(iface, struct parser, filter);
95 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
96 static const IMediaSeekingVtbl GST_Seeking_Vtbl;
97 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl;
99 static struct parser_source *create_pin(struct parser *filter, const WCHAR *name);
100 static HRESULT GST_RemoveOutputPins(struct parser *This);
101 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface);
102 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface);
103 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface);
105 static gboolean amt_from_gst_audio_info(const GstAudioInfo *info, AM_MEDIA_TYPE *amt)
107 WAVEFORMATEXTENSIBLE *wfe;
108 WAVEFORMATEX *wfx;
109 gint32 depth, bpp;
111 wfe = CoTaskMemAlloc(sizeof(*wfe));
112 wfx = (WAVEFORMATEX*)wfe;
113 amt->majortype = MEDIATYPE_Audio;
114 amt->subtype = MEDIASUBTYPE_PCM;
115 amt->formattype = FORMAT_WaveFormatEx;
116 amt->pbFormat = (BYTE*)wfe;
117 amt->cbFormat = sizeof(*wfe);
118 amt->bFixedSizeSamples = TRUE;
119 amt->bTemporalCompression = FALSE;
120 amt->pUnk = NULL;
122 wfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
124 wfx->nChannels = info->channels;
125 wfx->nSamplesPerSec = info->rate;
126 depth = GST_AUDIO_INFO_WIDTH(info);
127 bpp = GST_AUDIO_INFO_DEPTH(info);
129 if (!depth || depth > 32 || depth % 8)
130 depth = bpp;
131 else if (!bpp)
132 bpp = depth;
133 wfe->Samples.wValidBitsPerSample = depth;
134 wfx->wBitsPerSample = bpp;
135 wfx->cbSize = sizeof(*wfe)-sizeof(*wfx);
136 switch (wfx->nChannels) {
137 case 1: wfe->dwChannelMask = KSAUDIO_SPEAKER_MONO; break;
138 case 2: wfe->dwChannelMask = KSAUDIO_SPEAKER_STEREO; break;
139 case 4: wfe->dwChannelMask = KSAUDIO_SPEAKER_SURROUND; break;
140 case 5: wfe->dwChannelMask = (KSAUDIO_SPEAKER_5POINT1 & ~SPEAKER_LOW_FREQUENCY); break;
141 case 6: wfe->dwChannelMask = KSAUDIO_SPEAKER_5POINT1; break;
142 case 8: wfe->dwChannelMask = KSAUDIO_SPEAKER_7POINT1; break;
143 default:
144 wfe->dwChannelMask = 0;
146 if (GST_AUDIO_INFO_IS_FLOAT(info))
148 amt->subtype = MEDIASUBTYPE_IEEE_FLOAT;
149 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
150 } else {
151 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
152 if (wfx->nChannels <= 2 && bpp <= 16 && depth == bpp) {
153 wfx->wFormatTag = WAVE_FORMAT_PCM;
154 wfx->cbSize = 0;
155 amt->cbFormat = sizeof(WAVEFORMATEX);
158 amt->lSampleSize = wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample/8;
159 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
160 return TRUE;
163 static gboolean amt_from_gst_video_info(const GstVideoInfo *info, AM_MEDIA_TYPE *amt)
165 VIDEOINFO *vih;
166 BITMAPINFOHEADER *bih;
167 gint32 width, height;
169 width = GST_VIDEO_INFO_WIDTH(info);
170 height = GST_VIDEO_INFO_HEIGHT(info);
172 vih = CoTaskMemAlloc(sizeof(*vih));
173 bih = &vih->bmiHeader;
175 amt->formattype = FORMAT_VideoInfo;
176 amt->pbFormat = (BYTE*)vih;
177 amt->cbFormat = sizeof(VIDEOINFOHEADER);
178 amt->bFixedSizeSamples = FALSE;
179 amt->bTemporalCompression = TRUE;
180 amt->lSampleSize = 1;
181 amt->pUnk = NULL;
182 ZeroMemory(vih, sizeof(*vih));
183 amt->majortype = MEDIATYPE_Video;
185 if (GST_VIDEO_INFO_IS_RGB(info))
187 bih->biCompression = BI_RGB;
188 switch (GST_VIDEO_INFO_FORMAT(info))
190 case GST_VIDEO_FORMAT_BGRA:
191 amt->subtype = MEDIASUBTYPE_ARGB32;
192 bih->biBitCount = 32;
193 break;
194 case GST_VIDEO_FORMAT_BGRx:
195 amt->subtype = MEDIASUBTYPE_RGB32;
196 bih->biBitCount = 32;
197 break;
198 case GST_VIDEO_FORMAT_BGR:
199 amt->subtype = MEDIASUBTYPE_RGB24;
200 bih->biBitCount = 24;
201 break;
202 case GST_VIDEO_FORMAT_RGB16:
203 amt->subtype = MEDIASUBTYPE_RGB565;
204 amt->cbFormat = offsetof(VIDEOINFO, u.dwBitMasks[3]);
205 vih->u.dwBitMasks[iRED] = 0xf800;
206 vih->u.dwBitMasks[iGREEN] = 0x07e0;
207 vih->u.dwBitMasks[iBLUE] = 0x001f;
208 bih->biBitCount = 16;
209 bih->biCompression = BI_BITFIELDS;
210 break;
211 case GST_VIDEO_FORMAT_RGB15:
212 amt->subtype = MEDIASUBTYPE_RGB555;
213 bih->biBitCount = 16;
214 break;
215 default:
216 WARN("Cannot convert %s to a DirectShow type.\n", GST_VIDEO_INFO_NAME(info));
217 CoTaskMemFree(vih);
218 return FALSE;
220 } else {
221 amt->subtype = MEDIATYPE_Video;
222 if (!(amt->subtype.Data1 = gst_video_format_to_fourcc(GST_VIDEO_INFO_FORMAT(info))))
224 CoTaskMemFree(vih);
225 return FALSE;
227 switch (amt->subtype.Data1) {
228 case mmioFOURCC('I','4','2','0'):
229 case mmioFOURCC('Y','V','1','2'):
230 case mmioFOURCC('N','V','1','2'):
231 case mmioFOURCC('N','V','2','1'):
232 bih->biBitCount = 12; break;
233 case mmioFOURCC('Y','U','Y','2'):
234 case mmioFOURCC('Y','V','Y','U'):
235 case mmioFOURCC('U','Y','V','Y'):
236 bih->biBitCount = 16; break;
238 bih->biCompression = amt->subtype.Data1;
240 bih->biSizeImage = GST_VIDEO_INFO_SIZE(info);
241 if ((vih->AvgTimePerFrame = (REFERENCE_TIME)MulDiv(10000000,
242 GST_VIDEO_INFO_FPS_D(info), GST_VIDEO_INFO_FPS_N(info))) == -1)
243 vih->AvgTimePerFrame = 0; /* zero division or integer overflow */
244 bih->biSize = sizeof(*bih);
245 bih->biWidth = width;
246 bih->biHeight = height;
247 bih->biPlanes = 1;
248 return TRUE;
251 static gboolean amt_from_gst_caps_audio_mpeg(const GstCaps *caps, AM_MEDIA_TYPE *mt)
253 GstStructure *structure = gst_caps_get_structure(caps, 0);
254 gint layer, channels, rate;
256 mt->majortype = MEDIATYPE_Audio;
257 mt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
258 mt->bFixedSizeSamples = FALSE;
259 mt->bTemporalCompression = FALSE;
260 mt->lSampleSize = 0;
261 mt->formattype = FORMAT_WaveFormatEx;
262 mt->pUnk = NULL;
264 if (!gst_structure_get_int(structure, "layer", &layer))
266 WARN("Missing 'layer' value.\n");
267 return FALSE;
269 if (!gst_structure_get_int(structure, "channels", &channels))
271 WARN("Missing 'channels' value.\n");
272 return FALSE;
274 if (!gst_structure_get_int(structure, "rate", &rate))
276 WARN("Missing 'rate' value.\n");
277 return FALSE;
280 if (layer == 3)
282 MPEGLAYER3WAVEFORMAT *wfx = CoTaskMemAlloc(sizeof(*wfx));
283 memset(wfx, 0, sizeof(*wfx));
285 mt->subtype.Data1 = WAVE_FORMAT_MPEGLAYER3;
286 mt->cbFormat = sizeof(*wfx);
287 mt->pbFormat = (BYTE *)wfx;
288 wfx->wfx.wFormatTag = WAVE_FORMAT_MPEGLAYER3;
289 wfx->wfx.nChannels = channels;
290 wfx->wfx.nSamplesPerSec = rate;
291 /* FIXME: We can't get most of the MPEG data from the caps. We may have
292 * to manually parse the header. */
293 wfx->wfx.cbSize = sizeof(*wfx) - sizeof(WAVEFORMATEX);
294 wfx->wID = MPEGLAYER3_ID_MPEG;
295 wfx->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
296 wfx->nFramesPerBlock = 1;
297 wfx->nCodecDelay = 1393;
299 else
301 MPEG1WAVEFORMAT *wfx = CoTaskMemAlloc(sizeof(*wfx));
302 memset(wfx, 0, sizeof(*wfx));
304 mt->subtype.Data1 = WAVE_FORMAT_MPEG;
305 mt->cbFormat = sizeof(*wfx);
306 mt->pbFormat = (BYTE *)wfx;
307 wfx->wfx.wFormatTag = WAVE_FORMAT_MPEG;
308 wfx->wfx.nChannels = channels;
309 wfx->wfx.nSamplesPerSec = rate;
310 wfx->wfx.cbSize = sizeof(*wfx) - sizeof(WAVEFORMATEX);
311 wfx->fwHeadLayer = layer;
314 return TRUE;
317 static gboolean amt_from_gst_caps(const GstCaps *caps, AM_MEDIA_TYPE *mt)
319 const char *type = gst_structure_get_name(gst_caps_get_structure(caps, 0));
320 GstStructure *structure = gst_caps_get_structure(caps, 0);
322 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
324 if (!strcmp(type, "audio/x-raw"))
326 GstAudioInfo info;
328 if (!(gst_audio_info_from_caps(&info, caps)))
329 return FALSE;
330 return amt_from_gst_audio_info(&info, mt);
332 else if (!strcmp(type, "video/x-raw"))
334 GstVideoInfo info;
336 if (!gst_video_info_from_caps(&info, caps))
337 return FALSE;
338 return amt_from_gst_video_info(&info, mt);
340 else if (!strcmp(type, "audio/mpeg"))
341 return amt_from_gst_caps_audio_mpeg(caps, mt);
342 else if (!strcmp(type, "video/x-cinepak"))
344 VIDEOINFOHEADER *vih;
345 gint i;
347 mt->majortype = MEDIATYPE_Video;
348 mt->subtype = MEDIASUBTYPE_CVID;
349 mt->bTemporalCompression = TRUE;
350 mt->lSampleSize = 1;
351 mt->formattype = FORMAT_VideoInfo;
352 if (!(vih = CoTaskMemAlloc(sizeof(VIDEOINFOHEADER))))
353 return FALSE;
354 mt->cbFormat = sizeof(VIDEOINFOHEADER);
355 mt->pbFormat = (BYTE *)vih;
357 memset(vih, 0, sizeof(VIDEOINFOHEADER));
358 vih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
359 if (gst_structure_get_int(structure, "width", &i))
360 vih->bmiHeader.biWidth = i;
361 if (gst_structure_get_int(structure, "height", &i))
362 vih->bmiHeader.biHeight = i;
363 vih->bmiHeader.biPlanes = 1;
364 /* Both ffmpeg's encoder and a Cinepak file seen in the wild report
365 * 24 bpp. ffmpeg sets biSizeImage as below; others may be smaller, but
366 * as long as every sample fits into our allocator, we're fine. */
367 vih->bmiHeader.biBitCount = 24;
368 vih->bmiHeader.biCompression = mmioFOURCC('c','v','i','d');
369 vih->bmiHeader.biSizeImage = vih->bmiHeader.biWidth
370 * vih->bmiHeader.biHeight * vih->bmiHeader.biBitCount / 8;
371 return TRUE;
373 else
375 FIXME("Unhandled type %s.\n", debugstr_a(type));
376 return FALSE;
380 static GstCaps *amt_to_gst_caps_video(const AM_MEDIA_TYPE *mt)
382 static const struct
384 const GUID *subtype;
385 GstVideoFormat format;
387 format_map[] =
389 {&MEDIASUBTYPE_ARGB32, GST_VIDEO_FORMAT_BGRA},
390 {&MEDIASUBTYPE_RGB32, GST_VIDEO_FORMAT_BGRx},
391 {&MEDIASUBTYPE_RGB24, GST_VIDEO_FORMAT_BGR},
392 {&MEDIASUBTYPE_RGB565, GST_VIDEO_FORMAT_RGB16},
393 {&MEDIASUBTYPE_RGB555, GST_VIDEO_FORMAT_RGB15},
396 const VIDEOINFOHEADER *vih = (VIDEOINFOHEADER *)mt->pbFormat;
397 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
398 GstVideoInfo info;
399 unsigned int i;
400 GstCaps *caps;
402 if (!IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo)
403 || mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
404 return NULL;
406 for (i = 0; i < ARRAY_SIZE(format_map); ++i)
408 if (IsEqualGUID(&mt->subtype, format_map[i].subtype))
410 format = format_map[i].format;
411 break;
415 if (format == GST_VIDEO_FORMAT_UNKNOWN)
416 format = gst_video_format_from_fourcc(vih->bmiHeader.biCompression);
418 if (format == GST_VIDEO_FORMAT_UNKNOWN)
420 FIXME("Unknown video format (subtype %s, compression %#x).\n",
421 debugstr_guid(&mt->subtype), vih->bmiHeader.biCompression);
422 return NULL;
425 gst_video_info_set_format(&info, format, vih->bmiHeader.biWidth, vih->bmiHeader.biHeight);
426 if ((caps = gst_video_info_to_caps(&info)))
428 /* Clear some fields that shouldn't prevent us from connecting. */
429 for (i = 0; i < gst_caps_get_size(caps); ++i)
431 gst_structure_remove_fields(gst_caps_get_structure(caps, i),
432 "framerate", "pixel-aspect-ratio", "colorimetry", "chroma-site", NULL);
435 return caps;
438 static GstCaps *amt_to_gst_caps_audio(const AM_MEDIA_TYPE *mt)
440 const WAVEFORMATEX *wfx = (WAVEFORMATEX *)mt->pbFormat;
441 GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN;
442 GstAudioInfo info;
444 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx)
445 || mt->cbFormat < sizeof(WAVEFORMATEX) || !mt->pbFormat)
446 return NULL;
448 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_PCM))
449 format = gst_audio_format_build_integer(wfx->wBitsPerSample != 8,
450 G_LITTLE_ENDIAN, wfx->wBitsPerSample, wfx->wBitsPerSample);
451 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_IEEE_FLOAT))
453 if (wfx->wBitsPerSample == 32)
454 format = GST_AUDIO_FORMAT_F32LE;
455 else if (wfx->wBitsPerSample == 64)
456 format = GST_AUDIO_FORMAT_F64LE;
459 if (format == GST_AUDIO_FORMAT_UNKNOWN)
461 FIXME("Unknown audio format (subtype %s, depth %u).\n",
462 debugstr_guid(&mt->subtype), wfx->wBitsPerSample);
463 return NULL;
466 gst_audio_info_set_format(&info, format, wfx->nSamplesPerSec, wfx->nChannels, NULL);
467 return gst_audio_info_to_caps(&info);
470 static GstCaps *amt_to_gst_caps(const AM_MEDIA_TYPE *mt)
472 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Video))
473 return amt_to_gst_caps_video(mt);
474 else if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Audio))
475 return amt_to_gst_caps_audio(mt);
477 FIXME("Unknown major type %s.\n", debugstr_guid(&mt->majortype));
478 return NULL;
481 static gboolean query_sink(GstPad *pad, GstObject *parent, GstQuery *query)
483 struct parser_source *pin = gst_pad_get_element_private(pad);
485 TRACE("pin %p, type \"%s\".\n", pin, gst_query_type_get_name(query->type));
487 switch (query->type)
489 case GST_QUERY_CAPS:
491 GstCaps *caps, *filter, *temp;
493 gst_query_parse_caps(query, &filter);
495 if (pin->pin.pin.peer)
496 caps = amt_to_gst_caps(&pin->pin.pin.mt);
497 else
498 caps = gst_caps_new_any();
499 if (!caps)
500 return FALSE;
502 if (filter)
504 temp = gst_caps_intersect(caps, filter);
505 gst_caps_unref(caps);
506 caps = temp;
509 gst_query_set_caps_result(query, caps);
510 gst_caps_unref(caps);
511 return TRUE;
513 case GST_QUERY_ACCEPT_CAPS:
515 gboolean ret = TRUE;
516 AM_MEDIA_TYPE mt;
517 GstCaps *caps;
519 if (!pin->pin.pin.peer)
521 gst_query_set_accept_caps_result(query, TRUE);
522 return TRUE;
525 gst_query_parse_accept_caps(query, &caps);
526 if (!amt_from_gst_caps(caps, &mt))
527 return FALSE;
529 if (!IsEqualGUID(&mt.majortype, &pin->pin.pin.mt.majortype)
530 || !IsEqualGUID(&mt.subtype, &pin->pin.pin.mt.subtype)
531 || !IsEqualGUID(&mt.formattype, &pin->pin.pin.mt.formattype))
532 ret = FALSE;
534 if (IsEqualGUID(&mt.majortype, &MEDIATYPE_Video))
536 const VIDEOINFOHEADER *req_vih = (VIDEOINFOHEADER *)mt.pbFormat;
537 const VIDEOINFOHEADER *our_vih = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
539 if (req_vih->bmiHeader.biWidth != our_vih->bmiHeader.biWidth
540 || req_vih->bmiHeader.biHeight != our_vih->bmiHeader.biHeight
541 || req_vih->bmiHeader.biBitCount != our_vih->bmiHeader.biBitCount
542 || req_vih->bmiHeader.biCompression != our_vih->bmiHeader.biCompression)
543 ret = FALSE;
545 else if (IsEqualGUID(&mt.majortype, &MEDIATYPE_Audio))
547 const WAVEFORMATEX *req_wfx = (WAVEFORMATEX *)mt.pbFormat;
548 const WAVEFORMATEX *our_wfx = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
550 if (req_wfx->nChannels != our_wfx->nChannels
551 || req_wfx->nSamplesPerSec != our_wfx->nSamplesPerSec
552 || req_wfx->wBitsPerSample != our_wfx->wBitsPerSample)
553 ret = FALSE;
556 FreeMediaType(&mt);
558 if (!ret && WARN_ON(gstreamer))
560 gchar *str = gst_caps_to_string(caps);
561 WARN("Rejecting caps \"%s\".\n", debugstr_a(str));
562 g_free(str);
565 gst_query_set_accept_caps_result(query, ret);
566 return TRUE;
568 default:
569 return gst_pad_query_default (pad, parent, query);
573 static gboolean gst_base_src_perform_seek(struct parser *This, GstEvent *event)
575 gboolean res = TRUE;
576 gdouble rate;
577 GstFormat seek_format;
578 GstSeekFlags flags;
579 GstSeekType cur_type, stop_type;
580 gint64 cur, stop;
581 gboolean flush;
582 guint32 seqnum;
583 GstEvent *tevent;
584 BOOL thread = !!This->push_thread;
586 gst_event_parse_seek(event, &rate, &seek_format, &flags,
587 &cur_type, &cur, &stop_type, &stop);
589 if (seek_format != GST_FORMAT_BYTES)
591 FIXME("Unhandled format \"%s\".\n", gst_format_get_name(seek_format));
592 return FALSE;
595 flush = flags & GST_SEEK_FLAG_FLUSH;
596 seqnum = gst_event_get_seqnum(event);
598 /* send flush start */
599 if (flush) {
600 tevent = gst_event_new_flush_start();
601 gst_event_set_seqnum(tevent, seqnum);
602 gst_pad_push_event(This->my_src, tevent);
603 if (This->reader)
604 IAsyncReader_BeginFlush(This->reader);
605 if (thread)
606 gst_pad_set_active(This->my_src, 1);
609 This->nextofs = This->start = cur;
611 /* and prepare to continue streaming */
612 if (flush) {
613 tevent = gst_event_new_flush_stop(TRUE);
614 gst_event_set_seqnum(tevent, seqnum);
615 gst_pad_push_event(This->my_src, tevent);
616 if (This->reader)
617 IAsyncReader_EndFlush(This->reader);
618 if (thread)
619 gst_pad_set_active(This->my_src, 1);
622 return res;
625 static gboolean event_src(GstPad *pad, GstObject *parent, GstEvent *event)
627 struct parser *This = gst_pad_get_element_private(pad);
628 gboolean ret = TRUE;
630 TRACE("filter %p, type \"%s\".\n", This, GST_EVENT_TYPE_NAME(event));
632 switch (event->type) {
633 case GST_EVENT_SEEK:
634 ret = gst_base_src_perform_seek(This, event);
635 break;
636 case GST_EVENT_FLUSH_START:
637 EnterCriticalSection(&This->filter.filter_cs);
638 if (This->reader)
639 IAsyncReader_BeginFlush(This->reader);
640 LeaveCriticalSection(&This->filter.filter_cs);
641 break;
642 case GST_EVENT_FLUSH_STOP:
643 EnterCriticalSection(&This->filter.filter_cs);
644 if (This->reader)
645 IAsyncReader_EndFlush(This->reader);
646 LeaveCriticalSection(&This->filter.filter_cs);
647 break;
648 case GST_EVENT_QOS:
649 case GST_EVENT_RECONFIGURE:
650 break;
651 default:
652 WARN("Ignoring \"%s\" event.\n", GST_EVENT_TYPE_NAME(event));
653 ret = FALSE;
654 break;
656 gst_event_unref(event);
657 return ret;
660 static gboolean event_sink(GstPad *pad, GstObject *parent, GstEvent *event)
662 struct parser_source *pin = gst_pad_get_element_private(pad);
664 TRACE("pin %p, type \"%s\".\n", pin, GST_EVENT_TYPE_NAME(event));
666 switch (event->type) {
667 case GST_EVENT_SEGMENT: {
668 gdouble rate, applied_rate;
669 gint64 stop, pos;
670 const GstSegment *segment;
672 gst_event_parse_segment(event, &segment);
674 pos = segment->position;
675 stop = segment->stop;
676 rate = segment->rate;
677 applied_rate = segment->applied_rate;
679 if (segment->format != GST_FORMAT_TIME)
681 FIXME("Unhandled format \"%s\".\n", gst_format_get_name(segment->format));
682 break;
685 gst_segment_copy_into(segment, pin->segment);
687 pos /= 100;
689 if (stop > 0)
690 stop /= 100;
692 if (pin->pin.pin.peer)
693 IPin_NewSegment(pin->pin.pin.peer, pos, stop, rate*applied_rate);
695 break;
697 case GST_EVENT_EOS:
698 if (pin->pin.pin.peer)
699 IPin_EndOfStream(pin->pin.pin.peer);
700 else
701 SetEvent(pin->eos_event);
702 break;
703 case GST_EVENT_FLUSH_START:
704 if (impl_from_strmbase_filter(pin->pin.pin.filter)->ignore_flush) {
705 /* gst-plugins-base prior to 1.7 contains a bug which causes
706 * our sink pins to receive a flush-start event when the
707 * decodebin changes from PAUSED to READY (including
708 * PLAYING->PAUSED->READY), but no matching flush-stop event is
709 * sent. See <gst-plugins-base.git:60bad4815db966a8e4). Here we
710 * unset the flushing flag to avoid the problem. */
711 TRACE("Working around gst <1.7 bug, ignoring FLUSH_START\n");
712 GST_PAD_UNSET_FLUSHING (pad);
713 break;
715 if (pin->pin.pin.peer)
716 IPin_BeginFlush(pin->pin.pin.peer);
717 break;
718 case GST_EVENT_FLUSH_STOP:
719 gst_segment_init(pin->segment, GST_FORMAT_TIME);
720 if (pin->pin.pin.peer)
721 IPin_EndFlush(pin->pin.pin.peer);
722 break;
723 case GST_EVENT_CAPS:
725 GstCaps *caps;
727 gst_event_parse_caps(event, &caps);
728 gst_caps_replace(&pin->caps, caps);
729 SetEvent(pin->caps_event);
730 break;
732 default:
733 WARN("Ignoring \"%s\" event.\n", GST_EVENT_TYPE_NAME(event));
735 gst_event_unref(event);
736 return TRUE;
739 static DWORD CALLBACK push_data(LPVOID iface)
741 LONGLONG maxlen, curlen;
742 struct parser *This = iface;
743 GstMapInfo mapping;
744 GstBuffer *buffer;
745 HRESULT hr;
747 if (!(buffer = gst_buffer_new_allocate(NULL, 16384, NULL)))
749 ERR("Failed to allocate memory.\n");
750 return 0;
753 IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
755 if (!This->stop)
756 IAsyncReader_Length(This->reader, &maxlen, &curlen);
757 else
758 maxlen = This->stop;
760 TRACE("Starting..\n");
761 for (;;) {
762 ULONG len;
763 int ret;
765 if (This->nextofs >= maxlen)
766 break;
767 len = min(16384, maxlen - This->nextofs);
769 if (!gst_buffer_map_range(buffer, -1, len, &mapping, GST_MAP_WRITE))
771 ERR("Failed to map buffer.\n");
772 break;
774 hr = IAsyncReader_SyncRead(This->reader, This->nextofs, len, mapping.data);
775 gst_buffer_unmap(buffer, &mapping);
776 if (hr != S_OK)
778 ERR("Failed to read data, hr %#x.\n", hr);
779 break;
782 This->nextofs += len;
784 buffer->duration = buffer->pts = -1;
785 ret = gst_pad_push(This->my_src, buffer);
786 if (ret >= 0)
787 hr = S_OK;
788 else
789 ERR("Sending returned: %i\n", ret);
790 if (ret == GST_FLOW_ERROR)
791 hr = E_FAIL;
792 else if (ret == GST_FLOW_FLUSHING)
793 hr = VFW_E_WRONG_STATE;
794 if (hr != S_OK)
795 break;
798 gst_buffer_unref(buffer);
800 gst_pad_push_event(This->my_src, gst_event_new_eos());
802 TRACE("Stopping.. %08x\n", hr);
804 IBaseFilter_Release(&This->filter.IBaseFilter_iface);
806 return 0;
809 static HRESULT send_sample(struct parser_source *pin, IMediaSample *sample,
810 GstBuffer *buf, GstMapInfo *info, gsize offset, gsize size, DWORD bytes_per_second)
812 HRESULT hr;
813 BYTE *ptr = NULL;
815 hr = IMediaSample_SetActualDataLength(sample, size);
816 if(FAILED(hr)){
817 WARN("SetActualDataLength failed: %08x\n", hr);
818 return hr;
821 IMediaSample_GetPointer(sample, &ptr);
823 memcpy(ptr, &info->data[offset], size);
825 if (GST_BUFFER_PTS_IS_VALID(buf)) {
826 REFERENCE_TIME rtStart;
827 GstClockTime ptsStart = buf->pts;
828 if (offset > 0)
829 ptsStart = buf->pts + gst_util_uint64_scale(offset, GST_SECOND, bytes_per_second);
830 rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, ptsStart);
831 if (rtStart >= 0)
832 rtStart /= 100;
834 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
835 REFERENCE_TIME rtStop;
836 REFERENCE_TIME tStart;
837 REFERENCE_TIME tStop;
838 GstClockTime ptsStop = buf->pts + buf->duration;
839 if (offset + size < info->size)
840 ptsStop = buf->pts + gst_util_uint64_scale(offset + size, GST_SECOND, bytes_per_second);
841 tStart = ptsStart / 100;
842 tStop = ptsStop / 100;
843 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, ptsStop);
844 if (rtStop >= 0)
845 rtStop /= 100;
846 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
847 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
848 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
849 } else {
850 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
851 IMediaSample_SetMediaTime(sample, NULL, NULL);
853 } else {
854 IMediaSample_SetTime(sample, NULL, NULL);
855 IMediaSample_SetMediaTime(sample, NULL, NULL);
858 IMediaSample_SetDiscontinuity(sample, !offset && GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
859 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_LIVE));
860 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
862 if (!pin->pin.pin.peer)
863 hr = VFW_E_NOT_CONNECTED;
864 else
865 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
867 TRACE("sending sample returned: %08x\n", hr);
869 return hr;
872 static GstFlowReturn got_data_sink(GstPad *pad, GstObject *parent, GstBuffer *buf)
874 struct parser_source *pin = gst_pad_get_element_private(pad);
875 struct parser *This = impl_from_strmbase_filter(pin->pin.pin.filter);
876 HRESULT hr = S_OK;
877 IMediaSample *sample;
878 GstMapInfo info;
880 TRACE("%p %p\n", pad, buf);
882 if (This->initial) {
883 gst_buffer_unref(buf);
884 return GST_FLOW_OK;
887 gst_buffer_map(buf, &info, GST_MAP_READ);
889 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
890 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
891 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
893 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
894 gsize offset = 0;
895 while (offset < info.size)
897 gsize advance;
899 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
901 if (FAILED(hr))
903 if (hr != VFW_E_NOT_CONNECTED)
904 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
905 break;
908 advance = min(IMediaSample_GetSize(sample), info.size - offset);
910 hr = send_sample(pin, sample, buf, &info, offset, advance, format->nAvgBytesPerSec);
912 IMediaSample_Release(sample);
914 if (FAILED(hr))
915 break;
917 offset += advance;
920 else
922 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
924 if (FAILED(hr))
926 if (hr != VFW_E_NOT_CONNECTED)
927 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
929 else
931 hr = send_sample(pin, sample, buf, &info, 0, info.size, 0);
933 IMediaSample_Release(sample);
937 gst_buffer_unmap(buf, &info);
939 gst_buffer_unref(buf);
941 if (hr == VFW_E_NOT_CONNECTED)
942 return GST_FLOW_NOT_LINKED;
944 if (FAILED(hr))
945 return GST_FLOW_FLUSHING;
947 return GST_FLOW_OK;
950 static GstFlowReturn request_buffer_src(GstPad *pad, GstObject *parent, guint64 ofs, guint len, GstBuffer **buffer)
952 struct parser *This = gst_pad_get_element_private(pad);
953 GstBuffer *new_buffer = NULL;
954 HRESULT hr;
955 GstMapInfo info;
957 TRACE("pad %p, offset %s, length %u, buffer %p.\n", pad, wine_dbgstr_longlong(ofs), len, *buffer);
959 if (ofs == GST_BUFFER_OFFSET_NONE)
960 ofs = This->nextpullofs;
961 if (ofs >= This->filesize) {
962 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
963 return GST_FLOW_EOS;
965 if (len + ofs > This->filesize)
966 len = This->filesize - ofs;
967 This->nextpullofs = ofs + len;
969 if (!*buffer)
970 *buffer = new_buffer = gst_buffer_new_and_alloc(len);
971 gst_buffer_map(*buffer, &info, GST_MAP_WRITE);
972 hr = IAsyncReader_SyncRead(This->reader, ofs, len, info.data);
973 gst_buffer_unmap(*buffer, &info);
974 if (FAILED(hr))
976 ERR("Failed to read data, hr %#x.\n", hr);
977 if (new_buffer)
978 gst_buffer_unref(new_buffer);
979 return GST_FLOW_ERROR;
982 return GST_FLOW_OK;
985 static DWORD CALLBACK push_data_init(LPVOID iface)
987 struct parser *This = iface;
988 DWORD64 ofs = 0;
990 TRACE("Starting..\n");
991 for (;;) {
992 GstBuffer *buf;
993 GstFlowReturn ret = request_buffer_src(This->my_src, NULL, ofs, 4096, &buf);
994 if (ret < 0) {
995 ERR("Obtaining buffer returned: %i\n", ret);
996 break;
998 ret = gst_pad_push(This->my_src, buf);
999 ofs += 4096;
1000 if (ret)
1001 TRACE("Sending returned: %i\n", ret);
1002 if (ret < 0)
1003 break;
1005 TRACE("Stopping..\n");
1006 return 0;
1009 static void removed_decoded_pad(GstElement *bin, GstPad *pad, gpointer user)
1011 struct parser *filter = user;
1012 unsigned int i;
1013 char *name;
1015 TRACE("filter %p, bin %p, pad %p.\n", filter, bin, pad);
1017 for (i = 0; i < filter->source_count; ++i)
1019 struct parser_source *pin = filter->sources[i];
1021 if (pin->their_src == pad)
1023 if (pin->post_sink)
1024 gst_pad_unlink(pin->their_src, pin->post_sink);
1025 else
1026 gst_pad_unlink(pin->their_src, pin->my_sink);
1027 gst_object_unref(pin->their_src);
1028 pin->their_src = NULL;
1029 return;
1033 name = gst_pad_get_name(pad);
1034 WARN("No pin matching pad %s found.\n", debugstr_a(name));
1035 g_free(name);
1038 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, struct parser *This)
1040 static const WCHAR formatW[] = {'S','t','r','e','a','m',' ','%','0','2','u',0};
1041 const char *typename;
1042 char *name;
1043 GstCaps *caps;
1044 GstStructure *arg;
1045 struct parser_source *pin;
1046 int ret;
1047 WCHAR nameW[128];
1049 TRACE("%p %p %p\n", This, bin, pad);
1051 sprintfW(nameW, formatW, This->source_count);
1053 name = gst_pad_get_name(pad);
1054 TRACE("Name: %s\n", name);
1055 g_free(name);
1057 caps = gst_pad_query_caps(pad, NULL);
1058 caps = gst_caps_make_writable(caps);
1059 arg = gst_caps_get_structure(caps, 0);
1060 typename = gst_structure_get_name(arg);
1062 if (!(pin = create_pin(This, nameW)))
1064 ERR("Failed to allocate memory.\n");
1065 goto out;
1068 if (!strcmp(typename, "video/x-raw"))
1070 GstElement *deinterlace, *vconv, *flip, *vconv2;
1072 /* DirectShow can express interlaced video, but downstream filters can't
1073 * necessarily consume it. In particular, the video renderer can't. */
1074 if (!(deinterlace = gst_element_factory_make("deinterlace", NULL)))
1076 ERR("Failed to create deinterlace, are %u-bit GStreamer \"good\" plugins installed?\n",
1077 8 * (int)sizeof(void *));
1078 goto out;
1081 /* decodebin considers many YUV formats to be "raw", but some quartz
1082 * filters can't handle those. Also, videoflip can't handle all "raw"
1083 * formats either. Add a videoconvert to swap color spaces. */
1084 if (!(vconv = gst_element_factory_make("videoconvert", NULL)))
1086 ERR("Failed to create videoconvert, are %u-bit GStreamer \"base\" plugins installed?\n",
1087 8 * (int)sizeof(void *));
1088 goto out;
1091 /* GStreamer outputs RGB video top-down, but DirectShow expects bottom-up. */
1092 if (!(flip = gst_element_factory_make("videoflip", NULL)))
1094 ERR("Failed to create videoflip, are %u-bit GStreamer \"good\" plugins installed?\n",
1095 8 * (int)sizeof(void *));
1096 goto out;
1099 /* videoflip does not support 15 and 16-bit RGB so add a second videoconvert
1100 * to do the final conversion. */
1101 if (!(vconv2 = gst_element_factory_make("videoconvert", NULL)))
1103 ERR("Failed to create videoconvert, are %u-bit GStreamer \"base\" plugins installed?\n",
1104 8 * (int)sizeof(void *));
1105 goto out;
1108 /* The bin takes ownership of these elements. */
1109 gst_bin_add(GST_BIN(This->container), deinterlace);
1110 gst_element_sync_state_with_parent(deinterlace);
1111 gst_bin_add(GST_BIN(This->container), vconv);
1112 gst_element_sync_state_with_parent(vconv);
1113 gst_bin_add(GST_BIN(This->container), flip);
1114 gst_element_sync_state_with_parent(flip);
1115 gst_bin_add(GST_BIN(This->container), vconv2);
1116 gst_element_sync_state_with_parent(vconv2);
1118 gst_element_link(deinterlace, vconv);
1119 gst_element_link(vconv, flip);
1120 gst_element_link(flip, vconv2);
1122 pin->post_sink = gst_element_get_static_pad(deinterlace, "sink");
1123 pin->post_src = gst_element_get_static_pad(vconv2, "src");
1124 pin->flip = flip;
1126 else if (!strcmp(typename, "audio/x-raw"))
1128 GstElement *convert;
1130 /* Currently our dsound can't handle 64-bit formats or all
1131 * surround-sound configurations. Native dsound can't always handle
1132 * 64-bit formats either. Add an audioconvert to allow changing bit
1133 * depth and channel count. */
1134 if (!(convert = gst_element_factory_make("audioconvert", NULL)))
1136 ERR("Failed to create audioconvert, are %u-bit GStreamer \"base\" plugins installed?\n",
1137 8 * (int)sizeof(void *));
1138 goto out;
1141 gst_bin_add(GST_BIN(This->container), convert);
1142 gst_element_sync_state_with_parent(convert);
1144 pin->post_sink = gst_element_get_static_pad(convert, "sink");
1145 pin->post_src = gst_element_get_static_pad(convert, "src");
1148 if (pin->post_sink)
1150 if ((ret = gst_pad_link(pad, pin->post_sink)) < 0)
1152 ERR("Failed to link decodebin source pad to post-processing elements, error %s.\n",
1153 gst_pad_link_get_name(ret));
1154 gst_object_unref(pin->post_sink);
1155 pin->post_sink = NULL;
1156 goto out;
1159 if ((ret = gst_pad_link(pin->post_src, pin->my_sink)) < 0)
1161 ERR("Failed to link post-processing elements to our sink pad, error %s.\n",
1162 gst_pad_link_get_name(ret));
1163 gst_object_unref(pin->post_src);
1164 pin->post_src = NULL;
1165 gst_object_unref(pin->post_sink);
1166 pin->post_sink = NULL;
1167 goto out;
1170 else if ((ret = gst_pad_link(pad, pin->my_sink)) < 0)
1172 ERR("Failed to link decodebin source pad to our sink pad, error %s.\n",
1173 gst_pad_link_get_name(ret));
1174 goto out;
1177 gst_pad_set_active(pin->my_sink, 1);
1178 gst_object_ref(pin->their_src = pad);
1179 out:
1180 gst_caps_unref(caps);
1183 static void existing_new_pad(GstElement *bin, GstPad *pad, gpointer user)
1185 struct parser *This = user;
1186 unsigned int i;
1187 int ret;
1189 TRACE("%p %p %p\n", This, bin, pad);
1191 if (gst_pad_is_linked(pad))
1192 return;
1194 /* Still holding our own lock */
1195 if (This->initial) {
1196 init_new_decoded_pad(bin, pad, This);
1197 return;
1200 for (i = 0; i < This->source_count; ++i)
1202 struct parser_source *pin = This->sources[i];
1203 if (!pin->their_src) {
1204 gst_segment_init(pin->segment, GST_FORMAT_TIME);
1206 if (pin->post_sink)
1207 ret = gst_pad_link(pad, pin->post_sink);
1208 else
1209 ret = gst_pad_link(pad, pin->my_sink);
1211 if (ret >= 0) {
1212 pin->their_src = pad;
1213 gst_object_ref(pin->their_src);
1214 TRACE("Relinked\n");
1215 return;
1219 init_new_decoded_pad(bin, pad, This);
1222 static gboolean query_function(GstPad *pad, GstObject *parent, GstQuery *query)
1224 struct parser *This = gst_pad_get_element_private(pad);
1225 GstFormat format;
1227 TRACE("filter %p, type %s.\n", This, GST_QUERY_TYPE_NAME(query));
1229 switch (GST_QUERY_TYPE(query)) {
1230 case GST_QUERY_DURATION:
1231 gst_query_parse_duration(query, &format, NULL);
1232 if (format == GST_FORMAT_PERCENT)
1234 gst_query_set_duration(query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
1235 return TRUE;
1237 else if (format == GST_FORMAT_BYTES)
1239 gst_query_set_duration(query, GST_FORMAT_BYTES, This->filesize);
1240 return TRUE;
1242 return FALSE;
1243 case GST_QUERY_SEEKING:
1244 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1245 if (format != GST_FORMAT_BYTES)
1247 WARN("Cannot seek using format \"%s\".\n", gst_format_get_name(format));
1248 return FALSE;
1250 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
1251 return TRUE;
1252 case GST_QUERY_SCHEDULING:
1253 gst_query_set_scheduling(query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1254 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PUSH);
1255 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PULL);
1256 return TRUE;
1257 default:
1258 WARN("Unhandled query type %s.\n", GST_QUERY_TYPE_NAME(query));
1259 return FALSE;
1263 static gboolean activate_push(GstPad *pad, gboolean activate)
1265 struct parser *This = gst_pad_get_element_private(pad);
1267 EnterCriticalSection(&This->filter.filter_cs);
1268 if (!activate) {
1269 TRACE("Deactivating\n");
1270 if (!This->initial)
1271 IAsyncReader_BeginFlush(This->reader);
1272 if (This->push_thread) {
1273 WaitForSingleObject(This->push_thread, -1);
1274 CloseHandle(This->push_thread);
1275 This->push_thread = NULL;
1277 if (!This->initial)
1278 IAsyncReader_EndFlush(This->reader);
1279 if (This->filter.state == State_Stopped)
1280 This->nextofs = This->start;
1281 } else if (!This->push_thread) {
1282 TRACE("Activating\n");
1283 if (This->initial)
1284 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
1285 else
1286 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
1288 LeaveCriticalSection(&This->filter.filter_cs);
1289 return TRUE;
1292 static gboolean activate_mode(GstPad *pad, GstObject *parent, GstPadMode mode, gboolean activate)
1294 struct parser *filter = gst_pad_get_element_private(pad);
1296 TRACE("%s source pad for filter %p in %s mode.\n",
1297 activate ? "Activating" : "Deactivating", filter, gst_pad_mode_get_name(mode));
1299 switch (mode) {
1300 case GST_PAD_MODE_PULL:
1301 return TRUE;
1302 case GST_PAD_MODE_PUSH:
1303 return activate_push(pad, activate);
1304 default:
1305 return FALSE;
1307 return FALSE;
1310 static void no_more_pads(GstElement *decodebin, gpointer user)
1312 struct parser *filter = user;
1313 TRACE("filter %p.\n", filter);
1314 SetEvent(filter->no_more_pads_event);
1317 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, gpointer user)
1319 const char *name = gst_element_factory_get_longname(fact);
1321 GST_TRACE("Using \"%s\".", name);
1323 if (strstr(name, "Player protection"))
1325 GST_WARNING("Blacklisted a/52 decoder because it only works in Totem.");
1326 return GST_AUTOPLUG_SELECT_SKIP;
1328 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder"))
1330 GST_WARNING("Disabled video acceleration since it breaks in wine.");
1331 return GST_AUTOPLUG_SELECT_SKIP;
1333 return GST_AUTOPLUG_SELECT_TRY;
1336 static GstBusSyncReply watch_bus(GstBus *bus, GstMessage *msg, gpointer data)
1338 struct parser *filter = data;
1339 GError *err = NULL;
1340 gchar *dbg_info = NULL;
1342 TRACE("filter %p, message type %s.\n", filter, GST_MESSAGE_TYPE_NAME(msg));
1344 switch (msg->type)
1346 case GST_MESSAGE_ERROR:
1347 gst_message_parse_error(msg, &err, &dbg_info);
1348 ERR("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1349 ERR("%s\n", dbg_info);
1350 g_error_free(err);
1351 g_free(dbg_info);
1352 SetEvent(filter->error_event);
1353 break;
1354 case GST_MESSAGE_WARNING:
1355 gst_message_parse_warning(msg, &err, &dbg_info);
1356 WARN("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1357 WARN("%s\n", dbg_info);
1358 g_error_free(err);
1359 g_free(dbg_info);
1360 break;
1361 case GST_MESSAGE_DURATION_CHANGED:
1362 SetEvent(filter->duration_event);
1363 break;
1364 default:
1365 break;
1367 gst_message_unref(msg);
1368 return GST_BUS_DROP;
1371 static HRESULT GST_Connect(struct parser *This, IPin *pConnectPin)
1373 LONGLONG avail;
1374 GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
1375 "quartz_src",
1376 GST_PAD_SRC,
1377 GST_PAD_ALWAYS,
1378 GST_STATIC_CAPS_ANY);
1380 IAsyncReader_Length(This->reader, &This->filesize, &avail);
1382 if (!This->bus) {
1383 This->bus = gst_bus_new();
1384 gst_bus_set_sync_handler(This->bus, watch_bus_wrapper, This, NULL);
1387 This->container = gst_bin_new(NULL);
1388 gst_element_set_bus(This->container, This->bus);
1390 This->my_src = gst_pad_new_from_static_template(&src_template, "quartz-src");
1391 gst_pad_set_getrange_function(This->my_src, request_buffer_src_wrapper);
1392 gst_pad_set_query_function(This->my_src, query_function_wrapper);
1393 gst_pad_set_activatemode_function(This->my_src, activate_mode_wrapper);
1394 gst_pad_set_event_function(This->my_src, event_src_wrapper);
1395 gst_pad_set_element_private (This->my_src, This);
1397 This->start = This->nextofs = This->nextpullofs = This->stop = 0;
1399 This->initial = TRUE;
1400 if (!This->init_gst(This))
1401 return E_FAIL;
1402 This->initial = FALSE;
1404 This->nextofs = This->nextpullofs = 0;
1405 return S_OK;
1408 static LONGLONG query_duration(GstPad *pad)
1410 gint64 duration, byte_length;
1412 if (gst_pad_query_duration(pad, GST_FORMAT_TIME, &duration))
1413 return duration / 100;
1415 WARN("Failed to query time duration; trying to convert from byte length.\n");
1417 /* To accurately get a duration for the stream, we want to only consider the
1418 * length of that stream. Hence, query for the pad duration, instead of
1419 * using the file duration. */
1420 if (gst_pad_query_duration(pad, GST_FORMAT_BYTES, &byte_length)
1421 && gst_pad_query_convert(pad, GST_FORMAT_BYTES, byte_length, GST_FORMAT_TIME, &duration))
1422 return duration / 100;
1424 ERR("Failed to query duration.\n");
1425 return 0;
1428 static inline struct parser_source *impl_from_IMediaSeeking(IMediaSeeking *iface)
1430 return CONTAINING_RECORD(iface, struct parser_source, seek.IMediaSeeking_iface);
1433 static struct strmbase_pin *parser_get_pin(struct strmbase_filter *base, unsigned int index)
1435 struct parser *filter = impl_from_strmbase_filter(base);
1437 if (filter->enum_sink_first)
1439 if (!index)
1440 return &filter->sink.pin;
1441 else if (index <= filter->source_count)
1442 return &filter->sources[index - 1]->pin.pin;
1444 else
1446 if (index < filter->source_count)
1447 return &filter->sources[index]->pin.pin;
1448 else if (index == filter->source_count)
1449 return &filter->sink.pin;
1451 return NULL;
1454 static void parser_destroy(struct strmbase_filter *iface)
1456 struct parser *filter = impl_from_strmbase_filter(iface);
1457 HRESULT hr;
1459 CloseHandle(filter->no_more_pads_event);
1460 CloseHandle(filter->duration_event);
1461 CloseHandle(filter->error_event);
1463 /* Don't need to clean up output pins, disconnecting input pin will do that */
1464 if (filter->sink.pin.peer)
1466 hr = IPin_Disconnect(filter->sink.pin.peer);
1467 assert(hr == S_OK);
1468 hr = IPin_Disconnect(&filter->sink.pin.IPin_iface);
1469 assert(hr == S_OK);
1472 if (filter->reader)
1473 IAsyncReader_Release(filter->reader);
1474 filter->reader = NULL;
1476 if (filter->bus)
1478 gst_bus_set_sync_handler(filter->bus, NULL, NULL, NULL);
1479 gst_object_unref(filter->bus);
1481 strmbase_sink_cleanup(&filter->sink);
1482 strmbase_filter_cleanup(&filter->filter);
1483 heap_free(filter);
1486 static HRESULT parser_init_stream(struct strmbase_filter *iface)
1488 struct parser *filter = impl_from_strmbase_filter(iface);
1489 const SourceSeeking *seeking;
1490 GstStateChangeReturn ret;
1491 unsigned int i;
1493 if (!filter->container)
1494 return S_OK;
1496 for (i = 0; i < filter->source_count; ++i)
1498 HRESULT hr;
1500 if (filter->sources[i]->pin.pin.peer && FAILED(hr = IMemAllocator_Commit(filter->sources[i]->pin.pAllocator)))
1501 ERR("Failed to commit allocator, hr %#x.\n", hr);
1504 if (filter->no_more_pads_event)
1505 ResetEvent(filter->no_more_pads_event);
1507 if ((ret = gst_element_set_state(filter->container, GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE)
1509 ERR("Failed to pause stream.\n");
1510 return E_FAIL;
1513 /* Make sure that all of our pads are connected before returning, lest we
1514 * e.g. try to seek and fail. */
1515 if (filter->no_more_pads_event)
1516 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
1518 seeking = &filter->sources[0]->seek;
1520 /* GStreamer can't seek while stopped, and it resets position to the
1521 * beginning of the stream every time it is stopped. */
1522 if (seeking->llCurrent)
1524 GstSeekType stop_type = GST_SEEK_TYPE_NONE;
1526 if (seeking->llStop && seeking->llStop != seeking->llDuration)
1527 stop_type = GST_SEEK_TYPE_SET;
1529 gst_pad_push_event(filter->sources[0]->my_sink, gst_event_new_seek(
1530 seeking->dRate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
1531 GST_SEEK_TYPE_SET, seeking->llCurrent * 100,
1532 stop_type, seeking->llStop * 100));
1535 return S_OK;
1538 static HRESULT parser_cleanup_stream(struct strmbase_filter *iface)
1540 struct parser *filter = impl_from_strmbase_filter(iface);
1541 GstStateChangeReturn ret;
1542 unsigned int i;
1544 if (!filter->container)
1545 return S_OK;
1547 filter->ignore_flush = TRUE;
1548 if ((ret = gst_element_set_state(filter->container, GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE)
1550 ERR("Failed to pause stream.\n");
1551 return E_FAIL;
1553 gst_element_get_state(filter->container, NULL, NULL, GST_CLOCK_TIME_NONE);
1554 filter->ignore_flush = FALSE;
1556 for (i = 0; i < filter->source_count; ++i)
1558 if (filter->sources[i]->pin.pin.peer)
1559 IMemAllocator_Decommit(filter->sources[i]->pin.pAllocator);
1562 return S_OK;
1565 static HRESULT parser_wait_state(struct strmbase_filter *iface, DWORD timeout)
1567 struct parser *filter = impl_from_strmbase_filter(iface);
1568 GstStateChangeReturn ret;
1570 if (!filter->container)
1571 return S_OK;
1573 ret = gst_element_get_state(filter->container, NULL, NULL,
1574 timeout == INFINITE ? GST_CLOCK_TIME_NONE : timeout * 1000000);
1575 if (ret == GST_STATE_CHANGE_FAILURE)
1577 ERR("Failed to get state.\n");
1578 return E_FAIL;
1580 else if (ret == GST_STATE_CHANGE_ASYNC)
1581 return VFW_S_STATE_INTERMEDIATE;
1582 return S_OK;
1585 static const struct strmbase_filter_ops filter_ops =
1587 .filter_get_pin = parser_get_pin,
1588 .filter_destroy = parser_destroy,
1589 .filter_init_stream = parser_init_stream,
1590 .filter_cleanup_stream = parser_cleanup_stream,
1591 .filter_wait_state = parser_wait_state,
1594 static inline struct parser *impl_from_strmbase_sink(struct strmbase_sink *iface)
1596 return CONTAINING_RECORD(iface, struct parser, sink);
1599 static HRESULT sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1601 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
1602 return S_OK;
1603 return S_FALSE;
1606 static HRESULT parser_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *pmt)
1608 struct parser *filter = impl_from_strmbase_sink(iface);
1609 HRESULT hr = S_OK;
1611 mark_wine_thread();
1613 filter->reader = NULL;
1614 if (FAILED(hr = IPin_QueryInterface(peer, &IID_IAsyncReader, (void **)&filter->reader)))
1615 return hr;
1617 if (FAILED(hr = GST_Connect(filter, peer)))
1618 goto err;
1620 return S_OK;
1621 err:
1622 GST_RemoveOutputPins(filter);
1623 IAsyncReader_Release(filter->reader);
1624 filter->reader = NULL;
1625 return hr;
1628 static void parser_sink_disconnect(struct strmbase_sink *iface)
1630 struct parser *filter = impl_from_strmbase_sink(iface);
1632 mark_wine_thread();
1634 GST_RemoveOutputPins(filter);
1636 IAsyncReader_Release(filter->reader);
1637 filter->reader = NULL;
1640 static const struct strmbase_sink_ops sink_ops =
1642 .base.pin_query_accept = sink_query_accept,
1643 .sink_connect = parser_sink_connect,
1644 .sink_disconnect = parser_sink_disconnect,
1647 static BOOL decodebin_parser_init_gst(struct parser *filter)
1649 GstElement *element = gst_element_factory_make("decodebin", NULL);
1650 unsigned int i;
1651 HANDLE events[2];
1652 int ret;
1654 if (!element)
1656 ERR("Failed to create decodebin; are %u-bit GStreamer \"base\" plugins installed?\n",
1657 8 * (int)sizeof(void*));
1658 return FALSE;
1661 gst_bin_add(GST_BIN(filter->container), element);
1663 g_signal_connect(element, "pad-added", G_CALLBACK(existing_new_pad_wrapper), filter);
1664 g_signal_connect(element, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), filter);
1665 g_signal_connect(element, "autoplug-select", G_CALLBACK(autoplug_blacklist), filter);
1666 g_signal_connect(element, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), filter);
1668 filter->their_sink = gst_element_get_static_pad(element, "sink");
1669 ResetEvent(filter->no_more_pads_event);
1671 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
1673 ERR("Failed to link pads, error %d.\n", ret);
1674 return FALSE;
1677 gst_element_set_state(filter->container, GST_STATE_PLAYING);
1678 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
1679 if (ret == GST_STATE_CHANGE_FAILURE)
1681 ERR("Failed to play stream.\n");
1682 return FALSE;
1685 events[0] = filter->no_more_pads_event;
1686 events[1] = filter->error_event;
1687 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
1688 return FALSE;
1690 for (i = 0; i < filter->source_count; ++i)
1692 struct parser_source *pin = filter->sources[i];
1694 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
1695 pin->seek.llCurrent = 0;
1696 events[0] = pin->caps_event;
1697 events[1] = filter->error_event;
1698 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
1699 return FALSE;
1702 filter->ignore_flush = TRUE;
1703 gst_element_set_state(filter->container, GST_STATE_READY);
1704 gst_element_get_state(filter->container, NULL, NULL, -1);
1705 filter->ignore_flush = FALSE;
1707 return TRUE;
1710 static HRESULT decodebin_parser_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
1712 /* At least make sure we can convert it to GstCaps. */
1713 GstCaps *caps = amt_to_gst_caps(mt);
1715 if (!caps)
1716 return S_FALSE;
1717 gst_caps_unref(caps);
1718 return S_OK;
1721 static HRESULT decodebin_parser_source_get_media_type(struct parser_source *pin,
1722 unsigned int index, AM_MEDIA_TYPE *mt)
1724 const GstCaps *caps = pin->caps;
1725 const GstStructure *structure;
1726 const char *type;
1728 static const GstVideoFormat video_formats[] =
1730 /* Try to prefer YUV formats over RGB ones. Most decoders output in the
1731 * YUV color space, and it's generally much less expensive for
1732 * videoconvert to do YUV -> YUV transformations. */
1733 GST_VIDEO_FORMAT_AYUV,
1734 GST_VIDEO_FORMAT_I420,
1735 GST_VIDEO_FORMAT_YV12,
1736 GST_VIDEO_FORMAT_YUY2,
1737 GST_VIDEO_FORMAT_UYVY,
1738 GST_VIDEO_FORMAT_YVYU,
1739 GST_VIDEO_FORMAT_NV12,
1740 GST_VIDEO_FORMAT_BGRA,
1741 GST_VIDEO_FORMAT_BGRx,
1742 GST_VIDEO_FORMAT_BGR,
1743 GST_VIDEO_FORMAT_RGB16,
1744 GST_VIDEO_FORMAT_RGB15,
1747 assert(caps); /* We shouldn't be able to get here if caps haven't been set. */
1748 structure = gst_caps_get_structure(caps, 0);
1749 type = gst_structure_get_name(structure);
1751 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
1753 if (amt_from_gst_caps(caps, mt))
1755 if (!index--)
1756 return S_OK;
1757 FreeMediaType(mt);
1760 if (!strcmp(type, "video/x-raw") && index < ARRAY_SIZE(video_formats))
1762 gint width, height, fps_n, fps_d;
1763 GstVideoInfo info;
1765 gst_structure_get_int(structure, "width", &width);
1766 gst_structure_get_int(structure, "height", &height);
1767 gst_video_info_set_format(&info, video_formats[index], width, height);
1768 if (gst_structure_get_fraction(structure, "framerate", &fps_n, &fps_d) && fps_n)
1770 info.fps_n = fps_n;
1771 info.fps_d = fps_d;
1773 if (!amt_from_gst_video_info(&info, mt))
1774 return E_OUTOFMEMORY;
1775 return S_OK;
1777 else if (!strcmp(type, "audio/x-raw") && !index)
1779 GstAudioInfo info;
1780 gint rate;
1782 gst_structure_get_int(structure, "rate", &rate);
1783 gst_audio_info_set_format(&info, GST_AUDIO_FORMAT_S16LE, rate, 2, NULL);
1784 if (!amt_from_gst_audio_info(&info, mt))
1785 return E_OUTOFMEMORY;
1786 return S_OK;
1789 return VFW_S_NO_MORE_ITEMS;
1792 static BOOL parser_init_gstreamer(void)
1794 if (!init_gstreamer())
1795 return FALSE;
1796 GST_DEBUG_CATEGORY_INIT(wine, "WINE", GST_DEBUG_FG_RED, "Wine GStreamer support");
1797 return TRUE;
1800 HRESULT decodebin_parser_create(IUnknown *outer, IUnknown **out)
1802 struct parser *object;
1804 if (!parser_init_gstreamer())
1805 return E_FAIL;
1807 mark_wine_thread();
1809 if (!(object = heap_alloc_zero(sizeof(*object))))
1810 return E_OUTOFMEMORY;
1812 strmbase_filter_init(&object->filter, outer, &CLSID_decodebin_parser, &filter_ops);
1813 strmbase_sink_init(&object->sink, &object->filter, wcsInputPinName, &sink_ops, NULL);
1815 object->no_more_pads_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1816 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
1817 object->init_gst = decodebin_parser_init_gst;
1818 object->source_query_accept = decodebin_parser_source_query_accept;
1819 object->source_get_media_type = decodebin_parser_source_get_media_type;
1821 TRACE("Created GStreamer demuxer %p.\n", object);
1822 *out = &object->filter.IUnknown_inner;
1823 return S_OK;
1826 static struct parser *impl_from_IAMStreamSelect(IAMStreamSelect *iface)
1828 return CONTAINING_RECORD(iface, struct parser, IAMStreamSelect_iface);
1831 static HRESULT WINAPI stream_select_QueryInterface(IAMStreamSelect *iface, REFIID iid, void **out)
1833 struct parser *filter = impl_from_IAMStreamSelect(iface);
1834 return IUnknown_QueryInterface(filter->filter.outer_unk, iid, out);
1837 static ULONG WINAPI stream_select_AddRef(IAMStreamSelect *iface)
1839 struct parser *filter = impl_from_IAMStreamSelect(iface);
1840 return IUnknown_AddRef(filter->filter.outer_unk);
1843 static ULONG WINAPI stream_select_Release(IAMStreamSelect *iface)
1845 struct parser *filter = impl_from_IAMStreamSelect(iface);
1846 return IUnknown_Release(filter->filter.outer_unk);
1849 static HRESULT WINAPI stream_select_Count(IAMStreamSelect *iface, DWORD *count)
1851 FIXME("iface %p, count %p, stub!\n", iface, count);
1852 return E_NOTIMPL;
1855 static HRESULT WINAPI stream_select_Info(IAMStreamSelect *iface, LONG index,
1856 AM_MEDIA_TYPE **mt, DWORD *flags, LCID *lcid, DWORD *group, WCHAR **name,
1857 IUnknown **object, IUnknown **unknown)
1859 FIXME("iface %p, index %d, mt %p, flags %p, lcid %p, group %p, name %p, object %p, unknown %p, stub!\n",
1860 iface, index, mt, flags, lcid, group, name, object, unknown);
1861 return E_NOTIMPL;
1864 static HRESULT WINAPI stream_select_Enable(IAMStreamSelect *iface, LONG index, DWORD flags)
1866 FIXME("iface %p, index %d, flags %#x, stub!\n", iface, index, flags);
1867 return E_NOTIMPL;
1870 static const IAMStreamSelectVtbl stream_select_vtbl =
1872 stream_select_QueryInterface,
1873 stream_select_AddRef,
1874 stream_select_Release,
1875 stream_select_Count,
1876 stream_select_Info,
1877 stream_select_Enable,
1880 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1882 struct parser_source *This = impl_from_IMediaSeeking(iface);
1883 TRACE("(%p)\n", This);
1884 return S_OK;
1887 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1889 struct parser_source *This = impl_from_IMediaSeeking(iface);
1890 TRACE("(%p)\n", This);
1891 return S_OK;
1894 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1896 struct parser_source *This = impl_from_IMediaSeeking(iface);
1897 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1898 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1899 mark_wine_thread();
1900 gst_pad_push_event(This->my_sink, ev);
1901 return S_OK;
1904 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1906 struct parser_source *This = impl_from_IMediaSeeking(iface);
1907 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1910 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1912 struct parser_source *This = impl_from_IMediaSeeking(iface);
1913 return IPin_AddRef(&This->pin.pin.IPin_iface);
1916 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1918 struct parser_source *This = impl_from_IMediaSeeking(iface);
1919 return IPin_Release(&This->pin.pin.IPin_iface);
1922 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1923 LONGLONG *current, DWORD current_flags, LONGLONG *stop, DWORD stop_flags)
1925 GstSeekType current_type = GST_SEEK_TYPE_SET, stop_type = GST_SEEK_TYPE_SET;
1926 struct parser_source *pin = impl_from_IMediaSeeking(iface);
1927 GstSeekFlags flags = 0;
1929 TRACE("pin %p, current %s, current_flags %#x, stop %s, stop_flags %#x.\n",
1930 pin, current ? debugstr_time(*current) : "<null>", current_flags,
1931 stop ? debugstr_time(*stop) : "<null>", stop_flags);
1933 mark_wine_thread();
1935 SourceSeekingImpl_SetPositions(iface, current, current_flags, stop, stop_flags);
1936 if (pin->pin.pin.filter->state == State_Stopped)
1937 return S_OK;
1939 if (current_flags & AM_SEEKING_SeekToKeyFrame)
1940 flags |= GST_SEEK_FLAG_KEY_UNIT;
1941 if (current_flags & AM_SEEKING_Segment)
1942 flags |= GST_SEEK_FLAG_SEGMENT;
1943 if (!(current_flags & AM_SEEKING_NoFlush))
1944 flags |= GST_SEEK_FLAG_FLUSH;
1946 if ((current_flags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_NoPositioning)
1947 current_type = GST_SEEK_TYPE_NONE;
1948 if ((stop_flags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_NoPositioning)
1949 stop_type = GST_SEEK_TYPE_NONE;
1951 if (!gst_pad_push_event(pin->my_sink, gst_event_new_seek(pin->seek.dRate, GST_FORMAT_TIME, flags,
1952 current_type, pin->seek.llCurrent * 100, stop_type, pin->seek.llStop * 100)))
1954 ERR("Failed to seek (current %s, stop %s).\n",
1955 debugstr_time(pin->seek.llCurrent), debugstr_time(pin->seek.llStop));
1956 return E_FAIL;
1958 return S_OK;
1961 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1963 GST_Seeking_QueryInterface,
1964 GST_Seeking_AddRef,
1965 GST_Seeking_Release,
1966 SourceSeekingImpl_GetCapabilities,
1967 SourceSeekingImpl_CheckCapabilities,
1968 SourceSeekingImpl_IsFormatSupported,
1969 SourceSeekingImpl_QueryPreferredFormat,
1970 SourceSeekingImpl_GetTimeFormat,
1971 SourceSeekingImpl_IsUsingTimeFormat,
1972 SourceSeekingImpl_SetTimeFormat,
1973 SourceSeekingImpl_GetDuration,
1974 SourceSeekingImpl_GetStopPosition,
1975 SourceSeekingImpl_GetCurrentPosition,
1976 SourceSeekingImpl_ConvertTimeFormat,
1977 GST_Seeking_SetPositions,
1978 SourceSeekingImpl_GetPositions,
1979 SourceSeekingImpl_GetAvailable,
1980 SourceSeekingImpl_SetRate,
1981 SourceSeekingImpl_GetRate,
1982 SourceSeekingImpl_GetPreroll
1985 static inline struct parser_source *impl_from_IQualityControl( IQualityControl *iface )
1987 return CONTAINING_RECORD(iface, struct parser_source, IQualityControl_iface);
1990 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1992 struct parser_source *pin = impl_from_IQualityControl(iface);
1993 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1996 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1998 struct parser_source *pin = impl_from_IQualityControl(iface);
1999 return IPin_AddRef(&pin->pin.pin.IPin_iface);
2002 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
2004 struct parser_source *pin = impl_from_IQualityControl(iface);
2005 return IPin_Release(&pin->pin.pin.IPin_iface);
2008 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality q)
2010 struct parser_source *pin = impl_from_IQualityControl(iface);
2011 GstQOSType type = GST_QOS_TYPE_OVERFLOW;
2012 GstClockTime timestamp;
2013 GstClockTimeDiff diff;
2014 GstEvent *event;
2016 TRACE("pin %p, sender %p, type %s, proportion %u, late %s, timestamp %s.\n",
2017 pin, sender, q.Type == Famine ? "Famine" : "Flood", q.Proportion,
2018 debugstr_time(q.Late), debugstr_time(q.TimeStamp));
2020 mark_wine_thread();
2022 /* GST_QOS_TYPE_OVERFLOW is also used for buffers that arrive on time, but
2023 * DirectShow filters might use Famine, so check that there actually is an
2024 * underrun. */
2025 if (q.Type == Famine && q.Proportion < 1000)
2026 type = GST_QOS_TYPE_UNDERFLOW;
2028 /* DirectShow filters sometimes pass negative timestamps (Audiosurf uses the
2029 * current time instead of the time of the last buffer). GstClockTime is
2030 * unsigned, so clamp it to 0. */
2031 timestamp = max(q.TimeStamp * 100, 0);
2033 /* The documentation specifies that timestamp + diff must be nonnegative. */
2034 diff = q.Late * 100;
2035 if (diff < 0 && timestamp < (GstClockTime)-diff)
2036 diff = -timestamp;
2038 /* DirectShow "Proportion" describes what percentage of buffers the upstream
2039 * filter should keep (i.e. dropping the rest). If frames are late, the
2040 * proportion will be less than 1. For example, a proportion of 500 means
2041 * that the element should drop half of its frames, essentially because
2042 * frames are taking twice as long as they should to arrive.
2044 * GStreamer "proportion" is the inverse of this; it describes how much
2045 * faster the upstream element should produce frames. I.e. if frames are
2046 * taking twice as long as they should to arrive, we want the frames to be
2047 * decoded twice as fast, and so we pass 2.0 to GStreamer. */
2049 if (!q.Proportion)
2051 WARN("Ignoring quality message with zero proportion.\n");
2052 return S_OK;
2055 if (!(event = gst_event_new_qos(type, 1000.0 / q.Proportion, diff, timestamp)))
2056 ERR("Failed to create QOS event.\n");
2058 gst_pad_push_event(pin->my_sink, event);
2060 return S_OK;
2063 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
2065 struct parser_source *pin = impl_from_IQualityControl(iface);
2066 TRACE("(%p)->(%p)\n", pin, pin);
2067 /* Do nothing */
2068 return S_OK;
2071 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
2072 GST_QualityControl_QueryInterface,
2073 GST_QualityControl_AddRef,
2074 GST_QualityControl_Release,
2075 GST_QualityControl_Notify,
2076 GST_QualityControl_SetSink
2079 static inline struct parser_source *impl_source_from_IPin(IPin *iface)
2081 return CONTAINING_RECORD(iface, struct parser_source, pin.pin.IPin_iface);
2084 static HRESULT source_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
2086 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2088 if (IsEqualGUID(iid, &IID_IMediaSeeking))
2089 *out = &pin->seek.IMediaSeeking_iface;
2090 else if (IsEqualGUID(iid, &IID_IQualityControl))
2091 *out = &pin->IQualityControl_iface;
2092 else
2093 return E_NOINTERFACE;
2095 IUnknown_AddRef((IUnknown *)*out);
2096 return S_OK;
2099 static HRESULT source_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2101 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2102 struct parser *filter = impl_from_strmbase_filter(iface->filter);
2103 return filter->source_query_accept(pin, mt);
2106 static HRESULT source_get_media_type(struct strmbase_pin *iface, unsigned int index, AM_MEDIA_TYPE *mt)
2108 struct parser_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2109 struct parser *filter = impl_from_strmbase_filter(iface->filter);
2110 return filter->source_get_media_type(pin, index, mt);
2113 static HRESULT WINAPI GSTOutPin_DecideBufferSize(struct strmbase_source *iface,
2114 IMemAllocator *allocator, ALLOCATOR_PROPERTIES *props)
2116 struct parser_source *pin = impl_source_from_IPin(&iface->pin.IPin_iface);
2117 unsigned int buffer_size = 16384;
2118 ALLOCATOR_PROPERTIES ret_props;
2120 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_VideoInfo))
2122 VIDEOINFOHEADER *format = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
2123 buffer_size = format->bmiHeader.biSizeImage;
2125 gst_util_set_object_arg(G_OBJECT(pin->flip), "method",
2126 (format->bmiHeader.biCompression == BI_RGB
2127 || format->bmiHeader.biCompression == BI_BITFIELDS) ? "vertical-flip" : "none");
2129 else if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
2130 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
2131 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
2133 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
2134 buffer_size = format->nAvgBytesPerSec;
2137 props->cBuffers = max(props->cBuffers, 1);
2138 props->cbBuffer = max(props->cbBuffer, buffer_size);
2139 props->cbAlign = max(props->cbAlign, 1);
2140 return IMemAllocator_SetProperties(allocator, props, &ret_props);
2143 static void free_source_pin(struct parser_source *pin)
2145 if (pin->pin.pin.peer)
2147 if (SUCCEEDED(IMemAllocator_Decommit(pin->pin.pAllocator)))
2148 IPin_Disconnect(pin->pin.pin.peer);
2149 IPin_Disconnect(&pin->pin.pin.IPin_iface);
2152 if (pin->their_src)
2154 if (pin->post_sink)
2156 gst_pad_unlink(pin->their_src, pin->post_sink);
2157 gst_pad_unlink(pin->post_src, pin->my_sink);
2158 gst_object_unref(pin->post_src);
2159 gst_object_unref(pin->post_sink);
2160 pin->post_src = pin->post_sink = NULL;
2162 else
2163 gst_pad_unlink(pin->their_src, pin->my_sink);
2164 gst_object_unref(pin->their_src);
2166 gst_object_unref(pin->my_sink);
2167 CloseHandle(pin->caps_event);
2168 CloseHandle(pin->eos_event);
2169 gst_segment_free(pin->segment);
2171 strmbase_seeking_cleanup(&pin->seek);
2172 strmbase_source_cleanup(&pin->pin);
2173 heap_free(pin);
2176 static const struct strmbase_source_ops source_ops =
2178 .base.pin_query_interface = source_query_interface,
2179 .base.pin_query_accept = source_query_accept,
2180 .base.pin_get_media_type = source_get_media_type,
2181 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
2182 .pfnDecideAllocator = BaseOutputPinImpl_DecideAllocator,
2183 .pfnDecideBufferSize = GSTOutPin_DecideBufferSize,
2186 static struct parser_source *create_pin(struct parser *filter, const WCHAR *name)
2188 struct parser_source *pin, **new_array;
2189 char pad_name[19];
2191 if (!(new_array = heap_realloc(filter->sources, (filter->source_count + 1) * sizeof(*new_array))))
2192 return NULL;
2193 filter->sources = new_array;
2195 if (!(pin = heap_alloc_zero(sizeof(*pin))))
2196 return NULL;
2198 strmbase_source_init(&pin->pin, &filter->filter, name, &source_ops);
2199 pin->caps_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2200 pin->eos_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2201 pin->segment = gst_segment_new();
2202 gst_segment_init(pin->segment, GST_FORMAT_TIME);
2203 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
2204 strmbase_seeking_init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop,
2205 GST_ChangeCurrent, GST_ChangeRate);
2206 BaseFilterImpl_IncrementPinVersion(&filter->filter);
2208 sprintf(pad_name, "qz_sink_%u", filter->source_count);
2209 pin->my_sink = gst_pad_new(pad_name, GST_PAD_SINK);
2210 gst_pad_set_element_private(pin->my_sink, pin);
2211 gst_pad_set_chain_function(pin->my_sink, got_data_sink_wrapper);
2212 gst_pad_set_event_function(pin->my_sink, event_sink_wrapper);
2213 gst_pad_set_query_function(pin->my_sink, query_sink_wrapper);
2215 filter->sources[filter->source_count++] = pin;
2216 return pin;
2219 static HRESULT GST_RemoveOutputPins(struct parser *This)
2221 unsigned int i;
2223 TRACE("(%p)\n", This);
2224 mark_wine_thread();
2226 if (!This->container)
2227 return S_OK;
2228 gst_element_set_state(This->container, GST_STATE_NULL);
2229 gst_pad_unlink(This->my_src, This->their_sink);
2230 gst_object_unref(This->my_src);
2231 gst_object_unref(This->their_sink);
2232 This->my_src = This->their_sink = NULL;
2234 for (i = 0; i < This->source_count; ++i)
2235 free_source_pin(This->sources[i]);
2237 This->source_count = 0;
2238 heap_free(This->sources);
2239 This->sources = NULL;
2240 gst_element_set_bus(This->container, NULL);
2241 gst_object_unref(This->container);
2242 This->container = NULL;
2243 BaseFilterImpl_IncrementPinVersion(&This->filter);
2244 return S_OK;
2247 void perform_cb_gstdemux(struct cb_data *cbdata)
2249 switch(cbdata->type)
2251 case WATCH_BUS:
2253 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2254 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2255 break;
2257 case EXISTING_NEW_PAD:
2259 struct pad_added_data *data = &cbdata->u.pad_added_data;
2260 existing_new_pad(data->element, data->pad, data->user);
2261 break;
2263 case QUERY_FUNCTION:
2265 struct query_function_data *data = &cbdata->u.query_function_data;
2266 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2267 break;
2269 case ACTIVATE_MODE:
2271 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2272 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2273 break;
2275 case NO_MORE_PADS:
2277 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2278 no_more_pads(data->element, data->user);
2279 break;
2281 case REQUEST_BUFFER_SRC:
2283 struct getrange_data *data = &cbdata->u.getrange_data;
2284 cbdata->u.getrange_data.ret = request_buffer_src(data->pad, data->parent,
2285 data->ofs, data->len, data->buf);
2286 break;
2288 case EVENT_SRC:
2290 struct event_src_data *data = &cbdata->u.event_src_data;
2291 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2292 break;
2294 case EVENT_SINK:
2296 struct event_sink_data *data = &cbdata->u.event_sink_data;
2297 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2298 break;
2300 case GOT_DATA_SINK:
2302 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2303 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2304 break;
2306 case REMOVED_DECODED_PAD:
2308 struct pad_removed_data *data = &cbdata->u.pad_removed_data;
2309 removed_decoded_pad(data->element, data->pad, data->user);
2310 break;
2312 case QUERY_SINK:
2314 struct query_sink_data *data = &cbdata->u.query_sink_data;
2315 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2316 data->query);
2317 break;
2319 default:
2321 assert(0);
2326 static BOOL compare_media_types(const AM_MEDIA_TYPE *a, const AM_MEDIA_TYPE *b)
2328 return IsEqualGUID(&a->majortype, &b->majortype)
2329 && IsEqualGUID(&a->subtype, &b->subtype)
2330 && IsEqualGUID(&a->formattype, &b->formattype)
2331 && a->cbFormat == b->cbFormat
2332 && !memcmp(a->pbFormat, b->pbFormat, a->cbFormat);
2335 static HRESULT wave_parser_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2337 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2338 return S_FALSE;
2339 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WAVE))
2340 return S_OK;
2341 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AU) || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AIFF))
2342 FIXME("AU and AIFF files are not yet supported.\n");
2343 return S_FALSE;
2346 static const struct strmbase_sink_ops wave_parser_sink_ops =
2348 .base.pin_query_accept = wave_parser_sink_query_accept,
2349 .sink_connect = parser_sink_connect,
2350 .sink_disconnect = parser_sink_disconnect,
2353 static BOOL wave_parser_init_gst(struct parser *filter)
2355 static const WCHAR source_name[] = {'o','u','t','p','u','t',0};
2356 struct parser_source *pin;
2357 GstElement *element;
2358 HANDLE events[2];
2359 int ret;
2361 if (!(element = gst_element_factory_make("wavparse", NULL)))
2363 ERR("Failed to create wavparse; are %u-bit GStreamer \"good\" plugins installed?\n",
2364 8 * (int)sizeof(void*));
2365 return FALSE;
2368 gst_bin_add(GST_BIN(filter->container), element);
2370 filter->their_sink = gst_element_get_static_pad(element, "sink");
2371 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2373 ERR("Failed to link sink pads, error %d.\n", ret);
2374 return FALSE;
2377 if (!(pin = create_pin(filter, source_name)))
2378 return FALSE;
2379 pin->their_src = gst_element_get_static_pad(element, "src");
2380 gst_object_ref(pin->their_src);
2381 if ((ret = gst_pad_link(pin->their_src, pin->my_sink)) < 0)
2383 ERR("Failed to link source pads, error %d.\n", ret);
2384 return FALSE;
2387 gst_pad_set_active(pin->my_sink, 1);
2388 gst_element_set_state(filter->container, GST_STATE_PAUSED);
2389 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2390 if (ret == GST_STATE_CHANGE_FAILURE)
2392 ERR("Failed to play stream.\n");
2393 return FALSE;
2396 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2397 pin->seek.llCurrent = 0;
2399 events[0] = pin->caps_event;
2400 events[1] = filter->error_event;
2401 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2402 return FALSE;
2404 filter->ignore_flush = TRUE;
2405 gst_element_set_state(filter->container, GST_STATE_READY);
2406 gst_element_get_state(filter->container, NULL, NULL, -1);
2407 filter->ignore_flush = FALSE;
2409 return TRUE;
2412 static HRESULT wave_parser_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2414 AM_MEDIA_TYPE pad_mt;
2415 HRESULT hr;
2417 if (!amt_from_gst_caps(pin->caps, &pad_mt))
2418 return E_OUTOFMEMORY;
2419 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2420 FreeMediaType(&pad_mt);
2421 return hr;
2424 static HRESULT wave_parser_source_get_media_type(struct parser_source *pin,
2425 unsigned int index, AM_MEDIA_TYPE *mt)
2427 if (index > 0)
2428 return VFW_S_NO_MORE_ITEMS;
2429 if (!amt_from_gst_caps(pin->caps, mt))
2430 return E_OUTOFMEMORY;
2431 return S_OK;
2434 HRESULT wave_parser_create(IUnknown *outer, IUnknown **out)
2436 static const WCHAR sink_name[] = {'i','n','p','u','t',' ','p','i','n',0};
2437 struct parser *object;
2439 if (!parser_init_gstreamer())
2440 return E_FAIL;
2442 mark_wine_thread();
2444 if (!(object = heap_alloc_zero(sizeof(*object))))
2445 return E_OUTOFMEMORY;
2447 strmbase_filter_init(&object->filter, outer, &CLSID_WAVEParser, &filter_ops);
2448 strmbase_sink_init(&object->sink, &object->filter, sink_name, &wave_parser_sink_ops, NULL);
2449 object->init_gst = wave_parser_init_gst;
2450 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2451 object->source_query_accept = wave_parser_source_query_accept;
2452 object->source_get_media_type = wave_parser_source_get_media_type;
2454 TRACE("Created WAVE parser %p.\n", object);
2455 *out = &object->filter.IUnknown_inner;
2456 return S_OK;
2459 static HRESULT avi_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2461 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream)
2462 && IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_Avi))
2463 return S_OK;
2464 return S_FALSE;
2467 static const struct strmbase_sink_ops avi_splitter_sink_ops =
2469 .base.pin_query_accept = avi_splitter_sink_query_accept,
2470 .sink_connect = parser_sink_connect,
2471 .sink_disconnect = parser_sink_disconnect,
2474 static BOOL avi_splitter_init_gst(struct parser *filter)
2476 GstElement *element = gst_element_factory_make("avidemux", NULL);
2477 unsigned int i;
2478 HANDLE events[2];
2479 int ret;
2481 if (!element)
2483 ERR("Failed to create avidemux; are %u-bit GStreamer \"good\" plugins installed?\n",
2484 8 * (int)sizeof(void*));
2485 return FALSE;
2488 gst_bin_add(GST_BIN(filter->container), element);
2490 g_signal_connect(element, "pad-added", G_CALLBACK(existing_new_pad_wrapper), filter);
2491 g_signal_connect(element, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), filter);
2492 g_signal_connect(element, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), filter);
2494 filter->their_sink = gst_element_get_static_pad(element, "sink");
2495 ResetEvent(filter->no_more_pads_event);
2497 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2499 ERR("Failed to link pads, error %d.\n", ret);
2500 return FALSE;
2503 gst_element_set_state(filter->container, GST_STATE_PLAYING);
2504 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2505 if (ret == GST_STATE_CHANGE_FAILURE)
2507 ERR("Failed to play stream.\n");
2508 return FALSE;
2511 events[0] = filter->no_more_pads_event;
2512 events[1] = filter->error_event;
2513 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2514 return FALSE;
2516 for (i = 0; i < filter->source_count; ++i)
2518 struct parser_source *pin = filter->sources[i];
2520 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2521 pin->seek.llCurrent = 0;
2522 events[0] = pin->caps_event;
2523 events[1] = filter->error_event;
2524 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2525 return FALSE;
2528 filter->ignore_flush = TRUE;
2529 gst_element_set_state(filter->container, GST_STATE_READY);
2530 gst_element_get_state(filter->container, NULL, NULL, -1);
2531 filter->ignore_flush = FALSE;
2533 return TRUE;
2536 static HRESULT avi_splitter_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2538 AM_MEDIA_TYPE pad_mt;
2539 HRESULT hr;
2541 if (!amt_from_gst_caps(pin->caps, &pad_mt))
2542 return E_OUTOFMEMORY;
2543 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2544 FreeMediaType(&pad_mt);
2545 return hr;
2548 static HRESULT avi_splitter_source_get_media_type(struct parser_source *pin,
2549 unsigned int index, AM_MEDIA_TYPE *mt)
2551 if (index > 0)
2552 return VFW_S_NO_MORE_ITEMS;
2553 if (!amt_from_gst_caps(pin->caps, mt))
2554 return E_OUTOFMEMORY;
2555 return S_OK;
2558 HRESULT avi_splitter_create(IUnknown *outer, IUnknown **out)
2560 static const WCHAR sink_name[] = {'i','n','p','u','t',' ','p','i','n',0};
2561 struct parser *object;
2563 if (!parser_init_gstreamer())
2564 return E_FAIL;
2566 mark_wine_thread();
2568 if (!(object = heap_alloc_zero(sizeof(*object))))
2569 return E_OUTOFMEMORY;
2571 strmbase_filter_init(&object->filter, outer, &CLSID_AviSplitter, &filter_ops);
2572 strmbase_sink_init(&object->sink, &object->filter, sink_name, &avi_splitter_sink_ops, NULL);
2573 object->no_more_pads_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2574 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2575 object->init_gst = avi_splitter_init_gst;
2576 object->source_query_accept = avi_splitter_source_query_accept;
2577 object->source_get_media_type = avi_splitter_source_get_media_type;
2579 TRACE("Created AVI splitter %p.\n", object);
2580 *out = &object->filter.IUnknown_inner;
2581 return S_OK;
2584 static HRESULT mpeg_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2586 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2587 return S_FALSE;
2588 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Audio))
2589 return S_OK;
2590 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Video)
2591 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1System)
2592 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
2593 FIXME("Unsupported subtype %s.\n", wine_dbgstr_guid(&mt->subtype));
2594 return S_FALSE;
2597 static const struct strmbase_sink_ops mpeg_splitter_sink_ops =
2599 .base.pin_query_accept = mpeg_splitter_sink_query_accept,
2600 .sink_connect = parser_sink_connect,
2601 .sink_disconnect = parser_sink_disconnect,
2604 static BOOL mpeg_splitter_init_gst(struct parser *filter)
2606 static const WCHAR source_name[] = {'A','u','d','i','o',0};
2607 struct parser_source *pin;
2608 GstElement *element;
2609 HANDLE events[3];
2610 DWORD res;
2611 int ret;
2613 if (!(element = gst_element_factory_make("mpegaudioparse", NULL)))
2615 ERR("Failed to create mpegaudioparse; are %u-bit GStreamer \"good\" plugins installed?\n",
2616 8 * (int)sizeof(void*));
2617 return FALSE;
2620 gst_bin_add(GST_BIN(filter->container), element);
2622 filter->their_sink = gst_element_get_static_pad(element, "sink");
2623 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2625 ERR("Failed to link sink pads, error %d.\n", ret);
2626 return FALSE;
2629 if (!(pin = create_pin(filter, source_name)))
2630 return FALSE;
2631 gst_object_ref(pin->their_src = gst_element_get_static_pad(element, "src"));
2632 if ((ret = gst_pad_link(pin->their_src, pin->my_sink)) < 0)
2634 ERR("Failed to link source pads, error %d.\n", ret);
2635 return FALSE;
2638 gst_pad_set_active(pin->my_sink, 1);
2639 gst_element_set_state(filter->container, GST_STATE_PAUSED);
2640 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2641 if (ret == GST_STATE_CHANGE_FAILURE)
2643 ERR("Failed to play stream.\n");
2644 return FALSE;
2647 events[0] = filter->duration_event;
2648 events[1] = filter->error_event;
2649 events[2] = pin->eos_event;
2650 res = WaitForMultipleObjects(3, events, FALSE, INFINITE);
2651 if (res == 1)
2652 return FALSE;
2654 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2655 pin->seek.llCurrent = 0;
2657 events[0] = pin->caps_event;
2658 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2659 return FALSE;
2661 filter->ignore_flush = TRUE;
2662 gst_element_set_state(filter->container, GST_STATE_READY);
2663 gst_element_get_state(filter->container, NULL, NULL, -1);
2664 filter->ignore_flush = FALSE;
2666 return TRUE;
2669 static HRESULT mpeg_splitter_source_query_accept(struct parser_source *pin, const AM_MEDIA_TYPE *mt)
2671 AM_MEDIA_TYPE pad_mt;
2672 HRESULT hr;
2674 if (!amt_from_gst_caps(pin->caps, &pad_mt))
2675 return E_OUTOFMEMORY;
2676 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2677 FreeMediaType(&pad_mt);
2678 return hr;
2681 static HRESULT mpeg_splitter_source_get_media_type(struct parser_source *pin,
2682 unsigned int index, AM_MEDIA_TYPE *mt)
2684 if (index > 0)
2685 return VFW_S_NO_MORE_ITEMS;
2686 if (!amt_from_gst_caps(pin->caps, mt))
2687 return E_OUTOFMEMORY;
2688 return S_OK;
2691 static HRESULT mpeg_splitter_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
2693 struct parser *filter = impl_from_strmbase_filter(iface);
2695 if (IsEqualGUID(iid, &IID_IAMStreamSelect))
2697 *out = &filter->IAMStreamSelect_iface;
2698 IUnknown_AddRef((IUnknown *)*out);
2699 return S_OK;
2702 return E_NOINTERFACE;
2705 static const struct strmbase_filter_ops mpeg_splitter_ops =
2707 .filter_query_interface = mpeg_splitter_query_interface,
2708 .filter_get_pin = parser_get_pin,
2709 .filter_destroy = parser_destroy,
2710 .filter_init_stream = parser_init_stream,
2711 .filter_cleanup_stream = parser_cleanup_stream,
2712 .filter_wait_state = parser_wait_state,
2715 HRESULT mpeg_splitter_create(IUnknown *outer, IUnknown **out)
2717 static const WCHAR sink_name[] = {'I','n','p','u','t',0};
2718 struct parser *object;
2720 if (!parser_init_gstreamer())
2721 return E_FAIL;
2723 mark_wine_thread();
2725 if (!(object = heap_alloc_zero(sizeof(*object))))
2726 return E_OUTOFMEMORY;
2728 strmbase_filter_init(&object->filter, outer, &CLSID_MPEG1Splitter, &mpeg_splitter_ops);
2729 strmbase_sink_init(&object->sink, &object->filter, sink_name, &mpeg_splitter_sink_ops, NULL);
2730 object->IAMStreamSelect_iface.lpVtbl = &stream_select_vtbl;
2732 object->duration_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2733 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2734 object->init_gst = mpeg_splitter_init_gst;
2735 object->source_query_accept = mpeg_splitter_source_query_accept;
2736 object->source_get_media_type = mpeg_splitter_source_get_media_type;
2737 object->enum_sink_first = TRUE;
2739 TRACE("Created MPEG-1 splitter %p.\n", object);
2740 *out = &object->filter.IUnknown_inner;
2741 return S_OK;