Skip to content

fix(audiocontext): works on both Safari and Chrome #22

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

dselman
Copy link

@dselman dselman commented Dec 18, 2024

Fix for issue: #21

I'm not sure why the original code was so complex, but this simplified version seems to work on both Safari and Chrome on Mac OS.

Possibly related to: https://developer.chrome.com/blog/autoplay/#webaudio

const map: Map<string, AudioContext> = new Map();

export async function audioContext(options?: GetAudioContextOptions,) : Promise<AudioContext>  {
    if (options?.id && map.has(options.id)) {
      const ctx = map.get(options.id);
      if (ctx) {
        return ctx;
      }
    }

    const ctx = new AudioContext(options);
    if (options?.id) {
      map.set(options.id, ctx);
    }
    return ctx;
}

Copy link

google-cla bot commented Dec 18, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@dselman
Copy link
Author

dselman commented Dec 18, 2024

Yes, this is now broken on Chrome... More investigation needed.

@dselman dselman marked this pull request as draft December 18, 2024 16:20
@dselman
Copy link
Author

dselman commented Dec 18, 2024

Interesting thread: https://stackoverflow.com/a/74495761

Signed-off-by: dselman <[email protected]>
@dselman dselman marked this pull request as ready for review January 2, 2025 17:09
@hchen2020
Copy link

I tested in Chrome and Safari, it works as expected.

@sreeshas
Copy link

sreeshas commented Mar 7, 2025

I tested in Chrome and Safari, it works as expected.

did you test it on ios?

@sreeshas
Copy link

sreeshas commented Mar 7, 2025

existing audiocontext does not work on safari on ios. it also does not work on chrome on ios sometimes. audiocontext is getting suspended.

[Log] Creating new AudioStreamer...
[Log] Creating new AudioStreamer...
[Log] Disconnect called but no AudioStreamer exists
[Log] New AudioContext created with state: suspended
[Log] AudioStreamer instantiated
[Log] New AudioContext created with state: suspended
[Log] AudioStreamer instantiated
[Log] Volume meter worklet added successfully (x2)
[Log] Pre-initialized back camera
[Log] Joined meeting successfully
[Log] Connecting with config: – Object
Object
[Log] Before connect - AudioStreamer exists, AudioContext state: suspended
[Log] Camera list: – Array (7)
Array (7)
[Log] Found back camera: – "Back Camera"
[Log] After connect - AudioStreamer exists, AudioContext state: suspended
[Log] Camera list: – Array (0)
Array (0)
[Log] No back camera found in devices list
[Log] Camera list: – Array (0)
Array (0)
[Log] No back camera found in devices list
[Log] Received audio data: 9600 bytes, AudioContext state: suspended
[Log] Received audio data: 11520 bytes, AudioContext state: suspended

[Log] Received audio data: 11520 bytes, AudioContext state: running (x4)
[Log] Received audio data: 7680 bytes, AudioContext state: running
[Log] Received audio data: 9600 bytes, AudioContext state: running (x2)
[Log] Received audio data: 11520 bytes, AudioContext state: running (x10)
[Log] Received audio data: 5760 bytes, AudioContext state: running
[Log] Received audio data: 9600 bytes, AudioContext state: running

another instance
[Warning] Received audio data but AudioStreamer is not initialized (x21)

another instance
[Log] Creating new AudioStreamer...
[Log] Creating new AudioStreamer...
[Log] Disconnect called but no AudioStreamer exists
[Log] New AudioContext created with state: suspended
[Log] AudioStreamer instantiated
[Log] New AudioContext created with state: suspended
[Log] AudioStreamer instantiated
[Log] Volume meter worklet added successfully (x2)
[Log] Pre-initialized back camera
[Log] Joined meeting successfully
[Log] Connecting with config: – {model: "models/gemini-2.0-flash-exp", systemInstruction: Object, tools: Array, …}
{model: "models/gemini-2.0-flash-exp", systemInstruction: Object, tools: Array, generationConfig: Object}Object
[Log] Before connect - AudioStreamer exists, AudioContext state: suspended
[Log] Camera list: – [{label: "Back Camera", id: "EA0A4AF8B4997F9EF352DD8F5821EA0215A35E55", facingMode: undefined}, {label: "Back Dual Camera", id: "A291BFDDFED980CB607BAE8AAAE7593C17B1ED96", facingMode: undefined}, {label: "Back Dual Wide Camera", id: "71F94A26BAA1F4B0E597AF7F4F37E7A55BDFA16B", facingMode: undefined}, …] (7)

[Log] Found back camera: – "Back Camera"
[Log] After connect - AudioStreamer exists, AudioContext state: suspended
[Log] Camera list: – [] (0)
[Log] No back camera found in devices list
[Log] Camera list: – [] (0)
[Log] No back camera found in devices list
[Log] Received audio data: 9600 bytes, AudioContext state: suspended
[Log] Received audio data: 11520 bytes, AudioContext state: suspended (x2)
[Log] Received audio data: 11520 bytes, AudioContext state: running (x2)
[Log] Received audio data: 1920 bytes, AudioContext state: running
[Log] Received audio data: 9600 bytes, AudioContext state: running (x2)
[Log] Received audio data: 11520 bytes, AudioContext state: running (x9)
[Log] Received audio data: 9600 bytes, AudioContext state: running

@hchen2020
Copy link

I tested in Chrome and Safari, it works as expected.

did you test it on ios?

Yes, it works on Iphone iOS safari.

@thijssmudde
Copy link

I tested in Chrome and Safari, it works as expected.

did you test it on ios?

Yes, it works on Iphone iOS safari.

I tested on Safari and Chrome from an iPhone as well and I have the following findings:

  • It always asks for permission to use the mic (after every reload).
  • The first time after everything is cleared and permission is granted for the mic it works.
  • Every time after that that mic input is not working.

@thijssmudde
Copy link

Interesting thread: https://stackoverflow.com/a/74495761

Thanks this helped me fix 2 of the 3 the audio issues! Only remaining issue is that the user always has to give permission even after they already gave permission. But I think this is an iOS thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants