Created
December 13, 2012 05:42
-
-
Save turboladen/4274322 to your computer and use it in GitHub Desktop.
UDP stream copier. Now you can pass in a destination IP and port:
$ ruby io_copier.rb 192.168.10.5 1234
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I was able to stream an .m4v file using VLC using this (just to verify it works). | |
# | |
# 1. Use VLC's Streaming/Exporting Wizard: | |
# a) Stream to network | |
# b) Select a stream (choose the file you want to send) | |
# c) Streaming method: UDP unicast; destination 127.0.0.1 | |
# d) (Don't transcode. ...well, unless you want to I guess?) | |
# e) Encapsulation format: MPEG_TS (my only choice) | |
# f) (No additional options) | |
# 2. Run this script. It takes 2 args: destination_ip, destination_port. | |
# [destination_port] should be the IP of the box used in step 3 below. | |
# 3. On another box, in VLC: | |
# a) Open Network | |
# b) Open RTP/UDP Stream | |
# c) Protocol = UDP; Mode = Unicast; Port = [destination_port from above] | |
# 4. Give it a few seconds to start going... | |
require 'socket' | |
Thread.abort_on_exception = true | |
class IOCopier | |
def initialize(destination_ip, destination_port) | |
@ip = destination_ip | |
@port = destination_port | |
@copier = UDPSocket.open | |
end | |
def write(data) | |
#puts "WRITER: sending data to #{@ip}:#{@port} with size: #{data.size}" | |
#puts "WRITER: 50th byte: #{data[50]}" | |
@copier.send(data, 0, @ip, @port) | |
end | |
end | |
destination_ip, destination_port = *ARGV.dup | |
# This just validates that the copier is actually copying. | |
=begin | |
Thread.start do | |
end_point = UDPSocket.new | |
end_point.bind(destination_ip, destination_port) | |
loop do | |
data, _ = end_point.recvfrom(1500) | |
puts "END POINT: received data with size: #{data.size}" | |
puts "END POINT: 50th byte: #{data[50]}" | |
end | |
end | |
=end | |
# This does the copying. | |
Thread.start do | |
reader = UDPSocket.new | |
reader.bind '127.0.0.1', 1234 | |
writer = IOCopier.new(destination_ip, destination_port) | |
loop do | |
IO.copy_stream(reader, writer) | |
end | |
end | |
sleep 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment