本帖最后由 飞凌-marketing 于 2022-1-27 11:00 编辑
+ u' [! n8 q+ m# [5 X$ A3 N( |' X$ w/ a) w" y4 |
! _) R1 y _! }. V5 T
作者|donatello1996 来源 | 电子发烧友 题图|飞凌嵌入式 iMX8MPlus 核心板: https://www.forlinx.com/product/136.html' j* v! S- G' g+ c6 k, Q
( k& b2 u- d' K$ U4 f5 O本文采用的硬件板卡为飞凌嵌入式OKMX8MP-C开发板,系统版本Linux5.4.70+Qt5.15.0,主要介绍基于HTTP网页服务器和UDP上位机的MJPG码流传输。 MJPG格式作为一种持续传输的视频码流,在远程监控领域中应用较广,而实现这种远程监控的第三方应用最常见的有两种:浏览器HTTP网页、UDP上位机。
: g7 f' {/ l# C" j* ?! J) ~- p, O' H% y; k
两者各有优势,对比鲜明,其中: 这两种应用各有优缺点,对于嵌入式开发者来说,两者都必须掌握。 * ^+ w& _1 f3 K; {- p
一、HTTP网页服务器
7 [( x4 X3 a; x# Q先说下HTTP网页服务器获取MJPG码流的代码,首先是OKMX8MP-C在开发板端建立TCP服务器: - int TCP_Server_Found(socklen_t* socket_found , char* ip , int port)
6 ~1 E9 h2 b& | - {
- I# W7 e3 S6 J) |$ w - struct sockaddr_in servaddr;
$ n$ q) ?5 e J/ v! l8 f% H - socklen_t addrsize = sizeof(struct sockaddr);
, C( B) O3 D: b9 h: l# n - bzero(&servaddr , sizeof(servaddr));
; U5 F0 y4 p& z( x. z5 v - servaddr.sin_family = AF_INET;8 G8 Z! P u: Z, i
- servaddr.sin_addr.s_addr = inet_addr(ip);
2 @$ K: c0 y$ d) }$ k2 B% G3 m5 h' G - servaddr.sin_port = htons(port);7 B% G# ^, M: E. M- E$ u1 n+ e' Z' K
- int ret;
- G; H9 s: \3 C! E+ w - IF( (*socket_found = socket(AF_INET , SOCK_STREAM , 0)) == -1)
% h5 F7 ?- z: B+ @1 m* J3 z$ _) `: ] - {
. ^+ _& [$ v! Q. ~# Z' @; I/ G - printf("Create socket error: %s (errno :%d)\n",strerror(errno),errno);
' T+ C, t5 j* U4 F. ?# J! Y5 o - return -1;
9 q7 @2 O' ~5 \" r9 T - }
' ~- R$ ]3 c! {& _6 } - int on = 1;
) _, L+ `; v" [: j6 M9 x, ~! ~ - if(setsockopt(*socket_found , SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
' G; K0 H- @* G: m - {! Z2 Z5 o3 a4 D1 \" ~; x& j
- printf("setsockopt error\n");
# k1 T7 w! M( i/ J% C8 t" f& a - }
% S7 r* c; k- w: ] - ret = bind(*socket_found , (struct sockaddr *)&servaddr , addrsize);2 M( `8 {& m( X L" @
- if(ret == -1)
! D2 L; Y) j* _5 J0 N - {# j& G' f- N1 w/ z* s/ e
- printf("Tcp bind faiLED!\n");
) S, f% g: }0 U - return -1;
4 W n. [- ?- b# t4 b - }: _6 }/ t: \/ E, ^
- if(listen(*socket_found , 5) == -1)* M3 w$ p- G3 j1 \' X& L' Y
- {
2 E# ]: T9 P9 m! q) d N+ q - printf("Listen failed!\n");& q' m0 O) Y* T: ?
- return -1;
7 ?4 N+ x4 {& g/ ^ - }1 d2 C4 c) Z9 C$ I4 D6 }0 W
- return 0;1 I1 a6 v$ _( q
- }
复制代码其中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);
, D6 b* C9 _; A, n b! v - void * Thread_TCP_Web_Recv(void *arg)7 X5 M# R5 o8 j [& r% n
- {* f7 R+ R3 U4 x! L: w
- 。。。
2 `# K z9 M) I - while(1)9 Y) c8 C5 y! l' F- x5 l
- {' w* h- Z/ T7 J6 H, L' L+ E2 F
- fd_socket_conn = accept(socket_web_server , (struct sockaddr *)&sockaddr_in_conn , &addrsize);
) s& z) o* R6 Y, D# Q5 K# o+ S: J - printf("fd_socket_conn = accept()\n");
Y# T+ l) W: ~. Z& j" g- ~ - 。。。
: H i, j% G/ L: u* J! g( W1 \ - recv(fd_socket_conn , recvbuf , 1000 , 0);
$ l8 d! {4 R; p }! R" E - }" t- V3 [( f8 i! s' U* Q; L2 y/ l
- 。。。! r9 w3 w0 k( r+ y4 |
- }
复制代码MJPG帧可以使用Grab操作获取,获取到的MJPG帧需要在TCP线程中读,在Grab操作线程中写,这种被多个线程访问的资源需要加锁防止读写冲突,即资源被Grab操作写入时,需要上锁,不允许其它线程访问,操作完成时需要解锁,允许其它线程访问: - pthread_mutex_lock(&pmt);
7 S5 R4 r# _4 t F6 y' e0 \ - pic_tmpbuffer = pic.tmpbuffer;
% |( l- y# W+ h1 D$ s; _) a3 @ - pic.tmpbytesused = buff.bytesused;
2 y# q9 s' P5 {$ } ^) k! O7 c; a4 m9 y - pic_tmpbytesused = pic.tmpbytesused;
1 N+ C( B x/ W, d8 R3 W. K - pthread_cond_broadcast(&pct);) Q7 c* V- Z3 w h8 i
- pthread_mutex_unlock(&pmt);
复制代码线程互斥锁使用之前需要初始化: - pthread_mutex_t pmt;4 q1 u" G2 N# C$ M
- pthread_cond_t pct;0 T0 T6 S) o" M
- int main(int argc, char* argv[])5 v9 }7 |0 D1 a* |3 u8 H! \2 I
- {0 o$ O4 F% C( A: K
- ...
+ @" z. B- [! \) g+ r/ P - TCP_Server_Found(&socket_web_server , (char*)argv[2] , PORT_TCP);
# F& q9 r3 I) e9 j5 M - pthread_mutex_init(&pmt , NULL);
/ S ^1 ?6 {, k/ F - pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);
9 g& u2 X! `$ A: a7 D) y9 M. T' v - pthread_create(&tid_tcp_web_send , NULL , Thread_TCP_Web_Send , NULL);
, O5 L2 U Y: }) g: p5 m) C - ...% Z- Q! D0 p8 j6 E# \
- while(1)
; R+ Q/ o+ |$ J$ g1 ` - {
. f# r: p% j( e; h8 v0 Z - V4l2_Grab_Mjpeg(false , MJPEG_FILE_NAME);
) X4 X9 b3 L6 D; r% J/ l* h q - ...
: H! k( G" V, k4 X4 _6 A - }
# d* t) J* v& K6 _3 i6 J& \0 Z - ...
: B3 v. G _% R R$ y - }
复制代码然后是发送的细节,发送图片文件之前,需要先发送HTTP标准头,这个相当于给发送图片或者其它类型的流数据铺路: - <p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"><span style="text-indent: 32px;">0 ~& c$ D8 c/ {9 w" D% g1 P5 a
- </span></p><p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"></p>
复制代码- #define STD_HEADER "Connection: close\r\n" \
( g. {6 i- U# s1 ]( I; o3 O* e - "Server: MJPG-Streamer/0.2\r\n" \/ G/ O% m i3 n% F5 ?! ~. d7 I
- "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \: `* w# U: G6 h7 T8 p2 v
- "Pragma: no-cache\r\n" \7 Z6 R( A) a* t1 ?- [ s: Q
- "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
% ]% p3 T; o0 G4 N) _ - #define BOUNDARY "boundarydonotcross"
' B3 O( p2 O0 a) q - printf("preparing header\n");7 Y7 M7 Q4 o/ Y' ~
- sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
8 F2 r0 E# Y k; v" s* n - "Access-Control-Allow-Origin: *\r\n" \( J+ T) j$ O' f# D2 z
- STD_HEADER \
6 z' m1 v3 G0 i4 y - "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \" n4 D: u( c$ ]
- "\r\n" \9 c- @2 C& R& I1 S
- "--" BOUNDARY "\r\n");
4 q: _7 v/ Z7 ~3 ~1 |! Z% r* | - if(write(fd, buffer, strlen(buffer)) < 0)
) ~; |9 b9 I* w* H+ h3 c9 G: u1 _ - {
5 D$ r( s' Y/ d) b - free(frame);/ m% a% i8 z: ?* t" h* D
- return;
3 j) V$ y, z) ^! f& e$ ] - }
复制代码发送完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" \
; H9 V! X! `5 O& S# { - "Content-Length: %d\r\n" \$ s! H) b+ q. i4 y9 @5 ~* H$ M
- "X-Timestamp: %d.%06d\r\n" \9 e2 ^: m4 n% Z% g
- "\r\n", frame_size, (int)timestamp.tv_sec, (int)timestamp.tv_usec);' V0 B+ Q9 I, a3 l0 i- @ s& k
- printf("sending intemdiate header\n");
. p5 i9 v8 O' N# p - if(write(fd, buffer, strlen(buffer)) < 0)0 c" A" t1 H: q0 W( B
- break;
& C+ H- a3 P/ y- y5 k+ H - printf("sending frame\n");
( B2 Z# b2 x2 f* F7 G- v# X - if(write(fd, frame, frame_size) < 0)
, w' j' g8 t$ B- M3 W4 [ - break;, o7 G1 `. l+ i- Z C. N, M
- printf("sending boundary\n");
. k' W0 {0 n! ?4 \ - sprintf(buffer, "\r\n--" BOUNDARY "\r\n");! y3 z. U6 j0 p7 i% @+ z4 l" ~3 ]) D" X
- if(write(fd, buffer, strlen(buffer)) < 0)$ x$ [2 l+ n0 r& j
- 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指令,从客户端使用者角度来看的效果就是网页一直在等待。 / K( ?( T0 d9 F5 L# l i5 U
$ P4 t& i- e. \二、UDP上位机UDP发送操作,同样需要先建立UDP Socket:$ E4 B1 o) U/ ?# y# y
- int UDP_Send_Found(socklen_t* socket_found , struct sockaddr_in *addr , char* ip , int port)6 G* U7 p8 m% @+ v3 |1 {
- {
; `8 Y1 f; Z2 r N% {2 y - *socket_found = socket(AF_INET, SOCK_DGRAM, 0);& `4 h- V! Z0 m/ U2 a& s7 x
- if(*socket_found == (~0))
) _. s' {, J/ s$ \% n& { - {( b. `/ l" E+ x! U9 a/ X4 t
- printf("Create udp send socket failed!\n");
+ R; P* E+ K1 |2 Q! q - return -1;8 O2 w: N- g: E1 T: q# Z& Z( q" x
- }
3 p0 Y5 I' C* D. @% i. b - addr->sin_family = AF_INET;: M& E: b3 V2 G" d- c( u9 e
- addr->sin_addr.s_addr = inet_addr(ip);
+ Z3 V+ k2 J" N! l - addr->sin_port = htons(port);
( r3 \* | A. |7 ] - memset(addr->sin_zero, 0, 8);, b, R, q3 ?# _1 X% X8 P: t
- return 0;6 y( ]' a# r* @& k! K
- }
复制代码 ; b& D" ?, p/ G9 P5 w
/ _3 G w. ~9 C* {, s8 F而UDP文件发送则要比HTTP发送简单得多,只需要将文件切片,每一片为固定长度的UDP帧长度,逐帧发送即可:
3 Z8 P6 l1 o* e
: S, z+ h# G/ A" Q! j6 B4 K8 O$ i) I9 T3 G: p
- while(fend > 0)
( I0 E4 b4 C- w; V c& \+ Z' S - {/ `0 p( W! Y9 x' @, N( `% T
- memset(picture.data , 0 , sizeof(picture.data));* L3 {7 j+ ^. E2 _% ?# Z! U; z
- fread(picture.data , UDP_FRAME_LEN , 1, fp);
, x7 E: u6 e, ~) { - if(fend >= UDP_FRAME_LEN)! I' N+ o( ^) v
- {- A' Z! Z2 K/ y! X" e& h$ I+ s
- picture.length = UDP_FRAME_LEN;1 Q# y& r0 d. A2 U& x4 Z& N
- picture.fin = 0;
, w3 _- l6 R, J: `# ^, w( u) Y - }
0 R; O+ S3 N. v- j - else N' n0 t1 K/ H$ V& h1 L, g
- {2 i V4 ?: L; C
- picture.length = fend;
' ~ s; o' v8 B+ R/ P - picture.fin = 1;; B1 F) r# u+ p( K6 M( k
- }& n* M8 \ A' }: q; M3 v/ B& s
- //printf("sendbytes = %d \n",sendbytes);
; L, o; Z) x4 i4 L; p) b3 E8 p - sendbytes = sendto(socket_send, (char *)&picture, sizeof(struct Package), 0, (struct sockaddr*)&addr,addr_len);
\% K& c" Z, N. D) F; z" K - if(sendbytes == -1)0 s' h4 s; m0 Z" \/ Q7 n
- {7 h+ I' ]+ R C
- printf("Send Picture Failed!d\n");
( l4 M& d3 E2 x7 q - return -1;0 p/ P% ^- L8 [8 \
- }
4 _$ U. z; ^+ s. s! m$ `" p5 N; P - else
& E, ]$ e8 x% f" H6 O* W0 t" {! A - {
7 w9 b; I7 r- S f2 E2 u - fend -= UDP_FRAME_LEN;
0 y) T5 K) ?7 {1 m. n - }! w! S: v' r* L! Y; p( m4 J+ s# ~
- }
复制代码
, x3 J ?! T0 X% }
) x: E: ]/ K% L( T8 j& @+ n: L" o' [- y
) y+ d& `6 W- W3 M7 T5 Y+ {2 yiMX8MPlus 核心板: https://www.forlinx.com/product/136.html |