From 7039a05147b8bbfc829babea1c65bd436450b505 Mon Sep 17 00:00:00 2001 From: Björn Stenberg Date: Mon, 8 Jan 2007 23:53:00 +0000 Subject: Splitting out songdbj git-svn-id: svn://svn.rockbox.org/rockbox/trunk@11953 a1c6a512-1295-4272-9138-f99709370657 --- songdbj/de/jarnbjo/ogg/BasicStream.java | 121 ------ songdbj/de/jarnbjo/ogg/CachedUrlStream.java | 252 ------------ .../de/jarnbjo/ogg/EndOfOggStreamException.java | 45 --- songdbj/de/jarnbjo/ogg/FileStream.java | 154 -------- songdbj/de/jarnbjo/ogg/LogicalOggStream.java | 151 -------- songdbj/de/jarnbjo/ogg/LogicalOggStreamImpl.java | 213 ---------- songdbj/de/jarnbjo/ogg/OggFormatException.java | 50 --- songdbj/de/jarnbjo/ogg/OggPage.java | 431 --------------------- songdbj/de/jarnbjo/ogg/OnDemandUrlStream.java | 127 ------ songdbj/de/jarnbjo/ogg/PhysicalOggStream.java | 124 ------ songdbj/de/jarnbjo/ogg/UncachedUrlStream.java | 207 ---------- 11 files changed, 1875 deletions(-) delete mode 100644 songdbj/de/jarnbjo/ogg/BasicStream.java delete mode 100644 songdbj/de/jarnbjo/ogg/CachedUrlStream.java delete mode 100644 songdbj/de/jarnbjo/ogg/EndOfOggStreamException.java delete mode 100644 songdbj/de/jarnbjo/ogg/FileStream.java delete mode 100644 songdbj/de/jarnbjo/ogg/LogicalOggStream.java delete mode 100644 songdbj/de/jarnbjo/ogg/LogicalOggStreamImpl.java delete mode 100644 songdbj/de/jarnbjo/ogg/OggFormatException.java delete mode 100644 songdbj/de/jarnbjo/ogg/OggPage.java delete mode 100644 songdbj/de/jarnbjo/ogg/OnDemandUrlStream.java delete mode 100644 songdbj/de/jarnbjo/ogg/PhysicalOggStream.java delete mode 100644 songdbj/de/jarnbjo/ogg/UncachedUrlStream.java (limited to 'songdbj/de/jarnbjo/ogg') diff --git a/songdbj/de/jarnbjo/ogg/BasicStream.java b/songdbj/de/jarnbjo/ogg/BasicStream.java deleted file mode 100644 index 9939524d6c..0000000000 --- a/songdbj/de/jarnbjo/ogg/BasicStream.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.3 2004/09/21 12:09:45 shred - * *** empty log message *** - * - * Revision 1.2 2004/09/21 06:38:45 shred - * Importe reorganisiert, damit Eclipse Ruhe gibt. ;-) - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * - */ - -package de.jarnbjo.ogg; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; - -/** - * Implementation of the PhysicalOggStream interface for reading - * an Ogg stream from a URL. This class performs - * no internal caching, and will not read data from the network before - * requested to do so. It is intended to be used in non-realtime applications - * like file download managers or similar. - */ - -public class BasicStream implements PhysicalOggStream { - - private boolean closed=false; - private InputStream sourceStream; - private Object drainLock=new Object(); - private LinkedList pageCache=new LinkedList(); - private long numberOfSamples=-1; - private int position=0; - - private HashMap logicalStreams=new HashMap(); - private OggPage firstPage; - - public BasicStream(InputStream sourceStream) throws OggFormatException, IOException { - firstPage=OggPage.create(sourceStream); - position+=firstPage.getTotalLength(); - LogicalOggStreamImpl los=new LogicalOggStreamImpl(this, firstPage.getStreamSerialNumber()); - logicalStreams.put(new Integer(firstPage.getStreamSerialNumber()), los); - los.checkFormat(firstPage); - } - - public Collection getLogicalStreams() { - return logicalStreams.values(); - } - - public boolean isOpen() { - return !closed; - } - - public void close() throws IOException { - closed=true; - sourceStream.close(); - } - - public int getContentLength() { - return -1; - } - - public int getPosition() { - return position; - } - - int pageNumber=2; - - public OggPage getOggPage(int index) throws IOException { - if(firstPage!=null) { - OggPage tmp=firstPage; - firstPage=null; - return tmp; - } - else { - OggPage page=OggPage.create(sourceStream); - position+=page.getTotalLength(); - return page; - } - } - - private LogicalOggStream getLogicalStream(int serialNumber) { - return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber)); - } - - public void setTime(long granulePosition) throws IOException { - throw new UnsupportedOperationException("Method not supported by this class"); - } - - /** - * @return always false - */ - - public boolean isSeekable() { - return false; - } - -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/CachedUrlStream.java b/songdbj/de/jarnbjo/ogg/CachedUrlStream.java deleted file mode 100644 index 86f792e272..0000000000 --- a/songdbj/de/jarnbjo/ogg/CachedUrlStream.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.1 2003/04/10 19:48:22 jarnbjo - * no message - * - * - */ - -package de.jarnbjo.ogg; - -import java.io.*; -import java.net.*; -import java.util.*; - -/** - * Implementation of the PhysicalOggStream interface for reading - * and caching an Ogg stream from a URL. This class reads the data as fast as - * possible from the URL, caches it locally either in memory or on disk, and - * supports seeking within the available data. - */ - -public class CachedUrlStream implements PhysicalOggStream { - - private boolean closed=false; - private URLConnection source; - private InputStream sourceStream; - private Object drainLock=new Object(); - private RandomAccessFile drain; - private byte[] memoryCache; - private ArrayList pageOffsets=new ArrayList(); - private ArrayList pageLengths=new ArrayList(); - private long numberOfSamples=-1; - private long cacheLength; - - private HashMap logicalStreams=new HashMap(); - - private LoaderThread loaderThread; - - /** - * Creates an instance of this class, using a memory cache. - */ - - public CachedUrlStream(URL source) throws OggFormatException, IOException { - this(source, null); - } - - /** - * Creates an instance of this class, using the specified file as cache. The - * file is not automatically deleted when this class is disposed. - */ - - public CachedUrlStream(URL source, RandomAccessFile drain) throws OggFormatException, IOException { - - this.source=source.openConnection(); - - if(drain==null) { - int contentLength=this.source.getContentLength(); - if(contentLength==-1) { - throw new IOException("The URLConncetion's content length must be set when operating with a in-memory cache."); - } - memoryCache=new byte[contentLength]; - } - - this.drain=drain; - this.sourceStream=this.source.getInputStream(); - - loaderThread=new LoaderThread(sourceStream, drain, memoryCache); - new Thread(loaderThread).start(); - - while(!loaderThread.isBosDone() || pageOffsets.size()<20) { - System.out.print("pageOffsets.size(): "+pageOffsets.size()+"\r"); - try { - Thread.sleep(200); - } - catch (InterruptedException ex) { - } - } - System.out.println(); - System.out.println("caching "+pageOffsets.size()+"/20 pages\r"); - } - - public Collection getLogicalStreams() { - return logicalStreams.values(); - } - - public boolean isOpen() { - return !closed; - } - - public void close() throws IOException { - closed=true; - sourceStream.close(); - } - - public long getCacheLength() { - return cacheLength; - } - - /* - private OggPage getNextPage() throws EndOfOggStreamException, IOException, OggFormatException { - return getNextPage(false); - } - - private OggPage getNextPage(boolean skipData) throws EndOfOggStreamException, IOException, OggFormatException { - return OggPage.create(sourceStream, skipData); - } - */ - - public OggPage getOggPage(int index) throws IOException { - synchronized(drainLock) { - Long offset=(Long)pageOffsets.get(index); - Long length=(Long)pageLengths.get(index); - if(offset!=null) { - if(drain!=null) { - drain.seek(offset.longValue()); - return OggPage.create(drain); - } - else { - byte[] tmpArray=new byte[length.intValue()]; - System.arraycopy(memoryCache, offset.intValue(), tmpArray, 0, length.intValue()); - return OggPage.create(tmpArray); - } - } - else { - return null; - } - } - } - - private LogicalOggStream getLogicalStream(int serialNumber) { - return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber)); - } - - public void setTime(long granulePosition) throws IOException { - for(Iterator iter=logicalStreams.values().iterator(); iter.hasNext(); ) { - LogicalOggStream los=(LogicalOggStream)iter.next(); - los.setTime(granulePosition); - } - } - - public class LoaderThread implements Runnable { - - private InputStream source; - private RandomAccessFile drain; - private byte[] memoryCache; - - private boolean bosDone=false; - - private int pageNumber; - - public LoaderThread(InputStream source, RandomAccessFile drain, byte[] memoryCache) { - this.source=source; - this.drain=drain; - this.memoryCache=memoryCache; - } - - public void run() { - try { - boolean eos=false; - byte[] buffer=new byte[8192]; - while(!eos) { - OggPage op=OggPage.create(source); - synchronized (drainLock) { - int listSize=pageOffsets.size(); - - long pos= - listSize>0? - ((Long)pageOffsets.get(listSize-1)).longValue()+ - ((Long)pageLengths.get(listSize-1)).longValue(): - 0; - - byte[] arr1=op.getHeader(); - byte[] arr2=op.getSegmentTable(); - byte[] arr3=op.getData(); - - if(drain!=null) { - drain.seek(pos); - drain.write(arr1); - drain.write(arr2); - drain.write(arr3); - } - else { - System.arraycopy(arr1, 0, memoryCache, (int)pos, arr1.length); - System.arraycopy(arr2, 0, memoryCache, (int)pos+arr1.length, arr2.length); - System.arraycopy(arr3, 0, memoryCache, (int)pos+arr1.length+arr2.length, arr3.length); - } - - pageOffsets.add(new Long(pos)); - pageLengths.add(new Long(arr1.length+arr2.length+arr3.length)); - } - - if(!op.isBos()) { - bosDone=true; - //System.out.println("bosDone=true;"); - } - if(op.isEos()) { - eos=true; - } - - LogicalOggStreamImpl los=(LogicalOggStreamImpl)getLogicalStream(op.getStreamSerialNumber()); - if(los==null) { - los=new LogicalOggStreamImpl(CachedUrlStream.this, op.getStreamSerialNumber()); - logicalStreams.put(new Integer(op.getStreamSerialNumber()), los); - los.checkFormat(op); - } - - los.addPageNumberMapping(pageNumber); - los.addGranulePosition(op.getAbsoluteGranulePosition()); - - pageNumber++; - cacheLength=op.getAbsoluteGranulePosition(); - //System.out.println("read page: "+pageNumber); - } - } - catch(EndOfOggStreamException e) { - // ok - } - catch(IOException e) { - e.printStackTrace(); - } - } - - public boolean isBosDone() { - return bosDone; - } - } - - public boolean isSeekable() { - return true; - } -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/EndOfOggStreamException.java b/songdbj/de/jarnbjo/ogg/EndOfOggStreamException.java deleted file mode 100644 index 4a0c3200f4..0000000000 --- a/songdbj/de/jarnbjo/ogg/EndOfOggStreamException.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.2 2005/02/09 23:10:47 shred - * Serial UID für jarnbjo - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.1 2003/03/03 21:02:20 jarnbjo - * no message - * - */ - - package de.jarnbjo.ogg; - -import java.io.IOException; - -/** - * Exception thrown when reaching the end of an Ogg stream - */ - -public class EndOfOggStreamException extends IOException { - private static final long serialVersionUID = 3907210438109444408L; - - public EndOfOggStreamException() { - } -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/FileStream.java b/songdbj/de/jarnbjo/ogg/FileStream.java deleted file mode 100644 index 5a526300bf..0000000000 --- a/songdbj/de/jarnbjo/ogg/FileStream.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.1 2003/04/10 19:48:22 jarnbjo - * no message - * - * - */ - -package de.jarnbjo.ogg; - -import java.io.*; -import java.util.*; - -/** - * Implementation of the PhysicalOggStream interface for accessing - * normal disk files. - */ - -public class FileStream implements PhysicalOggStream { - - private boolean closed=false; - private RandomAccessFile source; - private long[] pageOffsets; - private long numberOfSamples=-1; - - private HashMap logicalStreams=new HashMap(); - - /** - * Creates access to the specified file through the PhysicalOggStream interface. - * The specified source file must have been opened for reading. - * - * @param source the file to read from - * - * @throws OggFormatException if the stream format is incorrect - * @throws IOException if some other IO error occurs when reading the file - */ - - public FileStream(RandomAccessFile source) throws OggFormatException, IOException { - this.source=source; - - ArrayList po=new ArrayList(); - int pageNumber=0; - try { - while(true) { - po.add(new Long(this.source.getFilePointer())); - - // skip data if pageNumber>0 - OggPage op=getNextPage(pageNumber>0); - if(op==null) { - break; - } - - LogicalOggStreamImpl los=(LogicalOggStreamImpl)getLogicalStream(op.getStreamSerialNumber()); - if(los==null) { - los=new LogicalOggStreamImpl(this, op.getStreamSerialNumber()); - logicalStreams.put(new Integer(op.getStreamSerialNumber()), los); - } - - if(pageNumber==0) { - los.checkFormat(op); - } - - los.addPageNumberMapping(pageNumber); - los.addGranulePosition(op.getAbsoluteGranulePosition()); - - if(pageNumber>0) { - this.source.seek(this.source.getFilePointer()+op.getTotalLength()); - } - - pageNumber++; - } - } - catch(EndOfOggStreamException e) { - // ok - } - catch(IOException e) { - throw e; - } - //System.out.println("pageNumber: "+pageNumber); - this.source.seek(0L); - pageOffsets=new long[po.size()]; - int i=0; - Iterator iter=po.iterator(); - while(iter.hasNext()) { - pageOffsets[i++]=((Long)iter.next()).longValue(); - } - } - - public Collection getLogicalStreams() { - return logicalStreams.values(); - } - - public boolean isOpen() { - return !closed; - } - - public void close() throws IOException { - closed=true; - source.close(); - } - - private OggPage getNextPage() throws EndOfOggStreamException, IOException, OggFormatException { - return getNextPage(false); - } - - private OggPage getNextPage(boolean skipData) throws EndOfOggStreamException, IOException, OggFormatException { - return OggPage.create(source, skipData); - } - - public OggPage getOggPage(int index) throws IOException { - source.seek(pageOffsets[index]); - return OggPage.create(source); - } - - private LogicalOggStream getLogicalStream(int serialNumber) { - return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber)); - } - - public void setTime(long granulePosition) throws IOException { - for(Iterator iter=logicalStreams.values().iterator(); iter.hasNext(); ) { - LogicalOggStream los=(LogicalOggStream)iter.next(); - los.setTime(granulePosition); - } - } - - /** - * @return always true - */ - - public boolean isSeekable() { - return true; - } -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/LogicalOggStream.java b/songdbj/de/jarnbjo/ogg/LogicalOggStream.java deleted file mode 100644 index 2f97b2a728..0000000000 --- a/songdbj/de/jarnbjo/ogg/LogicalOggStream.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.2 2003/04/10 19:48:22 jarnbjo - * no message - * - * Revision 1.1 2003/03/03 21:02:20 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.IOException; - -/** - * Interface providing access to a logical Ogg stream as part of a - * physical Ogg stream. - */ - - -public interface LogicalOggStream { - - public static final String FORMAT_UNKNOWN = "application/octet-stream"; - - public static final String FORMAT_VORBIS = "audio/x-vorbis"; - public static final String FORMAT_FLAC = "audio/x-flac"; - public static final String FORMAT_THEORA = "video/x-theora"; - - /** - * Note: To read from the stream, you must use either - * this method or the method getNextOggPacket. - * Mixing calls to the two methods will cause data corruption. - * - * @return the next Ogg page - * - * @see #getNextOggPacket() - * - * @throws OggFormatException if the ogg stream is corrupted - * @throws IOException if some other IO error occurs - */ - - public OggPage getNextOggPage() throws OggFormatException, IOException; - - /** - * Note: To read from the stream, you must use either - * this method or the method getNextOggPage. - * Mixing calls to the two methods will cause data corruption. - * - * @return the next packet as a byte array - * - * @see #getNextOggPage() - * - * @throws OggFormatException if the ogg stream is corrupted - * @throws IOException if some other IO error occurs - */ - - public byte[] getNextOggPacket() throws OggFormatException, IOException; - - /** - * Checks if this stream is open for reading. - * - * @return true if this stream is open for reading, - * false otherwise - */ - - public boolean isOpen(); - - /** - * Closes this stream. After invoking this method, no further access - * to the streams data is possible. - * - * @throws IOException if an IO error occurs - */ - - public void close() throws IOException; - - /** - * Sets the stream's position to the beginning of the stream. - * This method does not work if the physical Ogg stream is not - * seekable. - * - * @throws OggFormatException if the ogg stream is corrupted - * @throws IOException if some other IO error occurs - */ - - public void reset() throws OggFormatException, IOException; - - /** - * This method does not work if the physical Ogg stream is not - * seekable. - * - * @return the granule position of the last page within - * this stream - */ - - public long getMaximumGranulePosition(); - - /** - * This method is invoked on all logical streams when - * calling the same method on the physical stream. The - * same restrictions as mentioned there apply. - * This method does not work if the physical Ogg stream is not - * seekable. - * - * @param granulePosition - * - * @see PhysicalOggStream#setTime(long) - * - * @throws IOException if an IO error occurs - */ - - public void setTime(long granulePosition) throws IOException; - - /** - * @return the last parsed granule position of this stream - */ - - public long getTime(); - - /** - * @return the content type of this stream - * - * @see #FORMAT_UNKNOWN - * @see #FORMAT_VORBIS - * @see #FORMAT_FLAC - * @see #FORMAT_THEORA - */ - - public String getFormat(); -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/LogicalOggStreamImpl.java b/songdbj/de/jarnbjo/ogg/LogicalOggStreamImpl.java deleted file mode 100644 index 1a503e91ca..0000000000 --- a/songdbj/de/jarnbjo/ogg/LogicalOggStreamImpl.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.3 2003/03/31 00:23:04 jarnbjo - * no message - * - * Revision 1.2 2003/03/16 01:11:26 jarnbjo - * no message - * - * Revision 1.1 2003/03/03 21:02:20 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.*; -import java.util.*; - -public class LogicalOggStreamImpl implements LogicalOggStream { - - private PhysicalOggStream source; - private int serialNumber; - - private ArrayList pageNumberMapping=new ArrayList(); - private ArrayList granulePositions=new ArrayList(); - - private int pageIndex=0; - private OggPage currentPage; - private int currentSegmentIndex; - - private boolean open=true; - - private String format=FORMAT_UNKNOWN; - - public LogicalOggStreamImpl(PhysicalOggStream source, int serialNumber) { - this.source=source; - this.serialNumber=serialNumber; - } - - public void addPageNumberMapping(int physicalPageNumber) { - pageNumberMapping.add(new Integer(physicalPageNumber)); - } - - public void addGranulePosition(long granulePosition) { - granulePositions.add(new Long(granulePosition)); - } - - public synchronized void reset() throws OggFormatException, IOException { - currentPage=null; - currentSegmentIndex=0; - pageIndex=0; - } - - public synchronized OggPage getNextOggPage() throws EndOfOggStreamException, OggFormatException, IOException { - if(source.isSeekable()) { - currentPage=source.getOggPage(((Integer)pageNumberMapping.get(pageIndex++)).intValue()); - } - else { - currentPage=source.getOggPage(-1); - } - return currentPage; - } - - public synchronized byte[] getNextOggPacket() throws EndOfOggStreamException, OggFormatException, IOException { - ByteArrayOutputStream res=new ByteArrayOutputStream(); - int segmentLength=0; - - if(currentPage==null) { - currentPage=getNextOggPage(); - } - - do { - if(currentSegmentIndex>=currentPage.getSegmentOffsets().length) { - currentSegmentIndex=0; - - if(!currentPage.isEos()) { - if(source.isSeekable() && pageNumberMapping.size()<=pageIndex) { - while(pageNumberMapping.size()<=pageIndex+10) { - try { - Thread.sleep(1000); - } - catch (InterruptedException ex) { - } - } - } - currentPage=getNextOggPage(); - - if(res.size()==0 && currentPage.isContinued()) { - boolean done=false; - while(!done) { - if(currentPage.getSegmentLengths()[currentSegmentIndex++]!=255) { - done=true; - } - if(currentSegmentIndex>currentPage.getSegmentTable().length) { - currentPage=source.getOggPage(((Integer)pageNumberMapping.get(pageIndex++)).intValue()); - } - } - } - } - else { - throw new EndOfOggStreamException(); - } - } - segmentLength=currentPage.getSegmentLengths()[currentSegmentIndex]; - res.write(currentPage.getData(), currentPage.getSegmentOffsets()[currentSegmentIndex], segmentLength); - currentSegmentIndex++; - } while(segmentLength==255); - - return res.toByteArray(); - } - - public boolean isOpen() { - return open; - } - - public void close() throws IOException { - open=false; - } - - public long getMaximumGranulePosition() { - Long mgp=(Long)granulePositions.get(granulePositions.size()-1); - return mgp.longValue(); - } - - public synchronized long getTime() { - return currentPage!=null?currentPage.getAbsoluteGranulePosition():-1; - } - - public synchronized void setTime(long granulePosition) throws IOException { - - int page=0; - for(page=0; pagegranulePosition) { - break; - } - } - - pageIndex=page; - currentPage=source.getOggPage(((Integer)pageNumberMapping.get(pageIndex++)).intValue()); - currentSegmentIndex=0; - int segmentLength=0; - do { - if(currentSegmentIndex>=currentPage.getSegmentOffsets().length) { - currentSegmentIndex=0; - if(pageIndex>=pageNumberMapping.size()) { - throw new EndOfOggStreamException(); - } - currentPage=source.getOggPage(((Integer)pageNumberMapping.get(pageIndex++)).intValue()); - } - segmentLength=currentPage.getSegmentLengths()[currentSegmentIndex]; - currentSegmentIndex++; - } while(segmentLength==255); - } - - public void checkFormat(OggPage page) { - byte[] data=page.getData(); - - if(data.length>=7 && - data[1]==0x76 && - data[2]==0x6f && - data[3]==0x72 && - data[4]==0x62 && - data[5]==0x69 && - data[6]==0x73) { - - format=FORMAT_VORBIS; - } - else if(data.length>=7 && - data[1]==0x74 && - data[2]==0x68 && - data[3]==0x65 && - data[4]==0x6f && - data[5]==0x72 && - data[6]==0x61) { - - format=FORMAT_THEORA; - } - else if (data.length==4 && - data[0]==0x66 && - data[1]==0x4c && - data[2]==0x61 && - data[3]==0x43) { - - format=FORMAT_FLAC; - } - } - - public String getFormat() { - return format; - } -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/OggFormatException.java b/songdbj/de/jarnbjo/ogg/OggFormatException.java deleted file mode 100644 index a6b2466b92..0000000000 --- a/songdbj/de/jarnbjo/ogg/OggFormatException.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.2 2005/02/09 23:10:47 shred - * Serial UID für jarnbjo - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.1 2003/03/03 21:02:20 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.IOException; - -/** - * Exception thrown when trying to read a corrupted Ogg stream. - */ - -public class OggFormatException extends IOException { - private static final long serialVersionUID = 3544953238333175349L; - - public OggFormatException() { - super(); - } - - public OggFormatException(String message) { - super(message); - } -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/OggPage.java b/songdbj/de/jarnbjo/ogg/OggPage.java deleted file mode 100644 index cc965cc7a9..0000000000 --- a/songdbj/de/jarnbjo/ogg/OggPage.java +++ /dev/null @@ -1,431 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.3 2003/04/10 19:48:22 jarnbjo - * no message - * - * Revision 1.2 2003/03/31 00:23:04 jarnbjo - * no message - * - * Revision 1.1 2003/03/03 21:02:20 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.*; - -import de.jarnbjo.util.io.*; - -/** - *

