Created
October 30, 2012 12:37
-
-
Save relrod/3979940 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
# Praxis -- a multicaster proxy for Axis Camera MJPEGs. | |
# License: MIT | |
# Author: Ricky Elrod <[email protected]> | |
STREAM_URL = 'http://wm161.kicks-ass.net:8080/mjpg/video.mjpg' | |
require 'em-http-request' | |
require 'sinatra' | |
set :server, :thin | |
$connections = [] | |
# Get a persistent connection to the mjpeg. | |
# Any time we get data, send it to all connections. | |
input = Thread.new do | |
run = true | |
while run | |
run = false | |
EventMachine.run do | |
puts "Connection to stream initialized at #{Time.now}" | |
http = EventMachine::HttpRequest.new(STREAM_URL).get | |
http.stream do |chunk| | |
$connections.each { |client| client << chunk } | |
end | |
http.callback do |everything_ever| | |
# If we hit here, we have two issues. | |
# 1) We dropped the stream for some reason. | |
# 2) We need to recover from that. | |
run = true | |
next | |
end | |
end | |
end | |
end | |
class Praxis < Sinatra::Application | |
get '/' do | |
response['Content-Type'] = 'multipart/x-mixed-replace; boundary=--myboundary' | |
response['Cache-Control'] = 'no-cache' | |
response['Pragma'] = 'no-cache' | |
stream(:keep_open) do |out| | |
$connections << out | |
end | |
end | |
end | |
output = Thread.new do | |
Praxis.run! | |
end | |
input.join | |
output.join |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment