[MPlayer-users] Slave mode bugs (?) and Java Swing embedding

Bart van Deenen bart.vandeenen at gmail.com
Wed Oct 1 16:39:04 CEST 2008


On Wednesday 01 October 2008 15:00:33 Kevin DeKorte wrote:
> Alex Sherwin wrote:
> > I do this in a  Java now.  You can use Swing or SWT (SWT is actually
> > easier, they expose the window handle  on frames, in Swing you have to
> > use a hack that is not "supported" in the JDK, but exists and works
> > from 1.4.2 to the latest 1.6)
> >
> > Unfortunately, if you read the man page for the -wid option, you will
> > see that it only works on Windows.  I've spent awhile trying to find a
> > way to make a x-platform java Swing or SWT app that will embed MPlayer
> > on Windows Linux and OS X through Java Web Start.  The closest I've
> > been able to come is to deliver an app that embeds MPlayer on Windows,
> > and provides a slimmed down interface on Linux and OS X to control
> > mplayer, which will open its own video display window.
> >
> > I spent a bit of time on this mailing list and the IRC channel trying
> > to figure out a way, and had someone point me to the code where the
> > MPlayer GUI manages to embed the player, but this is able to do it
> > using shared memory segments to paint the display (which you would not
> > be able to accomplish with Java)
> >
> > Good luck on Linux, but I'm going to say its not possible...
>
> Incorrect, the -wid option works perfectly fine on X (Linux/BSD).
>
> Kevin

Here's some x-platform code that I use to get the wid of an awt canvas. Works 
fine on Linux and Windows.
The ExtendedCanvas.java is used to generate the ExtendedCanvas.h header file
The C file I've created  myself. 

From java:
make a new ExtendedCanvas object. Its basically just an awt canvas with one 
extra function, to get its native id.
add the canvas somehow to a top level awt frame. If you forget this, you get 
an assert error.
ask the ExtendedCanvas its native id, and pass that to mplayer.

If you don't know how to build jni's, have a look on the net. I actually 
generate my ExtendedCanvas.dll (for Windows) on Linux, with the mingw 
crosscompiler :-)
You have to add the compiled libExtendedCanvas.so or ExtendedCanvas.dll to 
your classpath, so that java can find them, with System.loadlibrary

Good luck

Bart


-----------------------------------------------------------------------------------------------------------------------------
ExtendedCanvas.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class avialogic_jmplayer_ExtendedCanvas */

#ifndef _Included_avialogic_jmplayer_ExtendedCanvas
#define _Included_avialogic_jmplayer_ExtendedCanvas
#ifdef __cplusplus
extern "C" {
#endif

#undef avialogic_jmplayer_ExtendedCanvas_FOCUS_TRAVERSABLE_UNKNOWN
#define avialogic_jmplayer_ExtendedCanvas_FOCUS_TRAVERSABLE_UNKNOWN 0L
#undef avialogic_jmplayer_ExtendedCanvas_FOCUS_TRAVERSABLE_DEFAULT
#define avialogic_jmplayer_ExtendedCanvas_FOCUS_TRAVERSABLE_DEFAULT 1L
#undef avialogic_jmplayer_ExtendedCanvas_FOCUS_TRAVERSABLE_SET
#define avialogic_jmplayer_ExtendedCanvas_FOCUS_TRAVERSABLE_SET 2L
#undef avialogic_jmplayer_ExtendedCanvas_TOP_ALIGNMENT
#define avialogic_jmplayer_ExtendedCanvas_TOP_ALIGNMENT 0.0f
#undef avialogic_jmplayer_ExtendedCanvas_CENTER_ALIGNMENT
#define avialogic_jmplayer_ExtendedCanvas_CENTER_ALIGNMENT 0.5f
#undef avialogic_jmplayer_ExtendedCanvas_BOTTOM_ALIGNMENT
#define avialogic_jmplayer_ExtendedCanvas_BOTTOM_ALIGNMENT 1.0f
#undef avialogic_jmplayer_ExtendedCanvas_LEFT_ALIGNMENT
#define avialogic_jmplayer_ExtendedCanvas_LEFT_ALIGNMENT 0.0f
#undef avialogic_jmplayer_ExtendedCanvas_RIGHT_ALIGNMENT
#define avialogic_jmplayer_ExtendedCanvas_RIGHT_ALIGNMENT 1.0f
#undef avialogic_jmplayer_ExtendedCanvas_serialVersionUID
#define 
avialogic_jmplayer_ExtendedCanvas_serialVersionUID -7644114512714619750LL
#undef avialogic_jmplayer_ExtendedCanvas_serialVersionUID
#define 
avialogic_jmplayer_ExtendedCanvas_serialVersionUID -2284879212465893870LL
/*
 * Class:     avialogic_jmplayer_ExtendedCanvas
 * Method:    getNativeWindow
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_avialogic_jmplayer_ExtendedCanvas_getNativeWindow
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

-----------------------------------------------------------------------------------------------------------------------------
ExtendedCanvas.c

#include <jni.h>
#include <jawt.h>
#include <jawt_md.h>
#include <assert.h>
#ifdef WIN32
#include <windows.h>
#endif

#include "ExtendedCanvas.h"


JNIEXPORT jint JNICALL Java_avialogic_jmplayer_ExtendedCanvas_getNativeWindow
  (JNIEnv *env, jobject obj_this)
{
jboolean result;
	jint lock;

	JAWT awt;
	JAWT_DrawingSurface* ds;
	JAWT_DrawingSurfaceInfo* dsi;
#ifdef WIN32
	JAWT_Win32DrawingSurfaceInfo* dsi_win;
	HWND hWnd;
#else
	JAWT_X11DrawingSurfaceInfo* dsi_x11;
#endif

	/* Get the AWT */
	awt.version = JAWT_VERSION_1_3;
	result = JAWT_GetAWT(env, &awt);
	if(result == JNI_FALSE) return -1;

								/* Get the drawing surface */
	if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
		return 0L;

	/* Lock the drawing surface */
	lock = ds->Lock(ds);
	if((lock & JAWT_LOCK_ERROR)  ) {
		return -1;
	}

	/* Get the drawing surface info */
	dsi = ds->GetDrawingSurfaceInfo(ds);

	/* Get the platform-specific drawing info */
#ifdef WIN32
	dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
	//hdc= dsi_win->hdc;
	hWnd = dsi_win->hwnd;
	return ((jlong)hWnd);
#else
	dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
	int id = dsi_x11->drawable;

	ds->Unlock(ds);
	return id;
#endif


}


-----------------------------------------------------------------------------------------------------------------------------
ExtendedCanvas.java

package avialogic.jmplayer;


/** awt.canvas with access method forwindow id.
 * 
 * @author bvandeenen
 *
 * uses JNI file for getting to window id 
 */
public class ExtendedCanvas extends java.awt.Canvas
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /** gets native window id.
     * 
     * @return native window id
     */
    public native int getNativeWindow();

    static
    {
        // if this fails make sure you read 
        try
        {
            Util.loadLibrary("ExtendedCanvas");
            Util.loadLibrary("jawt");
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            System.exit(1);
        }

    }

}



More information about the MPlayer-users mailing list