- Main Page
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

# mpcinfo.cpp

    00001 /*
    00002  *      Copyright (C) 2003-2005 Gabest
    00003  *      http://www.gabest.org
    00004  *
    00005  *  This Program is free software; you can redistribute it and/or modify
    00006  *  it under the terms of the GNU General Public License as published by
    00007  *  the Free Software Foundation; either version 2, or (at your option)
    00008  *  any later version.
    00009  *
    00010  *  This Program is distributed in the hope that it will be useful,
    00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    00013  *  GNU General Public License for more details.
    00014  *
    00015  *  You should have received a copy of the GNU General Public License
    00016  *  along with GNU Make; see the file COPYING.  If not, write to
    00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
    00018  *  http://www.gnu.org/copyleft/gpl.html
    00019  *
    00020  */
    00021
    00022 // mpcinfo.cpp : Defines the initialization routines for the DLL.
    00023 //
    00024
    00025 #include "stdafx.h"
    00026 #include "mpcinfo.h"
    00027
    00028 #ifdef _DEBUG
    00029 #define new DEBUG_NEW
    00030 #endif
    00031
    00032 //
    00033 //      Note!
    00034 //
    00035 //              If this DLL is dynamically linked against the MFC
    00036 //              DLLs, any functions exported from this DLL which
    00037 //              call into MFC must have the AFX_MANAGE_STATE macro
    00038 //              added at the very beginning of the function.
    00039 //
    00040 //              For example:
    00041 //
    00042 //              extern "C" BOOL PASCAL EXPORT ExportedFunction()
    00043 //              {
    00044 //                      AFX_MANAGE_STATE(AfxGetStaticModuleState());
    00045 //                      // normal function body here
    00046 //              }
    00047 //
    00048 //              It is very important that this macro appear in each
    00049 //              function, prior to any calls into MFC.  This means that
    00050 //              it must appear as the first statement within the
    00051 //              function, even before any object variable declarations
    00052 //              as their constructors may generate calls into the MFC
    00053 //              DLL.
    00054 //
    00055 //              Please see MFC Technical Notes 33 and 58 for additional
    00056 //              details.
    00057 //
    00058
    00059 // CmpcinfoApp
    00060
    00061 BEGIN_MESSAGE_MAP(CmpcinfoApp, CWinApp)
    00062 END_MESSAGE_MAP()
    00063
    00064
    00065 // CmpcinfoApp construction
    00066
    00067 CmpcinfoApp::CmpcinfoApp()
    00068 {
    00069         // TODO: add construction code here,
    00070         // Place all significant initialization in InitInstance
    00071 }
    00072
    00073
    00074 // The one and only CmpcinfoApp object
    00075
    00076 CmpcinfoApp theApp;
    00077
    00078
    00079 // CmpcinfoApp initialization
    00080
    00081 BOOL CmpcinfoApp::InitInstance()
    00082 {
    00083         CWinApp::InitInstance();
    00084
    00085         return TRUE;
    00086 }
    00087
    00088 #include <dshow.h>
    00089 #include <streams.h>
    00090 #include <atlbase.h>
    00091
    00092 static bool GetFilterGraph(IFilterGraph** ppFG)
    00093 {
    00094         if(!ppFG) return(false);
    00095
    00096     CComPtr<IRunningObjectTable> pROT;
    00097     if(FAILED(GetRunningObjectTable(0, &pROT)))
    00098                 return 1;
    00099
    00100         CComPtr<IEnumMoniker> pEM;
    00101         if(FAILED(pROT->EnumRunning(&pEM)))
    00102                 return 1;
    00103
    00104         CComPtr<IBindCtx> pBindCtx;
    00105         CreateBindCtx(0, &pBindCtx);
    00106
    00107         for(CComPtr<IMoniker> pMoniker; S_OK == pEM->Next(1, &pMoniker, NULL); pMoniker = NULL)
    00108         {
    00109                 LPOLESTR pDispName = NULL;
    00110                 if(FAILED(pMoniker->GetDisplayName(pBindCtx, NULL, &pDispName)))
    00111                         continue;
    00112
    00113                 CStringW strw(pDispName);
    00114
    00115                 CComPtr<IMalloc> pMalloc;
    00116                 if(FAILED(CoGetMalloc(1, &pMalloc)))
    00117                         continue;
    00118                 pMalloc->Free(pDispName);
    00119
    00120                 if(strw.Find(L"(MPC)") < 0)
    00121                         continue;
    00122
    00123                 CComPtr<IUnknown> pUnk;
    00124                 if(S_OK != pROT->GetObject(pMoniker, &pUnk))
    00125                         continue;
    00126
    00127                 CComQIPtr<IFilterGraph> pFG = pUnk;
    00128                 if(!pFG)
    00129                         continue;
    00130
    00131                 *ppFG = pFG.Detach();
    00132
    00133                 break;
    00134         }
    00135
    00136         return(!!*ppFG);
    00137 }
    00138
    00139 extern "C" int WINAPI file(HWND,HWND,char *data,char*,BOOL,BOOL)
    00140 {
    00141         CComPtr<IFilterGraph> pFG;
    00142         if(!GetFilterGraph(&pFG))
    00143                 return 1;
    00144
    00145         CString fn;
    00146
    00147         CComPtr<IEnumFilters> pEF;
    00148         if(FAILED(pFG->EnumFilters(&pEF)))
    00149                 return 1;
    00150
    00151         ULONG cFetched = 0;
    00152         for(CComPtr<IBaseFilter> pBF; S_OK == pEF->Next(1, &pBF, &cFetched); pBF = NULL)
    00153         {
    00154                 if(CComQIPtr<IFileSourceFilter> pFSF = pBF)
    00155                 {
    00156                         LPOLESTR pFileName = NULL;
    00157                         AM_MEDIA_TYPE mt;
    00158                         if(FAILED(pFSF->GetCurFile(&pFileName, &mt)))
    00159                                 continue;
    00160
    00161                         fn = CStringW(pFileName);
    00162
    00163                         CoTaskMemFree(pFileName);
    00164                         FreeMediaType(mt);
    00165
    00166                         break;
    00167                 }
    00168         }
    00169
    00170         if(fn.IsEmpty())
    00171                 return 1;
    00172
    00173         sprintf(data, _T("%s"), fn);
    00174
    00175         return 3;
    00176 }
    00177
    00178 extern "C" int WINAPI size(HWND,HWND,char *data,char*,BOOL,BOOL)
    00179 {
    00180         if(file(0,0,data,0,0,0) != 3)
    00181                 return 1;
    00182
    00183         CString fn = CStringA(data);
    00184         data[0] = 0;
    00185
    00186         HANDLE hFile = CreateFile(fn, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
    00187         if(hFile == INVALID_HANDLE_VALUE)
    00188                 return 1;
    00189
    00190         LARGE_INTEGER size;
    00191         size.QuadPart = 0;
    00192         size.LowPart = GetFileSize(hFile, (DWORD*)&size.HighPart);
    00193
    00194         sprintf(data, _T("%I64d"), size.QuadPart);
    00195
    00196         CloseHandle(hFile);
    00197
    00198         return 3;
    00199 }
    00200
    00201 extern "C" int WINAPI pos(HWND,HWND,char *data,char*,BOOL,BOOL)
    00202 {
    00203         CComPtr<IFilterGraph> pFG;
    00204         if(!GetFilterGraph(&pFG))
    00205                 return 1;
    00206
    00207         CComQIPtr<IMediaSeeking> pMS = pFG;
    00208         REFERENCE_TIME pos, dur;
    00209         if(FAILED(pMS->GetCurrentPosition(&pos)) || FAILED(pMS->GetDuration(&dur)))
    00210                 return 1;
    00211
    00212         if(dur > 10000000i64*60*60)
    00213         {
    00214                 sprintf(data, _T("%02d:%02d:%02d/%02d:%02d:%02d"),
    00215                         (int)(pos/10000000/60/60), (int)(pos/10000000/60)%60, (int)(pos/10000000)%60,
    00216                         (int)(dur/10000000/60/60), (int)(dur/10000000/60)%60, (int)(dur/10000000)%60);
    00217         }
    00218         else
    00219         {
    00220                 sprintf(data, _T("%02d:%02d/%02d:%02d"),
    00221                         (int)(pos/10000000/60)%60, (int)(pos/10000000)%60,
    00222                         (int)(dur/10000000/60)%60, (int)(dur/10000000)%60);
    00223         }
    00224
    00225         return 3;
    00226 }
    00227
    00228 extern "C" int WINAPI info(HWND,HWND,char *data,char*,BOOL,BOOL)
    00229 {
    00230         CStringA ret;
    00231         if(file(0,0,data,0,0,0)!=3) return 1;
    00232         ret += data;
    00233         ret += ", ";
    00234         if(size(0,0,data,0,0,0)!=3) return 1;
    00235         ret += data;
    00236         ret += ", ";
    00237         if(pos(0,0,data,0,0,0)!=3) return 1;
    00238         ret += data;
    00239
    00240         strcpy(data, ret);
    00241
    00242         return 3;
    00243 }
    00244
    00245 extern "C" int WINAPI stopped(HWND,HWND,char *data,char*,BOOL,BOOL)
    00246 {
    00247         sprintf(data, _T("2"));
    00248
    00249         CComPtr<IFilterGraph> pFG;
    00250         CComQIPtr<IMediaControl> pMC;
    00251         OAFilterState fs;
    00252         if(!GetFilterGraph(&pFG) || !(pMC = pFG) || FAILED(pMC->GetState(0, &fs)))
    00253                 return 3;
    00254
    00255         sprintf(data, _T("%d"), fs == State_Stopped ? 1 : 0);
    00256
    00257         return 3;
    00258 }
    00259
    00260 extern "C" int WINAPI paused(HWND,HWND,char *data,char*,BOOL,BOOL)
    00261 {
    00262         sprintf(data, _T("2"));
    00263
    00264         CComPtr<IFilterGraph> pFG;
    00265         CComQIPtr<IMediaControl> pMC;
    00266         OAFilterState fs;
    00267         if(!GetFilterGraph(&pFG) || !(pMC = pFG) || FAILED(pMC->GetState(0, &fs)))
    00268                 return 3;
    00269
    00270         sprintf(data, _T("%d"), fs == State_Paused ? 1 : 0);
    00271
    00272         return 3;
    00273 }
    00274
    00275 extern "C" int WINAPI running(HWND,HWND,char *data,char*,BOOL,BOOL)
    00276 {
    00277         sprintf(data, _T("2"));
    00278
    00279         CComPtr<IFilterGraph> pFG;
    00280         CComQIPtr<IMediaControl> pMC;
    00281         OAFilterState fs;
    00282         if(!GetFilterGraph(&pFG) || !(pMC = pFG) || FAILED(pMC->GetState(0, &fs)))
    00283                 return 3;
    00284
    00285         sprintf(data, _T("%d"), fs == State_Running ? 1 : 0);
    00286
    00287         return 3;
    00288 }