123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- // CutFile.cpp : implementation file
- //
- #include "stdafx.h"
- #include "StoneU_HC_CARDOCX.h"
- #include "CutFile.h"
- #include <Shlwapi.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #pragma comment(lib, "Shlwapi.lib")
- extern UINT PORT;
- /////////////////////////////////////////////////////////////////////////////
- // CCutFile dialog
- CCutFile::CCutFile(CWnd* pParent /*=NULL*/)
- : CDialog(CCutFile::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CCutFile)
- m_nBegin = 0;
- m_nType = 0;
- m_nEnd = 0;
- m_csSaveFileName = _T("D:\\Clip.mp4");
- //}}AFX_DATA_INIT
- }
- void CCutFile::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCutFile)
- DDX_Text(pDX, IDC_BEGIN, m_nBegin);
- DDX_Radio(pDX, IDC_BYFRAME, m_nType);
- DDX_Text(pDX, IDC_END, m_nEnd);
- DDX_Text(pDX, IDC_FILENAME, m_csSaveFileName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CCutFile, CDialog)
- //{{AFX_MSG_MAP(CCutFile)
- // ON_BN_CLICKED(IDC_VIEW, OnView)
- ON_BN_CLICKED(IDC_SAVE, OnSave)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCutFile message handlers
- BOOL CCutFile::OnView()
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- if(!CheckValue())
- {
- MessageBox("Input value over");
- return FALSE;
- }
-
- DWORD nType;
- ZeroMemory(&m_strRealBegin, sizeof(FRAME_POS));
- ZeroMemory(&m_strRealBegin, sizeof(FRAME_POS));
- if(m_nType == 0)
- nType = BY_FRAMENUM;
- else
- {
- nType = BY_FRAMETIME;
- //conver to ms
- m_nBegin *= 1000;
- m_nEnd *= 1000;
- }
-
- //locate the I-Frame .
- NAME(PlayM4_GetKeyFramePos)(PORT, m_nBegin, nType, &m_strRealBegin);
-
- if(!NAME(PlayM4_GetNextKeyFramePos)(PORT, m_nEnd, nType, &m_strRealEnd))
- {
- m_strRealEnd.nFilePos = SetFilePointer(m_hPlayFile, 0, 0, FILE_END);
- m_strRealEnd.nFrameNum = m_nMaxFrameNum;
- m_strRealEnd.nFrameTime = m_nMaxTime*1000;
- }
- DWORD nBegin = 0, nEnd = 0;
- if(nType == BY_FRAMENUM)
- {
- nBegin = m_strRealBegin.nFrameNum;
- nEnd = m_strRealEnd.nFrameNum;
- }
- else if(nType == BY_FRAMETIME)
- {
- nBegin = m_strRealBegin.nFrameTime / 1000;
- nEnd = m_strRealEnd.nFrameTime / 1000;
- }
- else
- {
- MessageBox("Type is unknown");
- return FALSE;
- }
- CString csReal;
- csReal.Format("%d", nBegin);
- GetDlgItem(IDC_REAL_BEGIN)->SetWindowText(csReal);
- csReal.Format("%d", nEnd);
- GetDlgItem(IDC_REAL_END)->SetWindowText(csReal);
- return TRUE;
- }
- void CCutFile::OnSave()
- {
- // TODO: Add your control notification handler code here
- if(!OnView())
- return;
-
- int nNewFileLen = int(m_strRealEnd.nFilePos - m_strRealBegin.nFilePos);
- if(nNewFileLen <= 0)
- {
- MessageBox("Stop position less than start position");
- return;
- }
- //UpdateData(TRUE);
- //Copy file
- HANDLE hNewFile = INVALID_HANDLE_VALUE;
- DWORD nSize = 2048; //Per 2k data;
- PBYTE pBuf = NULL;
- DWORD nFileHeadLen = NAME(PlayM4_GetFileHeadLength)();
- DWORD nRet;
- DWORD nBlock = nNewFileLen / nSize;
- DWORD nRemain = nNewFileLen % nSize;
- DWORD nCount;
- try
- {
- if(PathFileExists(m_csSaveFileName))
- {
- int nVal = MessageBox("The file already exists, are u sure of covering it now?", "Warning", MB_YESNO | MB_ICONQUESTION);
- if(nVal == IDNO)
- {
- throw 0;
- }
- }
- hNewFile = CreateFile(
- m_csSaveFileName,
- GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- NULL );
- if(hNewFile == INVALID_HANDLE_VALUE)
- {
- MessageBox("Create saving file faild");
- throw 0;
- }
-
- pBuf = new BYTE [nSize];
- if(NULL == pBuf)
- {
- MessageBox("Alloc memory failed!");
- throw 0;
- }
- //file header
- SetFilePointer(m_hPlayFile, 0, 0, FILE_BEGIN);
- if(!ReadFile(m_hPlayFile, pBuf, nFileHeadLen, &nRet, NULL))
- {
- MessageBox("Read file faild!");
- throw 0;
- }
- else
- {
- if(!WriteFile(hNewFile, pBuf, nFileHeadLen, &nRet, NULL))
- {
- MessageBox("Write file faild!");
- throw 0;
- }
- }
- SetFilePointer(m_hPlayFile, m_strRealBegin.nFilePos, 0, FILE_BEGIN);
-
- for(nCount = 0; nCount < nBlock; nCount ++)
- {
- if(!ReadFile(m_hPlayFile, pBuf, nSize, &nRet, NULL))
- {
- MessageBox("Read file faild");
- break;
- }
- if(!WriteFile(hNewFile, pBuf, nSize, &nRet, NULL))
- {
- MessageBox("Write file faild");
- break;
- }
- }
- if(!ReadFile(m_hPlayFile, pBuf, nRemain, &nRet, NULL))
- {
- MessageBox("Read file faild");
- }
- else
- {
- if(!WriteFile(hNewFile, pBuf, nRemain, &nRet, NULL))
- MessageBox("Write file faild");
- }
- }
-
- catch(...)
- {
- }
- if(pBuf != NULL)
- {
- delete []pBuf;
- pBuf = NULL;
- }
- if(hNewFile != INVALID_HANDLE_VALUE)
- {
- CloseHandle(hNewFile);
- hNewFile = INVALID_HANDLE_VALUE;
- }
- }
- void CCutFile::OnOK()
- {
- // TODO: Add extra validation here
- CloseHandle(m_hPlayFile);
- CDialog::OnOK();
- }
- BOOL CCutFile::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // TODO: Add extra initialization here
- m_nMaxTime = NAME(PlayM4_GetFileTime)(PORT);
- m_nMaxFrameNum = NAME(PlayM4_GetFileTotalFrames)(PORT);
- CString csRange;
- csRange.Format("Ö¡ºÅ·¶Î§(Ö¡): %d~%d\r\nʱ¼ä·¶Î§(Ãë): %d~%d\r\n", 0, m_nMaxFrameNum, 0, m_nMaxTime);
- GetDlgItem(IDC_RANGE)->SetWindowText(csRange);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- BOOL CCutFile::CheckValue()
- {
- DWORD nMaxValue;
- if(m_nType == 0)
- nMaxValue = m_nMaxFrameNum;
- else
- nMaxValue = m_nMaxTime;
- if( (m_nBegin > nMaxValue) || (m_nEnd > nMaxValue) )
- return FALSE;
- return TRUE;
- }
- BOOL CCutFile::SetFileName(CString csName)
- {
- m_csPlayFileName = csName;
- m_hPlayFile = CreateFile(
- csName,
- GENERIC_READ,
- FILE_SHARE_READ|FILE_SHARE_WRITE,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- if(m_hPlayFile == INVALID_HANDLE_VALUE)
- {
- MessageBox("Open file faild");
- return FALSE;
- }
- return TRUE;
- }
|