- HACK: Return the oldest used sample when the allocator runs out of samples. Fixes playback as the render does not seem to release the media samples
- Start implementing IKsClockForwarder interface
- Fix a few bugs in KsOpenDefaultDevice
- Implement IMediaSample::GetMediaType
- Always use the CKsAllocator interface for allocating samples
svn path=/trunk/; revision=46346
{
public:
typedef std::stack<IMediaSample *>MediaSampleStack;
+ typedef std::list<IMediaSample *>MediaSampleList;
STDMETHODIMP QueryInterface( REFIID InterfaceId, PVOID* Interface);
BOOL m_Commited;
CRITICAL_SECTION m_CriticalSection;
MediaSampleStack m_FreeList;
+ MediaSampleList m_UsedList;
LPVOID m_Buffer;
BOOL m_FreeSamples;
};
m_cbPrefix(0),
m_Commited(FALSE),
m_FreeList(),
+ m_UsedList(),
m_Buffer(0),
m_FreeSamples(FALSE)
{
if (!Sample)
{
// no sample acquired
- return VFW_E_TIMEOUT;
+ //HACKKKKKKK
+ Sample = m_UsedList.back();
+ m_UsedList.pop_back();
+
+ if (!Sample)
+ return VFW_E_TIMEOUT;
}
// store result
*ppBuffer = Sample;
+ // store sample in used list
+ m_UsedList.push_front(Sample);
+
// done
return NOERROR;
}
// add the sample to the free list
m_FreeList.push(pBuffer);
+
if (m_FreeSamples)
{
// pending de-commit
const GUID KSCATEGORY_CLOCK = {0x53172480, 0x4791, 0x11D0, {0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00}};
#endif
+DWORD WINAPI CKsClockForwarder_ThreadStartup(LPVOID lpParameter);
+
class CKsClockForwarder : public IDistributorNotify,
public IKsObject
{
// IKsObject interface
HANDLE STDMETHODCALLTYPE KsGetObjectHandle();
- CKsClockForwarder(HANDLE handle) : m_Ref(0), m_Handle(handle){}
- virtual ~CKsClockForwarder(){ if (m_Handle) CloseHandle(m_Handle);}
-
+ CKsClockForwarder(HANDLE handle);
+ virtual ~CKsClockForwarder(){};
+ HRESULT STDMETHODCALLTYPE SetClockState(KSSTATE State);
protected:
LONG m_Ref;
HANDLE m_Handle;
+ IReferenceClock * m_Clock;
+ HANDLE m_hEvent;
+ HANDLE m_hThread;
+ BOOL m_ThreadStarted;
+ BOOL m_PendingStop;
+ BOOL m_ForceStart;
+ KSSTATE m_State;
+ REFERENCE_TIME m_Time;
+
+ friend DWORD WINAPI CKsClockForwarder_ThreadStartup(LPVOID lpParameter);
};
+CKsClockForwarder::CKsClockForwarder(
+ HANDLE handle) : m_Ref(0),
+ m_Handle(handle),
+ m_Clock(0),
+ m_hEvent(NULL),
+ m_hThread(NULL),
+ m_ThreadStarted(FALSE),
+ m_PendingStop(FALSE),
+ m_ForceStart(FALSE),
+ m_State(KSSTATE_STOP),
+ m_Time(0)
+{
+}
+
HRESULT
STDMETHODCALLTYPE
CKsClockForwarder::QueryInterface(
STDMETHODCALLTYPE
CKsClockForwarder::Stop()
{
- OutputDebugString("UNIMPLEMENTED\n");
- return E_NOTIMPL;
+ OutputDebugString("CKsClockForwarder::Stop\n");
+
+ if (m_ThreadStarted)
+ {
+ // signal pending stop
+ m_PendingStop = true;
+
+ assert(m_hThread);
+ assert(m_hEvent);
+
+ // set stop event
+ SetEvent(m_hEvent);
+
+ // wait untill the thread has finished
+ WaitForSingleObject(m_hThread, INFINITE);
+
+ // close thread handle
+ CloseHandle(m_hThread);
+
+ // zero handle
+ m_hThread = NULL;
+ }
+
+ if (m_hEvent)
+ {
+ // close stop event
+ CloseHandle(m_hEvent);
+ m_hEvent = NULL;
+ }
+
+ m_PendingStop = false;
+
+ SetClockState(KSSTATE_STOP);
+ return NOERROR;
}
HRESULT
STDMETHODCALLTYPE
CKsClockForwarder::Pause()
{
- OutputDebugString("UNIMPLEMENTED\n");
- return E_NOTIMPL;
+ OutputDebugString("CKsClockForwarder::Pause\n");
+
+ if (!m_hEvent)
+ {
+ m_hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
+ if (!m_hEvent)
+ return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, GetLastError());
+ }
+
+ if (m_State <= KSSTATE_PAUSE)
+ {
+ if (m_State == KSSTATE_STOP)
+ SetClockState(KSSTATE_ACQUIRE);
+
+ if (m_State == KSSTATE_ACQUIRE)
+ SetClockState(KSSTATE_PAUSE);
+ }
+ else
+ {
+ if (!m_ForceStart)
+ {
+ SetClockState(KSSTATE_PAUSE);
+ }
+ }
+
+ if (!m_hThread)
+ {
+ m_hThread = CreateThread(NULL, 0, CKsClockForwarder_ThreadStartup, (LPVOID)this, 0, NULL);
+ if (!m_hThread)
+ return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, GetLastError());
+ }
+
+ return NOERROR;
}
HRESULT
CKsClockForwarder::Run(
REFERENCE_TIME tStart)
{
- OutputDebugString("UNIMPLEMENTED\n");
- return E_NOTIMPL;
+ OutputDebugString("CKsClockForwarder::Run\n");
+
+ m_Time = tStart;
+
+ if (!m_hEvent || !m_hThread)
+ {
+ m_ForceStart = TRUE;
+ HRESULT hr = Pause();
+ m_ForceStart = FALSE;
+
+ if (FAILED(hr))
+ return hr;
+ }
+
+ assert(m_hThread);
+
+ SetClockState(KSSTATE_RUN);
+ SetEvent(m_hEvent);
+
+ return NOERROR;
}
HRESULT
CKsClockForwarder::SetSyncSource(
IReferenceClock *pClock)
{
- OutputDebugString("UNIMPLEMENTED\n");
- return E_NOTIMPL;
+ OutputDebugString("CKsClockForwarder::SetSyncSource\n");
+
+ if (pClock)
+ pClock->AddRef();
+
+ if (m_Clock)
+ m_Clock->Release();
+
+
+ m_Clock = pClock;
+ return NOERROR;
}
HRESULT
STDMETHODCALLTYPE
CKsClockForwarder::NotifyGraphChange()
{
- OutputDebugString("UNIMPLEMENTED\n");
+ OutputDebugString("CKsClockForwarder::NotifyGraphChange\n");
+ DebugBreak();
return E_NOTIMPL;
}
return m_Handle;
}
+//-------------------------------------------------------------------
+HRESULT
+STDMETHODCALLTYPE
+CKsClockForwarder::SetClockState(KSSTATE State)
+{
+ KSPROPERTY Property;
+ ULONG BytesReturned;
+
+ Property.Set = KSPROPSETID_Clock;
+ Property.Id = KSPROPERTY_CLOCK_STATE;
+ Property.Flags = KSPROPERTY_TYPE_SET;
+
+ HRESULT hr = KsSynchronousDeviceControl(m_Handle, IOCTL_KS_PROPERTY, (PVOID)&Property, sizeof(KSPROPERTY), &State, sizeof(KSSTATE), &BytesReturned);
+ if (SUCCEEDED(hr))
+ m_State = State;
+
+ return hr;
+}
+
+DWORD
+WINAPI
+CKsClockForwarder_ThreadStartup(LPVOID lpParameter)
+{
+ REFERENCE_TIME Time;
+ ULONG BytesReturned;
+
+ CKsClockForwarder * Fwd = (CKsClockForwarder*)lpParameter;
+
+ Fwd->m_ThreadStarted = TRUE;
+
+ do
+ {
+ if (Fwd->m_PendingStop)
+ break;
+
+ if (Fwd->m_State != KSSTATE_RUN)
+ WaitForSingleObject(Fwd->m_hEvent, INFINITE);
+
+ KSPROPERTY Property;
+ Property.Set = KSPROPSETID_Clock;
+ Property.Id = KSPROPERTY_CLOCK_TIME;
+ Property.Flags = KSPROPERTY_TYPE_SET;
+
+ Fwd->m_Clock->GetTime(&Time);
+ Time -= Fwd->m_Time;
+
+ KsSynchronousDeviceControl(Fwd->m_Handle, IOCTL_KS_PROPERTY, (PVOID)&Property, sizeof(KSPROPERTY), &Time, sizeof(REFERENCE_TIME), &BytesReturned);
+ }
+ while(TRUE);
+
+ Fwd->m_ThreadStarted = FALSE;
+ return NOERROR;
+}
+
HRESULT
WINAPI
CKsClockForwarder_Constructor(
CInputPin::Receive(IMediaSample *pSample)
{
OutputDebugStringW(L"CInputPin::Receive NotImplemented\n");
+ DebugBreak();
return E_NOTIMPL;
}
CInputPin::ReceiveMultiple(IMediaSample **pSamples, long nSamples, long *nSamplesProcessed)
{
OutputDebugStringW(L"CInputPin::ReceiveMultiple NotImplemented\n");
+ DebugBreak();
return E_NOTIMPL;
}
CInputPin::ReceiveCanBlock( void)
{
OutputDebugStringW(L"CInputPin::ReceiveCanBlock NotImplemented\n");
+ DebugBreak();
return S_FALSE;
}
ULONG BytesReturned;
HRESULT hr;
- OutputDebugStringW(L"CInputPin::KsPropagateAcquire\n");
-
assert(m_hPin != INVALID_HANDLE_VALUE);
Property.Set = KSPROPSETID_Connection;
REFERENCE_TIME TimeDelta)
{
OutputDebugStringW(L"CInputPin::KsQualityNotify NotImplemented\n");
+ DebugBreak();
return E_NOTIMPL;
}
CInputPin::Connect(IPin *pReceivePin, const AM_MEDIA_TYPE *pmt)
{
OutputDebugStringW(L"CInputPin::Connect NotImplemented\n");
+ DebugBreak();
return NOERROR;
}
return VFW_E_NOT_CONNECTED;
OutputDebugStringW(L"CInputPin::ConnectionMediaType NotImplemented\n");
+ DebugBreak();
return E_NOTIMPL;
}
HRESULT
}
pPin->Release();
}
-
- IKsAllocatorEx * Allocator;
-
- if (SUCCEEDED(KsPin->QueryInterface(IID_IKsAllocatorEx, (void**)&Allocator)))
- {
- PALLOCATOR_PROPERTIES_EX Properties = Allocator->KsGetProperties();
-
- if (Properties)
- {
- WCHAR Buffer[100];
- swprintf(Buffer, L"CKsInterfaceHandler::KsSetPin PinName %s Properties.cbAlign %u cbBuffer %u cbPrefix %u cBuffers %u\n", m_PinName, Properties->cbAlign, Properties->cbBuffer, Properties->cbPrefix, Properties->cBuffers);
- OutputDebugStringW(Buffer);
- }
- Allocator->Release();
- }
-
#endif
// done
// query for IMediaSample2 interface
IMediaSample2 * MediaSample;
AM_SAMPLE2_PROPERTIES Properties;
+ ZeroMemory(&Properties, sizeof(AM_SAMPLE2_PROPERTIES));
hr = SampleList[Index]->QueryInterface(IID_IMediaSample2, (void**)&MediaSample);
if (SUCCEEDED(hr))
hr = SampleList[Index]->GetPointer((BYTE**)&Properties.pbBuffer);
assert(hr == NOERROR);
hr = SampleList[Index]->GetTime(&Properties.tStart, &Properties.tStop);
- assert(hr == NOERROR);
Properties.cbBuffer = SampleList[Index]->GetSize();
assert(Properties.cbBuffer);
}
WCHAR Buffer[200];
- swprintf(Buffer, L"CKsInterfaceHandler::KsProcessMediaSamples PinName %s BufferLength %lu Property Buffer %p ExtendedSize %u lActual %u\n", m_PinName, Properties.cbBuffer, Properties.pbBuffer, ExtendedSize, Properties.lActual);
+ swprintf(Buffer, L"CKsInterfaceHandler::KsProcessMediaSamples PinName %s BufferLength %lu Property Buffer %p ExtendedSize %u lActual %u dwSampleFlags %lx\n", m_PinName, Properties.cbBuffer, Properties.pbBuffer, ExtendedSize, Properties.lActual, Properties.dwSampleFlags);
//OutputDebugStringW(Buffer);
CurStreamHeader->Size = sizeof(KSSTREAM_HEADER) + ExtendedSize;
CurStreamHeader = StreamSegment->StreamHeader;
- WCHAR Buffer[100];
- swprintf(Buffer, L"CKsInterfaceHandler::KsCompleteIo PinName %s bOverlapped %u hr %lx\n", m_PinName, bOverlapped, dwError);
- //OutputDebugStringW(Buffer);
-
//iterate through all stream headers
for(Index = 0; Index < StreamSegment->SampleCount; Index++)
{
WCHAR Path[MAX_PATH+sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W)];
/* open device list */
- hList = SetupDiGetClassDevsW(&Category, NULL, NULL, DIGCF_DEVICEINTERFACE /* | DIGCF_PRESENT*/);
+ hList = SetupDiGetClassDevsW(&Category, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (hList == INVALID_HANDLE_VALUE)
{
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
- if (SetupDiEnumDeviceInterfaces(hList, &DeviceInfoData, &Category, 0, &DeviceInterfaceData))
+ if (SetupDiEnumDeviceInterfaces(hList, NULL, &Category, 0, &DeviceInterfaceData))
{
/* setup interface data struct */
DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)Path;
virtual ~CMediaSample(){}
protected:
- LONG m_Ref;
- IMemAllocator * m_Allocator;
+ ULONG m_Flags;
+ ULONG m_TypeFlags;
BYTE * m_Buffer;
- LONG m_BufferSize;
LONG m_ActualLength;
+ LONG m_BufferSize;
+ IMemAllocator * m_Allocator;
+ CMediaSample * m_Next;
REFERENCE_TIME m_StartTime;
REFERENCE_TIME m_StopTime;
- ULONG m_Flags;
- BOOL m_bMediaTimeValid;
LONGLONG m_MediaStart;
LONGLONG m_MediaStop;
+ AM_MEDIA_TYPE * m_MediaType;
+ ULONG m_StreamId;
+
+public:
+ LONG m_Ref;
+
+ BOOL m_bMediaTimeValid;
+
};
CMediaSample::CMediaSample(
IMemAllocator * Allocator,
BYTE * Buffer,
- LONG BufferSize) : m_Ref(0),
- m_Allocator(Allocator),
+ LONG BufferSize) :
+ m_Flags(0),
+ m_TypeFlags(0),
m_Buffer(Buffer),
- m_BufferSize(BufferSize),
m_ActualLength(BufferSize),
+ m_BufferSize(BufferSize),
+ m_Allocator(Allocator),
+ m_Next(0),
m_StartTime(0),
m_StopTime(0),
- m_Flags(0),
- m_bMediaTimeValid(0),
m_MediaStart(0),
- m_MediaStop(0)
+ m_MediaStop(0),
+ m_MediaType(0),
+ m_StreamId(0),
+ m_Ref(0),
+ m_bMediaTimeValid(0)
{
}
STDMETHODCALLTYPE
CMediaSample::GetMediaType(AM_MEDIA_TYPE **ppMediaType)
{
- OutputDebugStringW(L"CMediaSample::GetMediaType NotImplemented\n");
- DebugBreak();
+ OutputDebugStringW(L"CMediaSample::GetMediaType\n");
+
+ if (!m_MediaType)
+ {
+ *ppMediaType = NULL;
+ return S_FALSE;
+ }
+
+ assert(0);
return E_NOTIMPL;
}
m_MemInputPin->GetAllocatorRequirements(&Properties);
//FIXME determine allocator properties
- Properties.cBuffers = 16;
+ Properties.cBuffers = 32;
Properties.cbBuffer = 2048 * 188; //2048 frames * MPEG2 TS Payload size
Properties.cbAlign = 4;
// get input pin allocator
+#if 0
hr = m_MemInputPin->GetAllocator(&m_MemAllocator);
if (SUCCEEDED(hr))
{
if (FAILED(hr))
m_MemAllocator->Release();
}
+#endif
- if (FAILED(hr))
+ if (1)
{
hr = CKsAllocator_Constructor(NULL, IID_IMemAllocator, (void**)&m_MemAllocator);
if (FAILED(hr))
STDMETHODCALLTYPE
COutputPin::QueryPinInfo(PIN_INFO *pInfo)
{
- OutputDebugStringW(L"COutputPin::QueryPinInfo\n");
-
wcscpy(pInfo->achName, m_PinName);
pInfo->dir = PINDIR_OUTPUT;
pInfo->pFilter = m_ParentFilter;
STDMETHODCALLTYPE
COutputPin::QueryDirection(PIN_DIRECTION *pPinDir)
{
- OutputDebugStringW(L"COutputPin::QueryDirection\n");
-
if (pPinDir)
{
*pPinDir = PINDIR_OUTPUT;
STDMETHODCALLTYPE
COutputPin::QueryId(LPWSTR *Id)
{
- OutputDebugStringW(L"COutputPin::QueryId\n");
-
*Id = (LPWSTR)CoTaskMemAlloc((wcslen(m_PinName)+1)*sizeof(WCHAR));
if (!*Id)
return E_OUTOFMEMORY;
AM_MEDIA_TYPE * MediaTypes;
HANDLE hFilter;
- OutputDebugStringW(L"COutputPin::EnumMediaTypes called\n");
-
if (!m_KsObjectParent)
{
// no interface
hr = KsGetMediaTypeCount(hFilter, m_PinId, &MediaTypeCount);
if (FAILED(hr) || !MediaTypeCount)
{
- OutputDebugStringW(L"COutputPin::EnumMediaTypes failed1\n");
return hr;
}
if (!MediaTypes)
{
// not enough memory
- OutputDebugStringW(L"COutputPin::EnumMediaTypes CoTaskMemAlloc\n");
return E_OUTOFMEMORY;
}
{
// failed
CoTaskMemFree(MediaTypes);
- OutputDebugStringW(L"COutputPin::EnumMediaTypes failed\n");
return hr;
}
}
STDMETHODCALLTYPE
COutputPin::QueryInternalConnections(IPin **apPin, ULONG *nPin)
{
- OutputDebugStringW(L"COutputPin::QueryInternalConnections called\n");
return E_NOTIMPL;
}
HRESULT
{
// format is supported
CoTaskMemFree(MultipleItem);
- OutputDebugStringW(L"COutputPin::CheckFormat format OK\n");
return S_OK;
}
DataFormat = (PKSDATAFORMAT)((ULONG_PTR)DataFormat + DataFormat->FormatSize);
if (FAILED(hr))
{
// failed to load interface handler plugin
- OutputDebugStringW(L"COutputPin::CreatePin failed to load InterfaceHandlerPlugin\n");
CoTaskMemFree(MediumList);
CoTaskMemFree(InterfaceList);
if (FAILED(hr))
{
// failed to load interface handler plugin
- OutputDebugStringW(L"COutputPin::CreatePin failed to initialize InterfaceHandlerPlugin\n");
InterfaceHandler->Release();
CoTaskMemFree(MediumList);
CoTaskMemFree(InterfaceList);
if (FAILED(hr))
{
+ OutputDebugStringW(L"OutOfSamples\n");
m_Pin->BeginFlush();
- OutputDebugStringW(L"Beginning flushing...\n");
WaitForSingleObject(m_hBufferAvailable, INFINITE);
m_Pin->EndFlush();
+ OutputDebugStringW(L"After Wait OutOfSamples\n");
// now retry again
continue;
}
SampleCount = 1;
Samples[0] = Sample;
-
- Sample->SetTime(&Start, &Stop);
+ Sample->SetTime(NULL, NULL);
hr = m_InterfaceHandler->KsProcessMediaSamples(NULL, /* FIXME */
Samples,
&SampleCount,
&StreamSegment);
if (FAILED(hr) || !StreamSegment)
{
- swprintf(Buffer, L"COutputPin::IoProcessRoutine KsProcessMediaSamples PinName %s hr %lx StreamSegment %p\n", m_PinName, hr, StreamSegment);
+ swprintf(Buffer, L"COutputPin::IoProcessRoutine KsProcessMediaSamples FAILED PinName %s hr %lx\n", m_PinName, hr);
OutputDebugStringW(Buffer);
- break;
+ SetEvent(m_hStopEvent);
+ m_IoThreadStarted = false;
+ ExitThread(0);
}
// get completion event
if (SUCCEEDED(hr))
{
- Sample->GetTime(&Start, &Stop);
+ LONG Length = Sample->GetActualDataLength();
+ Stop += Length;
+ // Sample->SetMediaTime(&Start, &Stop);
Start = Stop;
- Stop++;
// now deliver the sample
hr = m_MemInputPin->Receive(Sample);
- swprintf(Buffer, L"COutputPin::IoProcessRoutine IMemInputPin::Receive hr %lx Start %I64u Stop %I64u\n", hr, Start, Stop);
+ swprintf(Buffer, L"COutputPin::IoProcessRoutine PinName %s IMemInputPin::Receive hr %lx Sample %p m_MemAllocator %p\n", m_PinName, hr, Sample, m_MemAllocator);
OutputDebugStringW(Buffer);
+ if (FAILED(hr))
+ DebugBreak();
+ Sample = NULL;
}
}while(TRUE);
if (FAILED(hr))
return hr;
+ // release any waiting threads
+ SetEvent(pPin->m_hBufferAvailable);
+
// wait until i/o thread is done
WaitForSingleObject(pPin->m_hStopEvent, INFINITE);
#include <stdio.h>
#include <vector>
#include <stack>
+#include <list>
#include <assert.h>
#include <ksmedia.h>
//#include <debug.h>