Dana
2025-11-29 496b0660b198ea449122f527c3d68609a9f941e5
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
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发送、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;  // 音频负载类型
    
    // UDP参数
    private String serverIp;
    private int serverPort;
    private DatagramSocket udpSocket;
    private InetAddress serverAddress;
    
    // RTP协议参数
    private String simCardNumber = "123456789012"; // 12位SIM卡号
    private byte logicalChannelNumber = 1; // 逻辑通道号
    private short sequenceNumber = 0; // 包序号(自动递增)
    
    /**
     * 设置UDP服务器地址
     */
    public void setServerAddress(String ip, int port) {
        this.serverIp = ip;
        this.serverPort = port;
    }
    
    /**
     * 设置SIM卡号和逻辑通道号
     */
    public void setProtocolParams(String simCardNumber, byte logicalChannelNumber) {
        this.simCardNumber = simCardNumber;
        this.logicalChannelNumber = logicalChannelNumber;
    }
    
    /**
     * 初始化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;
        }
    }
    
    /**
     * 关闭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;
    }
    
    /**
     * 发送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);
        }
    }
    
    /**
     * 将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);
    }
}