1 /*
2  * Copyright (C) 2024 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 #ifndef AVTRANSCODER_CALLBACK_H
16 #define AVTRANSCODER_CALLBACK_H
17 
18 #include "avtranscoder_napi.h"
19 #include "transcoder.h"
20 #include "av_common.h"
21 #include "media_errors.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "common_napi.h"
25 #include <uv.h>
26 #include "scope_guard.h"
27 
28 namespace OHOS {
29 namespace Media {
30 class AVTransCoderCallback : public TransCoderCallback {
31 public:
32     explicit AVTransCoderCallback(napi_env env);
33     virtual ~AVTransCoderCallback();
34 
35     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
36     void CancelCallbackReference(const std::string &name);
37     void ClearCallbackReference();
38     void SendErrorCallback(MediaServiceExtErrCodeAPI9 errCode, const std::string &msg);
39     void SendStateCallback(const std::string &state, const StateChangeReason &reason);
40     void SendCompleteCallback();
41     void SendProgressUpdateCallback(int32_t progress);
42     std::string GetState();
43 
44 protected:
45     void OnError(int32_t errCode, const std::string &errorMsg) override;
46     void OnInfo(int32_t type, int32_t extra) override;
47 
48 private:
49     struct AVTransCoderJsCallback {
50         std::weak_ptr<AutoRef> autoRef;
51         std::string callbackName = "unknown";
52         std::string errorMsg = "unknown";
53         int32_t errorCode = MSERR_EXT_UNKNOWN;
54         int32_t reason = 1;
55         std::string state = "unknown";
56         int32_t progress;
57     };
58     void OnJsErrorCallBack(AVTransCoderJsCallback *jsCb) const;
59     void OnJsCompleteCallBack(AVTransCoderJsCallback *jsCb) const;
60     void OnJsProgressUpdateCallback(AVTransCoderJsCallback *jsCb) const;
61     int32_t QueueErrorWork(uv_loop_s *loop, uv_work_t *work) const;
62     int32_t QueueCompleteWork(uv_loop_s *loop, uv_work_t *work) const;
63     int32_t QueueProgressUpdateWork(uv_loop_s *loop, uv_work_t *work) const;
64     napi_env env_ = nullptr;
65     std::mutex mutex_;
66     std::string currentState_ = AVTransCoderState::STATE_IDLE;
67     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
68 };
69 } // namespace Media
70 } // namespace OHOS
71 #endif // AVRECORDER_CALLBACK_H