class MPlayer

Attributes

lock[RW]
log[RW]
player_pid[RW]
stdin[RW]
stdout[RW]
thread[RW]

Public Class Methods

new(options={fifo: "/tmp/mplayer_rb_ click to toggle source

returns an instance of MPlayer. The fifo-file to control the related mplayer-process is created. Also an mplayer-instance gests startet.

Some Options can be used to describe different asspects of the object. They can be set in the options-hash. Following options are avaiable:

  • fifo: path to the fifo-file which will be created

@param options [Hash] Specifies some options for the mplayer-instance. @return [MPlayer] New MPlayer-object

# File lib/mplayer.rb, line 17
def initialize options={fifo: "/tmp/mplayer_rb_#{$$}_fifo"}
  @options = options
  @command = "mplayer -slave -idle -quiet -input file=#{options[:fifo]}"
  @log = ""
  @lock = Mutex.new
  create_fifo
  start_mplayer
end

Public Instance Methods

method_missing(name,*args) click to toggle source

dynamically creates and calls methods when the missing method is a mplayer-command (see COMMANDS)

Calls superclass method
# File lib/mplayer.rb, line 65
def method_missing name,*args
  if COMMANDS.include? name
    with_args = Proc.new do |params|
      if params.is_a? Array
        params = params.map do |param|
          if param.start_with? "/"
            param = "'" + param + "'"
          end
        end
        params = params.join " "
      end
      run "#{name.to_s} #{params}"
    end
    argless = Proc.new do
      run "#{name.to_s}"
    end
    if args.empty?
      self.class.send :define_method, name, &argless
      self.send name
    else
      self.class.send :define_method, name, &with_args
      self.send name, args
    end
  else
    super name,args
  end
end
respond_to_missing?(method,priv) click to toggle source

override respond_to_missing?, so when a method is called, whichs name is a mplayer-command, the object should respond to it. When not, the standard-behaviour is executed. {Object#respond_to?}

Calls superclass method
# File lib/mplayer.rb, line 56
def respond_to_missing? method,priv
  if COMMANDS.include? method
    return true
  else
    super method,priv
  end
end
run(cmd) click to toggle source

sends a command to the FIFO-file, which controlls the related mplayer-instance. Example: mp = MPlayer.new mp.run(“pause) <–send pause-command to mplayer

@params cmd [String] The command which should be sent to mplayer @return nil at the moment @TODO:it should return the output of mplayer for the sent command

# File lib/mplayer.rb, line 36
def run(cmd)
  begin
    @stdout.read_nonblock 1500
  rescue
  end
  File.open(@options[:fifo],"w+") do |f|
    f.puts cmd
  end
  begin
    sleep 0.1
    response = @stdout.read_nonblock 1500
  rescue
    response = ""
  end
  response
end

Private Instance Methods

create_fifo() click to toggle source

creates the fifo-file which will be used to controll the mplayer-instance. It used the path which was specified in the options in new

@return [Fixnum] The process-id of the mkfifo-command

# File lib/mplayer.rb, line 99
def create_fifo
  system "mkfifo #{@options[:fifo]}"
end
kill_mplayer() click to toggle source

kill_mplayer kills the related mplayer-instance and deletes all created files.

@return [Boolean] True if the mplayer-instance-kill-command succeeded

# File lib/mplayer.rb, line 106
def kill_mplayer
  File.delete(@options[:fifo]) if File.exists? @options[:fifo]
  `kill -9 #{@player_pid}`
  thread.join
end
start_mplayer() click to toggle source

starts the related mplayer-instance and stores the pid of it in the player_pid-attribute

@return [Fixnum] The pid of the related mplayer-instance

# File lib/mplayer.rb, line 115
def start_mplayer
  at_exit do
    kill_mplayer
  end

  @stdin,@stdout,@thread = Open3.popen2e(@command)
  @player_pid = @thread.pid
end