스트리밍 구현 끝

This commit is contained in:
2025-02-01 03:36:31 +09:00
parent c2cb16c6d7
commit 7e43900f22
36 changed files with 640 additions and 364 deletions

View File

@@ -0,0 +1,50 @@
#pragma once
#ifndef _MUSICPLAYMANAGER_HPP_
#define _MUSICPLAYMANAGER_HPP_
#include <dpp/dpp.h>
#include <Queue/MusicQueue.hpp>
#include <condition_variable>
namespace bumbleBee {
class MusicPlayManager {
public:
MusicPlayManager(std::shared_ptr<dpp::cluster> cluster) {
this->cluster = cluster;
this->queue = std::make_unique<MusicQueue>();
cluster->on_voice_ready([this](const dpp::voice_ready_t &event){on_voice_ready(event);});
cluster->on_voice_track_marker([this](const dpp::voice_track_marker_t &event){on_voice_track_marker(event);});
cluster->on_voice_client_disconnect([this](const dpp::voice_client_disconnect_t& event){on_voice_client_disconnect(event);});
}
/**
* @brief voice_ready 이벤트 인지시 콜백되는 함수
* @param event const dpp::voice_ready_t&
**/
void on_voice_ready(const dpp::voice_ready_t& event);
/**
* @brief voice_track_marker 이벤트 인지시 콜백되는 함수
* @param event const dpp::voice_track_marker_t&
**/
void on_voice_track_marker(const dpp::voice_track_marker_t& event);
/**
* @brief voice_client_disconnect 이벤트 인지시 콜백되는 함수
* @param event const dpp::voice_client_disconnect_t&
**/
void on_voice_client_disconnect(const dpp::voice_client_disconnect_t& event);
void queue_music(const std::shared_ptr<MusicQueueElement> music);
std::condition_variable queuedCondition;
private:
std::shared_ptr<dpp::cluster> cluster;
/// @brief 음악 큐
std::unique_ptr<MusicQueue> queue;
std::mutex queueEmptyMutex;
void send_audio_to_voice(const MusicQueueElement& music, const dpp::voice_ready_t &event);
void send_audio_to_voice(const MusicQueueElement& music, const dpp::voice_track_marker_t& event);
};
}
#endif

View File

@@ -4,9 +4,7 @@
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <memory>
#include <BumbleBeeCommand.hpp>
#include <MusicQueue.hpp>
#include <mariadb/conncpp.hpp>
#include <Commands/BumbleBeeCommand.hpp>
namespace bumbleBee {
/**
@@ -16,49 +14,38 @@ namespace bumbleBee {
class BumbleBee {
public:
/**
* @fn BumbleBee(nlohmann::json settings)
* @brief 생성자
**/
BumbleBee();
/**
* @fn ~BumbleBee()
* @brief 파괴자
* @details BumbleBee의 모든 Property를 책임지고 파괴합니다
**/
~BumbleBee() {}
/**
* @fn void start()
* @brief 봇 시작
**/
void start();
/**
* @fn void on_slashcommand(const dpp::slashcommand_t& event)
* @brief slashcommand 이벤트 인지시 콜백되는 함수
* @param event
* @param event const dpp::slashcommand_t&
**/
void on_slashcommand(const dpp::slashcommand_t& event);
/**
* @fn void on_ready(const dpp::ready_t &event)
* @brief ready 이벤트 인지시 콜백되는 함수
* @param event
* @param event const dpp::ready_t&
**/
void on_ready(const dpp::ready_t &event);
void on_ready(const dpp::ready_t& event);
/// @brief DPP 기본 클러스터 객체
std::shared_ptr<dpp::cluster> cluster;
/// @brief 음악 큐
std::shared_ptr<MusicQueue> queue;
private:
/// @brief db 드라이버
sql::Driver* dbDriver;
/// @brief db 접속 URL
std::shared_ptr<sql::SQLString> dbURL;
/// @brief db 접속 속성
std::shared_ptr<sql::Properties> dbProperties;
/// @brief Command 목록
std::vector<std::shared_ptr<commands::ICommand>> commands;
std::unordered_map<std::string, std::shared_ptr<commands::ICommand>> commands;
/// @brief voiceclient 관련 event 처리기 <guild id, 각 길드별 MusicPlayManager 인스턴스>
std::shared_ptr<MusicPlayManager> musicManager;
};
}
#endif

