From e685f79ec7071e266a1bd3d3ce3e742397b5372e Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Sun, 16 Feb 2014 06:12:45 +0100
Subject: Bug 927: Fix minor MT issues w/ libav/ffmpeg

Issue:
  [NULL @ 0x35bde60] insufficient thread locking around avcodec_open/close()

Decorating said libav functions w/ mutex lock/release.

Abstract impl. to either use pthread or JNI Monitor,
but using the latter to reduce dependencies (ming64 windows).

FFMPEGNatives is now an abstract class containing the
  'static final Object mutex_avcodec_openclose'
---
 .../jogamp/opengl/util/av/impl/FFMPEGNatives.java  | 44 ++++++++++++----------
 .../opengl/util/av/impl/FFMPEGv08Natives.java      | 34 ++++++++---------
 .../opengl/util/av/impl/FFMPEGv09Natives.java      | 34 ++++++++---------
 .../opengl/util/av/impl/FFMPEGv10Natives.java      | 34 ++++++++---------
 4 files changed, 76 insertions(+), 70 deletions(-)

(limited to 'src/jogl/classes/jogamp/opengl/util/av')

diff --git a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGNatives.java b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGNatives.java
index bc0865aa9..c3fc2898f 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGNatives.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGNatives.java
@@ -29,17 +29,23 @@ package jogamp.opengl.util.av.impl;
 
 import com.jogamp.opengl.util.texture.TextureSequence.TextureFrame;
 
