backDlg.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:10k
源码类别:

VC书籍

开发平台:

Visual C++

  1. // backDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "back.h"
  5. #include "backDlg.h"
  6. #include "....includeav8api.h"
  7. #include "....includeDefine.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. static CHAR caFileName[STRSIZE] = "temp.mpg";
  14. typedef WORD (CALLBACK *USER_CALLBACK) (HDRVR hdrvr, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2);
  15. WORD CALLBACK _loadds MyCallBack(HDRVR hdrvr, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2);
  16. static HMMIO hmmioInput;
  17. static HMMIO hmmioOutput;
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CAboutDlg dialog used for App About
  20. class CAboutDlg : public CDialog
  21. {
  22. public:
  23. CAboutDlg();
  24. // Dialog Data
  25. //{{AFX_DATA(CAboutDlg)
  26. enum { IDD = IDD_ABOUTBOX };
  27. //}}AFX_DATA
  28. // ClassWizard generated virtual function overrides
  29. //{{AFX_VIRTUAL(CAboutDlg)
  30. protected:
  31. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  32. //}}AFX_VIRTUAL
  33. // Implementation
  34. protected:
  35. //{{AFX_MSG(CAboutDlg)
  36. //}}AFX_MSG
  37. DECLARE_MESSAGE_MAP()
  38. };
  39. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  40. {
  41. //{{AFX_DATA_INIT(CAboutDlg)
  42. //}}AFX_DATA_INIT
  43. }
  44. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  45. {
  46. CDialog::DoDataExchange(pDX);
  47. //{{AFX_DATA_MAP(CAboutDlg)
  48. //}}AFX_DATA_MAP
  49. }
  50. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  51. //{{AFX_MSG_MAP(CAboutDlg)
  52. // No message handlers
  53. //}}AFX_MSG_MAP
  54. END_MESSAGE_MAP()
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CBackDlg dialog
  57. CBackDlg::CBackDlg(CWnd* pParent /*=NULL*/)
  58. : CDialog(CBackDlg::IDD, pParent), m_hVFDrv(0), m_bStream(0)
  59. {
  60. //{{AFX_DATA_INIT(CBackDlg)
  61. // NOTE: the ClassWizard will add member initialization here
  62. //}}AFX_DATA_INIT
  63. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  64. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  65. }
  66. CBackDlg::~CBackDlg()
  67. {
  68.     // make sure that on destruct, OnStop() is called to 
  69.     // stop/close/unlock.  This prevents a system crash (blue screen)
  70.     OnStop();
  71. }
  72. void CBackDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74. CDialog::DoDataExchange(pDX);
  75. //{{AFX_DATA_MAP(CBackDlg)
  76. // NOTE: the ClassWizard will add DDX and DDV calls here
  77. //}}AFX_DATA_MAP
  78. }
  79. BEGIN_MESSAGE_MAP(CBackDlg, CDialog)
  80. //{{AFX_MSG_MAP(CBackDlg)
  81. ON_WM_SYSCOMMAND()
  82. ON_WM_PAINT()
  83. ON_WM_QUERYDRAGICON()
  84. ON_BN_CLICKED(IDC_RECORD, OnRecord)
  85. ON_BN_CLICKED(IDC_STOP, OnStop)
  86. //}}AFX_MSG_MAP
  87. END_MESSAGE_MAP()
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CBackDlg message handlers
  90. BOOL CBackDlg::OnInitDialog()
  91. {
  92. CDialog::OnInitDialog();
  93. // Add "About..." menu item to system menu.
  94. // IDM_ABOUTBOX must be in the system command range.
  95. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  96. ASSERT(IDM_ABOUTBOX < 0xF000);
  97. CMenu* pSysMenu = GetSystemMenu(FALSE);
  98. CString strAboutMenu;
  99. strAboutMenu.LoadString(IDS_ABOUTBOX);
  100. if (!strAboutMenu.IsEmpty())
  101. {
  102. pSysMenu->AppendMenu(MF_SEPARATOR);
  103. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  104. }
  105. // Set the icon for this dialog.  The framework does this automatically
  106. //  when the application's main window is not a dialog
  107. SetIcon(m_hIcon, TRUE); // Set big icon
  108. SetIcon(m_hIcon, FALSE); // Set small icon
  109. // TODO: Add extra initialization here
  110. cbSystem=(CButton *)GetDlgItem(IDC_SYSTEM);
  111. cbVideo=(CButton *)GetDlgItem(IDC_VIDEO);
  112. cbAudio=(CButton *)GetDlgItem(IDC_AUDIO);
  113. cbSystem->SetCheck(TRUE);
  114.     return TRUE;  // return TRUE  unless you set the focus to a control
  115. }
  116. void CBackDlg::OnSysCommand(UINT nID, LPARAM lParam)
  117. {
  118. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  119. {
  120. CAboutDlg dlgAbout;
  121. dlgAbout.DoModal();
  122. }
  123. else
  124. {
  125. CDialog::OnSysCommand(nID, lParam);
  126. }
  127. }
  128. // If you add a minimize button to your dialog, you will need the code below
  129. //  to draw the icon.  For MFC applications using the document/view model,
  130. //  this is automatically done for you by the framework.
  131. void CBackDlg::OnPaint() 
  132. {
  133. if (IsIconic())
  134. {
  135. CPaintDC dc(this); // device context for painting
  136. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  137. // Center icon in client rectangle
  138. int cxIcon = GetSystemMetrics(SM_CXICON);
  139. int cyIcon = GetSystemMetrics(SM_CYICON);
  140. CRect rect;
  141. GetClientRect(&rect);
  142. int x = (rect.Width() - cxIcon + 1) / 2;
  143. int y = (rect.Height() - cyIcon + 1) / 2;
  144. // Draw the icon
  145. dc.DrawIcon(x, y, m_hIcon);
  146. }
  147. else
  148. {
  149. CDialog::OnPaint();
  150. }
  151. }
  152. // The system calls this to obtain the cursor to display while the user drags
  153. //  the minimized window.
  154. HCURSOR CBackDlg::OnQueryDragIcon()
  155. {
  156. return (HCURSOR) m_hIcon;
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CAspenApp commands
  160. WORD CALLBACK _loadds MyCallBack(HDRVR hdrvr, UINT msg, DWORD dwUser, DWORD dwParam1, DWORD dwParam2)
  161. {
  162. DWORD dwFlags = 0;
  163. TRACE0("MyCallBack");
  164. switch (msg)
  165. {
  166. case VF_MSGUSER_BUF_WRITE:
  167.     {
  168.         LONG lRet;
  169.         LPVF_BUFWRITE_STRUCT lpBufWrite = (LPVF_BUFWRITE_STRUCT)dwParam1;
  170.         lRet = mmioWrite(hmmioOutput, (LPSTR)lpBufWrite->lpBuffer, (LONG)lpBufWrite->dwBufferWrite);
  171.         if (lRet == -1L)
  172.             {
  173.         return (FALSE);
  174.             }
  175.         break;
  176.     }
  177. case VF_MSGUSER_BUF_CREATE:
  178.     {
  179.         LPVF_CALLBACK_STRUCT lpCallback = (LPVF_CALLBACK_STRUCT)dwParam1;
  180.         if (lpCallback->wSubMsg == VF_SUBMSG_OUTPUT)
  181.     {
  182.                 dwFlags = MMIO_CREATE | MMIO_WRITE;
  183.                 hmmioOutput = mmioOpen((LPTSTR)caFileName, (LPMMIOINFO)NULL, dwFlags);
  184.     }
  185.         else
  186.             {
  187.                 dwFlags = MMIO_READ | MMIO_DENYWRITE;
  188.                 hmmioInput = mmioOpen((LPTSTR)caFileName, (LPMMIOINFO)NULL, dwFlags);
  189.             }
  190.         break;
  191.     }
  192. case VF_MSGUSER_BUF_CLOSE:
  193.     {
  194.         LPVF_CALLBACK_STRUCT lpCallback = (LPVF_CALLBACK_STRUCT)dwParam1;
  195.         if (lpCallback->wSubMsg == VF_SUBMSG_OUTPUT)
  196.     {
  197.         if (hmmioOutput)
  198.     {
  199.         mmioClose(hmmioOutput, NULL);
  200.         hmmioOutput = NULL;
  201.     }
  202.     }
  203.         else
  204.     {
  205.         if (hmmioInput)
  206.         {
  207.             mmioClose(hmmioInput, NULL);
  208.             hmmioInput = NULL;
  209.         }
  210.     }
  211.         break;
  212.     }
  213.     } // switch
  214. return 1;
  215. }
  216. VOID CBackDlg::OnRecord() 
  217. {
  218.     // TODO: Add your control notification handler code here
  219.     WORD flags;
  220.     m_hVFDrv = OpenDriver (L"av8api.dll", NULL, NULL);
  221.     if (!m_hVFDrv)
  222.     {
  223.         AfxMessageBox(_TEXT("Can't OpenDriver()."));
  224.         return ;
  225.     }
  226.     if (!HVFAssign(m_hVFDrv, 0))
  227.     {
  228.         AfxMessageBox(_TEXT("Can't Assign Dev!"));
  229.         return ;
  230.     }
  231.     if (!HVFLock(m_hVFDrv, VF_CAP_ALL))
  232.     {
  233.         AfxMessageBox(_TEXT("Can't Lock Dev!"));
  234.         return ;
  235.     }
  236.     flags = VF_FLAG_MPEG | VF_FLAG_ENCODE | VF_FLAG_OUTBUF;
  237.     m_bStream = static_cast<BYTE>
  238.         (HVFOpen(m_hVFDrv, 
  239.                  flags, 
  240.                  reinterpret_cast<unsigned long>(MyCallBack)));
  241.     if(cbSystem->GetCheck())
  242.     {
  243.         InitEncodeVideoVxD();
  244.         InitEncodeAudioVxD();
  245.     } 
  246.     else 
  247.     {
  248.         if(cbVideo->GetCheck())
  249.         {
  250.             InitEncodeVideoVxD();
  251.         }
  252.         else 
  253.         {
  254.             InitEncodeAudioVxD();
  255.         }
  256.     }
  257.     InitEncodeSystemVxD();
  258.     if (m_bStream) 
  259.     {
  260.         HVFRecord(m_hVFDrv, m_bStream, NULL, NULL);
  261.     }
  262. } // CBackDlg::OnRecord()
  263. VOID CBackDlg::OnStop() 
  264. {
  265.     // TODO: Add your control notification handler code here
  266.     m_bStream = 0;
  267.     if (m_hVFDrv)
  268.     {
  269.         HVFStop(m_hVFDrv, m_bStream);
  270.         HVFClose(m_hVFDrv, m_bStream);
  271.         HVFUnlock(m_hVFDrv, VF_CAP_ALL);
  272.         CloseDriver(m_hVFDrv, NULL, NULL);
  273.         m_hVFDrv = 0;
  274.     }
  275. } // CBackDlg::OnStop()
  276. BOOL CBackDlg::InitEncodeVideoVxD(void)
  277. {
  278.     DWORD dwValue;
  279.     WORD  wWidth, wHeight;
  280.     wWidth=352;
  281.     wHeight=240;
  282.     dwValue=MAKELONG(wWidth, wHeight);
  283.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_OUTPUTSIZE, dwValue);
  284.     /* SDL - also set video width and height to scale images */
  285.     wWidth = (wWidth * 45) / 44; // try to maintain standard MPEG SIF aspect ratio
  286.     dwValue = MAKELONG(wWidth, wHeight /* [sic] */);
  287.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_INPUTSIZE, dwValue);
  288.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_BITRATE, BITRATE_DEFAULT);
  289.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_RATE, VF_FLAG_VID_25);
  290.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_IINTERVAL, IINTERVAL_DEFAULT);
  291.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_BINTERVAL, PINTERVAL_DEFAULT);
  292.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_MODE, VF_FLAG_VID_PAL);
  293.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_SOURCE, VF_FLAG_VID_COMPOSITE);
  294.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_ALGORITHM, VF_FLAG_VID_MPEG);
  295.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_BRIGHTNESS, BRIGHTNESS_DEFAULT);
  296.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_CONTRAST, CONTRAST_DEFAULT);    
  297.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_HUE, HUE_DEFAULT);
  298.     HVFSet(m_hVFDrv, m_bStream, VF_INDEX_VID_SATURATION, SATURATION_DEFAULT);
  299.     return TRUE;
  300. }
  301. BOOL CBackDlg::InitEncodeAudioVxD(void)
  302. {
  303.   DWORD dwValue;
  304. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_SAMPLE, SAMPLE_RATE_DEFAULT);
  305. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_BITRATE, BIT_RATE_DEFAULT);
  306. dwValue=VF_FLAG_AUD_MPEG; // set to MPEG algorithm
  307. dwValue=(dwValue<<16)+VF_FLAG_AUD_NONE;
  308. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_ALGORITHM, dwValue);
  309. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_MODE, MODE_DEFAULT);
  310. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_VOLUME, VOLUME_MIN);
  311. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_VOLUME, VOLUME_DEFAULT);
  312. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_AUD_GAIN, GAIN_DEFAULT);
  313. return TRUE;
  314. }
  315. BOOL CBackDlg::InitEncodeSystemVxD(void)
  316. {
  317. if(cbSystem->GetCheck()){
  318. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_STM_TYPE, VF_FLAG_MPGSYS);
  319. } else {
  320. if(cbVideo->GetCheck()){
  321. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_STM_TYPE, VF_FLAG_MPGVID);
  322. } else {
  323. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_STM_TYPE, VF_FLAG_MPGAUD);
  324. }
  325. }
  326. HVFSet(m_hVFDrv, m_bStream, VF_INDEX_STM_ENCODEMODE, VF_FLAG_STM_NORMAL);
  327. return TRUE;
  328. }