Skip to content

Commit cba65b0

Browse files
committed
fix getPosition and bufferirng for released buffer
(cherry picked from commit b2a65651244b233402571aa5dd885006384ebf23)
1 parent ee05bb1 commit cba65b0

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

src/audiobuffer/audiobuffer.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace SoLoud
1515
{
1616
std::mutex buffer_lock_mutex;
17+
std::mutex check_buffer_mutex;
1718

1819
BufferStreamInstance::BufferStreamInstance(BufferStream *aParent)
1920
{
@@ -36,9 +37,10 @@ namespace SoLoud
3637
// Calculate mStreamPosition based on mOffset
3738
mStreamPosition = mOffset / (float)(mSamplerate * mChannels);
3839

40+
3941
// This is not nice to do in the audio callback, but I didn't
4042
// find a better way to get lenght and pause the sound and the
41-
// `chakeBuffering` function is fast enough.
43+
// `checkBuffering` function is fast enough.
4244
if (!mParent->mIsBuffering) {
4345
mParent->mThePlayer->soloud.unlockAudioMutex_internal();
4446
mParent->checkBuffering(0);
@@ -118,6 +120,10 @@ namespace SoLoud
118120

119121
result BufferStreamInstance::seek(double aSeconds, float *mScratch, unsigned int mScratchSize)
120122
{
123+
if (aSeconds <= 0.0) {
124+
rewind();
125+
return SO_NO_ERROR;
126+
}
121127
if (mParent->mBuffer.bufferingType == BufferingType::RELEASED) {
122128
// Seeking not supported in RELEASED mode since data is discarded
123129
// TODO: Support seeking forward in RELEASED mode
@@ -151,10 +157,6 @@ namespace SoLoud
151157

152158
result BufferStreamInstance::rewind()
153159
{
154-
if (mParent->mBuffer.bufferingType == BufferingType::RELEASED) {
155-
// Rewinding not supported in RELEASED mode since data is discarded
156-
return INVALID_PARAMETER;
157-
}
158160
mOffset = 0;
159161
mStreamPosition = 0.0f;
160162
return 0;
@@ -255,6 +257,13 @@ namespace SoLoud
255257
mBuffer.clear();
256258
mSampleCount = 0;
257259
mBytesReceived = 0;
260+
mUncompressedBytesReceived = 0;
261+
262+
for (int i = 0; i < mParent->handle.size(); i++)
263+
{
264+
mThePlayer->soloud.seek(mParent->handle[i].handle, 0.0f);
265+
mParent->handle[i].bufferingTime = 0.0f;
266+
}
258267
}
259268

260269
void BufferStream::setDataIsEnded()
@@ -335,7 +344,8 @@ namespace SoLoud
335344
buffer.erase(buffer.begin(), buffer.begin() + bufferDataToAdd);
336345
}
337346

338-
checkBuffering(bytesWritten);
347+
if (mIsBuffering)
348+
checkBuffering(bytesWritten);
339349
mUncompressedBytesReceived += bytesWritten;
340350

341351
mSampleCount += bytesWritten / mPCMformat.bytesPerSample;
@@ -355,17 +365,24 @@ namespace SoLoud
355365
/// if needed after adding [afterAddingBytesCount] bytes.
356366
void BufferStream::checkBuffering(unsigned int afterAddingBytesCount)
357367
{
368+
std::lock_guard<std::mutex> lock(check_buffer_mutex);
369+
358370
// If a handle reaches the end and data is not ended, we have to wait for it has enough data
359371
// to reach [TIME_FOR_BUFFERING] and restart playing it.
360372
SoLoud::time currBufferTime = getLength();
361373
SoLoud::time addedDataTime = (afterAddingBytesCount / mPCMformat.bytesPerSample) / (mBaseSamplerate * mChannels);
374+
SoLoud::time bufferLength = (double)mBuffer.getFloatsBufferSize() / (mBaseSamplerate * mChannels);
362375

363376
for (int i = 0; i < mParent->handle.size(); i++)
364377
{
365378
SoLoud::handle handle = mParent->handle[i].handle;
366-
SoLoud::time pos = mBuffer.bufferingType == BufferingType::RELEASED ? getStreamTimeConsumed() : mThePlayer->getPosition(handle);
379+
SoLoud::time pos = mBuffer.bufferingType == BufferingType::RELEASED ? getStreamTimeConsumed() : mThePlayer->getPosition(handle);
380+
// SoLoud::time pos = mThePlayer->getPosition(handle);
367381
bool isPaused = mThePlayer->getPause(handle);
368382

383+
printf("checkBuffering -- bufferLength: %lf, currBufferTime: %lf\n",
384+
bufferLength, currBufferTime);
385+
369386
// This handle needs to wait for [TIME_FOR_BUFFERING]. Pause it.
370387
if (pos >= currBufferTime + addedDataTime && !isPaused)
371388
{
@@ -375,8 +392,7 @@ namespace SoLoud
375392
callOnBufferingCallback(true, handle, currBufferTime);
376393
} else
377394
// This handle has reached [TIME_FOR_BUFFERING]. Unpause it.
378-
if (currBufferTime + addedDataTime - mParent->handle[i].bufferingTime >= mBufferingTimeNeeds &&
379-
isPaused)
395+
if (currBufferTime + addedDataTime - mParent->handle[i].bufferingTime >= mBufferingTimeNeeds && isPaused)
380396
{
381397
mThePlayer->setPause(handle, false);
382398
isPaused = false;
@@ -387,6 +403,7 @@ namespace SoLoud
387403
if (dataIsEnded && isPaused)
388404
{
389405
mThePlayer->setPause(handle, false);
406+
isPaused = false;
390407
mParent->handle[i].bufferingTime = MAX_DOUBLE;
391408
callOnBufferingCallback(false, handle, currBufferTime);
392409
}
@@ -438,6 +455,7 @@ namespace SoLoud
438455
/// Get the time consumed by this stream of type RELEASED
439456
SoLoud::time BufferStream::getStreamTimeConsumed()
440457
{
441-
return (mBytesConsumed / mPCMformat.bytesPerSample) / (mBaseSamplerate * mPCMformat.channels);
458+
// printf("getStreamTimeConsumed %f\n", (mBytesConsumed / mPCMformat.bytesPerSample) / (mBaseSamplerate * mPCMformat.channels));
459+
return (double)(mBytesConsumed / mPCMformat.bytesPerSample) / (mBaseSamplerate * mPCMformat.channels);
442460
}
443461
};

0 commit comments

Comments
 (0)