RevoluPowered / one-voip-godot-4

One voip plugin to rule them all
MIT License
49 stars 4 forks source link

macOS support? #14

Closed cortrano closed 2 months ago

cortrano commented 3 months ago

Is that possible to add macOS support?

marc-weber1 commented 3 months ago

From what I understand gdextensions will only work if you sign them with the apple $100 developer license, and modules will only work if you recompile the entire engine using this code. I've considered a module version but I have no macs I could test it on

cortrano commented 3 months ago

From what I understand gdextensions will only work if you sign them with the apple $100 developer license, and modules will only work if you recompile the entire engine using this code. I've considered a module version but I have no macs I could test it on

I'm sure that $100 license is unnecessary because it's required only for posting apps in AppStore. For example I've already built gdextension libs for tbloader and godot-steam addons absolutely for free. I will try to build this lib too

cortrano commented 3 months ago

Haven't figure out how to compile it for mac on m1. I successfully compiled opus lib running cmake -Bbuild -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"

But i get errors like "This header is only meant to be used on x86 and x64 architecture" in thirdparty/speex/libspeexdsp/resample.c and thirdparty/speex/libspeexdsp/resample_sse when try to run scons platform=macos arch=arm64

And when i try to run scons platform=macos arch=x86_64 looks like it builds successfully

marc-weber1 commented 3 months ago

If you look in the SConstruct file you'll see these lines I just hardcoded for x86:

# Speex (resampler / jitter buffer)

env.Append(CPPPATH=["thirdparty/speex/include"])
env.Append(CPPDEFINES={"USE_SSE": None, "USE_SSE2": None, "FLOATING_POINT": None, "USE_SMALLFT": None}) # "EXPORT": None ?
sources += ["thirdparty/speex/libspeexdsp/resample.c", "thirdparty/speex/libspeexdsp/jitter.c"]

Obviously USE_SSE and USE_SSE2 will not work on ARM; it has something else it uses for float vectors called NEON. For a temporary fix, just remove the SSE defines, but if you want to help make the code a bit faster try finding a USE_NEON header or something like that in the speex resample header (and tell me what it is pls)

marc-weber1 commented 3 months ago

(Looks like it is USE_NEON - if that gets it to build lmk)

marc-weber1 commented 3 months ago

If u can check for me whether this pull request is right I can merge it for the next person on mac ty ty

cortrano commented 3 months ago
image

I get this error on your pr :(

`In file included from src/audio_stream_voip.cpp:3: include/audio_stream_voip.h:7:10: fatal error: 'opus.h' file not found

include "opus.h"

     ^~~~~~~~

In file included from src/audio_stream_playback_voip.cpp:1: In file included from include/audio_stream_playback_voip.h:7: include/audio_stream_voip.h:7:10: fatal error: 'opus.h' file not found

include "opus.h"

     ^~~~~~~~

In file included from src/register_types.cpp:3: In file included from include/voip_input_capture.h:6: include/audio_stream_voip.h:7:10: fatal error: 'opus.h' file not found

include "opus.h"

     ^~~~~~~~

In file included from src/voip_input_capture.cpp:1: In file included from include/voip_input_capture.h:6: include/audio_stream_voip.h:7:10: fatal error: 'opus.h' file not found

include "opus.h"

     ^~~~~~~~

1 error generated. 1 error generated. 1 error generated. 1 error generated. scons: [src/audio_stream_playback_voip.os] Error 1 scons: [src/audio_stream_voip.os] Error 1 scons: [src/register_types.os] Error 1 scons: [src/voip_input_capture.os] Error 1 scons: building terminated because of errors.`

cortrano commented 3 months ago

It look like you forgot add macOS here elif env["platform"] == "linux" or env["platform"] == "javascript" or env["platform"] == "macos": env.Append(CPPPATH=["thirdparty/opus/include"], LIBS=["opus"], LIBPATH="thirdparty/opus/build") env.Append(CXXFLAGS = ['-fpermissive'])

Now it works!

cortrano commented 3 months ago

Also i would add these env["arch"] and env["SHLIBSUFFIX"] for proper lib naming

if env["platform"] == "macos":
    library = env.SharedLibrary(
        "demo_rtc/bin/libonevoip.{}.{}.framework/libonevoip.{}.{}.{}{}".format(
            env["platform"], env["target"], env["platform"], env["target"], env["arch"], env["SHLIBSUFFIX"]
        ),
        source=sources,
    )
cortrano commented 3 months ago

I also tested buld for x86_64 and now it fails with:

In file included from thirdparty/speex/libspeexdsp/resample.c:104: thirdparty/speex/libspeexdsp/resample_neon.h:213:12: error: unknown register name 'q0' in asm : "q0"); ^ thirdparty/speex/libspeexdsp/resample_neon.h:334:13: error: unknown register name 'q0' in asm : "cc", "q0", "q1", "q2", "q3", ^ 2 errors generated. clang++ -o src/voip_input_capture.os -c -fno-exceptions -std=c++17 -fpermissive -arch x86_64 -O2 -fvisibility=hidden -fPIC -DHOT_RELOAD_ENABLED -DMACOS_ENABLED -DUNIX_ENABLED -DDEBUG_ENABLED -DDEBUG_METHODS_ENABLED -DNDEBUG -DFLOATING_POINT -DUSE_SMALLFT -DUSE_NEON -Igodot-cpp/gdextension -Igodot-cpp/include -Igodot-cpp/gen/include -Ithirdparty/opus/include -Ithirdparty/speex/include -Ithirdparty -Iinclude src/voip_input_capture.cpp scons: *** [thirdparty/speex/libspeexdsp/resample.os] Error 1 scons: building terminated because of errors.

