usp10/tests: A spelling fix in an ok() message.
[wine.git] / dlls / winegstreamer / gstdemux.c
blobb3d6a8350e011fe04bb1b9667d1828b4f3bdd195
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"
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26 #include <gst/audio/audio.h>
28 #include "gst_private.h"
29 #include "gst_guids.h"
30 #include "gst_cbs.h"
32 #include "vfwmsgs.h"
33 #include "amvideo.h"
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 #include <assert.h>
40 #include "dvdmedia.h"
41 #include "mmreg.h"
42 #include "ks.h"
43 #include "initguid.h"
44 #include "ksmedia.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
48 static pthread_key_t wine_gst_key;
50 typedef struct GSTOutPin GSTOutPin;
51 typedef struct GSTInPin {
52 BasePin pin;
53 IAsyncReader *pReader;
54 IMemAllocator *pAlloc;
55 } GSTInPin;
57 typedef struct GSTImpl {
58 BaseFilter filter;
60 GSTInPin pInputPin;
61 GSTOutPin **ppPins;
62 LONG cStreams;
64 LONGLONG filesize;
66 BOOL discont, initial, ignore_flush;
67 GstElement *container;
68 GstPad *my_src, *their_sink;
69 GstBus *bus;
70 guint64 start, nextofs, nextpullofs, stop;
71 ALLOCATOR_PROPERTIES props;
72 HANDLE no_more_pads_event, push_event;
74 HANDLE push_thread;
75 } GSTImpl;
77 struct GSTOutPin {
78 BaseOutputPin pin;
79 IQualityControl IQualityControl_iface;
81 GstElement *flipfilter;
82 GstPad *flip_sink, *flip_src;
83 GstPad *their_src;
84 GstPad *my_sink;
85 GstBufferPool *gstpool;
86 BOOL isaud, isvid;
87 AM_MEDIA_TYPE * pmt;
88 HANDLE caps_event;
89 GstSegment *segment;
90 SourceSeeking seek;
93 static inline GSTImpl *impl_from_IBaseFilter(IBaseFilter *iface)
95 return CONTAINING_RECORD(iface, GSTImpl, filter.IBaseFilter_iface);
98 const char* media_quark_string = "media-sample";
100 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
101 static const IMediaSeekingVtbl GST_Seeking_Vtbl;
102 static const IPinVtbl GST_OutputPin_Vtbl;
103 static const IPinVtbl GST_InputPin_Vtbl;
104 static const IBaseFilterVtbl GST_Vtbl;
105 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl;
107 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt);
108 static HRESULT GST_RemoveOutputPins(GSTImpl *This);
109 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface);
110 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface);
111 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface);
113 void mark_wine_thread(void)
115 /* set it to non-NULL to indicate that this is a Wine thread */
116 pthread_setspecific(wine_gst_key, &wine_gst_key);
119 BOOL is_wine_thread(void)
121 return pthread_getspecific(wine_gst_key) != NULL;
124 static gboolean amt_from_gst_caps_audio(GstCaps *caps, AM_MEDIA_TYPE *amt)
126 WAVEFORMATEXTENSIBLE *wfe;
127 WAVEFORMATEX *wfx;
128 gint32 depth, bpp;
129 GstAudioInfo ainfo;
131 if (!gst_audio_info_from_caps (&ainfo, caps))
132 return FALSE;
134 wfe = CoTaskMemAlloc(sizeof(*wfe));
135 wfx = (WAVEFORMATEX*)wfe;
136 amt->majortype = MEDIATYPE_Audio;
137 amt->subtype = MEDIASUBTYPE_PCM;
138 amt->formattype = FORMAT_WaveFormatEx;
139 amt->pbFormat = (BYTE*)wfe;
140 amt->cbFormat = sizeof(*wfe);
141 amt->bFixedSizeSamples = 0;
142 amt->bTemporalCompression = 1;
143 amt->lSampleSize = 0;
144 amt->pUnk = NULL;
146 wfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
148 wfx->nChannels = ainfo.channels;
149 wfx->nSamplesPerSec = ainfo.rate;
150 depth = GST_AUDIO_INFO_WIDTH(&ainfo);
151 bpp = GST_AUDIO_INFO_DEPTH(&ainfo);
153 if (!depth || depth > 32 || depth % 8)
154 depth = bpp;
155 else if (!bpp)
156 bpp = depth;
157 wfe->Samples.wValidBitsPerSample = depth;
158 wfx->wBitsPerSample = bpp;
159 wfx->cbSize = sizeof(*wfe)-sizeof(*wfx);
160 switch (wfx->nChannels) {
161 case 1: wfe->dwChannelMask = KSAUDIO_SPEAKER_MONO; break;
162 case 2: wfe->dwChannelMask = KSAUDIO_SPEAKER_STEREO; break;
163 case 4: wfe->dwChannelMask = KSAUDIO_SPEAKER_SURROUND; break;
164 case 5: wfe->dwChannelMask = (KSAUDIO_SPEAKER_5POINT1 & ~SPEAKER_LOW_FREQUENCY); break;
165 case 6: wfe->dwChannelMask = KSAUDIO_SPEAKER_5POINT1; break;
166 case 8: wfe->dwChannelMask = KSAUDIO_SPEAKER_7POINT1; break;
167 default:
168 wfe->dwChannelMask = 0;
170 if (GST_AUDIO_INFO_IS_FLOAT(&ainfo)) {
171 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
172 wfx->wBitsPerSample = wfe->Samples.wValidBitsPerSample = 32;
173 } else {
174 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
175 if (wfx->nChannels <= 2 && bpp <= 16 && depth == bpp) {
176 wfx->wFormatTag = WAVE_FORMAT_PCM;
177 wfx->cbSize = 0;
180 wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample/8;
181 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
182 return TRUE;
185 static gboolean amt_from_gst_caps_video(GstCaps *caps, AM_MEDIA_TYPE *amt)
187 VIDEOINFOHEADER *vih;
188 BITMAPINFOHEADER *bih;
189 gint32 width, height, nom, denom;
190 GstVideoInfo vinfo;
192 if (!gst_video_info_from_caps (&vinfo, caps))
193 return FALSE;
194 width = vinfo.width;
195 height = vinfo.height;
196 nom = vinfo.fps_n;
197 denom = vinfo.fps_d;
199 vih = CoTaskMemAlloc(sizeof(*vih));
200 bih = &vih->bmiHeader;
202 amt->formattype = FORMAT_VideoInfo;
203 amt->pbFormat = (BYTE*)vih;
204 amt->cbFormat = sizeof(*vih);
205 amt->bFixedSizeSamples = amt->bTemporalCompression = 1;
206 amt->lSampleSize = 0;
207 amt->pUnk = NULL;
208 ZeroMemory(vih, sizeof(*vih));
209 amt->majortype = MEDIATYPE_Video;
210 if (GST_VIDEO_INFO_IS_RGB(&vinfo)) {
211 bih->biBitCount = GST_VIDEO_FORMAT_INFO_BITS(vinfo.finfo);
212 switch (bih->biBitCount) {
213 case 16: amt->subtype = MEDIASUBTYPE_RGB555; break;
214 case 24: amt->subtype = MEDIASUBTYPE_RGB24; break;
215 case 32: amt->subtype = MEDIASUBTYPE_RGB32; break;
216 default:
217 FIXME("Unknown bpp %u\n", bih->biBitCount);
218 CoTaskMemFree(vih);
219 return FALSE;
221 bih->biCompression = BI_RGB;
222 } else {
223 amt->subtype = MEDIATYPE_Video;
224 if (!(amt->subtype.Data1 = gst_video_format_to_fourcc(vinfo.finfo->format))) {
225 CoTaskMemFree(vih);
226 return FALSE;
228 switch (amt->subtype.Data1) {
229 case mmioFOURCC('I','4','2','0'):
230 case mmioFOURCC('Y','V','1','2'):
231 case mmioFOURCC('N','V','1','2'):
232 case mmioFOURCC('N','V','2','1'):
233 bih->biBitCount = 12; break;
234 case mmioFOURCC('Y','U','Y','2'):
235 case mmioFOURCC('Y','V','Y','U'):
236 bih->biBitCount = 16; break;
238 bih->biCompression = amt->subtype.Data1;
240 bih->biSizeImage = width * height * bih->biBitCount / 8;
241 if ((vih->AvgTimePerFrame = (REFERENCE_TIME)MulDiv(10000000, denom, nom)) == -1)
242 vih->AvgTimePerFrame = 0; /* zero division or integer overflow */
243 vih->rcSource.left = 0;
244 vih->rcSource.right = width;
245 vih->rcSource.top = height;
246 vih->rcSource.bottom = 0;
247 vih->rcTarget = vih->rcSource;
248 bih->biSize = sizeof(*bih);
249 bih->biWidth = width;
250 bih->biHeight = height;
251 bih->biPlanes = 1;
252 return TRUE;
255 static gboolean accept_caps_sink(GstPad *pad, GstCaps *caps)
257 GSTOutPin *pin = gst_pad_get_element_private(pad);
258 AM_MEDIA_TYPE amt;
259 GstStructure *arg;
260 const char *typename;
261 gboolean ret;
263 TRACE("%p %p\n", pad, caps);
265 arg = gst_caps_get_structure(caps, 0);
266 typename = gst_structure_get_name(arg);
267 if (!strcmp(typename, "audio/x-raw")) {
268 if (!pin->isaud) {
269 ERR("Setting audio caps on non-audio pad?\n");
270 return FALSE;
272 ret = amt_from_gst_caps_audio(caps, &amt);
273 if (ret)
274 FreeMediaType(&amt);
275 TRACE("+%i\n", ret);
276 return ret;
277 } else if (!strcmp(typename, "video/x-raw")) {
278 if (!pin->isvid) {
279 ERR("Setting video caps on non-video pad?\n");
280 return FALSE;
282 ret = amt_from_gst_caps_video(caps, &amt);
283 if (ret)
284 FreeMediaType(&amt);
285 TRACE("-%i\n", ret);
286 return ret;
287 } else {
288 FIXME("Unhandled type \"%s\"\n", typename);
289 return FALSE;
293 static gboolean setcaps_sink(GstPad *pad, GstCaps *caps)
295 GSTOutPin *pin = gst_pad_get_element_private(pad);
296 GSTImpl *This = impl_from_IBaseFilter(pin->pin.pin.pinInfo.pFilter);
297 AM_MEDIA_TYPE amt;
298 GstStructure *arg;
299 const char *typename;
300 gboolean ret;
302 TRACE("%p %p\n", pad, caps);
304 arg = gst_caps_get_structure(caps, 0);
305 typename = gst_structure_get_name(arg);
306 if (!strcmp(typename, "audio/x-raw")) {
307 if (!pin->isaud) {
308 ERR("Setting audio caps on non-audio pad?\n");
309 return FALSE;
311 ret = amt_from_gst_caps_audio(caps, &amt);
312 } else if (!strcmp(typename, "video/x-raw")) {
313 if (!pin->isvid) {
314 ERR("Setting video caps on non-video pad?\n");
315 return FALSE;
317 ret = amt_from_gst_caps_video(caps, &amt);
318 if (ret)
319 This->props.cbBuffer = max(This->props.cbBuffer, ((VIDEOINFOHEADER*)amt.pbFormat)->bmiHeader.biSizeImage);
320 } else {
321 FIXME("Unhandled type \"%s\"\n", typename);
322 return FALSE;
324 TRACE("Linking returned %i for %s\n", ret, typename);
325 if (!ret)
326 return FALSE;
327 FreeMediaType(pin->pmt);
328 *pin->pmt = amt;
329 SetEvent(pin->caps_event);
330 return TRUE;
333 static gboolean query_sink(GstPad *pad, GstObject *parent, GstQuery *query)
335 switch (GST_QUERY_TYPE (query)) {
336 case GST_QUERY_ACCEPT_CAPS:
338 GstCaps *caps;
339 gboolean res;
340 gst_query_parse_accept_caps(query, &caps);
341 res = accept_caps_sink(pad, caps);
342 gst_query_set_accept_caps_result(query, res);
343 return TRUE; /* FIXME */
345 default:
346 return gst_pad_query_default (pad, parent, query);
350 static gboolean gst_base_src_perform_seek(GSTImpl *This, GstEvent *event)
352 gboolean res = TRUE;
353 gdouble rate;
354 GstFormat seek_format;
355 GstSeekFlags flags;
356 GstSeekType cur_type, stop_type;
357 gint64 cur, stop;
358 gboolean flush;
359 guint32 seqnum;
360 GstEvent *tevent;
361 BOOL thread = !!This->push_thread;
363 TRACE("%p %p\n", This, event);
365 gst_event_parse_seek(event, &rate, &seek_format, &flags,
366 &cur_type, &cur, &stop_type, &stop);
368 if (seek_format != GST_FORMAT_BYTES) {
369 FIXME("Not handling other format %i\n", seek_format);
370 return FALSE;
373 flush = flags & GST_SEEK_FLAG_FLUSH;
374 seqnum = gst_event_get_seqnum(event);
376 /* send flush start */
377 if (flush) {
378 tevent = gst_event_new_flush_start();
379 gst_event_set_seqnum(tevent, seqnum);
380 gst_pad_push_event(This->my_src, tevent);
381 if (This->pInputPin.pReader)
382 IAsyncReader_BeginFlush(This->pInputPin.pReader);
383 if (thread)
384 gst_pad_set_active(This->my_src, 1);
387 This->nextofs = This->start = cur;
389 /* and prepare to continue streaming */
390 if (flush) {
391 tevent = gst_event_new_flush_stop(TRUE);
392 gst_event_set_seqnum(tevent, seqnum);
393 gst_pad_push_event(This->my_src, tevent);
394 if (This->pInputPin.pReader)
395 IAsyncReader_EndFlush(This->pInputPin.pReader);
396 if (thread)
397 gst_pad_set_active(This->my_src, 1);
400 return res;
403 static gboolean event_src(GstPad *pad, GstObject *parent, GstEvent *event)
405 GSTImpl *This = gst_pad_get_element_private(pad);
407 TRACE("%p %p\n", pad, event);
409 switch (event->type) {
410 case GST_EVENT_SEEK:
411 return gst_base_src_perform_seek(This, event);
412 case GST_EVENT_FLUSH_START:
413 EnterCriticalSection(&This->filter.csFilter);
414 if (This->pInputPin.pReader)
415 IAsyncReader_BeginFlush(This->pInputPin.pReader);
416 LeaveCriticalSection(&This->filter.csFilter);
417 break;
418 case GST_EVENT_FLUSH_STOP:
419 EnterCriticalSection(&This->filter.csFilter);
420 if (This->pInputPin.pReader)
421 IAsyncReader_EndFlush(This->pInputPin.pReader);
422 LeaveCriticalSection(&This->filter.csFilter);
423 break;
424 default:
425 FIXME("%p (%u) stub\n", event, event->type);
426 case GST_EVENT_TAG:
427 case GST_EVENT_QOS:
428 return gst_pad_event_default(pad, parent, event);
430 return TRUE;
433 static gboolean event_sink(GstPad *pad, GstObject *parent, GstEvent *event)
435 GSTOutPin *pin = gst_pad_get_element_private(pad);
437 TRACE("%p %p\n", pad, event);
439 switch (event->type) {
440 case GST_EVENT_SEGMENT: {
441 gdouble rate, applied_rate;
442 gint64 stop, pos;
443 const GstSegment *segment;
445 gst_event_parse_segment(event, &segment);
447 pos = segment->position;
448 stop = segment->stop;
449 rate = segment->rate;
450 applied_rate = segment->applied_rate;
452 if (segment->format != GST_FORMAT_TIME) {
453 FIXME("Ignoring new segment because of format %i\n", segment->format);
454 return TRUE;
457 gst_segment_copy_into(segment, pin->segment);
459 pos /= 100;
461 if (stop > 0)
462 stop /= 100;
464 if (pin->pin.pin.pConnectedTo)
465 IPin_NewSegment(pin->pin.pin.pConnectedTo, pos, stop, rate*applied_rate);
467 return TRUE;
469 case GST_EVENT_EOS:
470 if (pin->pin.pin.pConnectedTo)
471 IPin_EndOfStream(pin->pin.pin.pConnectedTo);
472 return TRUE;
473 case GST_EVENT_FLUSH_START:
474 if (impl_from_IBaseFilter(pin->pin.pin.pinInfo.pFilter)->ignore_flush) {
475 /* gst-plugins-base prior to 1.7 contains a bug which causes
476 * our sink pins to receive a flush-start event when the
477 * decodebin changes from PAUSED to READY (including
478 * PLAYING->PAUSED->READY), but no matching flush-stop event is
479 * sent. See <gst-plugins-base.git:60bad4815db966a8e4). Here we
480 * unset the flushing flag to avoid the problem. */
481 TRACE("Working around gst <1.7 bug, ignoring FLUSH_START\n");
482 GST_PAD_UNSET_FLUSHING (pad);
483 return TRUE;
485 if (pin->pin.pin.pConnectedTo)
486 IPin_BeginFlush(pin->pin.pin.pConnectedTo);
487 return TRUE;
488 case GST_EVENT_FLUSH_STOP:
489 gst_segment_init(pin->segment, GST_FORMAT_TIME);
490 if (pin->pin.pin.pConnectedTo)
491 IPin_EndFlush(pin->pin.pin.pConnectedTo);
492 return TRUE;
493 case GST_EVENT_CAPS: {
494 GstCaps *caps;
495 gst_event_parse_caps(event, &caps);
496 return setcaps_sink(pad, caps);
498 default:
499 TRACE("%p stub %s\n", event, gst_event_type_get_name(event->type));
500 return gst_pad_event_default(pad, parent, event);
504 static void release_sample(void *data)
506 ULONG ret;
507 ret = IMediaSample_Release((IMediaSample *)data);
508 TRACE("Releasing %p returns %u\n", data, ret);
511 static DWORD CALLBACK push_data(LPVOID iface)
513 LONGLONG maxlen, curlen;
514 GSTImpl *This = iface;
515 IMediaSample *buf;
516 DWORD_PTR user;
517 HRESULT hr;
519 IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
521 if (!This->stop)
522 IAsyncReader_Length(This->pInputPin.pReader, &maxlen, &curlen);
523 else
524 maxlen = This->stop;
526 TRACE("Waiting..\n");
528 WaitForSingleObject(This->push_event, INFINITE);
530 TRACE("Starting..\n");
531 for (;;) {
532 REFERENCE_TIME tStart, tStop;
533 ULONG len;
534 GstBuffer *gstbuf;
535 gsize bufsize;
536 BYTE *data;
537 int ret;
539 TRACE("pAlloc: %p\n", This->pInputPin.pAlloc);
540 hr = IMemAllocator_GetBuffer(This->pInputPin.pAlloc, &buf, NULL, NULL, 0);
541 if (FAILED(hr))
542 break;
544 if (This->nextofs >= maxlen)
545 break;
546 len = IMediaSample_GetSize(buf);
547 if (This->nextofs + len > maxlen)
548 len = maxlen - This->nextofs;
550 tStart = MEDIATIME_FROM_BYTES(This->nextofs);
551 tStop = tStart + MEDIATIME_FROM_BYTES(len);
552 IMediaSample_SetTime(buf, &tStart, &tStop);
554 hr = IAsyncReader_Request(This->pInputPin.pReader, buf, 0);
555 if (FAILED(hr)) {
556 IMediaSample_Release(buf);
557 break;
559 This->nextofs += len;
560 hr = IAsyncReader_WaitForNext(This->pInputPin.pReader, -1, &buf, &user);
561 if (FAILED(hr) || !buf) {
562 if (buf)
563 IMediaSample_Release(buf);
564 break;
567 IMediaSample_GetPointer(buf, &data);
568 bufsize = IMediaSample_GetActualDataLength(buf);
569 gstbuf = gst_buffer_new_wrapped_full(0, data, bufsize, 0, bufsize, buf, release_sample_wrapper);
570 IMediaSample_AddRef(buf);
571 gst_mini_object_set_qdata(GST_MINI_OBJECT(gstbuf), g_quark_from_static_string(media_quark_string), buf, release_sample_wrapper);
572 if (!gstbuf) {
573 IMediaSample_Release(buf);
574 break;
576 gstbuf->duration = gstbuf->pts = -1;
577 ret = gst_pad_push(This->my_src, gstbuf);
578 if (ret >= 0)
579 hr = S_OK;
580 else
581 ERR("Sending returned: %i\n", ret);
582 if (ret == GST_FLOW_ERROR)
583 hr = E_FAIL;
584 else if (ret == GST_FLOW_FLUSHING)
585 hr = VFW_E_WRONG_STATE;
586 if (hr != S_OK)
587 break;
590 gst_pad_push_event(This->my_src, gst_event_new_eos());
592 TRACE("Almost stopping.. %08x\n", hr);
593 do {
594 IAsyncReader_WaitForNext(This->pInputPin.pReader, 0, &buf, &user);
595 if (buf)
596 IMediaSample_Release(buf);
597 } while (buf);
599 TRACE("Stopping.. %08x\n", hr);
601 IBaseFilter_Release(&This->filter.IBaseFilter_iface);
603 return 0;
606 static GstFlowReturn got_data_sink(GstPad *pad, GstObject *parent, GstBuffer *buf)
608 GSTOutPin *pin = gst_pad_get_element_private(pad);
609 GSTImpl *This = impl_from_IBaseFilter(pin->pin.pin.pinInfo.pFilter);
610 HRESULT hr;
611 BYTE *ptr = NULL;
612 IMediaSample *sample;
613 GstMapInfo info;
615 TRACE("%p %p\n", pad, buf);
617 if (This->initial) {
618 gst_buffer_unref(buf);
619 return GST_FLOW_OK;
622 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
624 if (hr == VFW_E_NOT_CONNECTED) {
625 gst_buffer_unref(buf);
626 return GST_FLOW_NOT_LINKED;
629 if (FAILED(hr)) {
630 gst_buffer_unref(buf);
631 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
632 return GST_FLOW_FLUSHING;
635 gst_buffer_map(buf, &info, GST_MAP_READ);
637 hr = IMediaSample_SetActualDataLength(sample, info.size);
638 if(FAILED(hr)){
639 WARN("SetActualDataLength failed: %08x\n", hr);
640 return GST_FLOW_FLUSHING;
643 IMediaSample_GetPointer(sample, &ptr);
645 memcpy(ptr, info.data, info.size);
647 gst_buffer_unmap(buf, &info);
649 if (GST_BUFFER_PTS_IS_VALID(buf)) {
650 REFERENCE_TIME rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts);
651 if (rtStart >= 0)
652 rtStart /= 100;
654 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
655 REFERENCE_TIME tStart = buf->pts / 100;
656 REFERENCE_TIME tStop = (buf->pts + buf->duration) / 100;
657 REFERENCE_TIME rtStop;
658 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts + buf->duration);
659 if (rtStop >= 0)
660 rtStop /= 100;
661 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
662 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
663 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
664 } else {
665 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
666 IMediaSample_SetMediaTime(sample, NULL, NULL);
668 } else {
669 IMediaSample_SetTime(sample, NULL, NULL);
670 IMediaSample_SetMediaTime(sample, NULL, NULL);
673 IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
674 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_LIVE));
675 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
677 if (!pin->pin.pin.pConnectedTo)
678 hr = VFW_E_NOT_CONNECTED;
679 else
680 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
682 TRACE("sending sample returned: %08x\n", hr);
684 gst_buffer_unref(buf);
685 IMediaSample_Release(sample);
687 if (hr == VFW_E_NOT_CONNECTED)
688 return GST_FLOW_NOT_LINKED;
690 if (FAILED(hr))
691 return GST_FLOW_FLUSHING;
693 return GST_FLOW_OK;
696 static GstFlowReturn request_buffer_src(GstPad *pad, GstObject *parent, guint64 ofs, guint len, GstBuffer **buf)
698 GSTImpl *This = gst_pad_get_element_private(pad);
699 HRESULT hr;
700 GstMapInfo info;
702 TRACE("%p %s %i %p\n", pad, wine_dbgstr_longlong(ofs), len, buf);
704 *buf = NULL;
705 if (ofs == GST_BUFFER_OFFSET_NONE)
706 ofs = This->nextpullofs;
707 if (ofs >= This->filesize) {
708 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
709 return GST_FLOW_EOS;
711 if (len + ofs > This->filesize)
712 len = This->filesize - ofs;
713 This->nextpullofs = ofs + len;
715 *buf = gst_buffer_new_and_alloc(len);
716 gst_buffer_map(*buf, &info, GST_MAP_WRITE);
717 hr = IAsyncReader_SyncRead(This->pInputPin.pReader, ofs, len, info.data);
718 gst_buffer_unmap(*buf, &info);
719 if (FAILED(hr)) {
720 ERR("Returned %08x\n", hr);
721 return GST_FLOW_ERROR;
724 GST_BUFFER_OFFSET(*buf) = ofs;
725 return GST_FLOW_OK;
728 static DWORD CALLBACK push_data_init(LPVOID iface)
730 GSTImpl *This = iface;
731 DWORD64 ofs = 0;
733 TRACE("Starting..\n");
734 for (;;) {
735 GstBuffer *buf;
736 GstFlowReturn ret = request_buffer_src(This->my_src, NULL, ofs, 4096, &buf);
737 if (ret < 0) {
738 ERR("Obtaining buffer returned: %i\n", ret);
739 break;
741 ret = gst_pad_push(This->my_src, buf);
742 ofs += 4096;
743 if (ret)
744 TRACE("Sending returned: %i\n", ret);
745 if (ret < 0)
746 break;
748 TRACE("Stopping..\n");
749 return 0;
752 static void removed_decoded_pad(GstElement *bin, GstPad *pad, gpointer user)
754 GSTImpl *This = (GSTImpl*)user;
755 int x;
756 GSTOutPin *pin;
758 TRACE("%p %p %p\n", This, bin, pad);
760 EnterCriticalSection(&This->filter.csFilter);
761 for (x = 0; x < This->cStreams; ++x) {
762 if (This->ppPins[x]->their_src == pad)
763 break;
765 if (x == This->cStreams)
766 goto out;
768 pin = This->ppPins[x];
770 if(pin->flipfilter)
771 gst_pad_unlink(pin->their_src, pin->flip_sink);
772 else
773 gst_pad_unlink(pin->their_src, pin->my_sink);
775 gst_object_unref(pin->their_src);
776 pin->their_src = NULL;
777 out:
778 TRACE("Removed %i/%i\n", x, This->cStreams);
779 LeaveCriticalSection(&This->filter.csFilter);
782 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, GSTImpl *This)
784 HRESULT hr;
785 PIN_INFO piOutput;
786 const char *typename;
787 char *name;
788 AM_MEDIA_TYPE amt = {{0}};
789 GstCaps *caps;
790 GstStructure *arg;
791 GstPad *mypad;
792 GSTOutPin *pin;
793 int ret;
794 BOOL isvid = FALSE, isaud = FALSE;
795 gchar my_name[1024];
797 TRACE("%p %p %p\n", This, bin, pad);
799 piOutput.dir = PINDIR_OUTPUT;
800 piOutput.pFilter = &This->filter.IBaseFilter_iface;
801 name = gst_pad_get_name(pad);
802 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, piOutput.achName, ARRAY_SIZE(piOutput.achName) - 1);
803 TRACE("Name: %s\n", name);
804 strcpy(my_name, "qz_sink_");
805 strcat(my_name, name);
806 g_free(name);
807 piOutput.achName[ARRAY_SIZE(piOutput.achName) - 1] = 0;
809 caps = gst_pad_query_caps(pad, NULL);
810 caps = gst_caps_make_writable(caps);
811 arg = gst_caps_get_structure(caps, 0);
812 typename = gst_structure_get_name(arg);
814 mypad = gst_pad_new(my_name, GST_PAD_SINK);
815 gst_pad_set_chain_function(mypad, got_data_sink_wrapper);
816 gst_pad_set_event_function(mypad, event_sink_wrapper);
817 gst_pad_set_query_function(mypad, query_sink_wrapper);
819 if (!strcmp(typename, "audio/x-raw")) {
820 isaud = TRUE;
821 } else if (!strcmp(typename, "video/x-raw")) {
822 isvid = TRUE;
823 } else {
824 FIXME("Unknown type \'%s\'\n", typename);
825 return;
828 hr = GST_AddPin(This, &piOutput, &amt);
829 if (FAILED(hr)) {
830 ERR("%08x\n", hr);
831 return;
834 pin = This->ppPins[This->cStreams - 1];
835 gst_pad_set_element_private(mypad, pin);
836 pin->my_sink = mypad;
837 pin->isaud = isaud;
838 pin->isvid = isvid;
840 gst_segment_init(pin->segment, GST_FORMAT_TIME);
842 if (isvid) {
843 GstElement *vconv;
845 TRACE("setting up videoflip filter for pin %p, my_sink: %p, their_src: %p\n",
846 pin, pin->my_sink, pad);
848 /* gstreamer outputs video top-down, but dshow expects bottom-up, so
849 * make new transform filter to invert video */
850 vconv = gst_element_factory_make("videoconvert", NULL);
851 if(!vconv){
852 ERR("Missing videoconvert filter?\n");
853 ret = -1;
854 goto exit;
857 pin->flipfilter = gst_element_factory_make("videoflip", NULL);
858 if(!pin->flipfilter){
859 ERR("Missing videoflip filter?\n");
860 ret = -1;
861 goto exit;
864 gst_util_set_object_arg(G_OBJECT(pin->flipfilter), "method", "vertical-flip");
866 gst_bin_add(GST_BIN(This->container), vconv); /* bin takes ownership */
867 gst_element_sync_state_with_parent(vconv);
868 gst_bin_add(GST_BIN(This->container), pin->flipfilter); /* bin takes ownership */
869 gst_element_sync_state_with_parent(pin->flipfilter);
871 gst_element_link (vconv, pin->flipfilter);
873 pin->flip_sink = gst_element_get_static_pad(vconv, "sink");
874 if(!pin->flip_sink){
875 WARN("Couldn't find sink on flip filter\n");
876 pin->flipfilter = NULL;
877 ret = -1;
878 goto exit;
881 ret = gst_pad_link(pad, pin->flip_sink);
882 if(ret < 0){
883 WARN("gst_pad_link failed: %d\n", ret);
884 gst_object_unref(pin->flip_sink);
885 pin->flip_sink = NULL;
886 pin->flipfilter = NULL;
887 goto exit;
890 pin->flip_src = gst_element_get_static_pad(pin->flipfilter, "src");
891 if(!pin->flip_src){
892 WARN("Couldn't find src on flip filter\n");
893 gst_object_unref(pin->flip_sink);
894 pin->flip_sink = NULL;
895 pin->flipfilter = NULL;
896 ret = -1;
897 goto exit;
900 ret = gst_pad_link(pin->flip_src, pin->my_sink);
901 if(ret < 0){
902 WARN("gst_pad_link failed: %d\n", ret);
903 gst_object_unref(pin->flip_src);
904 pin->flip_src = NULL;
905 gst_object_unref(pin->flip_sink);
906 pin->flip_sink = NULL;
907 pin->flipfilter = NULL;
908 goto exit;
910 } else
911 ret = gst_pad_link(pad, mypad);
913 gst_pad_set_active(mypad, 1);
915 exit:
916 TRACE("Linking: %i\n", ret);
918 if (ret >= 0) {
919 pin->their_src = pad;
920 gst_object_ref(pin->their_src);
924 static void existing_new_pad(GstElement *bin, GstPad *pad, gpointer user)
926 GSTImpl *This = (GSTImpl*)user;
927 int x, ret;
929 TRACE("%p %p %p\n", This, bin, pad);
931 if (gst_pad_is_linked(pad))
932 return;
934 /* Still holding our own lock */
935 if (This->initial) {
936 init_new_decoded_pad(bin, pad, This);
937 return;
940 EnterCriticalSection(&This->filter.csFilter);
941 for (x = 0; x < This->cStreams; ++x) {
942 GSTOutPin *pin = This->ppPins[x];
943 if (!pin->their_src) {
944 gst_segment_init(pin->segment, GST_FORMAT_TIME);
946 if (pin->flipfilter)
947 ret = gst_pad_link(pad, pin->flip_sink);
948 else
949 ret = gst_pad_link(pad, pin->my_sink);
951 if (ret >= 0) {
952 pin->their_src = pad;
953 gst_object_ref(pin->their_src);
954 TRACE("Relinked\n");
955 LeaveCriticalSection(&This->filter.csFilter);
956 return;
960 init_new_decoded_pad(bin, pad, This);
961 LeaveCriticalSection(&This->filter.csFilter);
964 static gboolean query_function(GstPad *pad, GstObject *parent, GstQuery *query)
966 GSTImpl *This = gst_pad_get_element_private(pad);
967 GstFormat format;
968 int ret;
969 LONGLONG duration;
971 TRACE("%p %p %p\n", This, pad, query);
973 switch (GST_QUERY_TYPE(query)) {
974 case GST_QUERY_DURATION:
975 gst_query_parse_duration (query, &format, NULL);
976 if (format == GST_FORMAT_PERCENT) {
977 gst_query_set_duration (query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
978 return TRUE;
980 ret = gst_pad_query_convert (pad, GST_FORMAT_BYTES, This->filesize, format, &duration);
981 gst_query_set_duration(query, format, duration);
982 return ret;
983 case GST_QUERY_SEEKING:
984 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
985 TRACE("Seeking %i %i\n", format, GST_FORMAT_BYTES);
986 if (format != GST_FORMAT_BYTES)
987 return FALSE;
988 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
989 return TRUE;
990 case GST_QUERY_SCHEDULING:
991 gst_query_set_scheduling(query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
992 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PUSH);
993 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PULL);
994 return TRUE;
995 default:
996 TRACE("Unhandled query type: %s\n", GST_QUERY_TYPE_NAME(query));
997 return FALSE;
1001 static gboolean activate_push(GstPad *pad, gboolean activate)
1003 GSTImpl *This = gst_pad_get_element_private(pad);
1005 TRACE("%p %p %u\n", This, pad, activate);
1007 EnterCriticalSection(&This->filter.csFilter);
1008 if (!activate) {
1009 TRACE("Deactivating\n");
1010 if (!This->initial)
1011 IAsyncReader_BeginFlush(This->pInputPin.pReader);
1012 if (This->push_thread) {
1013 WaitForSingleObject(This->push_thread, -1);
1014 CloseHandle(This->push_thread);
1015 This->push_thread = NULL;
1017 if (!This->initial)
1018 IAsyncReader_EndFlush(This->pInputPin.pReader);
1019 if (This->filter.state == State_Stopped)
1020 This->nextofs = This->start;
1021 } else if (!This->push_thread) {
1022 TRACE("Activating\n");
1023 if (This->initial)
1024 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
1025 else
1026 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
1028 LeaveCriticalSection(&This->filter.csFilter);
1029 return TRUE;
1032 static gboolean activate_mode(GstPad *pad, GstObject *parent, GstPadMode mode, gboolean activate)
1034 TRACE("%p %p 0x%x %u\n", pad, parent, mode, activate);
1035 switch (mode) {
1036 case GST_PAD_MODE_PULL:
1037 return TRUE;
1038 case GST_PAD_MODE_PUSH:
1039 return activate_push(pad, activate);
1040 default:
1041 return FALSE;
1043 return FALSE;
1046 static void no_more_pads(GstElement *decodebin, gpointer user)
1048 GSTImpl *This = (GSTImpl*)user;
1049 TRACE("%p %p\n", This, decodebin);
1050 SetEvent(This->no_more_pads_event);
1053 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, gpointer user)
1055 const char *name = gst_element_factory_get_longname(fact);
1057 if (strstr(name, "Player protection")) {
1058 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
1059 return GST_AUTOPLUG_SELECT_SKIP;
1061 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder")) {
1062 WARN("Disabled video acceleration since it breaks in wine\n");
1063 return GST_AUTOPLUG_SELECT_SKIP;
1065 TRACE("using \"%s\"\n", name);
1066 return GST_AUTOPLUG_SELECT_TRY;
1069 static GstBusSyncReply watch_bus(GstBus *bus, GstMessage *msg, gpointer data)
1071 GSTImpl *This = data;
1072 GError *err = NULL;
1073 gchar *dbg_info = NULL;
1075 TRACE("%p %p %p\n", This, bus, msg);
1077 if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_ERROR) {
1078 gst_message_parse_error(msg, &err, &dbg_info);
1079 ERR("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1080 ERR("%s\n", dbg_info);
1081 } else if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_WARNING) {
1082 gst_message_parse_warning(msg, &err, &dbg_info);
1083 WARN("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1084 WARN("%s\n", dbg_info);
1086 if (err)
1087 g_error_free(err);
1088 g_free(dbg_info);
1089 return GST_BUS_DROP;
1092 static void unknown_type(GstElement *bin, GstPad *pad, GstCaps *caps, gpointer user)
1094 gchar *strcaps = gst_caps_to_string(caps);
1095 ERR("Could not find a filter for caps: %s\n", debugstr_a(strcaps));
1096 g_free(strcaps);
1099 static HRESULT GST_Connect(GSTInPin *pPin, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
1101 GSTImpl *This = impl_from_IBaseFilter(pPin->pin.pinInfo.pFilter);
1102 int ret, i;
1103 LONGLONG avail, duration;
1104 GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
1105 "quartz_src",
1106 GST_PAD_SRC,
1107 GST_PAD_ALWAYS,
1108 GST_STATIC_CAPS_ANY);
1109 GstElement *gstfilter;
1111 TRACE("%p %p %p\n", pPin, pConnectPin, props);
1113 This->props = *props;
1114 IAsyncReader_Length(pPin->pReader, &This->filesize, &avail);
1116 if (!This->bus) {
1117 This->bus = gst_bus_new();
1118 gst_bus_set_sync_handler(This->bus, watch_bus_wrapper, This, NULL);
1121 This->container = gst_bin_new(NULL);
1122 gst_element_set_bus(This->container, This->bus);
1124 gstfilter = gst_element_factory_make("decodebin", NULL);
1125 if (!gstfilter) {
1126 ERR("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
1127 8 * (int)sizeof(void*));
1128 return E_FAIL;
1131 gst_bin_add(GST_BIN(This->container), gstfilter);
1133 g_signal_connect(gstfilter, "pad-added", G_CALLBACK(existing_new_pad_wrapper), This);
1134 g_signal_connect(gstfilter, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), This);
1135 g_signal_connect(gstfilter, "autoplug-select", G_CALLBACK(autoplug_blacklist_wrapper), This);
1136 g_signal_connect(gstfilter, "unknown-type", G_CALLBACK(unknown_type_wrapper), This);
1138 This->my_src = gst_pad_new_from_static_template(&src_template, "quartz-src");
1139 gst_pad_set_getrange_function(This->my_src, request_buffer_src_wrapper);
1140 gst_pad_set_query_function(This->my_src, query_function_wrapper);
1141 gst_pad_set_activatemode_function(This->my_src, activate_mode_wrapper);
1142 gst_pad_set_event_function(This->my_src, event_src_wrapper);
1143 gst_pad_set_element_private (This->my_src, This);
1144 This->their_sink = gst_element_get_static_pad(gstfilter, "sink");
1146 g_signal_connect(gstfilter, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), This);
1147 ret = gst_pad_link(This->my_src, This->their_sink);
1148 if (ret < 0) {
1149 ERR("Returns: %i\n", ret);
1150 return E_FAIL;
1152 This->start = This->nextofs = This->nextpullofs = This->stop = 0;
1154 /* Add initial pins */
1155 This->initial = This->discont = TRUE;
1156 ResetEvent(This->no_more_pads_event);
1157 gst_element_set_state(This->container, GST_STATE_PLAYING);
1158 ret = gst_element_get_state(This->container, NULL, NULL, -1);
1160 if (ret == GST_STATE_CHANGE_FAILURE)
1162 ERR("GStreamer failed to play stream\n");
1163 return E_FAIL;
1166 WaitForSingleObject(This->no_more_pads_event, INFINITE);
1168 gst_pad_query_duration(This->ppPins[0]->their_src, GST_FORMAT_TIME, &duration);
1169 for (i = 0; i < This->cStreams; ++i)
1171 This->ppPins[i]->seek.llDuration = This->ppPins[i]->seek.llStop = duration / 100;
1172 This->ppPins[i]->seek.llCurrent = 0;
1173 if (!This->ppPins[i]->seek.llDuration)
1174 This->ppPins[i]->seek.dwCapabilities = 0;
1175 WaitForSingleObject(This->ppPins[i]->caps_event, INFINITE);
1177 *props = This->props;
1179 This->ignore_flush = TRUE;
1180 gst_element_set_state(This->container, GST_STATE_READY);
1181 gst_element_get_state(This->container, NULL, NULL, -1);
1182 This->ignore_flush = FALSE;
1184 This->initial = FALSE;
1186 /* don't set active during test-play, as we don't want to push/pull data
1187 * from the source yet */
1188 gst_pad_set_active(This->my_src, 1);
1190 This->nextofs = This->nextpullofs = 0;
1191 return S_OK;
1194 static inline GSTOutPin *impl_from_IMediaSeeking( IMediaSeeking *iface )
1196 return CONTAINING_RECORD(iface, GSTOutPin, seek.IMediaSeeking_iface);
1199 static IPin *gstdemux_get_pin(BaseFilter *base, unsigned int index)
1201 GSTImpl *filter = impl_from_IBaseFilter(&base->IBaseFilter_iface);
1203 if (!index)
1204 return &filter->pInputPin.pin.IPin_iface;
1205 else if (index <= filter->cStreams)
1206 return &filter->ppPins[index - 1]->pin.pin.IPin_iface;
1207 return NULL;
1210 static void gstdemux_destroy(BaseFilter *iface)
1212 GSTImpl *filter = impl_from_IBaseFilter(&iface->IBaseFilter_iface);
1213 IPin *connected = NULL;
1214 HRESULT hr;
1216 CloseHandle(filter->no_more_pads_event);
1217 CloseHandle(filter->push_event);
1219 /* Don't need to clean up output pins, disconnecting input pin will do that */
1220 IPin_ConnectedTo((IPin *)&filter->pInputPin, &connected);
1221 if (connected)
1223 hr = IPin_Disconnect(connected);
1224 assert(hr == S_OK);
1225 IPin_Release(connected);
1226 hr = IPin_Disconnect(&filter->pInputPin.pin.IPin_iface);
1227 assert(hr == S_OK);
1230 FreeMediaType(&filter->pInputPin.pin.mtCurrent);
1231 if (filter->pInputPin.pAlloc)
1232 IMemAllocator_Release(filter->pInputPin.pAlloc);
1233 filter->pInputPin.pAlloc = NULL;
1234 if (filter->pInputPin.pReader)
1235 IAsyncReader_Release(filter->pInputPin.pReader);
1236 filter->pInputPin.pReader = NULL;
1237 filter->pInputPin.pin.IPin_iface.lpVtbl = NULL;
1239 if (filter->bus)
1241 gst_bus_set_sync_handler(filter->bus, NULL, NULL, NULL);
1242 gst_object_unref(filter->bus);
1244 strmbase_filter_cleanup(&filter->filter);
1245 CoTaskMemFree(filter);
1248 static const BaseFilterFuncTable BaseFuncTable = {
1249 .filter_get_pin = gstdemux_get_pin,
1250 .filter_destroy = gstdemux_destroy,
1253 IUnknown * CALLBACK Gstreamer_Splitter_create(IUnknown *outer, HRESULT *phr)
1255 PIN_INFO *piInput;
1256 GSTImpl *This;
1258 if (!init_gstreamer())
1260 *phr = E_FAIL;
1261 return NULL;
1264 mark_wine_thread();
1266 This = CoTaskMemAlloc(sizeof(*This));
1267 if (!This)
1269 *phr = E_OUTOFMEMORY;
1270 return NULL;
1272 memset(This, 0, sizeof(*This));
1274 strmbase_filter_init(&This->filter, &GST_Vtbl, outer, &CLSID_Gstreamer_Splitter,
1275 (DWORD_PTR)(__FILE__ ": GSTImpl.csFilter"), &BaseFuncTable);
1277 This->cStreams = 0;
1278 This->ppPins = NULL;
1279 This->push_thread = NULL;
1280 This->no_more_pads_event = CreateEventW(NULL, 0, 0, NULL);
1281 This->push_event = CreateEventW(NULL, 0, 0, NULL);
1282 This->bus = NULL;
1284 piInput = &This->pInputPin.pin.pinInfo;
1285 piInput->dir = PINDIR_INPUT;
1286 piInput->pFilter = &This->filter.IBaseFilter_iface;
1287 lstrcpynW(piInput->achName, wcsInputPinName, ARRAY_SIZE(piInput->achName));
1288 This->pInputPin.pin.IPin_iface.lpVtbl = &GST_InputPin_Vtbl;
1289 This->pInputPin.pin.pConnectedTo = NULL;
1290 This->pInputPin.pin.pCritSec = &This->filter.csFilter;
1291 ZeroMemory(&This->pInputPin.pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1292 *phr = S_OK;
1294 TRACE("Created GStreamer demuxer %p.\n", This);
1295 return &This->filter.IUnknown_inner;
1298 static HRESULT WINAPI GST_Stop(IBaseFilter *iface)
1300 GSTImpl *This = impl_from_IBaseFilter(iface);
1302 TRACE("(%p)\n", This);
1304 mark_wine_thread();
1306 if (This->container) {
1307 This->ignore_flush = TRUE;
1308 gst_element_set_state(This->container, GST_STATE_READY);
1309 gst_element_get_state(This->container, NULL, NULL, -1);
1310 This->ignore_flush = FALSE;
1312 return S_OK;
1315 static HRESULT WINAPI GST_Pause(IBaseFilter *iface)
1317 GSTImpl *This = impl_from_IBaseFilter(iface);
1318 HRESULT hr = S_OK;
1319 GstState now;
1320 GstStateChangeReturn ret;
1322 TRACE("(%p)\n", This);
1324 if (!This->container)
1325 return VFW_E_NOT_CONNECTED;
1327 mark_wine_thread();
1329 gst_element_get_state(This->container, &now, NULL, -1);
1330 if (now == GST_STATE_PAUSED)
1331 return S_OK;
1332 if (now != GST_STATE_PLAYING)
1333 hr = IBaseFilter_Run(iface, -1);
1334 if (FAILED(hr))
1335 return hr;
1336 ret = gst_element_set_state(This->container, GST_STATE_PAUSED);
1337 if (ret == GST_STATE_CHANGE_ASYNC)
1338 hr = S_FALSE;
1339 return hr;
1342 static HRESULT WINAPI GST_Run(IBaseFilter *iface, REFERENCE_TIME tStart)
1344 GSTImpl *This = impl_from_IBaseFilter(iface);
1345 HRESULT hr = S_OK;
1346 ULONG i;
1347 GstState now;
1348 HRESULT hr_any = VFW_E_NOT_CONNECTED;
1350 TRACE("(%p)->(%s)\n", This, wine_dbgstr_longlong(tStart));
1352 mark_wine_thread();
1354 if (!This->container)
1355 return VFW_E_NOT_CONNECTED;
1357 EnterCriticalSection(&This->filter.csFilter);
1358 This->filter.rtStreamStart = tStart;
1359 LeaveCriticalSection(&This->filter.csFilter);
1361 gst_element_get_state(This->container, &now, NULL, -1);
1362 if (now == GST_STATE_PLAYING)
1363 return S_OK;
1364 if (now == GST_STATE_PAUSED) {
1365 GstStateChangeReturn ret;
1366 ret = gst_element_set_state(This->container, GST_STATE_PLAYING);
1367 if (ret == GST_STATE_CHANGE_ASYNC)
1368 return S_FALSE;
1369 return S_OK;
1372 EnterCriticalSection(&This->filter.csFilter);
1373 gst_element_set_state(This->container, GST_STATE_PLAYING);
1374 This->filter.rtStreamStart = tStart;
1376 for (i = 0; i < This->cStreams; i++) {
1377 hr = BaseOutputPinImpl_Active(&This->ppPins[i]->pin);
1378 if (SUCCEEDED(hr)) {
1379 hr_any = hr;
1382 hr = hr_any;
1383 LeaveCriticalSection(&This->filter.csFilter);
1385 return hr;
1388 static HRESULT WINAPI GST_GetState(IBaseFilter *iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
1390 GSTImpl *This = impl_from_IBaseFilter(iface);
1391 HRESULT hr = S_OK;
1392 GstState now, pending;
1393 GstStateChangeReturn ret;
1395 TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
1397 mark_wine_thread();
1399 if (!This->container) {
1400 *pState = State_Stopped;
1401 return S_OK;
1404 ret = gst_element_get_state(This->container, &now, &pending, dwMilliSecsTimeout == INFINITE ? -1 : dwMilliSecsTimeout * 1000);
1406 if (ret == GST_STATE_CHANGE_ASYNC)
1407 hr = VFW_S_STATE_INTERMEDIATE;
1408 else
1409 pending = now;
1411 switch (pending) {
1412 case GST_STATE_PAUSED: *pState = State_Paused; return hr;
1413 case GST_STATE_PLAYING: *pState = State_Running; return hr;
1414 default: *pState = State_Stopped; return hr;
1418 static const IBaseFilterVtbl GST_Vtbl = {
1419 BaseFilterImpl_QueryInterface,
1420 BaseFilterImpl_AddRef,
1421 BaseFilterImpl_Release,
1422 BaseFilterImpl_GetClassID,
1423 GST_Stop,
1424 GST_Pause,
1425 GST_Run,
1426 GST_GetState,
1427 BaseFilterImpl_SetSyncSource,
1428 BaseFilterImpl_GetSyncSource,
1429 BaseFilterImpl_EnumPins,
1430 BaseFilterImpl_FindPin,
1431 BaseFilterImpl_QueryFilterInfo,
1432 BaseFilterImpl_JoinFilterGraph,
1433 BaseFilterImpl_QueryVendorInfo
1436 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1438 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1439 TRACE("(%p)\n", This);
1440 return S_OK;
1443 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1445 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1446 TRACE("(%p)\n", This);
1447 return S_OK;
1450 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1452 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1453 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1454 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1455 mark_wine_thread();
1456 gst_pad_push_event(This->my_sink, ev);
1457 return S_OK;
1460 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1462 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1463 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1466 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1468 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1469 return IPin_AddRef(&This->pin.pin.IPin_iface);
1472 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1474 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1475 return IPin_Release(&This->pin.pin.IPin_iface);
1478 static HRESULT WINAPI GST_Seeking_GetCurrentPosition(IMediaSeeking *iface, REFERENCE_TIME *pos)
1480 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1482 TRACE("(%p)->(%p)\n", This, pos);
1484 if (!pos)
1485 return E_POINTER;
1487 mark_wine_thread();
1489 if (!This->their_src) {
1490 *pos = This->seek.llCurrent;
1491 TRACE("Cached value\n");
1492 if (This->seek.llDuration)
1493 return S_OK;
1494 else
1495 return E_NOTIMPL;
1498 if (!gst_pad_query_position(This->their_src, GST_FORMAT_TIME, pos)) {
1499 WARN("Could not query position\n");
1500 return E_NOTIMPL;
1502 *pos /= 100;
1503 This->seek.llCurrent = *pos;
1504 return S_OK;
1507 static GstSeekType type_from_flags(DWORD flags)
1509 switch (flags & AM_SEEKING_PositioningBitsMask) {
1510 case AM_SEEKING_NoPositioning:
1511 return GST_SEEK_TYPE_NONE;
1512 case AM_SEEKING_AbsolutePositioning:
1513 case AM_SEEKING_RelativePositioning:
1514 return GST_SEEK_TYPE_SET;
1515 case AM_SEEKING_IncrementalPositioning:
1516 return GST_SEEK_TYPE_END;
1518 return GST_SEEK_TYPE_NONE;
1521 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1522 REFERENCE_TIME *pCur, DWORD curflags, REFERENCE_TIME *pStop,
1523 DWORD stopflags)
1525 HRESULT hr;
1526 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1527 GstSeekFlags f = 0;
1528 GstSeekType curtype, stoptype;
1529 GstEvent *e;
1530 gint64 stop_pos = 0, curr_pos = 0;
1532 TRACE("(%p)->(%p, 0x%x, %p, 0x%x)\n", This, pCur, curflags, pStop, stopflags);
1534 mark_wine_thread();
1536 if (!This->seek.llDuration)
1537 return E_NOTIMPL;
1539 hr = SourceSeekingImpl_SetPositions(iface, pCur, curflags, pStop, stopflags);
1540 if (!This->their_src)
1541 return hr;
1543 curtype = type_from_flags(curflags);
1544 stoptype = type_from_flags(stopflags);
1545 if (curflags & AM_SEEKING_SeekToKeyFrame)
1546 f |= GST_SEEK_FLAG_KEY_UNIT;
1547 if (curflags & AM_SEEKING_Segment)
1548 f |= GST_SEEK_FLAG_SEGMENT;
1549 if (!(curflags & AM_SEEKING_NoFlush))
1550 f |= GST_SEEK_FLAG_FLUSH;
1552 if (((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning) ||
1553 ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)) {
1554 gint64 tmp_pos;
1555 gst_pad_query_position (This->my_sink, GST_FORMAT_TIME, &tmp_pos);
1556 if ((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1557 curr_pos = tmp_pos;
1558 if ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1559 stop_pos = tmp_pos;
1562 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);
1563 if (gst_pad_push_event(This->my_sink, e))
1564 return S_OK;
1565 else
1566 return E_NOTIMPL;
1569 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1571 GST_Seeking_QueryInterface,
1572 GST_Seeking_AddRef,
1573 GST_Seeking_Release,
1574 SourceSeekingImpl_GetCapabilities,
1575 SourceSeekingImpl_CheckCapabilities,
1576 SourceSeekingImpl_IsFormatSupported,
1577 SourceSeekingImpl_QueryPreferredFormat,
1578 SourceSeekingImpl_GetTimeFormat,
1579 SourceSeekingImpl_IsUsingTimeFormat,
1580 SourceSeekingImpl_SetTimeFormat,
1581 SourceSeekingImpl_GetDuration,
1582 SourceSeekingImpl_GetStopPosition,
1583 GST_Seeking_GetCurrentPosition,
1584 SourceSeekingImpl_ConvertTimeFormat,
1585 GST_Seeking_SetPositions,
1586 SourceSeekingImpl_GetPositions,
1587 SourceSeekingImpl_GetAvailable,
1588 SourceSeekingImpl_SetRate,
1589 SourceSeekingImpl_GetRate,
1590 SourceSeekingImpl_GetPreroll
1593 static inline GSTOutPin *impl_from_IQualityControl( IQualityControl *iface )
1595 return CONTAINING_RECORD(iface, GSTOutPin, IQualityControl_iface);
1598 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1600 GSTOutPin *pin = impl_from_IQualityControl(iface);
1601 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1604 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1606 GSTOutPin *pin = impl_from_IQualityControl(iface);
1607 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1610 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1612 GSTOutPin *pin = impl_from_IQualityControl(iface);
1613 return IPin_Release(&pin->pin.pin.IPin_iface);
1616 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
1618 GSTOutPin *pin = impl_from_IQualityControl(iface);
1619 GstEvent *evt;
1621 TRACE("(%p)->(%p, { 0x%x %u %s %s })\n", pin, sender,
1622 qm.Type, qm.Proportion,
1623 wine_dbgstr_longlong(qm.Late),
1624 wine_dbgstr_longlong(qm.TimeStamp));
1626 mark_wine_thread();
1628 if (qm.Type == Flood)
1629 qm.Late = 0;
1631 evt = gst_event_new_qos(qm.Type == Famine ? GST_QOS_TYPE_UNDERFLOW : GST_QOS_TYPE_OVERFLOW,
1632 qm.Proportion / 1000., qm.Late * 100, qm.TimeStamp * 100);
1634 if (!evt) {
1635 WARN("Failed to create QOS event\n");
1636 return E_INVALIDARG;
1639 gst_pad_push_event(pin->my_sink, evt);
1641 return S_OK;
1644 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1646 GSTOutPin *pin = impl_from_IQualityControl(iface);
1647 TRACE("(%p)->(%p)\n", pin, pin);
1648 /* Do nothing */
1649 return S_OK;
1652 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1653 GST_QualityControl_QueryInterface,
1654 GST_QualityControl_AddRef,
1655 GST_QualityControl_Release,
1656 GST_QualityControl_Notify,
1657 GST_QualityControl_SetSink
1660 static inline GSTOutPin *impl_source_from_IPin(IPin *iface)
1662 return CONTAINING_RECORD(iface, GSTOutPin, pin.pin.IPin_iface);
1665 static HRESULT WINAPI GSTOutPin_QueryInterface(IPin *iface, REFIID riid, void **ppv)
1667 GSTOutPin *This = impl_source_from_IPin(iface);
1669 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1671 *ppv = NULL;
1673 if (IsEqualIID(riid, &IID_IUnknown))
1674 *ppv = iface;
1675 else if (IsEqualIID(riid, &IID_IPin))
1676 *ppv = iface;
1677 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1678 *ppv = &This->seek;
1679 else if (IsEqualIID(riid, &IID_IQualityControl))
1680 *ppv = &This->IQualityControl_iface;
1682 if (*ppv) {
1683 IUnknown_AddRef((IUnknown *)(*ppv));
1684 return S_OK;
1686 FIXME("No interface for %s!\n", debugstr_guid(riid));
1687 return E_NOINTERFACE;
1690 static HRESULT WINAPI GSTOutPin_CheckMediaType(BasePin *base, const AM_MEDIA_TYPE *amt)
1692 FIXME("(%p) stub\n", base);
1693 return S_OK;
1696 static HRESULT WINAPI GSTOutPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
1698 GSTOutPin *This = impl_source_from_IPin(&iface->IPin_iface);
1700 TRACE("(%p)->(%i, %p)\n", This, iPosition, pmt);
1702 if (iPosition < 0)
1703 return E_INVALIDARG;
1705 if (iPosition > 0)
1706 return VFW_S_NO_MORE_ITEMS;
1708 CopyMediaType(pmt, This->pmt);
1710 return S_OK;
1713 static HRESULT WINAPI GSTOutPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
1715 GSTOutPin *This = impl_source_from_IPin(&iface->pin.IPin_iface);
1716 TRACE("(%p)->(%p, %p)\n", This, pAlloc, ppropInputRequest);
1717 /* Unused */
1718 return S_OK;
1721 static HRESULT WINAPI GSTOutPin_DecideAllocator(BaseOutputPin *base, IMemInputPin *pPin, IMemAllocator **pAlloc)
1723 GSTOutPin *pin = impl_source_from_IPin(&base->pin.IPin_iface);
1724 GSTImpl *GSTfilter = impl_from_IBaseFilter(pin->pin.pin.pinInfo.pFilter);
1725 HRESULT hr;
1727 TRACE("pin %p, peer %p, allocator %p.\n", pin, pPin, pAlloc);
1729 *pAlloc = NULL;
1730 if (GSTfilter->pInputPin.pAlloc)
1732 hr = IMemInputPin_NotifyAllocator(pPin, GSTfilter->pInputPin.pAlloc, FALSE);
1733 if (SUCCEEDED(hr))
1735 *pAlloc = GSTfilter->pInputPin.pAlloc;
1736 IMemAllocator_AddRef(*pAlloc);
1739 else
1740 hr = VFW_E_NO_ALLOCATOR;
1742 return hr;
1745 static void free_source_pin(GSTOutPin *pin)
1747 EnterCriticalSection(pin->pin.pin.pCritSec);
1748 if (pin->pin.pin.pConnectedTo)
1750 if (SUCCEEDED(IMemAllocator_Decommit(pin->pin.pAllocator)))
1751 IPin_Disconnect(pin->pin.pin.pConnectedTo);
1752 IPin_Disconnect(&pin->pin.pin.IPin_iface);
1754 LeaveCriticalSection(pin->pin.pin.pCritSec);
1756 if (pin->their_src)
1758 if (pin->flipfilter)
1760 gst_pad_unlink(pin->their_src, pin->flip_sink);
1761 gst_pad_unlink(pin->flip_src, pin->my_sink);
1762 gst_object_unref(pin->flip_src);
1763 gst_object_unref(pin->flip_sink);
1764 pin->flipfilter = NULL;
1765 pin->flip_src = pin->flip_sink = NULL;
1767 else
1768 gst_pad_unlink(pin->their_src, pin->my_sink);
1769 gst_object_unref(pin->their_src);
1771 gst_object_unref(pin->my_sink);
1772 CloseHandle(pin->caps_event);
1773 DeleteMediaType(pin->pmt);
1774 FreeMediaType(&pin->pin.pin.mtCurrent);
1775 gst_segment_free(pin->segment);
1776 if (pin->gstpool)
1777 gst_object_unref(pin->gstpool);
1778 if (pin->pin.pAllocator)
1779 IMemAllocator_Release(pin->pin.pAllocator);
1780 CoTaskMemFree(pin);
1783 static const IPinVtbl GST_OutputPin_Vtbl = {
1784 GSTOutPin_QueryInterface,
1785 BasePinImpl_AddRef,
1786 BasePinImpl_Release,
1787 BaseOutputPinImpl_Connect,
1788 BaseOutputPinImpl_ReceiveConnection,
1789 BaseOutputPinImpl_Disconnect,
1790 BasePinImpl_ConnectedTo,
1791 BasePinImpl_ConnectionMediaType,
1792 BasePinImpl_QueryPinInfo,
1793 BasePinImpl_QueryDirection,
1794 BasePinImpl_QueryId,
1795 BasePinImpl_QueryAccept,
1796 BasePinImpl_EnumMediaTypes,
1797 BasePinImpl_QueryInternalConnections,
1798 BaseOutputPinImpl_EndOfStream,
1799 BaseOutputPinImpl_BeginFlush,
1800 BaseOutputPinImpl_EndFlush,
1801 BasePinImpl_NewSegment
1804 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
1806 GSTOutPin_CheckMediaType,
1807 GSTOutPin_GetMediaType
1809 BaseOutputPinImpl_AttemptConnection,
1810 GSTOutPin_DecideBufferSize,
1811 GSTOutPin_DecideAllocator,
1814 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt)
1816 HRESULT hr;
1817 This->ppPins = CoTaskMemRealloc(This->ppPins, (This->cStreams + 1) * sizeof(IPin *));
1819 hr = BaseOutputPin_Construct(&GST_OutputPin_Vtbl, sizeof(GSTOutPin), piOutput, &output_BaseOutputFuncTable, &This->filter.csFilter, (IPin**)(This->ppPins + This->cStreams));
1820 if (SUCCEEDED(hr)) {
1821 GSTOutPin *pin = This->ppPins[This->cStreams];
1822 memset((char*)pin + sizeof(pin->pin), 0, sizeof(GSTOutPin) - sizeof(pin->pin));
1823 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1824 CopyMediaType(pin->pmt, amt);
1825 pin->pin.pin.pinInfo.pFilter = &This->filter.IBaseFilter_iface;
1826 pin->caps_event = CreateEventW(NULL, 0, 0, NULL);
1827 pin->segment = gst_segment_new();
1828 This->cStreams++;
1829 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1830 SourceSeeking_Init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop, GST_ChangeCurrent, GST_ChangeRate, &This->filter.csFilter);
1831 BaseFilterImpl_IncrementPinVersion(&This->filter);
1832 } else
1833 ERR("Failed with error %x\n", hr);
1834 return hr;
1837 static HRESULT GST_RemoveOutputPins(GSTImpl *This)
1839 ULONG i;
1841 TRACE("(%p)\n", This);
1842 mark_wine_thread();
1844 if (!This->container)
1845 return S_OK;
1846 gst_element_set_state(This->container, GST_STATE_NULL);
1847 gst_pad_unlink(This->my_src, This->their_sink);
1848 gst_object_unref(This->my_src);
1849 gst_object_unref(This->their_sink);
1850 This->my_src = This->their_sink = NULL;
1852 for (i = 0; i < This->cStreams; ++i)
1853 free_source_pin(This->ppPins[i]);
1855 This->cStreams = 0;
1856 CoTaskMemFree(This->ppPins);
1857 This->ppPins = NULL;
1858 gst_element_set_bus(This->container, NULL);
1859 gst_object_unref(This->container);
1860 This->container = NULL;
1861 BaseFilterImpl_IncrementPinVersion(&This->filter);
1862 return S_OK;
1865 static inline GSTInPin *impl_sink_from_IPin(IPin *iface)
1867 return CONTAINING_RECORD(iface, GSTInPin, pin.IPin_iface);
1870 static HRESULT WINAPI GSTInPin_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
1872 GSTInPin *This = impl_sink_from_IPin(iface);
1873 PIN_DIRECTION pindirReceive;
1874 HRESULT hr = S_OK;
1876 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1877 dump_AM_MEDIA_TYPE(pmt);
1879 mark_wine_thread();
1881 EnterCriticalSection(This->pin.pCritSec);
1882 if (!This->pin.pConnectedTo) {
1883 ALLOCATOR_PROPERTIES props;
1884 IMemAllocator *pAlloc = NULL;
1886 props.cBuffers = 8;
1887 props.cbBuffer = 16384;
1888 props.cbAlign = 1;
1889 props.cbPrefix = 0;
1891 if (IPin_QueryAccept(iface, pmt) != S_OK)
1892 hr = VFW_E_TYPE_NOT_ACCEPTED;
1894 if (SUCCEEDED(hr)) {
1895 IPin_QueryDirection(pReceivePin, &pindirReceive);
1896 if (pindirReceive != PINDIR_OUTPUT) {
1897 ERR("Can't connect from non-output pin\n");
1898 hr = VFW_E_INVALID_DIRECTION;
1902 This->pReader = NULL;
1903 This->pAlloc = NULL;
1904 ResetEvent(impl_from_IBaseFilter(This->pin.pinInfo.pFilter)->push_event);
1905 if (SUCCEEDED(hr))
1906 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1907 if (SUCCEEDED(hr))
1908 hr = GST_Connect(This, pReceivePin, &props);
1910 /* A certain IAsyncReader::RequestAllocator expects to be passed
1911 non-NULL preferred allocator */
1912 if (SUCCEEDED(hr))
1913 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC,
1914 &IID_IMemAllocator, (LPVOID *)&pAlloc);
1915 if (SUCCEEDED(hr)) {
1916 hr = IAsyncReader_RequestAllocator(This->pReader, pAlloc, &props, &This->pAlloc);
1917 if (FAILED(hr))
1918 WARN("Can't get an allocator, got %08x\n", hr);
1920 if (pAlloc)
1921 IMemAllocator_Release(pAlloc);
1922 if (SUCCEEDED(hr)) {
1923 CopyMediaType(&This->pin.mtCurrent, pmt);
1924 This->pin.pConnectedTo = pReceivePin;
1925 IPin_AddRef(pReceivePin);
1926 hr = IMemAllocator_Commit(This->pAlloc);
1927 SetEvent(impl_from_IBaseFilter(This->pin.pinInfo.pFilter)->push_event);
1928 } else {
1929 GST_RemoveOutputPins(impl_from_IBaseFilter(This->pin.pinInfo.pFilter));
1930 if (This->pReader)
1931 IAsyncReader_Release(This->pReader);
1932 This->pReader = NULL;
1933 if (This->pAlloc)
1934 IMemAllocator_Release(This->pAlloc);
1935 This->pAlloc = NULL;
1937 TRACE("Size: %i\n", props.cbBuffer);
1938 } else
1939 hr = VFW_E_ALREADY_CONNECTED;
1940 LeaveCriticalSection(This->pin.pCritSec);
1941 return hr;
1944 static HRESULT WINAPI GSTInPin_Disconnect(IPin *iface)
1946 GSTInPin *This = impl_sink_from_IPin(iface);
1947 HRESULT hr;
1948 FILTER_STATE state;
1950 TRACE("(%p)\n", This);
1952 mark_wine_thread();
1954 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1955 EnterCriticalSection(This->pin.pCritSec);
1956 if (This->pin.pConnectedTo) {
1957 GSTImpl *Parser = impl_from_IBaseFilter(This->pin.pinInfo.pFilter);
1959 if (SUCCEEDED(hr) && state == State_Stopped) {
1960 IMemAllocator_Decommit(This->pAlloc);
1961 IPin_Disconnect(This->pin.pConnectedTo);
1962 IPin_Release(This->pin.pConnectedTo);
1963 This->pin.pConnectedTo = NULL;
1964 hr = GST_RemoveOutputPins(Parser);
1965 } else
1966 hr = VFW_E_NOT_STOPPED;
1967 } else
1968 hr = S_FALSE;
1969 LeaveCriticalSection(This->pin.pCritSec);
1970 return hr;
1973 static HRESULT WINAPI GSTInPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
1975 GSTInPin *This = impl_sink_from_IPin(iface);
1977 TRACE("(%p)->(%p)\n", This, pmt);
1978 dump_AM_MEDIA_TYPE(pmt);
1980 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
1981 return S_OK;
1982 return S_FALSE;
1985 static HRESULT WINAPI GSTInPin_EndOfStream(IPin *iface)
1987 FIXME("iface %p, stub!\n", iface);
1988 return S_OK;
1991 static HRESULT WINAPI GSTInPin_BeginFlush(IPin *iface)
1993 FIXME("iface %p, stub!\n", iface);
1994 return S_OK;
1997 static HRESULT WINAPI GSTInPin_EndFlush(IPin *iface)
1999 FIXME("iface %p, stub!\n", iface);
2000 return S_OK;
2003 static HRESULT WINAPI GSTInPin_NewSegment(IPin *iface, REFERENCE_TIME start,
2004 REFERENCE_TIME stop, double rate)
2006 FIXME("iface %p, start %s, stop %s, rate %.16e, stub!\n",
2007 iface, wine_dbgstr_longlong(start), wine_dbgstr_longlong(stop), rate);
2009 BasePinImpl_NewSegment(iface, start, stop, rate);
2010 return S_OK;
2013 static HRESULT WINAPI GSTInPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
2015 GSTInPin *This = impl_sink_from_IPin(iface);
2017 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
2019 *ppv = NULL;
2021 if (IsEqualIID(riid, &IID_IUnknown))
2022 *ppv = iface;
2023 else if (IsEqualIID(riid, &IID_IPin))
2024 *ppv = iface;
2025 else if (IsEqualIID(riid, &IID_IMediaSeeking))
2027 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
2030 if (*ppv)
2032 IUnknown_AddRef((IUnknown *)(*ppv));
2033 return S_OK;
2036 FIXME("No interface for %s!\n", debugstr_guid(riid));
2038 return E_NOINTERFACE;
2041 static HRESULT WINAPI GSTInPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
2043 BasePin *This = (BasePin *)iface;
2045 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
2047 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
2050 static const IPinVtbl GST_InputPin_Vtbl = {
2051 GSTInPin_QueryInterface,
2052 BasePinImpl_AddRef,
2053 BasePinImpl_Release,
2054 BaseInputPinImpl_Connect,
2055 GSTInPin_ReceiveConnection,
2056 GSTInPin_Disconnect,
2057 BasePinImpl_ConnectedTo,
2058 BasePinImpl_ConnectionMediaType,
2059 BasePinImpl_QueryPinInfo,
2060 BasePinImpl_QueryDirection,
2061 BasePinImpl_QueryId,
2062 GSTInPin_QueryAccept,
2063 GSTInPin_EnumMediaTypes,
2064 BasePinImpl_QueryInternalConnections,
2065 GSTInPin_EndOfStream,
2066 GSTInPin_BeginFlush,
2067 GSTInPin_EndFlush,
2068 GSTInPin_NewSegment
2071 pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
2072 pthread_cond_t cb_list_cond = PTHREAD_COND_INITIALIZER;
2073 struct list cb_list = LIST_INIT(cb_list);
2075 void CALLBACK perform_cb(TP_CALLBACK_INSTANCE *instance, void *user)
2077 struct cb_data *cbdata = user;
2079 TRACE("got cb type: 0x%x\n", cbdata->type);
2081 switch(cbdata->type)
2083 case WATCH_BUS:
2085 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2086 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2087 break;
2089 case EXISTING_NEW_PAD:
2091 struct existing_new_pad_data *data = &cbdata->u.existing_new_pad_data;
2092 existing_new_pad(data->bin, data->pad, data->user);
2093 break;
2095 case QUERY_FUNCTION:
2097 struct query_function_data *data = &cbdata->u.query_function_data;
2098 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2099 break;
2101 case ACTIVATE_MODE:
2103 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2104 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2105 break;
2107 case NO_MORE_PADS:
2109 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2110 no_more_pads(data->decodebin, data->user);
2111 break;
2113 case REQUEST_BUFFER_SRC:
2115 struct request_buffer_src_data *data = &cbdata->u.request_buffer_src_data;
2116 cbdata->u.request_buffer_src_data.ret = request_buffer_src(data->pad, data->parent,
2117 data->ofs, data->len, data->buf);
2118 break;
2120 case EVENT_SRC:
2122 struct event_src_data *data = &cbdata->u.event_src_data;
2123 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2124 break;
2126 case EVENT_SINK:
2128 struct event_sink_data *data = &cbdata->u.event_sink_data;
2129 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2130 break;
2132 case GOT_DATA_SINK:
2134 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2135 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2136 break;
2138 case GOT_DATA:
2140 struct got_data_data *data = &cbdata->u.got_data_data;
2141 cbdata->u.got_data_data.ret = got_data(data->pad, data->parent, data->buf);
2142 break;
2144 case REMOVED_DECODED_PAD:
2146 struct removed_decoded_pad_data *data = &cbdata->u.removed_decoded_pad_data;
2147 removed_decoded_pad(data->bin, data->pad, data->user);
2148 break;
2150 case AUTOPLUG_BLACKLIST:
2152 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2153 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2154 data->pad, data->caps, data->fact, data->user);
2155 break;
2157 case UNKNOWN_TYPE:
2159 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2160 unknown_type(data->bin, data->pad, data->caps, data->user);
2161 break;
2163 case RELEASE_SAMPLE:
2165 struct release_sample_data *data = &cbdata->u.release_sample_data;
2166 release_sample(data->data);
2167 break;
2169 case TRANSFORM_PAD_ADDED:
2171 struct transform_pad_added_data *data = &cbdata->u.transform_pad_added_data;
2172 Gstreamer_transform_pad_added(data->filter, data->pad, data->user);
2173 break;
2175 case QUERY_SINK:
2177 struct query_sink_data *data = &cbdata->u.query_sink_data;
2178 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2179 data->query);
2180 break;
2184 pthread_mutex_lock(&cbdata->lock);
2185 cbdata->finished = 1;
2186 pthread_cond_broadcast(&cbdata->cond);
2187 pthread_mutex_unlock(&cbdata->lock);
2190 static DWORD WINAPI dispatch_thread(void *user)
2192 struct cb_data *cbdata;
2194 pthread_mutex_lock(&cb_list_lock);
2196 while(1){
2197 pthread_cond_wait(&cb_list_cond, &cb_list_lock);
2199 while(!list_empty(&cb_list)){
2200 cbdata = LIST_ENTRY(list_head(&cb_list), struct cb_data, entry);
2201 list_remove(&cbdata->entry);
2203 TrySubmitThreadpoolCallback(&perform_cb, cbdata, NULL);
2207 pthread_mutex_unlock(&cb_list_lock);
2209 return 0;
2212 void start_dispatch_thread(void)
2214 pthread_key_create(&wine_gst_key, NULL);
2215 CloseHandle(CreateThread(NULL, 0, &dispatch_thread, NULL, 0, NULL));