Skip to content

Commit

Permalink
throw exception if voice is disconnected or canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkrissym committed Oct 9, 2024
1 parent 324399a commit 94dbbc3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ FetchContent_GetProperties(certify)

set(BOOST_INCLUDE_LIBRARIES thread filesystem system process asio endian logic static_string)
set(BOOST_ENABLE_CMAKE ON)
add_definitions(-DBOOST_BIND_GLOBAL_PLACEHOLDERS)

FetchContent_Declare(Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
Expand Down
12 changes: 11 additions & 1 deletion Discord.C++/Exceptions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <exception>
#include <iostream>
#include <string>

#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
Expand Down Expand Up @@ -33,6 +33,16 @@ class ClientException : public DiscordException {
DLL_EXPORT explicit ClientException(std::string what);
};

class DisconnectException : public DiscordException {
public:
DLL_EXPORT explicit DisconnectException(std::string what);
};

class CanceledException : public DiscordException {
public:
DLL_EXPORT explicit CanceledException(std::string what);
};

class SizeError : public DiscordException {
public:
DLL_EXPORT explicit SizeError(std::string what);
Expand Down
11 changes: 9 additions & 2 deletions Discord.C++/VoiceClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ DiscordCPP::SharedFuture<void> DiscordCPP::VoiceClient::play(AudioSource* source
}

while (_ready && !_cancel_playing) {
if (source->read((char*)pcm_data, FRAME_SIZE * CHANNELS * 2) !=
true)
if (source->read((char*)pcm_data, FRAME_SIZE * CHANNELS * 2) != true) {
break;
}

in_data = reinterpret_cast<opus_int16*>(pcm_data);

Expand Down Expand Up @@ -357,5 +357,12 @@ DiscordCPP::SharedFuture<void> DiscordCPP::VoiceClient::play(AudioSource* source
delete[] pcm_data;

_log.debug("finished playing audio");

if (!_ready) {
throw DisconnectException("disconnected from voice");
}
if (_cancel_playing) {
throw CanceledException("audio stream canceled");
}
});
}
4 changes: 3 additions & 1 deletion Discord.C++/VoiceClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class VoiceClient {
DLL_EXPORT ~VoiceClient();

/** play an AudioSource
@throws OpusError ClientException
@throws OpusError if the opus encode could not be initialized
@throws DisconnectException if the client is disconnected from the voice channel
@throws CanceledException if stop_playing() has been called
*/
DLL_EXPORT SharedFuture<void> play(AudioSource* source);
DLL_EXPORT void stop_playing();
Expand Down

0 comments on commit 94dbbc3

Please sign in to comment.