스킵 구현

This commit is contained in:
2023-12-20 19:45:25 +09:00
parent 6503fd167b
commit 8a987320e0
775 changed files with 162601 additions and 135 deletions

View File

@@ -1,8 +1,10 @@
#include "BumbleCeepp.hpp"
#include <string>
#include <ogg/ogg.h>
#include <oggz/oggz.h>
#include <opus/opusfile.h>
#include <FQueueElement.hpp>
#include <thread>
BumbleCeepp::BumbleCeepp(std::string Token) : IBot(Token) {
VoiceJoinedShardId = 0;
@@ -15,137 +17,96 @@ void BumbleCeepp::enqueue(struct FQueueElement Element) {
}
void BumbleCeepp::QueuePlay(){
dpp::discord_client* JoinedShared = BotCluster->get_shard(VoiceJoinedShardId);
if (!JoinedShared) {
if (QueuePlaying) {
return;
}
QueuePlaying = true;
QueuePlayMutex.lock();
while (!MusicQueue.empty()) {
QueueMutex.lock();
FQueueElement Music = MusicQueue.front();
MusicQueue.pop_front();
QueueMutex.unlock();
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
std::thread T1([this] (){
dpp::discord_client* JoinedShared = BotCluster->get_shard(VoiceJoinedShardId);
if (!JoinedShared) {
return;
}
ogg_sync_state oy;
ogg_stream_state os;
ogg_page og;
ogg_packet op;
OpusHead header;
char *buffer;
while (!MusicQueue.empty()) {
QueueMutex.lock();
FQueueElement Music = MusicQueue.front();
QueueMutex.unlock();
FILE *fd;
fd = fopen((std::string(Music.FileName.c_str()) + ".ogg").c_str(), "rb");
if (!fd) {
continue;
}
fseek(fd, 0L, SEEK_END);
size_t sz = ftell(fd);
rewind(fd);
ogg_sync_init(&oy);
buffer = ogg_sync_buffer(&oy, sz);
fread(buffer, 1, sz, fd);
ogg_sync_wrote(&oy, sz);
if (ogg_sync_pageout(&oy, &og) != 1) {
fprintf(stderr,"Does not appear to be ogg stream.\n");
exit(1);
}
ogg_stream_init(&os, ogg_page_serialno(&og));
if (ogg_stream_pagein(&os,&og) < 0) {
fprintf(stderr,"Error reading initial page of ogg stream.\n");
exit(1);
}
if (ogg_stream_packetout(&os,&op) != 1) {
fprintf(stderr,"Error reading header packet of ogg stream.\n");
exit(1);
}
/* We must ensure that the ogg stream actually contains opus data */
if (!(op.bytes > 8 && !memcmp("OpusHead", op.packet, 8))) {
fprintf(stderr,"Not an ogg opus stream.\n");
exit(1);
}
/* Parse the header to get stream info */
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
/* Now we ensure the encoding is correct for Discord */
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
std::list<ogg_packet> OpusList;
/* Now loop though all the pages and send the packets to the vc */
while (ogg_sync_pageout(&oy, &og) == 1) {
ogg_stream_init(&os, ogg_page_serialno(&og));
if(ogg_stream_pagein(&os,&og)<0) {
fprintf(stderr,"Error reading page of Ogg bitstream data.\n");
exit(1);
dpp::voiceconn* v = JoinedShared->get_voice(Music.guild_id);
if (!v || !v->voiceclient || !v->voiceclient->is_ready()) {
return;
}
while (ogg_stream_packetout(&os,&op) != 0) {
/* load the audio file with oggz */
OGGZ *track_og = oggz_open((std::string(Music.FileName.c_str()) + ".ogg").c_str(), OGGZ_READ);
/* Read remaining headers */
if (op.bytes > 8 && !memcmp("OpusHead", op.packet, 8)) {
int err = opus_head_parse(&header, op.packet, op.bytes);
if (err) {
fprintf(stderr,"Not a ogg opus stream\n");
exit(1);
}
/* If there was an issue reading the file, tell the user and stop */
if (!track_og) {
fprintf(stderr, "Error opening file\n");
return;
}
if (header.channel_count != 2 && header.input_sample_rate != 48000) {
fprintf(stderr,"Wrong encoding for Discord, must be 48000Hz sample rate with 2 channels.\n");
exit(1);
}
/* set read callback, this callback will be called on packets with the serialno,
* -1 means every packet will be handled with this callback.
*/
oggz_set_read_callback(
track_og, -1,
[](OGGZ *oggz, oggz_packet *packet, long serialno, void *user_data) {
dpp::voiceconn *voiceconn = (dpp::voiceconn *)user_data;
continue;
/* send the audio */
voiceconn->voiceclient->send_audio_opus(packet->op.packet, packet->op.bytes);
/* make sure to always return 0 here, read more on oggz documentation */
return 0;
},
/* this will be the value of void *user_data */
(void *)v
);
// read loop
while (v && v->voiceclient && !v->voiceclient->terminating) {
/* you can tweak this to whatever. Here I use BUFSIZ, defined in
* stdio.h as 8192.
*/
static const constexpr long CHUNK_READ = BUFSIZ * 2;
const long read_bytes = oggz_read(track_og, CHUNK_READ);
/* break on eof */
if (!read_bytes) {
break;
}
}
/* Skip the opus tags */
if (op.bytes > 8 && !memcmp("OpusTags", op.packet, 8))
continue;
/* Don't forget to free the memory */
oggz_close(track_og);
ogg_packet D;
std::cout << "audio sending complete\n";
memcpy(&D, &op, sizeof(ogg_packet));
int n = 0;
OpusList.push_back(D);
//v->voiceclient->send_audio_opus(op.packet, op.bytes, samples / 48);
while(v->voiceclient->is_playing()) {}
QueueMutex.lock();
MusicQueue.pop_front();
QueueMutex.unlock();
if (Repeat) {
QueueMutex.lock();
MusicQueue.push_back(Music);
QueueMutex.unlock();
}
}
QueuePlayMutex.unlock();
QueuePlaying = false;
while (!OpusList.empty()) {
ogg_packet D = OpusList.front();
OpusList.pop_front();
v->voiceclient->send_audio_opus(D.packet, D.bytes);
}
/* Cleanup */
ogg_stream_clear(&os);
ogg_sync_clear(&oy);
system(("rm -f " + std::string(Music.FileName.c_str()) + ".ogg").c_str());
}
std::cout << "Queue ended\n";
});
T1.detach();
}
void BumbleCeepp::OnCommand(const dpp::slashcommand_t& Event) {