本帖最后由 飞凌-marketing 于 2022-1-27 11:00 编辑
. k% O# K. e- Q* t, f% z) ~7 j' V9 s8 W# i

2 ^; b7 T3 |5 m! d% }作者|donatello1996 来源 | 电子发烧友 题图|飞凌嵌入式 iMX8MPlus 核心板: https://www.forlinx.com/product/136.html4 i' i8 B( i' E
8 w k$ b" K* L
本文采用的硬件板卡为飞凌嵌入式OKMX8MP-C开发板,系统版本Linux5.4.70+Qt5.15.0,主要介绍基于HTTP网页服务器和UDP上位机的MJPG码流传输。 MJPG格式作为一种持续传输的视频码流,在远程监控领域中应用较广,而实现这种远程监控的第三方应用最常见的有两种:浏览器HTTP网页、UDP上位机。
8 k. ^" f$ k' ]; C/ c9 e: S; ~ 6 g: F* ^; N+ J7 h6 ^
两者各有优势,对比鲜明,其中: 这两种应用各有优缺点,对于嵌入式开发者来说,两者都必须掌握。
& L3 R" K6 ~7 v一、HTTP网页服务器
- y' s* U$ ]/ U/ ?先说下HTTP网页服务器获取MJPG码流的代码,首先是OKMX8MP-C在开发板端建立TCP服务器: - int TCP_Server_Found(socklen_t* socket_found , char* ip , int port)
3 r: P. C" {7 g9 i' f# o9 I! |* { - {
. w/ g3 G' j- p, u$ q5 [+ T9 ~ - struct sockaddr_in servaddr;
2 b: y+ r# ^" `: S- x7 y - socklen_t addrsize = sizeof(struct sockaddr);; c E$ ` Q3 R* }2 P* I3 h
- bzero(&servaddr , sizeof(servaddr));) H" v2 e: w m. z" {9 s; E
- servaddr.sin_family = AF_INET;
; t& U* t( b& T' Q: l - servaddr.sin_addr.s_addr = inet_addr(ip);# y! @, z9 a$ ~
- servaddr.sin_port = htons(port);9 [! _- z+ G, l6 i
- int ret;4 T9 L9 E4 H0 Y
- IF( (*socket_found = socket(AF_INET , SOCK_STREAM , 0)) == -1)& j4 \. Y4 |2 \. ]3 w
- {
9 b1 \. j; z& }! _! w) Y, E - printf("Create socket error: %s (errno :%d)\n",strerror(errno),errno);; b; X& o: w2 G2 G' W) [6 M* g
- return -1;' Z5 B W. B8 D8 e
- }/ _- K% J; q8 I
- int on = 1;
/ a- g( ?, s) [( p1 U7 X - if(setsockopt(*socket_found , SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)# p4 J" S/ A/ a1 o
- {
8 i* m$ G, ]2 D5 C( Z7 R - printf("setsockopt error\n");
7 l- }9 w! t/ v+ a! |; n - }
. H+ E+ c2 \ b6 B% y - ret = bind(*socket_found , (struct sockaddr *)&servaddr , addrsize);
* ?0 x& g: N# [0 A9 Z8 S. c+ A - if(ret == -1)) V" Q" F( m& ^" v
- {
* ?. t. s: M" Q$ r; F - printf("Tcp bind faiLED!\n");
* c' [ @* d! ^7 I7 U - return -1;
( N3 v' o3 A9 g: {& l$ w& v - }2 ]5 P6 K2 R" P* {, |$ K% m! l
- if(listen(*socket_found , 5) == -1)9 o1 s7 I! d6 w% P4 a* q4 U
- {" W% B& G+ B7 _5 u
- printf("Listen failed!\n");) @- |& a9 Q7 W* L5 ^9 ?
- return -1;# l! [* s' S- w, g; c9 O7 ]7 \6 k
- }6 H+ r! }" C! p4 T
- return 0;
( \# r, [6 x, {7 @0 c - }
复制代码其中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);
+ ]0 F2 I( M8 ]2 l/ X9 @& I - void * Thread_TCP_Web_Recv(void *arg)& W) [# j0 P5 x, o' a' r
- {4 x# `( g4 d1 Y/ m4 V7 ~8 M" l, G
- 。。。7 N( F ^( k3 Y7 F5 }
- while(1)( s1 K% E) X& i% i3 f; B
- {* F/ f/ N' B" v+ v# W' n
- fd_socket_conn = accept(socket_web_server , (struct sockaddr *)&sockaddr_in_conn , &addrsize);$ S# W& U! U6 p: ]
- printf("fd_socket_conn = accept()\n");
$ |. ^( `6 V% Q. @ - 。。。
! E9 |6 H6 ]9 V* T: H$ L - recv(fd_socket_conn , recvbuf , 1000 , 0);
7 g& Q$ K( o( a2 V - }: Z) F, ^1 g4 P) Y3 l$ T1 e5 W
- 。。。
3 {4 N$ f# {$ ]/ Q, \. I. v - }
复制代码MJPG帧可以使用Grab操作获取,获取到的MJPG帧需要在TCP线程中读,在Grab操作线程中写,这种被多个线程访问的资源需要加锁防止读写冲突,即资源被Grab操作写入时,需要上锁,不允许其它线程访问,操作完成时需要解锁,允许其它线程访问: - pthread_mutex_lock(&pmt);" ~, a+ f* y+ E7 E" N+ w9 R6 Y
- pic_tmpbuffer = pic.tmpbuffer;4 ^1 R2 K* l! Z" q
- pic.tmpbytesused = buff.bytesused;1 _! |% J- e/ f; O% l: H* A m
- pic_tmpbytesused = pic.tmpbytesused;' U& c! u$ q" D- j1 ]8 i+ q* e: q
- pthread_cond_broadcast(&pct);
( S7 C! ?7 j4 T0 U: z - pthread_mutex_unlock(&pmt);
复制代码线程互斥锁使用之前需要初始化: - pthread_mutex_t pmt;. F8 Y+ C" Q, V, \+ x2 Q+ [. ^; \! N
- pthread_cond_t pct;
/ X/ M0 G1 G3 h7 N1 { - int main(int argc, char* argv[])
' {/ M1 G' \" D q ^, x - {
: Q' Q2 o8 ^% |' n* K+ G8 h - ...
$ T+ C1 V! U+ k# r5 z, w ^+ b - TCP_Server_Found(&socket_web_server , (char*)argv[2] , PORT_TCP);. d+ {' s1 P) H3 [! K
- pthread_mutex_init(&pmt , NULL);2 p' e3 H) C6 n. B, x
- pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);
/ N4 X1 a# b6 q$ ~; J* s: K - pthread_create(&tid_tcp_web_send , NULL , Thread_TCP_Web_Send , NULL);
9 F% `" A8 ?3 K/ [( H! O7 t2 U* Y# [ - ...
# W/ U" i8 }) ^) g" G - while(1)- m( S+ W# t8 }! |1 c; x5 E" u8 F
- {6 E/ L4 r% _% {) a$ D
- V4l2_Grab_Mjpeg(false , MJPEG_FILE_NAME); A7 y, v L) c( ~; x) q2 m
- ...
3 ^% u4 @7 n* E1 a - }; {( {% s6 j& _ e% l
- ...
}; t$ ?" b8 N7 R$ o. a2 g# R3 i - }
复制代码然后是发送的细节,发送图片文件之前,需要先发送HTTP标准头,这个相当于给发送图片或者其它类型的流数据铺路: - <p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"><span style="text-indent: 32px;">1 H4 ^- \( w5 Y; f. Q
- </span></p><p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"></p>
复制代码- #define STD_HEADER "Connection: close\r\n" \6 k! g& W, J' ?, H
- "Server: MJPG-Streamer/0.2\r\n" \
" V7 x6 U7 d- D5 z: N, N - "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \
5 V4 h/ B! y1 X' ^" I' l - "Pragma: no-cache\r\n" \2 g c! a. }, @# c6 [" k
- "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n") |# i6 z2 Q7 } H
- #define BOUNDARY "boundarydonotcross"
7 S3 Q/ ~) C; E E0 v - printf("preparing header\n");. [0 R* u% @% P+ j5 ^6 S
- sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
8 C; b7 B' m7 B2 I: m - "Access-Control-Allow-Origin: *\r\n" \) C+ ]' f4 j6 f& l3 L# X& P' V
- STD_HEADER \+ m U! d2 E7 m9 D' U
- "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \
0 m: Q. ^0 b" x2 Y& u7 R. O - "\r\n" \& m, g1 ?" W9 d( K5 j$ i+ t) W
- "--" BOUNDARY "\r\n");9 @2 J6 O2 K5 S$ f+ \
- if(write(fd, buffer, strlen(buffer)) < 0)) T! D' L% e" e
- {
+ F6 B6 Q5 D! ]8 a o) V8 n! l - free(frame);% e9 h1 I; W" T( b2 l& _) Y* D
- return;2 ?" L2 l! J7 a: Y+ [2 w# H
- }
复制代码发送完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" \! H) |! R. i1 S; w3 P' {) F
- "Content-Length: %d\r\n" \$ Q$ T" r ], y& [% O5 b/ s( T
- "X-Timestamp: %d.%06d\r\n" \
6 ~5 c1 n% B. Z, } - "\r\n", frame_size, (int)timestamp.tv_sec, (int)timestamp.tv_usec);
* m: r6 {$ y, Y* t0 i8 G" V# Q - printf("sending intemdiate header\n");
2 G4 r) F5 u# c* H - if(write(fd, buffer, strlen(buffer)) < 0)7 y [9 u" m( d
- break;) O! l# h G' g& n4 U6 R4 ]
- printf("sending frame\n");
4 ^1 x$ j$ H5 Q5 G2 M - if(write(fd, frame, frame_size) < 0)# _/ J; g0 F; K; a, U
- break;+ J1 V3 T$ u3 {% `$ Y
- printf("sending boundary\n");+ \+ {: `7 d4 X; [" a2 k
- sprintf(buffer, "\r\n--" BOUNDARY "\r\n");( s" q- @& t" B5 B% A
- if(write(fd, buffer, strlen(buffer)) < 0)4 K6 i# a( o* a! Y
- 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指令,从客户端使用者角度来看的效果就是网页一直在等待。  , s( O' `4 g% `8 B1 l H. R9 T0 ]2 s
 ) W% C: g3 q! N( u: L% F# t3 T5 A0 l
二、UDP上位机UDP发送操作,同样需要先建立UDP Socket:; X# O% O" v, f5 ?# P, h/ K7 {
- int UDP_Send_Found(socklen_t* socket_found , struct sockaddr_in *addr , char* ip , int port)# h% _, X/ S5 \
- {
1 T- l) a3 o' Y" t% k, ] - *socket_found = socket(AF_INET, SOCK_DGRAM, 0);# `* H0 D" i$ d9 u6 j
- if(*socket_found == (~0))
$ Z! x3 w7 p4 C# f3 l - {
; O- U% t S. @- k' L/ o4 q - printf("Create udp send socket failed!\n");
. H6 K0 {* L* P; k# B - return -1;
( O- ]7 g0 S' F$ [6 N- _ - }
& C5 z7 @: L! [# m# B - addr->sin_family = AF_INET;- ]5 p1 ^5 j. u( w. a
- addr->sin_addr.s_addr = inet_addr(ip);
; G6 V7 K. h+ s: q# U$ y - addr->sin_port = htons(port);, ^1 F! K& k- s- P2 q- r# B
- memset(addr->sin_zero, 0, 8);
" k% ~! P& i7 d' E - return 0;$ e! `5 C; {: q& ?) _$ g
- }
复制代码 / a" `, @/ T& s5 u) @ T
5 r& {+ `+ n! V0 ^ M
而UDP文件发送则要比HTTP发送简单得多,只需要将文件切片,每一片为固定长度的UDP帧长度,逐帧发送即可:$ |- g0 c" ~& c w3 [4 V
" e) }: P* C1 K7 B$ c) J" E% D3 {0 w. {' u0 }2 ?
- while(fend > 0)' N, Y$ Q' Z9 X& v1 ]
- {: O7 F) p9 k$ w6 Q+ x
- memset(picture.data , 0 , sizeof(picture.data));
2 { s- |5 O0 | - fread(picture.data , UDP_FRAME_LEN , 1, fp);
3 _& z( p: K' F6 v8 [ - if(fend >= UDP_FRAME_LEN)
. z1 X6 ?$ W; B* s. C' Z9 _- I5 G! h1 |. o - {. Z3 h! r1 ~8 G0 T
- picture.length = UDP_FRAME_LEN;
L6 z9 G: C1 I1 g - picture.fin = 0;" q; c- a7 `7 z! L/ w
- }; a1 _5 j2 c: s/ }9 ]5 {& R
- else F% \5 s3 B5 q8 e% M# L/ ^: E$ @* V0 U
- {
, Q8 Z/ Q" a" Y2 f" }. N - picture.length = fend;9 W1 `: R! n0 ^
- picture.fin = 1;
( o1 w. d. G* s: f& ~+ N: w - }& e! J! r+ k) S' a% Z0 r
- //printf("sendbytes = %d \n",sendbytes);
+ s- |$ i& P, a8 K- _" k - sendbytes = sendto(socket_send, (char *)&picture, sizeof(struct Package), 0, (struct sockaddr*)&addr,addr_len);7 L4 m8 U- P; s1 }
- if(sendbytes == -1)& n5 T8 E0 h, X+ w4 N( M
- {
2 ?" Q+ [& h# }7 \ - printf("Send Picture Failed!d\n");* Y$ r$ r# q. y7 ]* L6 `# D
- return -1;: [( l/ N7 Q) B+ O
- }
1 b6 p6 ^% j5 s' v$ G3 Y) L, N - else- i8 w! p. j; s4 e% P8 c# m
- {
8 Q* z8 h w$ {; c& e% d6 m" F - fend -= UDP_FRAME_LEN;
6 g2 P1 L6 i$ k. s F - }
6 N" L& B7 ?( k - }
复制代码
( r# {5 \, f6 c" }4 Q7 o/ Q/ ? u" J- X
 0 T7 o2 X) {. y4 a8 u9 z
' v1 V. }6 y3 {* ]- o( D4 [- J& p
iMX8MPlus 核心板: https://www.forlinx.com/product/136.html |