• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "audio_process_cb_stub.h"
17 #include "audio_process_proxy.h"
18 #include "audio_log.h"
19 #include "audio_errors.h"
20 
21 namespace OHOS {
22 namespace AudioStandard {
CheckInterfaceToken(MessageParcel & data)23 bool ProcessCbStub::CheckInterfaceToken(MessageParcel &data)
24 {
25     static auto localDescriptor = IProcessCb::GetDescriptor();
26     auto remoteDescriptor = data.ReadInterfaceToken();
27     if (remoteDescriptor != localDescriptor) {
28         AUDIO_ERR_LOG("CheckInterFfaceToken failed.");
29         return false;
30     }
31     return true;
32 }
33 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 int ProcessCbStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     if (!CheckInterfaceToken(data)) {
37         return AUDIO_ERR;
38     }
39     if (code >= IProcessCbMsg::PROCESS_CB_MAX_MSG) {
40         AUDIO_WARNING_LOG("OnRemoteRequest unsupported request code:%{public}d.", code);
41         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42     }
43     return (this->*funcList_[code])(data, reply);
44 }
45 
HandleOnEndpointChange(MessageParcel & data,MessageParcel & reply)46 int32_t ProcessCbStub::HandleOnEndpointChange(MessageParcel &data, MessageParcel &reply)
47 {
48     int32_t status = data.ReadInt32();
49     reply.WriteInt32(OnEndpointChange(status));
50     return AUDIO_OK;
51 }
52 
AudioProcessProxy(const sptr<IRemoteObject> & impl)53 AudioProcessProxy::AudioProcessProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAudioProcess>(impl)
54 {
55     AUDIO_INFO_LOG("AudioProcessProxy()");
56 }
57 
~AudioProcessProxy()58 AudioProcessProxy::~AudioProcessProxy()
59 {
60     AUDIO_INFO_LOG("~AudioProcessProxy()");
61 }
62 
ResolveBuffer(std::shared_ptr<OHAudioBuffer> & buffer)63 int32_t AudioProcessProxy::ResolveBuffer(std::shared_ptr<OHAudioBuffer> &buffer)
64 {
65     MessageParcel data;
66     MessageParcel reply;
67     MessageOption option;
68 
69     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
70 
71     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_RESOLVE_BUFFER, data, reply, option);
72     CHECK_AND_RETURN_RET_LOG((ret == AUDIO_OK && reply.ReadInt32() == AUDIO_OK), ERR_OPERATION_FAILED,
73         "ResolveBuffer failed, error: %{public}d", ret);
74     buffer = OHAudioBuffer::ReadFromParcel(reply);
75     CHECK_AND_RETURN_RET_LOG(buffer != nullptr, ERR_OPERATION_FAILED, "ReadFromParcel failed");
76     return SUCCESS;
77 }
78 
Start()79 int32_t AudioProcessProxy::Start()
80 {
81     MessageParcel data;
82     MessageParcel reply;
83     MessageOption option;
84 
85     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
86 
87     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_START, data, reply, option);
88     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Start failed, error: %{public}d", ret);
89 
90     return reply.ReadInt32();
91 }
92 
Pause(bool isFlush)93 int32_t AudioProcessProxy::Pause(bool isFlush)
94 {
95     MessageParcel data;
96     MessageParcel reply;
97     MessageOption option;
98 
99     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
100     data.WriteBool(isFlush);
101     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_PAUSE, data, reply, option);
102     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Pause failed, error: %{public}d", ret);
103 
104     return reply.ReadInt32();
105 }
106 
Resume()107 int32_t AudioProcessProxy::Resume()
108 {
109     MessageParcel data;
110     MessageParcel reply;
111     MessageOption option;
112 
113     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
114 
115     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_RESUME, data, reply, option);
116     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Resume failed, error: %{public}d", ret);
117 
118     return reply.ReadInt32();
119 }
120 
Stop()121 int32_t AudioProcessProxy::Stop()
122 {
123     MessageParcel data;
124     MessageParcel reply;
125     MessageOption option;
126 
127     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
128 
129     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_STOP, data, reply, option);
130     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Stop failed, error: %{public}d", ret);
131 
132     return reply.ReadInt32();
133 }
134 
RequestHandleInfo()135 int32_t AudioProcessProxy::RequestHandleInfo()
136 {
137     MessageParcel data;
138     MessageParcel reply;
139     MessageOption option(MessageOption::TF_ASYNC); // MessageOption::TF_ASYNC for no delay
140 
141     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
142 
143     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_REQUEST_HANDLE_INFO, data, reply, option);
144     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "RequestHandleInfo failed: %{public}d", ret);
145 
146     return reply.ReadInt32();
147 }
148 
Release()149 int32_t AudioProcessProxy::Release()
150 {
151     MessageParcel data;
152     MessageParcel reply;
153     MessageOption option;
154 
155     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
156 
157     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_RELEASE, data, reply, option);
158     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "Release failed, error: %{public}d", ret);
159 
160     return reply.ReadInt32();
161 }
162 
RegisterProcessCb(sptr<IRemoteObject> object)163 int32_t AudioProcessProxy::RegisterProcessCb(sptr<IRemoteObject> object)
164 {
165     MessageParcel data;
166     MessageParcel reply;
167     MessageOption option;
168 
169     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), ERROR, "Write descriptor failed!");
170 
171     if (object == nullptr) {
172         AUDIO_ERR_LOG("RegisterProcessCb object is null");
173         return ERR_NULL_OBJECT;
174     }
175 
176     data.WriteRemoteObject(object);
177 
178     int ret = Remote()->SendRequest(IAudioProcessMsg::ON_REGISTER_PROCESS_CB, data, reply, option);
179     CHECK_AND_RETURN_RET_LOG(ret == AUDIO_OK, ERR_OPERATION_FAILED, "RegisterProcessCb failed, error: %{public}d", ret);
180 
181     return reply.ReadInt32();
182 }
183 } // namespace AudioStandard
184 } // namespace OHOS
185