FileDocCategorySizeDatePackage
MediaThread.javaAPI DocJMF 2.1.1e7324Mon May 12 12:20:44 BST 2003com.sun.media.util

MediaThread

public class MediaThread extends Thread
Note: In netscape, if the user doesn't give Thread priority, you will not be able to reduce the priority also. All threads will run under same priority. /** A thread class where all JMF created threads should based on.

Fields Summary
private static ThreadGroup
threadGroup
static boolean
securityPrivilege
private static final boolean
debug
private static int
controlPriority
private static int
audioPriority
private static int
videoPriority
private static int
networkPriority
private static int
videoNetworkPriority
private static int
defaultMaxPriority
Constructors Summary
public MediaThread()

	this("JMF thread");
    
public MediaThread(String name)

	super(threadGroup, name);
    
public MediaThread(Runnable r)

	this(r, "JMF thread");
    
public MediaThread(Runnable r, String name)

	super(threadGroup, r,  name);
    
Methods Summary
private voidcheckPriority(java.lang.String name, int ask, boolean priv, int got)

	if (ask != got) {
	    System.out.println("MediaThread: " + name + " privilege? " + priv + "  ask pri: " + ask + " got pri:  " + got);
	}
    
public static intgetAudioPriority()

	return audioPriority;
    
public static intgetControlPriority()

	return controlPriority;
    
public static intgetNetworkPriority()

	return networkPriority;
    
private static java.lang.ThreadGroupgetRootThreadGroup()


     
	JMFSecurity jmfSecurity = null;
	Method m[] = new Method[1];
	Class cl[] = new Class[1];
	Object args[][] = new Object[1][0];

	try {
	    // If you dont get thread and threadgroup access,
	    // using default thread group (null), else
	    // use root thread group
	    jmfSecurity = JMFSecurityManager.getJMFSecurity();
	    if ( jmfSecurity != null ) {
		if (jmfSecurity.getName().startsWith("jmf-security")) {
		    boolean haveBoth = true;

		    defaultMaxPriority = Thread.currentThread().getPriority();
		    try {
			jmfSecurity.requestPermission(m, cl, args, JMFSecurity.THREAD);
			m[0].invoke(cl[0], args[0]);
		    } catch (Throwable t) {
			jmfSecurity.permissionFailureNotification(JMFSecurity.THREAD);
			haveBoth = false;
		    }
		    if (haveBoth) {
			defaultMaxPriority =
			    Thread.currentThread().getThreadGroup().getMaxPriority();
			
			// System.out.println(" $$$ Now setting defaultMaxPriority as the current threads MAX priority which is " + defaultMaxPriority);
		    }

		    try {
			jmfSecurity.requestPermission(m, cl, args, JMFSecurity.THREAD_GROUP);
			m[0].invoke(cl[0], args[0]);
		    } catch (Throwable t) {
			jmfSecurity.permissionFailureNotification(JMFSecurity.THREAD_GROUP);
			haveBoth = false;
		    }
		    if (!haveBoth) {
			throw new Exception("No thread and or threadgroup permission");
		    }
		} else if (jmfSecurity.getName().startsWith("internet")) {
		    PolicyEngine.assertPermission(PermissionID.THREAD);
		} else if (jmfSecurity.getName().startsWith("jdk12")) {
		    Constructor cons = jdk12Action.getCheckPermissionAction();

		    defaultMaxPriority = Thread.currentThread().getPriority();
		    jdk12.doPrivContextM.invoke(
					 jdk12.ac,
					 new Object[] {
			    cons.newInstance(
					 new Object[] {
			                     JDK12Security.getThreadPermission()
                                         }),
				jdk12.getContextM.invoke(null, null)
  		       });

		    defaultMaxPriority =
			Thread.currentThread().getThreadGroup().getMaxPriority();


		    jdk12.doPrivContextM.invoke(
					 jdk12.ac,
					 new Object[] {
			cons.newInstance(
					 new Object[] {
			                     JDK12Security.getThreadGroupPermission()
                                         }),
			    jdk12.getContextM.invoke(null, null)
		    });

		} else if (jmfSecurity.getName().startsWith("default")) {
		    // TODO: even if class loader is not null
		    // we should check to see if the classes are signed
		    if (MediaThread.class.getClassLoader() != null) {
			// jmf from server
			throw new SecurityException();
		    }
		}

	    }
	} catch (Throwable e) {
	    // System.err.println("Permission to manipulate threads and/or thread groups not granted " + e + " : " + e.getMessage());
            securityPrivilege=false;
	    // System.out.println("defaultMaxPriority is " + defaultMaxPriority);

	    // TODO: tweak these based on testing
	    controlPriority = defaultMaxPriority;
	    audioPriority = defaultMaxPriority;
	    videoPriority = defaultMaxPriority - 1;
	    networkPriority = defaultMaxPriority;
	    videoNetworkPriority = defaultMaxPriority;

	    // TODO: Do the right thing if permissions cannot be obtained.
	    // User should be notified via an event
	}

        if (securityPrivilege) {
	    threadGroup = getRootThreadGroup();
	    // System.out.println("threadGroup is " + threadGroup);
        }
        else {
	    threadGroup = null;
	    // System.out.println("threadGroup is null");
        }
	
    
	ThreadGroup current = null;
	try {
	    current = Thread.currentThread().getThreadGroup();
	    ThreadGroup g = current;
	    for (; g.getParent() != null; g = g.getParent());
	    // System.out.println("Root threadgroup is " + g);
	    return g;
	} catch (Exception e) {
	    return null; // current
	} catch (Error e) {
	    return null; // current;
	}
    
public static intgetVideoNetworkPriority()

	return videoNetworkPriority;
    
public static intgetVideoPriority()

	return videoPriority;
    
public voiduseAudioPriority()
This should be used for threads handling the audio medium.

	usePriority(audioPriority);
    
public voiduseControlPriority()
This should be used for Manager, events threads etc. -- the mechanism to maintain the players.

	usePriority(controlPriority);
    
public voiduseNetworkPriority()
This should be used for threads handling network packets. e.g. RTP

	usePriority(networkPriority);
    
private voidusePriority(int priority)

	try {
	    setPriority(priority);
	} catch (Throwable t) {
	}
	if (debug) {
	    checkPriority("priority",
			  priority,
			  securityPrivilege, getPriority());
	}
    
public voiduseVideoNetworkPriority()

	usePriority(videoNetworkPriority);
    
public voiduseVideoPriority()
This should be used for threads handling the video medium.

	usePriority(videoPriority);