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 "daudio_sink_service.h"
17
18 #include "if_system_ability_manager.h"
19 #include "ipc_skeleton.h"
20 #include "ipc_types.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24
25 #include "daudio_constants.h"
26 #include "daudio_errorcode.h"
27 #include "daudio_hisysevent.h"
28 #include "daudio_log.h"
29 #include "daudio_sink_manager.h"
30 #include "daudio_util.h"
31
32 #undef DH_LOG_TAG
33 #define DH_LOG_TAG "DAudioSinkService"
34
35 namespace OHOS {
36 namespace DistributedHardware {
37 REGISTER_SYSTEM_ABILITY_BY_ID(DAudioSinkService, DISTRIBUTED_HARDWARE_AUDIO_SINK_SA_ID, true);
38
DAudioSinkService(int32_t saId,bool runOnCreate)39 DAudioSinkService::DAudioSinkService(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
40 {
41 DHLOGD("Distributed audio sink service constructed.");
42 }
43
OnStart()44 void DAudioSinkService::OnStart()
45 {
46 DHLOGI("Distributed audio service on start.");
47 if (!Init()) {
48 DHLOGE("Init service failed.");
49 return;
50 }
51 DHLOGI("Start distributed audio service success.");
52 }
53
OnStop()54 void DAudioSinkService::OnStop()
55 {
56 DHLOGI("Distributed audio service on stop.");
57 isServiceStarted_ = false;
58 }
59
Init()60 bool DAudioSinkService::Init()
61 {
62 DHLOGI("Start init distributed audio service.");
63 if (!isServiceStarted_) {
64 bool ret = Publish(this);
65 if (!ret) {
66 DHLOGE("Publish service failed.");
67 return false;
68 }
69 isServiceStarted_ = true;
70 }
71 DHLOGI("Init distributed audio service success.");
72 return true;
73 }
74
InitSink(const std::string & params)75 int32_t DAudioSinkService::InitSink(const std::string ¶ms)
76 {
77 DAudioSinkManager::GetInstance().Init();
78 return DH_SUCCESS;
79 }
80
ReleaseSink()81 int32_t DAudioSinkService::ReleaseSink()
82 {
83 DHLOGI("Release sink service.");
84 DAudioSinkManager::GetInstance().UnInit();
85 DHLOGI("Audio sink service process exit.");
86 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
87 if (systemAbilityMgr == nullptr) {
88 DHLOGE("Failed to get systemabilitymanager.");
89 return ERR_DH_AUDIO_BAD_VALUE;
90 }
91 int32_t ret = systemAbilityMgr->UnloadSystemAbility(DISTRIBUTED_HARDWARE_AUDIO_SINK_SA_ID);
92 if (ret != DH_SUCCESS) {
93 DHLOGE("Sink systemabilitymgr unloadsystemability failed, ret: %d", ret);
94 return ERR_DH_AUDIO_BAD_VALUE;
95 }
96 DHLOGI("Sink systemabilitymgr unloadsystemability successfully!");
97 return DH_SUCCESS;
98 }
99
SubscribeLocalHardware(const std::string & dhId,const std::string & param)100 int32_t DAudioSinkService::SubscribeLocalHardware(const std::string &dhId, const std::string ¶m)
101 {
102 DHLOGI("Subscribe local hardware.");
103 return DH_SUCCESS;
104 }
105
UnsubscribeLocalHardware(const std::string & dhId)106 int32_t DAudioSinkService::UnsubscribeLocalHardware(const std::string &dhId)
107 {
108 DHLOGI("Unsubscribe local hardware.");
109 return DH_SUCCESS;
110 }
111
DAudioNotify(const std::string & devId,const std::string & dhId,const int32_t eventType,const std::string & eventContent)112 void DAudioSinkService::DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
113 const std::string &eventContent)
114 {
115 DHLOGI("DAudioNotify devId:%s, dhId:%s, eventType:%d.", GetAnonyString(devId).c_str(),
116 dhId.c_str(), eventType);
117 DAudioSinkManager::GetInstance().HandleDAudioNotify(devId, dhId, eventType, eventContent);
118 }
119 } // namespace DistributedHardware
120 } // namespace OHOS