-
Notifications
You must be signed in to change notification settings - Fork 26
Description
Hi!
I've been using oboe-rs in my program, and for phones > 8.0 (i.e. anything that uses AAudio) it seems to be a frequent source of crashes. I finally tested the aaudio paths on my own phone (8.0, the first buggy version that can use AAudio) and it looks like
- When I plug or unplug headphones, the stream disconnects (throws a DISCONNECTED error)
- After this happens, audio stops playing AND the next time I call request_stop I get a CLOSED error.
(If it makes a difference, my source code is here).
Reading the AAudio docs, it looks like I'm supposed to cope with this case by (opening a new thread and) creating a brand new audio stream. My audio callback owns a buffer which another thread writes to, so I can't easily construct a new callback; I think I need to create a new stream which reuses the callback from the disconnected one. My problem is, these rust bindings are getting in the way of this.
- I can't directly pass the callback into a new
AudioStreamBuilder, because the builder takes ownership of it. - I can't reuse the
AudioStreamBuilderto make a new stream, becauseAudioStreamBuilderAsync.open_streamtakesselfinstead of&selfand consumes the builder.
I think that both of these are issues with the rust typings.
The documentation for set_callback says that the API intentionally doesn't take a smart pointer, but is not meant to own the callback:
The caller should retain ownership of the object streamCallback points to. At first glance weak_ptr may seem like a good candidate for streamCallback as this implies temporary ownership. However, a weak_ptr can only be created from a shared_ptr. A shared_ptr incurs some performance overhead. The callback object is likely to be accessed every few milliseconds when the stream requires new data so this overhead is something we want to avoid.
This leaves a raw pointer as the logical type choice. The only caveat being that the caller must not destroy the callback before the stream has been closed.
The AAudio docs say that
You can save the builder and reuse it in the future to make more streams.
Do you have any guidance for how to handle this case? I can't find any sample usages of oboe-rs that deal with disconnected streams or non-trivial callbacks, and I can't follow the C++ examples because of these typings. I'm an amateur in programming audio, rust, and Android, so I suspect I'm doing something wrong, but can't find any good examples to copy from.