An instance of this class represents an ogg page read from an ogg file - * or network stream. It has no public constructor, but instances can be - * created by the create methods, supplying a JMF stream or - * a RandomAccessFile - * which is positioned at the beginning of an Ogg page.

- * - *

Furtheron, the class provides methods for accessing the raw page data, - * as well as data attributes like segmenting information, sequence number, - * stream serial number, chechsum and wether this page is the beginning or - * end of a logical bitstream (BOS, EOS) and if the page data starts with a - * continued packet or a fresh data packet.

- */ - -public class OggPage { - - private int version; - private boolean continued, bos, eos; - private long absoluteGranulePosition; - private int streamSerialNumber, pageSequenceNumber, pageCheckSum; - private int[] segmentOffsets; - private int[] segmentLengths; - private int totalLength; - private byte[] header, segmentTable, data; - - protected OggPage() { - } - - private OggPage( - int version, - boolean continued, - boolean bos, - boolean eos, - long absoluteGranulePosition, - int streamSerialNumber, - int pageSequenceNumber, - int pageCheckSum, - int[] segmentOffsets, - int[] segmentLengths, - int totalLength, - byte[] header, - byte[] segmentTable, - byte[] data) { - - this.version=version; - this.continued=continued; - this.bos=bos; - this.eos=eos; - this.absoluteGranulePosition=absoluteGranulePosition; - this.streamSerialNumber=streamSerialNumber; - this.pageSequenceNumber=pageSequenceNumber; - this.pageCheckSum=pageCheckSum; - this.segmentOffsets=segmentOffsets; - this.segmentLengths=segmentLengths; - this.totalLength=totalLength; - this.header=header; - this.segmentTable=segmentTable; - this.data=data; - } - - /** - * this method equals to create(RandomAccessFile source, false) - * - * @see #create(RandomAccessFile, boolean) - */ - - public static OggPage create(RandomAccessFile source) throws IOException, EndOfOggStreamException, OggFormatException { - return create(source, false); - } - - /** - * This method is called to read data from the current position in the - * specified RandomAccessFile and create a new OggPage instance based on the data - * read. If the parameter skipData is set to true, - * the actual page segments (page data) is skipped and not read into - * memory. This mode is useful when scanning through an ogg file to build - * a seek table. - * - * @param source the source from which the ogg page is generated - * @param skipData if set to true, the actual page data is not read into memory - * @return an ogg page created by reading data from the specified source, starting at the current position - * @throws FormatException if the data read from the specified source is not matching the specification for an ogg page - * @throws EndOfStreamException if it is not possible to read an entire ogg page from the specified source - * @throws IOException if some other I/O error is detected when reading from the source - * - * @see #create(RandomAccessFile) - */ - - public static OggPage create(RandomAccessFile source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException { - return create((Object)source, skipData); - } - - /** - * this method equals to create(InputStream source, false) - * - * @see #create(InputStream, boolean) - */ - - public static OggPage create(InputStream source) throws IOException, EndOfOggStreamException, OggFormatException { - return create(source, false); - } - - /** - * This method is called to read data from the current position in the - * specified InpuStream and create a new OggPage instance based on the data - * read. If the parameter skipData is set to true, - * the actual page segments (page data) is skipped and not read into - * memory. This mode is useful when scanning through an ogg file to build - * a seek table. - * - * @param source the source from which the ogg page is generated - * @param skipData if set to true, the actual page data is not read into memory - * @return an ogg page created by reading data from the specified source, starting at the current position - * @throws FormatException if the data read from the specified source is not matching the specification for an ogg page - * @throws EndOfStreamException if it is not possible to read an entire ogg page from the specified source - * @throws IOException if some other I/O error is detected when reading from the source - * - * @see #create(InputStream) - */ - - public static OggPage create(InputStream source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException { - return create((Object)source, skipData); - } - - /** - * this method equals to create(byte[] source, false) - * - * @see #create(byte[], boolean) - */ - - public static OggPage create(byte[] source) throws IOException, EndOfOggStreamException, OggFormatException { - return create(source, false); - } - - /** - * This method is called to - * create a new OggPage instance based on the specified byte array. - * - * @param source the source from which the ogg page is generated - * @param skipData if set to true, the actual page data is not read into memory - * @return an ogg page created by reading data from the specified source, starting at the current position - * @throws FormatException if the data read from the specified source is not matching the specification for an ogg page - * @throws EndOfStreamException if it is not possible to read an entire ogg page from the specified source - * @throws IOException if some other I/O error is detected when reading from the source - * - * @see #create(byte[]) - */ - - public static OggPage create(byte[] source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException { - return create((Object)source, skipData); - } - - private static OggPage create(Object source, boolean skipData) throws IOException, EndOfOggStreamException, OggFormatException { - - try { - int sourceOffset=27; - - byte[] header=new byte[27]; - if(source instanceof RandomAccessFile) { - RandomAccessFile raf=(RandomAccessFile)source; - if(raf.getFilePointer()==raf.length()) { - return null; - } - raf.readFully(header); - } - else if(source instanceof InputStream) { - readFully((InputStream)source, header); - } - else if(source instanceof byte[]) { - System.arraycopy((byte[])source, 0, header, 0, 27); - } - - BitInputStream bdSource=new ByteArrayBitInputStream(header); - - int capture=bdSource.getInt(32); - - if(capture!=0x5367674f) { - //throw new FormatException("Ogg page does not start with 'OggS' (0x4f676753)"); - - /* - ** This condition is IMHO an error, but older Ogg files often contain - ** pages with a different capture than OggS. I am not sure how to - ** manage these pages, but the decoder seems to work properly, if - ** the incorrect capture is simply ignored. - */ - - String cs=Integer.toHexString(capture); - while(cs.length()<8) { - cs="0"+cs; - } - cs=cs.substring(6, 8)+cs.substring(4, 6)+cs.substring(2, 4)+cs.substring(0, 2); - char c1=(char)(Integer.valueOf(cs.substring(0, 2), 16).intValue()); - char c2=(char)(Integer.valueOf(cs.substring(2, 4), 16).intValue()); - char c3=(char)(Integer.valueOf(cs.substring(4, 6), 16).intValue()); - char c4=(char)(Integer.valueOf(cs.substring(6, 8), 16).intValue()); - System.out.println("Ogg packet header is 0x"+cs+" ("+c1+c2+c3+c4+"), should be 0x4f676753 (OggS)"); - } - - int version=bdSource.getInt(8); - byte tmp=(byte)bdSource.getInt(8); - boolean bf1=(tmp&1)!=0; - boolean bos=(tmp&2)!=0; - boolean eos=(tmp&4)!=0; - long absoluteGranulePosition=bdSource.getLong(64); - int streamSerialNumber=bdSource.getInt(32); - int pageSequenceNumber=bdSource.getInt(32); - int pageCheckSum=bdSource.getInt(32); - int pageSegments=bdSource.getInt(8); - - //System.out.println("OggPage: "+streamSerialNumber+" / "+absoluteGranulePosition+" / "+pageSequenceNumber); - - int[] segmentOffsets=new int[pageSegments]; - int[] segmentLengths=new int[pageSegments]; - int totalLength=0; - - byte[] segmentTable=new byte[pageSegments]; - byte[] tmpBuf=new byte[1]; - - for(int i=0; itrue if this page begins with a continued packet - */ - - public boolean isContinued() { - return continued; - } - - /** - * @return true if this page begins with a fresh packet - */ - - public boolean isFresh() { - return !continued; - } - - /** - * @return true if this page is the beginning of a logical stream - */ - - public boolean isBos() { - return bos; - } - - /** - * @return true if this page is the end of a logical stream - */ - - public boolean isEos() { - return eos; - } - -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/OnDemandUrlStream.java b/songdbj/de/jarnbjo/ogg/OnDemandUrlStream.java deleted file mode 100644 index 98159c4e7c..0000000000 --- a/songdbj/de/jarnbjo/ogg/OnDemandUrlStream.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.1 2003/04/10 19:48:22 jarnbjo - * no message - * - * Revision 1.1 2003/03/31 00:23:04 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.*; -import java.net.*; -import java.util.*; - -/** - * Implementation of the PhysicalOggStream interface for reading - * an Ogg stream from a URL. This class performs - * no internal caching, and will not read data from the network before - * requested to do so. It is intended to be used in non-realtime applications - * like file download managers or similar. - */ - -public class OnDemandUrlStream implements PhysicalOggStream { - - private boolean closed=false; - private URLConnection source; - private InputStream sourceStream; - private Object drainLock=new Object(); - private LinkedList pageCache=new LinkedList(); - private long numberOfSamples=-1; - private int contentLength=0; - private int position=0; - - private HashMap logicalStreams=new HashMap(); - private OggPage firstPage; - - private static final int PAGECACHE_SIZE = 20; - - public OnDemandUrlStream(URL source) throws OggFormatException, IOException { - this.source=source.openConnection(); - this.sourceStream=this.source.getInputStream(); - - contentLength=this.source.getContentLength(); - - firstPage=OggPage.create(sourceStream); - position+=firstPage.getTotalLength(); - LogicalOggStreamImpl los=new LogicalOggStreamImpl(this, firstPage.getStreamSerialNumber()); - logicalStreams.put(new Integer(firstPage.getStreamSerialNumber()), los); - los.checkFormat(firstPage); - } - - public Collection getLogicalStreams() { - return logicalStreams.values(); - } - - public boolean isOpen() { - return !closed; - } - - public void close() throws IOException { - closed=true; - sourceStream.close(); - } - - public int getContentLength() { - return contentLength; - } - - public int getPosition() { - return position; - } - - int pageNumber=2; - - public OggPage getOggPage(int index) throws IOException { - if(firstPage!=null) { - OggPage tmp=firstPage; - firstPage=null; - return tmp; - } - else { - OggPage page=OggPage.create(sourceStream); - position+=page.getTotalLength(); - return page; - } - } - - private LogicalOggStream getLogicalStream(int serialNumber) { - return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber)); - } - - public void setTime(long granulePosition) throws IOException { - throw new UnsupportedOperationException("Method not supported by this class"); - } - - /** - * @return always false - */ - - public boolean isSeekable() { - return false; - } - -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/PhysicalOggStream.java b/songdbj/de/jarnbjo/ogg/PhysicalOggStream.java deleted file mode 100644 index 5f342a38b7..0000000000 --- a/songdbj/de/jarnbjo/ogg/PhysicalOggStream.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.3 2003/04/10 19:48:22 jarnbjo - * no message - * - * Revision 1.2 2003/03/31 00:23:04 jarnbjo - * no message - * - * Revision 1.1 2003/03/03 21:02:20 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.IOException; -import java.util.Collection; - -/** - * Interface providing access to a physical Ogg stream. Typically this is - * a file. - */ - -public interface PhysicalOggStream { - - /** - * Returns a collection of objects implementing LogicalOggStream - * for accessing the separate logical streams within this physical Ogg stream. - * - * @return a collection of objects implementing LogicalOggStream - * which are representing the logical streams contained within this - * physical stream - * - * @see LogicalOggStream - */ - - public Collection getLogicalStreams(); - - /** - * Return the Ogg page with the absolute index index, - * independent from the logical structure of this stream or if the - * index parameter is -1, the next Ogg page is returned. - * This method should only be used by implementations of LogicalOggStream - * to access the raw pages. - * - * @param index the absolute index starting from 0 at the beginning of - * the file or stream or -1 to get the next page in a non-seekable - * stream - * - * @return the Ogg page with the physical absolute index index - * - * @throws OggFormatException if the ogg stream is corrupted - * @throws IOException if some other IO error occurs - */ - - public OggPage getOggPage(int index) throws OggFormatException, IOException; - - /** - * Checks if this stream is open for reading. - * - * @return true if this stream is open for reading, - * false otherwise - */ - - public boolean isOpen(); - - /** - * Closes this stream. After invoking this method, no further access - * to the streams data is possible. - * - * @throws IOException - */ - - public void close() throws IOException; - - /** - * Sets this stream's (and its logical stream's) position to the granule - * position. The next packet read from any logical stream will be the - * first packet beginning on the first page with a granule position higher - * than the argument.

- * - * At the moment, this method only works correctly for Ogg files with - * a single logical Vorbis stream, and due to the different interpretations - * of the granule position, depending on mixed content, this method will - * never be able to work for mixed streams. Chained and interleaved streams are - * also not yet supported. Actually, this method is only a hack to support - * seeking from JMF, but may of course be abused otherwise too :) - * - * @param granulePosition - * - * @throws OggFormatException if the ogg stream is corrupted - * @throws IOException if some other IO error occurs - */ - - public void setTime(long granulePosition) throws OggFormatException, IOException; - - /** - * @return true if the stream is seekable, false - * otherwise - */ - - public boolean isSeekable(); -} \ No newline at end of file diff --git a/songdbj/de/jarnbjo/ogg/UncachedUrlStream.java b/songdbj/de/jarnbjo/ogg/UncachedUrlStream.java deleted file mode 100644 index a07f0ac00e..0000000000 --- a/songdbj/de/jarnbjo/ogg/UncachedUrlStream.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * $ProjectName$ - * $ProjectRevision$ - * ----------------------------------------------------------- - * $Id$ - * ----------------------------------------------------------- - * - * $Author$ - * - * Description: - * - * Copyright 2002-2003 Tor-Einar Jarnbjo - * ----------------------------------------------------------- - * - * Change History - * ----------------------------------------------------------- - * $Log$ - * Revision 1.1 2005/07/11 15:42:36 hcl - * Songdb java version, source. only 1.5 compatible - * - * Revision 1.1.1.1 2004/04/04 22:09:12 shred - * First Import - * - * Revision 1.1 2003/04/10 19:48:22 jarnbjo - * no message - * - */ - -package de.jarnbjo.ogg; - -import java.io.*; -import java.net.*; -import java.util.*; - -/** - * Implementation of the PhysicalOggStream interface for reading - * an Ogg stream from a URL. This class performs only the necessary caching - * to provide continous playback. Seeking within the stream is not supported. - */ - -public class UncachedUrlStream implements PhysicalOggStream { - - private boolean closed=false; - private URLConnection source; - private InputStream sourceStream; - private Object drainLock=new Object(); - private LinkedList pageCache=new LinkedList(); - private long numberOfSamples=-1; - - private HashMap logicalStreams=new HashMap(); - - private LoaderThread loaderThread; - - private static final int PAGECACHE_SIZE = 10; - - /** Creates an instance of the PhysicalOggStream interface - * suitable for reading an Ogg stream from a URL. - */ - - public UncachedUrlStream(URL source) throws OggFormatException, IOException { - - this.source=source.openConnection(); - this.sourceStream=this.source.getInputStream(); - - loaderThread=new LoaderThread(sourceStream, pageCache); - new Thread(loaderThread).start(); - - while(!loaderThread.isBosDone() || pageCache.size()PAGECACHE_SIZE) { - try { - Thread.sleep(200); - } - catch (InterruptedException ex) { - } - } - } - } - catch(EndOfOggStreamException e) { - // ok - } - catch(IOException e) { - e.printStackTrace(); - } - } - - public boolean isBosDone() { - return bosDone; - } - } - - /** - * @return always false - */ - - public boolean isSeekable() { - return false; - } - -} \ No newline at end of file -- cgit v1.2.3