summaryrefslogtreecommitdiff
path: root/avsys-audio-alsa.c
blob: 4b8072e1fa17ebff52446358137ad2711f6fac04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * avsystem
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <alsa/asoundlib.h>

#include "avsys-audio-alsa.h"
#include "avsys-types.h"
#include "avsys-error.h"
#include "avsys-debug.h"

#if defined(_MMFW_I386_ALL_SIMULATOR)
#define AIF_CP_DEVICE_NAME "default"
#define AIF_BT_DEVICE_NAME "default"
#define AIF_RADIO_DEVICE_NAME "default"
#else
#define AIF_CP_DEVICE_NAME "AIF2"
#define AIF_BT_DEVICE_NAME "AIF3"
#define AIF_RADIO_DEVICE_NAME "AIF4"
#endif

int avsys_audio_alsa_open_AIF_device(aif_device_type_t aif_type, avsys_audio_alsa_aif_handle_t *handle)
{
	snd_pcm_t *ahandle = NULL;
	int err = -1;
	char dev_name[16] = { 0, };
	snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;

	avsys_info(AVAUDIO, "%s\n", __func__);
	if (!handle)
		return AVSYS_STATE_ERR_NULL_POINTER;

	memset(dev_name, '\0', sizeof(dev_name));
	switch (aif_type) {
	case AIF_CP_CAPTURE:
		strncpy(dev_name, AIF_CP_DEVICE_NAME, sizeof(dev_name) - 1);
		stream = SND_PCM_STREAM_CAPTURE;
		break;
	case AIF_CP_PLAYBACK:
		strncpy(dev_name, AIF_CP_DEVICE_NAME, sizeof(dev_name) - 1);
		stream = SND_PCM_STREAM_PLAYBACK;
		break;
	case AIF_BT_CAPTURE:
		strncpy(dev_name, AIF_BT_DEVICE_NAME, sizeof(dev_name) - 1);
		stream = SND_PCM_STREAM_CAPTURE;
		break;
	case AIF_BT_PLAYBACK:
		strncpy(dev_name, AIF_BT_DEVICE_NAME, sizeof(dev_name) - 1);
		stream = SND_PCM_STREAM_PLAYBACK;
		break;
	case AIF_RADIO_PLAYBACK:
		strncpy(dev_name, AIF_RADIO_DEVICE_NAME, sizeof(dev_name) - 1);
		stream = SND_PCM_STREAM_PLAYBACK;
		break;
	default:
		avsys_critical_r(AVAUDIO, "Invalid AIF device %d\n", aif_type);
		return AVSYS_STATE_ERR_INVALID_MODE;
		break;
	}
#if !defined(_MMFW_I386_ALL_SIMULATOR)
	err = snd_pcm_open(&ahandle, dev_name, stream, 0);
#else
	avsys_warning_r(AVAUDIO, "Skip real device open in SDK\n");
	err = 0;		/* set fake return value */
#endif

	if (err < 0) {
		avsys_error_r(AVAUDIO, "unable to open AIF device: %s\n", snd_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	handle->alsa_handle = (void *)ahandle;
	handle->type = aif_type;

	return AVSYS_STATE_SUCCESS;
}

int avsys_audio_alsa_close_AIF_device(avsys_audio_alsa_aif_handle_t* handle)
{
	int err = 0;
	snd_pcm_t *ahandle = NULL;

	avsys_info(AVAUDIO, "%s\n", __func__);
#if defined(_MMFW_I386_ALL_SIMULATOR)
	avsys_warning(AVAUDIO, "Skip close call device in SDK");
	return AVSYS_STATE_SUCCESS;
#endif
	if (!handle)
		return AVSYS_STATE_ERR_NULL_POINTER;

	ahandle = (snd_pcm_t *)handle->alsa_handle;
	if (!ahandle) {
		avsys_error_r(AVAUDIO, "[%s] alsa handle is null\n", __func__);
		return AVSYS_STATE_ERR_NULL_POINTER;
	}

	err = snd_pcm_close(ahandle);
	if (err < 0) {
		avsys_critical_r(AVAUDIO, "unable to close pcm device: %s\n", snd_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	handle->alsa_handle = NULL;

	return AVSYS_STATE_SUCCESS;
}

int avsys_audio_alsa_set_AIF_params(avsys_audio_alsa_aif_handle_t *handle, aif_rate_t rate)
{
#if defined(_MMFW_I386_ALL_SIMULATOR)
	return AVSYS_STATE_SUCCESS;
#else
	int err = 0;
	snd_pcm_t *ahandle = NULL;
	snd_pcm_hw_params_t *params;
	unsigned int val = 0;
	int dir = 0;

	avsys_info(AVAUDIO, "%s\n", __func__);
	if (!handle)
		return AVSYS_STATE_ERR_NULL_POINTER;

	ahandle = (snd_pcm_t *)handle->alsa_handle;
	if (!ahandle)
		return AVSYS_STATE_ERR_NULL_POINTER;

	/* Skip parameter setting to null device. */
	if (snd_pcm_type(ahandle) == SND_PCM_TYPE_NULL)
		return AVSYS_STATE_SUCCESS;

	/* Allocate a hardware parameters object. */
	snd_pcm_hw_params_alloca(&params);
	/* Fill it in with default values. */
	err = snd_pcm_hw_params_any(ahandle, params);
	if (err < 0) {
		avsys_error_r(AVAUDIO, "snd_pcm_hw_params_any() : failed! - %s\n", snd_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	/* Set the desired hardware parameters. */
	/* Interleaved mode */
	err = snd_pcm_hw_params_set_access(ahandle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
	if (err < 0) {
		avsys_error_r(AVAUDIO, "snd_pcm_hw_params_set_access() : failed! - %s\n", snd_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	if (rate == AIF_CONF_RATE) {
		/* Auto rate by conf */
		avsys_info(AVAUDIO, "Let rate by configuration\n");
	} else {
		/* Force input rate */
		avsys_info(AVAUDIO, "Force rate (%d)\n", rate);
		err = snd_pcm_hw_params_set_rate(ahandle, params, rate, 0);
		if (err < 0) {
			avsys_error_r(AVAUDIO, "snd_pcm_hw_params_set_rate() : failed! - %s\n", snd_strerror(err));
			return AVSYS_STATE_ERR_INTERNAL;
		}
	}

	err = snd_pcm_hw_params(ahandle, params);
	if (err < 0) {
		avsys_error_r(AVAUDIO, "snd_pcm_hw_params() : failed! - %s\n", snd_strerror(err));
		return AVSYS_STATE_ERR_INTERNAL;
	}

	/* Dump current param */
	snd_pcm_hw_params_get_access(params, (snd_pcm_access_t *) &val);
	avsys_info(AVAUDIO, "access type = %s\n", snd_pcm_access_name((snd_pcm_access_t)val));

	snd_pcm_hw_params_get_format(params, &val);
	avsys_info(AVAUDIO, "format = '%s' (%s)\n",
					snd_pcm_format_name((snd_pcm_format_t)val),
					snd_pcm_format_description((snd_pcm_format_t)val));

	snd_pcm_hw_params_get_subformat(params, (snd_pcm_subformat_t *)&val);
	avsys_info(AVAUDIO, "subformat = '%s' (%s)\n",
					snd_pcm_subformat_name((snd_pcm_subformat_t)val),
					snd_pcm_subformat_description((snd_pcm_subformat_t)val));

	snd_pcm_hw_params_get_channels(params, &val);
	avsys_info(AVAUDIO, "channels = %d\n", val);

	snd_pcm_hw_params_get_rate(params, &val, &dir);
	avsys_info(AVAUDIO, "rate = %d(%d) Hz\n", val, dir);

	/* Save current rate for later check */
	handle->rate = val;

	return AVSYS_STATE_SUCCESS;
#endif /* _MMFW_I386_ALL_SIMULATOR */
}