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 
16 #ifndef NATIVE_MEDIA_PLAYER_IMPL_H
17 #define NATIVE_MEDIA_PLAYER_IMPL_H
18 
19 #include "napi/native_api.h"
20 #include "nweb_native_media_player.h"
21 
22 namespace OHOS::NWeb {
23 
24 class NWebNativeMediaPlayerBridgeImpl : public NWebNativeMediaPlayerBridge {
25 public:
26     NWebNativeMediaPlayerBridgeImpl(int32_t nwebId, napi_env env, napi_value value);
27     ~NWebNativeMediaPlayerBridgeImpl() = default;
28 
29     void UpdateRect(double x, double y, double width, double height) override;
30 
31     void Play() override;
32 
33     void Pause() override;
34 
35     void Seek(double time) override;
36 
37     void SetVolume(double volume) override;
38 
39     void SetMuted(bool isMuted) override;
40 
41     void SetPlaybackRate(double playbackRate) override;
42 
43     void Release() override;
44 
45     void EnterFullScreen() override;
46 
47     void ExitFullScreen() override;
48 
49     void ResumeMediaPlayer() override;
50 
51     void SuspendMediaPlayer(SuspendType type) override;
52 
53 private:
54     int32_t nwebId_ = -1;
55     napi_env env_ = nullptr;
56     napi_value value_ = nullptr;
57 };
58 
59 class NapiNativeMediaPlayerHandlerImpl {
60 public:
61     NapiNativeMediaPlayerHandlerImpl(int32_t nwebId, std::shared_ptr<NWebNativeMediaPlayerHandler> handler);
62     ~NapiNativeMediaPlayerHandlerImpl() = default;
63 
64     void HandleStatusChanged(PlaybackStatus status);
65 
66     void HandleVolumeChanged(double volume);
67 
68     void HandleMutedChanged(bool isMuted);
69 
70     void HandlePlaybackRateChanged(double playbackRate);
71 
72     void HandleDurationChanged(double duration);
73 
74     void HandleTimeUpdate(double playTime);
75 
76     void HandleBufferedEndTimeChanged(double bufferedEndTime);
77 
78     void HandleEnded();
79 
80     void HandleNetworkStateChanged(NetworkState state);
81 
82     void HandleReadyStateChanged(ReadyState state);
83 
84     void HandleFullScreenChanged(bool isFullScreen);
85 
86     void HandleSeeking();
87 
88     void HandleSeekFinished();
89 
90     void HandleError(MediaError error, const std::string& message);
91 
92     void HandleVideoSizeChanged(double width, double height);
93 
94 private:
95     int32_t nwebId_ = -1;
96     std::shared_ptr<NWebNativeMediaPlayerHandler> handler_ = nullptr;
97 };
98 
99 class NWebCreateNativeMediaPlayerCallbackImpl : public NWebCreateNativeMediaPlayerCallback {
100 public:
101     explicit NWebCreateNativeMediaPlayerCallbackImpl(int32_t nwebId, napi_env env, napi_ref callback);
102     ~NWebCreateNativeMediaPlayerCallbackImpl();
103 
104     std::shared_ptr<NWebNativeMediaPlayerBridge> OnCreate(
105         std::shared_ptr<NWebNativeMediaPlayerHandler> handler, std::shared_ptr<NWebMediaInfo> mediaInfo) override;
106 
107 private:
108     void ConstructRect(napi_value* value, std::shared_ptr<NWebNativeMediaPlayerSurfaceInfo> surfaceInfo);
109 
110     void ConstructHandler(napi_value* value, std::shared_ptr<NWebNativeMediaPlayerHandler> handler);
111 
112     void ConstructControls(napi_value* value, const std::vector<std::string>& controls);
113 
114     void ConstructHeaders(napi_value* value, const std::map<std::string, std::string>& headers);
115 
116     void ConstructAttributes(napi_value* value, const std::map<std::string, std::string>& attributes);
117 
118     void ConstructMediaInfo(napi_value* value, std::shared_ptr<NWebMediaInfo> mediaInfo);
119 
120     void ConstructSourceInfos(napi_value* value, const std::vector<std::shared_ptr<NWebMediaSourceInfo>>& sourceInfos);
121 
122     void ConstructSurfaceInfo(napi_value* value, std::shared_ptr<NWebNativeMediaPlayerSurfaceInfo> surfaceInfo);
123 
124 private:
125     int32_t nwebId_ = -1;
126     napi_env env_ = nullptr;
127     napi_ref callback_ = nullptr;
128 };
129 
130 } // namespace OHOS::NWeb
131 
132 #endif // NATIVE_MEDIA_PLAYER_IMPL_H
133