Repository navigation
Expand file tree
/
Copy pathsymphonia.rs
More file actions
420 lines (375 loc) · 15.3 KB
/
Copy pathsymphonia.rs
File metadata and controls
420 lines (375 loc) · 15.3 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use core::time::Duration;
use std::{
fmt::{self, Debug},
sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard},
};
use symphonia::{
core::{
audio::{AudioBufferRef, SampleBuffer, SignalSpec},
codecs::{CodecRegistry, Decoder, DecoderOptions, CODEC_TYPE_NULL},
errors::Error,
formats::{FormatOptions, FormatReader, SeekMode, SeekTo, SeekedTo},
io::MediaSourceStream,
meta::MetadataOptions,
probe::Hint,
units,
},
default::get_probe,
};
use super::{DecoderError, Settings};
use crate::{
common::{assert_error_traits, ChannelCount, Sample, SampleRate},
source::{self, padding_samples_needed},
Source,
};
use dasp_sample::Sample as _;
#[derive(Clone)]
pub(crate) struct Registry(Arc<RwLock<CodecRegistry>>);
impl Debug for Registry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Registry")
}
}
impl Registry {
pub(crate) fn new(registry: CodecRegistry) -> Self {
Self(Arc::new(RwLock::new(registry)))
}
pub(crate) fn write(&self) -> RwLockWriteGuard<'_, CodecRegistry> {
self.0.write().unwrap()
}
pub(crate) fn read(&self) -> RwLockReadGuard<'_, CodecRegistry> {
self.0.read().unwrap()
}
}
pub(crate) struct SymphoniaDecoder {
decoder: Box<dyn Decoder>,
current_span_offset: usize,
format: Box<dyn FormatReader>,
total_duration: Option<Duration>,
buffer: SampleBuffer<Sample>,
spec: SignalSpec,
seek_mode: SeekMode,
selected_track_id: u32,
samples_in_current_frame: usize,
silence_samples_remaining: usize,
}
impl SymphoniaDecoder {
pub(crate) fn new(mss: MediaSourceStream, settings: &Settings) -> Result<Self, DecoderError> {
match SymphoniaDecoder::init(mss, settings) {
Err(e) => match e {
Error::IoError(e) => Err(DecoderError::IoError(e.to_string())),
Error::DecodeError(e) => Err(DecoderError::DecodeError(e)),
Error::SeekError(_) => {
unreachable!("Seek errors should not occur during initialization")
}
Error::Unsupported(_) => Err(DecoderError::UnrecognizedFormat),
Error::LimitError(e) => Err(DecoderError::LimitError(e)),
Error::ResetRequired => Err(DecoderError::ResetRequired),
},
Ok(Some(decoder)) => Ok(decoder),
Ok(None) => Err(DecoderError::NoStreams),
}
}
#[inline]
pub(crate) fn into_inner(self) -> MediaSourceStream {
self.format.into_inner()
}
fn init(
mss: MediaSourceStream,
settings: &Settings,
) -> symphonia::core::errors::Result<Option<SymphoniaDecoder>> {
let mut hint = Hint::new();
if let Some(ext) = settings.hint.as_ref() {
hint.with_extension(ext);
}
if let Some(typ) = settings.mime_type.as_ref() {
hint.mime_type(typ);
}
let format_opts: FormatOptions = FormatOptions {
enable_gapless: settings.gapless,
..Default::default()
};
let metadata_opts: MetadataOptions = Default::default();
let seek_mode = if settings.coarse_seek {
SeekMode::Coarse
} else {
SeekMode::Accurate
};
let mut probed = get_probe().format(&hint, mss, &format_opts, &metadata_opts)?;
let stream = match probed.format.default_track() {
Some(stream) => stream,
None => return Ok(None),
};
// Select the first supported track
let track = probed
.format
.tracks()
.iter()
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
.ok_or(symphonia::core::errors::Error::Unsupported(
"No track with supported codec",
))?;
let track_id = track.id;
let mut decoder = settings
.codec_registry
.read()
.make(&track.codec_params, &DecoderOptions::default())?;
let total_duration = track
.codec_params
.time_base
.zip(stream.codec_params.n_frames)
.map(|(base, spans)| base.calc_time(spans).into())
.filter(|d: &Duration| !d.is_zero());
let decoded = loop {
let current_span = match probed.format.next_packet() {
Ok(packet) => packet,
Err(Error::IoError(_)) => break decoder.last_decoded(),
Err(e) => return Err(e),
};
// If the packet does not belong to the selected track, skip over it
if current_span.track_id() != track_id {
continue;
}
let decoded = match decoder.decode(¤t_span) {
Ok(decoded) => decoded,
Err(e) => match e {
Error::DecodeError(_) => {
// Decode errors are intentionally ignored with no retry limit.
// This behavior ensures that the decoder skips over problematic packets
// and continues processing the rest of the stream.
continue;
}
_ => return Err(e),
},
};
// Loop until we get a packet with audio frames. This is necessary because some
// formats can have packets with only metadata, particularly when rewinding, in
// which case the iterator would otherwise end with `None`.
// Note: checking `decoded.frames()` is more reliable than `packet.dur()`, which
// can resturn non-zero durations for packets without audio frames.
if decoded.frames() > 0 {
break decoded;
}
};
let spec = decoded.spec().to_owned();
let buffer = SymphoniaDecoder::get_buffer(decoded, &spec);
Ok(Some(SymphoniaDecoder {
decoder,
current_span_offset: 0,
format: probed.format,
total_duration,
buffer,
spec,
seek_mode,
selected_track_id: track_id,
samples_in_current_frame: 0,
silence_samples_remaining: 0,
}))
}
#[inline]
fn get_buffer(decoded: AudioBufferRef, spec: &SignalSpec) -> SampleBuffer<Sample> {
let duration = units::Duration::from(decoded.capacity() as u64);
let mut buffer = SampleBuffer::<Sample>::new(duration, *spec);
buffer.copy_interleaved_ref(decoded);
buffer
}
}
impl Source for SymphoniaDecoder {
#[inline]
fn current_span_len(&self) -> Option<usize> {
Some(self.buffer.len())
}
#[inline]
fn channels(&self) -> ChannelCount {
ChannelCount::new(
self.spec
.channels
.count()
.try_into()
.expect("rodio only support up to u16::MAX channels (65_535)"),
)
.expect("audio should always have at least one channel")
}
#[inline]
fn sample_rate(&self) -> SampleRate {
SampleRate::new(self.spec.rate).expect("audio should always have a non zero SampleRate")
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
self.total_duration
}
fn try_seek(&mut self, pos: Duration) -> Result<(), source::SeekError> {
if matches!(self.seek_mode, SeekMode::Accurate)
&& self.decoder.codec_params().time_base.is_none()
{
return Err(source::SeekError::SymphoniaDecoder(
SeekError::AccurateSeekNotSupported,
));
}
// Seeking should be "saturating", meaning: target positions beyond the end of the stream
// are clamped to the end.
let mut target = pos;
if let Some(total_duration) = self.total_duration {
if target > total_duration {
target = total_duration;
}
}
// Remember the current channel, so we can restore it after seeking.
let active_channel = self.current_span_offset % self.channels().get() as usize;
let seek_res = match self.format.seek(
self.seek_mode,
SeekTo::Time {
time: target.into(),
track_id: None,
},
) {
Err(Error::SeekError(symphonia::core::errors::SeekErrorKind::ForwardOnly)) => {
return Err(source::SeekError::SymphoniaDecoder(
SeekError::RandomAccessNotSupported,
));
}
other => other.map_err(Arc::new).map_err(SeekError::Demuxer),
}?;
// Seeking is a demuxer operation without the decoder knowing about it,
// so we need to reset the decoder to make sure it's in sync and prevent
// audio glitches.
self.decoder.reset();
// Force the iterator to decode the next packet.
self.current_span_offset = usize::MAX;
// Symphonia does not seek to the exact position, it seeks to the closest keyframe.
// If accurate seeking is required, fast-forward to the exact position.
if matches!(self.seek_mode, SeekMode::Accurate) {
self.refine_position(seek_res)?;
}
// After seeking, we are at the beginning of an inter-sample frame, i.e. the first
// channel. We need to advance the iterator to the right channel.
for _ in 0..active_channel {
self.next();
}
Ok(())
}
}
/// Error returned when the try_seek implementation of the symphonia decoder fails.
#[derive(Debug, thiserror::Error, Clone)]
pub enum SeekError {
/// Accurate seeking is not supported
///
/// This error occurs when the decoder cannot extract time base information from the source.
/// You may catch this error to try a coarse seek instead.
#[error("Accurate seeking is not supported on this file/byte stream that lacks time base information")]
AccurateSeekNotSupported,
/// The decoder does not support random access seeking
///
/// This error occurs when the source is not seekable or does not have a known byte length.
#[error("The decoder needs to know the length of the file/byte stream to be able to seek backwards. You can set that by using the `DecoderBuilder` or creating a decoder using `Decoder::try_from(some_file)`.")]
RandomAccessNotSupported,
/// Demuxer failed to seek
#[error("Demuxer failed to seek")]
Demuxer(#[source] Arc<symphonia::core::errors::Error>),
}
assert_error_traits!(SeekError);
impl SymphoniaDecoder {
/// Note span offset must be set after
fn refine_position(&mut self, seek_res: SeekedTo) -> Result<(), source::SeekError> {
// Calculate the number of samples to skip.
let mut samples_to_skip = (Duration::from(
self.decoder
.codec_params()
.time_base
.expect("time base availability guaranteed by caller")
.calc_time(seek_res.required_ts.saturating_sub(seek_res.actual_ts)),
)
.as_secs_f32()
* self.sample_rate().get() as f32
* self.channels().get() as f32)
.ceil() as usize;
// Re-align the seek position to the first channel.
samples_to_skip -= samples_to_skip % self.channels().get() as usize;
// Skip ahead to the precise position.
for _ in 0..samples_to_skip {
self.next();
}
Ok(())
}
}
impl Iterator for SymphoniaDecoder {
type Item = Sample;
fn next(&mut self) -> Option<Self::Item> {
loop {
// If padding to complete a frame, return silence
if self.silence_samples_remaining > 0 {
self.silence_samples_remaining -= 1;
return Some(Sample::EQUILIBRIUM);
}
if self.current_span_offset >= self.buffer.len() {
let decoded = loop {
let packet = match self.format.next_packet() {
Ok(packet) => {
if packet.track_id() == self.selected_track_id {
packet
} else {
continue;
}
}
Err(_) => {
// Input exhausted - check if mid-frame
let channels = self.channels();
self.silence_samples_remaining =
padding_samples_needed(self.samples_in_current_frame, channels);
if self.silence_samples_remaining > 0 {
self.samples_in_current_frame = 0;
break None;
}
return None;
}
};
let decoded = match self.decoder.decode(&packet) {
Ok(decoded) => decoded,
Err(Error::DecodeError(_)) => {
// Skip over packets that cannot be decoded. This ensures the iterator
// continues processing subsequent packets instead of terminating due to
// non-critical decode errors.
continue;
}
Err(_) => {
// Input exhausted - check if mid-frame
let channels = self.channels();
self.silence_samples_remaining =
padding_samples_needed(self.samples_in_current_frame, channels);
if self.silence_samples_remaining > 0 {
self.samples_in_current_frame = 0;
break None;
}
return None;
}
};
// Loop until we get a packet with audio frames. This is necessary because some
// formats can have packets with only metadata, particularly when rewinding, in
// which case the iterator would otherwise end with `None`.
// Note: checking `decoded.frames()` is more reliable than `packet.dur()`, which
// can resturn non-zero durations for packets without audio frames.
if decoded.frames() > 0 {
break Some(decoded);
}
};
match decoded {
Some(decoded) => {
decoded.spec().clone_into(&mut self.spec);
self.buffer = SymphoniaDecoder::get_buffer(decoded, &self.spec);
self.current_span_offset = 0;
}
None => {
// Break out happened due to exhaustion, continue to emit padding
continue;
}
}
}
let sample = *self.buffer.samples().get(self.current_span_offset)?;
self.current_span_offset += 1;
let channels = self.channels();
self.samples_in_current_frame =
(self.samples_in_current_frame + 1) % channels.get() as usize;
return Some(sample);
}
}
}