4 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Sangchul Lee <sc11.lee@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 #include "include/mm_sound.h"
30 #include "include/mm_sound_device.h"
31 #include "include/mm_sound_client.h"
33 #define VOLUME_TYPE_LEN 64
36 static int _check_for_valid_mask (mm_sound_device_flags_e flags)
38 int ret = MM_ERROR_NONE;
39 bool at_least_cond = false;
41 if (flags > 0 && flags <= MM_SOUND_DEVICE_ALL_FLAG) {
42 if (flags & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG)
44 if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG))
46 if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG))
48 if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG))
50 if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG))
52 if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG))
54 if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG))
57 ret = MM_ERROR_INVALID_ARGUMENT;
60 ret = MM_ERROR_INVALID_ARGUMENT;
63 debug_error("flags[0x%x] is not valid\n", flags);
68 static int __convert_device_type_to_enum (char *device_type, mm_sound_device_type_e *device_type_enum)
70 int ret = MM_ERROR_NONE;
72 if (!device_type || !device_type_enum) {
73 return MM_ERROR_INVALID_ARGUMENT;
76 if (!strncmp(device_type, "builtin-speaker", VOLUME_TYPE_LEN)) {
77 *device_type_enum = MM_SOUND_DEVICE_TYPE_BUILTIN_SPEAKER;
78 } else if (!strncmp(device_type, "builtin-receiver", VOLUME_TYPE_LEN)) {
79 *device_type_enum = MM_SOUND_DEVICE_TYPE_BUILTIN_RECEIVER;
80 } else if (!strncmp(device_type, "builtin-mic", VOLUME_TYPE_LEN)) {
81 *device_type_enum = MM_SOUND_DEVICE_TYPE_BUILTIN_MIC;
82 } else if (!strncmp(device_type, "audio-jack", VOLUME_TYPE_LEN)) {
83 *device_type_enum = MM_SOUND_DEVICE_TYPE_AUDIOJACK;
84 } else if (!strncmp(device_type, "bt", VOLUME_TYPE_LEN)) {
85 *device_type_enum = MM_SOUND_DEVICE_TYPE_BLUETOOTH;
86 } else if (!strncmp(device_type, "hdmi", VOLUME_TYPE_LEN)) {
87 *device_type_enum = MM_SOUND_DEVICE_TYPE_HDMI;
88 } else if (!strncmp(device_type, "forwarding", VOLUME_TYPE_LEN)) {
89 *device_type_enum = MM_SOUND_DEVICE_TYPE_MIRRORING;
90 } else if (!strncmp(device_type, "usb-audio", VOLUME_TYPE_LEN)) {
91 *device_type_enum = MM_SOUND_DEVICE_TYPE_USB_AUDIO;
93 ret = MM_ERROR_INVALID_ARGUMENT;
94 debug_error("not supported device_type(%s), err(0x%08x)", device_type, ret);
101 int mm_sound_add_device_connected_callback(mm_sound_device_flags_e flags, mm_sound_device_connected_cb func, void *user_data, unsigned int *subs_id)
103 int ret = MM_ERROR_NONE;
105 if (func == NULL || subs_id == NULL) {
106 debug_error("argument is not valid\n");
107 return MM_ERROR_INVALID_ARGUMENT;
109 ret = _check_for_valid_mask(flags);
110 if (ret == MM_ERROR_NONE) {
111 ret = mm_sound_client_add_device_connected_callback(flags, func, user_data, subs_id);
113 debug_error("Could not add device connected callback, ret = %x\n", ret);
121 int mm_sound_remove_device_connected_callback(unsigned int subs_id)
123 int ret = MM_ERROR_NONE;
125 ret = mm_sound_client_remove_device_connected_callback(subs_id);
127 debug_error("Could not remove device connected callback, ret = %x\n", ret);
134 int mm_sound_add_device_information_changed_callback(mm_sound_device_flags_e flags, mm_sound_device_info_changed_cb func, void *user_data, unsigned int *subs_id)
136 int ret = MM_ERROR_NONE;
138 if (func == NULL || subs_id == NULL) {
139 debug_error("argument is not valid\n");
140 return MM_ERROR_INVALID_ARGUMENT;
142 ret = _check_for_valid_mask(flags);
143 if (ret == MM_ERROR_NONE) {
144 ret = mm_sound_client_add_device_info_changed_callback(flags, func, user_data, subs_id);
146 debug_error("Could not add device information changed callback, ret = %x\n", ret);
154 int mm_sound_remove_device_information_changed_callback(unsigned int subs_id)
156 int ret = MM_ERROR_NONE;
158 ret = mm_sound_client_remove_device_info_changed_callback(subs_id);
160 debug_error("Could not remove device information changed callback, ret = %x\n", ret);
167 int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDeviceList_t *device_list)
169 int ret = MM_ERROR_NONE;
170 mm_sound_device_list_t *_device_list;
173 return MM_ERROR_INVALID_ARGUMENT;
175 ret = _check_for_valid_mask(flags);
176 if (ret != MM_ERROR_NONE) {
177 debug_error("mask[0x%x] is invalid, ret=0x%x", flags, ret);
181 if (!(_device_list = g_malloc0(sizeof(mm_sound_device_list_t)))) {
182 debug_error("[Client] Allocate device list failed");
183 return MM_ERROR_SOUND_INTERNAL;
186 _device_list->is_new_device_list = true;
188 ret = mm_sound_client_get_current_connected_device_list(flags, _device_list);
190 debug_error("Could not get current connected device list, ret = %x\n", ret);
191 g_free(_device_list);
193 *device_list = _device_list;
200 int mm_sound_free_device_list(MMSoundDeviceList_t device_list)
202 mm_sound_device_list_t *device_list_t = NULL;
205 return MM_ERROR_INVALID_ARGUMENT;
207 device_list_t = (mm_sound_device_list_t*) device_list;
208 g_list_free_full(device_list_t->list, g_free);
209 g_free(device_list_t);
211 return MM_ERROR_NONE;
215 int mm_sound_get_next_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
217 int ret = MM_ERROR_NONE;
218 mm_sound_device_list_t *device_list_t = NULL;
220 if (!device_list || !device) {
221 return MM_ERROR_INVALID_ARGUMENT;
223 device_list_t = (mm_sound_device_list_t*) device_list;
224 if (device_list_t->is_new_device_list) {
225 node = g_list_first(device_list_t->list);
227 node = g_list_next(device_list_t->list);
230 ret = MM_ERROR_SOUND_NO_DATA;
232 if (device_list_t->is_new_device_list) {
233 device_list_t->is_new_device_list = false;
235 device_list_t->list = node;
237 *device = (mm_sound_device_t*)node->data;
238 debug_log("next device[0x%x]\n", *device);
244 int mm_sound_get_prev_device (MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
246 int ret = MM_ERROR_NONE;
247 mm_sound_device_list_t *device_list_t = NULL;
249 if (!device_list || !device) {
250 return MM_ERROR_INVALID_ARGUMENT;
252 device_list_t = (mm_sound_device_list_t*) device_list;
253 node = g_list_previous(device_list_t->list);
255 ret = MM_ERROR_SOUND_NO_DATA;
256 debug_error("Could not get previous device, ret = %x\n", ret);
258 device_list_t->list = node;
259 *device = (mm_sound_device_t*)node->data;
260 debug_log("previous device[0x%x]\n", *device);
266 int mm_sound_get_device_type(MMSoundDevice_t device_h, mm_sound_device_type_e *type)
268 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
269 if(!device || !type) {
270 debug_error("invalid argument\n");
271 return MM_ERROR_INVALID_ARGUMENT;
273 __convert_device_type_to_enum(device->type, type);
274 debug_log("device_handle:0x%x, type:%d\n", device, *type);
276 return MM_ERROR_NONE;
280 int mm_sound_get_device_io_direction(MMSoundDevice_t device_h, mm_sound_device_io_direction_e *io_direction)
282 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
284 debug_error("invalid handle\n");
285 return MM_ERROR_INVALID_ARGUMENT;
287 *io_direction = device->io_direction;
288 debug_log("device_handle:0x%x, io_direction:%d (1:IN,2:OUT,3:INOUT)\n", device, *io_direction);
290 return MM_ERROR_NONE;
294 int mm_sound_get_device_id(MMSoundDevice_t device_h, int *id)
296 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
298 debug_error("invalid handle\n");
299 return MM_ERROR_INVALID_ARGUMENT;
302 debug_log("device_handle:0x%x, id:%d\n", device, *id);
304 return MM_ERROR_NONE;
308 int mm_sound_get_device_state(MMSoundDevice_t device_h, mm_sound_device_state_e *state)
310 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
312 debug_error("invalid handle\n");
313 return MM_ERROR_INVALID_ARGUMENT;
315 *state = device->state;
316 debug_log("device_handle:0x%x, state:%d (0:INACTIVATED,1:ACTIVATED)\n", device, *state);
318 return MM_ERROR_NONE;
322 int mm_sound_get_device_name(MMSoundDevice_t device_h, char **name)
324 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
326 debug_error("invalid handle\n");
327 return MM_ERROR_INVALID_ARGUMENT;
329 *name = device->name;
330 debug_log("device_handle:0x%x, name:%s\n", device, *name);
332 return MM_ERROR_NONE;