summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-06-29 18:51:53 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-07-01 17:16:13 +0200
commit7a5e10b8a93c0d38d114b2683460c150c1b43229 (patch)
tree93e67807502de29d555c7422954c9d14dee4072f /platform
parent1a637b07b11c2a205eeb5936b8f020d36d8b07a9 (diff)
Use dummy driver when JS AudioContext is unavailable.
Diffstat (limited to 'platform')
-rw-r--r--platform/javascript/audio_driver_javascript.cpp20
-rw-r--r--platform/javascript/audio_driver_javascript.h7
-rw-r--r--platform/javascript/javascript_main.cpp9
-rw-r--r--platform/javascript/os_javascript.cpp17
-rw-r--r--platform/javascript/os_javascript.h2
5 files changed, 40 insertions, 15 deletions
diff --git a/platform/javascript/audio_driver_javascript.cpp b/platform/javascript/audio_driver_javascript.cpp
index b8914414e6..9604914b2c 100644
--- a/platform/javascript/audio_driver_javascript.cpp
+++ b/platform/javascript/audio_driver_javascript.cpp
@@ -36,6 +36,15 @@
AudioDriverJavaScript *AudioDriverJavaScript::singleton = nullptr;
+bool AudioDriverJavaScript::is_available() {
+ return EM_ASM_INT({
+ if (!(window.AudioContext || window.webkitAudioContext)) {
+ return 0;
+ }
+ return 1;
+ }) != 0;
+}
+
const char *AudioDriverJavaScript::get_name() const {
return "JavaScript";
}
@@ -207,12 +216,14 @@ void AudioDriverJavaScript::finish_async() {
/* clang-format off */
EM_ASM({
- var ref = Module.IDHandler.get($0);
+ const id = $0;
+ var ref = Module.IDHandler.get(id);
Module.async_finish.push(new Promise(function(accept, reject) {
if (!ref) {
- console.log("Ref not found!", $0, Module.IDHandler);
+ console.log("Ref not found!", id, Module.IDHandler);
setTimeout(accept, 0);
} else {
+ Module.IDHandler.remove(id);
const context = ref['context'];
// Disconnect script and input.
ref['script'].disconnect();
@@ -226,7 +237,6 @@ void AudioDriverJavaScript::finish_async() {
});
}
}));
- Module.IDHandler.remove($0);
}, id);
/* clang-format on */
}
@@ -293,9 +303,5 @@ Error AudioDriverJavaScript::capture_stop() {
}
AudioDriverJavaScript::AudioDriverJavaScript() {
- _driver_id = 0;
- internal_buffer = nullptr;
- buffer_length = 0;
-
singleton = this;
}
diff --git a/platform/javascript/audio_driver_javascript.h b/platform/javascript/audio_driver_javascript.h
index 9b26be001e..f029a91db0 100644
--- a/platform/javascript/audio_driver_javascript.h
+++ b/platform/javascript/audio_driver_javascript.h
@@ -34,12 +34,13 @@
#include "servers/audio_server.h"
class AudioDriverJavaScript : public AudioDriver {
- float *internal_buffer;
+ float *internal_buffer = nullptr;
- int _driver_id;
- int buffer_length;
+ int _driver_id = 0;
+ int buffer_length = 0;
public:
+ static bool is_available();
void mix_to_js();
void process_capture(float sample);
diff --git a/platform/javascript/javascript_main.cpp b/platform/javascript/javascript_main.cpp
index 8b7bd2170f..99672745e7 100644
--- a/platform/javascript/javascript_main.cpp
+++ b/platform/javascript/javascript_main.cpp
@@ -63,10 +63,17 @@ void main_loop_callback() {
}
if (os->main_loop_iterate()) {
emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
+ /* clang-format off */
EM_ASM({
// This will contain the list of operations that need to complete before cleanup.
- Module.async_finish = [];
+ Module.async_finish = [
+ // Always contains at least one async promise, to avoid firing immediately if nothing is added.
+ new Promise(function(accept, reject) {
+ setTimeout(accept, 0);
+ })
+ ];
});
+ /* clang-format on */
os->get_main_loop()->finish();
os->finalize_async(); // Will add all the async finish functions.
EM_ASM({
diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp
index 84cc5c1fe0..1ff4304bcf 100644
--- a/platform/javascript/os_javascript.cpp
+++ b/platform/javascript/os_javascript.cpp
@@ -77,7 +77,9 @@ void OS_JavaScript::initialize() {
}
void OS_JavaScript::resume_audio() {
- audio_driver_javascript.resume();
+ if (audio_driver_javascript) {
+ audio_driver_javascript->resume();
+ }
}
void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) {
@@ -125,11 +127,17 @@ void OS_JavaScript::delete_main_loop() {
void OS_JavaScript::finalize_async() {
finalizing = true;
- audio_driver_javascript.finish_async();
+ if (audio_driver_javascript) {
+ audio_driver_javascript->finish_async();
+ }
}
void OS_JavaScript::finalize() {
delete_main_loop();
+ if (audio_driver_javascript) {
+ memdelete(audio_driver_javascript);
+ audio_driver_javascript = nullptr;
+ }
}
// Miscellaneous
@@ -238,7 +246,10 @@ void OS_JavaScript::initialize_joypads() {
}
OS_JavaScript::OS_JavaScript() {
- AudioDriverManager::add_driver(&audio_driver_javascript);
+ if (AudioDriverJavaScript::is_available()) {
+ audio_driver_javascript = memnew(AudioDriverJavaScript);
+ AudioDriverManager::add_driver(audio_driver_javascript);
+ }
Vector<Logger *> loggers;
loggers.push_back(memnew(StdLogger));
diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h
index dddeef836c..22234f9355 100644
--- a/platform/javascript/os_javascript.h
+++ b/platform/javascript/os_javascript.h
@@ -40,7 +40,7 @@
class OS_JavaScript : public OS_Unix {
MainLoop *main_loop = nullptr;
- AudioDriverJavaScript audio_driver_javascript;
+ AudioDriverJavaScript *audio_driver_javascript = nullptr;
bool finalizing = false;
bool idb_available = false;