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 "renderer_sink_adapter.h"
21 #include "audio_log.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 const int32_t SUCCESS = 0;
28 const int32_t ERROR = -1;
29
30 const int32_t CLASS_TYPE_PRIMARY = 0;
31 const int32_t CLASS_TYPE_A2DP = 1;
32 const int32_t CLASS_TYPE_FILE = 2;
33 const int32_t CLASS_TYPE_REMOTE = 3;
34
35 const char *g_deviceClassPrimary = "primary";
36 const char *g_deviceClassA2DP = "a2dp";
37 const char *g_deviceClassFile = "file_io";
38 const char *g_deviceClassRemote = "remote";
39
LoadSinkAdapter(const char * device,const char * deviceNetworkId,struct RendererSinkAdapter ** sinkAdapter)40 int32_t LoadSinkAdapter(const char *device, const char *deviceNetworkId, struct RendererSinkAdapter **sinkAdapter)
41 {
42 AUDIO_INFO_LOG("%{public}s: device:[%{public}s]", __func__, device);
43 if ((device == NULL) || (sinkAdapter == NULL)) {
44 AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
45 return ERROR;
46 }
47
48 struct RendererSinkAdapter *adapter = (struct RendererSinkAdapter *)calloc(1, sizeof(*adapter));
49 if (adapter == NULL) {
50 AUDIO_ERR_LOG("%{public}s: alloc sink adapter failed", __func__);
51 return ERROR;
52 }
53
54 if (FillinSinkWapper(device, deviceNetworkId, adapter) != SUCCESS) {
55 AUDIO_ERR_LOG("%{public}s: Device not supported", __func__);
56 free(adapter);
57 return ERROR;
58 }
59 // fill deviceClass for hdi_sink.c
60 if (!strcmp(device, g_deviceClassPrimary)) {
61 adapter->deviceClass = CLASS_TYPE_PRIMARY;
62 }
63 if (!strcmp(device, g_deviceClassA2DP)) {
64 adapter->deviceClass = CLASS_TYPE_A2DP;
65 }
66 if (!strcmp(device, g_deviceClassFile)) {
67 adapter->deviceClass = CLASS_TYPE_FILE;
68 }
69 if (!strcmp(device, g_deviceClassRemote)) {
70 adapter->deviceClass = CLASS_TYPE_REMOTE;
71 }
72 adapter->RendererSinkInit = IAudioRendererSinkInit;
73 adapter->RendererSinkDeInit = IAudioRendererSinkDeInit;
74 adapter->RendererSinkStart = IAudioRendererSinkStart;
75 adapter->RendererSinkStop = IAudioRendererSinkStop;
76 adapter->RendererSinkPause = IAudioRendererSinkPause;
77 adapter->RendererSinkResume = IAudioRendererSinkResume;
78 adapter->RendererRenderFrame = IAudioRendererSinkRenderFrame;
79 adapter->RendererSinkSetVolume = IAudioRendererSinkSetVolume;
80 adapter->RendererSinkGetLatency = IAudioRendererSinkGetLatency;
81
82 *sinkAdapter = adapter;
83
84 return SUCCESS;
85 }
86
UnLoadSinkAdapter(struct RendererSinkAdapter * sinkAdapter)87 int32_t UnLoadSinkAdapter(struct RendererSinkAdapter *sinkAdapter)
88 {
89 if (sinkAdapter == NULL) {
90 AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
91 return ERROR;
92 }
93
94 free(sinkAdapter);
95
96 return SUCCESS;
97 }
98
GetDeviceClass(int32_t deviceClass)99 const char *GetDeviceClass(int32_t deviceClass)
100 {
101 if (deviceClass == CLASS_TYPE_PRIMARY) {
102 return g_deviceClassPrimary;
103 } else if (deviceClass == CLASS_TYPE_A2DP) {
104 return g_deviceClassA2DP;
105 } else if (deviceClass == CLASS_TYPE_FILE) {
106 return g_deviceClassFile;
107 } else if (deviceClass == CLASS_TYPE_REMOTE) {
108 return g_deviceClassRemote;
109 } else {
110 return "";
111 }
112 }
113 #ifdef __cplusplus
114 }
115 #endif
116