Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: calculate total transforming cost #351

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/AudioSource/audiosource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { exportableCustom } from "./custom";
import type { EmbedField } from "eris";
import type { Readable } from "stream";

type StreamType =
export type StreamType =
| "dca"
| "ogg"
| "webm"
Expand Down Expand Up @@ -57,5 +57,14 @@ export abstract class AudioSource {
}

export type StreamInfo = ReadableStreamInfo|UrlStreamInfo;
export type ReadableStreamInfo = {type:"readable", stream:Readable, streamType?:StreamType};
export type UrlStreamInfo = {type:"url", url:string, streamType?:StreamType, userAgent?:string};
export type ReadableStreamInfo = {
type:"readable",
stream:Readable,
streamType?:StreamType,
};
export type UrlStreamInfo = {
type:"url",
url:string,
streamType?:StreamType,
userAgent?:string,
};
1 change: 1 addition & 0 deletions src/Commands/systeminfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class SystemInfo extends BaseCommand {
.addField("Version (commit hash)", `\`${options.bot.version}\``, true)
.addField("Managed embed toggles", `\`${options.embedPageToggle.length}\``, true)
.addField("Guilds that have modified data", `\`${options.bot.queueModifiedGuilds.length}\``, true)
.addField("Current total transforming costs", `\`${options.bot.totalTransformingCost}\``)
.setColor(getColor("UPTIME"))
.toEris()
);
Expand Down
8 changes: 7 additions & 1 deletion src/Component/PlayManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class PlayManager extends ManagerBase {
private _errorUrl = "";
private _preparing = false;
private _currentAudioInfo = null as AudioSource;
private _cost = 0;

get preparing(){
return this._preparing;
Expand All @@ -42,6 +43,10 @@ export class PlayManager extends ManagerBase {
else return "";
}

get cost(){
return this._cost;
}

/**
* 接続され、再生途中にあるか(たとえ一時停止されていても)
*/
Expand Down Expand Up @@ -148,7 +153,7 @@ export class PlayManager extends ManagerBase {
// QueueContentからストリーム情報を取得
const rawStream = await this.currentAudioInfo.fetch(time > 0);
// 情報からストリームを作成
const { stream, streamType } = resolveStreamToPlayable(rawStream, getFFmpegEffectArgs(this.server), this._seek, this.volume !== 100);
const { stream, streamType, cost } = resolveStreamToPlayable(rawStream, getFFmpegEffectArgs(this.server), this._seek, this.volume !== 100);
// ストリームがまだ利用できない場合待機
let errorWhileWaiting = null as Error;
stream.once("error", e => errorWhileWaiting = e || new Error("An error occurred in stream"));
Expand All @@ -164,6 +169,7 @@ export class PlayManager extends ManagerBase {
}
// 各種準備
this._errorReportChannel = mes.channel as TextChannel;
this._cost = cost;
const connection = this.server.connection;
t.end();
// 再生
Expand Down
31 changes: 22 additions & 9 deletions src/Component/streams/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ReadableStreamInfo, StreamInfo, UrlStreamInfo } from "../../AudioSource";
import type { StreamInfo, StreamType, UrlStreamInfo } from "../../AudioSource";
import type { Readable, TransformOptions } from "stream";

import { opus } from "prism-media";
Expand All @@ -7,6 +7,15 @@ import Util from "../../Util";
import { InitPassThrough } from "../../Util/general";
import { transformThroughFFmpeg } from "./ffmpeg";

type PlayableStreamInfo = PartialPlayableStream & {
cost:number,
};

type PartialPlayableStream = {
stream:Readable,
streamType:StreamType,
};

/*
Convertion cost:
FFmpeg:2
Expand All @@ -26,15 +35,20 @@ Refer at: https://github.com/discordjs/discord.js/blob/13baf75cae395353f0528804f
* @param volumeTransform whether volume transform is required
* @returns if volume transform is required, this will return a stream info that represents Ogg/Webm Opus, otherwise return a stream info represents PCM Opus.
*/
export function resolveStreamToPlayable(streamInfo:StreamInfo, effects:string[], seek:number, volumeTransform:boolean):ReadableStreamInfo{
export function resolveStreamToPlayable(streamInfo:StreamInfo, effects:string[], seek:number, volumeTransform:boolean):PlayableStreamInfo{
const effectEnabled = effects.length !== 0;
if((streamInfo.streamType === "webm" || streamInfo.streamType === "ogg") && seek <= 0 && !effectEnabled && !volumeTransform){
// 1. effect is off, volume is off, stream is webm or ogg
// Webm/Ogg --(Demuxer)--> Opus
// 1
// Total: 1
Util.logger.log(`[StreamResolver] stream edges: raw(${streamInfo.streamType}) (no convertion/cost: 1)`);
return streamInfo.type === "url" ? convertUrlStreamInfoToReadableStreamInfo(streamInfo) : streamInfo;
const info = streamInfo.type === "url" ? convertUrlStreamInfoToReadableStreamInfo(streamInfo) : streamInfo;
return {
stream: info.stream,
streamType: info.streamType,
cost: 1,
};
}else if(!volumeTransform){
// 2. volume is off and stream is any
// Unknown --(FFmpeg)--> Ogg/Opus --(Demuxer)--> Opus
Expand All @@ -49,9 +63,9 @@ export function resolveStreamToPlayable(streamInfo:StreamInfo, effects:string[],
.on("close", () => destroyStream(ffmpeg))
;
return {
type: "readable",
stream: passThrough,
streamType: "ogg",
cost: 3,
};
}else if((streamInfo.streamType === "webm" || streamInfo.streamType === "ogg") && !effectEnabled){
// 3. volume is on and stream is webm or ogg
Expand Down Expand Up @@ -80,9 +94,9 @@ export function resolveStreamToPlayable(streamInfo:StreamInfo, effects:string[],
.on("close", () => destroyStream(decoder))
;
return {
type: "readable",
stream: passThrough,
streamType: "pcm"
streamType: "pcm",
cost: 4.5,
};
}else{
// 4. volume is on and stream is unknown
Expand All @@ -98,16 +112,15 @@ export function resolveStreamToPlayable(streamInfo:StreamInfo, effects:string[],
.on("close", () => destroyStream(ffmpegPCM))
;
return {
type: "readable",
stream: passThrough,
streamType: "pcm",
cost: 5,
};
}
}

function convertUrlStreamInfoToReadableStreamInfo(streamInfo:UrlStreamInfo):ReadableStreamInfo{
function convertUrlStreamInfoToReadableStreamInfo(streamInfo:UrlStreamInfo):PartialPlayableStream{
return {
type: "readable",
stream: Util.web.DownloadAsReadable(streamInfo.url, streamInfo.userAgent ? {
headers: {
"User-Agent": streamInfo.userAgent
Expand Down
7 changes: 7 additions & 0 deletions src/botBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export abstract class MusicBotBase extends LogEmitter {
return Object.keys(this.data).length;
}

get totalTransformingCost(){
return Object.keys(this.data)
.map(id => this.data[id].player.cost)
.reduce((prev, current) => prev + current, 0)
;
}

constructor(protected readonly maintenance:boolean = false){
super();
this.SetTag("Main");
Expand Down