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 "SourceInterface"
18 #endif
19
20 #include "source/source_intf.h"
21 #include <stdlib.h>
22 #include "audio_hdi_log.h"
23 #include "common/hdi_adapter_info.h"
24 #include "source/source_adapter.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 static const char *g_deviceClassMap[CLASS_TYPE_NUM] = { "primary", "a2dp", "file_io", "remote", "usb", NULL,
31 NULL, NULL };
32
FillAdapterFuncPtr(struct SourceAdapter * adapter)33 static void FillAdapterFuncPtr(struct SourceAdapter *adapter)
34 {
35 CHECK_AND_RETURN_LOG(adapter != NULL, "adapter is nullptr");
36
37 adapter->SourceAdapterInit = SourceAdapterInit;
38 adapter->SourceAdapterDeInit = SourceAdapterDeInit;
39
40 adapter->SourceAdapterStart = SourceAdapterStart;
41 adapter->SourceAdapterStop = SourceAdapterStop;
42 adapter->SourceAdapterCaptureFrame = SourceAdapterCaptureFrame;
43 adapter->SourceAdapterCaptureFrameWithEc = SourceAdapterCaptureFrameWithEc;
44
45 adapter->SourceAdapterSetVolume = SourceAdapterSetVolume;
46 adapter->SourceAdapterGetVolume = SourceAdapterGetVolume;
47 adapter->SourceAdapterSetMute = SourceAdapterSetMute;
48 adapter->SourceAdapterGetMute = SourceAdapterGetMute;
49
50 adapter->SourceAdapterUpdateAppsUid = SourceAdapterUpdateAppsUid;
51 }
52
GetSourceAdapter(const char * deviceClass,const int32_t sourceType,const char * info)53 struct SourceAdapter *GetSourceAdapter(const char *deviceClass, const int32_t sourceType, const char *info)
54 {
55 CHECK_AND_RETURN_RET_LOG(deviceClass != NULL, NULL, "deviceClass is nullptr");
56 struct SourceAdapter *adapter = (struct SourceAdapter *)calloc(1, sizeof(*adapter));
57 CHECK_AND_RETURN_RET_LOG(adapter != NULL, NULL, "alloc source adapter fail");
58
59 AUDIO_INFO_LOG("deviceClass: %{public}s, sourceType: %{public}d, networkId: %{public}s", deviceClass, sourceType,
60 info);
61 int32_t ret = InitSourceAdapter(adapter, deviceClass, sourceType, info);
62 if (ret != 0) {
63 AUDIO_ERR_LOG("not support, deviceClass: %{public}s, info: %{public}s", deviceClass, info);
64 free(adapter);
65 return NULL;
66 }
67 FillAdapterFuncPtr(adapter);
68 return adapter;
69 }
70
ReleaseSourceAdapter(struct SourceAdapter * sourceAdapter)71 void ReleaseSourceAdapter(struct SourceAdapter *sourceAdapter)
72 {
73 CHECK_AND_RETURN_LOG(sourceAdapter != NULL, "adapter is nullptr");
74 DeInitSourceAdapter(sourceAdapter);
75 free(sourceAdapter);
76 }
77
GetSourceDeviceClass(uint32_t classType)78 const char *GetSourceDeviceClass(uint32_t classType)
79 {
80 if (classType >= CLASS_TYPE_NUM) {
81 AUDIO_ERR_LOG("invalid param, classType: %{public}u", classType);
82 return NULL;
83 }
84 return g_deviceClassMap[classType];
85 }
86
87 #ifdef __cplusplus
88 }
89 #endif