package com.srbenoit.media.movie;

import java.awt.image.BufferedImage;
import java.util.List;
import javax.media.MediaLocator;
import javax.media.Time;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.PullBufferDataSource;
import javax.media.protocol.PullBufferStream;

/**
 * A <code>DataSource</code> to read from a list of JPEG image files and turn that into a stream of
 * JMF buffers. The <code>DataSource</code> is not seekable or positionable.
 */
public class BufferedImageDataSource extends PullBufferDataSource {

    /** the list of input streams */
    private final transient BufferedImageSourceStream[] streams;

    /**
     * Constructs a new <code>BufferedImageDataSource</code>.
     *
     * @param  width      the target frame width
     * @param  height     the target frame height
     * @param  frameRate  the target frame rate
     * @param  images     the list of image names
     */
    public BufferedImageDataSource(final int width, final int height, final int frameRate,
        final List<BufferedImage> images) {

        super();

        this.streams = new BufferedImageSourceStream[1];
        this.streams[0] = new BufferedImageSourceStream(width, height, frameRate, images);
    }

    /**
     * Sets the media locator (does nothing).
     *
     * @param  source  the source media locator
     */
    @Override public void setLocator(final MediaLocator source) {

        /* Empty */
    }

    /**
     * Gets the media locator (returns <code>null</code>).
     *
     * @return  the media locator
     */
    @Override public MediaLocator getLocator() {

        return null;
    }

    /**
     * Content type is of RAW since we are sending buffers of video frames without a container
     * format.
     *
     * @return  the content type (RAW)
     */
    @Override public String getContentType() {

        return ContentDescriptor.RAW;
    }

    /**
     * Dummy implementation of the superclass method.
     */
    @Override public void connect() {

        /* Empty */
    }

    /**
     * Dummy implementation of the superclass method.
     */
    @Override public void disconnect() {

        /* Empty */
    }

    /**
     * Dummy implementation of the superclass method.
     */
    @Override public void start() {

        /* Empty */
    }

    /**
     * Dummy implementation of the superclass method.
     */
    @Override public void stop() {

        /* Empty */
    }

    /**
     * Returns the <code>PullBufferStream</code>.
     *
     * @return  the streams
     */
    @Override public PullBufferStream[] getStreams() {

        return this.streams.clone();
    }

    /**
     * Returns the duration of the movie. We could have derived the duration from the number of
     * frames and frame rate. But for the purpose of this program, it's not necessary.
     *
     * @return  <code>DURATION_UNKNOWN</code>
     */
    @Override public Time getDuration() {

        return DURATION_UNKNOWN;
    }

    /**
     * Dummy implementation of the superclass method.
     *
     * @return  a zero-length object list
     */
    @Override public Object[] getControls() {

        return new Object[0];
    }

    /**
     * Dummy implementation of the superclass method.
     *
     * @param   type  the type of control to get
     * @return  <code>null</code>
     */
    @Override public Object getControl(final String type) {

        return null;
    }
}
