diff options
Diffstat (limited to 'songdbj/de/jarnbjo/ogg/UncachedUrlStream.java')
-rw-r--r-- | songdbj/de/jarnbjo/ogg/UncachedUrlStream.java | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/songdbj/de/jarnbjo/ogg/UncachedUrlStream.java b/songdbj/de/jarnbjo/ogg/UncachedUrlStream.java new file mode 100644 index 0000000000..a07f0ac00e --- /dev/null +++ b/songdbj/de/jarnbjo/ogg/UncachedUrlStream.java | |||
@@ -0,0 +1,207 @@ | |||
1 | /* | ||
2 | * $ProjectName$ | ||
3 | * $ProjectRevision$ | ||
4 | * ----------------------------------------------------------- | ||
5 | * $Id$ | ||
6 | * ----------------------------------------------------------- | ||
7 | * | ||
8 | * $Author$ | ||
9 | * | ||
10 | * Description: | ||
11 | * | ||
12 | * Copyright 2002-2003 Tor-Einar Jarnbjo | ||
13 | * ----------------------------------------------------------- | ||
14 | * | ||
15 | * Change History | ||
16 | * ----------------------------------------------------------- | ||
17 | * $Log$ | ||
18 | * Revision 1.1 2005/07/11 15:42:36 hcl | ||
19 | * Songdb java version, source. only 1.5 compatible | ||
20 | * | ||
21 | * Revision 1.1.1.1 2004/04/04 22:09:12 shred | ||
22 | * First Import | ||
23 | * | ||
24 | * Revision 1.1 2003/04/10 19:48:22 jarnbjo | ||
25 | * no message | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | package de.jarnbjo.ogg; | ||
30 | |||
31 | import java.io.*; | ||
32 | import java.net.*; | ||
33 | import java.util.*; | ||
34 | |||
35 | /** | ||
36 | * Implementation of the <code>PhysicalOggStream</code> interface for reading | ||
37 | * an Ogg stream from a URL. This class performs only the necessary caching | ||
38 | * to provide continous playback. Seeking within the stream is not supported. | ||
39 | */ | ||
40 | |||
41 | public class UncachedUrlStream implements PhysicalOggStream { | ||
42 | |||
43 | private boolean closed=false; | ||
44 | private URLConnection source; | ||
45 | private InputStream sourceStream; | ||
46 | private Object drainLock=new Object(); | ||
47 | private LinkedList pageCache=new LinkedList(); | ||
48 | private long numberOfSamples=-1; | ||
49 | |||
50 | private HashMap logicalStreams=new HashMap(); | ||
51 | |||
52 | private LoaderThread loaderThread; | ||
53 | |||
54 | private static final int PAGECACHE_SIZE = 10; | ||
55 | |||
56 | /** Creates an instance of the <code>PhysicalOggStream</code> interface | ||
57 | * suitable for reading an Ogg stream from a URL. | ||
58 | */ | ||
59 | |||
60 | public UncachedUrlStream(URL source) throws OggFormatException, IOException { | ||
61 | |||
62 | this.source=source.openConnection(); | ||
63 | this.sourceStream=this.source.getInputStream(); | ||
64 | |||
65 | loaderThread=new LoaderThread(sourceStream, pageCache); | ||
66 | new Thread(loaderThread).start(); | ||
67 | |||
68 | while(!loaderThread.isBosDone() || pageCache.size()<PAGECACHE_SIZE) { | ||
69 | try { | ||
70 | Thread.sleep(200); | ||
71 | } | ||
72 | catch (InterruptedException ex) { | ||
73 | } | ||
74 | //System.out.print("caching "+pageCache.size()+"/"+PAGECACHE_SIZE+" pages\r"); | ||
75 | } | ||
76 | //System.out.println(); | ||
77 | } | ||
78 | |||
79 | public Collection getLogicalStreams() { | ||
80 | return logicalStreams.values(); | ||
81 | } | ||
82 | |||
83 | public boolean isOpen() { | ||
84 | return !closed; | ||
85 | } | ||
86 | |||
87 | public void close() throws IOException { | ||
88 | closed=true; | ||
89 | sourceStream.close(); | ||
90 | } | ||
91 | |||
92 | /* | ||
93 | public long getCacheLength() { | ||
94 | return cacheLength; | ||
95 | } | ||
96 | */ | ||
97 | |||
98 | /* | ||
99 | private OggPage getNextPage() throws EndOfOggStreamException, IOException, OggFormatException { | ||
100 | return getNextPage(false); | ||
101 | } | ||
102 | |||
103 | private OggPage getNextPage(boolean skipData) throws EndOfOggStreamException, IOException, OggFormatException { | ||
104 | return OggPage.create(sourceStream, skipData); | ||
105 | } | ||
106 | */ | ||
107 | |||
108 | public OggPage getOggPage(int index) throws IOException { | ||
109 | while(pageCache.size()==0) { | ||
110 | try { | ||
111 | Thread.sleep(100); | ||
112 | } | ||
113 | catch (InterruptedException ex) { | ||
114 | } | ||
115 | } | ||
116 | synchronized(drainLock) { | ||
117 | //OggPage page=(OggPage)pageCache.getFirst(); | ||
118 | //pageCache.removeFirst(); | ||
119 | //return page; | ||
120 | return (OggPage)pageCache.removeFirst(); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | private LogicalOggStream getLogicalStream(int serialNumber) { | ||
125 | return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber)); | ||
126 | } | ||
127 | |||
128 | public void setTime(long granulePosition) throws IOException { | ||
129 | throw new UnsupportedOperationException("Method not supported by this class"); | ||
130 | } | ||
131 | |||
132 | public class LoaderThread implements Runnable { | ||
133 | |||
134 | private InputStream source; | ||
135 | private LinkedList pageCache; | ||
136 | private RandomAccessFile drain; | ||
137 | private byte[] memoryCache; | ||
138 | |||
139 | private boolean bosDone=false; | ||
140 | |||
141 | private int pageNumber; | ||
142 | |||
143 | public LoaderThread(InputStream source, LinkedList pageCache) { | ||
144 | this.source=source; | ||
145 | this.pageCache=pageCache; | ||
146 | } | ||
147 | |||
148 | public void run() { | ||
149 | try { | ||
150 | boolean eos=false; | ||
151 | byte[] buffer=new byte[8192]; | ||
152 | while(!eos) { | ||
153 | OggPage op=OggPage.create(source); | ||
154 | synchronized (drainLock) { | ||
155 | pageCache.add(op); | ||
156 | } | ||
157 | |||
158 | if(!op.isBos()) { | ||
159 | bosDone=true; | ||
160 | } | ||
161 | if(op.isEos()) { | ||
162 | eos=true; | ||
163 | } | ||
164 | |||
165 | LogicalOggStreamImpl los=(LogicalOggStreamImpl)getLogicalStream(op.getStreamSerialNumber()); | ||
166 | if(los==null) { | ||
167 | los=new LogicalOggStreamImpl(UncachedUrlStream.this, op.getStreamSerialNumber()); | ||
168 | logicalStreams.put(new Integer(op.getStreamSerialNumber()), los); | ||
169 | los.checkFormat(op); | ||
170 | } | ||
171 | |||
172 | //los.addPageNumberMapping(pageNumber); | ||
173 | //los.addGranulePosition(op.getAbsoluteGranulePosition()); | ||
174 | |||
175 | pageNumber++; | ||
176 | |||
177 | while(pageCache.size()>PAGECACHE_SIZE) { | ||
178 | try { | ||
179 | Thread.sleep(200); | ||
180 | } | ||
181 | catch (InterruptedException ex) { | ||
182 | } | ||
183 | } | ||
184 | } | ||
185 | } | ||
186 | catch(EndOfOggStreamException e) { | ||
187 | // ok | ||
188 | } | ||
189 | catch(IOException e) { | ||
190 | e.printStackTrace(); | ||
191 | } | ||
192 | } | ||
193 | |||
194 | public boolean isBosDone() { | ||
195 | return bosDone; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | /** | ||
200 | * @return always <code>false</code> | ||
201 | */ | ||
202 | |||
203 | public boolean isSeekable() { | ||
204 | return false; | ||
205 | } | ||
206 | |||
207 | } \ No newline at end of file | ||