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 using OnInfoFunc = std::function<void (const int32_t, const Format &)>;
29 namespace AVMetadataHelperState {
30 const std::string STATE_IDLE = "idle";
31 const std::string STATE_PREPARED = "prepared";
32 const std::string STATE_RELEASED = "released";
33 const std::string STATE_CALL_DONE = "callDone";
34 const std::string STATE_ERROR = "error";
35 }
36 
37 namespace AVMetadataHelperEvent {
38 const std::string EVENT_STATE_CHANGE = "stateChange";
39 const std::string EVENT_ERROR = "error";
40 }
41 
42 class AVMetadataHelperNotify {
43 public:
44     AVMetadataHelperNotify() = default;
45     virtual ~AVMetadataHelperNotify() = default;
46     virtual void NotifyState(HelperStates state) = 0;
47 };
48 
49 class AVMetadataHelperCallback : public HelperCallback {
50 public:
51     AVMetadataHelperCallback(napi_env env, AVMetadataHelperNotify *listener);
52     virtual ~AVMetadataHelperCallback();
53     void OnError(int32_t errorCode, const std::string &errorMsg);
54     void OnInfo(HelperOnInfoType type, int32_t extra, const Format &infoBody);
55     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
56     HelperStates GetCurrentState() const;
57     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
58     void ClearCallbackReference();
59     void ClearCallbackReference(const std::string &name);
60     void Start();
61     void Pause();
62     void Release();
63 
64 private:
65     void OnStateChangeCb(const int32_t extra, const Format &infoBody);
66     std::map<HelperOnInfoType, OnInfoFunc> onInfoFuncs_;
67     std::mutex mutex_;
68     napi_env env_ = nullptr;
69     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
70     AVMetadataHelperNotify *listener_ = nullptr;
71     std::atomic<bool> isloaded_ = false;
72     HelperStates state_ = HELPER_IDLE;
73 };
74 } // namespace Media
75 } // namespace OHOS
76 #endif // AV_META_DATA_HELPER_CALLBACK_H