1 /*
2 * Copyright (c) 2022-2023 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 "daudio_manager_callback.h"
17
18 #include <cstdint>
19 #include <hdf_base.h>
20 #include <securec.h>
21
22 #include "audio_types.h"
23
24 #include "daudio_constants.h"
25 #include "daudio_errorcode.h"
26 #include "daudio_log.h"
27
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "DAudioManagerCallback"
30
31 using OHOS::HDI::DistributedAudio::Audioext::V1_0::AudioParameter;
32
33 namespace OHOS {
34 namespace DistributedHardware {
OpenDevice(const std::string & adpName,int32_t devId)35 int32_t DAudioManagerCallback::OpenDevice(const std::string& adpName, int32_t devId)
36 {
37 DHLOGI("Open device.");
38 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
39 if (callback_->OpenDevice(adpName, devId) != DH_SUCCESS) {
40 DHLOGE("Call hdi callback failed.");
41 return HDF_FAILURE;
42 }
43 return HDF_SUCCESS;
44 }
45
CloseDevice(const std::string & adpName,int32_t devId)46 int32_t DAudioManagerCallback::CloseDevice(const std::string& adpName, int32_t devId)
47 {
48 DHLOGI("Close device.");
49 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
50 if (callback_->CloseDevice(adpName, devId) != DH_SUCCESS) {
51 DHLOGE("Rall hdi callback failed.");
52 return HDF_FAILURE;
53 }
54 return HDF_SUCCESS;
55 }
56
GetAudioParamHDF(const AudioParameter & param,AudioParamHDF & paramHDF)57 int32_t DAudioManagerCallback::GetAudioParamHDF(const AudioParameter& param, AudioParamHDF& paramHDF)
58 {
59 paramHDF.sampleRate = static_cast<AudioSampleRate>(param.sampleRate);
60 paramHDF.channelMask = static_cast<AudioChannel>(param.channelCount);
61 switch (static_cast<AudioFormat>(param.format)) {
62 case AUDIO_FORMAT_TYPE_PCM_8_BIT:
63 paramHDF.bitFormat = AudioSampleFormat::SAMPLE_U8;
64 break;
65 case AUDIO_FORMAT_TYPE_PCM_16_BIT:
66 paramHDF.bitFormat = AudioSampleFormat::SAMPLE_S16LE;
67 break;
68 case AUDIO_FORMAT_TYPE_PCM_24_BIT:
69 paramHDF.bitFormat = AudioSampleFormat::SAMPLE_S24LE;
70 break;
71 default:
72 DHLOGE("Format [%zu] does not support conversion.", param.format);
73 return HDF_FAILURE;
74 }
75 switch (static_cast<AudioCategory>(param.streamUsage)) {
76 case AUDIO_IN_MEDIA:
77 paramHDF.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
78 break;
79 case AUDIO_IN_COMMUNICATION:
80 paramHDF.streamUsage = StreamUsage::STREAM_USAGE_VOICE_COMMUNICATION;
81 break;
82 case AUDIO_IN_RINGTONE:
83 paramHDF.streamUsage = StreamUsage::STREAM_USAGE_NOTIFICATION_RINGTONE;
84 break;
85 case AUDIO_MMAP_NOIRQ:
86 paramHDF.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
87 break;
88 default:
89 DHLOGE("Stream usage [%zu] does not support conversion.", param.streamUsage);
90 return HDF_FAILURE;
91 }
92 paramHDF.frameSize = param.frameSize;
93 paramHDF.period = param.period;
94 paramHDF.ext = param.ext;
95 paramHDF.renderFlags = static_cast<OHOS::DistributedHardware::PortOperationMode>(param.renderFlags);
96 paramHDF.capturerFlags = static_cast<OHOS::DistributedHardware::PortOperationMode>(param.capturerFlags);
97 DHLOGI("HDF Param: sample rate %d, channel %d, bit format %d, stream usage %d, frame size %zu, " +
98 "period %zu, renderFlags %d, capturerFlags %d, ext {%s}.", paramHDF.sampleRate, paramHDF.channelMask,
99 paramHDF.bitFormat, paramHDF.streamUsage, paramHDF.frameSize, paramHDF.period, paramHDF.renderFlags,
100 paramHDF.capturerFlags, paramHDF.ext.c_str());
101 return HDF_SUCCESS;
102 }
103
SetParameters(const std::string & adpName,int32_t devId,const AudioParameter & param)104 int32_t DAudioManagerCallback::SetParameters(const std::string& adpName, int32_t devId, const AudioParameter& param)
105 {
106 DHLOGD("Set Parameters.");
107 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
108 AudioParamHDF paramHDF;
109 int32_t ret = GetAudioParamHDF(param, paramHDF);
110 if (ret != DH_SUCCESS) {
111 DHLOGE("Get audio HDF param failed.");
112 return HDF_FAILURE;
113 }
114 ret = callback_->SetParameters(adpName, devId, paramHDF);
115 if (ret != DH_SUCCESS) {
116 DHLOGE("Call hdi callback failed.");
117 return HDF_FAILURE;
118 }
119 return HDF_SUCCESS;
120 }
121
NotifyEvent(const std::string & adpName,int32_t devId,const OHOS::HDI::DistributedAudio::Audioext::V1_0::DAudioEvent & event)122 int32_t DAudioManagerCallback::NotifyEvent(const std::string& adpName, int32_t devId,
123 const OHOS::HDI::DistributedAudio::Audioext::V1_0::DAudioEvent& event)
124 {
125 DHLOGI("Notify event.");
126 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
127 AudioEvent newEvent(AudioEventType::EVENT_UNKNOWN, event.content);
128 switch (event.type) {
129 case AudioEventHDF::AUDIO_EVENT_VOLUME_SET:
130 newEvent.type = AudioEventType::VOLUME_SET;
131 break;
132 case AudioEventHDF::AUDIO_EVENT_MUTE_SET:
133 newEvent.type = AudioEventType::VOLUME_MUTE_SET;
134 break;
135 case AudioEventHDF::AUDIO_EVENT_CHANGE_PLAY_STATUS:
136 newEvent.type = AudioEventType::CHANGE_PLAY_STATUS;
137 break;
138 case AudioEventHDF::AUDIO_EVENT_MMAP_START_SPK:
139 newEvent.type = AudioEventType::MMAP_SPK_START;
140 break;
141 case AudioEventHDF::AUDIO_EVENT_MMAP_STOP_SPK:
142 newEvent.type = AudioEventType::MMAP_SPK_STOP;
143 break;
144 case AudioEventHDF::AUDIO_EVENT_MMAP_START_MIC:
145 newEvent.type = AudioEventType::MMAP_MIC_START;
146 break;
147 case AudioEventHDF::AUDIO_EVENT_MMAP_STOP_MIC:
148 newEvent.type = AudioEventType::MMAP_MIC_STOP;
149 break;
150 case AudioEventHDF::AUDIO_EVENT_START:
151 newEvent.type = AudioEventType::AUDIO_START;
152 break;
153 case AudioEventHDF::AUDIO_EVENT_STOP:
154 newEvent.type = AudioEventType::AUDIO_STOP;
155 break;
156 default:
157 DHLOGE("Unsupport event tpye.");
158 break;
159 }
160
161 int32_t ret = callback_->NotifyEvent(adpName, devId, newEvent);
162 if (ret != DH_SUCCESS) {
163 DHLOGE("Call hdi callback failed.");
164 return HDF_FAILURE;
165 }
166 return HDF_SUCCESS;
167 }
168
WriteStreamData(const std::string & adpName,int32_t devId,const OHOS::HDI::DistributedAudio::Audioext::V1_0::AudioData & data)169 int32_t DAudioManagerCallback::WriteStreamData(const std::string &adpName, int32_t devId,
170 const OHOS::HDI::DistributedAudio::Audioext::V1_0::AudioData &data)
171 {
172 DHLOGD("Write Stream Data, audio data param frameSize is %d.", data.param.frameSize);
173 if (data.param.frameSize == 0 || data.param.frameSize > DEFAULT_AUDIO_DATA_SIZE) {
174 DHLOGE("Audio data param frameSize is 0. or > 4096");
175 return HDF_FAILURE;
176 }
177
178 std::shared_ptr<AudioData> audioData = std::make_shared<AudioData>(data.param.frameSize);
179 int32_t ret = memcpy_s(audioData->Data(), audioData->Capacity(), data.data.data(), data.data.size());
180 if (ret != EOK) {
181 DHLOGE("Copy audio data failed, error code %d.", ret);
182 return HDF_FAILURE;
183 }
184
185 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
186 if (callback_->WriteStreamData(adpName, devId, audioData) != DH_SUCCESS) {
187 DHLOGE("WriteStreamData failed.");
188 return HDF_FAILURE;
189 }
190 return HDF_SUCCESS;
191 }
192
ReadStreamData(const std::string & adpName,int32_t devId,OHOS::HDI::DistributedAudio::Audioext::V1_0::AudioData & data)193 int32_t DAudioManagerCallback::ReadStreamData(const std::string &adpName, int32_t devId,
194 OHOS::HDI::DistributedAudio::Audioext::V1_0::AudioData &data)
195 {
196 DHLOGD("Read stream data.");
197 std::shared_ptr<AudioData> audioData = nullptr;
198 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
199 if (callback_->ReadStreamData(adpName, devId, audioData) != DH_SUCCESS) {
200 DHLOGE("Read stream data failed.");
201 return HDF_FAILURE;
202 }
203
204 CHECK_NULL_RETURN(audioData, HDF_FAILURE);
205 data.data.assign(audioData->Data(), audioData->Data()+audioData->Capacity());
206 DHLOGD("Read stream data success.");
207 return HDF_SUCCESS;
208 }
209
ReadMmapPosition(const std::string & adpName,int32_t devId,uint64_t & frames,OHOS::HDI::DistributedAudio::Audioext::V1_0::CurrentTime & time)210 int32_t DAudioManagerCallback::ReadMmapPosition(const std::string &adpName, int32_t devId,
211 uint64_t &frames, OHOS::HDI::DistributedAudio::Audioext::V1_0::CurrentTime &time)
212 {
213 DHLOGD("Read mmap position");
214 CurrentTimeHDF timeHdf;
215 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
216 if (callback_->ReadMmapPosition(adpName, devId, frames, timeHdf) != DH_SUCCESS) {
217 DHLOGE("Read mmap position failed.");
218 return HDF_FAILURE;
219 }
220 time.tvSec = timeHdf.tvSec;
221 time.tvNSec = timeHdf.tvNSec;
222 DHLOGD("Read mmap position success.");
223 return HDF_SUCCESS;
224 }
225
RefreshAshmemInfo(const std::string & adpName,int32_t devId,int fd,int32_t ashmemLength,int32_t lengthPerTrans)226 int32_t DAudioManagerCallback::RefreshAshmemInfo(const std::string &adpName, int32_t devId,
227 int fd, int32_t ashmemLength, int32_t lengthPerTrans)
228 {
229 DHLOGD("Refresh ashmem info.");
230 CHECK_NULL_RETURN(callback_, HDF_FAILURE);
231 if (callback_->RefreshAshmemInfo(adpName, devId, fd, ashmemLength, lengthPerTrans) != DH_SUCCESS) {
232 DHLOGE("Refresh ashmem info failed.");
233 return HDF_FAILURE;
234 }
235 DHLOGD("Refresh ashmem info success.");
236 return HDF_SUCCESS;
237 }
238 } // DistributedHardware
239 } // OHOS
240