Refactor OggStreamImpl to use exceptions for error handling

Introduce a new `Open` method in `OggStreamImpl` that encapsulates the
logic for opening Ogg Vorbis files. This method replaces the previous
`Open` method and utilizes `std::runtime_error` for error reporting
instead of returning error codes. The constructor has been updated to
call this new method, ensuring files are opened upon instantiation. The
changes streamline the code and improve error handling.
This commit is contained in:
trompetin17
2025-06-19 15:09:39 -05:00
parent 336ff333ed
commit 2ae95d3c49
+44 -36
View File
@@ -30,6 +30,7 @@
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include <stdexcept>
static Status LibErrorFromVorbis(int err)
{
@@ -142,10 +143,11 @@ class OggStreamImpl : public OggStream
{
public:
OggStreamImpl(const Adapter& adapter)
: adapter(adapter)
: adapter(adapter),
m_fileEOF(false),
info(nullptr)
{
m_fileEOF = false;
info = NULL;
Open();
}
Status Close()
@@ -155,35 +157,6 @@ public:
return 0;
}
Status Open()
{
ov_callbacks callbacks;
callbacks.read_func = Adapter::Read;
callbacks.close_func = Adapter::Close;
callbacks.seek_func = Adapter::Seek;
callbacks.tell_func = Adapter::Tell;
const int ret = ov_open_callbacks(&adapter, &vf, 0, 0, callbacks);
switch (ret)
{
case 0:
break;
case OV_EBADHEADER:
case OV_EREAD:
case OV_ENOTVORBIS:
case OV_EVERSION:
return LibErrorFromVorbis(ret);
default:
WARN_RETURN(LibErrorFromVorbis(ret));
}
const int link = -1; // retrieve info for current bitstream
info = ov_info(&vf, link);
if(!info)
WARN_RETURN(ERR::INVALID_HANDLE);
return INFO::OK;
}
virtual ALenum Format()
{
return (info->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
@@ -233,6 +206,34 @@ public:
}
private:
void Open()
{
ov_callbacks callbacks;
callbacks.read_func = Adapter::Read;
callbacks.close_func = Adapter::Close;
callbacks.seek_func = Adapter::Seek;
callbacks.tell_func = Adapter::Tell;
const int ret{ov_open_callbacks(&adapter, &vf, 0, 0, callbacks)};
switch (ret)
{
case 0:
break;
case OV_EBADHEADER:
case OV_EREAD:
case OV_ENOTVORBIS:
case OV_EVERSION:
throw std::runtime_error(fmt::format("Failed to open Ogg Vorbis file: {}", LibErrorFromVorbis(ret)));
default:
throw std::runtime_error(fmt::format("Unknown error opening Ogg Vorbis file: {}", LibErrorFromVorbis(ret)));
}
// Retrieve info for current bitstream.
const int link{-1};
info = ov_info(&vf, link);
if(!info)
DEBUG_WARN_ERR(ERR::INVALID_HANDLE);
}
Adapter adapter;
OggVorbis_File vf;
vorbis_info* info;
@@ -248,10 +249,17 @@ Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr&
size_t size;
RETURN_STATUS_IF_ERR(vfs->LoadFile(pathname, contents, size));
std::shared_ptr<OggStreamImpl<VorbisBufferAdapter>> tmp = std::make_shared<OggStreamImpl<VorbisBufferAdapter>>(VorbisBufferAdapter(contents, size));
RETURN_STATUS_IF_ERR(tmp->Open());
stream = tmp;
return INFO::OK;
try
{
std::shared_ptr<OggStreamImpl<VorbisBufferAdapter>> tmp{std::make_shared<OggStreamImpl<VorbisBufferAdapter>>(VorbisBufferAdapter(contents, size))};
stream = tmp;
return INFO::OK;
}
catch(const std::runtime_error& e)
{
LOGERROR(e.what());
return ERR::IO;
}
}
#endif // CONFIG2_AUDIO