Class: Aniview::Interface::MPVBridge

Inherits:
Object
  • Object
show all
Includes:
Util, Observable
Defined in:
lib/aniview/interface/mpv/mpvbridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

decode_object, encode_object, error_message, format_duration, format_progress, format_size, mounted_filesystem?, parse_format, readline

Constructor Details

#initialize(pref) ⇒ MPVBridge

Returns a new instance of MPVBridge.



17
18
19
20
21
22
23
24
25
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 17

def initialize pref
  @pref = pref
  @mpv_enabled = true
  @logger = Logger.new(@pref.parseDir('$conf_dir/mpv.log'))
  @logger.level = Logger::DEBUG
  connect
  @what_changed = ""
  @playing = false
end

Instance Attribute Details

#playingObject

Returns the value of attribute playing.



13
14
15
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 13

def playing
  @playing
end

#playing_fileObject

Returns the value of attribute playing_file.



14
15
16
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 14

def playing_file
  @playing_file
end

#what_changedObject

Returns the value of attribute what_changed.



15
16
17
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 15

def what_changed
  @what_changed
end

Instance Method Details

#attributesObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 105

def attributes
  if playing?
    r = {}
    props = [
      "time-pos",
      "percent-pos",
      "duration"
    ]
    props.each { |prop|
      prop_val = @mpv.client.get_property(prop)
      prop_val = 0 if prop_val == nil
      r.merge!(
        prop[0] => prop_val
      )
    }
    
    if @playing_file != nil
      r.merge!("n" => @playing_file.attributes["t"])
    else
      r.merge!("n" => "loading")
    end
    
    checkSetWatched r["p"]
    
    {
      "t" => Util.format_duration(r["t"]),
      "p" => Util.format_progress(r["p"]),
      "d" => Util.format_duration(r["d"]),
      "n" => r["n"],
    }
  else
    {
      "t" => "-",
      "p" => "0",
      "d" => "0:00",
      "n" => ".",
    }
  end
end

#checkSetWatched(percentage) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 92

def checkSetWatched percentage
  if @playing_file != nil
    swp = Integer(@pref.get "set_watched_percentage")
    pct = Float(percentage)
    if pct >= swp and not @playing_file.seen?
      @playing_file.watch
      @what_changed = {:local_anime => :watched}
      changed
      notify_observers
    end
  end
end

#connectObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 54

def connect
  return unless @mpv_enabled
  return unless @mpv == nil or @mpv.client.get_property("idle-active") == nil
  @mpv = nil
  @mpv_enabled = false
  begin
    @mpv = MPV::Session.new(user_args: @pref.get("mpv_args").split(" "))
    @mpv.callbacks << MPV::Callback.new(self, :event_handler)
    @mpv.client.command("observe_property_string", 1, "path")
    #@mpv.client.command("observe_property_string", 1, "playback-abort")
  rescue MPV::MPVNotAvailableError
  rescue MPV::MPVUnsupportedFlagError
  else
    @mpv_enabled = true
  end
end

#event_handler(event) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 27

def event_handler event
  @logger.debug event
  case event["event"]
    #file-loaded
  when "start-file"
    @playing = true
    @what_changed = {:playing_status => :start}
    changed
    notify_observers
  when "end-file"
    @playing = false
    @what_changed = {:playing_status => :end}
    changed
    notify_observers
  when "property-change"
    case event["name"]
    when "path"
      @playing_file = @playing_files[event["data"]]
    end
  else
  end
end

#play(file) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 71

def play file
  return unless @mpv_enabled
  connect
  @playing_files = {file.path => file}
  @mpv.client.command "loadfile", file.path
  @playing = true
end

#playing?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 88

def playing?
  @playing
end

#playlist(playlist, files) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 79

def playlist playlist, files
  return unless @mpv_enabled
  connect
  @playing_files = files
  #@playing_file = @playing_files[0]
  @mpv.client.command "loadlist", playlist
  @playing = true
end

#quit!Object



50
51
52
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 50

def quit!
  @mpv.quit! unless @mpv == nil
end