Skip to content

Commit

Permalink
adding in a trivial opus example
Browse files Browse the repository at this point in the history
  • Loading branch information
goatchurchprime committed Sep 12, 2024
1 parent 39ce578 commit 35d7dd3
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
14 changes: 14 additions & 0 deletions trivialexample/default_bus_layout.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_resource type="AudioBusLayout" load_steps=2 format=3 uid="uid://ceqh8sdin07i8"]

[sub_resource type="AudioEffectOpusChunked" id="AudioEffectOpusChunked_lwptv"]
resource_name = "OpusChunked"

[resource]
bus/1/name = &"Mic"
bus/1/solo = false
bus/1/mute = true
bus/1/bypass_fx = false
bus/1/volume_db = 0.0
bus/1/send = &"Master"
bus/1/effect/0/effect = SubResource("AudioEffectOpusChunked_lwptv")
bus/1/effect/0/enabled = true
71 changes: 71 additions & 0 deletions trivialexample/main.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
extends Node

@export var SERVER_URL = "127.0.0.1"
@export var SERVER_PORT = 8009

var user = load("res://user.tscn")

var mic_capture : AudioEffectOpusChunked

var users = {} # {Peer ID: AudioStreamPlayer}

var packets_received = 0
var packets_sent = 0

func _ready():
multiplayer.peer_connected.connect(_peer_connected)
multiplayer.peer_disconnected.connect(_peer_disconnected)
multiplayer.connected_to_server.connect(_connected_to_server)
multiplayer.connection_failed.connect(_connection_failed)
multiplayer.server_disconnected.connect(_server_disconnected)

var mic_bus = AudioServer.get_bus_index("Mic")
mic_capture = AudioServer.get_bus_effect(mic_bus, 0)

var peer = ENetMultiplayerPeer.new()
var err = peer.create_server(SERVER_PORT)
if err == ERR_ALREADY_IN_USE or err == ERR_CANT_CREATE:
peer.close()
err = peer.create_client(SERVER_URL, SERVER_PORT)
print("Created client ", ("Error %d" % err if err else ""))
else:
print("Created server ", ("Error %d" % err if err else ""))
set_process(false)
multiplayer.multiplayer_peer = peer

func _peer_connected(id):
print("Peer connected with ID ", id)
users[id] = user.instantiate()
add_child(users[id])

func _peer_disconnected(id):
print("Peer disconnected with ID ", id)
users[id].queue_free()
users.erase(id)

func _connected_to_server():
print("Connected to server ", SERVER_URL, ":", SERVER_PORT)

func _connection_failed():
print("Failed to connect to server ", SERVER_URL, ":", SERVER_PORT)

func _server_disconnected():
print("Server disconnected.")

@rpc("any_peer", "unreliable")
func _voice_packet_received(packet):
packets_received += 1
if (packets_received % 100) == 0:
print("Packets received: ", packets_received, " from id ", multiplayer.get_unique_id())
var sender_id = multiplayer.get_remote_sender_id()
users[sender_id].stream.push_opus_packet(packet, 0, 0)

func _process(_delta):
while mic_capture.chunk_available():
var packet = mic_capture.read_opus_packet(PackedByteArray())
mic_capture.drop_chunk()
if multiplayer.multiplayer_peer.get_connection_status() == MultiplayerPeer.CONNECTION_CONNECTED:
_voice_packet_received.rpc(packet)
packets_sent += 1
if (packets_sent % 100) == 0:
print("Packets sent: ", packets_sent, " from id ", multiplayer.get_unique_id())
12 changes: 12 additions & 0 deletions trivialexample/main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[gd_scene load_steps=3 format=3 uid="uid://dpsvywutgtu38"]

[ext_resource type="Script" path="res://main.gd" id="1_0j44c"]

[sub_resource type="AudioStreamMicrophone" id="AudioStreamMicrophone_d5gmj"]

[node name="Node3D" type="Node3D"]
script = ExtResource("1_0j44c")

[node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."]
stream = SubResource("AudioStreamMicrophone_d5gmj")
autoplay = true
24 changes: 24 additions & 0 deletions trivialexample/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=5

[application]

config/name="twovoip trivial ENET"
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.3", "GL Compatibility")

[audio]

driver/enable_input=true

[rendering]

renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"
7 changes: 7 additions & 0 deletions trivialexample/user.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://e8q0gjrr45rq"]

[sub_resource type="AudioStreamOpusChunked" id="AudioStreamOpusChunked_mrrtk"]

[node name="AudioStreamPlayer" type="AudioStreamPlayer"]
stream = SubResource("AudioStreamOpusChunked_mrrtk")
autoplay = true

0 comments on commit 35d7dd3

Please sign in to comment.