Dana
2025-11-30 5a5240e9db90474d8da4e20d241246d3f1fa2950
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package com.anyun.h264;
 
import android.util.Log;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;
 
/**
 * JT/T 1076-2016 协议工具类
 * 提供UDP/TCP发送、SIM卡号BCD转换、RTP包创建等公共功能
 */
public class JT1076ProtocolHelper {
    private static final String TAG = "JT1076ProtocolHelper";
    
    // JT/T 1076-2016 RTP协议常量
    public static final byte[] FRAME_HEADER = {0x30, 0x31, 0x63, 0x64}; // 帧头标识
    public static final int MAX_PACKET_SIZE = 950; // 数据体最大长度
    
    // 视频数据类型
    public static final int DATA_TYPE_I_FRAME = 0x00; // I帧
    public static final int DATA_TYPE_P_FRAME = 0x10; // P帧
    public static final int DATA_TYPE_B_FRAME = 0x20; // B帧
    
    // 音频数据类型
    public static final int DATA_TYPE_AUDIO = 0x30; // 音频帧
    
    // 分包处理标记
    public static final int PACKET_MARK_ATOMIC = 0x00; // 原子包
    public static final int PACKET_MARK_FIRST = 0x01;  // 首包
    public static final int PACKET_MARK_LAST = 0x02;   // 末包
    public static final int PACKET_MARK_MIDDLE = 0x03; // 中间包
    
    // RTP负载类型
    public static final int RTP_PAYLOAD_TYPE_VIDEO = 96;  // 视频负载类型
    public static final int RTP_PAYLOAD_TYPE_AUDIO = 97;  // 音频负载类型
    
    // 传输协议类型
    public static final int PROTOCOL_TYPE_UDP = 0;  // UDP协议
    public static final int PROTOCOL_TYPE_TCP = 1;  // TCP协议
    
    // 服务器参数
    private String serverIp;
    private int serverPort;
    
    // 协议类型(默认UDP)
    private int protocolType = PROTOCOL_TYPE_UDP;
    
    // UDP参数
    private DatagramSocket udpSocket;
    private InetAddress serverAddress;
    
    // TCP参数
    private JT1076TcpClient tcpClient;
    
    // RTP协议参数
    private String simCardNumber = "123456789012"; // 12位SIM卡号
    private byte logicalChannelNumber = 1; // 逻辑通道号
    private short sequenceNumber = 0; // 包序号(自动递增)
    
    /**
     * 设置服务器地址
     */
    public void setServerAddress(String ip, int port) {
        this.serverIp = ip;
        this.serverPort = port;
        // 如果TCP客户端已存在,更新地址
        if (tcpClient != null) {
            tcpClient.setServerAddress(ip, port);
        }
    }
    
    /**
     * 设置传输协议类型(UDP或TCP)
     * @param protocolType PROTOCOL_TYPE_UDP 或 PROTOCOL_TYPE_TCP
     */
    public void setProtocolType(int protocolType) {
        if (protocolType != PROTOCOL_TYPE_UDP && protocolType != PROTOCOL_TYPE_TCP) {
            Log.w(TAG, "Invalid protocol type: " + protocolType + ", using UDP");
            protocolType = PROTOCOL_TYPE_UDP;
        }
        
        // 如果协议类型改变,先关闭旧的连接
        if (this.protocolType != protocolType) {
            if (this.protocolType == PROTOCOL_TYPE_UDP) {
                closeUdpSocket();
            } else {
                closeTcpSocket();
            }
        }
        
        this.protocolType = protocolType;
        Log.d(TAG, "Protocol type set to: " + (protocolType == PROTOCOL_TYPE_UDP ? "UDP" : "TCP"));
    }
    
    /**
     * 获取当前协议类型
     */
    public int getProtocolType() {
        return protocolType;
    }
    
    /**
     * 设置SIM卡号和逻辑通道号
     */
    public void setProtocolParams(String simCardNumber, byte logicalChannelNumber) {
        this.simCardNumber = simCardNumber;
        this.logicalChannelNumber = logicalChannelNumber;
    }
    
