本帖最后由 飞凌-marketing 于 2022-1-27 11:00 编辑
$ ?9 m+ h. m; }- n* E: {
' k! ]/ H, ^3 {1 D
) v T% f4 b: M7 `5 Q作者|donatello1996 来源 | 电子发烧友 题图|飞凌嵌入式 iMX8MPlus 核心板: https://www.forlinx.com/product/136.html. V$ e; }& i6 S8 ?: D& q
% G. T; ]0 L- U- c7 J( u本文采用的硬件板卡为飞凌嵌入式OKMX8MP-C开发板,系统版本Linux5.4.70+Qt5.15.0,主要介绍基于HTTP网页服务器和UDP上位机的MJPG码流传输。 MJPG格式作为一种持续传输的视频码流,在远程监控领域中应用较广,而实现这种远程监控的第三方应用最常见的有两种:浏览器HTTP网页、UDP上位机。
9 D2 ]/ l$ ^% G1 U! f" J
2 Y- F# x/ f# e两者各有优势,对比鲜明,其中: 这两种应用各有优缺点,对于嵌入式开发者来说,两者都必须掌握。
7 D0 V/ S1 J9 D( T2 o! h7 D一、HTTP网页服务器7 I Q. d; ~! B
先说下HTTP网页服务器获取MJPG码流的代码,首先是OKMX8MP-C在开发板端建立TCP服务器: - int TCP_Server_Found(socklen_t* socket_found , char* ip , int port)
7 U4 T' ^5 p/ A3 k- E8 _+ m' j! | - {
4 G* Y# l6 {3 { - struct sockaddr_in servaddr;
: R* A3 I, s$ o% h. i- F( p, Z, V - socklen_t addrsize = sizeof(struct sockaddr); d( h7 }% d5 w% p8 ]
- bzero(&servaddr , sizeof(servaddr));
3 @! x3 s1 t0 `. z3 J - servaddr.sin_family = AF_INET;2 C' p/ H: E: k7 C) U Z; ?
- servaddr.sin_addr.s_addr = inet_addr(ip);( j$ G5 F+ w$ P$ u( I: j
- servaddr.sin_port = htons(port);
- h; K( |! z" | - int ret;
: Q; p7 e; ]4 h( M - IF( (*socket_found = socket(AF_INET , SOCK_STREAM , 0)) == -1)
" h5 {! ^( S! [( r - {$ }* r& H# p ^( n% `6 M
- printf("Create socket error: %s (errno :%d)\n",strerror(errno),errno);( N! s1 M& B0 f7 a* [3 _
- return -1;
' {4 N8 x. u# N" i3 W4 t) h - }
/ y% l" O3 S: L% I( s8 U - int on = 1;
2 b$ A$ j* T: {% b, Y5 g1 j- I# J/ n - if(setsockopt(*socket_found , SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)6 X# k' P/ ?) A. t* a+ S
- {
8 N5 t3 ?/ o( v* N" a - printf("setsockopt error\n");3 u* N1 z: g6 t; Y, i3 y5 C E
- }. k: z- ~9 e/ b& V; ?
- ret = bind(*socket_found , (struct sockaddr *)&servaddr , addrsize);6 h9 `. K+ N$ u+ u0 I6 \+ B% d
- if(ret == -1). a" b8 {- A- n: I1 d' q) J4 d: F
- {
: `5 h8 G8 ^7 H5 ^& n7 }: k( M$ X- v - printf("Tcp bind faiLED!\n");
% t/ h# b$ Q* \' Y* j. S - return -1;
$ z7 {; ~) G$ w: E - }% X, t7 b, d( w; ^7 u
- if(listen(*socket_found , 5) == -1)5 W. T8 X3 x4 @+ [
- {1 Z5 _/ F5 q" p0 r- Y8 L# ^
- printf("Listen failed!\n");+ ]/ m: M9 g+ K4 s; G6 `( m
- return -1;
* d1 y) E: R; D9 c- n - }
1 A8 B% I7 R0 L: r - return 0;
# }- w# Y4 p9 w. k6 ~ - }
复制代码其中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);- w r( D7 U' j. o% p5 }5 ?! A
- void * Thread_TCP_Web_Recv(void *arg)
* ]( p. W! U: i5 |0 S - {
( |; ^# g c4 v5 E - 。。。
: C) S- h! l* e6 C& |( z: P - while(1)6 P/ S) t: g* K, Q) Y
- {# t. q4 c$ a8 y# Z. |; d- |
- fd_socket_conn = accept(socket_web_server , (struct sockaddr *)&sockaddr_in_conn , &addrsize);( \* Z. F) h% J" T( J" |# R
- printf("fd_socket_conn = accept()\n");
# W8 g& Y( q2 c8 A' K - 。。。
9 @3 I3 K) h" `- W7 k - recv(fd_socket_conn , recvbuf , 1000 , 0);" ?6 @3 r( {7 u* I' {: e6 h
- }) V8 P2 g5 Z3 [" @7 V
- 。。。$ P. C+ d G* z( T3 I; D2 W/ F! c
- }
复制代码MJPG帧可以使用Grab操作获取,获取到的MJPG帧需要在TCP线程中读,在Grab操作线程中写,这种被多个线程访问的资源需要加锁防止读写冲突,即资源被Grab操作写入时,需要上锁,不允许其它线程访问,操作完成时需要解锁,允许其它线程访问: - pthread_mutex_lock(&pmt);
8 N6 E5 t, m- A( i6 v2 d% m - pic_tmpbuffer = pic.tmpbuffer;6 N( w. `/ v% I, O: I
- pic.tmpbytesused = buff.bytesused;
3 x8 |& b# M, V( C - pic_tmpbytesused = pic.tmpbytesused;: G$ v$ i0 o# W( E* R
- pthread_cond_broadcast(&pct);
8 N# l |* G1 M/ J$ d: a$ w) U - pthread_mutex_unlock(&pmt);
复制代码线程互斥锁使用之前需要初始化: - pthread_mutex_t pmt;
$ o) ]0 F. y- t2 p" \+ v* } - pthread_cond_t pct;
3 j$ m6 p' l3 m9 q - int main(int argc, char* argv[])
! H2 z, L/ i8 W) a0 T+ \ - {
: a/ L6 U, l# j" c( Y7 W - ...
# k, { l( h4 h - TCP_Server_Found(&socket_web_server , (char*)argv[2] , PORT_TCP);/ L! u6 A8 V# R+ N8 Z' J
- pthread_mutex_init(&pmt , NULL);: n1 S* |! H! t0 ^% M# S
- pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);
& i0 k. O: L$ o7 l! L2 H - pthread_create(&tid_tcp_web_send , NULL , Thread_TCP_Web_Send , NULL);
* e/ k3 m% S6 n8 t - ...
, w2 V0 E- X- I1 g - while(1), k6 A( }0 D \; T5 o) w! [8 m
- {
, k, g7 t( A% [0 N - V4l2_Grab_Mjpeg(false , MJPEG_FILE_NAME);( C( d1 j$ e, K8 o. G/ l6 K; [# u
- ...
8 L' I+ K& c o; L/ C! e) d- O - }
z! ~: R( z. a4 e& a - ..." ^2 n" Y- ^4 Y% }% e
- }
复制代码然后是发送的细节,发送图片文件之前,需要先发送HTTP标准头,这个相当于给发送图片或者其它类型的流数据铺路: - <p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"><span style="text-indent: 32px;">- o/ }! A H9 _: X, T" 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" \
) o9 [) n$ D& T8 { - "Server: MJPG-Streamer/0.2\r\n" \0 a! o" T' g( f F5 c7 d0 d5 a
- "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \6 m* v) K9 f V: A: ~8 k6 m
- "Pragma: no-cache\r\n" \
. k% B. B4 q) s! N - "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"' E8 j3 p! \; S/ L' X
- #define BOUNDARY "boundarydonotcross"
0 g& q0 _& j8 J - printf("preparing header\n");& O$ ]- Z4 E: U1 ~' w
- sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
7 R, S7 u4 l6 N! i - "Access-Control-Allow-Origin: *\r\n" \8 S6 n- Y" }+ M& B: O5 K
- STD_HEADER \
3 H3 m# @" H% M7 M - "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \
2 h8 B# h# _5 Q7 u" [$ C - "\r\n" \. Z5 q1 n1 Q! E. }
- "--" BOUNDARY "\r\n");! g$ W' S i! @" c9 S
- if(write(fd, buffer, strlen(buffer)) < 0)/ L8 {; M/ m' A0 x x7 s+ D
- {- C7 ~8 A% v/ x6 f" q0 t: n
- free(frame);8 f) T |0 {7 s( r- ] a
- return;
5 c e; u' v5 g: s9 x2 p0 Q - }
复制代码发送完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" \
/ `% c, o: E/ Q' M: u- d) x - "Content-Length: %d\r\n" \- q3 Q; _* s4 g/ X3 _7 R3 U
- "X-Timestamp: %d.%06d\r\n" \( ~3 [' ^1 s" |- l7 [
- "\r\n", frame_size, (int)timestamp.tv_sec, (int)timestamp.tv_usec);* x5 m& _: | } z+ I
- printf("sending intemdiate header\n");2 q3 B) y+ g7 }# \2 Y
- if(write(fd, buffer, strlen(buffer)) < 0)9 d; y0 n+ [, z/ u$ q% F" O
- break;
: m1 q3 Q, ` f - printf("sending frame\n");$ E- W& v4 e$ V
- if(write(fd, frame, frame_size) < 0)- m( d% R/ x2 I0 n9 o& K# N
- break;, i/ g8 e5 U# b( O% i7 b T8 s
- printf("sending boundary\n");! e4 c1 E+ }! I
- sprintf(buffer, "\r\n--" BOUNDARY "\r\n");- e: j9 E: q5 T8 H' a* M+ P2 Y8 V
- if(write(fd, buffer, strlen(buffer)) < 0). [4 M- u5 `. x/ h% C
- 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指令,从客户端使用者角度来看的效果就是网页一直在等待。 0 g' P+ C4 }1 w0 \0 @+ z: R
7 r1 Z! ^# J/ r3 X
二、UDP上位机UDP发送操作,同样需要先建立UDP Socket:0 b3 b3 {' p" i, \0 t4 T4 r
- int UDP_Send_Found(socklen_t* socket_found , struct sockaddr_in *addr , char* ip , int port)
. O- T6 l8 x9 s) W - {
# v% y) d H/ ^$ N' g - *socket_found = socket(AF_INET, SOCK_DGRAM, 0);
8 T$ s& c: D. r# A! S - if(*socket_found == (~0))1 D' F" Q. H8 d' J
- {
. o B Y1 m. m% [ - printf("Create udp send socket failed!\n");3 F8 b- \$ a5 j
- return -1;; q; e- L3 J9 T# ]+ A% n) r
- }
D# k- H& H* ^6 T - addr->sin_family = AF_INET;/ r+ ?. _$ z. W& j- z
- addr->sin_addr.s_addr = inet_addr(ip);8 M7 W& G; k) l6 d
- addr->sin_port = htons(port);
' y# e9 O" m' P, ^; [- B4 J - memset(addr->sin_zero, 0, 8);
; `1 { p4 ?$ K6 r+ V" }3 | - return 0;, f# j( C; ], e
- }
复制代码 ; B' ?# f0 b* N7 @* A& |8 |. t# U6 p$ C
7 X- M8 G$ M9 u. Y- Z5 k6 a- j3 K
而UDP文件发送则要比HTTP发送简单得多,只需要将文件切片,每一片为固定长度的UDP帧长度,逐帧发送即可:. v% E6 g. F: v
. h v) [* G6 v
. K+ D7 E: g5 x) W' C% ?
- while(fend > 0)
6 m4 e/ u- n4 @ - {
n! \8 O8 C7 M% a V% P - memset(picture.data , 0 , sizeof(picture.data));6 F9 Q5 K$ W% C3 z) F/ l1 N# u
- fread(picture.data , UDP_FRAME_LEN , 1, fp);3 p! Y. P6 U- x2 ~) B
- if(fend >= UDP_FRAME_LEN)9 e9 @1 C0 l; z, \* _: l- L! P: [
- {9 A+ f% g! b0 ~9 Q
- picture.length = UDP_FRAME_LEN;
/ C0 I: Y3 U) N' L! Q! } - picture.fin = 0;
7 c( n% n# w8 ^" Q( m) F% p+ d4 A - }
# Z9 q' A2 `; K - else, J* l1 e6 ]6 L3 K, l
- {
+ ~& A' j5 c- S8 i$ q2 U - picture.length = fend;+ d( G; [8 ?$ [% D5 S% v& E, p
- picture.fin = 1;
( u+ j/ h- A2 h3 L8 D# S# F - }% H5 G# `5 ^/ t/ }( V: L
- //printf("sendbytes = %d \n",sendbytes);
9 }; n# u: F3 E2 T - sendbytes = sendto(socket_send, (char *)&picture, sizeof(struct Package), 0, (struct sockaddr*)&addr,addr_len);7 r: l2 }# z+ v
- if(sendbytes == -1)
' q, N: F; b$ Z9 \2 I' x( N G - {% R6 N3 V4 X, Z9 w) g
- printf("Send Picture Failed!d\n");" Q# O$ ?3 }+ i! Z$ ]2 Y" {2 ~- p3 y
- return -1;" A/ a! S1 d8 R; t& q6 g
- }) O8 T7 u( E" o6 G/ t% @" o
- else
7 ]6 [4 A! }6 I" L# q5 c - {( O4 @& }/ {( s" r& S
- fend -= UDP_FRAME_LEN;5 b5 @, I2 s4 X+ g+ q
- }5 F5 a( H/ u3 w& \
- }
复制代码
8 \& x. o, n8 T% P: @/ \
4 ^" u# E4 b) I- t; a# u1 Z3 y; @9 |% Q
) l9 j! ]7 I- c% ~
iMX8MPlus 核心板: https://www.forlinx.com/product/136.html |