Fix for new automake and 64 bit compatibility.
[platform/core/multimedia/avsystem.git] / avsys-audio-alsa.c
1 /*
2  * avsystem
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <alsa/asoundlib.h>
23
24 #include "avsys-audio-alsa.h"
25 #include "avsys-types.h"
26 #include "avsys-error.h"
27 #include "avsys-debug.h"
28
29 #if defined(_MMFW_I386_ALL_SIMULATOR)
30 #define AIF_CP_DEVICE_NAME "default"
31 #define AIF_BT_DEVICE_NAME "default"
32 #define AIF_RADIO_DEVICE_NAME "default"
33 #else
34 #define AIF_CP_DEVICE_NAME "AIF2"
35 #define AIF_BT_DEVICE_NAME "AIF3"
36 #define AIF_RADIO_DEVICE_NAME "AIF4"
37 #endif
38
39 int avsys_audio_alsa_open_AIF_device(aif_device_type_t aif_type, avsys_audio_alsa_aif_handle_t *handle)
40 {
41         snd_pcm_t *ahandle = NULL;
42         int err = -1;
43         char dev_name[16] = { 0, };
44         snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
45
46         avsys_info(AVAUDIO, "%s\n", __func__);
47         if (!handle)
48                 return AVSYS_STATE_ERR_NULL_POINTER;
49
50         memset(dev_name, '\0', sizeof(dev_name));
51         switch (aif_type) {
52         case AIF_CP_CAPTURE:
53                 strncpy(dev_name, AIF_CP_DEVICE_NAME, sizeof(dev_name) - 1);
54                 stream = SND_PCM_STREAM_CAPTURE;
55                 break;
56         case AIF_CP_PLAYBACK:
57                 strncpy(dev_name, AIF_CP_DEVICE_NAME, sizeof(dev_name) - 1);
58                 stream = SND_PCM_STREAM_PLAYBACK;
59                 break;
60         case AIF_BT_CAPTURE:
61                 strncpy(dev_name, AIF_BT_DEVICE_NAME, sizeof(dev_name) - 1);
62                 stream = SND_PCM_STREAM_CAPTURE;
63                 break;
64         case AIF_BT_PLAYBACK:
65                 strncpy(dev_name, AIF_BT_DEVICE_NAME, sizeof(dev_name) - 1);
66                 stream = SND_PCM_STREAM_PLAYBACK;
67                 break;
68         case AIF_RADIO_PLAYBACK:
69                 strncpy(dev_name, AIF_RADIO_DEVICE_NAME, sizeof(dev_name) - 1);
70                 stream = SND_PCM_STREAM_PLAYBACK;
71                 break;
72         default:
73                 avsys_critical_r(AVAUDIO, "Invalid AIF device %d\n", aif_type);
74                 return AVSYS_STATE_ERR_INVALID_MODE;
75                 break;
76         }
77 #if !defined(_MMFW_I386_ALL_SIMULATOR)
78         err = snd_pcm_open(&ahandle, dev_name, stream, 0);
79 #else
80         avsys_warning_r(AVAUDIO, "Skip real device open in SDK\n");
81         err = 0;                /* set fake return value */
82 #endif
83
84         if (err < 0) {
85                 avsys_error_r(AVAUDIO, "unable to open AIF device: %s\n", snd_strerror(err));
86                 return AVSYS_STATE_ERR_INTERNAL;
87         }
88
89         handle->alsa_handle = (void *)ahandle;
90         handle->type = aif_type;
91
92         return AVSYS_STATE_SUCCESS;
93 }
94
95 int avsys_audio_alsa_close_AIF_device(avsys_audio_alsa_aif_handle_t* handle)
96 {
97         int err = 0;
98         snd_pcm_t *ahandle = NULL;
99
100         avsys_info(AVAUDIO, "%s\n", __func__);
101 #if defined(_MMFW_I386_ALL_SIMULATOR)
102         avsys_warning(AVAUDIO, "Skip close call device in SDK");
103         return AVSYS_STATE_SUCCESS;
104 #endif
105         if (!handle)
106                 return AVSYS_STATE_ERR_NULL_POINTER;
107
108         ahandle = (snd_pcm_t *)handle->alsa_handle;
109         if (!ahandle) {
110                 avsys_error_r(AVAUDIO, "[%s] alsa handle is null\n", __func__);
111                 return AVSYS_STATE_ERR_NULL_POINTER;
112         }
113
114         err = snd_pcm_close(ahandle);
115         if (err < 0) {
116                 avsys_critical_r(AVAUDIO, "unable to close pcm device: %s\n", snd_strerror(err));
117                 return AVSYS_STATE_ERR_INTERNAL;
118         }
119
120         handle->alsa_handle = NULL;
121
122         return AVSYS_STATE_SUCCESS;
123 }
124
125 int avsys_audio_alsa_set_AIF_params(avsys_audio_alsa_aif_handle_t *handle, aif_rate_t rate)
126 {
127 #if defined(_MMFW_I386_ALL_SIMULATOR)
128         return AVSYS_STATE_SUCCESS;
129 #else
130         int err = 0;
131         snd_pcm_t *ahandle = NULL;
132         snd_pcm_hw_params_t *params;
133         unsigned int val = 0;
134         int dir = 0;
135
136         avsys_info(AVAUDIO, "%s\n", __func__);
137         if (!handle)
138                 return AVSYS_STATE_ERR_NULL_POINTER;
139
140         ahandle = (snd_pcm_t *)handle->alsa_handle;
141         if (!ahandle)
142                 return AVSYS_STATE_ERR_NULL_POINTER;
143
144         /* Skip parameter setting to null device. */
145         if (snd_pcm_type(ahandle) == SND_PCM_TYPE_NULL)
146                 return AVSYS_STATE_SUCCESS;
147
148         /* Allocate a hardware parameters object. */
149         snd_pcm_hw_params_alloca(&params);
150         /* Fill it in with default values. */
151         err = snd_pcm_hw_params_any(ahandle, params);
152         if (err < 0) {
153                 avsys_error_r(AVAUDIO, "snd_pcm_hw_params_any() : failed! - %s\n", snd_strerror(err));
154                 return AVSYS_STATE_ERR_INTERNAL;
155         }
156
157         /* Set the desired hardware parameters. */
158         /* Interleaved mode */
159         err = snd_pcm_hw_params_set_access(ahandle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
160         if (err < 0) {
161                 avsys_error_r(AVAUDIO, "snd_pcm_hw_params_set_access() : failed! - %s\n", snd_strerror(err));
162                 return AVSYS_STATE_ERR_INTERNAL;
163         }
164
165         if (rate == AIF_CONF_RATE) {
166                 /* Auto rate by conf */
167                 avsys_info(AVAUDIO, "Let rate by configuration\n");
168         } else {
169                 /* Force input rate */
170                 avsys_info(AVAUDIO, "Force rate (%d)\n", rate);
171                 err = snd_pcm_hw_params_set_rate(ahandle, params, rate, 0);
172                 if (err < 0) {
173                         avsys_error_r(AVAUDIO, "snd_pcm_hw_params_set_rate() : failed! - %s\n", snd_strerror(err));
174                         return AVSYS_STATE_ERR_INTERNAL;
175                 }
176         }
177
178         err = snd_pcm_hw_params(ahandle, params);
179         if (err < 0) {
180                 avsys_error_r(AVAUDIO, "snd_pcm_hw_params() : failed! - %s\n", snd_strerror(err));
181                 return AVSYS_STATE_ERR_INTERNAL;
182         }
183
184         /* Dump current param */
185         snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) &val);
186         avsys_info(AVAUDIO, "access type = %s\n", snd_pcm_access_name((snd_pcm_access_t)val));
187
188         snd_pcm_hw_params_get_format(params, &val);
189         avsys_info(AVAUDIO, "format = '%s' (%s)\n",
190                                         snd_pcm_format_name((snd_pcm_format_t)val),
191                                         snd_pcm_format_description((snd_pcm_format_t)val));
192
193         snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);
194         avsys_info(AVAUDIO, "subformat = '%s' (%s)\n",
195                                         snd_pcm_subformat_name((snd_pcm_subformat_t)val),
196                                         snd_pcm_subformat_description((snd_pcm_subformat_t)val));
197
198         snd_pcm_hw_params_get_channels(params, &val);
199         avsys_info(AVAUDIO, "channels = %d\n", val);
200
201         snd_pcm_hw_params_get_rate(params, &val, &dir);
202         avsys_info(AVAUDIO, "rate = %d(%d) Hz\n", val, dir);
203
204         /* Save current rate for later check */
205         handle->rate = val;
206
207         return AVSYS_STATE_SUCCESS;
208 #endif /* _MMFW_I386_ALL_SIMULATOR */
209 }