desc:视频相关优化

main
xiaowusky 1 year ago
parent f24181b49b
commit 32e82b0873

@ -27,18 +27,8 @@ class App : CommonApplication() {
super.onCreate() super.onCreate()
// LztekUtil.setObject(Lztek.create(this)) // LztekUtil.setObject(Lztek.create(this))
ipConfig() ipConfig()
wifiConfig()
tryFixDbData() tryFixDbData()
TestUtils.insertData() TestUtils.insertData()
SoundUtils.init(this, R.raw.sound)
}
private fun wifiConfig() {
//初始化
val config: WiFiConfig = WiFiConfig.Builder()
.setTimeOut(1000 * 20)
.build()
WiFiModule.getInstance().setConfig(config).init(this)
} }
private fun ipConfig() { private fun ipConfig() {

@ -202,7 +202,7 @@ abstract class BaseActivity : AppCompatActivity() {
return super.dispatchKeyEvent(event) return super.dispatchKeyEvent(event)
} }
fun playSound() { private fun playSound() {
SoundUtils.playSound() SoundUtils.playSound()
} }

@ -26,6 +26,7 @@ import com.yinuo.safetywatcher.watcher.utils.BatteryHelper
import com.yinuo.safetywatcher.watcher.utils.LztekUtil import com.yinuo.safetywatcher.watcher.utils.LztekUtil
import com.yinuo.safetywatcher.watcher.utils.RecordHelper import com.yinuo.safetywatcher.watcher.utils.RecordHelper
import com.yinuo.safetywatcher.watcher.utils.SimHelper import com.yinuo.safetywatcher.watcher.utils.SimHelper
import com.yinuo.safetywatcher.watcher.utils.SoundUtils
import com.yinuo.safetywatcher.watcher.utils.WifiHelper import com.yinuo.safetywatcher.watcher.utils.WifiHelper
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
@ -104,14 +105,13 @@ class HomeActivity : NoOptionsActivity() {
* 设置摄像头 * 设置摄像头
*/ */
private fun setForCamera() { private fun setForCamera() {
mClient?.stop()
mClient = EasyPlayerClient( mClient = EasyPlayerClient(
this@HomeActivity, this@HomeActivity,
mBinding.surface, mBinding.surface,
null null
) { ) {
LogUtils.w("onI420Data c0")
RecordHelper.onFrameAvailable(mBinding.surface, it) RecordHelper.onFrameAvailable(mBinding.surface, it)
LogUtils.w("onI420Data c1")
val currentTimeMillis = System.currentTimeMillis() val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis - lastUpdateTime > 1000) { if (currentTimeMillis - lastUpdateTime > 1000) {
lastUpdateTime = currentTimeMillis lastUpdateTime = currentTimeMillis
@ -122,7 +122,6 @@ class HomeActivity : NoOptionsActivity() {
} }
watchCamera(DELAY_TIME_CHECK_CAMERA) watchCamera(DELAY_TIME_CHECK_CAMERA)
} }
LogUtils.w("onI420Data c2")
} }
mClient?.play(CAMERA_URL) mClient?.play(CAMERA_URL)
// 第一次很慢所以10秒 // 第一次很慢所以10秒
@ -157,11 +156,12 @@ class HomeActivity : NoOptionsActivity() {
AppData.setCameraData(false) AppData.setCameraData(false)
changeViewStatus() changeViewStatus()
if (NetworkHelper.isNetworkConnect(this@HomeActivity)) { if (NetworkHelper.isNetworkConnect(this@HomeActivity)) {
if (!isLoadingShowing() && !AppData.hasSensorData()) { // if (!isLoadingShowing() && !AppData.hasSensorData()) {
showLoadingDialog(R.string.connecting_camera) // showLoadingDialog(R.string.connecting_camera)
} // }
mClient?.play(CAMERA_URL) // mClient?.play(CAMERA_URL)
watchCamera(DELAY_TIME_OPEN_CAMERA) // watchCamera(DELAY_TIME_OPEN_CAMERA)
setForCamera()
} else { } else {
watchCamera(DELAY_TIME_CHECK_CAMERA) watchCamera(DELAY_TIME_CHECK_CAMERA)
} }

@ -10,6 +10,8 @@ import com.yinuo.safetywatcher.databinding.ActivityNetSettingBinding
import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity import com.yinuo.safetywatcher.watcher.base.NoOptionsActivity
import com.yinuo.safetywatcher.watcher.utils.NetworkStatsHelper import com.yinuo.safetywatcher.watcher.utils.NetworkStatsHelper
import com.yinuo.safetywatcher.watcher.utils.TrafficFormat import com.yinuo.safetywatcher.watcher.utils.TrafficFormat
import com.yinuo.safetywatcher.watcher.wifi.WiFiConfig
import com.yinuo.safetywatcher.watcher.wifi.WiFiModule
import com.yinuo.safetywatcher.watcher.wifi.ui.GlobalMonitorActivity import com.yinuo.safetywatcher.watcher.wifi.ui.GlobalMonitorActivity
@ -28,6 +30,7 @@ class NetSettingActivity : NoOptionsActivity() {
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
override fun initView() { override fun initView() {
wifiConfig()
mBinding.apply { mBinding.apply {
itemWifi.setOnClickListener { itemWifi.setOnClickListener {
// startActivity(Intent(Settings.ACTION_WIFI_SETTINGS)) // startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
@ -42,4 +45,17 @@ class NetSettingActivity : NoOptionsActivity() {
val showStr = if (info != null) TrafficFormat.formatByte(info.totalData) else "0B" val showStr = if (info != null) TrafficFormat.formatByte(info.totalData) else "0B"
mBinding.tvUsed.text = "流量使用情况:$showStr" mBinding.tvUsed.text = "流量使用情况:$showStr"
} }
private fun wifiConfig() {
//初始化
val config: WiFiConfig = WiFiConfig.Builder()
.setTimeOut(1000 * 20)
.build()
WiFiModule.getInstance().setConfig(config).init(this)
}
override fun onDestroy() {
super.onDestroy()
WiFiModule.getInstance().destroy()
}
} }

@ -10,7 +10,7 @@ import java.io.IOException
object GPIOUtils { object GPIOUtils {
private const val TAG: String = "GPIOUtils" private const val TAG: String = "GPIOUtils"
private fun setGpioDirection(gpio: Int, direction: String) { fun setGpioDirection(gpio: Int, direction: String) {
val path = "/sys/class/gpio/gpio$gpio/direction" val path = "/sys/class/gpio/gpio$gpio/direction"
if (File(path).exists()) { if (File(path).exists()) {
var writer: FileWriter? = null var writer: FileWriter? = null
@ -33,7 +33,11 @@ object GPIOUtils {
} }
} }
private fun setGpioValue(gpio: Int, value: Int) { /**
* @param gpio 端口号
* @param value 1高电平 0低电平
*/
fun setGpioValue(gpio: Int, value: Int) {
val path = "/sys/class/gpio/gpio$gpio/value" val path = "/sys/class/gpio/gpio$gpio/value"
if (File(path).exists()) { if (File(path).exists()) {
var writer: FileWriter? = null var writer: FileWriter? = null
@ -56,7 +60,7 @@ object GPIOUtils {
} }
} }
private fun getGpioValue(gpio: Int): String? { fun getGpioValue(gpio: Int): String? {
val path = "/sys/class/gpio/gpio$gpio/value" val path = "/sys/class/gpio/gpio$gpio/value"
if (File(path).exists()) { if (File(path).exists()) {
var reader: FileReader? = null var reader: FileReader? = null

@ -1,39 +0,0 @@
package com.yinuo.safetywatcher.watcher.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.renderscript.Type;
/**
* Created by caydencui on 2018/12/7.
*/
public class NV21ToBitmap {
private RenderScript rs;
private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
private Type.Builder yuvType, rgbaType;
private Allocation in, out;
public NV21ToBitmap(Context context) {
rs = RenderScript.create(context);
yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
}
public Bitmap nv21ToBitmap(byte[] nv21, int width, int height) {
if (yuvType == null) {
yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
}
in.copyFrom(nv21);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
out.copyTo(bmpout);
return bmpout;
}
}

@ -59,7 +59,7 @@ object RecordHelper {
fun stopRecording() { fun stopRecording() {
if (recording) { if (recording) {
recording = false recording = false
codecManager.pauseMediaCodec() codecManager.stopMediaCodec()
} }
} }
} }

@ -93,6 +93,7 @@ public class MediaCodecManager {
* *
*/ */
public void startMediaCodec() { public void startMediaCodec() {
mHandler.post(() -> {
if (androidMuxer == null) { if (androidMuxer == null) {
androidMuxer = new AndroidMuxer(); androidMuxer = new AndroidMuxer();
} }
@ -100,7 +101,6 @@ public class MediaCodecManager {
LogUtils.w("startMediaCodec: was started"); LogUtils.w("startMediaCodec: was started");
return; return;
} }
mHandler.post(() -> {
start(); start();
}); });
} }
@ -295,6 +295,7 @@ public class MediaCodecManager {
} catch (Exception e) { } catch (Exception e) {
return; return;
} }
isPause = false;
isStart = true; isStart = true;
} }
@ -311,7 +312,12 @@ public class MediaCodecManager {
} }
private void stopMediaCodec() { public void stopMediaCodec() {
if (androidMuxer != null) {
androidMuxer.release();
androidMuxer = null;
}
mHandler.post(() -> {
if (isStart && mMediaCodec != null) { if (isStart && mMediaCodec != null) {
mMediaCodec.stop(); mMediaCodec.stop();
mMediaCodec.release(); mMediaCodec.release();
@ -319,7 +325,9 @@ public class MediaCodecManager {
} }
isStart = false; isStart = false;
isPause = true; isPause = true;
isHasKeyFrame = false;
LogUtils.w(TAG, "stopMediaCodec video"); LogUtils.w(TAG, "stopMediaCodec video");
});
} }
private int getIndex(char c) { private int getIndex(char c) {

@ -14,6 +14,8 @@ import android.media.MediaCodecList;
import android.media.MediaFormat; import android.media.MediaFormat;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process; import android.os.Process;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -21,6 +23,7 @@ import android.util.Log;
import android.view.Surface; import android.view.Surface;
import android.view.TextureView; import android.view.TextureView;
import androidx.core.util.Pools;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver; import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
@ -805,7 +808,9 @@ public class EasyPlayerClient implements Client.SourceCallBack {
} }
byte[] in = new byte[1920 * 1080 * 3 / 2]; byte[] in = new byte[1920 * 1080 * 3 / 2];
byte[] nv12Data = new byte[in.length]; byte[] nv12Data = new byte[1920 * 1080 * 3 / 2];
VideoCodec.VideoDecoderLite displayer = null;
ByteBuffer displayTmp = null;
private void startCodec() { private void startCodec() {
mThread = new Thread("VIDEO_CONSUMER") { mThread = new Thread("VIDEO_CONSUMER") {
@ -816,7 +821,7 @@ public class EasyPlayerClient implements Client.SourceCallBack {
Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO); Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
MediaCodec mCodec = null; MediaCodec mCodec = null;
int mColorFormat = 0; int mColorFormat = 0;
VideoCodec.VideoDecoderLite mDecoder = null, displayer = null; VideoCodec.VideoDecoderLite mDecoder = null;
try { try {
boolean pushBlankBuffersOnStop = true; boolean pushBlankBuffersOnStop = true;
@ -932,7 +937,7 @@ public class EasyPlayerClient implements Client.SourceCallBack {
frameInfo = null; frameInfo = null;
} }
} }
index = mCodec.dequeueOutputBuffer(info, 10); // index = mCodec.dequeueOutputBuffer(info, 15); //
switch (index) { switch (index) {
case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED: case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:
break; break;
@ -971,12 +976,13 @@ public class EasyPlayerClient implements Client.SourceCallBack {
JNIUtil.yuvConvert(in, realWidth, realHeight, 4); JNIUtil.yuvConvert(in, realWidth, realHeight, 4);
// // 旋转90或180或270度 // // 旋转90或180或270度
// yuvRotate(in, 0, realWidth, realHeight, 90); // yuvRotate(in, 0, realWidth, realHeight, 90);
if (displayTmp == null) {
ByteBuffer tmp = ByteBuffer.allocateDirect(realWidth * realHeight * 3 / 2); displayTmp = ByteBuffer.allocateDirect(realWidth * realHeight * 3 / 2);
tmp.clear(); }
tmp.put(in); displayTmp.clear();
displayTmp.put(in);
// 旋转90或270度则宽高需要互换 // 旋转90或270度则宽高需要互换
displayer.decoder_decodeBuffer(tmp, realWidth, realHeight); displayer.decoder_decodeBuffer(displayTmp, realWidth, realHeight);
} else { } else {
mIndex = 0; mIndex = 0;
} }

@ -13,7 +13,13 @@ class EasySerialPort<T>(
) : Runnable { ) : Runnable {
private var mStartReceiveMsg = true private var mStartReceiveMsg = true
private var mAutoRetryConnect = false private var mAutoRetryConnect = false
private val mTaskScheduler = TaskScheduler() private val mTaskScheduler by lazy {
TaskScheduler()
}
private val mReadBytes by lazy {
ByteArray(25)
}
init { init {
openSerialPort(); openSerialPort();
@ -42,11 +48,11 @@ class EasySerialPort<T>(
while (mStartReceiveMsg) { while (mStartReceiveMsg) {
try { try {
val ips = helper?.inputStream() val ips = helper?.inputStream()
val readByte = ByteArray(25) // val readByte = ByteArray(25)
val size = ips?.read(readByte) val size = ips?.read(mReadBytes)
if (size != null) { if (size != null) {
if (size > 0) { if (size > 0) {
mReceiver.invoke(readByte) mReceiver.invoke(mReadBytes)
} }
} }
Thread.sleep(50L) Thread.sleep(50L)

Loading…
Cancel
Save