1 /*
2 * Copyright (c) 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 "avcall_state.h"
17 #include "avsession_log.h"
18
19 namespace OHOS::AVSession {
AVCallState()20 AVCallState::AVCallState()
21 {
22 }
23
Marshalling(Parcel & parcel) const24 bool AVCallState::Marshalling(Parcel& parcel) const
25 {
26 return parcel.WriteString(mask_.to_string()) &&
27 parcel.WriteInt32(avCallState_) &&
28 parcel.WriteBool(isAVCallMuted_);
29 }
30
Unmarshalling(Parcel & parcel)31 AVCallState *AVCallState::Unmarshalling(Parcel& parcel)
32 {
33 std::string mask;
34 CHECK_AND_RETURN_RET_LOG(parcel.ReadString(mask) && mask.length() == AVCALL_STATE_KEY_MAX,
35 nullptr, "mask not valid");
36 CHECK_AND_RETURN_RET_LOG(mask.find_first_not_of("01") == std::string::npos,
37 nullptr, "mask string not 0 or 1");
38
39 auto *result = new (std::nothrow) AVCallState();
40 CHECK_AND_RETURN_RET_LOG(result != nullptr, nullptr, "new AVCallState failed");
41 result->mask_ = AVCallStateMaskType(mask);
42 if (!parcel.ReadInt32(result->avCallState_) ||
43 !parcel.ReadBool(result->isAVCallMuted_)) {
44 SLOGE("Read AVCallState failed");
45 delete result;
46 return nullptr;
47 }
48 return result;
49 }
50
IsValid() const51 bool AVCallState::IsValid() const
52 {
53 return avCallState_ >= AVCALL_STATE_IDLE &&
54 avCallState_ < AVCALL_STATE_MAX;
55 }
56
SetAVCallState(int32_t avCallState)57 void AVCallState::SetAVCallState(int32_t avCallState)
58 {
59 mask_.set(AVCALL_STATE_KEY_STATE);
60 avCallState_ = avCallState;
61 }
62
GetAVCallState() const63 int32_t AVCallState::GetAVCallState() const
64 {
65 return avCallState_;
66 }
67
SetAVCallMuted(bool isAVCallMuted)68 void AVCallState::SetAVCallMuted(bool isAVCallMuted)
69 {
70 mask_.set(AVCALL_STATE_KEY_IS_MUTED);
71 isAVCallMuted_ = isAVCallMuted;
72 }
73
IsAVCallMuted() const74 bool AVCallState::IsAVCallMuted() const
75 {
76 return isAVCallMuted_;
77 }
78
GetMask() const79 AVCallState::AVCallStateMaskType AVCallState::GetMask() const
80 {
81 return mask_;
82 }
83
CopyToByMask(AVCallStateMaskType & mask,AVCallState & out) const84 bool AVCallState::CopyToByMask(AVCallStateMaskType& mask, AVCallState& out) const
85 {
86 bool result = false;
87 auto intersection = mask_ & mask;
88 for (int i = 0; i < AVCALL_STATE_KEY_MAX; i++) {
89 if (intersection.test(i)) {
90 cloneActions[i](*this, out);
91 out.mask_.set(i);
92 result = true;
93 }
94 }
95 return result;
96 }
97
CopyFrom(const AVCallState & in)98 bool AVCallState::CopyFrom(const AVCallState& in)
99 {
100 bool result = false;
101 for (int i = 0; i < AVCALL_STATE_KEY_MAX; i++) {
102 if (in.mask_.test(i)) {
103 cloneActions[i](in, *this);
104 mask_.set(i);
105 result = true;
106 }
107 }
108 return result;
109 }
110
CloneAVCallState(const AVCallState & from,AVCallState & to)111 void AVCallState::CloneAVCallState(const AVCallState& from, AVCallState& to)
112 {
113 to.avCallState_ = from.avCallState_;
114 }
115
CloneAVCallIsMuted(const AVCallState & from,AVCallState & to)116 void AVCallState::CloneAVCallIsMuted(const AVCallState& from, AVCallState& to)
117 {
118 to.isAVCallMuted_ = from.isAVCallMuted_;
119 }
120 } // namespace OHOS::AVSession
121