diehardii
5th March 2004, 20:29
Hi, I was hoping that someone with directshow experience could give me a hand. Hopefully, I'm not too off topic. Here is my post from another forum. If you're poking around Gabest, its your code (any help would be appreciated:D )
~Steve
Hi, this seemed like the most appropriate forum. I am connecting to the ROT and grabbing the graph of an exposed process. From this, I am querying the graph play state. I am threading PlayerState and calling GetFilterGraph from PlayerState. I am very new to the ATL stuff and it seems as if I am not releasing the graph once I get a hold of it. Even after the thread ends, the graph stays alive (despite the program trying to end it). If anyone can tell me how to release the graph when I am finished with it, I would appreciate it. Also, I thought ATL cleaned itself up once it went out of scope. Is this not true? Thanks.
~Steve
static bool GetFilterGraph(IFilterGraph** ppFG)
{
if(!ppFG) return(false);
IRunningObjectTable *pROT;
GetRunningObjectTable(0, &pROT);
CComPtr<IEnumMoniker> pEM;
if(FAILED(pROT->EnumRunning(&pEM)))
{
return 1;
}
CComPtr<IBindCtx> pBindCtx;
CreateBindCtx(0, &pBindCtx);
for(CComPtr<IMoniker> pMoniker; S_OK == pEM->Next(1, &pMoniker, NULL); pMoniker = NULL)
{
LPOLESTR pDispName = NULL;
if(FAILED(pMoniker->GetDisplayName(pBindCtx, NULL, &pDispName)))
continue;
CAtlStringW strw(pDispName);
CComPtr<IMalloc> pMalloc;
if(FAILED(CoGetMalloc(1, &pMalloc)))
continue;
pMalloc->Free(pDispName);
if(strw.Find(L"(MPC)") < 0)
continue;
CComPtr<IUnknown> pUnk;
if(S_OK != pROT->GetObject(pMoniker, &pUnk))
continue;
CComQIPtr<IFilterGraph> pFG = pUnk;
if(!pFG)
continue;
*ppFG = pFG.Detach();
break;
}
pROT->Release();
return(!!*ppFG);
}
unsigned _stdcall PlayerState(void* pArguments)
{
FILE *logfile;
logfile=fopen("c:\\threadfile.txt","at");
fprintf(logfile,"Thread Executing\n");
CoInitialize(NULL);
CComPtr<IFilterGraph> pFG;
CComQIPtr<IMediaControl> pMC;
OAFilterState fs;
while (g_stop == false)
{
if(GetFilterGraph(&pFG))
{
if(!GetFilterGraph(&pFG) || !(pMC = pFG))
{
fprintf(logfile, "Thread error");
}
if (SUCCEEDED(pMC->GetState(0, &fs)))
{
if (fs == State_Stopped)
{
fprintf(logfile, "State - Stopped\n");
this_Playerstate = MYHTPC_PLAY_STATE_STOPPED;
}
if (fs == State_Paused)
{
this_Playerstate = MYHTPC_PLAY_STATE_PAUSED;
}
if (fs == State_Running)
{
fprintf(logfile, "State - Playing\n");
this_Playerstate = MYHTPC_PLAY_STATE_PLAYING;
}
}
}
else
{
this_Playerstate = MYHTPC_PLAY_STATE_STOPPED;
}
Sleep(500);
}
pMC.Release();
pFG.Release();
fprintf(logfile,"Ending Thread *************************************\n");
fclose(logfile);
CoUninitialize();
return 0;
}
~Steve
Hi, this seemed like the most appropriate forum. I am connecting to the ROT and grabbing the graph of an exposed process. From this, I am querying the graph play state. I am threading PlayerState and calling GetFilterGraph from PlayerState. I am very new to the ATL stuff and it seems as if I am not releasing the graph once I get a hold of it. Even after the thread ends, the graph stays alive (despite the program trying to end it). If anyone can tell me how to release the graph when I am finished with it, I would appreciate it. Also, I thought ATL cleaned itself up once it went out of scope. Is this not true? Thanks.
~Steve
static bool GetFilterGraph(IFilterGraph** ppFG)
{
if(!ppFG) return(false);
IRunningObjectTable *pROT;
GetRunningObjectTable(0, &pROT);
CComPtr<IEnumMoniker> pEM;
if(FAILED(pROT->EnumRunning(&pEM)))
{
return 1;
}
CComPtr<IBindCtx> pBindCtx;
CreateBindCtx(0, &pBindCtx);
for(CComPtr<IMoniker> pMoniker; S_OK == pEM->Next(1, &pMoniker, NULL); pMoniker = NULL)
{
LPOLESTR pDispName = NULL;
if(FAILED(pMoniker->GetDisplayName(pBindCtx, NULL, &pDispName)))
continue;
CAtlStringW strw(pDispName);
CComPtr<IMalloc> pMalloc;
if(FAILED(CoGetMalloc(1, &pMalloc)))
continue;
pMalloc->Free(pDispName);
if(strw.Find(L"(MPC)") < 0)
continue;
CComPtr<IUnknown> pUnk;
if(S_OK != pROT->GetObject(pMoniker, &pUnk))
continue;
CComQIPtr<IFilterGraph> pFG = pUnk;
if(!pFG)
continue;
*ppFG = pFG.Detach();
break;
}
pROT->Release();
return(!!*ppFG);
}
unsigned _stdcall PlayerState(void* pArguments)
{
FILE *logfile;
logfile=fopen("c:\\threadfile.txt","at");
fprintf(logfile,"Thread Executing\n");
CoInitialize(NULL);
CComPtr<IFilterGraph> pFG;
CComQIPtr<IMediaControl> pMC;
OAFilterState fs;
while (g_stop == false)
{
if(GetFilterGraph(&pFG))
{
if(!GetFilterGraph(&pFG) || !(pMC = pFG))
{
fprintf(logfile, "Thread error");
}
if (SUCCEEDED(pMC->GetState(0, &fs)))
{
if (fs == State_Stopped)
{
fprintf(logfile, "State - Stopped\n");
this_Playerstate = MYHTPC_PLAY_STATE_STOPPED;
}
if (fs == State_Paused)
{
this_Playerstate = MYHTPC_PLAY_STATE_PAUSED;
}
if (fs == State_Running)
{
fprintf(logfile, "State - Playing\n");
this_Playerstate = MYHTPC_PLAY_STATE_PLAYING;
}
}
}
else
{
this_Playerstate = MYHTPC_PLAY_STATE_STOPPED;
}
Sleep(500);
}
pMC.Release();
pFG.Release();
fprintf(logfile,"Ending Thread *************************************\n");
fclose(logfile);
CoUninitialize();
return 0;
}