Skip to content

fix(SRP): crash when native OpenAL is unavailable - #195

Merged
star-e merged 11 commits into
cocos:v4.0.0from
troublemaker52025:fix(SRP)-Native-Audio-Crash
Jul 15, 2026
Merged

fix(SRP): crash when native OpenAL is unavailable#195
star-e merged 11 commits into
cocos:v4.0.0from
troublemaker52025:fix(SRP)-Native-Audio-Crash

Conversation

@troublemaker52025

@troublemaker52025 troublemaker52025 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Re: #

Changelog

  • 原生 Audio 存在一个设计缺陷:当 OpenAL 不可用时,会使用这个 sAudioEngineImpl = nullptr 指针直接调用 getPCMHeader()、getOriginalPCMBuffer() 导致程序崩溃
  • 现在按照以下三种情况修复:
情况 处理
OpenAL 可用且已初始化 直接复用 sAudioEngineImpl ,无额外开销
OpenAL 可用但尚未初始化 lazyInit() 触发完整初始化,行为与改动前完全一致
OpenAL 不可用 lazyInit() 失败后降级到 sDecoderImpl ,PCM 解码依然正常

Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

@troublemaker52025

Copy link
Copy Markdown
Contributor Author

@cocos-robot run test cases

@github-actions

github-actions Bot commented Jul 10, 2026

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1014446 bytes 1014446 bytes ✅ 0 bytes
2D All (legacy pipeline) 2681971 bytes 2681971 bytes ✅ 0 bytes
2D All (new pipeline) 2773763 bytes 2773763 bytes ✅ 0 bytes
(2D + 3D) All 10031072 bytes 10031072 bytes ✅ 0 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16867485 bytes 16867485 bytes ✅ 0 bytes

Interface Check Report

This pull request does not change any public interfaces !

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-draw-before-init
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash in the native AudioEngine PCM decode APIs (getPCMHeader / getOriginalPCMBuffer) when the OpenAL-backed AudioEngineImpl cannot be initialized, by introducing a decoder-only fallback path.

Changes:

  • Adds a decoder-only initialization entrypoint (AudioEngineImpl::initDecoder) and routes PCM decode APIs through a dedicated getDecoderImpl() selector.
  • Improves OpenAL initialization error handling/logging when device/context creation fails.
  • Applies multiple formatting/comment cleanups across platform backends (oalsoft/apple/android).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
native/cocos/audio/oalsoft/AudioEngine-soft.h Adds initDecoder() declaration; minor comment formatting.
native/cocos/audio/oalsoft/AudioEngine-soft.cpp Implements initDecoder(), adds OpenAL init error checks/logs, and minor comment formatting.
native/cocos/audio/include/AudioEngine.h Adds protected getDecoderImpl() strategy + decoder-only fallback member.
native/cocos/audio/AudioEngine.cpp Implements sDecoderImpl, getDecoderImpl(), and routes PCM APIs through it.
native/cocos/audio/apple/AudioEngine-inl.mm Adds AudioEngineImpl::initDecoder() stub for uniform compilation.
native/cocos/audio/apple/AudioEngine-inl.h Declares initDecoder(); minor comment formatting.
native/cocos/audio/android/AudioEngine-inl.h Declares initDecoder(); minor formatting.
native/cocos/audio/android/AudioEngine-inl.cpp Adds initDecoder() stub; mostly formatting/indentation changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread native/cocos/audio/AudioEngine.cpp
Comment thread native/cocos/audio/oalsoft/AudioEngine-soft.cpp
Comment thread native/cocos/audio/oalsoft/AudioEngine-soft.cpp Outdated
Comment thread native/cocos/audio/oalsoft/AudioEngine-soft.cpp Outdated
troublemaker52025 and others added 2 commits July 10, 2026 15:03
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@troublemaker52025

Copy link
Copy Markdown
Contributor Author

@cocos-robot run test cases

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-continuous-filling
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX
if (AudioEngine::sDecoderImpl == nullptr) {
AudioEngine::sDecoderImpl = ccnew AudioEngineImpl();
AudioEngineImpl::initDecoder();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to make initDecoder() a non-static member function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keep static member function will be batter. Called independently without relying on a full OpenAL initialization

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

Comment thread native/cocos/audio/oalsoft/AudioEngine-soft.cpp Outdated
troublemaker52025 and others added 2 commits July 15, 2026 11:29
@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@troublemaker52025, Please check the result of run test cases:

Task Details

@star-e
star-e merged commit df96ee8 into cocos:v4.0.0 Jul 15, 2026
35 checks passed
@github-actions

Copy link
Copy Markdown

@troublemaker52025 ❗ There was an error during the execution of the tasks. Please check the logs for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants