FileDocCategorySizeDatePackage
BasicClock.javaAPI DocJMF 2.1.1e7478Mon May 12 12:20:48 BST 2003com.sun.media

BasicClock

public class BasicClock extends Object implements Clock
BasicClock implements javax.media.Clock. This is not a running thread that implements the clock ticks. It just implements the math and maintains the correct states to perform the computations from media-time to time-base-time.
version
1.7, 02/08/21

Fields Summary
private TimeBase
master
private long
startTime
private long
stopTime
private long
mediaTime
private long
mediaStart
private long
mediaLength
private float
rate
public static final int
STOPPED
public static final int
STARTED
Constructors Summary
public BasicClock()


      
	master = new SystemTimeBase();
    
Methods Summary
public longgetMediaNanoseconds()
Get the current media time in nanoseconds.

return
the current media time in nanoseconds.

	if (getState() == STOPPED) {
	    return mediaTime;
	}

	long now = master.getNanoseconds();
	if (now > startTime) {
	    // The media has been playing for a while.
	    long t = (long)((double)(now - startTime) * rate) + mediaTime;
	    if (mediaLength != -1 && t > mediaStart + mediaLength)
	        return mediaStart + mediaLength;
	    else
		return t;
	} else {
	    // We haven't reached the scheduled start time yet.
	    return mediaTime;
	}
    
public javax.media.TimegetMediaTime()
Return the current media time.

return
the current media time.

	return new Time(getMediaNanoseconds());
    
public floatgetRate()
Get the current presentation speed.

return
the current presentation speed.

	return rate;
    
public intgetState()
Return the current state of the clock in either started or stopped state.

return
the current clock state.


	// A start time has not been set.
	if (startTime == Long.MAX_VALUE)
	    return STOPPED;

	// A stop time has not been set.
	if (stopTime == Long.MAX_VALUE)
	    return STARTED;

	// The tricky case is when the clocked has started and
	// the media has reached the scheduled stop time.  In that
	// case, the clock is considered stopped.
// COMMENTED OUT BY BABU
// 	long now = master.getNanoseconds();
//         if (now > startTime) {
// 	    // The media has already been playing for some time.
		
// 	    long curMediaTime = (long)((now - startTime) * rate) + mediaTime; 
// 	    if ((rate > 0 && curMediaTime >= stopTime) ||
// 		(rate < 0 && curMediaTime <= stopTime)) {
// 	        // We have gone past the scheduled stop time.
// 		// We are in the stop state.
// 		System.out.println(this + ": BasicClock getState() SIDEEFFECT");
// 		mediaTime = validateTime(stopTime);
// 		startTime = Long.MAX_VALUE;
// 	        return STOPPED;
// 	    }
// 	}

	// In all other cases, the clock has already been started.
	return STARTED;
    
public javax.media.TimegetStopTime()
Return the preset stop time.

return
the preset stop time.

	return new Time(stopTime);
    
public javax.media.TimegetSyncTime()
Return the Sync Time. Not yet implementated.

	return new Time(0);
    
public javax.media.TimeBasegetTimeBase()
Get the Time Base that the clock is currently using.

return
the Time Base that the clock is currently using.

	return master;
    
public javax.media.TimemapToTimeBase(javax.media.Time t)
Map the the given media-time to time-base-time.

param
t media time
return
time base time.
exception
ClockStoppedException is thrown if this method is invoked on a stopped clock.

	if (getState() == STOPPED) {
	    ClockStoppedException e = new ClockStoppedException();
	    Log.dumpStack(e);
	    throw e;
	}
	return new Time((long)((t.getNanoseconds() - mediaTime)/rate) + startTime);
    
protected voidsetMediaLength(long t)
Set the upper bound of the media time.

param
t the upper bound of the media time.

	mediaLength = t;
    
protected voidsetMediaStart(long t)
Set the lower bound of the media time.

param
t the lower bound of the media time.

	mediaStart = t;
    
public voidsetMediaTime(javax.media.Time now)
Set the media time. This will be the media presented at the clock's start time.

param
the media time to set to.

	if (getState() == STARTED) {
	    throwError(new ClockStartedError("setMediaTime() cannot be used on a started clock."));
	}
	long t = now.getNanoseconds();
	if (t < mediaStart)
	    mediaTime = mediaStart;
	else if (mediaLength != -1 && t > mediaStart + mediaLength)
	    mediaTime = mediaStart + mediaLength;
	else
	    mediaTime = t;
    
public floatsetRate(float factor)
Set the rate of presentation: 1.0: normal, 2.0: twice the speed. -2.0: twice the speed in reverse.

param
the speed factor.
return
the actual rate the clock is set to.

	if (getState() == STARTED) {
	    throwError(new ClockStartedError("setRate() cannot be used on a started clock."));
	}
	rate = factor;
	return rate;
    
public voidsetStopTime(javax.media.Time t)
Preset a stop time in media time.

param
t the preset stop time.

	if (getState() == STARTED && stopTime != Long.MAX_VALUE) {
	    throwError(new StopTimeSetError("setStopTime() may be set only once on a Started Clock"));
	}
	stopTime = t.getNanoseconds();
    
public voidsetTimeBase(javax.media.TimeBase master)
Set a time base on the clock. All media-time to time-base-time will be computed with the given time base.

param
master the new master time base.
exception
IncompatibleTimeBaseException thrown if clock cannot accept the given time base.

	if (getState() == STARTED) {
	    throwError(new ClockStartedError("setTimeBase cannot be used on a started clock."));
	}
	if (master == null) {
	    if (!(this.master instanceof SystemTimeBase))
		this.master = new SystemTimeBase();
	} else
	    this.master = master;
    
public voidstop()
Stop the clock immediately.

	if (getState() == STOPPED) {
	    // It's already stopped.  No-op.
	    return;
	}

	// It's a change of state, so we'll mark the mediaTime.
	mediaTime = getMediaNanoseconds();

	// Reset the start time.
	startTime = Long.MAX_VALUE;
    
public voidsyncStart(javax.media.Time tbt)
Start the clock with the given time-base-time

param
the time base time to start the clock.

	if (getState() == STARTED) {
	    throwError(new ClockStartedError("syncStart() cannot be used on an already started clock."));
	}
	// If the given start time is already later than now.  We'll reset
	// the clock start time to now.
	if (master.getNanoseconds() > tbt.getNanoseconds())
	    startTime = master.getNanoseconds();
	else
	    startTime = tbt.getNanoseconds();
    
protected voidthrowError(java.lang.Error e)

	Log.dumpStack(e);
	throw e;