From ad72773f38debb7df18825694abc850fbf4b88fb Mon Sep 17 00:00:00 2001
From: Brian Peiris <brianpeiris@gmail.com>
Date: Fri, 6 Apr 2018 08:59:17 -0700
Subject: [PATCH] reuse naf's media stream source to avoid distorted audio in
 safari

---
 package.json                     |  2 +-
 src/components/audio-feedback.js | 22 ++++++++--------------
 yarn.lock                        |  4 ++--
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/package.json b/package.json
index 37c85f02d..797e07dc1 100644
--- a/package.json
+++ b/package.json
@@ -33,7 +33,7 @@
     "mobile-detect": "^1.4.1",
     "moving-average": "^1.0.0",
     "naf-janus-adapter": "https://github.com/mozilla/naf-janus-adapter#feature/disconnect",
-    "networked-aframe": "https://github.com/networked-aframe/networked-aframe",
+    "networked-aframe": "github:mozillareality/networked-aframe#feature/sound-source-set",
     "nipplejs": "^0.6.7",
     "query-string": "^5.0.1",
     "raven-js": "^3.20.1",
diff --git a/src/components/audio-feedback.js b/src/components/audio-feedback.js
index d4bdf5792..cc4c0741d 100644
--- a/src/components/audio-feedback.js
+++ b/src/components/audio-feedback.js
@@ -1,20 +1,14 @@
 AFRAME.registerComponent("networked-audio-analyser", {
   schema: {},
   async init() {
-    const networkedEl = await NAF.utils.getNetworkedEntity(this.el);
-    const ownerId = networkedEl.components.networked.data.owner;
-
-    const stream = await NAF.connection.adapter.getMediaStream(ownerId);
-
-    if (!stream) {
-      return;
-    }
-
-    const ctx = THREE.AudioContext.getContext();
-    const source = ctx.createMediaStreamSource(stream);
-    this.analyser = ctx.createAnalyser();
-    this.levels = new Uint8Array(this.analyser.frequencyBinCount);
-    source.connect(this.analyser);
+    this.connected = false;
+    this.el.addEventListener("sound-source-set", event => {
+      const ctx = THREE.AudioContext.getContext();
+      this.analyser = ctx.createAnalyser();
+      this.analyser.fftSize = 32;
+      this.levels = new Uint8Array(this.analyser.frequencyBinCount);
+      event.detail.soundSource.connect(this.analyser);
+    });
   },
 
   tick: function() {
diff --git a/yarn.lock b/yarn.lock
index 3700485cc..e3f42ffef 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5249,9 +5249,9 @@ neo-async@^2.5.0:
   version "2.5.0"
   resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.5.0.tgz#76b1c823130cca26acfbaccc8fbaf0a2fa33b18f"
 
-"networked-aframe@https://github.com/networked-aframe/networked-aframe":
+"networked-aframe@github:mozillareality/networked-aframe#feature/sound-source-set":
   version "0.6.0"
-  resolved "https://github.com/networked-aframe/networked-aframe#be5b2e949ec63fc44a1c6ab40b627cee5fda057e"
+  resolved "https://codeload.github.com/mozillareality/networked-aframe/tar.gz/978b7c25aff26a0c1db47beb35d8a6556f717d36"
   dependencies:
     aframe-lerp-component "^1.1.0"
     easyrtc "1.1.0"
-- 
GitLab