mshtml: Don't crash creating a URI if we have no document.
[wine.git] / dlls / winegstreamer / gstdemux.c
blobf25720261f1f302b6dfdd1884fae43730ac627fb
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 = (IBaseFilter *)This;
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 TRACE("%p: Asking for pos %x\n", This, pos);
1198 if (pos > This->cStreams || pos < 0)
1199 return NULL;
1200 if (!pos)
1202 IPin_AddRef((IPin*)&This->pInputPin);
1203 return (IPin*)&This->pInputPin;
1205 else
1207 IPin_AddRef((IPin*)This->ppPins[pos - 1]);
1208 return (IPin*)This->ppPins[pos - 1];
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 obj = (IUnknown*)This;
1242 if (!This)
1244 *phr = E_OUTOFMEMORY;
1245 return NULL;
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 = (IBaseFilter *)This;
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((IPin *)&This->pInputPin);
1289 assert(hr == S_OK);
1291 pinref = IPin_Release((IPin *)&This->pInputPin);
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((IPin *)&This->pInputPin);
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 *ppv = This;
1318 else if (IsEqualIID(riid, &IID_IPersist))
1319 *ppv = This;
1320 else if (IsEqualIID(riid, &IID_IMediaFilter))
1321 *ppv = This;
1322 else if (IsEqualIID(riid, &IID_IBaseFilter))
1323 *ppv = This;
1325 if (*ppv) {
1326 IUnknown_AddRef((IUnknown *)(*ppv));
1327 return S_OK;
1330 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
1331 !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
1332 FIXME("No interface for %s!\n", debugstr_guid(riid));
1334 return E_NOINTERFACE;
1337 static ULONG WINAPI GST_Release(IBaseFilter *iface)
1339 GSTImpl *This = (GSTImpl *)iface;
1340 ULONG refCount = InterlockedDecrement(&This->filter.refCount);
1342 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1344 if (!refCount)
1345 GST_Destroy(This);
1347 return refCount;
1350 static HRESULT WINAPI GST_Stop(IBaseFilter *iface)
1352 GSTImpl *This = (GSTImpl *)iface;
1354 TRACE("(%p)\n", This);
1356 mark_wine_thread();
1358 if (This->container) {
1359 This->ignore_flush = TRUE;
1360 gst_element_set_state(This->container, GST_STATE_READY);
1361 gst_element_get_state(This->container, NULL, NULL, -1);
1362 This->ignore_flush = FALSE;
1364 return S_OK;
1367 static HRESULT WINAPI GST_Pause(IBaseFilter *iface)
1369 HRESULT hr = S_OK;
1370 GSTImpl *This = (GSTImpl *)iface;
1371 GstState now;
1372 GstStateChangeReturn ret;
1374 TRACE("(%p)\n", This);
1376 if (!This->container)
1377 return VFW_E_NOT_CONNECTED;
1379 mark_wine_thread();
1381 gst_element_get_state(This->container, &now, NULL, -1);
1382 if (now == GST_STATE_PAUSED)
1383 return S_OK;
1384 if (now != GST_STATE_PLAYING)
1385 hr = IBaseFilter_Run(iface, -1);
1386 if (FAILED(hr))
1387 return hr;
1388 ret = gst_element_set_state(This->container, GST_STATE_PAUSED);
1389 if (ret == GST_STATE_CHANGE_ASYNC)
1390 hr = S_FALSE;
1391 return hr;
1394 static HRESULT WINAPI GST_Run(IBaseFilter *iface, REFERENCE_TIME tStart)
1396 HRESULT hr = S_OK;
1397 GSTImpl *This = (GSTImpl *)iface;
1398 ULONG i;
1399 GstState now;
1400 HRESULT hr_any = VFW_E_NOT_CONNECTED;
1402 TRACE("(%p)->(%s)\n", This, wine_dbgstr_longlong(tStart));
1404 mark_wine_thread();
1406 if (!This->container)
1407 return VFW_E_NOT_CONNECTED;
1409 EnterCriticalSection(&This->filter.csFilter);
1410 This->filter.rtStreamStart = tStart;
1411 LeaveCriticalSection(&This->filter.csFilter);
1413 gst_element_get_state(This->container, &now, NULL, -1);
1414 if (now == GST_STATE_PLAYING)
1415 return S_OK;
1416 if (now == GST_STATE_PAUSED) {
1417 GstStateChangeReturn ret;
1418 ret = gst_element_set_state(This->container, GST_STATE_PLAYING);
1419 if (ret == GST_STATE_CHANGE_ASYNC)
1420 return S_FALSE;
1421 return S_OK;
1424 EnterCriticalSection(&This->filter.csFilter);
1425 gst_element_set_state(This->container, GST_STATE_PLAYING);
1426 This->filter.rtStreamStart = tStart;
1428 for (i = 0; i < This->cStreams; i++) {
1429 hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[i]);
1430 if (SUCCEEDED(hr)) {
1431 hr_any = hr;
1434 hr = hr_any;
1435 LeaveCriticalSection(&This->filter.csFilter);
1437 return hr;
1440 static HRESULT WINAPI GST_GetState(IBaseFilter *iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
1442 GSTImpl *This = (GSTImpl *)iface;
1443 HRESULT hr = S_OK;
1444 GstState now, pending;
1445 GstStateChangeReturn ret;
1447 TRACE("(%p)->(%d, %p)\n", This, dwMilliSecsTimeout, pState);
1449 mark_wine_thread();
1451 if (!This->container) {
1452 *pState = State_Stopped;
1453 return S_OK;
1456 ret = gst_element_get_state(This->container, &now, &pending, dwMilliSecsTimeout == INFINITE ? -1 : dwMilliSecsTimeout * 1000);
1458 if (ret == GST_STATE_CHANGE_ASYNC)
1459 hr = VFW_S_STATE_INTERMEDIATE;
1460 else
1461 pending = now;
1463 switch (pending) {
1464 case GST_STATE_PAUSED: *pState = State_Paused; return hr;
1465 case GST_STATE_PLAYING: *pState = State_Running; return hr;
1466 default: *pState = State_Stopped; return hr;
1470 static HRESULT WINAPI GST_FindPin(IBaseFilter *iface, LPCWSTR Id, IPin **ppPin)
1472 FIXME("(%p)->(%s,%p) stub\n", iface, debugstr_w(Id), ppPin);
1473 return E_NOTIMPL;
1476 static const IBaseFilterVtbl GST_Vtbl = {
1477 GST_QueryInterface,
1478 BaseFilterImpl_AddRef,
1479 GST_Release,
1480 BaseFilterImpl_GetClassID,
1481 GST_Stop,
1482 GST_Pause,
1483 GST_Run,
1484 GST_GetState,
1485 BaseFilterImpl_SetSyncSource,
1486 BaseFilterImpl_GetSyncSource,
1487 BaseFilterImpl_EnumPins,
1488 GST_FindPin,
1489 BaseFilterImpl_QueryFilterInfo,
1490 BaseFilterImpl_JoinFilterGraph,
1491 BaseFilterImpl_QueryVendorInfo
1494 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface)
1496 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1497 TRACE("(%p)\n", This);
1498 return S_OK;
1501 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface)
1503 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1504 TRACE("(%p)\n", This);
1505 return S_OK;
1508 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface)
1510 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1511 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1512 TRACE("(%p) New rate %g\n", This, This->seek.dRate);
1513 mark_wine_thread();
1514 gst_pad_push_event(This->my_sink, ev);
1515 return S_OK;
1518 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv)
1520 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1521 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1524 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface)
1526 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1527 return IUnknown_AddRef((IUnknown *)This);
1530 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface)
1532 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1533 return IUnknown_Release((IUnknown *)This);
1536 static HRESULT WINAPI GST_Seeking_GetCurrentPosition(IMediaSeeking *iface, REFERENCE_TIME *pos)
1538 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1540 TRACE("(%p)->(%p)\n", This, pos);
1542 if (!pos)
1543 return E_POINTER;
1545 mark_wine_thread();
1547 if (!This->their_src) {
1548 *pos = This->seek.llCurrent;
1549 TRACE("Cached value\n");
1550 if (This->seek.llDuration)
1551 return S_OK;
1552 else
1553 return E_NOTIMPL;
1556 if (!gst_pad_query_position(This->their_src, GST_FORMAT_TIME, pos)) {
1557 WARN("Could not query position\n");
1558 return E_NOTIMPL;
1560 *pos /= 100;
1561 This->seek.llCurrent = *pos;
1562 return S_OK;
1565 static GstSeekType type_from_flags(DWORD flags)
1567 switch (flags & AM_SEEKING_PositioningBitsMask) {
1568 case AM_SEEKING_NoPositioning:
1569 return GST_SEEK_TYPE_NONE;
1570 case AM_SEEKING_AbsolutePositioning:
1571 case AM_SEEKING_RelativePositioning:
1572 return GST_SEEK_TYPE_SET;
1573 case AM_SEEKING_IncrementalPositioning:
1574 return GST_SEEK_TYPE_END;
1576 return GST_SEEK_TYPE_NONE;
1579 static HRESULT WINAPI GST_Seeking_SetPositions(IMediaSeeking *iface,
1580 REFERENCE_TIME *pCur, DWORD curflags, REFERENCE_TIME *pStop,
1581 DWORD stopflags)
1583 HRESULT hr;
1584 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1585 GstSeekFlags f = 0;
1586 GstSeekType curtype, stoptype;
1587 GstEvent *e;
1588 gint64 stop_pos = 0, curr_pos = 0;
1590 TRACE("(%p)->(%p, 0x%x, %p, 0x%x)\n", This, pCur, curflags, pStop, stopflags);
1592 mark_wine_thread();
1594 if (!This->seek.llDuration)
1595 return E_NOTIMPL;
1597 hr = SourceSeekingImpl_SetPositions(iface, pCur, curflags, pStop, stopflags);
1598 if (!This->their_src)
1599 return hr;
1601 curtype = type_from_flags(curflags);
1602 stoptype = type_from_flags(stopflags);
1603 if (curflags & AM_SEEKING_SeekToKeyFrame)
1604 f |= GST_SEEK_FLAG_KEY_UNIT;
1605 if (curflags & AM_SEEKING_Segment)
1606 f |= GST_SEEK_FLAG_SEGMENT;
1607 if (!(curflags & AM_SEEKING_NoFlush))
1608 f |= GST_SEEK_FLAG_FLUSH;
1610 if (((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning) ||
1611 ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)) {
1612 gint64 tmp_pos;
1613 gst_pad_query_position (This->my_sink, GST_FORMAT_TIME, &tmp_pos);
1614 if ((curflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1615 curr_pos = tmp_pos;
1616 if ((stopflags & AM_SEEKING_PositioningBitsMask) == AM_SEEKING_RelativePositioning)
1617 stop_pos = tmp_pos;
1620 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);
1621 if (gst_pad_push_event(This->my_sink, e))
1622 return S_OK;
1623 else
1624 return E_NOTIMPL;
1627 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1629 GST_Seeking_QueryInterface,
1630 GST_Seeking_AddRef,
1631 GST_Seeking_Release,
1632 SourceSeekingImpl_GetCapabilities,
1633 SourceSeekingImpl_CheckCapabilities,
1634 SourceSeekingImpl_IsFormatSupported,
1635 SourceSeekingImpl_QueryPreferredFormat,
1636 SourceSeekingImpl_GetTimeFormat,
1637 SourceSeekingImpl_IsUsingTimeFormat,
1638 SourceSeekingImpl_SetTimeFormat,
1639 SourceSeekingImpl_GetDuration,
1640 SourceSeekingImpl_GetStopPosition,
1641 GST_Seeking_GetCurrentPosition,
1642 SourceSeekingImpl_ConvertTimeFormat,
1643 GST_Seeking_SetPositions,
1644 SourceSeekingImpl_GetPositions,
1645 SourceSeekingImpl_GetAvailable,
1646 SourceSeekingImpl_SetRate,
1647 SourceSeekingImpl_GetRate,
1648 SourceSeekingImpl_GetPreroll
1651 static inline GSTOutPin *impl_from_IQualityControl( IQualityControl *iface )
1653 return (GSTOutPin*)CONTAINING_RECORD(iface, GSTOutPin, IQualityControl_iface);
1656 static HRESULT WINAPI GST_QualityControl_QueryInterface(IQualityControl *iface, REFIID riid, void **ppv)
1658 GSTOutPin *pin = impl_from_IQualityControl(iface);
1659 return IPin_QueryInterface((IPin*)pin, riid, ppv);
1662 static ULONG WINAPI GST_QualityControl_AddRef(IQualityControl *iface)
1664 GSTOutPin *pin = impl_from_IQualityControl(iface);
1665 return IPin_AddRef((IPin*)pin);
1668 static ULONG WINAPI GST_QualityControl_Release(IQualityControl *iface)
1670 GSTOutPin *pin = impl_from_IQualityControl(iface);
1671 return IPin_Release((IPin*)pin);
1674 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm)
1676 GSTOutPin *pin = impl_from_IQualityControl(iface);
1677 GstEvent *evt;
1679 TRACE("(%p)->(%p, { 0x%x %u %s %s })\n", pin, sender,
1680 qm.Type, qm.Proportion,
1681 wine_dbgstr_longlong(qm.Late),
1682 wine_dbgstr_longlong(qm.TimeStamp));
1684 mark_wine_thread();
1686 if (qm.Type == Flood)
1687 qm.Late = 0;
1689 evt = gst_event_new_qos(qm.Type == Famine ? GST_QOS_TYPE_UNDERFLOW : GST_QOS_TYPE_OVERFLOW,
1690 qm.Proportion / 1000., qm.Late * 100, qm.TimeStamp * 100);
1692 if (!evt) {
1693 WARN("Failed to create QOS event\n");
1694 return E_INVALIDARG;
1697 gst_pad_push_event(pin->my_sink, evt);
1699 return S_OK;
1702 static HRESULT WINAPI GST_QualityControl_SetSink(IQualityControl *iface, IQualityControl *tonotify)
1704 GSTOutPin *pin = impl_from_IQualityControl(iface);
1705 TRACE("(%p)->(%p)\n", pin, pin);
1706 /* Do nothing */
1707 return S_OK;
1710 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1711 GST_QualityControl_QueryInterface,
1712 GST_QualityControl_AddRef,
1713 GST_QualityControl_Release,
1714 GST_QualityControl_Notify,
1715 GST_QualityControl_SetSink
1718 static HRESULT WINAPI GSTOutPin_QueryInterface(IPin *iface, REFIID riid, void **ppv)
1720 GSTOutPin *This = (GSTOutPin *)iface;
1722 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1724 *ppv = NULL;
1726 if (IsEqualIID(riid, &IID_IUnknown))
1727 *ppv = iface;
1728 else if (IsEqualIID(riid, &IID_IPin))
1729 *ppv = iface;
1730 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1731 *ppv = &This->seek;
1732 else if (IsEqualIID(riid, &IID_IQualityControl))
1733 *ppv = &This->IQualityControl_iface;
1735 if (*ppv) {
1736 IUnknown_AddRef((IUnknown *)(*ppv));
1737 return S_OK;
1739 FIXME("No interface for %s!\n", debugstr_guid(riid));
1740 return E_NOINTERFACE;
1743 static ULONG WINAPI GSTOutPin_Release(IPin *iface)
1745 GSTOutPin *This = (GSTOutPin *)iface;
1746 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
1748 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1750 mark_wine_thread();
1752 if (!refCount) {
1753 if (This->their_src) {
1754 if (This->flipfilter) {
1755 gst_pad_unlink(This->their_src, This->flip_sink);
1756 gst_pad_unlink(This->flip_src, This->my_sink);
1757 gst_object_unref(This->flip_src);
1758 gst_object_unref(This->flip_sink);
1759 This->flipfilter = NULL;
1760 This->flip_src = This->flip_sink = NULL;
1761 } else
1762 gst_pad_unlink(This->their_src, This->my_sink);
1763 gst_object_unref(This->their_src);
1765 gst_object_unref(This->my_sink);
1766 CloseHandle(This->caps_event);
1767 DeleteMediaType(This->pmt);
1768 FreeMediaType(&This->pin.pin.mtCurrent);
1769 gst_segment_free(This->segment);
1770 if(This->gstpool)
1771 gst_object_unref(This->gstpool);
1772 if (This->pin.pAllocator)
1773 IMemAllocator_Release(This->pin.pAllocator);
1774 CoTaskMemFree(This);
1775 return 0;
1777 return refCount;
1780 static HRESULT WINAPI GSTOutPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
1782 GSTOutPin *This = (GSTOutPin *)iface;
1784 TRACE("(%p)->(%i, %p)\n", This, iPosition, pmt);
1786 if (iPosition < 0)
1787 return E_INVALIDARG;
1789 if (iPosition > 0)
1790 return VFW_S_NO_MORE_ITEMS;
1792 CopyMediaType(pmt, This->pmt);
1794 return S_OK;
1797 static HRESULT WINAPI GSTOutPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
1799 GSTOutPin *This = (GSTOutPin *)iface;
1800 TRACE("(%p)->(%p, %p)\n", This, pAlloc, ppropInputRequest);
1801 /* Unused */
1802 return S_OK;
1805 static HRESULT WINAPI GSTOutPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
1807 HRESULT hr;
1808 GSTOutPin *This = (GSTOutPin *)iface;
1809 GSTImpl *GSTfilter = (GSTImpl*)This->pin.pin.pinInfo.pFilter;
1811 TRACE("(%p)->(%p, %p)\n", This, pPin, pAlloc);
1813 *pAlloc = NULL;
1814 if (GSTfilter->pInputPin.pAlloc)
1816 hr = IMemInputPin_NotifyAllocator(pPin, GSTfilter->pInputPin.pAlloc, FALSE);
1817 if (SUCCEEDED(hr))
1819 *pAlloc = GSTfilter->pInputPin.pAlloc;
1820 IMemAllocator_AddRef(*pAlloc);
1823 else
1824 hr = VFW_E_NO_ALLOCATOR;
1826 return hr;
1829 static HRESULT WINAPI GSTOutPin_BreakConnect(BaseOutputPin *This)
1831 HRESULT hr;
1833 TRACE("(%p)->()\n", This);
1835 EnterCriticalSection(This->pin.pCritSec);
1836 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1837 hr = VFW_E_NOT_CONNECTED;
1838 else
1840 hr = IPin_Disconnect(This->pin.pConnectedTo);
1841 IPin_Disconnect((IPin *)This);
1843 LeaveCriticalSection(This->pin.pCritSec);
1845 return hr;
1848 static const IPinVtbl GST_OutputPin_Vtbl = {
1849 GSTOutPin_QueryInterface,
1850 BasePinImpl_AddRef,
1851 GSTOutPin_Release,
1852 BaseOutputPinImpl_Connect,
1853 BaseOutputPinImpl_ReceiveConnection,
1854 BaseOutputPinImpl_Disconnect,
1855 BasePinImpl_ConnectedTo,
1856 BasePinImpl_ConnectionMediaType,
1857 BasePinImpl_QueryPinInfo,
1858 BasePinImpl_QueryDirection,
1859 BasePinImpl_QueryId,
1860 GST_OutPin_QueryAccept,
1861 BasePinImpl_EnumMediaTypes,
1862 BasePinImpl_QueryInternalConnections,
1863 BaseOutputPinImpl_EndOfStream,
1864 BaseOutputPinImpl_BeginFlush,
1865 BaseOutputPinImpl_EndFlush,
1866 BasePinImpl_NewSegment
1869 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
1871 NULL,
1872 BaseOutputPinImpl_AttemptConnection,
1873 BasePinImpl_GetMediaTypeVersion,
1874 GSTOutPin_GetMediaType
1876 GSTOutPin_DecideBufferSize,
1877 GSTOutPin_DecideAllocator,
1878 GSTOutPin_BreakConnect
1881 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt)
1883 HRESULT hr;
1884 This->ppPins = CoTaskMemRealloc(This->ppPins, (This->cStreams + 1) * sizeof(IPin *));
1886 hr = BaseOutputPin_Construct(&GST_OutputPin_Vtbl, sizeof(GSTOutPin), piOutput, &output_BaseOutputFuncTable, &This->filter.csFilter, (IPin**)(This->ppPins + This->cStreams));
1887 if (SUCCEEDED(hr)) {
1888 GSTOutPin *pin = This->ppPins[This->cStreams];
1889 memset((char*)pin + sizeof(pin->pin), 0, sizeof(GSTOutPin) - sizeof(pin->pin));
1890 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1891 CopyMediaType(pin->pmt, amt);
1892 pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
1893 pin->caps_event = CreateEventW(NULL, 0, 0, NULL);
1894 pin->segment = gst_segment_new();
1895 This->cStreams++;
1896 pin->IQualityControl_iface.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1897 SourceSeeking_Init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop, GST_ChangeCurrent, GST_ChangeRate, &This->filter.csFilter);
1898 BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
1899 } else
1900 ERR("Failed with error %x\n", hr);
1901 return hr;
1904 static HRESULT GST_RemoveOutputPins(GSTImpl *This)
1906 HRESULT hr;
1907 ULONG i;
1908 GSTOutPin **ppOldPins = This->ppPins;
1910 TRACE("(%p)\n", This);
1911 mark_wine_thread();
1913 if (!This->container)
1914 return S_OK;
1915 gst_element_set_state(This->container, GST_STATE_NULL);
1916 gst_pad_unlink(This->my_src, This->their_sink);
1917 gst_object_unref(This->my_src);
1918 gst_object_unref(This->their_sink);
1919 This->my_src = This->their_sink = NULL;
1921 for (i = 0; i < This->cStreams; i++) {
1922 hr = BaseOutputPinImpl_BreakConnect(&ppOldPins[i]->pin);
1923 TRACE("Disconnect: %08x\n", hr);
1924 IPin_Release((IPin*)ppOldPins[i]);
1926 This->cStreams = 0;
1927 This->ppPins = NULL;
1928 gst_element_set_bus(This->container, NULL);
1929 gst_object_unref(This->container);
1930 This->container = NULL;
1931 BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
1932 CoTaskMemFree(ppOldPins);
1933 return S_OK;
1936 static ULONG WINAPI GSTInPin_Release(IPin *iface)
1938 GSTInPin *This = (GSTInPin*)iface;
1939 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1941 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
1942 if (!refCount) {
1943 FreeMediaType(&This->pin.mtCurrent);
1944 if (This->pAlloc)
1945 IMemAllocator_Release(This->pAlloc);
1946 This->pAlloc = NULL;
1947 This->pin.IPin_iface.lpVtbl = NULL;
1948 return 0;
1949 } else
1950 return refCount;
1953 static HRESULT WINAPI GSTInPin_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
1955 PIN_DIRECTION pindirReceive;
1956 HRESULT hr = S_OK;
1957 GSTInPin *This = (GSTInPin*)iface;
1959 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1960 dump_AM_MEDIA_TYPE(pmt);
1962 mark_wine_thread();
1964 EnterCriticalSection(This->pin.pCritSec);
1965 if (!This->pin.pConnectedTo) {
1966 ALLOCATOR_PROPERTIES props;
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);
1990 if (SUCCEEDED(hr))
1991 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1992 if (SUCCEEDED(hr)) {
1993 CopyMediaType(&This->pin.mtCurrent, pmt);
1994 This->pin.pConnectedTo = pReceivePin;
1995 IPin_AddRef(pReceivePin);
1996 hr = IMemAllocator_Commit(This->pAlloc);
1997 SetEvent(((GSTImpl*)This->pin.pinInfo.pFilter)->event);
1998 } else {
1999 GST_RemoveOutputPins((GSTImpl *)This->pin.pinInfo.pFilter);
2000 if (This->pReader)
2001 IAsyncReader_Release(This->pReader);
2002 This->pReader = NULL;
2003 if (This->pAlloc)
2004 IMemAllocator_Release(This->pAlloc);
2005 This->pAlloc = NULL;
2007 TRACE("Size: %i\n", props.cbBuffer);
2008 } else
2009 hr = VFW_E_ALREADY_CONNECTED;
2010 LeaveCriticalSection(This->pin.pCritSec);
2011 return hr;
2014 static HRESULT WINAPI GSTInPin_Disconnect(IPin *iface)
2016 HRESULT hr;
2017 GSTInPin *This = (GSTInPin*)iface;
2018 FILTER_STATE state;
2020 TRACE("(%p)\n", This);
2022 mark_wine_thread();
2024 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
2025 EnterCriticalSection(This->pin.pCritSec);
2026 if (This->pin.pConnectedTo) {
2027 GSTImpl *Parser = (GSTImpl *)This->pin.pinInfo.pFilter;
2029 if (SUCCEEDED(hr) && state == State_Stopped) {
2030 IMemAllocator_Decommit(This->pAlloc);
2031 IPin_Disconnect(This->pin.pConnectedTo);
2032 This->pin.pConnectedTo = NULL;
2033 hr = GST_RemoveOutputPins(Parser);
2034 } else
2035 hr = VFW_E_NOT_STOPPED;
2036 } else
2037 hr = S_FALSE;
2038 LeaveCriticalSection(This->pin.pCritSec);
2039 return hr;
2042 static HRESULT WINAPI GSTInPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
2044 GSTInPin *This = (GSTInPin*)iface;
2046 TRACE("(%p)->(%p)\n", This, pmt);
2047 dump_AM_MEDIA_TYPE(pmt);
2049 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
2050 return S_OK;
2051 return S_FALSE;
2054 static HRESULT WINAPI GSTInPin_EndOfStream(IPin *iface)
2056 GSTInPin *pin = (GSTInPin*)iface;
2057 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2059 FIXME("Propagate message on %p\n", This);
2060 return S_OK;
2063 static HRESULT WINAPI GSTInPin_BeginFlush(IPin *iface)
2065 GSTInPin *pin = (GSTInPin*)iface;
2066 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2068 FIXME("Propagate message on %p\n", This);
2069 return S_OK;
2072 static HRESULT WINAPI GSTInPin_EndFlush(IPin *iface)
2074 GSTInPin *pin = (GSTInPin*)iface;
2075 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2077 FIXME("Propagate message on %p\n", This);
2078 return S_OK;
2081 static HRESULT WINAPI GSTInPin_NewSegment(IPin *iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate)
2083 GSTInPin *pin = (GSTInPin*)iface;
2084 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
2086 BasePinImpl_NewSegment(iface, tStart, tStop, dRate);
2087 FIXME("Propagate message on %p\n", This);
2088 return S_OK;
2091 static HRESULT WINAPI GSTInPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
2093 GSTInPin *This = (GSTInPin*)iface;
2095 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
2097 *ppv = NULL;
2099 if (IsEqualIID(riid, &IID_IUnknown))
2100 *ppv = iface;
2101 else if (IsEqualIID(riid, &IID_IPin))
2102 *ppv = iface;
2103 else if (IsEqualIID(riid, &IID_IMediaSeeking))
2105 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
2108 if (*ppv)
2110 IUnknown_AddRef((IUnknown *)(*ppv));
2111 return S_OK;
2114 FIXME("No interface for %s!\n", debugstr_guid(riid));
2116 return E_NOINTERFACE;
2119 static HRESULT WINAPI GSTInPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
2121 BasePin *This = (BasePin *)iface;
2123 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
2125 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
2128 static const IPinVtbl GST_InputPin_Vtbl = {
2129 GSTInPin_QueryInterface,
2130 BasePinImpl_AddRef,
2131 GSTInPin_Release,
2132 BaseInputPinImpl_Connect,
2133 GSTInPin_ReceiveConnection,
2134 GSTInPin_Disconnect,
2135 BasePinImpl_ConnectedTo,
2136 BasePinImpl_ConnectionMediaType,
2137 BasePinImpl_QueryPinInfo,
2138 BasePinImpl_QueryDirection,
2139 BasePinImpl_QueryId,
2140 GSTInPin_QueryAccept,
2141 GSTInPin_EnumMediaTypes,
2142 BasePinImpl_QueryInternalConnections,
2143 GSTInPin_EndOfStream,
2144 GSTInPin_BeginFlush,
2145 GSTInPin_EndFlush,
2146 GSTInPin_NewSegment
2149 pthread_mutex_t cb_list_lock = PTHREAD_MUTEX_INITIALIZER;
2150 pthread_cond_t cb_list_cond = PTHREAD_COND_INITIALIZER;
2151 struct list cb_list = LIST_INIT(cb_list);
2153 void CALLBACK perform_cb(TP_CALLBACK_INSTANCE *instance, void *user)
2155 struct cb_data *cbdata = user;
2157 TRACE("got cb type: 0x%x\n", cbdata->type);
2159 switch(cbdata->type)
2161 case WATCH_BUS:
2163 struct watch_bus_data *data = &cbdata->u.watch_bus_data;
2164 cbdata->u.watch_bus_data.ret = watch_bus(data->bus, data->msg, data->user);
2165 break;
2167 case EXISTING_NEW_PAD:
2169 struct existing_new_pad_data *data = &cbdata->u.existing_new_pad_data;
2170 existing_new_pad(data->bin, data->pad, data->user);
2171 break;
2173 case QUERY_FUNCTION:
2175 struct query_function_data *data = &cbdata->u.query_function_data;
2176 cbdata->u.query_function_data.ret = query_function(data->pad, data->parent, data->query);
2177 break;
2179 case ACTIVATE_MODE:
2181 struct activate_mode_data *data = &cbdata->u.activate_mode_data;
2182 cbdata->u.activate_mode_data.ret = activate_mode(data->pad, data->parent, data->mode, data->activate);
2183 break;
2185 case NO_MORE_PADS:
2187 struct no_more_pads_data *data = &cbdata->u.no_more_pads_data;
2188 no_more_pads(data->decodebin, data->user);
2189 break;
2191 case REQUEST_BUFFER_SRC:
2193 struct request_buffer_src_data *data = &cbdata->u.request_buffer_src_data;
2194 cbdata->u.request_buffer_src_data.ret = request_buffer_src(data->pad, data->parent,
2195 data->ofs, data->len, data->buf);
2196 break;
2198 case EVENT_SRC:
2200 struct event_src_data *data = &cbdata->u.event_src_data;
2201 cbdata->u.event_src_data.ret = event_src(data->pad, data->parent, data->event);
2202 break;
2204 case EVENT_SINK:
2206 struct event_sink_data *data = &cbdata->u.event_sink_data;
2207 cbdata->u.event_sink_data.ret = event_sink(data->pad, data->parent, data->event);
2208 break;
2210 case ACCEPT_CAPS_SINK:
2212 struct accept_caps_sink_data *data = &cbdata->u.accept_caps_sink_data;
2213 cbdata->u.accept_caps_sink_data.ret = accept_caps_sink(data->pad, data->caps);
2214 break;
2216 case SETCAPS_SINK:
2218 struct setcaps_sink_data *data = &cbdata->u.setcaps_sink_data;
2219 cbdata->u.setcaps_sink_data.ret = setcaps_sink(data->pad, data->caps);
2220 break;
2222 case GOT_DATA_SINK:
2224 struct got_data_sink_data *data = &cbdata->u.got_data_sink_data;
2225 cbdata->u.got_data_sink_data.ret = got_data_sink(data->pad, data->parent, data->buf);
2226 break;
2228 case GOT_DATA:
2230 struct got_data_data *data = &cbdata->u.got_data_data;
2231 cbdata->u.got_data_data.ret = got_data(data->pad, data->parent, data->buf);
2232 break;
2234 case REMOVED_DECODED_PAD:
2236 struct removed_decoded_pad_data *data = &cbdata->u.removed_decoded_pad_data;
2237 removed_decoded_pad(data->bin, data->pad, data->user);
2238 break;
2240 case AUTOPLUG_BLACKLIST:
2242 struct autoplug_blacklist_data *data = &cbdata->u.autoplug_blacklist_data;
2243 cbdata->u.autoplug_blacklist_data.ret = autoplug_blacklist(data->bin,
2244 data->pad, data->caps, data->fact, data->user);
2245 break;
2247 case UNKNOWN_TYPE:
2249 struct unknown_type_data *data = &cbdata->u.unknown_type_data;
2250 unknown_type(data->bin, data->pad, data->caps, data->user);
2251 break;
2253 case RELEASE_SAMPLE:
2255 struct release_sample_data *data = &cbdata->u.release_sample_data;
2256 release_sample(data->data);
2257 break;
2259 case TRANSFORM_PAD_ADDED:
2261 struct transform_pad_added_data *data = &cbdata->u.transform_pad_added_data;
2262 Gstreamer_transform_pad_added(data->filter, data->pad, data->user);
2263 break;
2265 case QUERY_SINK:
2267 struct query_sink_data *data = &cbdata->u.query_sink_data;
2268 cbdata->u.query_sink_data.ret = query_sink(data->pad, data->parent,
2269 data->query);
2270 break;
2274 pthread_mutex_lock(&cbdata->lock);
2275 cbdata->finished = 1;
2276 pthread_cond_broadcast(&cbdata->cond);
2277 pthread_mutex_unlock(&cbdata->lock);
2280 static DWORD WINAPI dispatch_thread(void *user)
2282 struct cb_data *cbdata;
2284 pthread_mutex_lock(&cb_list_lock);
2286 while(1){
2287 pthread_cond_wait(&cb_list_cond, &cb_list_lock);
2289 while(!list_empty(&cb_list)){
2290 cbdata = LIST_ENTRY(list_head(&cb_list), struct cb_data, entry);
2291 list_remove(&cbdata->entry);
2293 TrySubmitThreadpoolCallback(&perform_cb, cbdata, NULL);
2297 pthread_mutex_unlock(&cb_list_lock);
2299 return 0;
2302 void start_dispatch_thread(void)
2304 pthread_key_create(&wine_gst_key, NULL);
2305 CloseHandle(CreateThread(NULL, 0, &dispatch_thread, NULL, 0, NULL));