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         return nullptr;
70     }
71     result->extras_ = std::shared_ptr<AAFwk::WantParams>(parcel.ReadParcelable<AAFwk::WantParams>());
72     if (result->extras_ == nullptr) {
73         SLOGD("Read AVPlaybackState with no extras");
74     }
75     return result;
76 }
77 
IsValid() const78 bool AVPlaybackState::IsValid() const
79 {
80     return state_ >= PLAYBACK_STATE_INITIAL &&
81         state_ < PLAYBACK_STATE_MAX &&
82         speed_ > 0 &&
83         position_.elapsedTime_ >= 0 &&
84         position_.updateTime_ >= 0 &&
85         bufferedTime_ >= 0 &&
86         loopMode_ >= LOOP_MODE_SEQUENCE &&
87         loopMode_ <= LOOP_MODE_CUSTOM &&
88         volume_ >= 0 &&
89         maxVolume_ >= 0;
90 }
91 
SetState(int32_t state)92 void AVPlaybackState::SetState(int32_t state)
93 {
94     mask_.set(PLAYBACK_KEY_STATE);
95     state_ = state;
96 }
97 
SetSpeed(double speed)98 void AVPlaybackState::SetSpeed(double speed)
99 {
100     mask_.set(PLAYBACK_KEY_SPEED);
101     speed_ = speed;
102 }
103 
SetPosition(const Position & position)104 void AVPlaybackState::SetPosition(const Position& position)
105 {
106     mask_.set(PLAYBACK_KEY_POSITION);
107     position_ = position;
108 }
109 
SetBufferedTime(int64_t time)110 void AVPlaybackState::SetBufferedTime(int64_t time)
111 {
112     mask_.set(PLAYBACK_KEY_BUFFERED_TIME);
113     bufferedTime_ = time;
114 }
115 
SetLoopMode(int32_t mode)116 void AVPlaybackState::SetLoopMode(int32_t mode)
117 {
118     mask_.set(PLAYBACK_KEY_LOOP_MODE);
119     loopMode_ = mode;
120 }
121 
SetFavorite(bool isFavorite)122 void AVPlaybackState::SetFavorite(bool isFavorite)
123 {
124     mask_.set(PLAYBACK_KEY_IS_FAVORITE);
125     isFavorite_ = isFavorite;
126 }
127 
SetActiveItemId(int32_t activeItemId)128 void AVPlaybackState::SetActiveItemId(int32_t activeItemId)
129 {
130     mask_.set(PLAYBACK_KEY_ACTIVE_ITEM_ID);
131     activeItemId_ = activeItemId;
132 }
133 
SetVolume(int32_t volume)134 void AVPlaybackState::SetVolume(int32_t volume)
135 {
136     mask_.set(PLAYBACK_KEY_VOLUME);
137     volume_ = volume;
138 }
139 
SetMaxVolume(int32_t maxVolume)140 void AVPlaybackState::SetMaxVolume(int32_t maxVolume)
141 {
142     mask_.set(PLAYBACK_KEY_MAX_VOLUME);
143     maxVolume_ = maxVolume;
144 }
145 
SetMuted(bool muted)146 void AVPlaybackState::SetMuted(bool muted)
147 {
148     mask_.set(PLAYBACK_KEY_MUTED);
149     muted_ = muted;
150 }
151 
SetDuration(int32_t duration)152 void AVPlaybackState::SetDuration(int32_t duration)
153 {
154     mask_.set(PLAYBACK_KEY_DURATION);
155     duration_ = duration;
156 }
157 
SetVideoWidth(int32_t videoWidth)158 void AVPlaybackState::SetVideoWidth(int32_t videoWidth)
159 {
160     mask_.set(PLAYBACK_KEY_VIDEO_WIDTH);
161     videoWidth_ = videoWidth;
162 }
163 
SetVideoHeight(int32_t videoHeight)164 void AVPlaybackState::SetVideoHeight(int32_t videoHeight)
165 {
166     mask_.set(PLAYBACK_KEY_VIDEO_HEIGHT);
167     videoHeight_ = videoHeight;
168 }
169 
SetExtras(const std::shared_ptr<AAFwk::WantParams> & extras)170 void AVPlaybackState::SetExtras(const std::shared_ptr<AAFwk::WantParams>& extras)
171 {
172     mask_.set(PLAYBACK_KEY_EXTRAS);
173     extras_ = extras;
174 }
175 
GetState() const176 int32_t AVPlaybackState::GetState() const
177 {
178     return state_;
179 }
180 
GetSpeed() const181 double AVPlaybackState::GetSpeed() const
182 {
183     return speed_;
184 }
185 
GetPosition() const186 AVPlaybackState::Position AVPlaybackState::GetPosition() const
187 {
188     return position_;
189 }
190 
GetBufferedTime() const191 int64_t AVPlaybackState::GetBufferedTime() const
192 {
193     return bufferedTime_;
194 }
195 
GetLoopMode() const196 int32_t AVPlaybackState::GetLoopMode() const
197 {
198     return loopMode_;
199 }
200 
GetFavorite() const201 bool AVPlaybackState::GetFavorite() const
202 {
203     return isFavorite_;
204 }
205 
GetActiveItemId() const206 int32_t AVPlaybackState::GetActiveItemId() const
207 {
208     return activeItemId_;
209 }
210 
GetVolume() const211 int32_t AVPlaybackState::GetVolume() const
212 {
213     return volume_;
214 }
215 
GetMaxVolume() const216 int32_t AVPlaybackState::GetMaxVolume() const
217 {
218     return maxVolume_;
219 }
220 
GetMuted() const221 bool AVPlaybackState::GetMuted() const
222 {
223     return muted_;
224 }
225 
GetDuration() const226 int32_t AVPlaybackState::GetDuration() const
227 {
228     return duration_;
229 }
230 
GetVideoWidth() const231 int32_t AVPlaybackState::GetVideoWidth() const
232 {
233     return videoWidth_;
234 }
235 
GetVideoHeight() const236 int32_t AVPlaybackState::GetVideoHeight() const
237 {
238     return videoHeight_;
239 }
240 
GetExtras() const241 std::shared_ptr<AAFwk::WantParams> AVPlaybackState::GetExtras() const
242 {
243     return extras_;
244 }
245 
GetMask() const246 AVPlaybackState::PlaybackStateMaskType AVPlaybackState::GetMask() const
247 {
248     return mask_;
249 }
250 
CopyToByMask(PlaybackStateMaskType & mask,AVPlaybackState & out) const251 bool AVPlaybackState::CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const
252 {
253     bool result = false;
254     auto intersection = mask_ & mask;
255     for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
256         if (intersection.test(i)) {
257             cloneActions[i](*this, out);
258             out.mask_.set(i);
259             result = true;
260         }
261     }
262 
263     return result;
264 }
265 
CopyFrom(const AVPlaybackState & in)266 bool AVPlaybackState::CopyFrom(const AVPlaybackState& in)
267 {
268     bool result = false;
269     for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
270         if (in.mask_.test(i)) {
271             cloneActions[i](in, *this);
272             mask_.set(i);
273             result = true;
274         }
275     }
276 
277     return result;
278 }
279 
CloneState(const AVPlaybackState & from,AVPlaybackState & to)280 void AVPlaybackState::CloneState(const AVPlaybackState& from, AVPlaybackState& to)
281 {
282     to.state_ = from.state_;
283 }
284 
CloneSpeed(const AVPlaybackState & from,AVPlaybackState & to)285 void AVPlaybackState::CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to)
286 {
287     to.speed_ = from.speed_;
288 }
289 
ClonePosition(const AVPlaybackState & from,AVPlaybackState & to)290 void AVPlaybackState::ClonePosition(const AVPlaybackState& from, AVPlaybackState& to)
291 {
292     to.position_ = from.position_;
293 }
294 
CloneBufferedTime(const AVPlaybackState & from,AVPlaybackState & to)295 void AVPlaybackState::CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to)
296 {
297     to.bufferedTime_ = from.bufferedTime_;
298 }
299 
CloneLoopMode(const AVPlaybackState & from,AVPlaybackState & to)300 void AVPlaybackState::CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to)
301 {
302     to.loopMode_ = from.loopMode_;
303 }
304 
CloneIsFavorite(const AVPlaybackState & from,AVPlaybackState & to)305 void AVPlaybackState::CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to)
306 {
307     to.isFavorite_ = from.isFavorite_;
308 }
309 
CloneActiveItemId(const AVPlaybackState & from,AVPlaybackState & to)310 void AVPlaybackState::CloneActiveItemId(const AVPlaybackState& from, AVPlaybackState& to)
311 {
312     to.activeItemId_ = from.activeItemId_;
313 }
314 
CloneVolume(const AVPlaybackState & from,AVPlaybackState & to)315 void AVPlaybackState::CloneVolume(const AVPlaybackState& from, AVPlaybackState& to)
316 {
317     to.volume_ = from.volume_;
318 }
319 
CloneMaxVolume(const AVPlaybackState & from,AVPlaybackState & to)320 void AVPlaybackState::CloneMaxVolume(const AVPlaybackState& from, AVPlaybackState& to)
321 {
322     to.maxVolume_ = from.maxVolume_;
323 }
324 
CloneMuted(const AVPlaybackState & from,AVPlaybackState & to)325 void AVPlaybackState::CloneMuted(const AVPlaybackState& from, AVPlaybackState& to)
326 {
327     to.muted_ = from.muted_;
328 }
329 
CloneDuration(const AVPlaybackState & from,AVPlaybackState & to)330 void AVPlaybackState::CloneDuration(const AVPlaybackState& from, AVPlaybackState& to)
331 {
332     to.duration_ = from.duration_;
333 }
334 
CloneVideoWidth(const AVPlaybackState & from,AVPlaybackState & to)335 void AVPlaybackState::CloneVideoWidth(const AVPlaybackState& from, AVPlaybackState& to)
336 {
337     to.videoWidth_ = from.videoWidth_;
338 }
339 
CloneVideoHeight(const AVPlaybackState & from,AVPlaybackState & to)340 void AVPlaybackState::CloneVideoHeight(const AVPlaybackState& from, AVPlaybackState& to)
341 {
342     to.videoHeight_ = from.videoHeight_;
343 }
344 
CloneExtras(const AVPlaybackState & from,AVPlaybackState & to)345 void AVPlaybackState::CloneExtras(const AVPlaybackState& from, AVPlaybackState& to)
346 {
347     to.extras_ = from.extras_;
348 }
349 } // OHOS::AVSession
350