|
|
#!/usr/bin/env ruby
# (c) 2007 camico A7 users.sourceforge.net
# License: GNU General Public License V2
video_player = "mplayer"
video_files = [
"3gp",
"asf",
"avi",
"flv",
"m4v",
"mkv",
"mov",
"mp4",
"mpg",
"ogm",
".rm",
"vob",
"wmv"
]
amarok = "dcop amarok player"
require 'ftools'
require 'open3'
require 'escape'
MP_INITALIZING = -1
MP_STOPPED = 0
MP_PAUSED = 1
MP_PLAYING = 2
class MplayerThread
def mplayerConfigDir
@mplayerConfigDir = ENV['HOME'] + "/.mplayer/"
end
def mplayerConfig
@mplayerConfig = @mplayerConfigDir + "/config"
end
def mplayerConfigBak
@mplayerConfigBak = @mplayerConfig + ".bak"
end
def mplayerNeverQuietConfig
@mplayerNeverQuietConfig = @mplayerConfigDir + "neverQuietConfig"
end
def mplayerPaused
@mplayerPaused = false
end
def mplayerStdin
@mplayerStdin = false
end
def mplayerStdout
@mplayerStdout = false
end
def mplayerStderr
@mplayerStderr = false
end
def amarokState
@amarokState = 0
end
def mplayerState
# -1 = not yet initialized
# 0 = stopped
# 1 = paused
# 2 = playing
@mplayerState = 0
end
def sendCommand(cmd)
if mplayerStdoutThread.status
puts "sendCommand:"+cmd+" "
mplayerStdin.write(cmd + "\n")
end
end
def amarokState(value)
amarokState = value
puts "amarokState:" + amarokState.to_s + " "
case amarokState
when MP_STOPPED
sendCommand("quit")
when MP_PAUSED
sendCommand("pause")
when MP_PLAYING
## mplayer has a bug if it's paused, get_time_pos will make
# it play again. If it's not nothing happens.
sendCommand("get_time_pos")
when -1
end
end
def mplayerStdoutThread
## start the thread & exit for a reason.
@mplayerStdoutThread = Thread.new() {
Thread.exit
}
end
def setupTmpMplayerConfig()
setupTmp = false
if !File.exist?(mplayerConfigDir)
return setupTmp
end
puts "setupTmpMplayerConfig "
src = File.new(mplayerConfig,"r")
dst = File.new(mplayerNeverQuietConfig, "w")
src.each { |line|
key, value = line.split(/=/,2);
if key != "quiet"
# puts line
dst.write(line)
else
setupTmp = true
end
}
src.close
dst.close
if setupTmp
## Backup the origianl mplayer
File.copy(mplayerConfig, mplayerConfigBak, true)
## copy the tmp we just created
File.copy(mplayerNeverQuietConfig, mplayerConfig, true)
else
File.unlink(mplayerNeverQuietConfig)
end
return setupTmp
end
def restoreOriginalMplayerConfig()
File.copy(mplayerConfigBak, mplayerConfig, true)
# Clean up after ourselves.
File.unlink(mplayerConfigBak)
File.unlink(mplayerNeverQuietConfig)
end
def play(url)
# puts mplayerConfigDir
escaped = Escape.shell_command([url]);
execCommand = "mplayer -slave " + escaped.to_s + ";echo \"\n*done with mplayer*\n\""
puts "execCommand:" + execCommand + " "
amarokVol = `dcop amarok player getVolume`
`dcop amarok player setVolume 0`
`dcop amarok player pause`
tmpSetup = setupTmpMplayerConfig()
mplayerStdin, mplayerStdout, mplayerStderr = Open3.popen3(execCommand);
mplayerState = MP_INITALIZING
mplayerStdoutThread = Thread.new {
sb = "";
while mplayerStdout
s = mplayerStdout.getc
if (mplayerPaused)
mplayerPaused = false
end
mplayerState = MP_PLAYING
if s.chr == "\n" || s.chr == "\r"
stripped = sb.strip
## this is just to debug what's happening.
# puts "sb:" + sb.strip + "\n"
if stripped == '===== PAUSE ====='
puts "MP_PAUSED CAUGHT "
if (amarokState == MP_PLAYING)
puts "Pause "
`dcop amarok player pause`
mplayerState = 1
amarokState = 1
end
elsif stripped == "*done with mplayer*"
## puts "stopping read thread"
mplayerState = 0
break
end
sb = ""
end
sb += s.chr
end
`dcop amarok setVolume #{amarokVol}`
## puts "*** done with loop"
Thread.exit
}
# Wait until mplayer is playing
while mplayerState == MP_INITALIZING
sleep 1
end
if tmpSetup
restoreOriginalMplayerConfig()
end
# Wait a few more seconds, and start amarok
`dcop amarok player play`
end
end
mplayer = MplayerThread.new()
# puts mplayer.mplayerConfigDir
loop do
message = STDIN.gets().chomp() #Read message from stdin
puts "message:" + message + " "
case message
when "configure"
msg = '"This script does not have configuration options."'
`dcop amarok playlist popupMessage "#{msg}"`
when "engineStateChange: empty"
# amarok has stopped
mplayer.amarokState(MP_STOPPED);
when "engineStateChange: paused"
mplayer.amarokState(MP_PAUSED);
when "engineStateChange: playing"
mplayer.amarokState(MP_PLAYING);
when "trackChange"
puts "*trackChange "
url = `dcop amarok player encodedURL`.strip
if url[0...4] == "file"
is_local = true
url = `dcop amarok player path`.strip
else
is_local = false
end
puts "url:"+url+" "
filetype = url[-3..-1]
if filetype and video_files.include? filetype.downcase
mplayer.play(url)
end
end
end
|