Repository navigation
feat: read decoded audio in chunks with openAudioDecodeStream #1446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dff8d7b
12f88f5
6b7ff68
e036602
9696360
27ccd32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| #include <audioapi/HostObjects/sources/AudioBufferHostObject.h> | ||
| #include <audioapi/HostObjects/utils/AudioDecodeStreamHostObject.h> | ||
| #include <audioapi/jsi/JsiPromise.h> | ||
|
|
||
| #include <jsi/jsi.h> | ||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <thread> | ||
| #include <utility> | ||
|
|
||
| namespace audioapi { | ||
|
|
||
| namespace { | ||
|
|
||
| /// @brief A lane that is destroyed on a thread of its own. Destroying a `SerialTaskExecutor` | ||
| /// waits for the tasks still queued, and its last owner can be the garbage collector: the | ||
| /// host object, or a promise executor function JS has not collected yet. | ||
| std::shared_ptr<SerialTaskExecutor> makeLane() { | ||
| return {new SerialTaskExecutor(), [](SerialTaskExecutor *lane) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not block the JS thread when the stream lane is full.
Submit work without blocking. Keep an ordered pending queue on the JS side that feeds the lane as it drains, or reject calls beyond a bound. Keep
|
||
| std::thread([lane]() { delete lane; }).detach(); | ||
| }}; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| AudioDecodeStreamHostObject::AudioDecodeStreamHostObject( | ||
| jsi::Runtime *runtime, | ||
| const std::shared_ptr<react::CallInvoker> &callInvoker, | ||
| std::shared_ptr<audiodecoding::AudioDecodeStream> stream) | ||
| : stream_(std::move(stream)), | ||
| lane_(makeLane()), | ||
| promiseVendor_(std::make_shared<PromiseVendor>(runtime, callInvoker, lane_)) { | ||
| addGetters( | ||
| JSI_EXPORT_PROPERTY_GETTER(AudioDecodeStreamHostObject, sampleRate), | ||
| JSI_EXPORT_PROPERTY_GETTER(AudioDecodeStreamHostObject, numberOfChannels), | ||
| JSI_EXPORT_PROPERTY_GETTER(AudioDecodeStreamHostObject, duration)); | ||
| addFunctions( | ||
| JSI_EXPORT_FUNCTION(AudioDecodeStreamHostObject, read), | ||
| JSI_EXPORT_FUNCTION(AudioDecodeStreamHostObject, seek), | ||
| JSI_EXPORT_FUNCTION(AudioDecodeStreamHostObject, close)); | ||
| } | ||
|
|
||
| AudioDecodeStreamHostObject::~AudioDecodeStreamHostObject() { | ||
| // Dropping the last reference to the stream closes its decoder, which can take a codec | ||
| // teardown: not inside the garbage collector. Reads still queued hold references of their | ||
| // own and release them on the lane. | ||
| std::thread([stream = std::move(stream_)]() mutable { stream.reset(); }).detach(); | ||
| } | ||
|
|
||
| jsi::Object AudioDecodeStreamHostObject::createJsObject( | ||
| jsi::Runtime &runtime, | ||
| const std::shared_ptr<AudioDecodeStreamHostObject> &hostObject) { | ||
| auto object = jsi::Object::createFromHostObject(runtime, hostObject); | ||
| object.setExternalMemoryPressure( | ||
| runtime, OPEN_DECODER_MEMORY_PRESSURE + hostObject->stream_->encodedSizeInBytes()); | ||
| return object; | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| PromiseResolver rejectWithMessage(std::string message) { | ||
| return [message = std::move(message)](jsi::Runtime &) { | ||
| return message; | ||
| }; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| JSI_PROPERTY_GETTER_IMPL(AudioDecodeStreamHostObject, sampleRate) { | ||
| return {stream_->sampleRate()}; | ||
| } | ||
|
|
||
| JSI_PROPERTY_GETTER_IMPL(AudioDecodeStreamHostObject, numberOfChannels) { | ||
| return {stream_->numberOfChannels()}; | ||
| } | ||
|
|
||
| JSI_PROPERTY_GETTER_IMPL(AudioDecodeStreamHostObject, duration) { | ||
| return {stream_->duration()}; | ||
| } | ||
|
|
||
| // Scheduling on the lane waits when its queue is full, and these run on the JS thread. The | ||
| // TypeScript `AudioDecodeStream` forwards one operation at a time, so the queue never holds | ||
| // more than that operation and a `close`. | ||
| JSI_HOST_FUNCTION_IMPL(AudioDecodeStreamHostObject, read) { | ||
| if (promiseVendor_ == nullptr) { | ||
| throw jsi::JSError(runtime, "Cannot read from a closed decode stream"); | ||
| } | ||
|
|
||
| // Zero, which the stream rejects, for anything that is not a positive frame count. | ||
| const double requested = args[0].getNumber(); | ||
| const auto frameCount = | ||
| requested >= 1.0 ? static_cast<size_t>(std::min(requested, MAX_FRAME_COUNT)) : size_t{0}; | ||
|
|
||
| return promiseVendor_->createAsyncPromise([stream = stream_, frameCount]() -> PromiseResolver { | ||
| auto result = stream->read(frameCount); | ||
| if (result.is_err()) { | ||
| return rejectWithMessage(result.unwrap_err()); | ||
| } | ||
|
|
||
| return [buffer = result.unwrap()](jsi::Runtime &runtime) -> jsi::Value { | ||
| if (buffer == nullptr) { | ||
| return jsi::Value::null(); | ||
| } | ||
| return AudioBufferHostObject::createJsObject(runtime, buffer); | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| JSI_HOST_FUNCTION_IMPL(AudioDecodeStreamHostObject, seek) { | ||
| if (promiseVendor_ == nullptr) { | ||
| throw jsi::JSError(runtime, "Cannot seek in a closed decode stream"); | ||
| } | ||
|
|
||
| const auto seconds = args[0].getNumber(); | ||
|
|
||
| return promiseVendor_->createAsyncPromise([stream = stream_, seconds]() -> PromiseResolver { | ||
| auto result = stream->seek(seconds); | ||
| if (result.is_err()) { | ||
| return rejectWithMessage(result.unwrap_err()); | ||
| } | ||
|
|
||
| return [](jsi::Runtime &) { | ||
| return jsi::Value::undefined(); | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| JSI_HOST_FUNCTION_IMPL(AudioDecodeStreamHostObject, close) { | ||
| if (lane_ == nullptr) { | ||
| return jsi::Value::undefined(); | ||
| } | ||
|
|
||
| lane_->schedule([stream = stream_]() { stream->close(); }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Release the worker when the stream closes.
Retire the executor asynchronously after the queued close completes. Preserve rejection of later reads and seeks without creating another worker. As per coding guidelines, a
|
||
| // Nothing runs on the lane after a close, so it is let go now rather than when the | ||
| // collector reaches this object: a closed stream does not keep a worker thread. Its deleter | ||
| // lets the close run, then joins the worker on a thread of its own. | ||
| promiseVendor_.reset(); | ||
| lane_.reset(); | ||
| return jsi::Value::undefined(); | ||
| } | ||
|
|
||
| } // namespace audioapi | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #pragma once | ||
|
|
||
| #include <audioapi/decoding/AudioDecodeStream.h> | ||
| #include <audioapi/jsi/HostObject.h> | ||
| #include <audioapi/jsi/JsiPromise.h> | ||
| #include <audioapi/utils/Macros.h> | ||
| #include <audioapi/utils/SerialTaskExecutor.hpp> | ||
|
|
||
| #include <jsi/jsi.h> | ||
| #include <cstddef> | ||
| #include <memory> | ||
|
|
||
| namespace audioapi { | ||
| using namespace facebook; | ||
|
|
||
| /// @brief JS handle on an open `AudioDecodeStream`. | ||
| /// @note `read`, `seek` and `close` run on a lane owned by this object, in the order JS called | ||
| /// them: the stream is never touched by two threads at once, and a `close` waits for the reads | ||
| /// requested before it. `close` gives the lane up: `read` and `seek` throw afterwards. | ||
| class AudioDecodeStreamHostObject : public HostObject { | ||
| public: | ||
| explicit AudioDecodeStreamHostObject( | ||
| jsi::Runtime *runtime, | ||
| const std::shared_ptr<react::CallInvoker> &callInvoker, | ||
| std::shared_ptr<audiodecoding::AudioDecodeStream> stream); | ||
| /// @note Runs inside the garbage collector, so it waits for nothing: the lane and the | ||
| /// decoder are released on threads of their own. | ||
| ~AudioDecodeStreamHostObject() override; | ||
| DELETE_COPY_AND_MOVE(AudioDecodeStreamHostObject); | ||
|
|
||
| /// The JS object for @p hostObject, reporting what the open stream holds as external memory | ||
| /// pressure, so a stream left open weighs on the collector. | ||
| static jsi::Object createJsObject( | ||
| jsi::Runtime &runtime, | ||
| const std::shared_ptr<AudioDecodeStreamHostObject> &hostObject); | ||
|
|
||
| JSI_PROPERTY_GETTER_DECL(sampleRate); | ||
| JSI_PROPERTY_GETTER_DECL(numberOfChannels); | ||
| JSI_PROPERTY_GETTER_DECL(duration); | ||
|
|
||
| JSI_HOST_FUNCTION_DECL(read); | ||
| JSI_HOST_FUNCTION_DECL(seek); | ||
| JSI_HOST_FUNCTION_DECL(close); | ||
|
|
||
| private: | ||
| /// An open decoder holds a codec, a file descriptor and a thread, which no byte count | ||
| /// reflects: a nominal amount stands for them. | ||
| static constexpr size_t OPEN_DECODER_MEMORY_PRESSURE = 1024 * 1024; | ||
| /// Frame counts above this are requests for "everything left". Clamping them keeps the | ||
| /// conversion from a JS number defined on every ABI. | ||
| static constexpr double MAX_FRAME_COUNT = 2147483647.0; | ||
|
|
||
| std::shared_ptr<audiodecoding::AudioDecodeStream> stream_; | ||
| std::shared_ptr<SerialTaskExecutor> lane_; | ||
| std::shared_ptr<PromiseVendor> promiseVendor_; | ||
| }; | ||
| } // namespace audioapi |
Uh oh!
There was an error while loading. Please reload this page.