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 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
400 if (pin
->pin
.pin
.pConnectedTo
)
401 IPin_EndFlush(pin
->pin
.pin
.pConnectedTo
);
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
;
423 IAsyncReader_Length(This
->pInputPin
.pReader
, &maxlen
, &curlen
);
426 TRACE("Starting..\n");
428 REFERENCE_TIME tStart
, tStop
;
434 hr
= IMemAllocator_GetBuffer(This
->pInputPin
.pAlloc
, &buf
, NULL
, NULL
, 0);
438 if (This
->nextofs
>= maxlen
)
440 len
= IMediaSample_GetSize(buf
);
441 if (This
->nextofs
+ len
> maxlen
)
442 len
= maxlen
- This
->nextofs
;
444 tStart
= MEDIATIME_FROM_BYTES(This
->nextofs
);
445 tStop
= tStart
+ MEDIATIME_FROM_BYTES(len
);
446 IMediaSample_SetTime(buf
, &tStart
, &tStop
);
448 hr
= IAsyncReader_Request(This
->pInputPin
.pReader
, buf
, 0);
450 IMediaSample_Release(buf
);
453 This
->nextofs
+= len
;
454 hr
= IAsyncReader_WaitForNext(This
->pInputPin
.pReader
, -1, &buf
, &user
);
455 if (FAILED(hr
) || !buf
) {
457 IMediaSample_Release(buf
);
461 IMediaSample_GetPointer(buf
, &data
);
462 gstbuf
= gst_app_buffer_new(data
, IMediaSample_GetActualDataLength(buf
), release_sample
, buf
);
464 IMediaSample_Release(buf
);
467 gstbuf
->duration
= gstbuf
->timestamp
= -1;
468 ret
= gst_pad_push(This
->my_src
, gstbuf
);
472 ERR("Sending returned: %i\n", ret
);
473 if (ret
== GST_FLOW_ERROR
)
475 else if (ret
== GST_FLOW_WRONG_STATE
)
476 hr
= VFW_E_WRONG_STATE
;
477 else if (ret
== GST_FLOW_RESEND
)
483 gst_pad_push_event(This
->my_src
, gst_event_new_eos());
485 TRACE("Almost stopping.. %08x\n", hr
);
487 IAsyncReader_WaitForNext(This
->pInputPin
.pReader
, 0, &buf
, &user
);
489 IMediaSample_Release(buf
);
492 TRACE("Stopping.. %08x\n", hr
);
496 static HRESULT WINAPI
GST_OutPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
) {
497 GSTOutPin
*pin
= (GSTOutPin
*)iface
;
498 FIXME("stub %p\n", pin
);
502 static GstFlowReturn
got_data_sink(GstPad
*pad
, GstBuffer
*buf
) {
503 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
504 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pin
.pinInfo
.pFilter
;
505 IMediaSample
*sample
;
507 BOOL freeSamp
= FALSE
;
510 gst_buffer_unref(buf
);
511 FIXME("Triggering %p %p\n", pad
, pin
->caps_event
);
512 SetEvent(pin
->caps_event
);
513 return GST_FLOW_NOT_LINKED
;
516 if (GST_IS_APP_BUFFER(buf
)) {
517 sample
= GST_APP_BUFFER(buf
)->priv
;
518 TRACE("Pushing buffer\n");
519 } else if (buf
->parent
&& GST_IS_APP_BUFFER(buf
->parent
)) {
520 sample
= GST_APP_BUFFER(buf
->parent
)->priv
;
521 TRACE("Pushing sub-buffer\n");
524 hr
= BaseOutputPinImpl_GetDeliveryBuffer(&pin
->pin
, &sample
, NULL
, NULL
, 0);
526 if (hr
== VFW_E_NOT_CONNECTED
) {
527 gst_buffer_unref(buf
);
528 return GST_FLOW_NOT_LINKED
;
531 gst_buffer_unref(buf
);
532 ERR("Didn't get a GST_APP_BUFFER, and could not get a delivery buffer (%x), returning GST_FLOW_WRONG_STATE\n", hr
);
533 return GST_FLOW_WRONG_STATE
;
535 FIXME("Did not get a GST_APP_BUFFER, creating a sample\n");
536 IMediaSample_SetActualDataLength(sample
, GST_BUFFER_SIZE(buf
));
537 IMediaSample_GetPointer(sample
, &ptr
);
538 memcpy(ptr
, GST_BUFFER_DATA(buf
), GST_BUFFER_SIZE(buf
));
541 if (GST_BUFFER_TIMESTAMP_IS_VALID(buf
)) {
542 REFERENCE_TIME rtStart
= gst_segment_to_running_time(pin
->segment
, GST_FORMAT_TIME
, buf
->timestamp
);
546 if (GST_BUFFER_DURATION_IS_VALID(buf
)) {
547 REFERENCE_TIME tStart
= buf
->timestamp
/ 100;
548 REFERENCE_TIME tStop
= (buf
->timestamp
+ buf
->duration
) / 100;
549 REFERENCE_TIME rtStop
;
550 rtStop
= gst_segment_to_running_time(pin
->segment
, GST_FORMAT_TIME
, buf
->timestamp
+ buf
->duration
);
553 TRACE("Current time on %p: %i to %i ms\n", pin
, (int)(rtStart
/ 10000), (int)(rtStop
/ 10000));
554 IMediaSample_SetTime(sample
, &rtStart
, rtStop
>= 0 ? &rtStop
: NULL
);
555 IMediaSample_SetMediaTime(sample
, &tStart
, &tStop
);
557 IMediaSample_SetTime(sample
, rtStart
>= 0 ? &rtStart
: NULL
, NULL
);
558 IMediaSample_SetMediaTime(sample
, NULL
, NULL
);
561 IMediaSample_SetTime(sample
, NULL
, NULL
);
562 IMediaSample_SetMediaTime(sample
, NULL
, NULL
);
565 IMediaSample_SetDiscontinuity(sample
, GST_BUFFER_FLAG_IS_SET(buf
, GST_BUFFER_FLAG_DISCONT
));
566 IMediaSample_SetPreroll(sample
, GST_BUFFER_FLAG_IS_SET(buf
, GST_BUFFER_FLAG_PREROLL
));
567 IMediaSample_SetSyncPoint(sample
, !GST_BUFFER_FLAG_IS_SET(buf
, GST_BUFFER_FLAG_DELTA_UNIT
));
569 if (!pin
->pin
.pin
.pConnectedTo
)
570 hr
= VFW_E_NOT_CONNECTED
;
572 hr
= IMemInputPin_Receive(pin
->pin
.pMemInputPin
, sample
);
573 TRACE("sending sample: %08x\n", hr
);
574 gst_buffer_unref(buf
);
576 IMediaSample_Release(sample
);
577 if (hr
== VFW_E_NOT_CONNECTED
)
578 return GST_FLOW_NOT_LINKED
;
580 return GST_FLOW_WRONG_STATE
;
582 return GST_FLOW_RESEND
;
586 static GstFlowReturn
request_buffer_sink(GstPad
*pad
, guint64 ofs
, guint size
, GstCaps
*caps
, GstBuffer
**buf
) {
587 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
588 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pin
.pinInfo
.pFilter
;
589 IMediaSample
*sample
;
593 TRACE("Requesting buffer\n");
596 ret
= setcaps_sink(pad
, caps
);
598 return GST_FLOW_NOT_NEGOTIATED
;
599 *buf
= gst_buffer_new_and_alloc(size
);
603 if (caps
&& caps
!= GST_PAD_CAPS(pad
))
604 if (!setcaps_sink(pad
, caps
))
605 return GST_FLOW_NOT_NEGOTIATED
;
607 hr
= BaseOutputPinImpl_GetDeliveryBuffer(&pin
->pin
, &sample
, NULL
, NULL
, 0);
608 if (hr
== VFW_E_NOT_CONNECTED
)
609 return GST_FLOW_NOT_LINKED
;
611 ERR("Could not get output buffer: %08x\n", hr
);
613 return GST_FLOW_WRONG_STATE
;
615 IMediaSample_SetActualDataLength(sample
, size
);
616 IMediaSample_GetPointer(sample
, &ptr
);
617 *buf
= gst_app_buffer_new(ptr
, size
, release_sample
, sample
);
619 IMediaSample_Release(sample
);
620 ERR("Out of memory\n");
621 return GST_FLOW_ERROR
;
623 gst_buffer_set_caps(*buf
, caps
);
627 static GstFlowReturn
request_buffer_src(GstPad
*pad
, guint64 ofs
, guint len
, GstBuffer
**buf
) {
628 GSTImpl
*This
= gst_pad_get_element_private(pad
);
632 TRACE("Requesting %s %u\n", wine_dbgstr_longlong(ofs
), len
);
633 if (ofs
== (guint64
)-1)
634 ofs
= This
->nextpullofs
;
635 if (ofs
>= This
->filesize
) {
636 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs
), len
);
637 return GST_FLOW_UNEXPECTED
;
639 if (len
+ ofs
> This
->filesize
)
640 len
= This
->filesize
- ofs
;
641 This
->nextpullofs
= ofs
+ len
;
643 ret
= gst_pad_alloc_buffer(This
->my_src
, ofs
, len
, NULL
, buf
);
646 hr
= IAsyncReader_SyncRead(This
->pInputPin
.pReader
, ofs
, len
, GST_BUFFER_DATA(*buf
));
648 ERR("Returned %08x\n", hr
);
649 return GST_FLOW_ERROR
;
655 static DWORD CALLBACK
push_data_init(LPVOID iface
) {
656 GSTImpl
*This
= iface
;
659 TRACE("Starting..\n");
662 GstFlowReturn ret
= request_buffer_src(This
->my_src
, ofs
, 4096, &buf
);
664 ERR("Obtaining buffer returned: %i\n", ret
);
667 ret
= gst_pad_push(This
->my_src
, buf
);
670 TRACE("Sending returned: %i\n", ret
);
674 TRACE("Stopping..\n");
678 static void removed_decoded_pad(GstElement
*bin
, GstPad
*pad
, GSTImpl
*This
) {
682 EnterCriticalSection(&This
->filter
.csFilter
);
683 for (x
= 0; x
< This
->cStreams
; ++x
) {
684 if (This
->ppPins
[x
]->their_src
== pad
)
687 if (x
== This
->cStreams
)
689 pin
= This
->ppPins
[x
];
690 gst_pad_unlink(pin
->their_src
, pin
->my_sink
);
691 gst_object_unref(pin
->their_src
);
692 pin
->their_src
= NULL
;
694 TRACE("Removed %i/%i\n", x
, This
->cStreams
);
695 LeaveCriticalSection(&This
->filter
.csFilter
);
698 static void init_new_decoded_pad(GstElement
*bin
, GstPad
*pad
, gboolean last
, GSTImpl
*This
) {
701 const char *typename
;
703 AM_MEDIA_TYPE amt
= { };
709 int isvid
= 0, isaud
= 0;
711 piOutput
.dir
= PINDIR_OUTPUT
;
712 piOutput
.pFilter
= (IBaseFilter
*)This
;
713 name
= gst_pad_get_name(pad
);
714 MultiByteToWideChar(CP_UNIXCP
, 0, name
, -1, piOutput
.achName
, sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]) - 1);
715 TRACE("Name: %s\n", name
);
717 piOutput
.achName
[sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]) - 1] = 0;
719 caps
= gst_pad_get_caps_reffed(pad
);
720 arg
= gst_caps_get_structure(caps
, 0);
721 typename
= gst_structure_get_name(arg
);
723 mypad
= gst_pad_new(NULL
, GST_PAD_SINK
);
724 gst_pad_set_chain_function(mypad
, got_data_sink
);
725 gst_pad_set_event_function(mypad
, event_sink
);
726 gst_pad_set_bufferalloc_function(mypad
, request_buffer_sink
);
727 gst_pad_set_acceptcaps_function(mypad
, accept_caps_sink
);
728 gst_pad_set_acceptcaps_function(mypad
, setcaps_sink
);
730 if (!strcmp(typename
, "audio/x-raw-int") ||
731 !strcmp(typename
, "audio/x-raw-float")) {
733 } else if (!strcmp(typename
, "video/x-raw-rgb")
734 || !strcmp(typename
, "video/x-raw-yuv")) {
737 FIXME("Unknown type \'%s\'\n", typename
);
740 GST_PAD_CAPS(mypad
) = GST_CAPS_ANY
;
741 hr
= GST_AddPin(This
, &piOutput
, &amt
);
746 pin
= This
->ppPins
[This
->cStreams
- 1];
747 gst_pad_set_element_private(mypad
, pin
);
748 pin
->my_sink
= mypad
;
752 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
753 ret
= gst_pad_link(pad
, mypad
);
754 gst_pad_activate_push(mypad
, 1);
755 FIXME("Linking: %i\n", ret
);
757 pin
->their_src
= pad
;
758 gst_object_ref(pin
->their_src
);
762 static void existing_new_pad(GstElement
*bin
, GstPad
*pad
, gboolean last
, GSTImpl
*This
) {
765 if (gst_pad_is_linked(pad
))
768 /* Still holding our own lock */
770 init_new_decoded_pad(bin
, pad
, last
, This
);
774 EnterCriticalSection(&This
->filter
.csFilter
);
775 for (x
= 0; x
< This
->cStreams
; ++x
) {
776 GSTOutPin
*pin
= This
->ppPins
[x
];
777 if (!pin
->their_src
) {
778 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
779 if (gst_pad_link(pad
, pin
->my_sink
) >= 0) {
780 pin
->their_src
= pad
;
781 gst_object_ref(pin
->their_src
);
783 LeaveCriticalSection(&This
->filter
.csFilter
);
788 init_new_decoded_pad(bin
, pad
, last
, This
);
789 LeaveCriticalSection(&This
->filter
.csFilter
);
792 static gboolean
check_get_range(GstPad
*pad
) {
796 static gboolean
query_function(GstPad
*pad
, GstQuery
*query
) {
797 GSTImpl
*This
= gst_pad_get_element_private(pad
);
802 switch (GST_QUERY_TYPE(query
)) {
803 case GST_QUERY_DURATION
:
804 gst_query_parse_duration (query
, &format
, NULL
);
805 if (format
== GST_FORMAT_PERCENT
) {
806 gst_query_set_duration (query
, GST_FORMAT_PERCENT
, GST_FORMAT_PERCENT_MAX
);
809 ret
= gst_pad_query_convert (pad
, GST_FORMAT_BYTES
, This
->filesize
, &format
, &duration
);
810 gst_query_set_duration(query
, format
, duration
);
812 case GST_QUERY_SEEKING
:
813 gst_query_parse_seeking (query
, &format
, NULL
, NULL
, NULL
);
814 TRACE("Seeking %i %i\n", format
, GST_FORMAT_BYTES
);
815 if (format
!= GST_FORMAT_BYTES
)
817 gst_query_set_seeking(query
, GST_FORMAT_BYTES
, 1, 0, This
->filesize
);
820 FIXME("Unhandled query type %i\n", GST_QUERY_TYPE(query
));
822 case GST_QUERY_CONVERT
:
827 static gboolean
activate_push(GstPad
*pad
, gboolean activate
) {
828 GSTImpl
*This
= gst_pad_get_element_private(pad
);
829 EnterCriticalSection(&This
->filter
.csFilter
);
831 TRACE("Deactivating\n");
832 if (This
->push_thread
) {
833 WaitForSingleObject(This
->push_thread
, -1);
834 CloseHandle(This
->push_thread
);
835 This
->push_thread
= NULL
;
837 if (This
->filter
.state
== State_Stopped
)
838 This
->nextofs
= This
->start
;
839 } else if (!This
->push_thread
) {
840 TRACE("Activating\n");
842 This
->push_thread
= CreateThread(NULL
, 0, push_data_init
, This
, 0, NULL
);
844 This
->push_thread
= CreateThread(NULL
, 0, push_data
, This
, 0, NULL
);
846 LeaveCriticalSection(&This
->filter
.csFilter
);
850 static void no_more_pads(GstElement
*decodebin
, GSTImpl
*This
) {
852 SetEvent(This
->event
);
856 GST_AUTOPLUG_SELECT_TRY
,
857 GST_AUTOPLUG_SELECT_EXPOSE
,
858 GST_AUTOPLUG_SELECT_SKIP
859 } GstAutoplugSelectResult
;
861 static GstAutoplugSelectResult
autoplug_blacklist(GstElement
*bin
, GstPad
*pad
, GstCaps
*caps
, GstElementFactory
*fact
, GSTImpl
*This
) {
862 const char *name
= gst_element_factory_get_longname(fact
);
864 if (strstr(name
, "Player protection")) {
865 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
866 return GST_AUTOPLUG_SELECT_SKIP
;
868 if (!strcmp(name
, "Fluendo Hardware Accelerated Video Decoder")) {
869 WARN("Disabled video acceleration since it breaks in wine\n");
870 return GST_AUTOPLUG_SELECT_SKIP
;
872 TRACE("using \"%s\"\n", name
);
873 return GST_AUTOPLUG_SELECT_TRY
;
876 static GstBusSyncReply
watch_bus(GstBus
*bus
, GstMessage
*msg
, gpointer data
) {
877 GSTImpl
*This
= data
;
879 gchar
*dbg_info
= NULL
;
880 if (GST_MESSAGE_TYPE(msg
) & GST_MESSAGE_ERROR
) {
881 gst_message_parse_error(msg
, &err
, &dbg_info
);
882 FIXME("%s: %s\n", GST_OBJECT_NAME(msg
->src
), err
->message
);
883 WARN("%s\n", dbg_info
);
884 SetEvent(This
->event
);
885 } else if (GST_MESSAGE_TYPE(msg
) & GST_MESSAGE_WARNING
) {
886 gst_message_parse_warning(msg
, &err
, &dbg_info
);
887 WARN("%s: %s\n", GST_OBJECT_NAME(msg
->src
), err
->message
);
888 WARN("%s\n", dbg_info
);
896 static void unknown_type(GstElement
*bin
, GstPad
*pad
, GstCaps
*caps
, GSTImpl
*This
) {
897 gchar
*strcaps
= gst_caps_to_string(caps
);
898 FIXME("Could not find a filter for caps: %s\n", strcaps
);
902 static HRESULT
GST_Connect(GSTInPin
*pPin
, IPin
*pConnectPin
, ALLOCATOR_PROPERTIES
*props
) {
903 GSTImpl
*This
= (GSTImpl
*)pPin
->pin
.pinInfo
.pFilter
;
906 LONGLONG avail
, duration
;
907 GstFormat format
= GST_FORMAT_TIME
;
908 GstStaticPadTemplate src_template
= GST_STATIC_PAD_TEMPLATE(
912 GST_STATIC_CAPS_ANY
);
914 TRACE("%p %p %p\n", pPin
, pConnectPin
, props
);
915 This
->props
= *props
;
916 IAsyncReader_Length(pPin
->pReader
, &This
->filesize
, &avail
);
919 This
->bus
= gst_bus_new();
920 gst_bus_set_sync_handler(This
->bus
, watch_bus
, This
);
923 This
->gstfilter
= gst_element_factory_make("decodebin2", NULL
);
924 if (!This
->gstfilter
) {
925 FIXME("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
926 8 * (int)sizeof(void*));
929 gst_element_set_bus(This
->gstfilter
, This
->bus
);
930 g_signal_connect(This
->gstfilter
, "new-decoded-pad", G_CALLBACK(existing_new_pad
), This
);
931 g_signal_connect(This
->gstfilter
, "pad-removed", G_CALLBACK(removed_decoded_pad
), This
);
932 g_signal_connect(This
->gstfilter
, "autoplug-select", G_CALLBACK(autoplug_blacklist
), This
);
933 g_signal_connect(This
->gstfilter
, "unknown-type", G_CALLBACK(unknown_type
), This
);
935 This
->my_src
= gst_pad_new_from_static_template(&src_template
, "quartz-src");
936 gst_pad_set_getrange_function(This
->my_src
, request_buffer_src
);
937 gst_pad_set_checkgetrange_function(This
->my_src
, check_get_range
);
938 gst_pad_set_query_function(This
->my_src
, query_function
);
939 gst_pad_set_activatepush_function(This
->my_src
, activate_push
);
940 gst_pad_set_event_function(This
->my_src
, event_src
);
941 gst_pad_set_element_private (This
->my_src
, This
);
942 This
->their_sink
= gst_element_get_static_pad(This
->gstfilter
, "sink");
944 g_signal_connect(This
->gstfilter
, "no-more-pads", G_CALLBACK(no_more_pads
), This
);
945 ret
= gst_pad_link(This
->my_src
, This
->their_sink
);
946 gst_object_unref(This
->their_sink
);
948 ERR("Returns: %i\n", ret
);
951 This
->start
= This
->nextofs
= This
->nextpullofs
= This
->stop
= 0;
953 /* Add initial pins */
954 This
->initial
= This
->discont
= 1;
955 ResetEvent(This
->event
);
956 gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
957 gst_pad_set_active(This
->my_src
, 1);
958 WaitForSingleObject(This
->event
, -1);
959 gst_element_get_state(This
->gstfilter
, NULL
, NULL
, -1);
962 WARN("Ret: %i\n", ret
);
964 } else if (!This
->cStreams
) {
965 FIXME("Gstreamer could not find any streams\n");
968 gst_pad_query_duration(This
->ppPins
[0]->their_src
, &format
, &duration
);
969 for (i
= 0; i
< This
->cStreams
; ++i
) {
970 This
->ppPins
[i
]->seek
.llDuration
= This
->ppPins
[i
]->seek
.llStop
= duration
/ 100;
971 if (!This
->ppPins
[i
]->seek
.llDuration
)
972 This
->ppPins
[i
]->seek
.dwCapabilities
= 0;
973 WaitForSingleObject(This
->ppPins
[i
]->caps_event
, -1);
977 *props
= This
->props
;
978 gst_element_set_state(This
->gstfilter
, GST_STATE_READY
);
979 gst_element_get_state(This
->gstfilter
, NULL
, NULL
, -1);
982 This
->nextofs
= This
->nextpullofs
= 0;
986 static inline GSTOutPin
*impl_from_IMediaSeeking( IMediaSeeking
*iface
) {
987 return (GSTOutPin
*)((char*)iface
- FIELD_OFFSET(GSTOutPin
, seek
.lpVtbl
));
990 static IPin
* WINAPI
GST_GetPin(BaseFilter
*iface
, int pos
)
992 GSTImpl
*This
= (GSTImpl
*)iface
;
993 TRACE("Asking for pos %x\n", pos
);
995 if (pos
> This
->cStreams
|| pos
< 0)
999 IPin_AddRef((IPin
*)&This
->pInputPin
);
1000 return (IPin
*)&This
->pInputPin
;
1004 IPin_AddRef((IPin
*)This
->ppPins
[pos
- 1]);
1005 return (IPin
*)This
->ppPins
[pos
- 1];
1009 static LONG WINAPI
GST_GetPinCount(BaseFilter
*iface
)
1011 GSTImpl
*This
= (GSTImpl
*)iface
;
1012 return (This
->cStreams
+ 1);
1015 static const BaseFilterFuncTable BaseFuncTable
= {
1020 IUnknown
* CALLBACK
Gstreamer_Splitter_create(IUnknown
*punkout
, HRESULT
*phr
) {
1021 IUnknown
*obj
= NULL
;
1025 if (!Gstreamer_init())
1031 This
= CoTaskMemAlloc(sizeof(*This
));
1032 obj
= (IUnknown
*)This
;
1035 *phr
= E_OUTOFMEMORY
;
1039 BaseFilter_Init(&This
->filter
, &GST_Vtbl
, &CLSID_Gstreamer_Splitter
, (DWORD_PTR
)(__FILE__
": GSTImpl.csFilter"), &BaseFuncTable
);
1042 This
->ppPins
= NULL
;
1043 This
->push_thread
= NULL
;
1044 This
->event
= CreateEventW(NULL
, 0, 0, NULL
);
1047 piInput
= &This
->pInputPin
.pin
.pinInfo
;
1048 piInput
->dir
= PINDIR_INPUT
;
1049 piInput
->pFilter
= (IBaseFilter
*)This
;
1050 lstrcpynW(piInput
->achName
, wcsInputPinName
, sizeof(piInput
->achName
) / sizeof(piInput
->achName
[0]));
1051 This
->pInputPin
.pin
.lpVtbl
= &GST_InputPin_Vtbl
;
1052 This
->pInputPin
.pin
.refCount
= 1;
1053 This
->pInputPin
.pin
.pConnectedTo
= NULL
;
1054 This
->pInputPin
.pin
.pCritSec
= &This
->filter
.csFilter
;
1055 ZeroMemory(&This
->pInputPin
.pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1060 static void GST_Destroy(GSTImpl
*This
) {
1061 IPin
*connected
= NULL
;
1064 TRACE("Destroying\n");
1066 CloseHandle(This
->event
);
1068 /* Don't need to clean up output pins, disconnecting input pin will do that */
1069 IPin_ConnectedTo((IPin
*)&This
->pInputPin
, &connected
);
1071 assert(IPin_Disconnect(connected
) == S_OK
);
1072 IPin_Release(connected
);
1073 assert(IPin_Disconnect((IPin
*)&This
->pInputPin
) == S_OK
);
1075 pinref
= IPin_Release((IPin
*)&This
->pInputPin
);
1077 /* Valgrind could find this, if I kill it here */
1078 ERR("pinref should be null, is %u, destroying anyway\n", pinref
);
1079 assert((LONG
)pinref
> 0);
1082 pinref
= IPin_Release((IPin
*)&This
->pInputPin
);
1085 gst_bus_set_sync_handler(This
->bus
, NULL
, NULL
);
1086 gst_object_unref(This
->bus
);
1088 CoTaskMemFree(This
);
1091 static HRESULT WINAPI
GST_QueryInterface(IBaseFilter
*iface
, REFIID riid
, LPVOID
*ppv
) {
1092 GSTImpl
*This
= (GSTImpl
*)iface
;
1093 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1097 if (IsEqualIID(riid
, &IID_IUnknown
))
1099 else if (IsEqualIID(riid
, &IID_IPersist
))
1101 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
1103 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
1107 IUnknown_AddRef((IUnknown
*)(*ppv
));
1111 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
) &&
1112 !IsEqualIID(riid
, &IID_IAMFilterMiscFlags
))
1113 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1115 return E_NOINTERFACE
;
1118 static ULONG WINAPI
GST_Release(IBaseFilter
*iface
) {
1119 GSTImpl
*This
= (GSTImpl
*)iface
;
1120 ULONG refCount
= BaseFilterImpl_Release(iface
);
1122 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
1130 static HRESULT WINAPI
GST_Stop(IBaseFilter
*iface
) {
1131 GSTImpl
*This
= (GSTImpl
*)iface
;
1135 if (This
->gstfilter
) {
1136 IAsyncReader_BeginFlush(This
->pInputPin
.pReader
);
1137 gst_element_set_state(This
->gstfilter
, GST_STATE_READY
);
1138 IAsyncReader_EndFlush(This
->pInputPin
.pReader
);
1143 static HRESULT WINAPI
GST_Pause(IBaseFilter
*iface
) {
1145 GSTImpl
*This
= (GSTImpl
*)iface
;
1147 GstStateChangeReturn ret
;
1150 if (!This
->gstfilter
)
1151 return VFW_E_NOT_CONNECTED
;
1153 gst_element_get_state(This
->gstfilter
, &now
, NULL
, -1);
1154 if (now
== GST_STATE_PAUSED
)
1156 if (now
!= GST_STATE_PLAYING
)
1157 hr
= IBaseFilter_Run(iface
, -1);
1160 ret
= gst_element_set_state(This
->gstfilter
, GST_STATE_PAUSED
);
1161 if (ret
== GST_STATE_CHANGE_ASYNC
)
1166 static HRESULT WINAPI
GST_Run(IBaseFilter
*iface
, REFERENCE_TIME tStart
) {
1168 GSTImpl
*This
= (GSTImpl
*)iface
;
1171 HRESULT hr_any
= VFW_E_NOT_CONNECTED
;
1173 TRACE("(%s)\n", wine_dbgstr_longlong(tStart
));
1175 if (!This
->gstfilter
)
1176 return VFW_E_NOT_CONNECTED
;
1178 gst_element_get_state(This
->gstfilter
, &now
, NULL
, -1);
1179 if (now
== GST_STATE_PLAYING
)
1181 if (now
== GST_STATE_PAUSED
) {
1182 GstStateChangeReturn ret
;
1183 ret
= gst_element_set_state(This
->gstfilter
, GST_STATE_PAUSED
);
1184 if (ret
== GST_STATE_CHANGE_ASYNC
)
1189 EnterCriticalSection(&This
->filter
.csFilter
);
1190 gst_pad_set_blocked(This
->my_src
, 0);
1191 gst_pad_set_blocked(This
->their_sink
, 0);
1192 gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
1193 This
->filter
.rtStreamStart
= tStart
;
1195 for (i
= 0; i
< This
->cStreams
; i
++) {
1196 hr
= BaseOutputPinImpl_Active((BaseOutputPin
*)This
->ppPins
[i
]);
1197 if (SUCCEEDED(hr
)) {
1198 gst_pad_set_blocked(This
->ppPins
[i
]->my_sink
, 0);
1199 if (This
->ppPins
[i
]->their_src
)
1200 gst_pad_set_blocked(This
->ppPins
[i
]->their_src
, 0);
1206 gst_pad_set_active(This
->my_src
, 1);
1207 LeaveCriticalSection(&This
->filter
.csFilter
);
1212 static HRESULT WINAPI
GST_GetState(IBaseFilter
*iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
) {
1213 GSTImpl
*This
= (GSTImpl
*)iface
;
1215 GstState now
, pending
;
1216 GstStateChangeReturn ret
;
1218 TRACE("(%d, %p)\n", dwMilliSecsTimeout
, pState
);
1220 if (!This
->gstfilter
) {
1221 *pState
= State_Stopped
;
1225 ret
= gst_element_get_state(This
->gstfilter
, &now
, &pending
, dwMilliSecsTimeout
== INFINITE
? -1 : dwMilliSecsTimeout
* 1000);
1227 if (ret
== GST_STATE_CHANGE_ASYNC
)
1228 hr
= VFW_S_STATE_INTERMEDIATE
;
1233 case GST_STATE_PAUSED
: *pState
= State_Paused
; return hr
;
1234 case GST_STATE_PLAYING
: *pState
= State_Running
; return hr
;
1235 default: *pState
= State_Stopped
; return hr
;
1239 static HRESULT WINAPI
GST_FindPin(IBaseFilter
*iface
, LPCWSTR Id
, IPin
**ppPin
) {
1240 FIXME("(%p)->(%s,%p) stub\n", iface
, debugstr_w(Id
), ppPin
);
1244 static const IBaseFilterVtbl GST_Vtbl
= {
1246 BaseFilterImpl_AddRef
,
1248 BaseFilterImpl_GetClassID
,
1253 BaseFilterImpl_SetSyncSource
,
1254 BaseFilterImpl_GetSyncSource
,
1255 BaseFilterImpl_EnumPins
,
1257 BaseFilterImpl_QueryFilterInfo
,
1258 BaseFilterImpl_JoinFilterGraph
,
1259 BaseFilterImpl_QueryVendorInfo
1262 static HRESULT WINAPI
GST_ChangeCurrent(IMediaSeeking
*iface
) {
1266 static HRESULT WINAPI
GST_ChangeStop(IMediaSeeking
*iface
) {
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 HRESULT WINAPI
GST_Seeking_GetCurrentPosition(IMediaSeeking
*iface
, REFERENCE_TIME
*pos
) {
1294 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1295 GstFormat format
= GST_FORMAT_TIME
;
1300 if (!This
->their_src
) {
1301 *pos
= This
->seek
.llCurrent
;
1302 TRACE("Cached value\n");
1303 if (This
->seek
.llDuration
)
1309 if (!gst_pad_query_position(This
->their_src
, &format
, pos
)) {
1310 WARN("Could not query position\n");
1317 static GstSeekType
type_from_flags(DWORD flags
) {
1318 switch (flags
& AM_SEEKING_PositioningBitsMask
) {
1319 case AM_SEEKING_NoPositioning
: return GST_SEEK_TYPE_NONE
;
1320 case AM_SEEKING_AbsolutePositioning
: return GST_SEEK_TYPE_SET
;
1321 case AM_SEEKING_RelativePositioning
: return GST_SEEK_TYPE_CUR
;
1322 case AM_SEEKING_IncrementalPositioning
: return GST_SEEK_TYPE_END
;
1324 return GST_SEEK_TYPE_NONE
;
1328 static HRESULT WINAPI
GST_Seeking_SetPositions(IMediaSeeking
*iface
, REFERENCE_TIME
*pCur
, DWORD curflags
, REFERENCE_TIME
*pStop
, DWORD stopflags
) {
1330 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1332 GstSeekType curtype
, stoptype
;
1335 if (!This
->seek
.llDuration
)
1338 hr
= SourceSeekingImpl_SetPositions(iface
, pCur
, curflags
, pStop
, stopflags
);
1339 if (!This
->their_src
)
1340 return This
->seek
.llDuration
? hr
: E_NOTIMPL
;
1342 if (curflags
& AM_SEEKING_SeekToKeyFrame
)
1343 f
|= GST_SEEK_FLAG_KEY_UNIT
;
1344 if (curflags
& AM_SEEKING_Segment
)
1345 f
|= GST_SEEK_FLAG_SEGMENT
;
1346 if (!(curflags
& AM_SEEKING_NoFlush
))
1347 f
|= GST_SEEK_FLAG_FLUSH
;
1349 curtype
= type_from_flags(curflags
);
1350 stoptype
= type_from_flags(stopflags
);
1351 e
= gst_event_new_seek(This
->seek
.dRate
, GST_FORMAT_TIME
, f
, curtype
, pCur
? *pCur
* 100 : -1, stoptype
, pStop
? *pStop
* 100 : -1);
1352 if (gst_pad_push_event(This
->my_sink
, e
))
1358 static const IMediaSeekingVtbl GST_Seeking_Vtbl
=
1360 GST_Seeking_QueryInterface
,
1362 GST_Seeking_Release
,
1363 SourceSeekingImpl_GetCapabilities
,
1364 SourceSeekingImpl_CheckCapabilities
,
1365 SourceSeekingImpl_IsFormatSupported
,
1366 SourceSeekingImpl_QueryPreferredFormat
,
1367 SourceSeekingImpl_GetTimeFormat
,
1368 SourceSeekingImpl_IsUsingTimeFormat
,
1369 SourceSeekingImpl_SetTimeFormat
,
1370 SourceSeekingImpl_GetDuration
,
1371 SourceSeekingImpl_GetStopPosition
,
1372 GST_Seeking_GetCurrentPosition
,
1373 SourceSeekingImpl_ConvertTimeFormat
,
1374 GST_Seeking_SetPositions
,
1375 SourceSeekingImpl_GetPositions
,
1376 SourceSeekingImpl_GetAvailable
,
1377 SourceSeekingImpl_SetRate
,
1378 SourceSeekingImpl_GetRate
,
1379 SourceSeekingImpl_GetPreroll
1382 static HRESULT WINAPI
GST_QualityControl_Notify(IQualityControl
*iface
, IBaseFilter
*sender
, Quality qm
) {
1383 QualityControlImpl
*This
= (QualityControlImpl
*)iface
;
1384 GSTOutPin
*pin
= (GSTOutPin
*)This
->self
;
1385 gst_pad_push_event(pin
->my_sink
, gst_event_new_qos(1000./qm
.Proportion
, qm
.Late
*100, qm
.TimeStamp
*100));
1389 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl
= {
1390 QualityControlImpl_QueryInterface
,
1391 QualityControlImpl_AddRef
,
1392 QualityControlImpl_Release
,
1393 GST_QualityControl_Notify
,
1394 QualityControlImpl_SetSink
1397 static HRESULT WINAPI
GSTOutPin_QueryInterface(IPin
*iface
, REFIID riid
, void **ppv
) {
1398 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1400 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1404 if (IsEqualIID(riid
, &IID_IUnknown
))
1406 else if (IsEqualIID(riid
, &IID_IPin
))
1408 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1410 else if (IsEqualIID(riid
, &IID_IQualityControl
))
1411 *ppv
= &This
->qcimpl
;
1414 IUnknown_AddRef((IUnknown
*)(*ppv
));
1417 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1418 return E_NOINTERFACE
;
1421 static ULONG WINAPI
GSTOutPin_Release(IPin
*iface
) {
1422 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1423 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
1424 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
1427 if (This
->their_src
)
1428 gst_pad_unlink(This
->their_src
, This
->my_sink
);
1429 gst_object_unref(This
->my_sink
);
1430 CloseHandle(This
->caps_event
);
1431 DeleteMediaType(This
->pmt
);
1432 FreeMediaType(&This
->pin
.pin
.mtCurrent
);
1433 gst_segment_free(This
->segment
);
1434 CoTaskMemFree(This
);
1440 static HRESULT WINAPI
GSTOutPin_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
1442 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1445 return E_INVALIDARG
;
1447 return VFW_S_NO_MORE_ITEMS
;
1448 CopyMediaType(pmt
, This
->pmt
);
1452 static HRESULT WINAPI
GSTOutPin_DecideBufferSize(BaseOutputPin
*iface
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
1458 static HRESULT WINAPI
GSTOutPin_DecideAllocator(BaseOutputPin
*iface
, IMemInputPin
*pPin
, IMemAllocator
**pAlloc
)
1461 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1462 GSTImpl
*GSTfilter
= (GSTImpl
*)This
->pin
.pin
.pinInfo
.pFilter
;
1465 if (GSTfilter
->pInputPin
.pAlloc
)
1466 hr
= IMemInputPin_NotifyAllocator(pPin
, GSTfilter
->pInputPin
.pAlloc
, FALSE
);
1468 hr
= VFW_E_NO_ALLOCATOR
;
1473 static HRESULT WINAPI
GSTOutPin_BreakConnect(BaseOutputPin
*This
)
1477 TRACE("(%p)->()\n", This
);
1479 EnterCriticalSection(This
->pin
.pCritSec
);
1480 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1481 hr
= VFW_E_NOT_CONNECTED
;
1484 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1485 IPin_Disconnect((IPin
*)This
);
1487 LeaveCriticalSection(This
->pin
.pCritSec
);
1492 static const IPinVtbl GST_OutputPin_Vtbl
= {
1493 GSTOutPin_QueryInterface
,
1496 BaseOutputPinImpl_Connect
,
1497 BaseOutputPinImpl_ReceiveConnection
,
1498 BaseOutputPinImpl_Disconnect
,
1499 BasePinImpl_ConnectedTo
,
1500 BasePinImpl_ConnectionMediaType
,
1501 BasePinImpl_QueryPinInfo
,
1502 BasePinImpl_QueryDirection
,
1503 BasePinImpl_QueryId
,
1504 GST_OutPin_QueryAccept
,
1505 BasePinImpl_EnumMediaTypes
,
1506 BasePinImpl_QueryInternalConnections
,
1507 BaseOutputPinImpl_EndOfStream
,
1508 BaseOutputPinImpl_BeginFlush
,
1509 BaseOutputPinImpl_EndFlush
,
1510 BasePinImpl_NewSegment
1513 static const BasePinFuncTable output_BaseFuncTable
= {
1515 BaseOutputPinImpl_AttemptConnection
,
1516 BasePinImpl_GetMediaTypeVersion
,
1517 GSTOutPin_GetMediaType
1520 static const BaseOutputPinFuncTable output_BaseOutputFuncTable
= {
1521 GSTOutPin_DecideBufferSize
,
1522 GSTOutPin_DecideAllocator
,
1523 GSTOutPin_BreakConnect
1526 static HRESULT
GST_AddPin(GSTImpl
*This
, const PIN_INFO
*piOutput
, const AM_MEDIA_TYPE
*amt
) {
1528 This
->ppPins
= CoTaskMemRealloc(This
->ppPins
, (This
->cStreams
+ 1) * sizeof(IPin
*));
1530 hr
= BaseOutputPin_Construct(&GST_OutputPin_Vtbl
, sizeof(GSTOutPin
), piOutput
, &output_BaseFuncTable
, &output_BaseOutputFuncTable
, &This
->filter
.csFilter
, (IPin
**)(This
->ppPins
+ This
->cStreams
));
1531 if (SUCCEEDED(hr
)) {
1532 GSTOutPin
*pin
= This
->ppPins
[This
->cStreams
];
1533 pin
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
1534 CopyMediaType(pin
->pmt
, amt
);
1535 pin
->pin
.pin
.pinInfo
.pFilter
= (LPVOID
)This
;
1536 pin
->caps_event
= CreateEventW(NULL
, 0, 0, NULL
);
1537 pin
->segment
= gst_segment_new();
1539 QualityControlImpl_init(&pin
->qcimpl
, NULL
, (IBaseFilter
*)pin
);
1540 pin
->qcimpl
.lpVtbl
= &GSTOutPin_QualityControl_Vtbl
;
1541 SourceSeeking_Init(&pin
->seek
, &GST_Seeking_Vtbl
, GST_ChangeStop
, GST_ChangeCurrent
, GST_ChangeRate
, &This
->filter
.csFilter
);
1542 BaseFilterImpl_IncrementPinVersion((BaseFilter
*)This
);
1544 ERR("Failed with error %x\n", hr
);
1548 static HRESULT
GST_RemoveOutputPins(GSTImpl
*This
) {
1551 GSTOutPin
**ppOldPins
= This
->ppPins
;
1552 TRACE("(%p)\n", This
);
1554 if (!This
->gstfilter
)
1556 gst_element_set_bus(This
->gstfilter
, NULL
);
1557 gst_element_set_state(This
->gstfilter
, GST_STATE_NULL
);
1558 gst_pad_unlink(This
->my_src
, This
->their_sink
);
1559 This
->my_src
= This
->their_sink
= NULL
;
1561 for (i
= 0; i
< This
->cStreams
; i
++) {
1562 hr
= BaseOutputPinImpl_BreakConnect(&ppOldPins
[i
]->pin
);
1563 TRACE("Disconnect: %08x\n", hr
);
1564 IPin_Release((IPin
*)ppOldPins
[i
]);
1567 This
->ppPins
= NULL
;
1568 gst_object_unref(This
->gstfilter
);
1569 This
->gstfilter
= NULL
;
1570 BaseFilterImpl_IncrementPinVersion((BaseFilter
*)This
);
1571 CoTaskMemFree(ppOldPins
);
1575 static ULONG WINAPI
GSTInPin_Release(IPin
*iface
) {
1576 GSTInPin
*This
= (GSTInPin
*)iface
;
1577 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
1579 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
1581 FreeMediaType(&This
->pin
.mtCurrent
);
1583 IMemAllocator_Release(This
->pAlloc
);
1584 This
->pAlloc
= NULL
;
1585 This
->pin
.lpVtbl
= NULL
;
1591 static HRESULT WINAPI
GSTInPin_ReceiveConnection(IPin
*iface
, IPin
*pReceivePin
, const AM_MEDIA_TYPE
*pmt
) {
1592 PIN_DIRECTION pindirReceive
;
1594 GSTInPin
*This
= (GSTInPin
*)iface
;
1596 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1597 dump_AM_MEDIA_TYPE(pmt
);
1599 EnterCriticalSection(This
->pin
.pCritSec
);
1600 if (!This
->pin
.pConnectedTo
) {
1601 ALLOCATOR_PROPERTIES props
;
1604 props
.cbBuffer
= 16384;
1608 if (SUCCEEDED(hr
) && IPin_QueryAccept(iface
, pmt
) != S_OK
)
1609 hr
= VFW_E_TYPE_NOT_ACCEPTED
;
1610 if (SUCCEEDED(hr
)) {
1611 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1612 if (pindirReceive
!= PINDIR_OUTPUT
) {
1613 ERR("Can't connect from non-output pin\n");
1614 hr
= VFW_E_INVALID_DIRECTION
;
1618 This
->pReader
= NULL
;
1619 This
->pAlloc
= NULL
;
1621 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1623 hr
= GST_Connect(This
, pReceivePin
, &props
);
1625 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1626 if (SUCCEEDED(hr
)) {
1627 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1628 This
->pin
.pConnectedTo
= pReceivePin
;
1629 IPin_AddRef(pReceivePin
);
1630 hr
= IMemAllocator_Commit(This
->pAlloc
);
1632 GST_RemoveOutputPins((GSTImpl
*)This
->pin
.pinInfo
.pFilter
);
1634 IAsyncReader_Release(This
->pReader
);
1635 This
->pReader
= NULL
;
1637 IMemAllocator_Release(This
->pAlloc
);
1638 This
->pAlloc
= NULL
;
1640 TRACE("Size: %i\n", props
.cbBuffer
);
1642 hr
= VFW_E_ALREADY_CONNECTED
;
1643 LeaveCriticalSection(This
->pin
.pCritSec
);
1647 static HRESULT WINAPI
GSTInPin_Disconnect(IPin
*iface
) {
1649 GSTInPin
*This
= (GSTInPin
*)iface
;
1653 hr
= IBaseFilter_GetState(This
->pin
.pinInfo
.pFilter
, INFINITE
, &state
);
1654 EnterCriticalSection(This
->pin
.pCritSec
);
1655 if (This
->pin
.pConnectedTo
) {
1656 GSTImpl
*Parser
= (GSTImpl
*)This
->pin
.pinInfo
.pFilter
;
1658 if (SUCCEEDED(hr
) && state
== State_Stopped
) {
1659 IMemAllocator_Decommit(This
->pAlloc
);
1660 IPin_Disconnect(This
->pin
.pConnectedTo
);
1661 This
->pin
.pConnectedTo
= NULL
;
1662 hr
= GST_RemoveOutputPins(Parser
);
1664 hr
= VFW_E_NOT_STOPPED
;
1667 LeaveCriticalSection(This
->pin
.pCritSec
);
1671 static HRESULT WINAPI
GSTInPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
) {
1672 GSTInPin
*This
= (GSTInPin
*)iface
;
1674 TRACE("(%p)->(%p)\n", This
, pmt
);
1675 dump_AM_MEDIA_TYPE(pmt
);
1677 if (IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
1682 static HRESULT WINAPI
GSTInPin_EndOfStream(IPin
*iface
) {
1683 GSTInPin
*pin
= (GSTInPin
*)iface
;
1684 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1686 FIXME("Propagate message on %p\n", This
);
1690 static HRESULT WINAPI
GSTInPin_BeginFlush(IPin
*iface
) {
1691 GSTInPin
*pin
= (GSTInPin
*)iface
;
1692 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1694 FIXME("Propagate message on %p\n", This
);
1698 static HRESULT WINAPI
GSTInPin_EndFlush(IPin
*iface
) {
1699 GSTInPin
*pin
= (GSTInPin
*)iface
;
1700 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1702 FIXME("Propagate message on %p\n", This
);
1706 static HRESULT WINAPI
GSTInPin_NewSegment(IPin
*iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
) {
1707 GSTInPin
*pin
= (GSTInPin
*)iface
;
1708 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1710 BasePinImpl_NewSegment(iface
, tStart
, tStop
, dRate
);
1711 FIXME("Propagate message on %p\n", This
);
1715 static HRESULT WINAPI
GSTInPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1717 GSTInPin
*This
= (GSTInPin
*)iface
;
1719 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
1723 if (IsEqualIID(riid
, &IID_IUnknown
))
1725 else if (IsEqualIID(riid
, &IID_IPin
))
1727 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1729 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
1734 IUnknown_AddRef((IUnknown
*)(*ppv
));
1738 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1740 return E_NOINTERFACE
;
1743 static HRESULT WINAPI
GSTInPin_EnumMediaTypes(IPin
*iface
, IEnumMediaTypes
**ppEnum
)
1745 BasePin
*This
= (BasePin
*)iface
;
1747 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
1749 return EnumMediaTypes_Construct(This
, BasePinImpl_GetMediaType
, BasePinImpl_GetMediaTypeVersion
, ppEnum
);
1752 static const IPinVtbl GST_InputPin_Vtbl
= {
1753 GSTInPin_QueryInterface
,
1756 BaseInputPinImpl_Connect
,
1757 GSTInPin_ReceiveConnection
,
1758 GSTInPin_Disconnect
,
1759 BasePinImpl_ConnectedTo
,
1760 BasePinImpl_ConnectionMediaType
,
1761 BasePinImpl_QueryPinInfo
,
1762 BasePinImpl_QueryDirection
,
1763 BasePinImpl_QueryId
,
1764 GSTInPin_QueryAccept
,
1765 GSTInPin_EnumMediaTypes
,
1766 BasePinImpl_QueryInternalConnections
,
1767 GSTInPin_EndOfStream
,
1768 GSTInPin_BeginFlush
,