add seek functionality

This commit is contained in:
KGT1 2020-09-23 16:47:12 +02:00
parent 4a65cceca3
commit 242ae2ffff
4 changed files with 54 additions and 18 deletions

View File

@ -113,7 +113,7 @@ async function playThis (message) {
discordClient.user.client.voice.connections.forEach((element) => { discordClient.user.client.voice.connections.forEach((element) => {
songPlayMessage(message, argument); songPlayMessage(message, argument);
playbackmanager.startPlaying(element, itemID, isSummendByPlay); playbackmanager.startPlaying(element, itemID, 0, isSummendByPlay);
}); });
} }

View File

@ -1,11 +1,17 @@
const discordclientmanager = require("./discordclientmanager");
const { const {
getAudioDispatcher, getAudioDispatcher,
setAudioDispatcher setAudioDispatcher
} = require("./dispachermanager"); } = require("./dispachermanager");
const {
ticksToSeconds
} = require("./util");
var currentPlayingItemId; var currentPlayingItemId;
var progressInterval; var progressInterval;
var isPaused; var isPaused;
var _disconnectOnFinish;
var _seek;
const jellyfinClientManager = require("./jellyfinclientmanager"); const jellyfinClientManager = require("./jellyfinclientmanager");
function streamURLbuilder (itemID, bitrate) { function streamURLbuilder (itemID, bitrate) {
@ -15,13 +21,20 @@ function streamURLbuilder (itemID, bitrate) {
return `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${bitrate}&Container=${supportedContainers}&AudioCodec=${supportedCodecs}&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`; return `${jellyfinClientManager.getJellyfinClient().serverAddress()}/Audio/${itemID}/universal?UserId=${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}&DeviceId=${jellyfinClientManager.getJellyfinClient().deviceId()}&MaxStreamingBitrate=${bitrate}&Container=${supportedContainers}&AudioCodec=${supportedCodecs}&api_key=${jellyfinClientManager.getJellyfinClient().accessToken()}&TranscodingContainer=ts&TranscodingProtocol=hls`;
} }
function startPlaying (voiceconnection, itemID, disconnectOnFinish) { function startPlaying (voiceconnection = discordclientmanager.getDiscordClient().user.client.voice.connections.first(), itemID = currentPlayingItemId, seekTo, disconnectOnFinish = _disconnectOnFinish) {
isPaused = false; isPaused = false;
currentPlayingItemId = itemID;
_disconnectOnFinish = disconnectOnFinish;
_seek=seekTo*1000;
async function playasync () { async function playasync () {
const url = streamURLbuilder(itemID, voiceconnection.channel.bitrate); const url = streamURLbuilder(itemID, voiceconnection.channel.bitrate);
jellyfinClientManager.getJellyfinClient().reportPlaybackStart({ userID: `${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}`, itemID: `${itemID}` }); setAudioDispatcher(voiceconnection.play(url ,{seek: seekTo}));
currentPlayingItemId = itemID; console.log(seekTo, ticksToSeconds(getPostitionTicks()));
setAudioDispatcher(voiceconnection.play(url)); if(seekTo){
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
}else{
jellyfinClientManager.getJellyfinClient().reportPlaybackStart({ userID: `${jellyfinClientManager.getJellyfinClient().getCurrentUserId()}`, itemID: `${itemID}`, canSeek: true ,playSessionId: getPlaySessionId(), playMethod:getPlayMethod()});
}
getAudioDispatcher().on("finish", () => { getAudioDispatcher().on("finish", () => {
if (disconnectOnFinish) { if (disconnectOnFinish) {
@ -33,6 +46,16 @@ function startPlaying (voiceconnection, itemID, disconnectOnFinish) {
} }
playasync().catch((rsn) => { console.log(rsn); }); playasync().catch((rsn) => { console.log(rsn); });
} }
/**
* @param {Number} toSeek - where to seek in ticks
*/
function seek(toSeek = 0){
if(getAudioDispatcher()){
startPlaying(undefined,undefined,ticksToSeconds(toSeek),_disconnectOnFinish);
jellyfinClientManager.getJellyfinClient().reportPlaybackProgress(getProgressPayload());
}
}
/** /**
* @param {Object=} disconnectVoiceConnection - Optional The voice Connection do disconnect from * @param {Object=} disconnectVoiceConnection - Optional The voice Connection do disconnect from
*/ */
@ -41,7 +64,7 @@ function stop (disconnectVoiceConnection) {
if (disconnectVoiceConnection) { if (disconnectVoiceConnection) {
disconnectVoiceConnection.disconnect(); disconnectVoiceConnection.disconnect();
} }
jellyfinClientManager.getJellyfinClient().reportPlaybackStopped({ userId: jellyfinClientManager.getJellyfinClient().getCurrentUserId(), itemId: currentPlayingItemId }); jellyfinClientManager.getJellyfinClient().reportPlaybackStopped({ userId: jellyfinClientManager.getJellyfinClient().getCurrentUserId(), itemId: currentPlayingItemId ,playSessionId: getPlaySessionId()});
if (getAudioDispatcher()) { getAudioDispatcher().destroy(); } if (getAudioDispatcher()) { getAudioDispatcher().destroy(); }
setAudioDispatcher(undefined); setAudioDispatcher(undefined);
clearInterval(progressInterval); clearInterval(progressInterval);
@ -63,12 +86,12 @@ function playPause () {
function getPostitionTicks () { function getPostitionTicks () {
// this is very sketchy but i dont know how else to do it // this is very sketchy but i dont know how else to do it
return (getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000; return (_seek+getAudioDispatcher().streamTime - getAudioDispatcher().pausedTime) * 10000;
} }
function getPlayMethod () { function getPlayMethod () {
// TODO figure out how to figure this out // TODO figure out how to figure this out
return "Transcode"; return 0;
} }
function getRepeatMode () { function getRepeatMode () {
@ -94,7 +117,7 @@ function getNowPLayingQueue () {
} }
function getCanSeek () { function getCanSeek () {
return false; return true;
} }
function getIsMuted () { function getIsMuted () {
@ -143,5 +166,6 @@ module.exports = {
stop, stop,
playPause, playPause,
resume, resume,
pause pause,
seek
}; };

View File

@ -3,6 +3,11 @@ function checkJellyfinItemIDRegex (strgintomatch) {
return regexresult; return regexresult;
} }
function ticksToSeconds (ticks){
return ticks/10000000;
}
module.exports = { module.exports = {
checkJellyfinItemIDRegex checkJellyfinItemIDRegex,
ticksToSeconds
}; };

View File

@ -11,22 +11,29 @@ function openSocket () {
SupportedCommands: "Play,Playstate" SupportedCommands: "Play,Playstate"
} }
); );
jellyfinClientManager.getJellyfinClient().ajax({
type: 'POST',
url: jellyfinClientManager.getJellyfinClient().getUrl('system/info/public'),
data: JSON.stringify({}),
contentType: 'application/json'
}).then((resp)=>{console.log(resp)})
jellyfinClientManager.getJellyfinEvents().on(jellyfinClientManager.getJellyfinClient(), "message", (type, data) => { jellyfinClientManager.getJellyfinEvents().on(jellyfinClientManager.getJellyfinClient(), "message", (type, data) => {
// console.log(data);
if (data.MessageType === "Play") { if (data.MessageType === "Play") {
if (data.Data.PlayCommand === "PlayNow") { if (data.Data.PlayCommand === "PlayNow") {
discordclientmanager.getDiscordClient().user.client.voice.connections.forEach((element) => { playbackmanager.startPlaying(undefined, data.Data.ItemIds[data.Data.StartIndex || 0],0, false);
playbackmanager.startPlaying(element, data.Data.ItemIds[data.Data.StartIndex || 0], false);
element.on("error", (error) => {
console.error(error);
});
});
} }
} else if (data.MessageType === "Playstate") { } else if (data.MessageType === "Playstate") {
if (data.Data.Command === "PlayPause") { if (data.Data.Command === "PlayPause") {
playbackmanager.playPause(); playbackmanager.playPause();
} else if (data.Data.Command === "Stop") { } else if (data.Data.Command === "Stop") {
playbackmanager.stop(); playbackmanager.stop();
} else if (data.Data.Command === "Seek") {
playbackmanager.seek(data.Data.SeekPositionTicks);
} }
} }
}); });