3,881
社区成员




//头文件
#include "Ra_Video.h"
//**********************************
//构造函数
Video::Video()
{
}
//设置
void Video::Setvideo(string filename,HWND hwnd,bool isfull)
{
//设置可用
this->avilable = false;
//文件名
this->name = filename;
//窗口句柄
this->windowshwnd = hwnd;
//是否全屏
this->isfullscreen = isfull;
//设置COM
CoInitialize(0);
//创建滤波器
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&g_graph_builder);
//设置控制接口
g_graph_builder->QueryInterface(IID_IMediaControl, (void**)&g_media_control);
//设置位置接口
g_graph_builder->QueryInterface(IID_IMediaPosition,(void**)&g_media_position);
//设置事件接口
g_graph_builder->QueryInterface(IID_IMediaEvent, (void**)&g_media_event);
//添加VRM
InitWindowlessVMR(windowshwnd,g_graph_builder, &g_pWc);
long lWidth, lHeight;
//获得本地视频大小
g_pWc->GetNativeVideoSize(&lWidth, &lHeight, NULL, NULL);
// 设置Source尺寸
SetRect(&rcSrc, 0, 0, lWidth, lHeight);
// 获得显示窗体的客户区尺寸
GetClientRect(windowshwnd, &rcDest);
//设置destination尺寸
SetRect(&rcDest, 0, 0, rcDest.right, rcDest.bottom);
// 视频定位
g_pWc->SetVideoPosition(&rcSrc,&rcDest);
//设定位置
g_media_position->put_CurrentPosition(0);
}
//播放
bool Video::Load()
{
WCHAR w_filename[MAX_PATH] = {0};
mbstowcs(w_filename, name.c_str(), MAX_PATH);
// render the file
g_graph_builder->RenderFile(w_filename, NULL);
// play the file, switches the entire filter graph into a running state.
this->g_media_control->Run();
return TRUE;
}
//删除
bool Video::Delete()
{
g_media_control->Stop();
g_media_event->Release();
g_media_event = NULL;
g_media_position->Release();
g_media_position = NULL;
g_media_control->Release();
g_media_control = NULL;
CoUninitialize();
return true;
}
//检查是否完成
bool Video::Iscomplete()
{
long event_code, param1, param2;
// retrieves the next notification event
if(SUCCEEDED(this->g_media_event->GetEvent(&event_code, ¶m1, ¶m2, 1)))
{
if(event_code == EC_COMPLETE)
{
// frees resources associated with the parameters of an events.
g_media_event->FreeEventParams(event_code, param1, param2);
return true;
}
}
g_media_event->FreeEventParams(event_code, param1, param2);
return false;
}
//添加VMR
HRESULT Video::InitWindowlessVMR(HWND hwndApp,IGraphBuilder* pGraph,IVMRWindowlessControl** ppWc)
{
//if (!pGraph || !ppWc) return E_POINTER;
IBaseFilter* pVmr = NULL;
IVMRWindowlessControl* pWc = NULL;
// 创建VMR
CoCreateInstance(CLSID_VideoMixingRenderer, NULL,CLSCTX_INPROC, IID_IBaseFilter, (void**)&pVmr);
// 把VMR添加到过滤器图表中
pGraph->AddFilter(pVmr, L"Video Mixing Renderer");
// 设置显示模式
IVMRFilterConfig* pConfig;
pVmr->QueryInterface(IID_IVMRFilterConfig, (void**)&pConfig);
pConfig->SetRenderingMode(VMRMode_Windowless);
pConfig->Release();
// 设置窗体
pVmr->QueryInterface(IID_IVMRWindowlessControl, (void**)&pWc);
pWc->SetVideoClippingWindow(hwndApp);
//返回AddRef指针
*ppWc = pWc;
pVmr->Release();
return NULL;
}
//响应PAINT消息
void Video::Dopaint()
{
PAINTSTRUCT ps;
HDC hdc;
RECT rcClient;
RECT g_rcDest;
GetClientRect(windowshwnd, &rcClient);
hdc = BeginPaint(windowshwnd, &ps);
if (g_pWc != NULL)
{
g_rcDest=rcClient;
// Find the region where the application can paint by subtracting
// the video destination rectangle from the client area.
// (Assume that g_rcDest was calculated previously.)
HRGN rgnClient = CreateRectRgnIndirect(&rcClient);
HRGN rgnVideo = CreateRectRgnIndirect(&g_rcDest);
CombineRgn(rgnClient, rgnClient, rgnVideo, RGN_DIFF);
// Paint on window.
HBRUSH hbr = GetSysColorBrush(COLOR_BTNFACE);
FillRgn(hdc, rgnClient, hbr);
// Clean up.
DeleteObject(hbr);
DeleteObject(rgnClient);
DeleteObject(rgnVideo);
// Request the VMR to paint the video.
HRESULT hr = g_pWc->RepaintVideo(windowshwnd, hdc);
}
else // There is no video, so paint the whole client area.
{
//FillRect(hdc, &rc2, (HBRUSH)(COLOR_BTNFACE + 1));
}
EndPaint(windowshwnd, &ps);
}