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 "avsession_controller_stub.h"
17 #include "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "avsession_trace.h"
20
21 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)22 bool AVSessionControllerStub::CheckInterfaceToken(MessageParcel& data)
23 {
24 auto localDescriptor = IAVSessionController::GetDescriptor();
25 auto remoteDescriptor = data.ReadInterfaceToken();
26 if (remoteDescriptor != localDescriptor) {
27 SLOGI("interface token is not equal");
28 return false;
29 }
30 return true;
31 }
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t AVSessionControllerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
34 MessageOption &option)
35 {
36 if (!CheckInterfaceToken(data)) {
37 return AVSESSION_ERROR;
38 }
39 if (code < CONTROLLER_CMD_MAX) {
40 return (this->*handlers[code])(data, reply);
41 }
42 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
43 }
44
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)45 int32_t AVSessionControllerStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
46 {
47 auto remoteObject = data.ReadRemoteObject();
48 if (remoteObject == nullptr) {
49 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write RegisterCallback ret failed");
50 } else {
51 CHECK_AND_PRINT_LOG(reply.WriteInt32(RegisterCallbackInner(remoteObject)),
52 "write RegisterCallback ret failed");
53 }
54 return ERR_NONE;
55 }
56
HandleDestroy(MessageParcel & data,MessageParcel & reply)57 int32_t AVSessionControllerStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
58 {
59 CHECK_AND_PRINT_LOG(reply.WriteInt32(Destroy()), "write release() ret failed");
60 return ERR_NONE;
61 }
62
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)63 int32_t AVSessionControllerStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
64 {
65 AVPlaybackState state;
66 int32_t ret = GetAVPlaybackState(state);
67 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
68 if (ret == AVSESSION_SUCCESS) {
69 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&state), "write AVPlaybackState failed");
70 }
71 return ERR_NONE;
72 }
73
HandleSendControlCommand(MessageParcel & data,MessageParcel & reply)74 int32_t AVSessionControllerStub::HandleSendControlCommand(MessageParcel& data, MessageParcel& reply)
75 {
76 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendControlCommand");
77 sptr<AVControlCommand> cmd = data.ReadParcelable<AVControlCommand>();
78 if (cmd == nullptr) {
79 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendCommand ret failed");
80 } else {
81 CHECK_AND_PRINT_LOG(reply.WriteInt32(SendControlCommand(*cmd)), "write SendCommand ret failed");
82 }
83 return ERR_NONE;
84 }
85
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)86 int32_t AVSessionControllerStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
87 {
88 AVMetaData metaData;
89 int32_t ret = GetAVMetaData(metaData);
90 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
91 if (ret == AVSESSION_SUCCESS) {
92 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&metaData), "write AVMetaData failed");
93 }
94 return ERR_NONE;
95 }
96
HandleSendAVKeyEvent(MessageParcel & data,MessageParcel & reply)97 int32_t AVSessionControllerStub::HandleSendAVKeyEvent(MessageParcel& data, MessageParcel& reply)
98 {
99 AVSESSION_TRACE_SYNC_START("AVSessionControllerStub::SendAVKeyEvent");
100 std::shared_ptr<MMI::KeyEvent> event = MMI::KeyEvent::Create();
101 if (event == nullptr) {
102 SLOGI("malloc keyEvent failed");
103 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_NO_MEMORY), "write SendAVKeyEvent ret failed");
104 return ERR_NONE;
105 }
106
107 event->ReadFromParcel(data);
108 if (!event->IsValid()) {
109 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SendAVKeyEvent ret failed");
110 } else {
111 CHECK_AND_PRINT_LOG(reply.WriteInt32(SendAVKeyEvent(*(event.get()))),
112 "write SendAVKeyEvent ret failed");
113 }
114 return ERR_NONE;
115 }
116
HandleGetLaunchAbility(MessageParcel & data,MessageParcel & reply)117 int32_t AVSessionControllerStub::HandleGetLaunchAbility(MessageParcel& data, MessageParcel& reply)
118 {
119 AbilityRuntime::WantAgent::WantAgent ability;
120 int32_t ret = GetLaunchAbility(ability);
121 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
122 if (ret == AVSESSION_SUCCESS) {
123 CHECK_AND_PRINT_LOG(reply.WriteParcelable(&ability), "write LaunchAbility failed");
124 }
125 return ERR_NONE;
126 }
127
HandleGetValidCommands(MessageParcel & data,MessageParcel & reply)128 int32_t AVSessionControllerStub::HandleGetValidCommands(MessageParcel& data, MessageParcel& reply)
129 {
130 std::vector<int32_t> cmds;
131 int32_t ret = GetValidCommands(cmds);
132 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
133 if (ret == AVSESSION_SUCCESS) {
134 CHECK_AND_PRINT_LOG(reply.WriteInt32Vector(cmds), "write int32 vector failed");
135 }
136 return ERR_NONE;
137 }
138
HandleSetMetaFilter(MessageParcel & data,MessageParcel & reply)139 int32_t AVSessionControllerStub::HandleSetMetaFilter(MessageParcel& data, MessageParcel& reply)
140 {
141 std::string str = data.ReadString();
142 if (str.length() != AVMetaData::META_KEY_MAX) {
143 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetMetaFilter ret failed");
144 return ERR_NONE;
145 }
146 if (str.find_first_not_of("01") != std::string::npos) {
147 SLOGI("mask string not 0 or 1");
148 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
149 return ERR_NONE;
150 }
151 AVMetaData::MetaMaskType filter(str);
152 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetMetaFilter(filter)), "write int32 failed");
153 return ERR_NONE;
154 }
155
HandleSetPlaybackFilter(MessageParcel & data,MessageParcel & reply)156 int32_t AVSessionControllerStub::HandleSetPlaybackFilter(MessageParcel& data, MessageParcel& reply)
157 {
158 std::string str = data.ReadString();
159 if (str.length() != AVPlaybackState::PLAYBACK_KEY_MAX) {
160 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write SetPlaybackFilter ret failed");
161 return ERR_NONE;
162 }
163 if (str.find_first_not_of("01") != std::string::npos) {
164 SLOGI("mask string not all 0 or 1");
165 CHECK_AND_PRINT_LOG(reply.WriteInt32(ERR_UNMARSHALLING), "write int32 failed");
166 return ERR_NONE;
167 }
168 AVPlaybackState::PlaybackStateMaskType filter(str);
169 CHECK_AND_PRINT_LOG(reply.WriteInt32(SetPlaybackFilter(filter)), "write int32 failed");
170 return ERR_NONE;
171 }
172
HandleIsSessionActive(MessageParcel & data,MessageParcel & reply)173 int32_t AVSessionControllerStub::HandleIsSessionActive(MessageParcel& data, MessageParcel& reply)
174 {
175 bool isActive = false;
176 int32_t ret = IsSessionActive(isActive);
177 CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
178 if (ret == AVSESSION_SUCCESS) {
179 CHECK_AND_PRINT_LOG(reply.WriteBool(isActive), "write bool failed");
180 }
181 return ERR_NONE;
182 }
183
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)184 int32_t AVSessionControllerStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
185 {
186 CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
187 return ERR_NONE;
188 }
189 } // OHOS::AVSession