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_FFI_H 17 #define NATIVE_MEDIA_PLAYER_IMPL_FFI_H 18 19 #include <cstdint> 20 #include <map> 21 #include "ffi_remote_data.h" 22 #include "web_errors.h" 23 #include "webview_javascript_result_callback.h" 24 #include "nweb.h" 25 #include "nweb_helper.h" 26 #include "nweb_native_media_player.h" 27 28 namespace OHOS::Webview { 29 class NativeMediaPlayerHandlerImpl : public OHOS::FFI::FFIData { 30 DECL_TYPE(NativeMediaPlayerHandlerImpl, OHOS::FFI::FFIData) 31 public: 32 NativeMediaPlayerHandlerImpl()=default; 33 NativeMediaPlayerHandlerImpl(int32_t nwebId, std::shared_ptr<NWeb::NWebNativeMediaPlayerHandler> handler); 34 ~NativeMediaPlayerHandlerImpl() = default; 35 void HandleStatusChanged(NWeb::PlaybackStatus status); 36 void HandleVideoSizeChanged(double width, double height); 37 void HandleError(NWeb::MediaError error, const char* errorMessage); 38 void HandleSeekFinished(); 39 void HandleSeeking(); 40 void HandleFullScreenChanged(bool fullscreen); 41 void HandleReadyStateChanged(NWeb::ReadyState state); 42 void HandleNetworkStateChanged(NWeb::NetworkState state); 43 void HandleEnded(); 44 void HandleBufferedEndTimeChanged(double bufferedEndTime); 45 void HandleTimeUpdate(double playTime); 46 void HandleDurationChanged(double duration); 47 void HandlePlaybackRateChanged(double playbackRate); 48 void HandleMutedChanged(bool muted); 49 void HandleVolumeChanged(double volume); 50 int32_t nwebId_ = -1; 51 std::shared_ptr<NWeb::NWebNativeMediaPlayerHandler> handler_ = nullptr; 52 }; 53 54 class RemoteMediaPlayer : public OHOS::FFI::RemoteData { 55 CJ_REMOTE_CLASS(RemoteMediaPlayer) 56 public: 57 void UpdateRect(int64_t id, double x, double y, double width, double height); 58 void Play(int64_t id); 59 void Pause(int64_t id); 60 void Seek(int64_t id, double time); 61 void SetVolume(int64_t id, double volume); 62 void SetMuted(int64_t id, bool isMuted); 63 void SetPlaybackRate(int64_t id, double playbackRate); 64 void Release(int64_t id); 65 void EnterFullScreen(int64_t id); 66 void ExitFullScreen(int64_t id); 67 void ResumeMediaPlayer(int64_t id); 68 void SuspendMediaPlayer(int64_t id, NWeb::SuspendType type); 69 }; 70 71 class NWebNativeMediaPlayerBridgeImpl : public NWeb::NWebNativeMediaPlayerBridge, public OHOS::FFI::FFIData { 72 DECL_TYPE(NWebNativeMediaPlayerBridgeImpl, OHOS::FFI::FFIData) 73 public: 74 NWebNativeMediaPlayerBridgeImpl(int64_t nwebId, sptr<RemoteMediaPlayer> remoteMediaPlayer); 75 ~NWebNativeMediaPlayerBridgeImpl() override = default; 76 void UpdateRect(double x, double y, double width, double height) override; 77 void Play() override; 78 void Pause() override; 79 void Seek(double time) override; 80 void SetVolume(double volume) override; 81 void SetMuted(bool isMuted) override; 82 void SetPlaybackRate(double playbackRate) override; 83 void Release() override; 84 void EnterFullScreen() override; 85 void ExitFullScreen() override; 86 void ResumeMediaPlayer() override; 87 void SuspendMediaPlayer(NWeb::SuspendType type) override; 88 private: 89 int64_t nwebId_; 90 sptr<RemoteMediaPlayer> remoteMediaPlayer_; 91 }; 92 93 class NWebCreateNativeMediaPlayerCallbackImpl : 94 public NWeb::NWebCreateNativeMediaPlayerCallback, public OHOS::FFI::FFIData { 95 DECL_TYPE(NWebCreateNativeMediaPlayerCallbackImpl, OHOS::FFI::FFIData) 96 public: 97 explicit NWebCreateNativeMediaPlayerCallbackImpl(int32_t nwebId, 98 std::function<int64_t(int64_t, CMediaInfo)> callback); 99 ~NWebCreateNativeMediaPlayerCallbackImpl() = default; 100 101 std::shared_ptr<NWeb::NWebNativeMediaPlayerBridge> OnCreate( 102 std::shared_ptr<NWeb::NWebNativeMediaPlayerHandler> handler, 103 std::shared_ptr<NWeb::NWebMediaInfo> mediaInfo) override; 104 CNativeMediaPlayerSurfaceInfo ConstructSurfaceInfo( 105 std::shared_ptr<NWeb::NWebNativeMediaPlayerSurfaceInfo> surfaceInfo_); 106 CArrMediaSourceInfo ConstructMediaSrcList( 107 std::vector<std::shared_ptr<NWeb::NWebMediaSourceInfo>> mediaSrcList_); 108 CArrString ConstructControlList(std::vector<std::string> controlList_); 109 ArrMapItem ConstructMap(std::map<std::string, std::string> headers_); 110 111 private: 112 int32_t nwebId_ = -1; 113 std::function<int64_t(int64_t, CMediaInfo)> callback_ = nullptr; 114 }; 115 } 116 #endif // NATIVE_MEDIA_PLAYER_IMPL_FFI_H