AudioMixer: check for errors.

* Improve CreateBufferGroup function.
* Initialize variables.
* Check for BBufferGroup::InitCheck.
This commit is contained in:
Dario Casalinuovo
2015-07-11 15:40:17 +02:00
parent e3b2c4efea
commit 8c19f07f3c
2 changed files with 43 additions and 18 deletions
@@ -108,8 +108,8 @@ AudioMixer::AudioMixer(BMediaAddOn *addOn, bool isSystemMixer)
BMediaEventLooper(), BMediaEventLooper(),
fAddOn(addOn), fAddOn(addOn),
fCore(new MixerCore(this)), fCore(new MixerCore(this)),
fWeb(0), fWeb(NULL),
fBufferGroup(0), fBufferGroup(NULL),
fDownstreamLatency(1), fDownstreamLatency(1),
fInternalLatency(1), fInternalLatency(1),
fDisableStop(false), fDisableStop(false),
@@ -573,6 +573,8 @@ AudioMixer::FormatChangeRequested(const media_source &source,
fCore->Lock(); fCore->Lock();
status_t status = B_OK;
BBufferGroup *group = NULL;
MixerOutput *output = fCore->Output(); MixerOutput *output = fCore->Output();
if (!output) { if (!output) {
ERROR("AudioMixer::FormatChangeRequested: no output\n"); ERROR("AudioMixer::FormatChangeRequested: no output\n");
@@ -638,16 +640,21 @@ AudioMixer::FormatChangeRequested(const media_source &source,
// apply format change // apply format change
fCore->OutputFormatChanged(io_format->u.raw_audio); fCore->OutputFormatChanged(io_format->u.raw_audio);
delete fBufferGroup; status = CreateBufferGroup(&group);
fBufferGroup = CreateBufferGroup(); if (status != B_OK)
fCore->SetOutputBufferGroup(fBufferGroup); return status;
else {
delete fBufferGroup;
fBufferGroup = group;
fCore->SetOutputBufferGroup(fBufferGroup);
}
fCore->Unlock(); fCore->Unlock();
return B_OK; return status;
err: err:
fCore->Unlock(); fCore->Unlock();
return B_ERROR; return status;
} }
@@ -703,8 +710,11 @@ AudioMixer::SetBufferGroup(const media_source &for_source,
} }
fCore->Lock(); fCore->Lock();
if (!newGroup) if (!newGroup) {
newGroup = CreateBufferGroup(); status_t status = CreateBufferGroup(&newGroup);
if (status != B_OK)
return status;
}
fCore->SetOutputBufferGroup(newGroup); fCore->SetOutputBufferGroup(newGroup);
delete fBufferGroup; delete fBufferGroup;
fBufferGroup = newGroup; fBufferGroup = newGroup;
@@ -931,14 +941,18 @@ AudioMixer::Connect(status_t error, const media_source &source,
// we need to inform all connected *inputs* about *our* change in latency // we need to inform all connected *inputs* about *our* change in latency
PublishEventLatencyChange(); PublishEventLatencyChange();
fCore->Lock();
// Set up the buffer group for our connection, as long as nobody handed // Set up the buffer group for our connection, as long as nobody handed
// us a buffer group (via SetBufferGroup()) prior to this. That can // us a buffer group (via SetBufferGroup()) prior to this. That can
// happen, for example, if the consumer calls SetOutputBuffersFor() on // happen, for example, if the consumer calls SetOutputBuffersFor() on
// us from within its Connected() method. // us from within its Connected() method.
if (!fBufferGroup) if (!fBufferGroup) {
fBufferGroup = CreateBufferGroup(); BBufferGroup *group = NULL;
if (CreateBufferGroup(&group) != B_OK)
fCore->Lock(); return;
fBufferGroup = group;
}
ASSERT(fCore->Output() != 0); ASSERT(fCore->Output() != 0);
@@ -991,7 +1005,7 @@ AudioMixer::Disconnect(const media_source& what, const media_destination& where)
// destroy buffer group // destroy buffer group
delete fBufferGroup; delete fBufferGroup;
fBufferGroup = 0; fBufferGroup = NULL;
fCore->SetOutputBufferGroup(0); fCore->SetOutputBufferGroup(0);
fCore->Unlock(); fCore->Unlock();
@@ -1148,8 +1162,8 @@ AudioMixer::PublishEventLatencyChange()
} }
BBufferGroup* status_t
AudioMixer::CreateBufferGroup() AudioMixer::CreateBufferGroup(BBufferGroup** buffer) const
{ {
// allocate enough buffers to span our downstream latency // allocate enough buffers to span our downstream latency
// (plus one for rounding up), plus one extra // (plus one for rounding up), plus one extra
@@ -1168,7 +1182,18 @@ AudioMixer::CreateBufferGroup()
TRACE("AudioMixer: allocating %ld buffers of %ld bytes each\n", TRACE("AudioMixer: allocating %ld buffers of %ld bytes each\n",
count, size); count, size);
return new BBufferGroup(size, count);
BBufferGroup* buf = new BBufferGroup(size, count);
if (buf == NULL)
return B_NO_MEMORY;
status_t status = buf->InitCheck();
if (status != B_OK)
delete buf;
else
*buffer = buf;
return status;
} }
@@ -38,7 +38,7 @@ public:
void HandleInputBuffer(BBuffer* buffer, void HandleInputBuffer(BBuffer* buffer,
bigtime_t lateness); bigtime_t lateness);
BBufferGroup* CreateBufferGroup(); status_t CreateBufferGroup(BBufferGroup** buffer) const;
status_t SendBuffer(BBuffer* buffer, status_t SendBuffer(BBuffer* buffer,
MixerOutput* output); MixerOutput* output);