here it is ... the upload-api, script-server, js2 (javascript phase2) branch merge...
[lhc/web/wiklou.git] / js2 / mwEmbed / binPlayers / omtk-fx / src / as / org / omtk / ogg / LogicalOggStream.as
1 /*
2
3 Copyright 2008 Tor-Einar Jarnbjo
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16
17 */
18
19 package org.omtk.ogg {
20
21 import org.omtk.util.BitByteArray;
22 import flash.utils.Endian;
23
24 public class LogicalOggStream {
25
26 private var source:UncachedUrlStream;
27
28 private var pageIndex:int;
29 private var currentPage:OggPage;
30 private var currentSegmentIndex:int;
31
32 public function LogicalOggStream(source:UncachedUrlStream) {
33 this.source = source;
34 }
35
36 public function getNextOggPacket(): OggPacket {
37
38 var res:BitByteArray = new BitByteArray();
39
40 var segmentLength:int = 0;
41
42 if(currentPage == null) {
43 currentPage = source.getNextOggPage();
44 }
45
46 do {
47 if(currentSegmentIndex >= currentPage.segmentOffsets.length) {
48 currentSegmentIndex=0;
49
50 if(currentPage.eos) {
51 throw new EndOfOggStreamError("End of OGG stream");
52 }
53
54 currentPage = source.getNextOggPage();
55 }
56
57 segmentLength = currentPage.segmentLengths[currentSegmentIndex];
58 res.writeBytes(currentPage.data, currentPage.segmentOffsets[currentSegmentIndex], segmentLength);
59 currentSegmentIndex++;
60 }
61 while(segmentLength==255);
62
63 res.position = 0;
64
65 var lastPacket: Boolean = currentPage.eos && currentSegmentIndex == currentPage.segmentOffsets.length;
66 var lastGranulePosition: int = currentPage.absoluteGranulePosition;
67
68 return new OggPacket(res, lastPacket, lastGranulePosition);
69 }
70
71 }
72 }