1 /*
2  * Copyright (c) 2021-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 #include <ohos_errno.h>
16 #include <ohos_init.h>
17 #include "audio_capturer_server.h"
18 #include "feature.h"
19 #include "iproxy_server.h"
20 #include "media_log.h"
21 #include "samgr_lite.h"
22 #include "service.h"
23 
24 namespace OHOS {
25 namespace Audio {
26 struct DefaultFeatureApi {
27     INHERIT_SERVER_IPROXY;
28 };
29 
30 struct AudioCapturerService {
31     INHERIT_SERVICE;
32     INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
33     Identity identity;
34 };
35 
GetName(Service * service)36 static const char *GetName(Service *service)
37 {
38     (void)service;
39     return AUDIO_CAPTURER_SERVICE_NAME;
40 }
41 
Initialize(Service * service,Identity identity)42 static BOOL Initialize(Service *service, Identity identity)
43 {
44     if (service == nullptr) {
45         MEDIA_ERR_LOG("Initialize: service is nullptr");
46         return FALSE;
47     }
48 
49     AudioCapturerService *capturerSvc = reinterpret_cast<AudioCapturerService*>(service);
50     capturerSvc->identity = identity;
51     MEDIA_DEBUG_LOG("Initialize(%s)! Identity<%d, %d>",
52                     AUDIO_CAPTURER_SERVICE_NAME, identity.serviceId,
53                     identity.featureId);
54     return TRUE;
55 }
56 
MessageHandle(Service * service,Request * msg)57 static BOOL MessageHandle(Service *service, Request *msg)
58 {
59     if (service == nullptr || msg == nullptr) {
60         MEDIA_ERR_LOG("MessageHandle: service or msg is nullptr");
61         return FALSE;
62     }
63 
64     MEDIA_DEBUG_LOG("MessageHandle(%s)! Request<%d, %d>", service->GetName(service),
65                     msg->msgId, msg->msgValue);
66     return FALSE;
67 }
68 
GetTaskConfig(Service * service)69 static TaskConfig GetTaskConfig(Service *service)
70 {
71     (void)service;
72     TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK};
73     return config;
74 }
75 
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)76 static int32 Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
77 {
78     if (origin == nullptr) {
79         MEDIA_ERR_LOG("Invoke: origin is nullptr");
80         return FALSE;
81     }
82 
83     pid_t pid = GetCallingPid();
84     AudioCapturerServer *mng = AudioCapturerServer::GetInstance();
85     if (mng == nullptr) {
86         MEDIA_ERR_LOG("Invoke failed, mng is nullptr");
87         return FALSE;
88     }
89 
90     mng->Dispatch(funcId, pid, req, reply);
91     return EC_SUCCESS;
92 }
93 
AudioCapturerServiceReg()94 void AudioCapturerServiceReg()
95 {
96     static AudioCapturerService audioCapturerSvc = {
97         .GetName = GetName,
98         .Initialize = Initialize,
99         .MessageHandle = MessageHandle,
100         .GetTaskConfig = GetTaskConfig,
101         SERVER_IPROXY_IMPL_BEGIN,
102         .Invoke = Invoke,
103         IPROXY_END,
104     };
105     MEDIA_INFO_LOG("Input AudioCapturerServiceReg");
106     bool ret = SAMGR_GetInstance()->RegisterService(reinterpret_cast<Service*>(&audioCapturerSvc));
107     if (!ret) {
108         MEDIA_ERR_LOG("AudioCapturer register service failed.");
109         return;
110     }
111     ret = SAMGR_GetInstance()->RegisterDefaultFeatureApi(AUDIO_CAPTURER_SERVICE_NAME, GET_IUNKNOWN(audioCapturerSvc));
112     if (!ret) {
113         MEDIA_ERR_LOG("AudioCapturer register feature failed.");
114         return;
115     }
116 }
117 }  // namespace Audio
118 }  // namespace OHOS
119