mpv_client 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d60f3a4af287f1ac6b2b11747a70c321ccc391af20fad025f85cf8396d7a1ad4
4
+ data.tar.gz: efcf9a2bf9462cead535062df2e8c977de63ed6ecf4090115315fd97232a636c
5
+ SHA512:
6
+ metadata.gz: 01304ea3875cc0d379b4e66ccaedb231fa584101eb258d6b398a0457a559f299ff813916895c9c51959042a97e2c40d2109172ffd29853781fa4ecc00b59709b
7
+ data.tar.gz: b27f90410619d5f6ae3c23663f7ed0e3028eb61e0e8955c0f74fefd027f0753b836b3e8a5922bbde6eb9816607c052bf20c7564a8e197965971f87f086660ff7
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ Gemfile.lock
3
+ *.gem
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 John Labovitz
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.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # mpv_client
2
+
3
+ FIXME
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'simple-rake-tasks'
data/lib/mpv_client.rb ADDED
@@ -0,0 +1,114 @@
1
+ require 'json'
2
+ require 'socket'
3
+
4
+ class MPVClient
5
+
6
+ class Error < StandardError; end
7
+
8
+ attr_accessor :socket
9
+
10
+ def initialize(mpv_params={})
11
+ @requests = {}
12
+ @property_observers = {}
13
+ @event_observers = {}
14
+ @request_id = @property_observer_id = 0
15
+ @socket_path = '/tmp/mpv_socket'
16
+ @mpv_params = {
17
+ 'idle' => 'yes',
18
+ 'input-ipc-server' => @socket_path,
19
+ }.merge(mpv_params).compact
20
+ start
21
+ register_event('property-change') do |event|
22
+ observer = @property_observers[event['name']] \
23
+ or raise Error, "Can't find property observer for event: #{event.inspect}"
24
+ observer.call(event['name'], event['data'])
25
+ end
26
+ end
27
+
28
+ def start
29
+ cmd = ['mpv'] + @mpv_params.map { |k, v| "--#{k}" + (v.to_s.empty? ? '' : "=#{v}") }
30
+ # ;;pp(cmd: cmd)
31
+ File.unlink(@socket_path) if File.exist?(@socket_path)
32
+ @pid = Process.spawn(*cmd)
33
+ raise Error, "mpv failed to start" unless @pid > 0
34
+ until File.exist?(@socket_path)
35
+ # ;;puts "[sleeping until socket path exists]"
36
+ sleep 0.1
37
+ if Process.waitpid(@pid, Process::WNOHANG)
38
+ raise Error, "mpv failed to start (exit status #{$?.exitstatus.inspect})"
39
+ end
40
+ end
41
+ @socket = UNIXSocket.new(@socket_path)
42
+ end
43
+
44
+ def stop
45
+ @socket.close
46
+ @socket = nil
47
+ Process.kill('INT', @pid)
48
+ # ;;puts "[waiting for mpv to exit: #{@pid}]"
49
+ Process.waitpid(@pid)
50
+ # ;;puts "[mpv exited with status #{$?.exitstatus.inspect}]"
51
+ @pid = nil
52
+ File.unlink(@socket_path) if File.exist?(@socket_path)
53
+ end
54
+
55
+ def command(*args)
56
+ # ;;puts "COMMAND: #{args.inspect}"
57
+ responded = false
58
+ result = nil
59
+ @requests[@request_id] = proc do |response|
60
+ case response['error']
61
+ when 'success'
62
+ responded = true
63
+ result = response['data']
64
+ else
65
+ raise Error, "Response error: #{response.inspect}"
66
+ end
67
+ end
68
+ request = { 'command' => args, 'request_id' => @request_id }
69
+ # ;;puts "=> #{request.inspect}"
70
+ @socket.puts(JSON.generate(request))
71
+ @request_id += 1
72
+ process_response until responded
73
+ result
74
+ end
75
+
76
+ def process_response
77
+ response = JSON.load(@socket.gets) or return
78
+ # ;;puts "<= #{response.inspect}"
79
+ if (name = response['event'])
80
+ if (observer = @event_observers[name])
81
+ observer.call(response)
82
+ end
83
+ elsif (request_id = response['request_id'])
84
+ handler = @requests.delete(request_id) \
85
+ or raise Error, "No handler for response: #{response.inspect}"
86
+ handler.call(response)
87
+ else
88
+ raise Error, "Unhandled response: #{response.inspect}"
89
+ end
90
+ end
91
+
92
+ def unpack_version(packed)
93
+ [packed >> 16, packed & 0x00FF]
94
+ end
95
+
96
+ def set_property(name, value)
97
+ command('set_property', name, value)
98
+ end
99
+
100
+ def get_property(name)
101
+ command('get_property', name)
102
+ end
103
+
104
+ def observe_property(name, &block)
105
+ @property_observers[name] = block
106
+ command('observe_property', @property_observer_id, name)
107
+ @property_observer_id += 1
108
+ end
109
+
110
+ def register_event(name, &block)
111
+ @event_observers[name] = block
112
+ end
113
+
114
+ end
@@ -0,0 +1,26 @@
1
+ #encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'mpv_client'
5
+ s.version = '0.3'
6
+ s.summary = %q{Ruby bindings to the MPV media player, via MPV's JSON-IPC protocol.}
7
+ s.description = %q{
8
+ MPVClient provides Ruby bindings to the MPV media player, via MPV's JSON-IPC protocol.
9
+ }
10
+ s.author = 'John Labovitz'
11
+ s.email = 'johnl@johnlabovitz.com'
12
+ s.homepage = 'http://github.com/jslabovitz/mpv_client'
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ s.require_path = 'lib'
19
+
20
+ s.add_dependency 'json', '~> 2.15'
21
+
22
+ s.add_development_dependency 'minitest', '~> 5.26'
23
+ s.add_development_dependency 'minitest-power_assert', '~> 0.3'
24
+ s.add_development_dependency 'rake', '~> 13.3'
25
+ s.add_development_dependency 'simple-rake-tasks', '~> 0.1'
26
+ end
@@ -0,0 +1,106 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'mpv_client'
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/power_assert'
6
+
7
+ class MPVClient
8
+
9
+ class ClientTest < Minitest::Test
10
+
11
+ # i_suck_and_my_tests_are_order_dependent!
12
+
13
+ def setup
14
+ # ;;puts
15
+ @mpv = MPVClient.new
16
+ @logs = []
17
+ @mpv.register_event('log-message') do |event|
18
+ # ;;puts "** #{event.inspect}"
19
+ @logs << event
20
+ end
21
+ @mpv.command('request_log_messages', 'status')
22
+ end
23
+
24
+ def shutdown
25
+ @mpv.command('quit')
26
+ @mpv.stop
27
+ end
28
+
29
+ def test_version
30
+ version = @mpv.command('get_version')
31
+ assert { version != nil }
32
+ version = @mpv.unpack_version(version)
33
+ assert { version == [1, 109] }
34
+ end
35
+
36
+ def test_client_name
37
+ client_name = @mpv.command('client_name')
38
+ assert { client_name == 'ipc_0' }
39
+ end
40
+
41
+ def test_time_us
42
+ time = @mpv.command('get_time_us')
43
+ assert { time.kind_of?(Numeric) }
44
+ assert { time > 0 }
45
+ end
46
+
47
+ def test_good_command
48
+ assert {
49
+ @mpv.command('stop')
50
+ true
51
+ }
52
+ end
53
+
54
+ def test_bad_command
55
+ assert_raises(MPVClient::Error) {
56
+ @mpv.command('xyzzy')
57
+ }
58
+ end
59
+
60
+ def test_set_get_property
61
+ expected_value = 50.0
62
+ @mpv.set_property('volume', expected_value)
63
+ actual_value = @mpv.get_property('volume')
64
+ assert {
65
+ actual_value == expected_value
66
+ }
67
+ end
68
+
69
+ def test_observe_property
70
+ expected_value = 50.0
71
+ actual_value = nil
72
+ stop = false
73
+ @mpv.observe_property('volume') do |name, value|
74
+ actual_value = value
75
+ stop = true
76
+ end
77
+ @mpv.set_property('volume', expected_value)
78
+ until stop
79
+ @mpv.process_response
80
+ end
81
+ assert {
82
+ actual_value == expected_value
83
+ }
84
+ end
85
+
86
+ def test_process_events
87
+ volume = nil
88
+ @mpv.observe_property('volume') do |name, value|
89
+ volume = value
90
+ end
91
+ first = true
92
+ until volume == 50.0
93
+ if (IO.select([@mpv.socket], [], [], 1))
94
+ @mpv.process_response
95
+ else
96
+ if first
97
+ @mpv.set_property('volume', 50.0)
98
+ first = false
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mpv_client
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.3'
5
+ platform: ruby
6
+ authors:
7
+ - John Labovitz
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: json
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.15'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.15'
26
+ - !ruby/object:Gem::Dependency
27
+ name: minitest
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.26'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.26'
40
+ - !ruby/object:Gem::Dependency
41
+ name: minitest-power_assert
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.3'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '0.3'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '13.3'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '13.3'
68
+ - !ruby/object:Gem::Dependency
69
+ name: simple-rake-tasks
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.1'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.1'
82
+ description: "\n MPVClient provides Ruby bindings to the MPV media player, via
83
+ MPV's JSON-IPC protocol.\n "
84
+ email: johnl@johnlabovitz.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".gitignore"
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/mpv_client.rb
94
+ - mpv_client.gemspec
95
+ - test/client_test.rb
96
+ homepage: http://github.com/jslabovitz/mpv_client
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.7.2
115
+ specification_version: 4
116
+ summary: Ruby bindings to the MPV media player, via MPV's JSON-IPC protocol.
117
+ test_files:
118
+ - test/client_test.rb