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 22 namespace OHOS::AVSession { 23 class AVPlaybackState : public Parcelable { 24 public: 25 enum { 26 PLAYBACK_STATE_INITIAL = 0, 27 PLAYBACK_STATE_PREPARING = 1, 28 PLAYBACK_STATE_PLAYING = 2, 29 PLAYBACK_STATE_PAUSED = 3, 30 PLAYBACK_STATE_FAST_FORWARD = 4, 31 PLAYBACK_STATE_REWIND = 5, 32 PLAYBACK_STATE_STOP = 6, 33 PLAYBACK_STATE_MAX = 7 34 }; 35 36 enum { 37 PLAYBACK_KEY_STATE = 0, 38 PLAYBACK_KEY_SPEED = 1, 39 PLAYBACK_KEY_POSITION = 2, 40 PLAYBACK_KEY_BUFFERED_TIME = 3, 41 PLAYBACK_KEY_LOOP_MODE = 4, 42 PLAYBACK_KEY_IS_FAVORITE = 5, 43 PLAYBACK_KEY_MAX = 6 44 }; 45 46 enum { 47 LOOP_MODE_SEQUENCE = 0, 48 LOOP_MODE_SINGLE = 1, 49 LOOP_MODE_LIST = 2, 50 LOOP_MODE_SHUFFLE = 3 51 }; 52 53 struct Position { 54 int64_t elapsedTime_ {}; 55 int64_t updateTime_ {}; 56 }; 57 58 using PlaybackStateMaskType = std::bitset<PLAYBACK_KEY_MAX>; 59 60 AVPlaybackState(); 61 ~AVPlaybackState() override = default; 62 63 static AVPlaybackState* Unmarshalling(Parcel& parcel); 64 bool Marshalling(Parcel& parcel) const override; 65 66 bool IsValid() const; 67 68 void SetState(int32_t state); 69 int32_t GetState() const; 70 71 void SetSpeed(double speed); 72 double GetSpeed() const; 73 74 void SetPosition(const Position& position); 75 Position GetPosition() const; 76 77 void SetBufferedTime(int64_t time); 78 int64_t GetBufferedTime() const; 79 80 void SetLoopMode(int32_t mode); 81 int32_t GetLoopMode() const; 82 83 void SetFavorite(bool isFavorite); 84 bool GetFavorite() const; 85 86 PlaybackStateMaskType GetMask() const; 87 88 bool CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const; 89 bool CopyFrom(const AVPlaybackState& in); 90 91 const static inline std::vector<int32_t> localCapability { 92 PLAYBACK_KEY_STATE, 93 PLAYBACK_KEY_SPEED, 94 PLAYBACK_KEY_POSITION, 95 PLAYBACK_KEY_BUFFERED_TIME, 96 PLAYBACK_KEY_LOOP_MODE, 97 PLAYBACK_KEY_IS_FAVORITE, 98 }; 99 100 private: 101 PlaybackStateMaskType mask_; 102 103 int32_t state_ = PLAYBACK_STATE_INITIAL; 104 double speed_ = 1.0; 105 Position position_; 106 int64_t bufferedTime_ {}; 107 int32_t loopMode_ = LOOP_MODE_SEQUENCE; 108 bool isFavorite_ {}; 109 110 static void CloneState(const AVPlaybackState& from, AVPlaybackState& to); 111 static void CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to); 112 static void ClonePosition(const AVPlaybackState& from, AVPlaybackState& to); 113 static void CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to); 114 static void CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to); 115 static void CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to); 116 117 using CloneActionType = void(*)(const AVPlaybackState& from, AVPlaybackState& to); 118 static inline CloneActionType cloneActions[PLAYBACK_KEY_MAX] = { 119 [PLAYBACK_KEY_STATE] = &AVPlaybackState::CloneState, 120 [PLAYBACK_KEY_SPEED] = &AVPlaybackState::CloneSpeed, 121 [PLAYBACK_KEY_POSITION] = &AVPlaybackState::ClonePosition, 122 [PLAYBACK_KEY_BUFFERED_TIME] = &AVPlaybackState::CloneBufferedTime, 123 [PLAYBACK_KEY_LOOP_MODE] = &AVPlaybackState::CloneLoopMode, 124 [PLAYBACK_KEY_IS_FAVORITE] = &AVPlaybackState::CloneIsFavorite, 125 }; 126 }; 127 } // namespace OHOS::AVSession 128 #endif // OHOS_AVPLAYBACK_STATE_H