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