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/remote_audio_render_sink.h"
33 #include "sink/remote_fast_audio_render_sink.h"
34 #include "source/audio_capture_source.h"
35 #include "source/bluetooth_audio_capture_source.h"
36 #include "source/wakeup_audio_capture_source.h"
37 #include "source/fast_audio_capture_source.h"
38 #include "source/file_audio_capture_source.h"
39 #include "source/remote_audio_capture_source.h"
40 #include "source/remote_fast_audio_capture_source.h"
41 #include "adapter/local_device_manager.h"
42 #include "adapter/bluetooth_device_manager.h"
43 #include "adapter/remote_device_manager.h"
44
45 namespace OHOS {
46 namespace AudioStandard {
GetInstance(void)47 HdiAdapterFactory &HdiAdapterFactory::GetInstance(void)
48 {
49 static HdiAdapterFactory instance;
50 return instance;
51 }
52
CreateRenderSink(uint32_t renderId)53 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRenderSink(uint32_t renderId)
54 {
55 IdHandler &idHandler = IdHandler::GetInstance();
56 CHECK_AND_RETURN_RET(idHandler.CheckId(renderId, HDI_ID_BASE_RENDER), nullptr);
57 uint32_t type = idHandler.ParseType(renderId);
58 std::string info = idHandler.ParseInfo(renderId);
59
60 std::shared_ptr<IAudioRenderSink> sink = nullptr;
61 switch (type) {
62 case HDI_ID_TYPE_PRIMARY:
63 sink = CreatePrimaryRenderSink(renderId, info);
64 break;
65 case HDI_ID_TYPE_BLUETOOTH:
66 sink = CreateBluetoothRenderSink(info);
67 break;
68 case HDI_ID_TYPE_FAST:
69 sink = std::make_shared<FastAudioRenderSink>();
70 break;
71 case HDI_ID_TYPE_FILE:
72 sink = std::make_shared<FileAudioRenderSink>();
73 break;
74 case HDI_ID_TYPE_MULTICHANNEL:
75 sink = std::make_shared<MultichannelAudioRenderSink>();
76 break;
77 case HDI_ID_TYPE_OFFLOAD:
78 sink = std::make_shared<OffloadAudioRenderSink>();
79 break;
80 case HDI_ID_TYPE_REMOTE:
81 sink = CreateRemoteRenderSink(info);
82 break;
83 case HDI_ID_TYPE_REMOTE_FAST:
84 sink = CreateRemoteFastRenderSink(info);
85 break;
86 default:
87 AUDIO_ERR_LOG("invalid type");
88 break;
89 }
90 return sink;
91 }
92
CreateCaptureSource(uint32_t captureId)93 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreateCaptureSource(uint32_t captureId)
94 {
95 IdHandler &idHandler = IdHandler::GetInstance();
96 CHECK_AND_RETURN_RET(idHandler.CheckId(captureId, HDI_ID_BASE_CAPTURE), nullptr);
97 uint32_t type = idHandler.ParseType(captureId);
98 std::string info = idHandler.ParseInfo(captureId);
99
100 std::shared_ptr<IAudioCaptureSource> source = nullptr;
101 switch (type) {
102 case HDI_ID_TYPE_PRIMARY:
103 source = CreatePrimaryCaptureSource(captureId, info);
104 break;
105 case HDI_ID_TYPE_BLUETOOTH:
106 source = std::make_shared<BluetoothAudioCaptureSource>(captureId);
107 break;
108 case HDI_ID_TYPE_WAKEUP:
109 source = std::make_shared<WakeupAudioCaptureSource>(captureId);
110 break;
111 case HDI_ID_TYPE_FAST:
112 source = std::make_shared<FastAudioCaptureSource>();
113 break;
114 case HDI_ID_TYPE_FILE:
115 source = std::make_shared<FileAudioCaptureSource>();
116 break;
117 case HDI_ID_TYPE_REMOTE:
118 source = CreateRemoteCaptureSource(info);
119 break;
120 case HDI_ID_TYPE_REMOTE_FAST:
121 source = CreateRemoteFastCaptureSource(info);
122 break;
123 default:
124 AUDIO_ERR_LOG("invalid type");
125 break;
126 }
127 return source;
128 }
129
CreateDeviceManager(uint32_t type)130 std::shared_ptr<IDeviceManager> HdiAdapterFactory::CreateDeviceManager(uint32_t type)
131 {
132 std::shared_ptr<IDeviceManager> deviceManager = nullptr;
133 switch (type) {
134 case HDI_DEVICE_MANAGER_TYPE_LOCAL:
135 deviceManager = std::make_shared<LocalDeviceManager>();
136 break;
137 case HDI_DEVICE_MANAGER_TYPE_BLUETOOTH:
138 deviceManager = std::make_shared<BluetoothDeviceManager>();
139 break;
140 case HDI_DEVICE_MANAGER_TYPE_REMOTE:
141 deviceManager = std::make_shared<RemoteDeviceManager>();
142 break;
143 default:
144 AUDIO_ERR_LOG("invalid type");
145 break;
146 }
147 return deviceManager;
148 }
149
CreatePrimaryRenderSink(const uint32_t renderId,const std::string & info)150 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreatePrimaryRenderSink(const uint32_t renderId,
151 const std::string &info)
152 {
153 if (info == HDI_ID_INFO_DIRECT || info == HDI_ID_INFO_VOIP || info == HDI_ID_INFO_DP ||
154 info == HDI_ID_INFO_USB) {
155 return std::make_shared<AudioRenderSink>(renderId, info);
156 }
157 return std::make_shared<AudioRenderSink>(renderId);
158 }
159
CreateBluetoothRenderSink(const std::string & info)160 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateBluetoothRenderSink(const std::string &info)
161 {
162 if (info == HDI_ID_INFO_MMAP) {
163 return std::make_shared<BluetoothAudioRenderSink>(true);
164 }
165 return std::make_shared<BluetoothAudioRenderSink>();
166 }
167
CreateRemoteRenderSink(const std::string & info)168 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRemoteRenderSink(const std::string &info)
169 {
170 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
171 return std::make_shared<RemoteAudioRenderSink>(info);
172 }
173
CreateRemoteFastRenderSink(const std::string & info)174 std::shared_ptr<IAudioRenderSink> HdiAdapterFactory::CreateRemoteFastRenderSink(const std::string &info)
175 {
176 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
177 return std::make_shared<RemoteFastAudioRenderSink>(info);
178 }
179
CreatePrimaryCaptureSource(const uint32_t captureId,const std::string & info)180 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreatePrimaryCaptureSource(const uint32_t captureId,
181 const std::string &info)
182 {
183 if (info == HDI_ID_INFO_USB) {
184 return std::make_shared<AudioCaptureSource>(captureId, info);
185 }
186 return std::make_shared<AudioCaptureSource>(captureId);
187 }
188
CreateRemoteCaptureSource(const std::string & info)189 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreateRemoteCaptureSource(const std::string &info)
190 {
191 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
192 return std::make_shared<RemoteAudioCaptureSource>(info);
193 }
194
CreateRemoteFastCaptureSource(const std::string & info)195 std::shared_ptr<IAudioCaptureSource> HdiAdapterFactory::CreateRemoteFastCaptureSource(const std::string &info)
196 {
197 CHECK_AND_RETURN_RET_LOG(!info.empty(), nullptr, "deviceNetworkId is nullptr");
198 return std::make_shared<RemoteFastAudioCaptureSource>(info);
199 }
200
201 } // namespace AudioStandard
202 } // namespace OHOS
203