1 /* 2 * Copyright (c) 2022 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 OHOS_AVPLAYBACK_STATE_H 17 #define OHOS_AVPLAYBACK_STATE_H 18 19 #include <bitset> 20 #include <parcel.h> 21 #include "want_agent.h" 22 23 namespace OHOS::AVSession { 24 class AVPlaybackState : public Parcelable { 25 public: 26 enum { 27 PLAYBACK_STATE_INITIAL = 0, 28 PLAYBACK_STATE_PREPARE = 1, 29 PLAYBACK_STATE_PLAY = 2, 30 PLAYBACK_STATE_PAUSE = 3, 31 PLAYBACK_STATE_FAST_FORWARD = 4, 32 PLAYBACK_STATE_REWIND = 5, 33 PLAYBACK_STATE_STOP = 6, 34 PLAYBACK_STATE_COMPLETED = 7, 35 PLAYBACK_STATE_RELEASED = 8, 36 PLAYBACK_STATE_ERROR = 9, 37 PLAYBACK_STATE_MAX = 10, 38 }; 39 40 enum { 41 PLAYBACK_KEY_STATE = 0, 42 PLAYBACK_KEY_SPEED = 1, 43 PLAYBACK_KEY_POSITION = 2, 44 PLAYBACK_KEY_BUFFERED_TIME = 3, 45 PLAYBACK_KEY_LOOP_MODE = 4, 46 PLAYBACK_KEY_IS_FAVORITE = 5, 47 PLAYBACK_KEY_ACTIVE_ITEM_ID = 6, 48 PLAYBACK_KEY_VOLUME = 7, 49 PLAYBACK_KEY_EXTRAS = 8, 50 PLAYBACK_KEY_MAX = 9, 51 }; 52 53 enum { 54 LOOP_MODE_SEQUENCE = 0, 55 LOOP_MODE_SINGLE = 1, 56 LOOP_MODE_LIST = 2, 57 LOOP_MODE_SHUFFLE = 3 58 }; 59 60 struct Position { 61 int64_t elapsedTime_ {}; 62 int64_t updateTime_ {}; 63 }; 64 65 using PlaybackStateMaskType = std::bitset<PLAYBACK_KEY_MAX>; 66 67 AVPlaybackState(); 68 ~AVPlaybackState() override = default; 69 70 static AVPlaybackState* Unmarshalling(Parcel& parcel); 71 bool Marshalling(Parcel& parcel) const override; 72 73 bool IsValid() const; 74 75 void SetState(int32_t state); 76 int32_t GetState() const; 77 78 void SetSpeed(double speed); 79 double GetSpeed() const; 80 81 void SetPosition(const Position& position); 82 Position GetPosition() const; 83 84 void SetBufferedTime(int64_t time); 85 int64_t GetBufferedTime() const; 86 87 void SetLoopMode(int32_t mode); 88 int32_t GetLoopMode() const; 89 90 void SetFavorite(bool isFavorite); 91 bool GetFavorite() const; 92 93 void SetActiveItemId(int32_t activeItemId); 94 int32_t GetActiveItemId() const; 95 96 void SetVolume(int32_t volume); 97 int32_t GetVolume() const; 98 99 void SetExtras(const std::shared_ptr<AAFwk::WantParams>& extras); 100 std::shared_ptr<AAFwk::WantParams> GetExtras() const; 101 102 PlaybackStateMaskType GetMask() const; 103 104 bool CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const; 105 bool CopyFrom(const AVPlaybackState& in); 106 107 const static inline std::vector<int32_t> localCapability { 108 PLAYBACK_KEY_STATE, 109 PLAYBACK_KEY_SPEED, 110 PLAYBACK_KEY_POSITION, 111 PLAYBACK_KEY_BUFFERED_TIME, 112 PLAYBACK_KEY_LOOP_MODE, 113 PLAYBACK_KEY_IS_FAVORITE, 114 PLAYBACK_KEY_ACTIVE_ITEM_ID, 115 PLAYBACK_KEY_VOLUME, 116 }; 117 118 private: 119 PlaybackStateMaskType mask_; 120 121 int32_t state_ = PLAYBACK_STATE_INITIAL; 122 double speed_ = 1.0; 123 Position position_; 124 int64_t bufferedTime_ {}; 125 int32_t loopMode_ = LOOP_MODE_SEQUENCE; 126 bool isFavorite_ {}; 127 int32_t activeItemId_ {}; 128 int32_t volume_ = 0; 129 std::shared_ptr<AAFwk::WantParams> extras_ = nullptr; 130 131 static void CloneState(const AVPlaybackState& from, AVPlaybackState& to); 132 static void CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to); 133 static void ClonePosition(const AVPlaybackState& from, AVPlaybackState& to); 134 static void CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to); 135 static void CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to); 136 static void CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to); 137 static void CloneActiveItemId(const AVPlaybackState& from, AVPlaybackState& to); 138 static void CloneVolume(const AVPlaybackState& from, AVPlaybackState& to); 139 static void CloneExtras(const AVPlaybackState& from, AVPlaybackState& to); 140 141 using CloneActionType = void(*)(const AVPlaybackState& from, AVPlaybackState& to); 142 static inline CloneActionType cloneActions[PLAYBACK_KEY_MAX] = { 143 &AVPlaybackState::CloneState, 144 &AVPlaybackState::CloneSpeed, 145 &AVPlaybackState::ClonePosition, 146 &AVPlaybackState::CloneBufferedTime, 147 &AVPlaybackState::CloneLoopMode, 148 &AVPlaybackState::CloneIsFavorite, 149 &AVPlaybackState::CloneActiveItemId, 150 &AVPlaybackState::CloneVolume, 151 &AVPlaybackState::CloneExtras, 152 }; 153 }; 154 } // namespace OHOS::AVSession 155 #endif // OHOS_AVPLAYBACK_STATE_H