From 517371b2c200783890e2f6a173748cf65d3c8c91 Mon Sep 17 00:00:00 2001
From: Sven Gothel
Date: Sat, 24 Aug 2013 23:38:42 +0200
Subject: AudioSink.init(..) abstract 'frame count' -> duration [ms] allowing
non-frame based AudioSink's to deal w/ desired queue sizes.
- Rename AudioSink.initSink(..) -> AudioSink.init(..)
- Move: "int initialFrameCount, int frameGrowAmount, int frameLimit" to
"int initialQueueSize, int queueGrowAmount, int queueLimit"
based on milliseconds instead of frame count.
- Passing hint 'float frameDuration' to calculate frame count for fame based audio sink, i.e. ALAudioSink.
- Adding sensible static final default values
- AudioDataFormat: Add convenient conversion routines (samples/bytes/frame-count)
- FFMPEGMediaPlayer: Retrieve audio frame size in samples per channel, pass it to AudioSink.init(..)
to properly calculate frame count/limits based on duration.
---
.../com/jogamp/opengl/util/av/AudioSink.java | 104 ++++++++++++++++-----
.../jogamp/opengl/openal/av/ALAudioSink.java | 23 +++--
.../jogamp/opengl/util/av/JavaSoundAudioSink.java | 4 +-
.../jogamp/opengl/util/av/NullAudioSink.java | 2 +-
.../opengl/util/av/impl/FFMPEGMediaPlayer.java | 46 +++++----
src/jogl/native/libav/ffmpeg_tool.h | 2 +-
.../jogamp_opengl_util_av_impl_FFMPEGMediaPlayer.c | 6 +-
7 files changed, 131 insertions(+), 56 deletions(-)
diff --git a/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java b/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java
index cacf8c5a4..2b8da8af9 100644
--- a/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java
+++ b/src/jogl/classes/com/jogamp/opengl/util/av/AudioSink.java
@@ -36,6 +36,18 @@ import jogamp.opengl.Debug;
public interface AudioSink {
public static final boolean DEBUG = Debug.debug("AudioSink");
+ /** Default frame duration in millisecond, i.e. 1 frame per {@value} ms. */
+ public static final int DefaultFrameDuration = 32;
+
+ /** Initial audio queue size in milliseconds. {@value} ms, i.e. 16 frames per 32 ms. See {@link #init(AudioDataFormat, float, int, int, int)}.*/
+ public static final int DefaultInitialQueueSize = 16 * 32; // 512 ms
+ /** Audio queue grow size in milliseconds. {@value} ms, i.e. 16 frames per 32 ms. See {@link #init(AudioDataFormat, float, int, int, int)}.*/
+ public static final int DefaultQueueGrowAmount = 16 * 32; // 512 ms
+ /** Audio queue limit w/ video in milliseconds. {@value} ms, i.e. 96 frames per 32 ms. See {@link #init(AudioDataFormat, float, int, int, int)}.*/
+ public static final int DefaultQueueLimitWithVideo = 96 * 32; // 3072 ms
+ /** Audio queue limit w/o video in milliseconds. {@value} ms, i.e. 32 frames per 32 ms. See {@link #init(AudioDataFormat, float, int, int, int)}.*/
+ public static final int DefaultQueueLimitAudioOnly = 32 * 32; // 1024 ms
+
/** Specifies the audio data type. Currently only PCM is supported. */
public static enum AudioDataType { PCM };
@@ -66,21 +78,65 @@ public interface AudioSink {
public final boolean littleEndian;
/**
- * Returns the duration in milliseconds of the given byte count according
- * to {@link #sampleSize}, {@link #channelCount} and {@link #sampleRate}.
+ * Returns the byte size of the given milliseconds
+ * according to {@link #sampleSize}, {@link #channelCount} and {@link #sampleRate}.
*/
- public final int getDuration(int byteCount) {
+ public final int getByteSize(int millisecs) {
final int bytesPerSample = sampleSize >>> 3; // /8
- return byteCount / ( channelCount * bytesPerSample * ( sampleRate / 1000 ) );
+ return millisecs * ( channelCount * bytesPerSample * ( sampleRate / 1000 ) );
}
/**
- * Returns the byte count of the given milliseconds according
- * to {@link #sampleSize}, {@link #channelCount} and {@link #sampleRate}.
+ * Returns the duration in milliseconds of the given byte count
+ * according to {@link #sampleSize}, {@link #channelCount} and {@link #sampleRate}.
*/
- public final int getByteCount(int millisecs) {
+ public final int getBytesDuration(int byteCount) {
final int bytesPerSample = sampleSize >>> 3; // /8
- return millisecs * ( channelCount * bytesPerSample * ( sampleRate / 1000 ) );
+ return byteCount / ( channelCount * bytesPerSample * ( sampleRate / 1000 ) );
+ }
+
+ /**
+ * Returns the duration in milliseconds of the given and sample count per frame and channel
+ * according to the {@link #sampleRate}, i.e.
+ *
+ * ( 1000f * sampleCount ) / sampleRate
+ *
+ * @param sampleCount sample count per frame and channel
+ */
+ public final float getSamplesDuration(int sampleCount) {
+ return ( 1000f * (float) sampleCount ) / (float)sampleRate;
+ }
+
+ /**
+ * Returns the rounded frame count of the given milliseconds and frame duration.
+ *
+ * Note: frameDuration can be derived by sample count per frame and channel
+ * via {@link #getSamplesDuration(int)}.
+ *
+ * @param millisecs time in milliseconds
+ * @param frameDuration duration per frame in milliseconds.
+ */
+ public final int getFrameCount(int millisecs, float frameDuration) {
+ return Math.max(1, (int) ( (float)millisecs / frameDuration + 0.5f ));
+ }
+
+ /**
+ * Returns the byte size of given sample count
+ * according to the {@link #sampleSize}, i.e.:
+ *
+ * sampleCount * ( sampleSize / 8 )
+ *
+ *
+ * Note: To retrieve the byte size for all channels, you need to pre-multiply sampleCount
+ * with {@link #channelCount}.
+ *
+ * @param sampleCount sample count
+ */
+ public final int getSamplesByteSize(int sampleCount) {
+ return sampleCount * ( sampleSize >>> 3 );
}
public String toString() {
@@ -175,12 +231,16 @@ public interface AudioSink {
* The {@link #DefaultFormat} should be supported by all implementations.
*
* @param requestedFormat the requested {@link AudioDataFormat}.
- * @param initialFrameCount initial number of frames to queue in this sink
- * @param frameGrowAmount number of frames to grow queue if full
- * @param frameLimit maximum number of frames
- * @return if successful the chosen AudioDataFormat based on the requestedFormat and this sinks capabilities, otherwise null.
+ * @param frameDuration average or fixed frame duration in milliseconds
+ * helping a caching {@link AudioFrame} based implementation to determine the frame count in the queue.
+ * See {@link #DefaultFrameDuration}.
+ * @param initialQueueSize initial time in milliseconds to queue in this sink, see {@link #DefaultInitialQueueSize}.
+ * @param queueGrowAmount time in milliseconds to grow queue if full, see {@link #DefaultQueueGrowAmount}.
+ * @param queueLimit maximum time in milliseconds the queue can hold (and grow), see {@link #DefaultQueueLimitWithVideo} and {@link #DefaultQueueLimitAudioOnly}.
+ * @return if successful the chosen AudioDataFormat based on the requestedFormat and this sinks capabilities, otherwise null.
*/
- public AudioDataFormat initSink(AudioDataFormat requestedFormat, int initialFrameCount, int frameGrowAmount, int frameLimit);
+ public AudioDataFormat init(AudioDataFormat requestedFormat, float frameDuration,
+ int initialQueueSize, int queueGrowAmount, int queueLimit);
/**
* Returns true, if {@link #play()} has been requested and the sink is still playing,
@@ -207,7 +267,7 @@ public interface AudioSink {
/**
* Flush all queued buffers, implies {@link #pause()}.
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
* @see #play()
* @see #pause()
@@ -220,17 +280,17 @@ public interface AudioSink {
/**
* Returns the number of allocated buffers as requested by
- * {@link #initSink(AudioDataFormat, int, int, int)}.
+ * {@link #init(AudioDataFormat, float, int, int, int)}.
*/
public int getFrameCount();
- /** @return the current enqueued frames count since {@link #initSink(AudioDataFormat, int, int, int)}. */
+ /** @return the current enqueued frames count since {@link #init(AudioDataFormat, float, int, int, int)}. */
public int getEnqueuedFrameCount();
/**
* Returns the current number of frames queued for playing.
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
*/
public int getQueuedFrameCount();
@@ -238,7 +298,7 @@ public interface AudioSink {
/**
* Returns the current number of bytes queued for playing.
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
*/
public int getQueuedByteCount();
@@ -246,7 +306,7 @@ public interface AudioSink {
/**
* Returns the current queued frame time in milliseconds for playing.
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
*/
public int getQueuedTime();
@@ -259,7 +319,7 @@ public interface AudioSink {
/**
* Returns the current number of frames in the sink available for writing.
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
*/
public int getFreeFrameCount();
@@ -270,7 +330,7 @@ public interface AudioSink {
* The data must comply with the chosen {@link AudioDataFormat} as returned by {@link #initSink(AudioDataFormat)}.
*
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
* @returns the enqueued internal {@link AudioFrame}, which may differ from the input audioDataFrame.
* @deprecated User shall use {@link #enqueueData(int, ByteBuffer, int)}, which allows implementation
@@ -284,7 +344,7 @@ public interface AudioSink {
* The data must comply with the chosen {@link AudioDataFormat} as returned by {@link #initSink(AudioDataFormat)}.
*
*
- * {@link #initSink(AudioDataFormat, int, int, int)} must be called first.
+ * {@link #init(AudioDataFormat, float, int, int, int)} must be called first.
*
* @returns the enqueued internal {@link AudioFrame}.
*/
diff --git a/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java b/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
index 5f98c3ad2..f9ca0c028 100644
--- a/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/openal/av/ALAudioSink.java
@@ -213,7 +213,8 @@ public class ALAudioSink implements AudioSink {
return "ALAudioSink[init "+initialized+", playRequested "+playRequested+", device "+deviceSpecifier+", ctx "+toHexString(ctxHash)+", alSource "+alSrcName+
", chosen "+chosenFormat+", alFormat "+toHexString(alFormat)+
", playSpeed "+playSpeed+", buffers[total "+alBuffersLen+", avail "+alFramesAvail.size()+", "+
- "queued["+alFramesPlaying.size()+", apts "+getPTS()+", "+getQueuedTime() + " ms, " + alBufferBytesQueued+" bytes]";
+ "queued["+alFramesPlaying.size()+", apts "+getPTS()+", "+getQueuedTime() + " ms, " + alBufferBytesQueued+" bytes], "+
+ "queue[g "+frameGrowAmount+", l "+frameLimit+"]";
}
public final String getPerfString() {
final int alBuffersLen = null != alBufferNames ? alBufferNames.length : 0;
@@ -226,7 +227,7 @@ public class ALAudioSink implements AudioSink {
}
@Override
- public final AudioDataFormat initSink(AudioDataFormat requestedFormat, int initialFrameCount, int frameGrowAmount, int frameLimit) {
+ public final AudioDataFormat init(AudioDataFormat requestedFormat, float frameDuration, int initialQueueSize, int queueGrowAmount, int queueLimit) {
if( !staticAvailable ) {
return null;
}
@@ -260,6 +261,10 @@ public class ALAudioSink implements AudioSink {
// Allocate buffers
destroyBuffers();
{
+ final float useFrameDuration = frameDuration > 1f ? frameDuration : AudioSink.DefaultFrameDuration;
+ final int initialFrameCount = requestedFormat.getFrameCount(
+ initialQueueSize > 0 ? initialQueueSize : AudioSink.DefaultInitialQueueSize, useFrameDuration);
+ // frameDuration, int initialQueueSize, int queueGrowAmount, int queueLimit) {
alBufferNames = new int[initialFrameCount];
al.alGenBuffers(initialFrameCount, alBufferNames, 0);
final int err = al.alGetError();
@@ -274,8 +279,10 @@ public class ALAudioSink implements AudioSink {
alFramesAvail = new LFRingbuffer(alFrames);
alFramesPlaying = new LFRingbuffer(ALAudioFrame[].class, initialFrameCount);
- this.frameGrowAmount = frameGrowAmount;
- this.frameLimit = frameLimit;
+ this.frameGrowAmount = requestedFormat.getFrameCount(
+ queueGrowAmount > 0 ? queueGrowAmount : AudioSink.DefaultQueueGrowAmount, useFrameDuration);
+ this.frameLimit = requestedFormat.getFrameCount(
+ queueLimit > 0 ? queueLimit : AudioSink.DefaultQueueLimitWithVideo, useFrameDuration);
}
} finally {
unlockContext();
@@ -423,9 +430,9 @@ public class ALAudioSink implements AudioSink {
if( wait && val[0] < releaseBufferLimes ) {
i++;
// clip wait at [2 .. 100] ms
- final int avgBufferDura = chosenFormat.getDuration( alBufferBytesQueued / alFramesPlaying.size() );
+ final int avgBufferDura = chosenFormat.getBytesDuration( alBufferBytesQueued / alFramesPlaying.size() );
final int sleep = Math.max(2, Math.min(100, releaseBufferLimes * avgBufferDura));
- if( DEBUG ) {
+ if( DEBUG || true ) {
System.err.println(getThreadName()+": ALAudioSink: Dequeue.wait["+i+"]: avgBufferDura "+avgBufferDura+", releaseBufferLimes "+releaseBufferLimes+", sleep "+sleep+" ms, playImpl "+isPlayingImpl1()+", processed "+val[0]+", "+this);
}
unlockContext();
@@ -512,7 +519,7 @@ public class ALAudioSink implements AudioSink {
throw new RuntimeException("ALError "+toHexString(alErr)+" while makeCurrent. "+this);
}
- final int duration = chosenFormat.getDuration(byteCount);
+ final int duration = chosenFormat.getBytesDuration(byteCount);
final boolean dequeueDone;
if( alFramesAvail.isEmpty() ) {
// try to dequeue first
@@ -738,7 +745,7 @@ public class ALAudioSink implements AudioSink {
if( !initialized || null == chosenFormat ) {
return 0;
}
- return chosenFormat.getDuration(alBufferBytesQueued);
+ return chosenFormat.getBytesDuration(alBufferBytesQueued);
}
@Override
diff --git a/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java b/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java
index dcf096f05..b46b64748 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/JavaSoundAudioSink.java
@@ -68,7 +68,7 @@ public class JavaSoundAudioSink implements AudioSink {
}
@Override
- public AudioDataFormat initSink(AudioDataFormat requestedFormat, int initialFrameCount, int frameGrowAmount, int frameLimit) {
+ public AudioDataFormat init(AudioDataFormat requestedFormat, float frameDuration, int initialQueueSize, int queueGrowAmount, int queueLimit) {
if( !staticAvailable ) {
return null;
}
@@ -178,7 +178,7 @@ public class JavaSoundAudioSink implements AudioSink {
@Override
public AudioFrame enqueueData(int pts, ByteBuffer bytes, int byteCount) {
- return enqueueData(new AudioDataFrame(pts, chosenFormat.getDuration(byteCount), bytes, byteCount));
+ return enqueueData(new AudioDataFrame(pts, chosenFormat.getBytesDuration(byteCount), bytes, byteCount));
}
@Override
diff --git a/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java b/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
index af4b83bb6..067322819 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/NullAudioSink.java
@@ -33,7 +33,7 @@ public class NullAudioSink implements AudioSink {
}
@Override
- public AudioDataFormat initSink(AudioDataFormat requestedFormat, int initialFrameCount, int frameGrowAmount, int frameLimit) {
+ public AudioDataFormat init(AudioDataFormat requestedFormat, float frameDuration, int initialQueueSize, int queueGrowAmount, int queueLimit) {
return requestedFormat;
}
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 a800f2a31..e59a8849f 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGMediaPlayer.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGMediaPlayer.java
@@ -167,14 +167,10 @@ public class FFMPEGMediaPlayer extends GLMediaPlayerImpl {
// Audio
//
- /** Initial audio frame count, ALAudioSink may grow buffer! */
- private int initialAudioFrameCount = AV_DEFAULT_AFRAMES;
- private final int audioFrameGrowAmount = 8;
- private final int audioFrameLimitWithVideo = 64; // 128;
- private final int audioFrameLimitAudioOnly = 32; // 64;
private SampleFormat aSampleFmt = null;
private AudioSink.AudioDataFormat avChosenAudioFormat;
private AudioSink.AudioDataFormat sinkChosenAudioFormat;
+ private int audioSamplesPerFrameAndChannel = 0;
public FFMPEGMediaPlayer() {
if(!available) {
@@ -237,7 +233,7 @@ public class FFMPEGMediaPlayer extends GLMediaPlayerImpl {
if(null == audioSink) {
throw new GLException("AudioSink null");
}
- final int audioFrameLimit;
+ final int audioQueueLimit;
if( null != gl ) {
final GLContextImpl ctx = (GLContextImpl)gl.getContext();
AccessController.doPrivileged(new PrivilegedAction