zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.basic.security.utils.socket;
 
import android.os.SystemClock;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.model.OfficeDevice;
import com.basic.security.utils.Constants;
import com.basic.security.utils.SocketUtil;
 
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.Map;
 
public class SnapshotYuvSocketServer {
    public static boolean serverStarted = false;
 
    public static void startYuvSocketServer() {
        if (serverStarted) {
            return;
        }
        BaseApplication.getApplication().executorService.execute(() -> {
            ServerSocket server = null;
            try {
                server = new ServerSocket(Constants.snapshotServerPort);
            } catch (Exception e) {
                e.printStackTrace();
            }
            while (true) {
                try {
                    if (server == null) {
                        break;
                    }
                    serverStarted = true;
                    Socket client = server.accept();
                    SnapshotYuvAcceptedClient snapshotYuvAcceptedClient = new SnapshotYuvAcceptedClient(client);
                    snapshotYuvAcceptedClient.start();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    SystemClock.sleep(3000);
                }
            }
        });
 
    }
 
    public static class SnapshotYuvAcceptedClient extends Thread {
 
        private static final int MAX_BUFFER = 2;
        private static LinkedList<byte[]> mRgbCameraQueue = new LinkedList<byte[]>();
        Socket mSocket;
 
        public SnapshotYuvAcceptedClient(Socket client) {
            mSocket = client;
        }
 
        public static void addCameraData(byte[] data) {
            synchronized (mRgbCameraQueue) {
                if (mRgbCameraQueue.size() == MAX_BUFFER) {
                    mRgbCameraQueue.poll();
                }
                mRgbCameraQueue.add(data);
            }
        }
 
        public byte[] getImageBuffer() {
            byte[] mLastFrame = null;
            synchronized (mRgbCameraQueue) {
                if (mRgbCameraQueue.size() > 0) {
                    mLastFrame = mRgbCameraQueue.poll();
                }
            }
            return mLastFrame;
        }
 
        @Override
        public void run() {
            SocketUtil socketUtil = new SocketUtil();
            try {
                socketUtil.setSocket(mSocket);
                Map authMap = socketUtil.readMap();
                socketUtil.deviceId = (String) authMap.get(OfficeDevice.deviceId);
                if (socketUtil.writeBitmapHeader()) {
                    socketUtil.writeOneBitmap(getImageBuffer());
                }
                while (!Thread.currentThread().isInterrupted()) {
                    SystemClock.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                socketUtil.close();
            }
        }
 
    }
 
}