FileDocCategorySizeDatePackage
ActiveMovie.javaAPI DocJMF 2.1.1e8944Mon May 12 12:21:20 BST 2003com.sun.media.amovie

ActiveMovie

public class ActiveMovie extends Object implements Runnable

Fields Summary
private int
pGraph
private int
aStream
private int
filterPin
private byte[]
jbuffer
private boolean
paused
private boolean
donePaused
private Thread
spinner
private Integer
semaphore
private boolean
realized
private int
streamType
private AMController
controller
private PullSourceStream
stream
private boolean
seekable
private boolean
randomAccess
private long
readLocation
private long
streamLocation
private boolean
controllerRealized
private int
cacheBuffer
private int
cacheTotalSize
private int
cacheAllocated
private boolean
deallocated
public static final int
MIN_VOLUME
public static final int
MAX_VOLUME
Constructors Summary
ActiveMovie(AMController controller, String file)

    
        
	this.controller = controller;
        realized = openFile(file);
    
ActiveMovie(AMController controller, PullSourceStream source, boolean randomAccess, long contentLength)


	this.controller = controller;
	this.stream  = source;
	this.seekable = source instanceof Seekable;
	this.randomAccess = randomAccess;

	// boolean seekable;
	this.jbuffer = new byte[65536];
	
	//seekable = (source instanceof Seekable) &&
	//         ((Seekable)source).isRandomAccess();

	initiateSpin();
	if (seekable)
	    seek(0);
	controller.canRead(64 * 1024);
	int size = controller.read(jbuffer, 0, 64 * 1024);

	if (!randomAccess && size > 0)
	    addToCache(jbuffer, 0, size);
	if (size > 0) {
	    //System.err.println("Content Length = " + contentLength);
	    if (!randomAccess && contentLength > 0)
		contentLength += 600 * 1024;
	    streamType = getStreamType(jbuffer, size);
	    streamLocation += size;
	    seek(0);

	    realized = openStream(seekable, randomAccess, streamType,
				  contentLength);

	} else
	    realized = false;
    
Methods Summary
private voidaddToCache(byte[] buffer, int offset, int size)
Cache Stuff

	if (cacheBuffer == 0) {
	    cacheBuffer = nCreateCache(384 * 1024);
	    cacheAllocated = 384 * 1024;
	}
	
	if ((cacheTotalSize + size) > cacheAllocated)
	    return;

	nAddToCache(cacheBuffer, cacheTotalSize, buffer, offset, size);
	cacheTotalSize += size;
    
native voidamPause()

native voidamRun()

native voidamStop()

native voidamStopWhenReady()

public intcanRead(int nBytes)

	return controller.canRead(nBytes);
    
public longcanSeek(long seekTo)

	return controller.canSeek(seekTo);
    
voiddispose()
End Cache Stuff

	if (spinner != null) {
	    spinner.stop();
	    spinner = null;
	}

	dispose0();				      // native call
	
	if (cacheBuffer != 0) {
	    nFreeCache(cacheBuffer);
	    cacheBuffer = 0;
	}
    
native voiddispose0()

native voiddoNRequest(byte[] array)

voiddoneRealize()

	controllerRealized = true;
    
protected voidfinalize()

        dispose();
    
static native intfindWindow(java.lang.String name)

native intgetBitRate()

native doublegetCurrentPosition()

native doublegetDuration()

native doublegetFrameRate()

private voidgetFromCache(int location, byte[] buffer, int offset, int size)

	nGetFromCache(cacheBuffer, location, buffer, offset, size);
    
native doublegetRate()

native intgetStreamType(byte[] array, int size)

native longgetTime()

native intgetVideoHeight()

native intgetVideoWidth()

native intgetVolume()

booleanhasAudio()

	return (streamType & 1) == 1;
    
booleanhasVideo()

	return (streamType & 2) == 2;
    
private voidinitiateSpin()

	spinner = new Thread( this );
	spinner.start();
    
booleanisRealized()

	return realized;
    
public voidkill()

	deallocated = true;
	unPause();
	stopDataFlow(true);
	amStop();
    
private native voidnAddToCache(int cacheBuffer, int cacheOffset, byte[] buffer, int bufOffset, int size)

private native intnCreateCache(int cacheSize)

private native voidnFreeCache(int cacheBuffer)

