| | |
| | | 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 协议工具类 |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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")); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | public boolean initializeTcpSocket() { |
| | | try { |
| | | if (serverIp == null || serverIp.isEmpty()) { |
| | | Log.e(TAG, "Server IP not set"); |
| | | Timber.e("Server IP not set"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | } |
| | |
| | | try { |
| | | udpSocket.close(); |
| | | } catch (Exception e) { |
| | | Log.e(TAG, "Close UDP socket error", e); |
| | | Timber.e( e,"Close UDP socket error"); |
| | | } |
| | | udpSocket = null; |
| | | } |
| | |
| | | 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"); |
| | | } |
| | | } |
| | | |
| | |
| | | if (tcpClient != null && tcpClient.isConnected()) { |
| | | tcpClient.sendPacket(packet); |
| | | } else { |
| | | Log.w(TAG, "TCP socket not connected"); |
| | | Timber.w("TCP socket not connected"); |
| | | } |
| | | } |
| | | |