winegstreamer: Always pass non-NULL preferred allocator to IAsyncReader::RequestAlloc...
[wine.git] / dlls / winegstreamer / gstdemux.c
bloba6a41ab7537f538d234186cfb2bd017b2e98bdbc
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 = 0, bpp = 0;
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 = CoTaskMemAlloc(sizeof(*vih));
183 BITMAPINFOHEADER *bih = &vih->bmiHeader;
184 gint32 width = 0, height = 0, nom = 0, denom = 0;
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 amt->formattype = FORMAT_VideoInfo;
195 amt->pbFormat = (BYTE*)vih;
196 amt->cbFormat = sizeof(*vih);
197 amt->bFixedSizeSamples = amt->bTemporalCompression = 1;
198 amt->lSampleSize = 0;
199 amt->pUnk = NULL;
200 ZeroMemory(vih, sizeof(*vih));
201 amt->majortype = MEDIATYPE_Video;
202 if (GST_VIDEO_INFO_IS_RGB(&vinfo)) {
203 bih->biBitCount = GST_VIDEO_FORMAT_INFO_BITS(vinfo.finfo);
204 switch (bih->biBitCount) {
205 case 16: amt->subtype = MEDIASUBTYPE_RGB555; break;
206 case 24: amt->subtype = MEDIASUBTYPE_RGB24; break;
207 case 32: amt->subtype = MEDIASUBTYPE_RGB32; break;
208 default:
209 FIXME("Unknown bpp %u\n", bih->biBitCount);
210 return FALSE;
212 bih->biCompression = BI_RGB;
213 } else {
214 amt->subtype = MEDIATYPE_Video;
215 if (!(amt->subtype.Data1 = gst_video_format_to_fourcc(vinfo.finfo->format)))
216 return FALSE;
217 switch (amt->subtype.Data1) {
218 case mmioFOURCC('I','4','2','0'):
219 case mmioFOURCC('Y','V','1','2'):
220 case mmioFOURCC('N','V','1','2'):
221 case mmioFOURCC('N','V','2','1'):
222 bih->biBitCount = 12; break;
223 case mmioFOURCC('Y','U','Y','2'):
224 case mmioFOURCC('Y','V','Y','U'):
225 bih->biBitCount = 16; break;
227 bih->biCompression = amt->subtype.Data1;
229 bih->biSizeImage = width * height * bih->biBitCount / 8;
230 vih->AvgTimePerFrame = 10000000;
231 vih->AvgTimePerFrame *= denom;
232 vih->AvgTimePerFrame /= nom;
233 vih->rcSource.left = 0;
234 vih->rcSource.right = width;
235 vih->rcSource.top = height;
236 vih->rcSource.bottom = 0;
237 vih->rcTarget = vih->rcSource;
238 bih->biSize = sizeof(*bih);
239 bih->biWidth = width;
240 bih->biHeight = height;
241 bih->biPlanes = 1;
242 return TRUE;
245 static gboolean accept_caps_sink(GstPad *pad, GstCaps *caps)
247 GSTOutPin *pin = gst_pad_get_element_private(pad);
248 AM_MEDIA_TYPE amt;
249 GstStructure *arg;
250 const char *typename;
251 gboolean ret;
253 TRACE("%p %p\n", pad, caps);
255 arg = gst_caps_get_structure(caps, 0);
256 typename = gst_structure_get_name(arg);
257 if (!strcmp(typename, "audio/x-raw")) {
258 if (!pin->isaud) {
259 ERR("Setting audio caps on non-audio pad?\n");
260 return FALSE;
262 ret = amt_from_gst_caps_audio(caps, &amt);
263 FreeMediaType(&amt);
264 TRACE("+%i\n", ret);
265 return ret;
266 } else if (!strcmp(typename, "video/x-raw")) {
267 if (!pin->isvid) {
268 ERR("Setting video caps on non-video pad?\n");
269 return FALSE;
271 ret = amt_from_gst_caps_video(caps, &amt);
272 FreeMediaType(&amt);
273 TRACE("-%i\n", ret);
274 return ret;
275 } else {
276 FIXME("Unhandled type \"%s\"\n", typename);
277 return FALSE;
281 static gboolean setcaps_sink(GstPad *pad, GstCaps *caps)
283 GSTOutPin *pin = gst_pad_get_element_private(pad);
284 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
285 AM_MEDIA_TYPE amt;
286 GstStructure *arg;
287 const char *typename;
288 gboolean ret;
290 TRACE("%p %p\n", pad, caps);
292 arg = gst_caps_get_structure(caps, 0);
293 typename = gst_structure_get_name(arg);
294 if (!strcmp(typename, "audio/x-raw")) {
295 if (!pin->isaud) {
296 ERR("Setting audio caps on non-audio pad?\n");
297 return FALSE;
299 ret = amt_from_gst_caps_audio(caps, &amt);
300 } else if (!strcmp(typename, "video/x-raw")) {
301 if (!pin->isvid) {
302 ERR("Setting video caps on non-video pad?\n");
303 return FALSE;
305 ret = amt_from_gst_caps_video(caps, &amt);
306 if (ret)
307 This->props.cbBuffer = max(This->props.cbBuffer, ((VIDEOINFOHEADER*)amt.pbFormat)->bmiHeader.biSizeImage);
308 } else {
309 FIXME("Unhandled type \"%s\"\n", typename);
310 return FALSE;
312 TRACE("Linking returned %i for %s\n", ret, typename);
313 if (!ret)
314 return FALSE;
315 FreeMediaType(pin->pmt);
316 *pin->pmt = amt;
317 return TRUE;
320 static gboolean query_sink(GstPad *pad, GstObject *parent, GstQuery *query)
322 switch (GST_QUERY_TYPE (query)) {
323 case GST_QUERY_ACCEPT_CAPS:
325 GstCaps *caps;
326 gboolean res;
327 gst_query_parse_accept_caps(query, &caps);
328 res = accept_caps_sink(pad, caps);
329 gst_query_set_accept_caps_result(query, res);
330 return TRUE; /* FIXME */
332 default:
333 return gst_pad_query_default (pad, parent, query);
337 static gboolean gst_base_src_perform_seek(GSTImpl *This, GstEvent *event)
339 gboolean res = TRUE;
340 gdouble rate;
341 GstFormat seek_format;
342 GstSeekFlags flags;
343 GstSeekType cur_type, stop_type;
344 gint64 cur, stop;
345 gboolean flush;
346 guint32 seqnum;
347 GstEvent *tevent;
348 BOOL thread = !!This->push_thread;
350 TRACE("%p %p\n", This, event);
352 gst_event_parse_seek(event, &rate, &seek_format, &flags,
353 &cur_type, &cur, &stop_type, &stop);
355 if (seek_format != GST_FORMAT_BYTES) {
356 FIXME("Not handling other format %i\n", seek_format);
357 return FALSE;
360 flush = flags & GST_SEEK_FLAG_FLUSH;
361 seqnum = gst_event_get_seqnum(event);
363 /* send flush start */
364 if (flush) {
365 tevent = gst_event_new_flush_start();
366 gst_event_set_seqnum(tevent, seqnum);
367 gst_pad_push_event(This->my_src, tevent);
368 if (This->pInputPin.pReader)
369 IAsyncReader_BeginFlush(This->pInputPin.pReader);
370 if (thread)
371 gst_pad_set_active(This->my_src, 1);
374 This->nextofs = This->start = cur;
376 /* and prepare to continue streaming */
377 if (flush) {
378 tevent = gst_event_new_flush_stop(TRUE);
379 gst_event_set_seqnum(tevent, seqnum);
380 gst_pad_push_event(This->my_src, tevent);
381 if (This->pInputPin.pReader)
382 IAsyncReader_EndFlush(This->pInputPin.pReader);
383 if (thread)
384 gst_pad_set_active(This->my_src, 1);
387 return res;
390 static gboolean event_src(GstPad *pad, GstObject *parent, GstEvent *event)
392 GSTImpl *This = gst_pad_get_element_private(pad);
394 TRACE("%p %p\n", pad, event);
396 switch (event->type) {
397 case GST_EVENT_SEEK:
398 return gst_base_src_perform_seek(This, event);
399 case GST_EVENT_FLUSH_START:
400 EnterCriticalSection(&This->filter.csFilter);
401 if (This->pInputPin.pReader)
402 IAsyncReader_BeginFlush(This->pInputPin.pReader);
403 LeaveCriticalSection(&This->filter.csFilter);
404 break;
405 case GST_EVENT_FLUSH_STOP:
406 EnterCriticalSection(&This->filter.csFilter);
407 if (This->pInputPin.pReader)
408 IAsyncReader_EndFlush(This->pInputPin.pReader);
409 LeaveCriticalSection(&This->filter.csFilter);
410 break;
411 default:
412 FIXME("%p (%u) stub\n", event, event->type);
413 case GST_EVENT_TAG:
414 case GST_EVENT_QOS:
415 return gst_pad_event_default(pad, parent, event);
417 return TRUE;
420 static gboolean event_sink(GstPad *pad, GstObject *parent, GstEvent *event)
422 GSTOutPin *pin = gst_pad_get_element_private(pad);
424 TRACE("%p %p\n", pad, event);
426 switch (event->type) {
427 case GST_EVENT_SEGMENT: {
428 gdouble rate, applied_rate;
429 gint64 stop, pos;
430 const GstSegment *segment;
432 gst_event_parse_segment(event, &segment);
434 pos = segment->position;
435 stop = segment->stop;
436 rate = segment->rate;
437 applied_rate = segment->applied_rate;
439 if (segment->format != GST_FORMAT_TIME) {
440 FIXME("Ignoring new segment because of format %i\n", segment->format);
441 return TRUE;
444 gst_segment_copy_into(segment, pin->segment);
446 pos /= 100;
448 if (stop > 0)
449 stop /= 100;
451 if (pin->pin.pin.pConnectedTo)
452 IPin_NewSegment(pin->pin.pin.pConnectedTo, pos, stop, rate*applied_rate);
454 return TRUE;
456 case GST_EVENT_EOS:
457 if (pin->pin.pin.pConnectedTo)
458 IPin_EndOfStream(pin->pin.pin.pConnectedTo);
459 return TRUE;
460 case GST_EVENT_FLUSH_START:
461 if (((GSTImpl *)pin->pin.pin.pinInfo.pFilter)->ignore_flush) {
462 /* gst-plugins-base prior to 1.7 contains a bug which causes
463 * our sink pins to receive a flush-start event when the
464 * decodebin changes from PAUSED to READY (including
465 * PLAYING->PAUSED->READY), but no matching flush-stop event is
466 * sent. See <gst-plugins-base.git:60bad4815db966a8e4). Here we
467 * unset the flushing flag to avoid the problem. */
468 TRACE("Working around gst <1.7 bug, ignoring FLUSH_START\n");
469 GST_PAD_UNSET_FLUSHING (pad);
470 return TRUE;
472 if (pin->pin.pin.pConnectedTo)
473 IPin_BeginFlush(pin->pin.pin.pConnectedTo);
474 return TRUE;
475 case GST_EVENT_FLUSH_STOP:
476 gst_segment_init(pin->segment, GST_FORMAT_TIME);
477 if (pin->pin.pin.pConnectedTo)
478 IPin_EndFlush(pin->pin.pin.pConnectedTo);
479 return TRUE;
480 case GST_EVENT_CAPS: {
481 GstCaps *caps;
482 gst_event_parse_caps(event, &caps);
483 return setcaps_sink(pad, caps);
485 default:
486 TRACE("%p stub %s\n", event, gst_event_type_get_name(event->type));
487 return gst_pad_event_default(pad, parent, event);
491 static void release_sample(void *data)
493 ULONG ret;
494 ret = IMediaSample_Release((IMediaSample *)data);
495 TRACE("Releasing %p returns %u\n", data, ret);
498 static DWORD CALLBACK push_data(LPVOID iface)
500 LONGLONG maxlen, curlen;
501 GSTImpl *This = iface;
502 IMediaSample *buf;
503 DWORD_PTR user;
504 HRESULT hr;
506 if (!This->stop)
507 IAsyncReader_Length(This->pInputPin.pReader, &maxlen, &curlen);
508 else
509 maxlen = This->stop;
511 TRACE("Waiting..\n");
513 WaitForSingleObject(This->event, INFINITE);
515 TRACE("Starting..\n");
516 for (;;) {
517 REFERENCE_TIME tStart, tStop;
518 ULONG len;
519 GstBuffer *gstbuf;
520 gsize bufsize;
521 BYTE *data;
522 int ret;
524 TRACE("pAlloc: %p\n", This->pInputPin.pAlloc);
525 hr = IMemAllocator_GetBuffer(This->pInputPin.pAlloc, &buf, NULL, NULL, 0);
526 if (FAILED(hr))
527 break;
529 if (This->nextofs >= maxlen)
530 break;
531 len = IMediaSample_GetSize(buf);
532 if (This->nextofs + len > maxlen)
533 len = maxlen - This->nextofs;
535 tStart = MEDIATIME_FROM_BYTES(This->nextofs);
536 tStop = tStart + MEDIATIME_FROM_BYTES(len);
537 IMediaSample_SetTime(buf, &tStart, &tStop);
539 hr = IAsyncReader_Request(This->pInputPin.pReader, buf, 0);
540 if (FAILED(hr)) {
541 IMediaSample_Release(buf);
542 break;
544 This->nextofs += len;
545 hr = IAsyncReader_WaitForNext(This->pInputPin.pReader, -1, &buf, &user);
546 if (FAILED(hr) || !buf) {
547 if (buf)
548 IMediaSample_Release(buf);
549 break;
552 IMediaSample_GetPointer(buf, &data);
553 bufsize = IMediaSample_GetActualDataLength(buf);
554 gstbuf = gst_buffer_new_wrapped_full(0, data, bufsize, 0, bufsize, buf, release_sample_wrapper);
555 IMediaSample_AddRef(buf);
556 gst_mini_object_set_qdata(GST_MINI_OBJECT(gstbuf), g_quark_from_static_string(media_quark_string), buf, release_sample_wrapper);
557 if (!gstbuf) {
558 IMediaSample_Release(buf);
559 break;
561 gstbuf->duration = gstbuf->pts = -1;
562 ret = gst_pad_push(This->my_src, gstbuf);
563 if (ret >= 0)
564 hr = S_OK;
565 else
566 ERR("Sending returned: %i\n", ret);
567 if (ret == GST_FLOW_ERROR)
568 hr = E_FAIL;
569 else if (ret == GST_FLOW_FLUSHING)
570 hr = VFW_E_WRONG_STATE;
571 if (hr != S_OK)
572 break;
575 gst_pad_push_event(This->my_src, gst_event_new_eos());
577 TRACE("Almost stopping.. %08x\n", hr);
578 do {
579 IAsyncReader_WaitForNext(This->pInputPin.pReader, 0, &buf, &user);
580 if (buf)
581 IMediaSample_Release(buf);
582 } while (buf);
584 TRACE("Stopping.. %08x\n", hr);
585 return 0;
588 static HRESULT WINAPI GST_OutPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
590 GSTOutPin *pin = (GSTOutPin*)iface;
591 FIXME("stub %p\n", pin);
592 return S_OK;
595 static GstFlowReturn got_data_sink(GstPad *pad, GstObject *parent, GstBuffer *buf)
597 GSTOutPin *pin = gst_pad_get_element_private(pad);
598 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
599 HRESULT hr;
600 BYTE *ptr = NULL;
601 IMediaSample *sample;
602 GstMapInfo info;
604 TRACE("%p %p\n", pad, buf);
606 if (This->initial) {
607 gst_buffer_unref(buf);
608 TRACE("Triggering %p %p\n", pad, pin->caps_event);
609 SetEvent(pin->caps_event);
610 return GST_FLOW_OK;
613 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
615 if (hr == VFW_E_NOT_CONNECTED) {
616 gst_buffer_unref(buf);
617 return GST_FLOW_NOT_LINKED;
620 if (FAILED(hr)) {
621 gst_buffer_unref(buf);
622 ERR("Could not get a delivery buffer (%x), returning GST_FLOW_FLUSHING\n", hr);
623 return GST_FLOW_FLUSHING;
626 gst_buffer_map(buf, &info, GST_MAP_READ);
628 hr = IMediaSample_SetActualDataLength(sample, info.size);
629 if(FAILED(hr)){
630 WARN("SetActualDataLength failed: %08x\n", hr);
631 return GST_FLOW_FLUSHING;
634 IMediaSample_GetPointer(sample, &ptr);
636 memcpy(ptr, info.data, info.size);
638 gst_buffer_unmap(buf, &info);
640 if (GST_BUFFER_PTS_IS_VALID(buf)) {
641 REFERENCE_TIME rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts);
642 if (rtStart >= 0)
643 rtStart /= 100;
645 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
646 REFERENCE_TIME tStart = buf->pts / 100;
647 REFERENCE_TIME tStop = (buf->pts + buf->duration) / 100;
648 REFERENCE_TIME rtStop;
649 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->pts + buf->duration);
650 if (rtStop >= 0)
651 rtStop /= 100;
652 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
653 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
654 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
655 } else {
656 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
657 IMediaSample_SetMediaTime(sample, NULL, NULL);
659 } else {
660 IMediaSample_SetTime(sample, NULL, NULL);
661 IMediaSample_SetMediaTime(sample, NULL, NULL);
664 IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
665 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_LIVE));
666 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
668 if (!pin->pin.pin.pConnectedTo)
669 hr = VFW_E_NOT_CONNECTED;
670 else
671 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
673 TRACE("sending sample returned: %08x\n", hr);
675 gst_buffer_unref(buf);
676 IMediaSample_Release(sample);
678 if (hr == VFW_E_NOT_CONNECTED)
679 return GST_FLOW_NOT_LINKED;
681 if (FAILED(hr))
682 return GST_FLOW_FLUSHING;
684 return GST_FLOW_OK;
687 static GstFlowReturn request_buffer_src(GstPad *pad, GstObject *parent, guint64 ofs, guint len, GstBuffer **buf)
689 GSTImpl *This = gst_pad_get_element_private(pad);
690 HRESULT hr;
691 GstMapInfo info;
693 TRACE("%p %s %i %p\n", pad, wine_dbgstr_longlong(ofs), len, buf);
695 *buf = NULL;
696 if (ofs == (guint64)-1)
697 ofs = This->nextpullofs;
698 if (ofs >= This->filesize) {
699 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
700 return GST_FLOW_EOS;
702 if (len + ofs > This->filesize)
703 len = This->filesize - ofs;
704 This->nextpullofs = ofs + len;
706 *buf = gst_buffer_new_and_alloc(len);
707 gst_buffer_map(*buf, &info, GST_MAP_WRITE);
708 hr = IAsyncReader_SyncRead(This->pInputPin.pReader, ofs, len, info.data);
709 gst_buffer_unmap(*buf, &info);
710 if (FAILED(hr)) {
711 ERR("Returned %08x\n", hr);
712 return GST_FLOW_ERROR;
715 GST_BUFFER_OFFSET(*buf) = ofs;
716 return GST_FLOW_OK;
719 static DWORD CALLBACK push_data_init(LPVOID iface)
721 GSTImpl *This = iface;
722 DWORD64 ofs = 0;
724 TRACE("Starting..\n");
725 for (;;) {
726 GstBuffer *buf;
727 GstFlowReturn ret = request_buffer_src(This->my_src, NULL, ofs, 4096, &buf);
728 if (ret < 0) {
729 ERR("Obtaining buffer returned: %i\n", ret);
730 break;
732 ret = gst_pad_push(This->my_src, buf);
733 ofs += 4096;
734 if (ret)
735 TRACE("Sending returned: %i\n", ret);
736 if (ret < 0)
737 break;
739 TRACE("Stopping..\n");
740 return 0;
743 static void removed_decoded_pad(GstElement *bin, GstPad *pad, gpointer user)
745 GSTImpl *This = (GSTImpl*)user;
746 int x;
747 GSTOutPin *pin;
749 TRACE("%p %p %p\n", This, bin, pad);
751 EnterCriticalSection(&This->filter.csFilter);
752 for (x = 0; x < This->cStreams; ++x) {
753 if (This->ppPins[x]->their_src == pad)
754 break;
756 if (x == This->cStreams)
757 goto out;
759 pin = This->ppPins[x];
761 if(pin->flipfilter)
762 gst_pad_unlink(pin->their_src, pin->flip_sink);
763 else
764 gst_pad_unlink(pin->their_src, pin->my_sink);
766 gst_object_unref(pin->their_src);
767 pin->their_src = NULL;
768 out:
769 TRACE("Removed %i/%i\n", x, This->cStreams);
770 LeaveCriticalSection(&This->filter.csFilter);
773 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, GSTImpl *This)
775 HRESULT hr;
776 PIN_INFO piOutput;
777 const char *typename;
778 char *name;
779 AM_MEDIA_TYPE amt = {{0}};
780 GstCaps *caps;
781 GstStructure *arg;
782 GstPad *mypad;
783 GSTOutPin *pin;
784 int ret;
785 BOOL isvid = FALSE, isaud = FALSE;
786 gchar my_name[1024];
788 TRACE("%p %p %p\n", This, bin, pad);
790 piOutput.dir = PINDIR_OUTPUT;
791 piOutput.pFilter = &This->filter.IBaseFilter_iface;
792 name = gst_pad_get_name(pad);
793 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1);
794 TRACE("Name: %s\n", name);
795 strcpy(my_name, "qz_sink_");
796 strcat(my_name, name);
797 g_free(name);
798 piOutput.achName[sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1] = 0;
800 caps = gst_pad_query_caps(pad, NULL);
801 caps = gst_caps_make_writable(caps);
802 arg = gst_caps_get_structure(caps, 0);
803 typename = gst_structure_get_name(arg);
805 mypad = gst_pad_new(my_name, GST_PAD_SINK);
806 gst_pad_set_chain_function(mypad, got_data_sink_wrapper);
807 gst_pad_set_event_function(mypad, event_sink_wrapper);
808 gst_pad_set_query_function(mypad, query_sink_wrapper);
810 if (!strcmp(typename, "audio/x-raw")) {
811 isaud = TRUE;
812 } else if (!strcmp(typename, "video/x-raw")) {
813 isvid = TRUE;
814 } else {
815 FIXME("Unknown type \'%s\'\n", typename);
816 return;
819 hr = GST_AddPin(This, &piOutput, &amt);
820 if (FAILED(hr)) {
821 ERR("%08x\n", hr);
822 return;
825 pin = This->ppPins[This->cStreams - 1];
826 gst_pad_set_element_private(mypad, pin);
827 pin->my_sink = mypad;
828 pin->isaud = isaud;
829 pin->isvid = isvid;
831 gst_segment_init(pin->segment, GST_FORMAT_TIME);
833 if (isvid) {
834 GstElement *vconv;
836 TRACE("setting up videoflip filter for pin %p, my_sink: %p, their_src: %p\n",
837 pin, pin->my_sink, pad);
839 /* gstreamer outputs video top-down, but dshow expects bottom-up, so
840 * make new transform filter to invert video */
841 vconv = gst_element_factory_make("videoconvert", NULL);
842 if(!vconv){
843 ERR("Missing videoconvert filter?\n");
844 ret = -1;
845 goto exit;
848 pin->flipfilter = gst_element_factory_make("videoflip", NULL);
849 if(!pin->flipfilter){
850 ERR("Missing videoflip filter?\n");
851 ret = -1;
852 goto exit;
855 gst_util_set_object_arg(G_OBJECT(pin->flipfilter), "method", "vertical-flip");
857 gst_bin_add(GST_BIN(This->container), vconv); /* bin takes ownership */
858 gst_element_sync_state_with_parent(vconv);
859 gst_bin_add(GST_BIN(This->container), pin->flipfilter); /* bin takes ownership */
860 gst_element_sync_state_with_parent(pin->flipfilter);
862 gst_element_link (vconv, pin->flipfilter);
864 pin->flip_sink = gst_element_get_static_pad(vconv, "sink");
865 if(!pin->flip_sink){
866 WARN("Couldn't find sink on flip filter\n");
867 pin->flipfilter = NULL;
868 ret = -1;
869 goto exit;
872 ret = gst_pad_link(pad, pin->flip_sink);
873 if(ret < 0){
874 WARN("gst_pad_link failed: %d\n", ret);
875 gst_object_unref(pin->flip_sink);
876 pin->flip_sink = NULL;
877 pin->flipfilter = NULL;
878 goto exit;
881 pin->flip_src = gst_element_get_static_pad(pin->flipfilter, "src");
882 if(!pin->flip_src){
883 WARN("Couldn't find src on flip filter\n");
884 gst_object_unref(pin->flip_sink);
885 pin->flip_sink = NULL;
886 pin->flipfilter = NULL;
887 ret = -1;
888 goto exit;
891 ret = gst_pad_link(pin->flip_src, pin->my_sink);
892 if(ret < 0){
893 WARN("gst_pad_link failed: %d\n", ret);
894 gst_object_unref(pin->flip_src);
895 pin->flip_src = NULL;
896 gst_object_unref(pin->flip_sink);
897 pin->flip_sink = NULL;
898 pin->flipfilter = NULL;
899 goto exit;
901 } else
902 ret = gst_pad_link(pad, mypad);
904 gst_pad_set_active(mypad, 1);
906 exit:
907 TRACE("Linking: %i\n", ret);
909 if (ret >= 0) {
910 pin->their_src = pad;
911 gst_object_ref(pin->their_src);
915 static void existing_new_pad(GstElement *bin, GstPad *pad, gpointer user)
917 GSTImpl *This = (GSTImpl*)user;
918 int x, ret;
920 TRACE("%p %p %p\n", This, bin, pad);
922 if (gst_pad_is_linked(pad))
923 return;
925 /* Still holding our own lock */
926 if (This->initial) {
927 init_new_decoded_pad(bin, pad, This);
928 return;
931 EnterCriticalSection(&This->filter.csFilter);
932 for (x = 0; x < This->cStreams; ++x) {
933 GSTOutPin *pin = This->ppPins[x];
934 if (!pin->their_src) {
935 gst_segment_init(pin->segment, GST_FORMAT_TIME);
937 if (pin->flipfilter)
938 ret = gst_pad_link(pad, pin->flip_sink);
939 else
940 ret = gst_pad_link(pad, pin->my_sink);
942 if (ret >= 0) {
943 pin->their_src = pad;
944 gst_object_ref(pin->their_src);
945 TRACE("Relinked\n");
946 LeaveCriticalSection(&This->filter.csFilter);
947 return;
951 init_new_decoded_pad(bin, pad, This);
952 LeaveCriticalSection(&This->filter.csFilter);
955 static gboolean query_function(GstPad *pad, GstObject *parent, GstQuery *query)
957 GSTImpl *This = gst_pad_get_element_private(pad);
958 GstFormat format;
959 int ret;
960 LONGLONG duration;
962 TRACE("%p %p %p\n", This, pad, query);
964 switch (GST_QUERY_TYPE(query)) {
965 case GST_QUERY_DURATION:
966 gst_query_parse_duration (query, &format, NULL);
967 if (format == GST_FORMAT_PERCENT) {
968 gst_query_set_duration (query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
969 return TRUE;
971 ret = gst_pad_query_convert (pad, GST_FORMAT_BYTES, This->filesize, format, &duration);
972 gst_query_set_duration(query, format, duration);
973 return ret;
974 case GST_QUERY_SEEKING:
975 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
976 TRACE("Seeking %i %i\n", format, GST_FORMAT_BYTES);
977 if (format != GST_FORMAT_BYTES)
978 return FALSE;
979 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
980 return TRUE;
981 case GST_QUERY_SCHEDULING:
982 gst_query_set_scheduling(query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1, 0);
983 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PUSH);
984 gst_query_add_scheduling_mode(query, GST_PAD_MODE_PULL);
985 return TRUE;
986 default:
987 TRACE("Unhandled query type: %s\n", GST_QUERY_TYPE_NAME(query));
988 return FALSE;
992 static gboolean activate_push(GstPad *pad, gboolean activate)
994 GSTImpl *This = gst_pad_get_element_private(pad);
996 TRACE("%p %p %u\n", This, pad, activate);
998 EnterCriticalSection(&This->filter.csFilter);
999 if (!activate) {
1000 TRACE("Deactivating\n");
1001 if (!This->initial)
1002 IAsyncReader_BeginFlush(This->pInputPin.pReader);
1003 if (This->push_thread) {
1004 WaitForSingleObject(This->push_thread, -1);
1005 CloseHandle(This->push_thread);
1006 This->push_thread = NULL;
1008 if (!This->initial)
1009 IAsyncReader_EndFlush(This->pInputPin.pReader);
1010 if (This->filter.state == State_Stopped)
1011 This->nextofs = This->start;
1012 } else if (!This->push_thread) {
1013 TRACE("Activating\n");
1014 if (This->initial)
1015 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
1016 else
1017 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
1019 LeaveCriticalSection(&This->filter.csFilter);
1020 return TRUE;
1023 static gboolean activate_mode(GstPad *pad, GstObject *parent, GstPadMode mode, gboolean activate)
1025 TRACE("%p %p 0x%x %u\n", pad, parent, mode, activate);
1026 switch (mode) {
1027 case GST_PAD_MODE_PULL:
1028 return TRUE;
1029 case GST_PAD_MODE_PUSH:
1030 return activate_push(pad, activate);
1031 default:
1032 return FALSE;
1034 return FALSE;
1037 static void no_more_pads(GstElement *decodebin, gpointer user)
1039 GSTImpl *This = (GSTImpl*)user;
1040 TRACE("%p %p\n", This, decodebin);
1041 SetEvent(This->event);
1044 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, gpointer user)
1046 const char *name = gst_element_factory_get_longname(fact);
1048 if (strstr(name, "Player protection")) {
1049 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
1050 return GST_AUTOPLUG_SELECT_SKIP;
1052 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder")) {
1053 WARN("Disabled video acceleration since it breaks in wine\n");
1054 return GST_AUTOPLUG_SELECT_SKIP;
1056 TRACE("using \"%s\"\n", name);
1057 return GST_AUTOPLUG_SELECT_TRY;
1060 static GstBusSyncReply watch_bus(GstBus *bus, GstMessage *msg, gpointer data)
1062 GSTImpl *This = data;
1063 GError *err = NULL;
1064 gchar *dbg_info = NULL;
1066 TRACE("%p %p %p\n", This, bus, msg);
1068 if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_ERROR) {
1069 gst_message_parse_error(msg, &err, &dbg_info);
1070 FIXME("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1071 WARN("%s\n", dbg_info);
1072 SetEvent(This->event);
1073 } else if (GST_MESSAGE_TYPE(msg) & GST_MESSAGE_WARNING) {
1074 gst_message_parse_warning(msg, &err, &dbg_info);
1075 WARN("%s: %s\n", GST_OBJECT_NAME(msg->src), err->message);
1076 WARN("%s\n", dbg_info);
1078 if (err)
1079 g_error_free(err);
1080 g_free(dbg_info);
1081 return GST_BUS_DROP;
1084 static void unknown_type(GstElement *bin, GstPad *pad, GstCaps *caps, gpointer user)
1086 gchar *strcaps = gst_caps_to_string(caps);
1087 FIXME("Could not find a filter for caps: %s\n", strcaps);
1088 g_free(strcaps);
1091 static HRESULT GST_Connect(GSTInPin *pPin, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props)
1093 GSTImpl *This = (GSTImpl*)pPin->pin.pinInfo.pFilter;
1094 HRESULT hr;
1095 int ret, i;
1096 LONGLONG avail, duration;
1097 GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
1098 "quartz_src",
1099 GST_PAD_SRC,
1100 GST_PAD_ALWAYS,
1101 GST_STATIC_CAPS_ANY);
1102 GstElement *gstfilter;
1104 TRACE("%p %p %p\n", pPin, pConnectPin, props);
1106 This->props = *props;
1107 IAsyncReader_Length(pPin->pReader, &This->filesize, &avail);
1109 if (!This->bus) {
1110 This->bus = gst_bus_new();
1111 gst_bus_set_sync_handler(This->bus, watch_bus_wrapper, This, NULL);
1114 This->container = gst_bin_new(NULL);
1115 gst_element_set_bus(This->container, This->bus);
1117 gstfilter = gst_element_factory_make("decodebin", NULL);
1118 if (!gstfilter) {
1119 FIXME("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
1120 8 * (int)sizeof(void*));
1121 return E_FAIL;
1124 gst_bin_add(GST_BIN(This->container), gstfilter);
1126 g_signal_connect(gstfilter, "pad-added", G_CALLBACK(existing_new_pad_wrapper), This);
1127 g_signal_connect(gstfilter, "pad-removed", G_CALLBACK(removed_decoded_pad_wrapper), This);
1128 g_signal_connect(gstfilter, "autoplug-select", G_CALLBACK(autoplug_blacklist_wrapper), This);
1129 g_signal_connect(gstfilter, "unknown-type", G_CALLBACK(unknown_type_wrapper), This);
1131 This->my_src = gst_pad_new_from_static_template(&src_template, "quartz-src");
1132 gst_pad_set_getrange_function(This->my_src, request_buffer_src_wrapper);
1133 gst_pad_set_query_function(This->my_src, query_function_wrapper);
1134 gst_pad_set_activatemode_function(This->my_src, activate_mode_wrapper);
1135 gst_pad_set_event_function(This->my_src, event_src_wrapper);
1136 gst_pad_set_element_private (This->my_src, This);
1137 This->their_sink = gst_element_get_static_pad(gstfilter, "sink");
1139 g_signal_connect(gstfilter, "no-more-pads", G_CALLBACK(no_more_pads_wrapper), This);
1140 ret = gst_pad_link(This->my_src, This->their_sink);
1141 if (ret < 0) {
1142 ERR("Returns: %i\n", ret);
1143 return E_FAIL;
1145 This->start = This->nextofs = This->nextpullofs = This->stop = 0;
1147 /* Add initial pins */
1148 This->initial = This->discont = TRUE;
1149 ResetEvent(This->event);
1150 gst_element_set_state(This->container, GST_STATE_PLAYING);
1151 WaitForSingleObject(This->event, -1);
1152 gst_element_get_state(This->container, NULL, NULL, -1);
1154 if (ret < 0) {
1155 WARN("Ret: %i\n", ret);
1156 hr = E_FAIL;
1157 } else if (!This->cStreams) {
1158 FIXME("GStreamer could not find any streams\n");
1159 hr = E_FAIL;
1160 } else {
1161 gst_pad_query_duration(This->ppPins[0]->their_src, GST_FORMAT_TIME, &duration);
1162 for (i = 0; i < This->cStreams; ++i) {
1163 This->ppPins[i]->seek.llDuration = This->ppPins[i]->seek.llStop = duration / 100;
1164 This->ppPins[i]->seek.llCurrent = 0;
1165 if (!This->ppPins[i]->seek.llDuration)
1166 This->ppPins[i]->seek.dwCapabilities = 0;
1167 WaitForSingleObject(This->ppPins[i]->caps_event, -1);
1169 hr = S_OK;
1171 *props = This->props;
1173 This->ignore_flush = TRUE;
1174 gst_element_set_state(This->container, GST_STATE_READY);
1175 gst_element_get_state(This->container, NULL, NULL, -1);
1176 This->ignore_flush = FALSE;
1178 This->initial = FALSE;
1180 /* don't set active during test-play, as we don't want to push/pull data
1181 * from the source yet */
1182 gst_pad_set_active(This->my_src, 1);
1184 This->nextofs = This->nextpullofs = 0;
1185 return hr;
1188 static inline GSTOutPin *impl_from_IMediaSeeking( IMediaSeeking *iface )
1190 return CONTAINING_RECORD(iface, GSTOutPin, seek.IMediaSeeking_iface);
1193 static IPin* WINAPI GST_GetPin(BaseFilter *iface, int pos)
1195 GSTImpl *This = (GSTImpl *)iface;
1196 IPin *pin;
1198 TRACE("%p: Asking for pos %x\n", This, pos);
1200 if (pos > This->cStreams || pos < 0)
1201 return NULL;
1203 if (!pos)
1204 pin = &This->pInputPin.pin.IPin_iface;
1205 else
1206 pin = &This->ppPins[pos - 1]->pin.pin.IPin_iface;
1208 IPin_AddRef(pin);
1209 return pin;
1212 static LONG WINAPI GST_GetPinCount(BaseFilter *iface)
1214 GSTImpl *This = (GSTImpl *)iface;
1215 TRACE("%p -> %u\n", This, This->cStreams + 1);
1216 return (This->cStreams + 1);
1219 static const BaseFilterFuncTable BaseFuncTable = {
1220 GST_GetPin,
1221 GST_GetPinCount
1224 IUnknown * CALLBACK Gstreamer_Splitter_create(IUnknown *pUnkOuter, HRESULT *phr)
1226 IUnknown *obj = NULL;
1227 PIN_INFO *piInput;
1228 GSTImpl *This;
1230 TRACE("%p %p\n", pUnkOuter, phr);
1232 if (!Gstreamer_init())
1234 *phr = E_FAIL;
1235 return NULL;
1238 mark_wine_thread();
1240 This = CoTaskMemAlloc(sizeof(*This));
1241 if (!This)
1243 *phr = E_OUTOFMEMORY;
1244 return NULL;
1247 obj = (IUnknown*)&This->filter.IBaseFilter_iface;
1248 BaseFilter_Init(&This->filter, &GST_Vtbl, &CLSID_Gstreamer_Splitter, (DWORD_PTR)(__FILE__ ": GSTImpl.csFilter"), &BaseFuncTable);
1250 This->cStreams = 0;
1251 This->ppPins = NULL;
1252 This->push_thread = NULL;
1253 This->event = CreateEventW(NULL, 0, 0, NULL);
1254 This->bus = NULL;
1256 piInput = &This->pInputPin.pin.pinInfo;
1257 piInput->dir = PINDIR_INPUT;
1258 piInput->pFilter = &This->filter.IBaseFilter_iface;
1259 lstrcpynW(piInput->achName, wcsInputPinName, sizeof(piInput->achName) / sizeof(piInput->achName[0]));
1260 This->pInputPin.pin.IPin_iface.lpVtbl = &GST_InputPin_Vtbl;
1261 This->pInputPin.pin.refCount = 1;
1262 This->pInputPin.pin.pConnectedTo = NULL;
1263 This->pInputPin.pin.pCritSec = &This->filter.csFilter;
1264 ZeroMemory(&This->pInputPin.pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1265 *phr = S_OK;
1267 TRACE("returning %p\n", obj);
1269 return obj;
1272 static void GST_Destroy(GSTImpl *This)
1274 IPin *connected = NULL;
1275 ULONG pinref;
1276 HRESULT hr;
1278 TRACE("Destroying %p\n", This);
1280 CloseHandle(This->event);
1282 /* Don't need to clean up output pins, disconnecting input pin will do that */
1283 IPin_ConnectedTo((IPin *)&This->pInputPin, &connected);
1284 if (connected) {
1285 hr = IPin_Disconnect(connected);
1286 assert(hr == S_OK);
1287 IPin_Release(connected);
1288 hr = IPin_Disconnect(&This->pInputPin.pin.IPin_iface);
1289 assert(hr == S_OK);
1291 pinref = IPin_Release(&This->pInputPin.pin.IPin_iface);
1292 if (pinref) {
1293 /* Valgrind could find this, if I kill it here */
1294 ERR("pinref should be null, is %u, destroying anyway\n", pinref);
1295 assert((LONG)pinref > 0);
1297 while (pinref)
1298 pinref = IPin_Release(&This->pInputPin.pin.IPin_iface);
1300 if (This->bus) {
1301 gst_bus_set_sync_handler(This->bus, NULL, NULL, NULL);
1302 gst_object_unref(This->bus);
1304 BaseFilter_Destroy(&This->filter);
1305 CoTaskMemFree(This);
1308 static HRESULT WINAPI GST_QueryInterface(IBaseFilter *iface, REFIID riid, LPVOID *ppv)
1310 GSTImpl *This = (GSTImpl *)iface;
1312 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1314 *ppv = NULL;
1316 if (IsEqualIID(riid, &IID_IUnknown) ||
1317 IsEqualIID(riid, &IID_IPersist) ||
1318 IsEqualIID(riid, &IID_IMediaFilter) ||
1319 IsEqualIID(riid, &IID_IBaseFilter))
1321 *ppv = &This->filter.IBaseFilter_iface;
1324 if (*ppv) {
1325 IUnknown_AddRef((IUnknown *)(*ppv));
1326 return S_OK;
1329 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
1330 !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
1331 FIXME("No interface for %s!\n", debugstr_guid(riid));
1333 return E_NOINTERFACE;
1336 static ULONG WINAPI GST_Release(IBaseFilter *iface)
1338 GSTImpl *This = (GSTImpl *)iface;
1339 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
1341 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1343 if (!refCount)
1344 GST_Destroy(This);
1346 return refCount;
1349 static HRESULT WINAPI GST_Stop(IBaseFilter *iface)
1351 GSTImpl *This = (GSTImpl *)iface;
1353 TRACE("(%p)\n", This);
1355 mark_wine_thread();
1357 if (This->container) {
1358 This->ignore_flush = TRUE;
1359 gst_element_set_state(This->container, GST_STATE_READY);
1360 gst_element_get_state(This->container, NULL, NULL, -1);
1361 This->ignore_flush = FALSE;
1363 return S_OK;
1366 static HRESULT WINAPI GST_Pause(IBaseFilter *iface)
1368 HRESULT hr = S_OK;
1369 GSTImpl *This = (GSTImpl *)iface;
1370 GstState now;
1371 GstStateChangeReturn ret;
1373 TRACE("(%p)\n", This);
1375 if (!This->container)
1376 return VFW_E_NOT_CONNECTED;
1378 mark_wine_thread();
1380 gst_element_get_state(This->container, &now, NULL, -1);
1381 if (now == GST_STATE_PAUSED)
1382 return S_OK;
1383 if (now != GST_STATE_PLAYING)
1384 hr = IBaseFilter_Run(iface, -1);
1385 if (FAILED(hr))
1386 return hr;
1387 ret = gst_element_set_state(This->container, GST_STATE_PAUSED);
1388 if (ret == GST_STATE_CHANGE_ASYNC)
1389 hr = S_FALSE;
1390 return hr;
1393 static HRESULT WINAPI GST_Run(IBaseFilter *iface, REFERENCE_TIME tStart)
1395 HRESULT hr = S_OK;
1396 GSTImpl *This = (GSTImpl *)iface;
1397 ULONG i;
1398 GstState now;
1399 HRESULT hr_any = VFW_E_NOT_CONNECTED;
1401 TRACE("(%p)->(%s)\n", This, wine_dbgstr_longlong(tStart));
1403 mark_wine_thread();
1405 if (!This->container)
1406 return VFW_E_NOT_CONNECTED;
1408 EnterCriticalSection(&This->filter.csFilter);
1409 This->filter.rtStreamStart = tStart;
1410 LeaveCriticalSection(&This->filter.csFilter);
1412 gst_element_get_state(This->container, &now, NULL, -1);
1413 if (now == GST_STATE_PLAYING)
1414 return S_OK;
1415 if (now == GST_STATE_PAUSED) {
1416 GstStateChangeReturn ret;
1417 ret = gst_element_set_state(This->container, GST_STATE_PLAYING);
1418 if (ret == GST_STATE_CHANGE_ASYNC)
1419 return S_FALSE;
1420 return S_OK;
1423 EnterCriticalSection(&This->filter.csFilter);
1424 gst_element_set_state(This->container, GST_STATE_PLAYING);
1425 This->filter.rtStreamStart = tStart;
1427 for (i = 0; i < This->cStreams; i++) {
1428 hr = BaseOutputPinImpl_Active(&This->ppPins[i]->pin);
1429 if (SUCCEEDED(hr)) {
1430 hr_any = hr;
1433 hr = hr_any;
1434 LeaveCriticalSection(&This->filter.csFilter);
1436 return hr;
1439 static HRESULT WINAPI GST_GetState(IBaseFilter *iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
1441 GSTImpl *This = (GSTImpl *)iface;
1442 HRESULT hr = S_OK;
1443 GstState now, pending;
1444 GstStateChangeReturn ret;
1446 TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
1448 mark_wine_thread();
1450 if (!This->container) {
1451 *pState = State_Stopped;
1452 return S_OK;
1455 ret = gst_element_get_state(This->container, &now, &pending, dwMilliSecsTimeout == INFINITE ? -1 : dwMilliSecsTimeout * 1000);
1457 if (ret == GST_STATE_CHANGE_ASYNC)
1458 hr = VFW_S_STATE_INTERMEDIATE;
1459 else
1460 pending = now;
1462 switch (pending) {
1463 case GST_STATE_PAUSED: *pState = State_Paused; return hr;
1464 case GST_STATE_PLAYING: *pState = State_Running; return hr;
1465 default: *pState = State_Stopped; return hr;
1469 static HRESULT WINAPI GST_FindPin(IBaseFilter *iface, LPCWSTR Id, IPin **ppPin)
1471 FIXME("(%p)->(%s,%p) stub\n", iface, debugstr_w(Id), ppPin);
1472 return E_NOTIMPL;
1475 static const IBaseFilterVtbl GST_Vtbl = {
1476 GST_QueryInterface,
1477 BaseFilterImpl_AddRef,
1478 GST_Release,
1479 BaseFilterImpl_GetClassID,
1480 GST_Stop,
1481 GST_Pause,
1482 GST_Run,
1483 GST_GetState,
1484 BaseFilterImpl_SetSyncSource,
1485 BaseFilterImpl_GetSyncSource,
1486 BaseFilterImpl_EnumPins,
1487 GST_FindPin,
1488 BaseFilterImpl_QueryFilterInfo,
1489 BaseFilterImpl_JoinFilterGraph,
1490 BaseFilterImpl_QueryVendorInfo
1493 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1495 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1496 TRACE("(%p)\n", This);
1497 return S_OK;
1500 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1502 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1503 TRACE("(%p)\n", This);
1504 return S_OK;
1507 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1509 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1510 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1511 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1512 mark_wine_thread();
1513 gst_pad_push_event(This->my_sink, ev);
1514 return S_OK;
1517 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1519 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1520 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
1523 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1525 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1526 return IPin_AddRef(&This->pin.pin.IPin_iface);
1529 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1531 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1532 return IPin_Release(&This->pin.pin.IPin_iface);
1535 static HRESULT WINAPI GST_Seeking_GetCurrentPosition(IMediaSeeking *iface, REFERENCE_TIME *pos)
1537 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1539 TRACE("(%p)->(%p)\n", This, pos);
1541 if (!pos)
1542 return E_POINTER;
1544 mark_wine_thread();
1546 if (!This->their_src) {
1547 *pos = This->seek.llCurrent;
1548 TRACE("Cached value\n");
1549 if (This->seek.llDuration)
1550 return S_OK;
1551 else
1552 return E_NOTIMPL;
1555 if (!gst_pad_query_position(This->their_src, GST_FORMAT_TIME, pos)) {
1556 WARN("Could not query position\n");
1557 return E_NOTIMPL;
1559 *pos /= 100;
1560 This->seek.llCurrent = *pos;
1561 return S_OK;
1564 static GstSeekType type_from_flags(DWORD flags)
1566 switch (flags & AM_SEEKING_PositioningBitsMask) {
1567 case AM_SEEKING_NoPositioning:
1568 return GST_SEEK_TYPE_NONE;
1569 case AM_SEEKING_AbsolutePositioning:
1570 case AM_SEEKING_RelativePositioning:
1571 return GST_SEEK_TYPE_SET;
1572 case AM_SEEKING_IncrementalPositioning:
1573 return GST_SEEK_TYPE_END;
1575 return GST_SEEK_TYPE_NONE;
1578 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1579 REFERENCE_TIME *pCur, DWORD curflags, REFERENCE_TIME *pStop,
1580 DWORD stopflags)
1582 HRESULT hr;
1583 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1584 GstSeekFlags f = 0;
1585 GstSeekType curtype, stoptype;
1586 GstEvent *e;
1587 gint64 stop_pos = 0, curr_pos = 0;
1589 TRACE("(%p)->(%p, 0x%x, %p, 0x%x)\n", This, pCur, curflags, pStop, stopflags);
1591 mark_wine_thread();
1593 if (!This->seek.llDuration)
1594 return E_NOTIMPL;
1596 hr = SourceSeekingImpl_SetPositions(iface, pCur, curflags, pStop, stopflags);
1597 if (!This->their_src)
1598 return hr;
1600 curtype = type_from_flags(curflags);
1601 stoptype = type_from_flags(stopflags);
1602 if (curflags & AM_SEEKING_SeekToKeyFrame)
1603 f |= GST_SEEK_FLAG_KEY_UNIT;
1604 if (curflags & AM_SEEKING_Segment)
1605 f |= GST_SEEK_FLAG_SEGMENT;
1606 if (!(curflags & AM_SEEKING_NoFlush))
1607 f |= GST_SEEK_FLAG_FLUSH;
1609 if (((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning) ||
1610 ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)) {
1611 gint64 tmp_pos;
1612 gst_pad_query_position (This->my_sink, GST_FORMAT_TIME, &tmp_pos);
1613 if ((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1614 curr_pos = tmp_pos;
1615 if ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1616 stop_pos = tmp_pos;
1619 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);
1620 if (gst_pad_push_event(This->my_sink, e))
1621 return S_OK;
1622 else
1623 return E_NOTIMPL;
1626 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1628 GST_Seeking_QueryInterface,
1629 GST_Seeking_AddRef,
1630 GST_Seeking_Release,
1631 SourceSeekingImpl_GetCapabilities,
1632 SourceSeekingImpl_CheckCapabilities,
1633 SourceSeekingImpl_IsFormatSupported,
1634 SourceSeekingImpl_QueryPreferredFormat,
1635 SourceSeekingImpl_GetTimeFormat,
1636 SourceSeekingImpl_IsUsingTimeFormat,
1637 SourceSeekingImpl_SetTimeFormat,
1638 SourceSeekingImpl_GetDuration,
1639 SourceSeekingImpl_GetStopPosition,
1640 GST_Seeking_GetCurrentPosition,
1641 SourceSeekingImpl_ConvertTimeFormat,
1642 GST_Seeking_SetPositions,
1643 SourceSeekingImpl_GetPositions,
1644 SourceSeekingImpl_GetAvailable,
1645 SourceSeekingImpl_SetRate,
1646 SourceSeekingImpl_GetRate,
1647 SourceSeekingImpl_GetPreroll
1650 static inline GSTOutPin *impl_from_IQualityControl( IQualityControl *iface )
1652 return CONTAINING_RECORD(iface, GSTOutPin, IQualityControl_iface);
1655 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1657 GSTOutPin *pin = impl_from_IQualityControl(iface);
1658 return IPin_QueryInterface(&pin->pin.pin.IPin_iface, riid, ppv);
1661 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1663 GSTOutPin *pin = impl_from_IQualityControl(iface);
1664 return IPin_AddRef(&pin->pin.pin.IPin_iface);
1667 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1669 GSTOutPin *pin = impl_from_IQualityControl(iface);
1670 return IPin_Release(&pin->pin.pin.IPin_iface);
1673 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
1675 GSTOutPin *pin = impl_from_IQualityControl(iface);
1676 GstEvent *evt;
1678 TRACE("(%p)->(%p, { 0x%x %u %s %s })\n", pin, sender,
1679 qm.Type, qm.Proportion,
1680 wine_dbgstr_longlong(qm.Late),
1681 wine_dbgstr_longlong(qm.TimeStamp));
1683 mark_wine_thread();
1685 if (qm.Type == Flood)
1686 qm.Late = 0;
1688 evt = gst_event_new_qos(qm.Type == Famine ? GST_QOS_TYPE_UNDERFLOW : GST_QOS_TYPE_OVERFLOW,
1689 qm.Proportion / 1000., qm.Late * 100, qm.TimeStamp * 100);
1691 if (!evt) {
1692 WARN("Failed to create QOS event\n");
1693 return E_INVALIDARG;
1696 gst_pad_push_event(pin->my_sink, evt);
1698 return S_OK;
1701 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1703 GSTOutPin *pin = impl_from_IQualityControl(iface);
1704 TRACE("(%p)->(%p)\n", pin, pin);
1705 /* Do nothing */
1706 return S_OK;
1709 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1710 GST_QualityControl_QueryInterface,
1711 GST_QualityControl_AddRef,
1712 GST_QualityControl_Release,
1713 GST_QualityControl_Notify,
1714 GST_QualityControl_SetSink
1717 static HRESULT WINAPI GSTOutPin_QueryInterface(IPin *iface, REFIID riid, void **ppv)
1719 GSTOutPin *This = (GSTOutPin *)iface;
1721 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1723 *ppv = NULL;
1725 if (IsEqualIID(riid, &IID_IUnknown))
1726 *ppv = iface;
1727 else if (IsEqualIID(riid, &IID_IPin))
1728 *ppv = iface;
1729 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1730 *ppv = &This->seek;
1731 else if (IsEqualIID(riid, &IID_IQualityControl))
1732 *ppv = &This->IQualityControl_iface;
1734 if (*ppv) {
1735 IUnknown_AddRef((IUnknown *)(*ppv));
1736 return S_OK;
1738 FIXME("No interface for %s!\n", debugstr_guid(riid));
1739 return E_NOINTERFACE;
1742 static ULONG WINAPI GSTOutPin_Release(IPin *iface)
1744 GSTOutPin *This = (GSTOutPin *)iface;
1745 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
1747 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1749 mark_wine_thread();
1751 if (!refCount) {
1752 if (This->their_src) {
1753 if (This->flipfilter) {
1754 gst_pad_unlink(This->their_src, This->flip_sink);
1755 gst_pad_unlink(This->flip_src, This->my_sink);
1756 gst_object_unref(This->flip_src);
1757 gst_object_unref(This->flip_sink);
1758 This->flipfilter = NULL;
1759 This->flip_src = This->flip_sink = NULL;
1760 } else
1761 gst_pad_unlink(This->their_src, This->my_sink);
1762 gst_object_unref(This->their_src);
1764 gst_object_unref(This->my_sink);
1765 CloseHandle(This->caps_event);
1766 DeleteMediaType(This->pmt);
1767 FreeMediaType(&This->pin.pin.mtCurrent);
1768 gst_segment_free(This->segment);
1769 if(This->gstpool)
1770 gst_object_unref(This->gstpool);
1771 if (This->pin.pAllocator)
1772 IMemAllocator_Release(This->pin.pAllocator);
1773 CoTaskMemFree(This);
1774 return 0;
1776 return refCount;
1779 static HRESULT WINAPI GSTOutPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
1781 GSTOutPin *This = (GSTOutPin *)iface;
1783 TRACE("(%p)->(%i, %p)\n", This, iPosition, pmt);
1785 if (iPosition < 0)
1786 return E_INVALIDARG;
1788 if (iPosition > 0)
1789 return VFW_S_NO_MORE_ITEMS;
1791 CopyMediaType(pmt, This->pmt);
1793 return S_OK;
1796 static HRESULT WINAPI GSTOutPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
1798 GSTOutPin *This = (GSTOutPin *)iface;
1799 TRACE("(%p)->(%p, %p)\n", This, pAlloc, ppropInputRequest);
1800 /* Unused */
1801 return S_OK;
1804 static HRESULT WINAPI GSTOutPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
1806 HRESULT hr;
1807 GSTOutPin *This = (GSTOutPin *)iface;
1808 GSTImpl *GSTfilter = (GSTImpl*)This->pin.pin.pinInfo.pFilter;
1810 TRACE("(%p)->(%p, %p)\n", This, pPin, pAlloc);
1812 *pAlloc = NULL;
1813 if (GSTfilter->pInputPin.pAlloc)
1815 hr = IMemInputPin_NotifyAllocator(pPin, GSTfilter->pInputPin.pAlloc, FALSE);
1816 if (SUCCEEDED(hr))
1818 *pAlloc = GSTfilter->pInputPin.pAlloc;
1819 IMemAllocator_AddRef(*pAlloc);
1822 else
1823 hr = VFW_E_NO_ALLOCATOR;
1825 return hr;
1828 static HRESULT WINAPI GSTOutPin_BreakConnect(BaseOutputPin *This)
1830 HRESULT hr;
1832 TRACE("(%p)->()\n", This);
1834 EnterCriticalSection(This->pin.pCritSec);
1835 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1836 hr = VFW_E_NOT_CONNECTED;
1837 else
1839 hr = IPin_Disconnect(This->pin.pConnectedTo);
1840 IPin_Disconnect((IPin *)This);
1842 LeaveCriticalSection(This->pin.pCritSec);
1844 return hr;
1847 static const IPinVtbl GST_OutputPin_Vtbl = {
1848 GSTOutPin_QueryInterface,
1849 BasePinImpl_AddRef,
1850 GSTOutPin_Release,
1851 BaseOutputPinImpl_Connect,
1852 BaseOutputPinImpl_ReceiveConnection,
1853 BaseOutputPinImpl_Disconnect,
1854 BasePinImpl_ConnectedTo,
1855 BasePinImpl_ConnectionMediaType,
1856 BasePinImpl_QueryPinInfo,
1857 BasePinImpl_QueryDirection,
1858 BasePinImpl_QueryId,
1859 GST_OutPin_QueryAccept,
1860 BasePinImpl_EnumMediaTypes,
1861 BasePinImpl_QueryInternalConnections,
1862 BaseOutputPinImpl_EndOfStream,
1863 BaseOutputPinImpl_BeginFlush,
1864 BaseOutputPinImpl_EndFlush,
1865 BasePinImpl_NewSegment
1868 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
1870 NULL,
1871 BaseOutputPinImpl_AttemptConnection,
1872 BasePinImpl_GetMediaTypeVersion,
1873 GSTOutPin_GetMediaType
1875 GSTOutPin_DecideBufferSize,
1876 GSTOutPin_DecideAllocator,
1877 GSTOutPin_BreakConnect
1880 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt)
1882 HRESULT hr;
1883 This->ppPins = CoTaskMemRealloc(This->ppPins, (This->cStreams + 1) * sizeof(IPin *));
1885 hr = BaseOutputPin_Construct(&GST_OutputPin_Vtbl, sizeof(GSTOutPin), piOutput, &output_BaseOutputFuncTable, &This->filter.csFilter, (IPin**)(This->ppPins + This->cStreams));
1886 if (SUCCEEDED(hr)) {
1887 GSTOutPin *pin = This->ppPins[This->cStreams];
1888 memset((char*)pin + sizeof(pin->pin), 0, sizeof(GSTOutPin) - sizeof(pin->pin));
1889 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1890 CopyMediaType(pin->pmt, amt);
1891 pin->pin.pin.pinInfo.pFilter = &This->filter.IBaseFilter_iface;
1892 pin->caps_event = CreateEventW(NULL, 0, 0, NULL);
1893 pin->segment = gst_segment_new();
1894 This->cStreams++;
1895 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1896 SourceSeeking_Init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop, GST_ChangeCurrent, GST_ChangeRate, &This->filter.csFilter);
1897 BaseFilterImpl_IncrementPinVersion(&This->filter);
1898 } else
1899 ERR("Failed with error %x\n", hr);
1900 return hr;
1903 static HRESULT GST_RemoveOutputPins(GSTImpl *This)
1905 HRESULT hr;
1906 ULONG i;
1907 GSTOutPin **ppOldPins = This->ppPins;
1909 TRACE("(%p)\n", This);
1910 mark_wine_thread();
1912 if (!This->container)
1913 return S_OK;
1914 gst_element_set_state(This->container, GST_STATE_NULL);
1915 gst_pad_unlink(This->my_src, This->their_sink);
1916 gst_object_unref(This->my_src);
1917 gst_object_unref(This->their_sink);
1918 This->my_src = This->their_sink = NULL;
1920 for (i = 0; i < This->cStreams; i++) {
1921 hr = BaseOutputPinImpl_BreakConnect(&ppOldPins[i]->pin);
1922 TRACE("Disconnect: %08x\n", hr);
1923 IPin_Release(&ppOldPins[i]->pin.pin.IPin_iface);
1925 This->cStreams = 0;
1926 This->ppPins = NULL;
1927 gst_element_set_bus(This->container, NULL);
1928 gst_object_unref(This->container);
1929 This->container = NULL;
1930 BaseFilterImpl_IncrementPinVersion(&This->filter);
1931 CoTaskMemFree(ppOldPins);
1932 return S_OK;
1935 static ULONG WINAPI GSTInPin_Release(IPin *iface)
1937 GSTInPin *This = (GSTInPin*)iface;
1938 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1940 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
1941 if (!refCount) {
1942 FreeMediaType(&This->pin.mtCurrent);
1943 if (This->pAlloc)
1944 IMemAllocator_Release(This->pAlloc);
1945 This->pAlloc = NULL;
1946 This->pin.IPin_iface.lpVtbl = NULL;
1947 return 0;
1948 } else
1949 return refCount;
1952 static HRESULT WINAPI GSTInPin_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
1954 PIN_DIRECTION pindirReceive;
1955 HRESULT hr = S_OK;
1956 GSTInPin *This = (GSTInPin*)iface;
1958 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1959 dump_AM_MEDIA_TYPE(pmt);
1961 mark_wine_thread();
1963 EnterCriticalSection(This->pin.pCritSec);
1964 if (!This->pin.pConnectedTo) {
1965 ALLOCATOR_PROPERTIES props;
1966 IMemAllocator *pAlloc = NULL;
1968 props.cBuffers = 8;
1969 props.cbBuffer = 16384;
1970 props.cbAlign = 1;
1971 props.cbPrefix = 0;
1973 if (IPin_QueryAccept(iface, pmt) != S_OK)
1974 hr = VFW_E_TYPE_NOT_ACCEPTED;
1976 if (SUCCEEDED(hr)) {
1977 IPin_QueryDirection(pReceivePin, &pindirReceive);
1978 if (pindirReceive != PINDIR_OUTPUT) {
1979 ERR("Can't connect from non-output pin\n");
1980 hr = VFW_E_INVALID_DIRECTION;
1984 This->pReader = NULL;
1985 This->pAlloc = NULL;
1986 if (SUCCEEDED(hr))
1987 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1988 if (SUCCEEDED(hr))
1989 hr = GST_Connect(This, pReceivePin, &props);
1991 /* A certain IAsyncReader::RequestAllocator expects to be passed
1992 non-NULL preferred allocator */
1993 if (SUCCEEDED(hr))
1994 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC,
1995 &IID_IMemAllocator, (LPVOID *)&pAlloc);
1996 if (SUCCEEDED(hr)) {
1997 hr = IAsyncReader_RequestAllocator(This->pReader, pAlloc, &props, &This->pAlloc);
1998 if (FAILED(hr))
1999 WARN("Can't get an allocator, got %08x\n", hr);
2001 if (pAlloc)
2002 IMemAllocator_Release(pAlloc);
2003 if (SUCCEEDED(hr)) {
2004 CopyMediaType(&This->pin.mtCurrent, pmt);
2005 This->pin.pConnectedTo = pReceivePin;
2006 IPin_AddRef(pReceivePin);
2007 hr = IMemAllocator_Commit(This->pAlloc);
2008 SetEvent(((GSTImpl*)This->pin.pinInfo.pFilter)->event);
2009 } else {
2010 GST_RemoveOutputPins((GSTImpl *)This->pin.pinInfo.pFilter);
2011 if (This->pReader)
2012 IAsyncReader_Release(This->pReader);
2013 This->pReader = NULL;
2014 if (This->pAlloc)
2015 IMemAllocator_Release(This->pAlloc);
2016 This->pAlloc = NULL;
2018 TRACE("Size: %i\n", props.cbBuffer);
2019 } else
2020 hr = VFW_E_ALREADY_CONNECTED;
2021 LeaveCriticalSection(This->pin.pCritSec);
2022 return hr;
2025 static HRESULT WINAPI GSTInPin_Disconnect(IPin *iface)
2027 HRESULT hr;
2028 GSTInPin *This = (GSTInPin*)iface;
2029 FILTER_STATE state;
2031 TRACE("(%p)\n", This);
2033 mark_wine_thread();
2035 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
2036 EnterCriticalSection(This->pin.pCritSec);
2037 if (This->pin.pConnectedTo) {
2038 GSTImpl *Parser = (GSTImpl *)This->pin.pinInfo.pFilter;
2040 if (SUCCEEDED(hr) && state == State_Stopped) {
2041 IMemAllocator_Decommit(This->pAlloc);
2042 IPin_Disconnect(This->pin.pConnectedTo);
2043 This->pin.pConnectedTo = NULL;
2044 hr = GST_RemoveOutputPins(Parser);
2045 } else
2046 hr = VFW_E_NOT_STOPPED;
2047 } else
2048 hr = S_FALSE;
2049 LeaveCriticalSection(This->pin.pCritSec);
2050 return hr;
2053 static HRESULT WINAPI GSTInPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
2055 GSTInPin *This = (GSTInPin*)iface;
2057 TRACE("(%p)->(%p)\n", This, pmt);
2058 dump_AM_MEDIA_TYPE(pmt);
2060 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
2061 return S_OK;
2062 return S_FALSE;
2065 static HRESULT WINAPI GSTInPin_EndOfStream(IPin *iface)
2067 GSTInPin *pin = (GSTInPin*)iface;
2068 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2070 FIXME("Propagate message on %p\n", This);
2071 return S_OK;
2074 static HRESULT WINAPI GSTInPin_BeginFlush(IPin *iface)
2076 GSTInPin *pin = (GSTInPin*)iface;
2077 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2079 FIXME("Propagate message on %p\n", This);
2080 return S_OK;
2083 static HRESULT WINAPI GSTInPin_EndFlush(IPin *iface)
2085 GSTInPin *pin = (GSTInPin*)iface;
2086 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2088 FIXME("Propagate message on %p\n", This);
2089 return S_OK;
2092 static HRESULT WINAPI GSTInPin_NewSegment(IPin *iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
2094 GSTInPin *pin = (GSTInPin*)iface;
2095 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2097 BasePinImpl_NewSegment(iface, tStart, tStop, dRate);
2098 FIXME("Propagate message on %p\n", This);
2099 return S_OK;
2102 static HRESULT WINAPI GSTInPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
2104 GSTInPin *This = (GSTInPin*)iface;
2106 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
2108 *ppv = NULL;
2110 if (IsEqualIID(riid, &IID_IUnknown))
2111 *ppv = iface;
2112 else if (IsEqualIID(riid, &IID_IPin))
2113 *ppv = iface;
2114 else if (IsEqualIID(riid, &IID_IMediaSeeking))
2116 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
2119 if (*ppv)
2121 IUnknown_AddRef((IUnknown *)(*ppv));
2122 return S_OK;
2125 FIXME("No interface for %s!\n", debugstr_guid(riid));
2127 return E_NOINTERFACE;
2130 static HRESULT WINAPI GSTInPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
2132 BasePin *This = (BasePin *)iface;
2134 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
2136 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
2139 static const IPinVtbl GST_InputPin_Vtbl = {
2140 GSTInPin_QueryInterface,
2141 BasePinImpl_AddRef,
2142 GSTInPin_Release,
2143 BaseInputPinImpl_Connect,
2144 GSTInPin_ReceiveConnection,
2145 GSTInPin_Disconnect,
2146 BasePinImpl_ConnectedTo,
2147 BasePinImpl_ConnectionMediaType,
2148 BasePinImpl_QueryPinInfo,
2149 BasePinImpl_QueryDirection,
2150 BasePinImpl_QueryId,
2151 GSTInPin_QueryAccept,
2152 GSTInPin_EnumMediaTypes,
2153 BasePinImpl_QueryInternalConnections,
2154 GSTInPin_EndOfStream,
2155 GSTInPin_BeginFlush,
2156 GSTInPin_EndFlush,
2157 GSTInPin_NewSegment
2160 pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
2161 pthread_cond_t cb_list_cond = PTHREAD_COND_INITIALIZER;
2162 struct list cb_list = LIST_INIT(cb_list);
2164 void CALLBACK perform_cb(TP_CALLBACK_INSTANCE *instance, void *user)
2166 struct cb_data *cbdata = user;
2168 TRACE("got cb type: 0x%x\n", cbdata->type);
2170 switch(cbdata->type)
2172 case WATCH_BUS:
2174 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2175 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2176 break;
2178 case EXISTING_NEW_PAD:
2180 struct existing_new_pad_data *data = &cbdata->u.existing_new_pad_data;
2181 existing_new_pad(data->bin, data->pad, data->user);
2182 break;
2184 case QUERY_FUNCTION:
2186 struct query_function_data *data = &cbdata->u.query_function_data;
2187 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2188 break;
2190 case ACTIVATE_MODE:
2192 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2193 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2194 break;
2196 case NO_MORE_PADS:
2198 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2199 no_more_pads(data->decodebin, data->user);
2200 break;
2202 case REQUEST_BUFFER_SRC:
2204 struct request_buffer_src_data *data = &cbdata->u.request_buffer_src_data;
2205 cbdata->u.request_buffer_src_data.ret = request_buffer_src(data->pad, data->parent,
2206 data->ofs, data->len, data->buf);
2207 break;
2209 case EVENT_SRC:
2211 struct event_src_data *data = &cbdata->u.event_src_data;
2212 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2213 break;
2215 case EVENT_SINK:
2217 struct event_sink_data *data = &cbdata->u.event_sink_data;
2218 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2219 break;
2221 case ACCEPT_CAPS_SINK:
2223 struct accept_caps_sink_data *data = &cbdata->u.accept_caps_sink_data;
2224 cbdata->u.accept_caps_sink_data.ret = accept_caps_sink(data->pad, data->caps);
2225 break;
2227 case SETCAPS_SINK:
2229 struct setcaps_sink_data *data = &cbdata->u.setcaps_sink_data;
2230 cbdata->u.setcaps_sink_data.ret = setcaps_sink(data->pad, data->caps);
2231 break;
2233 case GOT_DATA_SINK:
2235 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2236 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2237 break;
2239 case GOT_DATA:
2241 struct got_data_data *data = &cbdata->u.got_data_data;
2242 cbdata->u.got_data_data.ret = got_data(data->pad, data->parent, data->buf);
2243 break;
2245 case REMOVED_DECODED_PAD:
2247 struct removed_decoded_pad_data *data = &cbdata->u.removed_decoded_pad_data;
2248 removed_decoded_pad(data->bin, data->pad, data->user);
2249 break;
2251 case AUTOPLUG_BLACKLIST:
2253 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2254 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2255 data->pad, data->caps, data->fact, data->user);
2256 break;
2258 case UNKNOWN_TYPE:
2260 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2261 unknown_type(data->bin, data->pad, data->caps, data->user);
2262 break;
2264 case RELEASE_SAMPLE:
2266 struct release_sample_data *data = &cbdata->u.release_sample_data;
2267 release_sample(data->data);
2268 break;
2270 case TRANSFORM_PAD_ADDED:
2272 struct transform_pad_added_data *data = &cbdata->u.transform_pad_added_data;
2273 Gstreamer_transform_pad_added(data->filter, data->pad, data->user);
2274 break;
2276 case QUERY_SINK:
2278 struct query_sink_data *data = &cbdata->u.query_sink_data;
2279 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2280 data->query);
2281 break;
2285 pthread_mutex_lock(&cbdata->lock);
2286 cbdata->finished = 1;
2287 pthread_cond_broadcast(&cbdata->cond);
2288 pthread_mutex_unlock(&cbdata->lock);
2291 static DWORD WINAPI dispatch_thread(void *user)
2293 struct cb_data *cbdata;
2295 pthread_mutex_lock(&cb_list_lock);
2297 while(1){
2298 pthread_cond_wait(&cb_list_cond, &cb_list_lock);
2300 while(!list_empty(&cb_list)){
2301 cbdata = LIST_ENTRY(list_head(&cb_list), struct cb_data, entry);
2302 list_remove(&cbdata->entry);
2304 TrySubmitThreadpoolCallback(&perform_cb, cbdata, NULL);
2308 pthread_mutex_unlock(&cb_list_lock);
2310 return 0;
2313 void start_dispatch_thread(void)
2315 pthread_key_create(&wine_gst_key, NULL);
2316 CloseHandle(CreateThread(NULL, 0, &dispatch_thread, NULL, 0, NULL));