winegstreamer: Fix GetState.
[wine/multimedia.git] / dlls / winegstreamer / gstdemux.c
blob709dc429343b62b12ed5e7a7ed10dfd4fa6f9506
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"
23 #include <gst/app/gstappsink.h>
24 #include <gst/app/gstappsrc.h>
25 #include <gst/app/gstappbuffer.h>
26 #include <gst/gstutils.h>
28 #include "gst_private.h"
29 #include "gst_guids.h"
31 #include "vfwmsgs.h"
32 #include "amvideo.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 #include <assert.h>
39 #include "dvdmedia.h"
40 #include "mmreg.h"
41 #include "ks.h"
42 #include "initguid.h"
43 #include "ksmedia.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer);
47 typedef struct GSTOutPin GSTOutPin;
48 typedef struct GSTInPin {
49 BasePin pin;
50 IAsyncReader *pReader;
51 IMemAllocator *pAlloc;
52 } GSTInPin;
54 typedef struct GSTImpl {
55 BaseFilter filter;
57 GSTInPin pInputPin;
58 GSTOutPin **ppPins;
59 LONG cStreams;
61 LONGLONG filesize;
63 BOOL discont, initial;
64 GstElement *gstfilter;
65 GstPad *my_src, *their_sink;
66 GstBus *bus;
67 guint64 start, nextofs, nextpullofs, stop;
68 ALLOCATOR_PROPERTIES props;
69 HANDLE event, changed_ofs;
71 HANDLE push_thread;
72 } GSTImpl;
74 struct GSTOutPin {
75 BaseOutputPin pin;
77 GstPad *their_src;
78 GstPad *my_sink;
79 int isaud, isvid;
80 AM_MEDIA_TYPE * pmt;
81 HANDLE caps_event;
82 GstSegment *segment;
83 QualityControlImpl qcimpl;
84 SourceSeeking seek;
87 static const WCHAR wcsInputPinName[] = {'i','n','p','u','t',' ','p','i','n',0};
88 static const IMediaSeekingVtbl GST_Seeking_Vtbl;
89 static const IPinVtbl GST_OutputPin_Vtbl;
90 static const IPinVtbl GST_InputPin_Vtbl;
91 static const IBaseFilterVtbl GST_Vtbl;
92 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl;
94 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt);
95 static HRESULT GST_RemoveOutputPins(GSTImpl *This);
96 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface);
97 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface);
98 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface);
100 static int amt_from_gst_caps_audio(GstCaps *caps, AM_MEDIA_TYPE *amt) {
101 WAVEFORMATEXTENSIBLE *wfe;
102 WAVEFORMATEX *wfx;
103 GstStructure *arg;
104 gint32 depth = 0, bpp = 0;
105 const char *typename;
106 arg = gst_caps_get_structure(caps, 0);
107 typename = gst_structure_get_name(arg);
108 if (!typename)
109 return 0;
111 wfe = CoTaskMemAlloc(sizeof(*wfe));
112 wfx = (WAVEFORMATEX*)wfe;
113 amt->majortype = MEDIATYPE_Audio;
114 amt->subtype = MEDIASUBTYPE_PCM;
115 amt->formattype = FORMAT_WaveFormatEx;
116 amt->pbFormat = (BYTE*)wfe;
117 amt->cbFormat = sizeof(*wfe);
118 amt->bFixedSizeSamples = 0;
119 amt->bTemporalCompression = 1;
120 amt->lSampleSize = 0;
121 amt->pUnk = NULL;
123 wfx->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
124 if (!gst_structure_get_int(arg, "channels", (INT*)&wfx->nChannels))
125 return 0;
126 if (!gst_structure_get_int(arg, "rate", (INT*)&wfx->nSamplesPerSec))
127 return 0;
128 gst_structure_get_int(arg, "width", &depth);
129 gst_structure_get_int(arg, "depth", &bpp);
130 if (!depth || depth > 32 || depth % 8)
131 depth = bpp;
132 else if (!bpp)
133 bpp = depth;
134 wfe->Samples.wValidBitsPerSample = depth;
135 wfx->wBitsPerSample = bpp;
136 wfx->cbSize = sizeof(*wfe)-sizeof(*wfx);
137 switch (wfx->nChannels) {
138 case 1: wfe->dwChannelMask = KSAUDIO_SPEAKER_MONO; break;
139 case 2: wfe->dwChannelMask = KSAUDIO_SPEAKER_STEREO; break;
140 case 4: wfe->dwChannelMask = KSAUDIO_SPEAKER_SURROUND; break;
141 case 5: wfe->dwChannelMask = (KSAUDIO_SPEAKER_5POINT1 & ~SPEAKER_LOW_FREQUENCY); break;
142 case 6: wfe->dwChannelMask = KSAUDIO_SPEAKER_5POINT1; break;
143 case 8: wfe->dwChannelMask = KSAUDIO_SPEAKER_7POINT1; break;
144 default:
145 wfe->dwChannelMask = 0;
147 if (!strcmp(typename, "audio/x-raw-float")) {
148 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
149 wfx->wBitsPerSample = wfe->Samples.wValidBitsPerSample = 32;
150 } else
151 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
152 wfx->nBlockAlign = wfx->nChannels * wfx->wBitsPerSample/8;
153 wfx->nAvgBytesPerSec = wfx->nSamplesPerSec * wfx->nBlockAlign;
154 return 1;
157 static int amt_from_gst_caps_video(GstCaps *caps, AM_MEDIA_TYPE *amt) {
158 VIDEOINFOHEADER *vih = CoTaskMemAlloc(sizeof(*vih));
159 BITMAPINFOHEADER *bih = &vih->bmiHeader;
160 GstStructure *arg;
161 gint32 width = 0, height = 0, nom = 0, denom = 0;
162 const char *typename;
163 arg = gst_caps_get_structure(caps, 0);
164 typename = gst_structure_get_name(arg);
165 if (!typename)
166 return 0;
167 if (!gst_structure_get_int(arg, "width", &width) ||
168 !gst_structure_get_int(arg, "height", &height) ||
169 !gst_structure_get_fraction(arg, "framerate", &nom, &denom))
170 return 0;
171 amt->formattype = FORMAT_VideoInfo;
172 amt->pbFormat = (BYTE*)vih;
173 amt->cbFormat = sizeof(*vih);
174 amt->bFixedSizeSamples = amt->bTemporalCompression = 1;
175 amt->lSampleSize = 0;
176 amt->pUnk = NULL;
177 ZeroMemory(vih, sizeof(*vih));
178 amt->majortype = MEDIATYPE_Video;
179 if (!strcmp(typename, "video/x-raw-rgb")) {
180 if (!gst_structure_get_int(arg, "bpp", (INT*)&bih->biBitCount))
181 return 0;
182 switch (bih->biBitCount) {
183 case 16: amt->subtype = MEDIASUBTYPE_RGB555; break;
184 case 24: amt->subtype = MEDIASUBTYPE_RGB24; break;
185 case 32: amt->subtype = MEDIASUBTYPE_RGB32; break;
186 default:
187 FIXME("Unknown bpp %u\n", bih->biBitCount);
188 return 0;
190 bih->biCompression = BI_RGB;
191 } else {
192 amt->subtype = MEDIATYPE_Video;
193 if (!gst_structure_get_fourcc(arg, "format", &amt->subtype.Data1))
194 return 0;
195 switch (amt->subtype.Data1) {
196 case mmioFOURCC('I','4','2','0'):
197 case mmioFOURCC('Y','V','1','2'):
198 case mmioFOURCC('N','V','1','2'):
199 case mmioFOURCC('N','V','2','1'):
200 bih->biBitCount = 12; break;
201 case mmioFOURCC('Y','U','Y','2'):
202 case mmioFOURCC('Y','V','Y','U'):
203 bih->biBitCount = 16; break;
205 bih->biCompression = amt->subtype.Data1;
207 bih->biSizeImage = width * height * bih->biBitCount / 8;
208 vih->AvgTimePerFrame = 10000000;
209 vih->AvgTimePerFrame *= denom;
210 vih->AvgTimePerFrame /= nom;
211 vih->rcSource.left = 0;
212 vih->rcSource.right = width;
213 vih->rcSource.top = height;
214 vih->rcSource.bottom = 0;
215 vih->rcTarget = vih->rcSource;
216 bih->biSize = sizeof(*bih);
217 bih->biWidth = width;
218 bih->biHeight = height;
219 bih->biPlanes = 1;
220 return 1;
223 static gboolean accept_caps_sink(GstPad *pad, GstCaps *caps) {
224 GSTOutPin *pin = gst_pad_get_element_private(pad);
225 AM_MEDIA_TYPE amt;
226 GstStructure *arg;
227 const char *typename;
228 int ret;
229 arg = gst_caps_get_structure(caps, 0);
230 typename = gst_structure_get_name(arg);
231 if (!strcmp(typename, "audio/x-raw-int") ||
232 !strcmp(typename, "audio/x-raw-float")) {
233 if (!pin->isaud) {
234 ERR("Setting audio caps on non-audio pad?\n");
235 return 0;
237 ret = amt_from_gst_caps_audio(caps, &amt);
238 FreeMediaType(&amt);
239 TRACE("+%i\n", ret);
240 return ret;
241 } else if (!strcmp(typename, "video/x-raw-rgb")
242 || !strcmp(typename, "video/x-raw-yuv")) {
243 if (!pin->isvid) {
244 ERR("Setting video caps on non-video pad?\n");
245 return 0;
247 ret = amt_from_gst_caps_video(caps, &amt);
248 FreeMediaType(&amt);
249 TRACE("-%i\n", ret);
250 return ret;
251 } else {
252 FIXME("Unhandled type \"%s\"\n", typename);
253 return 0;
257 static gboolean setcaps_sink(GstPad *pad, GstCaps *caps) {
258 GSTOutPin *pin = gst_pad_get_element_private(pad);
259 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
260 AM_MEDIA_TYPE amt;
261 GstStructure *arg;
262 const char *typename;
263 int ret;
264 arg = gst_caps_get_structure(caps, 0);
265 typename = gst_structure_get_name(arg);
266 if (!strcmp(typename, "audio/x-raw-int") ||
267 !strcmp(typename, "audio/x-raw-float")) {
268 if (!pin->isaud) {
269 ERR("Setting audio caps on non-audio pad?\n");
270 return 0;
272 ret = amt_from_gst_caps_audio(caps, &amt);
273 } else if (!strcmp(typename, "video/x-raw-rgb")
274 || !strcmp(typename, "video/x-raw-yuv")) {
275 if (!pin->isvid) {
276 ERR("Setting video caps on non-video pad?\n");
277 return 0;
279 ret = amt_from_gst_caps_video(caps, &amt);
280 if (ret)
281 This->props.cbBuffer = max(This->props.cbBuffer, ((VIDEOINFOHEADER*)amt.pbFormat)->bmiHeader.biSizeImage);
282 } else {
283 FIXME("Unhandled type \"%s\"\n", typename);
284 return 0;
286 TRACE("Linking returned %i for %s\n", ret, typename);
287 if (!ret)
288 return 0;
289 FreeMediaType(pin->pmt);
290 *pin->pmt = amt;
291 return 1;
294 static gboolean gst_base_src_perform_seek(GSTImpl *This, GstEvent *event)
296 gboolean res = TRUE;
297 gdouble rate;
298 GstFormat seek_format;
299 GstSeekFlags flags;
300 GstSeekType cur_type, stop_type;
301 gint64 cur, stop;
302 gboolean flush;
303 guint32 seqnum;
304 GstEvent *tevent;
305 BOOL thread = !!This->push_thread;
307 gst_event_parse_seek(event, &rate, &seek_format, &flags,
308 &cur_type, &cur, &stop_type, &stop);
310 if (seek_format != GST_FORMAT_BYTES) {
311 FIXME("Not handling other format %i\n", seek_format);
312 return 0;
315 flush = flags & GST_SEEK_FLAG_FLUSH;
316 seqnum = gst_event_get_seqnum(event);
318 /* send flush start */
319 if (flush) {
320 tevent = gst_event_new_flush_start();
321 gst_event_set_seqnum(tevent, seqnum);
322 gst_pad_push_event(This->my_src, tevent);
323 if (This->pInputPin.pReader)
324 IAsyncReader_BeginFlush(This->pInputPin.pReader);
325 if (thread)
326 gst_pad_activate_push(This->my_src, 0);
329 TRACE("++++++++++++++++ perform byte seek ------------------\n");
331 /* and prepare to continue streaming */
332 if (flush) {
333 tevent = gst_event_new_flush_stop();
334 gst_event_set_seqnum(tevent, seqnum);
335 gst_pad_push_event(This->my_src, tevent);
336 if (This->pInputPin.pReader)
337 IAsyncReader_EndFlush(This->pInputPin.pReader);
338 if (thread)
339 gst_pad_activate_push(This->my_src, 1);
342 return res;
345 static gboolean event_src(GstPad *pad, GstEvent *event) {
346 GSTImpl *This = gst_pad_get_element_private(pad);
347 switch (event->type) {
348 case GST_EVENT_SEEK:
349 return gst_base_src_perform_seek(This, event);
350 case GST_EVENT_FLUSH_START:
351 EnterCriticalSection(&This->filter.csFilter);
352 if (This->pInputPin.pReader)
353 IAsyncReader_BeginFlush(This->pInputPin.pReader);
354 LeaveCriticalSection(&This->filter.csFilter);
355 break;
356 case GST_EVENT_FLUSH_STOP:
357 EnterCriticalSection(&This->filter.csFilter);
358 if (This->pInputPin.pReader)
359 IAsyncReader_EndFlush(This->pInputPin.pReader);
360 LeaveCriticalSection(&This->filter.csFilter);
361 break;
362 default:
363 FIXME("%p stub\n", event);
364 return gst_pad_event_default(pad, event);
366 return 1;
369 static gboolean event_sink(GstPad *pad, GstEvent *event) {
370 GSTOutPin *pin = gst_pad_get_element_private(pad);
371 switch (event->type) {
372 case GST_EVENT_NEWSEGMENT: {
373 gboolean update;
374 gdouble rate, applied_rate;
375 GstFormat format;
376 gint64 start, stop, pos;
377 gst_event_parse_new_segment_full(event, &update, &rate, &applied_rate, &format, &start, &stop, &pos);
378 if (format != GST_FORMAT_TIME) {
379 FIXME("Ignoring new segment because of format %i\n", format);
380 return 1;
382 gst_segment_set_newsegment_full(pin->segment, update, rate, applied_rate, format, start, stop, pos);
383 pos /= 100;
384 if (stop > 0)
385 stop /= 100;
386 if (pin->pin.pin.pConnectedTo)
387 IPin_NewSegment(pin->pin.pin.pConnectedTo, pos, stop, rate*applied_rate);
388 return 1;
390 case GST_EVENT_EOS:
391 if (pin->pin.pin.pConnectedTo)
392 IPin_EndOfStream(pin->pin.pin.pConnectedTo);
393 return 1;
394 case GST_EVENT_FLUSH_START:
395 if (pin->pin.pin.pConnectedTo)
396 IPin_BeginFlush(pin->pin.pin.pConnectedTo);
397 return 1;
398 case GST_EVENT_FLUSH_STOP:
399 if (pin->pin.pin.pConnectedTo)
400 IPin_EndFlush(pin->pin.pin.pConnectedTo);
401 gst_segment_init(pin->segment, GST_FORMAT_TIME);
402 return 1;
403 default:
404 FIXME("%p stub %s\n", event, gst_event_type_get_name(event->type));
405 return gst_pad_event_default(pad, event);
409 static void release_sample(void *data) {
410 ULONG ret;
411 ret = IMediaSample_Release((IMediaSample *)data);
412 TRACE("Releasing %p returns %u\n", data, ret);
415 static DWORD CALLBACK push_data(LPVOID iface) {
416 LONGLONG maxlen, curlen;
417 GSTImpl *This = iface;
418 IMediaSample *buf;
419 DWORD_PTR user;
420 HRESULT hr;
422 IAsyncReader_Length(This->pInputPin.pReader, &maxlen, &curlen);
423 TRACE("Starting..\n");
424 for (;;) {
425 REFERENCE_TIME tStart, tStop;
426 ULONG len;
427 GstBuffer *gstbuf;
428 BYTE *data;
429 int ret;
431 hr = IMemAllocator_GetBuffer(This->pInputPin.pAlloc, &buf, NULL, NULL, 0);
432 if (FAILED(hr))
433 break;
435 if (This->nextofs >= maxlen)
436 break;
437 len = IMediaSample_GetSize(buf);
438 if (This->nextofs + len > maxlen)
439 len = maxlen - This->nextofs;
441 tStart = MEDIATIME_FROM_BYTES(This->nextofs);
442 tStop = tStart + MEDIATIME_FROM_BYTES(len);
443 IMediaSample_SetTime(buf, &tStart, &tStop);
445 hr = IAsyncReader_Request(This->pInputPin.pReader, buf, 0);
446 if (FAILED(hr)) {
447 IMediaSample_Release(buf);
448 break;
450 This->nextofs += len;
451 hr = IAsyncReader_WaitForNext(This->pInputPin.pReader, -1, &buf, &user);
452 if (FAILED(hr) || !buf) {
453 if (buf)
454 IMediaSample_Release(buf);
455 break;
458 IMediaSample_GetPointer(buf, &data);
459 gstbuf = gst_app_buffer_new(data, IMediaSample_GetActualDataLength(buf), release_sample, buf);
460 if (!gstbuf) {
461 IMediaSample_Release(buf);
462 break;
464 gstbuf->duration = gstbuf->timestamp = -1;
465 ret = gst_pad_push(This->my_src, gstbuf);
466 if (ret >= 0)
467 hr = S_OK;
468 else
469 ERR("Sending returned: %i\n", ret);
470 if (ret == GST_FLOW_ERROR)
471 hr = E_FAIL;
472 else if (ret == GST_FLOW_WRONG_STATE)
473 hr = VFW_E_WRONG_STATE;
474 else if (ret == GST_FLOW_RESEND)
475 hr = S_FALSE;
476 if (hr != S_OK)
477 break;
480 gst_pad_push_event(This->my_src, gst_event_new_eos());
482 TRACE("Almost stopping.. %08x\n", hr);
483 do {
484 IAsyncReader_WaitForNext(This->pInputPin.pReader, 0, &buf, &user);
485 if (buf)
486 IMediaSample_Release(buf);
487 } while (buf);
489 TRACE("Stopping.. %08x\n", hr);
490 return 0;
493 static HRESULT WINAPI GST_OutPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt) {
494 GSTOutPin *pin = (GSTOutPin*)iface;
495 FIXME("stub %p\n", pin);
496 return S_OK;
499 static GstFlowReturn got_data_sink(GstPad *pad, GstBuffer *buf) {
500 GSTOutPin *pin = gst_pad_get_element_private(pad);
501 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
502 IMediaSample *sample;
503 HRESULT hr;
504 BOOL freeSamp = FALSE;
506 if (This->initial) {
507 gst_buffer_unref(buf);
508 FIXME("Triggering %p %p\n", pad, pin->caps_event);
509 SetEvent(pin->caps_event);
510 return GST_FLOW_NOT_LINKED;
513 if (GST_IS_APP_BUFFER(buf)) {
514 sample = GST_APP_BUFFER(buf)->priv;
515 TRACE("Pushing buffer\n");
516 } else if (buf->parent && GST_IS_APP_BUFFER(buf->parent)) {
517 sample = GST_APP_BUFFER(buf->parent)->priv;
518 TRACE("Pushing sub-buffer\n");
519 } else {
520 BYTE *ptr = NULL;
521 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
522 if (hr == VFW_E_NOT_CONNECTED) {
523 gst_buffer_unref(buf);
524 return GST_FLOW_NOT_LINKED;
526 if (FAILED(hr)) {
527 gst_buffer_unref(buf);
528 ERR("Didn't get a GST_APP_BUFFER, and could not get a delivery buffer (%x), returning GST_FLOW_WRONG_STATE\n", hr);
529 return GST_FLOW_WRONG_STATE;
531 FIXME("Did not get a GST_APP_BUFFER, creating a sample\n");
532 IMediaSample_SetActualDataLength(sample, GST_BUFFER_SIZE(buf));
533 IMediaSample_GetPointer(sample, &ptr);
534 memcpy(ptr, GST_BUFFER_DATA(buf), GST_BUFFER_SIZE(buf));
537 if (GST_BUFFER_TIMESTAMP_IS_VALID(buf)) {
538 REFERENCE_TIME rtStart = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->timestamp);
539 if (rtStart >= 0)
540 rtStart /= 100;
542 if (GST_BUFFER_DURATION_IS_VALID(buf)) {
543 REFERENCE_TIME tStart = buf->timestamp / 100;
544 REFERENCE_TIME tStop = (buf->timestamp + buf->duration) / 100;
545 REFERENCE_TIME rtStop;
546 rtStop = gst_segment_to_running_time(pin->segment, GST_FORMAT_TIME, buf->timestamp + buf->duration);
547 if (rtStop >= 0)
548 rtStop /= 100;
549 TRACE("Current time on %p: %i to %i ms\n", pin, (int)(rtStart / 10000), (int)(rtStop / 10000));
550 IMediaSample_SetTime(sample, &rtStart, rtStop >= 0 ? &rtStop : NULL);
551 IMediaSample_SetMediaTime(sample, &tStart, &tStop);
552 } else {
553 IMediaSample_SetTime(sample, rtStart >= 0 ? &rtStart : NULL, NULL);
554 IMediaSample_SetMediaTime(sample, NULL, NULL);
556 } else {
557 IMediaSample_SetTime(sample, NULL, NULL);
558 IMediaSample_SetMediaTime(sample, NULL, NULL);
561 IMediaSample_SetDiscontinuity(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DISCONT));
562 IMediaSample_SetPreroll(sample, GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_PREROLL));
563 IMediaSample_SetSyncPoint(sample, !GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_DELTA_UNIT));
565 if (!pin->pin.pin.pConnectedTo)
566 hr = VFW_E_NOT_CONNECTED;
567 else
568 hr = IMemInputPin_Receive(pin->pin.pMemInputPin, sample);
569 TRACE("sending sample: %08x\n", hr);
570 gst_buffer_unref(buf);
571 if (freeSamp)
572 IMediaSample_Release(sample);
573 if (hr == VFW_E_NOT_CONNECTED)
574 return GST_FLOW_NOT_LINKED;
575 else if (FAILED(hr))
576 return GST_FLOW_WRONG_STATE;
577 if (hr != S_OK)
578 return GST_FLOW_RESEND;
579 return GST_FLOW_OK;
582 static GstFlowReturn request_buffer_sink(GstPad *pad, guint64 ofs, guint size, GstCaps *caps, GstBuffer **buf) {
583 GSTOutPin *pin = gst_pad_get_element_private(pad);
584 GSTImpl *This = (GSTImpl *)pin->pin.pin.pinInfo.pFilter;
585 IMediaSample *sample;
586 BYTE *ptr;
587 HRESULT hr;
589 TRACE("Requesting buffer\n");
590 if (This->initial) {
591 int ret;
592 ret = setcaps_sink(pad, caps);
593 if (!ret)
594 return GST_FLOW_NOT_NEGOTIATED;
595 *buf = gst_buffer_new_and_alloc(size);
596 return GST_FLOW_OK;
599 if (caps && caps != GST_PAD_CAPS(pad))
600 if (!setcaps_sink(pad, caps))
601 return GST_FLOW_NOT_NEGOTIATED;
603 hr = BaseOutputPinImpl_GetDeliveryBuffer(&pin->pin, &sample, NULL, NULL, 0);
604 if (hr == VFW_E_NOT_CONNECTED)
605 return GST_FLOW_NOT_LINKED;
606 if (FAILED(hr)) {
607 ERR("Could not get output buffer: %08x\n", hr);
608 *buf = NULL;
609 return GST_FLOW_WRONG_STATE;
611 IMediaSample_SetActualDataLength(sample, size);
612 IMediaSample_GetPointer(sample, &ptr);
613 *buf = gst_app_buffer_new(ptr, size, release_sample, sample);
614 if (!*buf) {
615 IMediaSample_Release(sample);
616 ERR("Out of memory\n");
617 return GST_FLOW_ERROR;
619 gst_buffer_set_caps(*buf, caps);
620 return GST_FLOW_OK;
623 static GstFlowReturn request_buffer_src(GstPad *pad, guint64 ofs, guint len, GstBuffer **buf) {
624 GSTImpl *This = gst_pad_get_element_private(pad);
625 int ret;
627 *buf = NULL;
628 TRACE("Requesting %s %u\n", wine_dbgstr_longlong(ofs), len);
629 if (ofs == (guint64)-1)
630 ofs = This->nextpullofs;
631 if (ofs >= This->filesize) {
632 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs), len);
633 return GST_FLOW_UNEXPECTED;
635 if (len + ofs > This->filesize)
636 len = This->filesize - ofs;
637 This->nextpullofs = ofs + len;
639 ret = gst_pad_alloc_buffer(This->my_src, ofs, len, NULL, buf);
640 if (ret >= 0) {
641 HRESULT hr;
642 hr = IAsyncReader_SyncRead(This->pInputPin.pReader, ofs, len, GST_BUFFER_DATA(*buf));
643 if (FAILED(hr)) {
644 ERR("Returned %08x\n", hr);
645 return GST_FLOW_ERROR;
648 return ret;
651 static DWORD CALLBACK push_data_init(LPVOID iface) {
652 GSTImpl *This = iface;
653 DWORD64 ofs = 0;
655 TRACE("Starting..\n");
656 for (;;) {
657 GstBuffer *buf;
658 GstFlowReturn ret = request_buffer_src(This->my_src, ofs, 4096, &buf);
659 if (ret < 0) {
660 ERR("Obtaining buffer returned: %i\n", ret);
661 break;
663 ret = gst_pad_push(This->my_src, buf);
664 ofs += 4096;
665 if (ret)
666 TRACE("Sending returned: %i\n", ret);
667 if (ret < 0)
668 break;
670 TRACE("Stopping..\n");
671 return 0;
674 static void removed_decoded_pad(GstElement *bin, GstPad *pad, GSTImpl *This) {
675 int x;
676 GSTOutPin *pin;
678 EnterCriticalSection(&This->filter.csFilter);
679 for (x = 0; x < This->cStreams; ++x) {
680 if (This->ppPins[x]->their_src == pad)
681 break;
683 if (x == This->cStreams)
684 goto out;
685 pin = This->ppPins[x];
686 gst_pad_unlink(pin->their_src, pin->my_sink);
687 gst_object_unref(pin->their_src);
688 pin->their_src = NULL;
689 out:
690 TRACE("Removed %i/%i\n", x, This->cStreams);
691 LeaveCriticalSection(&This->filter.csFilter);
694 static void init_new_decoded_pad(GstElement *bin, GstPad *pad, gboolean last, GSTImpl *This) {
695 HRESULT hr;
696 PIN_INFO piOutput;
697 const char *typename;
698 char *name;
699 AM_MEDIA_TYPE amt = { };
700 GstCaps *caps;
701 GstStructure *arg;
702 GstPad *mypad;
703 GSTOutPin *pin;
704 int ret;
705 int isvid = 0, isaud = 0;
707 piOutput.dir = PINDIR_OUTPUT;
708 piOutput.pFilter = (IBaseFilter *)This;
709 name = gst_pad_get_name(pad);
710 MultiByteToWideChar(CP_UNIXCP, 0, name, -1, piOutput.achName, sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1);
711 TRACE("Name: %s\n", name);
712 g_free(name);
713 piOutput.achName[sizeof(piOutput.achName) / sizeof(piOutput.achName[0]) - 1] = 0;
715 caps = gst_pad_get_caps_reffed(pad);
716 arg = gst_caps_get_structure(caps, 0);
717 typename = gst_structure_get_name(arg);
719 mypad = gst_pad_new(NULL, GST_PAD_SINK);
720 gst_pad_set_chain_function(mypad, got_data_sink);
721 gst_pad_set_event_function(mypad, event_sink);
722 gst_pad_set_bufferalloc_function(mypad, request_buffer_sink);
723 gst_pad_set_acceptcaps_function(mypad, accept_caps_sink);
724 gst_pad_set_acceptcaps_function(mypad, setcaps_sink);
726 if (!strcmp(typename, "audio/x-raw-int") ||
727 !strcmp(typename, "audio/x-raw-float")) {
728 isaud = 1;
729 } else if (!strcmp(typename, "video/x-raw-rgb")
730 || !strcmp(typename, "video/x-raw-yuv")) {
731 isvid = 1;
732 } else {
733 FIXME("Unknown type \'%s\'\n", typename);
734 return;
736 GST_PAD_CAPS(mypad) = GST_CAPS_ANY;
737 hr = GST_AddPin(This, &piOutput, &amt);
738 if (FAILED(hr)) {
739 ERR("%08x\n", hr);
740 return;
742 pin = This->ppPins[This->cStreams - 1];
743 gst_pad_set_element_private(mypad, pin);
744 pin->my_sink = mypad;
745 pin->isaud = isaud;
746 pin->isvid = isvid;
748 ret = gst_pad_link(pad, mypad);
749 gst_pad_activate_push(mypad, 1);
750 FIXME("Linking: %i\n", ret);
751 if (ret >= 0) {
752 pin->their_src = pad;
753 gst_segment_init(pin->segment, GST_FORMAT_TIME);
754 gst_object_ref(pin->their_src);
758 static void existing_new_pad(GstElement *bin, GstPad *pad, gboolean last, GSTImpl *This) {
759 int x;
760 GstCaps *caps;
761 GstStructure *arg;
762 const char *typename, *ownname;
763 caps = gst_pad_get_caps_reffed(pad);
764 arg = gst_caps_get_structure(caps, 0);
765 typename = gst_structure_get_name(arg);
767 if (gst_pad_is_linked(pad))
768 return;
770 /* Still holding our own lock */
771 if (This->initial) {
772 init_new_decoded_pad(bin, pad, last, This);
773 return;
776 EnterCriticalSection(&This->filter.csFilter);
777 for (x = 0; x < This->cStreams; ++x) {
778 GSTOutPin *pin = This->ppPins[x];
779 if (!pin->their_src) {
780 caps = gst_pad_get_caps_reffed(pin->my_sink);
781 arg = gst_caps_get_structure(caps, 0);
782 ownname = gst_structure_get_name(arg);
783 if (!strcmp(typename, ownname) && gst_pad_link(pad, pin->my_sink) >= 0) {
784 pin->their_src = pad;
785 gst_segment_init(pin->segment, GST_FORMAT_TIME);
786 gst_object_ref(pin->their_src);
787 TRACE("Relinked\n");
788 LeaveCriticalSection(&This->filter.csFilter);
789 return;
793 init_new_decoded_pad(bin, pad, last, This);
794 LeaveCriticalSection(&This->filter.csFilter);
797 static gboolean check_get_range(GstPad *pad) {
798 return 1;
801 static gboolean query_function(GstPad *pad, GstQuery *query) {
802 GSTImpl *This = gst_pad_get_element_private(pad);
803 GstFormat format;
804 int ret;
805 LONGLONG duration;
807 switch (GST_QUERY_TYPE(query)) {
808 case GST_QUERY_DURATION:
809 gst_query_parse_duration (query, &format, NULL);
810 if (format == GST_FORMAT_PERCENT) {
811 gst_query_set_duration (query, GST_FORMAT_PERCENT, GST_FORMAT_PERCENT_MAX);
812 return 1;
814 ret = gst_pad_query_convert (pad, GST_FORMAT_BYTES, This->filesize, &format, &duration);
815 gst_query_set_duration(query, format, duration);
816 return ret;
817 case GST_QUERY_SEEKING:
818 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
819 TRACE("Seeking %i %i\n", format, GST_FORMAT_BYTES);
820 if (format != GST_FORMAT_BYTES)
821 return 0;
822 gst_query_set_seeking(query, GST_FORMAT_BYTES, 1, 0, This->filesize);
823 return 1;
824 default:
825 FIXME("Unhandled query type %i\n", GST_QUERY_TYPE(query));
826 case GST_QUERY_URI:
827 return 1;
831 static gboolean activate_push(GstPad *pad, gboolean activate) {
832 GSTImpl *This = gst_pad_get_element_private(pad);
833 EnterCriticalSection(&This->filter.csFilter);
834 if (!activate) {
835 TRACE("Deactivating\n");
836 if (This->push_thread) {
837 WaitForSingleObject(This->push_thread, -1);
838 CloseHandle(This->push_thread);
839 This->push_thread = NULL;
841 } else if (!This->push_thread) {
842 TRACE("Activating\n");
843 if (This->initial)
844 This->push_thread = CreateThread(NULL, 0, push_data_init, This, 0, NULL);
845 else
846 This->push_thread = CreateThread(NULL, 0, push_data, This, 0, NULL);
848 LeaveCriticalSection(&This->filter.csFilter);
849 return 1;
852 static void no_more_pads(GstElement *decodebin, GSTImpl *This) {
853 FIXME("Done\n");
854 SetEvent(This->event);
857 typedef enum {
858 GST_AUTOPLUG_SELECT_TRY,
859 GST_AUTOPLUG_SELECT_EXPOSE,
860 GST_AUTOPLUG_SELECT_SKIP
861 } GstAutoplugSelectResult;
863 static GstAutoplugSelectResult autoplug_blacklist(GstElement *bin, GstPad *pad, GstCaps *caps, GstElementFactory *fact, GSTImpl *This) {
864 const char *name = gst_element_factory_get_longname(fact);
866 if (strstr(name, "Player protection")) {
867 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
868 return GST_AUTOPLUG_SELECT_SKIP;
870 if (!strcmp(name, "Fluendo Hardware Accelerated Video Decoder")) {
871 WARN("Disabled video acceleration since it breaks in wine\n");
872 return GST_AUTOPLUG_SELECT_SKIP;
874 TRACE("using \"%s\"\n", name);
875 return GST_AUTOPLUG_SELECT_TRY;
878 static HRESULT GST_Connect(GSTInPin *pPin, IPin *pConnectPin, ALLOCATOR_PROPERTIES *props) {
879 GSTImpl *This = (GSTImpl*)pPin->pin.pinInfo.pFilter;
880 HRESULT hr;
881 int ret, i;
882 LONGLONG avail, duration;
883 GstFormat format = GST_FORMAT_TIME;
884 GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE(
885 "quartz_src",
886 GST_PAD_SRC,
887 GST_PAD_ALWAYS,
888 GST_STATIC_CAPS_ANY);
890 TRACE("%p %p %p\n", pPin, pConnectPin, props);
891 This->props = *props;
892 IAsyncReader_Length(pPin->pReader, &This->filesize, &avail);
894 This->gstfilter = gst_element_factory_make("decodebin2", NULL);
895 if (!This->gstfilter) {
896 FIXME("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
897 8 * (int)sizeof(void*));
898 return E_FAIL;
900 g_signal_connect(This->gstfilter, "new-decoded-pad", G_CALLBACK(existing_new_pad), This);
901 g_signal_connect(This->gstfilter, "pad-removed", G_CALLBACK(removed_decoded_pad), This);
902 g_signal_connect(This->gstfilter, "autoplug-select", G_CALLBACK(autoplug_blacklist), This);
904 This->my_src = gst_pad_new_from_static_template(&src_template, "quartz-src");
905 gst_pad_set_getrange_function(This->my_src, request_buffer_src);
906 gst_pad_set_checkgetrange_function(This->my_src, check_get_range);
907 gst_pad_set_query_function(This->my_src, query_function);
908 gst_pad_set_activatepush_function(This->my_src, activate_push);
909 gst_pad_set_event_function(This->my_src, event_src);
910 gst_pad_set_element_private (This->my_src, This);
911 This->their_sink = gst_element_get_static_pad(This->gstfilter, "sink");
913 g_signal_connect(This->gstfilter, "no-more-pads", G_CALLBACK(no_more_pads), This);
914 ret = gst_pad_link(This->my_src, This->their_sink);
915 gst_object_unref(This->their_sink);
916 if (ret < 0) {
917 ERR("Returns: %i\n", ret);
918 return E_FAIL;
920 This->start = This->nextofs = This->nextpullofs = 0;
921 This->stop = This->filesize;
923 /* Add initial pins */
924 This->initial = This->discont = 1;
925 gst_element_set_state(This->gstfilter, GST_STATE_PLAYING);
926 gst_pad_set_active(This->my_src, 1);
927 WaitForSingleObject(This->event, -1);
928 gst_element_get_state(This->gstfilter, NULL, NULL, -1);
930 if (ret < 0) {
931 WARN("Ret: %i\n", ret);
932 hr = E_FAIL;
933 } else if (!This->cStreams) {
934 FIXME("Gstreamer could not find any streams\n");
935 hr = E_FAIL;
936 } else {
937 gst_pad_query_duration(This->ppPins[0]->their_src, &format, &duration);
938 for (i = 0; i < This->cStreams; ++i) {
939 This->ppPins[i]->seek.llDuration = This->ppPins[i]->seek.llStop = duration / 100;
940 WaitForSingleObject(This->ppPins[i]->caps_event, -1);
942 hr = S_OK;
944 *props = This->props;
945 gst_element_set_state(This->gstfilter, GST_STATE_READY);
946 gst_element_get_state(This->gstfilter, NULL, NULL, -1);
948 This->initial = 0;
949 This->nextofs = This->nextpullofs = 0;
950 return hr;
953 static inline GSTOutPin *impl_from_IMediaSeeking( IMediaSeeking *iface ) {
954 return (GSTOutPin *)((char*)iface - FIELD_OFFSET(GSTOutPin, seek.lpVtbl));
957 static IPin* WINAPI GST_GetPin(BaseFilter *iface, int pos)
959 GSTImpl *This = (GSTImpl *)iface;
960 TRACE("Asking for pos %x\n", pos);
962 if (pos > This->cStreams || pos < 0)
963 return NULL;
964 if (!pos)
966 IPin_AddRef((IPin*)&This->pInputPin);
967 return (IPin*)&This->pInputPin;
969 else
971 IPin_AddRef((IPin*)This->ppPins[pos - 1]);
972 return (IPin*)This->ppPins[pos - 1];
976 static LONG WINAPI GST_GetPinCount(BaseFilter *iface)
978 GSTImpl *This = (GSTImpl *)iface;
979 return (This->cStreams + 1);
982 static const BaseFilterFuncTable BaseFuncTable = {
983 GST_GetPin,
984 GST_GetPinCount
987 IUnknown * CALLBACK Gstreamer_Splitter_create(IUnknown *punkout, HRESULT *phr) {
988 IUnknown *obj = NULL;
989 PIN_INFO *piInput;
990 GSTImpl *This;
992 if (!Gstreamer_init())
994 *phr = E_FAIL;
995 return NULL;
998 This = CoTaskMemAlloc(sizeof(*This));
999 obj = (IUnknown*)This;
1000 if (!This)
1002 *phr = E_OUTOFMEMORY;
1003 return NULL;
1006 BaseFilter_Init(&This->filter, &GST_Vtbl, &CLSID_Gstreamer_Splitter, (DWORD_PTR)(__FILE__ ": GSTImpl.csFilter"), &BaseFuncTable);
1008 This->cStreams = 0;
1009 This->ppPins = NULL;
1010 This->push_thread = NULL;
1011 This->event = CreateEventW(NULL, 0, 0, NULL);
1013 piInput = &This->pInputPin.pin.pinInfo;
1014 piInput->dir = PINDIR_INPUT;
1015 piInput->pFilter = (IBaseFilter *)This;
1016 lstrcpynW(piInput->achName, wcsInputPinName, sizeof(piInput->achName) / sizeof(piInput->achName[0]));
1017 This->pInputPin.pin.lpVtbl = &GST_InputPin_Vtbl;
1018 This->pInputPin.pin.refCount = 1;
1019 This->pInputPin.pin.pConnectedTo = NULL;
1020 This->pInputPin.pin.pCritSec = &This->filter.csFilter;
1021 ZeroMemory(&This->pInputPin.pin.mtCurrent, sizeof(AM_MEDIA_TYPE));
1022 *phr = S_OK;
1023 return obj;
1026 static void GST_Destroy(GSTImpl *This) {
1027 IPin *connected = NULL;
1028 ULONG pinref;
1030 TRACE("Destroying\n");
1032 CloseHandle(This->event);
1034 /* Don't need to clean up output pins, disconnecting input pin will do that */
1035 IPin_ConnectedTo((IPin *)&This->pInputPin, &connected);
1036 if (connected) {
1037 assert(IPin_Disconnect(connected) == S_OK);
1038 IPin_Release(connected);
1039 assert(IPin_Disconnect((IPin *)&This->pInputPin) == S_OK);
1041 pinref = IPin_Release((IPin *)&This->pInputPin);
1042 if (pinref) {
1043 /* Valgrind could find this, if I kill it here */
1044 ERR("pinref should be null, is %u, destroying anyway\n", pinref);
1045 assert((LONG)pinref > 0);
1047 while (pinref)
1048 pinref = IPin_Release((IPin *)&This->pInputPin);
1050 CoTaskMemFree(This);
1053 static HRESULT WINAPI GST_QueryInterface(IBaseFilter *iface, REFIID riid, LPVOID *ppv) {
1054 GSTImpl *This = (GSTImpl *)iface;
1055 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1057 *ppv = NULL;
1059 if (IsEqualIID(riid, &IID_IUnknown))
1060 *ppv = This;
1061 else if (IsEqualIID(riid, &IID_IPersist))
1062 *ppv = This;
1063 else if (IsEqualIID(riid, &IID_IMediaFilter))
1064 *ppv = This;
1065 else if (IsEqualIID(riid, &IID_IBaseFilter))
1066 *ppv = This;
1068 if (*ppv) {
1069 IUnknown_AddRef((IUnknown *)(*ppv));
1070 return S_OK;
1073 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IVideoWindow) &&
1074 !IsEqualIID(riid, &IID_IAMFilterMiscFlags))
1075 FIXME("No interface for %s!\n", debugstr_guid(riid));
1077 return E_NOINTERFACE;
1080 static ULONG WINAPI GST_Release(IBaseFilter *iface) {
1081 GSTImpl *This = (GSTImpl *)iface;
1082 ULONG refCount = BaseFilterImpl_Release(iface);
1084 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
1086 if (!refCount)
1087 GST_Destroy(This);
1089 return refCount;
1092 static HRESULT WINAPI GST_Stop(IBaseFilter *iface) {
1093 GSTImpl *This = (GSTImpl *)iface;
1095 TRACE("()\n");
1097 if (This->gstfilter) {
1098 IAsyncReader_BeginFlush(This->pInputPin.pReader);
1099 gst_element_set_state(This->gstfilter, GST_STATE_READY);
1100 IAsyncReader_EndFlush(This->pInputPin.pReader);
1102 return S_OK;
1105 static HRESULT WINAPI GST_Pause(IBaseFilter *iface) {
1106 HRESULT hr = S_OK;
1107 GSTImpl *This = (GSTImpl *)iface;
1108 GstState now;
1109 GstStateChangeReturn ret;
1110 TRACE("()\n");
1112 if (!This->gstfilter)
1113 return VFW_E_NOT_CONNECTED;
1115 gst_element_get_state(This->gstfilter, &now, NULL, -1);
1116 if (now == GST_STATE_PAUSED)
1117 return S_OK;
1118 if (now != GST_STATE_PLAYING)
1119 hr = IBaseFilter_Run(iface, -1);
1120 if (FAILED(hr))
1121 return hr;
1122 ret = gst_element_set_state(This->gstfilter, GST_STATE_PAUSED);
1123 if (ret == GST_STATE_CHANGE_ASYNC)
1124 hr = S_FALSE;
1125 return hr;
1128 static HRESULT WINAPI GST_Run(IBaseFilter *iface, REFERENCE_TIME tStart) {
1129 HRESULT hr = S_OK;
1130 GSTImpl *This = (GSTImpl *)iface;
1131 ULONG i;
1132 GstState now;
1133 HRESULT hr_any = VFW_E_NOT_CONNECTED;
1135 TRACE("(%s)\n", wine_dbgstr_longlong(tStart));
1137 if (!This->gstfilter)
1138 return VFW_E_NOT_CONNECTED;
1140 gst_element_get_state(This->gstfilter, &now, NULL, -1);
1141 if (now == GST_STATE_PLAYING)
1142 return S_OK;
1143 if (now == GST_STATE_PAUSED) {
1144 GstStateChangeReturn ret;
1145 ret = gst_element_set_state(This->gstfilter, GST_STATE_PAUSED);
1146 if (ret == GST_STATE_CHANGE_ASYNC)
1147 return S_FALSE;
1148 return S_OK;
1151 EnterCriticalSection(&This->filter.csFilter);
1152 gst_pad_set_blocked(This->my_src, 0);
1153 gst_pad_set_blocked(This->their_sink, 0);
1154 gst_element_set_state(This->gstfilter, GST_STATE_PLAYING);
1155 This->filter.rtStreamStart = tStart;
1157 for (i = 0; i < This->cStreams; i++) {
1158 hr = BaseOutputPinImpl_Active((BaseOutputPin *)This->ppPins[i]);
1159 if (SUCCEEDED(hr)) {
1160 gst_pad_set_blocked(This->ppPins[i]->my_sink, 0);
1161 if (This->ppPins[i]->their_src)
1162 gst_pad_set_blocked(This->ppPins[i]->their_src, 0);
1163 hr_any = hr;
1166 hr = hr_any;
1167 if (SUCCEEDED(hr))
1168 gst_pad_set_active(This->my_src, 1);
1169 LeaveCriticalSection(&This->filter.csFilter);
1171 return hr;
1174 static HRESULT WINAPI GST_GetState(IBaseFilter *iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState) {
1175 GSTImpl *This = (GSTImpl *)iface;
1176 HRESULT hr = S_OK;
1177 GstState now, pending;
1178 GstStateChangeReturn ret;
1180 TRACE("(%d, %p)\n", dwMilliSecsTimeout, pState);
1182 if (!This->gstfilter) {
1183 *pState = State_Stopped;
1184 return S_OK;
1187 ret = gst_element_get_state(This->gstfilter, &now, &pending, dwMilliSecsTimeout == INFINITE ? -1 : dwMilliSecsTimeout * 1000);
1189 if (ret == GST_STATE_CHANGE_ASYNC)
1190 hr = VFW_S_STATE_INTERMEDIATE;
1191 else
1192 pending = now;
1194 switch (pending) {
1195 case GST_STATE_PAUSED: *pState = State_Paused; return hr;
1196 case GST_STATE_PLAYING: *pState = State_Running; return hr;
1197 default: *pState = State_Stopped; return hr;
1201 static HRESULT WINAPI GST_FindPin(IBaseFilter *iface, LPCWSTR Id, IPin **ppPin) {
1202 FIXME("(%p)->(%s,%p) stub\n", iface, debugstr_w(Id), ppPin);
1203 return E_NOTIMPL;
1206 static const IBaseFilterVtbl GST_Vtbl = {
1207 GST_QueryInterface,
1208 BaseFilterImpl_AddRef,
1209 GST_Release,
1210 BaseFilterImpl_GetClassID,
1211 GST_Stop,
1212 GST_Pause,
1213 GST_Run,
1214 GST_GetState,
1215 BaseFilterImpl_SetSyncSource,
1216 BaseFilterImpl_GetSyncSource,
1217 BaseFilterImpl_EnumPins,
1218 GST_FindPin,
1219 BaseFilterImpl_QueryFilterInfo,
1220 BaseFilterImpl_JoinFilterGraph,
1221 BaseFilterImpl_QueryVendorInfo
1224 static HRESULT WINAPI GST_ChangeCurrent(IMediaSeeking *iface) {
1225 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1226 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, This->seek.llCurrent * 100, GST_SEEK_TYPE_NONE, -1);
1227 TRACE("(%p) going to %i.%i!\n", iface, (int)(This->seek.llCurrent / 10000000), (int)((This->seek.llCurrent / 10000)%1000));
1228 gst_pad_push_event(This->my_sink, ev);
1229 return S_OK;
1232 static HRESULT WINAPI GST_ChangeStop(IMediaSeeking *iface) {
1233 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1234 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, This->seek.llStop);
1235 TRACE("(%p) going to %i.%i!\n", iface, (int)(This->seek.llCurrent / 10000000), (int)((This->seek.llCurrent / 10000)%1000));
1236 gst_pad_push_event(This->my_sink, ev);
1237 return S_OK;
1240 static HRESULT WINAPI GST_ChangeRate(IMediaSeeking *iface) {
1241 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1242 GstEvent *ev = gst_event_new_seek(This->seek.dRate, GST_FORMAT_TIME, 0, GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1);
1243 TRACE("(%p) New rate %g\n", iface, This->seek.dRate);
1244 gst_pad_push_event(This->my_sink, ev);
1245 return S_OK;
1248 static HRESULT WINAPI GST_Seeking_QueryInterface(IMediaSeeking *iface, REFIID riid, void **ppv) {
1249 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1250 return IUnknown_QueryInterface((IUnknown *)This, riid, ppv);
1253 static ULONG WINAPI GST_Seeking_AddRef(IMediaSeeking *iface) {
1254 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1255 return IUnknown_AddRef((IUnknown *)This);
1258 static ULONG WINAPI GST_Seeking_Release(IMediaSeeking *iface) {
1259 GSTOutPin *This = impl_from_IMediaSeeking(iface);
1260 return IUnknown_Release((IUnknown *)This);
1263 static const IMediaSeekingVtbl GST_Seeking_Vtbl =
1265 GST_Seeking_QueryInterface,
1266 GST_Seeking_AddRef,
1267 GST_Seeking_Release,
1268 SourceSeekingImpl_GetCapabilities,
1269 SourceSeekingImpl_CheckCapabilities,
1270 SourceSeekingImpl_IsFormatSupported,
1271 SourceSeekingImpl_QueryPreferredFormat,
1272 SourceSeekingImpl_GetTimeFormat,
1273 SourceSeekingImpl_IsUsingTimeFormat,
1274 SourceSeekingImpl_SetTimeFormat,
1275 SourceSeekingImpl_GetDuration,
1276 SourceSeekingImpl_GetStopPosition,
1277 SourceSeekingImpl_GetCurrentPosition,
1278 SourceSeekingImpl_ConvertTimeFormat,
1279 SourceSeekingImpl_SetPositions,
1280 SourceSeekingImpl_GetPositions,
1281 SourceSeekingImpl_GetAvailable,
1282 SourceSeekingImpl_SetRate,
1283 SourceSeekingImpl_GetRate,
1284 SourceSeekingImpl_GetPreroll
1287 static HRESULT WINAPI GST_QualityControl_Notify(IQualityControl *iface, IBaseFilter *sender, Quality qm) {
1288 QualityControlImpl *This = (QualityControlImpl*)iface;
1289 GSTOutPin *pin = (GSTOutPin*)This->self;
1290 gst_pad_push_event(pin->my_sink, gst_event_new_qos(1000./qm.Proportion, qm.Late*100, qm.TimeStamp*100));
1291 return S_OK;
1294 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl = {
1295 QualityControlImpl_QueryInterface,
1296 QualityControlImpl_AddRef,
1297 QualityControlImpl_Release,
1298 GST_QualityControl_Notify,
1299 QualityControlImpl_SetSink
1302 static HRESULT WINAPI GSTOutPin_QueryInterface(IPin *iface, REFIID riid, void **ppv) {
1303 GSTOutPin *This = (GSTOutPin *)iface;
1305 TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
1307 *ppv = NULL;
1309 if (IsEqualIID(riid, &IID_IUnknown))
1310 *ppv = iface;
1311 else if (IsEqualIID(riid, &IID_IPin))
1312 *ppv = iface;
1313 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1314 *ppv = &This->seek;
1315 else if (IsEqualIID(riid, &IID_IQualityControl))
1316 *ppv = &This->qcimpl;
1318 if (*ppv) {
1319 IUnknown_AddRef((IUnknown *)(*ppv));
1320 return S_OK;
1322 FIXME("No interface for %s!\n", debugstr_guid(riid));
1323 return E_NOINTERFACE;
1326 static ULONG WINAPI GSTOutPin_Release(IPin *iface) {
1327 GSTOutPin *This = (GSTOutPin *)iface;
1328 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
1329 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
1331 if (!refCount) {
1332 if (This->their_src)
1333 gst_pad_unlink(This->their_src, This->my_sink);
1334 gst_object_unref(This->my_sink);
1335 CloseHandle(This->caps_event);
1336 DeleteMediaType(This->pmt);
1337 FreeMediaType(&This->pin.pin.mtCurrent);
1338 gst_segment_free(This->segment);
1339 CoTaskMemFree(This);
1340 return 0;
1342 return refCount;
1345 static HRESULT WINAPI GSTOutPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
1347 GSTOutPin *This = (GSTOutPin *)iface;
1349 if (iPosition < 0)
1350 return E_INVALIDARG;
1351 if (iPosition > 0)
1352 return VFW_S_NO_MORE_ITEMS;
1353 CopyMediaType(pmt, This->pmt);
1354 return S_OK;
1357 static HRESULT WINAPI GSTOutPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
1359 /* Unused */
1360 return S_OK;
1363 static HRESULT WINAPI GSTOutPin_DecideAllocator(BaseOutputPin *iface, IMemInputPin *pPin, IMemAllocator **pAlloc)
1365 HRESULT hr;
1366 GSTOutPin *This = (GSTOutPin *)iface;
1367 GSTImpl *GSTfilter = (GSTImpl*)This->pin.pin.pinInfo.pFilter;
1369 pAlloc = NULL;
1370 if (GSTfilter->pInputPin.pAlloc)
1371 hr = IMemInputPin_NotifyAllocator(pPin, GSTfilter->pInputPin.pAlloc, FALSE);
1372 else
1373 hr = VFW_E_NO_ALLOCATOR;
1375 return hr;
1378 static HRESULT WINAPI GSTOutPin_BreakConnect(BaseOutputPin *This)
1380 HRESULT hr;
1382 TRACE("(%p)->()\n", This);
1384 EnterCriticalSection(This->pin.pCritSec);
1385 if (!This->pin.pConnectedTo || !This->pMemInputPin)
1386 hr = VFW_E_NOT_CONNECTED;
1387 else
1389 hr = IPin_Disconnect(This->pin.pConnectedTo);
1390 IPin_Disconnect((IPin *)This);
1392 LeaveCriticalSection(This->pin.pCritSec);
1394 return hr;
1397 static const IPinVtbl GST_OutputPin_Vtbl = {
1398 GSTOutPin_QueryInterface,
1399 BasePinImpl_AddRef,
1400 GSTOutPin_Release,
1401 BaseOutputPinImpl_Connect,
1402 BaseOutputPinImpl_ReceiveConnection,
1403 BaseOutputPinImpl_Disconnect,
1404 BasePinImpl_ConnectedTo,
1405 BasePinImpl_ConnectionMediaType,
1406 BasePinImpl_QueryPinInfo,
1407 BasePinImpl_QueryDirection,
1408 BasePinImpl_QueryId,
1409 GST_OutPin_QueryAccept,
1410 BasePinImpl_EnumMediaTypes,
1411 BasePinImpl_QueryInternalConnections,
1412 BaseOutputPinImpl_EndOfStream,
1413 BaseOutputPinImpl_BeginFlush,
1414 BaseOutputPinImpl_EndFlush,
1415 BasePinImpl_NewSegment
1418 static const BasePinFuncTable output_BaseFuncTable = {
1419 NULL,
1420 BaseOutputPinImpl_AttemptConnection,
1421 BasePinImpl_GetMediaTypeVersion,
1422 GSTOutPin_GetMediaType
1425 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
1426 GSTOutPin_DecideBufferSize,
1427 GSTOutPin_DecideAllocator,
1428 GSTOutPin_BreakConnect
1431 static HRESULT GST_AddPin(GSTImpl *This, const PIN_INFO *piOutput, const AM_MEDIA_TYPE *amt) {
1432 HRESULT hr;
1433 This->ppPins = CoTaskMemRealloc(This->ppPins, (This->cStreams + 1) * sizeof(IPin *));
1435 hr = BaseOutputPin_Construct(&GST_OutputPin_Vtbl, sizeof(GSTOutPin), piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, &This->filter.csFilter, (IPin**)(This->ppPins + This->cStreams));
1436 if (SUCCEEDED(hr)) {
1437 GSTOutPin *pin = This->ppPins[This->cStreams];
1438 pin->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
1439 CopyMediaType(pin->pmt, amt);
1440 pin->pin.pin.pinInfo.pFilter = (LPVOID)This;
1441 pin->caps_event = CreateEventW(NULL, 0, 0, NULL);
1442 pin->segment = gst_segment_new();
1443 This->cStreams++;
1444 QualityControlImpl_init(&pin->qcimpl, NULL, (IBaseFilter*)pin);
1445 pin->qcimpl.lpVtbl = &GSTOutPin_QualityControl_Vtbl;
1446 SourceSeeking_Init(&pin->seek, &GST_Seeking_Vtbl, GST_ChangeStop, GST_ChangeCurrent, GST_ChangeRate, &This->filter.csFilter);
1447 BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
1448 } else
1449 ERR("Failed with error %x\n", hr);
1450 return hr;
1453 static HRESULT GST_RemoveOutputPins(GSTImpl *This) {
1454 HRESULT hr;
1455 ULONG i;
1456 GSTOutPin **ppOldPins = This->ppPins;
1457 TRACE("(%p)\n", This);
1459 if (!This->gstfilter)
1460 return S_OK;
1461 gst_element_set_state(This->gstfilter, GST_STATE_NULL);
1462 gst_pad_unlink(This->my_src, This->their_sink);
1463 This->my_src = This->their_sink = NULL;
1465 for (i = 0; i < This->cStreams; i++) {
1466 hr = BaseOutputPinImpl_BreakConnect(&ppOldPins[i]->pin);
1467 TRACE("Disconnect: %08x\n", hr);
1468 IPin_Release((IPin*)ppOldPins[i]);
1470 This->cStreams = 0;
1471 This->ppPins = NULL;
1472 gst_object_unref(This->gstfilter);
1473 This->gstfilter = NULL;
1474 BaseFilterImpl_IncrementPinVersion((BaseFilter*)This);
1475 CoTaskMemFree(ppOldPins);
1476 return S_OK;
1479 static ULONG WINAPI GSTInPin_Release(IPin *iface) {
1480 GSTInPin *This = (GSTInPin*)iface;
1481 ULONG refCount = InterlockedDecrement(&This->pin.refCount);
1483 TRACE("(%p)->() Release from %d\n", iface, refCount + 1);
1484 if (!refCount) {
1485 FreeMediaType(&This->pin.mtCurrent);
1486 if (This->pAlloc)
1487 IMemAllocator_Release(This->pAlloc);
1488 This->pAlloc = NULL;
1489 This->pin.lpVtbl = NULL;
1490 return 0;
1491 } else
1492 return refCount;
1495 static HRESULT WINAPI GSTInPin_ReceiveConnection(IPin *iface, IPin *pReceivePin, const AM_MEDIA_TYPE *pmt) {
1496 PIN_DIRECTION pindirReceive;
1497 HRESULT hr = S_OK;
1498 GSTInPin *This = (GSTInPin*)iface;
1500 TRACE("(%p/%p)->(%p, %p)\n", This, iface, pReceivePin, pmt);
1501 dump_AM_MEDIA_TYPE(pmt);
1503 EnterCriticalSection(This->pin.pCritSec);
1504 if (!This->pin.pConnectedTo) {
1505 ALLOCATOR_PROPERTIES props;
1507 props.cBuffers = 8;
1508 props.cbBuffer = 16384;
1509 props.cbAlign = 1;
1510 props.cbPrefix = 0;
1512 if (SUCCEEDED(hr) && IPin_QueryAccept(iface, pmt) != S_OK)
1513 hr = VFW_E_TYPE_NOT_ACCEPTED;
1514 if (SUCCEEDED(hr)) {
1515 IPin_QueryDirection(pReceivePin, &pindirReceive);
1516 if (pindirReceive != PINDIR_OUTPUT) {
1517 ERR("Can't connect from non-output pin\n");
1518 hr = VFW_E_INVALID_DIRECTION;
1522 This->pReader = NULL;
1523 This->pAlloc = NULL;
1524 if (SUCCEEDED(hr))
1525 hr = IPin_QueryInterface(pReceivePin, &IID_IAsyncReader, (LPVOID *)&This->pReader);
1526 if (SUCCEEDED(hr))
1527 hr = GST_Connect(This, pReceivePin, &props);
1528 if (SUCCEEDED(hr))
1529 hr = IAsyncReader_RequestAllocator(This->pReader, NULL, &props, &This->pAlloc);
1530 if (SUCCEEDED(hr)) {
1531 CopyMediaType(&This->pin.mtCurrent, pmt);
1532 This->pin.pConnectedTo = pReceivePin;
1533 IPin_AddRef(pReceivePin);
1534 hr = IMemAllocator_Commit(This->pAlloc);
1535 } else {
1536 GST_RemoveOutputPins((GSTImpl *)This->pin.pinInfo.pFilter);
1537 if (This->pReader)
1538 IAsyncReader_Release(This->pReader);
1539 This->pReader = NULL;
1540 if (This->pAlloc)
1541 IMemAllocator_Release(This->pAlloc);
1542 This->pAlloc = NULL;
1544 TRACE("Size: %i\n", props.cbBuffer);
1545 } else
1546 hr = VFW_E_ALREADY_CONNECTED;
1547 LeaveCriticalSection(This->pin.pCritSec);
1548 return hr;
1551 static HRESULT WINAPI GSTInPin_Disconnect(IPin *iface) {
1552 HRESULT hr;
1553 GSTInPin *This = (GSTInPin*)iface;
1554 FILTER_STATE state;
1555 TRACE("()\n");
1557 hr = IBaseFilter_GetState(This->pin.pinInfo.pFilter, INFINITE, &state);
1558 EnterCriticalSection(This->pin.pCritSec);
1559 if (This->pin.pConnectedTo) {
1560 GSTImpl *Parser = (GSTImpl *)This->pin.pinInfo.pFilter;
1562 if (SUCCEEDED(hr) && state == State_Stopped) {
1563 IMemAllocator_Decommit(This->pAlloc);
1564 IPin_Disconnect(This->pin.pConnectedTo);
1565 This->pin.pConnectedTo = NULL;
1566 hr = GST_RemoveOutputPins(Parser);
1567 } else
1568 hr = VFW_E_NOT_STOPPED;
1569 } else
1570 hr = S_FALSE;
1571 LeaveCriticalSection(This->pin.pCritSec);
1572 return hr;
1575 static HRESULT WINAPI GSTInPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt) {
1576 GSTInPin *This = (GSTInPin*)iface;
1578 TRACE("(%p)->(%p)\n", This, pmt);
1579 dump_AM_MEDIA_TYPE(pmt);
1581 if (IsEqualIID(&pmt->majortype, &MEDIATYPE_Stream))
1582 return S_OK;
1583 return S_FALSE;
1586 static HRESULT WINAPI GSTInPin_EndOfStream(IPin *iface) {
1587 GSTInPin *pin = (GSTInPin*)iface;
1588 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
1590 FIXME("Propagate message on %p\n", This);
1591 return S_OK;
1594 static HRESULT WINAPI GSTInPin_BeginFlush(IPin *iface) {
1595 GSTInPin *pin = (GSTInPin*)iface;
1596 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
1598 FIXME("Propagate message on %p\n", This);
1599 return S_OK;
1602 static HRESULT WINAPI GSTInPin_EndFlush(IPin *iface) {
1603 GSTInPin *pin = (GSTInPin*)iface;
1604 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
1606 FIXME("Propagate message on %p\n", This);
1607 return S_OK;
1610 static HRESULT WINAPI GSTInPin_NewSegment(IPin *iface, REFERENCE_TIME tStart, REFERENCE_TIME tStop, double dRate) {
1611 GSTInPin *pin = (GSTInPin*)iface;
1612 GSTImpl *This = (GSTImpl*)pin->pin.pinInfo.pFilter;
1614 BasePinImpl_NewSegment(iface, tStart, tStop, dRate);
1615 FIXME("Propagate message on %p\n", This);
1616 return S_OK;
1619 static HRESULT WINAPI GSTInPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
1621 GSTInPin *This = (GSTInPin*)iface;
1623 TRACE("(%p/%p)->(%s, %p)\n", This, iface, debugstr_guid(riid), ppv);
1625 *ppv = NULL;
1627 if (IsEqualIID(riid, &IID_IUnknown))
1628 *ppv = iface;
1629 else if (IsEqualIID(riid, &IID_IPin))
1630 *ppv = iface;
1631 else if (IsEqualIID(riid, &IID_IMediaSeeking))
1633 return IBaseFilter_QueryInterface(This->pin.pinInfo.pFilter, &IID_IMediaSeeking, ppv);
1636 if (*ppv)
1638 IUnknown_AddRef((IUnknown *)(*ppv));
1639 return S_OK;
1642 FIXME("No interface for %s!\n", debugstr_guid(riid));
1644 return E_NOINTERFACE;
1647 static HRESULT WINAPI GSTInPin_EnumMediaTypes(IPin *iface, IEnumMediaTypes **ppEnum)
1649 BasePin *This = (BasePin *)iface;
1651 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
1653 return EnumMediaTypes_Construct(This, BasePinImpl_GetMediaType, BasePinImpl_GetMediaTypeVersion, ppEnum);
1656 static const IPinVtbl GST_InputPin_Vtbl = {
1657 GSTInPin_QueryInterface,
1658 BasePinImpl_AddRef,
1659 GSTInPin_Release,
1660 BaseInputPinImpl_Connect,
1661 GSTInPin_ReceiveConnection,
1662 GSTInPin_Disconnect,
1663 BasePinImpl_ConnectedTo,
1664 BasePinImpl_ConnectionMediaType,
1665 BasePinImpl_QueryPinInfo,
1666 BasePinImpl_QueryDirection,
1667 BasePinImpl_QueryId,
1668 GSTInPin_QueryAccept,
1669 GSTInPin_EnumMediaTypes,
1670 BasePinImpl_QueryInternalConnections,
1671 GSTInPin_EndOfStream,
1672 GSTInPin_BeginFlush,
1673 GSTInPin_EndFlush,
1674 GSTInPin_NewSegment