MAV8Dlg.cpp
资源名称:VC++视频传输.rar [点击查看]
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:15k
源码类别:
VC书籍
开发平台:
Visual C++
- // MAV8Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MAV8.h"
- #include <windows.h>
- #include <mmsystem.h>
- #include ".av8incav8api.h"
- #include ".av8incdefine.h"
- #include "Overlay.h"
- #include "PlayWnd.h"
- #include "MAV8Dlg.h"
- #include "PlayWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define WM_DATABLOCK_UPDATE WM_USER+20
- static char caFileName[STRSIZE];// = "catest1.mpg";
- typedef WORD (CALLBACK *USER_CALLBACK) (HDRVR hdrvr, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2);
- WORD CALLBACK _loadds MyCallBack(HDRVR hdrvr, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2);
- static HMMIO hmmioInput;
- static HMMIO hmmioOutput;
- HDRVR hVFDrv;
- BYTE bStream;
- HWND hWnd;
- BOOL Ready = false;
- extern PBYTE pbMem[];
- extern BOOL Rendered;
- PBYTE RWPtr;
- PBYTE tmpMem;
- DWORD m_ReceivedBytes = 0; // maximize = 0x8000
- DWORD m_RemainBytes = 0; // maximize = 0x8000
- int rwIndex = 0;
- PBYTE Ptr[100];
- BOOL CaptureToFile;
- extern BOOL PeekAndPump();
- void Setdata (LPSTR lpBuffer, LONG dwBufferWrite)
- {
- DWORD Bytes = (DWORD)(dwBufferWrite);
- if ((m_ReceivedBytes + Bytes )> 0x8000) {
- CopyMemory((PVOID)(Ptr[rwIndex]+(LONGLONG)m_ReceivedBytes),
- (PVOID)(lpBuffer),
- (DWORD)(Bytes = 0x8000 - m_ReceivedBytes));
- m_RemainBytes = dwBufferWrite - Bytes;
- RWPtr = Ptr[rwIndex];
- rwIndex = (rwIndex + 1)%100;
- Ready = true;
- tmpMem = new BYTE[m_RemainBytes];
- CopyMemory((PVOID)(tmpMem),
- (PVOID)(lpBuffer + Bytes),
- (DWORD)(m_RemainBytes));
- m_ReceivedBytes = 0;
- TRACE2 ("A Block Ready = %lX, %lXn",(DWORD)(lpBuffer), Bytes);
- } else {
- if (m_RemainBytes != 0) {
- CopyMemory((PVOID)(Ptr[rwIndex]+(LONGLONG)m_ReceivedBytes),
- (PVOID)(tmpMem),
- (DWORD)(m_RemainBytes));
- m_ReceivedBytes += m_RemainBytes;
- delete tmpMem;
- m_RemainBytes = 0;
- }
- CopyMemory((PVOID)(Ptr[rwIndex]+(LONGLONG)m_ReceivedBytes),
- (PVOID)(lpBuffer),
- Bytes);
- m_ReceivedBytes += Bytes;
- }
- }
- WORD CALLBACK _loadds MyCallBack(HDRVR hdrvr, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
- {
- DWORD dwFlags = 0;
- // TRACE0("MyCallBack ");
- switch (msg)
- {
- case VF_MSGUSER_BUF_WRITE:
- {
- LONG lRet;
- LPVF_BUFWRITE_STRUCT lpBufWrite = (LPVF_BUFWRITE_STRUCT)dwParam1;
- Setdata ((LPSTR)lpBufWrite->lpBuffer, (LONG)lpBufWrite->dwBufferWrite);
- TRACE0 ("Sentn");
- if (CaptureToFile) {
- lRet = mmioWrite(hmmioOutput, (LPSTR)lpBufWrite->lpBuffer, (LONG)lpBufWrite->dwBufferWrite);
- if (lRet == -1L)
- {
- return (FALSE);
- }
- }
- break;
- }
- case VF_MSGUSER_BUF_CREATE:
- {
- LPVF_CALLBACK_STRUCT lpCallback = (LPVF_CALLBACK_STRUCT)dwParam1;
- if (CaptureToFile) {
- if (lpCallback->wSubMsg == VF_SUBMSG_OUTPUT)
- {
- dwFlags = MMIO_CREATE | MMIO_WRITE;
- hmmioOutput = mmioOpen((LPTSTR)caFileName, (LPMMIOINFO)NULL, dwFlags);
- }
- else
- {
- dwFlags = MMIO_READ | MMIO_DENYWRITE;
- hmmioInput = mmioOpen((LPTSTR)caFileName, (LPMMIOINFO)NULL, dwFlags);
- }
- }
- TRACE0 ("Createn");
- break;
- }
- case VF_MSGUSER_BUF_CLOSE:
- {
- LPVF_CALLBACK_STRUCT lpCallback = (LPVF_CALLBACK_STRUCT)dwParam1;
- if (CaptureToFile) {
- if (lpCallback->wSubMsg == VF_SUBMSG_OUTPUT)
- {
- if (hmmioOutput)
- {
- mmioClose(hmmioOutput, NULL);
- hmmioOutput = NULL;
- }
- }
- else
- {
- if (hmmioInput)
- {
- mmioClose(hmmioInput, NULL);
- hmmioInput = NULL;
- }
- }
- }
- TRACE0 ("Closedn");
- break;
- }
- } // switch
- return 1;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- void CMAV8Dlg::CountDevice ()
- {
- CString Msg;
- hVFDrv = OpenDriver (L"AV8api.dll", NULL, NULL);
- if (hVFDrv) {
- if (HVFAssign (hVFDrv, 0)) {
- if (HVFLock (hVFDrv, VF_CAP_ALL)) {
- pbDeviceLocked = TRUE;
- }
- else {
- pbDeviceLocked= FALSE;
- }
- }
- else {
- pbDeviceLocked = FALSE;
- CloseDriver (hVFDrv,NULL,NULL);
- hVFDrv = 0;
- }
- }
- GetDlgItem (IDC_CHECK1)->EnableWindow(pbDeviceLocked);
- GetDlgItem (IDC_RECORD)->EnableWindow(false);
- }
- void CMAV8Dlg::CloseAllDevice ()
- {
- if (hVFDrv) {
- HVFUnlock (hVFDrv, VF_CAP_ALL);
- CloseDriver (hVFDrv, NULL, NULL);
- hVFDrv = NULL;
- }
- }
- BOOL CMAV8Dlg::InitEncodeVideoVxD(HDRVR m_hVFDrv, BYTE m_bStream)
- {
- DWORD dwValue;
- WORD wWidth, wHeight;
- wWidth=352;
- wHeight=288;
- dwValue=MAKELONG(wWidth, wHeight);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_OUTPUTSIZE, dwValue);
- /* SDL - also set video width and height to scale images */
- wWidth = (wWidth * 45) / 44; // try to maintain standard MPEG SIF aspect ratio
- dwValue = MAKELONG(wWidth, wHeight /* [sic] */);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_INPUTSIZE, dwValue);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_BITRATE, 512000); //BITRATE_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_RATE, VF_FLAG_VID_25);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_IINTERVAL, IINTERVAL_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_BINTERVAL, PINTERVAL_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_MODE, VF_FLAG_VID_PAL);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_SOURCE, VF_FLAG_VID_COMPOSITE);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_ALGORITHM, VF_FLAG_VID_MPEG);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_BRIGHTNESS, BRIGHTNESS_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_CONTRAST, CONTRAST_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_HUE, HUE_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_SATURATION, SATURATION_DEFAULT);
- return TRUE;
- }
- BOOL CMAV8Dlg::InitEncodeAudioVxD(HDRVR m_hVFDrv, BYTE m_bStream)
- {
- DWORD dwValue;
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_SAMPLE, SAMPLE_RATE_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_BITRATE, BIT_RATE_DEFAULT);
- dwValue=VF_FLAG_AUD_MPEG; // set to MPEG algorithm
- dwValue=(dwValue<<16)+VF_FLAG_AUD_NONE;
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_ALGORITHM, dwValue);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_MODE, MODE_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_VOLUME, VOLUME_MIN);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_VOLUME, VOLUME_DEFAULT);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_GAIN, GAIN_DEFAULT);
- return TRUE;
- }
- BOOL CMAV8Dlg::InitEncodeSystemVxD(HDRVR m_hVFDrv, BYTE m_bStream)
- {
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_STM_TYPE, VF_FLAG_MPGSYS);
- HVFSet(m_hVFDrv, m_bStream, VF_INDEX_STM_ENCODEMODE, VF_FLAG_STM_NORMAL);
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMAV8Dlg dialog
- CMAV8Dlg::CMAV8Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMAV8Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMAV8Dlg)
- m_InputWnd = TRUE;
- m_ToFile = FALSE;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- pOverlayWnd=NULL;
- IsCapture = FALSE;
- hVFDrv = 0;
- pPlayWnd = NULL;
- }
- CMAV8Dlg::~CMAV8Dlg()
- {
- if(pOverlayWnd)
- delete pOverlayWnd;
- pOverlayWnd = NULL;
- IsCapture = FALSE;
- hVFDrv = 0;
- }
- void CMAV8Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMAV8Dlg)
- DDX_Control(pDX, IDC_CHECK1, m_Check1);
- DDX_Check(pDX, IDC_INWINDOW, m_InputWnd);
- DDX_Check(pDX, IDC_TOFILE, m_ToFile);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMAV8Dlg, CDialog)
- //{{AFX_MSG_MAP(CMAV8Dlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
- ON_BN_CLICKED(IDC_RECORD, OnRecstop1)
- ON_BN_CLICKED(IDC_TOFILE, OnTofile)
- ON_BN_CLICKED(IDC_STOP, OnRecstop1)
- ON_BN_CLICKED(IDC_INWINDOW, OnInwindow)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMAV8Dlg message handlers
- BOOL CMAV8Dlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- CountDevice ();
- GetDlgItem (IDC_EDIT1)->SetWindowText ("c:\temp1.mpg");
- GetDlgItem (IDC_EDIT1)->EnableWindow (m_ToFile);
- GetDlgItem (IDC_STOP)->EnableWindow (IsCapture);
- CaptureToFile = m_ToFile;
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMAV8Dlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CMAV8Dlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CMAV8Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CMAV8Dlg::OnDestroy()
- {
- CDialog::OnDestroy();
- CloseAllDevice ();
- }
- void CMAV8Dlg::CreateVideoWindow ()
- {
- int nXDelta;
- int nXScreenSize, nXNew;
- CRect RectDlg;
- GetWindowRect(RectDlg);
- // readjust the control window location
- nXScreenSize=::GetSystemMetrics(SM_CXSCREEN);
- nXNew=nXScreenSize/2+50;
- nXDelta=nXNew-RectDlg.left;
- RectDlg.left=nXNew;
- RectDlg.right=RectDlg.right+nXDelta;
- MoveWindow(&RectDlg);
- // calcuate the overlay window size and location
- WORD wOverlayWidth;
- WORD wOverlayHeight;
- wOverlayWidth = 352;
- wOverlayHeight = 288;
- RectDlg.left=RectDlg.left-wOverlayWidth-10;
- RectDlg.right=RectDlg.left + wOverlayWidth + GetSystemMetrics(SM_CXFRAME)+5;
- RectDlg.bottom=RectDlg.top+ wOverlayHeight + 10+
- GetSystemMetrics(SM_CYFRAME)+
- //GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYMENU);
- GetSystemMetrics(SM_CYCAPTION); // test
- pOverlayWnd=new COverlay(hVFDrv, RectDlg, wOverlayWidth, wOverlayHeight);
- pOverlayWnd->ShowWindow(SW_SHOWNORMAL);
- pOverlayWnd->UpdateWindow();
- InitEncodeVideoVxD(hVFDrv, 0);
- InitEncodeAudioVxD(hVFDrv, 0);
- InitEncodeSystemVxD(hVFDrv, 0);
- }
- void CMAV8Dlg::RecordOrStop (int id, CString fn)
- {
- WORD flags;
- if (IsCapture) {
- // Stop capture
- HVFStop(hVFDrv, bStream);
- TRACE0 ("HVFStopn");
- if (id == 0) {
- if (pPlayWnd) {
- pPlayWnd->m_pimc->StopWhenReady();
- TRACE0 ("StopWhenReadyn");
- }
- }
- HVFClose(hVFDrv, bStream);
- TRACE0 ("HVFClosen");
- bStream = 0;
- IsCapture = FALSE;
- }
- else {
- // Start Record
- IsCapture = TRUE;
- flags = VF_FLAG_MPEG | VF_FLAG_ENCODE | VF_FLAG_OUTBUF;
- lstrcpy (caFileName, fn.GetBuffer (MAX_PATH));
- bStream = static_cast<BYTE> (HVFOpen(hVFDrv,
- flags,
- reinterpret_cast<unsigned long>(MyCallBack)));
- fn.ReleaseBuffer ();
- InitEncodeVideoVxD(hVFDrv, bStream);
- InitEncodeAudioVxD(hVFDrv, bStream);
- InitEncodeSystemVxD(hVFDrv, bStream);
- if (id == 0) {
- for (int i = 0; i< 100; i++)
- Ptr[i] = pbMem[i];
- rwIndex = 0;
- Ready = false;
- }
- HVFRecord(hVFDrv, bStream, NULL, NULL);
- if (id == 0) {
- pPlayWnd->Render ();
- }
- }
- GetDlgItem (IDC_STOP)->EnableWindow (IsCapture);
- GetDlgItem (IDC_INWINDOW)->EnableWindow (!IsCapture);
- GetDlgItem (IDC_TOFILE)->EnableWindow (!IsCapture);
- }
- void CMAV8Dlg::OnCheck1()
- {
- if (m_Check1.GetCheck () == 1) {
- if (m_InputWnd && !pOverlayWnd)
- CreateVideoWindow ();
- GetDlgItem (IDC_RECORD)->EnableWindow (TRUE);
- pPlayWnd = new CPlayWnd (); ///
- hWnd = pPlayWnd->GetSafeHwnd ();
- pPlayWnd->ShowWindow (SW_SHOW); ///
- pPlayWnd->UpdateWindow(); ///
- }
- else {
- delete pOverlayWnd;
- pOverlayWnd = NULL;
- delete pPlayWnd;
- pPlayWnd= NULL;
- GetDlgItem (IDC_RECORD)->EnableWindow (FALSE);
- GetDlgItem (IDC_STOP)->EnableWindow (FALSE);
- }
- }
- void CMAV8Dlg::OnRecstop1()
- {
- CString fn;
- GetDlgItem (IDC_EDIT1)->GetWindowText (fn);
- RecordOrStop (0, fn);
- }
- void CMAV8Dlg::OnTofile()
- {
- UpdateData (TRUE);
- CaptureToFile = m_ToFile;
- GetDlgItem (IDC_EDIT1)->EnableWindow (m_ToFile);
- }
- void CMAV8Dlg::OnInwindow()
- {
- UpdateData (TRUE);
- if (m_InputWnd) {
- if(!pOverlayWnd) {
- CreateVideoWindow ();
- }
- } else {
- if(pOverlayWnd)
- delete pOverlayWnd;
- pOverlayWnd = NULL;
- }
- }