private native voidnGetFromCache(int cacheBuffer, int cacheOffset, byte[] buffer, int bufOffset, int size)

native booleanopenFile(java.lang.String file)

native booleanopenStream(boolean seekable, boolean randomAccess, int streamType, long contentLength)

public synchronized voidpause()

	// System.err.println("In ActiveMovie.pause()");
	if (paused) return;
	donePaused = false;
	paused = true;
	// Block for the donePaused to clear.  This is
	// done so that the last read could be completed before
	// it returns.
	if (!donePaused) {
	    try {
		wait(250);
		donePaused = true;
	    } catch (InterruptedException e) {}
	}
    
public intread(byte[] array, int offset, int length)

	int totalRead = 0;
	if (deallocated)
	    return -1;
	if (cacheTotalSize > 0 && !randomAccess) {
	    if (readLocation < cacheTotalSize && streamLocation == cacheTotalSize) {
		totalRead = (int) (cacheTotalSize - readLocation);
		if (totalRead > length)
		    totalRead = length;
		getFromCache((int) readLocation,
			     array, offset, totalRead);
		readLocation += totalRead;
		if (totalRead == length) {
		    return totalRead;
		} else {
		    length -= totalRead;
		    offset += totalRead;
		}
	    }
	}
	
	int actualRead = 0;
	int remaining = length;
	
	while (totalRead < length) {
	    if (canRead(remaining) > 0)
		actualRead = controller.read(array, offset, remaining);
	    else
		actualRead = -1;
	    if (actualRead == -1) {
		// EOS
		// cacheTotalSize = 0;
		if (totalRead > 0)
		    return totalRead;
		else
		    return -1;
	    } else if (actualRead == -2) {
		return -2;
	    } else if (actualRead > 0) {
		remaining -= actualRead;
		totalRead += actualRead;

		// Cache the data if the controller is not realized yet.
		if (!controllerRealized && !randomAccess) {
		    if (streamLocation == cacheTotalSize) {
			addToCache(array, offset, actualRead);
		    }
		}

		offset += actualRead;
		streamLocation += actualRead;
		readLocation = streamLocation;
		if (streamLocation > cacheTotalSize &&
		    controllerRealized)

		    cacheTotalSize = 0;
	    }
	}
	
	if (actualRead > 0)
	    return totalRead;
	else
	    return actualRead;
    
public voidrestart()

	// Restart the paused thread.
	// System.err.println("In ActiveMovie.restart()");
	deallocated = false;
	stopDataFlow(false);
	unPause();
    
public voidrun()

	// We need to block the thread if ActiveMovie is paused.
	while (true) {
	    synchronized (this) {
		while (paused) {
		    if (!donePaused) {
			donePaused = true;
			notifyAll();
		    }
		    try {
			wait();
		    } catch (InterruptedException e) {}
		}
	    }
	    
	    doNRequest(jbuffer);
	    
	    try {
		spinner.sleep(50);
	    } catch (Exception e) {
		System.err.println("Exception in run()" + e);
	    }
	}

    
public longseek(long seekTo)

	if (deallocated)
	    return 0;
	if (seekTo < cacheTotalSize && !randomAccess) {
	    readLocation = seekTo;
	    return seekTo;
	} else if (seekable && (randomAccess || (seekTo == 0))) {
	    long seeked = controller.seek(seekTo);
	    streamLocation = seekTo;
	    return seeked;
	} else {
	    // Couldn't seek
	    return -1;
	}
    
native voidsetCurrentPosition(double pos)

native voidsetNSeekable(boolean seekable)

native voidsetOwner(int owner)

native voidsetRate(double rate)

public voidsetSeekable(boolean seekable)

	if (seekable) {
	    this.randomAccess = seekable;
	    this.seekable = seekable;
	}
	setNSeekable(seekable);
    
native voidsetStopTime(double time)

native voidsetVisible(int visible)

native voidsetVolume(int volume)

native voidsetWindowPosition(int left, int top, int right, int bottom)

voidstopDataFlow(boolean stop)

	if (filterPin != 0)
	    stopDataFlow(filterPin, stop);
    
native voidstopDataFlow(int filterPin, boolean stop)

private voidunPause()

	if (!paused) return;
	synchronized (this) {
	    donePaused = true;
	    paused = false;
	    notifyAll();
	}
    
native booleanwaitForCompletion()