• 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 <string.h>
19 #include "audio_log.h"
20 #include "audio_renderer_file_sink_intf.h"
21 #include "audio_renderer_sink_intf.h"
22 #include "bluetooth_renderer_sink_intf.h"
23 #include "remote_audio_renderer_sink_intf.h"
24 #include "renderer_sink_adapter.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 const int32_t  SUCCESS = 0;
31 const int32_t  ERROR = -1;
32 
33 const int32_t CLASS_TYPE_PRIMARY = 0;
34 const int32_t CLASS_TYPE_A2DP = 1;
35 const int32_t CLASS_TYPE_FILE = 2;
36 const int32_t CLASS_TYPE_REMOTE = 3;
37 
38 const char *g_deviceClassPrimary = "primary";
39 const char *g_deviceClassA2Dp = "a2dp";
40 const char *g_deviceClassFile = "file_io";
41 const char *g_deviceClassRemote = "remote";
42 
43 int32_t g_deviceClass = -1;
44 
RendererSinkInitInner(void * wapper,const SinkAttr * attr)45 static int32_t RendererSinkInitInner(void *wapper, const SinkAttr *attr)
46 {
47     if (attr == NULL) {
48         AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
49         return ERROR;
50     }
51 
52     if (g_deviceClass == -1) {
53         AUDIO_ERR_LOG("%{public}s: Adapter not loaded", __func__);
54         return ERROR;
55     }
56 
57     if (g_deviceClass == CLASS_TYPE_PRIMARY) {
58         AUDIO_INFO_LOG("%{public}s: CLASS_TYPE_PRIMARY", __func__);
59         return AudioRendererSinkInit(wapper, (AudioSinkAttr *)attr);
60     } else if (g_deviceClass == CLASS_TYPE_A2DP) {
61         AUDIO_INFO_LOG("%{public}s: CLASS_TYPE_A2DP", __func__);
62         BluetoothSinkAttr bluetoothSinkAttr;
63         bluetoothSinkAttr.format = attr->format;
64         bluetoothSinkAttr.sampleFmt = attr->sampleFmt;
65         bluetoothSinkAttr.sampleRate = attr->sampleRate;
66         bluetoothSinkAttr.channel = attr->channel;
67         bluetoothSinkAttr.volume = attr->volume;
68         return BluetoothRendererSinkInit(wapper, &bluetoothSinkAttr);
69     } else if (g_deviceClass == CLASS_TYPE_FILE) {
70         AUDIO_INFO_LOG("%{public}s: CLASS_TYPE_FILE", __func__);
71         return AudioRendererFileSinkInit(wapper, attr->filePath);
72     } else if (g_deviceClass == CLASS_TYPE_REMOTE) {
73         AUDIO_INFO_LOG("init [%{public}s]: CLASS_TYPE_REMOTE", attr->deviceNetworkId);
74         RemoteAudioSinkAttr remoteAudioSinkAttr;
75         remoteAudioSinkAttr.adapterName = attr->adapterName;
76         remoteAudioSinkAttr.openMicSpeaker = attr->open_mic_speaker;
77         remoteAudioSinkAttr.format = attr->format;
78         remoteAudioSinkAttr.sampleFmt = attr->sampleFmt;
79         remoteAudioSinkAttr.sampleRate = attr->sampleRate;
80         remoteAudioSinkAttr.channel = attr->channel;
81         remoteAudioSinkAttr.volume = attr->volume;
82         remoteAudioSinkAttr.filePath = attr->filePath;
83         remoteAudioSinkAttr.deviceNetworkId = attr->deviceNetworkId;
84         remoteAudioSinkAttr.device_type = attr->device_type;
85         return RemoteAudioRendererSinkInit(wapper, &remoteAudioSinkAttr);
86     } else {
87         AUDIO_ERR_LOG("%{public}s: Device not supported", __func__);
88         return ERROR;
89     }
90 }
91 
LoadSinkAdapter(const char * device,const char * deviceNetworkId,struct RendererSinkAdapter ** sinkAdapter)92 int32_t LoadSinkAdapter(const char *device, const char *deviceNetworkId, struct RendererSinkAdapter **sinkAdapter)
93 {
94     AUDIO_INFO_LOG("%{public}s: device:[%{public}s]", __func__, device);
95     if ((device == NULL) || (sinkAdapter == NULL)) {
96         AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
97         return ERROR;
98     }
99 
100     struct RendererSinkAdapter *adapter = (struct RendererSinkAdapter *)calloc(1, sizeof(*adapter));
101     if (adapter == NULL) {
102         AUDIO_ERR_LOG("%{public}s: alloc sink adapter failed", __func__);
103         return ERROR;
104     }
105 
106     if (!strcmp(device, g_deviceClassPrimary)) {
107         AUDIO_INFO_LOG("%{public}s: primary device", __func__);
108         FillinAudioRenderSinkWapper(device, &adapter->wapper);
109         adapter->RendererSinkInit = RendererSinkInitInner;
110         adapter->RendererSinkDeInit = AudioRendererSinkDeInit;
111         adapter->RendererSinkStart = AudioRendererSinkStart;
112         adapter->RendererSinkStop = AudioRendererSinkStop;
113         adapter->RendererSinkPause = AudioRendererSinkPause;
114         adapter->RendererSinkResume = AudioRendererSinkResume;
115         adapter->RendererRenderFrame = AudioRendererRenderFrame;
116         adapter->RendererSinkSetVolume = AudioRendererSinkSetVolume;
117         adapter->RendererSinkGetLatency = AudioRendererSinkGetLatency;
118         adapter->RendererSinkGetTransactionId = AudioRendererSinkGetTransactionId;
119         g_deviceClass = CLASS_TYPE_PRIMARY;
120         adapter->deviceClass = CLASS_TYPE_PRIMARY;
121     } else if (!strcmp(device, g_deviceClassA2Dp)) {
122         AUDIO_INFO_LOG("%{public}s: a2dp device", __func__);
123         BluetoothFillinAudioRenderSinkWapper(device, &adapter->wapper);
124         adapter->RendererSinkInit = RendererSinkInitInner;
125         adapter->RendererSinkDeInit = BluetoothRendererSinkDeInit;
126         adapter->RendererSinkStart = BluetoothRendererSinkStart;
127         adapter->RendererSinkStop = BluetoothRendererSinkStop;
128         adapter->RendererSinkPause = BluetoothRendererSinkPause;
129         adapter->RendererSinkResume = BluetoothRendererSinkResume;
130         adapter->RendererRenderFrame = BluetoothRendererRenderFrame;
131         adapter->RendererSinkSetVolume = BluetoothRendererSinkSetVolume;
132         adapter->RendererSinkGetLatency = BluetoothRendererSinkGetLatency;
133         adapter->RendererSinkGetTransactionId = BluetoothRendererSinkGetTransactionId;
134         g_deviceClass = CLASS_TYPE_A2DP;
135         adapter->deviceClass = CLASS_TYPE_A2DP;
136     } else if (!strcmp(device, g_deviceClassFile)) {
137         FillinAudioRenderFileSinkWapper(device, &adapter->wapper);
138         adapter->RendererSinkInit = RendererSinkInitInner;
139         adapter->RendererSinkDeInit = AudioRendererFileSinkDeInit;
140         adapter->RendererSinkStart = AudioRendererFileSinkStart;
141         adapter->RendererSinkStop = AudioRendererFileSinkStop;
142         adapter->RendererSinkPause = AudioRendererFileSinkPause;
143         adapter->RendererSinkResume = AudioRendererFileSinkResume;
144         adapter->RendererRenderFrame = AudioRendererFileSinkRenderFrame;
145         adapter->RendererSinkSetVolume = AudioRendererFileSinkSetVolume;
146         adapter->RendererSinkGetLatency = AudioRendererFileSinkGetLatency;
147         adapter->RendererSinkGetTransactionId = AudioRendererFileSinkGetTransactionId;
148         g_deviceClass = CLASS_TYPE_FILE;
149         adapter->deviceClass = CLASS_TYPE_FILE;
150     } else if (!strcmp(device, g_deviceClassRemote)) {
151         AUDIO_DEBUG_LOG("%{public}s: remote device", __func__);
152         FillinRemoteAudioRenderSinkWapper(deviceNetworkId, &adapter->wapper);
153         adapter->RendererSinkInit = RendererSinkInitInner;
154         adapter->RendererSinkDeInit = RemoteAudioRendererSinkDeInit;
155         adapter->RendererSinkStart = RemoteAudioRendererSinkStart;
156         adapter->RendererSinkStop = RemoteAudioRendererSinkStop;
157         adapter->RendererSinkPause = RemoteAudioRendererSinkPause;
158         adapter->RendererSinkResume = RemoteAudioRendererSinkResume;
159         adapter->RendererRenderFrame = RemoteAudioRendererRenderFrame;
160         adapter->RendererSinkSetVolume = RemoteAudioRendererSinkSetVolume;
161         adapter->RendererSinkGetLatency = RemoteAudioRendererSinkGetLatency;
162         adapter->RendererSinkGetTransactionId = RemoteAudioRendererSinkGetTransactionId;
163         g_deviceClass = CLASS_TYPE_REMOTE;
164         adapter->deviceClass = CLASS_TYPE_REMOTE;
165     } else {
166         AUDIO_ERR_LOG("%{public}s: Device not supported", __func__);
167         free(adapter);
168         return ERROR;
169     }
170 
171     *sinkAdapter = adapter;
172 
173     return SUCCESS;
174 }
175 
UnLoadSinkAdapter(struct RendererSinkAdapter * sinkAdapter)176 int32_t UnLoadSinkAdapter(struct RendererSinkAdapter *sinkAdapter)
177 {
178     if (sinkAdapter == NULL) {
179         AUDIO_ERR_LOG("%{public}s: Invalid parameter", __func__);
180         return ERROR;
181     }
182 
183     free(sinkAdapter);
184 
185     return SUCCESS;
186 }
187 
GetDeviceClass(int32_t deviceClass)188 const char *GetDeviceClass(int32_t deviceClass)
189 {
190     if (deviceClass == CLASS_TYPE_PRIMARY) {
191         return g_deviceClassPrimary;
192     } else if (deviceClass == CLASS_TYPE_A2DP) {
193         return g_deviceClassA2Dp;
194     } else if (deviceClass == CLASS_TYPE_FILE) {
195         return g_deviceClassFile;
196     } else if (deviceClass == CLASS_TYPE_REMOTE) {
197         return g_deviceClassRemote;
198     } else {
199         return "";
200     }
201 }
202 #ifdef __cplusplus
203 }
204 #endif
205