gdi32: Move the text metrics cache into the generic font structure.
[wine/zf.git] / dlls / winegstreamer / gstdemux.c
blob9f5e147b7ed622bbe70fcca46dc3a2470c4d309e
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 HRESULT hr = VFW_E_NOT_CONNECTED, pin_hr;
1436 const SourceSeeking *seeking;
1437 GstStateChangeReturn ret;
1438 unsigned int i;
1440 if (!filter->container)
1441 return VFW_E_NOT_CONNECTED;
1443 for (i = 0; i < filter->source_count; ++i)
1445 if (SUCCEEDED(pin_hr = BaseOutputPinImpl_Active(&filter->sources[i]->pin)))
1446 hr = pin_hr;
1449 if (FAILED(hr))
1450 return hr;
1452 if (filter->no_more_pads_event)
1453 ResetEvent(filter->no_more_pads_event);
1455 if ((ret = gst_element_set_state(filter->container, GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE)
1457 ERR("Failed to pause stream.\n");
1458 return E_FAIL;
1461 /* Make sure that all of our pads are connected before returning, lest we
1462 * e.g. try to seek and fail. */
1463 if (filter->no_more_pads_event)
1464 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
1466 seeking = &filter->sources[0]->seek;
1468 /* GStreamer can't seek while stopped, and it resets position to the
1469 * beginning of the stream every time it is stopped. */
1470 if (seeking->llCurrent)
1472 GstSeekType stop_type = GST_SEEK_TYPE_NONE;
1474 if (seeking->llStop && seeking->llStop != seeking->llDuration)
1475 stop_type = GST_SEEK_TYPE_SET;
1477 gst_pad_push_event(filter->sources[0]->my_sink, gst_event_new_seek(
1478 seeking->dRate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH,
1479 GST_SEEK_TYPE_SET, seeking->llCurrent * 100,
1480 stop_type, seeking->llStop * 100));
1483 return hr;
1486 static HRESULT gstdemux_start_stream(struct strmbase_filter *iface, REFERENCE_TIME time)
1488 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1489 GstStateChangeReturn ret;
1491 if (!filter->container)
1492 return VFW_E_NOT_CONNECTED;
1494 if ((ret = gst_element_set_state(filter->container, GST_STATE_PLAYING)) == GST_STATE_CHANGE_FAILURE)
1496 ERR("Failed to play stream.\n");
1497 return E_FAIL;
1499 else if (ret == GST_STATE_CHANGE_ASYNC)
1500 return S_FALSE;
1501 return S_OK;
1504 static HRESULT gstdemux_stop_stream(struct strmbase_filter *iface)
1506 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1507 GstStateChangeReturn ret;
1509 if (!filter->container)
1510 return VFW_E_NOT_CONNECTED;
1512 if ((ret = gst_element_set_state(filter->container, GST_STATE_PAUSED)) == GST_STATE_CHANGE_FAILURE)
1514 ERR("Failed to pause stream.\n");
1515 return E_FAIL;
1517 else if (ret == GST_STATE_CHANGE_ASYNC)
1518 return S_FALSE;
1519 return S_OK;
1522 static HRESULT gstdemux_cleanup_stream(struct strmbase_filter *iface)
1524 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1525 GstStateChangeReturn ret;
1526 unsigned int i;
1528 if (!filter->container)
1529 return S_OK;
1531 filter->ignore_flush = TRUE;
1532 if ((ret = gst_element_set_state(filter->container, GST_STATE_READY)) == GST_STATE_CHANGE_FAILURE)
1534 ERR("Failed to pause stream.\n");
1535 return E_FAIL;
1537 gst_element_get_state(filter->container, NULL, NULL, GST_CLOCK_TIME_NONE);
1538 filter->ignore_flush = FALSE;
1540 for (i = 0; i < filter->source_count; ++i)
1542 if (filter->sources[i]->pin.pin.peer)
1543 IMemAllocator_Decommit(filter->sources[i]->pin.pAllocator);
1546 return S_OK;
1549 static HRESULT gstdemux_wait_state(struct strmbase_filter *iface, DWORD timeout)
1551 struct gstdemux *filter = impl_from_strmbase_filter(iface);
1552 GstStateChangeReturn ret;
1554 if (!filter->container)
1555 return S_OK;
1557 ret = gst_element_get_state(filter->container, NULL, NULL,
1558 timeout == INFINITE ? GST_CLOCK_TIME_NONE : timeout * 1000000);
1559 if (ret == GST_STATE_CHANGE_FAILURE)
1561 ERR("Failed to get state.\n");
1562 return E_FAIL;
1564 else if (ret == GST_STATE_CHANGE_ASYNC)
1565 return VFW_S_STATE_INTERMEDIATE;
1566 return S_OK;
1569 static const struct strmbase_filter_ops filter_ops =
1571 .filter_get_pin = gstdemux_get_pin,
1572 .filter_destroy = gstdemux_destroy,
1573 .filter_init_stream = gstdemux_init_stream,
1574 .filter_start_stream = gstdemux_start_stream,
1575 .filter_stop_stream = gstdemux_stop_stream,
1576 .filter_cleanup_stream = gstdemux_cleanup_stream,
1577 .filter_wait_state = gstdemux_wait_state,
1580 static inline struct gstdemux *impl_from_strmbase_sink(struct strmbase_sink *iface)
1582 return CONTAINING_RECORD(iface, struct gstdemux, sink);
1585 static HRESULT sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
1587 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
1588 return S_OK;
1589 return S_FALSE;
1592 static HRESULT gstdemux_sink_connect(struct strmbase_sink *iface, IPin *peer, const AM_MEDIA_TYPE *pmt)
1594 struct gstdemux *filter = impl_from_strmbase_sink(iface);
1595 HRESULT hr = S_OK;
1597 mark_wine_thread();
1599 filter->reader = NULL;
1600 if (FAILED(hr = IPin_QueryInterface(peer, &IID_IAsyncReader, (void **)&filter->reader)))
1601 return hr;
1603 if (FAILED(hr = GST_Connect(filter, peer)))
1604 goto err;
1606 return S_OK;
1607 err:
1608 GST_RemoveOutputPins(filter);
1609 IAsyncReader_Release(filter->reader);
1610 filter->reader = NULL;
1611 return hr;
1614 static void gstdemux_sink_disconnect(struct strmbase_sink *iface)
1616 struct gstdemux *filter = impl_from_strmbase_sink(iface);
1618 mark_wine_thread();
1620 GST_RemoveOutputPins(filter);
1622 IAsyncReader_Release(filter->reader);
1623 filter->reader = NULL;
1626 static const struct strmbase_sink_ops sink_ops =
1628 .base.pin_query_accept = sink_query_accept,
1629 .sink_connect = gstdemux_sink_connect,
1630 .sink_disconnect = gstdemux_sink_disconnect,
1633 static BOOL gstdecoder_init_gst(struct gstdemux *filter)
1635 GstElement *element = gst_element_factory_make("decodebin", NULL);
1636 unsigned int i;
1637 int ret;
1639 if (!element)
1641 ERR("Failed to create decodebin; are %u-bit GStreamer \"base\" plugins installed?\n",
1642 8 * (int)sizeof(void*));
1643 return FALSE;
1646 gst_bin_add(GST_BIN(filter->container), element);
1648 g_signal_connect(element, "pad-added", G_CALLBACK(existing_new_pad_wrapper), filter);
1649 g_signal_connect(element, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), filter);
1650 g_signal_connect(element, "autoplug-select", G_CALLBACK(autoplug_blacklist_wrapper), filter);
1651 g_signal_connect(element, "unknown-type", G_CALLBACK(unknown_type_wrapper), filter);
1652 g_signal_connect(element, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), filter);
1654 filter->their_sink = gst_element_get_static_pad(element, "sink");
1655 ResetEvent(filter->no_more_pads_event);
1657 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
1659 ERR("Failed to link pads, error %d.\n", ret);
1660 return FALSE;
1663 gst_element_set_state(filter->container, GST_STATE_PLAYING);
1664 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
1665 if (ret == GST_STATE_CHANGE_FAILURE)
1667 ERR("Failed to play stream.\n");
1668 return FALSE;
1671 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
1673 for (i = 0; i < filter->source_count; ++i)
1675 struct gstdemux_source *pin = filter->sources[i];
1676 const HANDLE events[2] = {pin->caps_event, filter->error_event};
1678 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
1679 pin->seek.llCurrent = 0;
1680 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
1681 return FALSE;
1684 filter->ignore_flush = TRUE;
1685 gst_element_set_state(filter->container, GST_STATE_READY);
1686 gst_element_get_state(filter->container, NULL, NULL, -1);
1687 filter->ignore_flush = FALSE;
1689 return TRUE;
1692 static HRESULT gstdecoder_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
1694 /* At least make sure we can convert it to GstCaps. */
1695 GstCaps *caps = amt_to_gst_caps(mt);
1697 if (!caps)
1698 return S_FALSE;
1699 gst_caps_unref(caps);
1700 return S_OK;
1703 static HRESULT gstdecoder_source_get_media_type(struct gstdemux_source *pin,
1704 unsigned int index, AM_MEDIA_TYPE *mt)
1706 GstCaps *caps = gst_pad_get_current_caps(pin->my_sink);
1707 const GstStructure *structure;
1708 const char *type;
1710 static const GstVideoFormat video_formats[] =
1712 /* Try to prefer YUV formats over RGB ones. Most decoders output in the
1713 * YUV color space, and it's generally much less expensive for
1714 * videoconvert to do YUV -> YUV transformations. */
1715 GST_VIDEO_FORMAT_AYUV,
1716 GST_VIDEO_FORMAT_I420,
1717 GST_VIDEO_FORMAT_YV12,
1718 GST_VIDEO_FORMAT_YUY2,
1719 GST_VIDEO_FORMAT_UYVY,
1720 GST_VIDEO_FORMAT_YVYU,
1721 GST_VIDEO_FORMAT_NV12,
1722 GST_VIDEO_FORMAT_BGRA,
1723 GST_VIDEO_FORMAT_BGRx,
1724 GST_VIDEO_FORMAT_BGR,
1725 GST_VIDEO_FORMAT_RGB16,
1726 GST_VIDEO_FORMAT_RGB15,
1729 assert(caps); /* We shouldn't be able to get here if caps haven't been set. */
1730 structure = gst_caps_get_structure(caps, 0);
1731 type = gst_structure_get_name(structure);
1733 memset(mt, 0, sizeof(AM_MEDIA_TYPE));
1735 if (amt_from_gst_caps(caps, mt))
1737 if (!index--)
1739 gst_caps_unref(caps);
1740 return S_OK;
1742 FreeMediaType(mt);
1745 if (!strcmp(type, "video/x-raw") && index < ARRAY_SIZE(video_formats))
1747 gint width, height, fps_n, fps_d;
1748 GstVideoInfo info;
1750 gst_caps_unref(caps);
1751 gst_structure_get_int(structure, "width", &width);
1752 gst_structure_get_int(structure, "height", &height);
1753 gst_video_info_set_format(&info, video_formats[index], width, height);
1754 if (gst_structure_get_fraction(structure, "framerate", &fps_n, &fps_d) && fps_n)
1756 info.fps_n = fps_n;
1757 info.fps_d = fps_d;
1759 if (!amt_from_gst_video_info(&info, mt))
1760 return E_OUTOFMEMORY;
1761 return S_OK;
1763 else if (!strcmp(type, "audio/x-raw") && !index)
1765 GstAudioInfo info;
1766 gint rate;
1768 gst_caps_unref(caps);
1769 gst_structure_get_int(structure, "rate", &rate);
1770 gst_audio_info_set_format(&info, GST_AUDIO_FORMAT_S16LE, rate, 2, NULL);
1771 if (!amt_from_gst_audio_info(&info, mt))
1772 return E_OUTOFMEMORY;
1773 return S_OK;
1776 gst_caps_unref(caps);
1777 return VFW_S_NO_MORE_ITEMS;
1780 HRESULT gstdemux_create(IUnknown *outer, IUnknown **out)
1782 struct gstdemux *object;
1784 if (!init_gstreamer())
1785 return E_FAIL;
1787 mark_wine_thread();
1789 if (!(object = heap_alloc_zero(sizeof(*object))))
1790 return E_OUTOFMEMORY;
1792 strmbase_filter_init(&object->filter, outer, &CLSID_Gstreamer_Splitter, &filter_ops);
1793 strmbase_sink_init(&object->sink, &object->filter, wcsInputPinName, &sink_ops, NULL);
1795 object->no_more_pads_event = CreateEventW(NULL, FALSE, FALSE, NULL);
1796 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
1797 object->init_gst = gstdecoder_init_gst;
1798 object->source_query_accept = gstdecoder_source_query_accept;
1799 object->source_get_media_type = gstdecoder_source_get_media_type;
1801 TRACE("Created GStreamer demuxer %p.\n", object);
1802 *out = &object->filter.IUnknown_inner;
1803 return S_OK;
1806 static struct gstdemux *impl_from_IAMStreamSelect(IAMStreamSelect *iface)
1808 return CONTAINING_RECORD(iface, struct gstdemux, IAMStreamSelect_iface);
1811 static HRESULT WINAPI stream_select_QueryInterface(IAMStreamSelect *iface, REFIID iid, void **out)
1813 struct gstdemux *filter = impl_from_IAMStreamSelect(iface);
1814 return IUnknown_QueryInterface(filter->filter.outer_unk, iid, out);
1817 static ULONG WINAPI stream_select_AddRef(IAMStreamSelect *iface)
1819 struct gstdemux *filter = impl_from_IAMStreamSelect(iface);
1820 return IUnknown_AddRef(filter->filter.outer_unk);
1823 static ULONG WINAPI stream_select_Release(IAMStreamSelect *iface)
1825 struct gstdemux *filter = impl_from_IAMStreamSelect(iface);
1826 return IUnknown_Release(filter->filter.outer_unk);
1829 static HRESULT WINAPI stream_select_Count(IAMStreamSelect *iface, DWORD *count)
1831 FIXME("iface %p, count %p, stub!\n", iface, count);
1832 return E_NOTIMPL;
1835 static HRESULT WINAPI stream_select_Info(IAMStreamSelect *iface, LONG index,
1836 AM_MEDIA_TYPE **mt, DWORD *flags, LCID *lcid, DWORD *group, WCHAR **name,
1837 IUnknown **object, IUnknown **unknown)
1839 FIXME("iface %p, index %d, mt %p, flags %p, lcid %p, group %p, name %p, object %p, unknown %p, stub!\n",
1840 iface, index, mt, flags, lcid, group, name, object, unknown);
1841 return E_NOTIMPL;
1844 static HRESULT WINAPI stream_select_Enable(IAMStreamSelect *iface, LONG index, DWORD flags)
1846 FIXME("iface %p, index %d, flags %#x, stub!\n", iface, index, flags);
1847 return E_NOTIMPL;
1850 static const IAMStreamSelectVtbl stream_select_vtbl =
1852 stream_select_QueryInterface,
1853 stream_select_AddRef,
1854 stream_select_Release,
1855 stream_select_Count,
1856 stream_select_Info,
1857 stream_select_Enable,
1860 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1862 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1863 TRACE("(%p)\n", This);
1864 return S_OK;
1867 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1869 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1870 TRACE("(%p)\n", This);
1871 return S_OK;
1874 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1876 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1877 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1878 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1879 mark_wine_thread();
1880 gst_pad_push_event(This->my_sink, ev);
1881 return S_OK;
1884 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1886 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1887 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1890 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1892 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1893 return IPin_AddRef(&This->pin.pin.IPin_iface);
1896 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1898 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1899 return IPin_Release(&This->pin.pin.IPin_iface);
1902 static HRESULT WINAPI GST_Seeking_GetCurrentPosition(IMediaSeeking *iface, REFERENCE_TIME *pos)
1904 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1906 TRACE("(%p)->(%p)\n", This, pos);
1908 if (!pos)
1909 return E_POINTER;
1911 mark_wine_thread();
1913 if (This->pin.pin.filter->state == State_Stopped)
1915 *pos = This->seek.llCurrent;
1916 TRACE("Cached value\n");
1917 return S_OK;
1920 if (!gst_pad_query_position(This->their_src, GST_FORMAT_TIME, pos)) {
1921 WARN("Could not query position\n");
1922 return E_NOTIMPL;
1924 *pos /= 100;
1925 This->seek.llCurrent = *pos;
1926 return S_OK;
1929 static GstSeekType type_from_flags(DWORD flags)
1931 switch (flags & AM_SEEKING_PositioningBitsMask) {
1932 case AM_SEEKING_NoPositioning:
1933 return GST_SEEK_TYPE_NONE;
1934 case AM_SEEKING_AbsolutePositioning:
1935 case AM_SEEKING_RelativePositioning:
1936 return GST_SEEK_TYPE_SET;
1937 case AM_SEEKING_IncrementalPositioning:
1938 return GST_SEEK_TYPE_END;
1940 return GST_SEEK_TYPE_NONE;
1943 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1944 REFERENCE_TIME *pCur, DWORD curflags, REFERENCE_TIME *pStop,
1945 DWORD stopflags)
1947 HRESULT hr;
1948 struct gstdemux_source *This = impl_from_IMediaSeeking(iface);
1949 GstSeekFlags f = 0;
1950 GstSeekType curtype, stoptype;
1951 GstEvent *e;
1952 gint64 stop_pos = 0, curr_pos = 0;
1954 TRACE("(%p)->(%p, 0x%x, %p, 0x%x)\n", This, pCur, curflags, pStop, stopflags);
1956 mark_wine_thread();
1958 hr = SourceSeekingImpl_SetPositions(iface, pCur, curflags, pStop, stopflags);
1959 if (This->pin.pin.filter->state == State_Stopped)
1960 return hr;
1962 curtype = type_from_flags(curflags);
1963 stoptype = type_from_flags(stopflags);
1964 if (curflags & AM_SEEKING_SeekToKeyFrame)
1965 f |= GST_SEEK_FLAG_KEY_UNIT;
1966 if (curflags & AM_SEEKING_Segment)
1967 f |= GST_SEEK_FLAG_SEGMENT;
1968 if (!(curflags & AM_SEEKING_NoFlush))
1969 f |= GST_SEEK_FLAG_FLUSH;
1971 if (((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning) ||
1972 ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)) {
1973 gint64 tmp_pos;
1974 gst_pad_query_position (This->my_sink, GST_FORMAT_TIME, &tmp_pos);
1975 if ((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1976 curr_pos = tmp_pos;
1977 if ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1978 stop_pos = tmp_pos;
1981 e = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, f, curtype, pCur ? curr_pos + *pCur * 100 : -1, stoptype, pStop ? stop_pos + *pStop * 100 : -1);
1982 if (gst_pad_push_event(This->my_sink, e))
1983 return S_OK;
1984 else
1985 return E_NOTIMPL;
1988 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1990 GST_Seeking_QueryInterface,
1991 GST_Seeking_AddRef,
1992 GST_Seeking_Release,
1993 SourceSeekingImpl_GetCapabilities,
1994 SourceSeekingImpl_CheckCapabilities,
1995 SourceSeekingImpl_IsFormatSupported,
1996 SourceSeekingImpl_QueryPreferredFormat,
1997 SourceSeekingImpl_GetTimeFormat,
1998 SourceSeekingImpl_IsUsingTimeFormat,
1999 SourceSeekingImpl_SetTimeFormat,
2000 SourceSeekingImpl_GetDuration,
2001 SourceSeekingImpl_GetStopPosition,
2002 GST_Seeking_GetCurrentPosition,
2003 SourceSeekingImpl_ConvertTimeFormat,
2004 GST_Seeking_SetPositions,
2005 SourceSeekingImpl_GetPositions,
2006 SourceSeekingImpl_GetAvailable,
2007 SourceSeekingImpl_SetRate,
2008 SourceSeekingImpl_GetRate,
2009 SourceSeekingImpl_GetPreroll
2012 static inline struct gstdemux_source *impl_from_IQualityControl( IQualityControl *iface )
2014 return CONTAINING_RECORD(iface, struct gstdemux_source, IQualityControl_iface);
2017 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
2019 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
2020 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
2023 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
2025 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
2026 return IPin_AddRef(&pin->pin.pin.IPin_iface);
2029 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
2031 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
2032 return IPin_Release(&pin->pin.pin.IPin_iface);
2035 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality q)
2037 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
2038 GstQOSType type = GST_QOS_TYPE_OVERFLOW;
2039 GstClockTime timestamp;
2040 GstClockTimeDiff diff;
2041 GstEvent *event;
2043 TRACE("pin %p, sender %p, type %s, proportion %u, late %s, timestamp %s.\n",
2044 pin, sender, q.Type == Famine ? "Famine" : "Flood", q.Proportion,
2045 debugstr_time(q.Late), debugstr_time(q.TimeStamp));
2047 mark_wine_thread();
2049 /* GST_QOS_TYPE_OVERFLOW is also used for buffers that arrive on time, but
2050 * DirectShow filters might use Famine, so check that there actually is an
2051 * underrun. */
2052 if (q.Type == Famine && q.Proportion < 1000)
2053 type = GST_QOS_TYPE_UNDERFLOW;
2055 /* DirectShow filters sometimes pass negative timestamps (Audiosurf uses the
2056 * current time instead of the time of the last buffer). GstClockTime is
2057 * unsigned, so clamp it to 0. */
2058 timestamp = max(q.TimeStamp * 100, 0);
2060 /* The documentation specifies that timestamp + diff must be nonnegative. */
2061 diff = q.Late * 100;
2062 if (diff < 0 && timestamp < (GstClockTime)-diff)
2063 diff = -timestamp;
2065 /* DirectShow "Proportion" describes what percentage of buffers the upstream
2066 * filter should keep (i.e. dropping the rest). If frames are late, the
2067 * proportion will be less than 1. For example, a proportion of 500 means
2068 * that the element should drop half of its frames, essentially because
2069 * frames are taking twice as long as they should to arrive.
2071 * GStreamer "proportion" is the inverse of this; it describes how much
2072 * faster the upstream element should produce frames. I.e. if frames are
2073 * taking twice as long as they should to arrive, we want the frames to be
2074 * decoded twice as fast, and so we pass 2.0 to GStreamer. */
2076 if (!q.Proportion)
2078 WARN("Ignoring quality message with zero proportion.\n");
2079 return S_OK;
2082 if (!(event = gst_event_new_qos(type, 1000.0 / q.Proportion, diff, timestamp)))
2083 ERR("Failed to create QOS event.\n");
2085 gst_pad_push_event(pin->my_sink, event);
2087 return S_OK;
2090 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
2092 struct gstdemux_source *pin = impl_from_IQualityControl(iface);
2093 TRACE("(%p)->(%p)\n", pin, pin);
2094 /* Do nothing */
2095 return S_OK;
2098 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
2099 GST_QualityControl_QueryInterface,
2100 GST_QualityControl_AddRef,
2101 GST_QualityControl_Release,
2102 GST_QualityControl_Notify,
2103 GST_QualityControl_SetSink
2106 static inline struct gstdemux_source *impl_source_from_IPin(IPin *iface)
2108 return CONTAINING_RECORD(iface, struct gstdemux_source, pin.pin.IPin_iface);
2111 static HRESULT source_query_interface(struct strmbase_pin *iface, REFIID iid, void **out)
2113 struct gstdemux_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2115 if (IsEqualGUID(iid, &IID_IMediaSeeking))
2116 *out = &pin->seek.IMediaSeeking_iface;
2117 else if (IsEqualGUID(iid, &IID_IQualityControl))
2118 *out = &pin->IQualityControl_iface;
2119 else
2120 return E_NOINTERFACE;
2122 IUnknown_AddRef((IUnknown *)*out);
2123 return S_OK;
2126 static HRESULT source_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2128 struct gstdemux_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2129 struct gstdemux *filter = impl_from_strmbase_filter(iface->filter);
2130 return filter->source_query_accept(pin, mt);
2133 static HRESULT source_get_media_type(struct strmbase_pin *iface, unsigned int index, AM_MEDIA_TYPE *mt)
2135 struct gstdemux_source *pin = impl_source_from_IPin(&iface->IPin_iface);
2136 struct gstdemux *filter = impl_from_strmbase_filter(iface->filter);
2137 return filter->source_get_media_type(pin, index, mt);
2140 static HRESULT WINAPI GSTOutPin_DecideBufferSize(struct strmbase_source *iface,
2141 IMemAllocator *allocator, ALLOCATOR_PROPERTIES *props)
2143 struct gstdemux_source *pin = impl_source_from_IPin(&iface->pin.IPin_iface);
2144 unsigned int buffer_size = 16384;
2145 ALLOCATOR_PROPERTIES ret_props;
2147 if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_VideoInfo))
2149 VIDEOINFOHEADER *format = (VIDEOINFOHEADER *)pin->pin.pin.mt.pbFormat;
2150 buffer_size = format->bmiHeader.biSizeImage;
2152 gst_util_set_object_arg(G_OBJECT(pin->flip), "method",
2153 (format->bmiHeader.biCompression == BI_RGB
2154 || format->bmiHeader.biCompression == BI_BITFIELDS) ? "vertical-flip" : "none");
2156 else if (IsEqualGUID(&pin->pin.pin.mt.formattype, &FORMAT_WaveFormatEx)
2157 && (IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_PCM)
2158 || IsEqualGUID(&pin->pin.pin.mt.subtype, &MEDIASUBTYPE_IEEE_FLOAT)))
2160 WAVEFORMATEX *format = (WAVEFORMATEX *)pin->pin.pin.mt.pbFormat;
2161 buffer_size = format->nAvgBytesPerSec;
2164 props->cBuffers = max(props->cBuffers, 1);
2165 props->cbBuffer = max(props->cbBuffer, buffer_size);
2166 props->cbAlign = max(props->cbAlign, 1);
2167 return IMemAllocator_SetProperties(allocator, props, &ret_props);
2170 static void free_source_pin(struct gstdemux_source *pin)
2172 if (pin->pin.pin.peer)
2174 if (SUCCEEDED(IMemAllocator_Decommit(pin->pin.pAllocator)))
2175 IPin_Disconnect(pin->pin.pin.peer);
2176 IPin_Disconnect(&pin->pin.pin.IPin_iface);
2179 if (pin->their_src)
2181 if (pin->post_sink)
2183 gst_pad_unlink(pin->their_src, pin->post_sink);
2184 gst_pad_unlink(pin->post_src, pin->my_sink);
2185 gst_object_unref(pin->post_src);
2186 gst_object_unref(pin->post_sink);
2187 pin->post_src = pin->post_sink = NULL;
2189 else
2190 gst_pad_unlink(pin->their_src, pin->my_sink);
2191 gst_object_unref(pin->their_src);
2193 gst_object_unref(pin->my_sink);
2194 CloseHandle(pin->caps_event);
2195 CloseHandle(pin->eos_event);
2196 gst_segment_free(pin->segment);
2198 strmbase_seeking_cleanup(&pin->seek);
2199 strmbase_source_cleanup(&pin->pin);
2200 heap_free(pin);
2203 static const struct strmbase_source_ops source_ops =
2205 .base.pin_query_interface = source_query_interface,
2206 .base.pin_query_accept = source_query_accept,
2207 .base.pin_get_media_type = source_get_media_type,
2208 .pfnAttemptConnection = BaseOutputPinImpl_AttemptConnection,
2209 .pfnDecideAllocator = BaseOutputPinImpl_DecideAllocator,
2210 .pfnDecideBufferSize = GSTOutPin_DecideBufferSize,
2213 static struct gstdemux_source *create_pin(struct gstdemux *filter, const WCHAR *name)
2215 struct gstdemux_source *pin, **new_array;
2216 char pad_name[19];
2218 if (!(new_array = heap_realloc(filter->sources, (filter->source_count + 1) * sizeof(*new_array))))
2219 return NULL;
2220 filter->sources = new_array;
2222 if (!(pin = heap_alloc_zero(sizeof(*pin))))
2223 return NULL;
2225 strmbase_source_init(&pin->pin, &filter->filter, name, &source_ops);
2226 pin->caps_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2227 pin->eos_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2228 pin->segment = gst_segment_new();
2229 gst_segment_init(pin->segment, GST_FORMAT_TIME);
2230 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
2231 strmbase_seeking_init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop,
2232 GST_ChangeCurrent, GST_ChangeRate);
2233 BaseFilterImpl_IncrementPinVersion(&filter->filter);
2235 sprintf(pad_name, "qz_sink_%u", filter->source_count);
2236 pin->my_sink = gst_pad_new(pad_name, GST_PAD_SINK);
2237 gst_pad_set_element_private(pin->my_sink, pin);
2238 gst_pad_set_chain_function(pin->my_sink, got_data_sink_wrapper);
2239 gst_pad_set_event_function(pin->my_sink, event_sink_wrapper);
2240 gst_pad_set_query_function(pin->my_sink, query_sink_wrapper);
2242 filter->sources[filter->source_count++] = pin;
2243 return pin;
2246 static HRESULT GST_RemoveOutputPins(struct gstdemux *This)
2248 unsigned int i;
2250 TRACE("(%p)\n", This);
2251 mark_wine_thread();
2253 if (!This->container)
2254 return S_OK;
2255 gst_element_set_state(This->container, GST_STATE_NULL);
2256 gst_pad_unlink(This->my_src, This->their_sink);
2257 gst_object_unref(This->my_src);
2258 gst_object_unref(This->their_sink);
2259 This->my_src = This->their_sink = NULL;
2261 for (i = 0; i < This->source_count; ++i)
2262 free_source_pin(This->sources[i]);
2264 This->source_count = 0;
2265 heap_free(This->sources);
2266 This->sources = NULL;
2267 gst_element_set_bus(This->container, NULL);
2268 gst_object_unref(This->container);
2269 This->container = NULL;
2270 BaseFilterImpl_IncrementPinVersion(&This->filter);
2271 return S_OK;
2274 void perform_cb_gstdemux(struct cb_data *cbdata)
2276 switch(cbdata->type)
2278 case WATCH_BUS:
2280 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2281 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2282 break;
2284 case EXISTING_NEW_PAD:
2286 struct pad_added_data *data = &cbdata->u.pad_added_data;
2287 existing_new_pad(data->element, data->pad, data->user);
2288 break;
2290 case QUERY_FUNCTION:
2292 struct query_function_data *data = &cbdata->u.query_function_data;
2293 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2294 break;
2296 case ACTIVATE_MODE:
2298 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2299 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2300 break;
2302 case NO_MORE_PADS:
2304 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2305 no_more_pads(data->element, data->user);
2306 break;
2308 case REQUEST_BUFFER_SRC:
2310 struct getrange_data *data = &cbdata->u.getrange_data;
2311 cbdata->u.getrange_data.ret = request_buffer_src(data->pad, data->parent,
2312 data->ofs, data->len, data->buf);
2313 break;
2315 case EVENT_SRC:
2317 struct event_src_data *data = &cbdata->u.event_src_data;
2318 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2319 break;
2321 case EVENT_SINK:
2323 struct event_sink_data *data = &cbdata->u.event_sink_data;
2324 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2325 break;
2327 case GOT_DATA_SINK:
2329 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2330 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2331 break;
2333 case REMOVED_DECODED_PAD:
2335 struct pad_removed_data *data = &cbdata->u.pad_removed_data;
2336 removed_decoded_pad(data->element, data->pad, data->user);
2337 break;
2339 case AUTOPLUG_BLACKLIST:
2341 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2342 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2343 data->pad, data->caps, data->fact, data->user);
2344 break;
2346 case UNKNOWN_TYPE:
2348 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2349 unknown_type(data->bin, data->pad, data->caps, data->user);
2350 break;
2352 case QUERY_SINK:
2354 struct query_sink_data *data = &cbdata->u.query_sink_data;
2355 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2356 data->query);
2357 break;
2359 default:
2361 assert(0);
2366 static BOOL compare_media_types(const AM_MEDIA_TYPE *a, const AM_MEDIA_TYPE *b)
2368 return IsEqualGUID(&a->majortype, &b->majortype)
2369 && IsEqualGUID(&a->subtype, &b->subtype)
2370 && IsEqualGUID(&a->formattype, &b->formattype)
2371 && a->cbFormat == b->cbFormat
2372 && !memcmp(a->pbFormat, b->pbFormat, a->cbFormat);
2375 static HRESULT wave_parser_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2377 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2378 return S_FALSE;
2379 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_WAVE))
2380 return S_OK;
2381 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AU) || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_AIFF))
2382 FIXME("AU and AIFF files are not yet supported.\n");
2383 return S_FALSE;
2386 static const struct strmbase_sink_ops wave_parser_sink_ops =
2388 .base.pin_query_accept = wave_parser_sink_query_accept,
2389 .sink_connect = gstdemux_sink_connect,
2390 .sink_disconnect = gstdemux_sink_disconnect,
2393 static BOOL wave_parser_init_gst(struct gstdemux *filter)
2395 static const WCHAR source_name[] = {'o','u','t','p','u','t',0};
2396 struct gstdemux_source *pin;
2397 GstElement *element;
2398 HANDLE events[2];
2399 int ret;
2401 if (!(element = gst_element_factory_make("wavparse", NULL)))
2403 ERR("Failed to create wavparse; are %u-bit GStreamer \"good\" plugins installed?\n",
2404 8 * (int)sizeof(void*));
2405 return FALSE;
2408 gst_bin_add(GST_BIN(filter->container), element);
2410 filter->their_sink = gst_element_get_static_pad(element, "sink");
2411 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2413 ERR("Failed to link sink pads, error %d.\n", ret);
2414 return FALSE;
2417 if (!(pin = create_pin(filter, source_name)))
2418 return FALSE;
2419 pin->their_src = gst_element_get_static_pad(element, "src");
2420 gst_object_ref(pin->their_src);
2421 if ((ret = gst_pad_link(pin->their_src, pin->my_sink)) < 0)
2423 ERR("Failed to link source pads, error %d.\n", ret);
2424 return FALSE;
2427 gst_pad_set_active(pin->my_sink, 1);
2428 gst_element_set_state(filter->container, GST_STATE_PAUSED);
2429 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2430 if (ret == GST_STATE_CHANGE_FAILURE)
2432 ERR("Failed to play stream.\n");
2433 return FALSE;
2436 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2437 pin->seek.llCurrent = 0;
2439 events[0] = pin->caps_event;
2440 events[1] = filter->error_event;
2441 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2442 return FALSE;
2444 filter->ignore_flush = TRUE;
2445 gst_element_set_state(filter->container, GST_STATE_READY);
2446 gst_element_get_state(filter->container, NULL, NULL, -1);
2447 filter->ignore_flush = FALSE;
2449 return TRUE;
2452 static gboolean get_source_amt(const struct gstdemux_source *pin, AM_MEDIA_TYPE *mt)
2454 GstCaps *caps = gst_pad_get_current_caps(pin->my_sink);
2455 gboolean ret = amt_from_gst_caps(caps, mt);
2456 gst_caps_unref(caps);
2457 return ret;
2460 static HRESULT wave_parser_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
2462 AM_MEDIA_TYPE pad_mt;
2463 HRESULT hr;
2465 if (!get_source_amt(pin, &pad_mt))
2466 return E_OUTOFMEMORY;
2467 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2468 FreeMediaType(&pad_mt);
2469 return hr;
2472 static HRESULT wave_parser_source_get_media_type(struct gstdemux_source *pin,
2473 unsigned int index, AM_MEDIA_TYPE *mt)
2475 if (index > 0)
2476 return VFW_S_NO_MORE_ITEMS;
2477 if (!get_source_amt(pin, mt))
2478 return E_OUTOFMEMORY;
2479 return S_OK;
2482 HRESULT wave_parser_create(IUnknown *outer, IUnknown **out)
2484 static const WCHAR sink_name[] = {'i','n','p','u','t',' ','p','i','n',0};
2485 struct gstdemux *object;
2487 if (!init_gstreamer())
2488 return E_FAIL;
2490 mark_wine_thread();
2492 if (!(object = heap_alloc_zero(sizeof(*object))))
2493 return E_OUTOFMEMORY;
2495 strmbase_filter_init(&object->filter, outer, &CLSID_WAVEParser, &filter_ops);
2496 strmbase_sink_init(&object->sink, &object->filter, sink_name, &wave_parser_sink_ops, NULL);
2497 object->init_gst = wave_parser_init_gst;
2498 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2499 object->source_query_accept = wave_parser_source_query_accept;
2500 object->source_get_media_type = wave_parser_source_get_media_type;
2502 TRACE("Created WAVE parser %p.\n", object);
2503 *out = &object->filter.IUnknown_inner;
2504 return S_OK;
2507 static HRESULT avi_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2509 if (IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream)
2510 && IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_Avi))
2511 return S_OK;
2512 return S_FALSE;
2515 static const struct strmbase_sink_ops avi_splitter_sink_ops =
2517 .base.pin_query_accept = avi_splitter_sink_query_accept,
2518 .sink_connect = gstdemux_sink_connect,
2519 .sink_disconnect = gstdemux_sink_disconnect,
2522 static BOOL avi_splitter_init_gst(struct gstdemux *filter)
2524 GstElement *element = gst_element_factory_make("avidemux", NULL);
2525 unsigned int i;
2526 int ret;
2528 if (!element)
2530 ERR("Failed to create avidemux; are %u-bit GStreamer \"good\" plugins installed?\n",
2531 8 * (int)sizeof(void*));
2532 return FALSE;
2535 gst_bin_add(GST_BIN(filter->container), element);
2537 g_signal_connect(element, "pad-added", G_CALLBACK(existing_new_pad_wrapper), filter);
2538 g_signal_connect(element, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), filter);
2539 g_signal_connect(element, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), filter);
2541 filter->their_sink = gst_element_get_static_pad(element, "sink");
2542 ResetEvent(filter->no_more_pads_event);
2544 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2546 ERR("Failed to link pads, error %d.\n", ret);
2547 return FALSE;
2550 gst_element_set_state(filter->container, GST_STATE_PLAYING);
2551 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2552 if (ret == GST_STATE_CHANGE_FAILURE)
2554 ERR("Failed to play stream.\n");
2555 return FALSE;
2558 WaitForSingleObject(filter->no_more_pads_event, INFINITE);
2560 for (i = 0; i < filter->source_count; ++i)
2562 struct gstdemux_source *pin = filter->sources[i];
2563 const HANDLE events[2] = {pin->caps_event, filter->error_event};
2565 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2566 pin->seek.llCurrent = 0;
2567 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2568 return FALSE;
2571 filter->ignore_flush = TRUE;
2572 gst_element_set_state(filter->container, GST_STATE_READY);
2573 gst_element_get_state(filter->container, NULL, NULL, -1);
2574 filter->ignore_flush = FALSE;
2576 return TRUE;
2579 static HRESULT avi_splitter_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
2581 AM_MEDIA_TYPE pad_mt;
2582 HRESULT hr;
2584 if (!get_source_amt(pin, &pad_mt))
2585 return E_OUTOFMEMORY;
2586 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2587 FreeMediaType(&pad_mt);
2588 return hr;
2591 static HRESULT avi_splitter_source_get_media_type(struct gstdemux_source *pin,
2592 unsigned int index, AM_MEDIA_TYPE *mt)
2594 if (index > 0)
2595 return VFW_S_NO_MORE_ITEMS;
2596 if (!get_source_amt(pin, mt))
2597 return E_OUTOFMEMORY;
2598 return S_OK;
2601 HRESULT avi_splitter_create(IUnknown *outer, IUnknown **out)
2603 static const WCHAR sink_name[] = {'i','n','p','u','t',' ','p','i','n',0};
2604 struct gstdemux *object;
2606 if (!init_gstreamer())
2607 return E_FAIL;
2609 mark_wine_thread();
2611 if (!(object = heap_alloc_zero(sizeof(*object))))
2612 return E_OUTOFMEMORY;
2614 strmbase_filter_init(&object->filter, outer, &CLSID_AviSplitter, &filter_ops);
2615 strmbase_sink_init(&object->sink, &object->filter, sink_name, &avi_splitter_sink_ops, NULL);
2616 object->no_more_pads_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2617 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2618 object->init_gst = avi_splitter_init_gst;
2619 object->source_query_accept = avi_splitter_source_query_accept;
2620 object->source_get_media_type = avi_splitter_source_get_media_type;
2622 TRACE("Created AVI splitter %p.\n", object);
2623 *out = &object->filter.IUnknown_inner;
2624 return S_OK;
2627 static HRESULT mpeg_splitter_sink_query_accept(struct strmbase_pin *iface, const AM_MEDIA_TYPE *mt)
2629 if (!IsEqualGUID(&mt->majortype, &MEDIATYPE_Stream))
2630 return S_FALSE;
2631 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Audio))
2632 return S_OK;
2633 if (IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1Video)
2634 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1System)
2635 || IsEqualGUID(&mt->subtype, &MEDIASUBTYPE_MPEG1VideoCD))
2636 FIXME("Unsupported subtype %s.\n", wine_dbgstr_guid(&mt->subtype));
2637 return S_FALSE;
2640 static const struct strmbase_sink_ops mpeg_splitter_sink_ops =
2642 .base.pin_query_accept = mpeg_splitter_sink_query_accept,
2643 .sink_connect = gstdemux_sink_connect,
2644 .sink_disconnect = gstdemux_sink_disconnect,
2647 static BOOL mpeg_splitter_init_gst(struct gstdemux *filter)
2649 static const WCHAR source_name[] = {'A','u','d','i','o',0};
2650 struct gstdemux_source *pin;
2651 GstElement *element;
2652 HANDLE events[3];
2653 DWORD res;
2654 int ret;
2656 if (!(element = gst_element_factory_make("mpegaudioparse", NULL)))
2658 ERR("Failed to create mpegaudioparse; are %u-bit GStreamer \"good\" plugins installed?\n",
2659 8 * (int)sizeof(void*));
2660 return FALSE;
2663 gst_bin_add(GST_BIN(filter->container), element);
2665 filter->their_sink = gst_element_get_static_pad(element, "sink");
2666 if ((ret = gst_pad_link(filter->my_src, filter->their_sink)) < 0)
2668 ERR("Failed to link sink pads, error %d.\n", ret);
2669 return FALSE;
2672 if (!(pin = create_pin(filter, source_name)))
2673 return FALSE;
2674 gst_object_ref(pin->their_src = gst_element_get_static_pad(element, "src"));
2675 if ((ret = gst_pad_link(pin->their_src, pin->my_sink)) < 0)
2677 ERR("Failed to link source pads, error %d.\n", ret);
2678 return FALSE;
2681 gst_pad_set_active(pin->my_sink, 1);
2682 gst_element_set_state(filter->container, GST_STATE_PAUSED);
2683 ret = gst_element_get_state(filter->container, NULL, NULL, -1);
2684 if (ret == GST_STATE_CHANGE_FAILURE)
2686 ERR("Failed to play stream.\n");
2687 return FALSE;
2690 events[0] = filter->duration_event;
2691 events[1] = filter->error_event;
2692 events[2] = pin->eos_event;
2693 res = WaitForMultipleObjects(3, events, FALSE, INFINITE);
2694 if (res == 1)
2695 return FALSE;
2697 pin->seek.llDuration = pin->seek.llStop = query_duration(pin->their_src);
2698 pin->seek.llCurrent = 0;
2700 events[0] = pin->caps_event;
2701 if (WaitForMultipleObjects(2, events, FALSE, INFINITE))
2702 return FALSE;
2704 filter->ignore_flush = TRUE;
2705 gst_element_set_state(filter->container, GST_STATE_READY);
2706 gst_element_get_state(filter->container, NULL, NULL, -1);
2707 filter->ignore_flush = FALSE;
2709 return TRUE;
2712 static HRESULT mpeg_splitter_source_query_accept(struct gstdemux_source *pin, const AM_MEDIA_TYPE *mt)
2714 AM_MEDIA_TYPE pad_mt;
2715 HRESULT hr;
2717 if (!get_source_amt(pin, &pad_mt))
2718 return E_OUTOFMEMORY;
2719 hr = compare_media_types(mt, &pad_mt) ? S_OK : S_FALSE;
2720 FreeMediaType(&pad_mt);
2721 return hr;
2724 static HRESULT mpeg_splitter_source_get_media_type(struct gstdemux_source *pin,
2725 unsigned int index, AM_MEDIA_TYPE *mt)
2727 if (index > 0)
2728 return VFW_S_NO_MORE_ITEMS;
2729 if (!get_source_amt(pin, mt))
2730 return E_OUTOFMEMORY;
2731 return S_OK;
2734 static HRESULT mpeg_splitter_query_interface(struct strmbase_filter *iface, REFIID iid, void **out)
2736 struct gstdemux *filter = impl_from_strmbase_filter(iface);
2738 if (IsEqualGUID(iid, &IID_IAMStreamSelect))
2740 *out = &filter->IAMStreamSelect_iface;
2741 IUnknown_AddRef((IUnknown *)*out);
2742 return S_OK;
2745 return E_NOINTERFACE;
2748 static const struct strmbase_filter_ops mpeg_splitter_ops =
2750 .filter_query_interface = mpeg_splitter_query_interface,
2751 .filter_get_pin = gstdemux_get_pin,
2752 .filter_destroy = gstdemux_destroy,
2753 .filter_init_stream = gstdemux_init_stream,
2754 .filter_start_stream = gstdemux_start_stream,
2755 .filter_stop_stream = gstdemux_stop_stream,
2756 .filter_cleanup_stream = gstdemux_cleanup_stream,
2757 .filter_wait_state = gstdemux_wait_state,
2760 HRESULT mpeg_splitter_create(IUnknown *outer, IUnknown **out)
2762 static const WCHAR sink_name[] = {'I','n','p','u','t',0};
2763 struct gstdemux *object;
2765 if (!init_gstreamer())
2766 return E_FAIL;
2768 mark_wine_thread();
2770 if (!(object = heap_alloc_zero(sizeof(*object))))
2771 return E_OUTOFMEMORY;
2773 strmbase_filter_init(&object->filter, outer, &CLSID_MPEG1Splitter, &mpeg_splitter_ops);
2774 strmbase_sink_init(&object->sink, &object->filter, sink_name, &mpeg_splitter_sink_ops, NULL);
2775 object->IAMStreamSelect_iface.lpVtbl = &stream_select_vtbl;
2777 object->duration_event = CreateEventW(NULL, FALSE, FALSE, NULL);
2778 object->error_event = CreateEventW(NULL, TRUE, FALSE, NULL);
2779 object->init_gst = mpeg_splitter_init_gst;
2780 object->source_query_accept = mpeg_splitter_source_query_accept;
2781 object->source_get_media_type = mpeg_splitter_source_get_media_type;
2782 object->enum_sink_first = TRUE;
2784 TRACE("Created MPEG-1 splitter %p.\n", object);
2785 *out = &object->filter.IUnknown_inner;
2786 return S_OK;