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

Return a promise on stream finish to improve async handling in playStream function #130

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

malthemorsing
Copy link

When using playStream in a playlist loop, the vStream.once("finish") callback in its current implementation does not provide a mechanism to await the completion of a video before proceeding to the next one. As a result, all videos in the playlist attempt to play simultaneously.

  • src/media/newApi.ts: Added a promise to the playStream function that resolves when the stream finishes.

Usage example:

const client = new Client();
const streamer = new Streamer(client);
let current: ReturnType<typeof NewApi.prepareStream>["command"];

client.on("ready", async () => {
  console.log("Ready");

  await streamer.joinVoice(config.guildId, config.channelId);
  if (streamer.client.user!.voice!.channel instanceof StageChannel) await streamer.client.user!.voice!.setSuppressed(false);

  const playlist = [
    {
      title: "Video 1",
      url: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
    },
    {
      title: "Video 2",
      url: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
    }
  ]

  for (const video of playlist) {
    await playVideo(video);
  }
});

const playVideo = async (video: { title: string; url: string }) => {
  console.log("Video: ", video);

  try {
    const { command, output } = NewApi.prepareStream(video.url, {
      height: config.streamOpts.height,
      bitrateVideo: config.streamOpts.bitrateKbps,
      bitrateVideoMax: config.streamOpts.maxBitrateKbps,
      hardwareAcceleratedDecoding: true,
      videoCodec: "H264",
      h26xPreset: "fast",
      frameRate: config.streamOpts.fps,
    });

    current = command;

    current.on("end", () => {
      console.log("ffmpeg ended");
    });

    current.on("error", (error) => {
      if (error.message.includes("SIGINT")) {
        streamer.stopStream();
        streamer.voiceConnection.streamConnection?.setSpeaking(false);
        streamer.voiceConnection.streamConnection?.setVideoStatus(false);
      } else console.log("Stream error: ", error);
    });

    await NewApi.playStream(output, streamer);
    current = undefined;
  } catch (error) {
    console.log(error);
  }
};

process.on("SIGINT", () => {
  client.logout();
  client.destroy();
  process.exit();
});

await client.login(config.token);

@longnguyen2004
Copy link
Collaborator

LGTM

@longnguyen2004 longnguyen2004 merged commit 830f5ad into Discord-RE:master Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants