Sha256: 68164d58850242271d95bc9d67ade92e72870923879add8eb520aa80bd8f6663

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2016
# License: GNU GPLv3 (or later) <http://www.gnu.org/copyleft/gpl.html>

module MPV
  class Slave
    def initialize
      socket = Dir::Tmpname.make_tmpname "/tmp/mpv-slave", ".socket"

      @pid = Process.spawn(
        "mpv",
        "--idle",
        "--really-quiet",
        "--input-unix-socket", socket,
      )
      puts "waiting for mpv to start up..."
      until File.exists?(socket) or not running?
        sleep 0.1
      end
      raise "failed to start mpv" if not running?

      @socket = Socket.new(socket)
    end

    def running?
      begin
        Process.waitpid(@pid, Process::WNOHANG) == nil
      rescue
        false
      end
    end

    # generic commands
    def command *args
      response = @socket.command(*args)
      response
    end

    def play file, mode: "replace", wait: true
      file = File.expand_path file
      raise "no such file: #{file}" unless File.exists? file
      command "loadfile", file, mode

      wait_for "playback-restart" if wait
    end

    def add file
      play file, mode: "append-play", wait: false
    end

    def get prop
      command "get_property", prop
    end

    def set prop, value
      command "set_property", prop, value
    end

    def toggle prop
      set(prop, !get(prop))
    end

    def wait_for event
      @socket.wait_for event
    end

    def wait_while prop, val=true
      while get(prop) == val
        sleep 0.01
      end
    end

    def enable_event event
      command "enable_event", event
    end

    def disable_event event
      command "disable_event", event
    end

    def enable_events
      enable_event "all"
    end

    def disable_events
      disable_event "all"
    end

    def close
      Process.kill :SIGTERM, @pid
      Process.wait @pid
      @socket.close
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mpv-slave-0.1 lib/mpv-slave/mpv.rb