mplayer.rb 0.0.1.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/mplayer.rb +26 -11
  2. data/spec/mplayer_spec.rb +4 -4
  3. metadata +1 -1
data/lib/mplayer.rb CHANGED
@@ -1,8 +1,9 @@
1
+ require 'open3'
1
2
  require_relative "./mplayer_commands"
2
3
 
3
4
  class MPlayer
4
5
  include MPlayerCommands
5
- attr_accessor :player_pid,:logfile
6
+ attr_accessor :player_pid,:log,:thread,:stdout,:stdin, :lock
6
7
 
7
8
  # returns an instance of MPlayer. The fifo-file to control the related mplayer-process is created. Also
8
9
  # an mplayer-instance gests startet.
@@ -10,17 +11,21 @@ class MPlayer
10
11
  # Some Options can be used to describe different asspects of the object.
11
12
  # They can be set in the options-hash. Following options are avaiable:
12
13
  # * fifo: path to the fifo-file which will be created
13
- # * logfile: path to a file, where all the std-out and std-err gets stored
14
14
  #
15
15
  # @param options [Hash] Specifies some options for the mplayer-instance.
16
16
  # @return [MPlayer] New MPlayer-object
17
- def initialize options={fifo: "/tmp/mplayer_rb_#{$$}_fifo",logfile: "/tmp/mplayer_rb_log"}
17
+ def initialize options={fifo: "/tmp/mplayer_rb_#{$$}_fifo"}
18
18
  @options = options
19
- self.logfile = File.new(@options[:logfile],"w")
19
+ @command = "mplayer -slave -idle -quiet -input file=#{options[:fifo]}"
20
+ @log = ""
21
+ @lock = Mutex.new
20
22
  create_fifo
21
23
  start_mplayer
22
24
  end
23
25
 
26
+ def player_pid
27
+ @lock.synchronize { @player_pid }
28
+ end
24
29
  # sends a command to the FIFO-file, which controlls the related mplayer-instance.
25
30
  # Example:
26
31
  # mp = MPlayer.new
@@ -29,9 +34,20 @@ class MPlayer
29
34
  # @params cmd [String] The command which should be sent to mplayer
30
35
  # @return nil at the moment @TODO:it should return the output of mplayer for the sent command
31
36
  def run(cmd)
37
+ begin
38
+ @stdout.read_nonblock 1500
39
+ rescue
40
+ end
32
41
  File.open(@options[:fifo],"w+") do |f|
33
42
  f.puts cmd
34
43
  end
44
+ begin
45
+ sleep 0.1
46
+ response = @stdout.read_nonblock 1500
47
+ rescue
48
+ response = ""
49
+ end
50
+ response
35
51
  end
36
52
 
37
53
  # override respond_to_missing?, so when a method is called, whichs name is a mplayer-command,
@@ -81,18 +97,16 @@ class MPlayer
81
97
  #
82
98
  # @return [Fixnum] The process-id of the mkfifo-command
83
99
  def create_fifo
84
- spawn "mkfifo #{@options[:fifo]}", out: self.logfile, err: self.logfile
100
+ system "mkfifo #{@options[:fifo]}"
85
101
  end
86
102
 
87
103
  # kill_mplayer kills the related mplayer-instance and deletes all created files.
88
104
  #
89
105
  # @return [Boolean] True if the mplayer-instance-kill-command succeeded
90
106
  def kill_mplayer
91
- self.logfile.close
92
- [@options[:logfile],@options[:fifo]].each do |file|
93
- File.delete(file) if File.exists? file
94
- end
95
- system "kill -9 #{self.player_pid.to_s}"
107
+ File.delete(@options[:fifo]) if File.exists? @options[:fifo]
108
+ `kill -9 #{@player_pid}`
109
+ thread.join
96
110
  end
97
111
 
98
112
  # starts the related mplayer-instance and stores the pid of it in the player_pid-attribute
@@ -103,6 +117,7 @@ class MPlayer
103
117
  kill_mplayer
104
118
  end
105
119
 
106
- self.player_pid = spawn("mplayer -slave -idle -quiet -input file=#{@options[:fifo]}", out: self.logfile, err: self.logfile)
120
+ @stdin,@stdout,@thread = Open3.popen2e(@command)
121
+ @player_pid = @thread.pid
107
122
  end
108
123
  end
data/spec/mplayer_spec.rb CHANGED
@@ -15,10 +15,6 @@ describe MPlayer do
15
15
  File.exists?(fifo_file).should be_true
16
16
  end
17
17
 
18
- it 'creates a file, whichs path is described in the logfile-option' do
19
- logfile = @mplayer.instance_variable_get(:@options)[:logfile]
20
- File.exists?(logfile).should be_true
21
- end
22
18
  end
23
19
 
24
20
  describe "#player_pid" do
@@ -40,6 +36,10 @@ describe MPlayer do
40
36
  data = fifo_stream.read_nonblock(6)
41
37
  data.should eql cmd+"\n"
42
38
  end
39
+
40
+ it 'returns the output of the given command' do
41
+ @mplayer.run("loadfile ./song.mp3").should =~ "Playing "
42
+ end
43
43
  end
44
44
 
45
45
  context "using mplayer-commands as method-calls" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mplayer.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: