Update discordeno-audio-plugin, fix a bug where the web socket was dropping (#1)

* update discordeno-audio-plugin

* rename discordeno-audio-plugin-main folder to discordeno-audio-plugin
This commit is contained in:
alexandra-love 2023-01-11 15:32:17 -06:00 committed by GitHub
parent 347531c102
commit b7d78405ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
45 changed files with 56 additions and 63 deletions

View File

@ -1,13 +0,0 @@
import { getLocalSources } from "./local.ts";
import { getYoutubeSources } from "./youtube.ts";
export type LoadSource = typeof loadLocalOrYoutube;
export function loadLocalOrYoutube(query: string, added_by?: string) {
const local = query.startsWith("./");
if (local) {
//return getLocalSources(query);
} else {
return getYoutubeSources(query, String(added_by));
}
}

View File

@ -2,11 +2,6 @@ export * from "https://deno.land/x/discordeno@17.1.0/mod.ts";
export * from "https://deno.land/x/discordeno@17.1.0/plugins/cache/mod.ts";
export * as opus from "https://unpkg.com/@evan/wasm@0.0.95/target/opus/deno.js";
export * from "https://unpkg.com/@evan/wasm@0.0.95/target/nacl/deno.js";
export {
default as ytdl,
getInfo,
downloadFromInfo,
} from "https://deno.land/x/ytdl_core@v0.1.2/mod.ts";
export { ytDownload } from "https://deno.land/x/yt_download@1.1/mod.ts";
export { ytDownload } from "https://deno.land/x/yt_download@1.2/mod.ts";
export type { VideoFormat } from "https://deno.land/x/ytdl_core@v0.1.0/src/types.ts";
export { default as YouTube } from "https://deno.land/x/youtube_sr@v4.3.4-deno/mod.ts";
export { default as YouTube } from "https://deno.land/x/youtube_sr@v4.1.17/mod.ts";

View File

@ -27,6 +27,7 @@ export function createAudioSource(
return data();
} catch (error) {
console.error(error);
console.log(`Failed to play ${title}\n Returning empty stream`);
return empty();
}
},

View File

@ -0,0 +1,7 @@
import { getYoutubeSources } from "./youtube.ts";
export type LoadSource = typeof loadLocalOrYoutube;
export function loadLocalOrYoutube(query: string, added_by?: string) {
return getYoutubeSources(query, String(added_by));
}

View File

@ -1,7 +1,7 @@
import { YouTube, getInfo, downloadFromInfo, VideoFormat, ytdl, ytDownload } from "../../deps.ts";
import { YouTube, ytDownload } from "../../deps.ts";
import { bufferIter } from "../../utils/mod.ts";
import { demux } from "../demux/mod.ts";
import { createAudioSource } from "./audio-source.ts";
import { createAudioSource, empty } from "./audio-source.ts";
function supportedFormatFilter(format: {
codecs: string;
@ -24,20 +24,21 @@ export async function getYoutubeSources(added_by?: string, ...queries: string[])
}
export async function getYoutubeSource(query: string, added_by?: string) {
//const results = await YouTube.getVideo(query);
try {
const result = await ytdl(query, { filter: "audio" });
const info = await getInfo(query!);
if (result) {
const title = info.player_response.videoDetails.title;
return createAudioSource(title!, async () => {
//const audio = await ytDownload(info.videoDetails.videoId.toString(), {
// mimeType: `audio/webm; codecs="opus"`,
//});
const audio = await downloadFromInfo(info, {
filter: supportedFormatFilter,
});
return bufferIter(demux(audio));
const results = await YouTube.search(query, { limit: 1, type: "video" });
if (results.length > 0) {
const { id, title } = results[0];
return createAudioSource(title!, async () => {
try {
const stream = await ytDownload(id!, {
mimeType: `audio/webm; codecs="opus"`,
});
return bufferIter(demux(stream));
} catch {
console.error("error");
console.log(`Failed to play ${title}\n Returning empty stream`);
return empty();
}
}, added_by);
}
} catch(err) {

View File

@ -1,4 +1,4 @@
import { EventSource, wait } from "../../utils/mod.ts";
import { EventSource } from "../../utils/mod.ts";
import { ConnectionData } from "../connection-data.ts";
import { FRAME_DURATION } from "../sample-consts.ts";
import { Player } from "./types.ts";
@ -32,35 +32,37 @@ export class RawPlayer implements Player {
play() {
try {
if (this.playing) {
return;
}
this.playing = true;
const inter = setDriftlessInterval(async () => {
if (this.playing === false) {
clearDriftless(inter);
}
if (this.#interrupt) {
const { done, value } = await this.#interrupt.next();
if (done) {
this.#interrupt = undefined;
} else {
this.conn.audio.trigger(value);
return;
}
}
const nextAudioIter = await this.#audio?.next();
if (nextAudioIter === undefined || nextAudioIter.done) {
this.#audio = undefined;
this.#doneSource.trigger();
await this.#onNext();
if (this.playing) {
return;
}
this.conn.audio.trigger(nextAudioIter.value);
}, FRAME_DURATION);
this.playing = true;
const inter = setDriftlessInterval(async () => {
if (this.playing === false) {
clearDriftless(inter);
}
if (this.#interrupt) {
const { done, value } = await this.#interrupt.next();
if (done) {
this.#interrupt = undefined;
} else {
this.conn.audio.trigger(value);
return;
}
}
const nextAudioIter = await this.#audio?.next();
if (nextAudioIter === undefined || nextAudioIter.done) {
this.#audio = undefined;
this.#doneSource.trigger();
this.playing = false;
await this.#onNext();
this.play();
return;
}
this.conn.audio.trigger(nextAudioIter.value);
}, FRAME_DURATION);
} catch(err) {
console.log("error!!");
console.log("error while playing!!");
console.error(err);
}
}

View File

@ -74,7 +74,7 @@ export async function sendAudioPacket(conn: ConnectionData, audio: Uint8Array) {
incrementAudioMetaData(conn.context);
try {
const packet = addRTP(conn, audio);
const result = await conn.udpSocket.send(packet, {
await conn.udpSocket.send(packet, {
...conn.remote,
transport: "udp",
});