How to play video file asychronously with chunk of buffer?
- From: mahendra <mahendra@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 9 Sep 2008 03:33:11 -0700
hi,
I am playing video file synchronously on window mobile m 6.0.
I want to play video file asynchronously with chunk of buffer.
Does any have idea about that?
here is my code.
// ChildView.h : interface of the CChildView class
//
#ifndef __CHILDVIEW__
#define __CHILDVIEW__
#include <streams.h> // link against Strmiids.lib for IDD , CLSID
// CChildView window
class CChildView : public CWnd
{
// Construction
public:
CChildView();
// Attributes
public:
// Operations
public:
// Overrides
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
// Implementation
public:
virtual ~CChildView();
// Generated message map functions
protected:
afx_msg void OnPaint();
afx_msg LRESULT OnGraphNotify(WPARAM, LPARAM);
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnVideoPlay();
afx_msg int OnCreate( LPCREATESTRUCT );
afx_msg void OnSize( UINT, int, int);
public:
HRESULT Initialize(LPCTSTR);
void CleanUp();
void ResizeVideoWindow();
private:
IGraphBuilder* m_pGraph;
IBaseFilter* m_pBaseFilter;
IFileSourceFilter* m_pSource;
IAsyncReader* m_pAsyncReader;
IMediaControl* m_pControl;
IMediaEventEx* m_pEvent;
IVideoWindow* m_pVideoWindow; // For full screen window.
public:
afx_msg void OnVideoPause();
afx_msg void OnVideoStop();
afx_msg void OnVideoOpen();
};
#endif // __CHILDVIEW__
// ChildView.cpp : implementation of the CChildView class
//
#include "stdafx.h"
#include "VideoTest1.h"
#include "ChildView.h"
#define WM_GRAPHNOTIFY WM_APP + 1
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CChildView
CChildView::CChildView()
:m_pGraph(NULL),
m_pControl(NULL),
m_pEvent(NULL),
m_pVideoWindow(NULL)
{
}
CChildView::~CChildView()
{
CleanUp();
}
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_SIZE()
ON_COMMAND(ID_VIDEO_PLAY, &CChildView::OnVideoPlay)
ON_MESSAGE(WM_GRAPHNOTIFY, &CChildView::OnGraphNotify)
ON_COMMAND(ID_VIDEO_PAUSE, &CChildView::OnVideoPause)
ON_COMMAND(ID_VIDEO_STOP, &CChildView::OnVideoStop)
ON_COMMAND(ID_VIDEO_OPEN, &CChildView::OnVideoOpen)
END_MESSAGE_MAP()
// CChildView message handlers
BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CWnd::PreCreateWindow(cs))
return FALSE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1),
NULL);
return TRUE;
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
}
LRESULT CChildView::OnGraphNotify(WPARAM wParam, LPARAM lParam)
{
if( m_pEvent == NULL){
return 0;
}
long evCode, param1, param2;
HRESULT hr;
while (hr = m_pEvent->GetEvent(&evCode, ¶m1, ¶m2, 0), SUCCEEDED(hr))
{
hr = m_pEvent->FreeEventParams(evCode, param1, param2);
switch(evCode)
{
case EC_COMPLETE:
case EC_USERABORT:
case EC_ERRORABORT:
{
CleanUp();
break;
}
default:
break;
}
}
return 0;
}
int CChildView::OnCreate( LPCREATESTRUCT lpStruct)
{
if ( CWnd::OnCreate(lpStruct) != 0)
return (-1);
return 0;
}
void CChildView ::ResizeVideoWindow()
{
// resize it.
if ( m_pVideoWindow ){
// resize it.
CRect window_rect;
GetWindowRect(&window_rect);
// caculte new position for video window.
window_rect.top += static_cast<int>(window_rect.Height()*0.05);
window_rect.left += static_cast<int>(window_rect.Width()*0.05);
window_rect.bottom = window_rect.top +
static_cast<int>(window_rect.Height()*0.85);
window_rect.right = window_rect.left +
static_cast<int>(window_rect.Width()*0.85);
m_pVideoWindow->SetWindowPosition(window_rect.left, window_rect.top,
window_rect.Width(), window_rect.Height());
}
}
void CChildView::OnSize( UINT, int, int)
{
ResizeVideoWindow();
}
//
HRESULT CChildView::Initialize(LPCTSTR aFilePath)
{
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)){
// ERROR - Could not initialize COM library;
return(-1);
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&m_pGraph);
if (FAILED(hr)){
// "ERROR - Could not create the Filter Graph Manager.";
CleanUp();
return(-1);
}
hr = m_pGraph->QueryInterface(IID_IMediaControl, (void **)&m_pControl);
if (FAILED(hr)){
// "ERROR - Could not create media control.";
CleanUp();
return(-1);
}
/*
//
hr = CoCreateInstance(CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER,
IID_IBaseFilter, (void **)&m_pBaseFilter);
if (FAILED(hr)){
// ERROR - Could not create media control event.;
CleanUp();
return(-1);
}
// add filter.
hr = m_pGraph->AddFilter(m_pBaseFilter, NULL);
if (FAILED(hr)){
// ERROR - Could not create media control event.;
CleanUp();
return(-1);
}
//
hr = m_pBaseFilter->QueryInterface(IID_IFileSourceFilter, (void
**)&m_pSource);
if (FAILED(hr)){
// ERROR - Could not create media control event.;
CleanUp();
return(-1);
}
*/
hr = m_pGraph->QueryInterface(IID_IMediaEvent, (void **)&m_pEvent);
if (FAILED(hr)){
// ERROR - Could not create media control event.;
CleanUp();
return(-1);
}
// Set the owner window to receive event notices.
m_pEvent->SetNotifyWindow((OAHWND)GetSafeHwnd(), WM_GRAPHNOTIFY, 0);
// Build the graph. IMPORTANT: Change this string to a file on your system.
//hr = m_pGraph->RenderFile(L"\\My Documents\\My Pictures\\VIDEO_001.mp4",
NULL);
hr = m_pGraph->RenderFile(aFilePath, NULL);
//hr = m_pSource->Load(aFilePath, NULL);
if (FAILED(hr)){
// "ERROR - Could not create graph." ;
CleanUp();
return(-1);
}
/*
IEnumPins *pEnum = NULL;
IPin *pPin = NULL;
PIN_DIRECTION PinDirThis = PINDIR_INPUT;
m_pBaseFilter->EnumPins(&pEnum);
while(pEnum->Next(1, &pPin, 0) == S_OK)
{
pPin->QueryDirection(&PinDirThis);
if (PINDIR_OUTPUT == PinDirThis)
{
hr = m_pGraph->Render(pPin);
if (FAILED(hr)){
pPin->Release();
// "ERROR - Could not create graph." ;
CleanUp();
return(-1);
}
}
pPin->Release();
}
*/
// get video window.
hr = m_pGraph->QueryInterface(IID_IVideoWindow, (LPVOID *)&m_pVideoWindow);
if (FAILED(hr)){
// "ERROR - Could not create video window." ;
CleanUp();
return(-1);
}
// set video window owner.
m_pVideoWindow->put_Owner((OAHWND)GetSafeHwnd());
// set video window style.
m_pVideoWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS);
// resize video window.
ResizeVideoWindow();
// sucess.
return 0;
}
void CChildView::CleanUp()
{
if( m_pVideoWindow ){
m_pControl->Stop();
m_pVideoWindow->put_Visible(OAFALSE);
m_pVideoWindow->put_Owner(NULL);
m_pVideoWindow->Release();
m_pVideoWindow = NULL;
}
if( m_pControl){
m_pControl->Release();
m_pControl = NULL;
}
if( m_pEvent ){
// Disable event notification before releasing the graph.
m_pEvent->SetNotifyWindow( NULL, 0 ,0);
m_pEvent->Release();
m_pEvent = NULL;
}
if( m_pGraph ){
m_pGraph->Release();
m_pGraph = NULL;
}
if( m_pBaseFilter ){
m_pBaseFilter->Release();
m_pBaseFilter = NULL;
}
if( m_pSource ){
m_pSource->Release();
m_pSource = NULL;
}
if( m_pAsyncReader ){
m_pAsyncReader->Release();
m_pAsyncReader = NULL;
}
::CoUninitialize();
}
void CChildView::OnVideoPause()
{
// TODO: Add your command handler code here
HRESULT hr = NULL;
hr = m_pControl->Pause();
}
void CChildView::OnVideoStop()
{
// TODO: Add your command handler code here
HRESULT hr = NULL;
hr = m_pControl->Stop();
CleanUp();
}
void CChildView::OnVideoPlay()
{
HRESULT hr = NULL;
if ( m_pControl )
hr = m_pControl->Run();
}
void CChildView::OnVideoOpen()
{
// TODO: Add your command handler code here
// file filter.
CString filter_text = TEXT("Video Files (*.mp4; *.3gp)|*.mp4; *.3gp;||");
// create file open dialog.
CFileDialog file_open_dlg( TRUE, // "open" dialog.
NULL,
NULL,
(OFN_EXPLORER|OFN_OVERWRITEPROMPT),
filter_text,
NULL,
NULL );
// store path.
CString media_file_path;
// check selection.
if( file_open_dlg.DoModal() != IDOK ){
return;
}
// if file selected.
media_file_path = file_open_dlg.GetPathName();
// first do clean up.
CleanUp();
// initialize.
if ( Initialize(media_file_path) != 0){
CleanUp();
return;
}
}
.
- Follow-Ups:
- Re: How to play video file asychronously with chunk of buffer?
- From: Alessandro Angeli
- Re: How to play video file asychronously with chunk of buffer?
- Prev by Date: Timeline Object cannot display high resolution picture
- Next by Date: Error code 0x80040204 when playing
- Previous by thread: Timeline Object cannot display high resolution picture
- Next by thread: Re: How to play video file asychronously with chunk of buffer?
- Index(es):
Relevant Pages
|