Created
December 16, 2010 15:04
-
-
Save branch14/743495 to your computer and use it in GitHub Desktop.
mplayer.rb
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
# control mplayer remotely via rack | |
# | |
# load & append files (mp3, ogg, etc) & lists (m3u) | |
# http://localhost:3000/mplayer/loadfile+http://www.archive.org/download/af002/03_Swiss_Jazz.mp3 | |
# http://localhost:3000/mplayer/loadlist+http://www.archive.org/stream/af002 | |
# | |
# playback | |
# http://localhost:3000/mplayer/pause | |
# http://localhost:3000/mplayer/stop | |
# | |
# reset | |
# http://localhost:3000/mplayer/quit | |
# | |
# volume control | |
# http://localhost:3000/mplayer/volume+10+1 | |
# http://localhost:3000/mplayer/volume+60+1 | |
# | |
module Rack | |
class Mplayer | |
HEADERS = { "Content-Type" => "text/plain" } | |
NAMESPACE = 'mplayer' | |
def initialize(app) | |
@app = app | |
@regexp = %r[^/#{NAMESPACE}/(.*)] | |
instanciate_mplayer | |
end | |
def call(env) | |
if md = env["PATH_INFO"].match(@regexp) | |
cmd = CGI::unescape(md.to_a.last) | |
begin | |
@in.puts cmd | |
rescue | |
instanciate_mplayer | |
@in.puts cmd | |
end | |
[ 200, HEADERS, cmd ] | |
else | |
@app.call env | |
end | |
end | |
private | |
def instanciate_mplayer | |
cmd = "mplayer -quiet -idle -slave" | |
@in, @out, @err = Open3.popen3 cmd | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment