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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioProcessStub"
17 #endif
18 
19 #include "audio_process_stub.h"
20 #include "audio_service_log.h"
21 #include "audio_errors.h"
22 #include "audio_utils.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
ProcessCbProxy(const sptr<IRemoteObject> & impl)26 ProcessCbProxy::ProcessCbProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IProcessCb>(impl)
27 {
28 }
29 
~ProcessCbProxy()30 ProcessCbProxy::~ProcessCbProxy()
31 {
32 }
33 
OnEndpointChange(int32_t status)34 int32_t ProcessCbProxy::OnEndpointChange(int32_t status)
35 {
36     MessageParcel data;
37     MessageParcel reply;
38     MessageOption option;
39 
40     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERR_OPERATION_FAILED,
41         "Write descriptor failed!");
42 
43     data.WriteInt32(status);
44     int ret = Remote()->SendRequest(IProcessCbMsg::ON_ENDPOINT_CHANGE, data, reply, option);
45     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "OnEndpointChange failed, error: %{public}d", ret);
46     return reply.ReadInt32();
47 }
48 
CheckInterfaceToken(MessageParcel & data)49 bool AudioProcessStub::CheckInterfaceToken(MessageParcel &data)
50 {
51     static auto localDescriptor = IAudioProcess::GetDescriptor();
52     auto remoteDescriptor = data.ReadInterfaceToken();
53     CHECK_AND_RETURN_RET_LOG(remoteDescriptor == localDescriptor, false, "CheckInterFfaceToken failed.");
54     return true;
55 }
56 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int AudioProcessStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
58 {
59     bool ret = CheckInterfaceToken(data);
60     CHECK_AND_RETURN_RET(ret, AUDIO_ERR);
61     Trace trace("AudioProcess::Handle::" + std::to_string(code));
62     if (code >= IAudioProcessMsg::PROCESS_MAX_MSG) {
63         AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
64         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
65     }
66     switch (code) {
67         case ON_RESOLVE_BUFFER:
68             return HandleResolveBuffer(data, reply);
69         case OH_GET_SESSIONID:
70             return HandleGetSessionId(data, reply);
71         case ON_START:
72             return HandleStart(data, reply);
73         case ON_PAUSE:
74             return HandlePause(data, reply);
75         case ON_RESUME:
76             return HandleResume(data, reply);
77         case ON_STOP:
78             return HandleStop(data, reply);
79         case ON_REQUEST_HANDLE_INFO:
80             return HandleRequestHandleInfo(data, reply);
81         case ON_RELEASE:
82             return HandleRelease(data, reply);
83         case ON_REGISTER_PROCESS_CB:
84             return HandleRegisterProcessCb(data, reply);
85         case ON_REGISTER_THREAD_PRIORITY:
86             return HandleRegisterThreadPriority(data, reply);
87         case ON_SET_SLITNT_MODE_AND_MIX_WITH_OTHERS:
88             return HandleSetSlientModeAndMixWithOther(data, reply);
89         default:
90             AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
91             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
92 }
93 }
94 
HandleResolveBuffer(MessageParcel & data,MessageParcel & reply)95 int32_t AudioProcessStub::HandleResolveBuffer(MessageParcel &data, MessageParcel &reply)
96 {
97     AUDIO_INFO_LOG("HandleResolveBuffer");
98     (void)data;
99     std::shared_ptr<OHAudioBuffer> buffer;
100     int32_t ret = ResolveBuffer(buffer);
101     reply.WriteInt32(ret);
102     if (ret == AUDIO_OK && buffer != nullptr) {
103         OHAudioBuffer::WriteToParcel(buffer, reply);
104     } else {
105         AUDIO_ERR_LOG("error: ResolveBuffer failed.");
106         return AUDIO_INVALID_PARAM;
107     }
108 
109     return AUDIO_OK;
110 }
111 
HandleGetSessionId(MessageParcel & data,MessageParcel & reply)112 int32_t AudioProcessStub::HandleGetSessionId(MessageParcel &data, MessageParcel &reply)
113 {
114     (void)data;
115     uint32_t sessionId = 0;
116     int32_t ret = GetSessionId(sessionId);
117     reply.WriteInt32(ret);
118     reply.WriteUint32(sessionId);
119     return AUDIO_OK;
120 }
121 
HandleStart(MessageParcel & data,MessageParcel & reply)122 int32_t AudioProcessStub::HandleStart(MessageParcel &data, MessageParcel &reply)
123 {
124     (void)data;
125     reply.WriteInt32(Start());
126     return AUDIO_OK;
127 }
128 
HandleResume(MessageParcel & data,MessageParcel & reply)129 int32_t AudioProcessStub::HandleResume(MessageParcel &data, MessageParcel &reply)
130 {
131     (void)data;
132     reply.WriteInt32(Resume());
133     return AUDIO_OK;
134 }
135 
HandlePause(MessageParcel & data,MessageParcel & reply)136 int32_t AudioProcessStub::HandlePause(MessageParcel &data, MessageParcel &reply)
137 {
138     bool isFlush = data.ReadBool();
139     reply.WriteInt32(Pause(isFlush));
140     return AUDIO_OK;
141 }
142 
HandleStop(MessageParcel & data,MessageParcel & reply)143 int32_t AudioProcessStub::HandleStop(MessageParcel &data, MessageParcel &reply)
144 {
145     (void)data;
146     reply.WriteInt32(Stop());
147     return AUDIO_OK;
148 }
149 
HandleRequestHandleInfo(MessageParcel & data,MessageParcel & reply)150 int32_t AudioProcessStub::HandleRequestHandleInfo(MessageParcel &data, MessageParcel &reply)
151 {
152     (void)data;
153     reply.WriteInt32(RequestHandleInfo());
154     return AUDIO_OK;
155 }
156 
HandleRelease(MessageParcel & data,MessageParcel & reply)157 int32_t AudioProcessStub::HandleRelease(MessageParcel &data, MessageParcel &reply)
158 {
159     bool isSwitchStream = data.ReadBool();
160     reply.WriteInt32(Release(isSwitchStream));
161     return AUDIO_OK;
162 }
163 
HandleRegisterProcessCb(MessageParcel & data,MessageParcel & reply)164 int32_t AudioProcessStub::HandleRegisterProcessCb(MessageParcel &data, MessageParcel &reply)
165 {
166     sptr<IRemoteObject> object = data.ReadRemoteObject();
167     CHECK_AND_RETURN_RET_LOG(object != nullptr, AUDIO_INVALID_PARAM, "obj is null");
168     reply.WriteInt32(RegisterProcessCb(object));
169     return AUDIO_OK;
170 }
171 
HandleRegisterThreadPriority(MessageParcel & data,MessageParcel & reply)172 int32_t AudioProcessStub::HandleRegisterThreadPriority(MessageParcel &data, MessageParcel &reply)
173 {
174     uint32_t tid = data.ReadUint32();
175     std::string bundleName = data.ReadString();
176     reply.WriteInt32(RegisterThreadPriority(tid, bundleName));
177     return AUDIO_OK;
178 }
179 
HandleSetSlientModeAndMixWithOther(MessageParcel & data,MessageParcel & reply)180 int32_t AudioProcessStub::HandleSetSlientModeAndMixWithOther(MessageParcel &data, MessageParcel &reply)
181 {
182     bool on = data.ReadBool();
183     reply.WriteInt32(SetSilentModeAndMixWithOthers(on));
184     return AUDIO_OK;
185 }
186 } // namespace AudioStandard
187 } // namespace OHOS
188