24 #include "com_tagtraum_ffsampledsp_FFStreamInputStream.h" 27 static jmethodID fillReadBuffer_MID = NULL;
28 static jfieldID readBuffer_FID = NULL;
30 static int CALLBACK_BUFFERSIZE = 32*1024;
38 static void init_ids(JNIEnv *env, jobject stream) {
39 if (!fillReadBuffer_MID || !readBuffer_FID) {
40 jclass streamClass = NULL;
41 streamClass = (*env)->GetObjectClass(env, stream);
42 readBuffer_FID = (*env)->GetFieldID(env, streamClass,
"readBuffer",
"Ljava/nio/ByteBuffer;");
43 fillReadBuffer_MID = (*env)->GetMethodID(env, streamClass,
"fillReadBuffer",
"()I");
55 static int read_callback(
void *opaque, uint8_t *buf,
int size) {
58 jobject read_buffer = NULL;
59 uint8_t *java_buffer = NULL;
63 available_data = (int) (*aio->
env)->CallIntMethod(aio->
env, aio->
java_instance, fillReadBuffer_MID);
64 if (available_data > size) {
69 if ((*aio->
env)->ExceptionCheck(aio->
env)) {
76 if (available_data <= 0) {
88 java_buffer = (uint8_t *)(*aio->
env)->GetDirectBufferAddress(aio->
env, read_buffer);
96 memcpy(buf, (
const uint8_t *)java_buffer, available_data);
136 AVIOContext *io_context;
137 unsigned char* callback_buffer = NULL;
139 init_ids(env, stream);
143 res = AVERROR(ENOMEM);
152 res = AVERROR(ENOMEM);
161 callback_buffer = (
unsigned char*)av_malloc(CALLBACK_BUFFERSIZE *
sizeof(uint8_t));
162 if (!callback_buffer) {
163 res = AVERROR(ENOMEM);
168 io_context = avio_alloc_context(
178 res = AVERROR(ENOMEM);
184 io_context->seekable = 0;
202 fprintf(stderr,
"stream->codecpar->bits_per_coded_sample: %i\n", aio->
stream->codecpar->bits_per_coded_sample);
203 fprintf(stderr,
"stream->codecpar->bits_per_raw_sample : %i\n", aio->
stream->codecpar->bits_per_raw_sample);
204 fprintf(stderr,
"stream->codecpar->bit_rate : %lli\n", aio->
stream->codecpar->bit_rate);
205 fprintf(stderr,
"frames : %" PRId64
"\n", aio->
stream->nb_frames);
206 fprintf(stderr,
"sample_rate: %i\n", aio->
stream->codecpar->sample_rate);
207 fprintf(stderr,
"channels : %i\n", aio->
stream->codecpar->channels);
208 fprintf(stderr,
"frame_size : %i\n", aio->
stream->codecpar->frame_size);
209 fprintf(stderr,
"codec_id : %i\n", aio->
stream->codecpar->codec_id);
215 return (jlong)(intptr_t)aio;
int stream_index
Index of the audio stream we are using.
void throwIOExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IOException.
AVFormatContext * format_context
Current AVFormatContext.
AVStream * stream
Audio stream we are interested in.
int ff_open_file(JNIEnv *env, AVFormatContext **format_context, AVStream **openedStream, AVCodecContext **context, int *stream_index, const char *url)
Opens the input file/url, allocates a AVFormatContext for it and opens the audio stream with an appro...
int ff_fill_buffer(FFAudioIO *aio)
Reads a frame via av_read_frame(AVFormatContext, AVPacket), decodes it to a AVPacket, and writes the result to the Java-side nativeBuffer.
int ff_init_audioio(JNIEnv *env, FFAudioIO *aio)
Initialize our main context FFAudioIO, so that SwrContext, decode buffers and the encoder are set to ...
JNIEnv * env
JNI environment.
AVCodecContext * decode_context
Codec context for decoding.
Central context representing the native peer to the Java FFNativePeerInputStream object.
void ff_audioio_free(FFAudioIO *aio)
Free all resources held by aio and then itself.
jobject java_instance
Calling Java instance.