本帖最后由 飞凌-marketing 于 2022-1-27 11:00 编辑 + U' Y: _% X" F8 l+ M4 h
; j1 t; L0 }8 A6 E8 d# O
+ [1 a% W0 _) n, ]. p) b作者|donatello1996 来源 | 电子发烧友 题图|飞凌嵌入式 iMX8MPlus 核心板: https://www.forlinx.com/product/136.html5 C5 f1 ~& ]* M
, n# N& O+ M1 F* E% A' e4 l8 E% L本文采用的硬件板卡为飞凌嵌入式OKMX8MP-C开发板,系统版本Linux5.4.70+Qt5.15.0,主要介绍基于HTTP网页服务器和UDP上位机的MJPG码流传输。 MJPG格式作为一种持续传输的视频码流,在远程监控领域中应用较广,而实现这种远程监控的第三方应用最常见的有两种:浏览器HTTP网页、UDP上位机。 7 x6 P: t. b8 ]+ j

# v' V( h+ G3 X两者各有优势,对比鲜明,其中: 这两种应用各有优缺点,对于嵌入式开发者来说,两者都必须掌握。 ' ? y. ^% D+ q
一、HTTP网页服务器
* k9 n+ R3 i# ~* x8 n2 B先说下HTTP网页服务器获取MJPG码流的代码,首先是OKMX8MP-C在开发板端建立TCP服务器: - int TCP_Server_Found(socklen_t* socket_found , char* ip , int port)
, s. b5 t8 u, j - {2 ?% q+ ]! l$ x% @; G
- struct sockaddr_in servaddr;
- p8 J+ F! S$ S - socklen_t addrsize = sizeof(struct sockaddr);" {# D0 Z3 \8 [/ F* K
- bzero(&servaddr , sizeof(servaddr));
4 x! u) E" O7 S, z0 R9 ?: l - servaddr.sin_family = AF_INET;
X- b9 A9 c# L4 M4 @3 F0 S - servaddr.sin_addr.s_addr = inet_addr(ip);
3 |7 L: `( G; O) E( p) ? - servaddr.sin_port = htons(port);
8 ~7 `) Y8 W. j$ [3 o - int ret;
, s2 j( n5 k( f" [! O4 H/ j - IF( (*socket_found = socket(AF_INET , SOCK_STREAM , 0)) == -1)
, l) Z: C. c8 ?+ H# u - {
" [4 _+ |3 F( X9 r: T1 p8 z0 S - printf("Create socket error: %s (errno :%d)\n",strerror(errno),errno);
1 k6 O2 k- L+ l2 r( m7 M3 U% h - return -1;8 [$ P9 V3 o9 Y' o; E; g
- }
" y1 s$ s& h. R, ` - int on = 1;
* Y+ H+ _# {6 P) {% D) U - if(setsockopt(*socket_found , SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0); i W4 V1 k$ Q; j( m
- {% G$ W8 V$ r, F7 u& D% Y, A8 J& \- ^
- printf("setsockopt error\n");
9 \& B7 p( v! Z9 A; j# ^! J - }+ W q* h9 \, s) ~; r+ ~& N
- ret = bind(*socket_found , (struct sockaddr *)&servaddr , addrsize);
. S1 w% B; m; H& a L. V% ^ - if(ret == -1)
) G0 s2 ~; w. ]0 Y& ?% { - {, O3 S% \$ D% D: O+ a
- printf("Tcp bind faiLED!\n");) n$ b( j" F( H
- return -1;( l" u4 o+ A5 X" g
- }: b8 E F& B5 N% a4 n7 l
- if(listen(*socket_found , 5) == -1). m" a7 Q; h* ]' I: W& }5 G
- {
" |9 G6 k8 J% c- y2 G) L) X% H* s2 p - printf("Listen failed!\n");
' Z- Q8 k4 `. f% j# f - return -1;
$ l: x% a8 Q Z* w7 I9 k - }( {+ W. ^# x7 y9 t; S+ n
- return 0;: a" M) J, J0 ?( O3 x
- }
复制代码其中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);& L$ |4 k U2 F
- void * Thread_TCP_Web_Recv(void *arg): O" j& Q: A& D$ [8 Q! Y& }
- {
% p3 B7 k( \, Y' o) d" V' I& o- S" k - 。。。9 G* D" K' P: S4 k
- while(1)6 h# R- h; O6 s! p" N. ~
- {
& V: g) v, _% _2 a$ F - fd_socket_conn = accept(socket_web_server , (struct sockaddr *)&sockaddr_in_conn , &addrsize);7 Q5 J- |" h, c* q& a6 K. d
- printf("fd_socket_conn = accept()\n");0 M- U5 P! n+ q) U4 C' E/ f
- 。。。7 I! B. e1 ^* t- @/ b8 j# g; |- v" p
- recv(fd_socket_conn , recvbuf , 1000 , 0);
* B- y. \; a! D: k% {( g - }
; n9 t2 d# O& w5 V$ _3 ] - 。。。+ w$ V3 w& q3 H
- }
复制代码MJPG帧可以使用Grab操作获取,获取到的MJPG帧需要在TCP线程中读,在Grab操作线程中写,这种被多个线程访问的资源需要加锁防止读写冲突,即资源被Grab操作写入时,需要上锁,不允许其它线程访问,操作完成时需要解锁,允许其它线程访问: - pthread_mutex_lock(&pmt);
0 y/ E. m, r/ Q* L - pic_tmpbuffer = pic.tmpbuffer;+ m7 F! x; {* H; [: j+ w- x
- pic.tmpbytesused = buff.bytesused;. J9 a; l5 r9 @ X
- pic_tmpbytesused = pic.tmpbytesused;. x7 w7 |7 c! c/ Z) g& ~
- pthread_cond_broadcast(&pct);
" [0 E2 o: h: ]7 J. T - pthread_mutex_unlock(&pmt);
复制代码线程互斥锁使用之前需要初始化: - pthread_mutex_t pmt;
m8 c: J( U9 [! u - pthread_cond_t pct;- G' g" f; k) B& z2 Q$ h
- int main(int argc, char* argv[])
% ~: `4 B+ Y1 }# w4 J( m% @ - {* D$ D% @. Q H! Q+ I
- ...! o2 n6 q3 l! \+ X1 P; \- ], {
- TCP_Server_Found(&socket_web_server , (char*)argv[2] , PORT_TCP);, _* L2 E9 k" j" I
- pthread_mutex_init(&pmt , NULL);
9 l3 \6 R6 J$ C$ |) M7 k - pthread_create(&tid_tcp_web_recv , NULL , Thread_TCP_Web_Recv , NULL);
+ s$ z- q* F, a# i - pthread_create(&tid_tcp_web_send , NULL , Thread_TCP_Web_Send , NULL);
{6 m0 `$ m# l1 A; A) N - ...+ ]. z$ s. K7 R' _$ g/ L" x
- while(1)
2 S3 ]; v- C5 G - {
7 s- ` }; p2 _- t! b. @ - V4l2_Grab_Mjpeg(false , MJPEG_FILE_NAME);
2 z$ \2 O E7 U: H+ M - ...
" |5 a, F. s7 B8 w - }
* Q/ H X# Z2 U, ^& s2 f - ...
( E6 Y" h" A( S7 ] - }
复制代码然后是发送的细节,发送图片文件之前,需要先发送HTTP标准头,这个相当于给发送图片或者其它类型的流数据铺路: - <p style="box-sizing: border-box; border: 0px; vertical-align: baseline; line-height: 26px;"><span style="text-indent: 32px;">5 d' ^6 d! x4 I$ {7 i6 r. w( B& 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" \! [0 ?( B$ A: i9 u. e# l- F
- "Server: MJPG-Streamer/0.2\r\n" \
2 l, [) L6 ^4 O3 B8 ] - "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0\r\n" \
0 c5 b; o3 W0 L# b- ] - "Pragma: no-cache\r\n" \
( t1 C0 j# v8 P% w6 J' }' u/ f - "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"0 i& |& O% z( V: ~( A+ R0 `
- #define BOUNDARY "boundarydonotcross". g$ A7 H# Y0 {& K2 y. h1 W2 I
- printf("preparing header\n");1 k% p# h. _& n, ~( P* |/ y
- sprintf(buffer, "HTTP/1.0 200 OK\r\n" \
9 K% F& ^; I" c# X' g$ J) O. j) ^ - "Access-Control-Allow-Origin: *\r\n" \, s0 ~, `% O! G) N/ l
- STD_HEADER \
/ j0 |5 }9 G6 i1 e - "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \9 R0 k' p6 Y6 k+ z/ J9 t2 b7 P
- "\r\n" \
8 T0 b2 d0 a# Z+ p" J! E3 A - "--" BOUNDARY "\r\n");' q- R2 z) ~& F/ U! p& F! P
- if(write(fd, buffer, strlen(buffer)) < 0)
+ T" ` {2 | ?( k3 _/ d - {
! x5 W8 f% ]* d3 y - free(frame);
6 ^% |; t2 x1 t" m9 G6 g - return;
# t9 I0 Y+ M- `2 V" z) N - }
复制代码发送完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" \ q% |) O2 P2 ~
- "Content-Length: %d\r\n" \! w5 y3 p- a; [. q# E+ C4 @5 u% B
- "X-Timestamp: %d.%06d\r\n" \3 f; A* L! j, `& U
- "\r\n", frame_size, (int)timestamp.tv_sec, (int)timestamp.tv_usec);6 |1 v, ?, I% N" o; q5 v
- printf("sending intemdiate header\n"); J9 I/ t/ k3 D0 i3 b8 C) }" w! {
- if(write(fd, buffer, strlen(buffer)) < 0)
( i" e# T# }9 i6 a+ [ ~. a" S - break;
' m9 F) x4 y/ q$ e2 S1 ?! e - printf("sending frame\n");1 N# }/ A- G# z5 Q* D) U
- if(write(fd, frame, frame_size) < 0)
: D' d# q1 d0 v& b7 p - break;
; Z) V/ ]2 l; | - printf("sending boundary\n");" j+ J8 ^! o& D
- sprintf(buffer, "\r\n--" BOUNDARY "\r\n");5 U+ f5 D4 d$ p
- if(write(fd, buffer, strlen(buffer)) < 0)
1 k' G( Q( D" s4 I0 k$ |9 U8 f - 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指令,从客户端使用者角度来看的效果就是网页一直在等待。 
( v) E) x. ]& l
5 j5 e* q b( Q, b S# C( ?4 H二、UDP上位机UDP发送操作,同样需要先建立UDP Socket:
& K* V. ?3 D+ c/ N& Z7 F( N- int UDP_Send_Found(socklen_t* socket_found , struct sockaddr_in *addr , char* ip , int port)3 w+ }0 W' y9 Q, c+ L
- {
; h M( Q0 \% W7 E& L - *socket_found = socket(AF_INET, SOCK_DGRAM, 0);
+ a, m$ E3 B! S/ T/ ?" h - if(*socket_found == (~0))
4 {3 s2 Z- S- b! x$ M - {- i7 D' m0 ^, ?; x
- printf("Create udp send socket failed!\n");0 l# L+ r& u3 l" C7 {
- return -1;
1 F" `& R0 b I1 v4 v) O - }
/ f, p+ W% o& A - addr->sin_family = AF_INET; y6 z8 H5 M! H
- addr->sin_addr.s_addr = inet_addr(ip);2 ^ J+ x% T9 r
- addr->sin_port = htons(port);
+ J1 T D& F9 O% v - memset(addr->sin_zero, 0, 8);: I& b9 K; t" N
- return 0;
* o- z1 \" b; u5 b3 X+ H( g - }
复制代码
+ a/ _4 R7 u+ J' Q' V6 i( ^& w" ~( Z2 E
而UDP文件发送则要比HTTP发送简单得多,只需要将文件切片,每一片为固定长度的UDP帧长度,逐帧发送即可:
4 W# O: B4 @( f* W0 H
; A1 T" |( s* p+ z; D, j$ u4 ~+ T; |: H. ~ [
- while(fend > 0)
) m- M- p$ }! j# C - {- X4 p" p, }8 [4 @5 I, p/ I
- memset(picture.data , 0 , sizeof(picture.data));9 O9 N% p7 K* ~8 _5 M0 s
- fread(picture.data , UDP_FRAME_LEN , 1, fp);, Y# N8 W2 t) f( Z
- if(fend >= UDP_FRAME_LEN)4 G6 @" i; U" p- |6 w7 m" k3 T
- {. O& s- s5 F8 j: E1 h
- picture.length = UDP_FRAME_LEN; h) Y7 \! e8 G
- picture.fin = 0;% [, b% O- h. e$ n* R
- }
/ x3 K9 P# Q9 y k - else0 P! q! x* B% p! f. _4 \
- {
6 o h6 F3 \* H8 I. h5 m4 \- F - picture.length = fend;
8 n1 O, y& P$ \& e5 }; b R: z - picture.fin = 1;
+ T3 Z- \( s1 U# c - }2 y& A5 K/ c" b/ P0 m. g( N' J8 a
- //printf("sendbytes = %d \n",sendbytes);" F$ Q, c( y- ^+ r, T( C
- sendbytes = sendto(socket_send, (char *)&picture, sizeof(struct Package), 0, (struct sockaddr*)&addr,addr_len);
3 @. p: {6 f+ O* s$ ~- x8 ]1 h: f4 n9 l - if(sendbytes == -1)
: ~# |3 \) \0 ] - {
& D6 P1 f3 L8 [' H" \8 [3 v! ` - printf("Send Picture Failed!d\n");
3 W6 h L* F5 L, P% C* u* t) E - return -1;
% j& X/ G( w- i - }
2 }! k. E. q$ _3 G3 r. u - else, c7 A$ A. I; m) ]9 o
- {
`) |/ x. b w5 e - fend -= UDP_FRAME_LEN;
/ t0 |& Q' Q: ^. r - }
. k# F' Q% K, t* B5 V! P: K0 i4 B2 X - }
复制代码 ; [! v6 f# D9 B4 l% m! H' M
( K- ^8 J8 L7 s" m) g, ~! _ + k3 T a% x' T( _- u
7 V. h! o z- b, j5 m1 RiMX8MPlus 核心板: https://www.forlinx.com/product/136.html |