• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include "avplayback_state.h"
17 #include "avsession_log.h"
18 
19 namespace OHOS::AVSession {
AVPlaybackState()20 AVPlaybackState::AVPlaybackState()
21 {
22 }
23 
Marshalling(Parcel & parcel) const24 bool AVPlaybackState::Marshalling(Parcel& parcel) const
25 {
26     return parcel.WriteString(mask_.to_string()) &&
27         parcel.WriteInt32(state_) &&
28         parcel.WriteDouble(speed_) &&
29         parcel.WriteInt64(position_.elapsedTime_) &&
30         parcel.WriteInt64(position_.updateTime_) &&
31         parcel.WriteInt64(bufferedTime_) &&
32         parcel.WriteInt32(loopMode_) &&
33         parcel.WriteBool(isFavorite_) &&
34         parcel.WriteInt32(activeItemId_) &&
35         parcel.WriteInt32(volume_) &&
36         parcel.WriteParcelable(extras_.get());
37 }
38 
Unmarshalling(Parcel & parcel)39 AVPlaybackState *AVPlaybackState::Unmarshalling(Parcel& parcel)
40 {
41     std::string mask;
42     CHECK_AND_RETURN_RET_LOG(parcel.ReadString(mask) && mask.length() == PLAYBACK_KEY_MAX, nullptr, "mask not valid");
43     CHECK_AND_RETURN_RET_LOG(mask.find_first_not_of("01") == std::string::npos, nullptr, "mask string not 0 or 1");
44 
45     auto *result = new (std::nothrow) AVPlaybackState();
46     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVPlaybackState failed");
47     result->mask_ = PlaybackStateMaskType(mask);
48     if (!parcel.ReadInt32(result->state_) ||
49         !parcel.ReadDouble(result->speed_) ||
50         !parcel.ReadInt64(result->position_.elapsedTime_) ||
51         !parcel.ReadInt64(result->position_.updateTime_) ||
52         !parcel.ReadInt64(result->bufferedTime_) ||
53         !parcel.ReadInt32(result->loopMode_) ||
54         !parcel.ReadBool(result->isFavorite_) ||
55         !parcel.ReadInt32(result->activeItemId_) ||
56         !parcel.ReadInt32(result->volume_)) {
57         SLOGE("Read AVPlaybackState failed");
58         delete result;
59         return nullptr;
60     }
61     result->extras_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
62     if (result->extras_ == nullptr) {
63         SLOGI("Read AVPlaybackState failed, the extras is null");
64     }
65     return result;
66 }
67 
IsValid() const68 bool AVPlaybackState::IsValid() const
69 {
70     return state_ >= PLAYBACK_STATE_INITIAL &&
71         state_ < PLAYBACK_STATE_MAX &&
72         speed_ > 0 &&
73         position_.elapsedTime_ >= 0 &&
74         position_.updateTime_ >= 0 &&
75         bufferedTime_ >= 0 &&
76         loopMode_ >= LOOP_MODE_SEQUENCE &&
77         loopMode_ <= LOOP_MODE_SHUFFLE &&
78         volume_ >= 0;
79 }
80 
SetState(int32_t state)81 void AVPlaybackState::SetState(int32_t state)
82 {
83     mask_.set(PLAYBACK_KEY_STATE);
84     state_ = state;
85 }
86 
SetSpeed(double speed)87 void AVPlaybackState::SetSpeed(double speed)
88 {
89     mask_.set(PLAYBACK_KEY_SPEED);
90     speed_ = speed;
91 }
92 
SetPosition(const Position & position)93 void AVPlaybackState::SetPosition(const Position& position)
94 {
95     mask_.set(PLAYBACK_KEY_POSITION);
96     position_ = position;
97 }
98 
SetBufferedTime(int64_t time)99 void AVPlaybackState::SetBufferedTime(int64_t time)
100 {
101     mask_.set(PLAYBACK_KEY_BUFFERED_TIME);
102     bufferedTime_ = time;
103 }
104 
SetLoopMode(int32_t mode)105 void AVPlaybackState::SetLoopMode(int32_t mode)
106 {
107     mask_.set(PLAYBACK_KEY_LOOP_MODE);
108     loopMode_ = mode;
109 }
110 
SetFavorite(bool isFavorite)111 void AVPlaybackState::SetFavorite(bool isFavorite)
112 {
113     mask_.set(PLAYBACK_KEY_IS_FAVORITE);
114     isFavorite_ = isFavorite;
115 }
116 
SetActiveItemId(int32_t activeItemId)117 void AVPlaybackState::SetActiveItemId(int32_t activeItemId)
118 {
119     mask_.set(PLAYBACK_KEY_ACTIVE_ITEM_ID);
120     activeItemId_ = activeItemId;
121 }
122 
SetVolume(int32_t volume)123 void AVPlaybackState::SetVolume(int32_t volume)
124 {
125     mask_.set(PLAYBACK_KEY_VOLUME);
126     volume_ = volume;
127 }
128 
SetExtras(const std::shared_ptr<AAFwk::WantParams> & extras)129 void AVPlaybackState::SetExtras(const std::shared_ptr<AAFwk::WantParams>& extras)
130 {
131     mask_.set(PLAYBACK_KEY_EXTRAS);
132     extras_ = extras;
133 }
134 
GetState() const135 int32_t AVPlaybackState::GetState() const
136 {
137     return state_;
138 }
139 
GetSpeed() const140 double AVPlaybackState::GetSpeed() const
141 {
142     return speed_;
143 }
144 
GetPosition() const145 AVPlaybackState::Position AVPlaybackState::GetPosition() const
146 {
147     return position_;
148 }
149 
GetBufferedTime() const150 int64_t AVPlaybackState::GetBufferedTime() const
151 {
152     return bufferedTime_;
153 }
154 
GetLoopMode() const155 int32_t AVPlaybackState::GetLoopMode() const
156 {
157     return loopMode_;
158 }
159 
GetFavorite() const160 bool AVPlaybackState::GetFavorite() const
161 {
162     return isFavorite_;
163 }
164 
GetActiveItemId() const165 int32_t AVPlaybackState::GetActiveItemId() const
166 {
167     return activeItemId_;
168 }
169 
GetVolume() const170 int32_t AVPlaybackState::GetVolume() const
171 {
172     return volume_;
173 }
174 
GetExtras() const175 std::shared_ptr<AAFwk::WantParams> AVPlaybackState::GetExtras() const
176 {
177     return extras_;
178 }
179 
GetMask() const180 AVPlaybackState::PlaybackStateMaskType AVPlaybackState::GetMask() const
181 {
182     return mask_;
183 }
184 
CopyToByMask(PlaybackStateMaskType & mask,AVPlaybackState & out) const185 bool AVPlaybackState::CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const
186 {
187     bool result = false;
188     auto intersection = mask_ & mask;
189     for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
190         if (intersection.test(i)) {
191             cloneActions[i](*this, out);
192             out.mask_.set(i);
193             result = true;
194         }
195     }
196 
197     return result;
198 }
199 
CopyFrom(const AVPlaybackState & in)200 bool AVPlaybackState::CopyFrom(const AVPlaybackState& in)
201 {
202     bool result = false;
203     for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
204         if (in.mask_.test(i)) {
205             cloneActions[i](in, *this);
206             mask_.set(i);
207             result = true;
208         }
209     }
210 
211     return result;
212 }
213 
CloneState(const AVPlaybackState & from,AVPlaybackState & to)214 void AVPlaybackState::CloneState(const AVPlaybackState& from, AVPlaybackState& to)
215 {
216     to.state_ = from.state_;
217 }
218 
CloneSpeed(const AVPlaybackState & from,AVPlaybackState & to)219 void AVPlaybackState::CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to)
220 {
221     to.speed_ = from.speed_;
222 }
223 
ClonePosition(const AVPlaybackState & from,AVPlaybackState & to)224 void AVPlaybackState::ClonePosition(const AVPlaybackState& from, AVPlaybackState& to)
225 {
226     to.position_ = from.position_;
227 }
228 
CloneBufferedTime(const AVPlaybackState & from,AVPlaybackState & to)229 void AVPlaybackState::CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to)
230 {
231     to.bufferedTime_ = from.bufferedTime_;
232 }
233 
CloneLoopMode(const AVPlaybackState & from,AVPlaybackState & to)234 void AVPlaybackState::CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to)
235 {
236     to.loopMode_ = from.loopMode_;
237 }
238 
CloneIsFavorite(const AVPlaybackState & from,AVPlaybackState & to)239 void AVPlaybackState::CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to)
240 {
241     to.isFavorite_ = from.isFavorite_;
242 }
243 
CloneActiveItemId(const AVPlaybackState & from,AVPlaybackState & to)244 void AVPlaybackState::CloneActiveItemId(const AVPlaybackState& from, AVPlaybackState& to)
245 {
246     to.activeItemId_ = from.activeItemId_;
247 }
248 
CloneVolume(const AVPlaybackState & from,AVPlaybackState & to)249 void AVPlaybackState::CloneVolume(const AVPlaybackState& from, AVPlaybackState& to)
250 {
251     to.volume_ = from.volume_;
252 }
253 
CloneExtras(const AVPlaybackState & from,AVPlaybackState & to)254 void AVPlaybackState::CloneExtras(const AVPlaybackState& from, AVPlaybackState& to)
255 {
256     to.extras_ = from.extras_;
257 }
258 } // OHOS::AVSession
259