2001-03-20 00:05:27 +00:00
|
|
|
#include "allocator.h"
|
|
|
|
#include <com.h>
|
2001-08-16 00:50:02 +00:00
|
|
|
#include <wine/winerror.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
//#undef Debug
|
|
|
|
//#define Debug
|
|
|
|
|
2001-05-06 21:43:45 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2001-03-20 00:05:27 +00:00
|
|
|
class AllocatorKeeper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AllocatorKeeper()
|
2001-08-16 00:50:02 +00:00
|
|
|
{
|
2001-03-20 00:05:27 +00:00
|
|
|
RegisterComClass(&CLSID_MemoryAllocator, MemAllocator::CreateAllocator);
|
|
|
|
}
|
2001-08-16 00:50:02 +00:00
|
|
|
~AllocatorKeeper()
|
|
|
|
{
|
|
|
|
UnregisterComClass(&CLSID_MemoryAllocator, MemAllocator::CreateAllocator);
|
|
|
|
}
|
2001-03-20 00:05:27 +00:00
|
|
|
};
|
|
|
|
static AllocatorKeeper keeper;
|
2001-08-16 00:50:02 +00:00
|
|
|
|
|
|
|
|
2001-03-20 00:05:27 +00:00
|
|
|
GUID MemAllocator::interfaces[]=
|
|
|
|
{
|
|
|
|
IID_IUnknown,
|
|
|
|
IID_IMemAllocator,
|
|
|
|
};
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
IMPLEMENT_IUNKNOWN(MemAllocator)
|
2001-03-20 00:05:27 +00:00
|
|
|
|
|
|
|
long MemAllocator::CreateAllocator(GUID* clsid, GUID* iid, void** ppv)
|
|
|
|
{
|
|
|
|
if(!ppv)return -1;
|
|
|
|
*ppv=0;
|
|
|
|
if(memcmp(clsid, &CLSID_MemoryAllocator, sizeof(GUID)))
|
|
|
|
return -1;
|
2001-08-16 00:50:02 +00:00
|
|
|
|
2001-03-20 00:05:27 +00:00
|
|
|
IMemAllocator* p=new MemAllocator;
|
|
|
|
int result=p->vt->QueryInterface((IUnknown*)p, iid, ppv);
|
|
|
|
p->vt->Release((IUnknown*)p);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
static HRESULT STDCALL MemAllocator_SetProperties(IMemAllocator * This,
|
|
|
|
/* [in] */ ALLOCATOR_PROPERTIES *pRequest,
|
|
|
|
/* [out] */ ALLOCATOR_PROPERTIES *pActual)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
2001-08-16 00:50:02 +00:00
|
|
|
Debug printf("MemAllocator_SetProperties() called\n");
|
|
|
|
if (!pRequest || !pActual)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
if (pRequest->cBuffers<=0 || pRequest->cbBuffer<=0)
|
|
|
|
return E_FAIL;
|
|
|
|
MemAllocator* me = (MemAllocator*)This;
|
|
|
|
if (me->used_list.size() || me->free_list.size())
|
|
|
|
return E_FAIL;
|
|
|
|
me->props = *pRequest;
|
|
|
|
*pActual = *pRequest;
|
2001-03-20 00:05:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
static HRESULT STDCALL MemAllocator_GetProperties(IMemAllocator * This,
|
|
|
|
/* [out] */ ALLOCATOR_PROPERTIES *pProps)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
2001-08-16 00:50:02 +00:00
|
|
|
Debug printf("MemAllocator_GetProperties(%p) called\n", This);
|
|
|
|
if (!pProps)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
if (((MemAllocator*)This)->props.cbBuffer<0)
|
|
|
|
return E_FAIL;
|
2001-03-20 00:05:27 +00:00
|
|
|
*pProps=((MemAllocator*)This)->props;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
static HRESULT STDCALL MemAllocator_Commit(IMemAllocator * This)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
2001-08-16 00:50:02 +00:00
|
|
|
Debug printf("MemAllocator_Commit(%p) called\n", This);
|
2001-03-20 00:05:27 +00:00
|
|
|
MemAllocator* me=(MemAllocator*)This;
|
2001-08-16 00:50:02 +00:00
|
|
|
if (((MemAllocator*)This)->props.cbBuffer < 0)
|
|
|
|
return E_FAIL;
|
|
|
|
if (me->used_list.size() || me->free_list.size())
|
|
|
|
return E_INVALIDARG;
|
|
|
|
for(int i = 0; i<me->props.cBuffers; i++)
|
2001-03-20 00:05:27 +00:00
|
|
|
me->free_list.push_back(new CMediaSample(me, me->props.cbBuffer));
|
2001-08-16 00:50:02 +00:00
|
|
|
|
2001-03-20 00:05:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
static HRESULT STDCALL MemAllocator_Decommit(IMemAllocator * This)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
2001-08-16 00:50:02 +00:00
|
|
|
Debug printf("MemAllocator_Decommit(%p) called\n", This);
|
2001-03-20 00:05:27 +00:00
|
|
|
MemAllocator* me=(MemAllocator*)This;
|
|
|
|
list<CMediaSample*>::iterator it;
|
|
|
|
for(it=me->free_list.begin(); it!=me->free_list.end(); it++)
|
|
|
|
delete *it;
|
|
|
|
for(it=me->used_list.begin(); it!=me->used_list.end(); it++)
|
|
|
|
delete *it;
|
|
|
|
me->free_list.clear();
|
2001-08-16 00:50:02 +00:00
|
|
|
me->used_list.clear();
|
2001-03-20 00:05:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
static HRESULT STDCALL MemAllocator_GetBuffer(IMemAllocator * This,
|
|
|
|
/* [out] */ IMediaSample **ppBuffer,
|
|
|
|
/* [in] */ REFERENCE_TIME *pStartTime,
|
|
|
|
/* [in] */ REFERENCE_TIME *pEndTime,
|
|
|
|
/* [in] */ DWORD dwFlags)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
2001-08-16 00:50:02 +00:00
|
|
|
Debug printf("MemAllocator_GetBuffer(%p) called\n", This);
|
|
|
|
MemAllocator* me = (MemAllocator*)This;
|
|
|
|
if (me->free_list.size() == 0)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
|
|
|
Debug printf("No samples available\n");
|
2001-08-16 00:50:02 +00:00
|
|
|
return E_FAIL;//should block here if no samples are available
|
|
|
|
}
|
|
|
|
list<CMediaSample*>::iterator it = me->free_list.begin();
|
2001-03-20 00:05:27 +00:00
|
|
|
me->used_list.push_back(*it);
|
2001-08-16 00:50:02 +00:00
|
|
|
*ppBuffer = *it;
|
2001-03-20 00:05:27 +00:00
|
|
|
(*ppBuffer)->vt->AddRef((IUnknown*)*ppBuffer);
|
2001-08-16 00:50:02 +00:00
|
|
|
if (me->new_pointer)
|
2001-05-06 21:43:45 +00:00
|
|
|
{
|
|
|
|
if(me->modified_sample)
|
|
|
|
me->modified_sample->ResetPointer();
|
2001-08-16 00:50:02 +00:00
|
|
|
(*it)->SetPointer(me->new_pointer);
|
2001-05-06 21:43:45 +00:00
|
|
|
me->modified_sample=*it;
|
2001-08-16 00:50:02 +00:00
|
|
|
me->new_pointer = 0;
|
2001-05-06 21:43:45 +00:00
|
|
|
}
|
2001-03-20 00:05:27 +00:00
|
|
|
me->free_list.remove(*it);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 00:50:02 +00:00
|
|
|
static HRESULT STDCALL MemAllocator_ReleaseBuffer(IMemAllocator * This,
|
|
|
|
/* [in] */ IMediaSample *pBuffer)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
2001-08-16 00:50:02 +00:00
|
|
|
Debug printf("MemAllocator_ReleaseBuffer(%p) called\n", This);
|
|
|
|
MemAllocator* me = (MemAllocator*)This;
|
2001-03-20 00:05:27 +00:00
|
|
|
list<CMediaSample*>::iterator it;
|
2001-08-16 00:50:02 +00:00
|
|
|
for (it = me->used_list.begin(); it != me->used_list.end(); it++)
|
|
|
|
if (*it == pBuffer)
|
2001-03-20 00:05:27 +00:00
|
|
|
{
|
|
|
|
me->used_list.erase(it);
|
|
|
|
me->free_list.push_back((CMediaSample*)pBuffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Debug printf("Releasing unknown buffer\n");
|
2001-08-16 00:50:02 +00:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemAllocator::MemAllocator()
|
|
|
|
{
|
|
|
|
Debug printf("MemAllocator::MemAllocator() called\n");
|
|
|
|
vt = new IMemAllocator_vt;
|
|
|
|
vt->QueryInterface = QueryInterface;
|
|
|
|
vt->AddRef = AddRef;
|
|
|
|
vt->Release = Release;
|
|
|
|
vt->SetProperties = MemAllocator_SetProperties;
|
|
|
|
vt->GetProperties = MemAllocator_GetProperties;
|
|
|
|
vt->Commit = MemAllocator_Commit;
|
|
|
|
vt->Decommit = MemAllocator_Decommit;
|
|
|
|
vt->GetBuffer = MemAllocator_GetBuffer;
|
|
|
|
vt->ReleaseBuffer = MemAllocator_ReleaseBuffer;
|
|
|
|
|
|
|
|
refcount = 1;
|
|
|
|
props.cBuffers = 1;
|
|
|
|
props.cbBuffer = 65536; /* :/ */
|
|
|
|
props.cbAlign = props.cbPrefix = 0;
|
|
|
|
|
|
|
|
new_pointer=0;
|
|
|
|
modified_sample=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemAllocator::~MemAllocator()
|
|
|
|
{
|
|
|
|
Debug printf("MemAllocator::~MemAllocator() called\n");
|
|
|
|
delete vt;
|
2001-03-20 00:05:27 +00:00
|
|
|
}
|