• 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_service_proxy.h"
17 #include "avsession_log.h"
18 #include "avsession_proxy.h"
19 #include "avsession_controller_proxy.h"
20 
21 namespace OHOS::AVSession {
AVSessionServiceProxy(const sptr<IRemoteObject> & impl)22 AVSessionServiceProxy::AVSessionServiceProxy(const sptr<IRemoteObject>& impl)
23     : IRemoteProxy<IAVSessionService>(impl)
24 {
25     SLOGI("constructor");
26 }
27 
CreateSession(const std::string & tag,int32_t type,const AppExecFwk::ElementName & elementName)28 std::shared_ptr<AVSession> AVSessionServiceProxy::CreateSession(const std::string& tag, int32_t type,
29                                                                 const AppExecFwk::ElementName& elementName)
30 {
31     auto object = CreateSessionInner(tag, type, elementName);
32     if (object == nullptr) {
33         SLOGI("object is nullptr");
34         return nullptr;
35     }
36     auto session = iface_cast<AVSessionProxy>(object);
37     if (session == nullptr) {
38         SLOGI("session is nullptr");
39         return nullptr;
40     }
41     return std::shared_ptr<AVSession>(session.GetRefPtr(), [holder = session](const auto*) {});
42 }
43 
CreateSessionInner(const std::string & tag,int32_t type,const AppExecFwk::ElementName & elementName)44 sptr<IRemoteObject> AVSessionServiceProxy::CreateSessionInner(const std::string& tag, int32_t type,
45                                                               const AppExecFwk::ElementName& elementName)
46 {
47     MessageParcel data;
48     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), nullptr, "write interface token failed");
49     CHECK_AND_RETURN_RET_LOG(data.WriteString(tag), nullptr, "write tag failed");
50     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(type), nullptr, "write type failed");
51     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&elementName), nullptr, "write bundleName failed");
52 
53     auto remote = Remote();
54     CHECK_AND_RETURN_RET_LOG(remote != nullptr, nullptr, "get remote service failed");
55     MessageParcel reply;
56     MessageOption option;
57     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_CREATE_SESSION, data, reply, option) == 0,
58                              nullptr, "send request failed");
59 
60     int32_t res = AVSESSION_ERROR;
61     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(res), nullptr, "read res failed");
62 
63     return res == AVSESSION_SUCCESS ? reply.ReadRemoteObject() : nullptr;
64 }
65 
GetAllSessionDescriptors(std::vector<AVSessionDescriptor> & descriptors)66 int32_t AVSessionServiceProxy::GetAllSessionDescriptors(std::vector<AVSessionDescriptor>& descriptors)
67 {
68     MessageParcel data;
69     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
70                              "write interface token failed");
71     auto remote = Remote();
72     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
73     MessageParcel reply;
74     MessageOption option;
75     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_GET_ALL_SESSION_DESCRIPTORS, data, reply, option) == 0,
76                              ERR_IPC_SEND_REQUEST, "send request failed");
77 
78     int32_t ret = AVSESSION_ERROR;
79     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
80     if (ret == AVSESSION_SUCCESS) {
81         uint32_t size {};
82         CHECK_AND_RETURN_RET_LOG(reply.ReadUint32(size), ERR_UNMARSHALLING, "read vector size failed");
83         CHECK_AND_RETURN_RET_LOG(size, ret, "size=0");
84 
85         std::vector<AVSessionDescriptor> result(size);
86         for (auto& descriptor : result) {
87             CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
88         }
89         descriptors = result;
90     }
91     return ret;
92 }
93 
GetSessionDescriptorsBySessionId(const std::string & sessionId,AVSessionDescriptor & descriptor)94 int32_t AVSessionServiceProxy::GetSessionDescriptorsBySessionId(const std::string& sessionId,
95     AVSessionDescriptor& descriptor)
96 {
97     MessageParcel data;
98     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
99         "write interface token failed");
100     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_MARSHALLING, "write sessionId failed");
101 
102     auto remote = Remote();
103     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
104     MessageParcel reply;
105     MessageOption option;
106     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_GET_SESSION_DESCRIPTORS_BY_ID, data, reply, option) == 0,
107         ERR_IPC_SEND_REQUEST, "send request failed");
108 
109     int32_t ret = AVSESSION_ERROR;
110     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
111     if (ret == AVSESSION_SUCCESS) {
112         CHECK_AND_RETURN_RET_LOG(descriptor.ReadFromParcel(reply), ERR_UNMARSHALLING, "read descriptor failed");
113     }
114     return ret;
115 }
116 
CreateController(const std::string & sessionId,std::shared_ptr<AVSessionController> & controller)117 int32_t AVSessionServiceProxy::CreateController(const std::string& sessionId,
118     std::shared_ptr<AVSessionController>& controller)
119 {
120     sptr<IRemoteObject> object;
121     auto ret = CreateControllerInner(sessionId, object);
122     CHECK_AND_RETURN_RET_LOG(ret == AVSESSION_SUCCESS, ret, "CreateControllerInner failed");
123 
124     auto controllerObject = iface_cast<AVSessionControllerProxy>(object);
125     CHECK_AND_RETURN_RET_LOG(controllerObject, AVSESSION_ERROR, "controllerObject is nullptr");
126 
127     controller = std::shared_ptr<AVSessionController>(controllerObject.GetRefPtr(),
128         [holder = controllerObject](const auto*) {});
129     return ret;
130 }
131 
CreateControllerInner(const std::string & sessionId,sptr<IRemoteObject> & object)132 int32_t AVSessionServiceProxy::CreateControllerInner(const std::string& sessionId, sptr<IRemoteObject>& object)
133 {
134     MessageParcel data;
135     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_UNMARSHALLING,
136                              "write interface token failed");
137     CHECK_AND_RETURN_RET_LOG(data.WriteString(sessionId), ERR_UNMARSHALLING, "write sessionId failed");
138 
139     auto remote = Remote();
140     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
141     MessageParcel reply;
142     MessageOption option;
143     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_CREATE_CONTROLLER, data, reply, option) == 0,
144                              ERR_IPC_SEND_REQUEST, "send request failed");
145     int32_t ret = AVSESSION_ERROR;
146     CHECK_AND_RETURN_RET_LOG(reply.ReadInt32(ret), ERR_UNMARSHALLING, "read int32 failed");
147     if (ret == AVSESSION_SUCCESS) {
148         object = reply.ReadRemoteObject();
149     }
150     return ret;
151 }
152 
RegisterSessionListener(const sptr<ISessionListener> & listener)153 int32_t AVSessionServiceProxy::RegisterSessionListener(const sptr<ISessionListener>& listener)
154 {
155     MessageParcel data;
156     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
157                              "write interface token failed");
158     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(listener->AsObject()), ERR_MARSHALLING, "write tag failed");
159 
160     auto remote = Remote();
161     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
162     MessageParcel reply;
163     MessageOption option;
164     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_REGISTER_SESSION_LISTENER, data, reply, option) == 0,
165                              ERR_IPC_SEND_REQUEST, "send request failed");
166     int32_t res = AVSESSION_ERROR;
167     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
168 }
169 
SendSystemAVKeyEvent(const MMI::KeyEvent & keyEvent)170 int32_t AVSessionServiceProxy::SendSystemAVKeyEvent(const MMI::KeyEvent& keyEvent)
171 {
172     MessageParcel data;
173     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
174                              "write interface token failed");
175     CHECK_AND_RETURN_RET_LOG(keyEvent.WriteToParcel(data), ERR_MARSHALLING, "write keyEvent failed");
176 
177     auto remote = Remote();
178     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
179     MessageParcel reply;
180     MessageOption option;
181     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_SEND_SYSTEM_AV_KEY_EVENT, data, reply, option) == 0,
182                              ERR_IPC_SEND_REQUEST, "send request failed");
183     int32_t res = AVSESSION_ERROR;
184     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
185 }
186 
SendSystemControlCommand(const AVControlCommand & command)187 int32_t AVSessionServiceProxy::SendSystemControlCommand(const AVControlCommand& command)
188 {
189     MessageParcel data;
190     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
191                              "write interface token failed");
192     CHECK_AND_RETURN_RET_LOG(data.WriteParcelable(&command), ERR_MARSHALLING, "write keyEvent failed");
193 
194     auto remote = Remote();
195     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
196     MessageParcel reply;
197     MessageOption option;
198     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_SEND_SYSTEM_CONTROL_COMMAND, data, reply, option) == 0,
199                              ERR_IPC_SEND_REQUEST, "send request failed");
200     int32_t res = AVSESSION_ERROR;
201     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
202 }
203 
RegisterClientDeathObserver(const sptr<IClientDeath> & observer)204 int32_t AVSessionServiceProxy::RegisterClientDeathObserver(const sptr<IClientDeath>& observer)
205 {
206     MessageParcel data;
207     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
208                              "write interface token failed");
209     CHECK_AND_RETURN_RET_LOG(data.WriteRemoteObject(observer->AsObject()), ERR_MARSHALLING, "write observer failed");
210 
211     auto remote = Remote();
212     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
213     MessageParcel reply;
214     MessageOption option;
215     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_REGISTER_CLIENT_DEATH, data, reply, option) == 0,
216                              ERR_IPC_SEND_REQUEST, "send request failed");
217     int32_t res = AVSESSION_ERROR;
218     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
219 }
220 
CastAudio(const SessionToken & token,const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)221 int32_t AVSessionServiceProxy::CastAudio(const SessionToken& token,
222                                          const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
223 {
224     MessageParcel data;
225     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
226                              "write interface token failed");
227     CHECK_AND_RETURN_RET_LOG(data.WriteString(token.sessionId), ERR_MARSHALLING, "write sessionId failed");
228     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.pid), ERR_MARSHALLING, "write pid failed");
229     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(token.uid), ERR_MARSHALLING, "write uid failed");
230     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
231                              "write descriptors size failed");
232     for (auto descriptor : descriptors) {
233         SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
234               static_cast<int32_t>(descriptor.deviceRole_));
235         CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
236     }
237 
238     auto remote = Remote();
239     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
240     MessageParcel reply;
241     MessageOption option;
242     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_CAST_AUDIO, data, reply, option) == 0,
243                              ERR_IPC_SEND_REQUEST, "send request failed");
244     int32_t res = AVSESSION_ERROR;
245     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
246 }
247 
CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor> & descriptors)248 int32_t AVSessionServiceProxy::CastAudioForAll(const std::vector<AudioStandard::AudioDeviceDescriptor>& descriptors)
249 {
250     MessageParcel data;
251     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_MARSHALLING,
252                              "write interface token failed");
253     CHECK_AND_RETURN_RET_LOG(data.WriteInt32(static_cast<int32_t>(descriptors.size())), ERR_MARSHALLING,
254                              "write descriptors size failed");
255     for (auto descriptor : descriptors) {
256         SLOGI("networkId_: %{public}.6s, role %{public}d", descriptor.networkId_.c_str(),
257               static_cast<int32_t>(descriptor.deviceRole_));
258         CHECK_AND_RETURN_RET_LOG(descriptor.Marshalling(data), ERR_MARSHALLING, "write descriptor failed");
259     }
260 
261     auto remote = Remote();
262     CHECK_AND_RETURN_RET_LOG(remote != nullptr, ERR_SERVICE_NOT_EXIST, "get remote service failed");
263     MessageParcel reply;
264     MessageOption option;
265     CHECK_AND_RETURN_RET_LOG(remote->SendRequest(SERVICE_CMD_CAST_AUDIO_FOR_ALL, data, reply, option) == 0,
266                              ERR_IPC_SEND_REQUEST, "send request failed");
267     int32_t res = AVSESSION_ERROR;
268     return reply.ReadInt32(res) ? res : AVSESSION_ERROR;
269 }
270 } // namespace OHOS::AVSession