• 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 "avsession_stub.h"
17 #include "avsession_callback_proxy.h"
18 #include "avsession_trace.h"
19 
20 namespace OHOS::AVSession {
CheckInterfaceToken(MessageParcel & data)21 bool AVSessionStub::CheckInterfaceToken(MessageParcel& data)
22 {
23     auto localDescriptor = IAVSession::GetDescriptor();
24     auto remoteDescriptor = data.ReadInterfaceToken();
25     if (remoteDescriptor != localDescriptor) {
26         SLOGI("interface token is not equal");
27         return false;
28     }
29     return true;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t AVSessionStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
33 {
34     if (!CheckInterfaceToken(data)) {
35         return AVSESSION_ERROR;
36     }
37     SLOGI("cmd code is %{public}d", code);
38     if (code < SESSION_CMD_MAX) {
39         return (this->*handlers[code])(data, reply);
40     }
41     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42 }
43 
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)44 int32_t AVSessionStub::HandleGetSessionId(MessageParcel& data, MessageParcel& reply)
45 {
46     CHECK_AND_PRINT_LOG(reply.WriteString(GetSessionId()), "write int32_t failed");
47     return ERR_NONE;
48 }
49 
HandleRegisterCallbackInner(MessageParcel & data,MessageParcel & reply)50 int32_t AVSessionStub::HandleRegisterCallbackInner(MessageParcel& data, MessageParcel& reply)
51 {
52     auto remoteObject = data.ReadRemoteObject();
53     if (remoteObject == nullptr) {
54         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "write int32_t failed");
55         return ERR_NONE;
56     }
57     auto callback = iface_cast<AVSessionCallbackProxy>(remoteObject);
58     CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_NONE, "callback is nullptr");
59     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(RegisterCallbackInner(callback)),
60                              ERR_NONE, "write int32_t failed");
61     return ERR_NONE;
62 }
63 
HandleDestroy(MessageParcel & data,MessageParcel & reply)64 int32_t AVSessionStub::HandleDestroy(MessageParcel& data, MessageParcel& reply)
65 {
66     int32_t ret = Destroy();
67     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32_t failed");
68     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "Destroy failed");
69     return ERR_NONE;
70 }
71 
HandleGetAVPlaybackState(MessageParcel & data,MessageParcel & reply)72 int32_t AVSessionStub::HandleGetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
73 {
74     AVPlaybackState avPlaybackState;
75     int32_t ret = GetAVPlaybackState(avPlaybackState);
76     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
77     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
78     CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&avPlaybackState), ERR_NONE, "write avPlaybackState failed");
79     return ERR_NONE;
80 }
81 
HandleSetAVPlaybackState(MessageParcel & data,MessageParcel & reply)82 int32_t AVSessionStub::HandleSetAVPlaybackState(MessageParcel& data, MessageParcel& reply)
83 {
84     AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVPlaybackState");
85     sptr avPlaybackState = data.ReadParcelable<AVPlaybackState>();
86     if (avPlaybackState == nullptr) {
87         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
88         return ERR_NONE;
89     }
90     int32_t ret = SetAVPlaybackState(*avPlaybackState);
91     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
92     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
93     return ERR_NONE;
94 }
95 
HandleSetAVMetaData(MessageParcel & data,MessageParcel & reply)96 int32_t AVSessionStub::HandleSetAVMetaData(MessageParcel& data, MessageParcel& reply)
97 {
98     AVSESSION_TRACE_SYNC_START("AVSessionStub::SetAVMetaData");
99     sptr avMetaData = data.ReadParcelable<AVMetaData>();
100     if (avMetaData == nullptr) {
101         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
102         return ERR_NONE;
103     }
104     int32_t ret = SetAVMetaData(*avMetaData);
105     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 result failed");
106     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "GetAVPlaybackState failed");
107     return ERR_NONE;
108 }
109 
HandleSetLaunchAbility(MessageParcel & data,MessageParcel & reply)110 int32_t AVSessionStub::HandleSetLaunchAbility(MessageParcel& data, MessageParcel& reply)
111 {
112     sptr want = data.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
113     if (want == nullptr) {
114         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ERR_UNMARSHALLING), ERR_NONE, "WriteInt32 result failed");
115         return ERR_NONE;
116     }
117     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(SetLaunchAbility(*want)), ERR_NONE, "WriteInt32 result failed");
118     return ERR_NONE;
119 }
120 
HandleGetAVMetaData(MessageParcel & data,MessageParcel & reply)121 int32_t AVSessionStub::HandleGetAVMetaData(MessageParcel& data, MessageParcel& reply)
122 {
123     AVMetaData avMetaData;
124     int32_t ret = GetAVMetaData(avMetaData);
125     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "write int32 failed");
126     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
127     CHECK_AND_RETURN_RET_LOG(reply.WriteParcelable(&avMetaData), ERR_NONE, "write avMetaData failed");
128     return ERR_NONE;
129 }
130 
HandleGetController(MessageParcel & data,MessageParcel & reply)131 int32_t AVSessionStub::HandleGetController(MessageParcel& data, MessageParcel& reply)
132 {
133     sptr<IRemoteObject>  controller = GetControllerInner();
134     if (controller == nullptr) {
135         CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_ERROR), ERR_NONE, "write int32 failed");
136         return ERR_NONE;
137     }
138     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(AVSESSION_SUCCESS), ERR_NONE, "write int32 failed");
139     CHECK_AND_RETURN_RET_LOG(reply.WriteRemoteObject(controller), ERR_NONE, "write object failed");
140     return ERR_NONE;
141 }
142 
HandleActivate(MessageParcel & data,MessageParcel & reply)143 int32_t AVSessionStub::HandleActivate(MessageParcel& data, MessageParcel& reply)
144 {
145     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Activate()), ERR_NONE, "WriteInt32 failed");
146     return ERR_NONE;
147 }
148 
HandleDeactivate(MessageParcel & data,MessageParcel & reply)149 int32_t AVSessionStub::HandleDeactivate(MessageParcel& data, MessageParcel& reply)
150 {
151     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(Deactivate()), ERR_NONE, "WriteInt32 failed");
152     return ERR_NONE;
153 }
154 
HandleIsActive(MessageParcel & data,MessageParcel & reply)155 int32_t AVSessionStub::HandleIsActive(MessageParcel& data, MessageParcel& reply)
156 {
157     CHECK_AND_RETURN_RET_LOG(reply.WriteBool(IsActive()), ERR_NONE, "WriteBool failed");
158     return ERR_NONE;
159 }
160 
HandleAddSupportCommand(MessageParcel & data,MessageParcel & reply)161 int32_t AVSessionStub::HandleAddSupportCommand(MessageParcel& data, MessageParcel& reply)
162 {
163     int32_t ret = AddSupportCommand(data.ReadInt32());
164     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
165     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
166     return ERR_NONE;
167 }
168 
HandleDeleteSupportCommand(MessageParcel & data,MessageParcel & reply)169 int32_t AVSessionStub::HandleDeleteSupportCommand(MessageParcel& data, MessageParcel& reply)
170 {
171     int32_t ret = DeleteSupportCommand(data.ReadInt32());
172     CHECK_AND_RETURN_RET_LOG(reply.WriteInt32(ret), ERR_NONE, "WriteInt32 failed");
173     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ERR_NONE, "GetAVMetaData failed");
174     return ERR_NONE;
175 }
176 }