27static jmethodID fillReadBuffer_MID = NULL;
28static jfieldID readBuffer_FID = NULL;
30static const int CALLBACK_BUFFERSIZE = 32 * 1024;
45 readBuffer_FID = (*env)->GetFieldID(env, streamClass,
"readBuffer",
46 "Ljava/nio/ByteBuffer;");
50 (*env)->GetMethodID(env, streamClass,
"fillReadBuffer",
"()I");
62static int read_callback(
void *opaque, uint8_t *buf,
int size) {
65 jobject read_buffer = NULL;
66 uint8_t *java_buffer = NULL;
71 if ((*aio->
env)->ExceptionCheck(aio->
env)) {
79 if ((*aio->
env)->ExceptionCheck(aio->
env)) {
83 if (available_data > size) {
86 aio->
env, 1,
"Available data must not be larger than callback buffer.");
90 if (available_data <= 0) {
104 (uint8_t *)(*aio->
env)->GetDirectBufferAddress(aio->
env, read_buffer);
108 "Failed to get address for read buffer.");
113 memcpy(buf, (
const uint8_t *)java_buffer, available_data);
115 res = available_data;
120 (*aio->
env)->DeleteLocalRef(aio->
env, read_buffer);
131JNIEXPORT
void JNICALL
133 JNIEnv *env, jobject stream, jlong aio_pointer) {
154 JNIEnv *env, jobject stream, jint streamIndex) {
158 AVIOContext *io_context;
159 unsigned char *callback_buffer = NULL;
163 res = AVERROR(ENOMEM);
172 res = AVERROR(ENOMEM);
182 (
unsigned char *)av_malloc(CALLBACK_BUFFERSIZE *
sizeof(uint8_t));
183 if (!callback_buffer) {
184 res = AVERROR(ENOMEM);
189 io_context = avio_alloc_context(
199 res = AVERROR(ENOMEM);
205 io_context->seekable = 0;
212 "MemoryAVIOContext", 0);
225 fprintf(stderr,
"stream->codecpar->bits_per_coded_sample: %i\n",
226 aio->
stream->codecpar->bits_per_coded_sample);
227 fprintf(stderr,
"stream->codecpar->bits_per_raw_sample : %i\n",
228 aio->
stream->codecpar->bits_per_raw_sample);
229 fprintf(stderr,
"stream->codecpar->bit_rate : %lli\n",
230 aio->
stream->codecpar->bit_rate);
231 fprintf(stderr,
"frames : %" PRId64
"\n", aio->
stream->nb_frames);
232 fprintf(stderr,
"sample_rate: %i\n", aio->
stream->codecpar->sample_rate);
233 fprintf(stderr,
"channels : %i\n",
234 aio->
stream->codecpar->ch_layout.nb_channels);
235 fprintf(stderr,
"frame_size : %i\n", aio->
stream->codecpar->frame_size);
236 fprintf(stderr,
"codec_id : %i\n", aio->
stream->codecpar->codec_id);
241 if (!res && (*env)->ExceptionCheck(env)) {
249 return (jlong)(intptr_t)aio;
260 JNIEnv *env, jobject stream, jlong aio_pointer) {
void ff_audioio_free(FFAudioIO *aio)
Free all resources held by aio and then itself.
int ff_open_file(JNIEnv *env, AVFormatContext **format_context, AVStream **openedStream, AVCodecContext **context, int *stream_index, const char *url, int io_buffer_size)
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 frames via av_read_frame, decodes them, 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 ...
void throwIOExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IOException.
Central context representing the native peer to the Java FFNativePeerInputStream object.
AVFormatContext * format_context
Current AVFormatContext.
int stream_index
Index of the audio stream we are using.
AVStream * stream
Audio stream we are interested in.
JNIEnv * env
JNI environment.
AVCodecContext * decode_context
Codec context for decoding.
jobject java_instance
Calling Java instance.