消息环形队列

这个Java类`YuvDataModel`实现了一个环形队列,用于存储和处理YUV帧数据。队列容量为5,包含`init`、`push`和`pop`等操作。`push`方法将YUV数据存入队列,`pop`方法取出数据。使用了`ReentrantLock`确保线程安全,并通过`ArrayList`维护空闲和已用索引。
摘要由CSDN通过智能技术生成
package org.libsdl.model;


import java.io.IOException;
import java.nio.ByteBuffer;


import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


import yuv.display.OpenGlJNI.FrameBean;


import android.R.integer;


class MyInt {
public int m_value = 0;


public MyInt(int v) {
m_value = v;
}
}


public class YuvDataModel {
final int m_nMaxDataNum = 5;

final int m_width = 640;
final int m_height = 480;

static public ByteBuffer m_YuvDirectBuf;
static public int        m_YuvLen;

ArrayList<FrameBean> datas = new ArrayList<FrameBean>(m_nMaxDataNum);
ArrayList<MyInt> indexEmpty = new ArrayList<MyInt>(m_nMaxDataNum);
ArrayList<MyInt> indexs = new ArrayList<MyInt>(0);
Lock lock = new ReentrantLock();

public YuvDataModel() {
Init();
}



void Init() {
m_YuvLen = m_width*m_height*3/2;
m_YuvDirectBuf = ByteBuffer.allocateDirect(m_YuvLen);
m_YuvDirectBuf.clear();

for (int i = 0; i < m_nMaxDataNum; i++) {
indexEmpty.add(new MyInt(i));
datas.add(new FrameBean());
}
}


void Lock() {


lock.lock();
}


void unlock() {
lock.unlock();
}


boolean IsIndexValid(int index) {
boolean bRet = false;
if (index >= 0 && index < m_nMaxDataNum) {
bRet = true;
}


return bRet;
}


MyInt GetEmptyIndex() {
MyInt nIndex = null;
Lock();
// 鍙栧嚭鏈夋晥鐨刬d
if (indexEmpty.size() > 0) {
MyInt nTemp = indexEmpty.get(0);
indexEmpty.remove(0);
if (IsIndexValid(nTemp.m_value) == false) {
System.out.println(String
.format("Error: Find Invalid empty index!"));
}
{
nIndex = nTemp;
// break;
}
}


unlock();
return nIndex;
}


MyInt GetDataIndex() {
MyInt nIndex = null;
try {
Lock();
// 鍙栧嚭鏈夋晥鐨刬d
if (indexs.size() > 0) {
MyInt nTemp = indexs.get(0);
indexs.remove(0);
if (IsIndexValid(nTemp.m_value) == false) {
System.out.println(String
.format("Error: Find Invalid data index!"));
}
{
nIndex = nTemp;
//break;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
unlock();
}
return nIndex;
}


void AddDataIndex(MyInt nIndex) {
try {
Lock();
indexs.add(nIndex);
} catch (Exception e) {


} finally {
unlock();
}
}


void AddEmptyIndex(MyInt nIndex) {
try {
Lock();
indexEmpty.add(nIndex);
} catch (Exception e) {


e.printStackTrace();
System.out.println(e.getMessage());
} finally {
unlock();
}
}


public void push() 
{
/*
int size=data.length;
if (size < 1) {
return;
}
*/
MyInt nIndex = GetEmptyIndex();
if (nIndex == null) {
return;
}


if (IsIndexValid(nIndex.m_value) == true) {
FrameBean dd = datas.get(nIndex.m_value);

if (dd.databyte == null ) {
dd.databyte = new byte[m_YuvLen];
}

System.arraycopy(m_YuvDirectBuf.array(), 0, dd.databyte, 0, m_YuvLen);
dd.width = m_width;
dd.height = m_height;
}


AddDataIndex(nIndex);
}


public int pop(FrameBean data) {
int ret = -1;
MyInt nIndex = GetDataIndex();
if (nIndex == null) {
return -1;
}


if (IsIndexValid(nIndex.m_value) == true) {
FrameBean dd = datas.get(nIndex.m_value);
if (dd.databyte == null || dd.databyte.length < 1) {
} else {
if (data.databyte == null
|| data.databyte.length != dd.databyte.length) {
data.databyte = new byte[dd.databyte.length];
}
System.arraycopy(dd.databyte, 0, data.databyte, 0,
dd.databyte.length);
data.width = dd.width;
data.height = dd.height;
ret = dd.databyte.length;


}
}
AddEmptyIndex(nIndex);
return ret;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值