Skip to content

Instantly share code, notes, and snippets.

@julesfern
Created July 22, 2010 14:20
Show Gist options
  • Save julesfern/486021 to your computer and use it in GitHub Desktop.
Save julesfern/486021 to your computer and use it in GitHub Desktop.
public function syncHandlersToViewportOffset():void
{
// Cancel any running sync operations
if(this._waitingForSync)
{
Logger.debug("Asked to start a sync operation, but one is already in progress. Cancelling existing sync operation.", this);
this.cancelOffsetSync();
}
Logger.debug("Beginning sync operation...", this);
this._offsetSyncHandlerList = new Vector.<SMILKitHandler>();
this._offsetSyncOffsetList = new Vector.<uint>();
// Loop over all handlers
for (var i:int = 0; i < this.elements.length; i++)
{
var node:TimingNode = this.elements[i];
var handler:SMILKitHandler = (node.mediaElement.handler as SMILKitHandler);
if(handler.seekable)
{
// Calculate the target offset for this handler
// TODO include clip-begin into the equation
var offset:uint = (this._objectPool.viewport.offset - node.begin);
// Push the handler onto the sync wait list
this._offsetSyncHandlerList.push(handler);
this._offsetSyncOffsetList.push(offset);
this._offsetSyncTimingNodeList.push(node);
// Calculate the destination seek offset
if(handler.completedResolving)
{
this.execSyncHandlerForViewportOffset(handler);
}
else
{
Logger.debug("Sync cycle encountered an unloaded or unresolved handler. Deferring sync on this handler until it has resolved itself.", this);
}
}
}
if (this._offsetSyncHandlerList.length > 0)
{
Logger.debug("Waiting for sync on "+this._offsetSyncHandlerList.length+" handlers.", this);
this._waitingForSync = true;
this.dispatchEvent(new RenderTreeEvent(RenderTreeEvent.WAITING_FOR_SYNC, null));
}
else
{
Logger.debug("No handlers require sync at this time.", this);
}
}
protected function execSyncHandlerForViewportOffset(handler:SMILKitHandler):void
{
var index:int = this._offsetSyncHandlerList.indexOf(handler);
if(index >= 0)
{
var node:TimingNode = this._offsetSyncTimingNodeList[index];
var offset:uint = this._offsetSyncOffsetList[index];
if(handler.seekable)
{
// Perform sync
var nearestSyncPoint:Number = handler.findNearestSyncPoint(offset);
var destinationOffset:Number;
if(nearestSyncPoint <= offset){
destinationOffset = nearestSyncPoint;
Logger.debug("Syncing a handler using known syncpoints. Seeking handler to "+destinationOffset+"ms with a target offset of "+offset+"ms. Node has begin time "+node.begin+"ms.", this);
}
else
{
destinationOffset = offset ;
Logger.debug("Syncing a handler using random access (since nearest syncpoint was "+nearestSyncPoint+"ms). Seeking handler to "+offset+"ms.", this);
};
handler.seek(destinationOffset); // The SEEK_NOTIFY operation dispatched by this call will continue the sync for this handler.
}
else
{
// Remove from sync wait list
Logger.debug("About to begin a deferred sync on a handler, but the handler is no longer seekable. About to remove from wait list.");
this.removeHandlerFromWaitingForSyncList(handler);
}
}
else
{
Logger.debug("Asked to begin a deferred sync for a handler, but the handler could not be found on the sync wait list.", this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment