1 /*
2 * Copyright (c) 2021-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 <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "audio_log.h"
21 #include "capturer_source_adapter.h"
22 #include "i_audio_capturer_source_intf.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 const int32_t SUCCESS = 0;
29 const int32_t ERROR = -1;
30
31 const int32_t CLASS_TYPE_PRIMARY = 0;
32 const int32_t CLASS_TYPE_A2DP = 1;
33 const int32_t CLASS_TYPE_FILE = 2;
34 const int32_t CLASS_TYPE_REMOTE = 3;
35
36 const char *g_deviceClassPrimary = "primary";
37 const char *g_deviceClassA2DP = "a2dp";
38 const char *g_deviceClassFile = "file_io";
39 const char *g_deviceClassRemote = "remote";
40
41 int32_t g_deviceClass = -1;
42
CapturerSourceInitInner(void * wapper,const SourceAttr * attr)43 static int32_t CapturerSourceInitInner(void *wapper, const SourceAttr *attr)
44 {
45 AUDIO_INFO_LOG("%{public}s: CapturerSourceInitInner", __func__);
46 if (attr == NULL) {
47 AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
48 return ERROR;
49 }
50
51 return IAudioCapturerSourceInit(wapper, (IAudioSourceAttr *)attr);
52 }
53
LoadSourceAdapter(const char * device,const char * deviceNetworkId,const int32_t sourceType,const char * sourceName,struct CapturerSourceAdapter ** sourceAdapter)54 int32_t LoadSourceAdapter(const char *device, const char *deviceNetworkId, const int32_t sourceType,
55 const char *sourceName, struct CapturerSourceAdapter **sourceAdapter)
56 {
57 AUDIO_INFO_LOG("%{public}s: sourceType: %{public}d device: %{public}s ", __func__, sourceType, device);
58 if ((device == NULL) || (deviceNetworkId == NULL) || (sourceAdapter == NULL)) {
59 AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
60 return ERROR;
61 }
62
63 struct CapturerSourceAdapter *adapter = (struct CapturerSourceAdapter *)calloc(1, sizeof(*adapter));
64 if (adapter == NULL) {
65 AUDIO_ERR_LOG("%{public}s: alloc sink adapter failed", __func__);
66 return ERROR;
67 }
68
69 if (FillinSourceWapper(device, deviceNetworkId, sourceType, sourceName, &adapter->wapper) != SUCCESS) {
70 AUDIO_ERR_LOG("%{public}s: Device not supported", __func__);
71 free(adapter);
72 return ERROR;
73 }
74 // fill deviceClass for hdi_source.c
75 if (!strcmp(device, g_deviceClassPrimary)) {
76 adapter->deviceClass = CLASS_TYPE_PRIMARY;
77 }
78 if (!strcmp(device, g_deviceClassA2DP)) {
79 adapter->deviceClass = CLASS_TYPE_A2DP;
80 }
81 if (!strcmp(device, g_deviceClassFile)) {
82 adapter->deviceClass = CLASS_TYPE_FILE;
83 }
84 if (!strcmp(device, g_deviceClassRemote)) {
85 adapter->deviceClass = CLASS_TYPE_REMOTE;
86 }
87 adapter->CapturerSourceInit = CapturerSourceInitInner;
88 adapter->CapturerSourceDeInit = IAudioCapturerSourceDeInit;
89 adapter->CapturerSourceStart = IAudioCapturerSourceStart;
90 adapter->CapturerSourceStop = IAudioCapturerSourceStop;
91 adapter->CapturerSourceSetMute = IAudioCapturerSourceSetMute;
92 adapter->CapturerSourceIsMuteRequired = IAudioCapturerSourceIsMuteRequired;
93 adapter->CapturerSourceFrame = IAudioCapturerSourceFrame;
94 adapter->CapturerSourceSetVolume = IAudioCapturerSourceSetVolume;
95 adapter->CapturerSourceGetVolume = IAudioCapturerSourceGetVolume;
96
97 *sourceAdapter = adapter;
98
99 return SUCCESS;
100 }
101
UnLoadSourceAdapter(struct CapturerSourceAdapter * sourceAdapter)102 int32_t UnLoadSourceAdapter(struct CapturerSourceAdapter *sourceAdapter)
103 {
104 if (sourceAdapter == NULL) {
105 AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
106 return ERROR;
107 }
108
109 free(sourceAdapter);
110
111 return SUCCESS;
112 }
113
GetDeviceClass(int32_t deviceClass)114 const char *GetDeviceClass(int32_t deviceClass)
115 {
116 if (deviceClass == CLASS_TYPE_PRIMARY) {
117 return g_deviceClassPrimary;
118 } else if (deviceClass == CLASS_TYPE_A2DP) {
119 return g_deviceClassA2DP;
120 } else if (deviceClass == CLASS_TYPE_FILE) {
121 return g_deviceClassFile;
122 } else if (deviceClass == CLASS_TYPE_REMOTE) {
123 return g_deviceClassRemote;
124 } else {
125 return "";
126 }
127 }
128 #ifdef __cplusplus
129 }
130 #endif
131