1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use ogg::{PacketReader, Packet};
use header::*;
use VorbisError;
use std::io::{Read, Seek};
use ::audio::{PreviousWindowRight, read_audio_packet};
use ::header::HeaderSet;
pub fn read_headers<'a, T: Read + Seek + 'a>(rdr: &mut PacketReader<T>) ->
Result<(HeaderSet, u32), VorbisError> {
let pck :Packet = try!(rdr.read_packet_expected());
let ident_hdr = try!(read_header_ident(&pck.data));
let pck :Packet = try!(rdr.read_packet_expected());
let comment_hdr = try!(read_header_comment(&pck.data));
let pck :Packet = try!(rdr.read_packet_expected());
let setup_hdr = try!(read_header_setup(&pck.data, ident_hdr.audio_channels,
(ident_hdr.blocksize_0, ident_hdr.blocksize_1)));
return Ok(((ident_hdr, comment_hdr, setup_hdr), pck.stream_serial));
}
pub struct OggStreamReader<T: Read + Seek> {
rdr :PacketReader<T>,
pwr :PreviousWindowRight,
stream_serial :u32,
pub ident_hdr :IdentHeader,
pub comment_hdr :CommentHeader,
pub setup_hdr :SetupHeader,
absgp_of_last_read :Option<u64>,
}
impl<T: Read + Seek> OggStreamReader<T> {
pub fn new(rdr :T) ->
Result<OggStreamReader<T>, VorbisError> {
OggStreamReader::from_ogg_reader(PacketReader::new(rdr))
}
pub fn from_ogg_reader(mut rdr :PacketReader<T>) ->
Result<OggStreamReader<T>, VorbisError> {
let ((ident_hdr, comment_hdr, setup_hdr), stream_serial) =
try!(read_headers(&mut rdr));
return Ok(OggStreamReader {
rdr,
pwr : PreviousWindowRight::new(),
ident_hdr,
comment_hdr,
setup_hdr,
stream_serial,
absgp_of_last_read : None,
});
}
pub fn into_inner(self) -> PacketReader<T> {
self.rdr
}
fn read_next_audio_packet(&mut self) -> Result<Option<Packet>, VorbisError> {
let pck = match try!(self.rdr.read_packet()) {
Some(p) => p,
None => return Ok(None),
};
if pck.stream_serial != self.stream_serial {
if pck.first_packet {
let ident_hdr = try!(read_header_ident(&pck.data));
let pck :Packet = try!(self.rdr.read_packet_expected());
let comment_hdr = try!(read_header_comment(&pck.data));
let pck :Packet = try!(self.rdr.read_packet_expected());
let setup_hdr = try!(read_header_setup(&pck.data, ident_hdr.audio_channels,
(ident_hdr.blocksize_0, ident_hdr.blocksize_1)));
self.pwr = PreviousWindowRight::new();
self.ident_hdr = ident_hdr;
self.comment_hdr = comment_hdr;
self.setup_hdr = setup_hdr;
self.stream_serial = pck.stream_serial;
self.absgp_of_last_read = None;
let pck = match try!(self.rdr.read_packet()) {
Some(p) => p,
None => return Ok(None),
};
let _decoded_pck = try!(read_audio_packet(&self.ident_hdr,
&self.setup_hdr, &pck.data, &mut self.pwr));
self.absgp_of_last_read = Some(pck.absgp_page);
return Ok(try!(self.rdr.read_packet()));
} else {
return Ok(Some(pck));
}
} else {
return Ok(Some(pck));
}
}
pub fn read_dec_packet(&mut self) ->
Result<Option<Vec<Vec<i16>>>, VorbisError> {
let pck = match try!(self.read_next_audio_packet()) {
Some(p) => p,
None => return Ok(None),
};
let decoded_pck = try!(read_audio_packet(&self.ident_hdr,
&self.setup_hdr, &pck.data, &mut self.pwr));
self.absgp_of_last_read = Some(pck.absgp_page);
return Ok(Some(decoded_pck));
}
pub fn read_dec_packet_itl(&mut self) ->
Result<Option<Vec<i16>>, VorbisError> {
let decoded_pck = match try!(self.read_dec_packet()) {
Some(p) => p,
None => return Ok(None),
};
let channel_count = decoded_pck.len();
let samples_interleaved = if channel_count == 1 {
decoded_pck.into_iter().next().unwrap()
} else {
let len = decoded_pck[0].len();
let mut samples = Vec::with_capacity(len * channel_count);
for i in 0 .. len {
for ref chan in decoded_pck.iter() {
samples.push(chan[i]);
}
}
samples
};
return Ok(Some(samples_interleaved));
}
pub fn get_last_absgp(&self) -> Option<u64> {
self.absgp_of_last_read
}
pub fn seek_absgp_pg(&mut self, absgp :u64) -> Result<(), VorbisError> {
try!(self.rdr.seek_absgp(None, absgp));
self.absgp_of_last_read = None;
self.pwr = PreviousWindowRight::new();
Ok(())
}
}
#[cfg(feature = "async_ogg")]
pub mod async_api {
use super::*;
use ogg::OggReadError;
use ogg::reading::async_api::PacketReader;
use futures::stream::Stream;
use tokio_io::AsyncRead;
use futures::{Async, Future, Poll};
use std::io::{Error, ErrorKind};
use std::mem::replace;
pub struct HeadersReader<T: AsyncRead> {
pck_rd :PacketReader<T>,
ident_hdr :Option<IdentHeader>,
comment_hdr :Option<CommentHeader>,
}
impl<T: AsyncRead> HeadersReader<T> {
pub fn new(inner :T) -> Self {
HeadersReader::from_packet_reader(PacketReader::new(inner))
}
pub fn from_packet_reader(pck_rd :PacketReader<T>) -> Self {
HeadersReader {
pck_rd,
ident_hdr : None,
comment_hdr : None,
}
}
}
impl<T: AsyncRead> Future for HeadersReader<T> {
type Item = HeaderSet;
type Error = VorbisError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
macro_rules! rd_pck {
() => {
if let Some(pck) = try_ready!(self.pck_rd.poll()) {
pck
} else {
try!(Err(OggReadError::ReadError(Error::new(ErrorKind::UnexpectedEof,
"Expected header packet but found end of stream"))))
}
}
}
if self.ident_hdr.is_none() {
let pck = rd_pck!();
self.ident_hdr = Some(try!(read_header_ident(&pck.data)));
}
if self.comment_hdr.is_none() {
let pck = rd_pck!();
self.comment_hdr = Some(try!(read_header_comment(&pck.data)));
}
let setup_hdr = {
let ident = self.ident_hdr.as_ref().unwrap();
let pck = rd_pck!();
try!(read_header_setup(&pck.data,
ident.audio_channels, (ident.blocksize_0, ident.blocksize_1)))
};
let ident_hdr = replace(&mut self.ident_hdr, None).unwrap();
let comment_hdr = replace(&mut self.comment_hdr, None).unwrap();
Ok(Async::Ready(((ident_hdr, comment_hdr, setup_hdr))))
}
}
pub struct OggStreamReader<T :AsyncRead> {
pck_rd :PacketReader<T>,
pwr :PreviousWindowRight,
pub ident_hdr :IdentHeader,
pub comment_hdr :CommentHeader,
pub setup_hdr :SetupHeader,
absgp_of_last_read :Option<u64>,
}
impl<T :AsyncRead> OggStreamReader<T> {
pub fn new(hdr_rdr :HeadersReader<T>, hdrs :HeaderSet) -> Self {
OggStreamReader::from_pck_rdr(hdr_rdr.pck_rd, hdrs)
}
pub fn from_pck_rdr(pck_rd :PacketReader<T>, hdrs :HeaderSet) -> Self {
OggStreamReader {
pck_rd,
pwr : PreviousWindowRight::new(),
ident_hdr : hdrs.0,
comment_hdr : hdrs.1,
setup_hdr : hdrs.2,
absgp_of_last_read : None,
}
}
}
impl<T :AsyncRead> Stream for OggStreamReader<T> {
type Item = Vec<Vec<i16>>;
type Error = VorbisError;
fn poll(&mut self) -> Poll<Option<Vec<Vec<i16>>>, VorbisError> {
let pck = match try_ready!(self.pck_rd.poll()) {
Some(p) => p,
None => return Ok(Async::Ready(None)),
};
let decoded_pck = try!(read_audio_packet(&self.ident_hdr,
&self.setup_hdr, &pck.data, &mut self.pwr));
self.absgp_of_last_read = Some(pck.absgp_page);
Ok(Async::Ready(Some(decoded_pck)))
}
}
}