/*
 * FSVideoObject.java
 * Transform
 *
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.flagstone.transform;

/**
 * The FSVideoObject is the root class for objects that represent each of the
 * tagged data structures that make up the Flash Video file format
 * specification.
 * 
 * Each of the major data structures defined in the Flash file format
 * specification start with a header that contains the type identifying the data
 * structure followed by the number of bytes that the body of the encoded data
 * structure occupies in the file. The FSVideoObject manages the encoding and
 * decoding of this information.
 * 
 * This class is primarily used internally in the library however the getType()
 * method is useful when manipulating decoded video files. Rather than using the
 * runtime type checking provided in the Java environment the method can be used
 * to identify the class, derived from FSVideoObject, that represents each data
 * structure, improving performance.
 */

public abstract class FSVideoObject extends Object implements Cloneable
{
	/** Type identifying objects containing sound */
	public static final int AudioData = 8;

	/** Type identifying objects containing video */
	public static final int VideoData = 9;

	/** Type identifying objects containing meta-data */
	public static final int MetaData = 18;

	protected int type = 0;

	protected int length = 0;

	protected int timestamp = 0;

	/**
	 * Constructs a movie object with the specified type. The timestamp
	 * identifies the time, in milliseconds, relative to the start of the file
	 * that the audio or video will be played.
	 * 
	 * The timestamp is used exclusively for determining when sound or video
	 * will be played. Any internal timing information in the audio or video
	 * data is ignored.
	 * 
	 * @param type
	 *            an identifier indicating the type of movie object.
	 * @param timestamp
	 *            the time in milliseconds at which the data should be played.
	 */
	public FSVideoObject(int type, int timestamp)
	{
		this.type = type;
		this.timestamp = timestamp;
	}

	/**
	 * Gets the code used that identifies the type of the object when it is
	 * encoded.
	 * 
	 * @return the code used to denote the type of the object.
	 */
	public int getType()
	{
		return type;
	}

	int getLength()
	{
		return length;
	}

	/**
	 * Gets the timestamp, in milliseconds, relative to the start of the file,
	 * when the audio or video will be played.
	 * 
	 * @return the time when the data will be played.
	 */
	public int getTimestamp()
	{
		return timestamp;
	}

	/**
	 * Sets the timestamp, in milliseconds, relative to the start of the file,
	 * when the audio or video will be played.
	 * 
	 * @param time
	 *            the time in milliseconds relative to the start of the file.
	 */
	public void setTimestamp(int time)
	{
		timestamp = time;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (getClass().isInstance(anObject))
		{
			FSVideoObject typedObject = (FSVideoObject) anObject;

			result = type == typedObject.type;
			result = result && timestamp == typedObject.timestamp;
		}
		return result;
	}

	/**
	 * Return the name of the class without the package prefix. This method is
	 * used when logging events to identify the object being encoded or decoded.
	 * 
	 * @return the name of the class, e.g. FSDefineShape.
	 */
	public String name()
	{
		String className = getClass().getName();
		int index = className.lastIndexOf(".") + 1;

		return className.substring(index, className.length());
	}

	/**
	 * Creates a deep copy of the entire object.
	 * 
	 * @return a copy of the object.
	 */
	public Object clone()
	{
		Object anObject = null;

		try
		{
			anObject = super.clone();
		} catch (CloneNotSupportedException e)
		{
			throw new InternalError();
		}
		return anObject;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());
	}

	int length(FSCoder coder)
	{
		coder.context[FSCoder.Type] = type;

		length = 11;

		return length;
	}

	void encode(FSCoder coder)
	{
		coder.context[FSCoder.Type] = type;

		coder.writeWord(type, 1);
		coder.writeWord(length-11, 3);
		coder.writeWord(timestamp, 3);
		coder.writeWord(0, 4);
	}

	void decode(FSCoder coder)
	{
		type = coder.readWord(1, false);
		length = coder.readWord(3, false);
		timestamp = coder.readWord(3, false);
		coder.readWord(4, false); // reserved
	}
}