    /**
     * 初始化Socket(根据协议类型自动选择UDP或TCP)
     */
    public boolean initializeSocket() {
        if (protocolType == PROTOCOL_TYPE_UDP) {
            return initializeUdpSocket();
        } else {
            return initializeTcpSocket();
        }
    }
    
    /**
     * 初始化UDP Socket
     */
    public boolean initializeUdpSocket() {
        try {
            if (serverIp == null || serverIp.isEmpty()) {
                Log.e(TAG, "Server IP not set");
                return false;
            }
            
            udpSocket = new DatagramSocket();
            serverAddress = InetAddress.getByName(serverIp);
            Log.d(TAG, "UDP socket initialized, target: " + serverIp + ":" + serverPort);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Initialize UDP socket failed", e);
            return false;
        }
    }
    
    /**
     * 初始化TCP Socket
     */
    public boolean initializeTcpSocket() {
        try {
            if (serverIp == null || serverIp.isEmpty()) {
                Log.e(TAG, "Server IP not set");
                return false;
            }
            
            if (tcpClient == null) {
                tcpClient = new JT1076TcpClient();
                tcpClient.setServerAddress(serverIp, serverPort);
                
                // 设置连接状态监听器
                tcpClient.setConnectionListener(new JT1076TcpClient.ConnectionListener() {
                    @Override
                    public void onConnected() {
                        Log.d(TAG, "TCP connection established");
                    }
                    
                    @Override
                    public void onDisconnected() {
                        Log.d(TAG, "TCP connection disconnected");
                    }
                    
                    @Override
                    public void onError(Throwable cause) {
                        Log.e(TAG, "TCP connection error", cause);
                    }
                });
            }
            
            tcpClient.connect();
            Log.d(TAG, "TCP socket initializing, target: " + serverIp + ":" + serverPort);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Initialize TCP socket failed", e);
            return false;
        }
    }
    
    /**
     * 关闭Socket(根据协议类型自动选择)
     */
    public void closeSocket() {
        if (protocolType == PROTOCOL_TYPE_UDP) {
            closeUdpSocket();
        } else {
            closeTcpSocket();
        }
    }
    
    /**
     * 关闭UDP Socket
     */
    public void closeUdpSocket() {
        if (udpSocket != null) {
            try {
                udpSocket.close();
            } catch (Exception e) {
                Log.e(TAG, "Close UDP socket error", e);
            }
            udpSocket = null;
        }
        serverAddress = null;
    }
    
    /**
     * 关闭TCP Socket
     */
    public void closeTcpSocket() {
        if (tcpClient != null) {
            tcpClient.disconnect();
            tcpClient = null;
        }
    }
    
    /**
     * 发送RTP包(根据协议类型自动选择UDP或TCP)
     */
    public void sendPacket(byte[] packet) {
        if (protocolType == PROTOCOL_TYPE_UDP) {
            sendUdpPacket(packet);
        } else {
            sendTcpPacket(packet);
        }
    }
    
    /**
     * 发送UDP包(保持向后兼容)
     */
    public void sendUdpPacket(byte[] packet) {
        try {
            if (udpSocket != null && serverAddress != null) {
                DatagramPacket datagramPacket = new DatagramPacket(
                    packet, packet.length, serverAddress, serverPort);
                udpSocket.send(datagramPacket);
            } else {
                Log.w(TAG, "UDP socket not initialized");
            }
        } catch (Exception e) {
            Log.e(TAG, "Send UDP packet error", e);
        }
    }
    
    /**
     * 发送TCP包
     */
    public void sendTcpPacket(byte[] packet) {
        if (tcpClient != null && tcpClient.isConnected()) {
            tcpClient.sendPacket(packet);
        } else {
            Log.w(TAG, "TCP socket not connected");
        }
    }
    
