1ad1488ecef9a58bd1eb3805e57b397697e60800
[platform/adaptation/emulator/audio-hal-emul.git] / tizen-audio-file.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include <tizen-audio.h>
31 #include <tizen-audio-internal.h>
32 #include <hal/hal-common-interface.h>
33 #include <tizen-audio-file.h>
34
35 audio_return_e audio_file_open(const char *type, const char *filename, uint32_t direction, void *sample_spec, uint32_t period_size, uint32_t periods, void **handle)
36 {
37     FILE *f;
38     audio_pcm_sample_spec_s ss;
39
40     AUDIO_RETURN_VAL_IF_FAIL(type, AUDIO_ERR_PARAMETER);
41     AUDIO_RETURN_VAL_IF_FAIL(filename, AUDIO_ERR_PARAMETER);
42     AUDIO_RETURN_VAL_IF_FAIL(sample_spec, AUDIO_ERR_PARAMETER);
43     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
44
45     /* TODO: FIXME format will contains a wrong value */
46     ss = *(audio_pcm_sample_spec_s *)sample_spec;
47
48     if (ss.rate != 16000) {
49         AUDIO_LOG_ERROR("Not support rate. rate(%d)", ss.rate);
50         return AUDIO_ERR_PARAMETER;
51     }
52
53     if (ss.channels != 1) {
54         AUDIO_LOG_ERROR("Not support channels. channels(%d)", ss.channels);
55         return AUDIO_ERR_PARAMETER;
56     }
57
58     /* Skip
59     if (ss.format != SND_PCM_FORMAT_S16_LE) {
60         AUDIO_LOG_ERROR("Not support format");
61         return AUDIO_ERR_PARAMETER;
62     }
63     */
64
65     f = fopen(filename, "r");
66     if (!f) {
67         AUDIO_LOG_ERROR("Failed to open file name(%s)", filename);
68         return AUDIO_ERR_INTERNAL;
69     }
70
71     *handle = (void *)f;
72
73     return AUDIO_RET_OK;
74 }
75
76 audio_return_e audio_file_start(void *handle)
77 {
78     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
79
80     AUDIO_LOG_DEBUG("file start was called");
81
82     return AUDIO_RET_OK;
83 }
84
85 audio_return_e audio_file_stop(void *handle)
86 {
87     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
88
89     AUDIO_LOG_DEBUG("file stop was called");
90
91     return AUDIO_RET_OK;
92 }
93
94 audio_return_e audio_file_close(void *handle)
95 {
96     FILE *f;
97
98     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
99
100     f = (FILE *)handle;
101
102     fclose(f);
103
104     return AUDIO_RET_OK;
105 }
106
107 audio_return_e audio_file_avail(void *handle, uint32_t *avail)
108 {
109     FILE *f;
110     long cur, end;
111
112     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
113
114     f = (FILE *)handle;
115
116     cur = ftell(f);
117     if (cur < 0) {
118         AUDIO_LOG_ERROR("Failed to get ftell(%s)", strerror(errno));
119         return AUDIO_ERR_INTERNAL;
120     }
121
122     if (fseek(f, 0L, SEEK_END) < 0) {
123         AUDIO_LOG_ERROR("Failed to seek. err(%s)", strerror(errno));
124         return AUDIO_ERR_INTERNAL;
125     }
126
127     end = ftell(f);
128     if (end < 0) {
129         AUDIO_LOG_ERROR("Failed to get ftell(%s)", strerror(errno));
130         return AUDIO_ERR_INTERNAL;
131     }
132
133     if (fseek(f, cur, SEEK_SET) < 0) {
134         AUDIO_LOG_ERROR("Failed to seek. err(%s)", strerror(errno));
135         return AUDIO_ERR_INTERNAL;
136     }
137
138     *avail = end - cur;
139
140     return AUDIO_RET_OK;
141 }
142
143 audio_return_e audio_file_write(void *handle, const void *buffer, uint32_t frames)
144 {
145     FILE *f;
146     size_t ret;
147
148     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
149
150     f = (FILE *)handle;
151
152     ret = fwrite(buffer, sizeof(short), frames, f);
153     if (ret == 0) {
154         AUDIO_LOG_ERROR("Failed to write(%s)", strerror(errno));
155         return AUDIO_ERR_INTERNAL;
156     }
157
158     return AUDIO_RET_OK;
159 }
160
161 audio_return_e audio_file_read(void *handle, void *buffer, uint32_t frames)
162 {
163     FILE *f;
164     size_t ret;
165
166     AUDIO_RETURN_VAL_IF_FAIL(handle, AUDIO_ERR_PARAMETER);
167
168     f = (FILE *)handle;
169
170     ret = fread(buffer, sizeof(short), frames, f);
171     if (ret == 0) {
172         AUDIO_LOG_ERROR("Failed to read(%s)", strerror(errno));
173         return AUDIO_ERR_INTERNAL;
174     }
175
176     return AUDIO_RET_OK;
177 }
178
179 audio_return_e audio_file_get_fd(void *handle, int *fd)
180 {
181     return AUDIO_ERR_IOCTL;
182 }
183
184 audio_return_e audio_file_recover(void *handle, int revents)
185 {
186     return AUDIO_ERR_IOCTL;
187 }
188
189 audio_return_e audio_file_get_params(void *handle, uint32_t direction, void *sample_spec, uint32_t *period_size, uint32_t *periods)
190 {
191     return AUDIO_ERR_IOCTL;
192 }
193
194 audio_return_e audio_file_set_params(void *handle, uint32_t direction, void *sample_spec, uint32_t period_size, uint32_t periods)
195 {
196     return AUDIO_ERR_IOCTL;
197 }
198