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