|
以下是PC部分的关键代码
- 1. #pragma once
- 2. #include "winsock2.h"
- 3.
- 4. //UDP支持
- 5. #include "..\UDP\UDP.h"
- 6.
- 7. //音频支持
- 8. #include "WaveIn.h"
- 9. #include "waveout.h"
- 10.
- 11. //G726支持
- 12. #include "g726.h"
- 13.
- 14. //视频支持
- 15. #include "Gdiplus.h"
- 16. using namespace Gdiplus;
- 17.
- 18.
- 19. #define VideoData_Size 1440 //每块视频数据包的大小
- 20. #define Video_Width 320 //视频宽度
- 21. #define Video_Height 240 //视频长度
- 22. #define AudioData_Size 960 //每块音频数据包的大小
- 23. #define Compr_AudioData_Size 120 //压缩后音频块的大小
- 24.
- 25.
- 26. //音频输入输出变量
- 27. CWaveIn *g_pIn;
- 28. CWaveOut *g_pOut;
- 29. char pin[AudioData_Size],pout[Compr_AudioData_Size];
- 30. char wave_data[AudioData_Size];
- 31.
- 32. //UDP变量
- 33. CUDP_CE m_CEUdp;
- 34.
- 35.
- 36. //视频输入变量
- 37. GdiplusStartupInput m_gdiPlusInPut;
- 38. ULONG_PTR m_gdiPlusToken;
- 39. char video_data[Video_Width*Video_Height];
- 40. int index;//视频数据当前索引
- 41. class AVClass
- 42. {
- 43. private:
- 44.
- 45. public:
- 46. //=====================================================================
- 47. // 语法格式: void InitAV(CWnd * p)
- 48. // 实现功能: 初始化音频和视频,用于录音、播放音频,以及播放视频
- 49. // 参数: p为窗口类指针
- 50. // 返回值: 无
- 51. //=====================================================================
- 52. void InitAV(CWnd * p,int local_port,CString remote_ip,int remote_port)
- 53. {
- 54. //-------------------------UDP连接--------------------------//
- 55. m_CEUdp.m_OnUdpRecv = OnUdpCERecv;
- 56. DWORD nResult = m_CEUdp.Open(p,local_port,remote_ip,remote_port);
- 57. if (nResult <=0)
- 58. {
- 59. AfxMessageBox(_T("打开端口失败"));
- 60. return;
- 61. }
- 62.
- 63. //-------------------------音频--------------------------//
- 64. g_pOut = new CWaveOut();
- 65. g_pIn = new CWaveIn();
- 66. g_pOut->StartPlay();
- 67. g_pIn->StartRec(OnRecording,(DWORD)p);
- 68. //-------------------------视频--------------------------//
- 69. GdiplusStartup( &m_gdiPlusToken, &m_gdiPlusInPut, NULL ); //初始化GDI+
- 70. memset(video_data,0,Video_Width*Video_Height);
- 71. index=0;
- 72.
- 73. }
- 74.
- 75. //=====================================================================
- 76. // 语法格式: void FreeAV()
- 77. // 实现功能: 释放音频、视频
- 78. // 参数: 无
- 79. // 返回值: 无
- 80. //=====================================================================
- 81. void FreeAV()
- 82. {
- 83.
- 84. //-------------------------音频--------------------------//
- 85. g_pOut->StopPlay();
- 86. g_pIn->StopRec();
- 87. delete g_pOut;
- 88. delete g_pIn;
- 89. //-------------------------视频--------------------------//
- 90. GdiplusShutdown(m_gdiPlusToken); //销毁GDI+
- 91.
- 92. //------------------------UDP--------------------------//
- 93. m_CEUdp.Close();
- 94.
- 95. }
- 96. //=====================================================================
- 97. // 语法格式: void RecAndPlay(WPARAM wParam,LPARAM lParam,HWND hwnd)
- 98. // 实现功能: 接收网络传来的音频,以及播放
- 99. // 参数: wParam,表示数据;lParam,表示数据长度;hwnd,表示显示视频的窗口句柄
- 100. // 返回值: 无
- 101. //=====================================================================
- 102. static void CALLBACK OnUdpCERecv(CWnd *pWnd,char* buf,int nLen,sockaddr * addr)
- 103. {
- 104. /*测试收到的数据大小
- 105.
- 106. CString tmp;
- 107. tmp.Format(L"%d",nLen);
- 108. MessageBox(0,tmp,0,0);
- 109. return;*/
- 110.
- 111. //-------------------------如果是音频数据--------------------------//
- 112. if(nLen==Compr_AudioData_Size)
- 113. {
- 114. g726_Decode(buf,(unsigned char*)wave_data);
- 115. g_pOut->Play(wave_data,AudioData_Size);
- 116. return;
- 117. }
- 118. //-------------------------如果是视频数据--------------------------//
- 119.
- 120. if(nLen==VideoData_Size)//完整的视频数据块
- 121. {
- 122. for(int i=0;i
- 123. {
- 124. video_data[index]=buf[i];
- 125. index++;
- 126. }
- 127. return;
- 128. }
- 129.
- 130. //视频数据块的最后一块
- 131. for(int i=0;i
- 132. {
- 133. video_data[index]=buf[i];
- 134. index++;
- 135. }
- 136.
- 137. //如果JPEG图像特别大,则肯定是出错,则抛弃
- 138. if(index>Video_Width*Video_Height)
- 139. {
- 140. //MessageBox(0,"缓冲区出错","错误信息",0);
- 141. return;
- 142. }
- 143.
- 144.
- 145. try{
- 146. IPicture *pPic;
- 147. IStream *pStm ;
- 148. //分配全局存储空间
- 149. HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,index);
- 150. LPVOID pvData=NULL ;
- 151. //锁定分配内存块
- 152. pvData=GlobalLock(hGlobal);
- 153. //复制数据包video_data到pvData
- 154. memcpy(pvData,video_data,index);
- 155.
- 156. GlobalUnlock(hGlobal);
- 157. CreateStreamOnHGlobal(hGlobal,TRUE,&pStm);
- 158.
- 159. ULARGE_INTEGER pSeek;
- 160. LARGE_INTEGER dlibMove ={ 0 } ;
- 161. pStm->Seek(dlibMove,STREAM_SEEK_SET ,&pSeek);
- 162. // Sleep(15);
- 163.
- 164. //装入图形文件
- 165. if(FAILED(OleLoadPicture(pStm,index,TRUE,IID_IPicture,(LPVOID*)&pPic)))
- 166. {//附:如果video_data这个数组包含的图像有错,则OleLoadPicture 容易产生读写内存错误
- 167. // pPic->Release();
- 168. // pStm->Release();
- 169. return ;
- 170. }
- 171.
- 172. Image img(pStm,0);
- 173. Graphics mGraphics(GetDC(pWnd->m_hWnd));
- 174. mGraphics.DrawImage(&img, 0, 0, Video_Width, Video_Height);
- 175. img.~Image();//会出错
- 176. mGraphics.~Graphics();
- 177. pPic->Release();
- 178. pStm->Release();
- 179. }
- 180. catch(CException * e)
- 181. {}
- 182. memset(video_data,0,Video_Width*Video_Height);
- 183. index=0;
- 184. }
- 185.
- 186. //=====================================================================
- 187. // 语法格式: static void OnRecording(char *data,int length,DWORD userdata)
- 188. // 实现功能: 释放音频
- 189. // 参数: data表示数据,length表示数据长度,userdata暂时没用
- 190. // 返回值: 无
- 191. //=====================================================================
- 192. static void OnRecording(char *data,int length,DWORD userdata)
- 193. {
- 194. memcpy(pin,g_pIn->buffer,AudioData_Size);
- 195. g726_Encode((unsigned char*)pin,pout);
- 196. m_CEUdp.SendData(pout,Compr_AudioData_Size);
- 197. }
- 198. };
复制代码 |
|