-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #386 from Mindgamesnl/feature/preloading-plus-comm…
…ands Fix syncing, prebuffering, and add command
- Loading branch information
Showing
17 changed files
with
219 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"buildMajor":1,"buildMinor":125,"buildRevision":194,"buildTag":"dev","buildDate":"Sun Jan 14 2024","build":"1.125.194 dev"} | ||
{"buildMajor":1,"buildMinor":125,"buildRevision":195,"buildTag":"dev","buildDate":"Thu Jan 25 2024","build":"1.125.195 dev"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { AudioSourceProcessor } from '../../util/AudioSourceProcessor'; | ||
import { PreloadedMedia } from './PreloadedMedia'; | ||
import { debugLog, feedDebugValue } from '../debugging/DebugService'; | ||
import { DebugStatistic } from '../debugging/DebugStatistic'; | ||
|
||
export const AudioPreloader = new class IAudPreload { | ||
constructor() { | ||
this.sourceRewriter = new AudioSourceProcessor(); | ||
this.namespaces = {}; // each namespace has a list of media | ||
} | ||
|
||
async fetch(source, namespace) { | ||
source = await this.sourceRewriter.translate(source); | ||
const media = new PreloadedMedia(source, namespace); | ||
|
||
if (this.namespaces[namespace] == null) { | ||
this.namespaces[namespace] = []; | ||
} | ||
|
||
this.namespaces[namespace].push(media); | ||
this.submitStatistic(); | ||
} | ||
|
||
drop(namespace) { | ||
// does the namespace exist? | ||
if (this.namespaces[namespace] == null) { | ||
return; | ||
} | ||
|
||
// loop through all media in the namespace | ||
this.namespaces[namespace].forEach((media) => { | ||
// remove the media from the source | ||
media.preDelete(); | ||
}); | ||
|
||
// delete the namespace | ||
delete this.namespaces[namespace]; | ||
this.submitStatistic(); | ||
} | ||
|
||
findAndRemoveMedia(source) { | ||
// loop through all namespaces | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const namespace in this.namespaces) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (this.namespaces.hasOwnProperty(namespace)) { | ||
// loop through all media in the namespace | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const media of this.namespaces[namespace]) { | ||
// does the media match the source? | ||
if (media.source === source) { | ||
// this is the one we want! now also remove it from the namespace | ||
const countPre = this.namespaces[namespace].length; | ||
this.namespaces[namespace].splice(this.namespaces[namespace].indexOf(media), 1); | ||
const countPost = this.namespaces[namespace].length; | ||
if (countPre === countPost) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Could not remove media from namespace'); | ||
} else { | ||
this.submitStatistic(); | ||
} | ||
return media; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async getResource(source) { | ||
source = await this.sourceRewriter.translate(source); | ||
|
||
// find a preloaded media that matches the source | ||
let media = this.findAndRemoveMedia(source); | ||
|
||
if (media == null) { | ||
// create new | ||
media = new PreloadedMedia(source, null); | ||
} else { | ||
debugLog(`Using preloaded media, found ${media.source} in namespace ${media.namespace}, and it already has ready state ${media.audio.readyState}`); | ||
} | ||
|
||
return media.audio; | ||
} | ||
|
||
submitStatistic() { | ||
let count = 0; | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const namespace in this.namespaces) { | ||
// eslint-disable-next-line no-prototype-builtins | ||
if (this.namespaces.hasOwnProperty(namespace)) { | ||
count += this.namespaces[namespace].length; | ||
} | ||
} | ||
feedDebugValue(DebugStatistic.PRELOADED_SOUNDS, count); | ||
} | ||
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export class PreloadedMedia { | ||
constructor(source, namespace) { | ||
this.source = source; | ||
this.namespace = namespace; | ||
|
||
const soundElement = new Audio(); | ||
soundElement.autoplay = false; | ||
soundElement.src = source; | ||
soundElement.load(); | ||
|
||
this.audio = soundElement; | ||
} | ||
|
||
preDelete() { | ||
// optionally do something before the media is deleted | ||
} | ||
} |
27 changes: 18 additions & 9 deletions
27
client/src/client/services/socket/handlers/HandlePrefetch.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import { ClearPrefetchedMedia, PreFetch } from '../../../util/AudioFactory'; | ||
import { getGlobalState } from '../../../../state/store'; | ||
import { AudioPreloader } from '../../preloading/AudioPreloader'; | ||
|
||
export function handlePrefetchPacket(data) { | ||
if (data.clear) { | ||
// clear all prefetched bullshit | ||
const { clear, source } = data; | ||
let { origin } = data; | ||
// clear = bool, whether the origin context should be cleared | ||
// origin = string, the origin context | ||
// source = untranslated media source | ||
|
||
// if origin is null, default to global | ||
if (origin == null) { | ||
origin = 'global'; | ||
} | ||
|
||
if (clear) { | ||
setTimeout(() => { | ||
ClearPrefetchedMedia(); | ||
}, 2500); | ||
AudioPreloader.drop(origin); | ||
}, 500); | ||
} else { | ||
if (!getGlobalState().settings.prefetchMedia) { | ||
return; | ||
} | ||
const toFetch = data.source; | ||
// fetch a file | ||
|
||
setTimeout(() => { | ||
PreFetch(toFetch); | ||
}, 2500); | ||
AudioPreloader.fetch(source, origin); | ||
}, 500); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"buildMajor":1,"buildMinor":125,"buildRevision":194,"buildTag":"dev","buildDate":"Sun Jan 14 2024","build":"1.125.194 dev"} | ||
{"buildMajor":1,"buildMinor":125,"buildRevision":195,"buildTag":"dev","buildDate":"Thu Jan 25 2024","build":"1.125.195 dev"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
BUILD_NUM="1056" | ||
BUILD_NUM="1058" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...c/main/java/com/craftmend/openaudiomc/generic/commands/subcommands/PreloadSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.craftmend.openaudiomc.generic.commands.subcommands; | ||
|
||
import com.craftmend.openaudiomc.api.interfaces.Client; | ||
import com.craftmend.openaudiomc.generic.client.objects.ClientConnection; | ||
import com.craftmend.openaudiomc.generic.commands.interfaces.SubCommand; | ||
import com.craftmend.openaudiomc.generic.commands.objects.Argument; | ||
import com.craftmend.openaudiomc.generic.networking.packets.client.media.PacketClientPreFetch; | ||
import com.craftmend.openaudiomc.generic.networking.payloads.client.media.ClientPreFetchPayload; | ||
import com.craftmend.openaudiomc.generic.platform.OaColor; | ||
import com.craftmend.openaudiomc.generic.user.User; | ||
|
||
import java.util.Optional; | ||
|
||
public class PreloadSubCommand extends SubCommand { | ||
|
||
public PreloadSubCommand() { | ||
super("preload"); | ||
registerArguments( | ||
new Argument("<selector> <source>", "Attempt to preload a sound for all players in a selection") | ||
); | ||
} | ||
|
||
@Override | ||
public void onExecute(User sender, String[] args) { | ||
if (args.length == 0) { | ||
sender.makeExecuteCommand("oa help " + getCommand()); | ||
return; | ||
} | ||
|
||
if (args.length == 2) { | ||
int affected = 0; | ||
|
||
ClientPreFetchPayload payload = new ClientPreFetchPayload(args[1], "command", false); | ||
|
||
for (User<?> user : resolveSelector(sender, args[0])) { | ||
Optional<Client> client = user.findClient(); | ||
if (client.isPresent()) { | ||
if (client.get().isConnected()) affected++; | ||
ClientConnection clientConnection = (ClientConnection) client.get(); | ||
clientConnection.sendPacket(new PacketClientPreFetch(payload)); | ||
} | ||
} | ||
message(sender, OaColor.GREEN + "Requested " + affected + " web-clients to preload the sound"); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
BUILD_NUM="1056" | ||
BUILD_NUM="1058" |
Oops, something went wrong.