for callback invoke when resolution is changed
[platform/adaptation/ap_samsung/libomxil-e3250-v4l2.git] / exynos4 / libcodec / audio / alp / dec / srp_api.c
1 /*
2  *
3  * Copyright 2012 Samsung Electronics S.LSI Co. LTD
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /*
19  * @file        srp_api.c
20  * @brief
21  * @author      Yunji Kim (yunji.kim@samsung.com)
22  * @version     1.1.0
23  * @history
24  *   2012.02.28 : Create
25  */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <sys/mman.h>
31 #include <sys/time.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdio.h>
38
39 #include "srp_api.h"
40
41 #define LOG_NDEBUG 1
42 #define LOG_TAG "libsrpapi"
43
44 #ifndef TIZEN_FEATURE_E3250 /* build env */
45 #include <utils/Log.h>
46 #else
47 #include "Exynos_OSAL_Log.h"
48 #endif
49
50 static struct srp_buf_info ibuf_info;
51 static struct srp_buf_info obuf_info;
52 static struct srp_buf_info pcm_info;
53
54 static int srp_dev = -1;
55 static int srp_block_mode = SRP_INIT_BLOCK_MODE;
56
57 int SRP_Create(int block_mode)
58 {
59     if (srp_dev == -1) {
60         srp_block_mode = block_mode;
61         srp_dev = open(SRP_DEV_NAME, O_RDWR |
62                     ((block_mode == SRP_INIT_NONBLOCK_MODE) ? O_NDELAY : 0));
63         if (srp_dev > 0)
64             return srp_dev;
65         else
66             return SRP_ERROR_OPEN_FAIL;
67     }
68
69     ALOGE("%s: Device is already opened", __func__);
70     return SRP_ERROR_ALREADY_OPEN;
71 }
72
73 int SRP_Init()
74 {
75     int ret = SRP_RETURN_OK;
76     unsigned int mmapped_size = 0;
77
78     if (srp_dev != -1) {
79         ret = ioctl(srp_dev, SRP_INIT);
80         if (ret < 0)
81             return ret;
82
83         /* mmap for OBUF */
84         ret = ioctl(srp_dev, SRP_GET_MMAP_SIZE, &mmapped_size);
85         if (ret < 0) {
86             ALOGE("%s: SRP_GET_MMAP_SIZE is failed", __func__);
87             return SRP_ERROR_OBUF_MMAP;
88         }
89         obuf_info.mmapped_addr = mmap(0, mmapped_size,
90                     PROT_READ | PROT_WRITE, MAP_SHARED, srp_dev, 0);
91         if (!obuf_info.mmapped_addr) {
92             ALOGE("%s: mmap is failed", __func__);
93             return SRP_ERROR_OBUF_MMAP;
94         }
95         obuf_info.mmapped_size = mmapped_size;
96
97         ret = SRP_RETURN_OK;
98     } else {
99         ALOGE("%s: Device is not ready", __func__);
100         ret = SRP_ERROR_NOT_READY; /* device is not created */
101     }
102
103     return ret;
104 }
105
106 int SRP_Decode(void *buff, int size_byte)
107 {
108     int ret = SRP_RETURN_OK;
109
110     if (srp_dev != -1) {
111         if (size_byte > 0) {
112             ALOGV("%s: Send data to RP (%d bytes)", __func__, size_byte);
113
114             ret = write(srp_dev, buff, size_byte);  /* Write Buffer to RP Driver */
115             if (ret < 0) {
116 #ifdef TIZEN_FEATURE_E3250
117 /* ret value is different from android. this is caused from glib. android uses bionic libc. */
118                 ret = -(errno);
119 #endif
120                 if (ret != SRP_ERROR_IBUF_OVERFLOW)
121                     ALOGE("SRP_Decode returned error code: %d", ret);
122             }
123             return ret; /* Write Success */
124         } else {
125             return ret;
126         }
127     }
128
129     ALOGE("%s: Device is not ready", __func__);
130     return SRP_ERROR_NOT_READY;
131 }
132
133 int SRP_Send_EOS(void)
134 {
135     if (srp_dev != -1)
136         return ioctl(srp_dev, SRP_SEND_EOS);
137
138     return SRP_ERROR_NOT_READY;
139 }
140
141 int SRP_SetParams(int id, unsigned long val)
142 {
143     if (srp_dev != -1)
144         return 0; /* not yet */
145
146     return SRP_ERROR_NOT_READY;
147 }
148
149 int SRP_GetParams(int id, unsigned long *pval)
150 {
151     if (srp_dev != -1)
152         return ioctl(srp_dev, id, pval);
153
154     return SRP_ERROR_NOT_READY;
155 }
156
157 int SRP_Flush(void)
158 {
159     if (srp_dev != -1)
160         return ioctl(srp_dev, SRP_FLUSH);
161
162     return SRP_ERROR_NOT_READY;
163 }
164
165 int SRP_Get_PCM(void **addr, unsigned int *size)
166 {
167     int ret = SRP_RETURN_OK;
168
169     if (srp_dev != -1) {
170         ret = read(srp_dev, &pcm_info, 0);
171         if (ret == -1) {
172             *size = 0;
173             ALOGE("%s: PCM read fail", __func__);
174             return SRP_ERROR_OBUF_READ;
175         }
176
177         *addr = pcm_info.addr;
178         *size = pcm_info.size;
179     } else {
180         return SRP_ERROR_NOT_READY;
181     }
182
183     return ret; /* Read Success */
184 }
185
186 int SRP_Get_Dec_Info(struct srp_dec_info *dec_info)
187 {
188     int ret;
189
190     if (srp_dev != -1) {
191         ret = ioctl(srp_dev, SRP_GET_DEC_INFO, dec_info);
192         if (ret < 0) {
193             ALOGE("%s: Failed to get dec info", __func__);
194             return SRP_ERROR_GETINFO_FAIL;
195         }
196
197         ALOGV("numChannels(%d), samplingRate(%d)", dec_info->channels, dec_info->sample_rate);
198
199         ret = SRP_RETURN_OK;
200     } else {
201         ret = SRP_ERROR_NOT_READY;
202     }
203
204     return ret;
205 }
206
207 int SRP_Get_Ibuf_Info(void **addr, unsigned int *size, unsigned int *num)
208 {
209     int ret = SRP_RETURN_OK;
210
211     if (srp_dev != -1) {
212         ret = ioctl(srp_dev, SRP_GET_IBUF_INFO, &ibuf_info);
213         if (ret == -1) {
214             ALOGE("%s: Failed to get Ibuf info", __func__);
215             return SRP_ERROR_IBUF_INFO;
216         }
217
218         *addr = ibuf_info.addr;
219         *size = ibuf_info.size;
220         *num = ibuf_info.num;
221
222         if (*num == 0) {
223             ALOGE("%s: IBUF num is 0", __func__);
224             return SRP_ERROR_INVALID_SETTING;
225         }
226
227         ret = SRP_RETURN_OK;
228     } else {
229         ret = SRP_ERROR_NOT_READY;
230     }
231
232     return ret;
233 }
234
235 int SRP_Get_Obuf_Info(void **addr, unsigned int *size, unsigned int *num)
236 {
237     int ret = SRP_RETURN_OK;
238
239     if (srp_dev != -1) {
240         if (obuf_info.addr == NULL) {
241             ret = ioctl(srp_dev, SRP_GET_OBUF_INFO, &obuf_info);
242             if (ret < 0) {
243                 ALOGE("%s: SRP_GET_OBUF_INFO is failed", __func__);
244                 return SRP_ERROR_OBUF_INFO;
245             }
246         }
247
248         *addr = obuf_info.addr;
249         *size = obuf_info.size;
250         *num = obuf_info.num;
251
252         if (*num == 0) {
253             ALOGE("%s: OBUF num is 0", __func__);
254             return SRP_ERROR_INVALID_SETTING;
255         }
256
257         ret = SRP_RETURN_OK;
258     } else {
259         ret = SRP_ERROR_NOT_READY;
260     }
261
262     return ret;
263 }
264
265 int SRP_Deinit(void)
266 {
267     if (srp_dev != -1) {
268         munmap(obuf_info.mmapped_addr, obuf_info.mmapped_size);
269         return ioctl(srp_dev, SRP_DEINIT);
270     }
271
272     return SRP_ERROR_NOT_READY;
273 }
274
275 int SRP_Terminate(void)
276 {
277     int ret;
278
279     if (srp_dev != -1) {
280         ret = close(srp_dev);
281
282         if (ret == 0) {
283             srp_dev = -1; /* device closed */
284             return SRP_RETURN_OK;
285         }
286     }
287
288     return SRP_ERROR_NOT_READY;
289 }
290
291 int SRP_IsOpen(void)
292 {
293     if (srp_dev == -1) {
294         ALOGV("%s: Device is not opened", __func__);
295         return 0;
296     }
297
298     ALOGV("%s: Device is opened", __func__);
299     return 1;
300 }