class MVLC::Player::Wrapper

Wrapper for video player functionality

Constants

VOLUME_FACTOR

Attributes

pid[R]
player[R]
state[R]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options

# File lib/mvlc/player/wrapper.rb, line 15
def initialize(options = {})
  @callback = {}
  @state = State.new
  @player = initialize_player
  @pid = player_pid
end

Public Instance Methods

active?() click to toggle source

Is the video player active? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 61
def active?
  !@player.nil?
end
add_end_of_file_callback(&block) click to toggle source

Add a callback to be called at the end of playback of a media file @param [Proc] block @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 94
def add_end_of_file_callback(&block)
  @callback[:end_of_file] = block
  true
end
add_progress_callback(&block) click to toggle source

Add a callback to be called when progress is updated during playback @param [Proc] block @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 86
def add_progress_callback(&block)
  @callback[:progress] = block
  true
end
method_missing(method, *args, &block) click to toggle source

Add all of the video player methods to the wrapper as instructions

Calls superclass method
# File lib/mvlc/player/wrapper.rb, line 107
def method_missing(method, *args, &block)
  if @player.respond_to?(method)
    @player.send(method, *args, &block)
  else
    super
  end
end
pause() click to toggle source

Toggles pause @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 67
def pause
  @state.toggle_pause
  @player.pause
  @state.pause?
end
play(file) click to toggle source

Play a media file @param [String] file @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 49
def play(file)
  if @player.nil?
    false
  else
    @player.play(file)
    handle_start
    true
  end
end
playback_loop() click to toggle source

Handle events while the player is running @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 75
def playback_loop
  loop do
    handle_playback_events
    sleep(0.05)
  end
  true
end
quit() click to toggle source

Exit the video player @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 101
def quit
  kill_player
  true
end
respond_to_missing?(method, include_private = false) click to toggle source

Add all of the video player methods to the wrapper as instructions

Calls superclass method
# File lib/mvlc/player/wrapper.rb, line 116
def respond_to_missing?(method, include_private = false)
  super || @player.respond_to?(method)
end
seek_percent(percent) click to toggle source

Seek to the given percent in the currently playing file Value is expected to be 0..100 @param [Integer] value @return [Integer]

# File lib/mvlc/player/wrapper.rb, line 37
def seek_percent(percent)
  begin
    length_in_seconds = @player.length
    seconds = length_in_seconds * (percent / 100.0)
    @player.seek(seconds)
  rescue VLC::ReadTimeoutError, VLC::BrokenConnectionError
  end
end
set_volume(value)
Alias for: volume=
volume=(value) click to toggle source

Set the volume level to the given value. Value is expected to be 0..100 but this is not strictly enforced @param [Integer] value @return [Integer]

# File lib/mvlc/player/wrapper.rb, line 26
def volume=(value)
  @state.volume = value * VOLUME_FACTOR
  @player.volume(@state.volume)
  @state.volume
end
Also aliased as: set_volume

Private Instance Methods

eof?() click to toggle source

Has the end of a media file been reached? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 218
def eof?
  @state.eof_possible? && !playing?
end
eof_callback?() click to toggle source

Has an EOF callback been specified? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 170
def eof_callback?
  !@callback[:end_of_file].nil?
end
handle_eof() click to toggle source

Handle the end of playback for a single media file @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 193
def handle_eof
  @callback[:end_of_file].call
  @state.handle_eof
  true
end
handle_eof?() click to toggle source

Should an EOF callback be fired? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 176
def handle_eof?
  eof? && eof_callback?
end
handle_playback_events() click to toggle source

Fire playback event callbacks when appropriate during the playback loop @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 125
def handle_playback_events
  handle_eof if handle_eof?
  handle_progress if handle_progress?
  true
end
handle_progress() click to toggle source

Fire the progress callback. Used during the playback loop @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 182
def handle_progress
  begin
    @callback[:progress].call(@player.progress)
    true
  rescue VLC::ReadTimeoutError
    false
  end
end
handle_progress?() click to toggle source

Should a progress callback be fired? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 158
def handle_progress?
  @state.progressing? && progress_callback?
end
handle_start() click to toggle source

Handle the beginning of playback for a single media file @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 201
def handle_start
  @state.handle_start
  true
end
initialize_player() click to toggle source

Instantiate the VLC player instance @return [VLC::System]

# File lib/mvlc/player/wrapper.rb, line 133
def initialize_player
  VLC::System.new(headless: true)
end
kill_player() click to toggle source

Kill the VLC player instance @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 145
def kill_player
  begin
    @player.connection.write("quit")
  rescue Errno::EBADF
  end
  # TODO: Process.kill not working here
  `kill -9 #{@pid}` unless @pid.nil?
  @player.server.stop
  true
end
player_pid() click to toggle source

The OS PID of the player @return [Integer]

# File lib/mvlc/player/wrapper.rb, line 139
def player_pid
  @player.server.instance_variable_get("@pid")
end
playing?() click to toggle source

Is media playing? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 208
def playing?
  begin
    @player.playing?
  rescue VLC::ReadTimeoutError, VLC::BrokenConnectionError
    false
  end
end
progress_callback?() click to toggle source

Has a progress callback been specified? @return [Boolean]

# File lib/mvlc/player/wrapper.rb, line 164
def progress_callback?
  !@callback[:progress].nil?
end