gdiplus/tests: Fix copy/paste error in format tests.
[wine.git] / dlls / winegstreamer / gstdemux.c
blob3ef2324cab73aaf786dcd4f34c9dbe9589216cf1
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 event, changed_ofs;
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 return TRUE;
327 static gboolean query_sink(GstPad *pad, GstObject *parent, GstQuery *query)
329 switch (GST_QUERY_TYPE (query)) {
330 case GST_QUERY_ACCEPT_CAPS:
332 GstCaps *caps;
333 gboolean res;
334 gst_query_parse_accept_caps(query, &caps);
335 res = accept_caps_sink(pad, caps);
336 gst_query_set_accept_caps_result(query, res);
337 return TRUE; /* FIXME */
339 default:
340 return gst_pad_query_default (pad, parent, query);
344 static gboolean gst_base_src_perform_seek(GSTImpl *This, GstEvent *event)
346 gboolean res = TRUE;
347 gdouble rate;
348 GstFormat seek_format;
349 GstSeekFlags flags;
350 GstSeekType cur_type, stop_type;
351 gint64 cur, stop;
352 gboolean flush;
353 guint32 seqnum;
354 GstEvent *tevent;
355 BOOL thread = !!This->push_thread;
357 TRACE("%p %p\n", This, event);
359 gst_event_parse_seek(event, &rate, &seek_format, &flags,
360 &cur_type, &cur, &stop_type, &stop);
362 if (seek_format != GST_FORMAT_BYTES) {
363 FIXME("Not handling other format %i\n", seek_format);
364 return FALSE;
367 flush = flags & GST_SEEK_FLAG_FLUSH;
368 seqnum = gst_event_get_seqnum(event);
370 /* send flush start */
371 if (flush) {
372 tevent = gst_event_new_flush_start();
373 gst_event_set_seqnum(tevent, seqnum);
374 gst_pad_push_event(This->my_src, tevent);
375 if (This->pInputPin.pReader)
376 IAsyncReader_BeginFlush(This->pInputPin.pReader);
377 if (thread)
378 gst_pad_set_active(This->my_src, 1);
381 This->nextofs = This->start = cur;
383 /* and prepare to continue streaming */
384 if (flush) {
385 tevent = gst_event_new_flush_stop(TRUE);
386 gst_event_set_seqnum(tevent, seqnum);
387 gst_pad_push_event(This->my_src, tevent);
388 if (This->pInputPin.pReader)
389 IAsyncReader_EndFlush(This->pInputPin.pReader);
390 if (thread)
391 gst_pad_set_active(This->my_src, 1);
394 return res;
397 static gboolean event_src(GstPad *pad, GstObject *parent, GstEvent *event)
399 GSTImpl *This = gst_pad_get_element_private(pad);
401 TRACE("%p %p\n", pad, event);
403 switch (event->type) {
404 case GST_EVENT_SEEK:
405 return gst_base_src_perform_seek(This, event);
406 case GST_EVENT_FLUSH_START:
407 EnterCriticalSection(&This->filter.csFilter);
408 if (This->pInputPin.pReader)
409 IAsyncReader_BeginFlush(This->pInputPin.pReader);
410 LeaveCriticalSection(&This->filter.csFilter);
411 break;
412 case GST_EVENT_FLUSH_STOP:
413 EnterCriticalSection(&This->filter.csFilter);
414 if (This->pInputPin.pReader)
415 IAsyncReader_EndFlush(This->pInputPin.pReader);
416 LeaveCriticalSection(&This->filter.csFilter);
417 break;
418 default:
419 FIXME("%p (%u) stub\n", event, event->type);
420 case GST_EVENT_TAG:
421 case GST_EVENT_QOS:
422 return gst_pad_event_default(pad, parent, event);
424 return TRUE;
427 static gboolean event_sink(GstPad *pad, GstObject *parent, GstEvent *event)
429 GSTOutPin *pin = gst_pad_get_element_private(pad);
431 TRACE("%p %p\n", pad, event);
433 switch (event->type) {
434 case GST_EVENT_SEGMENT: {
435 gdouble rate, applied_rate;
436 gint64 stop, pos;
437 const GstSegment *segment;
439 gst_event_parse_segment(event, &segment);
441 pos = segment->position;
442 stop = segment->stop;
443 rate = segment->rate;
444 applied_rate = segment->applied_rate;
446 if (segment->format != GST_FORMAT_TIME) {
447 FIXME("Ignoring new segment because of format %i\n", segment->format);
448 return TRUE;
451 gst_segment_copy_into(segment, pin->segment);
453 pos /= 100;
455 if (stop > 0)
456 stop /= 100;
458 if (pin->pin.pin.pConnectedTo)
459 IPin_NewSegment(pin->pin.pin.pConnectedTo, pos, stop, rate*applied_rate);
461 return TRUE;
463 case GST_EVENT_EOS:
464 if (pin->pin.pin.pConnectedTo)
465 IPin_EndOfStream(pin->pin.pin.pConnectedTo);
466 return TRUE;
467 case GST_EVENT_FLUSH_START:
468 if (((GSTImpl *)pin->pin.pin.pinInfo.pFilter)->ignore_flush) {
469 /* gst-plugins-base prior to 1.7 contains a bug which causes
470 * our sink pins to receive a flush-start event when the
471 * decodebin changes from PAUSED to READY (including
472 * PLAYING->PAUSED->READY), but no matching flush-stop event is
473 * sent. See <gst-plugins-base.git:60bad4815db966a8e4). Here we
474 * unset the flushing flag to avoid the problem. */
475 TRACE("Working around gst <1.7 bug, ignoring FLUSH_START\n");
476 GST_PAD_UNSET_FLUSHING (pad);
477 return TRUE;
479 if (pin->pin.pin.pConnectedTo)
480 IPin_BeginFlush(pin->pin.pin.pConnectedTo);
481 return TRUE;
482 case GST_EVENT_FLUSH_STOP:
483 gst_segment_init(pin->segment, GST_FORMAT_TIME);
484 if (pin->pin.pin.pConnectedTo)
485 IPin_EndFlush(pin->pin.pin.pConnectedTo);
486 return TRUE;
487 case GST_EVENT_CAPS: {
488 GstCaps *caps;
489 gst_event_parse_caps(event, &caps);
490 return setcaps_sink(pad, caps);
492 default:
493 TRACE("%p stub %s\n", event, gst_event_type_get_name(event->type));
494 return gst_pad_event_default(pad, parent, event);
498 static void release_sample(void *data)
500 ULONG ret;
501 ret = IMediaSample_Release((IMediaSample *)data);
502 TRACE("Releasing %p returns %u\n", data, ret);
505 static DWORD CALLBACK push_data(LPVOID iface)
507 LONGLONG maxlen, curlen;
508 GSTImpl *This = iface;
509 IMediaSample *buf;
510 DWORD_PTR user;
511 HRESULT hr;
513 if (!This->stop)
514 IAsyncReader_Length(This->pInputPin.pReader, &maxlen, &curlen);
515 else
516 maxlen = This->stop;
518 TRACE("Waiting..\n");
520 WaitForSingleObject(This->event, INFINITE);
522 TRACE("Starting..\n");
523 for (;;) {
524 REFERENCE_TIME tStart, tStop;
525 ULONG len;
526 GstBuffer *gstbuf;
527 gsize bufsize;
528 BYTE *data;
529 int ret;
531 TRACE("pAlloc: %p\n", This->pInputPin.pAlloc);
532 hr = IMemAllocator_GetBuffer(This->pInputPin.pAlloc, &buf, NULL, NULL, 0);
533 if (FAILED(hr))
534 break;
536 if (This->nextofs >= maxlen)
537 break;
538 len = IMediaSample_GetSize(buf);
539 if (This->nextofs + len > maxlen)
540 len = maxlen - This->nextofs;
542 tStart = MEDIATIME_FROM_BYTES(This->nextofs);
543 tStop = tStart + MEDIATIME_FROM_BYTES(len);
544 IMediaSample_SetTime(buf, &tStart, &tStop);
546 hr = IAsyncReader_Request(This->pInputPin.pReader, buf, 0);
547 if (FAILED(hr)) {
548 IMediaSample_Release(buf);
549 break;
551 This->nextofs += len;
552 hr = IAsyncReader_WaitForNext(This->pInputPin.pReader, -1, &buf, &user);
553 if (FAILED(hr) || !buf) {
554 if (buf)
555 IMediaSample_Release(buf);
556 break;
559 IMediaSample_GetPointer(buf, &data);
560 bufsize = IMediaSample_GetActualDataLength(buf);
561 gstbuf = gst_buffer_new_wrapped_full(0, data, bufsize, 0, bufsize, buf, release_sample_wrapper);
562 IMediaSample_AddRef(buf);
563 gst_mini_object_set_qdata(GST_MINI_OBJECT(gstbuf), g_quark_from_static_string(media_quark_string), buf, release_sample_wrapper);
564 if (!gstbuf) {
565 IMediaSample_Release(buf);
566 break;
568 gstbuf->duration = gstbuf->pts = -1;
569 ret = gst_pad_push(This->my_src, gstbuf);
570 if (ret >= 0)
571 hr = S_OK;
572 else
573 ERR("Sending returned: %i\n", ret);
574 if (ret == GST_FLOW_ERROR)
575 hr = E_FAIL;
576 else if (ret == GST_FLOW_FLUSHING)
577 hr = VFW_E_WRONG_STATE;
578 if (hr != S_OK)
579 break;
582 gst_pad_push_event(This->my_src, gst_event_new_eos());
584 TRACE("Almost stopping.. %08x\n", hr);
585 do {
586 IAsyncReader_WaitForNext(This->pInputPin.pReader, 0, &buf, &user);
587 if (buf)
588 IMediaSample_Release(buf);
589 } while (buf);
591 TRACE("Stopping.. %08x\n", hr);
592 return 0;
595 static HRESULT WINAPI GST_OutPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
597 GSTOutPin *pin = (GSTOutPin*)iface;
598 FIXME("stub %p\n", pin);
599 return S_OK;
602 static GstFlowReturn got_data_sink(GstPad *pad, GstObject *parent, GstBuffer *buf)
604 GSTOutPin *pin = gst_pad_get_element_private(pad);
605 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
606 HRESULT hr;
607 BYTE *ptr = NULL;
608 IMediaSample *sample;
609 GstMapInfo info;
611 TRACE("%p %p\n", pad, buf);
613 if (This->initial) {
614 gst_buffer_unref(buf);
615 TRACE("Triggering %p %p\n", pad, pin->caps_event);
616 SetEvent(pin->caps_event);
617 return GST_FLOW_OK;
620 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
622 if (hr == VFW_E_NOT_CONNECTED) {
623 gst_buffer_unref(buf);
624 return GST_FLOW_NOT_LINKED;
627 if (FAILED(hr)) {
628 gst_buffer_unref(buf);
629 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
630 return GST_FLOW_FLUSHING;
633 gst_buffer_map(buf, &info, GST_MAP_READ);
635 hr = IMediaSample_SetActualDataLength(sample, info.size);
636 if(FAILED(hr)){
637 WARN("SetActualDataLength failed: %08x\n", hr);
638 return GST_FLOW_FLUSHING;
641 IMediaSample_GetPointer(sample, &ptr);
643 memcpy(ptr, info.data, info.size);
645 gst_buffer_unmap(buf, &info);
647 if (GST_BUFFER_PTS_IS_VALID(buf)) {
648 REFERENCE_TIME rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts);
649 if (rtStart >= 0)
650 rtStart /= 100;
652 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
653 REFERENCE_TIME tStart = buf->pts / 100;
654 REFERENCE_TIME tStop = (buf->pts + buf->duration) / 100;
655 REFERENCE_TIME rtStop;
656 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts + buf->duration);
657 if (rtStop >= 0)
658 rtStop /= 100;
659 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
660 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
661 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
662 } else {
663 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
664 IMediaSample_SetMediaTime(sample, NULL, NULL);
666 } else {
667 IMediaSample_SetTime(sample, NULL, NULL);
668 IMediaSample_SetMediaTime(sample, NULL, NULL);
671 IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
672 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_LIVE));
673 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
675 if (!pin->pin.pin.pConnectedTo)
676 hr = VFW_E_NOT_CONNECTED;
677 else
678 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
680 TRACE("sending sample returned: %08x\n", hr);
682 gst_buffer_unref(buf);
683 IMediaSample_Release(sample);
685 if (hr == VFW_E_NOT_CONNECTED)
686 return GST_FLOW_NOT_LINKED;
688 if (FAILED(hr))
689 return GST_FLOW_FLUSHING;
691 return GST_FLOW_OK;
694 static GstFlowReturn request_buffer_src(GstPad *pad, GstObject *parent, guint64 ofs, guint len, GstBuffer **buf)
696 GSTImpl *This = gst_pad_get_element_private(pad);
697 HRESULT hr;
698 GstMapInfo info;
700 TRACE("%p %s %i %p\n", pad, wine_dbgstr_longlong(ofs), len, buf);
702 *buf = NULL;
703 if (ofs == GST_BUFFER_OFFSET_NONE)
704 ofs = This->nextpullofs;
705 if (ofs >= This->filesize) {
706 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
707 return GST_FLOW_EOS;
709 if (len + ofs > This->filesize)
710 len = This->filesize - ofs;
711 This->nextpullofs = ofs + len;
713 *buf = gst_buffer_new_and_alloc(len);
714 gst_buffer_map(*buf, &info, GST_MAP_WRITE);
715 hr = IAsyncReader_SyncRead(This->pInputPin.pReader, ofs, len, info.data);
716 gst_buffer_unmap(*buf, &info);
717 if (FAILED(hr)) {
718 ERR("Returned %08x\n", hr);
719 return GST_FLOW_ERROR;
722 GST_BUFFER_OFFSET(*buf) = ofs;
723 return GST_FLOW_OK;
726 static DWORD CALLBACK push_data_init(LPVOID iface)
728 GSTImpl *This = iface;
729 DWORD64 ofs = 0;
731 TRACE("Starting..\n");
732 for (;;) {
733 GstBuffer *buf;
734 GstFlowReturn ret = request_buffer_src(This->my_src, NULL, ofs, 4096, &buf);
735 if (ret < 0) {
736 ERR("Obtaining buffer returned: %i\n", ret);
737 break;
739 ret = gst_pad_push(This->my_src, buf);
740 ofs += 4096;
741 if (ret)
742 TRACE("Sending returned: %i\n", ret);
743 if (ret < 0)
744 break;
746 TRACE("Stopping..\n");
747 return 0;
750 static void removed_decoded_pad(GstElement *bin, GstPad *pad, gpointer user)
752 GSTImpl *This = (GSTImpl*)user;
753 int x;
754 GSTOutPin *pin;
756 TRACE("%p %p %p\n", This, bin, pad);
758 EnterCriticalSection(&This->filter.csFilter);
759 for (x = 0; x < This->cStreams; ++x) {
760 if (This->ppPins[x]->their_src == pad)
761 break;
763 if (x == This->cStreams)
764 goto out;
766 pin = This->ppPins[x];
768 if(pin->flipfilter)
769 gst_pad_unlink(pin->their_src, pin->flip_sink);
770 else
771 gst_pad_unlink(pin->their_src, pin->my_sink);
773 gst_object_unref(pin->their_src);
774 pin->their_src = NULL;
775 out:
776 TRACE("Removed %i/%i\n", x, This->cStreams);
777 LeaveCriticalSection(&This->filter.csFilter);
780 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, GSTImpl *This)
782 HRESULT hr;
783 PIN_INFO piOutput;
784 const char *typename;
785 char *name;
786 AM_MEDIA_TYPE amt = {{0}};
787 GstCaps *caps;
788 GstStructure *arg;
789 GstPad *mypad;
790 GSTOutPin *pin;
791 int ret;
792 BOOL isvid = FALSE, isaud = FALSE;
793 gchar my_name[1024];
795 TRACE("%p %p %p\n", This, bin, pad);
797 piOutput.dir = PINDIR_OUTPUT;
798 piOutput.pFilter = &This->filter.IBaseFilter_iface;
799 name = gst_pad_get_name(pad);
800 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1);
801 TRACE("Name: %s\n", name);
802 strcpy(my_name, "qz_sink_");
803 strcat(my_name, name);
804 g_free(name);
805 piOutput.achName[sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1] = 0;
807 caps = gst_pad_query_caps(pad, NULL);
808 caps = gst_caps_make_writable(caps);
809 arg = gst_caps_get_structure(caps, 0);
810 typename = gst_structure_get_name(arg);
812 mypad = gst_pad_new(my_name, GST_PAD_SINK);
813 gst_pad_set_chain_function(mypad, got_data_sink_wrapper);
814 gst_pad_set_event_function(mypad, event_sink_wrapper);
815 gst_pad_set_query_function(mypad, query_sink_wrapper);
817 if (!strcmp(typename, "audio/x-raw")) {
818 isaud = TRUE;
819 } else if (!strcmp(typename, "video/x-raw")) {
820 isvid = TRUE;
821 } else {
822 FIXME("Unknown type \'%s\'\n", typename);
823 return;
826 hr = GST_AddPin(This, &piOutput, &amt);
827 if (FAILED(hr)) {
828 ERR("%08x\n", hr);
829 return;
832 pin = This->ppPins[This->cStreams - 1];
833 gst_pad_set_element_private(mypad, pin);
834 pin->my_sink = mypad;
835 pin->isaud = isaud;
836 pin->isvid = isvid;
838 gst_segment_init(pin->segment, GST_FORMAT_TIME);
840 if (isvid) {
841 GstElement *vconv;
843 TRACE("setting up videoflip filter for pin %p, my_sink: %p, their_src: %p\n",
844 pin, pin->my_sink, pad);
846 /* gstreamer outputs video top-down, but dshow expects bottom-up, so
847 * make new transform filter to invert video */
848 vconv = gst_element_factory_make("videoconvert", NULL);
849 if(!vconv){
850 ERR("Missing videoconvert filter?\n");
851 ret = -1;
852 goto exit;
855 pin->flipfilter = gst_element_factory_make("videoflip", NULL);
856 if(!pin->flipfilter){
857 ERR("Missing videoflip filter?\n");
858 ret = -1;
859 goto exit;
862 gst_util_set_object_arg(G_OBJECT(pin->flipfilter), "method", "vertical-flip");
864 gst_bin_add(GST_BIN(This->container), vconv); /* bin takes ownership */
865 gst_element_sync_state_with_parent(vconv);
866 gst_bin_add(GST_BIN(This->container), pin->flipfilter); /* bin takes ownership */
867 gst_element_sync_state_with_parent(pin->flipfilter);
869 gst_element_link (vconv, pin->flipfilter);
871 pin->flip_sink = gst_element_get_static_pad(vconv, "sink");
872 if(!pin->flip_sink){
873 WARN("Couldn't find sink on flip filter\n");
874 pin->flipfilter = NULL;
875 ret = -1;
876 goto exit;
879 ret = gst_pad_link(pad, pin->flip_sink);
880 if(ret < 0){
881 WARN("gst_pad_link failed: %d\n", ret);
882 gst_object_unref(pin->flip_sink);
883 pin->flip_sink = NULL;
884 pin->flipfilter = NULL;
885 goto exit;
888 pin->flip_src = gst_element_get_static_pad(pin->flipfilter, "src");
889 if(!pin->flip_src){
890 WARN("Couldn't find src on flip filter\n");
891 gst_object_unref(pin->flip_sink);
892 pin->flip_sink = NULL;
893 pin->flipfilter = NULL;
894 ret = -1;
895 goto exit;
898 ret = gst_pad_link(pin->flip_src, pin->my_sink);
899 if(ret < 0){
900 WARN("gst_pad_link failed: %d\n", ret);
901 gst_object_unref(pin->flip_src);
902 pin->flip_src = NULL;
903 gst_object_unref(pin->flip_sink);
904 pin->flip_sink = NULL;
905 pin->flipfilter = NULL;
906 goto exit;
908 } else
909 ret = gst_pad_link(pad, mypad);
911 gst_pad_set_active(mypad, 1);
913 exit:
914 TRACE("Linking: %i\n", ret);
916 if (ret >= 0) {
917 pin->their_src = pad;
918 gst_object_ref(pin->their_src);
922 static void existing_new_pad(GstElement *bin, GstPad *pad, gpointer user)
924 GSTImpl *This = (GSTImpl*)user;
925 int x, ret;
927 TRACE("%p %p %p\n", This, bin, pad);
929 if (gst_pad_is_linked(pad))
930 return;
932 /* Still holding our own lock */
933 if (This->initial) {
934 init_new_decoded_pad(bin, pad, This);
935 return;
938 EnterCriticalSection(&This->filter.csFilter);
939 for (x = 0; x < This->cStreams; ++x) {
940 GSTOutPin *pin = This->ppPins[x];
941 if (!pin->their_src) {
942 gst_segment_init(pin->segment, GST_FORMAT_TIME);
944 if (pin->flipfilter)
945 ret = gst_pad_link(pad, pin->flip_sink);
946 else
947 ret = gst_pad_link(pad, pin->my_sink);
949 if (ret >= 0) {
950 pin->their_src = pad;
951 gst_object_ref(pin->their_src);
952 TRACE("Relinked\n");
953 LeaveCriticalSection(&This->filter.csFilter);
954 return;
958 init_new_decoded_pad(bin, pad, This);
959 LeaveCriticalSection(&This->filter.csFilter);
962 static gboolean query_function(GstPad *pad, GstObject *parent, GstQuery *query)
964 GSTImpl *This = gst_pad_get_element_private(pad);
965 GstFormat format;
966 int ret;
967 LONGLONG duration;
969 TRACE("%p %p %p\n", This, pad, query);
971 switch (GST_QUERY_TYPE(query)) {
972 case GST_QUERY_DURATION:
973 gst_query_parse_duration (query, &format, NULL);
974 if (format == GST_FORMAT_PERCENT) {
975 gst_query_set_duration (query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
976 return TRUE;
978 ret = gst_pad_query_convert (pad, GST_FORMAT_BYTES, This->filesize, format, &duration);
979 gst_query_set_duration(query, format, duration);
980 return ret;
981 case GST_QUERY_SEEKING:
982 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
983 TRACE("Seeking %i %i\n", format, GST_FORMAT_BYTES);
984 if (format != GST_FORMAT_BYTES)
985 return FALSE;
986 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
987 return TRUE;
988 case GST_QUERY_SCHEDULING:
989 gst_query_set_scheduling(query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
990 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PUSH);
991 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PULL);
992 return TRUE;
993 default:
994 TRACE("Unhandled query type: %s\n", GST_QUERY_TYPE_NAME(query));
995 return FALSE;
999 static gboolean activate_push(GstPad *pad, gboolean activate)
1001 GSTImpl *This = gst_pad_get_element_private(pad);
1003 TRACE("%p %p %u\n", This, pad, activate);
1005 EnterCriticalSection(&This->filter.csFilter);
1006 if (!activate) {
1007 TRACE("Deactivating\n");
1008 if (!This->initial)
1009 IAsyncReader_BeginFlush(This->pInputPin.pReader);
1010 if (This->push_thread) {
1011 WaitForSingleObject(This->push_thread, -1);
1012 CloseHandle(This->push_thread);
1013 This->push_thread = NULL;
1015 if (!This->initial)
1016 IAsyncReader_EndFlush(This->pInputPin.pReader);
1017 if (This->filter.state == State_Stopped)
1018 This->nextofs = This->start;
1019 } else if (!This->push_thread) {
1020 TRACE("Activating\n");
1021 if (This->initial)
1022 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
1023 else
1024 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
1026 LeaveCriticalSection(&This->filter.csFilter);
1027 return TRUE;
1030 static gboolean activate_mode(GstPad *pad, GstObject *parent, GstPadMode mode, gboolean activate)
1032 TRACE("%p %p 0x%x %u\n", pad, parent, mode, activate);
1033 switch (mode) {
1034 case GST_PAD_MODE_PULL:
1035 return TRUE;
1036 case GST_PAD_MODE_PUSH:
1037 return activate_push(pad, activate);
1038 default:
1039 return FALSE;
1041 return FALSE;
1044 static void no_more_pads(GstElement *decodebin, gpointer user)
1046 GSTImpl *This = (GSTImpl*)user;
1047 TRACE("%p %p\n", This, decodebin);
1048 SetEvent(This->event);
1051 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, gpointer user)
1053 const char *name = gst_element_factory_get_longname(fact);
1055 if (strstr(name, "Player protection")) {
1056 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
1057 return GST_AUTOPLUG_SELECT_SKIP;
1059 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder")) {
1060 WARN("Disabled video acceleration since it breaks in wine\n");
1061 return GST_AUTOPLUG_SELECT_SKIP;
1063 TRACE("using \"%s\"\n", name);
1064 return GST_AUTOPLUG_SELECT_TRY;
1067 static GstBusSyncReply watch_bus(GstBus *bus, GstMessage *msg, gpointer data)
1069 GSTImpl *This = data;
1070 GError *err = NULL;
1071 gchar *dbg_info = NULL;
1073 TRACE("%p %p %p\n", This, bus, msg);
1075 if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_ERROR) {
1076 gst_message_parse_error(msg, &err, &dbg_info);
1077 FIXME("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1078 WARN("%s\n", dbg_info);
1079 SetEvent(This->event);
1080 } else if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_WARNING) {
1081 gst_message_parse_warning(msg, &err, &dbg_info);
1082 WARN("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1083 WARN("%s\n", dbg_info);
1085 if (err)
1086 g_error_free(err);
1087 g_free(dbg_info);
1088 return GST_BUS_DROP;
1091 static void unknown_type(GstElement *bin, GstPad *pad, GstCaps *caps, gpointer user)
1093 gchar *strcaps = gst_caps_to_string(caps);
1094 FIXME("Could not find a filter for caps: %s\n", strcaps);
1095 g_free(strcaps);
1098 static HRESULT GST_Connect(GSTInPin *pPin, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
1100 GSTImpl *This = (GSTImpl*)pPin->pin.pinInfo.pFilter;
1101 HRESULT hr;
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 FIXME("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->event);
1157 gst_element_set_state(This->container, GST_STATE_PLAYING);
1158 WaitForSingleObject(This->event, -1);
1159 gst_element_get_state(This->container, NULL, NULL, -1);
1161 if (ret < 0) {
1162 WARN("Ret: %i\n", ret);
1163 hr = E_FAIL;
1164 } else if (!This->cStreams) {
1165 FIXME("GStreamer could not find any streams\n");
1166 hr = E_FAIL;
1167 } else {
1168 gst_pad_query_duration(This->ppPins[0]->their_src, GST_FORMAT_TIME, &duration);
1169 for (i = 0; i < This->cStreams; ++i) {
1170 This->ppPins[i]->seek.llDuration = This->ppPins[i]->seek.llStop = duration / 100;
1171 This->ppPins[i]->seek.llCurrent = 0;
1172 if (!This->ppPins[i]->seek.llDuration)
1173 This->ppPins[i]->seek.dwCapabilities = 0;
1174 WaitForSingleObject(This->ppPins[i]->caps_event, -1);
1176 hr = S_OK;
1178 *props = This->props;
1180 This->ignore_flush = TRUE;
1181 gst_element_set_state(This->container, GST_STATE_READY);
1182 gst_element_get_state(This->container, NULL, NULL, -1);
1183 This->ignore_flush = FALSE;
1185 This->initial = FALSE;
1187 /* don't set active during test-play, as we don't want to push/pull data
1188 * from the source yet */
1189 gst_pad_set_active(This->my_src, 1);
1191 This->nextofs = This->nextpullofs = 0;
1192 return hr;
1195 static inline GSTOutPin *impl_from_IMediaSeeking( IMediaSeeking *iface )
1197 return CONTAINING_RECORD(iface, GSTOutPin, seek.IMediaSeeking_iface);
1200 static IPin* WINAPI GST_GetPin(BaseFilter *iface, int pos)
1202 GSTImpl *This = (GSTImpl *)iface;
1203 IPin *pin;
1205 TRACE("%p: Asking for pos %x\n", This, pos);
1207 if (pos > This->cStreams || pos < 0)
1208 return NULL;
1210 if (!pos)
1211 pin = &This->pInputPin.pin.IPin_iface;
1212 else
1213 pin = &This->ppPins[pos - 1]->pin.pin.IPin_iface;
1215 IPin_AddRef(pin);
1216 return pin;
1219 static LONG WINAPI GST_GetPinCount(BaseFilter *iface)
1221 GSTImpl *This = (GSTImpl *)iface;
1222 TRACE("%p -> %u\n", This, This->cStreams + 1);
1223 return (This->cStreams + 1);
1226 static const BaseFilterFuncTable BaseFuncTable = {
1227 GST_GetPin,
1228 GST_GetPinCount
1231 IUnknown * CALLBACK Gstreamer_Splitter_create(IUnknown *pUnkOuter, HRESULT *phr)
1233 IUnknown *obj = NULL;
1234 PIN_INFO *piInput;
1235 GSTImpl *This;
1237 TRACE("%p %p\n", pUnkOuter, phr);
1239 if (!Gstreamer_init())
1241 *phr = E_FAIL;
1242 return NULL;
1245 mark_wine_thread();
1247 This = CoTaskMemAlloc(sizeof(*This));
1248 if (!This)
1250 *phr = E_OUTOFMEMORY;
1251 return NULL;
1254 obj = (IUnknown*)&This->filter.IBaseFilter_iface;
1255 BaseFilter_Init(&This->filter, &GST_Vtbl, &CLSID_Gstreamer_Splitter, (DWORD_PTR)(__FILE__ ": GSTImpl.csFilter"), &BaseFuncTable);
1257 This->cStreams = 0;
1258 This->ppPins = NULL;
1259 This->push_thread = NULL;
1260 This->event = CreateEventW(NULL, 0, 0, NULL);
1261 This->bus = NULL;
1263 piInput = &This->pInputPin.pin.pinInfo;
1264 piInput->dir = PINDIR_INPUT;
1265 piInput->pFilter = &This->filter.IBaseFilter_iface;
1266 lstrcpynW(piInput->achName, wcsInputPinName, sizeof(piInput->achName) / sizeof(piInput->achName[0]));
1267 This->pInputPin.pin.IPin_iface.lpVtbl = &GST_InputPin_Vtbl;
1268 This->pInputPin.pin.refCount = 1;
1269 This->pInputPin.pin.pConnectedTo = NULL;
1270 This->pInputPin.pin.pCritSec = &This->filter.csFilter;
1271 ZeroMemory(&This->pInputPin.pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1272 *phr = S_OK;
1274 TRACE("returning %p\n", obj);
1276 return obj;
1279 static void GST_Destroy(GSTImpl *This)
1281 IPin *connected = NULL;
1282 ULONG pinref;
1283 HRESULT hr;
1285 TRACE("Destroying %p\n", This);
1287 CloseHandle(This->event);
1289 /* Don't need to clean up output pins, disconnecting input pin will do that */
1290 IPin_ConnectedTo((IPin *)&This->pInputPin, &connected);
1291 if (connected) {
1292 hr = IPin_Disconnect(connected);
1293 assert(hr == S_OK);
1294 IPin_Release(connected);
1295 hr = IPin_Disconnect(&This->pInputPin.pin.IPin_iface);
1296 assert(hr == S_OK);
1298 pinref = IPin_Release(&This->pInputPin.pin.IPin_iface);
1299 if (pinref) {
1300 /* Valgrind could find this, if I kill it here */
1301 ERR("pinref should be null, is %u, destroying anyway\n", pinref);
1302 assert((LONG)pinref > 0);
1304 while (pinref)
1305 pinref = IPin_Release(&This->pInputPin.pin.IPin_iface);
1307 if (This->bus) {
1308 gst_bus_set_sync_handler(This->bus, NULL, NULL, NULL);
1309 gst_object_unref(This->bus);
1311 BaseFilter_Destroy(&This->filter);
1312 CoTaskMemFree(This);
1315 static HRESULT WINAPI GST_QueryInterface(IBaseFilter *iface, REFIID riid, LPVOID *ppv)
1317 GSTImpl *This = (GSTImpl *)iface;
1319 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1321 *ppv = NULL;
1323 if (IsEqualIID(riid, &IID_IUnknown) ||
1324 IsEqualIID(riid, &IID_IPersist) ||
1325 IsEqualIID(riid, &IID_IMediaFilter) ||
1326 IsEqualIID(riid, &IID_IBaseFilter))
1328 *ppv = &This->filter.IBaseFilter_iface;
1331 if (*ppv) {
1332 IUnknown_AddRef((IUnknown *)(*ppv));
1333 return S_OK;
1336 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
1337 !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
1338 FIXME("No interface for %s!\n", debugstr_guid(riid));
1340 return E_NOINTERFACE;
1343 static ULONG WINAPI GST_Release(IBaseFilter *iface)
1345 GSTImpl *This = (GSTImpl *)iface;
1346 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
1348 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1350 if (!refCount)
1351 GST_Destroy(This);
1353 return refCount;
1356 static HRESULT WINAPI GST_Stop(IBaseFilter *iface)
1358 GSTImpl *This = (GSTImpl *)iface;
1360 TRACE("(%p)\n", This);
1362 mark_wine_thread();
1364 if (This->container) {
1365 This->ignore_flush = TRUE;
1366 gst_element_set_state(This->container, GST_STATE_READY);
1367 gst_element_get_state(This->container, NULL, NULL, -1);
1368 This->ignore_flush = FALSE;
1370 return S_OK;
1373 static HRESULT WINAPI GST_Pause(IBaseFilter *iface)
1375 HRESULT hr = S_OK;
1376 GSTImpl *This = (GSTImpl *)iface;
1377 GstState now;
1378 GstStateChangeReturn ret;
1380 TRACE("(%p)\n", This);
1382 if (!This->container)
1383 return VFW_E_NOT_CONNECTED;
1385 mark_wine_thread();
1387 gst_element_get_state(This->container, &now, NULL, -1);
1388 if (now == GST_STATE_PAUSED)
1389 return S_OK;
1390 if (now != GST_STATE_PLAYING)
1391 hr = IBaseFilter_Run(iface, -1);
1392 if (FAILED(hr))
1393 return hr;
1394 ret = gst_element_set_state(This->container, GST_STATE_PAUSED);
1395 if (ret == GST_STATE_CHANGE_ASYNC)
1396 hr = S_FALSE;
1397 return hr;
1400 static HRESULT WINAPI GST_Run(IBaseFilter *iface, REFERENCE_TIME tStart)
1402 HRESULT hr = S_OK;
1403 GSTImpl *This = (GSTImpl *)iface;
1404 ULONG i;
1405 GstState now;
1406 HRESULT hr_any = VFW_E_NOT_CONNECTED;
1408 TRACE("(%p)->(%s)\n", This, wine_dbgstr_longlong(tStart));
1410 mark_wine_thread();
1412 if (!This->container)
1413 return VFW_E_NOT_CONNECTED;
1415 EnterCriticalSection(&This->filter.csFilter);
1416 This->filter.rtStreamStart = tStart;
1417 LeaveCriticalSection(&This->filter.csFilter);
1419 gst_element_get_state(This->container, &now, NULL, -1);
1420 if (now == GST_STATE_PLAYING)
1421 return S_OK;
1422 if (now == GST_STATE_PAUSED) {
1423 GstStateChangeReturn ret;
1424 ret = gst_element_set_state(This->container, GST_STATE_PLAYING);
1425 if (ret == GST_STATE_CHANGE_ASYNC)
1426 return S_FALSE;
1427 return S_OK;
1430 EnterCriticalSection(&This->filter.csFilter);
1431 gst_element_set_state(This->container, GST_STATE_PLAYING);
1432 This->filter.rtStreamStart = tStart;
1434 for (i = 0; i < This->cStreams; i++) {
1435 hr = BaseOutputPinImpl_Active(&This->ppPins[i]->pin);
1436 if (SUCCEEDED(hr)) {
1437 hr_any = hr;
1440 hr = hr_any;
1441 LeaveCriticalSection(&This->filter.csFilter);
1443 return hr;
1446 static HRESULT WINAPI GST_GetState(IBaseFilter *iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
1448 GSTImpl *This = (GSTImpl *)iface;
1449 HRESULT hr = S_OK;
1450 GstState now, pending;
1451 GstStateChangeReturn ret;
1453 TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
1455 mark_wine_thread();
1457 if (!This->container) {
1458 *pState = State_Stopped;
1459 return S_OK;
1462 ret = gst_element_get_state(This->container, &now, &pending, dwMilliSecsTimeout == INFINITE ? -1 : dwMilliSecsTimeout * 1000);
1464 if (ret == GST_STATE_CHANGE_ASYNC)
1465 hr = VFW_S_STATE_INTERMEDIATE;
1466 else
1467 pending = now;
1469 switch (pending) {
1470 case GST_STATE_PAUSED: *pState = State_Paused; return hr;
1471 case GST_STATE_PLAYING: *pState = State_Running; return hr;
1472 default: *pState = State_Stopped; return hr;
1476 static HRESULT WINAPI GST_FindPin(IBaseFilter *iface, LPCWSTR Id, IPin **ppPin)
1478 FIXME("(%p)->(%s,%p) stub\n", iface, debugstr_w(Id), ppPin);
1479 return E_NOTIMPL;
1482 static const IBaseFilterVtbl GST_Vtbl = {
1483 GST_QueryInterface,
1484 BaseFilterImpl_AddRef,
1485 GST_Release,
1486 BaseFilterImpl_GetClassID,
1487 GST_Stop,
1488 GST_Pause,
1489 GST_Run,
1490 GST_GetState,
1491 BaseFilterImpl_SetSyncSource,
1492 BaseFilterImpl_GetSyncSource,
1493 BaseFilterImpl_EnumPins,
1494 GST_FindPin,
1495 BaseFilterImpl_QueryFilterInfo,
1496 BaseFilterImpl_JoinFilterGraph,
1497 BaseFilterImpl_QueryVendorInfo
1500 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1502 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1503 TRACE("(%p)\n", This);
1504 return S_OK;
1507 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1509 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1510 TRACE("(%p)\n", This);
1511 return S_OK;
1514 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1516 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1517 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1518 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1519 mark_wine_thread();
1520 gst_pad_push_event(This->my_sink, ev);
1521 return S_OK;
1524 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1526 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1527 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1530 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1532 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1533 return IPin_AddRef(&This->pin.pin.IPin_iface);
1536 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1538 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1539 return IPin_Release(&This->pin.pin.IPin_iface);
1542 static HRESULT WINAPI GST_Seeking_GetCurrentPosition(IMediaSeeking *iface, REFERENCE_TIME *pos)
1544 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1546 TRACE("(%p)->(%p)\n", This, pos);
1548 if (!pos)
1549 return E_POINTER;
1551 mark_wine_thread();
1553 if (!This->their_src) {
1554 *pos = This->seek.llCurrent;
1555 TRACE("Cached value\n");
1556 if (This->seek.llDuration)
1557 return S_OK;
1558 else
1559 return E_NOTIMPL;
1562 if (!gst_pad_query_position(This->their_src, GST_FORMAT_TIME, pos)) {
1563 WARN("Could not query position\n");
1564 return E_NOTIMPL;
1566 *pos /= 100;
1567 This->seek.llCurrent = *pos;
1568 return S_OK;
1571 static GstSeekType type_from_flags(DWORD flags)
1573 switch (flags & AM_SEEKING_PositioningBitsMask) {
1574 case AM_SEEKING_NoPositioning:
1575 return GST_SEEK_TYPE_NONE;
1576 case AM_SEEKING_AbsolutePositioning:
1577 case AM_SEEKING_RelativePositioning:
1578 return GST_SEEK_TYPE_SET;
1579 case AM_SEEKING_IncrementalPositioning:
1580 return GST_SEEK_TYPE_END;
1582 return GST_SEEK_TYPE_NONE;
1585 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1586 REFERENCE_TIME *pCur, DWORD curflags, REFERENCE_TIME *pStop,
1587 DWORD stopflags)
1589 HRESULT hr;
1590 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1591 GstSeekFlags f = 0;
1592 GstSeekType curtype, stoptype;
1593 GstEvent *e;
1594 gint64 stop_pos = 0, curr_pos = 0;
1596 TRACE("(%p)->(%p, 0x%x, %p, 0x%x)\n", This, pCur, curflags, pStop, stopflags);
1598 mark_wine_thread();
1600 if (!This->seek.llDuration)
1601 return E_NOTIMPL;
1603 hr = SourceSeekingImpl_SetPositions(iface, pCur, curflags, pStop, stopflags);
1604 if (!This->their_src)
1605 return hr;
1607 curtype = type_from_flags(curflags);
1608 stoptype = type_from_flags(stopflags);
1609 if (curflags & AM_SEEKING_SeekToKeyFrame)
1610 f |= GST_SEEK_FLAG_KEY_UNIT;
1611 if (curflags & AM_SEEKING_Segment)
1612 f |= GST_SEEK_FLAG_SEGMENT;
1613 if (!(curflags & AM_SEEKING_NoFlush))
1614 f |= GST_SEEK_FLAG_FLUSH;
1616 if (((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning) ||
1617 ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)) {
1618 gint64 tmp_pos;
1619 gst_pad_query_position (This->my_sink, GST_FORMAT_TIME, &tmp_pos);
1620 if ((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1621 curr_pos = tmp_pos;
1622 if ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1623 stop_pos = tmp_pos;
1626 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);
1627 if (gst_pad_push_event(This->my_sink, e))
1628 return S_OK;
1629 else
1630 return E_NOTIMPL;
1633 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1635 GST_Seeking_QueryInterface,
1636 GST_Seeking_AddRef,
1637 GST_Seeking_Release,
1638 SourceSeekingImpl_GetCapabilities,
1639 SourceSeekingImpl_CheckCapabilities,
1640 SourceSeekingImpl_IsFormatSupported,
1641 SourceSeekingImpl_QueryPreferredFormat,
1642 SourceSeekingImpl_GetTimeFormat,
1643 SourceSeekingImpl_IsUsingTimeFormat,
1644 SourceSeekingImpl_SetTimeFormat,
1645 SourceSeekingImpl_GetDuration,
1646 SourceSeekingImpl_GetStopPosition,
1647 GST_Seeking_GetCurrentPosition,
1648 SourceSeekingImpl_ConvertTimeFormat,
1649 GST_Seeking_SetPositions,
1650 SourceSeekingImpl_GetPositions,
1651 SourceSeekingImpl_GetAvailable,
1652 SourceSeekingImpl_SetRate,
1653 SourceSeekingImpl_GetRate,
1654 SourceSeekingImpl_GetPreroll
1657 static inline GSTOutPin *impl_from_IQualityControl( IQualityControl *iface )
1659 return CONTAINING_RECORD(iface, GSTOutPin, IQualityControl_iface);
1662 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1664 GSTOutPin *pin = impl_from_IQualityControl(iface);
1665 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1668 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1670 GSTOutPin *pin = impl_from_IQualityControl(iface);
1671 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1674 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1676 GSTOutPin *pin = impl_from_IQualityControl(iface);
1677 return IPin_Release(&pin->pin.pin.IPin_iface);
1680 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
1682 GSTOutPin *pin = impl_from_IQualityControl(iface);
1683 GstEvent *evt;
1685 TRACE("(%p)->(%p, { 0x%x %u %s %s })\n", pin, sender,
1686 qm.Type, qm.Proportion,
1687 wine_dbgstr_longlong(qm.Late),
1688 wine_dbgstr_longlong(qm.TimeStamp));
1690 mark_wine_thread();
1692 if (qm.Type == Flood)
1693 qm.Late = 0;
1695 evt = gst_event_new_qos(qm.Type == Famine ? GST_QOS_TYPE_UNDERFLOW : GST_QOS_TYPE_OVERFLOW,
1696 qm.Proportion / 1000., qm.Late * 100, qm.TimeStamp * 100);
1698 if (!evt) {
1699 WARN("Failed to create QOS event\n");
1700 return E_INVALIDARG;
1703 gst_pad_push_event(pin->my_sink, evt);
1705 return S_OK;
1708 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1710 GSTOutPin *pin = impl_from_IQualityControl(iface);
1711 TRACE("(%p)->(%p)\n", pin, pin);
1712 /* Do nothing */
1713 return S_OK;
1716 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1717 GST_QualityControl_QueryInterface,
1718 GST_QualityControl_AddRef,
1719 GST_QualityControl_Release,
1720 GST_QualityControl_Notify,
1721 GST_QualityControl_SetSink
1724 static HRESULT WINAPI GSTOutPin_QueryInterface(IPin *iface, REFIID riid, void **ppv)
1726 GSTOutPin *This = (GSTOutPin *)iface;
1728 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1730 *ppv = NULL;
1732 if (IsEqualIID(riid, &IID_IUnknown))
1733 *ppv = iface;
1734 else if (IsEqualIID(riid, &IID_IPin))
1735 *ppv = iface;
1736 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1737 *ppv = &This->seek;
1738 else if (IsEqualIID(riid, &IID_IQualityControl))
1739 *ppv = &This->IQualityControl_iface;
1741 if (*ppv) {
1742 IUnknown_AddRef((IUnknown *)(*ppv));
1743 return S_OK;
1745 FIXME("No interface for %s!\n", debugstr_guid(riid));
1746 return E_NOINTERFACE;
1749 static ULONG WINAPI GSTOutPin_Release(IPin *iface)
1751 GSTOutPin *This = (GSTOutPin *)iface;
1752 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
1754 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1756 mark_wine_thread();
1758 if (!refCount) {
1759 if (This->their_src) {
1760 if (This->flipfilter) {
1761 gst_pad_unlink(This->their_src, This->flip_sink);
1762 gst_pad_unlink(This->flip_src, This->my_sink);
1763 gst_object_unref(This->flip_src);
1764 gst_object_unref(This->flip_sink);
1765 This->flipfilter = NULL;
1766 This->flip_src = This->flip_sink = NULL;
1767 } else
1768 gst_pad_unlink(This->their_src, This->my_sink);
1769 gst_object_unref(This->their_src);
1771 gst_object_unref(This->my_sink);
1772 CloseHandle(This->caps_event);
1773 DeleteMediaType(This->pmt);
1774 FreeMediaType(&This->pin.pin.mtCurrent);
1775 gst_segment_free(This->segment);
1776 if(This->gstpool)
1777 gst_object_unref(This->gstpool);
1778 if (This->pin.pAllocator)
1779 IMemAllocator_Release(This->pin.pAllocator);
1780 CoTaskMemFree(This);
1781 return 0;
1783 return refCount;
1786 static HRESULT WINAPI GSTOutPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
1788 GSTOutPin *This = (GSTOutPin *)iface;
1790 TRACE("(%p)->(%i, %p)\n", This, iPosition, pmt);
1792 if (iPosition < 0)
1793 return E_INVALIDARG;
1795 if (iPosition > 0)
1796 return VFW_S_NO_MORE_ITEMS;
1798 CopyMediaType(pmt, This->pmt);
1800 return S_OK;
1803 static HRESULT WINAPI GSTOutPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
1805 GSTOutPin *This = (GSTOutPin *)iface;
1806 TRACE("(%p)->(%p, %p)\n", This, pAlloc, ppropInputRequest);
1807 /* Unused */
1808 return S_OK;
1811 static HRESULT WINAPI GSTOutPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
1813 HRESULT hr;
1814 GSTOutPin *This = (GSTOutPin *)iface;
1815 GSTImpl *GSTfilter = (GSTImpl*)This->pin.pin.pinInfo.pFilter;
1817 TRACE("(%p)->(%p, %p)\n", This, pPin, pAlloc);
1819 *pAlloc = NULL;
1820 if (GSTfilter->pInputPin.pAlloc)
1822 hr = IMemInputPin_NotifyAllocator(pPin, GSTfilter->pInputPin.pAlloc, FALSE);
1823 if (SUCCEEDED(hr))
1825 *pAlloc = GSTfilter->pInputPin.pAlloc;
1826 IMemAllocator_AddRef(*pAlloc);
1829 else
1830 hr = VFW_E_NO_ALLOCATOR;
1832 return hr;
1835 static HRESULT WINAPI GSTOutPin_BreakConnect(BaseOutputPin *This)
1837 HRESULT hr;
1839 TRACE("(%p)->()\n", This);
1841 EnterCriticalSection(This->pin.pCritSec);
1842 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1843 hr = VFW_E_NOT_CONNECTED;
1844 else
1846 hr = IPin_Disconnect(This->pin.pConnectedTo);
1847 IPin_Disconnect((IPin *)This);
1849 LeaveCriticalSection(This->pin.pCritSec);
1851 return hr;
1854 static const IPinVtbl GST_OutputPin_Vtbl = {
1855 GSTOutPin_QueryInterface,
1856 BasePinImpl_AddRef,
1857 GSTOutPin_Release,
1858 BaseOutputPinImpl_Connect,
1859 BaseOutputPinImpl_ReceiveConnection,
1860 BaseOutputPinImpl_Disconnect,
1861 BasePinImpl_ConnectedTo,
1862 BasePinImpl_ConnectionMediaType,
1863 BasePinImpl_QueryPinInfo,
1864 BasePinImpl_QueryDirection,
1865 BasePinImpl_QueryId,
1866 GST_OutPin_QueryAccept,
1867 BasePinImpl_EnumMediaTypes,
1868 BasePinImpl_QueryInternalConnections,
1869 BaseOutputPinImpl_EndOfStream,
1870 BaseOutputPinImpl_BeginFlush,
1871 BaseOutputPinImpl_EndFlush,
1872 BasePinImpl_NewSegment
1875 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
1877 NULL,
1878 BaseOutputPinImpl_AttemptConnection,
1879 BasePinImpl_GetMediaTypeVersion,
1880 GSTOutPin_GetMediaType
1882 GSTOutPin_DecideBufferSize,
1883 GSTOutPin_DecideAllocator,
1884 GSTOutPin_BreakConnect
1887 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt)
1889 HRESULT hr;
1890 This->ppPins = CoTaskMemRealloc(This->ppPins, (This->cStreams + 1) * sizeof(IPin *));
1892 hr = BaseOutputPin_Construct(&GST_OutputPin_Vtbl, sizeof(GSTOutPin), piOutput, &output_BaseOutputFuncTable, &This->filter.csFilter, (IPin**)(This->ppPins + This->cStreams));
1893 if (SUCCEEDED(hr)) {
1894 GSTOutPin *pin = This->ppPins[This->cStreams];
1895 memset((char*)pin + sizeof(pin->pin), 0, sizeof(GSTOutPin) - sizeof(pin->pin));
1896 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1897 CopyMediaType(pin->pmt, amt);
1898 pin->pin.pin.pinInfo.pFilter = &This->filter.IBaseFilter_iface;
1899 pin->caps_event = CreateEventW(NULL, 0, 0, NULL);
1900 pin->segment = gst_segment_new();
1901 This->cStreams++;
1902 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1903 SourceSeeking_Init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop, GST_ChangeCurrent, GST_ChangeRate, &This->filter.csFilter);
1904 BaseFilterImpl_IncrementPinVersion(&This->filter);
1905 } else
1906 ERR("Failed with error %x\n", hr);
1907 return hr;
1910 static HRESULT GST_RemoveOutputPins(GSTImpl *This)
1912 HRESULT hr;
1913 ULONG i;
1914 GSTOutPin **ppOldPins = This->ppPins;
1916 TRACE("(%p)\n", This);
1917 mark_wine_thread();
1919 if (!This->container)
1920 return S_OK;
1921 gst_element_set_state(This->container, GST_STATE_NULL);
1922 gst_pad_unlink(This->my_src, This->their_sink);
1923 gst_object_unref(This->my_src);
1924 gst_object_unref(This->their_sink);
1925 This->my_src = This->their_sink = NULL;
1927 for (i = 0; i < This->cStreams; i++) {
1928 hr = BaseOutputPinImpl_BreakConnect(&ppOldPins[i]->pin);
1929 TRACE("Disconnect: %08x\n", hr);
1930 IPin_Release(&ppOldPins[i]->pin.pin.IPin_iface);
1932 This->cStreams = 0;
1933 This->ppPins = NULL;
1934 gst_element_set_bus(This->container, NULL);
1935 gst_object_unref(This->container);
1936 This->container = NULL;
1937 BaseFilterImpl_IncrementPinVersion(&This->filter);
1938 CoTaskMemFree(ppOldPins);
1939 return S_OK;
1942 static ULONG WINAPI GSTInPin_Release(IPin *iface)
1944 GSTInPin *This = (GSTInPin*)iface;
1945 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1947 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
1948 if (!refCount) {
1949 FreeMediaType(&This->pin.mtCurrent);
1950 if (This->pAlloc)
1951 IMemAllocator_Release(This->pAlloc);
1952 This->pAlloc = NULL;
1953 if (This->pReader)
1954 IAsyncReader_Release(This->pReader);
1955 This->pReader = NULL;
1956 This->pin.IPin_iface.lpVtbl = NULL;
1957 return 0;
1958 } else
1959 return refCount;
1962 static HRESULT WINAPI GSTInPin_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
1964 PIN_DIRECTION pindirReceive;
1965 HRESULT hr = S_OK;
1966 GSTInPin *This = (GSTInPin*)iface;
1968 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1969 dump_AM_MEDIA_TYPE(pmt);
1971 mark_wine_thread();
1973 EnterCriticalSection(This->pin.pCritSec);
1974 if (!This->pin.pConnectedTo) {
1975 ALLOCATOR_PROPERTIES props;
1976 IMemAllocator *pAlloc = NULL;
1978 props.cBuffers = 8;
1979 props.cbBuffer = 16384;
1980 props.cbAlign = 1;
1981 props.cbPrefix = 0;
1983 if (IPin_QueryAccept(iface, pmt) != S_OK)
1984 hr = VFW_E_TYPE_NOT_ACCEPTED;
1986 if (SUCCEEDED(hr)) {
1987 IPin_QueryDirection(pReceivePin, &pindirReceive);
1988 if (pindirReceive != PINDIR_OUTPUT) {
1989 ERR("Can't connect from non-output pin\n");
1990 hr = VFW_E_INVALID_DIRECTION;
1994 This->pReader = NULL;
1995 This->pAlloc = NULL;
1996 if (SUCCEEDED(hr))
1997 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1998 if (SUCCEEDED(hr))
1999 hr = GST_Connect(This, pReceivePin, &props);
2001 /* A certain IAsyncReader::RequestAllocator expects to be passed
2002 non-NULL preferred allocator */
2003 if (SUCCEEDED(hr))
2004 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC,
2005 &IID_IMemAllocator, (LPVOID *)&pAlloc);
2006 if (SUCCEEDED(hr)) {
2007 hr = IAsyncReader_RequestAllocator(This->pReader, pAlloc, &props, &This->pAlloc);
2008 if (FAILED(hr))
2009 WARN("Can't get an allocator, got %08x\n", hr);
2011 if (pAlloc)
2012 IMemAllocator_Release(pAlloc);
2013 if (SUCCEEDED(hr)) {
2014 CopyMediaType(&This->pin.mtCurrent, pmt);
2015 This->pin.pConnectedTo = pReceivePin;
2016 IPin_AddRef(pReceivePin);
2017 hr = IMemAllocator_Commit(This->pAlloc);
2018 SetEvent(((GSTImpl*)This->pin.pinInfo.pFilter)->event);
2019 } else {
2020 GST_RemoveOutputPins((GSTImpl *)This->pin.pinInfo.pFilter);
2021 if (This->pReader)
2022 IAsyncReader_Release(This->pReader);
2023 This->pReader = NULL;
2024 if (This->pAlloc)
2025 IMemAllocator_Release(This->pAlloc);
2026 This->pAlloc = NULL;
2028 TRACE("Size: %i\n", props.cbBuffer);
2029 } else
2030 hr = VFW_E_ALREADY_CONNECTED;
2031 LeaveCriticalSection(This->pin.pCritSec);
2032 return hr;
2035 static HRESULT WINAPI GSTInPin_Disconnect(IPin *iface)
2037 HRESULT hr;
2038 GSTInPin *This = (GSTInPin*)iface;
2039 FILTER_STATE state;
2041 TRACE("(%p)\n", This);
2043 mark_wine_thread();
2045 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
2046 EnterCriticalSection(This->pin.pCritSec);
2047 if (This->pin.pConnectedTo) {
2048 GSTImpl *Parser = (GSTImpl *)This->pin.pinInfo.pFilter;
2050 if (SUCCEEDED(hr) && state == State_Stopped) {
2051 IMemAllocator_Decommit(This->pAlloc);
2052 IPin_Disconnect(This->pin.pConnectedTo);
2053 IPin_Release(This->pin.pConnectedTo);
2054 This->pin.pConnectedTo = NULL;
2055 hr = GST_RemoveOutputPins(Parser);
2056 } else
2057 hr = VFW_E_NOT_STOPPED;
2058 } else
2059 hr = S_FALSE;
2060 LeaveCriticalSection(This->pin.pCritSec);
2061 return hr;
2064 static HRESULT WINAPI GSTInPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
2066 GSTInPin *This = (GSTInPin*)iface;
2068 TRACE("(%p)->(%p)\n", This, pmt);
2069 dump_AM_MEDIA_TYPE(pmt);
2071 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
2072 return S_OK;
2073 return S_FALSE;
2076 static HRESULT WINAPI GSTInPin_EndOfStream(IPin *iface)
2078 GSTInPin *pin = (GSTInPin*)iface;
2079 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2081 FIXME("Propagate message on %p\n", This);
2082 return S_OK;
2085 static HRESULT WINAPI GSTInPin_BeginFlush(IPin *iface)
2087 GSTInPin *pin = (GSTInPin*)iface;
2088 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2090 FIXME("Propagate message on %p\n", This);
2091 return S_OK;
2094 static HRESULT WINAPI GSTInPin_EndFlush(IPin *iface)
2096 GSTInPin *pin = (GSTInPin*)iface;
2097 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2099 FIXME("Propagate message on %p\n", This);
2100 return S_OK;
2103 static HRESULT WINAPI GSTInPin_NewSegment(IPin *iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
2105 GSTInPin *pin = (GSTInPin*)iface;
2106 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2108 BasePinImpl_NewSegment(iface, tStart, tStop, dRate);
2109 FIXME("Propagate message on %p\n", This);
2110 return S_OK;
2113 static HRESULT WINAPI GSTInPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
2115 GSTInPin *This = (GSTInPin*)iface;
2117 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
2119 *ppv = NULL;
2121 if (IsEqualIID(riid, &IID_IUnknown))
2122 *ppv = iface;
2123 else if (IsEqualIID(riid, &IID_IPin))
2124 *ppv = iface;
2125 else if (IsEqualIID(riid, &IID_IMediaSeeking))
2127 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
2130 if (*ppv)
2132 IUnknown_AddRef((IUnknown *)(*ppv));
2133 return S_OK;
2136 FIXME("No interface for %s!\n", debugstr_guid(riid));
2138 return E_NOINTERFACE;
2141 static HRESULT WINAPI GSTInPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
2143 BasePin *This = (BasePin *)iface;
2145 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
2147 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
2150 static const IPinVtbl GST_InputPin_Vtbl = {
2151 GSTInPin_QueryInterface,
2152 BasePinImpl_AddRef,
2153 GSTInPin_Release,
2154 BaseInputPinImpl_Connect,
2155 GSTInPin_ReceiveConnection,
2156 GSTInPin_Disconnect,
2157 BasePinImpl_ConnectedTo,
2158 BasePinImpl_ConnectionMediaType,
2159 BasePinImpl_QueryPinInfo,
2160 BasePinImpl_QueryDirection,
2161 BasePinImpl_QueryId,
2162 GSTInPin_QueryAccept,
2163 GSTInPin_EnumMediaTypes,
2164 BasePinImpl_QueryInternalConnections,
2165 GSTInPin_EndOfStream,
2166 GSTInPin_BeginFlush,
2167 GSTInPin_EndFlush,
2168 GSTInPin_NewSegment
2171 pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
2172 pthread_cond_t cb_list_cond = PTHREAD_COND_INITIALIZER;
2173 struct list cb_list = LIST_INIT(cb_list);
2175 void CALLBACK perform_cb(TP_CALLBACK_INSTANCE *instance, void *user)
2177 struct cb_data *cbdata = user;
2179 TRACE("got cb type: 0x%x\n", cbdata->type);
2181 switch(cbdata->type)
2183 case WATCH_BUS:
2185 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2186 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2187 break;
2189 case EXISTING_NEW_PAD:
2191 struct existing_new_pad_data *data = &cbdata->u.existing_new_pad_data;
2192 existing_new_pad(data->bin, data->pad, data->user);
2193 break;
2195 case QUERY_FUNCTION:
2197 struct query_function_data *data = &cbdata->u.query_function_data;
2198 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2199 break;
2201 case ACTIVATE_MODE:
2203 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2204 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2205 break;
2207 case NO_MORE_PADS:
2209 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2210 no_more_pads(data->decodebin, data->user);
2211 break;
2213 case REQUEST_BUFFER_SRC:
2215 struct request_buffer_src_data *data = &cbdata->u.request_buffer_src_data;
2216 cbdata->u.request_buffer_src_data.ret = request_buffer_src(data->pad, data->parent,
2217 data->ofs, data->len, data->buf);
2218 break;
2220 case EVENT_SRC:
2222 struct event_src_data *data = &cbdata->u.event_src_data;
2223 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2224 break;
2226 case EVENT_SINK:
2228 struct event_sink_data *data = &cbdata->u.event_sink_data;
2229 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2230 break;
2232 case ACCEPT_CAPS_SINK:
2234 struct accept_caps_sink_data *data = &cbdata->u.accept_caps_sink_data;
2235 cbdata->u.accept_caps_sink_data.ret = accept_caps_sink(data->pad, data->caps);
2236 break;
2238 case SETCAPS_SINK:
2240 struct setcaps_sink_data *data = &cbdata->u.setcaps_sink_data;
2241 cbdata->u.setcaps_sink_data.ret = setcaps_sink(data->pad, data->caps);
2242 break;
2244 case GOT_DATA_SINK:
2246 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2247 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2248 break;
2250 case GOT_DATA:
2252 struct got_data_data *data = &cbdata->u.got_data_data;
2253 cbdata->u.got_data_data.ret = got_data(data->pad, data->parent, data->buf);
2254 break;
2256 case REMOVED_DECODED_PAD:
2258 struct removed_decoded_pad_data *data = &cbdata->u.removed_decoded_pad_data;
2259 removed_decoded_pad(data->bin, data->pad, data->user);
2260 break;
2262 case AUTOPLUG_BLACKLIST:
2264 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2265 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2266 data->pad, data->caps, data->fact, data->user);
2267 break;
2269 case UNKNOWN_TYPE:
2271 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2272 unknown_type(data->bin, data->pad, data->caps, data->user);
2273 break;
2275 case RELEASE_SAMPLE:
2277 struct release_sample_data *data = &cbdata->u.release_sample_data;
2278 release_sample(data->data);
2279 break;
2281 case TRANSFORM_PAD_ADDED:
2283 struct transform_pad_added_data *data = &cbdata->u.transform_pad_added_data;
2284 Gstreamer_transform_pad_added(data->filter, data->pad, data->user);
2285 break;
2287 case QUERY_SINK:
2289 struct query_sink_data *data = &cbdata->u.query_sink_data;
2290 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2291 data->query);
2292 break;
2296 pthread_mutex_lock(&cbdata->lock);
2297 cbdata->finished = 1;
2298 pthread_cond_broadcast(&cbdata->cond);
2299 pthread_mutex_unlock(&cbdata->lock);
2302 static DWORD WINAPI dispatch_thread(void *user)
2304 struct cb_data *cbdata;
2306 pthread_mutex_lock(&cb_list_lock);
2308 while(1){
2309 pthread_cond_wait(&cb_list_cond, &cb_list_lock);
2311 while(!list_empty(&cb_list)){
2312 cbdata = LIST_ENTRY(list_head(&cb_list), struct cb_data, entry);
2313 list_remove(&cbdata->entry);
2315 TrySubmitThreadpoolCallback(&perform_cb, cbdata, NULL);
2319 pthread_mutex_unlock(&cb_list_lock);
2321 return 0;
2324 void start_dispatch_thread(void)
2326 pthread_key_create(&wine_gst_key, NULL);
2327 CloseHandle(CreateThread(NULL, 0, &dispatch_thread, NULL, 0, NULL));