Class: MultimediaParadise::Rubio::Model::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb

Overview

Rubio::Model::Player

Constant Summary collapse

CURRENTLY_PLAYING_NONE =
'None'
CURRENTLY_PLAYING_LENGTH_PER_LINE =
90
USE_THIS_PLAYER =
#

USE_THIS_PLAYER

#
'mpv'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend = USE_THIS_PLAYER, show_currently_playing: true) ⇒ Player

#

initialize

#


33
34
35
36
37
38
39
40
41
42
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 33

def initialize(
    backend = USE_THIS_PLAYER, #+' -I rc',
    show_currently_playing: true
  )
  raise unless backend.is_a?(String)
  reset
  @backend = backend
  @show_currently_playing = show_currently_playing
  self.currently_playing  = CURRENTLY_PLAYING_NONE
end

Instance Attribute Details

#backendObject

or vlc



21
22
23
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 21

def backend
  @backend
end

#currently_playingObject

Returns the value of attribute currently_playing.



27
28
29
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 27

def currently_playing
  @currently_playing
end

#historyObject

Returns the value of attribute history.



25
26
27
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 25

def history
  @history
end

#pidObject

Returns the value of attribute pid.



22
23
24
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 22

def pid
  @pid
end

#show_currently_playingObject (readonly) Also known as: show_currently_playing?

Returns the value of attribute show_currently_playing.



26
27
28
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 26

def show_currently_playing
  @show_currently_playing
end

#statusObject

Returns the value of attribute status.



24
25
26
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 24

def status
  @status
end

#thrObject

Returns the value of attribute thr.



23
24
25
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 23

def thr
  @thr
end

Instance Method Details

#alive?Boolean

#

alive?

#

Returns:

  • (Boolean)


68
69
70
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 68

def alive?
  @thr&.alive? || (@io && !@io.closed?)
end

#continuously_fetch_currently_playingObject

#

continuously_fetch_currently_playing

#


109
110
111
112
113
114
115
116
117
118
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 109

def continuously_fetch_currently_playing
  return if @continuously_fetching_currently_playing

  @continuously_fetching_currently_playing = true

  Glimmer::LibUI.timer(1) {
    update_currently_playing
    @continuously_fetching_currently_playing
  }
end

#currently_playing_text(currently_playing_info = info) ⇒ Object

#

currently_playing_text

#


132
133
134
135
136
137
138
139
140
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 132

def currently_playing_text(
    currently_playing_info = info
  )
  if currently_playing_info && !currently_playing_info.strip.empty?
    [@playing_station_name, currently_playing_info].join(' - ')
  else
    @playing_station_name
  end
end

#infoObject

#

info

Run “info”.

#


147
148
149
150
151
152
153
154
155
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 147

def info
  result = io_command('info')
  now_playing_line = result.lines.find {|l| l.include?('now_playing:')}
  if now_playing_line
    now_playing_line.split('now_playing:').last.chomp.strip
  end
rescue
  nil
end

#play(url, station_name: 'N/A') ⇒ Object

#

play

#


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 82

def play(url, station_name: 'N/A')
  # Do not include spaces in the command line
  # if a space exist :
  #   * sh -c command url # this process with @pid will be killed
  #   * cmmand url        # will not be killd because pid is differennt
  # if no space :
  #   * cmmand url        # will be killed by @pid
  raise if url.match(/\s/)

  @playing_station_name = station_name

  if show_currently_playing? && backend.include?(USE_THIS_PLAYER) && !OS.windows?
    @io = IO.popen("#{backend} \"#{url}\"", 'r+')
    update_currently_playing
    continuously_fetch_currently_playing
  else
    @pid = spawn(*backend.split(' '), url)
    @thr = Process.detach(@pid)
  end

  @status = { thr: @thr, pid: @pid, io: @io }
  @history << @status
end

#resetObject

#

reset

#


47
48
49
50
51
52
53
54
55
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 47

def reset
  # ======================================================================= #
  # === @pid
  # ======================================================================= #
  @pid = nil
  @thr = nil
  @status = {}
  @history = []
end

#stop(thr: nil, pid: nil, io: nil) ⇒ Object

#

stop

#


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 160

def stop(thr: nil, pid: nil, io: nil)
  thr ||= @thr
  pid ||= @pid
  io  ||= @io
  if thr and thr.alive?
    result = Process.kill(OS.windows? ? :KILL : :TERM, pid)
  elsif io && !io.closed?
    io.close
    result = io.closed?
    self.currently_playing = CURRENTLY_PLAYING_NONE
  end
  thr = nil
  pid = nil
  io = nil
  return result
end

#stop?Boolean

#

stop?

#

Returns:

  • (Boolean)


75
76
77
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 75

def stop?
  @thr.nil? || @thr.stop?
end

#stop_allObject

#

stop_all

#


180
181
182
183
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 180

def stop_all
  @continuously_fetching_currently_playing = false
  @history.each { |history| stop(**history) }
end

#update_currently_playingObject

#

update_currently_playing

#


123
124
125
126
127
# File 'lib/multimedia_paradise/gui/glimmer/rubio/model/player.rb', line 123

def update_currently_playing
  Glimmer::LibUI.queue_main {
    self.currently_playing = currently_playing_text if alive?
   }
end