mpv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9709a37f45494f1a9eb585ffd75d5c5bde46216
4
+ data.tar.gz: 8fe87470f1136dfb73d8fd7c5b8c0224a2f10f51
5
+ SHA512:
6
+ metadata.gz: 6182028b78524c2f4df68fbc21623cb411fa9fdf03264d29de43d201315c2dbe5c57e87a18433eda6c5d8da92643d38aac8d04f9595cb5be7be0d71c82ab92e1
7
+ data.tar.gz: becd50aef26eee2637429b8f02df03c638d4af8b609f7051cc7e1d707987207ff74dc7e1fc32a028193f0522f8d27b12905595329d16539a126b4da2582a50c7
@@ -0,0 +1 @@
1
+ --tag command:"muzak command" --tag cmdexample:"example invocation" --no-private --markup-provider=redcarpet --markup=markdown - *.md LICENSE
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 William Woodruff <william @ tuffbizz.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ ruby-mpv
2
+ ========
3
+
4
+ A ruby library for controlling mpv processes.
5
+
6
+ ### Installation
7
+
8
+ ```bash
9
+ $ gem install mpv
10
+ ```
11
+
12
+ ### Example
13
+
14
+ For full documentation, please see the [RubyDocs]().
15
+
16
+ ```irb
17
+ # this will be called every time mpv sends an event back over the socket
18
+ def event(event)
19
+ puts "look ma! a callback: #{event}"
20
+ end
21
+
22
+ session = MPV::Session.new # contains both a MPV::Server and a MPV::Client
23
+ session.client.callbacks << self
24
+ session.client.get_property "pause"
25
+ session.client.command "get_version"
26
+ session.client.command "loadlist", "my_huge_playlist.txt", "append"
27
+ session.client.quit!
28
+ ```
@@ -0,0 +1,9 @@
1
+ require_relative "mpv/client"
2
+ require_relative "mpv/server"
3
+ require_relative "mpv/session"
4
+
5
+ # The toplevel namespace for ruby-mpv.
6
+ module MPV
7
+ # The current version of ruby-mpv.
8
+ VERSION = "0.0.1".freeze
9
+ end
@@ -0,0 +1,119 @@
1
+ require "socket"
2
+ require "json"
3
+ require "thread"
4
+
5
+ module MPV
6
+ # Represents a connection to a mpv process that has been spawned
7
+ # with an IPC socket.
8
+ # @see https://mpv.io/manual/stable/#json-ipc
9
+ # MPV's IPC docs
10
+ # @see https://mpv.io/manual/master/#properties
11
+ # MPV's property docs
12
+ class Client
13
+ # @return [String] the path of the socket used to communicate with mpv
14
+ attr_reader :socket_path
15
+
16
+ # @return [Array<Object>] objects whose #event method will be called
17
+ # whenever mpv emits an event
18
+ attr_accessor :callbacks
19
+
20
+ # @param path [String] the domain socket for communication with mpv
21
+ def initialize(path)
22
+ @socket_path = path
23
+
24
+ @socket = UNIXSocket.new(@socket_path)
25
+
26
+ @callbacks = []
27
+
28
+ @command_queue = Queue.new
29
+ @result_queue = Queue.new
30
+ @event_queue = Queue.new
31
+
32
+ @command_thread = Thread.new { pump_commands! }
33
+ @results_thread = Thread.new { pump_results! }
34
+ @events_thread = Thread.new { dispatch_events! }
35
+ end
36
+
37
+ # Sends a command to the mpv process.
38
+ # @param args [Array] the individual command arguments to send
39
+ # @return [Hash] mpv's response to the command
40
+ # @example
41
+ # client.command "loadfile", "mymovie.mp4", "append-play"
42
+ def command(*args)
43
+ payload = {
44
+ "command" => args
45
+ }
46
+
47
+ @command_queue << JSON.generate(payload)
48
+
49
+ @result_queue.pop
50
+ end
51
+
52
+ # Sends a property change to the mpv process.
53
+ # @param args [Array] the individual property arguments to send
54
+ # @return [Hash] mpv's response
55
+ # @example
56
+ # client.set_property "pause", true
57
+ def set_property(*args)
58
+ command "set_property", *args
59
+ end
60
+
61
+ # Retrieves a property from the mpv process.
62
+ # @param args [Array] the individual property arguments to send
63
+ # @return [Object] the value of the property
64
+ # @example
65
+ # client.get_property "pause" # => true
66
+ def get_property(*args)
67
+ command("get_property", *args)["data"]
68
+ end
69
+
70
+ # Terminates the mpv process.
71
+ # @return [void]
72
+ # @note this object becomes garbage once this method is run
73
+ def quit!
74
+ command "quit"
75
+ @socket = nil
76
+ File.delete(@socket_path) if File.exist?(@socket_path)
77
+ end
78
+
79
+ private
80
+
81
+ def pump_commands!
82
+ loop do
83
+ begin
84
+ @socket.puts(@command_queue.pop)
85
+ rescue EOFError # the player is deactivating
86
+ Thread.exit
87
+ end
88
+ end
89
+ end
90
+
91
+ def pump_results!
92
+ loop do
93
+ begin
94
+ response = JSON.parse(@socket.readline)
95
+
96
+ if response["event"]
97
+ @event_queue << response["event"]
98
+ else
99
+ @result_queue << response
100
+ end
101
+ rescue EOFError, IOError # the player is deactivating
102
+ Thread.exit
103
+ end
104
+ end
105
+ end
106
+
107
+ def dispatch_events!
108
+ loop do
109
+ event = @event_queue.pop
110
+
111
+ callbacks.each do |callback|
112
+ Thread.new do
113
+ callback.send :event, event if callback.respond_to?(:event)
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,40 @@
1
+ require "tempfile"
2
+
3
+ module MPV
4
+ # Represents an active mpv process.
5
+ class Server
6
+ # @return [Array<String>] the command-line arguments used when spawning mpv
7
+ attr_reader :args
8
+
9
+ # @return [String] the path to the socket used by this mpv process
10
+ attr_reader :socket_path
11
+
12
+ # @return [Fixnum] the process id of the mpv process
13
+ attr_reader :pid
14
+
15
+ # @param path [String] the path of the socket to be created
16
+ # (defaults to a tmpname in `/tmp`)
17
+ # @param user_args [Array<String>] additional arguments to use when
18
+ # spawning mpv
19
+ def initialize(path: Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock"),
20
+ user_args: [])
21
+ @socket_path = path
22
+ @args = [
23
+ "--idle",
24
+ "--no-terminal",
25
+ "--input-ipc-server=%{path}" % { path: @socket_path },
26
+ ] + user_args
27
+
28
+ @pid = Process.spawn("mpv", *@args)
29
+ end
30
+
31
+ # @return [Boolean] Whether or not the current instance is running.
32
+ def running?
33
+ begin
34
+ !!@pid && Process.waitpid(@pid, Process::WNOHANG).nil?
35
+ rescue Errno::ECHILD
36
+ false
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,31 @@
1
+ module MPV
2
+ # Represents a combined mpv "server" and "client" communicating over
3
+ # JSON IPC.
4
+ class Session
5
+ # @return [String] the path of the socket being used for communication
6
+ attr_reader :socket_path
7
+
8
+ # @return [MPV::Server] the server object responsible for the mpv process
9
+ attr_reader :server
10
+
11
+ # @return [MPV::Client] the client communicating with mpv
12
+ attr_reader :client
13
+
14
+ # @param path [String] the path of the socket to create
15
+ # (defaults to a tmpname in `/tmp`)
16
+ # @param user_args [Array<String>] additional arguments to use when
17
+ # spawning mpv
18
+ def initialize(path: Dir::Tmpname.make_tmpname("/tmp/mpv", ".sock"),
19
+ user_args: [])
20
+ @socket_path = path
21
+
22
+ @server = Server.new(path: @socket_path, user_args: user_args)
23
+
24
+ until File.exist?(@socket_path)
25
+ sleep 0.1
26
+ end
27
+
28
+ @client = Client.new(@socket_path)
29
+ end
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mpv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - William Woodruff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A library for creating and controlling mpv instances.
14
+ email: william@tuffbizz.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".yardopts"
20
+ - LICENSE
21
+ - README.md
22
+ - lib/mpv.rb
23
+ - lib/mpv/client.rb
24
+ - lib/mpv/server.rb
25
+ - lib/mpv/session.rb
26
+ homepage: https://github.com/woodruffw/ruby-mpv
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 2.0.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.5.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: mpv - A ruby library for controlling mpv processes.
50
+ test_files: []