~arkadini/fogg/trunk

« back to all changes in this revision

Viewing changes to org/xiph/foggy/Demuxer.hx

  • Committer: Arek Korbik
  • Date: 2008-10-02 01:19:30 UTC
  • Revision ID: arkadini@gmail.com-20081002011930-2x2qbqdx536mf302
Add initial version of a simple ogg demuxer (modeled on the
oggz_read API).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.xiph.foggy;
 
2
 
 
3
import org.xiph.system.Bytes;
 
4
 
 
5
import org.xiph.fogg.SyncState;
 
6
import org.xiph.fogg.StreamState;
 
7
import org.xiph.fogg.Page;
 
8
import org.xiph.fogg.Packet;
 
9
 
 
10
import flash.utils.IDataInput;
 
11
 
 
12
enum DemuxerStatus {
 
13
    dmx_ok;
 
14
    dmx_stop;
 
15
}
 
16
 
 
17
class Demuxer {
 
18
    public static inline var ENOTOGG = -1;
 
19
 
 
20
    var oy : SyncState;
 
21
    //var os : StreamState;
 
22
    var og : Page;
 
23
    var op : Packet;
 
24
 
 
25
    var streams : IntHash<StreamState>;
 
26
    var bos_done : Bool;
 
27
 
 
28
    var page_cbs : IntHash<Page -> Int -> DemuxerStatus>;
 
29
    var packet_cbs : IntHash<Packet -> Int -> DemuxerStatus>;
 
30
 
 
31
    public function new() {
 
32
        page_cbs = new IntHash();
 
33
        packet_cbs = new IntHash();
 
34
        streams = new IntHash();
 
35
 
 
36
        bos_done = false;
 
37
 
 
38
        oy = new SyncState();
 
39
        //os = new StreamState();
 
40
 
 
41
        og = new Page();
 
42
        op = new Packet();
 
43
 
 
44
        oy.init();
 
45
    }
 
46
 
 
47
    public function set_page_cb(serialno : Int,
 
48
                                cb : Page -> Int -> DemuxerStatus) : Void {
 
49
        if (serialno != -1 && !streams.exists(serialno)) {
 
50
            // TODO: throw and exception?
 
51
        } else {
 
52
            page_cbs.set(serialno, cb);
 
53
        }
 
54
    }
 
55
 
 
56
    public function remove_page_cb(serialno : Int) : Void {
 
57
        page_cbs.remove(serialno);
 
58
    }
 
59
 
 
60
    public function set_packet_cb(serialno : Int,
 
61
                                  cb : Packet -> Int -> DemuxerStatus) : Void {
 
62
        if (serialno != -1 && !streams.exists(serialno)) {
 
63
            // TODO: throw and exception?
 
64
                trace("*** HERE ***");
 
65
                trace("streams: " + streams.toString());
 
66
        } else {
 
67
            packet_cbs.set(serialno, cb);
 
68
        }
 
69
    }
 
70
 
 
71
    public function remove_packet_cb(serialno : Int) : Void {
 
72
        packet_cbs.remove(serialno);
 
73
    }
 
74
 
 
75
    //public function read(data : Bytes, len : Int, pos : Int = -1) : Int {
 
76
    public function read(data : IDataInput, len : Int, pos : Int = -1) : Int {
 
77
        // TODO: handle len as end_of_data?
 
78
        var buffer : Bytes;
 
79
        var ret : Int;
 
80
        var index : Int = oy.buffer(len);
 
81
        buffer = oy.data;
 
82
 
 
83
        if (pos != -1) {
 
84
            //data.position = pos;
 
85
        }
 
86
        data.readBytes(buffer, index, len);
 
87
        oy.wrote(len);
 
88
 
 
89
        while (true) {
 
90
            if ((ret = oy.pageout(og)) != 1) {
 
91
                if (buffer.length < 16384 || ret == 0)
 
92
                    return len;
 
93
                else {
 
94
                    return ENOTOGG;
 
95
                }
 
96
            }
 
97
 
 
98
            _process_page(og);
 
99
            // TODO: check for returns from _process_page()
 
100
        }
 
101
 
 
102
        return len;
 
103
    }
 
104
 
 
105
    private function _process_page(p : Page) : Int {
 
106
        var sn = p.serialno();
 
107
        var cbret : DemuxerStatus;
 
108
        var ret : Int;
 
109
        var cb : Page -> Int -> DemuxerStatus;
 
110
 
 
111
        cb = page_cbs.get(sn);
 
112
        if (cb == null) {
 
113
            cb = page_cbs.get(-1);
 
114
        }
 
115
 
 
116
        if (cb != null) {
 
117
            cbret = cb(p, sn);
 
118
            // TODO handle stop request
 
119
        }
 
120
 
 
121
        var os : StreamState = streams.get(sn);
 
122
        if (os == null) {
 
123
            if (bos_done) {
 
124
                // unexpected new stream
 
125
                return -1;
 
126
            }
 
127
            os = new StreamState();
 
128
            os.init(sn);
 
129
            streams.set(sn, os);
 
130
        } else {
 
131
            // end of bos pages? handle!...
 
132
            if (!bos_done) {
 
133
                bos_done = true;
 
134
            }
 
135
        }
 
136
 
 
137
        if (os.pagein(p) < 0) {
 
138
            // can happen on an unsupported version
 
139
            return -1;
 
140
        }
 
141
 
 
142
        while (true) {
 
143
            if ((ret = os.packetout(op)) != 1) {
 
144
                if (ret == 0)
 
145
                    break;
 
146
            } else {
 
147
                _process_packet(op, sn);
 
148
            }
 
149
        }
 
150
 
 
151
        if (p.eos() != 0) {
 
152
            os.clear();
 
153
            streams.remove(sn);
 
154
            if (!streams.iterator().hasNext()) {
 
155
                bos_done = false;
 
156
                // we're ready for new chained streams
 
157
            }
 
158
        }
 
159
 
 
160
        return 0;
 
161
    }
 
162
 
 
163
    private function _process_packet(p : Packet, sn : Int) : Int {
 
164
        var ret : Int;
 
165
        var cbret : DemuxerStatus;
 
166
        var cb : Packet -> Int -> DemuxerStatus;
 
167
 
 
168
        cb = packet_cbs.get(sn);
 
169
        if (cb == null) {
 
170
            cb = packet_cbs.get(-1);
 
171
        }
 
172
 
 
173
        if (cb != null) {
 
174
            cbret = cb(p, sn);
 
175
            // TODO handle stop request
 
176
        }
 
177
 
 
178
        return 0;
 
179
    }
 
180
 
 
181
}
 
 
b'\\ No newline at end of file'