Mario Ant
Mario Ant

Reputation: 23

Audio stream relay with Ruby

I am trying to develop an audio stream relay using em-proxy.

Things like authentication and logging work out pretty well. The issue is that, every new incoming connection leads to a new connection to the audio stream.

A partial of my code:

require 'em-proxy'

host = "0.0.0.0"
port = 9889
puts "listening on #{host}:#{port}..."

Proxy.start(:host => host, :port => port) do |conn|

  conn.server :srv, host: "127.0.0.1", port: 8000

  conn.on_data do |data|
    # logging happens here
    data
  end

  conn.on_finish do |backend, name|
    # logging too
    unbind
  end

end

Ideally, I would like to have only one connection to the audio stream.

Any ideas?

Upvotes: 1

Views: 130

Answers (1)

Niels B.
Niels B.

Reputation: 6310

I am not familiar with em-proxy, but I did do a project in EventMachine a year ago.

What you want to do is connect to your audio stream when launching your proxy process. In addition to that, you will maintain a pool of clients. Each time data is received from the audio stream, you will iterate over your pool of clients and relay the received data.

Here is an EventMachine template:

module MyRadioClient
  def post_init
    some_client_list.add(self)
  end

  def receive_data data
    # do nothing        
  end

  def unbind
    some_client_list.remove(self)
  end
end

You'll launch the module with

EventMachine.run {
  EventMachine.start_server "0.0.0.0", 4567, MyRadioClient
}

Now people can connect on port 4567 and you will ignore anything they send.

Next, you'll need a connection to your audio stream, which - upon receiving data - will relay it to your radio clients.

You can see an example on it on page 20 in this document: http://everburning.com/images/2009/02/eventmachine_introduction_10.pdf

Upvotes: 1

Related Questions