23#include "com_tagtraum_casampledsp_CAStreamInputStream.h"
27static jfieldID nativeBufferFieldID = NULL;
28static jmethodID rewindMethodID = NULL;
29static jmethodID setLimitMethodID = NULL;
30static jmethodID getLimitMethodID = NULL;
31static jmethodID setPositionMethodID = NULL;
32static jmethodID getPositionMethodID = NULL;
39static void init_ids(JNIEnv *env, jobject stream) {
41 if (nativeBufferFieldID == NULL || rewindMethodID == NULL || setLimitMethodID == NULL
42 || getLimitMethodID == NULL || getPositionMethodID==NULL || setPositionMethodID==NULL) {
44 nativeBufferFieldID = env->GetFieldID(env->GetObjectClass(stream),
"nativeBuffer",
"Ljava/nio/ByteBuffer;");
45 jclass bufferClass = env->FindClass(
"java/nio/Buffer");
46 rewindMethodID = env->GetMethodID(bufferClass,
"rewind",
"()Ljava/nio/Buffer;");
47 setLimitMethodID = env->GetMethodID(bufferClass,
"limit",
"(I)Ljava/nio/Buffer;");
48 getLimitMethodID = env->GetMethodID(bufferClass,
"limit",
"()I");
49 getPositionMethodID = env->GetMethodID(bufferClass,
"position",
"()I");
50 setPositionMethodID = env->GetMethodID(bufferClass,
"position",
"(I)Ljava/nio/Buffer;");;
58static void CAStreamInputStream_PacketsProc (
61 UInt32 inNumberPackets,
62 const void *inInputData,
63 AudioStreamPacketDescription *inPacketDescriptions
66 fprintf(stderr,
"CAStreamInputStream_PacketsProc\n");
70 jobject byteBuffer = NULL;
75 AudioStreamPacketDescription *newPktDescs = NULL;
79 byteBuffer = asio->
env->GetObjectField(asio->
javaInstance, nativeBufferFieldID);
80 if (byteBuffer == NULL) {
84 limit = asio->
env->CallIntMethod(byteBuffer, getLimitMethodID);
87 asio->
srcBuffer = (
char *)asio->
env->GetDirectBufferAddress(byteBuffer);
88 capacity = asio->
env->GetDirectBufferCapacity(byteBuffer);
93 if (capacity-limit < inNumberBytes) {
98 memcpy(asio->
srcBuffer+limit, inInputData, inNumberBytes);
105 asio->
pos += inNumberPackets;
111 asio->
env->CallObjectMethod(byteBuffer, setPositionMethodID, 0);
112 asio->
env->CallObjectMethod(byteBuffer, setLimitMethodID, inNumberBytes+limit);
114 if (inPacketDescriptions) {
115 newPktDescs =
new AudioStreamPacketDescription[totalPackets];
118 memcpy(newPktDescs, asio->
pktDescs,
sizeof(AudioStreamPacketDescription)*oldPackets);
124 memcpy(&newPktDescs[oldPackets], inPacketDescriptions,
sizeof(AudioStreamPacketDescription)*inNumberPackets);
126 for (i=1; i<totalPackets; i++) {
127 newPktDescs[i].mStartOffset = newPktDescs[i-1].mDataByteSize+newPktDescs[i-1].mStartOffset;
140static void CAStreamInputStream_PropertyListenerProc(
void *inClientData,
141 AudioFileStreamID stream,
142 AudioFileStreamPropertyID inPropertyID,
148 fprintf(stderr,
"CAStreamInputStream_PropertyListenerProc\n");
149 fprintf(stderr,
"AudioFileStreamPropertyID %i\n", inPropertyID);
154 if (inPropertyID == kAudioFileStreamProperty_MagicCookieData) {
155 res = AudioFileStreamGetPropertyInfo(stream, kAudioFileStreamProperty_MagicCookieData, &asio->
cookieSize, NULL);
156 if (res && res != kAudioFileUnsupportedPropertyError) {
163 res = AudioFileStreamGetProperty(stream, kAudioFileStreamProperty_MagicCookieData, &asio->
cookieSize, asio->
cookie);
171 if (inPropertyID == kAudioFileStreamProperty_DataFormat) {
173 res = AudioFileStreamGetProperty(stream, kAudioFileStreamProperty_DataFormat, &size, &asio->
srcFormat);
215 fprintf(stderr,
"fillNativeBuffer: %lld\n", asioPtr);
227 env->GetByteArrayRegion(buf, 0, length, (jbyte*)inBuf);
230 res = AudioFileStreamParseBytes(asio->
asid, length, &inBuf, kAudioFileStreamPropertyFlag_CacheProperty);
249 (JNIEnv *env, jobject stream, jint hint, jint bufferSize) {
253 init_ids(env, stream);
267 res = AudioFileStreamOpen(asio, CAStreamInputStream_PropertyListenerProc, CAStreamInputStream_PacketsProc, hint, &asio->
asid);
275 if (asio->
cookie != NULL) {
281 if (asio->
asid != NULL) {
282 AudioFileStreamClose(asio->
asid);
298 (JNIEnv *env, jobject stream, jlong asioPtr) {
299 if (asioPtr == 0)
return;
301 if (asio->
cookie != NULL) {
307 int res = AudioFileStreamClose(asio->
asid);
void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an UnsupportedAudioFileException exception.
void throwIOExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IOException.
JNIEnv * env
JNI environment.
SInt64 lastPos
Last position (in packets)
jobject javaInstance
Calling java object.
char * srcBuffer
Source buffer.
UInt32 frameOffset
Frame offset (needed for seeking to the middles of a packet)
AudioStreamBasicDescription srcFormat
Source format.
UInt32 cookieSize
Cookie size.
SInt64 pos
Current position (in packets)
UInt32 srcBufferSize
Source buffer size.
AudioStreamPacketDescription * pktDescs
Packet descriptions.
Central context representing the native peer to the Java CAStreamInputStream object.
AudioFileStreamID asid
Stream id.