Dana
2025-11-30 c795d1c28858b3300ad43792d58cfe825961f06d
app/src/main/java/com/anyun/h264/JT1076ProtocolHelper.java
@@ -10,7 +10,7 @@
/**
 * JT/T 1076-2016 协议工具类
 * 提供UDP发送、SIM卡号BCD转换、RTP包创建等公共功能
 * 提供UDP/TCP发送、SIM卡号BCD转换、RTP包创建等公共功能
 */
public class JT1076ProtocolHelper {
    private static final String TAG = "JT1076ProtocolHelper";
@@ -37,11 +37,23 @@
    public static final int RTP_PAYLOAD_TYPE_VIDEO = 96;  // 视频负载类型
    public static final int RTP_PAYLOAD_TYPE_AUDIO = 97;  // 音频负载类型
    
    // UDP参数
    // 传输协议类型
    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卡号
@@ -49,11 +61,45 @@
    private short sequenceNumber = 0; // 包序号(自动递增)
    
    /**
     * 设置UDP服务器地址
     * 设置服务器地址
     */
    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;
    }
    
    /**
@@ -62,6 +108,17 @@
    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();
        }
    }
    
    /**
@@ -85,6 +142,59 @@
    }
    
    /**
     * 初始化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() {
@@ -100,7 +210,28 @@
    }
    
    /**
     * 发送UDP包
     * 关闭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 {
@@ -117,6 +248,17 @@
    }
    
    /**
     * 发送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) {