BFileGameSound: allow initializing from a BDataIO
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 <[email protected]>
This commit is contained in:
committed by
Stephan Aßmus
parent
041bbff9b0
commit
69f814cded
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include <StreamingGameSound.h>
|
#include <StreamingGameSound.h>
|
||||||
|
|
||||||
|
#include <DataIO.h>
|
||||||
|
|
||||||
|
|
||||||
struct entry_ref;
|
struct entry_ref;
|
||||||
struct _gs_media_tracker;
|
struct _gs_media_tracker;
|
||||||
@@ -25,6 +27,9 @@ public:
|
|||||||
BFileGameSound(const char* file,
|
BFileGameSound(const char* file,
|
||||||
bool looping = true,
|
bool looping = true,
|
||||||
BGameSoundDevice* device = NULL);
|
BGameSoundDevice* device = NULL);
|
||||||
|
BFileGameSound(BDataIO* data,
|
||||||
|
bool looping = true,
|
||||||
|
BGameSoundDevice* device = NULL);
|
||||||
|
|
||||||
virtual ~BFileGameSound();
|
virtual ~BFileGameSound();
|
||||||
|
|
||||||
@@ -52,7 +57,7 @@ private:
|
|||||||
BFileGameSound& operator=(const BFileGameSound& other);
|
BFileGameSound& operator=(const BFileGameSound& other);
|
||||||
// not implemented
|
// not implemented
|
||||||
|
|
||||||
status_t Init(const entry_ref* file);
|
status_t Init(BDataIO* data);
|
||||||
|
|
||||||
bool Load();
|
bool Load();
|
||||||
bool Read(void* buffer, size_t bytes);
|
bool Read(void* buffer, size_t bytes);
|
||||||
@@ -95,14 +100,17 @@ private:
|
|||||||
size_t fBufferSize;
|
size_t fBufferSize;
|
||||||
size_t fPlayPosition;
|
size_t fPlayPosition;
|
||||||
|
|
||||||
thread_id fReadThread;
|
|
||||||
port_id fPort;
|
|
||||||
|
|
||||||
_gs_ramp* fPausing;
|
_gs_ramp* fPausing;
|
||||||
bool fPaused;
|
bool fPaused;
|
||||||
float fPauseGain;
|
float fPauseGain;
|
||||||
|
|
||||||
|
BDataIO* fDataSource;
|
||||||
|
|
||||||
|
#ifdef B_HAIKU_64_BIT
|
||||||
uint32 _reserved[9];
|
uint32 _reserved[9];
|
||||||
|
#else
|
||||||
|
uint32 _reserved[10];
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <Entry.h>
|
#include <Entry.h>
|
||||||
|
#include <File.h>
|
||||||
#include <FileGameSound.h>
|
#include <FileGameSound.h>
|
||||||
#include <MediaFile.h>
|
#include <MediaFile.h>
|
||||||
#include <MediaTrack.h>
|
#include <MediaTrack.h>
|
||||||
@@ -130,7 +131,7 @@ BFileGameSound::BFileGameSound(const entry_ref* file, bool looping,
|
|||||||
fPauseGain(1.0)
|
fPauseGain(1.0)
|
||||||
{
|
{
|
||||||
if (InitCheck() == B_OK)
|
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)
|
if (get_ref_for_path(file, &node) != B_OK)
|
||||||
SetInitError(B_ENTRY_NOT_FOUND);
|
SetInitError(B_ENTRY_NOT_FOUND);
|
||||||
else
|
else {
|
||||||
SetInitError(Init(&node));
|
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()
|
BFileGameSound::~BFileGameSound()
|
||||||
{
|
{
|
||||||
if (fReadThread >= 0) {
|
if (fAudioStream != NULL) {
|
||||||
// TODO: kill_thread() is very bad, since it will leak any resources
|
if (fAudioStream->stream != NULL)
|
||||||
// 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)
|
|
||||||
fAudioStream->file->ReleaseTrack(fAudioStream->stream);
|
fAudioStream->file->ReleaseTrack(fAudioStream->stream);
|
||||||
|
|
||||||
delete fAudioStream->file;
|
delete fAudioStream->file;
|
||||||
@@ -177,6 +190,7 @@ BFileGameSound::~BFileGameSound()
|
|||||||
|
|
||||||
delete [] fBuffer;
|
delete [] fBuffer;
|
||||||
delete fAudioStream;
|
delete fAudioStream;
|
||||||
|
delete fDataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -204,7 +218,7 @@ BFileGameSound::StopPlaying()
|
|||||||
{
|
{
|
||||||
status_t error = BStreamingGameSound::StopPlaying();
|
status_t error = BStreamingGameSound::StopPlaying();
|
||||||
|
|
||||||
if (!fAudioStream || !fAudioStream->stream)
|
if ((fAudioStream == NULL) || (fAudioStream->stream == NULL))
|
||||||
return B_OK;
|
return B_OK;
|
||||||
|
|
||||||
// start reading next time from the start of the file
|
// start reading next time from the start of the file
|
||||||
@@ -367,15 +381,19 @@ BFileGameSound::IsPaused()
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
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;
|
fAudioStream = new(std::nothrow) _gs_media_tracker;
|
||||||
if (!fAudioStream)
|
if (fAudioStream == NULL)
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
|
|
||||||
memset(fAudioStream, 0, sizeof(_gs_media_tracker));
|
memset(fAudioStream, 0, sizeof(_gs_media_tracker));
|
||||||
fAudioStream->file = new(std::nothrow) BMediaFile(file);
|
fAudioStream->file = new(std::nothrow) BMediaFile(data);
|
||||||
if (!fAudioStream->file) {
|
if (fAudioStream->file == NULL) {
|
||||||
delete fAudioStream;
|
delete fAudioStream;
|
||||||
fAudioStream = NULL;
|
fAudioStream = NULL;
|
||||||
return B_NO_MEMORY;
|
return B_NO_MEMORY;
|
||||||
@@ -443,7 +461,7 @@ BFileGameSound::Init(const entry_ref* file)
|
|||||||
bool
|
bool
|
||||||
BFileGameSound::Load()
|
BFileGameSound::Load()
|
||||||
{
|
{
|
||||||
if (!fAudioStream || !fAudioStream->stream)
|
if ((fAudioStream == NULL) || (fAudioStream->stream == NULL))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// read a new buffer
|
// read a new buffer
|
||||||
|
|||||||
Reference in New Issue
Block a user