diff --git a/README.md b/README.md index 6034ac8..244578b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,70 @@ # GodotVoIP + A minimal VoIP example using no external libraries. + +## Networking + +The networking for this project follows the standard godot networking example found [here](https://docs.godotengine.org/en/stable/tutorials/networking/high_level_multiplayer.html#example-lobby-implementation). + +## Audio Bus + +In the audio tab I added two Buses, Mic and MicSink. The Mic Bus has a Capture effect on it and is pointing into MicSink which is muted. + +## Node Setup + +The only required nodes are AudioStreamPlayers and/or their 2D and 3D counterparts. + +### Microphone Node + +Every player must have an AudioStreamPlayer for capturing the microphone input. This node's stream must be of type AudioStreamMicrophone, Autoplay should be On and the Bus should point to the Mic Bus. + +![Microphone Node Setup](./assets/mic_node_setup.png) + +### VoIP Playback Node + +In this example only one VoIP playback node exists. In a real use case you would spawn one node per client. + +Depending on your use case this node can be an AudioStreamPlayer, AudioStreamPlayer2D or AudioStreamPlayer3D. For this project I used an AudioStreamPlayer. This node's stream must be of type AudioStreamGenerator, Autoplay should be On and the Bus can be left as the default. + +![VoIP Node Setup](./assets/voip_node_setup.png) + +## VoIP Script + +```GDScript +extends AudioStreamPlayer + +var effect: AudioEffectCapture +var playback: AudioStreamGeneratorPlayback + +@onready var voip: AudioStreamPlayer = $"../VoIP" + +func _ready(): + # Get the capture effect we added on the Mic Bus + var idx = AudioServer.get_bus_index("Mic") + effect = AudioServer.get_bus_effect(idx, 0) + + # Get the playback stream from our voip AudioStreamPlayer + playback = voip.get_stream_playback() + +func _process(_delta): + var frames: PackedVector2Array + + if effect.can_get_buffer(1024): + # Get 1024 audio frames at a time. + frames = effect.get_buffer(1024) + + # Send the audio frames over RPC + playback_voip.rpc(frames) + + +@rpc("any_peer", "call_remote", "unreliable_ordered", 1) +func playback_voip(frames: PackedVector2Array): + # Push the frames to the voip audio buffer + playback.push_buffer(frames) +``` + +## Know issues / TODO + +Sending the raw audio frames is not optimal since it ends up being a large amount of data. About 8Mb for 1024 audio frames which is way above the MTU of 1392 bytes. + +This can easily be improved by using compression but I haven't had the time to look into [StreamPeerGZIP](https://docs.godotengine.org/en/stable/classes/class_streampeergzip.html). \ No newline at end of file diff --git a/assets/mic_node_setup.png b/assets/mic_node_setup.png new file mode 100644 index 0000000..dce9088 Binary files /dev/null and b/assets/mic_node_setup.png differ diff --git a/assets/mic_node_setup.png.import b/assets/mic_node_setup.png.import new file mode 100644 index 0000000..aec6812 --- /dev/null +++ b/assets/mic_node_setup.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://cbnvr6iptbayo" +path="res://.godot/imported/mic_node_setup.png-ccd40e634f01535164b3a2a5868b0c04.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/mic_node_setup.png" +dest_files=["res://.godot/imported/mic_node_setup.png-ccd40e634f01535164b3a2a5868b0c04.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/assets/voip_node_setup.png b/assets/voip_node_setup.png new file mode 100644 index 0000000..ac7580d Binary files /dev/null and b/assets/voip_node_setup.png differ diff --git a/assets/voip_node_setup.png.import b/assets/voip_node_setup.png.import new file mode 100644 index 0000000..c708d0b --- /dev/null +++ b/assets/voip_node_setup.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://brxf5cpmvvkj6" +path="res://.godot/imported/voip_node_setup.png-2b6a34b1d29a6abdcf774f2c42cc502e.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/voip_node_setup.png" +dest_files=["res://.godot/imported/voip_node_setup.png-2b6a34b1d29a6abdcf774f2c42cc502e.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1