From 20dbfa1992b344448a4d3e4f68ebf4c85708987c Mon Sep 17 00:00:00 2001 From: zhangwenting15 Date: Tue, 29 Jul 2025 12:50:40 +0800 Subject: [PATCH] change createSoundpool impl Signed-off-by: zhangwenting15 Change-Id: I9fc4357b3a390e10f055c3f13a7e6eeec8e3f82f --- .../taihe/idl/ohos.multimedia.media.taihe | 2 +- frameworks/taihe/idl/soundPool.taihe | 2 +- .../media/include/avimagegenerator_taihe.h | 4 - .../media/include/avmetadataextractor_taihe.h | 8 +- .../media_data_source_callback_taihe.h | 9 +- .../media_source_loading_request_taihe.h | 6 +- .../taihe/media/include/media_taihe_utils.h | 1 - .../media/include/video_recorder_taihe.h | 13 +- .../media/src/avimagegenerator_taihe.cpp | 27 +- .../media/src/avmetadataextractor_taihe.cpp | 6 +- .../media/src/avplayer_callback_taihe.cpp | 18 +- frameworks/taihe/media/src/avplayer_taihe.cpp | 54 ++-- .../media/src/avrecorder_callback_taihe.cpp | 16 +- .../taihe/media/src/avrecorder_taihe.cpp | 2 +- .../media/src/avscreen_capture_taihe.cpp | 2 +- .../media/src/avtranscoder_callback_taihe.cpp | 19 +- .../src/media_data_source_callback_taihe.cpp | 96 ++++++- .../media_source_loading_request_taihe.cpp | 6 +- .../taihe/media/src/media_taihe_utils.cpp | 16 -- .../soundpool/include/sound_pool_taihe.h | 41 +-- .../taihe/soundpool/src/sound_pool_taihe.cpp | 259 ++++++++---------- 21 files changed, 305 insertions(+), 302 deletions(-) diff --git a/frameworks/taihe/idl/ohos.multimedia.media.taihe b/frameworks/taihe/idl/ohos.multimedia.media.taihe index 673c256d8..d1cc46d51 100644 --- a/frameworks/taihe/idl/ohos.multimedia.media.taihe +++ b/frameworks/taihe/idl/ohos.multimedia.media.taihe @@ -56,7 +56,7 @@ function GetScreenCaptureMonitorSync(): Optional; @gen_async("createSoundPool") @gen_promise("createSoundPool") -function CreateSoundPoolSync(maxStreams: i32, audioRendererInfo: @sts_type("audio.AudioRendererInfo") Opaque): Optional; +function CreateSoundPoolSync(maxStreams: i32, audioRendererInfo: audio.AudioRendererInfo): Optional; @gen_async("createVideoRecorder") @gen_promise("createVideoRecorder") diff --git a/frameworks/taihe/idl/soundPool.taihe b/frameworks/taihe/idl/soundPool.taihe index 6c96a10a1..0bef14b68 100644 --- a/frameworks/taihe/idl/soundPool.taihe +++ b/frameworks/taihe/idl/soundPool.taihe @@ -37,7 +37,7 @@ interface SoundPool { @gen_async("load") @gen_promise("load") - LoadWithFdSync(fd: i32, offset: f64, length: f64): i32; + LoadWithFdSync(fd: i32, offset: i64, length: i64): i32; @gen_promise("play") PlaySync(soundID: i32, params: Optional): i32; diff --git a/frameworks/taihe/media/include/avimagegenerator_taihe.h b/frameworks/taihe/media/include/avimagegenerator_taihe.h index a016e65c0..be6513a38 100644 --- a/frameworks/taihe/media/include/avimagegenerator_taihe.h +++ b/frameworks/taihe/media/include/avimagegenerator_taihe.h @@ -40,10 +40,6 @@ private: std::shared_ptr helper_; struct OHOS::Media::AVFileDescriptor fileDescriptor_; OHOS::Media::HelperState state_ { OHOS::Media::HelperState::HELPER_STATE_IDLE }; - std::shared_ptr pixel_ = nullptr; - int64_t timeUs_ = 0; - int32_t option_ = 0; - OHOS::Media::PixelMapParams param_; }; } // namespace ANI::Media #endif // AVIMAGEGENERATOR_TAIHE_H \ No newline at end of file diff --git a/frameworks/taihe/media/include/avmetadataextractor_taihe.h b/frameworks/taihe/media/include/avmetadataextractor_taihe.h index e9829db5c..c88207c0a 100644 --- a/frameworks/taihe/media/include/avmetadataextractor_taihe.h +++ b/frameworks/taihe/media/include/avmetadataextractor_taihe.h @@ -39,16 +39,16 @@ public: optional FetchMetadataSync(); ohos::multimedia::image::image::PixelMap FetchAlbumCoverSync(); void ReleaseSync(); - int32_t GetFrameIndexByTimeSync(double timeUs); - double GetTimeByFrameIndexSync(int32_t index); + int32_t GetFrameIndexByTimeSync(int64_t timeUs); + int64_t GetTimeByFrameIndexSync(int32_t index); private: std::shared_ptr helper_; std::shared_ptr dataSrcCb_ = nullptr; struct OHOS::Media::AVFileDescriptor fileDescriptor_; struct DataSrcDescriptor dataSrcDescriptor_; OHOS::Media::HelperState state_ { OHOS::Media::HelperState::HELPER_STATE_IDLE }; - uint64_t timeStamp_; - uint32_t index_; + uint64_t timeStamp_ = 0; + uint32_t index_ = 0; std::shared_ptr artPicture_ = nullptr; void SetMetadataProperty(std::shared_ptr metadata, AVMetadata &res); bool SetPropertyByType(AVMetadata &res, std::shared_ptr metadata, std::string key); diff --git a/frameworks/taihe/media/include/media_data_source_callback_taihe.h b/frameworks/taihe/media/include/media_data_source_callback_taihe.h index 1b1c70f72..041f80cf1 100644 --- a/frameworks/taihe/media/include/media_data_source_callback_taihe.h +++ b/frameworks/taihe/media/include/media_data_source_callback_taihe.h @@ -16,6 +16,7 @@ #define MEDIA_DATA_SOURCE_CALLBACK_TAIHE_H #include +#include "event_handler.h" #include "media_ani_common.h" #include "media_data_source.h" @@ -42,6 +43,10 @@ struct MediaDataSourceTHCallback { bool isExit_ = false; }; +struct MediaDataSourceTHCallbackWraper { + std::weak_ptr cb_; +}; + class MediaDataSourceCallback : public OHOS::Media::IMediaDataSource, public OHOS::NoCopyable { public: MediaDataSourceCallback(int64_t fileSize); @@ -54,12 +59,14 @@ public: int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr &mem) override; int32_t ReadAt(uint32_t length, const std::shared_ptr &mem) override; + std::shared_ptr mainHandler_ = nullptr; private: + void UvWork(MediaDataSourceTHCallbackWraper *cbWrap); std::mutex mutex_; std::map> refMap_; int64_t size_ = -1; std::shared_ptr cb_ = nullptr; }; } // namespace Media -} // namespace OHOS +} // namespace ANI #endif // MEDIA_DATA_SOURCE_CALLBACK_TAIHE_H diff --git a/frameworks/taihe/media/include/media_source_loading_request_taihe.h b/frameworks/taihe/media/include/media_source_loading_request_taihe.h index b4a79168a..38b550561 100644 --- a/frameworks/taihe/media/include/media_source_loading_request_taihe.h +++ b/frameworks/taihe/media/include/media_source_loading_request_taihe.h @@ -29,9 +29,9 @@ public: MediaSourceLoadingRequestImpl(uint64_t requestId); string GetUrl(); optional> GetHeader(); - optional RespondData(double uuid, double offset, array_view buffer); - void RespondHeader(double uuid, optional_view> header, optional_view redirectUrl); - void FinishLoading(double uuid, LoadingRequestError state); + optional RespondData(int64_t uuid, int64_t offset, array_view buffer); + void RespondHeader(int64_t uuid, optional_view> header, optional_view redirectUrl); + void FinishLoading(int64_t uuid, LoadingRequestError state); static ::ohos::multimedia::media::MediaSourceLoadingRequest CreateLoadingRequest( std::shared_ptr request); private: diff --git a/frameworks/taihe/media/include/media_taihe_utils.h b/frameworks/taihe/media/include/media_taihe_utils.h index 8ec6ecbf2..09c9e4e9d 100644 --- a/frameworks/taihe/media/include/media_taihe_utils.h +++ b/frameworks/taihe/media/include/media_taihe_utils.h @@ -42,7 +42,6 @@ public: template static bool GetEnumKeyByStringValue(::taihe::string_view value, typename EnumTypeString::key_t &key); static ani_object ToBusinessError(ani_env *env, int32_t code, const std::string &message); - static ani_object CreatePixelMap(ani_env *env, OHOS::Media::PixelMap &pixelMap); static ani_string ToAniString(ani_env *env, const std::string &str); static uintptr_t GetUndefined(ani_env* env); static map CreateFormatBuffer(OHOS::Media::Format &format); diff --git a/frameworks/taihe/media/include/video_recorder_taihe.h b/frameworks/taihe/media/include/video_recorder_taihe.h index a71752036..898ea00b4 100644 --- a/frameworks/taihe/media/include/video_recorder_taihe.h +++ b/frameworks/taihe/media/include/video_recorder_taihe.h @@ -99,8 +99,8 @@ private: OHOS::sptr surface_; std::shared_ptr callback_ = nullptr; std::map> refMap_; - int32_t videoSourceID; - int32_t audioSourceID; + int32_t videoSourceID = 0; + int32_t audioSourceID = 0; }; struct VideoRecorderAsyncContext { @@ -115,15 +115,6 @@ struct VideoRecorderAsyncContext { VideoRecorderImpl *taihe = nullptr; }; -class VideoRecorderConfigImpl { -public: - VideoRecorderConfigImpl(); - std::string GetUrl(); - void SetUrl(string_view url); - -private: -}; - } // namespace Media } // namespace ANI #endif // VIDEO_RECORDER_TAIHE_H_ \ No newline at end of file diff --git a/frameworks/taihe/media/src/avimagegenerator_taihe.cpp b/frameworks/taihe/media/src/avimagegenerator_taihe.cpp index 7d01f9aa3..188214d0e 100644 --- a/frameworks/taihe/media/src/avimagegenerator_taihe.cpp +++ b/frameworks/taihe/media/src/avimagegenerator_taihe.cpp @@ -91,26 +91,27 @@ void AVImageGeneratorImpl::SetFdSrc(optional_view fdSrc) set_business_error(OHOS::Media::MSERR_EXT_API9_OPERATE_NOT_PERMIT, "Current state is not runnable, can't fetchFrame."); } - timeUs_ = timeUs; - option_ = options; - if (param.height.has_value() && param.width.has_value() && param.colorFormat.has_value()) { - param_.dstWidth = static_cast(param.width.value()); - param_.dstHeight = static_cast(param.height.value()); - OHOS::Media::PixelFormat colorFormat = OHOS::Media::PixelFormat::RGBA_8888; - int32_t formatVal = 3; - formatVal = static_cast(param.colorFormat.value()); + OHOS::Media::PixelMapParams pixelMapParams; + if (param.height.has_value()) { + pixelMapParams.dstHeight = param.height.value(); + } + if (param.width.has_value()) { + pixelMapParams.dstWidth = param.width.value(); + } + OHOS::Media::PixelFormat colorFormat = OHOS::Media::PixelFormat::RGBA_8888; + if (param.colorFormat.has_value()) { + int32_t formatVal = static_cast(param.colorFormat.value()); colorFormat = static_cast(formatVal); if (colorFormat != OHOS::Media::PixelFormat::RGB_565 && colorFormat != OHOS::Media::PixelFormat::RGB_888 && colorFormat != OHOS::Media::PixelFormat::RGBA_8888) { set_business_error(OHOS::Media::MSERR_INVALID_VAL, "formatVal is invalid"); } - param_.colorFormat = colorFormat; } - - auto pixelMap = helper_->FetchFrameYuv(timeUs_, option_, param_); - pixel_ = pixelMap; - return Image::PixelMapImpl::CreatePixelMap(pixel_); + pixelMapParams.colorFormat = colorFormat; + auto pixelMap = helper_->FetchFrameYuv(timeUs, options.get_value(), pixelMapParams); + MEDIA_LOGI("FetchFrameByTimeSync Out"); + return Image::PixelMapImpl::CreatePixelMap(pixelMap); } void AVImageGeneratorImpl::ReleaseSync() diff --git a/frameworks/taihe/media/src/avmetadataextractor_taihe.cpp b/frameworks/taihe/media/src/avmetadataextractor_taihe.cpp index ebe2c8880..dede6bcac 100644 --- a/frameworks/taihe/media/src/avmetadataextractor_taihe.cpp +++ b/frameworks/taihe/media/src/avmetadataextractor_taihe.cpp @@ -337,7 +337,7 @@ void AVMetadataExtractorImpl::ReleaseSync() helper_->Release(); } -int32_t AVMetadataExtractorImpl::GetFrameIndexByTimeSync(double timeUs) +int32_t AVMetadataExtractorImpl::GetFrameIndexByTimeSync(int64_t timeUs) { OHOS::Media::MediaTrace trace("AVMetadataExtractorImpl::GetFrameIndexByTimeSync"); timeStamp_ = static_cast(timeUs); @@ -356,7 +356,7 @@ int32_t AVMetadataExtractorImpl::GetFrameIndexByTimeSync(double timeUs) return static_cast(index_); } -double AVMetadataExtractorImpl::GetTimeByFrameIndexSync(int32_t index) +int64_t AVMetadataExtractorImpl::GetTimeByFrameIndexSync(int32_t index) { OHOS::Media::MediaTrace trace("AVMetadataExtractorImpl::GetTimeByFrameIndexSync"); if (index < 0) { @@ -376,7 +376,7 @@ double AVMetadataExtractorImpl::GetTimeByFrameIndexSync(int32_t index) set_business_error(OHOS::Media::MSERR_EXT_API9_UNSUPPORT_FORMAT, "Demuxer getTimeByFrameIndex failed."); return -1; } - return static_cast(timeStamp_); + return static_cast(timeStamp_); } } // namespace ANI::Media diff --git a/frameworks/taihe/media/src/avplayer_callback_taihe.cpp b/frameworks/taihe/media/src/avplayer_callback_taihe.cpp index 5c986d304..aa2644a6b 100644 --- a/frameworks/taihe/media/src/avplayer_callback_taihe.cpp +++ b/frameworks/taihe/media/src/avplayer_callback_taihe.cpp @@ -329,7 +329,7 @@ public: seiMessages.push_back(seiMessage); } array_view seiMessageView(seiMessages); - (*cacheCallback)(seiMessageView, optional_view(&playbackPosition)); + (*cacheCallback)(seiMessageView, optional::make(playbackPosition)); } }; struct PropertyInt : public Base { @@ -346,22 +346,6 @@ public: std::reinterpret_pointer_cast>(func); - ohos::multimedia::audio::InterruptType::key_t eventTypeKey; - MediaTaiheUtils::GetEnumKeyByValue(interruptEvent_.eventType, - eventTypeKey); - valueMap["eventType"] = static_cast(eventTypeKey); - - ohos::multimedia::audio::InterruptForceType::key_t forceTypeKey; - MediaTaiheUtils::GetEnumKeyByValue(interruptEvent_.forceType, - forceTypeKey); - - valueMap["forceType"] = static_cast(forceTypeKey); - ohos::multimedia::audio::InterruptHint::key_t hintTypeKey; - MediaTaiheUtils::GetEnumKeyByValue(interruptEvent_.hintType, - hintTypeKey); - - valueMap["hintType"] = static_cast(hintTypeKey); - ohos::multimedia::audio::InterruptEvent interruptEvent = { .eventType = ohos::multimedia::audio::InterruptType(static_cast< ohos::multimedia::audio::InterruptType::key_t>(valueMap["eventType"])), diff --git a/frameworks/taihe/media/src/avplayer_taihe.cpp b/frameworks/taihe/media/src/avplayer_taihe.cpp index 6738efa4d..56b8f0ce6 100644 --- a/frameworks/taihe/media/src/avplayer_taihe.cpp +++ b/frameworks/taihe/media/src/avplayer_taihe.cpp @@ -118,12 +118,10 @@ optional AVPlayerImpl::GetAudioEffectM MEDIA_LOGI("TaiheGetAudioEffectMode In"); ohos::multimedia::audio::AudioEffectMode::key_t audioEffectModeKey; MediaTaiheUtils::GetEnumKeyByValue(audioEffectMode_, audioEffectModeKey); - ::ohos::multimedia::audio::AudioEffectMode changeReason = - static_cast<::ohos::multimedia::audio::AudioEffectMode>(audioEffectModeKey); - ::ohos::multimedia::audio::AudioEffectMode audioEffectMode = { - changeReason, - }; + ohos::multimedia::audio::AudioEffectMode audioEffectMode = ohos::multimedia::audio::AudioEffectMode( + audioEffectModeKey); + MEDIA_LOGI("TaiheGetAudioEffectMode Out"); return optional(std::in_place_t{}, audioEffectMode); } @@ -137,30 +135,32 @@ void AVPlayerImpl::SetAudioEffectMode(optional_view(audioEffectMode.value()); - if (effectMode > OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT || - effectMode < OHOS::AudioStandard::AudioEffectMode::EFFECT_NONE) { - OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, - "invalid audioEffectMode, please check the input audio effect Mode"); - return; - } - if (audioEffectMode_ == effectMode) { - MEDIA_LOGI("Same effectMode parameter"); - return; - } - audioEffectMode_ = effectMode; - auto task = std::make_shared>([this, effectMode]() { - MEDIA_LOGI("TaiheSetAudioEffectMode Task in"); - if (player_ != nullptr) { - Format format; - (void)format.PutIntValue(PlayerKeys::AUDIO_EFFECT_MODE, effectMode); - (void)player_->SetParameter(format); - } - MEDIA_LOGI("TaiheSetAudioEffectMode Task out"); - }); - (void)taskQue_->EnqueueTask(task); + effectMode = static_cast(audioEffectMode.value()); } + if (effectMode > OHOS::AudioStandard::AudioEffectMode::EFFECT_DEFAULT || + effectMode < OHOS::AudioStandard::AudioEffectMode::EFFECT_NONE) { + OnErrorCb(MSERR_EXT_API9_INVALID_PARAMETER, + "invalid audioEffectMode, please check the input audio effect Mode"); + return; + } + if (audioEffectMode_ == effectMode) { + MEDIA_LOGI("Same effectMode parameter"); + return; + } + audioEffectMode_ = effectMode; + auto task = std::make_shared>([this, effectMode]() { + MEDIA_LOGI("TaiheSetAudioEffectMode Task in"); + if (player_ != nullptr) { + Format format; + (void)format.PutIntValue(PlayerKeys::AUDIO_EFFECT_MODE, effectMode); + (void)player_->SetParameter(format); + } + MEDIA_LOGI("TaiheSetAudioEffectMode Task out"); + }); + (void)taskQue_->EnqueueTask(task); + MEDIA_LOGI("TaiheSetAudioEffectMode Out"); return; } diff --git a/frameworks/taihe/media/src/avrecorder_callback_taihe.cpp b/frameworks/taihe/media/src/avrecorder_callback_taihe.cpp index 31ae3c774..27b05c69e 100644 --- a/frameworks/taihe/media/src/avrecorder_callback_taihe.cpp +++ b/frameworks/taihe/media/src/avrecorder_callback_taihe.cpp @@ -137,12 +137,17 @@ void AVRecorderCallback::OnTaihePhotoAssertAvailableCallback(AVRecordTaiheCallba std::reinterpret_pointer_cast>(func); ani_object aniObject = MediaLibraryCommAni::CreatePhotoAssetAni( taihe::get_env(), event->uri, CAMERA_SHOT_TYPE); - uintptr_t photoAssetPtr = reinterpret_cast(&aniObject); + uintptr_t photoAssetPtr = reinterpret_cast(aniObject); (*cacheCallback)(photoAssetPtr); } while (0); delete event; }; - mainHandler_->PostTask(task, "OnPhotoAssetAvailable", 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + bool ret = mainHandler_->PostTask(task, "OnPhotoAssetAvailable", + 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + if (!ret) { + MEDIA_LOGE("Failed to PostTask!"); + delete taiheCb; + } } #endif @@ -165,7 +170,12 @@ void AVRecorderCallback::OnTaiheAudioCaptureChangeCallback(AVRecordTaiheCallback } while (0); delete event; }; - mainHandler_->PostTask(task, "OnAudioCapturerChange", 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + bool ret = mainHandler_->PostTask(task, "OnAudioCapturerChange", + 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + if (!ret) { + MEDIA_LOGE("Failed to PostTask!"); + delete taiheCb; + } } void AVRecorderCallback::SendAudioCaptureChangeCallback(const OHOS::Media::AudioRecorderChangeInfo diff --git a/frameworks/taihe/media/src/avrecorder_taihe.cpp b/frameworks/taihe/media/src/avrecorder_taihe.cpp index 38ebd6c2b..7a6d06219 100644 --- a/frameworks/taihe/media/src/avrecorder_taihe.cpp +++ b/frameworks/taihe/media/src/avrecorder_taihe.cpp @@ -137,7 +137,7 @@ void AVRecorderImpl::PrepareSync(ohos::multimedia::media::AVRecorderConfig const (void)asyncCtx->taihe->taskQue_->EnqueueTask(asyncCtx->task_); } } else { - SetRetInfoError(MSERR_INVALID_OPERATION, opt, ""); + SetRetInfoError(MSERR_INVALID_OPERATION, opt, ""); } if (asyncCtx->task_) { diff --git a/frameworks/taihe/media/src/avscreen_capture_taihe.cpp b/frameworks/taihe/media/src/avscreen_capture_taihe.cpp index ad2526422..529ce7423 100644 --- a/frameworks/taihe/media/src/avscreen_capture_taihe.cpp +++ b/frameworks/taihe/media/src/avscreen_capture_taihe.cpp @@ -462,7 +462,7 @@ void AVScreenCaptureRecorderImpl::SkipPrivacyModeSync(::taihe::array_viewtaihe->taskQue_ != nullptr, "taskQue is nullptr!"); std::vector windowIDsVec; - for (int i = 0; i < windowIDs.size(); i++) { + for (size_t i = 0; i < windowIDs.size(); i++) { int32_t tempValue = windowIDs[i]; if (tempValue >= 0) { windowIDsVec.push_back(static_cast(tempValue)); diff --git a/frameworks/taihe/media/src/avtranscoder_callback_taihe.cpp b/frameworks/taihe/media/src/avtranscoder_callback_taihe.cpp index 14d7bd060..add7565f2 100644 --- a/frameworks/taihe/media/src/avtranscoder_callback_taihe.cpp +++ b/frameworks/taihe/media/src/avtranscoder_callback_taihe.cpp @@ -37,7 +37,14 @@ void AVTransCoderCallback::SendCompleteCallback() CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr"); cb->autoRef = refMap_.at(AVTransCoderEvent::EVENT_COMPLETE); cb->callbackName = AVTransCoderEvent::EVENT_COMPLETE; - return OnTaiheCompleteCallBack(cb); + auto task = [this, cb]() { + this->OnTaiheCompleteCallBack(cb); + }; + bool ret = mainHandler_->PostTask(task, "OnComplete", 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + if (!ret) { + MEDIA_LOGE("Failed to PostTask!"); + delete cb; + } } void AVTransCoderCallback::SendProgressUpdateCallback(int32_t progress) @@ -52,6 +59,16 @@ void AVTransCoderCallback::SendProgressUpdateCallback(int32_t progress) cb->autoRef = refMap_.at(AVTransCoderEvent::EVENT_PROGRESS_UPDATE); cb->callbackName = AVTransCoderEvent::EVENT_PROGRESS_UPDATE; cb->progress = progress; + + auto task = [this, cb]() { + this->OnTaiheProgressUpdateCallback(cb); + }; + bool ret = mainHandler_->PostTask(task, "OnProgressUpdate", + 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + if (!ret) { + MEDIA_LOGE("Failed to PostTask!"); + delete cb; + } } void AVTransCoderCallback::OnError(int32_t errCode, const std::string &errorMsg) diff --git a/frameworks/taihe/media/src/media_data_source_callback_taihe.cpp b/frameworks/taihe/media/src/media_data_source_callback_taihe.cpp index bbb348027..72c44a445 100644 --- a/frameworks/taihe/media/src/media_data_source_callback_taihe.cpp +++ b/frameworks/taihe/media/src/media_data_source_callback_taihe.cpp @@ -21,24 +21,110 @@ namespace { constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "MediaDataSourceCallback"}; } +using DataSrcCallback = taihe::callback, int64_t, taihe::optional_view)>; + namespace ANI { namespace Media { +MediaDataSourceTHCallback::~MediaDataSourceTHCallback() +{ + isExit_ = true; + cond_.notify_all(); + memory_ = nullptr; +} + +void MediaDataSourceTHCallback::WaitResult() +{ + std::unique_lock lock(mutexCond_); + if (!setResult_) { + static constexpr int32_t timeout = 100; + cond_.wait_for(lock, std::chrono::milliseconds(timeout), [this]() { return setResult_ || isExit_; }); + if (!setResult_) { + readSize_ = 0; + if (isExit_) { + MEDIA_LOGW("Reset, ReadAt has been cancel!"); + } else { + MEDIA_LOGW("timeout 100ms!"); + } + } + } + setResult_ = false; +} MediaDataSourceCallback::MediaDataSourceCallback(int64_t fileSize) : size_(fileSize) { + MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); } MediaDataSourceCallback::~MediaDataSourceCallback() { + MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); } int32_t MediaDataSourceCallback::ReadAt(const std::shared_ptr &mem, uint32_t length, int64_t pos) { - (void)length; - (void)mem; - return OHOS::Media::MSERR_OK; + MEDIA_LOGD("MediaDataSourceCallback ReadAt in"); + { + std::lock_guard lock(mutex_); + if (refMap_.find(READAT_CALLBACK_NAME) == refMap_.end()) { + return OHOS::Media::SOURCE_ERROR_IO; + } + cb_ = std::make_shared(READAT_CALLBACK_NAME, mem, length, pos); + CHECK_AND_RETURN_RET_LOG(cb_ != nullptr, 0, "Failed to Create MediaDataSourceTHCallback"); + cb_->callback_ = refMap_.at(READAT_CALLBACK_NAME); + } + + MediaDataSourceTHCallbackWraper *cbWrap = new(std::nothrow) MediaDataSourceTHCallbackWraper(); + CHECK_AND_RETURN_RET_LOG(cbWrap != nullptr, 0, "Failed to new MediaDataSourceTHCallbackWraper"); + cbWrap->cb_ = cb_; + + UvWork(cbWrap); + cb_->WaitResult(); + MEDIA_LOGD("ReadAt out"); + return cb_->readSize_; +} + +void MediaDataSourceCallback::UvWork(MediaDataSourceTHCallbackWraper *cbWrap) +{ + MEDIA_LOGD("begin UvWork"); + auto task = [cbWrap]() { + CHECK_AND_RETURN_LOG(cbWrap != nullptr, "MediaDataSourceTHCallbackWraper is nullptr"); + std::shared_ptr event = cbWrap->cb_.lock(); + do { + CHECK_AND_BREAK_LOG(event != nullptr, "MediaDataSourceTHCallback is nullptr"); + MEDIA_LOGD("length is %{public}u", event->length_); + std::shared_ptr ref = event->callback_.lock(); + CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", event->callbackName_.c_str()); + + auto func = ref->callbackRef_; + CHECK_AND_RETURN_LOG(func != nullptr, "failed to get callback"); + std::shared_ptr cacheCallback = std::reinterpret_pointer_cast(func); + MEDIA_LOGD("call Taihe function"); + + CHECK_AND_BREAK_LOG(event->memory_ != nullptr, "failed to checkout memory"); + uint8_t *base = event->memory_->GetBase(); + int64_t length = static_cast(event->length_); + std::vector buffer(base, base + length); + taihe::array_view bufferView = taihe::array_view(buffer); + if (event->pos_ != -1) { + int64_t pos = event->pos_; + event->readSize_ = (*cacheCallback)(bufferView, length, taihe::optional::make(pos)); + } else { + event->readSize_ = (*cacheCallback)(bufferView, length, taihe::optional(std::nullopt)); + } + + std::unique_lock lock(event->mutexCond_); + event->setResult_ = true; + event->cond_.notify_all(); + } while (0); + delete cbWrap; + }; + bool ret = mainHandler_->PostTask(task, "dataSrc", 0, OHOS::AppExecFwk::EventQueue::Priority::IMMEDIATE, {}); + if (!ret) { + MEDIA_LOGE("Failed to PostTask!"); + delete cbWrap; + } } int32_t MediaDataSourceCallback::ReadAt(int64_t pos, uint32_t length, @@ -68,6 +154,10 @@ void MediaDataSourceCallback::SaveCallbackReference(const std::string &name, std MEDIA_LOGD("Add Callback: %{public}s", name.c_str()); std::lock_guard lock(mutex_); refMap_[name] = ref; + if (mainHandler_ == nullptr) { + std::shared_ptr runner = OHOS::AppExecFwk::EventRunner::GetMainEventRunner(); + mainHandler_ = std::make_shared(runner); + } } int32_t MediaDataSourceCallback::GetCallback(const std::string &name, std::shared_ptr &callback) diff --git a/frameworks/taihe/media/src/media_source_loading_request_taihe.cpp b/frameworks/taihe/media/src/media_source_loading_request_taihe.cpp index 3092b4ada..82b606949 100644 --- a/frameworks/taihe/media/src/media_source_loading_request_taihe.cpp +++ b/frameworks/taihe/media/src/media_source_loading_request_taihe.cpp @@ -58,7 +58,7 @@ optional> MediaSourceLoadingRequestImpl::GetHeader() return optional>(std::in_place_t{}, taiheHeader); } -optional MediaSourceLoadingRequestImpl::RespondData(double uuid, double offset, array_view buffer) +optional MediaSourceLoadingRequestImpl::RespondData(int64_t uuid, int64_t offset, array_view buffer) { MediaTrace trace("MediaSourceLoadingRequestTaihe::respondData"); MEDIA_LOGI("respondData In"); @@ -75,7 +75,7 @@ optional MediaSourceLoadingRequestImpl::RespondData(double uuid, double return optional(std::in_place_t{}, res); } -void MediaSourceLoadingRequestImpl::RespondHeader(double uuid, optional_view> header, +void MediaSourceLoadingRequestImpl::RespondHeader(int64_t uuid, optional_view> header, optional_view redirectUrl) { MediaTrace trace("MediaSourceLoadingRequestTaihe::respondHeader"); @@ -96,7 +96,7 @@ void MediaSourceLoadingRequestImpl::RespondHeader(double uuid, optional_viewFindClass(className, &cls) == ANI_OK, nullptr, - "Failed to find class: %{public}s", className); - ani_method ctorMethod {}; - CHECK_AND_RETURN_RET_LOG(env->Class_FindMethod(cls, "", nullptr, &ctorMethod) == ANI_OK, nullptr, - "Failed to find method: "); - ani_object aniObject {}; - CHECK_AND_RETURN_RET_LOG(env->Object_New(cls, ctorMethod, &aniObject) == ANI_OK, - nullptr, "Call method failed."); - - return aniObject; -} - ani_string MediaTaiheUtils::ToAniString(ani_env *env, const std::string &str) { ani_string aniString; diff --git a/frameworks/taihe/soundpool/include/sound_pool_taihe.h b/frameworks/taihe/soundpool/include/sound_pool_taihe.h index e0215cef2..527b978d5 100644 --- a/frameworks/taihe/soundpool/include/sound_pool_taihe.h +++ b/frameworks/taihe/soundpool/include/sound_pool_taihe.h @@ -33,10 +33,10 @@ struct SoundPoolAsyncContext; class SoundPoolImpl { public: - SoundPoolImpl(int32_t maxStreams, uintptr_t audioRendererInfo); + SoundPoolImpl(int32_t maxStreams, ohos::multimedia::audio::AudioRendererInfo const& audioRendererInfo); int32_t LoadSync(string_view uri); - int32_t LoadWithFdSync(int32_t fd, double offset, double length); + int32_t LoadWithFdSync(int32_t fd, int64_t offset, int64_t length); int32_t PlaySync(int32_t soundID, optional_view params); int32_t PlayWithoutParam(int32_t soundID); int32_t PlayWithParam(int32_t soundID, PlayParameters const& params); @@ -59,15 +59,13 @@ public: void OnPlayFinished(callback_view callback); void OffPlayFinished(); - int32_t ParserPlayOption(const PlayParameters ¶ms); + int32_t ParserPlayOption(const PlayParameters ¶ms, PlayParams &playParameters); void SetCallbackReference(const std::string &callbackName, std::shared_ptr ref); void CancelCallbackReference(const std::string &callbackName); void CancelCallback(std::shared_ptr callback); void SignError(int32_t code, const std::string &message, bool del = true); void SoundPoolAsyncSignError(int32_t errCode, const std::string &operate, const std::string ¶m, const std::string &add = ""); - static void ParseAudioRendererInfo(ani_env *env, ani_object src, - OHOS::AudioStandard::AudioRendererInfo &audioRendererInfo); private: bool errFlag = false; int32_t errCode = 0; @@ -78,39 +76,6 @@ private: std::shared_ptr soundPool_; std::shared_ptr callbackTaihe_; std::map> eventCbMap_; - PlayParams playParameters_; - std::string url_ = ""; - int32_t fd_ = 0; - int64_t offset_ = 0; - int64_t length_ = 0; - int32_t soundId_ = 0; - int32_t streamId_ = 0; - int32_t loop_ = 0; - int32_t priority_ = 0; - float leftVolume_ = 0.0f; - float rightVolume_ = 0.0f; - OHOS::AudioStandard::AudioRendererRate renderRate_ = OHOS::AudioStandard::AudioRendererRate::RENDER_RATE_NORMAL; -}; - -struct SoundPoolAsyncContext { - explicit SoundPoolAsyncContext() {} - ~SoundPoolAsyncContext() = default; - void SoundPoolAsyncSignError(int32_t errCode, const std::string &operate, - const std::string ¶m, const std::string &add = ""); - SoundPoolImpl *taihe = nullptr; - std::shared_ptr soundPool_; - std::shared_ptr callbackTaihe_; - std::string url_ = ""; - int32_t fd_ = 0; - int64_t offset_ = 0; - int64_t length_ = 0; - int32_t soundId_ = 0; - PlayParams playParameters_; - int32_t streamId_ = 0; - int32_t loop_ = 0; - int32_t priority_ = 0; - float leftVolume_ = 0.0f; - float rightVolume_ = 0.0f; }; } // namespace Media } // namespace ANI diff --git a/frameworks/taihe/soundpool/src/sound_pool_taihe.cpp b/frameworks/taihe/soundpool/src/sound_pool_taihe.cpp index 9ff2be4c3..60d6b8771 100644 --- a/frameworks/taihe/soundpool/src/sound_pool_taihe.cpp +++ b/frameworks/taihe/soundpool/src/sound_pool_taihe.cpp @@ -27,15 +27,13 @@ namespace { namespace ANI { namespace Media { -int32_t SoundPoolImpl::maxStreams = 0; -OHOS::AudioStandard::AudioRendererInfo SoundPoolImpl::rendererInfo; -SoundPoolImpl::SoundPoolImpl(int32_t maxStreams, uintptr_t audioRendererInfo) +SoundPoolImpl::SoundPoolImpl(int32_t maxStreams, ohos::multimedia::audio::AudioRendererInfo const& audioRendererInfo) { - ani_env *env = taihe::get_env(); - SoundPoolImpl::ParseAudioRendererInfo(env, reinterpret_cast(audioRendererInfo), - SoundPoolImpl::rendererInfo); - soundPool_ = OHOS::Media::SoundPoolFactory::CreateSoundPool(maxStreams, SoundPoolImpl::rendererInfo); + OHOS::AudioStandard::AudioRendererInfo rendererInfo; + rendererInfo.streamUsage = static_cast(audioRendererInfo.usage.get_value()); + rendererInfo.rendererFlags = audioRendererInfo.rendererFlags; + soundPool_ = OHOS::Media::SoundPoolFactory::CreateSoundPool(maxStreams, rendererInfo); if (soundPool_ == nullptr) { MEDIA_LOGE("failed to CreateSoundPool"); MediaTaiheUtils::ThrowExceptionError("failed to CreateSoundPool"); @@ -46,30 +44,6 @@ SoundPoolImpl::SoundPoolImpl(int32_t maxStreams, uintptr_t audioRendererInfo) } } -void SoundPoolImpl::ParseAudioRendererInfo(ani_env *env, ani_object src, - OHOS::AudioStandard::AudioRendererInfo &audioRendererInfo) -{ - static const char *className = "@ohos.multimedia.audio.audio.AudioRendererInfo;"; - ani_class cls {}; - CHECK_AND_RETURN_LOG(env->FindClass(className, &cls) == ANI_OK, "Failed to find class: %{public}s", className); - ani_method usageGetter {}; - CHECK_AND_RETURN_LOG(env->Class_FindMethod(cls, "usage", nullptr, &usageGetter) == ANI_OK, - "Failed to find method: usage"); - ani_enum_item usage {}; - CHECK_AND_RETURN_LOG(env->Object_CallMethod_Ref(src, usageGetter, - reinterpret_cast(&usage)) == ANI_OK, "usage fail"); - ani_size index {}; - env->EnumItem_GetIndex(usage, &index); - audioRendererInfo.streamUsage = static_cast(index); - ani_method rendererFlagsGetter {}; - CHECK_AND_RETURN_LOG(env->Class_FindMethod(cls, "rendererFlags", nullptr, &rendererFlagsGetter) == ANI_OK, - "Failed to find method: rendererFlags"); - ani_int rendererFlags {}; - CHECK_AND_RETURN_LOG(env->Object_CallMethod_Int(src, rendererFlagsGetter, &rendererFlags) == ANI_OK, - "rendererFlags fail"); - audioRendererInfo.rendererFlags = static_cast(rendererFlags); -} - RetInfo GetRetInfo(int32_t errCode, const std::string &operate, const std::string ¶m, const std::string &add = "") { MEDIA_LOGE("failed to %{public}s, param %{public}s, errCode = %{public}d", operate.c_str(), param.c_str(), errCode); @@ -93,7 +67,8 @@ RetInfo GetRetInfo(int32_t errCode, const std::string &operate, const std::strin return RetInfo(err, message); } -optional CreateSoundPoolSync(double maxStreams, uintptr_t audioRendererInfo) +optional CreateSoundPoolSync(int32_t maxStreams, + ohos::multimedia::audio::AudioRendererInfo const& audioRendererInfo) { auto res = make_holder(maxStreams, audioRendererInfo); if (taihe::has_error()) { @@ -109,99 +84,99 @@ int32_t SoundPoolImpl::PlaySync(int32_t soundID, optional_view p MediaTrace trace("SoundPool::TaihePlay"); MEDIA_LOGI("SoundPoolTaihe::TaihePlay"); - soundId_ = soundID; - CHECK_AND_RETURN_RET(soundId_ > 0, - (SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getplaysoundId", "soundId"), MSERR_INVALID_VAL)); - if (params.has_value()) { - int32_t ret = ParserPlayOption(params.value()); - CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, 0, "failed to SendEvent, ret = %{public}d", ret); - } - CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, 0, "soundPool_ is nullptr!"); - int32_t streamId = soundPool_->Play(soundId_, playParameters_); - if (streamId < 0) { - SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "play sound failed"); + if (soundID <= 0) { + SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getplaysoundId", "soundId"); + return 0; + } else { + PlayParams playParameters; + if (params.has_value()) { + int32_t ret = ParserPlayOption(params.value(), playParameters); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, 0, "failed to SendEvent, ret = %{public}d", ret); + } + CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, 0, "soundPool_ is nullptr!"); + int32_t streamId = soundPool_->Play(soundID, playParameters); + if (streamId < 0) { + SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "play sound failed"); + } + MEDIA_LOGI("The taihe thread of play finishes execution and returns, streamId: %{public}d", streamId); + return streamId; } - MEDIA_LOGI("The taihe thread of play finishes execution and returns, streamId: %{public}d", streamId); - return streamId; } int32_t SoundPoolImpl::PlayWithoutParam(int32_t soundID) { MediaTrace trace("SoundPool::TaihePlayWithoutParam"); MEDIA_LOGI("SoundPoolTaihe::TaihePlayWithoutParam"); - soundId_ = soundID; - CHECK_AND_RETURN_RET(soundId_ > 0, - (SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getplaysoundId", "soundId"), MSERR_INVALID_VAL)); - - if (soundPool_ == nullptr) { - MEDIA_LOGE("soundPool_ is nullptr!"); - return -1; - } - int32_t streamId = soundPool_->Play(soundId_, playParameters_); - if (streamId < 0) { - SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "play sound failed"); + if (soundID <= 0) { + SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getplaysoundId", "soundId"); + return 0; + } else { + if (soundPool_ == nullptr) { + MEDIA_LOGE("soundPool_ is nullptr!"); + return 0; + } + PlayParams playParameters; + int32_t streamId = soundPool_->Play(soundID, playParameters); + if (streamId < 0) { + SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "play sound failed"); + return 0; + } + MEDIA_LOGI("The taihe thread of play finishes execution and returns, streamId: %{public}d", streamId); + return streamId; } - MEDIA_LOGI("The taihe thread of play finishes execution and returns, streamId: %{public}d", streamId); - return streamId; } int32_t SoundPoolImpl::PlayWithParam(int32_t soundID, PlayParameters const& params) { MediaTrace trace("SoundPool::TaihePlayWithParam"); MEDIA_LOGI("SoundPoolTaihe::TaihePlayWithParam"); - soundId_ = soundID; - CHECK_AND_RETURN_RET(soundId_ > 0, - (SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getplaysoundId", "soundId"), MSERR_INVALID_VAL)); - int32_t ret = ParserPlayOption(params); - MEDIA_LOGE("failed to SendEvent, ret = %{public}d", ret); + if (soundID <= 0) { + SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getplaysoundId", "soundId"); + return 0; + } else { + PlayParams playParameters; + int32_t ret = ParserPlayOption(params, playParameters); + MEDIA_LOGE("failed to SendEvent, ret = %{public}d", ret); - if (soundPool_ == nullptr) { - MEDIA_LOGE("soundPool_ is nullptr!"); - return -1; - } - int32_t streamId = soundPool_->Play(soundId_, playParameters_); - if (streamId < 0) { - SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "play sound failed"); + if (soundPool_ == nullptr) { + MEDIA_LOGE("soundPool_ is nullptr!"); + return 0; + } + int32_t streamId = soundPool_->Play(soundID, playParameters); + if (streamId < 0) { + SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "play sound failed"); + return 0; + } + MEDIA_LOGI("The taihe thread of play finishes execution and returns, streamId: %{public}d", streamId); + return streamId; } - MEDIA_LOGI("The taihe thread of play finishes execution and returns, streamId: %{public}d", streamId); - return streamId; -} - -static std::shared_ptr GetAbilityContext(ani_env *env) -{ - return nullptr; } -int32_t SoundPoolImpl::ParserPlayOption(const PlayParameters ¶ms) +int32_t SoundPoolImpl::ParserPlayOption(const PlayParameters ¶ms, PlayParams &playParameters) { if (params.loop.has_value()) { - playParameters_.loop = static_cast(params.loop.value()); + playParameters.loop = static_cast(params.loop.value()); } if (params.rate.has_value()) { - playParameters_.rate = static_cast(params.rate.value()); + playParameters.rate = static_cast(params.rate.value()); } if (params.leftVolume.has_value()) { - playParameters_.leftVolume = static_cast(params.leftVolume.value()); + playParameters.leftVolume = static_cast(params.leftVolume.value()); } if (params.rightVolume.has_value()) { - playParameters_.rightVolume = static_cast(params.rightVolume.value()); + playParameters.rightVolume = static_cast(params.rightVolume.value()); } if (params.priority.has_value()) { - playParameters_.priority = static_cast(params.priority.value()); + playParameters.priority = static_cast(params.priority.value()); } if (params.parallelPlayFlag.has_value()) { - playParameters_.parallelPlayFlag = static_cast(params.parallelPlayFlag.value()); + playParameters.parallelPlayFlag = static_cast(params.parallelPlayFlag.value()); } - std::shared_ptr abilityContext = GetAbilityContext(get_env()); - if (abilityContext != nullptr) { - playParameters_.cacheDir = abilityContext->GetCacheDir(); - } else { - playParameters_.cacheDir = "/data/storage/el2/base/temp"; - } - MEDIA_LOGI("playParameters_ loop:%{public}d, rate:%{public}d, leftVolume:%{public}f, rightvolume:%{public}f," - "priority:%{public}d, parallelPlayFlag:%{public}d", playParameters_.loop, playParameters_.rate, - playParameters_.leftVolume, playParameters_.rightVolume, playParameters_.priority, - playParameters_.parallelPlayFlag); + playParameters.cacheDir = "/data/storage/el2/base/temp"; + MEDIA_LOGI("playParameters loop:%{public}d, rate:%{public}d, leftVolume:%{public}f, rightvolume:%{public}f," + "priority:%{public}d, parallelPlayFlag:%{public}d", playParameters.loop, playParameters.rate, + playParameters.leftVolume, playParameters.rightVolume, playParameters.priority, + playParameters.parallelPlayFlag); return MSERR_OK; } @@ -210,32 +185,28 @@ int32_t SoundPoolImpl::LoadSync(string_view uri) MediaTrace trace("SoundPool::TaiheLoad"); MEDIA_LOGI("SoundPoolNapi::TaiheLoad"); int32_t soundId = 0; - url_ = static_cast(uri); - CHECK_AND_RETURN_RET(url_ != "", + std::string url = static_cast(uri); + CHECK_AND_RETURN_RET(url != "", (SoundPoolAsyncSignError(MSERR_OPEN_FILE_FAILED, "geturl", "url"), soundId)); - soundId = soundPool_->Load(url_); + soundId = soundPool_->Load(url); if (soundId < 0) { SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "load sound failed"); } return soundId; } -int32_t SoundPoolImpl::LoadWithFdSync(int32_t fd, double offset, double length) +int32_t SoundPoolImpl::LoadWithFdSync(int32_t fd, int64_t offset, int64_t length) { MediaTrace trace("SoundPool::TaiheLoad"); MEDIA_LOGI("SoundPoolNapi::TaiheLoad"); int32_t soundId = 0; - fd_ = fd; - CHECK_AND_RETURN_RET(fd_ > 0, + CHECK_AND_RETURN_RET(fd > 0, (SoundPoolAsyncSignError(MSERR_OPEN_FILE_FAILED, "getfd", "fd"), soundId)); - offset_ = static_cast(offset); - CHECK_AND_RETURN_RET(offset_ >= 0, + CHECK_AND_RETURN_RET(offset >= 0, (SoundPoolAsyncSignError(MSERR_OPEN_FILE_FAILED, "getoffset", "offset"), soundId)); - length_ = static_cast(length); - CHECK_AND_RETURN_RET(length_ > 0, + CHECK_AND_RETURN_RET(length > 0, (SoundPoolAsyncSignError(MSERR_OPEN_FILE_FAILED, "getlength", "length"), soundId)); - soundId = - soundPool_->Load(fd, offset_, length_); + soundId = soundPool_->Load(fd, offset, length); if (soundId < 0) { SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "load sound failed"); } @@ -246,30 +217,27 @@ void SoundPoolImpl::StopSync(int32_t streamID) { MediaTrace trace("SoundPool::TaiheStop"); MEDIA_LOGI("SoundPoolNapi::TaiheStop"); - streamId_ = streamID; CHECK_AND_RETURN_LOG(soundPool_ != nullptr, "soundPool_ is nullptr!"); - if (streamId_ <= 0) { + if (streamID <= 0) { SignError(MSERR_EXT_API9_INVALID_PARAMETER, "stop streamId failed, invaild value"); + } else { + int32_t ret = soundPool_->Stop(streamID); + if (ret != MSERR_OK) { + SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "stop streamId failed"); + } + MEDIA_LOGI("The taihe thread of stop finishes execution and returns"); } - int32_t ret = soundPool_->Stop(streamId_); - if (ret != MSERR_OK) { - SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "stop streamId failed"); - } - MEDIA_LOGI("The taihe thread of stop finishes execution and returns"); } void SoundPoolImpl::SetLoopSync(int32_t streamID, int32_t loop) { MediaTrace trace("SoundPool::TaiheSetLoop"); MEDIA_LOGI("SoundPoolNapi::TaiheSetLoop"); - streamId_ = streamID; - if (streamId_ <= 0) { + if (streamID <= 0) { SignError(MSERR_EXT_API9_INVALID_PARAMETER, "SetLoop streamId failed,invaild value"); - } - loop_ = loop; - if (streamId_ > 0) { + } else { CHECK_AND_RETURN_LOG(soundPool_ != nullptr, "soundPool_ is nullptr!"); - int32_t ret = soundPool_->SetLoop(streamId_, loop_); + int32_t ret = soundPool_->SetLoop(streamID, loop); if (ret != MSERR_OK) { SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "setLoop streamId failed"); } @@ -281,17 +249,13 @@ void SoundPoolImpl::SetPrioritySync(int32_t streamID, int32_t priority) { MediaTrace trace("SoundPool::TaiheSetPriority"); MEDIA_LOGI("SoundPoolNapi::TaiheSetPriority"); - streamId_ = streamID; - if (streamId_ <= 0) { + if (streamID <= 0) { SignError(MSERR_EXT_API9_INVALID_PARAMETER, "SetPriority streamId failed"); - } - priority_ = priority; - if (priority_ < 0) { + } else if (priority < 0) { SignError(MSERR_EXT_API9_INVALID_PARAMETER, "SetPriority priority failed"); - } - if (streamId_ > 0) { + } else { CHECK_AND_RETURN_LOG(soundPool_ != nullptr, "soundPool_ is nullptr!"); - int32_t ret = soundPool_->SetPriority(streamId_, priority_); + int32_t ret = soundPool_->SetPriority(streamID, priority); if (ret != MSERR_OK) { SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "SetPriority streamId failed"); } @@ -303,15 +267,13 @@ void SoundPoolImpl::SetRateSync(int32_t streamID, ::ohos::multimedia::audio::Aud { MediaTrace trace("SoundPool::TaiheSetRate"); MEDIA_LOGI("SoundPoolNapi::TaiheSetRate"); - streamId_ = streamID; - if (streamId_ <= 0) { - SignError(MSERR_EXT_API9_INVALID_PARAMETER, "SetPriority streamId failed"); - } - int32_t rendderRate = rate.get_value(); - renderRate_ = static_cast(rendderRate); - if (streamId_ > 0) { + if (streamID <= 0) { + SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getratestreamId", "streamId"); + } else { + OHOS::AudioStandard::AudioRendererRate renderRate = + static_cast(rate.get_value()); CHECK_AND_RETURN_LOG(soundPool_ != nullptr, "soundPool_ is nullptr!"); - int32_t ret = soundPool_->SetRate(streamId_, renderRate_); + int32_t ret = soundPool_->SetRate(streamID, renderRate); if (ret != MSERR_OK) { SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "SetRate streamId failed"); } @@ -323,15 +285,12 @@ void SoundPoolImpl::SetVolumeSync(int32_t streamID, double leftVolume, double ri { MediaTrace trace("SoundPool::TaiheSetVolume"); MEDIA_LOGI("SoundPoolNapi::TaiheSetVolume"); - streamId_ = streamID; - if (streamId_ <= 0) { + if (streamID <= 0) { SoundPoolAsyncSignError(MSERR_INVALID_VAL, "getvolumestreamId", "streamId"); - } - leftVolume_ = static_cast(leftVolume); - rightVolume_ = static_cast(rightVolume); - if (streamId_ > 0) { + } else { CHECK_AND_RETURN_LOG(soundPool_ != nullptr, "soundPool_ is nullptr!"); - int32_t ret = soundPool_->SetVolume(streamId_, leftVolume_, rightVolume_); + int32_t ret = soundPool_->SetVolume(streamID, + static_cast(leftVolume), static_cast(rightVolume)); if (ret != MSERR_OK) { SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "setVolume streamId failed"); } @@ -343,17 +302,17 @@ void SoundPoolImpl::UnloadSync(int32_t soundID) { MediaTrace trace("SoundPool::TaiheUnload"); MEDIA_LOGI("SoundPoolNapi::TaiheUnload"); - soundId_ = soundID; CHECK_AND_RETURN_LOG(soundPool_ != nullptr, "soundPool_ is nullptr!"); - if (soundId_ <= 0) { + if (soundID <= 0) { SignError(MSERR_EXT_API9_IO, "unLoad failed,inavild value"); + } else { + int32_t ret = soundPool_->Unload(soundID); + if (ret != MSERR_OK) { + SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "unLoad soundID failed"); + } + MEDIA_LOGI("The taihe thread of Unload finishes execution and returns, soundID: %{public}d", + soundID); } - int32_t ret = soundPool_->Unload(soundId_); - if (ret != MSERR_OK) { - SignError(MSERR_EXT_API9_OPERATE_NOT_PERMIT, "unLoad soundID failed"); - } - MEDIA_LOGI("The taihe thread of Unload finishes execution and returns, soundID: %{public}d", - soundId_); } void SoundPoolImpl::ReleaseSync() -- Gitee