Skip to content
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

V3.8.5 oh api12 #17869

Merged
merged 11 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/rendering/legacy/index.jsb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { RenderTexture } from '../../asset/assets/render-texture';

export function createDefaultPipeline (): ForwardPipeline {
const rppl = new ForwardPipeline();
rppl.initialize({ flows: [] });
//rppl.initialize({ flows: [] });
minggo marked this conversation as resolved.
Show resolved Hide resolved
return rppl;
}
export const ForwardPipeline: typeof NrForwardPipeline = nr.ForwardPipeline;
Expand Down
101 changes: 90 additions & 11 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ cc_set_if_undefined(CC_DEBUG_FORCE OFF)
cc_set_if_undefined(USE_SE_V8 ON)
cc_set_if_undefined(USE_SE_SM OFF)
cc_set_if_undefined(USE_SE_NAPI OFF)
if(OPENHARMONY)
cc_set_if_undefined(USE_SE_JSVM OFF)
endif()
cc_set_if_undefined(USE_V8_DEBUGGER ON)
cc_set_if_undefined(USE_V8_DEBUGGER_FORCE OFF)
cc_set_if_undefined(USE_SOCKET ON)
Expand Down Expand Up @@ -344,21 +347,32 @@ NO_WERROR cocos/audio/common/utils/primitives.cpp
cocos/audio/android/OpenSLHelper.h
cocos/audio/android/PcmAudioPlayer.cpp
cocos/audio/android/PcmAudioPlayer.h
cocos/audio/android/PcmAudioService.cpp
cocos/audio/android/PcmAudioService.h
cocos/audio/android/PcmBufferProvider.cpp
cocos/audio/android/PcmBufferProvider.h
cocos/audio/android/PcmData.cpp
cocos/audio/android/PcmData.h
cocos/audio/android/Track.cpp
cocos/audio/android/Track.h
cocos/audio/android/UrlAudioPlayer.cpp
cocos/audio/android/UrlAudioPlayer.h
cocos/audio/android/utils/Compat.h
cocos/audio/android/utils/Errors.h
cocos/audio/android/utils/Utils.cpp
cocos/audio/android/utils/Utils.h
)
if(ANDROID)
cocos_source_files(
cocos/audio/android/PcmAudioService.cpp
cocos/audio/android/PcmAudioService.h
cocos/audio/android/UrlAudioPlayer.cpp
cocos/audio/android/UrlAudioPlayer.h
)
elseif(OPENHARMONY)
cocos_source_files(
cocos/audio/openharmony/PcmAudioService.cpp
cocos/audio/openharmony/PcmAudioService.h
cocos/audio/openharmony/UrlAudioPlayer.cpp
cocos/audio/openharmony/UrlAudioPlayer.h
)
endif()
elseif(OHOS)
cocos_source_files(
cocos/audio/common/utils/tinysndfile.cpp
Expand Down Expand Up @@ -558,7 +572,9 @@ elseif(OPENHARMONY)
cocos_source_files(
cocos/platform/openharmony/napi/NapiHelper.cpp
cocos/platform/openharmony/napi/NapiHelper.h

cocos/platform/openharmony/napi/NapiValueConverter.cpp
cocos/platform/openharmony/napi/NapiValueConverter.h
cocos/platform/openharmony/napi/native_common.h
cocos/platform/openharmony/WorkerMessageQueue.cpp
cocos/platform/openharmony/WorkerMessageQueue.h
cocos/platform/openharmony/OpenHarmonyPlatform.cpp
Expand Down Expand Up @@ -2575,6 +2591,8 @@ if(ANDROID)
elseif(OPENHARMONY)
cocos_source_files(
cocos/bindings/manual/jsb_platform_openharmony.cpp
cocos/bindings/manual/JavaScriptArkTsBridge.cpp
cocos/bindings/manual/JavaScriptArkTsBridge.h
)
elseif(OHOS)
cocos_source_files(
Expand Down Expand Up @@ -2695,6 +2713,25 @@ if(USE_SE_NAPI)
)
endif()



if(OPENHARMONY AND USE_SE_JSVM)
cocos_source_files(MODULE ccbindings
cocos/bindings/jswrapper/jsvm/Class.cpp
cocos/bindings/jswrapper/jsvm/Class.h
cocos/bindings/jswrapper/jsvm/HelperMacros.cpp
cocos/bindings/jswrapper/jsvm/HelperMacros.h
cocos/bindings/jswrapper/jsvm/Object.cpp
cocos/bindings/jswrapper/jsvm/Object.h
cocos/bindings/jswrapper/jsvm/ScriptEngine.cpp
cocos/bindings/jswrapper/jsvm/ScriptEngine.h
cocos/bindings/jswrapper/jsvm/SeApi.h
cocos/bindings/jswrapper/jsvm/Utils.cpp
cocos/bindings/jswrapper/jsvm/Utils.h
cocos/bindings/jswrapper/jsvm/CommonHeader.h
)
endif()

cocos_source_files(MODULE ccbindings
cocos/bindings/jswrapper/config.h
cocos/bindings/jswrapper/config.cpp
Expand Down Expand Up @@ -3074,6 +3111,7 @@ function(cc_apply_definations target)
$<$<BOOL:${USE_SE_SM}>:SCRIPT_ENGINE_TYPE=1>
$<$<BOOL:${USE_SE_V8}>:SCRIPT_ENGINE_TYPE=2>
$<$<BOOL:${USE_SE_NAPI}>:SCRIPT_ENGINE_TYPE=5>
$<$<BOOL:${USE_SE_JSVM}>:SCRIPT_ENGINE_TYPE=6>
$<$<OR:$<CONFIG:Debug>,$<BOOL:${CC_DEBUG_FORCE}>>:CC_DEBUG=1>
)
endfunction()
Expand Down Expand Up @@ -3267,14 +3305,55 @@ if(OPENHARMONY)

find_library( hilog-lib
hilog_ndk.z )

find_library( libace-lib
libace_ndk.z )

target_link_libraries(${ENGINE_NAME} PUBLIC
${EGL-lib} ${GLESv3-lib} ${hilog-lib} libace_napi.z.so libace_ndk.z.so libz.so libuv.so libnative_drawing.so librawfile.z.so libOpenSLES.so

find_library( # Sets the name of the path variable.
libace-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
ace_ndk.z )
find_library( # Sets the name of the path variable.
libnapi-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
ace_napi.z )
find_library( # Sets the name of the path variable.
libuv-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
uv )
find_library( # Sets the name of the path variable.
rawfile-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
rawfile.z )
set(LIBS
${EGL-lib}
${GLESv3-lib}
${hilog-lib}
${libace-lib}
${libnapi-lib}
${libuv-lib}
${rawfile-lib}
libnative_drawing.so
libc++.a
libOpenSLES.so
libohaudio.so
libavplayer.so
libnative_window.so
libnative_buffer.so
libbundle_ndk.z.so
libace_napi.z.so
libace_ndk.z.so
libz.so
librawfile.z.so
${CC_EXTERNAL_LIBS}
)
if(USE_SE_JSVM)
list(APPEND LIBS libjsvm.so)
endif()

target_link_libraries(${ENGINE_NAME} PUBLIC ${LIBS})

endif()

if(APPLE)
Expand Down
69 changes: 40 additions & 29 deletions native/cocos/audio/android/AudioEngine-inl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ AudioEngineImpl *gAudioImpl = nullptr;
int outputSampleRate = 44100;
#if CC_PLATFORM == CC_PLATFORM_ANDROID
int bufferSizeInFrames = 192;
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
// TODO(hack) : There is currently a bug in the opensles module,
// so openharmony must configure a fixed size, otherwise the callback will be suspended
int bufferSizeInFrames = 2048;
#endif

void getAudioInfo() {
Expand Down Expand Up @@ -129,7 +125,7 @@ static int fdGetter(const ccstd::string &url, off_t *start, off_t *length) {
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
FileUtilsOpenHarmony* fileUtils = dynamic_cast<FileUtilsOpenHarmony*>(FileUtils::getInstance());
if(fileUtils) {
RawFileDescriptor descriptor;
RawFileDescriptor64 descriptor;
fileUtils->getRawFileDescriptor(url, descriptor);
fd = descriptor.fd;
}
Expand Down Expand Up @@ -193,25 +189,27 @@ bool AudioEngineImpl::init() {
CC_LOG_ERROR("get the engine interface fail");
break;
}

// create output mix
const SLInterfaceID outputMixIIDs[] = {};
const SLboolean outputMixReqs[] = {};
result = (*_engineEngine)->CreateOutputMix(_engineEngine, &_outputMixObject, 0, outputMixIIDs, outputMixReqs);
if (SL_RESULT_SUCCESS != result) {
CC_LOG_ERROR("create output mix fail");
break;
}

// realize the output mix
result = (*_outputMixObject)->Realize(_outputMixObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
CC_LOG_ERROR("realize the output mix fail");
break;
}

_audioPlayerProvider = ccnew AudioPlayerProvider(_engineEngine, _outputMixObject, outputSampleRate, bufferSizeInFrames, fdGetter, &gCallerThreadUtils);

#if CC_PLATFORM == CC_PLATFORM_ANDROID
// create output mix
const SLInterfaceID outputMixIIDs[] = {};
const SLboolean outputMixReqs[] = {};
result = (*_engineEngine)->CreateOutputMix(_engineEngine, &_outputMixObject, 0, outputMixIIDs, outputMixReqs);
if (SL_RESULT_SUCCESS != result) {
CC_LOG_ERROR("create output mix fail");
break;
}

// realize the output mix
result = (*_outputMixObject)->Realize(_outputMixObject, SL_BOOLEAN_FALSE);
if (SL_RESULT_SUCCESS != result) {
CC_LOG_ERROR("realize the output mix fail");
break;
}

_audioPlayerProvider = ccnew AudioPlayerProvider(_engineEngine, _outputMixObject, outputSampleRate, bufferSizeInFrames, fdGetter, &gCallerThreadUtils);
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
_audioPlayerProvider = new AudioPlayerProvider(_engineEngine, outputSampleRate, fdGetter, &gCallerThreadUtils);
Copy link
Contributor

Choose a reason for hiding this comment

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

Use ccnew to keep the same coding style.

#endif
ret = true;
} while (false);

Expand All @@ -229,9 +227,14 @@ int AudioEngineImpl::play2d(const ccstd::string &filePath, bool loop, float volu
auto audioId = AudioEngine::INVALID_AUDIO_ID;

do {
if (_engineEngine == nullptr || _audioPlayerProvider == nullptr) {
break;
}
#if CC_PLATFORM == CC_PLATFORM_ANDROID
if (_engineEngine == nullptr || _audioPlayerProvider == nullptr) {
break;
}
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
if (_audioPlayerProvider == nullptr)
break;
#endif

auto fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);

Expand Down Expand Up @@ -434,9 +437,12 @@ PCMHeader AudioEngineImpl::getPCMHeader(const char *url) {
CC_LOG_DEBUG("file %s pcm data already cached", url);
return header;
}

#if CC_PLATFORM == CC_PLATFORM_ANDROID
AudioDecoder *decoder = AudioDecoderProvider::createAudioDecoder(_engineEngine, fileFullPath, bufferSizeInFrames, outputSampleRate, fdGetter);

#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
AudioDecoder *decoder = AudioDecoderProvider::createAudioDecoder(
_engineEngine, fileFullPath, _audioPlayerProvider->getBufferSizeInFrames(), outputSampleRate, fdGetter);
#endif
if (decoder == nullptr) {
CC_LOG_DEBUG("decode %s failed, the file formate might not support", url);
return header;
Expand Down Expand Up @@ -470,7 +476,12 @@ ccstd::vector<uint8_t> AudioEngineImpl::getOriginalPCMBuffer(const char *url, ui
if (_audioPlayerProvider->getPcmData(url, data)) {
CC_LOG_DEBUG("file %s pcm data already cached", url);
} else {
#if CC_PLATFORM == CC_PLATFORM_ANDROID
AudioDecoder *decoder = AudioDecoderProvider::createAudioDecoder(_engineEngine, fileFullPath, bufferSizeInFrames, outputSampleRate, fdGetter);
#else
AudioDecoder *decoder = AudioDecoderProvider::createAudioDecoder(
_engineEngine, fileFullPath, _audioPlayerProvider->getBufferSizeInFrames(), outputSampleRate, fdGetter);
#endif
if (decoder == nullptr) {
CC_LOG_DEBUG("decode %s failed, the file formate might not support", url);
return pcmData;
Expand Down
4 changes: 4 additions & 0 deletions native/cocos/audio/android/AudioMixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ static inline audio_format_t selectMixerInFormat(audio_format_t inputFormat __un
return kUseFloat && kUseNewMixer ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
}

void AudioMixer::setBufferSize(size_t size) {
mState.frameCount = size;
}

int AudioMixer::getTrackName(audio_channel_mask_t channelMask,
audio_format_t format, int sessionId) {
if (!isValidPcmTrackFormat(format)) {
Expand Down
1 change: 1 addition & 0 deletions native/cocos/audio/android/AudioMixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class AudioMixer {
void setParameter(int name, int target, int param, void *value);

void setBufferProvider(int name, AudioBufferProvider *bufferProvider);
void setBufferSize(size_t size);
void process(int64_t pts);

uint32_t trackNames() const { return mTrackNames; }
Expand Down
30 changes: 29 additions & 1 deletion native/cocos/audio/android/AudioMixerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,25 @@
#include "base/memory/Memory.h"

namespace cc {

#if CC_PLATFORM == CC_PLATFORM_ANDROID
AudioMixerController::AudioMixerController(int bufferSizeInFrames, int sampleRate, int channelCount)
: _bufferSizeInFrames(bufferSizeInFrames), _sampleRate(sampleRate), _channelCount(channelCount), _mixer(nullptr), _isPaused(false), _isMixingFrame(false) {
ALOGV("In the constructor of AudioMixerController!");

_mixingBuffer.size = (size_t)bufferSizeInFrames * 2 * channelCount;

Check failure on line 41 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast (google-readability-casting)
// Don't use posix_memalign since it was added from API 16, it will crash on Android 2.3
// Therefore, for a workaround, we uses memalign here.
_mixingBuffer.buf = memalign(32, _mixingBuffer.size);
memset(_mixingBuffer.buf, 0, _mixingBuffer.size);
}
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
AudioMixerController::AudioMixerController(int sampleRate, int channelCount)
: _sampleRate(sampleRate), _channelCount(channelCount), _mixer(nullptr), _isPaused(false), _isMixingFrame(false) {
ALOGV("In the constructor of AudioMixerController!");
int32_t maxBufferLength = MAX_AUDIO_BUFFER_SIZE * channelCount * 2;
_mixingBuffer.buf = memalign(32, maxBufferLength);
}
#endif

AudioMixerController::~AudioMixerController() {
destroy();
Expand All @@ -56,11 +64,31 @@
free(_mixingBuffer.buf);
}

#if CC_PLATFORM == CC_PLATFORM_ANDROID
bool AudioMixerController::init() {
_mixer = ccnew AudioMixer(_bufferSizeInFrames, _sampleRate);
return _mixer != nullptr;
}

#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
void AudioMixerController::updateBufferSize(int bufferSize) {
_mixer->setBufferSize(bufferSize / _channelCount / 2);
_mixingBuffer.size = bufferSize;

uint32_t channelMask = audio_channel_out_mask_from_count(_channelCount);
int32_t name = _mixer->getTrackName(channelMask, AUDIO_FORMAT_PCM_16_BIT, AUDIO_SESSION_OUTPUT_MIX);
_mixer->setParameter(name, AudioMixer::TRACK, AudioMixer::MAIN_BUFFER,
_mixingBuffer.buf);
}

bool AudioMixerController::init(int bufferSizeInFrames) {
_mixingBuffer.size = (size_t)bufferSizeInFrames * 2 * _channelCount;
memset(_mixingBuffer.buf, 0, _mixingBuffer.size);
_bufferSizeInFrames = bufferSizeInFrames;
_mixer = ccnew AudioMixer(_bufferSizeInFrames, _sampleRate);
return _mixer != nullptr;
}
#endif
bool AudioMixerController::addTrack(Track *track) {
ALOG_ASSERT(track != nullptr, "Shouldn't pass nullptr to addTrack");
bool ret = false;
Expand All @@ -85,7 +113,7 @@
}

void AudioMixerController::initTrack(Track *track, ccstd::vector<Track *> &tracksToRemove) {
if (track->isInitialized())

Check failure on line 116 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

statement should be inside braces (google-readability-braces-around-statements)
return;

uint32_t channelMask = audio_channel_out_mask_from_count(2);
Expand All @@ -103,22 +131,22 @@
name,
AudioMixer::TRACK,
AudioMixer::MIXER_FORMAT,
(void *)(uintptr_t)AUDIO_FORMAT_PCM_16_BIT);

Check failure on line 134 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast (google-readability-casting)

Check failure on line 134 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast (google-readability-casting)
_mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::FORMAT,
(void *)(uintptr_t)AUDIO_FORMAT_PCM_16_BIT);

Check failure on line 139 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast (google-readability-casting)

Check failure on line 139 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast (google-readability-casting)
_mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::MIXER_CHANNEL_MASK,
(void *)(uintptr_t)channelMask);

Check failure on line 144 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast (google-readability-casting)

Check failure on line 144 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast (google-readability-casting)
_mixer->setParameter(
name,
AudioMixer::TRACK,
AudioMixer::CHANNEL_MASK,
(void *)(uintptr_t)channelMask);

Check failure on line 149 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast (google-readability-casting)

Check failure on line 149 in native/cocos/audio/android/AudioMixerController.cpp

View workflow job for this annotation

GitHub Actions / ClangTidy Android

C-style casts are discouraged; use static_cast (google-readability-casting)

track->setName(name);
_mixer->enable(name);
Expand Down
15 changes: 12 additions & 3 deletions native/cocos/audio/android/AudioMixerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include "audio/android/utils/Errors.h"
#include "base/std/container/vector.h"

#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY
// for openharmony platform, buffer size is 4458 in normal latency mode, 240 in fast latency mode
#define MAX_AUDIO_BUFFER_SIZE 4458
#endif

namespace cc {

class Track;
Expand All @@ -43,13 +48,17 @@ class AudioMixerController {
void *buf;
size_t size;
};

#if CC_PLATFORM == CC_PLATFORM_ANDROID
AudioMixerController(int bufferSizeInFrames, int sampleRate, int channelCount);
bool init();
#elif CC_PLATFORM == CC_PLATFORM_OPENHARMONY
AudioMixerController(int sampleRate, int channelCount);
void updateBufferSize(int bufferSize);
bool init(int bufferSizeInFrames);
#endif

~AudioMixerController();

bool init();

bool addTrack(Track *track);
bool hasPlayingTacks();

Expand Down
Loading
Loading