add nosleep for android

peertube
Namekuji 2022-12-18 02:22:15 -05:00
rodzic 078a2c14a5
commit 45db6a98e0
6 zmienionych plików z 174 dodań i 13 usunięć

Wyświetl plik

@ -7,7 +7,7 @@
<title>Audon</title>
</head>
<body>
<div id="app" data-version='{%define "version"%}{%.%}{%end%}'></div>
<div id="app" data-version='%%VERSION%%'></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

Wyświetl plik

@ -5,6 +5,7 @@
"scripts": {
"dev": "vite",
"build": "vite build",
"hash": "sh -c 'sed -i -e \"s/%%VERSION%%/git-$(git rev-parse --short HEAD)/\" dist/index.html'",
"preview": "vite preview",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore"
},

File diff suppressed because one or more lines are too long

Wyświetl plik

@ -0,0 +1,153 @@
import { webm, mp4 } from "./media.js";
// Detect iOS browsers < version 10
const oldIOS = () =>
typeof navigator !== "undefined" &&
parseFloat(
(
"" +
(/CPU.*OS ([0-9_]{3,4})[0-9_]{0,1}|(CPU like).*AppleWebKit.*Mobile/i.exec(
navigator.userAgent
) || [0, ""])[1]
)
.replace("undefined", "3_2")
.replace("_", ".")
.replace("_", "")
) < 10 &&
!window.MSStream;
// Detect native Wake Lock API support (Samsung Browser supports it but cannot use it)
const nativeWakeLock = () =>
"wakeLock" in navigator &&
window.navigator.userAgent.indexOf("Samsung") === -1;
class NoSleep {
constructor() {
this.enabled = false;
if (nativeWakeLock()) {
this._wakeLock = null;
const handleVisibilityChange = () => {
if (this._wakeLock !== null && document.visibilityState === "visible") {
this.enable();
}
};
document.addEventListener("visibilitychange", handleVisibilityChange);
document.addEventListener("fullscreenchange", handleVisibilityChange);
} else if (oldIOS()) {
this.noSleepTimer = null;
} else {
// Set up no sleep video element
this.noSleepVideo = document.createElement("video");
this.noSleepVideo.setAttribute("title", "No Sleep");
this.noSleepVideo.setAttribute("playsinline", "");
this._addSourceToVideo(this.noSleepVideo, "webm", webm);
this._addSourceToVideo(this.noSleepVideo, "mp4", mp4);
// For iOS >15 video needs to be on the document to work as a wake lock
Object.assign(this.noSleepVideo.style, {
position: "absolute",
left: "-100%",
top: "-100%",
});
document.querySelector("body").append(this.noSleepVideo);
this.noSleepVideo.addEventListener("loadedmetadata", () => {
if (this.noSleepVideo.duration <= 1) {
// webm source
this.noSleepVideo.setAttribute("loop", "");
} else {
// mp4 source
this.noSleepVideo.addEventListener("timeupdate", () => {
if (this.noSleepVideo.currentTime > 0.5) {
this.noSleepVideo.currentTime = Math.random();
}
});
}
});
}
}
_addSourceToVideo(element, type, dataURI) {
var source = document.createElement("source");
source.src = dataURI;
source.type = `video/${type}`;
element.appendChild(source);
}
get isEnabled() {
return this.enabled;
}
enable() {
if (nativeWakeLock()) {
return navigator.wakeLock
.request("screen")
.then((wakeLock) => {
this._wakeLock = wakeLock;
this.enabled = true;
console.log("Wake Lock active.");
this._wakeLock.addEventListener("release", () => {
// ToDo: Potentially emit an event for the page to observe since
// Wake Lock releases happen when page visibility changes.
// (https://web.dev/wakelock/#wake-lock-lifecycle)
console.log("Wake Lock released.");
});
})
.catch((err) => {
this.enabled = false;
console.error(`${err.name}, ${err.message}`);
throw err;
});
} else if (oldIOS()) {
this.disable();
console.warn(`
NoSleep enabled for older iOS devices. This can interrupt
active or long-running network requests from completing successfully.
See https://github.com/richtr/NoSleep.js/issues/15 for more details.
`);
this.noSleepTimer = window.setInterval(() => {
if (!document.hidden) {
window.location.href = window.location.href.split("#")[0];
window.setTimeout(window.stop, 0);
}
}, 15000);
this.enabled = true;
return Promise.resolve();
} else {
let playPromise = this.noSleepVideo.play();
return playPromise
.then((res) => {
this.enabled = true;
return res;
})
.catch((err) => {
this.enabled = false;
throw err;
});
}
}
disable() {
if (nativeWakeLock()) {
if (this._wakeLock) {
this._wakeLock.release();
}
this._wakeLock = null;
} else if (oldIOS()) {
if (this.noSleepTimer) {
console.warn(`
NoSleep now disabled for older iOS devices.
`);
window.clearInterval(this.noSleepTimer);
this.noSleepTimer = null;
}
} else {
this.noSleepVideo.pause();
}
this.enabled = false;
}
}
export default NoSleep;

Wyświetl plik

@ -28,6 +28,7 @@ import {
import { login } from "masto";
import { useVuelidate } from "@vuelidate/core";
import { helpers, maxLength, required } from "@vuelidate/validators";
import NoSleep from "../assets/nosleep";
const publishOpts = {
audioBitrate: AudioPresets.music,
@ -121,6 +122,7 @@ export default {
showRequestedNotification: false,
isEditLoading: false,
showEditDialog: false,
noSleepHandler: new NoSleep(),
};
},
created() {
@ -134,10 +136,16 @@ export default {
// already being observed
{ immediate: true }
);
},
mounted() {
this.onResize();
},
async mounted() {
if (!this.noSleepHandler.isEnabled) {
try {
await this.noSleepHandler.enable();
} catch {}
}
},
computed: {
iamMuted() {
const myAudonID = this.donStore.oauth.audon_id;
@ -520,6 +528,9 @@ export default {
alert(this.$t("errors.connectionFailed"));
await this.roomClient.disconnect();
}
try {
await this.noSleepHandler.enable();
} catch {}
},
async onEditSubmit() {
this.editingRoomInfo.title = trim(this.editingRoomInfo.title);

Wyświetl plik

@ -74,7 +74,6 @@ func main() {
idx++
}
}
version := strings.Join(versionStrings, "-")
log.Println("Audon server started.")
@ -171,15 +170,7 @@ func main() {
api.PUT("/room/:room/:user", updatePermissionHandler)
e.Static("/assets", "audon-fe/dist/assets")
// e.File("/*", "audon-fe/dist/index.html")
if mainConfig.Environment != "development" {
e.Renderer = &Template{
templates: template.Must(template.New("tpl").Delims("{%", "%}").ParseFiles("audon-fe/dist/index.html")),
}
e.GET("/*", func(c echo.Context) error {
return c.Render(http.StatusOK, "version", version)
})
}
e.File("/*", "audon-fe/dist/index.html")
// use anonymous func to support graceful shutdown
go func() {