Dana
2025-12-01 ccf7a1f19b5c6276178c72609c88d057214c8239
app/src/main/java/com/anyun/h264/JT1076ProtocolHelper.java
@@ -1,12 +1,13 @@
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;
import timber.log.Timber;
/**
 * JT/T 1076-2016 协议工具类
@@ -78,7 +79,7 @@
     */
    public void setProtocolType(int protocolType) {
        if (protocolType != PROTOCOL_TYPE_UDP && protocolType != PROTOCOL_TYPE_TCP) {
            Log.w(TAG, "Invalid protocol type: " + protocolType + ", using UDP");
            Timber.w("Invalid protocol type: " + protocolType + ", using UDP");
            protocolType = PROTOCOL_TYPE_UDP;
        }
        
@@ -92,7 +93,7 @@
        }
        
        this.protocolType = protocolType;
        Log.d(TAG, "Protocol type set to: " + (protocolType == PROTOCOL_TYPE_UDP ? "UDP" : "TCP"));
        Timber.d("Protocol type set to: " + (protocolType == PROTOCOL_TYPE_UDP ? "UDP" : "TCP"));
    }
    
    /**
@@ -127,16 +128,16 @@
    public boolean initializeUdpSocket() {
        try {
            if (serverIp == null || serverIp.isEmpty()) {
                Log.e(TAG, "Server IP not set");
                Timber.e("Server IP not set");
                return false;
            }
            
            udpSocket = new DatagramSocket();
            serverAddress = InetAddress.getByName(serverIp);
            Log.d(TAG, "UDP socket initialized, target: " + serverIp + ":" + serverPort);
            Timber.d("UDP socket initialized, target: " + serverIp + ":" + serverPort);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Initialize UDP socket failed", e);
            Timber.e(e,"Initialize UDP socket failed");
            return false;
        }
    }
@@ -147,7 +148,7 @@
    public boolean initializeTcpSocket() {
        try {
            if (serverIp == null || serverIp.isEmpty()) {
                Log.e(TAG, "Server IP not set");
                Timber.e("Server IP not set");
                return false;
            }
            
@@ -159,26 +160,26 @@
                tcpClient.setConnectionListener(new JT1076TcpClient.ConnectionListener() {
                    @Override
                    public void onConnected() {
                        Log.d(TAG, "TCP connection established");
                        Timber.d("TCP connection established");
                    }
                    
                    @Override
                    public void onDisconnected() {
                        Log.d(TAG, "TCP connection disconnected");
                        Timber.d( "TCP connection disconnected");
                    }
                    
                    @Override
                    public void onError(Throwable cause) {
                        Log.e(TAG, "TCP connection error", cause);
                        Timber.e(cause, "TCP connection error");
                    }
                });
            }
            
            tcpClient.connect();
            Log.d(TAG, "TCP socket initializing, target: " + serverIp + ":" + serverPort);
            Timber.d("TCP socket initializing, target: " + serverIp + ":" + serverPort);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Initialize TCP socket failed", e);
            Timber.e(e,"Initialize TCP socket failed");
            return false;
        }
    }
@@ -202,7 +203,7 @@
            try {
                udpSocket.close();
            } catch (Exception e) {
                Log.e(TAG, "Close UDP socket error", e);
                Timber.e( e,"Close UDP socket error");
            }
            udpSocket = null;
        }
@@ -240,10 +241,10 @@
                    packet, packet.length, serverAddress, serverPort);
                udpSocket.send(datagramPacket);
            } else {
                Log.w(TAG, "UDP socket not initialized");
                Timber.w("UDP socket not initialized");
            }
        } catch (Exception e) {
            Log.e(TAG, "Send UDP packet error", e);
            Timber.e(e,"Send UDP packet error");
        }
    }
    
@@ -254,7 +255,7 @@
        if (tcpClient != null && tcpClient.isConnected()) {
            tcpClient.sendPacket(packet);
        } else {
            Log.w(TAG, "TCP socket not connected");
            Timber.w("TCP socket not connected");
        }
    }