From da7210c6498b6fcc2fbf829684ea399a6b4c3f65 Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Fri, 19 Jul 2013 05:08:34 +0200
Subject: FFMPEGPlayer Audio Sink Refactoring ..
- AudioSink w/ AudioFrame and formats public
- ALAudioSink uses a circular buffer now, hence relaxes the one-threaded player mode
- FFMPEGMediaPlayer uses multiple audio frames (equal to the ALAudioSink number)
and wraps data to NIO buffer w/o copy.
- FFMPEGMediaPlayer audio threading currently disabled: distorted sound
Seems that the ALAudioSink's circular buffer usage is good enough for now.
- Verbosity only w/ DEBUG flag
- New SyncedRingbuffer for effcient synced buffering
---
.../android/av/AndroidGLMediaPlayerAPI14.java | 10 +-
.../jogamp/opengl/openal/av/ALAudioSink.java | 484 ++++++++++++++++-----
.../classes/jogamp/opengl/util/av/AudioSink.java | 13 -
.../jogamp/opengl/util/av/GLMediaPlayerImpl.java | 4 +-
.../jogamp/opengl/util/av/JavaSoundAudioSink.java | 120 +++--
.../jogamp/opengl/util/av/NullAudioSink.java | 41 +-
.../jogamp/opengl/util/av/SyncedRingbuffer.java | 286 ++++++++++++
.../opengl/util/av/impl/FFMPEGMediaPlayer.java | 245 ++++++-----
8 files changed, 926 insertions(+), 277 deletions(-)
delete mode 100644 src/jogl/classes/jogamp/opengl/util/av/AudioSink.java
create mode 100644 src/jogl/classes/jogamp/opengl/util/av/SyncedRingbuffer.java
(limited to 'src/jogl/classes/jogamp')
diff --git a/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java b/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
index 23f9161d4..8356a2bae 100644
--- a/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
+++ b/src/jogl/classes/jogamp/opengl/android/av/AndroidGLMediaPlayerAPI14.java
@@ -32,6 +32,8 @@ import java.io.IOException;
import javax.media.opengl.GL;
import javax.media.opengl.GLES2;
+import com.jogamp.common.os.AndroidVersion;
+import com.jogamp.common.os.Platform;
import com.jogamp.opengl.util.texture.TextureSequence;
import jogamp.common.os.android.StaticContext;
@@ -60,7 +62,13 @@ public class AndroidGLMediaPlayerAPI14 extends GLMediaPlayerImpl {
static final boolean available;
static {
- available = true; // default .. TODO: May restrict availability ?
+ boolean _avail = false;
+ if(Platform.OS_TYPE.equals(Platform.OSType.ANDROID)) {
+ if(AndroidVersion.SDK_INT >= 14) {
+ _avail = true;
+ }
+ }
+ available = _avail;
}
public static final boolean isAvailable() { return available; }
diff --git a/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java b/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
index 690948c5a..87c7b937a 100644
--- a/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
@@ -1,176 +1,428 @@
+/**
+ * Copyright 2013 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
package jogamp.opengl.openal.av;
-import java.nio.Buffer;
-import java.nio.ByteBuffer;
-import java.lang.InterruptedException;
+import jogamp.opengl.util.av.SyncedRingbuffer;
-import jogamp.opengl.util.av.AudioSink;
-
-import com.jogamp.common.nio.Buffers;
-import com.jogamp.openal.*;
+import com.jogamp.openal.AL;
+import com.jogamp.openal.ALC;
+import com.jogamp.openal.ALCcontext;
+import com.jogamp.openal.ALCdevice;
+import com.jogamp.openal.ALFactory;
+import com.jogamp.opengl.util.av.AudioSink;
+/***
+ * OpenAL Audio Sink
+ */
public class ALAudioSink implements AudioSink {
- static ALC alc;
- static AL al;
- static ALCdevice device;
- static ALCcontext context;
-
- // AudioFormat parameters
- public static final int SAMPLE_RATE = 44100;
-
- // Chunk of audio processed at one time
- public static final int BUFFER_SIZE = 1000;
+ /** Chunk of audio processed at one time. FIXME: Parameterize .. */
+ public static final int BUFFER_SIZE = 4096;
public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
- // Sample time values
- public static final double SAMPLE_TIME_IN_SECS = 1.0 / SAMPLE_RATE;
- public static final double BUFFER_TIME_IN_SECS = SAMPLE_TIME_IN_SECS * SAMPLES_PER_BUFFER;
+ private static final ALC alc;
+ private static final AL al;
+ private static final boolean staticAvailable;
- private static int NUM_BUFFERS = 5;
- private static int bufferNumber = 0;
- private static int[] buffers = new int[NUM_BUFFERS];
- private static int[] source = new int[1];
- private static boolean initBuffer = true;
- private static int frequency = 44100;
- private static int format = AL.AL_FORMAT_STEREO16;
+ private String deviceSpecifier;
+ private ALCdevice device;
+ private ALCcontext context;
+
+ /** Sample period in seconds */
+ public float samplePeriod;
+ /** Buffer period in seconds */
+ public float bufferPeriod;
- private static boolean available = false;
+ static class ActiveBuffer {
+ ActiveBuffer(Integer name, int size) {
+ this.name = name;
+ this.size = size;
+ }
+ public final Integer name;
+ public final int size;
+ public String toString() { return "ABuffer[name "+name+", size "+size+"]"; }
+ }
+ int[] alBuffers = null;
+ private SyncedRingbuffer alBufferAvail = null;
+ private SyncedRingbuffer alBufferPlaying = null;
+ private int alBufferBytesQueued = 0;
+
+ private int[] alSource = null;
+ private AudioDataFormat chosenFormat;
+ private int alFormat;
+ private boolean initialized;
+
static {
-
- boolean joalFound = false;
+ ALC _alc = null;
+ AL _al = null;
try {
- Class.forName("com.jogamp.openal.ALFactory");
- joalFound = true;
- } catch(ClassNotFoundException e){
- // Joal not found on classpath
- }
-
- if(joalFound) {
+ _alc = ALFactory.getALC();
+ _al = ALFactory.getAL();
+ } catch(Throwable t) {
+ if( DEBUG ) {
+ System.err.println("ALAudioSink: Catched "+t.getClass().getName()+": "+t.getMessage());
+ t.printStackTrace();
+ }
+ }
+ alc = _alc;
+ al = _al;
+ staticAvailable = null != alc && null != al;
+ }
+
+ public ALAudioSink() {
+ initialized = false;
+ chosenFormat = null;
- alc = ALFactory.getALC();
- String deviceSpecifier;
-
+ if( !staticAvailable ) {
+ return;
+ }
+
+ try {
// Get handle to default device.
device = alc.alcOpenDevice(null);
if (device == null) {
- throw new ALException("Error opening default OpenAL device");
+ throw new RuntimeException("ALAudioSink: Error opening default OpenAL device");
}
// Get the device specifier.
deviceSpecifier = alc.alcGetString(device, ALC.ALC_DEVICE_SPECIFIER);
if (deviceSpecifier == null) {
- throw new ALException("Error getting specifier for default OpenAL device");
+ throw new RuntimeException("ALAudioSink: Error getting specifier for default OpenAL device");
}
// Create audio context.
context = alc.alcCreateContext(device, null);
if (context == null) {
- throw new ALException("Error creating OpenAL context");
+ throw new RuntimeException("ALAudioSink: Error creating OpenAL context");
}
// Set active context.
alc.alcMakeContextCurrent(context);
// Check for an error.
- if (alc.alcGetError(device) != ALC.ALC_NO_ERROR) {
- throw new ALException("Error making OpenAL context current");
+ if ( alc.alcGetError(device) != ALC.ALC_NO_ERROR ) {
+ throw new RuntimeException("ALAudioSink: Error making OpenAL context current");
}
- al = ALFactory.getAL();
-
- // Allocate buffers
- al.alGenBuffers(NUM_BUFFERS, buffers, 0);
- al.alGenSources(1, source, 0);
+ // Create source
+ {
+ alSource = new int[1];
+ al.alGenSources(1, alSource, 0);
+ final int err = al.alGetError();
+ if( err != AL.AL_NO_ERROR ) {
+ alSource = null;
+ throw new RuntimeException("ALAudioSink: Error generating Source: 0x"+Integer.toHexString(err));
+ }
+ }
- if(al.alGetError() != AL.AL_NO_ERROR) {
- throw new ALException("Error generating :(");
- }
-
- System.out.println("OpenAL audio sink using device: " + deviceSpecifier);
- available = true;
+ if( DEBUG ) {
+ System.err.println("ALAudioSink: Using device: " + deviceSpecifier);
+ }
+ initialized = true;
+ return;
+ } catch ( Exception e ) {
+ if( DEBUG ) {
+ System.err.println(e.getMessage());
+ }
+ destroy();
}
}
@Override
- public boolean isDataAvailable(int data_size) {
- return true;
+ public String toString() {
+ final int alSrcName = null != alSource ? alSource[0] : 0;
+ final int alBuffersLen = null != alBuffers ? alBuffers.length : 0;
+ return "ALAudioSink[init "+initialized+", device "+deviceSpecifier+", ctx "+context+", alSource "+alSrcName+
+ ", chosen "+chosenFormat+", alFormat "+toHexString(alFormat)+
+ ", buffers[total "+alBuffersLen+", avail "+alBufferAvail.size()+", "+alBufferPlaying.getFreeSlots()+
+ ", queued[bufferCount "+alBufferPlaying.size()+", "+getQueuedTime() + " ms, " + alBufferBytesQueued+" bytes]";
}
@Override
- public void writeData(byte[] sampleData, int data_size) {
- // OpenAL consumes buffers in the background
- // we first need to initialize the OpenAL buffers then
- // start continous playback.
- alc.alcMakeContextCurrent(context);
- if(initBuffer) {
-
- ByteBuffer data = Buffers.newDirectByteBuffer(sampleData);
- al.alBufferData(buffers[bufferNumber], format, data, data_size, frequency);
- int error = al.alGetError();
- if(error != AL.AL_NO_ERROR) {
- System.out.println("bufferNumber"+bufferNumber+" Data "+sampleData+" size"+data_size);
- throw new ALException("Error loading :( error code: " + error);
+ public AudioDataFormat getPreferredFormat() {
+ return DefaultFormat;
+ }
+
+ @Override
+ public AudioDataFormat initSink(AudioDataFormat requestedFormat, int bufferCount) {
+ if( !staticAvailable ) {
+ return null;
+ }
+ samplePeriod = 1.0f / requestedFormat.sampleRate;
+ bufferPeriod = samplePeriod * SAMPLES_PER_BUFFER;
+ switch( requestedFormat.channelCount ) {
+ case 1: {
+ switch ( requestedFormat.sampleSize ) {
+ case 8:
+ alFormat = AL.AL_FORMAT_MONO8; break;
+ case 16:
+ alFormat = AL.AL_FORMAT_MONO16; break;
+ default:
+ return null;
+ }
+ } break;
+ case 2:
+ switch ( requestedFormat.sampleSize ) {
+ case 8:
+ alFormat = AL.AL_FORMAT_STEREO8; break;
+ case 16:
+ alFormat = AL.AL_FORMAT_STEREO16; break;
+ default:
+ return null;
+ }
+ }
+ // Allocate buffers
+ destroyBuffers();
+ {
+ alBuffers = new int[bufferCount];
+ al.alGenBuffers(bufferCount, alBuffers, 0);
+ final int err = al.alGetError();
+ if( err != AL.AL_NO_ERROR ) {
+ alBuffers = null;
+ throw new RuntimeException("ALAudioSink: Error generating Buffers: 0x"+Integer.toHexString(err));
}
-
- if(bufferNumber==NUM_BUFFERS-1){
- // all buffers queued
- al.alSourceQueueBuffers(source[0], NUM_BUFFERS, buffers, 0);
- // start playback
- al.alSourcePlay(source[0]);
- if(al.alGetError() != AL.AL_NO_ERROR) {
- throw new ALException("Error starting :(");
+ final Integer[] alBufferRingArray = new Integer[bufferCount];
+ for(int i=0; i(alBufferRingArray, true /* full */);
+ alBufferPlaying = new SyncedRingbuffer(new ActiveBuffer[bufferCount], false /* full */);
+ }
+
+
+ chosenFormat = requestedFormat;
+ return chosenFormat;
+ }
+
+ private void destroyBuffers() {
+ if( !staticAvailable ) {
+ return;
+ }
+ if( null != alBuffers ) {
+ try {
+ al.alDeleteBuffers(alBufferAvail.capacity(), alBuffers, 0);
+ } catch (Throwable t) {
+ if( DEBUG ) {
+ System.err.println("Catched "+t.getClass().getName()+": "+t.getMessage());
+ t.printStackTrace();
}
- initBuffer=false;
}
-
- // update buffer number to fill
- bufferNumber=(bufferNumber+1)%NUM_BUFFERS;
- } else {
- // OpenAL is playing in the background.
- // one new frame with audio data is ready
-
- // first wait for openal to release one buffer
- int[] buffer=new int[1];
- int[] val=new int[1];
- do {
- al.alGetSourcei(source[0], AL.AL_BUFFERS_PROCESSED, val, 0);
- if(val[0] <= 0){
- try {
- Thread.sleep(1);
- } catch (InterruptedException e){
- }
+ alBufferAvail.clear(true);
+ alBufferAvail = null;
+ alBufferPlaying.clear(true);
+ alBufferPlaying = null;
+ alBufferBytesQueued = 0;
+ alBuffers = null;
+ }
+ }
+
+ @Override
+ public void destroy() {
+ initialized = false;
+ if( !staticAvailable ) {
+ return;
+ }
+ if( null != alSource ) {
+ try {
+ al.alDeleteSources(1, alSource, 0);
+ } catch (Throwable t) {
+ if( DEBUG ) {
+ System.err.println("Catched "+t.getClass().getName()+": "+t.getMessage());
+ t.printStackTrace();
}
- } while (val[0] <= 0);
-
- // fill and requeue the empty buffer
- al.alSourceUnqueueBuffers(source[0], 1, buffer , 0);
- Buffer data = Buffers.newDirectByteBuffer(sampleData);
- al.alBufferData(buffer[0], format, data, data_size, frequency);
- al.alSourceQueueBuffers(source[0], 1, buffer, 0);
- if(al.alGetError() != AL.AL_NO_ERROR) {
- throw new ALException("Error buffering :(");
}
+ alSource = null;
+ }
+
+ destroyBuffers();
- // Restart openal playback if needed
- al.alGetSourcei(source[0], AL.AL_SOURCE_STATE, val, 0);
- if(val[0] != al.AL_PLAYING) {
- al.alSourcePlay(source[0]);
+ if( null != context ) {
+ try {
+ alc.alcDestroyContext(context);
+ } catch (Throwable t) {
+ if( DEBUG ) {
+ System.err.println("Catched "+t.getClass().getName()+": "+t.getMessage());
+ t.printStackTrace();
+ }
+ }
+ context = null;
+ }
+ if( null != device ) {
+ try {
+ alc.alcCloseDevice(device);
+ } catch (Throwable t) {
+ if( DEBUG ) {
+ System.err.println("Catched "+t.getClass().getName()+": "+t.getMessage());
+ t.printStackTrace();
+ }
}
+ device = null;
}
+ chosenFormat = null;
+ }
+
+ @Override
+ public boolean isInitialized() {
+ return initialized;
}
+
+ private final void dequeueBuffer(boolean wait) {
+ int alErr = AL.AL_NO_ERROR;
+ final int[] val=new int[1];
+ do {
+ al.alGetSourcei(alSource[0], AL.AL_BUFFERS_PROCESSED, val, 0);
+ alErr = al.alGetError();
+ if( AL.AL_NO_ERROR != alErr ) {
+ throw new RuntimeException("ALError "+toHexString(alErr)+" while quering processed buffers at source. "+this);
+ }
+ if( wait && val[0] <= 0 ) {
+ try {
+ Thread.sleep(1);
+ } catch (InterruptedException e){
+ }
+ }
+ } while (val[0] <= 0);
+ final int processedBuffers = val[0];
+ if( processedBuffers > 0 ) {
+ int[] buffers=new int[processedBuffers];
+ al.alSourceUnqueueBuffers(alSource[0], processedBuffers, buffers, 0);
+ alErr = al.alGetError();
+ if( AL.AL_NO_ERROR != alErr ) {
+ throw new RuntimeException("ALError "+toHexString(alErr)+" while dequeueing "+processedBuffers+" processed buffers. "+this);
+ }
+ for ( int i=0; i
+ * FIXME: Parameterize .. all configs .. best via an init-method, passing requested
+ * audio capabilities
+ *
+ */
+public class JavaSoundAudioSink implements AudioSink {
// Chunk of audio processed at one time
public static final int BUFFER_SIZE = 1000;
public static final int SAMPLES_PER_BUFFER = BUFFER_SIZE / 2;
+ private static final boolean staticAvailable;
// Sample time values
- public static final double SAMPLE_TIME_IN_SECS = 1.0 / SAMPLE_RATE;
- public static final double BUFFER_TIME_IN_SECS = SAMPLE_TIME_IN_SECS * SAMPLES_PER_BUFFER;
+ // public static final double SAMPLE_TIME_IN_SECS = 1.0 / DEFAULT_SAMPLE_RATE;
+ // public static final double BUFFER_TIME_IN_SECS = SAMPLE_TIME_IN_SECS * SAMPLES_PER_BUFFER;
- private static AudioFormat format;
- private static DataLine.Info info;
- private static SourceDataLine auline;
- private static int bufferCount;
- private static byte [] sampleData = new byte[BUFFER_SIZE];
-
- private static boolean available;
+ private javax.sound.sampled.AudioFormat format;
+ private DataLine.Info info;
+ private SourceDataLine auline;
+ private int bufferCount;
+ private byte [] sampleData = new byte[BUFFER_SIZE];
+ private boolean initialized = false;
+ private AudioDataFormat chosenFormat = null;
static {
- // Create the audio format we wish to use
- format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, CHANNELS, SIGNED, BIG_ENDIAN);
+ boolean ok = false;
+ try {
+ AudioSystem.getAudioFileTypes();
+ ok = true;
+ } catch (Throwable t) {
+
+ }
+ staticAvailable=ok;
+ }
+
+ @Override
+ public String toString() {
+ return "JavaSoundSink[init "+initialized+", dataLine "+info+", source "+auline+", bufferCount "+bufferCount+
+ ", chosen "+chosenFormat+", jsFormat "+format;
+ }
+
+ @Override
+ public AudioDataFormat getPreferredFormat() {
+ return DefaultFormat;
+ }
+
+ @Override
+ public AudioDataFormat initSink(AudioDataFormat requestedFormat, int bufferCount) {
+ if( !staticAvailable ) {
+ return null;
+ }
+ // Create the audio format we wish to use
+ format = new javax.sound.sampled.AudioFormat(requestedFormat.sampleRate, requestedFormat.sampleSize, requestedFormat.channelCount, requestedFormat.signed, !requestedFormat.littleEndian);
// Create dataline info object describing line format
info = new DataLine.Info(SourceDataLine.class, format);
@@ -47,33 +75,61 @@ public class JavaSoundAudioSink implements AudioSink {
auline.open(format);
auline.start();
System.out.println("JavaSound audio sink");
- available=true;
+ initialized=true;
+ chosenFormat = requestedFormat;
} catch (Exception e) {
- available=false;
+ initialized=false;
}
- }
+ return chosenFormat;
+ }
- public void writeData(byte[] sampleData, int data_size) {
- int written = 0;
- int len;
- while (data_size > 0) {
- len = auline.write(sampleData, written, data_size);
+ @Override
+ public boolean isInitialized() {
+ return initialized;
+ }
+
+ @Override
+ public void destroy() {
+ initialized = false;
+ chosenFormat = null;
+ // FIXEM: complete code!
+ }
+
+ public void writeData(AudioFrame audioFrame) {
+ int data_size = audioFrame.dataSize;
+ final byte[] lala = new byte[data_size];
+ final int p = audioFrame.data.position();
+ audioFrame.data.get(lala, 0, data_size);
+ audioFrame.data.position(p);
+
+ int written = 0;
+ int len;
+ while (data_size > 0) {
+ // Nope: We don't make compromises for this crappy API !
+ len = auline.write(lala, written, data_size);
data_size -= len;
written += len;
}
}
- public int getDataAvailable() {
+ @Override
+ public int getQueuedByteCount() {
return auline.available();
}
- public boolean isDataAvailable(int data_size) {
- return auline.available()>=data_size;
+ @Override
+ public int getQueuedTime() {
+ return 0; // FIXME
}
+
@Override
- public boolean isAudioSinkAvailable() {
- return available;
+ public int getWritableBufferCount() {
+ return 1;
+ }
+
+ public boolean isDataAvailable(int data_size) {
+ return auline.available()>=data_size;
}
}
diff --git a/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java b/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
index d92967849..cef1c3361 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
@@ -1,23 +1,50 @@
package jogamp.opengl.util.av;
+
+import com.jogamp.opengl.util.av.AudioSink;
+
public class NullAudioSink implements AudioSink {
@Override
- public int getDataAvailable() {
- return 0;
+ public boolean isInitialized() {
+ return true;
}
@Override
- public boolean isDataAvailable(int data_size) {
- return false;
+ public AudioDataFormat getPreferredFormat() {
+ return DefaultFormat;
+ }
+
+ @Override
+ public AudioDataFormat initSink(AudioDataFormat requestedFormat, int bufferCount) {
+ return requestedFormat;
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+ @Override
+ public int getQueuedByteCount() {
+ return 0;
+ }
+
+ @Override
+ public int getQueuedTime() {
+ return 0;
}
@Override
- public void writeData(byte[] sampleData, int data_size) {
+ public int getWritableBufferCount() {
+ return 1;
+ }
+
+ @Override
+ public boolean isDataAvailable(int data_size) {
+ return false;
}
@Override
- public boolean isAudioSinkAvailable() {
- return true;
+ public void writeData(AudioFrame audioFrame) {
}
}
diff --git a/src/jogl/classes/jogamp/opengl/util/av/SyncedRingbuffer.java b/src/jogl/classes/jogamp/opengl/util/av/SyncedRingbuffer.java
new file mode 100644
index 000000000..5f5d69cf8
--- /dev/null
+++ b/src/jogl/classes/jogamp/opengl/util/av/SyncedRingbuffer.java
@@ -0,0 +1,286 @@
+/**
+ * Copyright 2013 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package jogamp.opengl.util.av;
+
+/**
+ * Simple synchronized ring buffer implementation.
+ *
+ * Caller can chose whether to block until get / put is able to proceed or not.
+ *
+ *
+ * Caller can chose whether to pass an empty array and clear references at get,
+ * or using a preset array for circular access of same objects.
+ *
+ *
+ * Circular write position is equal to the read position if buffer is full or if buffer is empty.
+ *
+ */
+public class SyncedRingbuffer {
+
+ protected final Object sync = new Object();
+ protected final T[] array;
+ protected final int capacity;
+ protected int readPos;
+ protected int writePos;
+ protected int size;
+
+ public final String toString() {
+ return "SyncedRingbuffer>[filled "+size+" / "+capacity+", writePos "+writePos+", readPos "+readPos+"]";
+ }
+
+ /**
+ * Create instance w/ the given array and it's capacity, e.g.:
+ *
+ * SyncedRingbuffer r = new SyncedRingbuffer(new Integer[10]);
+ *
+ *
+ * The array may either be clear, or preset w/ elements!
+ *
+ * @param full if true, given array is assumed to be full, i.e. {@link #isFull()} will return true.
+ * @param array
+ */
+ public SyncedRingbuffer(T[] array, boolean full) {
+ this.array = array;
+ this.capacity = array.length;
+ clearImpl(false);
+ if(full) {
+ size = capacity;
+ }
+ }
+
+ public final int capacity() {
+ return capacity;
+ }
+
+ /**
+ * Resets all ring buffer pointer to zero.
+ *
+ * {@link #isEmpty()} will return true
after calling this method.
+ *
+ *
+ * If clearRefs
is true, all ring buffer slots will be set to null
.
+ *
+ * @param clearRefs if true, all ring buffer slots will be flushed, otherwise they remain intact.
+ */
+ public final void clear(boolean clearRefs) {
+ synchronized ( sync ) {
+ clearImpl(clearRefs);
+ }
+ }
+
+ private final void clearImpl(boolean clearRefs) {
+ readPos = 0;
+ writePos = 0;
+ size = 0;
+ if( clearRefs ) {
+ for(int i=0; i
+ * Impl. returns the element at the current read position
+ * and advances the read position - if available.
+ *
+ *
+ * If clearRef
is true, the returned ring buffer slot will be set to null
.
+ *
+ *
+ * Method is non blocking and returns immediately;.
+ *
+ * @param clearRef if true, the returned ring buffer slot will be flushed, otherwise it remains intact.
+ * @return the oldest put element if available, otherwise null.
+ */
+ public final T get(boolean clearRef) {
+ try {
+ return getImpl(clearRef, false, false);
+ } catch (InterruptedException ie) { throw new RuntimeException(ie); }
+ }
+
+ /**
+ * Returns the oldest put element.
+ *
+ * Impl. returns the element at the current read position
+ * and advances the read position.
+ *
+ *
+ * If clearRef
is true, the returned ring buffer slot will be set to null
.
+ *
+ *
+ * Methods blocks until an element becomes available via put.
+ *
+ * @param clearRef if true, the returned ring buffer slot will be flushed, otherwise it remains intact.
+ * @return the oldest put element
+ * @throws InterruptedException
+ */
+ public final T getBlocking(boolean clearRef) throws InterruptedException {
+ return getImpl(clearRef, true, false);
+ }
+
+ public final T peek() throws InterruptedException {
+ return getImpl(false, false, true);
+ }
+ public final T peekBlocking() throws InterruptedException {
+ return getImpl(false, true, true);
+ }
+
+ private final T getImpl(boolean clearRef, boolean blocking, boolean peek) throws InterruptedException {
+ synchronized ( sync ) {
+ if( 0 == size ) {
+ if( blocking ) {
+ while( 0 == size ) {
+ sync.wait();
+ }
+ } else {
+ return null;
+ }
+ }
+ final T r = array[readPos];
+ if( !peek ) {
+ if( clearRef ) {
+ array[readPos] = null;
+ }
+ readPos = (readPos + 1) % capacity;
+ size--;
+ sync.notifyAll(); // notify waiting putter
+ }
+ return r;
+ }
+ }
+
+ /**
+ * Puts the element e
at the current write position
+ * and advances the write position.
+ *
+ * Returns true if successful, otherwise false in case buffer is full.
+ *
+ *
+ * Method is non blocking and returns immediately;.
+ *
+ */
+ public final boolean put(T e) {
+ try {
+ return putImpl(e, false, false);
+ } catch (InterruptedException ie) { throw new RuntimeException(ie); }
+ }
+
+ /**
+ * Puts the element e
at the current write position
+ * and advances the write position.
+ *
+ * Method blocks until a free slot becomes available via get.
+ *
+ * @throws InterruptedException
+ */
+ public final void putBlocking(T e) throws InterruptedException {
+ if( !putImpl(e, false, true) ) {
+ throw new InternalError("Blocking put failed: "+this);
+ }
+ }
+
+ /**
+ * Keeps the element at the current write position intact
+ * and advances the write position.
+ *
+ * Returns true if successful, otherwise false in case buffer is full.
+ *
+ *
+ * If blocking
is true, method blocks until a free slot becomes available via get.
+ *
+ * @param blocking if true, wait until a free slot becomes available via get.
+ * @throws InterruptedException
+ */
+ public final boolean putSame(boolean blocking) throws InterruptedException {
+ return putImpl(null, true, blocking);
+ }
+
+ private final boolean putImpl(T e, boolean sameRef, boolean blocking) throws InterruptedException {
+ synchronized ( sync ) {
+ if( capacity <= size ) {
+ if( blocking ) {
+ while( capacity <= size ) {
+ sync.wait();
+ }
+ } else {
+ return false;
+ }
+ }
+ if( !sameRef ) {
+ array[ writePos ] = e;
+ }
+ writePos = (writePos + 1) % capacity;
+ size++;
+ sync.notifyAll(); // notify waiting getter
+ return true;
+ }
+ }
+
+ public final void waitForFreeSlots(int count) throws InterruptedException {
+ synchronized ( sync ) {
+ if( capacity - size < count ) {
+ while( capacity - size < count ) {
+ System.err.println("XXXX AAA XXX");
+ sync.wait();
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGMediaPlayer.java b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGMediaPlayer.java
index 33b5b3b20..65b867ba1 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGMediaPlayer.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGMediaPlayer.java
@@ -38,20 +38,17 @@ import javax.media.opengl.GL;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GLException;
-import java.util.Arrays;
-import java.util.Queue;
-
-import com.jogamp.common.util.ReflectionUtil;
import com.jogamp.common.util.VersionNumber;
import com.jogamp.gluegen.runtime.ProcAddressTable;
import com.jogamp.opengl.util.GLPixelStorageModes;
-import com.jogamp.opengl.util.av.GLMediaPlayerFactory;
+import com.jogamp.opengl.util.av.AudioSink;
+import com.jogamp.opengl.util.av.AudioSinkFactory;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureSequence;
import jogamp.opengl.GLContextImpl;
-import jogamp.opengl.util.av.AudioSink;
import jogamp.opengl.util.av.EGLMediaPlayerImpl;
+import jogamp.opengl.util.av.SyncedRingbuffer;
/***
* Implementation utilizes Libav
@@ -111,13 +108,10 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
private static final int TEMP_BUFFER_COUNT = 20;
// Instance data
- private static AudioSink audioSink;
- private static int maxAvailableAudio;
-
public static final VersionNumber avUtilVersion;
public static final VersionNumber avFormatVersion;
public static final VersionNumber avCodecVersion;
- static boolean available;
+ static final boolean available;
static {
if(FFMPEGDynamicLibraryBundleInfo.initSingleton()) {
@@ -129,24 +123,6 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
System.err.println("LIB_AV Codec : "+avCodecVersion);
initIDs0();
available = true;
- final ClassLoader cl = GLMediaPlayerFactory.class.getClassLoader();
-
- if(ReflectionUtil.isClassAvailable("com.jogamp.openal.ALFactory", cl)){
- // Only instance ALAudioSink if JOAL is found on the classpath.
- audioSink = (AudioSink) ReflectionUtil.createInstance("jogamp.opengl.openal.av.ALAudioSink", cl);
- if(!audioSink.isAudioSinkAvailable()){
- // Failed to initialize OpenAL.
- audioSink=null;
- }
- }
- if(audioSink==null) {
- audioSink = (AudioSink) ReflectionUtil.createInstance("jogamp.opengl.util.av.JavaSoundAudioSink", cl);
- if(!audioSink.isAudioSinkAvailable()) {
- audioSink = (AudioSink) ReflectionUtil.createInstance("jogamp.opengl.util.av.NullAudioSink", cl);
- }
- }
- maxAvailableAudio = audioSink.getDataAvailable();
-
} else {
avUtilVersion = null;
avFormatVersion = null;
@@ -163,6 +139,10 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
( vers >> 0 ) & 0xFF );
}
+ //
+ // Video
+ //
+
protected long moviePtr = 0;
protected long procAddrGLTexSubImage2D = 0;
protected EGLMediaPlayerImpl.EGLTextureFrame lastTex = null;
@@ -176,17 +156,29 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
protected int texWidth, texHeight; // overall (stuffing planes in one texture)
protected ByteBuffer texCopy;
+ //
+ // Audio
+ //
+
+ protected final int AudioFrameCount = 8;
+ protected final AudioSink audioSink;
+ protected final int maxAvailableAudio;
+ protected AudioSink.AudioDataFormat chosenAudioFormat;
+ protected final SyncedRingbuffer audioFramesBuffer = new SyncedRingbuffer(new AudioSink.AudioFrame[AudioFrameCount], false /* full */);
+
public FFMPEGMediaPlayer() {
super(TextureType.GL, false);
if(!available) {
throw new RuntimeException("FFMPEGMediaPlayer not available");
}
setTextureCount(1);
- moviePtr = createInstance0(true);
+ moviePtr = createInstance0(DEBUG);
if(0==moviePtr) {
throw new GLException("Couldn't create FFMPEGInstance");
}
psm = new GLPixelStorageModes();
+ audioSink = AudioSinkFactory.createDefault();
+ maxAvailableAudio = audioSink.getQueuedByteCount();
}
@Override
@@ -221,9 +213,11 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
}
final String urlS=urlConn.getURL().toExternalForm();
- System.out.println("setURL: p1 "+this);
- setStream0(moviePtr, urlS, -1, -1);
- System.out.println("setURL: p2 "+this);
+ chosenAudioFormat = audioSink.initSink(audioSink.getPreferredFormat(), AudioFrameCount);
+ System.err.println("setURL: p1 "+this);
+ setStream0(moviePtr, urlS, -1, -1, AudioFrameCount);
+ System.err.println("setURL: p2 "+this);
+
int tf, tif=GL.GL_RGBA; // texture format and internal format
switch(vBytesPerPixelPerPlane) {
case 1:
@@ -264,74 +258,103 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
}
- private class AudioFrame {
- final byte[] sampleData;
- final int data_size;
- final int audio_pts;
- AudioFrame(byte[] sampleData, int data_size, int audio_pts) {
- this.sampleData=sampleData;
- this.data_size=data_size;
- this.audio_pts=audio_pts;
+ private final void pushSound(ByteBuffer sampleData, int data_size, int audio_pts) {
+ if( audioPusher != null && audioPusher.isRunning() ) {
+ try {
+ audioFramesBuffer.putBlocking(new AudioSink.AudioFrame(sampleData, data_size, audio_pts));
+ } catch (InterruptedException e) {
+ e.printStackTrace(); // oops
+ }
+ if( null != audioPusher ) {
+ audioPusher.pushOne();
+ }
}
}
- static final Queue audioFrameBuffer = new java.util.LinkedList();
-
- private void updateSound(byte[] sampleData, int data_size, int audio_pts) {
-/*
- // Visualize incomming data
- int c=0;
- for(byte b: sampleData){
- if(b<0) {
- System.out.print(" ");
- } else if(b<64) {
- System.out.print("_");
- } else if(b < 128) {
- System.out.print("-");
- } else if(b == 128) {
- System.out.print("=");
- } else if(b < 256-64) {
- System.out.print("\"");
- } else {
- System.out.print("'");
+ class AudioPusher extends Thread {
+ volatile boolean shallStop = false;
+ volatile boolean isBlocked = false;
+
+ AudioPusher() {
+ setDaemon(true);
+ }
+ public void requestStop() {
+ shallStop = true;
+ if( isBlocked ) {
+ // interrupt();
}
-
- c++;
- if(c>=40)
- break;
}
- System.out.println("jA");
-*/
-
- //TODO reduce GC
- audioFrameBuffer.add(new AudioFrame(sampleData, data_size, audio_pts));
- pumpAudio();
- }
-
- private void pumpAudio() {
- if(audioSink.getDataAvailable()==maxAvailableAudio){
- System.out.println("warning: audio buffer underrun");
+ public boolean isRunning() { return !shallStop; }
+
+ public void run() {
+ setName(getName()+"-AudioPusher_"+AudioPusherInstanceId);
+ AudioPusherInstanceId++;
+
+ while( !shallStop ){
+ pushOne();
+ }
}
- while(audioFrameBuffer.peek()!=null){
- AudioFrame a = audioFrameBuffer.peek();
-
- // poor mans audio sync .. TODO: off thread
- final long now = System.currentTimeMillis();
- final long now_d = now - lastAudioTime;
- final long pts_d = a.audio_pts - lastAudioPTS;
- final long dt = (long) ( (float) ( pts_d - now_d ) / getPlaySpeed() ) ;
-
- System.err.println("s: pts-a "+a.audio_pts+", pts-d "+pts_d+", now_d "+now_d+", dt "+dt);
- lastAudioTime = now;
- if( (dt audio_dt_d && !shallStop;
+ final long sleepP = dt - ( audio_dt_d / 2 );
+ if(DEBUG) {
+ final int qAT = audioSink.getQueuedTime();
+ System.err.println("s: pts-a "+audioFrame.audioPTS+", qAT "+qAT+", pts-d "+pts_d+", now_d "+now_d+", dt "+dt+", sleep "+sleep+", sleepP "+sleepP+" ms");
+ }
+ if( sleep ) {
+ try {
+ isBlocked = true;
+ Thread.sleep( sleepP );
+ } catch (InterruptedException e) {
+ e.printStackTrace(); // oops
+ }
+ isBlocked = false;
+ lastAudioTime = System.currentTimeMillis();
+ } else {
+ lastAudioTime = now;
+ }
+ if( !shallStop && audioSink.isDataAvailable(audioFrame.dataSize) ) {
+ audioSink.writeData(audioFrame);
+ lastAudioPTS=audioFrame.audioPTS;
+ }
}
}
}
+
+ static int AudioPusherInstanceId = 0;
+ private AudioPusher audioPusher = null;
+
+ private final void stopAudioPusher() {
+ if( null != audioPusher ) {
+ audioPusher.requestStop();
+ audioPusher = null;
+ }
+ audioFramesBuffer.clear(true);
+ }
+ private final void startAudioPusher() {
+ stopAudioPusher();
+ audioPusher = new AudioPusher();
+ // audioPusher.start();
+ }
private void updateAttributes2(int pixFmt, int planes, int bitsPerPixel, int bytesPerPixelPerPlane,
int lSz0, int lSz1, int lSz2,
@@ -448,6 +471,7 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
if(0==moviePtr) {
return false;
}
+ startAudioPusher();
return true;
}
@@ -457,6 +481,7 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
if(0==moviePtr) {
return false;
}
+ stopAudioPusher();
return true;
}
@@ -466,6 +491,7 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
if(0==moviePtr) {
return false;
}
+ stopAudioPusher();
return true;
}
@@ -475,12 +501,13 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
if(0==moviePtr) {
throw new GLException("FFMPEG native instance null");
}
+ stopAudioPusher();
int pts0 = getVideoPTS0(moviePtr);
int pts1 = seek0(moviePtr, msec);
System.err.println("Seek: "+pts0+" -> "+msec+" : "+pts1);
- audioFrameBuffer.clear();
lastAudioPTS=pts1;
lastVideoPTS=pts1;
+ startAudioPusher();
return pts1;
}
@@ -509,6 +536,12 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
tex.enable(gl);
tex.bind(gl);
+ try {
+ audioFramesBuffer.waitForFreeSlots(2);
+ } catch (InterruptedException e) {
+ e.printStackTrace(); // oops
+ }
+
/* try decode 10 packets to find one containing video
(res == 2) */
int res = 0;
@@ -529,27 +562,27 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
final long pts_d = pts - lastAudioPTS - 444; /* hack 444 == play video 444ms ahead of audio */
final long dt = Math.min(47, (long) ( (float) ( pts_d - now_d ) / getPlaySpeed() ) ) ;
//final long dt = (long) ( (float) ( pts_d - now_d ) / getPlaySpeed() ) ;
- lastVideoTime = now;
- System.err.println("s: pts-v "+pts+", pts-d "+pts_d+", now_d "+now_d+", dt "+dt);
-
- if(dt>video_dt_d && dt<1000 && audioSink.getDataAvailable()video_dt_d && dt<1000 && audioSink.getQueuedByteCount()pts_d) {
- System.err.println("s: pts-v "+pts+", pts-d "+pts_d+", now_d "+now_d+", dt "+dt);
- } */
+ lastVideoTime = System.currentTimeMillis();
+ } else {
+ lastVideoTime = now;
+ }
}
- pumpAudio();
lastVideoPTS = pts;
}
return lastTex;
}
- private void consumeAudio(int len) {
-
- }
-
private static native int getAvUtilVersion0();
private static native int getAvFormatVersion0();
private static native int getAvCodecVersion0();
@@ -557,7 +590,7 @@ public class FFMPEGMediaPlayer extends EGLMediaPlayerImpl {
private native long createInstance0(boolean verbose);
private native void destroyInstance0(long moviePtr);
- private native void setStream0(long moviePtr, String url, int vid, int aid);
+ private native void setStream0(long moviePtr, String url, int vid, int aid, int audioFrameCount);
private native int getVideoPTS0(long moviePtr);
--
cgit v1.2.3