// Decompiled by DJ v3.12.12.96 Copyright 2011 Atanas Neshkov  Date: 08/10/2012 8:47:54
// Home Page: http://members.fortunecity.com/neshkov/dj.html http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   LiveStream.java

package project;

import java.awt.Dimension;
import java.io.IOException;
import javax.media.*;
import javax.media.format.AudioFormat;
import javax.media.format.RGBFormat;
import javax.media.protocol.*;

public class LiveStream
    implements PushBufferStream, Runnable
{

    public LiveStream()
    {
        cd = new ContentDescriptor("raw");
        frameRate = 20F;
        controls = new Control[0];
        videoData = true;
        seqNo = 0;
        freq = 2D;
        if(videoData)
        {
            size = new Dimension(320, 240);
            maxDataLength = size.width * size.height * 3;
            rgbFormat = new RGBFormat(size, maxDataLength, Format.byteArray, frameRate, 24, 3, 2, 1, 3, size.width * 3, 0, -1);
            data = new byte[maxDataLength];
            int pos = 0;
            int revpos = (size.height - 1) * size.width * 3;
            for(int y = 0; y < size.height / 2; y++)
            {
                for(int x = 0; x < size.width; x++)
                {
                    byte value = (byte)(y * 2 & 0xff);
                    data[pos++] = value;
                    data[pos++] = 0;
                    data[pos++] = 0;
                    data[revpos++] = value;
                    data[revpos++] = 0;
                    data[revpos++] = 0;
                }

                revpos -= size.width * 6;
            }

        } else
        {
            audioFormat = new AudioFormat("LINEAR", 8000D, 8, 1, -1, 1, 8, -1D, Format.byteArray);
            maxDataLength = 1000;
        }
        thread = new Thread(this);
    }

    public ContentDescriptor getContentDescriptor()
    {
        return cd;
    }

    public long getContentLength()
    {
        return -1L;
    }

    public boolean endOfStream()
    {
        return false;
    }

    public Format getFormat()
    {
        if(videoData)
            return rgbFormat;
        else
            return audioFormat;
    }

    public void read(Buffer buffer)
        throws IOException
    {
        synchronized(this)
        {
            Object outdata = buffer.getData();
            if(outdata == null || outdata.getClass() != Format.byteArray || ((byte[])(byte[])outdata).length < maxDataLength)
            {
                outdata = new byte[maxDataLength];
                buffer.setData(outdata);
            }
            if(videoData)
            {
                buffer.setFormat(rgbFormat);
                buffer.setTimeStamp((long)((float)seqNo * (1000F / frameRate) * 1000000F));
                int lineNo = (seqNo * 2) % size.height;
                int chunkStart = lineNo * size.width * 3;
                System.arraycopy(data, chunkStart, outdata, 0, maxDataLength - chunkStart);
                if(chunkStart != 0)
                    System.arraycopy(data, 0, outdata, maxDataLength - chunkStart, chunkStart);
            } else
            {
                buffer.setFormat(audioFormat);
                buffer.setTimeStamp(0x7735940L);
                for(int i = 0; i < 1000; i++)
                {
                    ((byte[])(byte[])outdata)[i] = (byte)(int)(Math.sin((double)i / freq) * 32D);
                    freq = freq + 0.01D;
                    if(freq > 10D)
                        freq = 2D;
                }

            }
            buffer.setSequenceNumber(seqNo);
            buffer.setLength(maxDataLength);
            buffer.setFlags(0);
            buffer.setHeader(null);
            seqNo++;
        }
    }

    public void setTransferHandler(BufferTransferHandler transferHandler)
    {
        synchronized(this)
        {
            this.transferHandler = transferHandler;
            notifyAll();
        }
    }

    void start(boolean started)
    {
        synchronized(this)
        {
            this.started = started;
            if(started && !thread.isAlive())
            {
                thread = new Thread(this);
                thread.start();
            }
            notifyAll();
        }
    }

    public void run()
    {
        do
        {
            if(!started)
                break;
            synchronized(this)
            {
                while(transferHandler == null && started) 
                    try
                    {
                        wait(1000L);
                    }
                    catch(InterruptedException ie) { }
            }
            if(started && transferHandler != null)
            {
                transferHandler.transferData(this);
                try
                {
                    Thread.currentThread();
                    Thread.sleep(10L);
                }
                catch(InterruptedException ise) { }
            }
        } while(true);
    }

    public Object[] getControls()
    {
        return controls;
    }

    public Object getControl(String controlType)
    {
        Class cls;
        Object cs[];
        int i;
        cls = Class.forName(controlType);
        cs = getControls();
        i = 0;
_L1:
        if(i >= cs.length)
            break MISSING_BLOCK_LABEL_42;
        if(cls.isInstance(cs[i]))
            return cs[i];
        i++;
          goto _L1
        return null;
        Exception e;
        e;
        return null;
    }

    protected ContentDescriptor cd;
    protected int maxDataLength;
    protected byte data[];
    protected Dimension size;
    protected RGBFormat rgbFormat;
    protected AudioFormat audioFormat;
    protected boolean started;
    protected Thread thread;
    protected float frameRate;
    protected BufferTransferHandler transferHandler;
    protected Control controls[];
    protected boolean videoData;
    int seqNo;
    double freq;
}