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
;
76 IQualityControl IQualityControl_iface
;
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 gboolean
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 if (wfx
->nChannels
<= 2 && bpp
<= 16 && depth
== bpp
) {
153 wfx
->wFormatTag
= WAVE_FORMAT_PCM
;
157 wfx
->nBlockAlign
= wfx
->nChannels
* wfx
->wBitsPerSample
/8;
158 wfx
->nAvgBytesPerSec
= wfx
->nSamplesPerSec
* wfx
->nBlockAlign
;
162 static gboolean
amt_from_gst_caps_video(GstCaps
*caps
, AM_MEDIA_TYPE
*amt
) {
163 VIDEOINFOHEADER
*vih
= CoTaskMemAlloc(sizeof(*vih
));
164 BITMAPINFOHEADER
*bih
= &vih
->bmiHeader
;
166 gint32 width
= 0, height
= 0, nom
= 0, denom
= 0;
167 const char *typename
;
168 arg
= gst_caps_get_structure(caps
, 0);
169 typename
= gst_structure_get_name(arg
);
172 if (!gst_structure_get_int(arg
, "width", &width
) ||
173 !gst_structure_get_int(arg
, "height", &height
) ||
174 !gst_structure_get_fraction(arg
, "framerate", &nom
, &denom
))
176 amt
->formattype
= FORMAT_VideoInfo
;
177 amt
->pbFormat
= (BYTE
*)vih
;
178 amt
->cbFormat
= sizeof(*vih
);
179 amt
->bFixedSizeSamples
= amt
->bTemporalCompression
= 1;
180 amt
->lSampleSize
= 0;
182 ZeroMemory(vih
, sizeof(*vih
));
183 amt
->majortype
= MEDIATYPE_Video
;
184 if (!strcmp(typename
, "video/x-raw-rgb")) {
185 if (!gst_structure_get_int(arg
, "bpp", (INT
*)&bih
->biBitCount
))
187 switch (bih
->biBitCount
) {
188 case 16: amt
->subtype
= MEDIASUBTYPE_RGB555
; break;
189 case 24: amt
->subtype
= MEDIASUBTYPE_RGB24
; break;
190 case 32: amt
->subtype
= MEDIASUBTYPE_RGB32
; break;
192 FIXME("Unknown bpp %u\n", bih
->biBitCount
);
195 bih
->biCompression
= BI_RGB
;
197 amt
->subtype
= MEDIATYPE_Video
;
198 if (!gst_structure_get_fourcc(arg
, "format", &amt
->subtype
.Data1
))
200 switch (amt
->subtype
.Data1
) {
201 case mmioFOURCC('I','4','2','0'):
202 case mmioFOURCC('Y','V','1','2'):
203 case mmioFOURCC('N','V','1','2'):
204 case mmioFOURCC('N','V','2','1'):
205 bih
->biBitCount
= 12; break;
206 case mmioFOURCC('Y','U','Y','2'):
207 case mmioFOURCC('Y','V','Y','U'):
208 bih
->biBitCount
= 16; break;
210 bih
->biCompression
= amt
->subtype
.Data1
;
212 bih
->biSizeImage
= width
* height
* bih
->biBitCount
/ 8;
213 vih
->AvgTimePerFrame
= 10000000;
214 vih
->AvgTimePerFrame
*= denom
;
215 vih
->AvgTimePerFrame
/= nom
;
216 vih
->rcSource
.left
= 0;
217 vih
->rcSource
.right
= width
;
218 vih
->rcSource
.top
= height
;
219 vih
->rcSource
.bottom
= 0;
220 vih
->rcTarget
= vih
->rcSource
;
221 bih
->biSize
= sizeof(*bih
);
222 bih
->biWidth
= width
;
223 bih
->biHeight
= height
;
228 static gboolean
accept_caps_sink(GstPad
*pad
, GstCaps
*caps
) {
229 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
232 const char *typename
;
234 arg
= gst_caps_get_structure(caps
, 0);
235 typename
= gst_structure_get_name(arg
);
236 if (!strcmp(typename
, "audio/x-raw-int") ||
237 !strcmp(typename
, "audio/x-raw-float")) {
239 ERR("Setting audio caps on non-audio pad?\n");
242 ret
= amt_from_gst_caps_audio(caps
, &amt
);
246 } else if (!strcmp(typename
, "video/x-raw-rgb")
247 || !strcmp(typename
, "video/x-raw-yuv")) {
249 ERR("Setting video caps on non-video pad?\n");
252 ret
= amt_from_gst_caps_video(caps
, &amt
);
257 FIXME("Unhandled type \"%s\"\n", typename
);
262 static gboolean
setcaps_sink(GstPad
*pad
, GstCaps
*caps
) {
263 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
264 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pin
.pinInfo
.pFilter
;
267 const char *typename
;
269 arg
= gst_caps_get_structure(caps
, 0);
270 typename
= gst_structure_get_name(arg
);
271 if (!strcmp(typename
, "audio/x-raw-int") ||
272 !strcmp(typename
, "audio/x-raw-float")) {
274 ERR("Setting audio caps on non-audio pad?\n");
277 ret
= amt_from_gst_caps_audio(caps
, &amt
);
278 } else if (!strcmp(typename
, "video/x-raw-rgb")
279 || !strcmp(typename
, "video/x-raw-yuv")) {
281 ERR("Setting video caps on non-video pad?\n");
284 ret
= amt_from_gst_caps_video(caps
, &amt
);
286 This
->props
.cbBuffer
= max(This
->props
.cbBuffer
, ((VIDEOINFOHEADER
*)amt
.pbFormat
)->bmiHeader
.biSizeImage
);
288 FIXME("Unhandled type \"%s\"\n", typename
);
291 TRACE("Linking returned %i for %s\n", ret
, typename
);
294 FreeMediaType(pin
->pmt
);
299 static gboolean
gst_base_src_perform_seek(GSTImpl
*This
, GstEvent
*event
)
303 GstFormat seek_format
;
305 GstSeekType cur_type
, stop_type
;
310 BOOL thread
= !!This
->push_thread
;
312 gst_event_parse_seek(event
, &rate
, &seek_format
, &flags
,
313 &cur_type
, &cur
, &stop_type
, &stop
);
315 if (seek_format
!= GST_FORMAT_BYTES
) {
316 FIXME("Not handling other format %i\n", seek_format
);
320 flush
= flags
& GST_SEEK_FLAG_FLUSH
;
321 seqnum
= gst_event_get_seqnum(event
);
323 /* send flush start */
325 tevent
= gst_event_new_flush_start();
326 gst_event_set_seqnum(tevent
, seqnum
);
327 gst_pad_push_event(This
->my_src
, tevent
);
328 if (This
->pInputPin
.pReader
)
329 IAsyncReader_BeginFlush(This
->pInputPin
.pReader
);
331 gst_pad_activate_push(This
->my_src
, 0);
334 This
->nextofs
= This
->start
= cur
;
336 /* and prepare to continue streaming */
338 tevent
= gst_event_new_flush_stop();
339 gst_event_set_seqnum(tevent
, seqnum
);
340 gst_pad_push_event(This
->my_src
, tevent
);
341 if (This
->pInputPin
.pReader
)
342 IAsyncReader_EndFlush(This
->pInputPin
.pReader
);
344 gst_pad_activate_push(This
->my_src
, 1);
350 static gboolean
event_src(GstPad
*pad
, GstEvent
*event
) {
351 GSTImpl
*This
= gst_pad_get_element_private(pad
);
352 switch (event
->type
) {
354 return gst_base_src_perform_seek(This
, event
);
355 case GST_EVENT_FLUSH_START
:
356 EnterCriticalSection(&This
->filter
.csFilter
);
357 if (This
->pInputPin
.pReader
)
358 IAsyncReader_BeginFlush(This
->pInputPin
.pReader
);
359 LeaveCriticalSection(&This
->filter
.csFilter
);
361 case GST_EVENT_FLUSH_STOP
:
362 EnterCriticalSection(&This
->filter
.csFilter
);
363 if (This
->pInputPin
.pReader
)
364 IAsyncReader_EndFlush(This
->pInputPin
.pReader
);
365 LeaveCriticalSection(&This
->filter
.csFilter
);
368 FIXME("%p (%u) stub\n", event
, event
->type
);
371 return gst_pad_event_default(pad
, event
);
376 static gboolean
event_sink(GstPad
*pad
, GstEvent
*event
) {
377 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
378 switch (event
->type
) {
379 case GST_EVENT_NEWSEGMENT
: {
381 gdouble rate
, applied_rate
;
383 gint64 start
, stop
, pos
;
384 gst_event_parse_new_segment_full(event
, &update
, &rate
, &applied_rate
, &format
, &start
, &stop
, &pos
);
385 if (format
!= GST_FORMAT_TIME
) {
386 FIXME("Ignoring new segment because of format %i\n", format
);
389 gst_segment_set_newsegment_full(pin
->segment
, update
, rate
, applied_rate
, format
, start
, stop
, pos
);
393 if (pin
->pin
.pin
.pConnectedTo
)
394 IPin_NewSegment(pin
->pin
.pin
.pConnectedTo
, pos
, stop
, rate
*applied_rate
);
398 if (pin
->pin
.pin
.pConnectedTo
)
399 IPin_EndOfStream(pin
->pin
.pin
.pConnectedTo
);
401 case GST_EVENT_FLUSH_START
:
402 if (pin
->pin
.pin
.pConnectedTo
)
403 IPin_BeginFlush(pin
->pin
.pin
.pConnectedTo
);
405 case GST_EVENT_FLUSH_STOP
:
406 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
407 if (pin
->pin
.pin
.pConnectedTo
)
408 IPin_EndFlush(pin
->pin
.pin
.pConnectedTo
);
411 FIXME("%p stub %s\n", event
, gst_event_type_get_name(event
->type
));
412 return gst_pad_event_default(pad
, event
);
416 static void release_sample(void *data
) {
418 ret
= IMediaSample_Release((IMediaSample
*)data
);
419 TRACE("Releasing %p returns %u\n", data
, ret
);
422 static DWORD CALLBACK
push_data(LPVOID iface
) {
423 LONGLONG maxlen
, curlen
;
424 GSTImpl
*This
= iface
;
430 IAsyncReader_Length(This
->pInputPin
.pReader
, &maxlen
, &curlen
);
433 TRACE("Starting..\n");
435 REFERENCE_TIME tStart
, tStop
;
441 hr
= IMemAllocator_GetBuffer(This
->pInputPin
.pAlloc
, &buf
, NULL
, NULL
, 0);
445 if (This
->nextofs
>= maxlen
)
447 len
= IMediaSample_GetSize(buf
);
448 if (This
->nextofs
+ len
> maxlen
)
449 len
= maxlen
- This
->nextofs
;
451 tStart
= MEDIATIME_FROM_BYTES(This
->nextofs
);
452 tStop
= tStart
+ MEDIATIME_FROM_BYTES(len
);
453 IMediaSample_SetTime(buf
, &tStart
, &tStop
);
455 hr
= IAsyncReader_Request(This
->pInputPin
.pReader
, buf
, 0);
457 IMediaSample_Release(buf
);
460 This
->nextofs
+= len
;
461 hr
= IAsyncReader_WaitForNext(This
->pInputPin
.pReader
, -1, &buf
, &user
);
462 if (FAILED(hr
) || !buf
) {
464 IMediaSample_Release(buf
);
468 IMediaSample_GetPointer(buf
, &data
);
469 gstbuf
= gst_app_buffer_new(data
, IMediaSample_GetActualDataLength(buf
), release_sample
, buf
);
471 IMediaSample_Release(buf
);
474 gstbuf
->duration
= gstbuf
->timestamp
= -1;
475 ret
= gst_pad_push(This
->my_src
, gstbuf
);
479 ERR("Sending returned: %i\n", ret
);
480 if (ret
== GST_FLOW_ERROR
)
482 else if (ret
== GST_FLOW_WRONG_STATE
)
483 hr
= VFW_E_WRONG_STATE
;
484 else if (ret
== GST_FLOW_RESEND
)
490 gst_pad_push_event(This
->my_src
, gst_event_new_eos());
492 TRACE("Almost stopping.. %08x\n", hr
);
494 IAsyncReader_WaitForNext(This
->pInputPin
.pReader
, 0, &buf
, &user
);
496 IMediaSample_Release(buf
);
499 TRACE("Stopping.. %08x\n", hr
);
503 static HRESULT WINAPI
GST_OutPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
) {
504 GSTOutPin
*pin
= (GSTOutPin
*)iface
;
505 FIXME("stub %p\n", pin
);
509 static GstFlowReturn
got_data_sink(GstPad
*pad
, GstBuffer
*buf
) {
510 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
511 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pin
.pinInfo
.pFilter
;
512 IMediaSample
*sample
;
514 BOOL freeSamp
= FALSE
;
517 gst_buffer_unref(buf
);
518 TRACE("Triggering %p %p\n", pad
, pin
->caps_event
);
519 SetEvent(pin
->caps_event
);
520 return GST_FLOW_NOT_LINKED
;
523 if (GST_IS_APP_BUFFER(buf
)) {
524 sample
= GST_APP_BUFFER(buf
)->priv
;
525 TRACE("Pushing buffer\n");
526 } else if (buf
->parent
&& GST_IS_APP_BUFFER(buf
->parent
)) {
527 sample
= GST_APP_BUFFER(buf
->parent
)->priv
;
528 TRACE("Pushing sub-buffer\n");
531 hr
= BaseOutputPinImpl_GetDeliveryBuffer(&pin
->pin
, &sample
, NULL
, NULL
, 0);
533 if (hr
== VFW_E_NOT_CONNECTED
) {
534 gst_buffer_unref(buf
);
535 return GST_FLOW_NOT_LINKED
;
538 gst_buffer_unref(buf
);
539 ERR("Didn't get a GST_APP_BUFFER, and could not get a delivery buffer (%x), returning GST_FLOW_WRONG_STATE\n", hr
);
540 return GST_FLOW_WRONG_STATE
;
542 TRACE("Did not get a GST_APP_BUFFER, creating a sample\n");
543 IMediaSample_GetPointer(sample
, &ptr
);
544 memcpy(ptr
, GST_BUFFER_DATA(buf
), GST_BUFFER_SIZE(buf
));
546 IMediaSample_SetActualDataLength(sample
, GST_BUFFER_SIZE(buf
));
548 if (GST_BUFFER_TIMESTAMP_IS_VALID(buf
)) {
549 REFERENCE_TIME rtStart
= gst_segment_to_running_time(pin
->segment
, GST_FORMAT_TIME
, buf
->timestamp
);
553 if (GST_BUFFER_DURATION_IS_VALID(buf
)) {
554 REFERENCE_TIME tStart
= buf
->timestamp
/ 100;
555 REFERENCE_TIME tStop
= (buf
->timestamp
+ buf
->duration
) / 100;
556 REFERENCE_TIME rtStop
;
557 rtStop
= gst_segment_to_running_time(pin
->segment
, GST_FORMAT_TIME
, buf
->timestamp
+ buf
->duration
);
560 TRACE("Current time on %p: %i to %i ms\n", pin
, (int)(rtStart
/ 10000), (int)(rtStop
/ 10000));
561 IMediaSample_SetTime(sample
, &rtStart
, rtStop
>= 0 ? &rtStop
: NULL
);
562 IMediaSample_SetMediaTime(sample
, &tStart
, &tStop
);
564 IMediaSample_SetTime(sample
, rtStart
>= 0 ? &rtStart
: NULL
, NULL
);
565 IMediaSample_SetMediaTime(sample
, NULL
, NULL
);
568 IMediaSample_SetTime(sample
, NULL
, NULL
);
569 IMediaSample_SetMediaTime(sample
, NULL
, NULL
);
572 IMediaSample_SetDiscontinuity(sample
, GST_BUFFER_FLAG_IS_SET(buf
, GST_BUFFER_FLAG_DISCONT
));
573 IMediaSample_SetPreroll(sample
, GST_BUFFER_FLAG_IS_SET(buf
, GST_BUFFER_FLAG_PREROLL
));
574 IMediaSample_SetSyncPoint(sample
, !GST_BUFFER_FLAG_IS_SET(buf
, GST_BUFFER_FLAG_DELTA_UNIT
));
576 if (!pin
->pin
.pin
.pConnectedTo
)
577 hr
= VFW_E_NOT_CONNECTED
;
579 hr
= IMemInputPin_Receive(pin
->pin
.pMemInputPin
, sample
);
580 TRACE("sending sample: %08x\n", hr
);
581 gst_buffer_unref(buf
);
583 IMediaSample_Release(sample
);
584 if (hr
== VFW_E_NOT_CONNECTED
)
585 return GST_FLOW_NOT_LINKED
;
587 return GST_FLOW_WRONG_STATE
;
589 return GST_FLOW_RESEND
;
593 static GstFlowReturn
request_buffer_sink(GstPad
*pad
, guint64 ofs
, guint size
, GstCaps
*caps
, GstBuffer
**buf
) {
594 GSTOutPin
*pin
= gst_pad_get_element_private(pad
);
595 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pin
.pinInfo
.pFilter
;
596 IMediaSample
*sample
;
600 TRACE("Requesting buffer\n");
603 ret
= setcaps_sink(pad
, caps
);
605 return GST_FLOW_NOT_NEGOTIATED
;
606 *buf
= gst_buffer_new_and_alloc(size
);
610 if (caps
&& caps
!= GST_PAD_CAPS(pad
))
611 if (!setcaps_sink(pad
, caps
))
612 return GST_FLOW_NOT_NEGOTIATED
;
614 hr
= BaseOutputPinImpl_GetDeliveryBuffer(&pin
->pin
, &sample
, NULL
, NULL
, 0);
615 if (hr
== VFW_E_NOT_CONNECTED
)
616 return GST_FLOW_NOT_LINKED
;
618 ERR("Could not get output buffer: %08x\n", hr
);
620 return GST_FLOW_WRONG_STATE
;
622 IMediaSample_SetActualDataLength(sample
, size
);
623 IMediaSample_GetPointer(sample
, &ptr
);
624 *buf
= gst_app_buffer_new(ptr
, size
, release_sample
, sample
);
626 IMediaSample_Release(sample
);
627 ERR("Out of memory\n");
628 return GST_FLOW_ERROR
;
630 gst_buffer_set_caps(*buf
, caps
);
634 static GstFlowReturn
request_buffer_src(GstPad
*pad
, guint64 ofs
, guint len
, GstBuffer
**buf
) {
635 GSTImpl
*This
= gst_pad_get_element_private(pad
);
639 TRACE("Requesting %s %u\n", wine_dbgstr_longlong(ofs
), len
);
640 if (ofs
== (guint64
)-1)
641 ofs
= This
->nextpullofs
;
642 if (ofs
>= This
->filesize
) {
643 WARN("Reading past eof: %s, %u\n", wine_dbgstr_longlong(ofs
), len
);
644 return GST_FLOW_UNEXPECTED
;
646 if (len
+ ofs
> This
->filesize
)
647 len
= This
->filesize
- ofs
;
648 This
->nextpullofs
= ofs
+ len
;
650 ret
= gst_pad_alloc_buffer(This
->my_src
, ofs
, len
, NULL
, buf
);
653 hr
= IAsyncReader_SyncRead(This
->pInputPin
.pReader
, ofs
, len
, GST_BUFFER_DATA(*buf
));
655 ERR("Returned %08x\n", hr
);
656 return GST_FLOW_ERROR
;
662 static DWORD CALLBACK
push_data_init(LPVOID iface
) {
663 GSTImpl
*This
= iface
;
666 TRACE("Starting..\n");
669 GstFlowReturn ret
= request_buffer_src(This
->my_src
, ofs
, 4096, &buf
);
671 ERR("Obtaining buffer returned: %i\n", ret
);
674 ret
= gst_pad_push(This
->my_src
, buf
);
677 TRACE("Sending returned: %i\n", ret
);
681 TRACE("Stopping..\n");
685 static void removed_decoded_pad(GstElement
*bin
, GstPad
*pad
, GSTImpl
*This
) {
689 EnterCriticalSection(&This
->filter
.csFilter
);
690 for (x
= 0; x
< This
->cStreams
; ++x
) {
691 if (This
->ppPins
[x
]->their_src
== pad
)
694 if (x
== This
->cStreams
)
696 pin
= This
->ppPins
[x
];
697 gst_pad_unlink(pin
->their_src
, pin
->my_sink
);
698 gst_object_unref(pin
->their_src
);
699 pin
->their_src
= NULL
;
701 TRACE("Removed %i/%i\n", x
, This
->cStreams
);
702 LeaveCriticalSection(&This
->filter
.csFilter
);
705 static void init_new_decoded_pad(GstElement
*bin
, GstPad
*pad
, gboolean last
, GSTImpl
*This
) {
708 const char *typename
;
710 AM_MEDIA_TYPE amt
= { };
716 int isvid
= 0, isaud
= 0;
718 piOutput
.dir
= PINDIR_OUTPUT
;
719 piOutput
.pFilter
= (IBaseFilter
*)This
;
720 name
= gst_pad_get_name(pad
);
721 MultiByteToWideChar(CP_UNIXCP
, 0, name
, -1, piOutput
.achName
, sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]) - 1);
722 TRACE("Name: %s\n", name
);
724 piOutput
.achName
[sizeof(piOutput
.achName
) / sizeof(piOutput
.achName
[0]) - 1] = 0;
726 caps
= gst_pad_get_caps_reffed(pad
);
727 arg
= gst_caps_get_structure(caps
, 0);
728 typename
= gst_structure_get_name(arg
);
730 mypad
= gst_pad_new(NULL
, GST_PAD_SINK
);
731 gst_pad_set_chain_function(mypad
, got_data_sink
);
732 gst_pad_set_event_function(mypad
, event_sink
);
733 gst_pad_set_bufferalloc_function(mypad
, request_buffer_sink
);
734 gst_pad_set_acceptcaps_function(mypad
, accept_caps_sink
);
735 gst_pad_set_setcaps_function(mypad
, setcaps_sink
);
737 if (!strcmp(typename
, "audio/x-raw-int") ||
738 !strcmp(typename
, "audio/x-raw-float")) {
740 } else if (!strcmp(typename
, "video/x-raw-rgb")
741 || !strcmp(typename
, "video/x-raw-yuv")) {
744 FIXME("Unknown type \'%s\'\n", typename
);
747 GST_PAD_CAPS(mypad
) = GST_CAPS_ANY
;
748 hr
= GST_AddPin(This
, &piOutput
, &amt
);
753 pin
= This
->ppPins
[This
->cStreams
- 1];
754 gst_pad_set_element_private(mypad
, pin
);
755 pin
->my_sink
= mypad
;
759 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
760 ret
= gst_pad_link(pad
, mypad
);
761 gst_pad_activate_push(mypad
, 1);
762 TRACE("Linking: %i\n", ret
);
764 pin
->their_src
= pad
;
765 gst_object_ref(pin
->their_src
);
769 static void existing_new_pad(GstElement
*bin
, GstPad
*pad
, gboolean last
, GSTImpl
*This
) {
772 if (gst_pad_is_linked(pad
))
775 /* Still holding our own lock */
777 init_new_decoded_pad(bin
, pad
, last
, This
);
781 EnterCriticalSection(&This
->filter
.csFilter
);
782 for (x
= 0; x
< This
->cStreams
; ++x
) {
783 GSTOutPin
*pin
= This
->ppPins
[x
];
784 if (!pin
->their_src
) {
785 gst_segment_init(pin
->segment
, GST_FORMAT_TIME
);
786 if (gst_pad_link(pad
, pin
->my_sink
) >= 0) {
787 pin
->their_src
= pad
;
788 gst_object_ref(pin
->their_src
);
790 LeaveCriticalSection(&This
->filter
.csFilter
);
795 init_new_decoded_pad(bin
, pad
, last
, This
);
796 LeaveCriticalSection(&This
->filter
.csFilter
);
799 static gboolean
check_get_range(GstPad
*pad
) {
803 static gboolean
query_function(GstPad
*pad
, GstQuery
*query
) {
804 GSTImpl
*This
= gst_pad_get_element_private(pad
);
809 switch (GST_QUERY_TYPE(query
)) {
810 case GST_QUERY_DURATION
:
811 gst_query_parse_duration (query
, &format
, NULL
);
812 if (format
== GST_FORMAT_PERCENT
) {
813 gst_query_set_duration (query
, GST_FORMAT_PERCENT
, GST_FORMAT_PERCENT_MAX
);
816 ret
= gst_pad_query_convert (pad
, GST_FORMAT_BYTES
, This
->filesize
, &format
, &duration
);
817 gst_query_set_duration(query
, format
, duration
);
819 case GST_QUERY_SEEKING
:
820 gst_query_parse_seeking (query
, &format
, NULL
, NULL
, NULL
);
821 TRACE("Seeking %i %i\n", format
, GST_FORMAT_BYTES
);
822 if (format
!= GST_FORMAT_BYTES
)
824 gst_query_set_seeking(query
, GST_FORMAT_BYTES
, 1, 0, This
->filesize
);
827 FIXME("Unhandled query type %i\n", GST_QUERY_TYPE(query
));
829 case GST_QUERY_CONVERT
:
834 static gboolean
activate_push(GstPad
*pad
, gboolean activate
) {
835 GSTImpl
*This
= gst_pad_get_element_private(pad
);
836 EnterCriticalSection(&This
->filter
.csFilter
);
838 TRACE("Deactivating\n");
840 IAsyncReader_BeginFlush(This
->pInputPin
.pReader
);
841 if (This
->push_thread
) {
842 WaitForSingleObject(This
->push_thread
, -1);
843 CloseHandle(This
->push_thread
);
844 This
->push_thread
= NULL
;
847 IAsyncReader_EndFlush(This
->pInputPin
.pReader
);
848 if (This
->filter
.state
== State_Stopped
)
849 This
->nextofs
= This
->start
;
850 } else if (!This
->push_thread
) {
851 TRACE("Activating\n");
853 This
->push_thread
= CreateThread(NULL
, 0, push_data_init
, This
, 0, NULL
);
855 This
->push_thread
= CreateThread(NULL
, 0, push_data
, This
, 0, NULL
);
857 LeaveCriticalSection(&This
->filter
.csFilter
);
861 static void no_more_pads(GstElement
*decodebin
, GSTImpl
*This
) {
863 SetEvent(This
->event
);
867 GST_AUTOPLUG_SELECT_TRY
,
868 GST_AUTOPLUG_SELECT_EXPOSE
,
869 GST_AUTOPLUG_SELECT_SKIP
870 } GstAutoplugSelectResult
;
872 static GstAutoplugSelectResult
autoplug_blacklist(GstElement
*bin
, GstPad
*pad
, GstCaps
*caps
, GstElementFactory
*fact
, GSTImpl
*This
) {
873 const char *name
= gst_element_factory_get_longname(fact
);
875 if (strstr(name
, "Player protection")) {
876 WARN("Blacklisted a/52 decoder because it only works in Totem\n");
877 return GST_AUTOPLUG_SELECT_SKIP
;
879 if (!strcmp(name
, "Fluendo Hardware Accelerated Video Decoder")) {
880 WARN("Disabled video acceleration since it breaks in wine\n");
881 return GST_AUTOPLUG_SELECT_SKIP
;
883 TRACE("using \"%s\"\n", name
);
884 return GST_AUTOPLUG_SELECT_TRY
;
887 static GstBusSyncReply
watch_bus(GstBus
*bus
, GstMessage
*msg
, gpointer data
) {
888 GSTImpl
*This
= data
;
890 gchar
*dbg_info
= NULL
;
891 if (GST_MESSAGE_TYPE(msg
) & GST_MESSAGE_ERROR
) {
892 gst_message_parse_error(msg
, &err
, &dbg_info
);
893 FIXME("%s: %s\n", GST_OBJECT_NAME(msg
->src
), err
->message
);
894 WARN("%s\n", dbg_info
);
895 SetEvent(This
->event
);
896 } else if (GST_MESSAGE_TYPE(msg
) & GST_MESSAGE_WARNING
) {
897 gst_message_parse_warning(msg
, &err
, &dbg_info
);
898 WARN("%s: %s\n", GST_OBJECT_NAME(msg
->src
), err
->message
);
899 WARN("%s\n", dbg_info
);
907 static void unknown_type(GstElement
*bin
, GstPad
*pad
, GstCaps
*caps
, GSTImpl
*This
) {
908 gchar
*strcaps
= gst_caps_to_string(caps
);
909 FIXME("Could not find a filter for caps: %s\n", strcaps
);
913 static HRESULT
GST_Connect(GSTInPin
*pPin
, IPin
*pConnectPin
, ALLOCATOR_PROPERTIES
*props
) {
914 GSTImpl
*This
= (GSTImpl
*)pPin
->pin
.pinInfo
.pFilter
;
917 LONGLONG avail
, duration
;
918 GstFormat format
= GST_FORMAT_TIME
;
919 GstStaticPadTemplate src_template
= GST_STATIC_PAD_TEMPLATE(
923 GST_STATIC_CAPS_ANY
);
925 TRACE("%p %p %p\n", pPin
, pConnectPin
, props
);
926 This
->props
= *props
;
927 IAsyncReader_Length(pPin
->pReader
, &This
->filesize
, &avail
);
930 This
->bus
= gst_bus_new();
931 gst_bus_set_sync_handler(This
->bus
, watch_bus
, This
);
934 This
->gstfilter
= gst_element_factory_make("decodebin2", NULL
);
935 if (!This
->gstfilter
) {
936 FIXME("Could not make source filter, are gstreamer-plugins-* installed for %u bits?\n",
937 8 * (int)sizeof(void*));
940 gst_element_set_bus(This
->gstfilter
, This
->bus
);
941 g_signal_connect(This
->gstfilter
, "new-decoded-pad", G_CALLBACK(existing_new_pad
), This
);
942 g_signal_connect(This
->gstfilter
, "pad-removed", G_CALLBACK(removed_decoded_pad
), This
);
943 g_signal_connect(This
->gstfilter
, "autoplug-select", G_CALLBACK(autoplug_blacklist
), This
);
944 g_signal_connect(This
->gstfilter
, "unknown-type", G_CALLBACK(unknown_type
), This
);
946 This
->my_src
= gst_pad_new_from_static_template(&src_template
, "quartz-src");
947 gst_pad_set_getrange_function(This
->my_src
, request_buffer_src
);
948 gst_pad_set_checkgetrange_function(This
->my_src
, check_get_range
);
949 gst_pad_set_query_function(This
->my_src
, query_function
);
950 gst_pad_set_activatepush_function(This
->my_src
, activate_push
);
951 gst_pad_set_event_function(This
->my_src
, event_src
);
952 gst_pad_set_element_private (This
->my_src
, This
);
953 This
->their_sink
= gst_element_get_static_pad(This
->gstfilter
, "sink");
955 g_signal_connect(This
->gstfilter
, "no-more-pads", G_CALLBACK(no_more_pads
), This
);
956 ret
= gst_pad_link(This
->my_src
, This
->their_sink
);
957 gst_object_unref(This
->their_sink
);
959 ERR("Returns: %i\n", ret
);
962 This
->start
= This
->nextofs
= This
->nextpullofs
= This
->stop
= 0;
964 /* Add initial pins */
965 This
->initial
= This
->discont
= TRUE
;
966 ResetEvent(This
->event
);
967 gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
968 gst_pad_set_active(This
->my_src
, 1);
969 WaitForSingleObject(This
->event
, -1);
970 gst_element_get_state(This
->gstfilter
, NULL
, NULL
, -1);
973 WARN("Ret: %i\n", ret
);
975 } else if (!This
->cStreams
) {
976 FIXME("GStreamer could not find any streams\n");
979 gst_pad_query_duration(This
->ppPins
[0]->their_src
, &format
, &duration
);
980 for (i
= 0; i
< This
->cStreams
; ++i
) {
981 This
->ppPins
[i
]->seek
.llDuration
= This
->ppPins
[i
]->seek
.llStop
= duration
/ 100;
982 This
->ppPins
[i
]->seek
.llCurrent
= 0;
983 if (!This
->ppPins
[i
]->seek
.llDuration
)
984 This
->ppPins
[i
]->seek
.dwCapabilities
= 0;
985 WaitForSingleObject(This
->ppPins
[i
]->caps_event
, -1);
989 *props
= This
->props
;
990 gst_element_set_state(This
->gstfilter
, GST_STATE_READY
);
991 gst_element_get_state(This
->gstfilter
, NULL
, NULL
, -1);
992 if (This
->push_thread
)
993 gst_pad_activate_push(This
->my_src
, 0);
995 This
->initial
= FALSE
;
996 This
->nextofs
= This
->nextpullofs
= 0;
1000 static inline GSTOutPin
*impl_from_IMediaSeeking( IMediaSeeking
*iface
) {
1001 return CONTAINING_RECORD(iface
, GSTOutPin
, seek
.IMediaSeeking_iface
);
1004 static IPin
* WINAPI
GST_GetPin(BaseFilter
*iface
, int pos
)
1006 GSTImpl
*This
= (GSTImpl
*)iface
;
1007 TRACE("Asking for pos %x\n", pos
);
1009 if (pos
> This
->cStreams
|| pos
< 0)
1013 IPin_AddRef((IPin
*)&This
->pInputPin
);
1014 return (IPin
*)&This
->pInputPin
;
1018 IPin_AddRef((IPin
*)This
->ppPins
[pos
- 1]);
1019 return (IPin
*)This
->ppPins
[pos
- 1];
1023 static LONG WINAPI
GST_GetPinCount(BaseFilter
*iface
)
1025 GSTImpl
*This
= (GSTImpl
*)iface
;
1026 return (This
->cStreams
+ 1);
1029 static const BaseFilterFuncTable BaseFuncTable
= {
1034 IUnknown
* CALLBACK
Gstreamer_Splitter_create(IUnknown
*punkout
, HRESULT
*phr
) {
1035 IUnknown
*obj
= NULL
;
1039 if (!Gstreamer_init())
1045 This
= CoTaskMemAlloc(sizeof(*This
));
1046 obj
= (IUnknown
*)This
;
1049 *phr
= E_OUTOFMEMORY
;
1053 BaseFilter_Init(&This
->filter
, &GST_Vtbl
, &CLSID_Gstreamer_Splitter
, (DWORD_PTR
)(__FILE__
": GSTImpl.csFilter"), &BaseFuncTable
);
1056 This
->ppPins
= NULL
;
1057 This
->push_thread
= NULL
;
1058 This
->event
= CreateEventW(NULL
, 0, 0, NULL
);
1061 piInput
= &This
->pInputPin
.pin
.pinInfo
;
1062 piInput
->dir
= PINDIR_INPUT
;
1063 piInput
->pFilter
= (IBaseFilter
*)This
;
1064 lstrcpynW(piInput
->achName
, wcsInputPinName
, sizeof(piInput
->achName
) / sizeof(piInput
->achName
[0]));
1065 This
->pInputPin
.pin
.IPin_iface
.lpVtbl
= &GST_InputPin_Vtbl
;
1066 This
->pInputPin
.pin
.refCount
= 1;
1067 This
->pInputPin
.pin
.pConnectedTo
= NULL
;
1068 This
->pInputPin
.pin
.pCritSec
= &This
->filter
.csFilter
;
1069 ZeroMemory(&This
->pInputPin
.pin
.mtCurrent
, sizeof(AM_MEDIA_TYPE
));
1074 static void GST_Destroy(GSTImpl
*This
) {
1075 IPin
*connected
= NULL
;
1078 TRACE("Destroying\n");
1080 CloseHandle(This
->event
);
1082 /* Don't need to clean up output pins, disconnecting input pin will do that */
1083 IPin_ConnectedTo((IPin
*)&This
->pInputPin
, &connected
);
1085 assert(IPin_Disconnect(connected
) == S_OK
);
1086 IPin_Release(connected
);
1087 assert(IPin_Disconnect((IPin
*)&This
->pInputPin
) == S_OK
);
1089 pinref
= IPin_Release((IPin
*)&This
->pInputPin
);
1091 /* Valgrind could find this, if I kill it here */
1092 ERR("pinref should be null, is %u, destroying anyway\n", pinref
);
1093 assert((LONG
)pinref
> 0);
1096 pinref
= IPin_Release((IPin
*)&This
->pInputPin
);
1099 gst_bus_set_sync_handler(This
->bus
, NULL
, NULL
);
1100 gst_object_unref(This
->bus
);
1102 BaseFilter_Destroy(&This
->filter
);
1103 CoTaskMemFree(This
);
1106 static HRESULT WINAPI
GST_QueryInterface(IBaseFilter
*iface
, REFIID riid
, LPVOID
*ppv
) {
1107 GSTImpl
*This
= (GSTImpl
*)iface
;
1108 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1112 if (IsEqualIID(riid
, &IID_IUnknown
))
1114 else if (IsEqualIID(riid
, &IID_IPersist
))
1116 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
1118 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
1122 IUnknown_AddRef((IUnknown
*)(*ppv
));
1126 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IVideoWindow
) &&
1127 !IsEqualIID(riid
, &IID_IAMFilterMiscFlags
))
1128 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1130 return E_NOINTERFACE
;
1133 static ULONG WINAPI
GST_Release(IBaseFilter
*iface
) {
1134 GSTImpl
*This
= (GSTImpl
*)iface
;
1135 ULONG refCount
= InterlockedDecrement(&This
->filter
.refCount
);
1137 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
1145 static HRESULT WINAPI
GST_Stop(IBaseFilter
*iface
) {
1146 GSTImpl
*This
= (GSTImpl
*)iface
;
1150 if (This
->gstfilter
)
1151 gst_element_set_state(This
->gstfilter
, GST_STATE_READY
);
1155 static HRESULT WINAPI
GST_Pause(IBaseFilter
*iface
) {
1157 GSTImpl
*This
= (GSTImpl
*)iface
;
1159 GstStateChangeReturn ret
;
1162 if (!This
->gstfilter
)
1163 return VFW_E_NOT_CONNECTED
;
1165 gst_element_get_state(This
->gstfilter
, &now
, NULL
, -1);
1166 if (now
== GST_STATE_PAUSED
)
1168 if (now
!= GST_STATE_PLAYING
)
1169 hr
= IBaseFilter_Run(iface
, -1);
1172 ret
= gst_element_set_state(This
->gstfilter
, GST_STATE_PAUSED
);
1173 if (ret
== GST_STATE_CHANGE_ASYNC
)
1178 static HRESULT WINAPI
GST_Run(IBaseFilter
*iface
, REFERENCE_TIME tStart
) {
1180 GSTImpl
*This
= (GSTImpl
*)iface
;
1183 HRESULT hr_any
= VFW_E_NOT_CONNECTED
;
1185 TRACE("(%s)\n", wine_dbgstr_longlong(tStart
));
1187 if (!This
->gstfilter
)
1188 return VFW_E_NOT_CONNECTED
;
1190 EnterCriticalSection(&This
->filter
.csFilter
);
1191 This
->filter
.rtStreamStart
= tStart
;
1192 LeaveCriticalSection(&This
->filter
.csFilter
);
1194 gst_element_get_state(This
->gstfilter
, &now
, NULL
, -1);
1195 if (now
== GST_STATE_PLAYING
)
1197 if (now
== GST_STATE_PAUSED
) {
1198 GstStateChangeReturn ret
;
1199 ret
= gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
1200 if (ret
== GST_STATE_CHANGE_ASYNC
)
1205 EnterCriticalSection(&This
->filter
.csFilter
);
1206 gst_pad_set_blocked(This
->my_src
, 0);
1207 gst_pad_set_blocked(This
->their_sink
, 0);
1208 gst_element_set_state(This
->gstfilter
, GST_STATE_PLAYING
);
1209 This
->filter
.rtStreamStart
= tStart
;
1211 for (i
= 0; i
< This
->cStreams
; i
++) {
1212 hr
= BaseOutputPinImpl_Active((BaseOutputPin
*)This
->ppPins
[i
]);
1213 if (SUCCEEDED(hr
)) {
1214 gst_pad_set_blocked(This
->ppPins
[i
]->my_sink
, 0);
1215 if (This
->ppPins
[i
]->their_src
)
1216 gst_pad_set_blocked(This
->ppPins
[i
]->their_src
, 0);
1222 gst_pad_set_active(This
->my_src
, 1);
1223 LeaveCriticalSection(&This
->filter
.csFilter
);
1228 static HRESULT WINAPI
GST_GetState(IBaseFilter
*iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
) {
1229 GSTImpl
*This
= (GSTImpl
*)iface
;
1231 GstState now
, pending
;
1232 GstStateChangeReturn ret
;
1234 TRACE("(%d, %p)\n", dwMilliSecsTimeout
, pState
);
1236 if (!This
->gstfilter
) {
1237 *pState
= State_Stopped
;
1241 ret
= gst_element_get_state(This
->gstfilter
, &now
, &pending
, dwMilliSecsTimeout
== INFINITE
? -1 : dwMilliSecsTimeout
* 1000);
1243 if (ret
== GST_STATE_CHANGE_ASYNC
)
1244 hr
= VFW_S_STATE_INTERMEDIATE
;
1249 case GST_STATE_PAUSED
: *pState
= State_Paused
; return hr
;
1250 case GST_STATE_PLAYING
: *pState
= State_Running
; return hr
;
1251 default: *pState
= State_Stopped
; return hr
;
1255 static HRESULT WINAPI
GST_FindPin(IBaseFilter
*iface
, LPCWSTR Id
, IPin
**ppPin
) {
1256 FIXME("(%p)->(%s,%p) stub\n", iface
, debugstr_w(Id
), ppPin
);
1260 static const IBaseFilterVtbl GST_Vtbl
= {
1262 BaseFilterImpl_AddRef
,
1264 BaseFilterImpl_GetClassID
,
1269 BaseFilterImpl_SetSyncSource
,
1270 BaseFilterImpl_GetSyncSource
,
1271 BaseFilterImpl_EnumPins
,
1273 BaseFilterImpl_QueryFilterInfo
,
1274 BaseFilterImpl_JoinFilterGraph
,
1275 BaseFilterImpl_QueryVendorInfo
1278 static HRESULT WINAPI
GST_ChangeCurrent(IMediaSeeking
*iface
) {
1282 static HRESULT WINAPI
GST_ChangeStop(IMediaSeeking
*iface
) {
1286 static HRESULT WINAPI
GST_ChangeRate(IMediaSeeking
*iface
) {
1287 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1288 GstEvent
*ev
= gst_event_new_seek(This
->seek
.dRate
, GST_FORMAT_TIME
, 0, GST_SEEK_TYPE_NONE
, -1, GST_SEEK_TYPE_NONE
, -1);
1289 TRACE("(%p) New rate %g\n", iface
, This
->seek
.dRate
);
1290 gst_pad_push_event(This
->my_sink
, ev
);
1294 static HRESULT WINAPI
GST_Seeking_QueryInterface(IMediaSeeking
*iface
, REFIID riid
, void **ppv
) {
1295 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1296 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
1299 static ULONG WINAPI
GST_Seeking_AddRef(IMediaSeeking
*iface
) {
1300 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1301 return IUnknown_AddRef((IUnknown
*)This
);
1304 static ULONG WINAPI
GST_Seeking_Release(IMediaSeeking
*iface
) {
1305 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1306 return IUnknown_Release((IUnknown
*)This
);
1309 static HRESULT WINAPI
GST_Seeking_GetCurrentPosition(IMediaSeeking
*iface
, REFERENCE_TIME
*pos
) {
1310 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1311 GstFormat format
= GST_FORMAT_TIME
;
1316 if (!This
->their_src
) {
1317 *pos
= This
->seek
.llCurrent
;
1318 TRACE("Cached value\n");
1319 if (This
->seek
.llDuration
)
1325 if (!gst_pad_query_position(This
->their_src
, &format
, pos
)) {
1326 WARN("Could not query position\n");
1330 This
->seek
.llCurrent
= *pos
;
1334 static GstSeekType
type_from_flags(DWORD flags
) {
1335 switch (flags
& AM_SEEKING_PositioningBitsMask
) {
1336 case AM_SEEKING_NoPositioning
: return GST_SEEK_TYPE_NONE
;
1337 case AM_SEEKING_AbsolutePositioning
: return GST_SEEK_TYPE_SET
;
1338 case AM_SEEKING_RelativePositioning
: return GST_SEEK_TYPE_CUR
;
1339 case AM_SEEKING_IncrementalPositioning
: return GST_SEEK_TYPE_END
;
1341 return GST_SEEK_TYPE_NONE
;
1345 static HRESULT WINAPI
GST_Seeking_SetPositions(IMediaSeeking
*iface
, REFERENCE_TIME
*pCur
, DWORD curflags
, REFERENCE_TIME
*pStop
, DWORD stopflags
) {
1347 GSTOutPin
*This
= impl_from_IMediaSeeking(iface
);
1349 GstSeekType curtype
, stoptype
;
1352 if (!This
->seek
.llDuration
)
1355 hr
= SourceSeekingImpl_SetPositions(iface
, pCur
, curflags
, pStop
, stopflags
);
1356 if (!This
->their_src
)
1359 curtype
= type_from_flags(curflags
);
1360 stoptype
= type_from_flags(stopflags
);
1361 if (curflags
& AM_SEEKING_SeekToKeyFrame
)
1362 f
|= GST_SEEK_FLAG_KEY_UNIT
;
1363 if (curflags
& AM_SEEKING_Segment
)
1364 f
|= GST_SEEK_FLAG_SEGMENT
;
1365 if (!(curflags
& AM_SEEKING_NoFlush
))
1366 f
|= GST_SEEK_FLAG_FLUSH
;
1368 e
= gst_event_new_seek(This
->seek
.dRate
, GST_FORMAT_TIME
, f
, curtype
, pCur
? *pCur
* 100 : -1, stoptype
, pStop
? *pStop
* 100 : -1);
1369 if (gst_pad_push_event(This
->my_sink
, e
))
1375 static const IMediaSeekingVtbl GST_Seeking_Vtbl
=
1377 GST_Seeking_QueryInterface
,
1379 GST_Seeking_Release
,
1380 SourceSeekingImpl_GetCapabilities
,
1381 SourceSeekingImpl_CheckCapabilities
,
1382 SourceSeekingImpl_IsFormatSupported
,
1383 SourceSeekingImpl_QueryPreferredFormat
,
1384 SourceSeekingImpl_GetTimeFormat
,
1385 SourceSeekingImpl_IsUsingTimeFormat
,
1386 SourceSeekingImpl_SetTimeFormat
,
1387 SourceSeekingImpl_GetDuration
,
1388 SourceSeekingImpl_GetStopPosition
,
1389 GST_Seeking_GetCurrentPosition
,
1390 SourceSeekingImpl_ConvertTimeFormat
,
1391 GST_Seeking_SetPositions
,
1392 SourceSeekingImpl_GetPositions
,
1393 SourceSeekingImpl_GetAvailable
,
1394 SourceSeekingImpl_SetRate
,
1395 SourceSeekingImpl_GetRate
,
1396 SourceSeekingImpl_GetPreroll
1399 static inline GSTOutPin
*impl_from_IQualityControl( IQualityControl
*iface
)
1401 return (GSTOutPin
*)CONTAINING_RECORD(iface
, GSTOutPin
, IQualityControl_iface
);
1404 static HRESULT WINAPI
GST_QualityControl_QueryInterface(IQualityControl
*iface
, REFIID riid
, void **ppv
)
1406 GSTOutPin
*pin
= impl_from_IQualityControl(iface
);
1407 return IPin_QueryInterface((IPin
*)pin
, riid
, ppv
);
1410 static ULONG WINAPI
GST_QualityControl_AddRef(IQualityControl
*iface
)
1412 GSTOutPin
*pin
= impl_from_IQualityControl(iface
);
1413 return IPin_AddRef((IPin
*)pin
);
1416 static ULONG WINAPI
GST_QualityControl_Release(IQualityControl
*iface
)
1418 GSTOutPin
*pin
= impl_from_IQualityControl(iface
);
1419 return IPin_Release((IPin
*)pin
);
1422 static HRESULT WINAPI
GST_QualityControl_Notify(IQualityControl
*iface
, IBaseFilter
*sender
, Quality qm
) {
1423 GSTOutPin
*pin
= impl_from_IQualityControl(iface
);
1424 REFERENCE_TIME late
= qm
.Late
;
1425 if (qm
.Late
< 0 && -qm
.Late
> qm
.TimeStamp
)
1426 late
= -qm
.TimeStamp
;
1427 gst_pad_push_event(pin
->my_sink
, gst_event_new_qos(1000./qm
.Proportion
, late
*100, qm
.TimeStamp
*100));
1431 static HRESULT WINAPI
GST_QualityControl_SetSink(IQualityControl
*iface
, IQualityControl
*tonotify
)
1437 static const IQualityControlVtbl GSTOutPin_QualityControl_Vtbl
= {
1438 GST_QualityControl_QueryInterface
,
1439 GST_QualityControl_AddRef
,
1440 GST_QualityControl_Release
,
1441 GST_QualityControl_Notify
,
1442 GST_QualityControl_SetSink
1445 static HRESULT WINAPI
GSTOutPin_QueryInterface(IPin
*iface
, REFIID riid
, void **ppv
) {
1446 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1448 TRACE("(%s, %p)\n", debugstr_guid(riid
), ppv
);
1452 if (IsEqualIID(riid
, &IID_IUnknown
))
1454 else if (IsEqualIID(riid
, &IID_IPin
))
1456 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1458 else if (IsEqualIID(riid
, &IID_IQualityControl
))
1459 *ppv
= &This
->IQualityControl_iface
;
1462 IUnknown_AddRef((IUnknown
*)(*ppv
));
1465 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1466 return E_NOINTERFACE
;
1469 static ULONG WINAPI
GSTOutPin_Release(IPin
*iface
) {
1470 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1471 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
1472 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
1475 if (This
->their_src
)
1476 gst_pad_unlink(This
->their_src
, This
->my_sink
);
1477 gst_object_unref(This
->my_sink
);
1478 CloseHandle(This
->caps_event
);
1479 DeleteMediaType(This
->pmt
);
1480 FreeMediaType(&This
->pin
.pin
.mtCurrent
);
1481 gst_segment_free(This
->segment
);
1482 if (This
->pin
.pAllocator
)
1483 IMemAllocator_Release(This
->pin
.pAllocator
);
1484 CoTaskMemFree(This
);
1490 static HRESULT WINAPI
GSTOutPin_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
1492 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1495 return E_INVALIDARG
;
1497 return VFW_S_NO_MORE_ITEMS
;
1498 CopyMediaType(pmt
, This
->pmt
);
1502 static HRESULT WINAPI
GSTOutPin_DecideBufferSize(BaseOutputPin
*iface
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
1508 static HRESULT WINAPI
GSTOutPin_DecideAllocator(BaseOutputPin
*iface
, IMemInputPin
*pPin
, IMemAllocator
**pAlloc
)
1511 GSTOutPin
*This
= (GSTOutPin
*)iface
;
1512 GSTImpl
*GSTfilter
= (GSTImpl
*)This
->pin
.pin
.pinInfo
.pFilter
;
1515 if (GSTfilter
->pInputPin
.pAlloc
)
1517 hr
= IMemInputPin_NotifyAllocator(pPin
, GSTfilter
->pInputPin
.pAlloc
, FALSE
);
1520 *pAlloc
= GSTfilter
->pInputPin
.pAlloc
;
1521 IMemAllocator_AddRef(*pAlloc
);
1525 hr
= VFW_E_NO_ALLOCATOR
;
1530 static HRESULT WINAPI
GSTOutPin_BreakConnect(BaseOutputPin
*This
)
1534 TRACE("(%p)->()\n", This
);
1536 EnterCriticalSection(This
->pin
.pCritSec
);
1537 if (!This
->pin
.pConnectedTo
|| !This
->pMemInputPin
)
1538 hr
= VFW_E_NOT_CONNECTED
;
1541 hr
= IPin_Disconnect(This
->pin
.pConnectedTo
);
1542 IPin_Disconnect((IPin
*)This
);
1544 LeaveCriticalSection(This
->pin
.pCritSec
);
1549 static const IPinVtbl GST_OutputPin_Vtbl
= {
1550 GSTOutPin_QueryInterface
,
1553 BaseOutputPinImpl_Connect
,
1554 BaseOutputPinImpl_ReceiveConnection
,
1555 BaseOutputPinImpl_Disconnect
,
1556 BasePinImpl_ConnectedTo
,
1557 BasePinImpl_ConnectionMediaType
,
1558 BasePinImpl_QueryPinInfo
,
1559 BasePinImpl_QueryDirection
,
1560 BasePinImpl_QueryId
,
1561 GST_OutPin_QueryAccept
,
1562 BasePinImpl_EnumMediaTypes
,
1563 BasePinImpl_QueryInternalConnections
,
1564 BaseOutputPinImpl_EndOfStream
,
1565 BaseOutputPinImpl_BeginFlush
,
1566 BaseOutputPinImpl_EndFlush
,
1567 BasePinImpl_NewSegment
1570 static const BaseOutputPinFuncTable output_BaseOutputFuncTable
= {
1573 BaseOutputPinImpl_AttemptConnection
,
1574 BasePinImpl_GetMediaTypeVersion
,
1575 GSTOutPin_GetMediaType
1577 GSTOutPin_DecideBufferSize
,
1578 GSTOutPin_DecideAllocator
,
1579 GSTOutPin_BreakConnect
1582 static HRESULT
GST_AddPin(GSTImpl
*This
, const PIN_INFO
*piOutput
, const AM_MEDIA_TYPE
*amt
) {
1584 This
->ppPins
= CoTaskMemRealloc(This
->ppPins
, (This
->cStreams
+ 1) * sizeof(IPin
*));
1586 hr
= BaseOutputPin_Construct(&GST_OutputPin_Vtbl
, sizeof(GSTOutPin
), piOutput
, &output_BaseOutputFuncTable
, &This
->filter
.csFilter
, (IPin
**)(This
->ppPins
+ This
->cStreams
));
1587 if (SUCCEEDED(hr
)) {
1588 GSTOutPin
*pin
= This
->ppPins
[This
->cStreams
];
1589 pin
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
1590 CopyMediaType(pin
->pmt
, amt
);
1591 pin
->pin
.pin
.pinInfo
.pFilter
= (LPVOID
)This
;
1592 pin
->caps_event
= CreateEventW(NULL
, 0, 0, NULL
);
1593 pin
->segment
= gst_segment_new();
1595 pin
->IQualityControl_iface
.lpVtbl
= &GSTOutPin_QualityControl_Vtbl
;
1596 SourceSeeking_Init(&pin
->seek
, &GST_Seeking_Vtbl
, GST_ChangeStop
, GST_ChangeCurrent
, GST_ChangeRate
, &This
->filter
.csFilter
);
1597 BaseFilterImpl_IncrementPinVersion((BaseFilter
*)This
);
1599 ERR("Failed with error %x\n", hr
);
1603 static HRESULT
GST_RemoveOutputPins(GSTImpl
*This
) {
1606 GSTOutPin
**ppOldPins
= This
->ppPins
;
1607 TRACE("(%p)\n", This
);
1609 if (!This
->gstfilter
)
1611 gst_element_set_bus(This
->gstfilter
, NULL
);
1612 gst_element_set_state(This
->gstfilter
, GST_STATE_NULL
);
1613 gst_pad_unlink(This
->my_src
, This
->their_sink
);
1614 if (This
->push_thread
)
1615 gst_pad_activate_push(This
->my_src
, 0);
1616 gst_object_unref(This
->my_src
);
1617 This
->my_src
= This
->their_sink
= NULL
;
1619 for (i
= 0; i
< This
->cStreams
; i
++) {
1620 hr
= BaseOutputPinImpl_BreakConnect(&ppOldPins
[i
]->pin
);
1621 TRACE("Disconnect: %08x\n", hr
);
1622 IPin_Release((IPin
*)ppOldPins
[i
]);
1625 This
->ppPins
= NULL
;
1626 gst_object_unref(This
->gstfilter
);
1627 This
->gstfilter
= NULL
;
1628 BaseFilterImpl_IncrementPinVersion((BaseFilter
*)This
);
1629 CoTaskMemFree(ppOldPins
);
1633 static ULONG WINAPI
GSTInPin_Release(IPin
*iface
) {
1634 GSTInPin
*This
= (GSTInPin
*)iface
;
1635 ULONG refCount
= InterlockedDecrement(&This
->pin
.refCount
);
1637 TRACE("(%p)->() Release from %d\n", iface
, refCount
+ 1);
1639 FreeMediaType(&This
->pin
.mtCurrent
);
1641 IMemAllocator_Release(This
->pAlloc
);
1642 This
->pAlloc
= NULL
;
1643 This
->pin
.IPin_iface
.lpVtbl
= NULL
;
1649 static HRESULT WINAPI
GSTInPin_ReceiveConnection(IPin
*iface
, IPin
*pReceivePin
, const AM_MEDIA_TYPE
*pmt
) {
1650 PIN_DIRECTION pindirReceive
;
1652 GSTInPin
*This
= (GSTInPin
*)iface
;
1654 TRACE("(%p/%p)->(%p, %p)\n", This
, iface
, pReceivePin
, pmt
);
1655 dump_AM_MEDIA_TYPE(pmt
);
1657 EnterCriticalSection(This
->pin
.pCritSec
);
1658 if (!This
->pin
.pConnectedTo
) {
1659 ALLOCATOR_PROPERTIES props
;
1662 props
.cbBuffer
= 16384;
1666 if (SUCCEEDED(hr
) && IPin_QueryAccept(iface
, pmt
) != S_OK
)
1667 hr
= VFW_E_TYPE_NOT_ACCEPTED
;
1668 if (SUCCEEDED(hr
)) {
1669 IPin_QueryDirection(pReceivePin
, &pindirReceive
);
1670 if (pindirReceive
!= PINDIR_OUTPUT
) {
1671 ERR("Can't connect from non-output pin\n");
1672 hr
= VFW_E_INVALID_DIRECTION
;
1676 This
->pReader
= NULL
;
1677 This
->pAlloc
= NULL
;
1679 hr
= IPin_QueryInterface(pReceivePin
, &IID_IAsyncReader
, (LPVOID
*)&This
->pReader
);
1681 hr
= GST_Connect(This
, pReceivePin
, &props
);
1683 hr
= IAsyncReader_RequestAllocator(This
->pReader
, NULL
, &props
, &This
->pAlloc
);
1684 if (SUCCEEDED(hr
)) {
1685 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
1686 This
->pin
.pConnectedTo
= pReceivePin
;
1687 IPin_AddRef(pReceivePin
);
1688 hr
= IMemAllocator_Commit(This
->pAlloc
);
1690 GST_RemoveOutputPins((GSTImpl
*)This
->pin
.pinInfo
.pFilter
);
1692 IAsyncReader_Release(This
->pReader
);
1693 This
->pReader
= NULL
;
1695 IMemAllocator_Release(This
->pAlloc
);
1696 This
->pAlloc
= NULL
;
1698 TRACE("Size: %i\n", props
.cbBuffer
);
1700 hr
= VFW_E_ALREADY_CONNECTED
;
1701 LeaveCriticalSection(This
->pin
.pCritSec
);
1705 static HRESULT WINAPI
GSTInPin_Disconnect(IPin
*iface
) {
1707 GSTInPin
*This
= (GSTInPin
*)iface
;
1711 hr
= IBaseFilter_GetState(This
->pin
.pinInfo
.pFilter
, INFINITE
, &state
);
1712 EnterCriticalSection(This
->pin
.pCritSec
);
1713 if (This
->pin
.pConnectedTo
) {
1714 GSTImpl
*Parser
= (GSTImpl
*)This
->pin
.pinInfo
.pFilter
;
1716 if (SUCCEEDED(hr
) && state
== State_Stopped
) {
1717 IMemAllocator_Decommit(This
->pAlloc
);
1718 IPin_Disconnect(This
->pin
.pConnectedTo
);
1719 This
->pin
.pConnectedTo
= NULL
;
1720 hr
= GST_RemoveOutputPins(Parser
);
1722 hr
= VFW_E_NOT_STOPPED
;
1725 LeaveCriticalSection(This
->pin
.pCritSec
);
1729 static HRESULT WINAPI
GSTInPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
) {
1730 GSTInPin
*This
= (GSTInPin
*)iface
;
1732 TRACE("(%p)->(%p)\n", This
, pmt
);
1733 dump_AM_MEDIA_TYPE(pmt
);
1735 if (IsEqualIID(&pmt
->majortype
, &MEDIATYPE_Stream
))
1740 static HRESULT WINAPI
GSTInPin_EndOfStream(IPin
*iface
) {
1741 GSTInPin
*pin
= (GSTInPin
*)iface
;
1742 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1744 FIXME("Propagate message on %p\n", This
);
1748 static HRESULT WINAPI
GSTInPin_BeginFlush(IPin
*iface
) {
1749 GSTInPin
*pin
= (GSTInPin
*)iface
;
1750 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1752 FIXME("Propagate message on %p\n", This
);
1756 static HRESULT WINAPI
GSTInPin_EndFlush(IPin
*iface
) {
1757 GSTInPin
*pin
= (GSTInPin
*)iface
;
1758 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1760 FIXME("Propagate message on %p\n", This
);
1764 static HRESULT WINAPI
GSTInPin_NewSegment(IPin
*iface
, REFERENCE_TIME tStart
, REFERENCE_TIME tStop
, double dRate
) {
1765 GSTInPin
*pin
= (GSTInPin
*)iface
;
1766 GSTImpl
*This
= (GSTImpl
*)pin
->pin
.pinInfo
.pFilter
;
1768 BasePinImpl_NewSegment(iface
, tStart
, tStop
, dRate
);
1769 FIXME("Propagate message on %p\n", This
);
1773 static HRESULT WINAPI
GSTInPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
1775 GSTInPin
*This
= (GSTInPin
*)iface
;
1777 TRACE("(%p/%p)->(%s, %p)\n", This
, iface
, debugstr_guid(riid
), ppv
);
1781 if (IsEqualIID(riid
, &IID_IUnknown
))
1783 else if (IsEqualIID(riid
, &IID_IPin
))
1785 else if (IsEqualIID(riid
, &IID_IMediaSeeking
))
1787 return IBaseFilter_QueryInterface(This
->pin
.pinInfo
.pFilter
, &IID_IMediaSeeking
, ppv
);
1792 IUnknown_AddRef((IUnknown
*)(*ppv
));
1796 FIXME("No interface for %s!\n", debugstr_guid(riid
));
1798 return E_NOINTERFACE
;
1801 static HRESULT WINAPI
GSTInPin_EnumMediaTypes(IPin
*iface
, IEnumMediaTypes
**ppEnum
)
1803 BasePin
*This
= (BasePin
*)iface
;
1805 TRACE("(%p/%p)->(%p)\n", This
, iface
, ppEnum
);
1807 return EnumMediaTypes_Construct(This
, BasePinImpl_GetMediaType
, BasePinImpl_GetMediaTypeVersion
, ppEnum
);
1810 static const IPinVtbl GST_InputPin_Vtbl
= {
1811 GSTInPin_QueryInterface
,
1814 BaseInputPinImpl_Connect
,
1815 GSTInPin_ReceiveConnection
,
1816 GSTInPin_Disconnect
,
1817 BasePinImpl_ConnectedTo
,
1818 BasePinImpl_ConnectionMediaType
,
1819 BasePinImpl_QueryPinInfo
,
1820 BasePinImpl_QueryDirection
,
1821 BasePinImpl_QueryId
,
1822 GSTInPin_QueryAccept
,
1823 GSTInPin_EnumMediaTypes
,
1824 BasePinImpl_QueryInternalConnections
,
1825 GSTInPin_EndOfStream
,
1826 GSTInPin_BeginFlush
,