• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     adapter->SourceAdapterUpdateSessionUid = SourceAdapterUpdateSessionUid;
52 }
53 
GetSourceAdapter(const char * deviceClass,const int32_t sourceType,const char * info)54 struct SourceAdapter *GetSourceAdapter(const char *deviceClass, const int32_t sourceType, const char *info)
55 {
56     CHECK_AND_RETURN_RET_LOG(deviceClass != NULL, NULL, "deviceClass is nullptr");
57     struct SourceAdapter *adapter = (struct SourceAdapter *)calloc(1, sizeof(*adapter));
58     CHECK_AND_RETURN_RET_LOG(adapter != NULL, NULL, "alloc source adapter fail");
59 
60     AUDIO_INFO_LOG("deviceClass: %{public}s, sourceType: %{public}d, networkId: %{public}s", deviceClass, sourceType,
61         info);
62     int32_t ret = InitSourceAdapter(adapter, deviceClass, sourceType, info);
63     if (ret != 0) {
64         AUDIO_ERR_LOG("not support, deviceClass: %{public}s, info: %{public}s", deviceClass, info);
65         if (adapter != NULL) {
66             if (adapter->deviceClass != NULL) {
67                 free((char *)(adapter->deviceClass));
68                 adapter->deviceClass = NULL;
69             }
70             free(adapter);
71             adapter = NULL;
72         }
73         return NULL;
74     }
75     FillAdapterFuncPtr(adapter);
76     return adapter;
77 }
78 
ReleaseSourceAdapter(struct SourceAdapter * sourceAdapter)79 void ReleaseSourceAdapter(struct SourceAdapter *sourceAdapter)
80 {
81     CHECK_AND_RETURN_LOG(sourceAdapter != NULL, "adapter is nullptr");
82     DeInitSourceAdapter(sourceAdapter);
83     free(sourceAdapter);
84 }
85 
GetSourceDeviceClass(uint32_t classType)86 const char *GetSourceDeviceClass(uint32_t classType)
87 {
88     if (classType >= CLASS_TYPE_NUM) {
89         AUDIO_ERR_LOG("invalid param, classType: %{public}u", classType);
90         return NULL;
91     }
92     return g_deviceClassMap[classType];
93 }
94 
95 #ifdef __cplusplus
96 }
97 #endif