• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 }
35 
Unmarshalling(Parcel & parcel)36 AVPlaybackState *AVPlaybackState::Unmarshalling(Parcel& parcel)
37 {
38     std::string mask;
39     CHECK_AND_RETURN_RET_LOG(parcel.ReadString(mask) && mask.length() == PLAYBACK_KEY_MAX, nullptr, "mask not valid");
40     CHECK_AND_RETURN_RET_LOG(mask.find_first_not_of("01") == std::string::npos, nullptr, "mask string not 0 or 1");
41 
42     auto *result = new (std::nothrow) AVPlaybackState();
43     CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVPlaybackState failed");
44     result->mask_ = PlaybackStateMaskType(mask);
45     if (!parcel.ReadInt32(result->state_) ||
46         !parcel.ReadDouble(result->speed_) ||
47         !parcel.ReadInt64(result->position_.elapsedTime_) ||
48         !parcel.ReadInt64(result->position_.updateTime_) ||
49         !parcel.ReadInt64(result->bufferedTime_) ||
50         !parcel.ReadInt32(result->loopMode_) ||
51         !parcel.ReadBool(result->isFavorite_)) {
52         SLOGE("Read AVPlaybackState failed");
53         delete result;
54         return nullptr;
55     }
56     return result;
57 }
58 
IsValid() const59 bool AVPlaybackState::IsValid() const
60 {
61     return state_ >= PLAYBACK_STATE_INITIAL &&
62         state_ < PLAYBACK_STATE_MAX &&
63         speed_ > 0 &&
64         position_.elapsedTime_ >= 0 &&
65         position_.updateTime_ >= 0 &&
66         bufferedTime_ >= 0 &&
67         loopMode_ >= LOOP_MODE_SEQUENCE &&
68         loopMode_ <= LOOP_MODE_SHUFFLE;
69 }
70 
SetState(int32_t state)71 void AVPlaybackState::SetState(int32_t state)
72 {
73     mask_.set(PLAYBACK_KEY_STATE);
74     state_ = state;
75 }
76 
SetSpeed(double speed)77 void AVPlaybackState::SetSpeed(double speed)
78 {
79     mask_.set(PLAYBACK_KEY_SPEED);
80     speed_ = speed;
81 }
82 
SetPosition(const Position & position)83 void AVPlaybackState::SetPosition(const Position& position)
84 {
85     mask_.set(PLAYBACK_KEY_POSITION);
86     position_ = position;
87 }
88 
SetBufferedTime(int64_t time)89 void AVPlaybackState::SetBufferedTime(int64_t time)
90 {
91     mask_.set(PLAYBACK_KEY_BUFFERED_TIME);
92     bufferedTime_ = time;
93 }
94 
SetLoopMode(int32_t mode)95 void AVPlaybackState::SetLoopMode(int32_t mode)
96 {
97     mask_.set(PLAYBACK_KEY_LOOP_MODE);
98     loopMode_ = mode;
99 }
100 
SetFavorite(bool isFavorite)101 void AVPlaybackState::SetFavorite(bool isFavorite)
102 {
103     mask_.set(PLAYBACK_KEY_IS_FAVORITE);
104     isFavorite_ = isFavorite;
105 }
106 
GetState() const107 int32_t AVPlaybackState::GetState() const
108 {
109     return state_;
110 }
111 
GetSpeed() const112 double AVPlaybackState::GetSpeed() const
113 {
114     return speed_;
115 }
116 
GetPosition() const117 AVPlaybackState::Position AVPlaybackState::GetPosition() const
118 {
119     return position_;
120 }
121 
GetBufferedTime() const122 int64_t AVPlaybackState::GetBufferedTime() const
123 {
124     return bufferedTime_;
125 }
126 
GetLoopMode() const127 int32_t AVPlaybackState::GetLoopMode() const
128 {
129     return loopMode_;
130 }
131 
GetFavorite() const132 bool AVPlaybackState::GetFavorite() const
133 {
134     return isFavorite_;
135 }
136 
GetMask() const137 AVPlaybackState::PlaybackStateMaskType AVPlaybackState::GetMask() const
138 {
139     return mask_;
140 }
141 
CopyToByMask(PlaybackStateMaskType & mask,AVPlaybackState & out) const142 bool AVPlaybackState::CopyToByMask(PlaybackStateMaskType& mask, AVPlaybackState& out) const
143 {
144     bool result = false;
145     auto intersection = mask_ & mask;
146     for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
147         if (intersection.test(i)) {
148             cloneActions[i](*this, out);
149             out.mask_.set(i);
150             result = true;
151         }
152     }
153 
154     return result;
155 }
156 
CopyFrom(const AVPlaybackState & in)157 bool AVPlaybackState::CopyFrom(const AVPlaybackState& in)
158 {
159     bool result = false;
160     for (int i = 0; i < PLAYBACK_KEY_MAX; i++) {
161         if (in.mask_.test(i)) {
162             cloneActions[i](in, *this);
163             mask_.set(i);
164             result = true;
165         }
166     }
167 
168     return result;
169 }
170 
CloneState(const AVPlaybackState & from,AVPlaybackState & to)171 void AVPlaybackState::CloneState(const AVPlaybackState& from, AVPlaybackState& to)
172 {
173     to.state_ = from.state_;
174 }
175 
CloneSpeed(const AVPlaybackState & from,AVPlaybackState & to)176 void AVPlaybackState::CloneSpeed(const AVPlaybackState& from, AVPlaybackState& to)
177 {
178     to.speed_ = from.speed_;
179 }
180 
ClonePosition(const AVPlaybackState & from,AVPlaybackState & to)181 void AVPlaybackState::ClonePosition(const AVPlaybackState& from, AVPlaybackState& to)
182 {
183     to.position_ = from.position_;
184 }
185 
CloneBufferedTime(const AVPlaybackState & from,AVPlaybackState & to)186 void AVPlaybackState::CloneBufferedTime(const AVPlaybackState& from, AVPlaybackState& to)
187 {
188     to.bufferedTime_ = from.bufferedTime_;
189 }
190 
CloneLoopMode(const AVPlaybackState & from,AVPlaybackState & to)191 void AVPlaybackState::CloneLoopMode(const AVPlaybackState& from, AVPlaybackState& to)
192 {
193     to.loopMode_ = from.loopMode_;
194 }
195 
CloneIsFavorite(const AVPlaybackState & from,AVPlaybackState & to)196 void AVPlaybackState::CloneIsFavorite(const AVPlaybackState& from, AVPlaybackState& to)
197 {
198     to.isFavorite_ = from.isFavorite_;
199 }
200 } // OHOS::AVSession