    /**
     * 将SIM卡号转换为BCD格式(6字节)
     */
    public byte[] convertSimToBCD(String simNumber) {
        byte[] bcd = new byte[6];
        // 确保SIM卡号为12位数字,不足前面补0
        StringBuilder sim = new StringBuilder();
        if (simNumber != null) {
            // 只保留数字字符
            String digits = simNumber.replaceAll("[^0-9]", "");
            sim.append(digits);
        }
        // 补齐或截断到12位
        while (sim.length() < 12) {
            sim.insert(0, '0');
        }
        if (sim.length() > 12) {
            sim.setLength(12);
        }
        
        // 转换为BCD格式
        String simStr = sim.toString();
        for (int i = 0; i < 6; i++) {
            int high = Character.digit(simStr.charAt(i * 2), 10);
            int low = Character.digit(simStr.charAt(i * 2 + 1), 10);
            if (high < 0) high = 0;
            if (low < 0) low = 0;
            bcd[i] = (byte) ((high << 4) | low);
        }
        return bcd;
    }
    
    /**
     * 获取下一个包序号(自动递增)
     */
    public short getNextSequenceNumber() {
        return sequenceNumber++;
    }
    
    /**
     * 重置包序号
     */
    public void resetSequenceNumber() {
        sequenceNumber = 0;
    }
    
    /**
     * 创建视频RTP包(JT/T 1076-2016协议格式)
     * @param dataBody 数据体
     * @param timestamp 时间戳(毫秒)
     * @param dataType 数据类型(I帧/P帧/B帧)
     * @param packetMark 分包处理标记
     * @param lastIFrameInterval 距上一个I帧的时间间隔(毫秒)
     * @param lastFrameInterval 距上一帧的时间间隔(毫秒)
     * @param payloadType RTP负载类型(默认96)
     * @return RTP包数据
     */
    public byte[] createVideoRtpPacket(byte[] dataBody, long timestamp, int dataType, 
                                      int packetMark, long lastIFrameInterval, 
                                      long lastFrameInterval, int payloadType) {
        // 计算包大小:帧头(4) + RTP头(2) + 序号(2) + SIM卡号(6) + 逻辑通道(1) + 
        //            数据类型(1) + 时间戳(8) + Last I Frame Interval(2) + Last Frame Interval(2) + 
        //            数据体长度(2) + 数据体
        int packetSize = 4 + 2 + 2 + 6 + 1 + 1 + 8 + 2 + 2 + 2 + dataBody.length;
        ByteBuffer buffer = ByteBuffer.allocate(packetSize);
        buffer.order(java.nio.ByteOrder.BIG_ENDIAN);
        
        // 1. 帧头标识 (4 bytes)
        buffer.put(FRAME_HEADER);
        
        // 2. RTP头部 (2 bytes)
        // V=2 (2 bits), P=0 (1 bit), X=0 (1 bit), CC=1 (4 bits), M=0/1 (1 bit), PT (7 bits)
        byte rtpHeaderByte1 = (byte) 0x81; // V=2, P=0, X=0, CC=1
        byte rtpHeaderByte2 = (byte) (payloadType & 0x7F); // M=0, PT
        buffer.put(rtpHeaderByte1);
        buffer.put(rtpHeaderByte2);
        
        // 3. 包序号 (2 bytes)
        buffer.putShort(getNextSequenceNumber());
        
        // 4. SIM卡号 (6 bytes BCD格式)
        byte[] simBytes = convertSimToBCD(simCardNumber);
        buffer.put(simBytes, 0, 6);
        
        // 5. 逻辑通道号 (1 byte)
        buffer.put(logicalChannelNumber);
        
        // 6. 数据类型 + 分包处理标记 (1 byte)
        byte dataTypeAndMark = (byte) ((dataType & 0xF0) | (packetMark & 0x0F));
        buffer.put(dataTypeAndMark);
        
        // 7. 时间戳 (8 bytes, 毫秒)
        buffer.putLong(timestamp);
        
        // 8. Last I Frame Interval (2 bytes, 毫秒)
        buffer.putShort((short) lastIFrameInterval);
        
        // 9. Last Frame Interval (2 bytes, 毫秒)
        buffer.putShort((short) lastFrameInterval);
        
        // 10. 数据体长度 (2 bytes)
        buffer.putShort((short) dataBody.length);
        
        // 11. 数据体
        buffer.put(dataBody);
        
        return buffer.array();
    }
    
