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 
63 // LCOV_EXCL_START
GetAVCallState() const64 int32_t AVCallState::GetAVCallState() const
65 {
66     return avCallState_;
67 }
68 // LCOV_EXCL_STOP
69 
SetAVCallMuted(bool isAVCallMuted)70 void AVCallState::SetAVCallMuted(bool isAVCallMuted)
71 {
72     mask_.set(AVCALL_STATE_KEY_IS_MUTED);
73     isAVCallMuted_ = isAVCallMuted;
74 }
75 
76 // LCOV_EXCL_START
IsAVCallMuted() const77 bool AVCallState::IsAVCallMuted() const
78 {
79     return isAVCallMuted_;
80 }
81 
GetMask() const82 AVCallState::AVCallStateMaskType AVCallState::GetMask() const
83 {
84     return mask_;
85 }
86 
CopyToByMask(AVCallStateMaskType & mask,AVCallState & out) const87 bool AVCallState::CopyToByMask(AVCallStateMaskType& mask, AVCallState& out) const
88 {
89     bool result = false;
90     auto intersection = mask_ & mask;
91     for (int i = 0; i < AVCALL_STATE_KEY_MAX; i++) {
92         if (intersection.test(i)) {
93             cloneActions[i](*this, out);
94             out.mask_.set(i);
95             result = true;
96         }
97     }
98     return result;
99 }
100 // LCOV_EXCL_STOP
101 
CopyFrom(const AVCallState & in)102 bool AVCallState::CopyFrom(const AVCallState& in)
103 {
104     bool result = false;
105     for (int i = 0; i < AVCALL_STATE_KEY_MAX; i++) {
106         if (in.mask_.test(i)) {
107             cloneActions[i](in, *this);
108             mask_.set(i);
109             result = true;
110         }
111     }
112     return result;
113 }
114 
CloneAVCallState(const AVCallState & from,AVCallState & to)115 void AVCallState::CloneAVCallState(const AVCallState& from, AVCallState& to)
116 {
117     to.avCallState_ = from.avCallState_;
118 }
119 
CloneAVCallIsMuted(const AVCallState & from,AVCallState & to)120 void AVCallState::CloneAVCallIsMuted(const AVCallState& from, AVCallState& to)
121 {
122     to.isAVCallMuted_ = from.isAVCallMuted_;
123 }
124 } // namespace OHOS::AVSession
125