1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.audio.plugins; 34 35 import com.jme3.util.IntMap; 36 import de.jarnbjo.ogg.LogicalOggStream; 37 import de.jarnbjo.ogg.LogicalOggStreamImpl; 38 import de.jarnbjo.ogg.OggPage; 39 import de.jarnbjo.ogg.PhysicalOggStream; 40 import java.io.IOException; 41 import java.io.InputStream; 42 import java.util.Collection; 43 import java.util.HashMap; 44 import java.util.logging.Level; 45 import java.util.logging.Logger; 46 47 /** 48 * Implementation of the <code>PhysicalOggStream</code> interface for reading 49 * and caching an Ogg stream from a URL. This class reads the data as fast as 50 * possible from the URL, caches it locally either in memory or on disk, and 51 * supports seeking within the available data. 52 */ 53 public class CachedOggStream implements PhysicalOggStream { 54 55 private boolean closed = false; 56 private boolean eos = false; 57 private boolean bos = false; 58 private InputStream sourceStream; 59 private HashMap<Integer, LogicalOggStream> logicalStreams 60 = new HashMap<Integer, LogicalOggStream>(); 61 62 private IntMap<OggPage> oggPages = new IntMap<OggPage>(); 63 private OggPage lastPage; 64 65 private int pageNumber; 66 67 public CachedOggStream(InputStream in) throws IOException { 68 sourceStream = in; 69 70 // Read all OGG pages in file 71 long time = System.nanoTime(); 72 while (!eos){ 73 readOggNextPage(); 74 } 75 long dt = System.nanoTime() - time; 76 Logger.getLogger(CachedOggStream.class.getName()).log(Level.INFO, "Took {0} ms to load OGG", dt/1000000); 77 } 78 79 public OggPage getLastOggPage() { 80 return lastPage; 81 } 82 83 private LogicalOggStream getLogicalStream(int serialNumber) { 84 return logicalStreams.get(Integer.valueOf(serialNumber)); 85 } 86 87 public Collection<LogicalOggStream> getLogicalStreams() { 88 return logicalStreams.values(); 89 } 90 91 public boolean isOpen() { 92 return !closed; 93 } 94 95 public void close() throws IOException { 96 closed = true; 97 sourceStream.close(); 98 } 99 100 public OggPage getOggPage(int index) throws IOException { 101 return oggPages.get(index); 102 } 103 104 public void setTime(long granulePosition) throws IOException { 105 for (LogicalOggStream los : getLogicalStreams()){ 106 los.setTime(granulePosition); 107 } 108 } 109 110 private int readOggNextPage() throws IOException { 111 if (eos) 112 return -1; 113 114 OggPage op = OggPage.create(sourceStream); 115 if (!op.isBos()){ 116 bos = true; 117 } 118 if (op.isEos()){ 119 eos = true; 120 lastPage = op; 121 } 122 123 LogicalOggStreamImpl los = (LogicalOggStreamImpl) logicalStreams.get(op.getStreamSerialNumber()); 124 if(los == null) { 125 los = new LogicalOggStreamImpl(this, op.getStreamSerialNumber()); 126 logicalStreams.put(op.getStreamSerialNumber(), los); 127 los.checkFormat(op); 128 } 129 130 los.addPageNumberMapping(pageNumber); 131 los.addGranulePosition(op.getAbsoluteGranulePosition()); 132 133 oggPages.put(pageNumber, op); 134 pageNumber++; 135 136 return pageNumber-1; 137 } 138 139 public boolean isSeekable() { 140 return true; 141 } 142 }