    /**
     * 创建视频RTP包(使用默认视频负载类型96)
     */
    public byte[] createVideoRtpPacket(byte[] dataBody, long timestamp, int dataType, 
                                      int packetMark, long lastIFrameInterval, 
                                      long lastFrameInterval) {
        return createVideoRtpPacket(dataBody, timestamp, dataType, packetMark, 
                                   lastIFrameInterval, lastFrameInterval, RTP_PAYLOAD_TYPE_VIDEO);
    }
    
    /**
     * 创建音频RTP包(JT/T 1076-2016协议格式)
     * 注意:音频帧不包含Last I Frame Interval和Last Frame Interval字段
     * @param dataBody 数据体
     * @param timestamp 时间戳(毫秒)
     * @param dataType 数据类型(音频帧)
     * @param packetMark 分包处理标记
     * @param payloadType RTP负载类型(默认97)
     * @return RTP包数据
     */
    public byte[] createAudioRtpPacket(byte[] dataBody, long timestamp, int dataType, 
                                      int packetMark, int payloadType) {
        // 计算包大小(音频:不包含Last I Frame Interval和Last Frame Interval)
        // 帧头(4) + RTP头(2) + 序号(2) + SIM卡号(6) + 逻辑通道(1) + 
        // 数据类型(1) + 时间戳(8) + 数据体长度(2) + 数据体
        int packetSize = 4 + 2 + 2 + 6 + 1 + 1 + 8 + 2 + dataBody.length;
        ByteBuffer buffer = ByteBuffer.allocate(packetSize);
        buffer.order(java.nio.ByteOrder.BIG_ENDIAN);
        
        // 1. 帧头标识 (4 bytes)
        buffer.put(FRAME_HEADER);
        
        // 2. RTP头部 (2 bytes)
        // V=2 (2 bits), P=0 (1 bit), X=0 (1 bit), CC=1 (4 bits), M=0/1 (1 bit), PT (7 bits)
        byte rtpHeaderByte1 = (byte) 0x81; // V=2, P=0, X=0, CC=1
        byte rtpHeaderByte2 = (byte) (payloadType & 0x7F); // M=0, PT
        buffer.put(rtpHeaderByte1);
        buffer.put(rtpHeaderByte2);
        
        // 3. 包序号 (2 bytes)
        buffer.putShort(getNextSequenceNumber());
        
        // 4. SIM卡号 (6 bytes BCD格式)
        byte[] simBytes = convertSimToBCD(simCardNumber);
        buffer.put(simBytes, 0, 6);
        
        // 5. 逻辑通道号 (1 byte)
        buffer.put(logicalChannelNumber);
        
        // 6. 数据类型 + 分包处理标记 (1 byte)
        byte dataTypeAndMark = (byte) ((dataType & 0xF0) | (packetMark & 0x0F));
        buffer.put(dataTypeAndMark);
        
        // 7. 时间戳 (8 bytes, 毫秒)
        buffer.putLong(timestamp);
        
        // 注意:音频帧不包含Last I Frame Interval和Last Frame Interval字段
        
        // 8. 数据体长度 (2 bytes)
        buffer.putShort((short) dataBody.length);
        
        // 9. 数据体
        buffer.put(dataBody);
        
        return buffer.array();
    }
    
    /**
     * 创建音频RTP包(使用默认音频负载类型97)
     */
    public byte[] createAudioRtpPacket(byte[] dataBody, long timestamp, int dataType, int packetMark) {
        return createAudioRtpPacket(dataBody, timestamp, dataType, packetMark, RTP_PAYLOAD_TYPE_AUDIO);
    }
}