Class: RMPlayer::MPlayer

Inherits:
Object
  • Object
show all
Defined in:
lib/rmplayer/mplayer.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ MPlayer

Returns a new instance of MPlayer.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rmplayer/mplayer.rb', line 6

def initialize(args)
  @mutex = Mutex.new

  pstdin, pstdout, pstderr = IO.pipe, IO.pipe, IO.pipe

  command = %w(/usr/bin/mplayer -slave -quiet) + args
  @pid = fork do
    STDIN.reopen pstdin.first
    pstdin.last.close

    STDERR.reopen pstderr.last
    pstderr.first.close

    STDOUT.reopen pstdout.last
    pstdout.first.close

    STDERR.sync = true
    STDOUT.sync = true

    exec(*command)
  end

  pstdin.first.close
  pstderr.last.close
  pstdout.last.close

  @stdin, @stdout, @stderr = pstdin.last, pstdout.first, pstderr.first

  STDERR.sync = true
  STDOUT.sync = true

  sleep 2
end

Instance Method Details

#get(property) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rmplayer/mplayer.rb', line 52

def get(property)
  cmd = "pausing_keep_force get_#{property}"
  match = case property
      when "time_pos" then 'ANS_TIME_POSITION'
      when "percent_pos" then 'ANS_PERCENT_POSITION'
      when "time_length" then 'ANS_LENGTH'
      when "file_name" then 'ANS_FILENAME'
      else "ANS_#{property.to_s.upcase}"
      end
  rex = /#{match}=(.+)/
  response = send(cmd, rex)
  response[rex, 1]
end

#get_property(property) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/rmplayer/mplayer.rb', line 44

def get_property(property)
  cmd = "pausing_keep_force get_property #{property}"
  match = "ANS_#{property.to_s}"
  rex = /#{match}=(.+)/
  response = send(cmd, rex)
  response[rex, 1]
end

#load_file(file) ⇒ Object



95
96
97
# File 'lib/rmplayer/mplayer.rb', line 95

def load_file(file)
  send("loadfile #{file}")
end

#pauseObject



70
71
72
# File 'lib/rmplayer/mplayer.rb', line 70

def pause
  send('pause')
end

#paused?Boolean

Returns:



40
41
42
# File 'lib/rmplayer/mplayer.rb', line 40

def paused?
  get_property('pause') == 'yes'
end

#quitObject



74
75
76
77
78
79
80
81
# File 'lib/rmplayer/mplayer.rb', line 74

def quit
  begin
    send('quit')
  ensure
    Process.waitpid(@pid) if @pid
    @pid = nil
  end
end

#seek(value) ⇒ Object



83
84
85
# File 'lib/rmplayer/mplayer.rb', line 83

def seek(value)
  send("seek #{value} 2")
end

#send(cmd, match = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rmplayer/mplayer.rb', line 103

def send(cmd, match = nil)
  @mutex.synchronize do
    puts "Sending #{cmd}. Looking for #{match}" if DEBUG
    @stdin.puts(cmd)
    
    return nil if match.nil?

    response = @stdout.gets
    puts "Resp: #{response}. Looking for #{match}" if DEBUG
    until response =~ match
      response = @stdout.gets
      puts "Resp: #{response}. Looking for #{match} in loop" if DEBUG
    end
    puts "Matched response: #{response}" if DEBUG
    response
  end
end

#toggle_fullscreenObject



99
100
101
# File 'lib/rmplayer/mplayer.rb', line 99

def toggle_fullscreen
  send('vo_fullscreen')
end

#volumeObject



91
92
93
# File 'lib/rmplayer/mplayer.rb', line 91

def volume
  get_property('volume')
end

#volume=(value) ⇒ Object



87
88
89
# File 'lib/rmplayer/mplayer.rb', line 87

def volume=(value)
  send("volume #{value} 1")
end