style changes. Also added a TODO taken from a comment in bug #575

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27705 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2008-09-23 11:02:58 +00:00
parent 0e79ce1ebc
commit 3beb2f72d7
+42 -31
View File
@@ -45,28 +45,33 @@
const int32 kInitSoundCount = 32;
const int32 kGrowth = 16;
static int32 deviceCount = 0;
static BGameSoundDevice* theDevice = NULL;
static int32 sDeviceCount = 0;
static BGameSoundDevice* sDevice = NULL;
BGameSoundDevice * GetDefaultDevice()
BGameSoundDevice *
GetDefaultDevice()
{
if (!theDevice)
theDevice = new BGameSoundDevice();
if (!sDevice)
sDevice = new BGameSoundDevice();
deviceCount++;
return theDevice;
sDeviceCount++;
return sDevice;
}
void ReleaseDevice()
void
ReleaseDevice()
{
deviceCount--;
sDeviceCount--;
if (deviceCount <= 0) {
delete theDevice;
theDevice = NULL;
if (sDeviceCount <= 0) {
delete sDevice;
sDevice = NULL;
}
}
// BGameSoundDevice -------------------------------------------------------
BGameSoundDevice::BGameSoundDevice()
: fIsConnected(false),
@@ -85,7 +90,7 @@ BGameSoundDevice::BGameSoundDevice()
BGameSoundDevice::~BGameSoundDevice()
{
BMediaRoster* r = BMediaRoster::Roster();
BMediaRoster* roster = BMediaRoster::Roster();
// We need to stop all the sounds before we stop the mixer
for (int32 i = 0; i < fSoundCount; i++) {
@@ -96,17 +101,18 @@ BGameSoundDevice::~BGameSoundDevice()
if (fIsConnected) {
// stop the nodes if they are running
r->StopNode(fConnection->producer, 0, true); // synchronous stop
roster->StopNode(fConnection->producer, 0, true);
// synchronous stop
// Ordinarily we'd stop *all* of the nodes in the chain at this point. However,
// one of the nodes is the System Mixer, and stopping the Mixer is a Bad Idea (tm).
// So, we just disconnect from it, and release our references to the nodes that
// we're using. We *are* supposed to do that even for global nodes like the Mixer.
r->Disconnect(fConnection->producer.node, fConnection->source,
roster->Disconnect(fConnection->producer.node, fConnection->source,
fConnection->consumer.node, fConnection->destination);
r->ReleaseNode(fConnection->producer);
r->ReleaseNode(fConnection->consumer);
roster->ReleaseNode(fConnection->producer);
roster->ReleaseNode(fConnection->consumer);
}
delete[] fSounds;
@@ -159,6 +165,7 @@ BGameSoundDevice::CreateBuffer(gs_id * sound,
err = fSounds[position]->Connect(&fConnection->producer);
}
if (err == B_OK)
*sound = gs_id(position + 1);
return err;
}
@@ -180,6 +187,7 @@ BGameSoundDevice::CreateBuffer(gs_id * sound,
err = fSounds[position]->Connect(&fConnection->producer);
}
if (err == B_OK)
*sound = gs_id(position+1);
return err;
}
@@ -221,6 +229,7 @@ BGameSoundDevice::Buffer(gs_id sound,
return B_OK;
}
status_t
BGameSoundDevice::StartPlaying(gs_id sound)
{
@@ -289,56 +298,58 @@ BGameSoundDevice::SetAttributes(gs_id sound,
status_t
BGameSoundDevice::Connect()
{
BMediaRoster* r = BMediaRoster::Roster();
status_t err;
BMediaRoster* roster = BMediaRoster::Roster();
// create your own audio mixer
// TODO: Don't do this!!! See bug #575
dormant_node_info mixer_dormant_info;
int32 mixer_count = 1; // for now, we only care about the first we find.
err = r->GetDormantNodes(&mixer_dormant_info, &mixer_count, 0, 0, 0, B_SYSTEM_MIXER, 0);
status_t err = roster->GetDormantNodes(&mixer_dormant_info,
&mixer_count, 0, 0, 0, B_SYSTEM_MIXER, 0);
if (err != B_OK)
return err;
//fMixer = new media_node;
err = r->InstantiateDormantNode(mixer_dormant_info, &fConnection->producer);
err = roster->InstantiateDormantNode(mixer_dormant_info, &fConnection->producer);
if (err != B_OK)
return err;
// retieve the system's audio mixer
err = r->GetAudioMixer(&fConnection->consumer);
err = roster->GetAudioMixer(&fConnection->consumer);
if (err != B_OK)
return err;
int32 count = 1;
media_input mixerInput;
err = r->GetFreeInputsFor(fConnection->consumer, &mixerInput, 1, &count);
err = roster->GetFreeInputsFor(fConnection->consumer, &mixerInput, 1, &count);
if (err != B_OK)
return err;
count = 1;
media_output mixerOutput;
err = r->GetFreeOutputsFor(fConnection->producer, &mixerOutput, 1, &count);
err = roster->GetFreeOutputsFor(fConnection->producer, &mixerOutput, 1, &count);
if (err != B_OK)
return err;
media_format format(mixerOutput.format);
err = r->Connect(mixerOutput.source, mixerInput.destination, &format, &mixerOutput, &mixerInput);
err = roster->Connect(mixerOutput.source, mixerInput.destination,
&format, &mixerOutput, &mixerInput);
if (err != B_OK)
return err;
// set the producer's time source to be the "default" time source, which
// the Mixer uses too.
r->GetTimeSource(&fConnection->timeSource);
r->SetTimeSourceFor(fConnection->producer.node, fConnection->timeSource.node);
roster->GetTimeSource(&fConnection->timeSource);
roster->SetTimeSourceFor(fConnection->producer.node, fConnection->timeSource.node);
// Start our mixer's time source if need be. Chances are, it won't need to be,
// but if we forget to do this, our mixer might not do anything at all.
BTimeSource* mixerTimeSource = r->MakeTimeSourceFor(fConnection->producer);
BTimeSource* mixerTimeSource = roster->MakeTimeSourceFor(fConnection->producer);
if (!mixerTimeSource)
return B_ERROR;
if (!mixerTimeSource->IsRunning()) {
status_t err = r->StartNode(mixerTimeSource->Node(), BTimeSource::RealTime());
status_t err = roster->StartNode(mixerTimeSource->Node(), BTimeSource::RealTime());
if (err != B_OK) {
mixerTimeSource->Release();
return err;
@@ -347,7 +358,7 @@ BGameSoundDevice::Connect()
// Start up our mixer
bigtime_t tpNow = mixerTimeSource->Now();
err = r->StartNode(fConnection->producer, tpNow + 10000);
err = roster->StartNode(fConnection->producer, tpNow + 10000);
mixerTimeSource->Release();
if (err != B_OK)
return err;
@@ -360,7 +371,7 @@ BGameSoundDevice::Connect()
fConnection->destination = mixerInput.destination;
// Set an appropriate run mode for the producer
r->SetRunModeNode(fConnection->producer, BMediaNode::B_INCREASE_LATENCY);
roster->SetRunModeNode(fConnection->producer, BMediaNode::B_INCREASE_LATENCY);
media_to_gs_format(&fFormat, &format.u.raw_audio);
fIsConnected = true;