Skip to content

Add support for avplayer to play files in the application sandbox in openHarmony #18538

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

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions native/cocos/audio/android/AudioPlayerProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ AudioPlayerProvider::AudioFileInfo AudioPlayerProvider::getFileInfo(
FILE *fp = fopen(audioFilePath.c_str(), "rb");
if (fp != nullptr) {
fseek(fp, 0, SEEK_END);
#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY
int fd = fileno(fp);
if (fd > 0) {
assetFd = dup(fd);
Copy link
Contributor

Choose a reason for hiding this comment

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

@lynnllh Will dup the fd cause fd memory leak?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lynnllh Will dup the fd cause fd memory leak?

  1. In fact, when playing large files, AVPlayer is used for playback. After playback ends, the OH_AVPlayer_Release interface is called, which releases the file descriptor (fd) held by it. Therefore, under normal circumstances, playing large files will not cause FD leakage.
  2. However, when playing small files, they are parsed into PCM streams for playback, and at this time, the FD handle is not released, causing leakage.
  3. However, in reality, within the branch where audioFilePath[0]!= '/', fileUtils->getRawFileDescriptor is called. This method calls OH_ResourceManager_GetRawFileDescriptor64 to obtain the FD. This method also has a corresponding method OH_ResourceManager_ReleaseRawFileDescriptor64 that needs to be called to release the FD. However, this call is not made in the code, so FD leakage also occurs when playing small files. I will consider both scenarios together to see how to handle them appropriately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lynnllh Will dup the fd cause fd memory leak?

image
Sorry, I rechecked the code and found that there is a close operation for FD in the destructor of AssetFD, so it should not cause FD leakage.

}
#endif
fileSize = ftell(fp);
fclose(fp);
} else {
Expand All @@ -399,12 +405,6 @@ AudioPlayerProvider::AudioFileInfo AudioPlayerProvider::getFileInfo(
bool AudioPlayerProvider::isSmallFile(const AudioFileInfo &info) { //NOLINT(readability-convert-member-functions-to-static)
//REFINE: If file size is smaller than 100k, we think it's a small file. This value should be set by developers.
auto &audioFileInfo = const_cast<AudioFileInfo &>(info);
#if CC_PLATFORM == CC_PLATFORM_OPENHARMONY
if(audioFileInfo.url[0] == '/') {
// avplayer does not support playing audio files in sandbox path currently.
return true;
}
#endif
size_t judgeCount = sizeof(gAudioFileIndicator) / sizeof(gAudioFileIndicator[0]);
size_t pos = audioFileInfo.url.rfind('.');
ccstd::string extension;
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/audio/openharmony/UrlAudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void UrlAudioPlayer::destroy() {
if (!*_isDestroyed) {
*_isDestroyed = true;
OH_AVErrCode code = OH_AVPlayer_Release(_playObj);
if (code == AV_ERR_OK) {
if (code != AV_ERR_OK) {
ALOGE("UrlAudioPlayer release error, code: %d", code);
}
}
Expand Down
Loading