2 * Memory Allocator and Media Sample Implementation
4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "quartz_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
34 typedef struct StdMediaSample2
36 IMediaSample2 IMediaSample2_iface
;
38 AM_SAMPLE2_PROPERTIES props
;
39 IMemAllocator
* pParent
;
40 struct list listentry
;
43 BOOL media_time_valid
;
46 typedef struct BaseMemAllocator
48 IMemAllocator IMemAllocator_iface
;
51 ALLOCATOR_PROPERTIES props
;
52 HRESULT (* fnAlloc
) (IMemAllocator
*);
53 HRESULT (* fnFree
)(IMemAllocator
*);
54 HRESULT (* fnVerify
)(IMemAllocator
*, ALLOCATOR_PROPERTIES
*);
55 HRESULT (* fnBufferPrepare
)(IMemAllocator
*, StdMediaSample2
*, DWORD flags
);
56 HRESULT (* fnBufferReleased
)(IMemAllocator
*, StdMediaSample2
*);
57 void (* fnDestroyed
)(IMemAllocator
*);
62 struct list free_list
;
63 struct list used_list
;
64 CRITICAL_SECTION
*pCritSect
;
67 static inline BaseMemAllocator
*impl_from_IMemAllocator(IMemAllocator
*iface
)
69 return CONTAINING_RECORD(iface
, BaseMemAllocator
, IMemAllocator_iface
);
72 static const IMemAllocatorVtbl BaseMemAllocator_VTable
;
73 static const IMediaSample2Vtbl StdMediaSample2_VTable
;
74 static inline StdMediaSample2
*unsafe_impl_from_IMediaSample(IMediaSample
* iface
);
76 #define AM_SAMPLE2_PROP_SIZE_WRITABLE FIELD_OFFSET(AM_SAMPLE2_PROPERTIES, pbBuffer)
78 static HRESULT
BaseMemAllocator_Init(HRESULT (* fnAlloc
)(IMemAllocator
*),
79 HRESULT (* fnFree
)(IMemAllocator
*),
80 HRESULT (* fnVerify
)(IMemAllocator
*, ALLOCATOR_PROPERTIES
*),
81 HRESULT (* fnBufferPrepare
)(IMemAllocator
*, StdMediaSample2
*, DWORD
),
82 HRESULT (* fnBufferReleased
)(IMemAllocator
*, StdMediaSample2
*),
83 void (* fnDestroyed
)(IMemAllocator
*),
84 CRITICAL_SECTION
*pCritSect
,
85 BaseMemAllocator
* pMemAlloc
)
87 assert(fnAlloc
&& fnFree
&& fnDestroyed
);
89 pMemAlloc
->IMemAllocator_iface
.lpVtbl
= &BaseMemAllocator_VTable
;
92 ZeroMemory(&pMemAlloc
->props
, sizeof(pMemAlloc
->props
));
93 list_init(&pMemAlloc
->free_list
);
94 list_init(&pMemAlloc
->used_list
);
95 pMemAlloc
->fnAlloc
= fnAlloc
;
96 pMemAlloc
->fnFree
= fnFree
;
97 pMemAlloc
->fnVerify
= fnVerify
;
98 pMemAlloc
->fnBufferPrepare
= fnBufferPrepare
;
99 pMemAlloc
->fnBufferReleased
= fnBufferReleased
;
100 pMemAlloc
->fnDestroyed
= fnDestroyed
;
101 pMemAlloc
->bDecommitQueued
= FALSE
;
102 pMemAlloc
->bCommitted
= FALSE
;
103 pMemAlloc
->hSemWaiting
= NULL
;
104 pMemAlloc
->lWaiting
= 0;
105 pMemAlloc
->pCritSect
= pCritSect
;
110 static HRESULT WINAPI
BaseMemAllocator_QueryInterface(IMemAllocator
* iface
, REFIID riid
, LPVOID
* ppv
)
112 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
113 TRACE("(%p)->(%s, %p)\n", This
, qzdebugstr_guid(riid
), ppv
);
117 if (IsEqualIID(riid
, &IID_IUnknown
))
118 *ppv
= &This
->IMemAllocator_iface
;
119 else if (IsEqualIID(riid
, &IID_IMemAllocator
))
120 *ppv
= &This
->IMemAllocator_iface
;
124 IUnknown_AddRef((IUnknown
*)(*ppv
));
128 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
130 return E_NOINTERFACE
;
133 static ULONG WINAPI
BaseMemAllocator_AddRef(IMemAllocator
* iface
)
135 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
136 ULONG ref
= InterlockedIncrement(&This
->ref
);
138 TRACE("%p increasing refcount to %lu.\n", This
, ref
);
143 static ULONG WINAPI
BaseMemAllocator_Release(IMemAllocator
* iface
)
145 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
146 ULONG ref
= InterlockedDecrement(&This
->ref
);
148 TRACE("%p decreasing refcount to %lu.\n", This
, ref
);
152 CloseHandle(This
->hSemWaiting
);
153 if (This
->bCommitted
)
156 This
->fnDestroyed(iface
);
162 static HRESULT WINAPI
BaseMemAllocator_SetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pRequest
, ALLOCATOR_PROPERTIES
*pActual
)
164 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
167 TRACE("(%p)->(%p, %p)\n", This
, pRequest
, pActual
);
169 TRACE("Requested %ld buffers, size %ld, alignment %ld, prefix %ld.\n",
170 pRequest
->cBuffers
, pRequest
->cbBuffer
, pRequest
->cbAlign
, pRequest
->cbPrefix
);
172 EnterCriticalSection(This
->pCritSect
);
174 if (!list_empty(&This
->used_list
))
175 hr
= VFW_E_BUFFERS_OUTSTANDING
;
176 else if (This
->bCommitted
)
177 hr
= VFW_E_ALREADY_COMMITTED
;
178 else if (pRequest
->cbAlign
== 0)
183 hr
= This
->fnVerify(iface
, pRequest
);
188 This
->props
= *pRequest
;
190 *pActual
= This
->props
;
193 LeaveCriticalSection(This
->pCritSect
);
198 static HRESULT WINAPI
BaseMemAllocator_GetProperties(IMemAllocator
* iface
, ALLOCATOR_PROPERTIES
*pProps
)
200 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
202 TRACE("(%p)->(%p)\n", This
, pProps
);
204 EnterCriticalSection(This
->pCritSect
);
206 memcpy(pProps
, &This
->props
, sizeof(*pProps
));
208 LeaveCriticalSection(This
->pCritSect
);
213 static HRESULT WINAPI
BaseMemAllocator_Commit(IMemAllocator
* iface
)
215 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
218 TRACE("(%p)->()\n", This
);
220 EnterCriticalSection(This
->pCritSect
);
222 if (!This
->props
.cbAlign
)
224 else if (!This
->props
.cbBuffer
)
225 hr
= VFW_E_SIZENOTSET
;
226 else if (!This
->props
.cBuffers
)
227 hr
= VFW_E_BUFFER_NOTSET
;
228 else if (This
->bDecommitQueued
&& This
->bCommitted
)
230 This
->bDecommitQueued
= FALSE
;
233 else if (This
->bCommitted
)
237 if (!(This
->hSemWaiting
= CreateSemaphoreW(NULL
, This
->props
.cBuffers
, This
->props
.cBuffers
, NULL
)))
239 ERR("Failed to create semaphore, error %lu.\n", GetLastError());
240 hr
= HRESULT_FROM_WIN32(GetLastError());
244 hr
= This
->fnAlloc(iface
);
246 This
->bCommitted
= TRUE
;
248 ERR("Failed to allocate, hr %#lx.\n", hr
);
252 LeaveCriticalSection(This
->pCritSect
);
257 static HRESULT WINAPI
BaseMemAllocator_Decommit(IMemAllocator
* iface
)
259 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
262 TRACE("(%p)->()\n", This
);
264 EnterCriticalSection(This
->pCritSect
);
266 if (!This
->bCommitted
)
270 if (!list_empty(&This
->used_list
))
272 This
->bDecommitQueued
= TRUE
;
273 /* notify ALL waiting threads that they cannot be allocated a buffer any more */
274 ReleaseSemaphore(This
->hSemWaiting
, This
->lWaiting
, NULL
);
280 if (This
->lWaiting
!= 0)
281 ERR("Waiting: %ld\n", This
->lWaiting
);
283 This
->bCommitted
= FALSE
;
284 CloseHandle(This
->hSemWaiting
);
285 This
->hSemWaiting
= NULL
;
287 hr
= This
->fnFree(iface
);
291 LeaveCriticalSection(This
->pCritSect
);
296 static HRESULT WINAPI
BaseMemAllocator_GetBuffer(IMemAllocator
* iface
, IMediaSample
** pSample
, REFERENCE_TIME
*pStartTime
, REFERENCE_TIME
*pEndTime
, DWORD dwFlags
)
298 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
301 /* NOTE: The pStartTime and pEndTime parameters are not applied to the sample.
302 * The allocator might use these values to determine which buffer it retrieves */
304 TRACE("allocator %p, sample %p, start_time %p, end_time %p, flags %#lx.\n",
305 This
, pSample
, pStartTime
, pEndTime
, dwFlags
);
309 EnterCriticalSection(This
->pCritSect
);
310 if (!This
->bCommitted
|| This
->bDecommitQueued
)
312 WARN("Not committed\n");
313 hr
= VFW_E_NOT_COMMITTED
;
317 LeaveCriticalSection(This
->pCritSect
);
321 if (WaitForSingleObject(This
->hSemWaiting
, (dwFlags
& AM_GBF_NOWAIT
) ? 0 : INFINITE
) != WAIT_OBJECT_0
)
323 EnterCriticalSection(This
->pCritSect
);
325 LeaveCriticalSection(This
->pCritSect
);
327 return VFW_E_TIMEOUT
;
330 EnterCriticalSection(This
->pCritSect
);
333 if (!This
->bCommitted
)
334 hr
= VFW_E_NOT_COMMITTED
;
335 else if (This
->bDecommitQueued
)
340 struct list
* free
= list_head(&This
->free_list
);
342 list_add_head(&This
->used_list
, free
);
344 ms
= LIST_ENTRY(free
, StdMediaSample2
, listentry
);
345 assert(ms
->ref
== 0);
346 *pSample
= (IMediaSample
*)&ms
->IMediaSample2_iface
;
347 IMediaSample_AddRef(*pSample
);
350 LeaveCriticalSection(This
->pCritSect
);
353 WARN("Returning hr %#lx.\n", hr
);
357 static HRESULT WINAPI
BaseMemAllocator_ReleaseBuffer(IMemAllocator
* iface
, IMediaSample
* pSample
)
359 BaseMemAllocator
*This
= impl_from_IMemAllocator(iface
);
360 StdMediaSample2
* pStdSample
= unsafe_impl_from_IMediaSample(pSample
);
363 TRACE("(%p)->(%p)\n", This
, pSample
);
365 /* FIXME: make sure that sample is currently on the used list */
367 /* FIXME: we should probably check the ref count on the sample before freeing
368 * it to make sure that it is not still in use */
369 EnterCriticalSection(This
->pCritSect
);
371 if (!This
->bCommitted
)
372 ERR("Releasing a buffer when the allocator is not committed?!?\n");
374 /* remove from used_list */
375 list_remove(&pStdSample
->listentry
);
377 list_add_head(&This
->free_list
, &pStdSample
->listentry
);
379 if (list_empty(&This
->used_list
) && This
->bDecommitQueued
&& This
->bCommitted
)
381 if (This
->lWaiting
!= 0)
382 ERR("Waiting: %ld\n", This
->lWaiting
);
384 This
->bCommitted
= FALSE
;
385 This
->bDecommitQueued
= FALSE
;
387 CloseHandle(This
->hSemWaiting
);
388 This
->hSemWaiting
= NULL
;
393 LeaveCriticalSection(This
->pCritSect
);
395 /* notify a waiting thread that there is now a free buffer */
396 if (This
->hSemWaiting
&& !ReleaseSemaphore(This
->hSemWaiting
, 1, NULL
))
398 ERR("Failed to release semaphore, error %lu.\n", GetLastError());
399 hr
= HRESULT_FROM_WIN32(GetLastError());
405 static const IMemAllocatorVtbl BaseMemAllocator_VTable
=
407 BaseMemAllocator_QueryInterface
,
408 BaseMemAllocator_AddRef
,
409 BaseMemAllocator_Release
,
410 BaseMemAllocator_SetProperties
,
411 BaseMemAllocator_GetProperties
,
412 BaseMemAllocator_Commit
,
413 BaseMemAllocator_Decommit
,
414 BaseMemAllocator_GetBuffer
,
415 BaseMemAllocator_ReleaseBuffer
418 static HRESULT
StdMediaSample2_Construct(BYTE
* pbBuffer
, LONG cbBuffer
, IMemAllocator
* pParent
, StdMediaSample2
** ppSample
)
420 assert(pbBuffer
&& pParent
&& (cbBuffer
> 0));
422 if (!(*ppSample
= CoTaskMemAlloc(sizeof(StdMediaSample2
))))
423 return E_OUTOFMEMORY
;
425 (*ppSample
)->IMediaSample2_iface
.lpVtbl
= &StdMediaSample2_VTable
;
426 (*ppSample
)->ref
= 0;
427 ZeroMemory(&(*ppSample
)->props
, sizeof((*ppSample
)->props
));
429 /* NOTE: no need to AddRef as the parent is guaranteed to be around
430 * at least as long as us and we don't want to create circular
431 * dependencies on the ref count */
432 (*ppSample
)->pParent
= pParent
;
433 (*ppSample
)->props
.cbData
= sizeof(AM_SAMPLE2_PROPERTIES
);
434 (*ppSample
)->props
.cbBuffer
= (*ppSample
)->props
.lActual
= cbBuffer
;
435 (*ppSample
)->props
.pbBuffer
= pbBuffer
;
436 (*ppSample
)->media_time_valid
= FALSE
;
441 static void StdMediaSample2_Delete(StdMediaSample2
* This
)
443 if (This
->props
.pMediaType
)
444 DeleteMediaType(This
->props
.pMediaType
);
446 /* NOTE: does not remove itself from the list it belongs to */
450 static inline StdMediaSample2
*impl_from_IMediaSample2(IMediaSample2
* iface
)
452 return CONTAINING_RECORD(iface
, StdMediaSample2
, IMediaSample2_iface
);
455 static HRESULT WINAPI
StdMediaSample2_QueryInterface(IMediaSample2
* iface
, REFIID riid
, void ** ppv
)
457 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
461 if (IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IMediaSample
) ||
462 IsEqualIID(riid
, &IID_IMediaSample2
))
465 IMediaSample2_AddRef(iface
);
469 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
470 return E_NOINTERFACE
;
473 static ULONG WINAPI
StdMediaSample2_AddRef(IMediaSample2
* iface
)
475 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
476 ULONG ref
= InterlockedIncrement(&This
->ref
);
478 TRACE("%p increasing refcount to %lu.\n", This
, ref
);
483 static ULONG WINAPI
StdMediaSample2_Release(IMediaSample2
* iface
)
485 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
486 ULONG ref
= InterlockedDecrement(&This
->ref
);
488 TRACE("%p decreasing refcount to %lu.\n", This
, ref
);
492 if (This
->props
.pMediaType
)
493 DeleteMediaType(This
->props
.pMediaType
);
494 This
->props
.pMediaType
= NULL
;
495 This
->props
.dwSampleFlags
= 0;
496 This
->media_time_valid
= FALSE
;
499 IMemAllocator_ReleaseBuffer(This
->pParent
, (IMediaSample
*)iface
);
501 StdMediaSample2_Delete(This
);
506 static HRESULT WINAPI
StdMediaSample2_GetPointer(IMediaSample2
* iface
, BYTE
** ppBuffer
)
508 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
510 TRACE("(%p)->(%p)\n", iface
, ppBuffer
);
512 *ppBuffer
= This
->props
.pbBuffer
;
516 ERR("Requested an unlocked surface and trying to lock regardless\n");
523 static LONG WINAPI
StdMediaSample2_GetSize(IMediaSample2
* iface
)
525 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
527 TRACE("StdMediaSample2_GetSize()\n");
529 return This
->props
.cbBuffer
;
532 static HRESULT WINAPI
StdMediaSample2_GetTime(IMediaSample2
* iface
, REFERENCE_TIME
* pStart
, REFERENCE_TIME
* pEnd
)
534 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
537 TRACE("(%p)->(%p, %p)\n", iface
, pStart
, pEnd
);
539 if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_TIMEVALID
))
540 hr
= VFW_E_SAMPLE_TIME_NOT_SET
;
541 else if (!(This
->props
.dwSampleFlags
& AM_SAMPLE_STOPVALID
))
543 *pStart
= This
->props
.tStart
;
544 *pEnd
= This
->props
.tStart
+ 1;
546 hr
= VFW_S_NO_STOP_TIME
;
550 *pStart
= This
->props
.tStart
;
551 *pEnd
= This
->props
.tStop
;
559 static HRESULT WINAPI
StdMediaSample2_SetTime(IMediaSample2
*iface
, REFERENCE_TIME
*start
, REFERENCE_TIME
*end
)
561 StdMediaSample2
*sample
= impl_from_IMediaSample2(iface
);
563 TRACE("sample %p, start %s, end %s.\n", sample
, start
? debugstr_time(*start
) : "(null)",
564 end
? debugstr_time(*end
) : "(null)");
568 sample
->props
.tStart
= *start
;
569 sample
->props
.dwSampleFlags
|= AM_SAMPLE_TIMEVALID
;
573 sample
->props
.tStop
= *end
;
574 sample
->props
.dwSampleFlags
|= AM_SAMPLE_STOPVALID
;
577 sample
->props
.dwSampleFlags
&= ~AM_SAMPLE_STOPVALID
;
580 sample
->props
.dwSampleFlags
&= ~(AM_SAMPLE_TIMEVALID
| AM_SAMPLE_STOPVALID
);
585 static HRESULT WINAPI
StdMediaSample2_IsSyncPoint(IMediaSample2
* iface
)
587 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
589 TRACE("(%p)->()\n", iface
);
591 return (This
->props
.dwSampleFlags
& AM_SAMPLE_SPLICEPOINT
) ? S_OK
: S_FALSE
;
594 static HRESULT WINAPI
StdMediaSample2_SetSyncPoint(IMediaSample2
* iface
, BOOL bIsSyncPoint
)
596 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
598 TRACE("(%p)->(%s)\n", iface
, bIsSyncPoint
? "TRUE" : "FALSE");
601 This
->props
.dwSampleFlags
|= AM_SAMPLE_SPLICEPOINT
;
603 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_SPLICEPOINT
;
608 static HRESULT WINAPI
StdMediaSample2_IsPreroll(IMediaSample2
* iface
)
610 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
612 TRACE("(%p)->()\n", iface
);
614 return (This
->props
.dwSampleFlags
& AM_SAMPLE_PREROLL
) ? S_OK
: S_FALSE
;
617 static HRESULT WINAPI
StdMediaSample2_SetPreroll(IMediaSample2
* iface
, BOOL bIsPreroll
)
619 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
621 TRACE("(%p)->(%s)\n", iface
, bIsPreroll
? "TRUE" : "FALSE");
624 This
->props
.dwSampleFlags
|= AM_SAMPLE_PREROLL
;
626 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_PREROLL
;
631 static LONG WINAPI
StdMediaSample2_GetActualDataLength(IMediaSample2
* iface
)
633 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
635 TRACE("(%p)->()\n", iface
);
637 return This
->props
.lActual
;
640 static HRESULT WINAPI
StdMediaSample2_SetActualDataLength(IMediaSample2
* iface
, LONG len
)
642 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
644 TRACE("sample %p, len %ld.\n", This
, len
);
646 if ((len
> This
->props
.cbBuffer
) || (len
< 0))
648 ERR("Length %ld exceeds maximum %ld.\n", len
, This
->props
.cbBuffer
);
649 return VFW_E_BUFFER_OVERFLOW
;
653 This
->props
.lActual
= len
;
658 static HRESULT WINAPI
StdMediaSample2_GetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
** ppMediaType
)
660 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
662 TRACE("(%p)->(%p)\n", iface
, ppMediaType
);
664 if (!This
->props
.pMediaType
) {
665 /* Make sure we return a NULL pointer (required by native Quartz dll) */
671 if (!(*ppMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
672 return E_OUTOFMEMORY
;
674 return CopyMediaType(*ppMediaType
, This
->props
.pMediaType
);
677 static HRESULT WINAPI
StdMediaSample2_SetMediaType(IMediaSample2
* iface
, AM_MEDIA_TYPE
* pMediaType
)
679 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
681 TRACE("(%p)->(%p)\n", iface
, pMediaType
);
683 if (This
->props
.pMediaType
)
685 DeleteMediaType(This
->props
.pMediaType
);
686 This
->props
.pMediaType
= NULL
;
691 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_TYPECHANGED
;
695 This
->props
.dwSampleFlags
|= AM_SAMPLE_TYPECHANGED
;
697 if (!(This
->props
.pMediaType
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
))))
698 return E_OUTOFMEMORY
;
700 return CopyMediaType(This
->props
.pMediaType
, pMediaType
);
703 static HRESULT WINAPI
StdMediaSample2_IsDiscontinuity(IMediaSample2
* iface
)
705 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
707 TRACE("(%p)->()\n", iface
);
709 return (This
->props
.dwSampleFlags
& AM_SAMPLE_DATADISCONTINUITY
) ? S_OK
: S_FALSE
;
712 static HRESULT WINAPI
StdMediaSample2_SetDiscontinuity(IMediaSample2
* iface
, BOOL bIsDiscontinuity
)
714 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
716 TRACE("(%p)->(%s)\n", iface
, bIsDiscontinuity
? "TRUE" : "FALSE");
718 if (bIsDiscontinuity
)
719 This
->props
.dwSampleFlags
|= AM_SAMPLE_DATADISCONTINUITY
;
721 This
->props
.dwSampleFlags
&= ~AM_SAMPLE_DATADISCONTINUITY
;
726 static HRESULT WINAPI
StdMediaSample2_GetMediaTime(IMediaSample2
* iface
, LONGLONG
* pStart
, LONGLONG
* pEnd
)
728 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
730 TRACE("(%p)->(%p, %p)\n", iface
, pStart
, pEnd
);
732 if (!This
->media_time_valid
)
733 return VFW_E_MEDIA_TIME_NOT_SET
;
735 *pStart
= This
->tMediaStart
;
736 *pEnd
= This
->tMediaEnd
;
741 static HRESULT WINAPI
StdMediaSample2_SetMediaTime(IMediaSample2
*iface
, LONGLONG
*start
, LONGLONG
*end
)
743 StdMediaSample2
*sample
= impl_from_IMediaSample2(iface
);
745 TRACE("sample %p, start %s, end %s.\n", sample
, start
? debugstr_time(*start
) : "(null)",
746 end
? debugstr_time(*end
) : "(null)");
750 if (!end
) return E_POINTER
;
751 sample
->tMediaStart
= *start
;
752 sample
->tMediaEnd
= *end
;
753 sample
->media_time_valid
= TRUE
;
756 sample
->media_time_valid
= FALSE
;
761 static HRESULT WINAPI
StdMediaSample2_GetProperties(IMediaSample2
* iface
, DWORD cbProperties
, BYTE
* pbProperties
)
763 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
765 TRACE("sample %p, size %lu, properties %p.\n", This
, cbProperties
, pbProperties
);
767 memcpy(pbProperties
, &This
->props
, min(cbProperties
, sizeof(This
->props
)));
772 static HRESULT WINAPI
StdMediaSample2_SetProperties(IMediaSample2
* iface
, DWORD cbProperties
, const BYTE
* pbProperties
)
774 StdMediaSample2
*This
= impl_from_IMediaSample2(iface
);
776 TRACE("sample %p, size %lu, properties %p.\n", This
, cbProperties
, pbProperties
);
778 /* NOTE: pbBuffer and cbBuffer are read-only */
779 memcpy(&This
->props
, pbProperties
, min(cbProperties
, AM_SAMPLE2_PROP_SIZE_WRITABLE
));
784 static const IMediaSample2Vtbl StdMediaSample2_VTable
=
786 StdMediaSample2_QueryInterface
,
787 StdMediaSample2_AddRef
,
788 StdMediaSample2_Release
,
789 StdMediaSample2_GetPointer
,
790 StdMediaSample2_GetSize
,
791 StdMediaSample2_GetTime
,
792 StdMediaSample2_SetTime
,
793 StdMediaSample2_IsSyncPoint
,
794 StdMediaSample2_SetSyncPoint
,
795 StdMediaSample2_IsPreroll
,
796 StdMediaSample2_SetPreroll
,
797 StdMediaSample2_GetActualDataLength
,
798 StdMediaSample2_SetActualDataLength
,
799 StdMediaSample2_GetMediaType
,
800 StdMediaSample2_SetMediaType
,
801 StdMediaSample2_IsDiscontinuity
,
802 StdMediaSample2_SetDiscontinuity
,
803 StdMediaSample2_GetMediaTime
,
804 StdMediaSample2_SetMediaTime
,
805 StdMediaSample2_GetProperties
,
806 StdMediaSample2_SetProperties
809 static inline StdMediaSample2
*unsafe_impl_from_IMediaSample(IMediaSample
* iface
)
811 IMediaSample2
*iface2
= (IMediaSample2
*)iface
;
815 assert(iface2
->lpVtbl
== &StdMediaSample2_VTable
);
816 return impl_from_IMediaSample2(iface2
);
819 typedef struct StdMemAllocator
821 BaseMemAllocator base
;
822 CRITICAL_SECTION csState
;
826 static inline StdMemAllocator
*StdMemAllocator_from_IMemAllocator(IMemAllocator
* iface
)
828 return CONTAINING_RECORD(iface
, StdMemAllocator
, base
.IMemAllocator_iface
);
831 static HRESULT
StdMemAllocator_Alloc(IMemAllocator
* iface
)
833 StdMemAllocator
*This
= StdMemAllocator_from_IMemAllocator(iface
);
834 StdMediaSample2
* pSample
= NULL
;
838 assert(list_empty(&This
->base
.free_list
));
840 /* check alignment */
843 /* we do not allow a courser alignment than the OS page size */
844 if ((si
.dwPageSize
% This
->base
.props
.cbAlign
) != 0)
845 return VFW_E_BADALIGN
;
847 /* FIXME: each sample has to have its buffer start on the right alignment.
848 * We don't do this at the moment */
850 /* allocate memory */
851 This
->pMemory
= VirtualAlloc(NULL
, (This
->base
.props
.cbBuffer
+ This
->base
.props
.cbPrefix
) * This
->base
.props
.cBuffers
, MEM_COMMIT
, PAGE_READWRITE
);
854 return E_OUTOFMEMORY
;
856 for (i
= This
->base
.props
.cBuffers
- 1; i
>= 0; i
--)
858 /* pbBuffer does not start at the base address, it starts at base + cbPrefix */
859 BYTE
* pbBuffer
= (BYTE
*)This
->pMemory
+ i
* (This
->base
.props
.cbBuffer
+ This
->base
.props
.cbPrefix
) + This
->base
.props
.cbPrefix
;
861 StdMediaSample2_Construct(pbBuffer
, This
->base
.props
.cbBuffer
, iface
, &pSample
);
863 list_add_head(&This
->base
.free_list
, &pSample
->listentry
);
869 static HRESULT
StdMemAllocator_Free(IMemAllocator
* iface
)
871 StdMemAllocator
*This
= StdMemAllocator_from_IMemAllocator(iface
);
872 struct list
* cursor
;
874 if (!list_empty(&This
->base
.used_list
))
876 WARN("Freeing allocator with outstanding samples!\n");
877 while ((cursor
= list_head(&This
->base
.used_list
)) != NULL
)
879 StdMediaSample2
*pSample
;
881 pSample
= LIST_ENTRY(cursor
, StdMediaSample2
, listentry
);
882 pSample
->pParent
= NULL
;
886 while ((cursor
= list_head(&This
->base
.free_list
)) != NULL
)
889 StdMediaSample2_Delete(LIST_ENTRY(cursor
, StdMediaSample2
, listentry
));
893 if (!VirtualFree(This
->pMemory
, 0, MEM_RELEASE
))
895 ERR("Failed to free memory, error %lu.\n", GetLastError());
896 return HRESULT_FROM_WIN32(GetLastError());
902 static void StdMemAllocator_Destroy(IMemAllocator
*iface
)
904 StdMemAllocator
*This
= StdMemAllocator_from_IMemAllocator(iface
);
906 This
->csState
.DebugInfo
->Spare
[0] = 0;
907 DeleteCriticalSection(&This
->csState
);
912 HRESULT
mem_allocator_create(IUnknown
*lpUnkOuter
, IUnknown
**out
)
914 StdMemAllocator
* pMemAlloc
;
918 return CLASS_E_NOAGGREGATION
;
920 if (!(pMemAlloc
= CoTaskMemAlloc(sizeof(*pMemAlloc
))))
921 return E_OUTOFMEMORY
;
923 InitializeCriticalSectionEx(&pMemAlloc
->csState
, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO
);
924 pMemAlloc
->csState
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": StdMemAllocator.csState");
926 pMemAlloc
->pMemory
= NULL
;
928 if (SUCCEEDED(hr
= BaseMemAllocator_Init(StdMemAllocator_Alloc
, StdMemAllocator_Free
, NULL
, NULL
, NULL
, StdMemAllocator_Destroy
, &pMemAlloc
->csState
, &pMemAlloc
->base
)))
929 *out
= (IUnknown
*)&pMemAlloc
->base
.IMemAllocator_iface
;
931 CoTaskMemFree(pMemAlloc
);