tizen beta release
[profile/ivi/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 AIF2_DEVICE_NAME "default"
31 #define AIF3_DEVICE_NAME "default"
32 #else
33 #define AIF2_DEVICE_NAME "AIF2"
34 #define AIF3_DEVICE_NAME "AIF3"
35 #endif
36
37 int avsys_audio_alsa_open_AIF_device(const int AIF_type, avsys_audio_alsa_aif_handle_t *handle)
38 {
39         snd_pcm_t *ahandle = NULL;
40         int err = -1;
41         char dev_name[16] = { 0, };
42         snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
43
44         avsys_info(AVAUDIO, "%s\n", __func__);
45         if (!handle)
46                 return AVSYS_STATE_ERR_NULL_POINTER;
47
48         memset(dev_name, '\0', sizeof(dev_name));
49         switch (AIF_type) {
50         case AIF2_CAPTURE:
51                 strncpy(dev_name, AIF2_DEVICE_NAME, sizeof(dev_name) - 1);
52                 stream = SND_PCM_STREAM_CAPTURE;
53                 break;
54         case AIF2_PLAYBACK:
55                 strncpy(dev_name, AIF2_DEVICE_NAME, sizeof(dev_name) - 1);
56                 stream = SND_PCM_STREAM_PLAYBACK;
57                 break;
58         case AIF3_CAPTURE:
59                 strncpy(dev_name, AIF3_DEVICE_NAME, sizeof(dev_name) - 1);
60                 stream = SND_PCM_STREAM_CAPTURE;
61                 break;
62         case AIF3_PLAYBACK:
63                 strncpy(dev_name, AIF3_DEVICE_NAME, sizeof(dev_name) - 1);
64                 stream = SND_PCM_STREAM_PLAYBACK;
65                 break;
66         default:
67                 avsys_critical_r(AVAUDIO, "Invalid AIF device %d\n", AIF_type);
68                 return AVSYS_STATE_ERR_INVALID_MODE;
69                 break;
70         }
71 #if !defined(_MMFW_I386_ALL_SIMULATOR)
72         err = snd_pcm_open(&ahandle, dev_name, stream, 0);
73 #else
74         avsys_warning_r(AVAUDIO, "Skip real device open in SDK\n");
75         err = 0;                /* set fake return value */
76 #endif
77
78         if (err < 0) {
79                 avsys_error_r(AVAUDIO, "unable to open AIF device: %s\n", snd_strerror(err));
80                 return AVSYS_STATE_ERR_INTERNAL;
81         }
82
83         handle->alsa_handle = (void *)ahandle;
84         handle->type = AIF_type;
85
86         return AVSYS_STATE_SUCCESS;
87 }
88
89 int avsys_audio_alsa_close_AIF_device(avsys_audio_alsa_aif_handle_t* handle)
90 {
91         int err = 0;
92         snd_pcm_t *ahandle = NULL;
93
94         avsys_info(AVAUDIO, "%s\n", __func__);
95 #if defined(_MMFW_I386_ALL_SIMULATOR)
96         avsys_warning(AVAUDIO, "Skip close call device in SDK");
97         return AVSYS_STATE_SUCCESS;
98 #endif
99         if (!handle)
100                 return AVSYS_STATE_ERR_NULL_POINTER;
101
102         ahandle = (snd_pcm_t *)handle->alsa_handle;
103         if (!ahandle) {
104                 avsys_error_r(AVAUDIO, "[%s] alsa handle is null\n", __func__);
105                 return AVSYS_STATE_ERR_NULL_POINTER;
106         }
107
108         err = snd_pcm_close(ahandle);
109         if (err < 0) {
110                 avsys_critical_r(AVAUDIO, "unable to close pcm device: %s\n", snd_strerror(err));
111                 return AVSYS_STATE_ERR_INTERNAL;
112         }
113
114         handle->alsa_handle = NULL;
115
116         return AVSYS_STATE_SUCCESS;
117 }
118
119 int avsys_audio_alsa_set_AIF_params(avsys_audio_alsa_aif_handle_t *handle)
120 {
121 #if defined(_MMFW_I386_ALL_SIMULATOR)
122         return AVSYS_STATE_SUCCESS;
123 #else
124         int err = 0;
125         snd_pcm_t *ahandle = NULL;
126         snd_pcm_hw_params_t *params;
127         unsigned int val;
128         int dir;
129
130         avsys_info(AVAUDIO, "%s\n", __func__);
131         if (!handle)
132                 return AVSYS_STATE_ERR_NULL_POINTER;
133
134         ahandle = (snd_pcm_t *)handle->alsa_handle;
135         if (!ahandle)
136                 return AVSYS_STATE_ERR_NULL_POINTER;
137
138         /* Allocate a hardware parameters object. */
139         snd_pcm_hw_params_alloca(&params);
140         /* Fill it in with default values. */
141         snd_pcm_hw_params_any(ahandle, params);
142
143         /* Set the desired hardware parameters. */
144         /* Interleaved mode */
145         snd_pcm_hw_params_set_access(ahandle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
146
147         err = snd_pcm_hw_params(ahandle, params);
148         if (err < 0) {
149                 avsys_error_r(AVAUDIO, "snd_pcm_hw_params() : failed! - %s\n", snd_strerror(err));
150                 return AVSYS_STATE_ERR_INTERNAL;
151         }
152
153         /* Dump current param */
154         snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) &val);
155         avsys_info(AVAUDIO, "access type = %s\n", snd_pcm_access_name((snd_pcm_access_t)val));
156
157         snd_pcm_hw_params_get_format(params, &val);
158         avsys_info(AVAUDIO, "format = '%s' (%s)\n",
159                                         snd_pcm_format_name((snd_pcm_format_t)val),
160                                         snd_pcm_format_description((snd_pcm_format_t)val));
161
162         snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);
163         avsys_info(AVAUDIO, "subformat = '%s' (%s)\n",
164                                         snd_pcm_subformat_name((snd_pcm_subformat_t)val),
165                                         snd_pcm_subformat_description((snd_pcm_subformat_t)val));
166
167         snd_pcm_hw_params_get_channels(params, &val);
168         avsys_info(AVAUDIO, "channels = %d\n", val);
169
170         snd_pcm_hw_params_get_rate(params, &val, &dir);
171         avsys_info(AVAUDIO, "rate = %d bps\n", val);
172
173         return AVSYS_STATE_SUCCESS;
174 #endif
175 }