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, parse_format, readline

Constructor Details

#initialize(pref) ⇒ MPVBridge

Returns a new instance of MPVBridge.



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

def initialize pref
  @pref = pref
  @mpv_enabled = true
  @logger = Logger.new('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

#what_changedObject

Returns the value of attribute what_changed.



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

def what_changed
  @what_changed
end

Instance Method Details

#attributesObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 85

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



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 72

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"
      changed
      notify_observers
    end
  end
end

#connectObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 45

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)
  rescue MPV::MPVNotAvailableError
  rescue MPV::MPVUnsupportedFlagError
  else
    @mpv_enabled = true
  end
end

#event_handler(event) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 26

def event_handler event
  case event
  when "start-file"
    @playing = true
    @what_changed = "playing_status"
    changed
    notify_observers
  when "end-file"
    @playing = false
    @what_changed = "playing_status"
    changed
    notify_observers
  end
end

#play(file) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 60

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

#playing?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 68

def playing?
  @playing
end

#quit!Object



41
42
43
# File 'lib/aniview/interface/mpv/mpvbridge.rb', line 41

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