FileDocCategorySizeDatePackage
DirectAudioRenderer.javaAPI DocJMF 2.1.1e16213Mon May 12 12:21:20 BST 2003com.sun.media.renderer.audio

DirectAudioRenderer

public class DirectAudioRenderer extends AudioRenderer implements ExclusiveUse, Runnable
DirectAudioRenderer to render audio to a device directly. This is used in conjunction DirectAudioOutput.
version

Fields Summary
static String
NAME
public static final int
REQ_OPEN
public static final int
REQ_START
public static final int
REQ_STOP
public static final int
REQ_FLUSH
public static final int
REQ_DRAIN
public static final int
REQ_CLOSE
public static final int
REQ_SETGAIN
public static final int
REQ_SETMUTE
public static final int
REQ_SETRATE
public static final int
REQ_WRITE
public static final int
REQ_AVAILABLE
public static final int
REQ_TIME
private static JMFSecurity
jmfSecurity
private static boolean
securityPrivelege
private Method[]
m
private Class[]
cl
private Object[]
args
private Format
ulawFormat
private Format
linearFormat
private Codec
ulawDecoder
private Format
ulawOutputFormat
Buffer
decodeBuffer
static boolean
loaded
private int
nativeData
Constructors Summary
public DirectAudioRenderer()

    
     
	try {
	    jmfSecurity = JMFSecurityManager.getJMFSecurity();
	    securityPrivelege = true;
	} catch (SecurityException e) {
	}
    
        super();

	//
	// This line advertizes all the formats that are supported
	// by this renderer.
	// Here we assume the renderer can support all linear/PCM formats.
	//
	supportedFormats = new Format[2];
	ulawFormat = new AudioFormat(AudioFormat.ULAW);
 	linearFormat = new AudioFormat(AudioFormat.LINEAR);

        supportedFormats[0] = new AudioFormat(
                AudioFormat.LINEAR,
		AudioFormat.NOT_SPECIFIED,	// Sample rate
		AudioFormat.NOT_SPECIFIED,	// Sample size
		AudioFormat.NOT_SPECIFIED,	// # of channels
		AudioFormat.NOT_SPECIFIED,
		AudioFormat.NOT_SPECIFIED
            );
	supportedFormats[1] = ulawFormat;

	gainControl = new MCA(this);
    
Methods Summary
java.lang.ThreadchpThread()

	Thread thread = null;
	
	if ( /*securityPrivelege && */ (jmfSecurity != null) ) {
	    String permission = null;
	    try {
		if (jmfSecurity.getName().startsWith("jmf-security")) {
		    permission = "thread";
		    jmfSecurity.requestPermission(m, cl, args, JMFSecurity.THREAD);
		    m[0].invoke(cl[0], args[0]);
		    
		    permission = "thread group";
		    jmfSecurity.requestPermission(m, cl, args, JMFSecurity.THREAD_GROUP);
		    m[0].invoke(cl[0], args[0]);
		} else if (jmfSecurity.getName().startsWith("internet")) {
		    PolicyEngine.checkPermission(PermissionID.THREAD);
		    PolicyEngine.assertPermission(PermissionID.THREAD);
		}
	    } catch (Throwable e) {
		if (JMFSecurityManager.DEBUG) {
		    System.err.println("Unable to get " + permission +
				       " privilege  " + e);
		}
		securityPrivelege = false;
		// TODO: Do the right thing if permissions cannot be obtained.
		// User should be notified via an event
	    }
	}

 	if ( (jmfSecurity != null) && (jmfSecurity.getName().startsWith("jdk12"))) {
	    try {
		Constructor cons = jdk12CreateThreadRunnableAction.cons;
		
		thread = (Thread) jdk12.doPrivM.invoke(
					   jdk12.ac,
					   new Object[] {
					       cons.newInstance(
							new Object[] {
							    Thread.class,
							    this
							})});
		
		thread.setName("DirectSound Request Thread");
		
		cons = jdk12PriorityAction.cons;
		jdk12.doPrivM.invoke(
				     jdk12.ac,
				     new Object[] {
 					  cons.newInstance(
 					   new Object[] {
                                               thread,
                                               new Integer(Thread.MAX_PRIORITY)
                                           })});
	    } catch (Exception e) {
		e.printStackTrace();
	    }
 	} else {
	    thread = new Thread(this);
	    thread.setName("DirectSound Request Thread");
	    thread.setPriority(Thread.MAX_PRIORITY);
 	}
	return thread;
    
public voidclose()
Close the renderer and the device.

        super.close();
    
protected com.sun.media.renderer.audio.device.AudioOutputcreateDevice(javax.media.format.AudioFormat format)
Create the device - DirectAudioOutput.

	return (new DirectAudioOutput());
    
public java.lang.StringgetName()

	return NAME;
    
private static synchronized booleangrabDevice()
Grab the audio device without opening it.

	// If the device can be claimed without being opened,
	// then fill in this method.
	return true;
    
protected booleaninitDevice(javax.media.format.AudioFormat in)

	Format newInput = in;

	// Free the old ulaw decoder if there's one.
	if (ulawDecoder != null) {
	    ulawDecoder.close();
	    ulawDecoder = null;
	}

	// Initialize a ulaw decoder if the input format is ulaw.
	Format outs[] = new Format[1];
	if (ulawFormat.matches(in)) {

	    ulawDecoder = SimpleGraphBuilder.findCodec(in, linearFormat, null, outs);
	    if (ulawDecoder != null) {
		ulawOutputFormat = newInput = outs[0];
	    } else
		return false;
	}

	devFormat = in;

	return super.initDevice((AudioFormat)newInput);
    
public booleanisExclusive()

	return false;
    
private native intnBufferAvailable(int peer)

private native voidnCheckUnderflow(int peer)

private native voidnClose(int peer)

private native voidnDrain(int peer)

private native voidnFlush(int peer)

private native longnGetSamplesPlayed(int peer)

private native intnOpen(int rate, int sizeInBits, int channels, int bufSize)
native method declaraions

private native voidnPause(int peer)

private native voidnResume(int peer)

private native booleannSetFrequency(int peer, int frequency)

private native voidnSetGain(int peer, float g)

private native voidnSetMute(int peer, boolean m)

private native intnWrite(int peer, byte[] data, int off, int len, boolean swapBytes, boolean signChange)

public voidopen()

	if (device == null && inputFormat != null) {
	    if (!initDevice(inputFormat))
		throw new ResourceUnavailableException("Cannot intialize audio device for playback");
	}
    
public intprocessData(javax.media.Buffer buffer)


        
	if (!checkInput(buffer))
	    return BUFFER_PROCESSED_FAILED;

	// Process linear data
	if (ulawDecoder == null) {
	    return super.doProcessData(buffer);
	}

	// Pre-processing ulaw data, then feed it into JavaSound.
	if (decodeBuffer == null) {
	    decodeBuffer = new Buffer();
	    decodeBuffer.setFormat(ulawOutputFormat);
	}

	decodeBuffer.setLength(0);
	decodeBuffer.setOffset(0);
	decodeBuffer.setFlags(buffer.getFlags());
	decodeBuffer.setTimeStamp(buffer.getTimeStamp());
	decodeBuffer.setSequenceNumber(buffer.getSequenceNumber());

	int rc = ulawDecoder.process(buffer, decodeBuffer);

	if (rc == BUFFER_PROCESSED_OK) {
	    return super.doProcessData(decodeBuffer);
	}

	return BUFFER_PROCESSED_FAILED;
    
public voidrun()

	((Runnable) device).run();