Commit f4eac726 authored by Adrien Maglo's avatar Adrien Maglo
Browse files

Android: First working version of the aout.

Needs some cleaning.
parent f3b25ee8
......@@ -20,6 +20,7 @@ void aout_open(void **opaque, unsigned int *rate, unsigned int *nb_channels, uns
if(!p_sys)
return;
*nb_samples = 4096 * 2;
p_sys->j_libVlc = (jobject)*opaque;
// Attach the thread to the VM
......@@ -35,10 +36,9 @@ void aout_open(void **opaque, unsigned int *rate, unsigned int *nb_channels, uns
p_sys->p_env = p_env;
*(aout_sys_t**)opaque = p_sys;
// Call the init functions
// Call the init function.
jclass cls = (*p_env)->GetObjectClass(p_env, p_sys->j_libVlc);
jmethodID methodIdInitAout =
(*p_env)->GetMethodID(p_env, cls, "initAout", "(III)V");
jmethodID methodIdInitAout = (*p_env)->GetMethodID(p_env, cls, "initAout", "(III)V");
if(methodIdInitAout == 0)
{
LOGE("Method initAout() cannot be found\n");
......@@ -47,15 +47,43 @@ void aout_open(void **opaque, unsigned int *rate, unsigned int *nb_channels, uns
(*p_env)->CallVoidMethod(p_env, p_sys->j_libVlc, methodIdInitAout,
*rate, *nb_channels, *nb_samples);
/* Create a new byte array to store the audio data. */
jbyteArray byteArray = (*p_env)->NewByteArray(p_env, *nb_channels * *nb_samples * 2);
if (byteArray == NULL)
{
LOGE("Couldn't allocate the Java byte array to store the audio data!");
return;
}
/* Use a global reference to not reallocate memory each time we run
the display function. */
p_sys->byteArray = (*p_env)->NewGlobalRef(p_env, byteArray);
if (byteArray == NULL)
{
LOGE("Couldn't create the global reference!");
return;
}
/* The local reference is no longer useful. */
(*p_env)->DeleteLocalRef(p_env, byteArray);
// Get the play methodId
p_sys->play = (*p_env)->GetMethodID(p_env, cls, "playAudio", "()V");
p_sys->play = (*p_env)->GetMethodID(p_env, cls, "playAudio", "([BII)V");
}
void aout_play(void *opaque, unsigned char *buffer, size_t bufferSize, unsigned int nb_samples)
{
aout_sys_t *p_sys = (aout_sys_t*)opaque;
JNIEnv *p_env = p_sys->p_env;
LOGI("%i", bufferSize);
(*p_sys->p_env)->CallVoidMethod(p_sys->p_env, p_sys->j_libVlc, p_sys->play);
(*p_env)->SetByteArrayRegion(p_env, p_sys->byteArray, 0,
bufferSize, (jbyte *)buffer);
(*p_sys->p_env)->CallVoidMethod(p_sys->p_env, p_sys->j_libVlc, p_sys->play,
p_sys->byteArray, bufferSize, nb_samples);
}
void aout_close(void *opaque)
......
......@@ -6,6 +6,8 @@ typedef struct
jobject j_libVlc; // Pointer to the LibVLC Java object
jmethodID play;
JNIEnv *p_env;
jbyteArray byteArray;
} aout_sys_t;
void aout_open(void **opaque, unsigned int *rate, unsigned int *nb_channels, unsigned int *fourCCFormat, unsigned int *nb_samples);
......
......@@ -13,7 +13,14 @@ public class Aout {
{
Log.d(TAG, sampleRateInHz + ", " + channels + ", " + samples + "=>" + channels*samples);
mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, channels * samples * 2, AudioTrack.MODE_STREAM);
}
public void playAudio(byte[] audioData, int bufferSize, int nbSamples)
{
Log.d(TAG, "Buffer size: " + bufferSize + " nb samples: " + nbSamples);
Log.d(TAG, "Write audio data in Java: " + mAudioTrack.write(audioData, 0, bufferSize));
mAudioTrack.play();
}
}
......@@ -118,9 +118,10 @@ public class LibVLC {
* Play an audio buffer taken from the native code
* This function is called by the native code
*/
public void playAudio()
public void playAudio(byte[] audioData, int bufferSize, int nbSamples)
{
Log.d(TAG, "Playing an audio buffer in Java");
mAout.playAudio(audioData, bufferSize, nbSamples);
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment