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
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"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(gstreamer
);
47 typedef struct GSTOutPin GSTOutPin
;
48 typedef struct GSTInPin
{
50 IAsyncReader
*pReader
;
51 IMemAllocator
*pAlloc
;
54 typedef struct GSTImpl
{
63 BOOL discont
, initial
;
64 GstElement
*gstfilter
;
65 GstPad
*my_src
, *their_sink
;
67 guint64 start
, nextofs
, nextpullofs
, stop
;
68 ALLOCATOR_PROPERTIES props
;
69 HANDLE event
, changed_ofs
;
83 QualityControlImpl qcimpl
;
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
;
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
);
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;
123 wfx
->wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
124 if (!gst_structure_get_int(arg
, "channels", (INT
*)&wfx
->nChannels
))
126 if (!gst_structure_get_int(arg
, "rate", (INT
*)&wfx
->nSamplesPerSec
))
128 gst_structure_get_int(arg
, "width", &depth
);
129 gst_structure_get_int(arg
, "depth", &bpp
);
130 if (!depth
|| depth
> 32 || depth
% 8)
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;
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;
151 wfe
->SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
152 wfx
->nBlockAlign
= wfx
->nChannels
* wfx
->wBitsPerSample
/8;
153 wfx
->nAvgBytesPerSec
= wfx
->nSamplesPerSec
* wfx
->nBlockAlign
;
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
;
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
);
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
))
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;
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
))
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;
187 FIXME("Unknown bpp %u\n", bih
->biBitCount
);
190 bih
->biCompression
= BI_RGB
;
192 amt
->subtype
= MEDIATYPE_Video
;
193 if (!gst_structure_get_fourcc(arg
, "format", &amt
->subtype
.Data1
))
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
;
223 static gboolean
accept_caps_sink(GstPad
*pad
, GstCaps
*caps
) {
224 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
227 const char *typename
;
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")) {
234 ERR("Setting audio caps on non-audio pad?\n");
237 ret
= amt_from_gst_caps_audio(caps
, &amt
);
241 } else if (!strcmp(typename
, "video/x-raw-rgb")
242 || !strcmp(typename
, "video/x-raw-yuv")) {
244 ERR("Setting video caps on non-video pad?\n");
247 ret
= amt_from_gst_caps_video(caps
, &amt
);
252 FIXME("Unhandled type \"%s\"\n", typename
);
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
;
262 const char *typename
;
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")) {
269 ERR("Setting audio caps on non-audio pad?\n");
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")) {
276 ERR("Setting video caps on non-video pad?\n");
279 ret
= amt_from_gst_caps_video(caps
, &amt
);
281 This
->props
.cbBuffer
= max(This
->props
.cbBuffer
, ((VIDEOINFOHEADER
*)amt
.pbFormat
)->bmiHeader
.biSizeImage
);
283 FIXME("Unhandled type \"%s\"\n", typename
);
286 TRACE("Linking returned %i for %s\n", ret
, typename
);
289 FreeMediaType(pin
->pmt
);
294 static gboolean
gst_base_src_perform_seek(GSTImpl
*This
, GstEvent
*event
)
298 GstFormat seek_format
;
300 GstSeekType cur_type
, stop_type
;
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
);
315 flush
= flags
& GST_SEEK_FLAG_FLUSH
;
316 seqnum
= gst_event_get_seqnum(event
);
318 /* send flush start */
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
);
326 gst_pad_activate_push(This
->my_src
, 0);
329 TRACE("++++++++++++++++ perform byte seek ------------------\n");
331 /* and prepare to continue streaming */
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
);
339 gst_pad_activate_push(This
->my_src
, 1);
345 static gboolean
event_src(GstPad
*pad
, GstEvent
*event
) {
346 GSTImpl
*This
= gst_pad_get_element_private(pad
);
347 switch (event
->type
) {
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
);
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
);
363 FIXME("%p stub\n", event
);
364 return gst_pad_event_default(pad
, event
);
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
: {
374 gdouble rate
, applied_rate
;
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
);
382 gst_segment_set_newsegment_full(pin
->segment
, update
, rate
, applied_rate
, format
, start
, stop
, pos
);
386 if (pin
->pin
.pin
.pConnectedTo
)
387 IPin_NewSegment(pin
->pin
.pin
.pConnectedTo
, pos
, stop
, rate
*applied_rate
);
391 if (pin
->pin
.pin
.pConnectedTo
)
392 IPin_EndOfStream(pin
->pin
.pin
.pConnectedTo
);
394 case GST_EVENT_FLUSH_START
:
395 if (pin
->pin
.pin
.pConnectedTo
)
396 IPin_BeginFlush(pin
->pin
.pin
.pConnectedTo
);
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
);
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
) {
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
;
422 IAsyncReader_Length(This
->pInputPin
.pReader
, &maxlen
, &curlen
);
423 TRACE("Starting..\n");
425 REFERENCE_TIME tStart
, tStop
;
431 hr
= IMemAllocator_GetBuffer(This
->pInputPin
.pAlloc
, &buf
, NULL
, NULL
, 0);
435 if (This
->nextofs
>= maxlen
)
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);
447 IMediaSample_Release(buf
);
450 This
->nextofs
+= len
;
451 hr
= IAsyncReader_WaitForNext(This
->pInputPin
.pReader
, -1, &buf
, &user
);
452 if (FAILED(hr
) || !buf
) {
454 IMediaSample_Release(buf
);
458 IMediaSample_GetPointer(buf
, &data
);
459 gstbuf
= gst_app_buffer_new(data
, IMediaSample_GetActualDataLength(buf
), release_sample
, buf
);
461 IMediaSample_Release(buf
);
464 gstbuf
->duration
= gstbuf
->timestamp
= -1;
465 ret
= gst_pad_push(This
->my_src
, gstbuf
);
469 ERR("Sending returned: %i\n", ret
);
470 if (ret
== GST_FLOW_ERROR
)
472 else if (ret
== GST_FLOW_WRONG_STATE
)
473 hr
= VFW_E_WRONG_STATE
;
474 else if (ret
== GST_FLOW_RESEND
)
480 gst_pad_push_event(This
->my_src
, gst_event_new_eos());
482 TRACE("Almost stopping.. %08x\n", hr
);
484 IAsyncReader_WaitForNext(This
->pInputPin
.pReader
, 0, &buf
, &user
);
486 IMediaSample_Release(buf
);
489 TRACE("Stopping.. %08x\n", hr
);
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
);
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
;
504 BOOL freeSamp
= FALSE
;
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");
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
;
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
);
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
);
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
);
553 IMediaSample_SetTime(sample
, rtStart
>= 0 ? &rtStart
: NULL
, NULL
);
554 IMediaSample_SetMediaTime(sample
, NULL
, NULL
);
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
;
568 hr
= IMemInputPin_Receive(pin
->pin
.pMemInputPin
, sample
);
569 TRACE("sending sample: %08x\n", hr
);
570 gst_buffer_unref(buf
);
572 IMediaSample_Release(sample
);
573 if (hr
== VFW_E_NOT_CONNECTED
)
574 return GST_FLOW_NOT_LINKED
;
576 return GST_FLOW_WRONG_STATE
;
578 return GST_FLOW_RESEND
;
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
;
589 TRACE("Requesting buffer\n");
592 ret
= setcaps_sink(pad
, caps
);
594 return GST_FLOW_NOT_NEGOTIATED
;
595 *buf
= gst_buffer_new_and_alloc(size
);
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
;
607 ERR("Could not get output buffer: %08x\n", hr
);
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
);
615 IMediaSample_Release(sample
);
616 ERR("Out of memory\n");
617 return GST_FLOW_ERROR
;
619 gst_buffer_set_caps(*buf
, caps
);
623 static GstFlowReturn
request_buffer_src(GstPad
*pad
, guint64 ofs
, guint len
, GstBuffer
**buf
) {
624 GSTImpl
*This
= gst_pad_get_element_private(pad
);
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
);
642 hr
= IAsyncReader_SyncRead(This
->pInputPin
.pReader
, ofs
, len
, GST_BUFFER_DATA(*buf
));
644 ERR("Returned %08x\n", hr
);
645 return GST_FLOW_ERROR
;
651 static DWORD CALLBACK
push_data_init(LPVOID iface
) {
652 GSTImpl
*This
= iface
;
655 TRACE("Starting..\n");
658 GstFlowReturn ret
= request_buffer_src(This
->my_src
, ofs
, 4096, &buf
);
660 ERR("Obtaining buffer returned: %i\n", ret
);
663 ret
= gst_pad_push(This
->my_src
, buf
);
666 TRACE("Sending returned: %i\n", ret
);
670 TRACE("Stopping..\n");
674 static void removed_decoded_pad(GstElement
*bin
, GstPad
*pad
, GSTImpl
*This
) {
678 EnterCriticalSection(&This
->filter
.csFilter
);
679 for (x
= 0; x
< This
->cStreams
; ++x
) {
680 if (This
->ppPins
[x
]->their_src
== pad
)
683 if (x
== This
->cStreams
)
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
;
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
) {
697 const char *typename
;
699 AM_MEDIA_TYPE amt
= { };
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
);
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")) {
729 } else if (!strcmp(typename
, "video/x-raw-rgb")
730 || !strcmp(typename
, "video/x-raw-yuv")) {
733 FIXME("Unknown type \'%s\'\n", typename
);
736 GST_PAD_CAPS(mypad
) = GST_CAPS_ANY
;
737 hr
= GST_AddPin(This
, &piOutput
, &amt
);
742 pin
= This
->ppPins
[This
->cStreams
- 1];
743 gst_pad_set_element_private(mypad
, pin
);
744 pin
->my_sink
= mypad
;
748 ret
= gst_pad_link(pad
, mypad
);
749 gst_pad_activate_push(mypad
, 1);
750 FIXME("Linking: %i\n", ret
);
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
) {
761 if (gst_pad_is_linked(pad
))
764 /* Still holding our own lock */
766 init_new_decoded_pad(bin
, pad
, last
, This
);
770 EnterCriticalSection(&This
->filter
.csFilter
);
771 for (x
= 0; x
< This
->cStreams
; ++x
) {
772 GSTOutPin
*pin
= This
->ppPins
[x
];
773 if (!pin
->their_src
) {
774 if (gst_pad_link(pad
, pin
->my_sink
) >= 0) {
775 pin
->their_src
= pad
;
776 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
777 gst_object_ref(pin
->their_src
);
779 LeaveCriticalSection(&This
->filter
.csFilter
);
784 init_new_decoded_pad(bin
, pad
, last
, This
);
785 LeaveCriticalSection(&This
->filter
.csFilter
);
788 static gboolean
check_get_range(GstPad
*pad
) {
792 static gboolean
query_function(GstPad
*pad
, GstQuery
*query
) {
793 GSTImpl
*This
= gst_pad_get_element_private(pad
);
798 switch (GST_QUERY_TYPE(query
)) {
799 case GST_QUERY_DURATION
:
800 gst_query_parse_duration (query
, &format
, NULL
);
801 if (format
== GST_FORMAT_PERCENT
) {
802 gst_query_set_duration (query
, GST_FORMAT_PERCENT
, GST_FORMAT_PERCENT_MAX
);
805 ret
= gst_pad_query_convert (pad
, GST_FORMAT_BYTES
, This
->filesize
, &format
, &duration
);
806 gst_query_set_duration(query
, format
, duration
);
808 case GST_QUERY_SEEKING
:
809 gst_query_parse_seeking (query
, &format
, NULL
, NULL
, NULL
);
810 TRACE("Seeking %i %i\n", format
, GST_FORMAT_BYTES
);
811 if (format
!= GST_FORMAT_BYTES
)
813 gst_query_set_seeking(query
, GST_FORMAT_BYTES
, 1, 0, This
->filesize
);
816 FIXME("Unhandled query type %i\n", GST_QUERY_TYPE(query
));
822 static gboolean
activate_push(GstPad
*pad
, gboolean activate
) {
823 GSTImpl
*This
= gst_pad_get_element_private(pad
);
824 EnterCriticalSection(&This
->filter
.csFilter
);
826 TRACE("Deactivating\n");
827 if (This
->push_thread
) {
828 WaitForSingleObject(This
->push_thread
, -1);
829 CloseHandle(This
->push_thread
);
830 This
->push_thread
= NULL
;
832 } else if (!This
->push_thread
) {
833 TRACE("Activating\n");
835 This
->push_thread
= CreateThread(NULL
, 0, push_data_init
, This
, 0, NULL
);
837 This
->push_thread
= CreateThread(NULL
, 0, push_data
, This
, 0, NULL
);
839 LeaveCriticalSection(&This
->filter
.csFilter
);
843 static void no_more_pads(GstElement
*decodebin
, GSTImpl
*This
) {
845 SetEvent(This
->event
);
849 GST_AUTOPLUG_SELECT_TRY
,
850 GST_AUTOPLUG_SELECT_EXPOSE
,
851 GST_AUTOPLUG_SELECT_SKIP
852 } GstAutoplugSelectResult
;
854 static GstAutoplugSelectResult
autoplug_blacklist(GstElement
*bin
, GstPad
*pad
, GstCaps
*caps
, GstElementFactory
*fact
, GSTImpl
*This
) {
855 const char *name
= gst_element_factory_get_longname(fact
);
857 if (strstr(name
, "Player protection")) {
858 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
859 return GST_AUTOPLUG_SELECT_SKIP
;
861 if (!strcmp(name
, "Fluendo Hardware Accelerated Video Decoder")) {
862 WARN("Disabled video acceleration since it breaks in wine\n");
863 return GST_AUTOPLUG_SELECT_SKIP
;
865 TRACE("using \"%s\"\n", name
);
866 return GST_AUTOPLUG_SELECT_TRY
;
869 static GstBusSyncReply
watch_bus(GstBus
*bus
, GstMessage
*msg
, gpointer data
) {
870 GSTImpl
*This
= data
;
872 gchar
*dbg_info
= NULL
;
873 if (GST_MESSAGE_TYPE(msg
) & GST_MESSAGE_ERROR
) {
874 gst_message_parse_error(msg
, &err
, &dbg_info
);
875 FIXME("%s: %s\n", GST_OBJECT_NAME(msg
->src
), err
->message
);
876 WARN("%s\n", dbg_info
);
877 SetEvent(This
->event
);
878 } else if (GST_MESSAGE_TYPE(msg
) & GST_MESSAGE_WARNING
) {
879 gst_message_parse_warning(msg
, &err
, &dbg_info
);
880 WARN("%s: %s\n", GST_OBJECT_NAME(msg
->src
), err
->message
);
881 WARN("%s\n", dbg_info
);
889 static void unknown_type(GstElement
*bin
, GstPad
*pad
, GstCaps
*caps
, GSTImpl
*This
) {
890 gchar
*strcaps
= gst_caps_to_string(caps
);
891 FIXME("Could not find a filter for caps: %s\n", strcaps
);
895 static HRESULT
GST_Connect(GSTInPin
*pPin
, IPin
*pConnectPin
, ALLOCATOR_PROPERTIES
*props
) {
896 GSTImpl
*This
= (GSTImpl
*)pPin
->pin
.pinInfo
.pFilter
;
899 LONGLONG avail
, duration
;
900 GstFormat format
= GST_FORMAT_TIME
;
901 GstStaticPadTemplate src_template
= GST_STATIC_PAD_TEMPLATE(
905 GST_STATIC_CAPS_ANY
);
907 TRACE("%p %p %p\n", pPin
, pConnectPin
, props
);
908 This
->props
= *props
;
909 IAsyncReader_Length(pPin
->pReader
, &This
->filesize
, &avail
);
912 This
->bus
= gst_bus_new();
913 gst_bus_set_sync_handler(This
->bus
, watch_bus
, This
);
916 This
->gstfilter
= gst_element_factory_make("decodebin2", NULL
);
917 if (!This
->gstfilter
) {
918 FIXME("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
919 8 * (int)sizeof(void*));
922 gst_element_set_bus(This
->gstfilter
, This
->bus
);
923 g_signal_connect(This
->gstfilter
, "new-decoded-pad", G_CALLBACK(existing_new_pad
), This
);
924 g_signal_connect(This
->gstfilter
, "pad-removed", G_CALLBACK(removed_decoded_pad
), This
);
925 g_signal_connect(This
->gstfilter
, "autoplug-select", G_CALLBACK(autoplug_blacklist
), This
);
926 g_signal_connect(This
->gstfilter
, "unknown-type", G_CALLBACK(unknown_type
), This
);
928 This
->my_src
= gst_pad_new_from_static_template(&src_template
, "quartz-src");
929 gst_pad_set_getrange_function(This
->my_src
, request_buffer_src
);
930 gst_pad_set_checkgetrange_function(This
->my_src
, check_get_range
);
931 gst_pad_set_query_function(This
->my_src
, query_function
);
932 gst_pad_set_activatepush_function(This
->my_src
, activate_push
);
933 gst_pad_set_event_function(This
->my_src
, event_src
);
934 gst_pad_set_element_private (This
->my_src
, This
);
935 This
->their_sink
= gst_element_get_static_pad(This
->gstfilter
, "sink");
937 g_signal_connect(This
->gstfilter
, "no-more-pads", G_CALLBACK(no_more_pads
), This
);
938 ret
= gst_pad_link(This
->my_src
, This
->their_sink
);
939 gst_object_unref(This
->their_sink
);
941 ERR("Returns: %i\n", ret
);
944 This
->start
= This
->nextofs
= This
->nextpullofs
= 0;
945 This
->stop
= This
->filesize
;
947 /* Add initial pins */
948 This
->initial
= This
->discont
= 1;
949 ResetEvent(This
->event
);
950 gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
951 gst_pad_set_active(This
->my_src
, 1);
952 WaitForSingleObject(This
->event
, -1);
953 gst_element_get_state(This
->gstfilter
, NULL
, NULL
, -1);
956 WARN("Ret: %i\n", ret
);
958 } else if (!This
->cStreams
) {
959 FIXME("Gstreamer could not find any streams\n");
962 gst_pad_query_duration(This
->ppPins
[0]->their_src
, &format
, &duration
);
963 for (i
= 0; i
< This
->cStreams
; ++i
) {
964 This
->ppPins
[i
]->seek
.llDuration
= This
->ppPins
[i
]->seek
.llStop
= duration
/ 100;
965 WaitForSingleObject(This
->ppPins
[i
]->caps_event
, -1);
969 *props
= This
->props
;
970 gst_element_set_state(This
->gstfilter
, GST_STATE_READY
);
971 gst_element_get_state(This
->gstfilter
, NULL
, NULL
, -1);
974 This
->nextofs
= This
->nextpullofs
= 0;
978 static inline GSTOutPin
*impl_from_IMediaSeeking( IMediaSeeking
*iface
) {
979 return (GSTOutPin
*)((char*)iface
- FIELD_OFFSET(GSTOutPin
, seek
.lpVtbl
));
982 static IPin
* WINAPI
GST_GetPin(BaseFilter
*iface
, int pos
)
984 GSTImpl
*This
= (GSTImpl
*)iface
;
985 TRACE("Asking for pos %x\n", pos
);
987 if (pos
> This
->cStreams
|| pos
< 0)
991 IPin_AddRef((IPin
*)&This
->pInputPin
);
992 return (IPin
*)&This
->pInputPin
;
996 IPin_AddRef((IPin
*)This
->ppPins
[pos
- 1]);
997 return (IPin
*)This
->ppPins
[pos
- 1];
1001 static LONG WINAPI
GST_GetPinCount(BaseFilter
*iface
)
1003 GSTImpl
*This
= (GSTImpl
*)iface
;
1004 return (This
->cStreams
+ 1);
1007 static const BaseFilterFuncTable BaseFuncTable
= {
1012 IUnknown
* CALLBACK
Gstreamer_Splitter_create(IUnknown
*punkout
, HRESULT
*phr
) {
1013 IUnknown
*obj
= NULL
;
1017 if (!Gstreamer_init())
1023 This
= CoTaskMemAlloc(sizeof(*This
));
1024 obj
= (IUnknown
*)This
;
1027 *phr
= E_OUTOFMEMORY
;
1031 BaseFilter_Init(&This
->filter
, &GST_Vtbl
, &CLSID_Gstreamer_Splitter
, (DWORD_PTR
)(__FILE__
": GSTImpl.csFilter"), &BaseFuncTable
);
1034 This
->ppPins
= NULL
;
1035 This
->push_thread
= NULL
;
1036 This
->event
= CreateEventW(NULL
, 0, 0, NULL
);
1039 piInput
= &This
->pInputPin
.pin
.pinInfo
;
1040 piInput
->dir
= PINDIR_INPUT
;
1041 piInput
->pFilter
= (IBaseFilter
*)This
;
1042 lstrcpynW(piInput
->achName
, wcsInputPinName
, sizeof(piInput
->achName
) / sizeof(piInput
->achName
[0]));
1043 This
->pInputPin
.pin
.lpVtbl
= &GST_InputPin_Vtbl
;
1044 This
->pInputPin
.pin
.refCount
= 1;
1045 This
->pInputPin
.pin
.pConnectedTo
= NULL
;
1046 This
->pInputPin
.pin
.pCritSec
= &This
->filter
.csFilter
;
1047 ZeroMemory(&This
->pInputPin
.pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1052 static void GST_Destroy(GSTImpl
*This
) {
1053 IPin
*connected
= NULL
;
1056 TRACE("Destroying\n");
1058 CloseHandle(This
->event
);
1060 /* Don't need to clean up output pins, disconnecting input pin will do that */
1061 IPin_ConnectedTo((IPin
*)&This
->pInputPin
, &connected
);
1063 assert(IPin_Disconnect(connected
) == S_OK
);
1064 IPin_Release(connected
);
1065 assert(IPin_Disconnect((IPin
*)&This
->pInputPin
) == S_OK
);
1067 pinref
= IPin_Release((IPin
*)&This
->pInputPin
);
1069 /* Valgrind could find this, if I kill it here */
1070 ERR("pinref should be null, is %u, destroying anyway\n", pinref
);
1071 assert((LONG
)pinref
> 0);
1074 pinref
= IPin_Release((IPin
*)&This
->pInputPin
);
1077 gst_bus_set_sync_handler(This
->bus
, NULL
, NULL
);
1078 gst_object_unref(This
->bus
);
1080 CoTaskMemFree(This
);
1083 static HRESULT WINAPI
GST_QueryInterface(IBaseFilter
*iface
, REFIID riid
, LPVOID
*ppv
) {
1084 GSTImpl
*This
= (GSTImpl
*)iface
;
1085 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1089 if (IsEqualIID(riid
, &IID_IUnknown
))
1091 else if (IsEqualIID(riid
, &IID_IPersist
))
1093 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
1095 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
1099 IUnknown_AddRef((IUnknown
*)(*ppv
));
1103 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
) &&
1104 !IsEqualIID(riid
, &IID_IAMFilterMiscFlags
))
1105 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1107 return E_NOINTERFACE
;
1110 static ULONG WINAPI
GST_Release(IBaseFilter
*iface
) {
1111 GSTImpl
*This
= (GSTImpl
*)iface
;
1112 ULONG refCount
= BaseFilterImpl_Release(iface
);
1114 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
1122 static HRESULT WINAPI
GST_Stop(IBaseFilter
*iface
) {
1123 GSTImpl
*This
= (GSTImpl
*)iface
;
1127 if (This
->gstfilter
) {
1128 IAsyncReader_BeginFlush(This
->pInputPin
.pReader
);
1129 gst_element_set_state(This
->gstfilter
, GST_STATE_READY
);
1130 IAsyncReader_EndFlush(This
->pInputPin
.pReader
);
1135 static HRESULT WINAPI
GST_Pause(IBaseFilter
*iface
) {
1137 GSTImpl
*This
= (GSTImpl
*)iface
;
1139 GstStateChangeReturn ret
;
1142 if (!This
->gstfilter
)
1143 return VFW_E_NOT_CONNECTED
;
1145 gst_element_get_state(This
->gstfilter
, &now
, NULL
, -1);
1146 if (now
== GST_STATE_PAUSED
)
1148 if (now
!= GST_STATE_PLAYING
)
1149 hr
= IBaseFilter_Run(iface
, -1);
1152 ret
= gst_element_set_state(This
->gstfilter
, GST_STATE_PAUSED
);
1153 if (ret
== GST_STATE_CHANGE_ASYNC
)
1158 static HRESULT WINAPI
GST_Run(IBaseFilter
*iface
, REFERENCE_TIME tStart
) {
1160 GSTImpl
*This
= (GSTImpl
*)iface
;
1163 HRESULT hr_any
= VFW_E_NOT_CONNECTED
;
1165 TRACE("(%s)\n", wine_dbgstr_longlong(tStart
));
1167 if (!This
->gstfilter
)
1168 return VFW_E_NOT_CONNECTED
;
1170 gst_element_get_state(This
->gstfilter
, &now
, NULL
, -1);
1171 if (now
== GST_STATE_PLAYING
)
1173 if (now
== GST_STATE_PAUSED
) {
1174 GstStateChangeReturn ret
;
1175 ret
= gst_element_set_state(This
->gstfilter
, GST_STATE_PAUSED
);
1176 if (ret
== GST_STATE_CHANGE_ASYNC
)
1181 EnterCriticalSection(&This
->filter
.csFilter
);
1182 gst_pad_set_blocked(This
->my_src
, 0);
1183 gst_pad_set_blocked(This
->their_sink
, 0);
1184 gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
1185 This
->filter
.rtStreamStart
= tStart
;
1187 for (i
= 0; i
< This
->cStreams
; i
++) {
1188 hr
= BaseOutputPinImpl_Active((BaseOutputPin
*)This
->ppPins
[i
]);
1189 if (SUCCEEDED(hr
)) {
1190 gst_pad_set_blocked(This
->ppPins
[i
]->my_sink
, 0);
1191 if (This
->ppPins
[i
]->their_src
)
1192 gst_pad_set_blocked(This
->ppPins
[i
]->their_src
, 0);
1198 gst_pad_set_active(This
->my_src
, 1);
1199 LeaveCriticalSection(&This
->filter
.csFilter
);
1204 static HRESULT WINAPI
GST_GetState(IBaseFilter
*iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
) {
1205 GSTImpl
*This
= (GSTImpl
*)iface
;
1207 GstState now
, pending
;
1208 GstStateChangeReturn ret
;
1210 TRACE("(%d, %p)\n", dwMilliSecsTimeout
, pState
);
1212 if (!This
->gstfilter
) {
1213 *pState
= State_Stopped
;
1217 ret
= gst_element_get_state(This
->gstfilter
, &now
, &pending
, dwMilliSecsTimeout
== INFINITE
? -1 : dwMilliSecsTimeout
* 1000);
1219 if (ret
== GST_STATE_CHANGE_ASYNC
)
1220 hr
= VFW_S_STATE_INTERMEDIATE
;
1225 case GST_STATE_PAUSED
: *pState
= State_Paused
; return hr
;
1226 case GST_STATE_PLAYING
: *pState
= State_Running
; return hr
;
1227 default: *pState
= State_Stopped
; return hr
;
1231 static HRESULT WINAPI
GST_FindPin(IBaseFilter
*iface
, LPCWSTR Id
, IPin
**ppPin
) {
1232 FIXME("(%p)->(%s,%p) stub\n", iface
, debugstr_w(Id
), ppPin
);
1236 static const IBaseFilterVtbl GST_Vtbl
= {
1238 BaseFilterImpl_AddRef
,
1240 BaseFilterImpl_GetClassID
,
1245 BaseFilterImpl_SetSyncSource
,
1246 BaseFilterImpl_GetSyncSource
,
1247 BaseFilterImpl_EnumPins
,
1249 BaseFilterImpl_QueryFilterInfo
,
1250 BaseFilterImpl_JoinFilterGraph
,
1251 BaseFilterImpl_QueryVendorInfo
1254 static HRESULT WINAPI
GST_ChangeCurrent(IMediaSeeking
*iface
) {
1255 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1256 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);
1257 TRACE("(%p) going to %i.%i!\n", iface
, (int)(This
->seek
.llCurrent
/ 10000000), (int)((This
->seek
.llCurrent
/ 10000)%1000));
1258 gst_pad_push_event(This
->my_sink
, ev
);
1262 static HRESULT WINAPI
GST_ChangeStop(IMediaSeeking
*iface
) {
1263 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1264 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
);
1265 TRACE("(%p) going to %i.%i!\n", iface
, (int)(This
->seek
.llCurrent
/ 10000000), (int)((This
->seek
.llCurrent
/ 10000)%1000));
1266 gst_pad_push_event(This
->my_sink
, ev
);
1270 static HRESULT WINAPI
GST_ChangeRate(IMediaSeeking
*iface
) {
1271 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1272 GstEvent
*ev
= gst_event_new_seek(This
->seek
.dRate
, GST_FORMAT_TIME
, 0, GST_SEEK_TYPE_NONE
, -1, GST_SEEK_TYPE_NONE
, -1);
1273 TRACE("(%p) New rate %g\n", iface
, This
->seek
.dRate
);
1274 gst_pad_push_event(This
->my_sink
, ev
);
1278 static HRESULT WINAPI
GST_Seeking_QueryInterface(IMediaSeeking
*iface
, REFIID riid
, void **ppv
) {
1279 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1280 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
1283 static ULONG WINAPI
GST_Seeking_AddRef(IMediaSeeking
*iface
) {
1284 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1285 return IUnknown_AddRef((IUnknown
*)This
);
1288 static ULONG WINAPI
GST_Seeking_Release(IMediaSeeking
*iface
) {
1289 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1290 return IUnknown_Release((IUnknown
*)This
);
1293 static const IMediaSeekingVtbl GST_Seeking_Vtbl
=
1295 GST_Seeking_QueryInterface
,
1297 GST_Seeking_Release
,
1298 SourceSeekingImpl_GetCapabilities
,
1299 SourceSeekingImpl_CheckCapabilities
,
1300 SourceSeekingImpl_IsFormatSupported
,
1301 SourceSeekingImpl_QueryPreferredFormat
,
1302 SourceSeekingImpl_GetTimeFormat
,
1303 SourceSeekingImpl_IsUsingTimeFormat
,
1304 SourceSeekingImpl_SetTimeFormat
,
1305 SourceSeekingImpl_GetDuration
,
1306 SourceSeekingImpl_GetStopPosition
,
1307 SourceSeekingImpl_GetCurrentPosition
,
1308 SourceSeekingImpl_ConvertTimeFormat
,
1309 SourceSeekingImpl_SetPositions
,
1310 SourceSeekingImpl_GetPositions
,
1311 SourceSeekingImpl_GetAvailable
,
1312 SourceSeekingImpl_SetRate
,
1313 SourceSeekingImpl_GetRate
,
1314 SourceSeekingImpl_GetPreroll
1317 static HRESULT WINAPI
GST_QualityControl_Notify(IQualityControl
*iface
, IBaseFilter
*sender
, Quality qm
) {
1318 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
1319 GSTOutPin
*pin
= (GSTOutPin
*)This
->self
;
1320 gst_pad_push_event(pin
->my_sink
, gst_event_new_qos(1000./qm
.Proportion
, qm
.Late
*100, qm
.TimeStamp
*100));
1324 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl
= {
1325 QualityControlImpl_QueryInterface
,
1326 QualityControlImpl_AddRef
,
1327 QualityControlImpl_Release
,
1328 GST_QualityControl_Notify
,
1329 QualityControlImpl_SetSink
1332 static HRESULT WINAPI
GSTOutPin_QueryInterface(IPin
*iface
, REFIID riid
, void **ppv
) {
1333 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1335 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1339 if (IsEqualIID(riid
, &IID_IUnknown
))
1341 else if (IsEqualIID(riid
, &IID_IPin
))
1343 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1345 else if (IsEqualIID(riid
, &IID_IQualityControl
))
1346 *ppv
= &This
->qcimpl
;
1349 IUnknown_AddRef((IUnknown
*)(*ppv
));
1352 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1353 return E_NOINTERFACE
;
1356 static ULONG WINAPI
GSTOutPin_Release(IPin
*iface
) {
1357 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1358 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
1359 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
1362 if (This
->their_src
)
1363 gst_pad_unlink(This
->their_src
, This
->my_sink
);
1364 gst_object_unref(This
->my_sink
);
1365 CloseHandle(This
->caps_event
);
1366 DeleteMediaType(This
->pmt
);
1367 FreeMediaType(&This
->pin
.pin
.mtCurrent
);
1368 gst_segment_free(This
->segment
);
1369 CoTaskMemFree(This
);
1375 static HRESULT WINAPI
GSTOutPin_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
1377 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1380 return E_INVALIDARG
;
1382 return VFW_S_NO_MORE_ITEMS
;
1383 CopyMediaType(pmt
, This
->pmt
);
1387 static HRESULT WINAPI
GSTOutPin_DecideBufferSize(BaseOutputPin
*iface
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
1393 static HRESULT WINAPI
GSTOutPin_DecideAllocator(BaseOutputPin
*iface
, IMemInputPin
*pPin
, IMemAllocator
**pAlloc
)
1396 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1397 GSTImpl
*GSTfilter
= (GSTImpl
*)This
->pin
.pin
.pinInfo
.pFilter
;
1400 if (GSTfilter
->pInputPin
.pAlloc
)
1401 hr
= IMemInputPin_NotifyAllocator(pPin
, GSTfilter
->pInputPin
.pAlloc
, FALSE
);
1403 hr
= VFW_E_NO_ALLOCATOR
;
1408 static HRESULT WINAPI
GSTOutPin_BreakConnect(BaseOutputPin
*This
)
1412 TRACE("(%p)->()\n", This
);
1414 EnterCriticalSection(This
->pin
.pCritSec
);
1415 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1416 hr
= VFW_E_NOT_CONNECTED
;
1419 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1420 IPin_Disconnect((IPin
*)This
);
1422 LeaveCriticalSection(This
->pin
.pCritSec
);
1427 static const IPinVtbl GST_OutputPin_Vtbl
= {
1428 GSTOutPin_QueryInterface
,
1431 BaseOutputPinImpl_Connect
,
1432 BaseOutputPinImpl_ReceiveConnection
,
1433 BaseOutputPinImpl_Disconnect
,
1434 BasePinImpl_ConnectedTo
,
1435 BasePinImpl_ConnectionMediaType
,
1436 BasePinImpl_QueryPinInfo
,
1437 BasePinImpl_QueryDirection
,
1438 BasePinImpl_QueryId
,
1439 GST_OutPin_QueryAccept
,
1440 BasePinImpl_EnumMediaTypes
,
1441 BasePinImpl_QueryInternalConnections
,
1442 BaseOutputPinImpl_EndOfStream
,
1443 BaseOutputPinImpl_BeginFlush
,
1444 BaseOutputPinImpl_EndFlush
,
1445 BasePinImpl_NewSegment
1448 static const BasePinFuncTable output_BaseFuncTable
= {
1450 BaseOutputPinImpl_AttemptConnection
,
1451 BasePinImpl_GetMediaTypeVersion
,
1452 GSTOutPin_GetMediaType
1455 static const BaseOutputPinFuncTable output_BaseOutputFuncTable
= {
1456 GSTOutPin_DecideBufferSize
,
1457 GSTOutPin_DecideAllocator
,
1458 GSTOutPin_BreakConnect
1461 static HRESULT
GST_AddPin(GSTImpl
*This
, const PIN_INFO
*piOutput
, const AM_MEDIA_TYPE
*amt
) {
1463 This
->ppPins
= CoTaskMemRealloc(This
->ppPins
, (This
->cStreams
+ 1) * sizeof(IPin
*));
1465 hr
= BaseOutputPin_Construct(&GST_OutputPin_Vtbl
, sizeof(GSTOutPin
), piOutput
, &output_BaseFuncTable
, &output_BaseOutputFuncTable
, &This
->filter
.csFilter
, (IPin
**)(This
->ppPins
+ This
->cStreams
));
1466 if (SUCCEEDED(hr
)) {
1467 GSTOutPin
*pin
= This
->ppPins
[This
->cStreams
];
1468 pin
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
1469 CopyMediaType(pin
->pmt
, amt
);
1470 pin
->pin
.pin
.pinInfo
.pFilter
= (LPVOID
)This
;
1471 pin
->caps_event
= CreateEventW(NULL
, 0, 0, NULL
);
1472 pin
->segment
= gst_segment_new();
1474 QualityControlImpl_init(&pin
->qcimpl
, NULL
, (IBaseFilter
*)pin
);
1475 pin
->qcimpl
.lpVtbl
= &GSTOutPin_QualityControl_Vtbl
;
1476 SourceSeeking_Init(&pin
->seek
, &GST_Seeking_Vtbl
, GST_ChangeStop
, GST_ChangeCurrent
, GST_ChangeRate
, &This
->filter
.csFilter
);
1477 BaseFilterImpl_IncrementPinVersion((BaseFilter
*)This
);
1479 ERR("Failed with error %x\n", hr
);
1483 static HRESULT
GST_RemoveOutputPins(GSTImpl
*This
) {
1486 GSTOutPin
**ppOldPins
= This
->ppPins
;
1487 TRACE("(%p)\n", This
);
1489 if (!This
->gstfilter
)
1491 gst_element_set_bus(This
->gstfilter
, NULL
);
1492 gst_element_set_state(This
->gstfilter
, GST_STATE_NULL
);
1493 gst_pad_unlink(This
->my_src
, This
->their_sink
);
1494 This
->my_src
= This
->their_sink
= NULL
;
1496 for (i
= 0; i
< This
->cStreams
; i
++) {
1497 hr
= BaseOutputPinImpl_BreakConnect(&ppOldPins
[i
]->pin
);
1498 TRACE("Disconnect: %08x\n", hr
);
1499 IPin_Release((IPin
*)ppOldPins
[i
]);
1502 This
->ppPins
= NULL
;
1503 gst_object_unref(This
->gstfilter
);
1504 This
->gstfilter
= NULL
;
1505 BaseFilterImpl_IncrementPinVersion((BaseFilter
*)This
);
1506 CoTaskMemFree(ppOldPins
);
1510 static ULONG WINAPI
GSTInPin_Release(IPin
*iface
) {
1511 GSTInPin
*This
= (GSTInPin
*)iface
;
1512 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
1514 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
1516 FreeMediaType(&This
->pin
.mtCurrent
);
1518 IMemAllocator_Release(This
->pAlloc
);
1519 This
->pAlloc
= NULL
;
1520 This
->pin
.lpVtbl
= NULL
;
1526 static HRESULT WINAPI
GSTInPin_ReceiveConnection(IPin
*iface
, IPin
*pReceivePin
, const AM_MEDIA_TYPE
*pmt
) {
1527 PIN_DIRECTION pindirReceive
;
1529 GSTInPin
*This
= (GSTInPin
*)iface
;
1531 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1532 dump_AM_MEDIA_TYPE(pmt
);
1534 EnterCriticalSection(This
->pin
.pCritSec
);
1535 if (!This
->pin
.pConnectedTo
) {
1536 ALLOCATOR_PROPERTIES props
;
1539 props
.cbBuffer
= 16384;
1543 if (SUCCEEDED(hr
) && IPin_QueryAccept(iface
, pmt
) != S_OK
)
1544 hr
= VFW_E_TYPE_NOT_ACCEPTED
;
1545 if (SUCCEEDED(hr
)) {
1546 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1547 if (pindirReceive
!= PINDIR_OUTPUT
) {
1548 ERR("Can't connect from non-output pin\n");
1549 hr
= VFW_E_INVALID_DIRECTION
;
1553 This
->pReader
= NULL
;
1554 This
->pAlloc
= NULL
;
1556 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1558 hr
= GST_Connect(This
, pReceivePin
, &props
);
1560 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1561 if (SUCCEEDED(hr
)) {
1562 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1563 This
->pin
.pConnectedTo
= pReceivePin
;
1564 IPin_AddRef(pReceivePin
);
1565 hr
= IMemAllocator_Commit(This
->pAlloc
);
1567 GST_RemoveOutputPins((GSTImpl
*)This
->pin
.pinInfo
.pFilter
);
1569 IAsyncReader_Release(This
->pReader
);
1570 This
->pReader
= NULL
;
1572 IMemAllocator_Release(This
->pAlloc
);
1573 This
->pAlloc
= NULL
;
1575 TRACE("Size: %i\n", props
.cbBuffer
);
1577 hr
= VFW_E_ALREADY_CONNECTED
;
1578 LeaveCriticalSection(This
->pin
.pCritSec
);
1582 static HRESULT WINAPI
GSTInPin_Disconnect(IPin
*iface
) {
1584 GSTInPin
*This
= (GSTInPin
*)iface
;
1588 hr
= IBaseFilter_GetState(This
->pin
.pinInfo
.pFilter
, INFINITE
, &state
);
1589 EnterCriticalSection(This
->pin
.pCritSec
);
1590 if (This
->pin
.pConnectedTo
) {
1591 GSTImpl
*Parser
= (GSTImpl
*)This
->pin
.pinInfo
.pFilter
;
1593 if (SUCCEEDED(hr
) && state
== State_Stopped
) {
1594 IMemAllocator_Decommit(This
->pAlloc
);
1595 IPin_Disconnect(This
->pin
.pConnectedTo
);
1596 This
->pin
.pConnectedTo
= NULL
;
1597 hr
= GST_RemoveOutputPins(Parser
);
1599 hr
= VFW_E_NOT_STOPPED
;
1602 LeaveCriticalSection(This
->pin
.pCritSec
);
1606 static HRESULT WINAPI
GSTInPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
) {
1607 GSTInPin
*This
= (GSTInPin
*)iface
;
1609 TRACE("(%p)->(%p)\n", This
, pmt
);
1610 dump_AM_MEDIA_TYPE(pmt
);
1612 if (IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
1617 static HRESULT WINAPI
GSTInPin_EndOfStream(IPin
*iface
) {
1618 GSTInPin
*pin
= (GSTInPin
*)iface
;
1619 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1621 FIXME("Propagate message on %p\n", This
);
1625 static HRESULT WINAPI
GSTInPin_BeginFlush(IPin
*iface
) {
1626 GSTInPin
*pin
= (GSTInPin
*)iface
;
1627 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1629 FIXME("Propagate message on %p\n", This
);
1633 static HRESULT WINAPI
GSTInPin_EndFlush(IPin
*iface
) {
1634 GSTInPin
*pin
= (GSTInPin
*)iface
;
1635 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1637 FIXME("Propagate message on %p\n", This
);
1641 static HRESULT WINAPI
GSTInPin_NewSegment(IPin
*iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
) {
1642 GSTInPin
*pin
= (GSTInPin
*)iface
;
1643 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1645 BasePinImpl_NewSegment(iface
, tStart
, tStop
, dRate
);
1646 FIXME("Propagate message on %p\n", This
);
1650 static HRESULT WINAPI
GSTInPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1652 GSTInPin
*This
= (GSTInPin
*)iface
;
1654 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
1658 if (IsEqualIID(riid
, &IID_IUnknown
))
1660 else if (IsEqualIID(riid
, &IID_IPin
))
1662 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1664 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
1669 IUnknown_AddRef((IUnknown
*)(*ppv
));
1673 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1675 return E_NOINTERFACE
;
1678 static HRESULT WINAPI
GSTInPin_EnumMediaTypes(IPin
*iface
, IEnumMediaTypes
**ppEnum
)
1680 BasePin
*This
= (BasePin
*)iface
;
1682 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
1684 return EnumMediaTypes_Construct(This
, BasePinImpl_GetMediaType
, BasePinImpl_GetMediaTypeVersion
, ppEnum
);
1687 static const IPinVtbl GST_InputPin_Vtbl
= {
1688 GSTInPin_QueryInterface
,
1691 BaseInputPinImpl_Connect
,
1692 GSTInPin_ReceiveConnection
,
1693 GSTInPin_Disconnect
,
1694 BasePinImpl_ConnectedTo
,
1695 BasePinImpl_ConnectionMediaType
,
1696 BasePinImpl_QueryPinInfo
,
1697 BasePinImpl_QueryDirection
,
1698 BasePinImpl_QueryId
,
1699 GSTInPin_QueryAccept
,
1700 GSTInPin_EnumMediaTypes
,
1701 BasePinImpl_QueryInternalConnections
,
1702 GSTInPin_EndOfStream
,
1703 GSTInPin_BeginFlush
,