marc-weber1 commented 3 months ago

put the first two thru, but that last one makes it seem like resampler_neon is getting included - not sure if building both at once is the issue?

marc-weber1 commented 3 months ago

@Cortrano reviving this one more time - lmk if the pull request builds on mac, or if there's any fixes I can do to make it build if not

cortrano commented 2 months ago

@marc-weber1 Sorry for long wait before response. With your current pull request I can build for arm, but not for x86_64 on mac.

On current branch, though, build for x86_64 works

marc-weber1 commented 2 months ago

The only possible way you could be getting an error like In file included from thirdparty/speex/libspeexdsp/resample.c:104: thirdparty/speex/libspeexdsp/resample_neon.h is if use neon is true - are you absolutely sure you're compiling with scons platform=macos arch=x86_64 ?

cgodley commented 2 months ago

I was able to build, run, and hear audio by running demo_server and 2x demo on mac m1 (arm64) Sonoma 14.3 without signing anything

diff --git a/SConstruct b/SConstruct
index fc97dcd..09cb531 100644
--- a/SConstruct
+++ b/SConstruct
@@ -23,15 +23,17 @@ if env["platform"] == "windows":
 elif env["platform"] == "linux" or env["platform"] == "javascript":
     env.Append(CPPPATH=["thirdparty/opus/include"], LIBS=["opus"], LIBPATH="thirdparty/opus/build")
     env.Append(CXXFLAGS = ['-fpermissive'])
-
+elif env["platform"] == "macos":
+    env.Append(CPPPATH=["thirdparty/opus/include"], LIBS=["opus"], LIBPATH="thirdparty/opus/build")

 # Speex (resampler / jitter buffer)
-
 env.Append(CPPPATH=["thirdparty/speex/include"])
-env.Append(CPPDEFINES={"USE_SSE": None, "USE_SSE2": None, "FLOATING_POINT": None, "USE_SMALLFT": None}) # "EXPORT": None ?
+if env["platform"] == "macos":
+    env.Append(CPPDEFINES=['FLOATING_POINT', 'USE_NEON'])
+else:
+    env.Append(CPPDEFINES={"USE_SSE": None, "USE_SSE2": None, "FLOATING_POINT": None, "USE_SMALLFT": None}) # "EXPORT": None ?        
 sources += ["thirdparty/speex/libspeexdsp/resample.c", "thirdparty/speex/libspeexdsp/jitter.c"]

-
 # etc

 env.Append(CPPPATH=["thirdparty"])
diff --git a/demo/bin/onevoip.gdextension b/demo/bin/onevoip.gdextension
index 391d7cf..0ed3351 100644
--- a/demo/bin/onevoip.gdextension
+++ b/demo/bin/onevoip.gdextension
@@ -6,4 +6,5 @@ compatibility_minimum = 4.2
 [libraries]

 windows.debug.x86_64="res://bin/libonevoip.windows.template_debug.x86_64.dll"
-linux.debug.x86_64="res://bin/libonevoip.linux.template_debug.x86_64.so"
\ No newline at end of file
+linux.debug.x86_64="res://bin/libonevoip.linux.template_debug.x86_64.so"
+macos.debug.arm64="res://bin/libonevoip.macos.template_debug.framework"
diff --git a/demo/project.godot b/demo/project.godot
index dcfe73b..62db50e 100644
--- a/demo/project.godot
+++ b/demo/project.godot
@@ -17,6 +17,7 @@ config/features=PackedStringArray("4.2", "GL Compatibility")
 [audio]

 driver/enable_input=true
+driver/mix_rate=48000

 [rendering]
cgodley commented 2 months ago

The left ear issue (no audio signal on the right side) seems to be a problem in the CoreAudio driver of Godot Engine.

Until that is fixed, it might make sense to just always convert the input signal to mono. That would fix the left ear issue for anyone with a mac with a 1-channel microphone.

Details

My mac m1 microphone only has 1 channel, butaudio_driver_coreaudio.cpp is setting kAudioUnitProperty_StreamFormat and capture_channels to use 2 channels. Probably CoreAudio is allowing this but only putting the audio into the left channel and outputting an empty right channel.

I suspectAudioUnitGetProperty should be grabbing information from kAudioUnitScope_Input and not kAudioUnitScope_Output. When I make that change (below) and recompile the engine, it correctly detects 1 channel and I can hear microphone audio in both ears.

- result = AudioUnitGetProperty(input_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output kInputBus, &strdesc, &size);
+ result = AudioUnitGetProperty(input_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kInputBus, &strdesc, &size);
    ERR_FAIL_COND_V(result != noErr, FAILED);

    switch (strdesc.mChannelsPerFrame) {
        case 1: // Mono
            capture_channels = 1;