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 MEDIA_AVSESSION_ADAPTER_IMPL_H 17 #define MEDIA_AVSESSION_ADAPTER_IMPL_H 18 19 #include <unordered_map> 20 21 #include "media_avsession_adapter.h" 22 #include "av_session.h" 23 24 namespace OHOS::NWeb { 25 26 class MediaAVSessionCallbackImpl : public AVSession::AVSessionCallback { 27 public: 28 MediaAVSessionCallbackImpl(std::shared_ptr<MediaAVSessionCallbackAdapter> callbackAdapter); 29 ~MediaAVSessionCallbackImpl() = default; 30 31 void OnPlay() override; 32 void OnPause() override; 33 void OnStop() override; 34 void OnPlayNext() override; 35 void OnPlayPrevious() override; 36 void OnFastForward(int64_t time) override; 37 void OnRewind(int64_t time) override; 38 void OnSeek(int64_t time) override; 39 void OnSetSpeed(double speed) override; 40 void OnSetLoopMode(int32_t loopMode) override; 41 void OnToggleFavorite(const std::string& assertId) override; 42 void OnMediaKeyEvent(const MMI::KeyEvent& keyEvent) override; 43 void OnOutputDeviceChange(const int32_t connectionState, 44 const AVSession::OutputDeviceInfo& outputDeviceInfo) override; 45 void OnCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs) override; 46 void OnSkipToQueueItem(int32_t itemId) override; 47 void OnAVCallAnswer() override; 48 void OnAVCallHangUp() override; 49 void OnAVCallToggleCallMute() override; 50 void OnPlayFromAssetId(int64_t assetId) override; 51 void OnCastDisplayChange(const AVSession::CastDisplayInfo& castDisplayInfo) override; 52 53 private: 54 std::shared_ptr<MediaAVSessionCallbackAdapter> callbackAdapter_; 55 }; 56 57 class MediaAVSessionKey { 58 public: 59 MediaAVSessionKey() = default; 60 ~MediaAVSessionKey() = default; 61 62 void Init(); 63 int32_t GetPID(); 64 AppExecFwk::ElementName GetElement(); 65 void SetType(MediaAVSessionType type); 66 MediaAVSessionType GetType(); 67 std::string ToString(); 68 69 private: 70 int32_t pid_; 71 AppExecFwk::ElementName element_; 72 MediaAVSessionType type_; 73 }; 74 75 class MediaAVSessionAdapterImpl : public MediaAVSessionAdapter { 76 public: 77 MediaAVSessionAdapterImpl(); 78 ~MediaAVSessionAdapterImpl() override; 79 80 bool CreateAVSession(MediaAVSessionType type) override; 81 void DestroyAVSession() override; 82 bool RegistCallback(std::shared_ptr<MediaAVSessionCallbackAdapter> callbackAdapter) override; 83 bool IsActivated() override; 84 bool Activate() override; 85 void DeActivate() override; 86 void SetMetadata(const std::shared_ptr<MediaAVSessionMetadataAdapter> metadata) override; 87 void SetPlaybackState(MediaAVSessionPlayState state) override; 88 void SetPlaybackPosition(const std::shared_ptr<MediaAVSessionPositionAdapter> position) override; 89 90 private: 91 bool UpdateMetaDataCache(const std::shared_ptr<MediaAVSessionMetadataAdapter> metadata); 92 bool UpdateMetaDataCache(const std::shared_ptr<MediaAVSessionPositionAdapter> position); 93 bool UpdatePlaybackStateCache(MediaAVSessionPlayState state); 94 bool UpdatePlaybackStateCache(const std::shared_ptr<MediaAVSessionPositionAdapter> position); 95 void DestroyAndEraseSession(); 96 bool CreateNewSession(const MediaAVSessionType& type); 97 98 std::shared_ptr<MediaAVSessionKey> avSessionKey_; 99 std::shared_ptr<AVSession::AVMetaData> avMetadata_; 100 std::shared_ptr<AVSession::AVPlaybackState> avPlaybackState_; 101 std::shared_ptr<AVSession::AVSession> avSession_; 102 103 static std::unordered_map<std::string, std::shared_ptr<AVSession::AVSession>> avSessionMap; 104 }; 105 } // namespace OHOS::NWeb 106 107 #endif // MEDIA_AVSESSION_ADAPTER_IMPL_H