New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add test case for issue #18597(audio crash on iOS), and fix #18865
Conversation
@dumganhar could you review this PR? |
cocos/audio/apple/AudioPlayer.mm
Outdated
ALint bufferProcessed = 0; | ||
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed); | ||
while (bufferProcessed < QUEUEBUFFER_NUM) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this sleep waiting be smaller, it may lead to audio lag if there are buffers in queue.
Please test it with 1 or 2 milliseconds.
cocos/audio/apple/AudioPlayer.mm
Outdated
@@ -290,7 +305,13 @@ of this software and associated documentation files (the "Software"), to deal | |||
break; | |||
} | |||
} | |||
|
|||
/* | |||
While the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the
: don't put just two words in a line.
if (sourceState == AL_PLAYING) { | ||
alGetSourcei(_alSource, AL_BUFFERS_PROCESSED, &bufferProcessed); | ||
while (bufferProcessed < QUEUEBUFFER_NUM) { | ||
std::this_thread::sleep_for(std::chrono::milliseconds(2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have counted time cost of this code block, it's about 2 * QUEUEBUFFER_TIME_STEP
, about cost 160-240 ms when the QUEUEBUFFER_TIME_STEP is 0.1
, about 80-120 ms when the QUEUEBUFFER_TIME_STEP is 0.05
.
time cost when the QUEUEBUFFER_TIME_STEP is 0.05
UnqueueBuffers spend extra time: 131.72 ms
UnqueueBuffers spend extra time: 86.84 ms
issues 18597 audio crash test
UnqueueBuffers spend extra time: 98.89 ms
UnqueueBuffers spend extra time: 87.53 ms
issues 18597 audio crash test
UnqueueBuffers spend extra time: 121.34 ms
UnqueueBuffers spend extra time: 86.42 ms
issues 18597 audio crash test
UnqueueBuffers spend extra time: 80.64 ms
UnqueueBuffers spend extra time: 86.46 ms
not matter when changing the sleep time to 1 ms or 2 ms. change the QUEUEBUFFER_TIME_STEP from 0.1 to 0.05 will half the time cost. It's meaningful.
after changing QUEUEBUFFER_TIME_STEP
to 0.05
, I have tested play bigger audio file on iPhone 5s, 6, 8, all play smoothly.
@@ -305,15 +330,15 @@ of this software and associated documentation files (the "Software"), to deal | |||
|
|||
if (!_needWakeupRotateThread) | |||
{ | |||
_sleepCondition.wait_for(lk,std::chrono::milliseconds(75)); | |||
_sleepCondition.wait_for(lk,std::chrono::milliseconds(rotateSleepTime)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the sleep time in rotate buffer thread shouldn't been hard coded, it depends on QUEUEBUFFER_TIME_STEP
according to the feedback form #18597 , this patch works well. |
@drelaptop // Mainpage::init int idBackGroundMusic = AudioEngine::play2d(maintheme.mp3); after some background/foreground (pauseAll()/resumeAll()) I received this log, and maintheme stops V/AudioPlayer (70): ~AudioPlayer() (0x103817430), id=1 Cocos2d-x 3.16, iOS, iPad mini 3 |
@DonMizzi could you try to pick this PR into your 3.16, and then test again. |
Yes, I have just tried, but forgot to report: https://discuss.cocos2d-x.org/t/audioengine-stop-loop-background-music-on-resume/44552/2 With this PR frequency of occurency incraease. If I set QUEUEBUFFER_TIME_STEP = 0.1, problems happens less frequent with or without this PR. There are differences also using home button or lock screen button: with lock screen error always happens more frequent. With home button error happens on resume, with lock button happens on background So: with this patch and QUEUEBUFFER_TIME_STEP = 0.05 using Home Button - 9/10 tests - Error without this patch and QUEUEBUFFER_TIME_STEP = 0.1 using Home Button - 50 tests - NO Error |
After a few more tests, seems that with or without PR something change: without this patch and QUEUEBUFFER_TIME_STEP = 0.1 using Home Button - 50 tests - NO Error with this patch but QUEUEBUFFER_TIME_STEP = 0.1 using Home Button - 40/50 tests - Error (very rare) test is a single Background/Foreground |
Why is the AudioPlayer getting destroyed when pauseAll is called? |
add test case for #18597, and try to fix