winegstreamer: Don't query the pad position in IMediaSeeking::SetPositions().
[wine.git] / dlls / winegstreamer / gstdemux.c
blobe227ed9331dcdfa6f02390116b32625f54dce350
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 static const GUID MEDIASUBTYPE_CVID = {mmioFOURCC('c','v','i','d'), 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
46 struct gstdemux
48 struct strmbase_filter filter;
49 IAMStreamSelect IAMStreamSelect_iface;
51 struct strmbase_sink sink;
52 IAsyncReader *reader;
54 struct gstdemux_source **sources;
55 unsigned int source_count;
56 BOOL enum_sink_first;
58 LONGLONG filesize;
60 BOOL initial, ignore_flush;
61 GstElement *container;
62 GstPad *my_src, *their_sink;
63 GstBus *bus;
64 guint64 start, nextofs, nextpullofs, stop;
65 HANDLE no_more_pads_event, duration_event, error_event;
67 HANDLE push_thread;
69 BOOL (*init_gst)(struct gstdemux *filter);
70 HRESULT (*source_query_accept)(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt);
71 HRESULT (*source_get_media_type)(struct gstdemux_source *pin, unsigned int index, AM_MEDIA_TYPE *mt);
74 struct gstdemux_source
76 struct strmbase_source pin;
77 IQualityControl IQualityControl_iface;
79 GstPad *their_src, *post_sink, *post_src, *my_sink;
80 GstElement *flip;
81 HANDLE caps_event, eos_event;
82 GstSegment *segment;
83 SourceSeeking seek;
86 static inline struct gstdemux *impl_from_strmbase_filter(struct strmbase_filter *iface)
88 return CONTAINING_RECORD(iface, struct gstdemux, filter);
91 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
92 static const IMediaSeekingVtbl GST_Seeking_Vtbl;
93 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl;
95 static struct gstdemux_source *create_pin(struct gstdemux *filter, const WCHAR *name);
96 static HRESULT GST_RemoveOutputPins(struct gstdemux *This);
97 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface);
98 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface);
99 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface);
101 static gboolean amt_from_gst_audio_info(const GstAudioInfo *info, AM_MEDIA_TYPE *amt)
103 WAVEFORMATEXTENSIBLE *wfe;
104 WAVEFORMATEX *wfx;
105 gint32 depth, bpp;
107 wfe = CoTaskMemAlloc(sizeof(*wfe));
108 wfx = (WAVEFORMATEX*)wfe;
109 amt->majortype = MEDIATYPE_Audio;
110 amt->subtype = MEDIASUBTYPE_PCM;
111 amt->formattype = FORMAT_WaveFormatEx;
112 amt->pbFormat = (BYTE*)wfe;
113 amt->cbFormat = sizeof(*wfe);
114 amt->bFixedSizeSamples = TRUE;
115 amt->bTemporalCompression = FALSE;
116 amt->pUnk = NULL;
118 wfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
120 wfx->nChannels = info->channels;
121 wfx->nSamplesPerSec = info->rate;
122 depth = GST_AUDIO_INFO_WIDTH(info);
123 bpp = GST_AUDIO_INFO_DEPTH(info);
125 if (!depth || depth > 32 || depth % 8)
126 depth = bpp;
127 else if (!bpp)
128 bpp = depth;
129 wfe->Samples.wValidBitsPerSample = depth;
130 wfx->wBitsPerSample = bpp;
131 wfx->cbSize = sizeof(*wfe)-sizeof(*wfx);
132 switch (wfx->nChannels) {
133 case 1: wfe->dwChannelMask = KSAUDIO_SPEAKER_MONO; break;
134 case 2: wfe->dwChannelMask = KSAUDIO_SPEAKER_STEREO; break;
135 case 4: wfe->dwChannelMask = KSAUDIO_SPEAKER_SURROUND; break;
136 case 5: wfe->dwChannelMask = (KSAUDIO_SPEAKER_5POINT1 & ~SPEAKER_LOW_FREQUENCY); break;
137 case 6: wfe->dwChannelMask = KSAUDIO_SPEAKER_5POINT1; break;
138 case 8: wfe->dwChannelMask = KSAUDIO_SPEAKER_7POINT1; break;
139 default:
140 wfe->dwChannelMask = 0;
142 if (GST_AUDIO_INFO_IS_FLOAT(info))
144 amt->subtype = MEDIASUBTYPE_IEEE_FLOAT;
145 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
146 } else {
147 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
148 if (wfx->nChannels <= 2 && bpp <= 16 && depth == bpp) {
149 wfx->wFormatTag = WAVE_FORMAT_PCM;
150 wfx->cbSize = 0;
151 amt->cbFormat = sizeof(WAVEFORMATEX);
154 amt->lSampleSize = wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample/8;
155 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
156 return TRUE;
159 static gboolean amt_from_gst_video_info(const GstVideoInfo *info, AM_MEDIA_TYPE *amt)
161 VIDEOINFO *vih;
162 BITMAPINFOHEADER *bih;
163 gint32 width, height;
165 width = GST_VIDEO_INFO_WIDTH(info);
166 height = GST_VIDEO_INFO_HEIGHT(info);
168 vih = CoTaskMemAlloc(sizeof(*vih));
169 bih = &vih->bmiHeader;
171 amt->formattype = FORMAT_VideoInfo;
172 amt->pbFormat = (BYTE*)vih;
173 amt->cbFormat = sizeof(VIDEOINFOHEADER);
174 amt->bFixedSizeSamples = FALSE;
175 amt->bTemporalCompression = TRUE;
176 amt->lSampleSize = 1;
177 amt->pUnk = NULL;
178 ZeroMemory(vih, sizeof(*vih));
179 amt->majortype = MEDIATYPE_Video;
181 if (GST_VIDEO_INFO_IS_RGB(info))
183 bih->biCompression = BI_RGB;
184 switch (GST_VIDEO_INFO_FORMAT(info))
186 case GST_VIDEO_FORMAT_BGRA:
187 amt->subtype = MEDIASUBTYPE_ARGB32;
188 bih->biBitCount = 32;
189 break;
190 case GST_VIDEO_FORMAT_BGRx:
191 amt->subtype = MEDIASUBTYPE_RGB32;
192 bih->biBitCount = 32;
193 break;
194 case GST_VIDEO_FORMAT_BGR:
195 amt->subtype = MEDIASUBTYPE_RGB24;
196 bih->biBitCount = 24;
197 break;
198 case GST_VIDEO_FORMAT_RGB16:
199 amt->subtype = MEDIASUBTYPE_RGB565;
200 amt->cbFormat = offsetof(VIDEOINFO, u.dwBitMasks[3]);
201 vih->u.dwBitMasks[iRED] = 0xf800;
202 vih->u.dwBitMasks[iGREEN] = 0x07e0;
203 vih->u.dwBitMasks[iBLUE] = 0x001f;
204 bih->biBitCount = 16;
205 bih->biCompression = BI_BITFIELDS;
206 break;
207 case GST_VIDEO_FORMAT_RGB15:
208 amt->subtype = MEDIASUBTYPE_RGB555;
209 bih->biBitCount = 16;
210 break;
211 default:
212 WARN("Cannot convert %s to a DirectShow type.\n", GST_VIDEO_INFO_NAME(info));
213 CoTaskMemFree(vih);
214 return FALSE;
216 } else {
217 amt->subtype = MEDIATYPE_Video;
218 if (!(amt->subtype.Data1 = gst_video_format_to_fourcc(GST_VIDEO_INFO_FORMAT(info))))
220 CoTaskMemFree(vih);
221 return FALSE;
223 switch (amt->subtype.Data1) {
224 case mmioFOURCC('I','4','2','0'):
225 case mmioFOURCC('Y','V','1','2'):
226 case mmioFOURCC('N','V','1','2'):
227 case mmioFOURCC('N','V','2','1'):
228 bih->biBitCount = 12; break;
229 case mmioFOURCC('Y','U','Y','2'):
230 case mmioFOURCC('Y','V','Y','U'):
231 case mmioFOURCC('U','Y','V','Y'):
232 bih->biBitCount = 16; break;
234 bih->biCompression = amt->subtype.Data1;
236 bih->biSizeImage = GST_VIDEO_INFO_SIZE(info);
237 if ((vih->AvgTimePerFrame = (REFERENCE_TIME)MulDiv(10000000,
238 GST_VIDEO_INFO_FPS_D(info), GST_VIDEO_INFO_FPS_N(info))) == -1)
239 vih->AvgTimePerFrame = 0; /* zero division or integer overflow */
240 bih->biSize = sizeof(*bih);
241 bih->biWidth = width;
242 bih->biHeight = height;
243 bih->biPlanes = 1;
244 return TRUE;
247 static gboolean amt_from_gst_caps_audio_mpeg(const GstCaps *caps, AM_MEDIA_TYPE *mt)
249 GstStructure *structure = gst_caps_get_structure(caps, 0);
250 gint layer, channels, rate;
252 mt->majortype = MEDIATYPE_Audio;
253 mt->subtype = MEDIASUBTYPE_MPEG1AudioPayload;
254 mt->bFixedSizeSamples = FALSE;
255 mt->bTemporalCompression = FALSE;
256 mt->lSampleSize = 0;
257 mt->formattype = FORMAT_WaveFormatEx;
258 mt->pUnk = NULL;
260 if (!gst_structure_get_int(structure, "layer", &layer))
262 WARN("Missing 'layer' value.\n");
263 return FALSE;
265 if (!gst_structure_get_int(structure, "channels", &channels))
267 WARN("Missing 'channels' value.\n");
268 return FALSE;
270 if (!gst_structure_get_int(structure, "rate", &rate))
272 WARN("Missing 'rate' value.\n");
273 return FALSE;
276 if (layer == 3)
278 MPEGLAYER3WAVEFORMAT *wfx = CoTaskMemAlloc(sizeof(*wfx));
279 memset(wfx, 0, sizeof(*wfx));
281 mt->subtype.Data1 = WAVE_FORMAT_MPEGLAYER3;
282 mt->cbFormat = sizeof(*wfx);
283 mt->pbFormat = (BYTE *)wfx;
284 wfx->wfx.wFormatTag = WAVE_FORMAT_MPEGLAYER3;
285 wfx->wfx.nChannels = channels;
286 wfx->wfx.nSamplesPerSec = rate;
287 /* FIXME: We can't get most of the MPEG data from the caps. We may have
288 * to manually parse the header. */
289 wfx->wfx.cbSize = sizeof(*wfx) - sizeof(WAVEFORMATEX);
290 wfx->wID = MPEGLAYER3_ID_MPEG;
291 wfx->fdwFlags = MPEGLAYER3_FLAG_PADDING_ON;
292 wfx->nFramesPerBlock = 1;
293 wfx->nCodecDelay = 1393;
295 else
297 MPEG1WAVEFORMAT *wfx = CoTaskMemAlloc(sizeof(*wfx));
298 memset(wfx, 0, sizeof(*wfx));
300 mt->subtype.Data1 = WAVE_FORMAT_MPEG;
301 mt->cbFormat = sizeof(*wfx);
302 mt->pbFormat = (BYTE *)wfx;
303 wfx->wfx.wFormatTag = WAVE_FORMAT_MPEG;
304 wfx->wfx.nChannels = channels;
305 wfx->wfx.nSamplesPerSec = rate;
306 wfx->wfx.cbSize = sizeof(*wfx) - sizeof(WAVEFORMATEX);
307 wfx->fwHeadLayer = layer;
310 return TRUE;
313 static gboolean amt_from_gst_caps(const GstCaps *caps, AM_MEDIA_TYPE *mt)
315 const char *type = gst_structure_get_name(gst_caps_get_structure(caps, 0));
316 GstStructure *structure = gst_caps_get_structure(caps, 0);
318 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
320 if (!strcmp(type, "audio/x-raw"))
322 GstAudioInfo info;
324 if (!(gst_audio_info_from_caps(&info, caps)))
325 return FALSE;
326 return amt_from_gst_audio_info(&info, mt);
328 else if (!strcmp(type, "video/x-raw"))
330 GstVideoInfo info;
332 if (!gst_video_info_from_caps(&info, caps))
333 return FALSE;
334 return amt_from_gst_video_info(&info, mt);
336 else if (!strcmp(type, "audio/mpeg"))
337 return amt_from_gst_caps_audio_mpeg(caps, mt);
338 else if (!strcmp(type, "video/x-cinepak"))
340 VIDEOINFOHEADER *vih;
341 gint i;
343 mt->majortype = MEDIATYPE_Video;
344 mt->subtype = MEDIASUBTYPE_CVID;
345 mt->bTemporalCompression = TRUE;
346 mt->lSampleSize = 1;
347 mt->formattype = FORMAT_VideoInfo;
348 if (!(vih = CoTaskMemAlloc(sizeof(VIDEOINFOHEADER))))
349 return FALSE;
350 mt->cbFormat = sizeof(VIDEOINFOHEADER);
351 mt->pbFormat = (BYTE *)vih;
353 memset(vih, 0, sizeof(VIDEOINFOHEADER));
354 vih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
355 if (gst_structure_get_int(structure, "width", &i))
356 vih->bmiHeader.biWidth = i;
357 if (gst_structure_get_int(structure, "height", &i))
358 vih->bmiHeader.biHeight = i;
359 vih->bmiHeader.biPlanes = 1;
360 /* Both ffmpeg's encoder and a Cinepak file seen in the wild report
361 * 24 bpp. ffmpeg sets biSizeImage as below; others may be smaller, but
362 * as long as every sample fits into our allocator, we're fine. */
363 vih->bmiHeader.biBitCount = 24;
364 vih->bmiHeader.biCompression = mmioFOURCC('c','v','i','d');
365 vih->bmiHeader.biSizeImage = vih->bmiHeader.biWidth
366 * vih->bmiHeader.biHeight * vih->bmiHeader.biBitCount / 8;
367 return TRUE;
369 else
371 FIXME("Unhandled type %s.\n", debugstr_a(type));
372 return FALSE;
376 static GstCaps *amt_to_gst_caps_video(const AM_MEDIA_TYPE *mt)
378 static const struct
380 const GUID *subtype;
381 GstVideoFormat format;
383 format_map[] =
385 {&MEDIASUBTYPE_ARGB32, GST_VIDEO_FORMAT_BGRA},
386 {&MEDIASUBTYPE_RGB32, GST_VIDEO_FORMAT_BGRx},
387 {&MEDIASUBTYPE_RGB24, GST_VIDEO_FORMAT_BGR},
388 {&MEDIASUBTYPE_RGB565, GST_VIDEO_FORMAT_RGB16},
389 {&MEDIASUBTYPE_RGB555, GST_VIDEO_FORMAT_RGB15},
392 const VIDEOINFOHEADER *vih = (VIDEOINFOHEADER *)mt->pbFormat;
393 GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
394 GstVideoInfo info;
395 unsigned int i;
396 GstCaps *caps;
398 if (!IsEqualGUID(&mt->formattype, &FORMAT_VideoInfo)
399 || mt->cbFormat < sizeof(VIDEOINFOHEADER) || !mt->pbFormat)
400 return NULL;
402 for (i = 0; i < ARRAY_SIZE(format_map); ++i)
404 if (IsEqualGUID(&mt->subtype, format_map[i].subtype))
406 format = format_map[i].format;
407 break;
411 if (format == GST_VIDEO_FORMAT_UNKNOWN)
412 format = gst_video_format_from_fourcc(vih->bmiHeader.biCompression);
414 if (format == GST_VIDEO_FORMAT_UNKNOWN)
416 FIXME("Unknown video format (subtype %s, compression %#x).\n",
417 debugstr_guid(&mt->subtype), vih->bmiHeader.biCompression);
418 return NULL;
421 gst_video_info_set_format(&info, format, vih->bmiHeader.biWidth, vih->bmiHeader.biHeight);
422 if ((caps = gst_video_info_to_caps(&info)))
424 /* Clear some fields that shouldn't prevent us from connecting. */
425 for (i = 0; i < gst_caps_get_size(caps); ++i)
427 gst_structure_remove_fields(gst_caps_get_structure(caps, i),
428 "framerate", "pixel-aspect-ratio", "colorimetry", "chroma-site", NULL);
431 return caps;
434 static GstCaps *amt_to_gst_caps_audio(const AM_MEDIA_TYPE *mt)
436 const WAVEFORMATEX *wfx = (WAVEFORMATEX *)mt->pbFormat;
437 GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN;
438 GstAudioInfo info;
440 if (!IsEqualGUID(&mt->formattype, &FORMAT_WaveFormatEx)
441 || mt->cbFormat < sizeof(WAVEFORMATEX) || !mt->pbFormat)
442 return NULL;
444 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_PCM))
445 format = gst_audio_format_build_integer(wfx->wBitsPerSample != 8,
446 G_LITTLE_ENDIAN, wfx->wBitsPerSample, wfx->wBitsPerSample);
447 else if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_IEEE_FLOAT))
449 if (wfx->wBitsPerSample == 32)
450 format = GST_AUDIO_FORMAT_F32LE;
451 else if (wfx->wBitsPerSample == 64)
452 format = GST_AUDIO_FORMAT_F64LE;
455 if (format == GST_AUDIO_FORMAT_UNKNOWN)
457 FIXME("Unknown audio format (subtype %s, depth %u).\n",
458 debugstr_guid(&mt->subtype), wfx->wBitsPerSample);
459 return NULL;
462 gst_audio_info_set_format(&info, format, wfx->nSamplesPerSec, wfx->nChannels, NULL);
463 return gst_audio_info_to_caps(&info);
466 static GstCaps *amt_to_gst_caps(const AM_MEDIA_TYPE *mt)
468 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Video))
469 return amt_to_gst_caps_video(mt);
470 else if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Audio))
471 return amt_to_gst_caps_audio(mt);
473 FIXME("Unknown major type %s.\n", debugstr_guid(&mt->majortype));
474 return NULL;
477 static gboolean query_sink(GstPad *pad, GstObject *parent, GstQuery *query)
479 struct gstdemux_source *pin = gst_pad_get_element_private(pad);
481 TRACE("pin %p, type \"%s\".\n", pin, gst_query_type_get_name(query->type));
483 switch (query->type)
485 case GST_QUERY_CAPS:
487 GstCaps *caps, *filter, *temp;
489 gst_query_parse_caps(query, &filter);
491 if (pin->pin.pin.peer)
492 caps = amt_to_gst_caps(&pin->pin.pin.mt);
493 else
494 caps = gst_caps_new_any();
495 if (!caps)
496 return FALSE;
498 if (filter)
500 temp = gst_caps_intersect(caps, filter);
501 gst_caps_unref(caps);
502 caps = temp;
505 gst_query_set_caps_result(query, caps);
506 gst_caps_unref(caps);
507 return TRUE;
509 case GST_QUERY_ACCEPT_CAPS:
511 gboolean ret = TRUE;
512 AM_MEDIA_TYPE mt;
513 GstCaps *caps;
515 if (!pin->pin.pin.peer)
517 gst_query_set_accept_caps_result(query, TRUE);
518 return TRUE;
521 gst_query_parse_accept_caps(query, &caps);
522 if (!amt_from_gst_caps(caps, &mt))
523 return FALSE;
525 if (!IsEqualGUID(&mt.majortype, &pin->pin.pin.mt.majortype)
526 || !IsEqualGUID(&mt.subtype, &pin->pin.pin.mt.subtype)
527 || !IsEqualGUID(&mt.formattype, &pin->pin.pin.mt.formattype))
528 ret = FALSE;
530 if (IsEqualGUID(&mt.majortype, &MEDIATYPE_Video))
532 const VIDEOINFOHEADER *req_vih = (VIDEOINFOHEADER *)mt.pbFormat;
533 const VIDEOINFOHEADER *our_vih = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
535 if (req_vih->bmiHeader.biWidth != our_vih->bmiHeader.biWidth
536 || req_vih->bmiHeader.biHeight != our_vih->bmiHeader.biHeight
537 || req_vih->bmiHeader.biBitCount != our_vih->bmiHeader.biBitCount
538 || req_vih->bmiHeader.biCompression != our_vih->bmiHeader.biCompression)
539 ret = FALSE;
541 else if (IsEqualGUID(&mt.majortype, &MEDIATYPE_Audio))
543 const WAVEFORMATEX *req_wfx = (WAVEFORMATEX *)mt.pbFormat;
544 const WAVEFORMATEX *our_wfx = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
546 if (req_wfx->nChannels != our_wfx->nChannels
547 || req_wfx->nSamplesPerSec != our_wfx->nSamplesPerSec
548 || req_wfx->wBitsPerSample != our_wfx->wBitsPerSample)
549 ret = FALSE;
552 FreeMediaType(&mt);
554 if (!ret && WARN_ON(gstreamer))
556 gchar *str = gst_caps_to_string(caps);
557 WARN("Rejecting caps \"%s\".\n", debugstr_a(str));
558 g_free(str);
561 gst_query_set_accept_caps_result(query, ret);
562 return TRUE;
564 default:
565 return gst_pad_query_default (pad, parent, query);
569 static gboolean gst_base_src_perform_seek(struct gstdemux *This, GstEvent *event)
571 gboolean res = TRUE;
572 gdouble rate;
573 GstFormat seek_format;
574 GstSeekFlags flags;
575 GstSeekType cur_type, stop_type;
576 gint64 cur, stop;
577 gboolean flush;
578 guint32 seqnum;
579 GstEvent *tevent;
580 BOOL thread = !!This->push_thread;
582 gst_event_parse_seek(event, &rate, &seek_format, &flags,
583 &cur_type, &cur, &stop_type, &stop);
585 if (seek_format != GST_FORMAT_BYTES)
587 FIXME("Unhandled format \"%s\".\n", gst_format_get_name(seek_format));
588 return FALSE;
591 flush = flags & GST_SEEK_FLAG_FLUSH;
592 seqnum = gst_event_get_seqnum(event);
594 /* send flush start */
595 if (flush) {
596 tevent = gst_event_new_flush_start();
597 gst_event_set_seqnum(tevent, seqnum);
598 gst_pad_push_event(This->my_src, tevent);
599 if (This->reader)
600 IAsyncReader_BeginFlush(This->reader);
601 if (thread)
602 gst_pad_set_active(This->my_src, 1);
605 This->nextofs = This->start = cur;
607 /* and prepare to continue streaming */
608 if (flush) {
609 tevent = gst_event_new_flush_stop(TRUE);
610 gst_event_set_seqnum(tevent, seqnum);
611 gst_pad_push_event(This->my_src, tevent);
612 if (This->reader)
613 IAsyncReader_EndFlush(This->reader);
614 if (thread)
615 gst_pad_set_active(This->my_src, 1);
618 return res;
621 static gboolean event_src(GstPad *pad, GstObject *parent, GstEvent *event)
623 struct gstdemux *This = gst_pad_get_element_private(pad);
625 TRACE("filter %p, type \"%s\".\n", This, GST_EVENT_TYPE_NAME(event));
627 switch (event->type) {
628 case GST_EVENT_SEEK:
629 return gst_base_src_perform_seek(This, event);
630 case GST_EVENT_FLUSH_START:
631 EnterCriticalSection(&This->filter.csFilter);
632 if (This->reader)
633 IAsyncReader_BeginFlush(This->reader);
634 LeaveCriticalSection(&This->filter.csFilter);
635 break;
636 case GST_EVENT_FLUSH_STOP:
637 EnterCriticalSection(&This->filter.csFilter);
638 if (This->reader)
639 IAsyncReader_EndFlush(This->reader);
640 LeaveCriticalSection(&This->filter.csFilter);
641 break;
642 default:
643 WARN("Ignoring \"%s\" event.\n", GST_EVENT_TYPE_NAME(event));
644 case GST_EVENT_TAG:
645 case GST_EVENT_QOS:
646 case GST_EVENT_RECONFIGURE:
647 return gst_pad_event_default(pad, parent, event);
649 return TRUE;
652 static gboolean event_sink(GstPad *pad, GstObject *parent, GstEvent *event)
654 struct gstdemux_source *pin = gst_pad_get_element_private(pad);
655 gboolean ret;
657 TRACE("pin %p, type \"%s\".\n", pin, GST_EVENT_TYPE_NAME(event));
659 switch (event->type) {
660 case GST_EVENT_SEGMENT: {
661 gdouble rate, applied_rate;
662 gint64 stop, pos;
663 const GstSegment *segment;
665 gst_event_parse_segment(event, &segment);
667 pos = segment->position;
668 stop = segment->stop;
669 rate = segment->rate;
670 applied_rate = segment->applied_rate;
672 if (segment->format != GST_FORMAT_TIME)
674 FIXME("Unhandled format \"%s\".\n", gst_format_get_name(segment->format));
675 return TRUE;
678 gst_segment_copy_into(segment, pin->segment);
680 pos /= 100;
682 if (stop > 0)
683 stop /= 100;
685 if (pin->pin.pin.peer)
686 IPin_NewSegment(pin->pin.pin.peer, pos, stop, rate*applied_rate);
688 return TRUE;
690 case GST_EVENT_EOS:
691 if (pin->pin.pin.peer)
692 IPin_EndOfStream(pin->pin.pin.peer);
693 else
694 SetEvent(pin->eos_event);
695 return TRUE;
696 case GST_EVENT_FLUSH_START:
697 if (impl_from_strmbase_filter(pin->pin.pin.filter)->ignore_flush) {
698 /* gst-plugins-base prior to 1.7 contains a bug which causes
699 * our sink pins to receive a flush-start event when the
700 * decodebin changes from PAUSED to READY (including
701 * PLAYING->PAUSED->READY), but no matching flush-stop event is
702 * sent. See <gst-plugins-base.git:60bad4815db966a8e4). Here we
703 * unset the flushing flag to avoid the problem. */
704 TRACE("Working around gst <1.7 bug, ignoring FLUSH_START\n");
705 GST_PAD_UNSET_FLUSHING (pad);
706 return TRUE;
708 if (pin->pin.pin.peer)
709 IPin_BeginFlush(pin->pin.pin.peer);
710 return TRUE;
711 case GST_EVENT_FLUSH_STOP:
712 gst_segment_init(pin->segment, GST_FORMAT_TIME);
713 if (pin->pin.pin.peer)
714 IPin_EndFlush(pin->pin.pin.peer);
715 return TRUE;
716 case GST_EVENT_CAPS:
717 ret = gst_pad_event_default(pad, parent, event);
718 SetEvent(pin->caps_event);
719 return ret;
720 default:
721 WARN("Ignoring \"%s\" event.\n", GST_EVENT_TYPE_NAME(event));
722 return gst_pad_event_default(pad, parent, event);
726 static DWORD CALLBACK push_data(LPVOID iface)
728 LONGLONG maxlen, curlen;
729 struct gstdemux *This = iface;
730 GstMapInfo mapping;
731 GstBuffer *buffer;
732 HRESULT hr;
734 if (!(buffer = gst_buffer_new_allocate(NULL, 16384, NULL)))
736 ERR("Failed to allocate memory.\n");
737 return 0;
740 IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
742 if (!This->stop)
743 IAsyncReader_Length(This->reader, &maxlen, &curlen);
744 else
745 maxlen = This->stop;
747 TRACE("Starting..\n");
748 for (;;) {
749 ULONG len;
750 int ret;
752 if (This->nextofs >= maxlen)
753 break;
754 len = min(16384, maxlen - This->nextofs);
756 if (!gst_buffer_map_range(buffer, -1, len, &mapping, GST_MAP_WRITE))
758 ERR("Failed to map buffer.\n");
759 break;
761 hr = IAsyncReader_SyncRead(This->reader, This->nextofs, len, mapping.data);
762 gst_buffer_unmap(buffer, &mapping);
763 if (hr != S_OK)
765 ERR("Failed to read data, hr %#x.\n", hr);
766 break;
769 This->nextofs += len;
771 buffer->duration = buffer->pts = -1;
772 ret = gst_pad_push(This->my_src, buffer);
773 if (ret >= 0)
774 hr = S_OK;
775 else
776 ERR("Sending returned: %i\n", ret);
777 if (ret == GST_FLOW_ERROR)
778 hr = E_FAIL;
779 else if (ret == GST_FLOW_FLUSHING)
780 hr = VFW_E_WRONG_STATE;
781 if (hr != S_OK)
782 break;
785 gst_buffer_unref(buffer);
787 gst_pad_push_event(This->my_src, gst_event_new_eos());
789 TRACE("Stopping.. %08x\n", hr);
791 IBaseFilter_Release(&This->filter.IBaseFilter_iface);
793 return 0;
796 static GstFlowReturn got_data_sink(GstPad *pad, GstObject *parent, GstBuffer *buf)
798 struct gstdemux_source *pin = gst_pad_get_element_private(pad);
799 struct gstdemux *This = impl_from_strmbase_filter(pin->pin.pin.filter);
800 HRESULT hr;
801 BYTE *ptr = NULL;
802 IMediaSample *sample;
803 GstMapInfo info;
805 TRACE("%p %p\n", pad, buf);
807 if (This->initial) {
808 gst_buffer_unref(buf);
809 return GST_FLOW_OK;
812 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
814 if (hr == VFW_E_NOT_CONNECTED) {
815 gst_buffer_unref(buf);
816 return GST_FLOW_NOT_LINKED;
819 if (FAILED(hr)) {
820 gst_buffer_unref(buf);
821 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
822 return GST_FLOW_FLUSHING;
825 gst_buffer_map(buf, &info, GST_MAP_READ);
827 hr = IMediaSample_SetActualDataLength(sample, info.size);
828 if(FAILED(hr)){
829 WARN("SetActualDataLength failed: %08x\n", hr);
830 return GST_FLOW_FLUSHING;
833 IMediaSample_GetPointer(sample, &ptr);
835 memcpy(ptr, info.data, info.size);
837 gst_buffer_unmap(buf, &info);
839 if (GST_BUFFER_PTS_IS_VALID(buf)) {
840 REFERENCE_TIME rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts);
841 if (rtStart >= 0)
842 rtStart /= 100;
844 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
845 REFERENCE_TIME tStart = buf->pts / 100;
846 REFERENCE_TIME tStop = (buf->pts + buf->duration) / 100;
847 REFERENCE_TIME rtStop;
848 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts + buf->duration);
849 if (rtStop >= 0)
850 rtStop /= 100;
851 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
852 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
853 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
854 } else {
855 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
856 IMediaSample_SetMediaTime(sample, NULL, NULL);
858 } else {
859 IMediaSample_SetTime(sample, NULL, NULL);
860 IMediaSample_SetMediaTime(sample, NULL, NULL);
863 IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
864 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_LIVE));
865 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
867 if (!pin->pin.pin.peer)
868 hr = VFW_E_NOT_CONNECTED;
869 else
870 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
872 TRACE("sending sample returned: %08x\n", hr);
874 gst_buffer_unref(buf);
875 IMediaSample_Release(sample);
877 if (hr == VFW_E_NOT_CONNECTED)
878 return GST_FLOW_NOT_LINKED;
880 if (FAILED(hr))
881 return GST_FLOW_FLUSHING;
883 return GST_FLOW_OK;
886 static GstFlowReturn request_buffer_src(GstPad *pad, GstObject *parent, guint64 ofs, guint len, GstBuffer **buffer)
888 struct gstdemux *This = gst_pad_get_element_private(pad);
889 GstBuffer *new_buffer = NULL;
890 HRESULT hr;
891 GstMapInfo info;
893 TRACE("pad %p, offset %s, length %u, buffer %p.\n", pad, wine_dbgstr_longlong(ofs), len, *buffer);
895 if (ofs == GST_BUFFER_OFFSET_NONE)
896 ofs = This->nextpullofs;
897 if (ofs >= This->filesize) {
898 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
899 return GST_FLOW_EOS;
901 if (len + ofs > This->filesize)
902 len = This->filesize - ofs;
903 This->nextpullofs = ofs + len;
905 if (!*buffer)
906 *buffer = new_buffer = gst_buffer_new_and_alloc(len);
907 gst_buffer_map(*buffer, &info, GST_MAP_WRITE);
908 hr = IAsyncReader_SyncRead(This->reader, ofs, len, info.data);
909 gst_buffer_unmap(*buffer, &info);
910 if (FAILED(hr))
912 ERR("Failed to read data, hr %#x.\n", hr);
913 if (new_buffer)
914 gst_buffer_unref(new_buffer);
915 return GST_FLOW_ERROR;
918 return GST_FLOW_OK;
921 static DWORD CALLBACK push_data_init(LPVOID iface)
923 struct gstdemux *This = iface;
924 DWORD64 ofs = 0;
926 TRACE("Starting..\n");
927 for (;;) {
928 GstBuffer *buf;
929 GstFlowReturn ret = request_buffer_src(This->my_src, NULL, ofs, 4096, &buf);
930 if (ret < 0) {
931 ERR("Obtaining buffer returned: %i\n", ret);
932 break;
934 ret = gst_pad_push(This->my_src, buf);
935 ofs += 4096;
936 if (ret)
937 TRACE("Sending returned: %i\n", ret);
938 if (ret < 0)
939 break;
941 TRACE("Stopping..\n");
942 return 0;
945 static void removed_decoded_pad(GstElement *bin, GstPad *pad, gpointer user)
947 struct gstdemux *filter = user;
948 unsigned int i;
949 char *name;
951 TRACE("filter %p, bin %p, pad %p.\n", filter, bin, pad);
953 for (i = 0; i < filter->source_count; ++i)
955 struct gstdemux_source *pin = filter->sources[i];
957 if (pin->their_src == pad)
959 if (pin->post_sink)
960 gst_pad_unlink(pin->their_src, pin->post_sink);
961 else
962 gst_pad_unlink(pin->their_src, pin->my_sink);
963 gst_object_unref(pin->their_src);
964 pin->their_src = NULL;
965 return;
969 name = gst_pad_get_name(pad);
970 WARN("No pin matching pad %s found.\n", debugstr_a(name));
971 g_free(name);
974 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, struct gstdemux *This)
976 static const WCHAR formatW[] = {'S','t','r','e','a','m',' ','%','0','2','u',0};
977 const char *typename;
978 char *name;
979 GstCaps *caps;
980 GstStructure *arg;
981 struct gstdemux_source *pin;
982 int ret;
983 WCHAR nameW[128];
985 TRACE("%p %p %p\n", This, bin, pad);
987 sprintfW(nameW, formatW, This->source_count);
989 name = gst_pad_get_name(pad);
990 TRACE("Name: %s\n", name);
991 g_free(name);
993 caps = gst_pad_query_caps(pad, NULL);
994 caps = gst_caps_make_writable(caps);
995 arg = gst_caps_get_structure(caps, 0);
996 typename = gst_structure_get_name(arg);
998 if (!(pin = create_pin(This, nameW)))
1000 ERR("Failed to allocate memory.\n");
1001 goto out;
1004 if (!strcmp(typename, "video/x-raw"))
1006 GstElement *deinterlace, *vconv, *flip, *vconv2;
1008 /* DirectShow can express interlaced video, but downstream filters can't
1009 * necessarily consume it. In particular, the video renderer can't. */
1010 if (!(deinterlace = gst_element_factory_make("deinterlace", NULL)))
1012 ERR("Failed to create deinterlace, are %u-bit GStreamer \"good\" plugins installed?\n",
1013 8 * (int)sizeof(void *));
1014 goto out;
1017 /* decodebin considers many YUV formats to be "raw", but some quartz
1018 * filters can't handle those. Also, videoflip can't handle all "raw"
1019 * formats either. Add a videoconvert to swap color spaces. */
1020 if (!(vconv = gst_element_factory_make("videoconvert", NULL)))
1022 ERR("Failed to create videoconvert, are %u-bit GStreamer \"base\" plugins installed?\n",
1023 8 * (int)sizeof(void *));
1024 goto out;
1027 /* Avoid expensive color matrix conversions. */
1028 gst_util_set_object_arg(G_OBJECT(vconv), "matrix-mode", "none");
1030 /* GStreamer outputs RGB video top-down, but DirectShow expects bottom-up. */
1031 if (!(flip = gst_element_factory_make("videoflip", NULL)))
1033 ERR("Failed to create videoflip, are %u-bit GStreamer \"good\" plugins installed?\n",
1034 8 * (int)sizeof(void *));
1035 goto out;
1038 /* videoflip does not support 15 and 16-bit RGB so add a second videoconvert
1039 * to do the final conversion. */
1040 if (!(vconv2 = gst_element_factory_make("videoconvert", NULL)))
1042 ERR("Failed to create videoconvert, are %u-bit GStreamer \"base\" plugins installed?\n",
1043 8 * (int)sizeof(void *));
1044 goto out;
1047 /* Avoid expensive color matrix conversions. */
1048 gst_util_set_object_arg(G_OBJECT(vconv2), "matrix-mode", "none");
1050 /* The bin takes ownership of these elements. */
1051 gst_bin_add(GST_BIN(This->container), deinterlace);
1052 gst_element_sync_state_with_parent(deinterlace);
1053 gst_bin_add(GST_BIN(This->container), vconv);
1054 gst_element_sync_state_with_parent(vconv);
1055 gst_bin_add(GST_BIN(This->container), flip);
1056 gst_element_sync_state_with_parent(flip);
1057 gst_bin_add(GST_BIN(This->container), vconv2);
1058 gst_element_sync_state_with_parent(vconv2);
1060 gst_element_link(deinterlace, vconv);
1061 gst_element_link(vconv, flip);
1062 gst_element_link(flip, vconv2);
1064 pin->post_sink = gst_element_get_static_pad(deinterlace, "sink");
1065 pin->post_src = gst_element_get_static_pad(vconv2, "src");
1066 pin->flip = flip;
1068 else if (!strcmp(typename, "audio/x-raw"))
1070 GstElement *convert;
1072 /* Currently our dsound can't handle 64-bit formats or all
1073 * surround-sound configurations. Native dsound can't always handle
1074 * 64-bit formats either. Add an audioconvert to allow changing bit
1075 * depth and channel count. */
1076 if (!(convert = gst_element_factory_make("audioconvert", NULL)))
1078 ERR("Failed to create audioconvert, are %u-bit GStreamer \"base\" plugins installed?\n",
1079 8 * (int)sizeof(void *));
1080 goto out;
1083 gst_bin_add(GST_BIN(This->container), convert);
1084 gst_element_sync_state_with_parent(convert);
1086 pin->post_sink = gst_element_get_static_pad(convert, "sink");
1087 pin->post_src = gst_element_get_static_pad(convert, "src");
1090 if (pin->post_sink)
1092 if ((ret = gst_pad_link(pad, pin->post_sink)) < 0)
1094 ERR("Failed to link decodebin source pad to post-processing elements, error %s.\n",
1095 gst_pad_link_get_name(ret));
1096 gst_object_unref(pin->post_sink);
1097 pin->post_sink = NULL;
1098 goto out;
1101 if ((ret = gst_pad_link(pin->post_src, pin->my_sink)) < 0)
1103 ERR("Failed to link post-processing elements to our sink pad, error %s.\n",
1104 gst_pad_link_get_name(ret));
1105 gst_object_unref(pin->post_src);
1106 pin->post_src = NULL;
1107 gst_object_unref(pin->post_sink);
1108 pin->post_sink = NULL;
1109 goto out;
1112 else if ((ret = gst_pad_link(pad, pin->my_sink)) < 0)
1114 ERR("Failed to link decodebin source pad to our sink pad, error %s.\n",
1115 gst_pad_link_get_name(ret));
1116 goto out;
1119 gst_pad_set_active(pin->my_sink, 1);
1120 gst_object_ref(pin->their_src = pad);
1121 out:
1122 gst_caps_unref(caps);
1125 static void existing_new_pad(GstElement *bin, GstPad *pad, gpointer user)
1127 struct gstdemux *This = user;
1128 unsigned int i;
1129 int ret;
1131 TRACE("%p %p %p\n", This, bin, pad);
1133 if (gst_pad_is_linked(pad))
1134 return;
1136 /* Still holding our own lock */
1137 if (This->initial) {
1138 init_new_decoded_pad(bin, pad, This);
1139 return;
1142 for (i = 0; i < This->source_count; ++i)
1144 struct gstdemux_source *pin = This->sources[i];
1145 if (!pin->their_src) {
1146 gst_segment_init(pin->segment, GST_FORMAT_TIME);
1148 if (pin->post_sink)
1149 ret = gst_pad_link(pad, pin->post_sink);
1150 else
1151 ret = gst_pad_link(pad, pin->my_sink);
1153 if (ret >= 0) {
1154 pin->their_src = pad;
1155 gst_object_ref(pin->their_src);
1156 TRACE("Relinked\n");
1157 return;
1161 init_new_decoded_pad(bin, pad, This);
1164 static gboolean query_function(GstPad *pad, GstObject *parent, GstQuery *query)
1166 struct gstdemux *This = gst_pad_get_element_private(pad);
1167 GstFormat format;
1169 TRACE("filter %p, type %s.\n", This, GST_QUERY_TYPE_NAME(query));
1171 switch (GST_QUERY_TYPE(query)) {
1172 case GST_QUERY_DURATION:
1173 gst_query_parse_duration(query, &format, NULL);
1174 if (format == GST_FORMAT_PERCENT)
1176 gst_query_set_duration(query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
1177 return TRUE;
1179 else if (format == GST_FORMAT_BYTES)
1181 gst_query_set_duration(query, GST_FORMAT_BYTES, This->filesize);
1182 return TRUE;
1184 return FALSE;
1185 case GST_QUERY_SEEKING:
1186 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1187 if (format != GST_FORMAT_BYTES)
1189 WARN("Cannot seek using format \"%s\".\n", gst_format_get_name(format));
1190 return FALSE;
1192 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
1193 return TRUE;
1194 case GST_QUERY_SCHEDULING:
1195 gst_query_set_scheduling(query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
1196 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PUSH);
1197 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PULL);
1198 return TRUE;
1199 default:
1200 WARN("Unhandled query type %s.\n", GST_QUERY_TYPE_NAME(query));
1201 return FALSE;
1205 static gboolean activate_push(GstPad *pad, gboolean activate)
1207 struct gstdemux *This = gst_pad_get_element_private(pad);
1209 EnterCriticalSection(&This->filter.csFilter);
1210 if (!activate) {
1211 TRACE("Deactivating\n");
1212 if (!This->initial)
1213 IAsyncReader_BeginFlush(This->reader);
1214 if (This->push_thread) {
1215 WaitForSingleObject(This->push_thread, -1);
1216 CloseHandle(This->push_thread);
1217 This->push_thread = NULL;
1219 if (!This->initial)
1220 IAsyncReader_EndFlush(This->reader);
1221 if (This->filter.state == State_Stopped)
1222 This->nextofs = This->start;
1223 } else if (!This->push_thread) {
1224 TRACE("Activating\n");
1225 if (This->initial)
1226 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
1227 else
1228 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
1230 LeaveCriticalSection(&This->filter.csFilter);
1231 return TRUE;
1234 static gboolean activate_mode(GstPad *pad, GstObject *parent, GstPadMode mode, gboolean activate)
1236 struct gstdemux *filter = gst_pad_get_element_private(pad);
1238 TRACE("%s source pad for filter %p in %s mode.\n",
1239 activate ? "Activating" : "Deactivating", filter, gst_pad_mode_get_name(mode));
1241 switch (mode) {
1242 case GST_PAD_MODE_PULL:
1243 return TRUE;
1244 case GST_PAD_MODE_PUSH:
1245 return activate_push(pad, activate);
1246 default:
1247 return FALSE;
1249 return FALSE;
1252 static void no_more_pads(GstElement *decodebin, gpointer user)
1254 struct gstdemux *filter = user;
1255 TRACE("filter %p.\n", filter);
1256 SetEvent(filter->no_more_pads_event);
1259 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, gpointer user)
1261 const char *name = gst_element_factory_get_longname(fact);
1263 if (strstr(name, "Player protection")) {
1264 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
1265 return GST_AUTOPLUG_SELECT_SKIP;
1267 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder")) {
1268 WARN("Disabled video acceleration since it breaks in wine\n");
1269 return GST_AUTOPLUG_SELECT_SKIP;
1271 TRACE("using \"%s\"\n", name);
1272 return GST_AUTOPLUG_SELECT_TRY;
1275 static GstBusSyncReply watch_bus(GstBus *bus, GstMessage *msg, gpointer data)
1277 struct gstdemux *filter = data;
1278 GError *err = NULL;
1279 gchar *dbg_info = NULL;
1281 TRACE("filter %p, message type %s.\n", filter, GST_MESSAGE_TYPE_NAME(msg));
1283 switch (msg->type)
1285 case GST_MESSAGE_ERROR:
1286 gst_message_parse_error(msg, &err, &dbg_info);
1287 ERR("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1288 ERR("%s\n", dbg_info);
1289 g_error_free(err);
1290 g_free(dbg_info);
1291 SetEvent(filter->error_event);
1292 break;
1293 case GST_MESSAGE_WARNING:
1294 gst_message_parse_warning(msg, &err, &dbg_info);
1295 WARN("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1296 WARN("%s\n", dbg_info);
1297 g_error_free(err);
1298 g_free(dbg_info);
1299 break;
1300 case GST_MESSAGE_DURATION_CHANGED:
1301 SetEvent(filter->duration_event);
1302 break;
1303 default:
1304 break;
1306 gst_message_unref(msg);
1307 return GST_BUS_DROP;
1310 static void unknown_type(GstElement *bin, GstPad *pad, GstCaps *caps, gpointer user)
1312 gchar *strcaps = gst_caps_to_string(caps);
1313 ERR("Could not find a filter for caps: %s\n", debugstr_a(strcaps));
1314 g_free(strcaps);
1317 static HRESULT GST_Connect(struct gstdemux *This, IPin *pConnectPin)
1319 LONGLONG avail;
1320 GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
1321 "quartz_src",
1322 GST_PAD_SRC,
1323 GST_PAD_ALWAYS,
1324 GST_STATIC_CAPS_ANY);
1326 IAsyncReader_Length(This->reader, &This->filesize, &avail);
1328 if (!This->bus) {
1329 This->bus = gst_bus_new();
1330 gst_bus_set_sync_handler(This->bus, watch_bus_wrapper, This, NULL);
1333 This->container = gst_bin_new(NULL);
1334 gst_element_set_bus(This->container, This->bus);
1336 This->my_src = gst_pad_new_from_static_template(&src_template, "quartz-src");
1337 gst_pad_set_getrange_function(This->my_src, request_buffer_src_wrapper);
1338 gst_pad_set_query_function(This->my_src, query_function_wrapper);
1339 gst_pad_set_activatemode_function(This->my_src, activate_mode_wrapper);
1340 gst_pad_set_event_function(This->my_src, event_src_wrapper);
1341 gst_pad_set_element_private (This->my_src, This);
1343 This->start = This->nextofs = This->nextpullofs = This->stop = 0;
1345 This->initial = TRUE;
1346 if (!This->init_gst(This))
1347 return E_FAIL;
1348 This->initial = FALSE;
1350 This->nextofs = This->nextpullofs = 0;
1351 return S_OK;
1354 static LONGLONG query_duration(GstPad *pad)
1356 gint64 duration, byte_length;
1358 if (gst_pad_query_duration(pad, GST_FORMAT_TIME, &duration))
1359 return duration / 100;
1361 WARN("Failed to query time duration; trying to convert from byte length.\n");
1363 /* To accurately get a duration for the stream, we want to only consider the
1364 * length of that stream. Hence, query for the pad duration, instead of
1365 * using the file duration. */
1366 if (gst_pad_query_duration(pad, GST_FORMAT_BYTES, &byte_length)
1367 && gst_pad_query_convert(pad, GST_FORMAT_BYTES, byte_length, GST_FORMAT_TIME, &duration))
1368 return duration / 100;
1370 ERR("Failed to query duration.\n");
1371 return 0;
1374 static inline struct gstdemux_source *impl_from_IMediaSeeking(IMediaSeeking *iface)
1376 return CONTAINING_RECORD(iface, struct gstdemux_source, seek.IMediaSeeking_iface);
1379 static struct strmbase_pin *gstdemux_get_pin(struct strmbase_filter *base, unsigned int index)
1381 struct gstdemux *filter = impl_from_strmbase_filter(base);
1383 if (filter->enum_sink_first)
1385 if (!index)
1386 return &filter->sink.pin;
1387 else if (index <= filter->source_count)
1388 return &filter->sources[index - 1]->pin.pin;
1390 else
1392 if (index < filter->source_count)
1393 return &filter->sources[index]->pin.pin;
1394 else if (index == filter->source_count)
1395 return &filter->sink.pin;
1397 return NULL;
1400 static void gstdemux_destroy(struct strmbase_filter *iface)
1402 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1403 HRESULT hr;
1405 CloseHandle(filter->no_more_pads_event);
1406 CloseHandle(filter->duration_event);
1407 CloseHandle(filter->error_event);
1409 /* Don't need to clean up output pins, disconnecting input pin will do that */
1410 if (filter->sink.pin.peer)
1412 hr = IPin_Disconnect(filter->sink.pin.peer);
1413 assert(hr == S_OK);
1414 hr = IPin_Disconnect(&filter->sink.pin.IPin_iface);
1415 assert(hr == S_OK);
1418 if (filter->reader)
1419 IAsyncReader_Release(filter->reader);
1420 filter->reader = NULL;
1422 if (filter->bus)
1424 gst_bus_set_sync_handler(filter->bus, NULL, NULL, NULL);
1425 gst_object_unref(filter->bus);
1427 strmbase_sink_cleanup(&filter->sink);
1428 strmbase_filter_cleanup(&filter->filter);
1429 heap_free(filter);
1432 static HRESULT gstdemux_init_stream(struct strmbase_filter *iface)
1434 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1435 const SourceSeeking *seeking;
1436 GstStateChangeReturn ret;
1437 unsigned int i;
1439 if (!filter->container)
1440 return S_OK;
1442 for (i = 0; i < filter->source_count; ++i)
1444 HRESULT hr;
1446 if (filter->sources[i]->pin.pin.peer && FAILED(hr = IMemAllocator_Commit(filter->sources[i]->pin.pAllocator)))
1447 ERR("Failed to commit allocator, hr %#x.\n", hr);
1450 if (filter->no_more_pads_event)
1451 ResetEvent(filter->no_more_pads_event);
1453 if ((ret = gst_element_set_state(filter->container, GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE)
1455 ERR("Failed to pause stream.\n");
1456 return E_FAIL;
1459 /* Make sure that all of our pads are connected before returning, lest we
1460 * e.g. try to seek and fail. */
1461 if (filter->no_more_pads_event)
1462 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
1464 seeking = &filter->sources[0]->seek;
1466 /* GStreamer can't seek while stopped, and it resets position to the
1467 * beginning of the stream every time it is stopped. */
1468 if (seeking->llCurrent)
1470 GstSeekType stop_type = GST_SEEK_TYPE_NONE;
1472 if (seeking->llStop && seeking->llStop != seeking->llDuration)
1473 stop_type = GST_SEEK_TYPE_SET;
1475 gst_pad_push_event(filter->sources[0]->my_sink, gst_event_new_seek(
1476 seeking->dRate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
1477 GST_SEEK_TYPE_SET, seeking->llCurrent * 100,
1478 stop_type, seeking->llStop * 100));
1481 return S_OK;
1484 static HRESULT gstdemux_start_stream(struct strmbase_filter *iface, REFERENCE_TIME time)
1486 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1487 GstStateChangeReturn ret;
1489 if (!filter->container)
1490 return S_OK;
1492 if ((ret = gst_element_set_state(filter->container, GST_STATE_PLAYING)) == GST_STATE_CHANGE_FAILURE)
1494 ERR("Failed to play stream.\n");
1495 return E_FAIL;
1497 else if (ret == GST_STATE_CHANGE_ASYNC)
1498 return S_FALSE;
1499 return S_OK;
1502 static HRESULT gstdemux_stop_stream(struct strmbase_filter *iface)
1504 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1505 GstStateChangeReturn ret;
1507 if (!filter->container)
1508 return S_OK;
1510 if ((ret = gst_element_set_state(filter->container, GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE)
1512 ERR("Failed to pause stream.\n");
1513 return E_FAIL;
1515 else if (ret == GST_STATE_CHANGE_ASYNC)
1516 return S_FALSE;
1517 return S_OK;
1520 static HRESULT gstdemux_cleanup_stream(struct strmbase_filter *iface)
1522 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1523 GstStateChangeReturn ret;
1524 unsigned int i;
1526 if (!filter->container)
1527 return S_OK;
1529 filter->ignore_flush = TRUE;
1530 if ((ret = gst_element_set_state(filter->container, GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE)
1532 ERR("Failed to pause stream.\n");
1533 return E_FAIL;
1535 gst_element_get_state(filter->container, NULL, NULL, GST_CLOCK_TIME_NONE);
1536 filter->ignore_flush = FALSE;
1538 for (i = 0; i < filter->source_count; ++i)
1540 if (filter->sources[i]->pin.pin.peer)
1541 IMemAllocator_Decommit(filter->sources[i]->pin.pAllocator);
1544 return S_OK;
1547 static HRESULT gstdemux_wait_state(struct strmbase_filter *iface, DWORD timeout)
1549 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1550 GstStateChangeReturn ret;
1552 if (!filter->container)
1553 return S_OK;
1555 ret = gst_element_get_state(filter->container, NULL, NULL,
1556 timeout == INFINITE ? GST_CLOCK_TIME_NONE : timeout * 1000000);
1557 if (ret == GST_STATE_CHANGE_FAILURE)
1559 ERR("Failed to get state.\n");
1560 return E_FAIL;
1562 else if (ret == GST_STATE_CHANGE_ASYNC)
1563 return VFW_S_STATE_INTERMEDIATE;
1564 return S_OK;
1567 static const struct strmbase_filter_ops filter_ops =
1569 .filter_get_pin = gstdemux_get_pin,
1570 .filter_destroy = gstdemux_destroy,
1571 .filter_init_stream = gstdemux_init_stream,
1572 .filter_start_stream = gstdemux_start_stream,
1573 .filter_stop_stream = gstdemux_stop_stream,
1574 .filter_cleanup_stream = gstdemux_cleanup_stream,
1575 .filter_wait_state = gstdemux_wait_state,
1578 static inline struct gstdemux *impl_from_strmbase_sink(struct strmbase_sink *iface)
1580 return CONTAINING_RECORD(iface, struct gstdemux, sink);
1583 static HRESULT sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1585 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
1586 return S_OK;
1587 return S_FALSE;
1590 static HRESULT gstdemux_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *pmt)
1592 struct gstdemux *filter = impl_from_strmbase_sink(iface);
1593 HRESULT hr = S_OK;
1595 mark_wine_thread();
1597 filter->reader = NULL;
1598 if (FAILED(hr = IPin_QueryInterface(peer, &IID_IAsyncReader, (void **)&filter->reader)))
1599 return hr;
1601 if (FAILED(hr = GST_Connect(filter, peer)))
1602 goto err;
1604 return S_OK;
1605 err:
1606 GST_RemoveOutputPins(filter);
1607 IAsyncReader_Release(filter->reader);
1608 filter->reader = NULL;
1609 return hr;
1612 static void gstdemux_sink_disconnect(struct strmbase_sink *iface)
1614 struct gstdemux *filter = impl_from_strmbase_sink(iface);
1616 mark_wine_thread();
1618 GST_RemoveOutputPins(filter);
1620 IAsyncReader_Release(filter->reader);
1621 filter->reader = NULL;
1624 static const struct strmbase_sink_ops sink_ops =
1626 .base.pin_query_accept = sink_query_accept,
1627 .sink_connect = gstdemux_sink_connect,
1628 .sink_disconnect = gstdemux_sink_disconnect,
1631 static BOOL gstdecoder_init_gst(struct gstdemux *filter)
1633 GstElement *element = gst_element_factory_make("decodebin", NULL);
1634 unsigned int i;
1635 int ret;
1637 if (!element)
1639 ERR("Failed to create decodebin; are %u-bit GStreamer \"base\" plugins installed?\n",
1640 8 * (int)sizeof(void*));
1641 return FALSE;
1644 gst_bin_add(GST_BIN(filter->container), element);
1646 g_signal_connect(element, "pad-added", G_CALLBACK(existing_new_pad_wrapper), filter);
1647 g_signal_connect(element, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), filter);
1648 g_signal_connect(element, "autoplug-select", G_CALLBACK(autoplug_blacklist_wrapper), filter);
1649 g_signal_connect(element, "unknown-type", G_CALLBACK(unknown_type_wrapper), filter);
1650 g_signal_connect(element, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), filter);
1652 filter->their_sink = gst_element_get_static_pad(element, "sink");
1653 ResetEvent(filter->no_more_pads_event);
1655 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
1657 ERR("Failed to link pads, error %d.\n", ret);
1658 return FALSE;
1661 gst_element_set_state(filter->container, GST_STATE_PLAYING);
1662 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
1663 if (ret == GST_STATE_CHANGE_FAILURE)
1665 ERR("Failed to play stream.\n");
1666 return FALSE;
1669 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
1671 for (i = 0; i < filter->source_count; ++i)
1673 struct gstdemux_source *pin = filter->sources[i];
1674 const HANDLE events[2] = {pin->caps_event, filter->error_event};
1676 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
1677 pin->seek.llCurrent = 0;
1678 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
1679 return FALSE;
1682 filter->ignore_flush = TRUE;
1683 gst_element_set_state(filter->container, GST_STATE_READY);
1684 gst_element_get_state(filter->container, NULL, NULL, -1);
1685 filter->ignore_flush = FALSE;
1687 return TRUE;
1690 static HRESULT gstdecoder_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
1692 /* At least make sure we can convert it to GstCaps. */
1693 GstCaps *caps = amt_to_gst_caps(mt);
1695 if (!caps)
1696 return S_FALSE;
1697 gst_caps_unref(caps);
1698 return S_OK;
1701 static HRESULT gstdecoder_source_get_media_type(struct gstdemux_source *pin,
1702 unsigned int index, AM_MEDIA_TYPE *mt)
1704 GstCaps *caps = gst_pad_get_current_caps(pin->my_sink);
1705 const GstStructure *structure;
1706 const char *type;
1708 static const GstVideoFormat video_formats[] =
1710 /* Try to prefer YUV formats over RGB ones. Most decoders output in the
1711 * YUV color space, and it's generally much less expensive for
1712 * videoconvert to do YUV -> YUV transformations. */
1713 GST_VIDEO_FORMAT_AYUV,
1714 GST_VIDEO_FORMAT_I420,
1715 GST_VIDEO_FORMAT_YV12,
1716 GST_VIDEO_FORMAT_YUY2,
1717 GST_VIDEO_FORMAT_UYVY,
1718 GST_VIDEO_FORMAT_YVYU,
1719 GST_VIDEO_FORMAT_NV12,
1720 GST_VIDEO_FORMAT_BGRA,
1721 GST_VIDEO_FORMAT_BGRx,
1722 GST_VIDEO_FORMAT_BGR,
1723 GST_VIDEO_FORMAT_RGB16,
1724 GST_VIDEO_FORMAT_RGB15,
1727 assert(caps); /* We shouldn't be able to get here if caps haven't been set. */
1728 structure = gst_caps_get_structure(caps, 0);
1729 type = gst_structure_get_name(structure);
1731 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
1733 if (amt_from_gst_caps(caps, mt))
1735 if (!index--)
1737 gst_caps_unref(caps);
1738 return S_OK;
1740 FreeMediaType(mt);
1743 if (!strcmp(type, "video/x-raw") && index < ARRAY_SIZE(video_formats))
1745 gint width, height, fps_n, fps_d;
1746 GstVideoInfo info;
1748 gst_caps_unref(caps);
1749 gst_structure_get_int(structure, "width", &width);
1750 gst_structure_get_int(structure, "height", &height);
1751 gst_video_info_set_format(&info, video_formats[index], width, height);
1752 if (gst_structure_get_fraction(structure, "framerate", &fps_n, &fps_d) && fps_n)
1754 info.fps_n = fps_n;
1755 info.fps_d = fps_d;
1757 if (!amt_from_gst_video_info(&info, mt))
1758 return E_OUTOFMEMORY;
1759 return S_OK;
1761 else if (!strcmp(type, "audio/x-raw") && !index)
1763 GstAudioInfo info;
1764 gint rate;
1766 gst_caps_unref(caps);
1767 gst_structure_get_int(structure, "rate", &rate);
1768 gst_audio_info_set_format(&info, GST_AUDIO_FORMAT_S16LE, rate, 2, NULL);
1769 if (!amt_from_gst_audio_info(&info, mt))
1770 return E_OUTOFMEMORY;
1771 return S_OK;
1774 gst_caps_unref(caps);
1775 return VFW_S_NO_MORE_ITEMS;
1778 HRESULT gstdemux_create(IUnknown *outer, IUnknown **out)
1780 struct gstdemux *object;
1782 if (!init_gstreamer())
1783 return E_FAIL;
1785 mark_wine_thread();
1787 if (!(object = heap_alloc_zero(sizeof(*object))))
1788 return E_OUTOFMEMORY;
1790 strmbase_filter_init(&object->filter, outer, &CLSID_Gstreamer_Splitter, &filter_ops);
1791 strmbase_sink_init(&object->sink, &object->filter, wcsInputPinName, &sink_ops, NULL);
1793 object->no_more_pads_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1794 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
1795 object->init_gst = gstdecoder_init_gst;
1796 object->source_query_accept = gstdecoder_source_query_accept;
1797 object->source_get_media_type = gstdecoder_source_get_media_type;
1799 TRACE("Created GStreamer demuxer %p.\n", object);
1800 *out = &object->filter.IUnknown_inner;
1801 return S_OK;
1804 static struct gstdemux *impl_from_IAMStreamSelect(IAMStreamSelect *iface)
1806 return CONTAINING_RECORD(iface, struct gstdemux, IAMStreamSelect_iface);
1809 static HRESULT WINAPI stream_select_QueryInterface(IAMStreamSelect *iface, REFIID iid, void **out)
1811 struct gstdemux *filter = impl_from_IAMStreamSelect(iface);
1812 return IUnknown_QueryInterface(filter->filter.outer_unk, iid, out);
1815 static ULONG WINAPI stream_select_AddRef(IAMStreamSelect *iface)
1817 struct gstdemux *filter = impl_from_IAMStreamSelect(iface);
1818 return IUnknown_AddRef(filter->filter.outer_unk);
1821 static ULONG WINAPI stream_select_Release(IAMStreamSelect *iface)
1823 struct gstdemux *filter = impl_from_IAMStreamSelect(iface);
1824 return IUnknown_Release(filter->filter.outer_unk);
1827 static HRESULT WINAPI stream_select_Count(IAMStreamSelect *iface, DWORD *count)
1829 FIXME("iface %p, count %p, stub!\n", iface, count);
1830 return E_NOTIMPL;
1833 static HRESULT WINAPI stream_select_Info(IAMStreamSelect *iface, LONG index,
1834 AM_MEDIA_TYPE **mt, DWORD *flags, LCID *lcid, DWORD *group, WCHAR **name,
1835 IUnknown **object, IUnknown **unknown)
1837 FIXME("iface %p, index %d, mt %p, flags %p, lcid %p, group %p, name %p, object %p, unknown %p, stub!\n",
1838 iface, index, mt, flags, lcid, group, name, object, unknown);
1839 return E_NOTIMPL;
1842 static HRESULT WINAPI stream_select_Enable(IAMStreamSelect *iface, LONG index, DWORD flags)
1844 FIXME("iface %p, index %d, flags %#x, stub!\n", iface, index, flags);
1845 return E_NOTIMPL;
1848 static const IAMStreamSelectVtbl stream_select_vtbl =
1850 stream_select_QueryInterface,
1851 stream_select_AddRef,
1852 stream_select_Release,
1853 stream_select_Count,
1854 stream_select_Info,
1855 stream_select_Enable,
1858 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1860 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1861 TRACE("(%p)\n", This);
1862 return S_OK;
1865 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1867 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1868 TRACE("(%p)\n", This);
1869 return S_OK;
1872 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1874 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1875 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1876 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1877 mark_wine_thread();
1878 gst_pad_push_event(This->my_sink, ev);
1879 return S_OK;
1882 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1884 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1885 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1888 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1890 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1891 return IPin_AddRef(&This->pin.pin.IPin_iface);
1894 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1896 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1897 return IPin_Release(&This->pin.pin.IPin_iface);
1900 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1901 LONGLONG *current, DWORD current_flags, LONGLONG *stop, DWORD stop_flags)
1903 GstSeekType current_type = GST_SEEK_TYPE_SET, stop_type = GST_SEEK_TYPE_SET;
1904 struct gstdemux_source *pin = impl_from_IMediaSeeking(iface);
1905 GstSeekFlags flags = 0;
1907 TRACE("pin %p, current %s, current_flags %#x, stop %s, stop_flags %#x.\n",
1908 pin, current ? debugstr_time(*current) : "<null>", current_flags,
1909 stop ? debugstr_time(*stop) : "<null>", stop_flags);
1911 mark_wine_thread();
1913 SourceSeekingImpl_SetPositions(iface, current, current_flags, stop, stop_flags);
1914 if (pin->pin.pin.filter->state == State_Stopped)
1915 return S_OK;
1917 if (current_flags & AM_SEEKING_SeekToKeyFrame)
1918 flags |= GST_SEEK_FLAG_KEY_UNIT;
1919 if (current_flags & AM_SEEKING_Segment)
1920 flags |= GST_SEEK_FLAG_SEGMENT;
1921 if (!(current_flags & AM_SEEKING_NoFlush))
1922 flags |= GST_SEEK_FLAG_FLUSH;
1924 if ((current_flags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_NoPositioning)
1925 current_type = GST_SEEK_TYPE_NONE;
1926 if ((stop_flags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_NoPositioning)
1927 stop_type = GST_SEEK_TYPE_NONE;
1929 if (!gst_pad_push_event(pin->my_sink, gst_event_new_seek(pin->seek.dRate, GST_FORMAT_TIME, flags,
1930 current_type, pin->seek.llCurrent * 100, stop_type, pin->seek.llStop * 100)))
1932 ERR("Failed to seek (current %s, stop %s).\n",
1933 debugstr_time(pin->seek.llCurrent), debugstr_time(pin->seek.llStop));
1934 return E_FAIL;
1936 return S_OK;
1939 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1941 GST_Seeking_QueryInterface,
1942 GST_Seeking_AddRef,
1943 GST_Seeking_Release,
1944 SourceSeekingImpl_GetCapabilities,
1945 SourceSeekingImpl_CheckCapabilities,
1946 SourceSeekingImpl_IsFormatSupported,
1947 SourceSeekingImpl_QueryPreferredFormat,
1948 SourceSeekingImpl_GetTimeFormat,
1949 SourceSeekingImpl_IsUsingTimeFormat,
1950 SourceSeekingImpl_SetTimeFormat,
1951 SourceSeekingImpl_GetDuration,
1952 SourceSeekingImpl_GetStopPosition,
1953 SourceSeekingImpl_GetCurrentPosition,
1954 SourceSeekingImpl_ConvertTimeFormat,
1955 GST_Seeking_SetPositions,
1956 SourceSeekingImpl_GetPositions,
1957 SourceSeekingImpl_GetAvailable,
1958 SourceSeekingImpl_SetRate,
1959 SourceSeekingImpl_GetRate,
1960 SourceSeekingImpl_GetPreroll
1963 static inline struct gstdemux_source *impl_from_IQualityControl( IQualityControl *iface )
1965 return CONTAINING_RECORD(iface, struct gstdemux_source, IQualityControl_iface);
1968 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1970 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
1971 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1974 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1976 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
1977 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1980 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1982 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
1983 return IPin_Release(&pin->pin.pin.IPin_iface);
1986 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality q)
1988 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
1989 GstQOSType type = GST_QOS_TYPE_OVERFLOW;
1990 GstClockTime timestamp;
1991 GstClockTimeDiff diff;
1992 GstEvent *event;
1994 TRACE("pin %p, sender %p, type %s, proportion %u, late %s, timestamp %s.\n",
1995 pin, sender, q.Type == Famine ? "Famine" : "Flood", q.Proportion,
1996 debugstr_time(q.Late), debugstr_time(q.TimeStamp));
1998 mark_wine_thread();
2000 /* GST_QOS_TYPE_OVERFLOW is also used for buffers that arrive on time, but
2001 * DirectShow filters might use Famine, so check that there actually is an
2002 * underrun. */
2003 if (q.Type == Famine && q.Proportion < 1000)
2004 type = GST_QOS_TYPE_UNDERFLOW;
2006 /* DirectShow filters sometimes pass negative timestamps (Audiosurf uses the
2007 * current time instead of the time of the last buffer). GstClockTime is
2008 * unsigned, so clamp it to 0. */
2009 timestamp = max(q.TimeStamp * 100, 0);
2011 /* The documentation specifies that timestamp + diff must be nonnegative. */
2012 diff = q.Late * 100;
2013 if (diff < 0 && timestamp < (GstClockTime)-diff)
2014 diff = -timestamp;
2016 /* DirectShow "Proportion" describes what percentage of buffers the upstream
2017 * filter should keep (i.e. dropping the rest). If frames are late, the
2018 * proportion will be less than 1. For example, a proportion of 500 means
2019 * that the element should drop half of its frames, essentially because
2020 * frames are taking twice as long as they should to arrive.
2022 * GStreamer "proportion" is the inverse of this; it describes how much
2023 * faster the upstream element should produce frames. I.e. if frames are
2024 * taking twice as long as they should to arrive, we want the frames to be
2025 * decoded twice as fast, and so we pass 2.0 to GStreamer. */
2027 if (!q.Proportion)
2029 WARN("Ignoring quality message with zero proportion.\n");
2030 return S_OK;
2033 if (!(event = gst_event_new_qos(type, 1000.0 / q.Proportion, diff, timestamp)))
2034 ERR("Failed to create QOS event.\n");
2036 gst_pad_push_event(pin->my_sink, event);
2038 return S_OK;
2041 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
2043 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
2044 TRACE("(%p)->(%p)\n", pin, pin);
2045 /* Do nothing */
2046 return S_OK;
2049 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
2050 GST_QualityControl_QueryInterface,
2051 GST_QualityControl_AddRef,
2052 GST_QualityControl_Release,
2053 GST_QualityControl_Notify,
2054 GST_QualityControl_SetSink
2057 static inline struct gstdemux_source *impl_source_from_IPin(IPin *iface)
2059 return CONTAINING_RECORD(iface, struct gstdemux_source, pin.pin.IPin_iface);
2062 static HRESULT source_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
2064 struct gstdemux_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2066 if (IsEqualGUID(iid, &IID_IMediaSeeking))
2067 *out = &pin->seek.IMediaSeeking_iface;
2068 else if (IsEqualGUID(iid, &IID_IQualityControl))
2069 *out = &pin->IQualityControl_iface;
2070 else
2071 return E_NOINTERFACE;
2073 IUnknown_AddRef((IUnknown *)*out);
2074 return S_OK;
2077 static HRESULT source_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2079 struct gstdemux_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2080 struct gstdemux *filter = impl_from_strmbase_filter(iface->filter);
2081 return filter->source_query_accept(pin, mt);
2084 static HRESULT source_get_media_type(struct strmbase_pin *iface, unsigned int index, AM_MEDIA_TYPE *mt)
2086 struct gstdemux_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2087 struct gstdemux *filter = impl_from_strmbase_filter(iface->filter);
2088 return filter->source_get_media_type(pin, index, mt);
2091 static HRESULT WINAPI GSTOutPin_DecideBufferSize(struct strmbase_source *iface,
2092 IMemAllocator *allocator, ALLOCATOR_PROPERTIES *props)
2094 struct gstdemux_source *pin = impl_source_from_IPin(&iface->pin.IPin_iface);
2095 unsigned int buffer_size = 16384;
2096 ALLOCATOR_PROPERTIES ret_props;
2098 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_VideoInfo))
2100 VIDEOINFOHEADER *format = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
2101 buffer_size = format->bmiHeader.biSizeImage;
2103 gst_util_set_object_arg(G_OBJECT(pin->flip), "method",
2104 (format->bmiHeader.biCompression == BI_RGB
2105 || format->bmiHeader.biCompression == BI_BITFIELDS) ? "vertical-flip" : "none");
2107 else if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
2108 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
2109 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
2111 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
2112 buffer_size = format->nAvgBytesPerSec;
2115 props->cBuffers = max(props->cBuffers, 1);
2116 props->cbBuffer = max(props->cbBuffer, buffer_size);
2117 props->cbAlign = max(props->cbAlign, 1);
2118 return IMemAllocator_SetProperties(allocator, props, &ret_props);
2121 static void free_source_pin(struct gstdemux_source *pin)
2123 if (pin->pin.pin.peer)
2125 if (SUCCEEDED(IMemAllocator_Decommit(pin->pin.pAllocator)))
2126 IPin_Disconnect(pin->pin.pin.peer);
2127 IPin_Disconnect(&pin->pin.pin.IPin_iface);
2130 if (pin->their_src)
2132 if (pin->post_sink)
2134 gst_pad_unlink(pin->their_src, pin->post_sink);
2135 gst_pad_unlink(pin->post_src, pin->my_sink);
2136 gst_object_unref(pin->post_src);
2137 gst_object_unref(pin->post_sink);
2138 pin->post_src = pin->post_sink = NULL;
2140 else
2141 gst_pad_unlink(pin->their_src, pin->my_sink);
2142 gst_object_unref(pin->their_src);
2144 gst_object_unref(pin->my_sink);
2145 CloseHandle(pin->caps_event);
2146 CloseHandle(pin->eos_event);
2147 gst_segment_free(pin->segment);
2149 strmbase_seeking_cleanup(&pin->seek);
2150 strmbase_source_cleanup(&pin->pin);
2151 heap_free(pin);
2154 static const struct strmbase_source_ops source_ops =
2156 .base.pin_query_interface = source_query_interface,
2157 .base.pin_query_accept = source_query_accept,
2158 .base.pin_get_media_type = source_get_media_type,
2159 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
2160 .pfnDecideAllocator = BaseOutputPinImpl_DecideAllocator,
2161 .pfnDecideBufferSize = GSTOutPin_DecideBufferSize,
2164 static struct gstdemux_source *create_pin(struct gstdemux *filter, const WCHAR *name)
2166 struct gstdemux_source *pin, **new_array;
2167 char pad_name[19];
2169 if (!(new_array = heap_realloc(filter->sources, (filter->source_count + 1) * sizeof(*new_array))))
2170 return NULL;
2171 filter->sources = new_array;
2173 if (!(pin = heap_alloc_zero(sizeof(*pin))))
2174 return NULL;
2176 strmbase_source_init(&pin->pin, &filter->filter, name, &source_ops);
2177 pin->caps_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2178 pin->eos_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2179 pin->segment = gst_segment_new();
2180 gst_segment_init(pin->segment, GST_FORMAT_TIME);
2181 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
2182 strmbase_seeking_init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop,
2183 GST_ChangeCurrent, GST_ChangeRate);
2184 BaseFilterImpl_IncrementPinVersion(&filter->filter);
2186 sprintf(pad_name, "qz_sink_%u", filter->source_count);
2187 pin->my_sink = gst_pad_new(pad_name, GST_PAD_SINK);
2188 gst_pad_set_element_private(pin->my_sink, pin);
2189 gst_pad_set_chain_function(pin->my_sink, got_data_sink_wrapper);
2190 gst_pad_set_event_function(pin->my_sink, event_sink_wrapper);
2191 gst_pad_set_query_function(pin->my_sink, query_sink_wrapper);
2193 filter->sources[filter->source_count++] = pin;
2194 return pin;
2197 static HRESULT GST_RemoveOutputPins(struct gstdemux *This)
2199 unsigned int i;
2201 TRACE("(%p)\n", This);
2202 mark_wine_thread();
2204 if (!This->container)
2205 return S_OK;
2206 gst_element_set_state(This->container, GST_STATE_NULL);
2207 gst_pad_unlink(This->my_src, This->their_sink);
2208 gst_object_unref(This->my_src);
2209 gst_object_unref(This->their_sink);
2210 This->my_src = This->their_sink = NULL;
2212 for (i = 0; i < This->source_count; ++i)
2213 free_source_pin(This->sources[i]);
2215 This->source_count = 0;
2216 heap_free(This->sources);
2217 This->sources = NULL;
2218 gst_element_set_bus(This->container, NULL);
2219 gst_object_unref(This->container);
2220 This->container = NULL;
2221 BaseFilterImpl_IncrementPinVersion(&This->filter);
2222 return S_OK;
2225 void perform_cb_gstdemux(struct cb_data *cbdata)
2227 switch(cbdata->type)
2229 case WATCH_BUS:
2231 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2232 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2233 break;
2235 case EXISTING_NEW_PAD:
2237 struct pad_added_data *data = &cbdata->u.pad_added_data;
2238 existing_new_pad(data->element, data->pad, data->user);
2239 break;
2241 case QUERY_FUNCTION:
2243 struct query_function_data *data = &cbdata->u.query_function_data;
2244 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2245 break;
2247 case ACTIVATE_MODE:
2249 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2250 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2251 break;
2253 case NO_MORE_PADS:
2255 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2256 no_more_pads(data->element, data->user);
2257 break;
2259 case REQUEST_BUFFER_SRC:
2261 struct getrange_data *data = &cbdata->u.getrange_data;
2262 cbdata->u.getrange_data.ret = request_buffer_src(data->pad, data->parent,
2263 data->ofs, data->len, data->buf);
2264 break;
2266 case EVENT_SRC:
2268 struct event_src_data *data = &cbdata->u.event_src_data;
2269 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2270 break;
2272 case EVENT_SINK:
2274 struct event_sink_data *data = &cbdata->u.event_sink_data;
2275 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2276 break;
2278 case GOT_DATA_SINK:
2280 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2281 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2282 break;
2284 case REMOVED_DECODED_PAD:
2286 struct pad_removed_data *data = &cbdata->u.pad_removed_data;
2287 removed_decoded_pad(data->element, data->pad, data->user);
2288 break;
2290 case AUTOPLUG_BLACKLIST:
2292 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2293 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2294 data->pad, data->caps, data->fact, data->user);
2295 break;
2297 case UNKNOWN_TYPE:
2299 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2300 unknown_type(data->bin, data->pad, data->caps, data->user);
2301 break;
2303 case QUERY_SINK:
2305 struct query_sink_data *data = &cbdata->u.query_sink_data;
2306 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2307 data->query);
2308 break;
2310 default:
2312 assert(0);
2317 static BOOL compare_media_types(const AM_MEDIA_TYPE *a, const AM_MEDIA_TYPE *b)
2319 return IsEqualGUID(&a->majortype, &b->majortype)
2320 && IsEqualGUID(&a->subtype, &b->subtype)
2321 && IsEqualGUID(&a->formattype, &b->formattype)
2322 && a->cbFormat == b->cbFormat
2323 && !memcmp(a->pbFormat, b->pbFormat, a->cbFormat);
2326 static HRESULT wave_parser_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2328 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2329 return S_FALSE;
2330 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WAVE))
2331 return S_OK;
2332 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AU) || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AIFF))
2333 FIXME("AU and AIFF files are not yet supported.\n");
2334 return S_FALSE;
2337 static const struct strmbase_sink_ops wave_parser_sink_ops =
2339 .base.pin_query_accept = wave_parser_sink_query_accept,
2340 .sink_connect = gstdemux_sink_connect,
2341 .sink_disconnect = gstdemux_sink_disconnect,
2344 static BOOL wave_parser_init_gst(struct gstdemux *filter)
2346 static const WCHAR source_name[] = {'o','u','t','p','u','t',0};
2347 struct gstdemux_source *pin;
2348 GstElement *element;
2349 HANDLE events[2];
2350 int ret;
2352 if (!(element = gst_element_factory_make("wavparse", NULL)))
2354 ERR("Failed to create wavparse; are %u-bit GStreamer \"good\" plugins installed?\n",
2355 8 * (int)sizeof(void*));
2356 return FALSE;
2359 gst_bin_add(GST_BIN(filter->container), element);
2361 filter->their_sink = gst_element_get_static_pad(element, "sink");
2362 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2364 ERR("Failed to link sink pads, error %d.\n", ret);
2365 return FALSE;
2368 if (!(pin = create_pin(filter, source_name)))
2369 return FALSE;
2370 pin->their_src = gst_element_get_static_pad(element, "src");
2371 gst_object_ref(pin->their_src);
2372 if ((ret = gst_pad_link(pin->their_src, pin->my_sink)) < 0)
2374 ERR("Failed to link source pads, error %d.\n", ret);
2375 return FALSE;
2378 gst_pad_set_active(pin->my_sink, 1);
2379 gst_element_set_state(filter->container, GST_STATE_PAUSED);
2380 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2381 if (ret == GST_STATE_CHANGE_FAILURE)
2383 ERR("Failed to play stream.\n");
2384 return FALSE;
2387 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2388 pin->seek.llCurrent = 0;
2390 events[0] = pin->caps_event;
2391 events[1] = filter->error_event;
2392 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2393 return FALSE;
2395 filter->ignore_flush = TRUE;
2396 gst_element_set_state(filter->container, GST_STATE_READY);
2397 gst_element_get_state(filter->container, NULL, NULL, -1);
2398 filter->ignore_flush = FALSE;
2400 return TRUE;
2403 static gboolean get_source_amt(const struct gstdemux_source *pin, AM_MEDIA_TYPE *mt)
2405 GstCaps *caps = gst_pad_get_current_caps(pin->my_sink);
2406 gboolean ret = amt_from_gst_caps(caps, mt);
2407 gst_caps_unref(caps);
2408 return ret;
2411 static HRESULT wave_parser_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
2413 AM_MEDIA_TYPE pad_mt;
2414 HRESULT hr;
2416 if (!get_source_amt(pin, &pad_mt))
2417 return E_OUTOFMEMORY;
2418 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2419 FreeMediaType(&pad_mt);
2420 return hr;
2423 static HRESULT wave_parser_source_get_media_type(struct gstdemux_source *pin,
2424 unsigned int index, AM_MEDIA_TYPE *mt)
2426 if (index > 0)
2427 return VFW_S_NO_MORE_ITEMS;
2428 if (!get_source_amt(pin, mt))
2429 return E_OUTOFMEMORY;
2430 return S_OK;
2433 HRESULT wave_parser_create(IUnknown *outer, IUnknown **out)
2435 static const WCHAR sink_name[] = {'i','n','p','u','t',' ','p','i','n',0};
2436 struct gstdemux *object;
2438 if (!init_gstreamer())
2439 return E_FAIL;
2441 mark_wine_thread();
2443 if (!(object = heap_alloc_zero(sizeof(*object))))
2444 return E_OUTOFMEMORY;
2446 strmbase_filter_init(&object->filter, outer, &CLSID_WAVEParser, &filter_ops);
2447 strmbase_sink_init(&object->sink, &object->filter, sink_name, &wave_parser_sink_ops, NULL);
2448 object->init_gst = wave_parser_init_gst;
2449 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2450 object->source_query_accept = wave_parser_source_query_accept;
2451 object->source_get_media_type = wave_parser_source_get_media_type;
2453 TRACE("Created WAVE parser %p.\n", object);
2454 *out = &object->filter.IUnknown_inner;
2455 return S_OK;
2458 static HRESULT avi_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2460 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream)
2461 && IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_Avi))
2462 return S_OK;
2463 return S_FALSE;
2466 static const struct strmbase_sink_ops avi_splitter_sink_ops =
2468 .base.pin_query_accept = avi_splitter_sink_query_accept,
2469 .sink_connect = gstdemux_sink_connect,
2470 .sink_disconnect = gstdemux_sink_disconnect,
2473 static BOOL avi_splitter_init_gst(struct gstdemux *filter)
2475 GstElement *element = gst_element_factory_make("avidemux", NULL);
2476 unsigned int i;
2477 int ret;
2479 if (!element)
2481 ERR("Failed to create avidemux; are %u-bit GStreamer \"good\" plugins installed?\n",
2482 8 * (int)sizeof(void*));
2483 return FALSE;
2486 gst_bin_add(GST_BIN(filter->container), element);
2488 g_signal_connect(element, "pad-added", G_CALLBACK(existing_new_pad_wrapper), filter);
2489 g_signal_connect(element, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), filter);
2490 g_signal_connect(element, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), filter);
2492 filter->their_sink = gst_element_get_static_pad(element, "sink");
2493 ResetEvent(filter->no_more_pads_event);
2495 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2497 ERR("Failed to link pads, error %d.\n", ret);
2498 return FALSE;
2501 gst_element_set_state(filter->container, GST_STATE_PLAYING);
2502 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2503 if (ret == GST_STATE_CHANGE_FAILURE)
2505 ERR("Failed to play stream.\n");
2506 return FALSE;
2509 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
2511 for (i = 0; i < filter->source_count; ++i)
2513 struct gstdemux_source *pin = filter->sources[i];
2514 const HANDLE events[2] = {pin->caps_event, filter->error_event};
2516 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2517 pin->seek.llCurrent = 0;
2518 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2519 return FALSE;
2522 filter->ignore_flush = TRUE;
2523 gst_element_set_state(filter->container, GST_STATE_READY);
2524 gst_element_get_state(filter->container, NULL, NULL, -1);
2525 filter->ignore_flush = FALSE;
2527 return TRUE;
2530 static HRESULT avi_splitter_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
2532 AM_MEDIA_TYPE pad_mt;
2533 HRESULT hr;
2535 if (!get_source_amt(pin, &pad_mt))
2536 return E_OUTOFMEMORY;
2537 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2538 FreeMediaType(&pad_mt);
2539 return hr;
2542 static HRESULT avi_splitter_source_get_media_type(struct gstdemux_source *pin,
2543 unsigned int index, AM_MEDIA_TYPE *mt)
2545 if (index > 0)
2546 return VFW_S_NO_MORE_ITEMS;
2547 if (!get_source_amt(pin, mt))
2548 return E_OUTOFMEMORY;
2549 return S_OK;
2552 HRESULT avi_splitter_create(IUnknown *outer, IUnknown **out)
2554 static const WCHAR sink_name[] = {'i','n','p','u','t',' ','p','i','n',0};
2555 struct gstdemux *object;
2557 if (!init_gstreamer())
2558 return E_FAIL;
2560 mark_wine_thread();
2562 if (!(object = heap_alloc_zero(sizeof(*object))))
2563 return E_OUTOFMEMORY;
2565 strmbase_filter_init(&object->filter, outer, &CLSID_AviSplitter, &filter_ops);
2566 strmbase_sink_init(&object->sink, &object->filter, sink_name, &avi_splitter_sink_ops, NULL);
2567 object->no_more_pads_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2568 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2569 object->init_gst = avi_splitter_init_gst;
2570 object->source_query_accept = avi_splitter_source_query_accept;
2571 object->source_get_media_type = avi_splitter_source_get_media_type;
2573 TRACE("Created AVI splitter %p.\n", object);
2574 *out = &object->filter.IUnknown_inner;
2575 return S_OK;
2578 static HRESULT mpeg_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2580 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2581 return S_FALSE;
2582 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Audio))
2583 return S_OK;
2584 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Video)
2585 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1System)
2586 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
2587 FIXME("Unsupported subtype %s.\n", wine_dbgstr_guid(&mt->subtype));
2588 return S_FALSE;
2591 static const struct strmbase_sink_ops mpeg_splitter_sink_ops =
2593 .base.pin_query_accept = mpeg_splitter_sink_query_accept,
2594 .sink_connect = gstdemux_sink_connect,
2595 .sink_disconnect = gstdemux_sink_disconnect,
2598 static BOOL mpeg_splitter_init_gst(struct gstdemux *filter)
2600 static const WCHAR source_name[] = {'A','u','d','i','o',0};
2601 struct gstdemux_source *pin;
2602 GstElement *element;
2603 HANDLE events[3];
2604 DWORD res;
2605 int ret;
2607 if (!(element = gst_element_factory_make("mpegaudioparse", NULL)))
2609 ERR("Failed to create mpegaudioparse; are %u-bit GStreamer \"good\" plugins installed?\n",
2610 8 * (int)sizeof(void*));
2611 return FALSE;
2614 gst_bin_add(GST_BIN(filter->container), element);
2616 filter->their_sink = gst_element_get_static_pad(element, "sink");
2617 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2619 ERR("Failed to link sink pads, error %d.\n", ret);
2620 return FALSE;
2623 if (!(pin = create_pin(filter, source_name)))
2624 return FALSE;
2625 gst_object_ref(pin->their_src = gst_element_get_static_pad(element, "src"));
2626 if ((ret = gst_pad_link(pin->their_src, pin->my_sink)) < 0)
2628 ERR("Failed to link source pads, error %d.\n", ret);
2629 return FALSE;
2632 gst_pad_set_active(pin->my_sink, 1);
2633 gst_element_set_state(filter->container, GST_STATE_PAUSED);
2634 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2635 if (ret == GST_STATE_CHANGE_FAILURE)
2637 ERR("Failed to play stream.\n");
2638 return FALSE;
2641 events[0] = filter->duration_event;
2642 events[1] = filter->error_event;
2643 events[2] = pin->eos_event;
2644 res = WaitForMultipleObjects(3, events, FALSE, INFINITE);
2645 if (res == 1)
2646 return FALSE;
2648 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2649 pin->seek.llCurrent = 0;
2651 events[0] = pin->caps_event;
2652 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2653 return FALSE;
2655 filter->ignore_flush = TRUE;
2656 gst_element_set_state(filter->container, GST_STATE_READY);
2657 gst_element_get_state(filter->container, NULL, NULL, -1);
2658 filter->ignore_flush = FALSE;
2660 return TRUE;
2663 static HRESULT mpeg_splitter_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
2665 AM_MEDIA_TYPE pad_mt;
2666 HRESULT hr;
2668 if (!get_source_amt(pin, &pad_mt))
2669 return E_OUTOFMEMORY;
2670 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2671 FreeMediaType(&pad_mt);
2672 return hr;
2675 static HRESULT mpeg_splitter_source_get_media_type(struct gstdemux_source *pin,
2676 unsigned int index, AM_MEDIA_TYPE *mt)
2678 if (index > 0)
2679 return VFW_S_NO_MORE_ITEMS;
2680 if (!get_source_amt(pin, mt))
2681 return E_OUTOFMEMORY;
2682 return S_OK;
2685 static HRESULT mpeg_splitter_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
2687 struct gstdemux *filter = impl_from_strmbase_filter(iface);
2689 if (IsEqualGUID(iid, &IID_IAMStreamSelect))
2691 *out = &filter->IAMStreamSelect_iface;
2692 IUnknown_AddRef((IUnknown *)*out);
2693 return S_OK;
2696 return E_NOINTERFACE;
2699 static const struct strmbase_filter_ops mpeg_splitter_ops =
2701 .filter_query_interface = mpeg_splitter_query_interface,
2702 .filter_get_pin = gstdemux_get_pin,
2703 .filter_destroy = gstdemux_destroy,
2704 .filter_init_stream = gstdemux_init_stream,
2705 .filter_start_stream = gstdemux_start_stream,
2706 .filter_stop_stream = gstdemux_stop_stream,
2707 .filter_cleanup_stream = gstdemux_cleanup_stream,
2708 .filter_wait_state = gstdemux_wait_state,
2711 HRESULT mpeg_splitter_create(IUnknown *outer, IUnknown **out)
2713 static const WCHAR sink_name[] = {'I','n','p','u','t',0};
2714 struct gstdemux *object;
2716 if (!init_gstreamer())
2717 return E_FAIL;
2719 mark_wine_thread();
2721 if (!(object = heap_alloc_zero(sizeof(*object))))
2722 return E_OUTOFMEMORY;
2724 strmbase_filter_init(&object->filter, outer, &CLSID_MPEG1Splitter, &mpeg_splitter_ops);
2725 strmbase_sink_init(&object->sink, &object->filter, sink_name, &mpeg_splitter_sink_ops, NULL);
2726 object->IAMStreamSelect_iface.lpVtbl = &stream_select_vtbl;
2728 object->duration_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2729 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2730 object->init_gst = mpeg_splitter_init_gst;
2731 object->source_query_accept = mpeg_splitter_source_query_accept;
2732 object->source_get_media_type = mpeg_splitter_source_get_media_type;
2733 object->enum_sink_first = TRUE;
2735 TRACE("Created MPEG-1 splitter %p.\n", object);
2736 *out = &object->filter.IUnknown_inner;
2737 return S_OK;