本帖最后由 飞凌-marketing 于 2022-1-27 11:00 编辑
& ~$ k. n+ I/ e* c+ V$ }% x5 ]# I1 y: O' x) a. R! c# X
 0 C* N( ~; Q( W9 c: B- e% \8 u6 i
作者|donatello1996 来源 | 电子发烧友 题图|飞凌嵌入式 iMX8MPlus 核心板: https://www.forlinx.com/product/136.html) V" u; v! Q" l" e
/ f, {) P+ J. a8 g0 p+ `0 m2 |本文采用的硬件板卡为飞凌嵌入式OKMX8MP-C开发板,系统版本Linux5.4.70+Qt5.15.0,主要介绍基于HTTP网页服务器和UDP上位机的MJPG码流传输。 MJPG格式作为一种持续传输的视频码流,在远程监控领域中应用较广,而实现这种远程监控的第三方应用最常见的有两种:浏览器HTTP网页、UDP上位机。
1 V) t) T( g g$ Q# w
# M, v2 W* }1 _) r5 D) g$ Z/ ?两者各有优势,对比鲜明,其中: 这两种应用各有优缺点,对于嵌入式开发者来说,两者都必须掌握。
8 C- X% K, P0 z7 K/ C一、HTTP网页服务器
6 Z7 a/ z# ]- H4 }: @先说下HTTP网页服务器获取MJPG码流的代码,首先是OKMX8MP-C在开发板端建立TCP服务器: - int TCP_Server_Found(socklen_t* socket_found , char* ip , int port)
8 m, L' f- |, V: \! z - {! }9 Y1 N. P5 W' i
- struct sockaddr_in servaddr;
! D* g# B/ l& z& J7 c; R0 a - socklen_t addrsize = sizeof(struct sockaddr);/ o5 |9 i$ F- E, Y& s
- bzero(&servaddr , sizeof(servaddr));
. L7 D- e& S, Z! S3 o, k - servaddr.sin_family = AF_INET;
: z3 W% m8 t" o - servaddr.sin_addr.s_addr = inet_addr(ip);0 P5 a7 R6 c. o; |0 u _. C
- servaddr.sin_port = htons(port);
/ r* C+ V9 [9 @1 ? - int ret;
; f; C& X5 O/ Q - IF( (*socket_found = socket(AF_INET , SOCK_STREAM , 0)) == -1)
6 P3 V/ q$ v3 f - {
3 ~2 F* F% S* Q - printf("Create socket error: %s (errno :%d)\n",strerror(errno),errno);$ r' D% W, e! K9 ]4 U# J
- return -1;% ? ?$ [ J! t! c' Y& l7 i
- }
5 y. w/ q/ F- N7 o - int on = 1;
$ A( E) f+ S+ N: r. Z! j) v: e1 x - if(setsockopt(*socket_found , SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
3 Z. L% Z. o. T7 @. X* U; \. p - {
8 E7 q+ q& Y3 _. c - printf("setsockopt error\n");
9 H, M# C, X! r - }
4 j' N+ j& {; S( E# f# P - ret = bind(*socket_found , (struct sockaddr *)&servaddr , addrsize);
) Q5 P. @; q- }/ f5 ^ - if(ret == -1)
% B! y7 c- ]3 \4 v - {& j9 q; X: _/ x/ a' O6 e
- printf("Tcp bind faiLED!\n");
% S* Y# X5 ^0 x e$ {$ h; _ - return -1;
0 O; s4 _8 F7 K$ c; J - }, i6 u. \; V/ Y2 f
- if(listen(*socket_found , 5) == -1)# X9 u6 B* s8 k! I
- {. V4 N) r: C* ^ p* i" J' ?. x# ?2 I
- printf("Listen failed!\n");
' }( L* }- [+ @# v1 i - return -1;
0 y" Z' s& `7 ?" s3 o - }$ `1 B3 }" n9 B% C
- return 0;1 r& p! } _, E' N
- }
复制代码其中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);* G& l5 j) R$ ^, n1 y
- void * Thread_TCP_Web_Recv(void *arg)5 b. e3 [3 K5 ?+ g9 @, x
- {
* i8 M0 v2 F- a; t7 y2 b0 Z3 q - 。。。* X. M& C2 u/ L; G2 n' b, u
- while(1)" W* a: f- ~6 v1 E9 x( C" n
- {- l$ Q. u" |2 F2 u
- fd_socket_conn = accept(socket_web_server , (struct sockaddr *)&sockaddr_in_conn , &addrsize);' g6 K( C: N* n/ a
- printf("fd_socket_conn = accept()\n");. V$ t' ]" a; B# w, D/ F6 J
- 。。。
3 |0 d) s0 @2 i, R, r' y - recv(fd_socket_conn , recvbuf , 1000 , 0);( ~1 x' l" z E& N' ]
- }/ v1 ?' ]/ d9 y: e P9 r
- 。。。
0 K: A8 b' D* a7 N) _ - }
复制代码MJPG帧可以使用Grab操作获取,获取到的MJPG帧需要在TCP线程中读,在Grab操作线程中写,这种被多个线程访问的资源需要加锁防止读写冲突,即资源被Grab操作写入时,需要上锁,不允许其它线程访问,操作完成时需要解锁,允许其它线程访问: - pthread_mutex_lock(&pmt);
3 j- R6 o, ?* u& x' W- Z* j" Z4 B - pic_tmpbuffer = pic.tmpbuffer;3 V: q& q0 M2 k% I, n8 h
- pic.tmpbytesused = buff.bytesused;* x$ W- z/ _ [: }3 [
- pic_tmpbytesused = pic.tmpbytesused;2 R5 u' m# n) U5 Q, X
- pthread_cond_broadcast(&pct);
2 C' [- W& l+ W9 K$ Y' `' V4 I - pthread_mutex_unlock(&pmt);
复制代码线程互斥锁使用之前需要初始化: - pthread_mutex_t pmt;$ A) a& f1 B2 |! f3 r& a
- pthread_cond_t pct;8 A: v' c/ ^9 |2 R3 |2 X
- int main(int argc, char* argv[])
6 v" C# ]5 m+ V8 G% d7 _ - {
$ e4 N, V1 |6 m7 E - ...
7 e( }' P i. T, [! K - TCP_Server_Found(&socket_web_server , (char*)argv[2] , PORT_TCP);
' M; F; h' r- V% y, x - pthread_mutex_init(&pmt , NULL);1 d: N" q9 P: X, W% Z
- pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);
+ E! K* o/ S; r6 x - pthread_create(&tid_tcp_web_send , NULL , Thread_TCP_Web_Send , NULL);
+ I" a, @1 u$ h) i6 E0 m) N - ...& A) {) z4 n* N8 N7 P# u: x
- while(1)
0 \2 |8 y8 Z6 s9 v) G Q - {3 J1 t* C# X' `) l/ z
- V4l2_Grab_Mjpeg(false , MJPEG_FILE_NAME);
6 }; I% r; {$ `8 Q$ f' ~7 |5 n" c - ...
) D) \ r" I, H* S9 B - }6 ^5 l1 D0 v8 _ T7 G
- ...2 z+ R- l ^2 ?, ?8 s( p
- }
复制代码然后是发送的细节,发送图片文件之前,需要先发送HTTP标准头,这个相当于给发送图片或者其它类型的流数据铺路: - <p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"><span style="text-indent: 32px;">+ e, Z% v V; ]$ T; [, \* ]1 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" \ x2 G0 h2 P$ d9 x0 y* n+ n
- "Server: MJPG-Streamer/0.2\r\n" \
( f+ r1 R( w3 w' a7 T - "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \+ |7 T8 C5 H9 y* _. T3 p, @
- "Pragma: no-cache\r\n" \7 f% X' Y2 L$ G5 N: O
- "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"
! a/ r3 d: n+ r - #define BOUNDARY "boundarydonotcross"
x- V& S: y# ~ - printf("preparing header\n");5 d$ l/ @2 a6 ]/ I" r& F& W
- sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
: x- ^. U8 D- C/ p( V# `7 V) K - "Access-Control-Allow-Origin: *\r\n" \3 E# B$ K1 P# l5 B5 e
- STD_HEADER \
8 U. P& p; a" t/ b' u - "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \% }7 {2 A3 \8 \0 z
- "\r\n" \/ v- Y7 B% p: f5 \2 m
- "--" BOUNDARY "\r\n");! O. m* [; {5 M$ ~/ H2 O
- if(write(fd, buffer, strlen(buffer)) < 0)
7 K+ a3 w! ~' L" S$ @- z6 z - {
$ @ Q4 i, o" C2 ?/ A9 S" i% i2 O - free(frame);6 r; e1 t2 q& x4 e, B. Z
- return;/ l" l1 H% ~4 x: K( e x( }; m
- }
复制代码发送完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" \
0 X% b. u3 o2 b - "Content-Length: %d\r\n" \# A1 B( M( u4 [4 G5 t
- "X-Timestamp: %d.%06d\r\n" \
) E4 f6 ^) b; V7 _ - "\r\n", frame_size, (int)timestamp.tv_sec, (int)timestamp.tv_usec);
( D* z, e9 e7 W& _! v - printf("sending intemdiate header\n");
- r) i6 f+ {, u X$ U% y - if(write(fd, buffer, strlen(buffer)) < 0)
0 \( B/ L. a; W - break;3 V& W% H0 O" P* M4 J
- printf("sending frame\n");. {) a1 \+ x9 t, ^ }" R% v
- if(write(fd, frame, frame_size) < 0)
I! X1 l% g4 {$ n | - break;
8 f3 G. M6 j9 U2 Q - printf("sending boundary\n");4 f2 G: H; ^) u2 X
- sprintf(buffer, "\r\n--" BOUNDARY "\r\n");
& T' V! W9 T i, Q+ w - if(write(fd, buffer, strlen(buffer)) < 0)8 `* [2 p) \& F9 v7 M6 S
- 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指令,从客户端使用者角度来看的效果就是网页一直在等待。 
% e0 `1 v: `+ g
3 j& H2 J: b4 v, u7 {9 q2 s m$ y二、UDP上位机UDP发送操作,同样需要先建立UDP Socket:
/ ~8 J; H& y+ c( B9 Z- int UDP_Send_Found(socklen_t* socket_found , struct sockaddr_in *addr , char* ip , int port)
3 ?, ]* M5 y; I( h+ L$ ?* W( V - {8 b1 ^& l- d3 y4 b) z$ `1 A$ |1 w
- *socket_found = socket(AF_INET, SOCK_DGRAM, 0);
9 y0 S% m+ o% E8 N - if(*socket_found == (~0))/ u" d: w B6 [- @* h
- {/ M& X3 q* a* h6 F1 b9 ^7 r
- printf("Create udp send socket failed!\n");. X' b. W% W4 D
- return -1;
! F" ?. C& q- G: l, s8 X! g - }" |4 N3 p+ Q* A3 W/ s" d/ C
- addr->sin_family = AF_INET;
$ F3 ~4 o. T: `, F1 ]4 `1 U# c3 h - addr->sin_addr.s_addr = inet_addr(ip);
- j# _& J) m6 \: U6 A* J/ w) y. W% ] - addr->sin_port = htons(port);
/ `" R0 F" S9 k - memset(addr->sin_zero, 0, 8);
9 K2 @7 ]& ]/ N - return 0;. Y- H3 u+ e2 I+ @: e2 w
- }
复制代码 6 I! E* S, h, R4 T4 n$ ]
7 U. z4 j+ V; k/ _
而UDP文件发送则要比HTTP发送简单得多,只需要将文件切片,每一片为固定长度的UDP帧长度,逐帧发送即可:# H& D! o) h1 a7 k. f
0 f+ z6 x% @5 t( Y2 v U3 i" H& _# p: S K1 w9 i2 }
- while(fend > 0)
8 @( Z7 l7 I- O& N- m8 n - {* B$ a/ m. s C4 F/ M6 ?
- memset(picture.data , 0 , sizeof(picture.data));" G7 b3 V! U8 k
- fread(picture.data , UDP_FRAME_LEN , 1, fp);9 X3 y9 E3 e; d: C7 j% X; |
- if(fend >= UDP_FRAME_LEN); v, s5 R$ c* f4 ^! @
- {! T9 W! r: u0 k
- picture.length = UDP_FRAME_LEN;5 w( N0 w% t1 o0 y7 G4 h- }
- picture.fin = 0;; H' x" Q g0 _, F" f
- }
. n8 u5 Y1 S: p5 `) M$ C - else% }1 ~4 _% T' _) r" J
- {
2 M6 \' @, L+ p) N5 y* ^) \5 [ - picture.length = fend;$ J& s! Y' y! L# Z: Z
- picture.fin = 1;
. t; e% r+ R) R8 i - }
M: b, @9 @3 v7 P: R1 R' e - //printf("sendbytes = %d \n",sendbytes);) {0 @# C, @, {3 i- B+ C5 P- l
- sendbytes = sendto(socket_send, (char *)&picture, sizeof(struct Package), 0, (struct sockaddr*)&addr,addr_len);: f0 K; p5 `) D7 p( K
- if(sendbytes == -1)! L# J& A. d; C, n J7 S
- {5 S/ g( c9 r" A# G8 |' c3 D
- printf("Send Picture Failed!d\n");$ t. t0 S6 J- W$ B& E/ \+ p
- return -1;
: Y' a w: E3 l; ^# E' @ - }& x& E% T" k2 S& S
- else
$ U1 @! G0 B. \ E* |1 r2 y - {
/ R0 g( x+ A* Z - fend -= UDP_FRAME_LEN;
3 H E! B3 P% u0 D% p% V - }- V2 n+ z7 H; A" a! g" H8 N
- }
复制代码 2 M" q# j, |1 Y( ]9 W
2 V0 |; L( B: H. H# Q$ [7 L
- \7 b" Y% C$ u- T7 L$ b/ ?/ M6 ]9 J. G
iMX8MPlus 核心板: https://www.forlinx.com/product/136.html |