View File

@@ -1,102 +0,0 @@
#pragma once
#ifndef _BUMBLEBEECOMMAND_HPP_
#define _BUMBLEBEECOMMAND_HPP_
#include <dpp/dpp.h>
#include <MusicQueue.hpp>
namespace bumbleBee::commands {
class ICommand {
public:
/**
* @brief 기본 생성자
* @param botID 명령어 등록을 위한 봇 아이디
* @param queue 음악이 저장되어 있는 큐
**/
ICommand(dpp::snowflake botID, std::weak_ptr<MusicQueue> queue) {
this->botID = botID;
this->queue = queue;
}
/**
* @brief 명령어 호출 시에 콜백될 메소드
* @param event dpp::slashcommand_t
**/
virtual void operator()(const dpp::slashcommand_t &event){};
/// @brief 봇 명령어들의 이름과 별명들을 저장하는 벡터
std::vector<dpp::slashcommand> nameAndAliases;
private:
/// @brief 봇 ID
dpp::snowflake botID;
/// @brief 음악 큐에 대한 약한 포인터
std::weak_ptr<MusicQueue> queue;
/**
* @brief 명령어 별명 추가
* @param name 명령어 이름
* @param description 명령어 설명
**/
void addCommandAliase(std::string name, std::string description) {
nameAndAliases.push_back(dpp::slashcommand(name, description, botID));
}
};
}
/**
* @brief 명령어 인자가 없는 명령어의 boilerplate 대체 매크로
* @param name 명령어 이름 및 클래스명
* @param alias 명령어 별명
* @param description 명령어 설명
**/
#define _DECLARE_BUMBLEBEE_COMMAND_one_ALIAS(name, alias, description) \
namespace bumbleBee::commands { \
class name : public ICommand { \
public: \
name (dpp::snowflake botID, std::weak_ptr<MusicQueue> q) \
: ICommand(botID, q) { \
dpp::slashcommand command = dpp::slashcommand(#name, description, botID); \
dpp::slashcommand aliasCommand = dpp::slashcommand(alias, description, botID); \
nameAndAliases.push_back(command); \
nameAndAliases.push_back(aliasCommand); \
} \
virtual void operator()(const dpp::slashcommand_t &event) override; \
}; \
}
/**
* @brief 명령어 인자를 하나 갖는 명령어의 boilerplate 대체 매크로
* @param name 명령어 이름 및 클래스명
* @param alias 명령어 별명
* @param description 명령어 설명
* @param option_name 명령어 인자 이름
* @param option_desc 명령어 인자 설명
**/
#define _DECLARE_BUMBLEBEE_COMMAND_one_PARAM_one_ALIAS(name, alias, description, option_name, option_desc) \
namespace bumbleBee::commands { \
class name : public ICommand { \
public: \
name (dpp::snowflake botID, std::weak_ptr<MusicQueue> q) \
: ICommand(botID, q) { \
dpp::slashcommand command = dpp::slashcommand(#name, description, botID); \
dpp::slashcommand aliasCommand = dpp::slashcommand(alias, description, botID); \
command.add_option( \
dpp::command_option(dpp::co_string, option_name, option_desc, botID) \
); \
aliasCommand.add_option( \
dpp::command_option(dpp::co_string, option_name, option_desc, botID) \
); \
nameAndAliases.push_back(command); \
nameAndAliases.push_back(aliasCommand); \
} \
virtual void operator()(const dpp::slashcommand_t &event) override; \
}; \
}
_DECLARE_BUMBLEBEE_COMMAND_one_PARAM_one_ALIAS(Delete, "d", "큐의 해당하는 번호의 노래를 지웁니다", "pos", "큐 번호")
_DECLARE_BUMBLEBEE_COMMAND_one_ALIAS(Leave, "l", "음성 채팅방을 떠납니다")
_DECLARE_BUMBLEBEE_COMMAND_one_PARAM_one_ALIAS(Play, "p", "노래 재생", "query", "링크 또는 검색어")
_DECLARE_BUMBLEBEE_COMMAND_one_ALIAS(Queue, "q", "노래 예약 큐를 출력합니다")
_DECLARE_BUMBLEBEE_COMMAND_one_ALIAS(Repeat, "r", "반복을 켜거나 끕니다")
_DECLARE_BUMBLEBEE_COMMAND_one_ALIAS(Skip, "s", "현재 재생중인 곡을 스킵합니다")
#endif

View File

@@ -0,0 +1,70 @@
#pragma once
#ifndef _BUMBLEBEECOMMAND_HPP_
#define _BUMBLEBEECOMMAND_HPP_
#include <dpp/dpp.h>
#include <Audio/MusicPlayManager.hpp>
#include <functional>
namespace bumbleBee::commands {
class ICommand : public dpp::slashcommand {
public:
/**
* @brief 기본 생성자
* @param botID 봇 아이디
* @param manager 음악재생 매니저
**/
ICommand(dpp::snowflake botID, std::shared_ptr<MusicPlayManager> manager) {
this->botID = botID;
this->musicManager = manager;
}
/**
* @brief 명령어 호출 시에 콜백될 메소드
* @param event dpp::slashcommand_t
**/
virtual void execute(const dpp::slashcommand_t &event){};
/// @brief 명령어 별명
std::vector<std::string> aliases;
private:
/// @brief 봇 ID
dpp::snowflake botID;
protected:
/// @brief 음악재생 매니저
std::shared_ptr<MusicPlayManager> musicManager;
protected:
/// @brief concrete class에서 구현해야 하는 init 이벤트트
virtual void init() = 0;
};
}
/**
* @brief 명령어 인자가 없는 명령어의 boilerplate 대체 매크로
* @param name 명령어 이름 및 클래스명
* @param description 명령어 설명
**/
#define _DECLARE_BUMBLEBEE_COMMAND(CLASS, NAME, DESCRIPTION) \
namespace bumbleBee::commands { \
class CLASS : public ICommand { \
public: \
CLASS (dpp::snowflake botID, std::shared_ptr<MusicPlayManager> manager) \
: ICommand(botID, manager) { \
name = #NAME; \
description = DESCRIPTION; \
init(); \
} \
virtual void execute(const dpp::slashcommand_t &event) override; \
protected: \
virtual void init() override; \
}; \
}
_DECLARE_BUMBLEBEE_COMMAND(Delete, d, "큐의 해당하는 번호의 노래를 지웁니다")
_DECLARE_BUMBLEBEE_COMMAND(Leave, l, "음성 채팅방을 떠납니다")
_DECLARE_BUMBLEBEE_COMMAND(Play, p, "노래 재생")
_DECLARE_BUMBLEBEE_COMMAND(Queue, q, "노래 예약 큐를 출력합니다")
_DECLARE_BUMBLEBEE_COMMAND(Repeat, r, "반복을 켜거나 끕니다")
_DECLARE_BUMBLEBEE_COMMAND(Skip, s, "현재 재생중인 곡을 스킵합니다")
_DECLARE_BUMBLEBEE_COMMAND(Shuffle, shuffle, "큐를 섞습니다")
#endif

View File

@@ -1,16 +0,0 @@
#pragma once
#ifndef _MUSICPLAYMANAGER_HPP_
#define _MUSICPLAYMANAGER_HPP_
#include <dpp/dpp.h>
namespace BumbleBee {
class MusicPlayManager {
public:
MusicPlayManager(std::shared_ptr<dpp::cluster> cluster) {
this->cluster = cluster;
}
private:
std::shared_ptr<dpp::cluster> cluster;
};
}
#endif

View File

@@ -1,50 +0,0 @@
#pragma once
#ifndef _MUSICQUEUE_HPP_
#define _MUSICQUEUE_HPP_
#include <memory>
#include <functional>
#include <condition_variable>
#include <list>
#include <dpp/dpp.h>
namespace bumbleBee {
class MusicQueueElement {
public:
MusicQueueElement(
dpp::message originalResponse,
std::string id,
std::string url,
FILE* stream) :
originalResponse(originalResponse), id(id), url(url), stream(stream) {}
dpp::message originalResponse;
std::string id;
std::string url;
FILE* stream;
};
class MusicQueue {
public:
MusicQueue() {
currentPlayingPosition = queue.begin();
repeat = false;
}
void enqueue(std::shared_ptr<MusicQueueElement> Element);
std::shared_ptr<MusicQueueElement> dequeue();
std::shared_ptr<MusicQueueElement> findById(std::string id);
std::weak_ptr<MusicQueueElement> nowplaying();
std::weak_ptr<MusicQueueElement> next_music();
std::weak_ptr<MusicQueueElement> jump_to_index(int idx);
bool repeat;
std::function<void()> on_queue_added;
private:
std::condition_variable queueItemAdded;
std::mutex queueMutex;
std::list<std::shared_ptr<MusicQueueElement>> queue;
std::list<std::shared_ptr<MusicQueueElement>>::iterator currentPlayingPosition;
};
}
#endif

View File

@@ -0,0 +1,36 @@
#pragma once
#ifndef _MUSICQUEUE_HPP_
#define _MUSICQUEUE_HPP_
#include <memory>
#include <functional>
#include <condition_variable>
#include <list>
#include <dpp/dpp.h>
#include <Queue/MusicQueueElement.hpp>
namespace bumbleBee {
class MusicQueue {
public:
MusicQueue() {
queue = std::list<std::shared_ptr<MusicQueueElement>>();
currentPlayingPosition = queue.begin();
repeat = false;
}
void enqueue(std::shared_ptr<MusicQueueElement> Element);
std::shared_ptr<MusicQueueElement> dequeue();
std::list<std::shared_ptr<MusicQueueElement>>::iterator findById(std::string id);
std::shared_ptr<MusicQueueElement> nowplaying();
std::list<std::shared_ptr<MusicQueueElement>>::iterator next_music();
std::shared_ptr<MusicQueueElement> jump_to_index(int idx);
bool repeat;
std::list<std::shared_ptr<MusicQueueElement>>::iterator currentPlayingPosition;
std::list<std::shared_ptr<MusicQueueElement>> queue;
private:
std::mutex queueMutex;
};
}
#endif

View File

@@ -0,0 +1,21 @@
#pragma once
#ifndef _MUSICQUEUEELEMENT_HPP_
#define _MUSICQUEUEELEMENT_HPP_
#include <dpp/dpp.h>
namespace bumbleBee {
class MusicQueueElement {
public:
MusicQueueElement(
std::string id,
std::string query,
dpp::user issuingUser) :
id(id), query(query), issuingUser(issuingUser) {}
const std::string id;
const std::string query;
const dpp::user issuingUser;
};
}
#endif

View File

@@ -1,30 +1,37 @@
#pragma once
#ifndef _SETTINGSMANAGER_HPP_
#define _SETTINGSMANAGER_HPP_
#include <string>
#include <dpp/nlohmann/json.hpp>
#include <fstream>
#include <dpp/dpp.h>
#define _DECLARE_DEFAULT_ACCESSER_STATIC_VARIABLE(type, name, Name)\
private:\
static type name;\
public:\
static type get##Name() {return name;}\
static void set##Name(const type& value) {name = value; saveToFile();}
namespace bumbleBee {
/// @brief 모든 설정은 이 객체를 통해 스태틱하게 제공됨.
class settingsManager {
public:
/// @brief 봇 토큰
static std::string TOKEN;
_DECLARE_DEFAULT_ACCESSER_STATIC_VARIABLE(std::string, TOKEN, TOKEN)
/// @brief yt-dlp 실행 명령어
static std::string YTDLP_CMD;
_DECLARE_DEFAULT_ACCESSER_STATIC_VARIABLE(std::string, YTDLP_CMD, YTDLP_CMD)
/// @brief ffmpeg 실행 명령어
static std::string FFMPEG_CMD;
/// @brief db접속 url
static std::string DBURL;
/// @brief db접속 유저
static std::string DBUSER;
/// @brief db접속 비밀번호
static std::string DBPASSWORD;
static void load();
_DECLARE_DEFAULT_ACCESSER_STATIC_VARIABLE(std::string, FFMPEG_CMD, FFMPEG_CMD)
/// @brief 로그레벨
_DECLARE_DEFAULT_ACCESSER_STATIC_VARIABLE(dpp::loglevel, LOGLEVEL, LOGLEVEL)
/// @brief 이전에 있던 명령을 지우고 등록할지 선택합니다.
_DECLARE_DEFAULT_ACCESSER_STATIC_VARIABLE(bool, CLCOMMAND, CLCOMMAND)
public:
/// @brief 설정 로드하기, 설정은 이 load()를 부르기 전까지는 적절하지 못한 값을 가지고 있음.
/// @return 로드 성공 시 true, 실패 시 false 반환.
static bool load();
/// @brief 설정 변경사항 저장
static void saveToFile();
/// @brief 토큰이 유효한지 체크합니다.
/// @return 유효한 토큰이면 true, 아니면 false를 반환합니다.
static bool validateToken();
};
}
#endif

View File

@@ -6,18 +6,15 @@
#include <thread>
#include <condition_variable>
#include <dpp/dpp.h>
#include <MusicQueue.hpp>
#define WORKER_COUNT 5
#include <Queue/MusicQueue.hpp>
namespace bumbleBee {
/// @brief 싱글톤 멀티스레딩 다운로드 매니저
class AsyncDownloadManager {
class [[deprecated]] AsyncDownloadManager {
public:
static AsyncDownloadManager& getInstance(int worker_count, std::weak_ptr<dpp::cluster> weak_cluster, std::shared_ptr<bumbleBee::MusicQueue> musicQueue) {
static AsyncDownloadManager& getInstance(int worker_count, std::weak_ptr<dpp::cluster> weak_cluster) {
static AsyncDownloadManager dl(worker_count);
dl.weak_cluster = weak_cluster;
dl.musicQueue = musicQueue;
return dl;
}
void enqueue(std::pair<std::string, dpp::message> query) {
@@ -55,7 +52,6 @@ private:
std::condition_variable dlQueueCondition;
std::mutex dlQueueMutex;
std::weak_ptr<dpp::cluster> weak_cluster;
std::shared_ptr<bumbleBee::MusicQueue> musicQueue;
std::vector<std::thread> worker_thread;
bool terminate;
};

View File

@@ -19,7 +19,7 @@ public:
static void validateYTDLPFFMPEGBinary(std::shared_ptr<dpp::cluster> cluster) {
cluster->log(dpp::ll_info, "Checking if yt-dlp and ffmpeg is available...");
std::queue<std::string> result = ConsoleUtils::getResultFromCommand(settingsManager::FFMPEG_CMD + " -version");
std::queue<std::string> result = ConsoleUtils::getResultFromCommand(settingsManager::getFFMPEG_CMD() + " -version");
std::string front = result.front();
if (front[0] != 'f' ||
front[1] != 'f' ||
@@ -40,8 +40,9 @@ public:
system("tar -xf ffmpeg-master-latest-linux64-gpl.tar.xz");
system("rm ffmpeg-master-latest-linux64-gpl.tar.xz");
system("mv ffmpeg-master-latest-linux64-gpl ffmpeg");
settingsManager::setFFMPEG_CMD("./ffmpeg/bin/ffmpeg");
}
result = ConsoleUtils::getResultFromCommand(settingsManager::YTDLP_CMD + " --version");
result = ConsoleUtils::getResultFromCommand(settingsManager::getYTDLP_CMD() + " --version");
front = result.front();
if ((front[0]-'0' < 0 || front[0]-'0' > 9) ||
(front[1]-'0' < 0 || front[1]-'0' > 9) ||
@@ -58,6 +59,7 @@ public:
system("curl -LO https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp");
system("chmod +x ./yt-dlp");
settingsManager::setYTDLP_CMD("./yt-dlp");
}
}