-interface FFMPEGNatives {
+/* pp */ abstract class FFMPEGNatives {
 
-    boolean initSymbols0(long[] symbols, int count);
-    int getAvUtilMajorVersionCC0();
-    int getAvFormatMajorVersionCC0();
-    int getAvCodecMajorVersionCC0();
-    int getAvResampleMajorVersionCC0();
-    int getSwResampleMajorVersionCC0();
+    private static final Object mutex_avcodec_openclose = new Object();
 
-    long createInstance0(FFMPEGMediaPlayer upstream, boolean verbose);
-    void destroyInstance0(long moviePtr);
+    abstract boolean initSymbols0(long[] symbols, int count);
+    abstract int getAvUtilMajorVersionCC0();
+    abstract int getAvFormatMajorVersionCC0();
+    abstract int getAvCodecMajorVersionCC0();
+    abstract int getAvResampleMajorVersionCC0();
+    abstract int getSwResampleMajorVersionCC0();
+
+    final long createInstance0(FFMPEGMediaPlayer upstream, boolean verbose) {
+        return createInstance0(mutex_avcodec_openclose, upstream, verbose);
+    }
+    abstract long createInstance0(Object mutex_avcodec_openclose, FFMPEGMediaPlayer upstream, boolean verbose);
+
+    abstract void destroyInstance0(long moviePtr);
 
     /**
      * Issues {@link #updateAttributes(int, int, int, int, int, int, int, float, int, int, String, String)}
@@ -56,24 +62,24 @@ interface FFMPEGNatives {
      * @param aPrefSampleRate
      * @param aPrefChannelCount
      */
-    void setStream0(long moviePtr, String url, boolean isCameraInput,
-                    int vid, String sizes, int vWidth, int vHeight,
-                    int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
+    abstract void setStream0(long moviePtr, String url, boolean isCameraInput,
+                             int vid, String sizes, int vWidth, int vHeight,
+                             int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
 
-    void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
+    abstract void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
 
-    int getVideoPTS0(long moviePtr);
+    abstract int getVideoPTS0(long moviePtr);
 
-    int getAudioPTS0(long moviePtr);
+    abstract int getAudioPTS0(long moviePtr);
 
     /**
      * @return resulting current video PTS, or {@link TextureFrame#INVALID_PTS}
      */
-    int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
+    abstract int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
 
-    int play0(long moviePtr);
-    int pause0(long moviePtr);
-    int seek0(long moviePtr, int position);
+    abstract int play0(long moviePtr);
+    abstract int pause0(long moviePtr);
+    abstract int seek0(long moviePtr, int position);
 
     /** FFMPEG/libAV Audio Sample Format */
     public static enum SampleFormat {
diff --git a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv08Natives.java b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv08Natives.java
index 4b013c1b3..6ca0ea311 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv08Natives.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv08Natives.java
@@ -27,52 +27,52 @@
  */
 package jogamp.opengl.util.av.impl;
 
-class FFMPEGv08Natives implements FFMPEGNatives {
+class FFMPEGv08Natives extends FFMPEGNatives {
     @Override
-    public native boolean initSymbols0(long[] symbols, int count);
+    native boolean initSymbols0(long[] symbols, int count);
 
     @Override
-    public native int getAvUtilMajorVersionCC0();
+    native int getAvUtilMajorVersionCC0();
 
     @Override
-    public native int getAvFormatMajorVersionCC0();
+    native int getAvFormatMajorVersionCC0();
 
     @Override
-    public native int getAvCodecMajorVersionCC0();
+    native int getAvCodecMajorVersionCC0();
 
     @Override
-    public native int getAvResampleMajorVersionCC0();
+    native int getAvResampleMajorVersionCC0();
 
     @Override
-    public native int getSwResampleMajorVersionCC0();
+    native int getSwResampleMajorVersionCC0();
 
     @Override
-    public native long createInstance0(FFMPEGMediaPlayer upstream, boolean verbose);
+    native long createInstance0(Object mutex_avcodec_openclose, FFMPEGMediaPlayer upstream, boolean verbose);
 
     @Override
-    public native void destroyInstance0(long moviePtr);
+    native void destroyInstance0(long moviePtr);
 
     @Override
-    public native void setStream0(long moviePtr, String url, boolean isCameraInput, int vid, String sizes, int vWidth, int vHeight, int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
+    native void setStream0(long moviePtr, String url, boolean isCameraInput, int vid, String sizes, int vWidth, int vHeight, int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
 
     @Override
-    public native void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
+    native void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
 
     @Override
-    public native int getVideoPTS0(long moviePtr);
+    native int getVideoPTS0(long moviePtr);
 
     @Override
-    public native int getAudioPTS0(long moviePtr);
+    native int getAudioPTS0(long moviePtr);
 
     @Override
-    public native int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
+    native int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
 
     @Override
-    public native int play0(long moviePtr);
+    native int play0(long moviePtr);
 
     @Override
-    public native int pause0(long moviePtr);
+    native int pause0(long moviePtr);
 
     @Override
-    public native int seek0(long moviePtr, int position);
+    native int seek0(long moviePtr, int position);
 }
diff --git a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv09Natives.java b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv09Natives.java
index d69763287..1d9f7f5fa 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv09Natives.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv09Natives.java
@@ -27,52 +27,52 @@
  */
 package jogamp.opengl.util.av.impl;
 
-class FFMPEGv09Natives implements FFMPEGNatives {
+class FFMPEGv09Natives extends FFMPEGNatives {
     @Override
-    public native boolean initSymbols0(long[] symbols, int count);
+    native boolean initSymbols0(long[] symbols, int count);
 
     @Override
-    public native int getAvUtilMajorVersionCC0();
+    native int getAvUtilMajorVersionCC0();
 
     @Override
-    public native int getAvFormatMajorVersionCC0();
+    native int getAvFormatMajorVersionCC0();
 
     @Override
-    public native int getAvCodecMajorVersionCC0();
+    native int getAvCodecMajorVersionCC0();
 
     @Override
-    public native int getAvResampleMajorVersionCC0();
+    native int getAvResampleMajorVersionCC0();
 
     @Override
-    public native int getSwResampleMajorVersionCC0();
+    native int getSwResampleMajorVersionCC0();
 
     @Override
-    public native long createInstance0(FFMPEGMediaPlayer upstream, boolean verbose);
+    native long createInstance0(Object mutex_avcodec_openclose, FFMPEGMediaPlayer upstream, boolean verbose);
 
     @Override
-    public native void destroyInstance0(long moviePtr);
+    native void destroyInstance0(long moviePtr);
 
     @Override
-    public native void setStream0(long moviePtr, String url, boolean isCameraInput, int vid, String sizes, int vWidth, int vHeight, int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
+    native void setStream0(long moviePtr, String url, boolean isCameraInput, int vid, String sizes, int vWidth, int vHeight, int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
 
     @Override
-    public native void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
+    native void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
 
     @Override
-    public native int getVideoPTS0(long moviePtr);
+    native int getVideoPTS0(long moviePtr);
 
     @Override
-    public native int getAudioPTS0(long moviePtr);
+    native int getAudioPTS0(long moviePtr);
 
     @Override
-    public native int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
+    native int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
 
     @Override
-    public native int play0(long moviePtr);
+    native int play0(long moviePtr);
 
     @Override
-    public native int pause0(long moviePtr);
+    native int pause0(long moviePtr);
 
     @Override
-    public native int seek0(long moviePtr, int position);
+    native int seek0(long moviePtr, int position);
 }
diff --git a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv10Natives.java b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv10Natives.java
index 0b5f70d7c..19bc10f5f 100644
--- a/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv10Natives.java
+++ b/src/jogl/classes/jogamp/opengl/util/av/impl/FFMPEGv10Natives.java
@@ -27,52 +27,52 @@
  */
 package jogamp.opengl.util.av.impl;
 
-class FFMPEGv10Natives implements FFMPEGNatives {
+class FFMPEGv10Natives extends FFMPEGNatives {
     @Override
-    public native boolean initSymbols0(long[] symbols, int count);
+    native boolean initSymbols0(long[] symbols, int count);
 
     @Override
-    public native int getAvUtilMajorVersionCC0();
+    native int getAvUtilMajorVersionCC0();
 
     @Override
-    public native int getAvFormatMajorVersionCC0();
+    native int getAvFormatMajorVersionCC0();
 
     @Override
-    public native int getAvCodecMajorVersionCC0();
+    native int getAvCodecMajorVersionCC0();
 
     @Override
-    public native int getAvResampleMajorVersionCC0();
+    native int getAvResampleMajorVersionCC0();
 
     @Override
-    public native int getSwResampleMajorVersionCC0();
+    native int getSwResampleMajorVersionCC0();
 
     @Override
-    public native long createInstance0(FFMPEGMediaPlayer upstream, boolean verbose);
+    native long createInstance0(Object mutex_avcodec_openclose, FFMPEGMediaPlayer upstream, boolean verbose);
 
     @Override
-    public native void destroyInstance0(long moviePtr);
+    native void destroyInstance0(long moviePtr);
 
     @Override
-    public native void setStream0(long moviePtr, String url, boolean isCameraInput, int vid, String sizes, int vWidth, int vHeight, int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
+    native void setStream0(long moviePtr, String url, boolean isCameraInput, int vid, String sizes, int vWidth, int vHeight, int vRate, int aid, int aMaxChannelCount, int aPrefSampleRate);
 
     @Override
-    public native void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
+    native void setGLFuncs0(long moviePtr, long procAddrGLTexSubImage2D, long procAddrGLGetError, long procAddrGLFlush, long procAddrGLFinish);
 
     @Override
-    public native int getVideoPTS0(long moviePtr);
+    native int getVideoPTS0(long moviePtr);
 
     @Override
-    public native int getAudioPTS0(long moviePtr);
+    native int getAudioPTS0(long moviePtr);
 
     @Override
-    public native int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
+    native int readNextPacket0(long moviePtr, int texTarget, int texFmt, int texType);
 
     @Override
-    public native int play0(long moviePtr);
+    native int play0(long moviePtr);
 
     @Override
-    public native int pause0(long moviePtr);
+    native int pause0(long moviePtr);
 
     @Override
-    public native int seek0(long moviePtr, int position);
+    native int seek0(long moviePtr, int position);
 }
-- 
cgit v1.2.3