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;
}
}
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;
}
}