1 /*
2 * Copyright (c) 2025 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 #ifndef LOG_TAG
17 #define LOG_TAG "HdiAdapterFactory"
18 #endif
19
20 #include "manager/hdi_adapter_factory.h"
21 #include "audio_hdi_log.h"
22 #include "audio_errors.h"
23 #include "audio_utils.h"
24 #include "common/hdi_adapter_info.h"
25 #include "util/id_handler.h"
26 #include "sink/audio_render_sink.h"
27 #include "sink/bluetooth_audio_render_sink.h"
28 #include "sink/fast_audio_render_sink.h"
29 #include "sink/file_audio_render_sink.h"
30 #include "sink/multichannel_audio_render_sink.h"
31 #include "sink/offload_audio_render_sink.h"
32 #include "sink/direct_audio_render_sink.h"
33 #include "source/audio_capture_source.h"
34 #include "source/bluetooth_audio_capture_source.h"
35 #include "source/wakeup_audio_capture_source.h"
36 #include "source/fast_audio_capture_source.h"
37 #include "source/file_audio_capture_source.h"
38 #include "adapter/local_device_manager.h"
39 #include "adapter/bluetooth_device_manager.h"
40
41 #ifdef FEATURE_DISTRIBUTE_AUDIO
42 #include "sink/remote_audio_render_sink.h"
43 #include "sink/remote_fast_audio_render_sink.h"
44 #include "sink/remote_offload_audio_render_sink.h"
45 #include "source/remote_audio_capture_source.h"
46 #include "source/remote_fast_audio_capture_source.h"
47 #include "adapter/remote_device_manager.h"
48 #endif
49
50 namespace OHOS {
51 namespace AudioStandard {
GetInstance(void)52 HdiAdapterFactory &HdiAdapterFactory::GetInstance(void)
53 {
54 static HdiAdapterFactory instance;
55 return instance;
56 }
57
CreateRenderSink(uint32_t renderId)58 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRenderSink(uint32_t renderId)
59 {
60 IdHandler &idHandler = IdHandler::GetInstance();
61 CHECK_AND_RETURN_RET(idHandler.CheckId(renderId, HDI_ID_BASE_RENDER), nullptr);
62 uint32_t type = idHandler.ParseType(renderId);
63 std::string info = idHandler.ParseInfo(renderId);
64 AUDIO_INFO_LOG("Type: %{public}u, info: %{public}s", type, info.c_str());
65
66 std::shared_ptr<IAudioRenderSink> sink = nullptr;
67 switch (type) {
68 case HDI_ID_TYPE_PRIMARY:
69 sink = CreatePrimaryRenderSink(renderId, info);
70 break;
71 case HDI_ID_TYPE_BLUETOOTH:
72 sink = CreateBluetoothRenderSink(info);
73 break;
74 case HDI_ID_TYPE_FAST:
75 sink = std::make_shared<FastAudioRenderSink>();
76 break;
77 case HDI_ID_TYPE_FILE:
78 sink = std::make_shared<FileAudioRenderSink>();
79 break;
80 case HDI_ID_TYPE_MULTICHANNEL:
81 sink = std::make_shared<MultichannelAudioRenderSink>(info);
82 break;
83 case HDI_ID_TYPE_OFFLOAD:
84 sink = std::make_shared<OffloadAudioRenderSink>();
85 break;
86 case HDI_ID_TYPE_EAC3:
87 sink = std::make_shared<DirectAudioRenderSink>();
88 break;
89 #ifdef FEATURE_DISTRIBUTE_AUDIO
90 case HDI_ID_TYPE_REMOTE:
91 sink = CreateRemoteRenderSink(info);
92 break;
93 case HDI_ID_TYPE_REMOTE_FAST:
94 sink = CreateRemoteFastRenderSink(info);
95 break;
96 case HDI_ID_TYPE_REMOTE_OFFLOAD:
97 sink = CreateRemoteOffloadRenderSink(info);
98 break;
99 #endif
100 default:
101 AUDIO_ERR_LOG("invalid type");
102 break;
103 }
104 return sink;
105 }
106
CreateCaptureSource(uint32_t captureId)107 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreateCaptureSource(uint32_t captureId)
108 {
109 IdHandler &idHandler = IdHandler::GetInstance();
110 CHECK_AND_RETURN_RET(idHandler.CheckId(captureId, HDI_ID_BASE_CAPTURE), nullptr);
111 uint32_t type = idHandler.ParseType(captureId);
112 std::string info = idHandler.ParseInfo(captureId);
113
114 std::shared_ptr<IAudioCaptureSource> source = nullptr;
115 switch (type) {
116 case HDI_ID_TYPE_PRIMARY:
117 case HDI_ID_TYPE_ACCESSORY:
118 source = CreatePrimaryCaptureSource(captureId, info);
119 break;
120 case HDI_ID_TYPE_BLUETOOTH:
121 source = std::make_shared<BluetoothAudioCaptureSource>(captureId);
122 break;
123 case HDI_ID_TYPE_WAKEUP:
124 source = std::make_shared<WakeupAudioCaptureSource>(captureId);
125 break;
126 case HDI_ID_TYPE_FAST:
127 source = std::make_shared<FastAudioCaptureSource>();
128 break;
129 case HDI_ID_TYPE_FILE:
130 source = std::make_shared<FileAudioCaptureSource>();
131 break;
132 #ifdef FEATURE_DISTRIBUTE_AUDIO
133 case HDI_ID_TYPE_REMOTE:
134 source = CreateRemoteCaptureSource(info);
135 break;
136 case HDI_ID_TYPE_REMOTE_FAST:
137 source = CreateRemoteFastCaptureSource(info);
138 break;
139 #endif
140 default:
141 AUDIO_ERR_LOG("invalid type");
142 break;
143 }
144 return source;
145 }
146
CreateDeviceManager(uint32_t type)147 std::shared_ptr<IDeviceManager> HdiAdapterFactory::CreateDeviceManager(uint32_t type)
148 {
149 std::shared_ptr<IDeviceManager> deviceManager = nullptr;
150 switch (type) {
151 case HDI_DEVICE_MANAGER_TYPE_LOCAL:
152 deviceManager = std::make_shared<LocalDeviceManager>();
153 break;
154 case HDI_DEVICE_MANAGER_TYPE_BLUETOOTH:
155 deviceManager = std::make_shared<BluetoothDeviceManager>();
156 break;
157 #ifdef FEATURE_DISTRIBUTE_AUDIO
158 case HDI_DEVICE_MANAGER_TYPE_REMOTE:
159 deviceManager = std::make_shared<RemoteDeviceManager>();
160 break;
161 #endif
162 default:
163 AUDIO_ERR_LOG("invalid type");
164 break;
165 }
166 return deviceManager;
167 }
168
CreatePrimaryRenderSink(const uint32_t renderId,const std::string & info)169 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreatePrimaryRenderSink(const uint32_t renderId,
170 const std::string &info)
171 {
172 if (info == HDI_ID_INFO_DIRECT || info == HDI_ID_INFO_VOIP || info == HDI_ID_INFO_DP ||
173 info == HDI_ID_INFO_USB) {
174 return std::make_shared<AudioRenderSink>(renderId, info);
175 }
176 return std::make_shared<AudioRenderSink>(renderId);
177 }
178
CreateBluetoothRenderSink(const std::string & info)179 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateBluetoothRenderSink(const std::string &info)
180 {
181 if (info == HDI_ID_INFO_MMAP) {
182 return std::make_shared<BluetoothAudioRenderSink>(true);
183 }
184 if (info == HDI_ID_INFO_HEARING_AID) {
185 return std::make_shared<BluetoothAudioRenderSink>(false, info);
186 }
187 return std::make_shared<BluetoothAudioRenderSink>();
188 }
189
190 #ifdef FEATURE_DISTRIBUTE_AUDIO
CreateRemoteRenderSink(const std::string & info)191 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRemoteRenderSink(const std::string &info)
192 {
193 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
194 return std::make_shared<RemoteAudioRenderSink>(info);
195 }
196
CreateRemoteFastRenderSink(const std::string & info)197 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRemoteFastRenderSink(const std::string &info)
198 {
199 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
200 return std::make_shared<RemoteFastAudioRenderSink>(info);
201 }
202
CreateRemoteOffloadRenderSink(const std::string & info)203 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRemoteOffloadRenderSink(const std::string &info)
204 {
205 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
206 return std::make_shared<RemoteOffloadAudioRenderSink>(info);
207 }
208 #endif
209
CreatePrimaryCaptureSource(const uint32_t captureId,const std::string & info)210 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreatePrimaryCaptureSource(const uint32_t captureId,
211 const std::string &info)
212 {
213 if (info == HDI_ID_INFO_USB || info == HDI_ID_INFO_ACCESSORY) {
214 return std::make_shared<AudioCaptureSource>(captureId, info);
215 }
216 return std::make_shared<AudioCaptureSource>(captureId);
217 }
218
219 #ifdef FEATURE_DISTRIBUTE_AUDIO
CreateRemoteCaptureSource(const std::string & info)220 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreateRemoteCaptureSource(const std::string &info)
221 {
222 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
223 return std::make_shared<RemoteAudioCaptureSource>(info);
224 }
225
CreateRemoteFastCaptureSource(const std::string & info)226 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreateRemoteFastCaptureSource(const std::string &info)
227 {
228 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
229 return std::make_shared<RemoteFastAudioCaptureSource>(info);
230 }
231 #endif
232
233 } // namespace AudioStandard
234 } // namespace OHOS
235