freesig / cpal

Audio player in pure Rust
0 stars 0 forks source link

Stream index #20

Closed freesig closed 5 years ago

freesig commented 5 years ago

Problem

Each AsioStream is kept in a Acr<Mutex<Vec<Option<AsioStream>>>> and to get the stream you use the StreamId.0 - 1 because StreamId is created from incrementing stream_count = Arc::new(AtomicUsize::new(0)) . Note this count is never decremented even if a stream is destroyed. The stream that is destroyed is simply set to None. (Although theres nothing enforcing this The - 1 is because it starts off at 0 and the first stream is 1 but the index in the array is actually 0 for the first stream etc. This is pretty error prone.

Ideas

  1. Use a HashMap<StreamId, AsioStream> instead of the Vec. This would be a bit cleaner and be valid as long as the stream_count is never decremented. Downside is having to hash the StreamId inside the audio callback. I'm pretty sure hashing is one of the things to avoid inside audio stream callbacks.
  2. Change the stream_count to Arc<Option<AtomicUsize>> so that the first stream can check for None and actually be 0. Come to think of it the AtomicUsize shouldn't be in an Arc but will have to be if we use an Option.

I feel like there's a better solution that I haven't thought of