1 /* 2 * Copyright (C) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef AV_META_DATA_HELPER_CALLBACK_H 17 #define AV_META_DATA_HELPER_CALLBACK_H 18 19 #include <map> 20 #include <mutex> 21 #include "media_errors.h" 22 #include "common_napi.h" 23 #include "event_handler.h" 24 #include "avmetadatahelper.h" 25 26 namespace OHOS { 27 namespace Media { 28 namespace AVMetadataHelperState { 29 const std::string STATE_IDLE = "idle"; 30 const std::string STATE_PREPARED = "prepared"; 31 const std::string STATE_RELEASED = "released"; 32 const std::string STATE_CALL_DONE = "callDone"; 33 const std::string STATE_ERROR = "error"; 34 } 35 36 namespace AVMetadataHelperEvent { 37 const std::string EVENT_STATE_CHANGE = "stateChange"; 38 const std::string EVENT_ERROR = "error"; 39 } 40 41 class AVMetadataHelperNotify { 42 public: 43 AVMetadataHelperNotify() = default; 44 virtual ~AVMetadataHelperNotify() = default; 45 virtual void NotifyState(HelperStates state) = 0; 46 }; 47 48 class AVMetadataHelperCallback : public HelperCallback { 49 public: 50 AVMetadataHelperCallback(napi_env env, AVMetadataHelperNotify *listener); 51 virtual ~AVMetadataHelperCallback(); 52 void OnError(int32_t errorCode, const std::string &errorMsg); 53 void OnInfo(HelperOnInfoType type, int32_t extra, const Format &infoBody); 54 void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg); 55 HelperStates GetCurrentState() const; 56 void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref); 57 void ClearCallbackReference(); 58 void ClearCallbackReference(const std::string &name); 59 void Start(); 60 void Pause(); 61 void Release(); 62 63 private: 64 void OnStateChangeCb(const int32_t extra, const Format &infoBody); 65 66 std::mutex mutex_; 67 napi_env env_ = nullptr; 68 std::map<std::string, std::weak_ptr<AutoRef>> refMap_; 69 AVMetadataHelperNotify *listener_ = nullptr; 70 std::atomic<bool> isloaded_ = false; 71 HelperStates state_ = HELPER_IDLE; 72 std::map<uint32_t, void(AVMetadataHelperCallback::*)(const int32_t extra, const Format &infoBody)> onInfoFuncs_; 73 }; 74 } // namespace Media 75 } // namespace OHOS 76 #endif // AV_META_DATA_HELPER_CALLBACK_H