流媒体程序开发之:H264解码器移植到OPhone
2009-09-22 00:00:00 来源:WEB开发网5.3.2 实现本地方法
之前已经生成了JNI头文件,接下来只需要实现这个头文件的几个导出函数,这里以H264解码器的实现为例:
#include "ophone_streaming_video_h264_H264decode.h"
#include "H264Decode.h"
JNIEXPORT jint JNICALL Java_ophone_streaming_video_h264_H264decode_DecodeOneFrame
(JNIEnv * env, jclass obj, jlong decode, jobject pInBuffer, jobject pOutBuffer) {
H264Decode *pDecode = (H264Decode *)decode;
unsigned char *In = NULL;unsigned char *Out = NULL;
unsigned int InPosition = 0;unsigned int InRemaining = 0;unsigned int InSize = 0;
unsigned int OutSize = 0;
jint DecodeSize = -1;
jbyte *InJbyte = 0;
jbyte *OutJbyte = 0;
jbyteArray InByteArrary = 0;
jbyteArray OutByteArrary = 0;
//获取Input/Out ByteBuffer相关属性
{
//Input
{
jclass ByteBufferClass = env->GetObjectClass(pInBuffer);
jmethodID PositionMethodId = env->GetMethodID(ByteBufferClass,"position","()I");
jmethodID RemainingMethodId = env->GetMethodID(ByteBufferClass,"remaining","()I");
jmethodID ArraryMethodId = env->GetMethodID(ByteBufferClass,"array","()[B");
InPosition = env->CallIntMethod(pInBuffer,PositionMethodId);
InRemaining = env->CallIntMethod(pInBuffer,RemainingMethodId);
InSize = InPosition + InRemaining;
InByteArrary = (jbyteArray)env->CallObjectMethod(pInBuffer,ArraryMethodId);
InJbyte = env->GetByteArrayElements(InByteArrary,0);
In = (unsigned char*)InJbyte + InPosition;
}
//Output
{
jclass ByteBufferClass = env->GetObjectClass(pOutBuffer);
jmethodID ArraryMethodId = env->GetMethodID(ByteBufferClass,"array","()[B");
jmethodID ClearMethodId = env->GetMethodID(ByteBufferClass,"clear","()Ljava/nio/Buffer;");
//清理输出缓存区
env->CallObjectMethod(pOutBuffer,ClearMethodId);
OutByteArrary = (jbyteArray)env->CallObjectMethod(pOutBuffer,ArraryMethodId);
OutJbyte = env->GetByteArrayElements(OutByteArrary,0);
Out = (unsigned char*)OutJbyte;
}
}
//解码
DecodeSize = pDecode->DecodeOneFrame(In,InRemaining,Out,OutSize);
//设置Input/Output ByteBuffer相关属性
{
//Input
{
jclass ByteBufferClass = env->GetObjectClass(pInBuffer);
jmethodID SetPositionMethodId = env->GetMethodID(ByteBufferClass,"position","(I)Ljava/nio/Buffer;");
//设置输入缓冲区偏移
env->CallObjectMethod(pInBuffer,SetPositionMethodId,InPosition + DecodeSize);
}
//Output
{
jclass ByteBufferClass = env->GetObjectClass(pOutBuffer);
jmethodID SetPositionMethodId = env->GetMethodID(ByteBufferClass,"position","(I)Ljava/nio/Buffer;");
//设置输出缓冲区偏移
env->CallObjectMethod(pOutBuffer,SetPositionMethodId,OutSize);
}
}
//清理
env->ReleaseByteArrayElements(InByteArrary,InJbyte,0);
env->ReleaseByteArrayElements(OutByteArrary,OutJbyte,0);
return DecodeSize;
}
JNIEXPORT jlong JNICALL Java_ophone_streaming_video_h264_H264decode_Initialize
(JNIEnv * env, jclass obj) {
H264Decode *pDecode = H264Decode::H264DecodeConstruct();
return (jlong)pDecode;
}
JNIEXPORT void JNICALL Java_ophone_streaming_video_h264_H264decode_Destroy
(JNIEnv * env, jclass obj, jlong decode) {
H264Decode *pDecode = (H264Decode *)decode;
if (pDecode)
{
delete pDecode;
pDecode = NULL;
}
}
更多精彩
赞助商链接