1 /*
2  * Copyright (C) 2021 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 PLAYER_CALLBACK_NAPI_H
17 #define PLAYER_CALLBACK_NAPI_H
18 
19 #include "audio_player_napi.h"
20 #include "player.h"
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 #include "common_napi.h"
24 
25 namespace OHOS {
26 namespace Media {
27 struct AudioPlayerAsyncContext : public MediaAsyncContext {
AudioPlayerAsyncContextAudioPlayerAsyncContext28     explicit AudioPlayerAsyncContext(napi_env env) : MediaAsyncContext(env) {}
29     ~AudioPlayerAsyncContext() = default;
30     AudioPlayerNapi *jsPlayer = nullptr;
31 };
32 
33 class PlayerCallbackNapi : public PlayerCallback {
34 public:
35     explicit PlayerCallbackNapi(napi_env env);
36     virtual ~PlayerCallbackNapi();
37     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
38     void ClearCallbackReference();
39     void SendErrorCallback(MediaServiceExtErrCode errCode, const std::string &info = "error");
40     virtual PlayerStates GetCurrentState() const;
41     void OnError(int32_t errorCode, const std::string &errorMsg) override;
42     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
43 
44 protected:
45     struct PlayerJsCallback {
46         std::weak_ptr<AutoRef> callback;
47         std::string callbackName = "unknown";
48         std::string errorMsg = "unknown";
49         MediaServiceExtErrCode errorCode = MSERR_EXT_UNKNOWN;
50         std::vector<int32_t> valueVec;
51         OHOS::AudioStandard::InterruptEvent interruptEvent;
52     };
53     virtual void OnStateChangeCb(PlayerStates state);
54     void OnJsCallBack(PlayerJsCallback *jsCb) const;
55     void OnJsCallBackError(PlayerJsCallback *jsCb) const;
56     void OnJsCallBackInt(PlayerJsCallback *jsCb) const;
57     void OnJsCallBackIntVec(PlayerJsCallback *jsCb) const;
58     void OnJsCallBackIntArray(PlayerJsCallback *jsCb) const;
59     void OnJsCallBackInterrupt(PlayerJsCallback *jsCb) const;
60     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
61 
62 private:
63     void OnSeekDoneCb(int32_t currentPositon) const;
64     void OnEosCb(int32_t isLooping) const;
65     void OnPositionUpdateCb(int32_t position) const;
66     void OnMessageCb(int32_t type) const;
67     void OnVolumeChangeCb();
68     void OnBufferingUpdateCb(const Format &infoBody) const;
69     void OnAudioInterruptCb(const Format &infoBody) const;
70     std::mutex mutex_;
71     napi_env env_ = nullptr;
72     PlayerStates currentState_ = PLAYER_IDLE;
73 };
74 } // namespace Media
75 } // namespace OHOS
76 #endif // PLAYER_CALLBACK_NAPI_H
77