vlc-client 0.0.1.beta

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ .sublime/
20
+ Guardfile
21
+ trials
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -r ./spec/helper.rb -c -f documentation
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ script: bundle exec rake --verbose --trace
3
+ matrix:
4
+ allow_failures:
5
+ - rvm: ruby-head
6
+ rvm:
7
+ - 1.8.7
8
+ - 1.9.2
9
+ - 1.9.3
10
+ - ruby-head
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ --no-private --protected --markup markdown lib/**/*.rb - README.md LICENSE
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vlc.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Miguel Guinada
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # vlc-client [![Build Status](https://secure.travis-ci.org/mguinada/vlc-client.png?branch=master)](http://travis-ci.org/mguinada/vlc-client)
2
+
3
+ vlc-client manages a [VLC media player](http://www.videolan.org/vlc/) through it's RC interface.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vlc-client'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vlc-client
18
+
19
+ ## Usage
20
+
21
+ ### Create a client and connect to a running VLC media play instance.
22
+
23
+ ```ruby
24
+
25
+ #Expects a VLC media player running on 192.168.1.10:9999
26
+ #e.g. vlc --extraintf rc --rc-host 192.168.1.10:9999"
27
+
28
+ vlc = VLC::Client.new('192.168.1.10', 9999)
29
+ # => "#<VLC::Client:0x0000000229c370 @server=#<VLC::Server:0x0000000229c6e0 @headless=false, @port=9999, @host=\"192.168.1.10\", @pid=10514>, @self_managed=true, @connection=#<VLC::Connection:0x0000000229c258 @port=9999, @host=\"192.168.1.10\", @socket=#<TCPSocket:fd 5>>>"
30
+
31
+ vlc.connect
32
+ # => true
33
+ vlc.play('http://example.org/media.mp3') #play media
34
+ => true
35
+ vlc.playing?
36
+ => true
37
+ vlc.fullscreen
38
+ => true
39
+ #...
40
+
41
+ ```
42
+
43
+ ### Create a self managed client/server system.
44
+ _(requires a local installation of VLC media player)_
45
+
46
+ ```ruby
47
+
48
+ vlc = VLC::System.new #A local VLC client/server system where a local VLC server is automaticaly managed
49
+ # => "#<VLC::System:0x00000001bbb1a0 @client=#<VLC::Client:0x00000001bbade0 @server=#<VLC::Server:0x00000001bbb178 @headless=false, @port=9595, @host=\"localhost\", @pid=11225>, @connection=#<VLC::Connection:0x00000001bbacc8 @port=9595, @host=\"localhost\", @socket=#<TCPSocket:fd 5>>>>"
50
+
51
+ vlc.connected? #auto_connect
52
+ => true
53
+
54
+ vlc.play('http://example.org/media.mp3')
55
+ => true
56
+
57
+ vlc.progress
58
+ => 1 #%
59
+ #...
60
+
61
+ #Technically this the same as
62
+ vlc = VLC::Client.new(VLC::Server.new('localhost', 9595, false))
63
+ # => "#<VLC::Client:0x000000011de128 @server=#<VLC::Server:0x000000011de380 @headless=false, @port=9595, @host=\"localhost\", @pid=12656>, @connection=#<VLC::Connection:0x000000011de038 @port=9595, @host=\"localhost\", @socket=#<TCPSocket:fd 5>>>"
64
+ ```
65
+
66
+ ###Get local VLC server lifecycle management control
67
+ _(requires a local installation of VLC media player)_
68
+
69
+ ```ruby
70
+
71
+ vlc = VLC::System.new('127.0.0.1', 9999, auto_start: false)
72
+ => "#<VLC::System:0x00000001695f68 @client=#<VLC::Client:0x0000000169d718 @server=#<VLC::Server:0x00000001695ec8 @headless=false, @port=9999, @host=\"127.0.0.1\", @pid=VLC::NullObject>, @connection=#<VLC::Connection:0x0000000169d588 @port=9999, @host=\"127.0.0.1\", @socket=VLC::NullObject>>>"
73
+
74
+ vlc.server.running?
75
+ # => false
76
+ vlc.server.start
77
+ # => 12672
78
+ vlc.connect
79
+ # => true
80
+ ```
81
+
82
+ ## Reference
83
+
84
+ [reference](http://rdoc.info/github/mguinada/vlc-client)
85
+
86
+ ## Support
87
+
88
+ vlc-client has been tested on linux only.
89
+ If you need support for other OS a pull request is welcome.
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it
94
+ 2. Create your topic branch (`git checkout -b my-topic-branch`)
95
+ 3. Add/change specs for your unimplemented feature or bug fix
96
+ 4. Make sure specs fail by running `bundle exec rake spec`. If not return to step 3
97
+ 5. Hack it
98
+ 6. Make sure specs pass (`bundle exec rake spec`). If not return to step 5
99
+ 7. Edit the documentation so it is coherent with your feature or fix. Run `bundle exec rake yard` to review
100
+ 8. Commit changes (`git commit -am 'Add some feature/fix'`) and push to the branch (`git push origin my-topic-branch`)
101
+ 9. Submit a pull request for your branch.
102
+
103
+ ## Copyright
104
+
105
+ Copyright (c) 2012 Miguel Guinada
106
+ [LICENSE][] for details.
107
+
108
+ [license]: https://github.com/mguinada/vlc-client/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+ require 'yard'
5
+
6
+ task :test => :spec
7
+ task :default => :spec
8
+
9
+ RSpec::Core::RakeTask.new do |task|
10
+ task.rspec_opts = ["-c", "-f documentation", "-r ./spec/helper.rb"]
11
+ task.pattern = 'spec/**/*_spec.rb'
12
+ end
13
+
14
+ desc "Open an interactive session preloaded with this gem's code"
15
+ task :console do
16
+ if gem_available?("pry")
17
+ sh "pry -I lib -r vlc-client.rb"
18
+ else
19
+ sh "irb -rubygems -I lib -r vlc-client.rb"
20
+ end
21
+ end
22
+
23
+ namespace :doc do
24
+ desc "Generate documentation"
25
+ YARD::Rake::YardocTask.new do |t|
26
+ t.files = ['lib/**/*.rb']
27
+ t.options = ['--no-private', '--protected', '--markup', 'markdown']
28
+ end
29
+ end
30
+
31
+ #
32
+ # Determins if a gem is available at the current runtime
33
+ #
34
+ def gem_available?(name)
35
+ Gem::Specification.find_by_name(name)
36
+ rescue Gem::LoadError
37
+ false
38
+ rescue
39
+ Gem.available?(name) #for backwards compatibility
40
+ end
data/lib/vlc-client.rb ADDED
@@ -0,0 +1,102 @@
1
+ require 'uri'
2
+ require 'socket'
3
+ require 'retryable'
4
+
5
+ require 'vlc-client/version'
6
+
7
+ require 'vlc-client/null_object'
8
+
9
+ require 'vlc-client/core_ext/array'
10
+
11
+ require 'vlc-client/server'
12
+ require 'vlc-client/connection'
13
+ require 'vlc-client/errors'
14
+
15
+ require 'vlc-client/client/media_controls'
16
+ require 'vlc-client/client/video_controls'
17
+ require 'vlc-client/client/connection_management'
18
+
19
+ require 'vlc-client/system'
20
+
21
+ module VLC
22
+ # The VLC client
23
+ class Client
24
+ include VLC::Client::MediaControls
25
+ include VLC::Client::VideoControls
26
+ include VLC::Client::ConnectionManagement
27
+
28
+ attr_accessor :host,
29
+ :port,
30
+ :server
31
+
32
+ # Creates a client to manage VLC media player
33
+ #
34
+ # @overload initialize(host, port) sets the host and port
35
+ #
36
+ # @param [String] host The ip to connect to
37
+ # @param [Integer] port the port
38
+ #
39
+ # @example
40
+ # vlc = VLC::Client.new('10.10.0.10', 9000)
41
+ #
42
+ # @overload initialize(server, options) handle a server wrapper for a self-managed operation mode. This requires a local VLC media play instalation.
43
+ #
44
+ # @param [Server] server a VLC server lifecycle manager
45
+ # @param [Hash] options
46
+ # @option options [Boolean] :auto_start When false, the server lifecycle is not managed automatically and controll is passed to the developer
47
+ # @option options [Integer] :conn_retries Number of connection retries (each separated by a second) to make on auto-connect. Defaults to 5.
48
+ #
49
+ # @example
50
+ # vlc = VLC::Client.new(VLC::Server.new)
51
+ # vlc.server.started?
52
+ # => true
53
+ #
54
+ # vlc = VLC::Client.new(VLC::Server.new, auto_start: false)
55
+ # vlc.server.started?
56
+ # => false
57
+ #
58
+ # @return [VLC::VLC] a VLC client
59
+ #
60
+ # @raise [VLC::ConnectionRefused] if the connection fails
61
+ #
62
+ def initialize(*args)
63
+ args = NullObject.Null?(args)
64
+ options = args.extract_options!
65
+
66
+ process_args(args)
67
+ @connection = Connection.new(host, port)
68
+ bind_server(server, options) unless server.nil?
69
+ end
70
+
71
+ private
72
+ attr_reader :connection
73
+
74
+ def bind_server(server, options = {})
75
+ @connection.host = server.host
76
+ @connection.port = server.port
77
+
78
+ if options.fetch(:auto_start, true)
79
+ begin
80
+ @server.start
81
+ retryable(:tries => options.fetch(:conn_retries, 5), :on => VLC::ConnectionRefused) { connect }
82
+ rescue VLC::ConnectionRefused => e
83
+ @server.stop
84
+ raise e
85
+ end
86
+ end
87
+ end
88
+
89
+ def process_args(args)
90
+ case args.size
91
+ when 1
92
+ @server = args.first
93
+ when 2
94
+ @host = args.first.to_s
95
+ @port = Integer(args.last)
96
+ else
97
+ @host, @port = 'localhost', 9595
98
+ end
99
+ args
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,23 @@
1
+ module VLC
2
+ class Client
3
+ module ConnectionManagement
4
+ # Connects to VLC RC interface on Client#host and Client#port
5
+ def connect
6
+ connection.connect
7
+ end
8
+
9
+ # Disconnects from VLC RC interface
10
+ def disconnect
11
+ connection.disconnect
12
+ end
13
+
14
+ # Queries if there is an active connection to VLC RC interface
15
+ #
16
+ def connected?
17
+ connection.connected?
18
+ end
19
+
20
+ def disconnected?; not connected?; end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,98 @@
1
+ module VLC
2
+ class Client
3
+ module MediaControls
4
+ # Plays media or resumes playback
5
+ #
6
+ # @overload play(media)
7
+ # addes the given media and plays it
8
+ #
9
+ # @param media [String, File, URI] the media to be played
10
+ #
11
+ # @example
12
+ # vlc.play('http://example.org/media.mp3')
13
+ #
14
+ # @overload play
15
+ # plays the current media or resume playback is paused
16
+ #
17
+ # @example
18
+ # vlc.play('http://example.org/media.mp3')
19
+ # vlc.pause
20
+ # vlc.play #resume playback
21
+ #
22
+ def play(media = nil)
23
+ connection.write(media.nil? ? "play" : "add #{media_arg(media)}")
24
+ end
25
+
26
+ # Pauses playback
27
+ #
28
+ def pause
29
+ connection.write("pause")
30
+ end
31
+
32
+ # Stops media currently playing
33
+ #
34
+ def stop
35
+ connection.write("stop")
36
+ end
37
+
38
+ # Gets the title of the media at play
39
+ #
40
+ def title
41
+ connection.write("get_title", false)
42
+ end
43
+
44
+ # Gets the current playback progress in time
45
+ #
46
+ # @return [Integer] time in seconds
47
+ #
48
+ def time
49
+ Integer(connection.write("get_time", false))
50
+ rescue ArgumentError
51
+ 0
52
+ end
53
+
54
+ # Gets the length of the media being played
55
+ #
56
+ # @return [Integer] time in seconds
57
+ #
58
+ def length
59
+ Integer(connection.write("get_length", false))
60
+ rescue ArgumentError
61
+ 0
62
+ end
63
+
64
+ # Get the progress of the the media being played
65
+ #
66
+ # @return [Integer] a relative value on percentage
67
+ #
68
+ def progress
69
+ l = length
70
+ l.zero? ? 0 : 100 * time / l
71
+ end
72
+
73
+ # Queries VLC if media is being played
74
+ #
75
+ def playing?
76
+ connection.write("is_playing", false) == "1"
77
+ end
78
+
79
+ # Queries VLC if playback is currently stopped
80
+ #
81
+ def stopped?
82
+ connection.write("is_playing", false) == "0"
83
+ end
84
+
85
+ private
86
+ def media_arg(media)
87
+ case media
88
+ when File
89
+ media.path
90
+ when String, URI
91
+ media
92
+ else
93
+ raise ArgumentError, "Can play: #{media}"
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end