summaryrefslogtreecommitdiff
path: root/mpv/lua-modules/auto-profiles-functions.lua
blob: 539a84df9ae36f723ee694661ab57b10f5504a66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
local utils = require 'mp.utils'
local msg = require 'mp.msg'

-- Quality levels
local HIGH = "High Quality"
local MID  = "Mid Quality"
local LOW  = "Low Quality"
-- Platform
local is_linux, is_osx, is_windows = false, false, false
local exec_cache = {}


local function exec(process, force_exec)
    local key = table.concat(process, " ")
    if force_exec or exec_cache[key] == nil or exec_cache[key].error then
        local p_ret = utils.subprocess({args = process, playback_only = false})
        exec_cache[key] = p_ret
        if p_ret.error and p_ret.error == "init" then
            msg.error("executable not found: " .. key)
        end
        return p_ret
    else
        return exec_cache[key]
    end
end


local function get_platform()
    local is_linux = false
    local is_osx = false
    local is_windows = type(package) == 'table' and type(package.config) == 'string' and string.sub(package.config, 1, 1) == '\\'
    if not is_windows then
        uname = exec({"uname"})
        is_linux = uname.stdout == "Linux\n"
        is_osx = uname.stdout == "Darwin\n"
    end
    return is_linux, is_osx, is_windows
end


function on_battery()
    if is_osx then
        local bat = exec({"/usr/bin/pmset", "-g", "batt"}, true)
        return string.match(bat.stdout, "Now drawing from 'Battery Power'")
    elseif is_linux then
        local res = exec({"/bin/cat", "/sys/class/power_supply/AC0/online"}, true)
        return res.stdout == "0\n"
    elseif is_windows then
        msg.warn("on_battery() not implemented on windows. PRs welcome")
    end
    msg.warn("assuming AC power")
    return false
end


-- This is a crude attempt to figure out if a (beefy) dedicated GPU is present.
-- Can't identify the actually used GPU but works when we assume that an existing
-- dedicated GPU can/will be used in case we are drawing power from AC.
        -- local r = exec({"lshw", "-C", 'display'})
        -- local r = exec({"nvidia-smi"})
        -- r.stdout = string.lower(r.stdout)
        -- return string.find(r.stdout, "amd") ~= nil or string.find(r.stdout, "nvidia") ~= nil
        -- return string.find(r.stdout, "mpv") ~= nil
function dedicated_gpu()
    if is_osx then
        local r = exec({"system_profiler", "SPDisplaysDataType"})
        return string.find(r.stdout, "Chipset Model: Radeon") ~= nil or string.find(r.stdout, "Chipset Model: NVIDIA GeForce") ~= nil
    -- Untested
    elseif is_linux then
        local nv = os.getenv("__NV_PRIME_RENDER_OFFLOAD")
        return nv ~= nil
    elseif is_windows then
        msg.warn("dedicated_gpu() not implemented on windows. PRs welcome")
    end
    msg.warn("assuming dedicated GPU")
    return true
end


local function determine_level(width, height, fps)

    if on_battery() then
        return LOW
    end

    if dedicated_gpu() then
        if width > 4096 then
            return MID
        end
        if width > 1920 and fps > 61 then
            return MID
        end
        return HIGH
    else
        if width > 1919 then
            return LOW
        end
        if fps > 58 then
            return LOW
        end
        return MID
    end

    msg.error("could not determine profile")
    msg.warn("assuming HIGH")
    return HIGH
end


local function is_level(level)
    return function(width, height, fps)
        local l = determine_level(width, height, fps)
        return l == level
    end
end


is_high = is_level(HIGH)
is_mid = is_level(MID)
is_low = is_level(LOW)

is_linux, is_osx, is_windows = get_platform()