Class: Budik::Player

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/budik/player.rb

Overview

‘Player’ class handles communication between app and media players.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlayer

Sets player and loads its options.



16
17
18
19
20
21
22
23
24
25
# File 'lib/budik/player.rb', line 16

def initialize
  player_options = Config.instance.options['player']
  @player = player_options['player']

  if @player == 'omxplayer'
    @player_options = player_options['omxplayer']
  else
    @player_options = player_options['vlc']
  end
end

Instance Attribute Details

#playerObject

Gets current player and its options.



28
29
30
# File 'lib/budik/player.rb', line 28

def player
  @player
end

#player_optionsObject

Gets current player and its options.



28
29
30
# File 'lib/budik/player.rb', line 28

def player_options
  @player_options
end

Instance Method Details

#omx_build_command(item) ⇒ Object

Builds omxplayer’s command with required parameters.

  • Args:

    • item -> Item to play (path, String).



61
62
63
64
65
# File 'lib/budik/player.rb', line 61

def omx_build_command(item)
  command = @player_options['path']
  args = '--vol ' + @player_options['default_volume'].to_s
  command + ' ' + args + ' "' + Storage.instance.locate_item(item) + '"'
end

#omx_volume_control(i) ⇒ Object

Fades in volume using omxplayer’s volup command.

  • Args:

    • i -> Stdin object.



72
73
74
75
76
77
78
# File 'lib/budik/player.rb', line 72

def omx_volume_control(i)
  7.times do
    sleep(@player_options['volume_step_secs'])
    i.print '+'
  end
  i.close
end

#omxplayer(source) ⇒ Object

Plays a source using omxplayer.

  • Args:

    • source -> Source to play (Hash).



48
49
50
51
52
53
54
# File 'lib/budik/player.rb', line 48

def omxplayer(source)
  source[:path].each_with_index do |item, index|
    Open3.popen3(omx_build_command(item)) do |i, _o, _e, _t|
      omx_volume_control(i) if index == 0
    end
  end
end

#play(source) ⇒ Object

Plays a source using currently set player.

  • Args:

    • source -> Source to play (Hash).



35
36
37
38
39
40
41
# File 'lib/budik/player.rb', line 35

def play(source)
  if @player == 'omxplayer'
    omxplayer(source)
  else
    vlc(source)
  end
end

#vlc(source) ⇒ Object

Plays a source using vlc.

  • Args:

    • source -> Source to play (Hash).



85
86
87
88
89
90
91
# File 'lib/budik/player.rb', line 85

def vlc(source)
  vlc_pid = spawn(vlc_build_command(source))
  sleep(@player_options['wait_secs_after_run'])
  vlc_volume_control(vlc_rc_connect)

  Process.wait(vlc_pid)
end

#vlc_build_argsObject

Builds list of options/arguments fo VLC command



109
110
111
112
113
114
115
116
117
118
# File 'lib/budik/player.rb', line 109

def vlc_build_args
  rc_host = @player_options['rc_host']
  rc_port = @player_options['rc_port']
  rc = ' --extraintf rc --rc-host ' + rc_host + ':' + rc_port.to_s

  volume = ' --volume-step ' + @player_options['volume_step'].to_s
  fullscreen = @player_options['fullscreen'] ? ' --fullscreen ' : ' '

  { rc: rc, volume: volume, fullscreen: fullscreen }
end

#vlc_build_command(source) ⇒ Object

Builds VLC’s command with required parameters.

  • Args:

    • source -> Source to play (Hash).



98
99
100
101
102
103
104
105
106
# File 'lib/budik/player.rb', line 98

def vlc_build_command(source)
  vlc_path = Marshal.load(Marshal.dump(@player_options['path']))
  vlc_path.gsub!(/(^|$)/, '"') if vlc_path =~ /\s/

  args = vlc_build_args
  files = vlc_cmd_add_items(source)

  vlc_path + args[:rc] + args[:volume] + args[:fullscreen] + files
end

#vlc_cmd_add_items(source) ⇒ Object

Parses source and adds its items to the VLC command. Adds ‘vlc://quit’ to automatically quit VLC after play is over.

  • Args:

    • source -> Source to play (Hash).



126
127
128
129
130
131
132
133
# File 'lib/budik/player.rb', line 126

def vlc_cmd_add_items(source)
  files = ''
  source[:path].each do |item|
    item_path = Storage.instance.locate_item(item).gsub(%r{^/}, '')
    files += (vlc_cmd_item_prefix(item_path) + item_path + '" ')
  end
  files += 'vlc://quit'
end

#vlc_cmd_item_prefix(item_path) ⇒ Object

Adds ‘file:///’ prefix to local file paths so VLC plays them correctly.

  • Args:

    • item_path -> Path to item.



141
142
143
144
# File 'lib/budik/player.rb', line 141

def vlc_cmd_item_prefix(item_path)
  is_url = (item_path =~ /\A#{URI.regexp(%w(http https))}\z/)
  is_url ? '"' : '"file:///'
end

#vlc_rc_connectObject

Makes a connection to VLC’s remote control interface. FIXME: Possible infinite loop



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/budik/player.rb', line 148

def vlc_rc_connect
  rc_host = @player_options['rc_host']
  rc_port = @player_options['rc_port']
  loop do
    begin
      rc = TCPSocket.open(rc_host, rc_port)
      return rc
    rescue
      next
    end
  end
end

#vlc_volume_control(rc) ⇒ Object

Fades in volume using VLC’s remote control interface.

  • Args:

    • rc -> IO object (returned by TCPSocket.open).



166
167
168
169
170
171
172
# File 'lib/budik/player.rb', line 166

def vlc_volume_control(rc)
  rc.puts 'volume ' + @player_options['default_volume'].to_s
  128.times do
    sleep(@player_options['volume_fadein_secs'])
    rc.puts 'volup ' + @player_options['volume_step'].to_s
  end
end