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_dev_ctrl_manager.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20
21 #include "audio_ctrl_transport.h"
22 #include "audio_param.h"
23 #include "daudio_constants.h"
24 #include "daudio_errorcode.h"
25 #include "daudio_log.h"
26 #include "daudio_util.h"
27
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "DAudioSinkDevCtrlMgr"
30
31 namespace OHOS {
32 namespace DistributedHardware {
DAudioSinkDevCtrlMgr(const std::string & devId,std::shared_ptr<IAudioEventCallback> audioEventCallback)33 DAudioSinkDevCtrlMgr::DAudioSinkDevCtrlMgr(const std::string &devId,
34 std::shared_ptr<IAudioEventCallback> audioEventCallback)
35 : devId_(devId), audioEventCallback_(audioEventCallback)
36 {
37 DHLOGD("Control manager constructed.");
38 }
39
~DAudioSinkDevCtrlMgr()40 DAudioSinkDevCtrlMgr::~DAudioSinkDevCtrlMgr()
41 {
42 DHLOGD("Control manager deconstructed.");
43 }
44
OnStateChange(int32_t type)45 void DAudioSinkDevCtrlMgr::OnStateChange(int32_t type)
46 {
47 DHLOGD("Distributed audio sink device control manager state change, type: %d.", type);
48 switch (type) {
49 case AudioEventType::CTRL_OPENED:
50 isOpened_.store(true);
51 break;
52 case AudioEventType::CTRL_CLOSED:
53 isOpened_.store(false);
54 break;
55 default:
56 DHLOGE("Invalid parameter type, type: %d.", type);
57 return;
58 }
59
60 auto callback = audioEventCallback_.lock();
61 if (callback == nullptr) {
62 DHLOGE("Callback is nullptr.");
63 return;
64 }
65 AudioEvent event(static_cast<AudioEventType>(type), "");
66 callback->NotifyEvent(event);
67 }
68
SetUp()69 int32_t DAudioSinkDevCtrlMgr::SetUp()
70 {
71 DHLOGI("Set up sink device control manager.");
72 if (audioCtrlTrans_ == nullptr) {
73 audioCtrlTrans_ = std::make_shared<AudioCtrlTransport>(devId_);
74 }
75
76 int32_t ret = audioCtrlTrans_->SetUp(shared_from_this());
77 if (ret != DH_SUCCESS) {
78 DHLOGE("Ctrl trans setup failed, ret: %d.", ret);
79 return ret;
80 }
81 return DH_SUCCESS;
82 }
83
Start()84 int32_t DAudioSinkDevCtrlMgr::Start()
85 {
86 DHLOGI("Start sink device control manager.");
87 return DH_SUCCESS;
88 }
89
Stop()90 int32_t DAudioSinkDevCtrlMgr::Stop()
91 {
92 DHLOGI("Stop sink device control manager.");
93 isOpened_.store(false);
94 if (audioCtrlTrans_ == nullptr) {
95 DHLOGD("Ctrl trans already stop.");
96 return DH_SUCCESS;
97 }
98
99 int32_t ret = audioCtrlTrans_->Stop();
100 if (ret != DH_SUCCESS) {
101 DHLOGE("Ctrl trans stop failed, ret: %d.", ret);
102 return ret;
103 }
104 return DH_SUCCESS;
105 }
106
Release()107 int32_t DAudioSinkDevCtrlMgr::Release()
108 {
109 DHLOGI("Release sink device control manager.");
110 if (audioCtrlTrans_ == nullptr) {
111 DHLOGD("Ctrl trans already release.");
112 return DH_SUCCESS;
113 }
114 int32_t ret = audioCtrlTrans_->Release();
115 if (ret != DH_SUCCESS) {
116 DHLOGE("Ctrl trans release failed, ret: %d.", ret);
117 return ret;
118 }
119 audioCtrlTrans_ = nullptr;
120 return DH_SUCCESS;
121 }
122
IsOpened()123 bool DAudioSinkDevCtrlMgr::IsOpened()
124 {
125 return isOpened_.load();
126 }
127
OnEventReceived(const AudioEvent & event)128 void DAudioSinkDevCtrlMgr::OnEventReceived(const AudioEvent &event)
129 {
130 DHLOGD("Received event type %d.", event.type);
131 auto callback = audioEventCallback_.lock();
132 if (callback == nullptr) {
133 DHLOGE("Callback is nullptr.");
134 return;
135 }
136 callback->NotifyEvent(event);
137 }
138
SendAudioEvent(const AudioEvent & event)139 int32_t DAudioSinkDevCtrlMgr::SendAudioEvent(const AudioEvent &event)
140 {
141 DHLOGD("Send audio event.");
142 if (audioCtrlTrans_ == nullptr) {
143 return ERR_DH_AUDIO_SA_SINK_CTRL_TRANS_NULL;
144 }
145 int32_t ret = audioCtrlTrans_->SendAudioEvent(event);
146 if (ret != DH_SUCCESS) {
147 DHLOGE("Audio control transfer sending audio event error,ret: %d.", ret);
148 return ret;
149 }
150 return DH_SUCCESS;
151 }
152 } // namespace DistributedHardware
153 } // namespace OHOS
154