From 69f814cded60c5f977a61a2ab747e4456323e6f2 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Sun, 8 Dec 2019 13:47:15 +0100 Subject: [PATCH] BFileGameSound: allow initializing from a BDataIO MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is no reason to not allow this, and it makes it possible to load data from eg. a BResource instead of a file, which is very useful. Remove some unused members in the class and dead code, and fix style issues. Change-Id: I94cbd0c13c469ea80f55028cf33dfde2de4365ef Reviewed-on: https://review.haiku-os.org/c/haiku/+/2001 Reviewed-by: Stephan Aßmus --- headers/os/game/FileGameSound.h | 16 +++++++--- src/kits/game/FileGameSound.cpp | 56 ++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/headers/os/game/FileGameSound.h b/headers/os/game/FileGameSound.h index 873e1464e5..29fe9656e2 100644 --- a/headers/os/game/FileGameSound.h +++ b/headers/os/game/FileGameSound.h @@ -11,6 +11,8 @@ #include +#include + struct entry_ref; struct _gs_media_tracker; @@ -25,6 +27,9 @@ public: BFileGameSound(const char* file, bool looping = true, BGameSoundDevice* device = NULL); + BFileGameSound(BDataIO* data, + bool looping = true, + BGameSoundDevice* device = NULL); virtual ~BFileGameSound(); @@ -52,7 +57,7 @@ private: BFileGameSound& operator=(const BFileGameSound& other); // not implemented - status_t Init(const entry_ref* file); + status_t Init(BDataIO* data); bool Load(); bool Read(void* buffer, size_t bytes); @@ -95,14 +100,17 @@ private: size_t fBufferSize; size_t fPlayPosition; - thread_id fReadThread; - port_id fPort; - _gs_ramp* fPausing; bool fPaused; float fPauseGain; + BDataIO* fDataSource; + +#ifdef B_HAIKU_64_BIT uint32 _reserved[9]; +#else + uint32 _reserved[10]; +#endif }; diff --git a/src/kits/game/FileGameSound.cpp b/src/kits/game/FileGameSound.cpp index 31e6d719b0..0350b31ee7 100644 --- a/src/kits/game/FileGameSound.cpp +++ b/src/kits/game/FileGameSound.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -130,7 +131,7 @@ BFileGameSound::BFileGameSound(const entry_ref* file, bool looping, fPauseGain(1.0) { if (InitCheck() == B_OK) - SetInitError(Init(file)); + SetInitError(Init(new(std::nothrow) BFile(file, B_READ_ONLY))); } @@ -152,24 +153,36 @@ BFileGameSound::BFileGameSound(const char* file, bool looping, if (get_ref_for_path(file, &node) != B_OK) SetInitError(B_ENTRY_NOT_FOUND); - else - SetInitError(Init(&node)); + else { + BFile* file = new(std::nothrow) BFile(&node, B_READ_ONLY); + SetInitError(Init(file)); + } } } +BFileGameSound::BFileGameSound(BDataIO* data, bool looping, + BGameSoundDevice* device) + : + BStreamingGameSound(device), + fAudioStream(NULL), + fStopping(false), + fLooping(looping), + fBuffer(NULL), + fPlayPosition(0), + fPausing(NULL), + fPaused(false), + fPauseGain(1.0) +{ + if (InitCheck() == B_OK) + SetInitError(Init(data)); +} + + BFileGameSound::~BFileGameSound() { - if (fReadThread >= 0) { - // TODO: kill_thread() is very bad, since it will leak any resources - // that the thread had allocated. It will also keep locks locked that - // the thread holds! Set a flag to make the thread quit and use - // wait_for_thread() here! - kill_thread(fReadThread); - } - - if (fAudioStream) { - if (fAudioStream->stream) + if (fAudioStream != NULL) { + if (fAudioStream->stream != NULL) fAudioStream->file->ReleaseTrack(fAudioStream->stream); delete fAudioStream->file; @@ -177,6 +190,7 @@ BFileGameSound::~BFileGameSound() delete [] fBuffer; delete fAudioStream; + delete fDataSource; } @@ -204,7 +218,7 @@ BFileGameSound::StopPlaying() { status_t error = BStreamingGameSound::StopPlaying(); - if (!fAudioStream || !fAudioStream->stream) + if ((fAudioStream == NULL) || (fAudioStream->stream == NULL)) return B_OK; // start reading next time from the start of the file @@ -367,15 +381,19 @@ BFileGameSound::IsPaused() status_t -BFileGameSound::Init(const entry_ref* file) +BFileGameSound::Init(BDataIO* data) { + fDataSource = data; + if (fDataSource == NULL) + return B_NO_MEMORY; + fAudioStream = new(std::nothrow) _gs_media_tracker; - if (!fAudioStream) + if (fAudioStream == NULL) return B_NO_MEMORY; memset(fAudioStream, 0, sizeof(_gs_media_tracker)); - fAudioStream->file = new(std::nothrow) BMediaFile(file); - if (!fAudioStream->file) { + fAudioStream->file = new(std::nothrow) BMediaFile(data); + if (fAudioStream->file == NULL) { delete fAudioStream; fAudioStream = NULL; return B_NO_MEMORY; @@ -443,7 +461,7 @@ BFileGameSound::Init(const entry_ref* file) bool BFileGameSound::Load() { - if (!fAudioStream || !fAudioStream->stream) + if ((fAudioStream == NULL) || (fAudioStream->stream == NULL)) return false; // read a new buffer