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.WriteInt32(maxVolume_) &&
37 parcel.WriteBool(muted_) &&
38 parcel.WriteInt32(duration_) &&
39 parcel.WriteInt32(videoWidth_) &&
40 parcel.WriteInt32(videoHeight_) &&
41 parcel.WriteParcelable(extras_.get());
42 }
43
Unmarshalling(Parcel & parcel)44 AVPlaybackState *AVPlaybackState::Unmarshalling(Parcel& parcel)
45 {
46 std::string mask;
47 CHECK_AND_RETURN_RET_LOG(parcel.ReadString(mask) && mask.length() == PLAYBACK_KEY_MAX, nullptr, "mask not valid");
48 CHECK_AND_RETURN_RET_LOG(mask.find_first_not_of("01") == std::string::npos, nullptr, "mask string not 0 or 1");
49
50 auto *result = new (std::nothrow) AVPlaybackState();
51 CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVPlaybackState failed");
52 result->mask_ = PlaybackStateMaskType(mask);
53 if (!parcel.ReadInt32(result->state_) ||
54 !parcel.ReadDouble(result->speed_) ||
55 !parcel.ReadInt64(result->position_.elapsedTime_) ||
56 !parcel.ReadInt64(result->position_.updateTime_) ||
57 !parcel.ReadInt64(result->bufferedTime_) ||
58 !parcel.ReadInt32(result->loopMode_) ||
59 !parcel.ReadBool(result->isFavorite_) ||
60 !parcel.ReadInt32(result->activeItemId_) ||
61 !parcel.ReadInt32(result->volume_) ||
62 !parcel.ReadInt32(result->maxVolume_) ||
63 !parcel.ReadBool(result->muted_) ||
64 !parcel.ReadInt32(result->duration_) ||
65 !parcel.ReadInt32(result->videoWidth_) ||
66 !parcel.ReadInt32(result->videoHeight_)) {
67 SLOGE("Read AVPlaybackState failed");
68 delete result;
69 result = nullptr;
70 return nullptr;
71 }
72 result->extras_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
73 if (result->extras_ == nullptr) {
74 SLOGD("Read AVPlaybackState with no extras");
75 }
76 return result;
77 }
78
IsValid() const79 bool AVPlaybackState::IsValid() const
80 {
81 return state_ >= PLAYBACK_STATE_INITIAL &&
82 state_ < PLAYBACK_STATE_MAX &&
83 speed_ > 0 &&
84 position_.elapsedTime_ >= 0 &&
85 position_.updateTime_ >= 0 &&
86 bufferedTime_ >= 0 &&
87 loopMode_ >= LOOP_MODE_SEQUENCE &&
88 loopMode_ <= LOOP_MODE_CUSTOM &&
89 volume_ >= 0 &&
90 maxVolume_ >= 0;
91 }
92
SetState(int32_t state)93 void AVPlaybackState::SetState(int32_t state)
94 {
95 mask_.set(PLAYBACK_KEY_STATE);
96 state_ = state;
97 }
98
SetSpeed(double speed)99 void AVPlaybackState::SetSpeed(double speed)
100 {
101 mask_.set(PLAYBACK_KEY_SPEED);
102 speed_ = speed;
103 }
104
SetPosition(const Position & position)105 void AVPlaybackState::SetPosition(const Position& position)
106 {
107 mask_.set(PLAYBACK_KEY_POSITION);
108 position_ = position;
109 }
110
SetBufferedTime(int64_t time)111 void AVPlaybackState::SetBufferedTime(int64_t time)
112 {
113 mask_.set(PLAYBACK_KEY_BUFFERED_TIME);
114 bufferedTime_ = time;
115 }
116
SetLoopMode(int32_t mode)117 void AVPlaybackState::SetLoopMode(int32_t mode)
118 {
119 mask_.set(PLAYBACK_KEY_LOOP_MODE);
120 loopMode_ = mode;
121 }
122
SetFavorite(bool isFavorite)123 void AVPlaybackState::SetFavorite(bool isFavorite)
124 {
125 mask_.set(PLAYBACK_KEY_IS_FAVORITE);
126 isFavorite_ = isFavorite;
127 }
128
SetActiveItemId(int32_t activeItemId)129 void AVPlaybackState::SetActiveItemId(int32_t activeItemId)
130 {
131 mask_.set(PLAYBACK_KEY_ACTIVE_ITEM_ID);
132 activeItemId_ = activeItemId;
133 }
134
SetVolume(int32_t volume)135 void AVPlaybackState::SetVolume(int32_t volume)
136 {
137 mask_.set(PLAYBACK_KEY_VOLUME);
138 volume_ = volume;
139 }
140
SetMaxVolume(int32_t maxVolume)141 void AVPlaybackState::SetMaxVolume(int32_t maxVolume)
142 {
143 mask_.set(PLAYBACK_KEY_MAX_VOLUME);
144 maxVolume_ = maxVolume;
145 }
146
SetMuted(bool muted)147 void AVPlaybackState::SetMuted(bool muted)
148 {
149 mask_.set(PLAYBACK_KEY_MUTED);
150 muted_ = muted;
151 }
152
SetDuration(int32_t duration)153 void AVPlaybackState::SetDuration(int32_t duration)
154 {
155 mask_.set(PLAYBACK_KEY_DURATION);
156 duration_ = duration;
157 }
158
SetVideoWidth(int32_t videoWidth)159 void AVPlaybackState::SetVideoWidth(int32_t videoWidth)
160 {
161 mask_.set(PLAYBACK_KEY_VIDEO_WIDTH);
162 videoWidth_ = videoWidth;
163 }
164
SetVideoHeight(int32_t videoHeight)165 void AVPlaybackState::SetVideoHeight(int32_t videoHeight)
166 {
167 mask_.set(PLAYBACK_KEY_VIDEO_HEIGHT);
168 videoHeight_ = videoHeight;
169 }
170
SetExtras(const std::shared_ptr<AAFwk::WantParams> & extras)171 void AVPlaybackState::SetExtras(const std::shared_ptr<AAFwk::WantParams>& extras)
172 {
173 mask_.set(PLAYBACK_KEY_EXTRAS);
174 extras_ = extras;
175 }
176
GetState() const177 int32_t AVPlaybackState::GetState() const
178 {
179 return state_;
180 }
181
GetSpeed() const182 double AVPlaybackState::GetSpeed() const
183 {
184 return speed_;
185 }
186
GetPosition() const187 AVPlaybackState::Position AVPlaybackState::GetPosition() const
188 {
189 return position_;
190 }
191
GetBufferedTime() const192 int64_t AVPlaybackState::GetBufferedTime() const
193 {
194 return bufferedTime_;
195 }
196
GetLoopMode() const197 int32_t AVPlaybackState::GetLoopMode() const
198 {
199 return loopMode_;
200 }
201
GetFavorite() const202 bool AVPlaybackState::GetFavorite() const
203 {
204 return isFavorite_;
205 }
206
GetActiveItemId() const207 int32_t AVPlaybackState::GetActiveItemId() const
208 {
209 return activeItemId_;
210 }
211
GetVolume() const212 int32_t AVPlaybackState::GetVolume() const
213 {
214 return volume_;
215 }
216
GetMaxVolume() const217 int32_t AVPlaybackState::GetMaxVolume() const
218 {
219 return maxVolume_;
220 }
221
GetMuted() const222 bool AVPlaybackState::GetMuted() const
223 {
224 return muted_;
225 }
226
GetDuration() const227 int32_t AVPlaybackState::GetDuration() const
228 {
229 return duration_;
230 }
231
GetVideoWidth() const232 int32_t AVPlaybackState::GetVideoWidth() const
233 {
234 return videoWidth_;
235 }
236
GetVideoHeight() const237 int32_t AVPlaybackState::GetVideoHeight() const
238 {
239 return videoHeight_;
240 }
241
GetExtras() const242 std::shared_ptr<AAFwk::WantParams> AVPlaybackState::GetExtras() const
243 {
244 return extras_;
245 }
246
GetMask() const247 AVPlaybackState::PlaybackStateMaskType AVPlaybackState::GetMask() const
248 {
249 return mask_;
250 }
251
CopyToByMask(PlaybackStateMaskType & mask,AVPlaybackState & out) const252 bool AVPlaybackState::CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const
253 {
254 bool result = false;
255 auto intersection = mask_ & mask;
256 for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
257 if (intersection.test(i)) {
258 cloneActions[i](*this, out);
259 out.mask_.set(i);
260 result = true;
261 }
262 }
263
264 return result;
265 }
266
CopyFrom(const AVPlaybackState & in)267 bool AVPlaybackState::CopyFrom(const AVPlaybackState& in)
268 {
269 bool result = false;
270 for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
271 if (in.mask_.test(i)) {
272 cloneActions[i](in, *this);
273 mask_.set(i);
274 result = true;
275 }
276 }
277
278 return result;
279 }
280
CloneState(const AVPlaybackState & from,AVPlaybackState & to)281 void AVPlaybackState::CloneState(const AVPlaybackState& from, AVPlaybackState& to)
282 {
283 to.state_ = from.state_;
284 }
285
CloneSpeed(const AVPlaybackState & from,AVPlaybackState & to)286 void AVPlaybackState::CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to)
287 {
288 to.speed_ = from.speed_;
289 }
290
ClonePosition(const AVPlaybackState & from,AVPlaybackState & to)291 void AVPlaybackState::ClonePosition(const AVPlaybackState& from, AVPlaybackState& to)
292 {
293 to.position_ = from.position_;
294 }
295
CloneBufferedTime(const AVPlaybackState & from,AVPlaybackState & to)296 void AVPlaybackState::CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to)
297 {
298 to.bufferedTime_ = from.bufferedTime_;
299 }
300
CloneLoopMode(const AVPlaybackState & from,AVPlaybackState & to)301 void AVPlaybackState::CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to)
302 {
303 to.loopMode_ = from.loopMode_;
304 }
305
CloneIsFavorite(const AVPlaybackState & from,AVPlaybackState & to)306 void AVPlaybackState::CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to)
307 {
308 to.isFavorite_ = from.isFavorite_;
309 }
310
CloneActiveItemId(const AVPlaybackState & from,AVPlaybackState & to)311 void AVPlaybackState::CloneActiveItemId(const AVPlaybackState& from, AVPlaybackState& to)
312 {
313 to.activeItemId_ = from.activeItemId_;
314 }
315
CloneVolume(const AVPlaybackState & from,AVPlaybackState & to)316 void AVPlaybackState::CloneVolume(const AVPlaybackState& from, AVPlaybackState& to)
317 {
318 to.volume_ = from.volume_;
319 }
320
CloneMaxVolume(const AVPlaybackState & from,AVPlaybackState & to)321 void AVPlaybackState::CloneMaxVolume(const AVPlaybackState& from, AVPlaybackState& to)
322 {
323 to.maxVolume_ = from.maxVolume_;
324 }
325
CloneMuted(const AVPlaybackState & from,AVPlaybackState & to)326 void AVPlaybackState::CloneMuted(const AVPlaybackState& from, AVPlaybackState& to)
327 {
328 to.muted_ = from.muted_;
329 }
330
CloneDuration(const AVPlaybackState & from,AVPlaybackState & to)331 void AVPlaybackState::CloneDuration(const AVPlaybackState& from, AVPlaybackState& to)
332 {
333 to.duration_ = from.duration_;
334 }
335
CloneVideoWidth(const AVPlaybackState & from,AVPlaybackState & to)336 void AVPlaybackState::CloneVideoWidth(const AVPlaybackState& from, AVPlaybackState& to)
337 {
338 to.videoWidth_ = from.videoWidth_;
339 }
340
CloneVideoHeight(const AVPlaybackState & from,AVPlaybackState & to)341 void AVPlaybackState::CloneVideoHeight(const AVPlaybackState& from, AVPlaybackState& to)
342 {
343 to.videoHeight_ = from.videoHeight_;
344 }
345
CloneExtras(const AVPlaybackState & from,AVPlaybackState & to)346 void AVPlaybackState::CloneExtras(const AVPlaybackState& from, AVPlaybackState& to)
347 {
348 to.extras_ = from.extras_;
349 }
350 } // OHOS::AVSession
351