From 1b5731a9beb0ed76c8424aefffb7940ebbc69fab Mon Sep 17 00:00:00 2001
From: Pankaj Yadav <pk@videolan.org>
Date: Sat, 12 Feb 2011 13:11:27 +0530
Subject: [PATCH] Finish Aout and some cosmetic fixes

---
 extras/package/android/vlc-android/jni/aout.c      |   37 ++++++++++++++++++--
 extras/package/android/vlc-android/jni/aout.h      |    2 +
 .../android/vlc-android/src/vlc/android/Aout.java  |    9 ++++-
 .../vlc-android/src/vlc/android/LibVLC.java        |    7 ++--
 4 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/extras/package/android/vlc-android/jni/aout.c b/extras/package/android/vlc-android/jni/aout.c
index 039a4a6..f600263 100644
--- a/extras/package/android/vlc-android/jni/aout.c
+++ b/extras/package/android/vlc-android/jni/aout.c
@@ -48,14 +48,45 @@ void aout_open(void **opaque, unsigned int *rate, unsigned int *nb_channels, uns
                              *rate, *nb_channels, *nb_samples);
 
     // 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;
-    LOGI("%i", bufferSize);
-    (*p_sys->p_env)->CallVoidMethod(p_sys->p_env, p_sys->j_libVlc, p_sys->play);
+    JNIEnv *p_env = p_sys->p_env;
+
+//Dunno Why i have to comment this
+//    if (!p_sys->b_allocated)
+//    {
+        //Allocate a jByteArray
+        jbyteArray byteArray = (*p_env)->NewByteArray(p_env, bufferSize);
+        if (byteArray == NULL)
+        {
+            LOGE("Couldn't allocate the Java byte array to store the Audio frame !");
+            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 (p_sys->byteArray == NULL)
+        {
+            LOGE("Couldn't create the global reference !");
+            return;
+        }
+
+        /* The local reference is no longer useful. */
+        (*p_env)->DeleteLocalRef(p_env, byteArray);
+
+        p_sys->b_allocated = 1;
+//   }
+
+    (*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)
diff --git a/extras/package/android/vlc-android/jni/aout.h b/extras/package/android/vlc-android/jni/aout.h
index 2dd74d5..5ef8c80 100644
--- a/extras/package/android/vlc-android/jni/aout.h
+++ b/extras/package/android/vlc-android/jni/aout.h
@@ -6,6 +6,8 @@ typedef struct
     jobject j_libVlc;   // Pointer to the LibVLC Java object
     jmethodID play;
     JNIEnv *p_env;
+    jbyteArray byteArray;
+    unsigned char b_allocated; // whether ByteArray Allocated or not
 } aout_sys_t;
 
 void aout_open(void **opaque, unsigned int *rate, unsigned int *nb_channels, unsigned int *fourCCFormat, unsigned int *nb_samples);
diff --git a/extras/package/android/vlc-android/src/vlc/android/Aout.java b/extras/package/android/vlc-android/src/vlc/android/Aout.java
index 595d93f..7ec2800 100644
--- a/extras/package/android/vlc-android/src/vlc/android/Aout.java
+++ b/extras/package/android/vlc-android/src/vlc/android/Aout.java
@@ -5,9 +5,9 @@ import android.media.AudioFormat;
 import android.media.AudioManager;
 import android.media.AudioTrack;
 
+
 public class Aout {
 	private AudioTrack mAudioTrack;
-	private static final String TAG = "LibVLC";
 
 	public void Init(int sampleRateInHz, int channels, int samples)
 	{
@@ -15,5 +15,12 @@ public class Aout {
 		mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRateInHz,
 									 AudioFormat.CHANNEL_CONFIGURATION_MONO,
 									 AudioFormat.ENCODING_PCM_16BIT, channels * samples * 2, AudioTrack.MODE_STREAM);
+                mAudioTrack.play();
 	}
+
+        public void playBuffer(byte[] buffer,int bufferSize, int nb_samples)
+        {
+            mAudioTrack.write(buffer,0,bufferSize);
+        }
+
 }
diff --git a/extras/package/android/vlc-android/src/vlc/android/LibVLC.java b/extras/package/android/vlc-android/src/vlc/android/LibVLC.java
index 776327c..b6860ae 100644
--- a/extras/package/android/vlc-android/src/vlc/android/LibVLC.java
+++ b/extras/package/android/vlc-android/src/vlc/android/LibVLC.java
@@ -13,7 +13,7 @@ public class LibVLC {
 
     private GLSurfaceView mSurfaceView;
     private Vout mVout;
-    
+
     private Aout mAout;
 
     /* Load library before object instantiation */
@@ -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[] buffer,int bufferSize, int nb_samples)
     {
-    	Log.d(TAG, "Playing an audio buffer in Java");
+        Log.d(TAG, "Playing an audio buffer in Java");
+        mAout.playBuffer(buffer,bufferSize,nb_samples);
     }
 
     /**
-- 
1.7.1

