Simplest Possible P2P Voice Chat Using Fire★

voicechat

Fire★ is a platform for making writing peer-to-peer applications as simple as possible. I am going to show here how to write the simplest, most dumb and basic voice chat app using Fire★. I have recently added the ability to grab sound from the microphone and play sound through the speaker. With the capability to get raw audio from a microphone, ability to play sound, and the ability to send messages, writing a very basic voice chat app is easy. All Fire★ applications are written in the Lua programming language.

Creating a mic and speaker is easy.

m = app:mic("got_sound", "pcm")
s = app:speaker("pcm")
m:start()

The constructor for the microphone takes a callback and a codec to use. In this case we will use simple pcm. Whenever audio data is captured from the microphone, got_sound will be called.

function got_sound(d)
	local m = app:message()
	m:set_type("s")
	m:set_bin("d", d)
	app:send(m)
end

“got_sound” simply takes the pcm audio data, and wraps it in a message to send to all the peers.

app:when_message("s", "play_sound")
function play_sound(m)
	s:play(m:get_bin("d"))
end

The audio messages are of type ‘s’ and when a peer gets a message of type ‘s’, the “play_sound” function is called. “play_sound” simply plays the pcm audio to the speaker.

This is the code for possibly the simplest peer-to-peer voice chat application you can write. I have checked into GitHub a slightly more complicated version with the basic GUI you see in the screenshot above here: https://github.com/mempko/firestr/tree/master/src/packaged_apps/voice

Of course this is no where near a real voice chat app. I just wanted to demonstrate that there is a basic foundation that can support voice chat. Next up, video!

Leave a comment