Change get_params prototype
[platform/adaptation/spreadtrum/audio-hal-sc7727.git] / tizen-audio-pcm.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2016 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 "tizen-audio-internal.h"
25 #include "tizen-audio-impl.h"
26
27 audio_return_e _audio_pcm_init(audio_hal_s *ah)
28 {
29     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
30
31     ah->device.pcm_in = NULL;
32     ah->device.pcm_out = NULL;
33     ah->device.fmradio_pcm_out = NULL;
34     pthread_mutex_init(&ah->device.pcm_lock, NULL);
35     pthread_mutex_init(&ah->device.device_lock, NULL);
36     pthread_cond_init(&ah->device.device_cond, NULL);
37     ah->device.pcm_count = 0;
38
39     return AUDIO_RET_OK;
40 }
41
42 audio_return_e _audio_pcm_deinit(audio_hal_s *ah)
43 {
44     AUDIO_RETURN_VAL_IF_FAIL(ah, AUDIO_ERR_PARAMETER);
45
46     pthread_mutex_destroy(&ah->device.pcm_lock);
47     pthread_mutex_destroy(&ah->device.device_lock);
48     pthread_cond_destroy(&ah->device.device_cond);
49
50     return AUDIO_RET_OK;
51 }
52
53 audio_return_e audio_pcm_open(void *audio_handle, const char *card, const char *device, uint32_t direction, void *sample_spec,
54         uint32_t period_size, uint32_t periods, void **pcm_handle)
55 {
56     audio_return_e audio_ret = AUDIO_RET_OK;
57     audio_hal_s *ah = NULL;
58
59     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
60     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
61     AUDIO_RETURN_VAL_IF_FAIL(sample_spec, AUDIO_ERR_PARAMETER);
62     AUDIO_RETURN_VAL_IF_FAIL((period_size > 0), AUDIO_ERR_PARAMETER);
63     AUDIO_RETURN_VAL_IF_FAIL((periods > 0), AUDIO_ERR_PARAMETER);
64
65     if ((audio_ret = _pcm_open(card, device, direction, sample_spec, period_size, periods, pcm_handle)))
66         return audio_ret;
67
68     ah = (audio_hal_s*)audio_handle;
69     ah->device.pcm_count++;
70     AUDIO_LOG_INFO("Opening PCM handle %p", *pcm_handle);
71
72     return AUDIO_RET_OK;
73 }
74
75 audio_return_e audio_pcm_start(void *audio_handle, void *pcm_handle)
76 {
77     audio_return_e audio_ret = AUDIO_RET_OK;
78
79     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
80     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
81
82     audio_ret = _pcm_start(pcm_handle);
83
84     return audio_ret;
85 }
86
87 audio_return_e audio_pcm_stop(void *audio_handle, void *pcm_handle)
88 {
89     audio_return_e audio_ret = AUDIO_RET_OK;
90
91     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
92     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
93
94     audio_ret = _pcm_stop(pcm_handle);
95
96     return audio_ret;
97 }
98
99 audio_return_e audio_pcm_close(void *audio_handle, void *pcm_handle)
100 {
101     audio_return_e audio_ret = AUDIO_RET_OK;
102     audio_hal_s *ah = NULL;
103
104     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
105     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
106
107     if ((audio_ret = _pcm_close(pcm_handle)))
108         return audio_ret;
109
110     pcm_handle = NULL;
111     ah = (audio_hal_s*)audio_handle;
112     ah->device.pcm_count--;
113
114     AUDIO_LOG_INFO("PCM handle close success (count:%d)", ah->device.pcm_count);
115
116     return audio_ret;
117 }
118
119 audio_return_e audio_pcm_avail(void *audio_handle, void *pcm_handle, uint32_t *avail)
120 {
121     audio_return_e audio_ret = AUDIO_RET_OK;
122
123     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
124     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
125     AUDIO_RETURN_VAL_IF_FAIL(avail, AUDIO_ERR_PARAMETER);
126
127     audio_ret = _pcm_avail(pcm_handle, avail);
128
129     return audio_ret;
130 }
131
132 audio_return_e audio_pcm_write(void *audio_handle, void *pcm_handle, const void *buffer, uint32_t frames)
133 {
134     audio_return_e audio_ret = AUDIO_RET_OK;
135
136     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
137     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
138
139     audio_ret = _pcm_write(pcm_handle, buffer, frames);
140
141     return audio_ret;
142 }
143
144 audio_return_e audio_pcm_read(void *audio_handle, void *pcm_handle, void *buffer, uint32_t frames)
145 {
146     audio_return_e audio_ret = AUDIO_RET_OK;
147
148     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
149     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
150
151     audio_ret = _pcm_read(pcm_handle, buffer, frames);
152
153     return audio_ret;
154 }
155
156 audio_return_e audio_pcm_get_fd(void *audio_handle, void *pcm_handle, int *fd)
157 {
158     audio_return_e audio_ret = AUDIO_RET_OK;
159
160     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
161     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
162     AUDIO_RETURN_VAL_IF_FAIL(fd, AUDIO_ERR_PARAMETER);
163
164     audio_ret = _pcm_get_fd(pcm_handle, fd);
165
166     return audio_ret;
167 }
168
169 audio_return_e audio_pcm_recover(void *audio_handle, void *pcm_handle, int revents)
170 {
171     audio_return_e audio_ret = AUDIO_RET_OK;
172
173     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
174     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
175
176     audio_ret = _pcm_recover(pcm_handle, revents);
177
178     return audio_ret;
179 }
180
181 audio_return_e audio_pcm_get_params(void *audio_handle, void *pcm_handle, uint32_t direction, void *sample_spec, uint32_t *period_size, uint32_t *periods)
182 {
183     audio_return_e audio_ret = AUDIO_RET_OK;
184
185     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
186     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
187     AUDIO_RETURN_VAL_IF_FAIL(sample_spec, AUDIO_ERR_PARAMETER);
188     AUDIO_RETURN_VAL_IF_FAIL(period_size, AUDIO_ERR_PARAMETER);
189     AUDIO_RETURN_VAL_IF_FAIL(periods, AUDIO_ERR_PARAMETER);
190
191     audio_ret = _pcm_get_params(pcm_handle, direction, sample_spec, period_size, periods);
192
193     return audio_ret;
194 }
195
196 audio_return_e audio_pcm_set_params(void *audio_handle, void *pcm_handle, uint32_t direction, void *sample_spec, uint32_t period_size, uint32_t periods)
197 {
198     audio_return_e audio_ret = AUDIO_RET_OK;
199
200     AUDIO_RETURN_VAL_IF_FAIL(audio_handle, AUDIO_ERR_PARAMETER);
201     AUDIO_RETURN_VAL_IF_FAIL(pcm_handle, AUDIO_ERR_PARAMETER);
202     AUDIO_RETURN_VAL_IF_FAIL(sample_spec, AUDIO_ERR_PARAMETER);
203     AUDIO_RETURN_VAL_IF_FAIL(period_size, AUDIO_ERR_PARAMETER);
204     AUDIO_RETURN_VAL_IF_FAIL(periods, AUDIO_ERR_PARAMETER);
205
206     audio_ret = _pcm_set_params(pcm_handle, direction, sample_spec, period_size, periods);
207
208     return audio_ret;
209 }