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_IDLE = 10,
38         PLAYBACK_STATE_BUFFERING = 11,
39         PLAYBACK_STATE_MAX = 12,
40     };
41 
42     enum {
43         PLAYBACK_KEY_STATE = 0,
44         PLAYBACK_KEY_SPEED = 1,
45         PLAYBACK_KEY_POSITION = 2,
46         PLAYBACK_KEY_BUFFERED_TIME = 3,
47         PLAYBACK_KEY_LOOP_MODE = 4,
48         PLAYBACK_KEY_IS_FAVORITE = 5,
49         PLAYBACK_KEY_ACTIVE_ITEM_ID = 6,
50         PLAYBACK_KEY_VOLUME = 7,
51         PLAYBACK_KEY_MAX_VOLUME,
52         PLAYBACK_KEY_MUTED,
53         PLAYBACK_KEY_DURATION,
54         PLAYBACK_KEY_VIDEO_WIDTH,
55         PLAYBACK_KEY_VIDEO_HEIGHT,
56         PLAYBACK_KEY_EXTRAS,
57         PLAYBACK_KEY_MAX,
58     };
59 
60     enum {
61         LOOP_MODE_UNDEFINED = -1,
62         LOOP_MODE_SEQUENCE = 0,
63         LOOP_MODE_SINGLE = 1,
64         LOOP_MODE_LIST = 2,
65         LOOP_MODE_SHUFFLE = 3,
66         LOOP_MODE_CUSTOM = 4
67     };
68 
69     struct Position {
70         int64_t elapsedTime_ {};
71         int64_t updateTime_ {};
72     };
73 
74     using PlaybackStateMaskType = std::bitset<PLAYBACK_KEY_MAX>;
75 
76     AVPlaybackState();
77     ~AVPlaybackState() override = default;
78 
79     static AVPlaybackState* Unmarshalling(Parcel& parcel);
80     bool Marshalling(Parcel& parcel) const override;
81 
82     bool IsValid() const;
83 
84     void SetState(int32_t state);
85     int32_t GetState() const;
86 
87     void SetSpeed(double speed);
88     double GetSpeed() const;
89 
90     void SetPosition(const Position& position);
91     Position GetPosition() const;
92 
93     void SetBufferedTime(int64_t time);
94     int64_t GetBufferedTime() const;
95 
96     void SetLoopMode(int32_t mode);
97     int32_t GetLoopMode() const;
98 
99     void SetFavorite(bool isFavorite);
100     bool GetFavorite() const;
101 
102     void SetActiveItemId(int32_t activeItemId);
103     int32_t GetActiveItemId() const;
104 
105     void SetVolume(int32_t volume);
106     int32_t GetVolume() const;
107 
108     void SetMaxVolume(int32_t maxVolume);
109     int32_t GetMaxVolume() const;
110 
111     void SetMuted(bool muted);
112     bool GetMuted() const;
113 
114     void SetDuration(int32_t duration);
115     int32_t GetDuration() const;
116 
117     void SetVideoWidth(int32_t videoWidth);
118     int32_t GetVideoWidth() const;
119 
120     void SetVideoHeight(int32_t videoHeight);
121     int32_t GetVideoHeight() const;
122 
123     void SetExtras(const std::shared_ptr<AAFwk::WantParams>& extras);
124     std::shared_ptr<AAFwk::WantParams> GetExtras() const;
125 
126     PlaybackStateMaskType GetMask() const;
127 
128     bool CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const;
129     bool CopyFrom(const AVPlaybackState& in);
130 
131     const static inline std::vector<int32_t> localCapability {
132         PLAYBACK_KEY_STATE,
133         PLAYBACK_KEY_SPEED,
134         PLAYBACK_KEY_POSITION,
135         PLAYBACK_KEY_BUFFERED_TIME,
136         PLAYBACK_KEY_LOOP_MODE,
137         PLAYBACK_KEY_IS_FAVORITE,
138         PLAYBACK_KEY_ACTIVE_ITEM_ID,
139         PLAYBACK_KEY_VOLUME,
140         PLAYBACK_KEY_MAX_VOLUME,
141         PLAYBACK_KEY_MUTED,
142         PLAYBACK_KEY_DURATION,
143         PLAYBACK_KEY_VIDEO_WIDTH,
144         PLAYBACK_KEY_VIDEO_HEIGHT,
145     };
146 
147 private:
148     PlaybackStateMaskType mask_;
149 
150     int32_t state_ = PLAYBACK_STATE_INITIAL;
151     double speed_ = 1.0;
152     Position position_;
153     int64_t bufferedTime_ {};
154     int32_t loopMode_ = LOOP_MODE_SEQUENCE;
155     bool isFavorite_ {};
156     int32_t activeItemId_ {};
157     int32_t volume_ = 0;
158     int32_t maxVolume_ = 0;
159     bool muted_ {};
160     int32_t duration_ = 0;
161     int32_t videoWidth_ = 0;
162     int32_t videoHeight_ = 0;
163     std::shared_ptr<AAFwk::WantParams> extras_ = nullptr;
164 
165     static void CloneState(const AVPlaybackState& from, AVPlaybackState& to);
166     static void CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to);
167     static void ClonePosition(const AVPlaybackState& from, AVPlaybackState& to);
168     static void CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to);
169     static void CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to);
170     static void CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to);
171     static void CloneActiveItemId(const AVPlaybackState& from, AVPlaybackState& to);
172     static void CloneVolume(const AVPlaybackState& from, AVPlaybackState& to);
173     static void CloneMaxVolume(const AVPlaybackState& from, AVPlaybackState& to);
174     static void CloneMuted(const AVPlaybackState& from, AVPlaybackState& to);
175     static void CloneDuration(const AVPlaybackState& from, AVPlaybackState& to);
176     static void CloneVideoWidth(const AVPlaybackState& from, AVPlaybackState& to);
177     static void CloneVideoHeight(const AVPlaybackState& from, AVPlaybackState& to);
178     static void CloneExtras(const AVPlaybackState& from, AVPlaybackState& to);
179 
180     using CloneActionType = void(*)(const AVPlaybackState& from, AVPlaybackState& to);
181     static inline CloneActionType cloneActions[PLAYBACK_KEY_MAX] = {
182         &AVPlaybackState::CloneState,
183         &AVPlaybackState::CloneSpeed,
184         &AVPlaybackState::ClonePosition,
185         &AVPlaybackState::CloneBufferedTime,
186         &AVPlaybackState::CloneLoopMode,
187         &AVPlaybackState::CloneIsFavorite,
188         &AVPlaybackState::CloneActiveItemId,
189         &AVPlaybackState::CloneVolume,
190         &AVPlaybackState::CloneMaxVolume,
191         &AVPlaybackState::CloneMuted,
192         &AVPlaybackState::CloneDuration,
193         &AVPlaybackState::CloneVideoWidth,
194         &AVPlaybackState::CloneVideoHeight,
195         &AVPlaybackState::CloneExtras,
196     };
197 };
198 } // namespace OHOS::AVSession
199 #endif // OHOS_AVPLAYBACK_STATE_H
200