winegstreamer: Remove unused callbacks.
[wine.git] / dlls / winegstreamer / gstdemux.c
blob3ce7873f10266b2b7bac9415ff8af20dafd81708
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 const char* media_quark_string = "media-sample";
95 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
96 static const IMediaSeekingVtbl GST_Seeking_Vtbl;
97 static const IPinVtbl GST_OutputPin_Vtbl;
98 static const IPinVtbl GST_InputPin_Vtbl;
99 static const IBaseFilterVtbl GST_Vtbl;
100 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl;
102 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt);
103 static HRESULT GST_RemoveOutputPins(GSTImpl *This);
104 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface);
105 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface);
106 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface);
108 void mark_wine_thread(void)
110 /* set it to non-NULL to indicate that this is a Wine thread */
111 pthread_setspecific(wine_gst_key, &wine_gst_key);
114 BOOL is_wine_thread(void)
116 return pthread_getspecific(wine_gst_key) != NULL;
119 static gboolean amt_from_gst_caps_audio(GstCaps *caps, AM_MEDIA_TYPE *amt)
121 WAVEFORMATEXTENSIBLE *wfe;
122 WAVEFORMATEX *wfx;
123 gint32 depth, bpp;
124 GstAudioInfo ainfo;
126 if (!gst_audio_info_from_caps (&ainfo, caps))
127 return FALSE;
129 wfe = CoTaskMemAlloc(sizeof(*wfe));
130 wfx = (WAVEFORMATEX*)wfe;
131 amt->majortype = MEDIATYPE_Audio;
132 amt->subtype = MEDIASUBTYPE_PCM;
133 amt->formattype = FORMAT_WaveFormatEx;
134 amt->pbFormat = (BYTE*)wfe;
135 amt->cbFormat = sizeof(*wfe);
136 amt->bFixedSizeSamples = 0;
137 amt->bTemporalCompression = 1;
138 amt->lSampleSize = 0;
139 amt->pUnk = NULL;
141 wfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
143 wfx->nChannels = ainfo.channels;
144 wfx->nSamplesPerSec = ainfo.rate;
145 depth = GST_AUDIO_INFO_WIDTH(&ainfo);
146 bpp = GST_AUDIO_INFO_DEPTH(&ainfo);
148 if (!depth || depth > 32 || depth % 8)
149 depth = bpp;
150 else if (!bpp)
151 bpp = depth;
152 wfe->Samples.wValidBitsPerSample = depth;
153 wfx->wBitsPerSample = bpp;
154 wfx->cbSize = sizeof(*wfe)-sizeof(*wfx);
155 switch (wfx->nChannels) {
156 case 1: wfe->dwChannelMask = KSAUDIO_SPEAKER_MONO; break;
157 case 2: wfe->dwChannelMask = KSAUDIO_SPEAKER_STEREO; break;
158 case 4: wfe->dwChannelMask = KSAUDIO_SPEAKER_SURROUND; break;
159 case 5: wfe->dwChannelMask = (KSAUDIO_SPEAKER_5POINT1 & ~SPEAKER_LOW_FREQUENCY); break;
160 case 6: wfe->dwChannelMask = KSAUDIO_SPEAKER_5POINT1; break;
161 case 8: wfe->dwChannelMask = KSAUDIO_SPEAKER_7POINT1; break;
162 default:
163 wfe->dwChannelMask = 0;
165 if (GST_AUDIO_INFO_IS_FLOAT(&ainfo)) {
166 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
167 wfx->wBitsPerSample = wfe->Samples.wValidBitsPerSample = 32;
168 } else {
169 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
170 if (wfx->nChannels <= 2 && bpp <= 16 && depth == bpp) {
171 wfx->wFormatTag = WAVE_FORMAT_PCM;
172 wfx->cbSize = 0;
175 wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample/8;
176 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
177 return TRUE;
180 static gboolean amt_from_gst_caps_video(GstCaps *caps, AM_MEDIA_TYPE *amt)
182 VIDEOINFOHEADER *vih;
183 BITMAPINFOHEADER *bih;
184 gint32 width, height, nom, denom;
185 GstVideoInfo vinfo;
187 if (!gst_video_info_from_caps (&vinfo, caps))
188 return FALSE;
189 width = vinfo.width;
190 height = vinfo.height;
191 nom = vinfo.fps_n;
192 denom = vinfo.fps_d;
194 vih = CoTaskMemAlloc(sizeof(*vih));
195 bih = &vih->bmiHeader;
197 amt->formattype = FORMAT_VideoInfo;
198 amt->pbFormat = (BYTE*)vih;
199 amt->cbFormat = sizeof(*vih);
200 amt->bFixedSizeSamples = amt->bTemporalCompression = 1;
201 amt->lSampleSize = 0;
202 amt->pUnk = NULL;
203 ZeroMemory(vih, sizeof(*vih));
204 amt->majortype = MEDIATYPE_Video;
205 if (GST_VIDEO_INFO_IS_RGB(&vinfo)) {
206 bih->biBitCount = GST_VIDEO_FORMAT_INFO_BITS(vinfo.finfo);
207 switch (bih->biBitCount) {
208 case 16: amt->subtype = MEDIASUBTYPE_RGB555; break;
209 case 24: amt->subtype = MEDIASUBTYPE_RGB24; break;
210 case 32: amt->subtype = MEDIASUBTYPE_RGB32; break;
211 default:
212 FIXME("Unknown bpp %u\n", bih->biBitCount);
213 CoTaskMemFree(vih);
214 return FALSE;
216 bih->biCompression = BI_RGB;
217 } else {
218 amt->subtype = MEDIATYPE_Video;
219 if (!(amt->subtype.Data1 = gst_video_format_to_fourcc(vinfo.finfo->format))) {
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 bih->biBitCount = 16; break;
233 bih->biCompression = amt->subtype.Data1;
235 bih->biSizeImage = width * height * bih->biBitCount / 8;
236 if ((vih->AvgTimePerFrame = (REFERENCE_TIME)MulDiv(10000000, denom, nom)) == -1)
237 vih->AvgTimePerFrame = 0; /* zero division or integer overflow */
238 vih->rcSource.left = 0;
239 vih->rcSource.right = width;
240 vih->rcSource.top = height;
241 vih->rcSource.bottom = 0;
242 vih->rcTarget = vih->rcSource;
243 bih->biSize = sizeof(*bih);
244 bih->biWidth = width;
245 bih->biHeight = height;
246 bih->biPlanes = 1;
247 return TRUE;
250 static gboolean accept_caps_sink(GstPad *pad, GstCaps *caps)
252 GSTOutPin *pin = gst_pad_get_element_private(pad);
253 AM_MEDIA_TYPE amt;
254 GstStructure *arg;
255 const char *typename;
256 gboolean ret;
258 TRACE("%p %p\n", pad, caps);
260 arg = gst_caps_get_structure(caps, 0);
261 typename = gst_structure_get_name(arg);
262 if (!strcmp(typename, "audio/x-raw")) {
263 if (!pin->isaud) {
264 ERR("Setting audio caps on non-audio pad?\n");
265 return FALSE;
267 ret = amt_from_gst_caps_audio(caps, &amt);
268 if (ret)
269 FreeMediaType(&amt);
270 TRACE("+%i\n", ret);
271 return ret;
272 } else if (!strcmp(typename, "video/x-raw")) {
273 if (!pin->isvid) {
274 ERR("Setting video caps on non-video pad?\n");
275 return FALSE;
277 ret = amt_from_gst_caps_video(caps, &amt);
278 if (ret)
279 FreeMediaType(&amt);
280 TRACE("-%i\n", ret);
281 return ret;
282 } else {
283 FIXME("Unhandled type \"%s\"\n", typename);
284 return FALSE;
288 static gboolean setcaps_sink(GstPad *pad, GstCaps *caps)
290 GSTOutPin *pin = gst_pad_get_element_private(pad);
291 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
292 AM_MEDIA_TYPE amt;
293 GstStructure *arg;
294 const char *typename;
295 gboolean ret;
297 TRACE("%p %p\n", pad, caps);
299 arg = gst_caps_get_structure(caps, 0);
300 typename = gst_structure_get_name(arg);
301 if (!strcmp(typename, "audio/x-raw")) {
302 if (!pin->isaud) {
303 ERR("Setting audio caps on non-audio pad?\n");
304 return FALSE;
306 ret = amt_from_gst_caps_audio(caps, &amt);
307 } else if (!strcmp(typename, "video/x-raw")) {
308 if (!pin->isvid) {
309 ERR("Setting video caps on non-video pad?\n");
310 return FALSE;
312 ret = amt_from_gst_caps_video(caps, &amt);
313 if (ret)
314 This->props.cbBuffer = max(This->props.cbBuffer, ((VIDEOINFOHEADER*)amt.pbFormat)->bmiHeader.biSizeImage);
315 } else {
316 FIXME("Unhandled type \"%s\"\n", typename);
317 return FALSE;
319 TRACE("Linking returned %i for %s\n", ret, typename);
320 if (!ret)
321 return FALSE;
322 FreeMediaType(pin->pmt);
323 *pin->pmt = amt;
324 SetEvent(pin->caps_event);
325 return TRUE;
328 static gboolean query_sink(GstPad *pad, GstObject *parent, GstQuery *query)
330 switch (GST_QUERY_TYPE (query)) {
331 case GST_QUERY_ACCEPT_CAPS:
333 GstCaps *caps;
334 gboolean res;
335 gst_query_parse_accept_caps(query, &caps);
336 res = accept_caps_sink(pad, caps);
337 gst_query_set_accept_caps_result(query, res);
338 return TRUE; /* FIXME */
340 default:
341 return gst_pad_query_default (pad, parent, query);
345 static gboolean gst_base_src_perform_seek(GSTImpl *This, GstEvent *event)
347 gboolean res = TRUE;
348 gdouble rate;
349 GstFormat seek_format;
350 GstSeekFlags flags;
351 GstSeekType cur_type, stop_type;
352 gint64 cur, stop;
353 gboolean flush;
354 guint32 seqnum;
355 GstEvent *tevent;
356 BOOL thread = !!This->push_thread;
358 TRACE("%p %p\n", This, event);
360 gst_event_parse_seek(event, &rate, &seek_format, &flags,
361 &cur_type, &cur, &stop_type, &stop);
363 if (seek_format != GST_FORMAT_BYTES) {
364 FIXME("Not handling other format %i\n", seek_format);
365 return FALSE;
368 flush = flags & GST_SEEK_FLAG_FLUSH;
369 seqnum = gst_event_get_seqnum(event);
371 /* send flush start */
372 if (flush) {
373 tevent = gst_event_new_flush_start();
374 gst_event_set_seqnum(tevent, seqnum);
375 gst_pad_push_event(This->my_src, tevent);
376 if (This->pInputPin.pReader)
377 IAsyncReader_BeginFlush(This->pInputPin.pReader);
378 if (thread)
379 gst_pad_set_active(This->my_src, 1);
382 This->nextofs = This->start = cur;
384 /* and prepare to continue streaming */
385 if (flush) {
386 tevent = gst_event_new_flush_stop(TRUE);
387 gst_event_set_seqnum(tevent, seqnum);
388 gst_pad_push_event(This->my_src, tevent);
389 if (This->pInputPin.pReader)
390 IAsyncReader_EndFlush(This->pInputPin.pReader);
391 if (thread)
392 gst_pad_set_active(This->my_src, 1);
395 return res;
398 static gboolean event_src(GstPad *pad, GstObject *parent, GstEvent *event)
400 GSTImpl *This = gst_pad_get_element_private(pad);
402 TRACE("%p %p\n", pad, event);
404 switch (event->type) {
405 case GST_EVENT_SEEK:
406 return gst_base_src_perform_seek(This, event);
407 case GST_EVENT_FLUSH_START:
408 EnterCriticalSection(&This->filter.csFilter);
409 if (This->pInputPin.pReader)
410 IAsyncReader_BeginFlush(This->pInputPin.pReader);
411 LeaveCriticalSection(&This->filter.csFilter);
412 break;
413 case GST_EVENT_FLUSH_STOP:
414 EnterCriticalSection(&This->filter.csFilter);
415 if (This->pInputPin.pReader)
416 IAsyncReader_EndFlush(This->pInputPin.pReader);
417 LeaveCriticalSection(&This->filter.csFilter);
418 break;
419 default:
420 FIXME("%p (%u) stub\n", event, event->type);
421 case GST_EVENT_TAG:
422 case GST_EVENT_QOS:
423 return gst_pad_event_default(pad, parent, event);
425 return TRUE;
428 static gboolean event_sink(GstPad *pad, GstObject *parent, GstEvent *event)
430 GSTOutPin *pin = gst_pad_get_element_private(pad);
432 TRACE("%p %p\n", pad, event);
434 switch (event->type) {
435 case GST_EVENT_SEGMENT: {
436 gdouble rate, applied_rate;
437 gint64 stop, pos;
438 const GstSegment *segment;
440 gst_event_parse_segment(event, &segment);
442 pos = segment->position;
443 stop = segment->stop;
444 rate = segment->rate;
445 applied_rate = segment->applied_rate;
447 if (segment->format != GST_FORMAT_TIME) {
448 FIXME("Ignoring new segment because of format %i\n", segment->format);
449 return TRUE;
452 gst_segment_copy_into(segment, pin->segment);
454 pos /= 100;
456 if (stop > 0)
457 stop /= 100;
459 if (pin->pin.pin.pConnectedTo)
460 IPin_NewSegment(pin->pin.pin.pConnectedTo, pos, stop, rate*applied_rate);
462 return TRUE;
464 case GST_EVENT_EOS:
465 if (pin->pin.pin.pConnectedTo)
466 IPin_EndOfStream(pin->pin.pin.pConnectedTo);
467 return TRUE;
468 case GST_EVENT_FLUSH_START:
469 if (((GSTImpl *)pin->pin.pin.pinInfo.pFilter)->ignore_flush) {
470 /* gst-plugins-base prior to 1.7 contains a bug which causes
471 * our sink pins to receive a flush-start event when the
472 * decodebin changes from PAUSED to READY (including
473 * PLAYING->PAUSED->READY), but no matching flush-stop event is
474 * sent. See <gst-plugins-base.git:60bad4815db966a8e4). Here we
475 * unset the flushing flag to avoid the problem. */
476 TRACE("Working around gst <1.7 bug, ignoring FLUSH_START\n");
477 GST_PAD_UNSET_FLUSHING (pad);
478 return TRUE;
480 if (pin->pin.pin.pConnectedTo)
481 IPin_BeginFlush(pin->pin.pin.pConnectedTo);
482 return TRUE;
483 case GST_EVENT_FLUSH_STOP:
484 gst_segment_init(pin->segment, GST_FORMAT_TIME);
485 if (pin->pin.pin.pConnectedTo)
486 IPin_EndFlush(pin->pin.pin.pConnectedTo);
487 return TRUE;
488 case GST_EVENT_CAPS: {
489 GstCaps *caps;
490 gst_event_parse_caps(event, &caps);
491 return setcaps_sink(pad, caps);
493 default:
494 TRACE("%p stub %s\n", event, gst_event_type_get_name(event->type));
495 return gst_pad_event_default(pad, parent, event);
499 static void release_sample(void *data)
501 ULONG ret;
502 ret = IMediaSample_Release((IMediaSample *)data);
503 TRACE("Releasing %p returns %u\n", data, ret);
506 static DWORD CALLBACK push_data(LPVOID iface)
508 LONGLONG maxlen, curlen;
509 GSTImpl *This = iface;
510 IMediaSample *buf;
511 DWORD_PTR user;
512 HRESULT hr;
514 IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
516 if (!This->stop)
517 IAsyncReader_Length(This->pInputPin.pReader, &maxlen, &curlen);
518 else
519 maxlen = This->stop;
521 TRACE("Waiting..\n");
523 WaitForSingleObject(This->push_event, INFINITE);
525 TRACE("Starting..\n");
526 for (;;) {
527 REFERENCE_TIME tStart, tStop;
528 ULONG len;
529 GstBuffer *gstbuf;
530 gsize bufsize;
531 BYTE *data;
532 int ret;
534 TRACE("pAlloc: %p\n", This->pInputPin.pAlloc);
535 hr = IMemAllocator_GetBuffer(This->pInputPin.pAlloc, &buf, NULL, NULL, 0);
536 if (FAILED(hr))
537 break;
539 if (This->nextofs >= maxlen)
540 break;
541 len = IMediaSample_GetSize(buf);
542 if (This->nextofs + len > maxlen)
543 len = maxlen - This->nextofs;
545 tStart = MEDIATIME_FROM_BYTES(This->nextofs);
546 tStop = tStart + MEDIATIME_FROM_BYTES(len);
547 IMediaSample_SetTime(buf, &tStart, &tStop);
549 hr = IAsyncReader_Request(This->pInputPin.pReader, buf, 0);
550 if (FAILED(hr)) {
551 IMediaSample_Release(buf);
552 break;
554 This->nextofs += len;
555 hr = IAsyncReader_WaitForNext(This->pInputPin.pReader, -1, &buf, &user);
556 if (FAILED(hr) || !buf) {
557 if (buf)
558 IMediaSample_Release(buf);
559 break;
562 IMediaSample_GetPointer(buf, &data);
563 bufsize = IMediaSample_GetActualDataLength(buf);
564 gstbuf = gst_buffer_new_wrapped_full(0, data, bufsize, 0, bufsize, buf, release_sample_wrapper);
565 IMediaSample_AddRef(buf);
566 gst_mini_object_set_qdata(GST_MINI_OBJECT(gstbuf), g_quark_from_static_string(media_quark_string), buf, release_sample_wrapper);
567 if (!gstbuf) {
568 IMediaSample_Release(buf);
569 break;
571 gstbuf->duration = gstbuf->pts = -1;
572 ret = gst_pad_push(This->my_src, gstbuf);
573 if (ret >= 0)
574 hr = S_OK;
575 else
576 ERR("Sending returned: %i\n", ret);
577 if (ret == GST_FLOW_ERROR)
578 hr = E_FAIL;
579 else if (ret == GST_FLOW_FLUSHING)
580 hr = VFW_E_WRONG_STATE;
581 if (hr != S_OK)
582 break;
585 gst_pad_push_event(This->my_src, gst_event_new_eos());
587 TRACE("Almost stopping.. %08x\n", hr);
588 do {
589 IAsyncReader_WaitForNext(This->pInputPin.pReader, 0, &buf, &user);
590 if (buf)
591 IMediaSample_Release(buf);
592 } while (buf);
594 TRACE("Stopping.. %08x\n", hr);
596 IBaseFilter_Release(&This->filter.IBaseFilter_iface);
598 return 0;
601 static HRESULT WINAPI GST_OutPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
603 GSTOutPin *pin = (GSTOutPin*)iface;
604 FIXME("stub %p\n", pin);
605 return S_OK;
608 static GstFlowReturn got_data_sink(GstPad *pad, GstObject *parent, GstBuffer *buf)
610 GSTOutPin *pin = gst_pad_get_element_private(pad);
611 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
612 HRESULT hr;
613 BYTE *ptr = NULL;
614 IMediaSample *sample;
615 GstMapInfo info;
617 TRACE("%p %p\n", pad, buf);
619 if (This->initial) {
620 gst_buffer_unref(buf);
621 return GST_FLOW_OK;
624 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
626 if (hr == VFW_E_NOT_CONNECTED) {
627 gst_buffer_unref(buf);
628 return GST_FLOW_NOT_LINKED;
631 if (FAILED(hr)) {
632 gst_buffer_unref(buf);
633 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
634 return GST_FLOW_FLUSHING;
637 gst_buffer_map(buf, &info, GST_MAP_READ);
639 hr = IMediaSample_SetActualDataLength(sample, info.size);
640 if(FAILED(hr)){
641 WARN("SetActualDataLength failed: %08x\n", hr);
642 return GST_FLOW_FLUSHING;
645 IMediaSample_GetPointer(sample, &ptr);
647 memcpy(ptr, info.data, info.size);
649 gst_buffer_unmap(buf, &info);
651 if (GST_BUFFER_PTS_IS_VALID(buf)) {
652 REFERENCE_TIME rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts);
653 if (rtStart >= 0)
654 rtStart /= 100;
656 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
657 REFERENCE_TIME tStart = buf->pts / 100;
658 REFERENCE_TIME tStop = (buf->pts + buf->duration) / 100;
659 REFERENCE_TIME rtStop;
660 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts + buf->duration);
661 if (rtStop >= 0)
662 rtStop /= 100;
663 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
664 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
665 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
666 } else {
667 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
668 IMediaSample_SetMediaTime(sample, NULL, NULL);
670 } else {
671 IMediaSample_SetTime(sample, NULL, NULL);
672 IMediaSample_SetMediaTime(sample, NULL, NULL);
675 IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
676 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_LIVE));
677 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
679 if (!pin->pin.pin.pConnectedTo)
680 hr = VFW_E_NOT_CONNECTED;
681 else
682 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
684 TRACE("sending sample returned: %08x\n", hr);
686 gst_buffer_unref(buf);
687 IMediaSample_Release(sample);
689 if (hr == VFW_E_NOT_CONNECTED)
690 return GST_FLOW_NOT_LINKED;
692 if (FAILED(hr))
693 return GST_FLOW_FLUSHING;
695 return GST_FLOW_OK;
698 static GstFlowReturn request_buffer_src(GstPad *pad, GstObject *parent, guint64 ofs, guint len, GstBuffer **buf)
700 GSTImpl *This = gst_pad_get_element_private(pad);
701 HRESULT hr;
702 GstMapInfo info;
704 TRACE("%p %s %i %p\n", pad, wine_dbgstr_longlong(ofs), len, buf);
706 *buf = NULL;
707 if (ofs == GST_BUFFER_OFFSET_NONE)
708 ofs = This->nextpullofs;
709 if (ofs >= This->filesize) {
710 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
711 return GST_FLOW_EOS;
713 if (len + ofs > This->filesize)
714 len = This->filesize - ofs;
715 This->nextpullofs = ofs + len;
717 *buf = gst_buffer_new_and_alloc(len);
718 gst_buffer_map(*buf, &info, GST_MAP_WRITE);
719 hr = IAsyncReader_SyncRead(This->pInputPin.pReader, ofs, len, info.data);
720 gst_buffer_unmap(*buf, &info);
721 if (FAILED(hr)) {
722 ERR("Returned %08x\n", hr);
723 return GST_FLOW_ERROR;
726 GST_BUFFER_OFFSET(*buf) = ofs;
727 return GST_FLOW_OK;
730 static DWORD CALLBACK push_data_init(LPVOID iface)
732 GSTImpl *This = iface;
733 DWORD64 ofs = 0;
735 TRACE("Starting..\n");
736 for (;;) {
737 GstBuffer *buf;
738 GstFlowReturn ret = request_buffer_src(This->my_src, NULL, ofs, 4096, &buf);
739 if (ret < 0) {
740 ERR("Obtaining buffer returned: %i\n", ret);
741 break;
743 ret = gst_pad_push(This->my_src, buf);
744 ofs += 4096;
745 if (ret)
746 TRACE("Sending returned: %i\n", ret);
747 if (ret < 0)
748 break;
750 TRACE("Stopping..\n");
751 return 0;
754 static void removed_decoded_pad(GstElement *bin, GstPad *pad, gpointer user)
756 GSTImpl *This = (GSTImpl*)user;
757 int x;
758 GSTOutPin *pin;
760 TRACE("%p %p %p\n", This, bin, pad);
762 EnterCriticalSection(&This->filter.csFilter);
763 for (x = 0; x < This->cStreams; ++x) {
764 if (This->ppPins[x]->their_src == pad)
765 break;
767 if (x == This->cStreams)
768 goto out;
770 pin = This->ppPins[x];
772 if(pin->flipfilter)
773 gst_pad_unlink(pin->their_src, pin->flip_sink);
774 else
775 gst_pad_unlink(pin->their_src, pin->my_sink);
777 gst_object_unref(pin->their_src);
778 pin->their_src = NULL;
779 out:
780 TRACE("Removed %i/%i\n", x, This->cStreams);
781 LeaveCriticalSection(&This->filter.csFilter);
784 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, GSTImpl *This)
786 HRESULT hr;
787 PIN_INFO piOutput;
788 const char *typename;
789 char *name;
790 AM_MEDIA_TYPE amt = {{0}};
791 GstCaps *caps;
792 GstStructure *arg;
793 GstPad *mypad;
794 GSTOutPin *pin;
795 int ret;
796 BOOL isvid = FALSE, isaud = FALSE;
797 gchar my_name[1024];
799 TRACE("%p %p %p\n", This, bin, pad);
801 piOutput.dir = PINDIR_OUTPUT;
802 piOutput.pFilter = &This->filter.IBaseFilter_iface;
803 name = gst_pad_get_name(pad);
804 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1);
805 TRACE("Name: %s\n", name);
806 strcpy(my_name, "qz_sink_");
807 strcat(my_name, name);
808 g_free(name);
809 piOutput.achName[sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1] = 0;
811 caps = gst_pad_query_caps(pad, NULL);
812 caps = gst_caps_make_writable(caps);
813 arg = gst_caps_get_structure(caps, 0);
814 typename = gst_structure_get_name(arg);
816 mypad = gst_pad_new(my_name, GST_PAD_SINK);
817 gst_pad_set_chain_function(mypad, got_data_sink_wrapper);
818 gst_pad_set_event_function(mypad, event_sink_wrapper);
819 gst_pad_set_query_function(mypad, query_sink_wrapper);
821 if (!strcmp(typename, "audio/x-raw")) {
822 isaud = TRUE;
823 } else if (!strcmp(typename, "video/x-raw")) {
824 isvid = TRUE;
825 } else {
826 FIXME("Unknown type \'%s\'\n", typename);
827 return;
830 hr = GST_AddPin(This, &piOutput, &amt);
831 if (FAILED(hr)) {
832 ERR("%08x\n", hr);
833 return;
836 pin = This->ppPins[This->cStreams - 1];
837 gst_pad_set_element_private(mypad, pin);
838 pin->my_sink = mypad;
839 pin->isaud = isaud;
840 pin->isvid = isvid;
842 gst_segment_init(pin->segment, GST_FORMAT_TIME);
844 if (isvid) {
845 GstElement *vconv;
847 TRACE("setting up videoflip filter for pin %p, my_sink: %p, their_src: %p\n",
848 pin, pin->my_sink, pad);
850 /* gstreamer outputs video top-down, but dshow expects bottom-up, so
851 * make new transform filter to invert video */
852 vconv = gst_element_factory_make("videoconvert", NULL);
853 if(!vconv){
854 ERR("Missing videoconvert filter?\n");
855 ret = -1;
856 goto exit;
859 pin->flipfilter = gst_element_factory_make("videoflip", NULL);
860 if(!pin->flipfilter){
861 ERR("Missing videoflip filter?\n");
862 ret = -1;
863 goto exit;
866 gst_util_set_object_arg(G_OBJECT(pin->flipfilter), "method", "vertical-flip");
868 gst_bin_add(GST_BIN(This->container), vconv); /* bin takes ownership */
869 gst_element_sync_state_with_parent(vconv);
870 gst_bin_add(GST_BIN(This->container), pin->flipfilter); /* bin takes ownership */
871 gst_element_sync_state_with_parent(pin->flipfilter);
873 gst_element_link (vconv, pin->flipfilter);
875 pin->flip_sink = gst_element_get_static_pad(vconv, "sink");
876 if(!pin->flip_sink){
877 WARN("Couldn't find sink on flip filter\n");
878 pin->flipfilter = NULL;
879 ret = -1;
880 goto exit;
883 ret = gst_pad_link(pad, pin->flip_sink);
884 if(ret < 0){
885 WARN("gst_pad_link failed: %d\n", ret);
886 gst_object_unref(pin->flip_sink);
887 pin->flip_sink = NULL;
888 pin->flipfilter = NULL;
889 goto exit;
892 pin->flip_src = gst_element_get_static_pad(pin->flipfilter, "src");
893 if(!pin->flip_src){
894 WARN("Couldn't find src on flip filter\n");
895 gst_object_unref(pin->flip_sink);
896 pin->flip_sink = NULL;
897 pin->flipfilter = NULL;
898 ret = -1;
899 goto exit;
902 ret = gst_pad_link(pin->flip_src, pin->my_sink);
903 if(ret < 0){
904 WARN("gst_pad_link failed: %d\n", ret);
905 gst_object_unref(pin->flip_src);
906 pin->flip_src = NULL;
907 gst_object_unref(pin->flip_sink);
908 pin->flip_sink = NULL;
909 pin->flipfilter = NULL;
910 goto exit;
912 } else
913 ret = gst_pad_link(pad, mypad);
915 gst_pad_set_active(mypad, 1);
917 exit:
918 TRACE("Linking: %i\n", ret);
920 if (ret >= 0) {
921 pin->their_src = pad;
922 gst_object_ref(pin->their_src);
926 static void existing_new_pad(GstElement *bin, GstPad *pad, gpointer user)
928 GSTImpl *This = (GSTImpl*)user;
929 int x, ret;
931 TRACE("%p %p %p\n", This, bin, pad);
933 if (gst_pad_is_linked(pad))
934 return;
936 /* Still holding our own lock */
937 if (This->initial) {
938 init_new_decoded_pad(bin, pad, This);
939 return;
942 EnterCriticalSection(&This->filter.csFilter);
943 for (x = 0; x < This->cStreams; ++x) {
944 GSTOutPin *pin = This->ppPins[x];
945 if (!pin->their_src) {
946 gst_segment_init(pin->segment, GST_FORMAT_TIME);
948 if (pin->flipfilter)
949 ret = gst_pad_link(pad, pin->flip_sink);
950 else
951 ret = gst_pad_link(pad, pin->my_sink);
953 if (ret >= 0) {
954 pin->their_src = pad;
955 gst_object_ref(pin->their_src);
956 TRACE("Relinked\n");
957 LeaveCriticalSection(&This->filter.csFilter);
958 return;
962 init_new_decoded_pad(bin, pad, This);
963 LeaveCriticalSection(&This->filter.csFilter);
966 static gboolean query_function(GstPad *pad, GstObject *parent, GstQuery *query)
968 GSTImpl *This = gst_pad_get_element_private(pad);
969 GstFormat format;
970 int ret;
971 LONGLONG duration;
973 TRACE("%p %p %p\n", This, pad, query);
975 switch (GST_QUERY_TYPE(query)) {
976 case GST_QUERY_DURATION:
977 gst_query_parse_duration (query, &format, NULL);
978 if (format == GST_FORMAT_PERCENT) {
979 gst_query_set_duration (query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
980 return TRUE;
982 ret = gst_pad_query_convert (pad, GST_FORMAT_BYTES, This->filesize, format, &duration);
983 gst_query_set_duration(query, format, duration);
984 return ret;
985 case GST_QUERY_SEEKING:
986 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
987 TRACE("Seeking %i %i\n", format, GST_FORMAT_BYTES);
988 if (format != GST_FORMAT_BYTES)
989 return FALSE;
990 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
991 return TRUE;
992 case GST_QUERY_SCHEDULING:
993 gst_query_set_scheduling(query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
994 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PUSH);
995 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PULL);
996 return TRUE;
997 default:
998 TRACE("Unhandled query type: %s\n", GST_QUERY_TYPE_NAME(query));
999 return FALSE;
1003 static gboolean activate_push(GstPad *pad, gboolean activate)
1005 GSTImpl *This = gst_pad_get_element_private(pad);
1007 TRACE("%p %p %u\n", This, pad, activate);
1009 EnterCriticalSection(&This->filter.csFilter);
1010 if (!activate) {
1011 TRACE("Deactivating\n");
1012 if (!This->initial)
1013 IAsyncReader_BeginFlush(This->pInputPin.pReader);
1014 if (This->push_thread) {
1015 WaitForSingleObject(This->push_thread, -1);
1016 CloseHandle(This->push_thread);
1017 This->push_thread = NULL;
1019 if (!This->initial)
1020 IAsyncReader_EndFlush(This->pInputPin.pReader);
1021 if (This->filter.state == State_Stopped)
1022 This->nextofs = This->start;
1023 } else if (!This->push_thread) {
1024 TRACE("Activating\n");
1025 if (This->initial)
1026 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
1027 else
1028 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
1030 LeaveCriticalSection(&This->filter.csFilter);
1031 return TRUE;
1034 static gboolean activate_mode(GstPad *pad, GstObject *parent, GstPadMode mode, gboolean activate)
1036 TRACE("%p %p 0x%x %u\n", pad, parent, mode, activate);
1037 switch (mode) {
1038 case GST_PAD_MODE_PULL:
1039 return TRUE;
1040 case GST_PAD_MODE_PUSH:
1041 return activate_push(pad, activate);
1042 default:
1043 return FALSE;
1045 return FALSE;
1048 static void no_more_pads(GstElement *decodebin, gpointer user)
1050 GSTImpl *This = (GSTImpl*)user;
1051 TRACE("%p %p\n", This, decodebin);
1052 SetEvent(This->no_more_pads_event);
1055 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, gpointer user)
1057 const char *name = gst_element_factory_get_longname(fact);
1059 if (strstr(name, "Player protection")) {
1060 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
1061 return GST_AUTOPLUG_SELECT_SKIP;
1063 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder")) {
1064 WARN("Disabled video acceleration since it breaks in wine\n");
1065 return GST_AUTOPLUG_SELECT_SKIP;
1067 TRACE("using \"%s\"\n", name);
1068 return GST_AUTOPLUG_SELECT_TRY;
1071 static GstBusSyncReply watch_bus(GstBus *bus, GstMessage *msg, gpointer data)
1073 GSTImpl *This = data;
1074 GError *err = NULL;
1075 gchar *dbg_info = NULL;
1077 TRACE("%p %p %p\n", This, bus, msg);
1079 if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_ERROR) {
1080 gst_message_parse_error(msg, &err, &dbg_info);
1081 ERR("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1082 ERR("%s\n", dbg_info);
1083 } else if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_WARNING) {
1084 gst_message_parse_warning(msg, &err, &dbg_info);
1085 WARN("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1086 WARN("%s\n", dbg_info);
1088 if (err)
1089 g_error_free(err);
1090 g_free(dbg_info);
1091 return GST_BUS_DROP;
1094 static void unknown_type(GstElement *bin, GstPad *pad, GstCaps *caps, gpointer user)
1096 gchar *strcaps = gst_caps_to_string(caps);
1097 ERR("Could not find a filter for caps: %s\n", strcaps);
1098 g_free(strcaps);
1101 static HRESULT GST_Connect(GSTInPin *pPin, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
1103 GSTImpl *This = (GSTImpl*)pPin->pin.pinInfo.pFilter;
1104 int ret, i;
1105 LONGLONG avail, duration;
1106 GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
1107 "quartz_src",
1108 GST_PAD_SRC,
1109 GST_PAD_ALWAYS,
1110 GST_STATIC_CAPS_ANY);
1111 GstElement *gstfilter;
1113 TRACE("%p %p %p\n", pPin, pConnectPin, props);
1115 This->props = *props;
1116 IAsyncReader_Length(pPin->pReader, &This->filesize, &avail);
1118 if (!This->bus) {
1119 This->bus = gst_bus_new();
1120 gst_bus_set_sync_handler(This->bus, watch_bus_wrapper, This, NULL);
1123 This->container = gst_bin_new(NULL);
1124 gst_element_set_bus(This->container, This->bus);
1126 gstfilter = gst_element_factory_make("decodebin", NULL);
1127 if (!gstfilter) {
1128 ERR("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
1129 8 * (int)sizeof(void*));
1130 return E_FAIL;
1133 gst_bin_add(GST_BIN(This->container), gstfilter);
1135 g_signal_connect(gstfilter, "pad-added", G_CALLBACK(existing_new_pad_wrapper), This);
1136 g_signal_connect(gstfilter, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), This);
1137 g_signal_connect(gstfilter, "autoplug-select", G_CALLBACK(autoplug_blacklist_wrapper), This);
1138 g_signal_connect(gstfilter, "unknown-type", G_CALLBACK(unknown_type_wrapper), This);
1140 This->my_src = gst_pad_new_from_static_template(&src_template, "quartz-src");
1141 gst_pad_set_getrange_function(This->my_src, request_buffer_src_wrapper);
1142 gst_pad_set_query_function(This->my_src, query_function_wrapper);
1143 gst_pad_set_activatemode_function(This->my_src, activate_mode_wrapper);
1144 gst_pad_set_event_function(This->my_src, event_src_wrapper);
1145 gst_pad_set_element_private (This->my_src, This);
1146 This->their_sink = gst_element_get_static_pad(gstfilter, "sink");
1148 g_signal_connect(gstfilter, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), This);
1149 ret = gst_pad_link(This->my_src, This->their_sink);
1150 if (ret < 0) {
1151 ERR("Returns: %i\n", ret);
1152 return E_FAIL;
1154 This->start = This->nextofs = This->nextpullofs = This->stop = 0;
1156 /* Add initial pins */
1157 This->initial = This->discont = TRUE;
1158 ResetEvent(This->no_more_pads_event);
1159 gst_element_set_state(This->container, GST_STATE_PLAYING);
1160 ret = gst_element_get_state(This->container, NULL, NULL, -1);
1162 if (ret == GST_STATE_CHANGE_FAILURE)
1164 ERR("GStreamer failed to play stream\n");
1165 return E_FAIL;
1168 WaitForSingleObject(This->no_more_pads_event, INFINITE);
1170 gst_pad_query_duration(This->ppPins[0]->their_src, GST_FORMAT_TIME, &duration);
1171 for (i = 0; i < This->cStreams; ++i)
1173 This->ppPins[i]->seek.llDuration = This->ppPins[i]->seek.llStop = duration / 100;
1174 This->ppPins[i]->seek.llCurrent = 0;
1175 if (!This->ppPins[i]->seek.llDuration)
1176 This->ppPins[i]->seek.dwCapabilities = 0;
1177 WaitForSingleObject(This->ppPins[i]->caps_event, INFINITE);
1179 *props = This->props;
1181 This->ignore_flush = TRUE;
1182 gst_element_set_state(This->container, GST_STATE_READY);
1183 gst_element_get_state(This->container, NULL, NULL, -1);
1184 This->ignore_flush = FALSE;
1186 This->initial = FALSE;
1188 /* don't set active during test-play, as we don't want to push/pull data
1189 * from the source yet */
1190 gst_pad_set_active(This->my_src, 1);
1192 This->nextofs = This->nextpullofs = 0;
1193 return S_OK;
1196 static inline GSTOutPin *impl_from_IMediaSeeking( IMediaSeeking *iface )
1198 return CONTAINING_RECORD(iface, GSTOutPin, seek.IMediaSeeking_iface);
1201 static IPin* WINAPI GST_GetPin(BaseFilter *iface, int pos)
1203 GSTImpl *This = (GSTImpl *)iface;
1204 IPin *pin;
1206 TRACE("%p: Asking for pos %x\n", This, pos);
1208 if (pos > This->cStreams || pos < 0)
1209 return NULL;
1211 if (!pos)
1212 pin = &This->pInputPin.pin.IPin_iface;
1213 else
1214 pin = &This->ppPins[pos - 1]->pin.pin.IPin_iface;
1216 IPin_AddRef(pin);
1217 return pin;
1220 static LONG WINAPI GST_GetPinCount(BaseFilter *iface)
1222 GSTImpl *This = (GSTImpl *)iface;
1223 TRACE("%p -> %u\n", This, This->cStreams + 1);
1224 return (This->cStreams + 1);
1227 static const BaseFilterFuncTable BaseFuncTable = {
1228 GST_GetPin,
1229 GST_GetPinCount
1232 IUnknown * CALLBACK Gstreamer_Splitter_create(IUnknown *pUnkOuter, HRESULT *phr)
1234 IUnknown *obj = NULL;
1235 PIN_INFO *piInput;
1236 GSTImpl *This;
1238 TRACE("%p %p\n", pUnkOuter, phr);
1240 if (!Gstreamer_init())
1242 *phr = E_FAIL;
1243 return NULL;
1246 mark_wine_thread();
1248 This = CoTaskMemAlloc(sizeof(*This));
1249 if (!This)
1251 *phr = E_OUTOFMEMORY;
1252 return NULL;
1254 memset(This, 0, sizeof(*This));
1256 obj = (IUnknown*)&This->filter.IBaseFilter_iface;
1257 BaseFilter_Init(&This->filter, &GST_Vtbl, &CLSID_Gstreamer_Splitter, (DWORD_PTR)(__FILE__ ": GSTImpl.csFilter"), &BaseFuncTable);
1259 This->cStreams = 0;
1260 This->ppPins = NULL;
1261 This->push_thread = NULL;
1262 This->no_more_pads_event = CreateEventW(NULL, 0, 0, NULL);
1263 This->push_event = CreateEventW(NULL, 0, 0, NULL);
1264 This->bus = NULL;
1266 piInput = &This->pInputPin.pin.pinInfo;
1267 piInput->dir = PINDIR_INPUT;
1268 piInput->pFilter = &This->filter.IBaseFilter_iface;
1269 lstrcpynW(piInput->achName, wcsInputPinName, sizeof(piInput->achName) / sizeof(piInput->achName[0]));
1270 This->pInputPin.pin.IPin_iface.lpVtbl = &GST_InputPin_Vtbl;
1271 This->pInputPin.pin.refCount = 1;
1272 This->pInputPin.pin.pConnectedTo = NULL;
1273 This->pInputPin.pin.pCritSec = &This->filter.csFilter;
1274 ZeroMemory(&This->pInputPin.pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1275 *phr = S_OK;
1277 TRACE("returning %p\n", obj);
1279 return obj;
1282 static void GST_Destroy(GSTImpl *This)
1284 IPin *connected = NULL;
1285 ULONG pinref;
1286 HRESULT hr;
1288 TRACE("Destroying %p\n", This);
1290 CloseHandle(This->no_more_pads_event);
1291 CloseHandle(This->push_event);
1293 /* Don't need to clean up output pins, disconnecting input pin will do that */
1294 IPin_ConnectedTo((IPin *)&This->pInputPin, &connected);
1295 if (connected) {
1296 hr = IPin_Disconnect(connected);
1297 assert(hr == S_OK);
1298 IPin_Release(connected);
1299 hr = IPin_Disconnect(&This->pInputPin.pin.IPin_iface);
1300 assert(hr == S_OK);
1302 pinref = IPin_Release(&This->pInputPin.pin.IPin_iface);
1303 if (pinref) {
1304 /* Valgrind could find this, if I kill it here */
1305 ERR("pinref should be null, is %u, destroying anyway\n", pinref);
1306 assert((LONG)pinref > 0);
1308 while (pinref)
1309 pinref = IPin_Release(&This->pInputPin.pin.IPin_iface);
1311 if (This->bus) {
1312 gst_bus_set_sync_handler(This->bus, NULL, NULL, NULL);
1313 gst_object_unref(This->bus);
1315 BaseFilter_Destroy(&This->filter);
1316 CoTaskMemFree(This);
1319 static HRESULT WINAPI GST_QueryInterface(IBaseFilter *iface, REFIID riid, LPVOID *ppv)
1321 GSTImpl *This = (GSTImpl *)iface;
1323 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1325 *ppv = NULL;
1327 if (IsEqualIID(riid, &IID_IUnknown) ||
1328 IsEqualIID(riid, &IID_IPersist) ||
1329 IsEqualIID(riid, &IID_IMediaFilter) ||
1330 IsEqualIID(riid, &IID_IBaseFilter))
1332 *ppv = &This->filter.IBaseFilter_iface;
1335 if (*ppv) {
1336 IUnknown_AddRef((IUnknown *)(*ppv));
1337 return S_OK;
1340 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
1341 !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
1342 FIXME("No interface for %s!\n", debugstr_guid(riid));
1344 return E_NOINTERFACE;
1347 static ULONG WINAPI GST_Release(IBaseFilter *iface)
1349 GSTImpl *This = (GSTImpl *)iface;
1350 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
1352 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1354 if (!refCount)
1355 GST_Destroy(This);
1357 return refCount;
1360 static HRESULT WINAPI GST_Stop(IBaseFilter *iface)
1362 GSTImpl *This = (GSTImpl *)iface;
1364 TRACE("(%p)\n", This);
1366 mark_wine_thread();
1368 if (This->container) {
1369 This->ignore_flush = TRUE;
1370 gst_element_set_state(This->container, GST_STATE_READY);
1371 gst_element_get_state(This->container, NULL, NULL, -1);
1372 This->ignore_flush = FALSE;
1374 return S_OK;
1377 static HRESULT WINAPI GST_Pause(IBaseFilter *iface)
1379 HRESULT hr = S_OK;
1380 GSTImpl *This = (GSTImpl *)iface;
1381 GstState now;
1382 GstStateChangeReturn ret;
1384 TRACE("(%p)\n", This);
1386 if (!This->container)
1387 return VFW_E_NOT_CONNECTED;
1389 mark_wine_thread();
1391 gst_element_get_state(This->container, &now, NULL, -1);
1392 if (now == GST_STATE_PAUSED)
1393 return S_OK;
1394 if (now != GST_STATE_PLAYING)
1395 hr = IBaseFilter_Run(iface, -1);
1396 if (FAILED(hr))
1397 return hr;
1398 ret = gst_element_set_state(This->container, GST_STATE_PAUSED);
1399 if (ret == GST_STATE_CHANGE_ASYNC)
1400 hr = S_FALSE;
1401 return hr;
1404 static HRESULT WINAPI GST_Run(IBaseFilter *iface, REFERENCE_TIME tStart)
1406 HRESULT hr = S_OK;
1407 GSTImpl *This = (GSTImpl *)iface;
1408 ULONG i;
1409 GstState now;
1410 HRESULT hr_any = VFW_E_NOT_CONNECTED;
1412 TRACE("(%p)->(%s)\n", This, wine_dbgstr_longlong(tStart));
1414 mark_wine_thread();
1416 if (!This->container)
1417 return VFW_E_NOT_CONNECTED;
1419 EnterCriticalSection(&This->filter.csFilter);
1420 This->filter.rtStreamStart = tStart;
1421 LeaveCriticalSection(&This->filter.csFilter);
1423 gst_element_get_state(This->container, &now, NULL, -1);
1424 if (now == GST_STATE_PLAYING)
1425 return S_OK;
1426 if (now == GST_STATE_PAUSED) {
1427 GstStateChangeReturn ret;
1428 ret = gst_element_set_state(This->container, GST_STATE_PLAYING);
1429 if (ret == GST_STATE_CHANGE_ASYNC)
1430 return S_FALSE;
1431 return S_OK;
1434 EnterCriticalSection(&This->filter.csFilter);
1435 gst_element_set_state(This->container, GST_STATE_PLAYING);
1436 This->filter.rtStreamStart = tStart;
1438 for (i = 0; i < This->cStreams; i++) {
1439 hr = BaseOutputPinImpl_Active(&This->ppPins[i]->pin);
1440 if (SUCCEEDED(hr)) {
1441 hr_any = hr;
1444 hr = hr_any;
1445 LeaveCriticalSection(&This->filter.csFilter);
1447 return hr;
1450 static HRESULT WINAPI GST_GetState(IBaseFilter *iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
1452 GSTImpl *This = (GSTImpl *)iface;
1453 HRESULT hr = S_OK;
1454 GstState now, pending;
1455 GstStateChangeReturn ret;
1457 TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
1459 mark_wine_thread();
1461 if (!This->container) {
1462 *pState = State_Stopped;
1463 return S_OK;
1466 ret = gst_element_get_state(This->container, &now, &pending, dwMilliSecsTimeout == INFINITE ? -1 : dwMilliSecsTimeout * 1000);
1468 if (ret == GST_STATE_CHANGE_ASYNC)
1469 hr = VFW_S_STATE_INTERMEDIATE;
1470 else
1471 pending = now;
1473 switch (pending) {
1474 case GST_STATE_PAUSED: *pState = State_Paused; return hr;
1475 case GST_STATE_PLAYING: *pState = State_Running; return hr;
1476 default: *pState = State_Stopped; return hr;
1480 static HRESULT WINAPI GST_FindPin(IBaseFilter *iface, LPCWSTR Id, IPin **ppPin)
1482 FIXME("(%p)->(%s,%p) stub\n", iface, debugstr_w(Id), ppPin);
1483 return E_NOTIMPL;
1486 static const IBaseFilterVtbl GST_Vtbl = {
1487 GST_QueryInterface,
1488 BaseFilterImpl_AddRef,
1489 GST_Release,
1490 BaseFilterImpl_GetClassID,
1491 GST_Stop,
1492 GST_Pause,
1493 GST_Run,
1494 GST_GetState,
1495 BaseFilterImpl_SetSyncSource,
1496 BaseFilterImpl_GetSyncSource,
1497 BaseFilterImpl_EnumPins,
1498 GST_FindPin,
1499 BaseFilterImpl_QueryFilterInfo,
1500 BaseFilterImpl_JoinFilterGraph,
1501 BaseFilterImpl_QueryVendorInfo
1504 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1506 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1507 TRACE("(%p)\n", This);
1508 return S_OK;
1511 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1513 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1514 TRACE("(%p)\n", This);
1515 return S_OK;
1518 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1520 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1521 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1522 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1523 mark_wine_thread();
1524 gst_pad_push_event(This->my_sink, ev);
1525 return S_OK;
1528 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1530 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1531 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1534 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1536 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1537 return IPin_AddRef(&This->pin.pin.IPin_iface);
1540 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1542 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1543 return IPin_Release(&This->pin.pin.IPin_iface);
1546 static HRESULT WINAPI GST_Seeking_GetCurrentPosition(IMediaSeeking *iface, REFERENCE_TIME *pos)
1548 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1550 TRACE("(%p)->(%p)\n", This, pos);
1552 if (!pos)
1553 return E_POINTER;
1555 mark_wine_thread();
1557 if (!This->their_src) {
1558 *pos = This->seek.llCurrent;
1559 TRACE("Cached value\n");
1560 if (This->seek.llDuration)
1561 return S_OK;
1562 else
1563 return E_NOTIMPL;
1566 if (!gst_pad_query_position(This->their_src, GST_FORMAT_TIME, pos)) {
1567 WARN("Could not query position\n");
1568 return E_NOTIMPL;
1570 *pos /= 100;
1571 This->seek.llCurrent = *pos;
1572 return S_OK;
1575 static GstSeekType type_from_flags(DWORD flags)
1577 switch (flags & AM_SEEKING_PositioningBitsMask) {
1578 case AM_SEEKING_NoPositioning:
1579 return GST_SEEK_TYPE_NONE;
1580 case AM_SEEKING_AbsolutePositioning:
1581 case AM_SEEKING_RelativePositioning:
1582 return GST_SEEK_TYPE_SET;
1583 case AM_SEEKING_IncrementalPositioning:
1584 return GST_SEEK_TYPE_END;
1586 return GST_SEEK_TYPE_NONE;
1589 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1590 REFERENCE_TIME *pCur, DWORD curflags, REFERENCE_TIME *pStop,
1591 DWORD stopflags)
1593 HRESULT hr;
1594 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1595 GstSeekFlags f = 0;
1596 GstSeekType curtype, stoptype;
1597 GstEvent *e;
1598 gint64 stop_pos = 0, curr_pos = 0;
1600 TRACE("(%p)->(%p, 0x%x, %p, 0x%x)\n", This, pCur, curflags, pStop, stopflags);
1602 mark_wine_thread();
1604 if (!This->seek.llDuration)
1605 return E_NOTIMPL;
1607 hr = SourceSeekingImpl_SetPositions(iface, pCur, curflags, pStop, stopflags);
1608 if (!This->their_src)
1609 return hr;
1611 curtype = type_from_flags(curflags);
1612 stoptype = type_from_flags(stopflags);
1613 if (curflags & AM_SEEKING_SeekToKeyFrame)
1614 f |= GST_SEEK_FLAG_KEY_UNIT;
1615 if (curflags & AM_SEEKING_Segment)
1616 f |= GST_SEEK_FLAG_SEGMENT;
1617 if (!(curflags & AM_SEEKING_NoFlush))
1618 f |= GST_SEEK_FLAG_FLUSH;
1620 if (((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning) ||
1621 ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)) {
1622 gint64 tmp_pos;
1623 gst_pad_query_position (This->my_sink, GST_FORMAT_TIME, &tmp_pos);
1624 if ((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1625 curr_pos = tmp_pos;
1626 if ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1627 stop_pos = tmp_pos;
1630 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);
1631 if (gst_pad_push_event(This->my_sink, e))
1632 return S_OK;
1633 else
1634 return E_NOTIMPL;
1637 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1639 GST_Seeking_QueryInterface,
1640 GST_Seeking_AddRef,
1641 GST_Seeking_Release,
1642 SourceSeekingImpl_GetCapabilities,
1643 SourceSeekingImpl_CheckCapabilities,
1644 SourceSeekingImpl_IsFormatSupported,
1645 SourceSeekingImpl_QueryPreferredFormat,
1646 SourceSeekingImpl_GetTimeFormat,
1647 SourceSeekingImpl_IsUsingTimeFormat,
1648 SourceSeekingImpl_SetTimeFormat,
1649 SourceSeekingImpl_GetDuration,
1650 SourceSeekingImpl_GetStopPosition,
1651 GST_Seeking_GetCurrentPosition,
1652 SourceSeekingImpl_ConvertTimeFormat,
1653 GST_Seeking_SetPositions,
1654 SourceSeekingImpl_GetPositions,
1655 SourceSeekingImpl_GetAvailable,
1656 SourceSeekingImpl_SetRate,
1657 SourceSeekingImpl_GetRate,
1658 SourceSeekingImpl_GetPreroll
1661 static inline GSTOutPin *impl_from_IQualityControl( IQualityControl *iface )
1663 return CONTAINING_RECORD(iface, GSTOutPin, IQualityControl_iface);
1666 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1668 GSTOutPin *pin = impl_from_IQualityControl(iface);
1669 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1672 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1674 GSTOutPin *pin = impl_from_IQualityControl(iface);
1675 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1678 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1680 GSTOutPin *pin = impl_from_IQualityControl(iface);
1681 return IPin_Release(&pin->pin.pin.IPin_iface);
1684 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
1686 GSTOutPin *pin = impl_from_IQualityControl(iface);
1687 GstEvent *evt;
1689 TRACE("(%p)->(%p, { 0x%x %u %s %s })\n", pin, sender,
1690 qm.Type, qm.Proportion,
1691 wine_dbgstr_longlong(qm.Late),
1692 wine_dbgstr_longlong(qm.TimeStamp));
1694 mark_wine_thread();
1696 if (qm.Type == Flood)
1697 qm.Late = 0;
1699 evt = gst_event_new_qos(qm.Type == Famine ? GST_QOS_TYPE_UNDERFLOW : GST_QOS_TYPE_OVERFLOW,
1700 qm.Proportion / 1000., qm.Late * 100, qm.TimeStamp * 100);
1702 if (!evt) {
1703 WARN("Failed to create QOS event\n");
1704 return E_INVALIDARG;
1707 gst_pad_push_event(pin->my_sink, evt);
1709 return S_OK;
1712 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1714 GSTOutPin *pin = impl_from_IQualityControl(iface);
1715 TRACE("(%p)->(%p)\n", pin, pin);
1716 /* Do nothing */
1717 return S_OK;
1720 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1721 GST_QualityControl_QueryInterface,
1722 GST_QualityControl_AddRef,
1723 GST_QualityControl_Release,
1724 GST_QualityControl_Notify,
1725 GST_QualityControl_SetSink
1728 static HRESULT WINAPI GSTOutPin_QueryInterface(IPin *iface, REFIID riid, void **ppv)
1730 GSTOutPin *This = (GSTOutPin *)iface;
1732 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1734 *ppv = NULL;
1736 if (IsEqualIID(riid, &IID_IUnknown))
1737 *ppv = iface;
1738 else if (IsEqualIID(riid, &IID_IPin))
1739 *ppv = iface;
1740 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1741 *ppv = &This->seek;
1742 else if (IsEqualIID(riid, &IID_IQualityControl))
1743 *ppv = &This->IQualityControl_iface;
1745 if (*ppv) {
1746 IUnknown_AddRef((IUnknown *)(*ppv));
1747 return S_OK;
1749 FIXME("No interface for %s!\n", debugstr_guid(riid));
1750 return E_NOINTERFACE;
1753 static ULONG WINAPI GSTOutPin_Release(IPin *iface)
1755 GSTOutPin *This = (GSTOutPin *)iface;
1756 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
1758 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1760 mark_wine_thread();
1762 if (!refCount) {
1763 if (This->their_src) {
1764 if (This->flipfilter) {
1765 gst_pad_unlink(This->their_src, This->flip_sink);
1766 gst_pad_unlink(This->flip_src, This->my_sink);
1767 gst_object_unref(This->flip_src);
1768 gst_object_unref(This->flip_sink);
1769 This->flipfilter = NULL;
1770 This->flip_src = This->flip_sink = NULL;
1771 } else
1772 gst_pad_unlink(This->their_src, This->my_sink);
1773 gst_object_unref(This->their_src);
1775 gst_object_unref(This->my_sink);
1776 CloseHandle(This->caps_event);
1777 DeleteMediaType(This->pmt);
1778 FreeMediaType(&This->pin.pin.mtCurrent);
1779 gst_segment_free(This->segment);
1780 if(This->gstpool)
1781 gst_object_unref(This->gstpool);
1782 if (This->pin.pAllocator)
1783 IMemAllocator_Release(This->pin.pAllocator);
1784 CoTaskMemFree(This);
1785 return 0;
1787 return refCount;
1790 static HRESULT WINAPI GSTOutPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
1792 GSTOutPin *This = (GSTOutPin *)iface;
1794 TRACE("(%p)->(%i, %p)\n", This, iPosition, pmt);
1796 if (iPosition < 0)
1797 return E_INVALIDARG;
1799 if (iPosition > 0)
1800 return VFW_S_NO_MORE_ITEMS;
1802 CopyMediaType(pmt, This->pmt);
1804 return S_OK;
1807 static HRESULT WINAPI GSTOutPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
1809 GSTOutPin *This = (GSTOutPin *)iface;
1810 TRACE("(%p)->(%p, %p)\n", This, pAlloc, ppropInputRequest);
1811 /* Unused */
1812 return S_OK;
1815 static HRESULT WINAPI GSTOutPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
1817 HRESULT hr;
1818 GSTOutPin *This = (GSTOutPin *)iface;
1819 GSTImpl *GSTfilter = (GSTImpl*)This->pin.pin.pinInfo.pFilter;
1821 TRACE("(%p)->(%p, %p)\n", This, pPin, pAlloc);
1823 *pAlloc = NULL;
1824 if (GSTfilter->pInputPin.pAlloc)
1826 hr = IMemInputPin_NotifyAllocator(pPin, GSTfilter->pInputPin.pAlloc, FALSE);
1827 if (SUCCEEDED(hr))
1829 *pAlloc = GSTfilter->pInputPin.pAlloc;
1830 IMemAllocator_AddRef(*pAlloc);
1833 else
1834 hr = VFW_E_NO_ALLOCATOR;
1836 return hr;
1839 static HRESULT WINAPI GSTOutPin_BreakConnect(BaseOutputPin *This)
1841 HRESULT hr;
1843 TRACE("(%p)->()\n", This);
1845 EnterCriticalSection(This->pin.pCritSec);
1846 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1847 hr = VFW_E_NOT_CONNECTED;
1848 else
1850 hr = IPin_Disconnect(This->pin.pConnectedTo);
1851 IPin_Disconnect((IPin *)This);
1853 LeaveCriticalSection(This->pin.pCritSec);
1855 return hr;
1858 static const IPinVtbl GST_OutputPin_Vtbl = {
1859 GSTOutPin_QueryInterface,
1860 BasePinImpl_AddRef,
1861 GSTOutPin_Release,
1862 BaseOutputPinImpl_Connect,
1863 BaseOutputPinImpl_ReceiveConnection,
1864 BaseOutputPinImpl_Disconnect,
1865 BasePinImpl_ConnectedTo,
1866 BasePinImpl_ConnectionMediaType,
1867 BasePinImpl_QueryPinInfo,
1868 BasePinImpl_QueryDirection,
1869 BasePinImpl_QueryId,
1870 GST_OutPin_QueryAccept,
1871 BasePinImpl_EnumMediaTypes,
1872 BasePinImpl_QueryInternalConnections,
1873 BaseOutputPinImpl_EndOfStream,
1874 BaseOutputPinImpl_BeginFlush,
1875 BaseOutputPinImpl_EndFlush,
1876 BasePinImpl_NewSegment
1879 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
1881 NULL,
1882 BaseOutputPinImpl_AttemptConnection,
1883 BasePinImpl_GetMediaTypeVersion,
1884 GSTOutPin_GetMediaType
1886 GSTOutPin_DecideBufferSize,
1887 GSTOutPin_DecideAllocator,
1888 GSTOutPin_BreakConnect
1891 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt)
1893 HRESULT hr;
1894 This->ppPins = CoTaskMemRealloc(This->ppPins, (This->cStreams + 1) * sizeof(IPin *));
1896 hr = BaseOutputPin_Construct(&GST_OutputPin_Vtbl, sizeof(GSTOutPin), piOutput, &output_BaseOutputFuncTable, &This->filter.csFilter, (IPin**)(This->ppPins + This->cStreams));
1897 if (SUCCEEDED(hr)) {
1898 GSTOutPin *pin = This->ppPins[This->cStreams];
1899 memset((char*)pin + sizeof(pin->pin), 0, sizeof(GSTOutPin) - sizeof(pin->pin));
1900 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1901 CopyMediaType(pin->pmt, amt);
1902 pin->pin.pin.pinInfo.pFilter = &This->filter.IBaseFilter_iface;
1903 pin->caps_event = CreateEventW(NULL, 0, 0, NULL);
1904 pin->segment = gst_segment_new();
1905 This->cStreams++;
1906 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1907 SourceSeeking_Init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop, GST_ChangeCurrent, GST_ChangeRate, &This->filter.csFilter);
1908 BaseFilterImpl_IncrementPinVersion(&This->filter);
1909 } else
1910 ERR("Failed with error %x\n", hr);
1911 return hr;
1914 static HRESULT GST_RemoveOutputPins(GSTImpl *This)
1916 HRESULT hr;
1917 ULONG i;
1919 TRACE("(%p)\n", This);
1920 mark_wine_thread();
1922 if (!This->container)
1923 return S_OK;
1924 gst_element_set_state(This->container, GST_STATE_NULL);
1925 gst_pad_unlink(This->my_src, This->their_sink);
1926 gst_object_unref(This->my_src);
1927 gst_object_unref(This->their_sink);
1928 This->my_src = This->their_sink = NULL;
1930 for (i = 0; i < This->cStreams; i++) {
1931 hr = BaseOutputPinImpl_BreakConnect(&This->ppPins[i]->pin);
1932 TRACE("Disconnect: %08x\n", hr);
1933 IPin_Release(&This->ppPins[i]->pin.pin.IPin_iface);
1935 This->cStreams = 0;
1936 CoTaskMemFree(This->ppPins);
1937 This->ppPins = NULL;
1938 gst_element_set_bus(This->container, NULL);
1939 gst_object_unref(This->container);
1940 This->container = NULL;
1941 BaseFilterImpl_IncrementPinVersion(&This->filter);
1942 return S_OK;
1945 static ULONG WINAPI GSTInPin_Release(IPin *iface)
1947 GSTInPin *This = (GSTInPin*)iface;
1948 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1950 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
1951 if (!refCount) {
1952 FreeMediaType(&This->pin.mtCurrent);
1953 if (This->pAlloc)
1954 IMemAllocator_Release(This->pAlloc);
1955 This->pAlloc = NULL;
1956 if (This->pReader)
1957 IAsyncReader_Release(This->pReader);
1958 This->pReader = NULL;
1959 This->pin.IPin_iface.lpVtbl = NULL;
1960 return 0;
1961 } else
1962 return refCount;
1965 static HRESULT WINAPI GSTInPin_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
1967 PIN_DIRECTION pindirReceive;
1968 HRESULT hr = S_OK;
1969 GSTInPin *This = (GSTInPin*)iface;
1971 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1972 dump_AM_MEDIA_TYPE(pmt);
1974 mark_wine_thread();
1976 EnterCriticalSection(This->pin.pCritSec);
1977 if (!This->pin.pConnectedTo) {
1978 ALLOCATOR_PROPERTIES props;
1979 IMemAllocator *pAlloc = NULL;
1981 props.cBuffers = 8;
1982 props.cbBuffer = 16384;
1983 props.cbAlign = 1;
1984 props.cbPrefix = 0;
1986 if (IPin_QueryAccept(iface, pmt) != S_OK)
1987 hr = VFW_E_TYPE_NOT_ACCEPTED;
1989 if (SUCCEEDED(hr)) {
1990 IPin_QueryDirection(pReceivePin, &pindirReceive);
1991 if (pindirReceive != PINDIR_OUTPUT) {
1992 ERR("Can't connect from non-output pin\n");
1993 hr = VFW_E_INVALID_DIRECTION;
1997 This->pReader = NULL;
1998 This->pAlloc = NULL;
1999 ResetEvent(((GSTImpl *)This->pin.pinInfo.pFilter)->push_event);
2000 if (SUCCEEDED(hr))
2001 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
2002 if (SUCCEEDED(hr))
2003 hr = GST_Connect(This, pReceivePin, &props);
2005 /* A certain IAsyncReader::RequestAllocator expects to be passed
2006 non-NULL preferred allocator */
2007 if (SUCCEEDED(hr))
2008 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC,
2009 &IID_IMemAllocator, (LPVOID *)&pAlloc);
2010 if (SUCCEEDED(hr)) {
2011 hr = IAsyncReader_RequestAllocator(This->pReader, pAlloc, &props, &This->pAlloc);
2012 if (FAILED(hr))
2013 WARN("Can't get an allocator, got %08x\n", hr);
2015 if (pAlloc)
2016 IMemAllocator_Release(pAlloc);
2017 if (SUCCEEDED(hr)) {
2018 CopyMediaType(&This->pin.mtCurrent, pmt);
2019 This->pin.pConnectedTo = pReceivePin;
2020 IPin_AddRef(pReceivePin);
2021 hr = IMemAllocator_Commit(This->pAlloc);
2022 SetEvent(((GSTImpl*)This->pin.pinInfo.pFilter)->push_event);
2023 } else {
2024 GST_RemoveOutputPins((GSTImpl *)This->pin.pinInfo.pFilter);
2025 if (This->pReader)
2026 IAsyncReader_Release(This->pReader);
2027 This->pReader = NULL;
2028 if (This->pAlloc)
2029 IMemAllocator_Release(This->pAlloc);
2030 This->pAlloc = NULL;
2032 TRACE("Size: %i\n", props.cbBuffer);
2033 } else
2034 hr = VFW_E_ALREADY_CONNECTED;
2035 LeaveCriticalSection(This->pin.pCritSec);
2036 return hr;
2039 static HRESULT WINAPI GSTInPin_Disconnect(IPin *iface)
2041 HRESULT hr;
2042 GSTInPin *This = (GSTInPin*)iface;
2043 FILTER_STATE state;
2045 TRACE("(%p)\n", This);
2047 mark_wine_thread();
2049 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
2050 EnterCriticalSection(This->pin.pCritSec);
2051 if (This->pin.pConnectedTo) {
2052 GSTImpl *Parser = (GSTImpl *)This->pin.pinInfo.pFilter;
2054 if (SUCCEEDED(hr) && state == State_Stopped) {
2055 IMemAllocator_Decommit(This->pAlloc);
2056 IPin_Disconnect(This->pin.pConnectedTo);
2057 IPin_Release(This->pin.pConnectedTo);
2058 This->pin.pConnectedTo = NULL;
2059 hr = GST_RemoveOutputPins(Parser);
2060 } else
2061 hr = VFW_E_NOT_STOPPED;
2062 } else
2063 hr = S_FALSE;
2064 LeaveCriticalSection(This->pin.pCritSec);
2065 return hr;
2068 static HRESULT WINAPI GSTInPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
2070 GSTInPin *This = (GSTInPin*)iface;
2072 TRACE("(%p)->(%p)\n", This, pmt);
2073 dump_AM_MEDIA_TYPE(pmt);
2075 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
2076 return S_OK;
2077 return S_FALSE;
2080 static HRESULT WINAPI GSTInPin_EndOfStream(IPin *iface)
2082 GSTInPin *pin = (GSTInPin*)iface;
2083 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2085 FIXME("Propagate message on %p\n", This);
2086 return S_OK;
2089 static HRESULT WINAPI GSTInPin_BeginFlush(IPin *iface)
2091 GSTInPin *pin = (GSTInPin*)iface;
2092 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2094 FIXME("Propagate message on %p\n", This);
2095 return S_OK;
2098 static HRESULT WINAPI GSTInPin_EndFlush(IPin *iface)
2100 GSTInPin *pin = (GSTInPin*)iface;
2101 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2103 FIXME("Propagate message on %p\n", This);
2104 return S_OK;
2107 static HRESULT WINAPI GSTInPin_NewSegment(IPin *iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
2109 GSTInPin *pin = (GSTInPin*)iface;
2110 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2112 BasePinImpl_NewSegment(iface, tStart, tStop, dRate);
2113 FIXME("Propagate message on %p\n", This);
2114 return S_OK;
2117 static HRESULT WINAPI GSTInPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
2119 GSTInPin *This = (GSTInPin*)iface;
2121 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
2123 *ppv = NULL;
2125 if (IsEqualIID(riid, &IID_IUnknown))
2126 *ppv = iface;
2127 else if (IsEqualIID(riid, &IID_IPin))
2128 *ppv = iface;
2129 else if (IsEqualIID(riid, &IID_IMediaSeeking))
2131 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
2134 if (*ppv)
2136 IUnknown_AddRef((IUnknown *)(*ppv));
2137 return S_OK;
2140 FIXME("No interface for %s!\n", debugstr_guid(riid));
2142 return E_NOINTERFACE;
2145 static HRESULT WINAPI GSTInPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
2147 BasePin *This = (BasePin *)iface;
2149 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
2151 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
2154 static const IPinVtbl GST_InputPin_Vtbl = {
2155 GSTInPin_QueryInterface,
2156 BasePinImpl_AddRef,
2157 GSTInPin_Release,
2158 BaseInputPinImpl_Connect,
2159 GSTInPin_ReceiveConnection,
2160 GSTInPin_Disconnect,
2161 BasePinImpl_ConnectedTo,
2162 BasePinImpl_ConnectionMediaType,
2163 BasePinImpl_QueryPinInfo,
2164 BasePinImpl_QueryDirection,
2165 BasePinImpl_QueryId,
2166 GSTInPin_QueryAccept,
2167 GSTInPin_EnumMediaTypes,
2168 BasePinImpl_QueryInternalConnections,
2169 GSTInPin_EndOfStream,
2170 GSTInPin_BeginFlush,
2171 GSTInPin_EndFlush,
2172 GSTInPin_NewSegment
2175 pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
2176 pthread_cond_t cb_list_cond = PTHREAD_COND_INITIALIZER;
2177 struct list cb_list = LIST_INIT(cb_list);
2179 void CALLBACK perform_cb(TP_CALLBACK_INSTANCE *instance, void *user)
2181 struct cb_data *cbdata = user;
2183 TRACE("got cb type: 0x%x\n", cbdata->type);
2185 switch(cbdata->type)
2187 case WATCH_BUS:
2189 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2190 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2191 break;
2193 case EXISTING_NEW_PAD:
2195 struct existing_new_pad_data *data = &cbdata->u.existing_new_pad_data;
2196 existing_new_pad(data->bin, data->pad, data->user);
2197 break;
2199 case QUERY_FUNCTION:
2201 struct query_function_data *data = &cbdata->u.query_function_data;
2202 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2203 break;
2205 case ACTIVATE_MODE:
2207 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2208 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2209 break;
2211 case NO_MORE_PADS:
2213 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2214 no_more_pads(data->decodebin, data->user);
2215 break;
2217 case REQUEST_BUFFER_SRC:
2219 struct request_buffer_src_data *data = &cbdata->u.request_buffer_src_data;
2220 cbdata->u.request_buffer_src_data.ret = request_buffer_src(data->pad, data->parent,
2221 data->ofs, data->len, data->buf);
2222 break;
2224 case EVENT_SRC:
2226 struct event_src_data *data = &cbdata->u.event_src_data;
2227 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2228 break;
2230 case EVENT_SINK:
2232 struct event_sink_data *data = &cbdata->u.event_sink_data;
2233 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2234 break;
2236 case GOT_DATA_SINK:
2238 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2239 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2240 break;
2242 case GOT_DATA:
2244 struct got_data_data *data = &cbdata->u.got_data_data;
2245 cbdata->u.got_data_data.ret = got_data(data->pad, data->parent, data->buf);
2246 break;
2248 case REMOVED_DECODED_PAD:
2250 struct removed_decoded_pad_data *data = &cbdata->u.removed_decoded_pad_data;
2251 removed_decoded_pad(data->bin, data->pad, data->user);
2252 break;
2254 case AUTOPLUG_BLACKLIST:
2256 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2257 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2258 data->pad, data->caps, data->fact, data->user);
2259 break;
2261 case UNKNOWN_TYPE:
2263 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2264 unknown_type(data->bin, data->pad, data->caps, data->user);
2265 break;
2267 case RELEASE_SAMPLE:
2269 struct release_sample_data *data = &cbdata->u.release_sample_data;
2270 release_sample(data->data);
2271 break;
2273 case TRANSFORM_PAD_ADDED:
2275 struct transform_pad_added_data *data = &cbdata->u.transform_pad_added_data;
2276 Gstreamer_transform_pad_added(data->filter, data->pad, data->user);
2277 break;
2279 case QUERY_SINK:
2281 struct query_sink_data *data = &cbdata->u.query_sink_data;
2282 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2283 data->query);
2284 break;
2288 pthread_mutex_lock(&cbdata->lock);
2289 cbdata->finished = 1;
2290 pthread_cond_broadcast(&cbdata->cond);
2291 pthread_mutex_unlock(&cbdata->lock);
2294 static DWORD WINAPI dispatch_thread(void *user)
2296 struct cb_data *cbdata;
2298 pthread_mutex_lock(&cb_list_lock);
2300 while(1){
2301 pthread_cond_wait(&cb_list_cond, &cb_list_lock);
2303 while(!list_empty(&cb_list)){
2304 cbdata = LIST_ENTRY(list_head(&cb_list), struct cb_data, entry);
2305 list_remove(&cbdata->entry);
2307 TrySubmitThreadpoolCallback(&perform_cb, cbdata, NULL);
2311 pthread_mutex_unlock(&cb_list_lock);
2313 return 0;
2316 void start_dispatch_thread(void)
2318 pthread_key_create(&wine_gst_key, NULL);
2319 CloseHandle(CreateThread(NULL, 0, &dispatch_thread, NULL, 0, NULL));