| | |
| | | package com.anyun.h264; |
| | | |
| | | import android.util.Log; |
| | | import timber.log.Timber; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | |
| | | public void setFrameRate(int frameRate) { |
| | | this.frameRate = frameRate > 0 ? frameRate : 25; |
| | | this.frameInterval = 1000 / this.frameRate; |
| | | Log.d(TAG, "Frame rate set to: " + this.frameRate + " fps, interval: " + this.frameInterval + " ms"); |
| | | Timber.d("Frame rate set to: %d fps, interval: %d ms", this.frameRate, this.frameInterval); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public boolean initialize() { |
| | | if (isRunning.get()) { |
| | | Log.w(TAG, "Transmitter is already running"); |
| | | Timber.w("Transmitter is already running"); |
| | | return false; |
| | | } |
| | | |
| | | if (!protocolHelper.initializeSocket()) { |
| | | Log.e(TAG, "Failed to initialize socket"); |
| | | Timber.e("Failed to initialize socket"); |
| | | return false; |
| | | } |
| | | |
| | |
| | | lastIFrameTime = 0; |
| | | lastFrameTime = 0; |
| | | |
| | | Log.d(TAG, "Socket initialized successfully"); |
| | | Timber.d("Socket initialized successfully"); |
| | | return true; |
| | | } |
| | | |
| | |
| | | */ |
| | | public void transmitFile(String filePath) { |
| | | if (isRunning.get()) { |
| | | Log.w(TAG, "Transmitter is already running"); |
| | | Timber.w("Transmitter is already running"); |
| | | return; |
| | | } |
| | | |
| | | File file = new File(filePath); |
| | | if (!file.exists() || !file.isFile()) { |
| | | Log.e(TAG, "File does not exist: " + filePath); |
| | | Timber.e("File does not exist: %s", filePath); |
| | | if (progressCallback != null) { |
| | | progressCallback.onError("File does not exist: " + filePath); |
| | | } |
| | |
| | | }); |
| | | transmitThread.start(); |
| | | |
| | | Log.d(TAG, "Started transmitting file: " + filePath); |
| | | Timber.d("Started transmitting file: %s", filePath); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | if (totalRead != fileData.length) { |
| | | Log.w(TAG, "File read incomplete, expected: " + fileData.length + ", actual: " + totalRead); |
| | | Timber.w("File read incomplete, expected: %d, actual: %d", fileData.length, totalRead); |
| | | } |
| | | |
| | | Log.d(TAG, "File read complete, size: " + fileData.length + " bytes"); |
| | | Timber.d("File read complete, size: %d bytes", fileData.length); |
| | | |
| | | // 按帧解析并传输(一个帧包含从一个起始码到下一个起始码之间的所有数据,包括起始码) |
| | | int offset = 0; |
| | |
| | | offset = nextFrameStart; |
| | | } |
| | | |
| | | Log.d(TAG, "Transmission complete, total frames: " + frameCount); |
| | | Timber.d("Transmission complete, total frames: %d", frameCount); |
| | | |
| | | if (progressCallback != null) { |
| | | progressCallback.onComplete(); |
| | | } |
| | | |
| | | } catch (IOException e) { |
| | | Log.e(TAG, "Error transmitting file", e); |
| | | Timber.e(e, "Error transmitting file"); |
| | | if (progressCallback != null) { |
| | | progressCallback.onError("IO Error: " + e.getMessage()); |
| | | } |
| | | } catch (Exception e) { |
| | | Log.e(TAG, "Unexpected error during transmission", e); |
| | | Timber.e(e, "Unexpected error during transmission"); |
| | | if (progressCallback != null) { |
| | | progressCallback.onError("Error: " + e.getMessage()); |
| | | } |
| | |
| | | try { |
| | | fis.close(); |
| | | } catch (IOException e) { |
| | | Log.e(TAG, "Error closing file", e); |
| | | Timber.e(e, "Error closing file"); |
| | | } |
| | | } |
| | | isRunning.set(false); |
| | |
| | | Thread.sleep(frameInterval); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | Log.d(TAG, "Transmission interrupted"); |
| | | Timber.d("Transmission interrupted"); |
| | | return; |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | Log.e(TAG, "Error transmitting frame", e); |
| | | Timber.e(e, "Error transmitting frame"); |
| | | } |
| | | } |
| | | |
| | |
| | | try { |
| | | transmitThread.join(2000); |
| | | } catch (InterruptedException e) { |
| | | Log.e(TAG, "Wait transmit thread error", e); |
| | | Timber.e(e, "Wait transmit thread error"); |
| | | } |
| | | } |
| | | |
| | |
| | | protocolHelper.closeSocket(); |
| | | } |
| | | |
| | | Log.d(TAG, "H264 file transmitter stopped"); |
| | | Timber.d("H264 file transmitter stopped"); |
| | | } |
| | | |
| | | /** |