linux下视频采集服务器(UDP传输、多线程模式).doc_第1页
linux下视频采集服务器(UDP传输、多线程模式).doc_第2页
linux下视频采集服务器(UDP传输、多线程模式).doc_第3页
linux下视频采集服务器(UDP传输、多线程模式).doc_第4页
linux下视频采集服务器(UDP传输、多线程模式).doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

linux下多线程网络编程,实现arm9视频采集和网络发送jpeg数据,使用udp协议,自己写的,搞了好几天!linux下的多线程比较复杂易出错。linux network under the multi-threaded programming, video capture and realize ARM9 send jpeg data network, using udp protocol, wrote it myself, engaged in for several days! linux under the more complicated multi-threaded, error-prone.1. /多线程成功实例 2. #include 3. #include 4. #include 5. #include 6. #include 7. #include 8. #include 9. #include 10. #include 11. #include 12. #include 13. #include 14. #include 15. #include 16. #include 17. #include 18. #include 19. #include 20. #include 21. #include 22. #include 23. #include 24. #include 25. #include 26. #include 27. #include 28. #include 29. #include 30. 31. #defineBUFSIZE6 32. #defineDATA32*1024 33. #definePORT5000 34. #defineRTP_HDR_SZ12 35. #defineVIDEO_PALETTE_JPEG21 36. 37. unsignedcharbufBUFSIZE+2DATA; 38. inthead,tail; 39. 40. sem_twriten; 41. sem_treadn; 42. 43. structARGintsockfd; 44. intsin_size; 45. structsockaddr_inclient; 46. ; 47. 48. structFDG 49. intvideo_fd; 50. ; 51. 52. typedefunsignedcharu_int8; 53. typedefunsignedshortu_int16; 54. 55. intget_jpegsize(unsignedchar*buf,intinsize); 56. doubletdbl(structtimeval*a); 57. pthread_mutex_tbuffer_mutex=PTHREAD_MUTEX_INITIALIZER; 58. 59. staticvoid*producer(void*fdg) 60. 61. structFDG*vd; 62. intvideo_fd; 63. 64. if(sizeof(fdg)!=sizeof(structFDG) 65. 66. perror(producerargerror); 67. exit(1); 68. 69. else 70. 71. vd=(structFDG*)fdg; 72. video_fd=vd-video_fd; 73. free(fdg); 74. fdg=NULL; 75. 76. 77. 78. for(;) 79. 80. sem_wait(&writen);/减少可读的资源数 81. pthread_mutex_lock(&buffer_mutex);/进入互斥区 82. /memset(bufhead,s,20); 83. read(video_fd,bufhead,DATA); 84. head=(head+1)%BUFSIZE; 85. 86. pthread_mutex_unlock(&buffer_mutex);/离开互斥区 87. sem_post(&readn);/增加可读资源数 88. /sleep(0.0001); 89. 90. 91. 92. 93. staticvoid*consumer(void*arg) 94. 95. intsockfd; 96. intsin_size; 97. intjpegsize; 98. structsockaddr_inclient; 99. structARG*info; 100. 101. typedefstruct 102. unsignedintversion:2;/*protocolversion*/ 103. unsignedintp:1;/*paddingflag*/ 104. unsignedintx:1;/*headerextensionflag*/ 105. unsignedintcc:4;/*CSRCcount*/ 106. unsignedintm:1;/*markerbit*/ 107. unsignedintpt:7;/*payloadtype*/ 108. unsignedintseq:16;/*sequencenumber*/ 109. unsignedintts;/*timestamp*/ 110. unsignedintssrc;/*synchronizationsource*/ 111. rtp_hdr_t; 112. 113. structtimevalstart; 114. 115. rtp_hdr_trtphdr; 116. u_int8*jpeg_data; 117. u_int8*packet_buf; 118. unsignedintts; 119. unsignedintssrc; 120. u_int8*ptr; 121. u_int8frame,bframe; 122. intbytes_left;/jpeg数据总长度 123. intdata_len,packetsize;/packetsize变量 124. 125. info=(structARG*)arg; 126. if(info-sockfdsin_size!=16) 133. 134. perror(errerror); 135. exit(1); 136. 137. sockfd=info-sockfd; 138. sin_size=info-sin_size; 139. memcpy(&client,&info-client,sizeof(info-client); 140. free(arg); 141. arg=NULL; 142. 143. packetsize=RTP_HDR_SZ+2050; 144. packet_buf=(u_int8*)calloc(packetsize,sizeof(u_int8); 145. jpeg_data=(u_int8*)malloc(DATA); 146. 147. for(;) 148. 149. frame=0; 150. gettimeofday(&start,0); 151. ts=(unsignedint)(tdbl(&start)*1000); 152. ssrc=125; 153. /*InitializeRTPheader*/154. 155. rtphdr.version=2; 156. rtphdr.p=0; 157. rtphdr.x=0; 158. rtphdr.cc=0; 159. rtphdr.m=0; 160. rtphdr.pt=40; 161. rtphdr.seq=1; 162. rtphdr.ts=htonl(ts); 163. rtphdr.ssrc=htonl(ssrc); 164. 165. sem_wait(&readn);/减少可读的资源数 166. 167. pthread_mutex_lock(&buffer_mutex);/进入互斥区 168. 169. jpegsize=get_jpegsize(buftail,DATA); 170. 171. if(jpegsize!=-1) 172. 173. memcpy(jpeg_data,buftail,jpegsize); 174. 175. 176. tail=(tail+1)%BUFSIZE; 177. 178. pthread_mutex_unlock(&buffer_mutex);/离开互斥区 179. 180. sem_post(&writen);/增加可读资源数 181. 182. bytes_left=jpegsize; 183. 184. while(bytes_left0) 185. 186. ptr=packet_buf+RTP_HDR_SZ; 187. 188. bframe=frame; 189. 190. data_len=packetsize-(ptr-packet_buf)-2;/每一分片大小 191. 192. if(data_len=bytes_left)/当为最后一个分片时 193. 194. data_len=bytes_left; 195. rtphdr.m=1; 196. bframe=255; 197. data_len=jpegsize%2048; 198. 199. 200. *ptr=bframe;ptr+; 201. *ptr=frame;ptr+; 202. 203. rtphdr.seq=htons(rtphdr.seq); 204. memcpy(packet_buf,&rtphdr,RTP_HDR_SZ); 205. memcpy(ptr,jpeg_data+frame*2048,data_len); 206. 207. if(sendto(sockfd,packet_buf,(ptr-packet_buf)+data_len,208. 0,(structsockaddr*)&client,sin_size)0) 209. 210. perror(sendtoerror); 211. 212. 213. frame+; 214. bytes_left-=2048; 215. rtphdr.seq=ntohs(rtphdr.seq); 216. rtphdr.seq+; 217. 218. 219. sleep(0.0001); 220. 221. free(packet_buf); 222. free(jpeg_data); 223. pthread_exit(NULL); 224. 225. 226. 227. intmain() 228. 229. clock_toldtick,newtick; 230. floattime1; 231. staticintvf=0; 232. inti; 233. clock_ttotalold,totalnew; 234. intvideo_fd; 235. structvideo_capabilitygrab_cap; 236. 237. intwidth=320; 238. intheight=240; 239. 240. 241. structvideo_picturegrab_pic; 242. structvideo_mmapgrab_map; 243. structvideo_mbufgrab_buf; 244. 245. 246. intsockfd; 247. intsin_size; 248. structsockaddr_inclient; 249. structsockaddr_inserver; 250. charmsg100; 251. structARG*arg; 252. structFDG*fdg; 253. pthread_tp_tid; 254. pthread_tc_tid; 255. head=0; 256. tail=0; 257. for(i=0;iBUFSIZE+2;i+) 258. 259. bzero(bufi,DATA); 260. 261. 262. sem_init(&writen,0,BUFSIZE); 263. sem_init(&readn,0,0); 264. 265. /数据采集 266. loop: 267. totalold=clock(); 268. video_fd=open(/dev/video0,O_RDWR); 269. if(video_fd=-1) 270. 271. /u6253u5f00u6444u50cfu5934272. fprintf(stderr,cannotopenvideo0);273. exit(1);274. 275. oldtick=clock();276. if(ioctl(video_fd,VIDIOCGCAP,&grab_cap)0)277. 278. fprintf(stderr,ioctlVIDEOCGCAPfailed.);279. exit(1);280. 281. newtick=clock();282. time1=(double)(newtick-oldtick)/CLOCKS_PER_SEC;283. printf(n%fsecondistaketoioctlVIDEOCGCAPn,time1);284. 285. printf(TheVideoCapName:%sn,grab_);286. printf(Thehannels:%dn,grab_cap.channels);287. printf(TheAudios:%dn,grab_cap.audios);288. printf(Themaxwidth:%d,maxheight:%d,minwidth%d,minheight:%dn,289. grab_cap.maxwidth,grab_cap.maxheight,290. grab_cap.minwidth,grab_cap.minheight); 291. 292. oldtick=clock(); 293. 294. if(ioctl(video_fd,VIDIOCGPICT,&grab_pic)0) 295. 296. fprintf(stderr,ioctlVIDIOCGPICTfailed.); 297. exit(1); 298. 299. newtick=clock(); 300. time1=(double)(newtick-oldtick)/CLOCKS_PER_SEC; 301. printf(n%fsecondistaketoioctlVIDIOCGPICTn,time1); 302. 303. printf(Thebrightness:%dnThehue:%dnThecolour:%dn304. Thecontrast:%dnThewhiteness:%dnThedepth:%dnThepalette:%dn,305. grab_pic.brightness,grab_pic.hue,grab_pic.colour,grab_pic.contrast,306. grab_pic.whiteness,grab_pic.depth,grab_pic.palette); 307. 308. oldtick=clock(); 309. 310. if(ioctl(video_fd,VIDIOCGMBUF,&grab_buf)video_fd=video_fd; 341. 342. if(pthread_create(&p_tid,NULL,producer,(void*)fdg) 343. 344. perror(pthreap_tid1error!); 345. exit(1); 346. 347. 348. /free(fdg); 349. /fdg=NULL; 350. 351. /网络部分 352. if(sockfd=socket(AF_INET,SOCK_DGRAM,0)=-1) 353. 354. perror(creatsocketerror); 355. exit(1); 356. 357. 358. bzero(&server,sizeof(server); 359. server.sin_family=AF_INET; 360. server.sin_port=htons(PORT); 361. server.sin_addr.s_addr=htonl(INADDR_ANY); 362. if(bind(sockfd,(structsockaddr*)&server,sizeof(structsockaddr)=-1) 363. perror(binderror); 364. exit(1); 365. 366. sin_size=sizeof(structsockaddr_in); 367. 368. while(1) 369. 370. 371. if(recvfrom(sockfd,msg,100,0,(structsockaddr*)&client,&sin_size)sockfd=sockfd; 385. arg-sin_size=sin_size; 386. memcpy(void*)&arg-client,&client,sizeof(client); 387. 388. 3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论