本帖最后由 飞凌-marketing 于 2022-1-27 11:00 编辑 3 L8 @/ K& v' J& J$ x
1 O( ^! B* f) w# f+ K `; X4 N
7 ?$ r( b4 k, c5 M6 h作者|donatello1996 来源 | 电子发烧友 题图|飞凌嵌入式 iMX8MPlus 核心板: https://www.forlinx.com/product/136.html
' X! i4 u- _: ?% t& E0 [5 W: v
- Q" B' A- I7 U3 u1 X( S8 z本文采用的硬件板卡为飞凌嵌入式OKMX8MP-C开发板,系统版本Linux5.4.70+Qt5.15.0,主要介绍基于HTTP网页服务器和UDP上位机的MJPG码流传输。 MJPG格式作为一种持续传输的视频码流,在远程监控领域中应用较广,而实现这种远程监控的第三方应用最常见的有两种:浏览器HTTP网页、UDP上位机。
; H' \4 N/ ^8 i4 Q' S0 A. q" g' s/ ]' F+ e9 @3 y9 P
两者各有优势,对比鲜明,其中: 这两种应用各有优缺点,对于嵌入式开发者来说,两者都必须掌握。
. w& h+ F. ^3 \- {3 k! t( e8 Y$ m一、HTTP网页服务器
' B8 _$ e. N3 m, h+ s% L先说下HTTP网页服务器获取MJPG码流的代码,首先是OKMX8MP-C在开发板端建立TCP服务器: - int TCP_Server_Found(socklen_t* socket_found , char* ip , int port)
5 o& G. ^2 i) s7 B - {2 [# Y8 N( y3 z. |
- struct sockaddr_in servaddr;
- N+ t$ m% k* j* J* j - socklen_t addrsize = sizeof(struct sockaddr);4 Z. d; ^" F; P, _9 S8 R% v0 _
- bzero(&servaddr , sizeof(servaddr));4 w2 ^0 A1 T: n% o! ~! C7 h) c/ S
- servaddr.sin_family = AF_INET;
3 T7 _$ P, V! \8 B) U2 f8 g3 x1 P - servaddr.sin_addr.s_addr = inet_addr(ip);
; Z5 g" |5 z8 `4 h8 T% j - servaddr.sin_port = htons(port);
. [2 y+ g0 [ N. R - int ret;
9 [# D, Y9 o/ L( u5 E - IF( (*socket_found = socket(AF_INET , SOCK_STREAM , 0)) == -1) N- T# v1 c4 q
- {4 o+ i. F2 b/ C& \# t7 v; W
- printf("Create socket error: %s (errno :%d)\n",strerror(errno),errno);
: c; [1 q6 o* A$ K- b& c1 H' p% Q+ F - return -1;, k1 _; C& I4 Q7 D: `9 f
- }
. f% {5 e1 F4 T6 H6 y - int on = 1; k5 h9 a3 \1 \" d0 O* O
- if(setsockopt(*socket_found , SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
4 F( C% m; b( h: X# O% }4 c( s) n - {
% n' ]1 ` f, b8 y - printf("setsockopt error\n");
5 ~; M0 A1 c6 Z - }" g3 {% K2 ^5 S1 X8 m
- ret = bind(*socket_found , (struct sockaddr *)&servaddr , addrsize);6 \1 j) i |% t* K; O& d
- if(ret == -1)$ ~8 ]" O( I- R
- {7 W# Z( N( S: y" \9 p4 }/ u- D
- printf("Tcp bind faiLED!\n");
! U( }# t: c8 r0 Z" n7 ~$ ^ - return -1;+ f7 p1 f, a% V6 T
- }
' g: x6 C) Q8 z5 L) q5 R - if(listen(*socket_found , 5) == -1)
, q) e% r M% l _ - {0 Q. h) e, P+ `4 z- X+ @. @9 r
- printf("Listen failed!\n");
8 K/ o9 L* q" ]5 y( k - return -1;# @) S0 Z2 f# |. U
- }
! l+ Y& S% s U- S) v - return 0;
1 [, P+ g: k/ V: W3 O# a9 q6 D6 U - }
复制代码其中setsockopt()函数是可选的,一般只用于规避socket()函数的建立错误。 建立了TCP服务器后,返回的socklen_t型实参在后面的HTTP网页服务器中需要用到。 HTTP网页服务器所属的TCP操作是需要另起轮询线程来让客户端进行accept()握手操作的,accept()之前的listen()倒是只需要执行一次即可,accept()握手操作和recv()接收操作需要创建一个死循环线程: - pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);* Q* t9 f4 ~( o# N d% ^7 p( r! q
- void * Thread_TCP_Web_Recv(void *arg)2 j% w$ U; j0 V3 _1 J
- {5 J9 \9 G6 M) a7 G5 g8 [
- 。。。! n2 }7 w5 C8 u2 f3 s
- while(1)
; l8 K2 T" Y+ R% r, O1 c4 o/ w& ~ - {; j, R6 C$ A- r+ S
- fd_socket_conn = accept(socket_web_server , (struct sockaddr *)&sockaddr_in_conn , &addrsize);
0 R5 h$ o# p6 f6 p9 n2 d9 Q6 q' k - printf("fd_socket_conn = accept()\n");
4 G3 u( u7 ], o l/ o+ [ - 。。。# l g" l5 B: r9 s
- recv(fd_socket_conn , recvbuf , 1000 , 0);0 J- s/ }: R' Q/ Q! Z
- }; `2 ?, q- Q( Y7 P' ?
- 。。。
- e7 f' S8 @" U. C2 q- |( U - }
复制代码MJPG帧可以使用Grab操作获取,获取到的MJPG帧需要在TCP线程中读,在Grab操作线程中写,这种被多个线程访问的资源需要加锁防止读写冲突,即资源被Grab操作写入时,需要上锁,不允许其它线程访问,操作完成时需要解锁,允许其它线程访问: - pthread_mutex_lock(&pmt);8 R- e+ b* Y& @3 F+ [9 n
- pic_tmpbuffer = pic.tmpbuffer;2 F' a( S& F) z2 J- n2 b
- pic.tmpbytesused = buff.bytesused;
3 M7 M" l$ n. } - pic_tmpbytesused = pic.tmpbytesused;5 `. ~% H; }0 e# U3 p
- pthread_cond_broadcast(&pct);
0 K* r) H1 y) f' d% h% I2 h* D3 X - pthread_mutex_unlock(&pmt);
复制代码线程互斥锁使用之前需要初始化: - pthread_mutex_t pmt; X+ i6 L2 W1 h2 C; s4 L ]
- pthread_cond_t pct;
( n( w( J% ]! B& d2 m - int main(int argc, char* argv[])
9 L* P. U+ v. H - {
, z# H3 D3 b; y - ...
3 q% W" X: q8 G+ N5 } - TCP_Server_Found(&socket_web_server , (char*)argv[2] , PORT_TCP);, @; ?' C$ _' Y; z
- pthread_mutex_init(&pmt , NULL);
2 ?) d: e! c. T! W9 { - pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);; X8 k; m( R. }
- pthread_create(&tid_tcp_web_send , NULL , Thread_TCP_Web_Send , NULL);
9 h4 Z# K: \3 ?5 e* k9 w - ...
/ C6 K' V+ M0 T - while(1)
" d& h2 h8 ~ z1 Z - {7 E9 N% e" j* c R
- V4l2_Grab_Mjpeg(false , MJPEG_FILE_NAME);6 O! _. S1 s! F; e. R! T# Z& {
- ...9 V2 f% K. U3 H! D
- }$ q# n6 T" d2 d" T2 P: j; u
- ...
0 \8 S2 a' i. f' }2 b: |# k - }
复制代码然后是发送的细节,发送图片文件之前,需要先发送HTTP标准头,这个相当于给发送图片或者其它类型的流数据铺路: - <p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"><span style="text-indent: 32px;">, P: \. e9 I4 K m
- </span></p><p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"></p>
复制代码- #define STD_HEADER "Connection: close\r\n" \
. ?. u+ r, i) v) `# B" H9 q - "Server: MJPG-Streamer/0.2\r\n" \. N) [2 x9 B5 Y( i5 w0 R( D
- "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \3 _2 y! y5 V8 ~, x7 g7 k
- "Pragma: no-cache\r\n" \1 v u0 N3 f. }- j) ]
- "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
% n4 m R8 f# d" R, f$ h d s - #define BOUNDARY "boundarydonotcross"- L. n0 \& I& l- n, R5 P- I
- printf("preparing header\n");6 r1 b2 W; P' m, m1 _8 J9 e
- sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
5 I3 S' p' s5 l1 q, ^. o- X2 x - "Access-Control-Allow-Origin: *\r\n" \0 C" a* \9 }$ `' x
- STD_HEADER \
# V7 ^4 D) p! E+ S, K - "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \' B6 W/ j; ]( [) J' ^% S
- "\r\n" \
4 D' i( M$ V- F( u# ^$ Q - "--" BOUNDARY "\r\n");
+ p8 [4 p2 i- C - if(write(fd, buffer, strlen(buffer)) < 0)* {) e8 X- e& m( [! v/ V8 r6 P
- {. b" g# j( O5 d2 H
- free(frame);
2 Z! w: X/ F6 y/ t; b: u - return;
. ~/ _; u7 h# K! \: e, J5 ^% ` - }
复制代码发送完HTTP标准头之后,就需要发送内容头(Content-Type),这处的Content-Type为image/jpeg,同样,HTTP标准协议里面image支持的类型远不止jpeg一种,发送完内容头之后就是正文和boundary结尾,这样帧完整的HTTP头发送到指定的TCP GET地址,就会在浏览器中显示刚刚发送的图片: - <pre class="prettyprint lang-cpp" style="box-sizing: border-box; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 16px; white-space: pre-wrap; line-height: 1.38462; color: rgb(51, 51, 51); word-break: break-all; background-color: rgb(245, 245, 245); border: 0px; border-radius: 4px; vertical-align: baseline;"> sprintf(buffer, "Content-Type: image/jpeg\r\n" \6 m3 V8 F4 }( A+ W* L" z ]
- "Content-Length: %d\r\n" \
" s0 t8 ^% H, B C% m- R - "X-Timestamp: %d.%06d\r\n" \
8 H& k2 ~+ O; s - "\r\n", frame_size, (int)timestamp.tv_sec, (int)timestamp.tv_usec);$ f) Q* D$ A5 I$ J$ i7 i
- printf("sending intemdiate header\n");
8 L+ y3 P$ a- ?6 T" u8 j3 U$ _$ b - if(write(fd, buffer, strlen(buffer)) < 0)5 f. t( M9 I6 @8 a- n; s
- break;# |3 ?( B3 {1 z: d4 V
- printf("sending frame\n");
4 M* l1 }, `# ^/ u - if(write(fd, frame, frame_size) < 0)9 j1 f) J: j2 M( D
- break;
% C: Q* Q$ d% |' P2 ` - printf("sending boundary\n");( L: z, k" z" x1 R
- sprintf(buffer, "\r\n--" BOUNDARY "\r\n");
* b1 f; W; |1 P - if(write(fd, buffer, strlen(buffer)) < 0)2 d; [" h. A! x" R7 Y8 Z
- break;</pre><p style="box-sizing: border-box; border: 0px; font-size: 16px; vertical-align: baseline; line-height: 26px; color: rgb(51, 51, 51); font-family: " helvetica="" neue",="" helvetica,="" tahoma,="" arial,="" "microsoft="" yahei",="" "hiragino="" sans="" gb",="" "wenquanyi="" micro="" hei",="" sans-serif;"=""></p>
复制代码另外需要说明的是,TCP服务器线程在发送MJPEG流的时候是死循环发送的,因此TCP客户端在发送完GET指令之后,就会收到TCP服务器循环发送的图像缓存,TCP客户端会陷入忙等待状态无法再对外发送任何GET或者POST指令,从客户端使用者角度来看的效果就是网页一直在等待。 9 ^$ `- K! P8 e
2 `0 i& r0 {. @" p7 f5 U
二、UDP上位机UDP发送操作,同样需要先建立UDP Socket:
! a" ]7 W" K$ h9 ]- int UDP_Send_Found(socklen_t* socket_found , struct sockaddr_in *addr , char* ip , int port)4 e7 ^! Z. Q9 U2 m
- {+ g8 k$ [+ G, N' U8 U3 v3 N
- *socket_found = socket(AF_INET, SOCK_DGRAM, 0);0 N% L/ b: |9 p* l
- if(*socket_found == (~0))8 S2 f; \! @9 `1 e
- {
7 }7 x/ y! h# i2 S - printf("Create udp send socket failed!\n");
' R$ M, X; N1 E+ U/ b- X r - return -1;# L, J( n/ P: ~( g* T
- }
3 l/ C5 v6 F$ ~1 t0 s+ w - addr->sin_family = AF_INET;& W" M4 L2 s6 m* D3 A4 G s2 o0 W
- addr->sin_addr.s_addr = inet_addr(ip);
, `( a5 C# [" ?/ ^ - addr->sin_port = htons(port);
$ E4 [; @ L% c* ^ p - memset(addr->sin_zero, 0, 8);
( Q/ Y' @# A$ X( t" Q - return 0;
8 A2 m+ ^4 h: | L& b+ f* g" B+ _ - }
复制代码
! d' C8 @, x2 u9 e+ }* D* D8 W/ X9 b. y
而UDP文件发送则要比HTTP发送简单得多,只需要将文件切片,每一片为固定长度的UDP帧长度,逐帧发送即可:
+ E2 l$ E3 M% e$ _6 _5 F; ]; N5 r1 H1 L- V2 k2 Y# w, x
2 Q. q$ k% W+ |( m2 H& P+ H- while(fend > 0) J- W4 q. L- z/ i- p! r
- {
# @' K$ d6 v, P8 K0 [ L3 h" Y - memset(picture.data , 0 , sizeof(picture.data));9 |' u+ |" H% V+ v# J; [( O
- fread(picture.data , UDP_FRAME_LEN , 1, fp);
+ j4 t: ^' q6 F! T - if(fend >= UDP_FRAME_LEN)
, e7 q! M' G! @4 K/ \% q - {
" q& g8 Z- M% m5 b/ W - picture.length = UDP_FRAME_LEN;
1 L2 I2 x$ T* K' I9 F# H* Q8 n7 g - picture.fin = 0;
: v! e, z( z6 }! K0 l# ] - }
' M# r) u& u) j0 l0 \ - else/ B s" Y; l% J9 F: J* J
- {
" L: B4 z4 A/ f5 j }; n, D - picture.length = fend;' q% x& X7 _0 O9 Y% |/ v2 M% G, M2 }) c
- picture.fin = 1;* N3 p& r: ~7 ~! C/ F. Z/ R
- }$ Q0 c( E/ Q6 E2 }
- //printf("sendbytes = %d \n",sendbytes);- a* ], f7 F. N1 _: X
- sendbytes = sendto(socket_send, (char *)&picture, sizeof(struct Package), 0, (struct sockaddr*)&addr,addr_len);
. M: h. z, h' W+ Y$ Q - if(sendbytes == -1)4 N9 V0 |0 X' R: N
- {7 g$ e( [5 }. {$ C: D
- printf("Send Picture Failed!d\n");# F& a% z: v5 a ]# C
- return -1;
3 ?" ?! Z( a5 T - }' T" [+ R8 D; @* K7 ^) n: N, T
- else
& K5 c, V4 N1 ?3 e8 I/ q - {
4 q) H: u% r5 f! D* k - fend -= UDP_FRAME_LEN;: j1 z1 s6 Q/ y, y
- }
% }' x1 ]1 w. p$ e9 a( D: R - }
复制代码 5 R3 I* c, M" R3 N
0 r3 s( W& {* N1 l. Y2 K/ V$ t( F6 Y0 X4 m: l4 s
9 d& C9 G: v" U* S" r0 ?; hiMX8MPlus 核心板: https://www.forlinx.com/product/136.html |