desc:视频相关优化

main
xiaowusky 1 year ago
parent f24181b49b
commit 32e82b0873

@ -27,18 +27,8 @@ class App : CommonApplication() {
super.onCreate()
// LztekUtil.setObject(Lztek.create(this))
ipConfig()
wifiConfig()
tryFixDbData()
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() {

@ -202,7 +202,7 @@ abstract class BaseActivity : AppCompatActivity() {
return super.dispatchKeyEvent(event)
}
fun playSound() {
private fun 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.RecordHelper
import com.yinuo.safetywatcher.watcher.utils.SimHelper
import com.yinuo.safetywatcher.watcher.utils.SoundUtils
import com.yinuo.safetywatcher.watcher.utils.WifiHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@ -104,14 +105,13 @@ class HomeActivity : NoOptionsActivity() {
* 设置摄像头
*/
private fun setForCamera() {
mClient?.stop()
mClient = EasyPlayerClient(
this@HomeActivity,
mBinding.surface,
null
) {
LogUtils.w("onI420Data c0")
RecordHelper.onFrameAvailable(mBinding.surface, it)
LogUtils.w("onI420Data c1")
val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis - lastUpdateTime > 1000) {
lastUpdateTime = currentTimeMillis
@ -122,7 +122,6 @@ class HomeActivity : NoOptionsActivity() {
}
watchCamera(DELAY_TIME_CHECK_CAMERA)
}
LogUtils.w("onI420Data c2")
}
mClient?.play(CAMERA_URL)
// 第一次很慢所以10秒
@ -157,11 +156,12 @@ class HomeActivity : NoOptionsActivity() {
AppData.setCameraData(false)
changeViewStatus()
if (NetworkHelper.isNetworkConnect(this@HomeActivity)) {
if (!isLoadingShowing() && !AppData.hasSensorData()) {
showLoadingDialog(R.string.connecting_camera)
}
mClient?.play(CAMERA_URL)
watchCamera(DELAY_TIME_OPEN_CAMERA)
// if (!isLoadingShowing() && !AppData.hasSensorData()) {
// showLoadingDialog(R.string.connecting_camera)
// }
// mClient?.play(CAMERA_URL)
// watchCamera(DELAY_TIME_OPEN_CAMERA)
setForCamera()
} else {
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.utils.NetworkStatsHelper
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
@ -28,6 +30,7 @@ class NetSettingActivity : NoOptionsActivity() {
@SuppressLint("SetTextI18n")
override fun initView() {
wifiConfig()
mBinding.apply {
itemWifi.setOnClickListener {
// startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
@ -42,4 +45,17 @@ class NetSettingActivity : NoOptionsActivity() {
val showStr = if (info != null) TrafficFormat.formatByte(info.totalData) else "0B"
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 {
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"
if (File(path).exists()) {
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"
if (File(path).exists()) {
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"
if (File(path).exists()) {
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() {
if (recording) {
recording = false
codecManager.pauseMediaCodec()
codecManager.stopMediaCodec()
}
}
}

@ -93,14 +93,14 @@ public class MediaCodecManager {
*
*/
public void startMediaCodec() {
if (androidMuxer == null) {
androidMuxer = new AndroidMuxer();
}
if (isStart) {
LogUtils.w("startMediaCodec: was started");
return;
}
mHandler.post(() -> {
if (androidMuxer == null) {
androidMuxer = new AndroidMuxer();
}
if (isStart) {
LogUtils.w("startMediaCodec: was started");
return;
}
start();
});
}
@ -243,7 +243,7 @@ public class MediaCodecManager {
if (!isHasKeyFrame && info.flags == MediaCodec.BUFFER_FLAG_KEY_FRAME) {
isHasKeyFrame = true;
}
if (outData == null){
if (outData == null) {
outData = new byte[info.size];
}
if (outputBuffer == null)
@ -295,6 +295,7 @@ public class MediaCodecManager {
} catch (Exception e) {
return;
}
isPause = false;
isStart = true;
}
@ -311,15 +312,22 @@ public class MediaCodecManager {
}
private void stopMediaCodec() {
if (isStart && mMediaCodec != null) {
mMediaCodec.stop();
mMediaCodec.release();
mMediaCodec = null;
public void stopMediaCodec() {
if (androidMuxer != null) {
androidMuxer.release();
androidMuxer = null;
}
isStart = false;
isPause = true;
LogUtils.w(TAG, "stopMediaCodec video");
mHandler.post(() -> {
if (isStart && mMediaCodec != null) {
mMediaCodec.stop();
mMediaCodec.release();
mMediaCodec = null;
}
isStart = false;
isPause = true;
isHasKeyFrame = false;
LogUtils.w(TAG, "stopMediaCodec video");
});
}
private int getIndex(char c) {

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

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

Loading…
Cancel
Save