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.
27 #include "include/mm_sound.h"
28 #include "include/mm_sound_device.h"
29 #include "include/mm_sound_client.h"
31 #define VOLUME_TYPE_LEN 64
33 mm_sound_device_list_t g_device_list;
34 pthread_mutex_t g_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
36 static int _check_for_valid_mask(int 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;
61 ret = MM_ERROR_INVALID_ARGUMENT;
64 debug_error("flags[0x%x] is not valid", flags);
69 static int __convert_device_type_to_enum(char *device_type, mm_sound_device_type_e *device_type_enum)
71 int ret = MM_ERROR_NONE;
73 if (!device_type || !device_type_enum)
74 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-a2dp", VOLUME_TYPE_LEN)) {
85 *device_type_enum = MM_SOUND_DEVICE_TYPE_BLUETOOTH_A2DP;
86 } else if (!strncmp(device_type, "bt-sco", VOLUME_TYPE_LEN)) {
87 *device_type_enum = MM_SOUND_DEVICE_TYPE_BLUETOOTH_SCO;
88 } else if (!strncmp(device_type, "hdmi", VOLUME_TYPE_LEN)) {
89 *device_type_enum = MM_SOUND_DEVICE_TYPE_HDMI;
90 } else if (!strncmp(device_type, "forwarding", VOLUME_TYPE_LEN)) {
91 *device_type_enum = MM_SOUND_DEVICE_TYPE_MIRRORING;
92 } else if (!strncmp(device_type, "usb-audio", VOLUME_TYPE_LEN)) {
93 *device_type_enum = MM_SOUND_DEVICE_TYPE_USB_AUDIO;
95 ret = MM_ERROR_INVALID_ARGUMENT;
96 debug_error("not supported device_type(%s), err(0x%08x)", device_type, ret);
102 static int __free_device_list(mm_sound_device_list_t *device_list_t)
105 return MM_ERROR_INVALID_ARGUMENT;
107 debug_log("free device list %p", device_list_t);
108 g_list_free_full(g_list_first(device_list_t->list), g_free);
109 g_free(device_list_t);
111 return MM_ERROR_NONE;
115 int mm_sound_add_device_connected_callback(int flags, mm_sound_device_connected_cb func, void *user_data, unsigned int *id)
117 int ret = MM_ERROR_NONE;
119 if (func == NULL || id == NULL) {
120 debug_error("argument is not valid");
121 return MM_ERROR_INVALID_ARGUMENT;
123 ret = _check_for_valid_mask(flags);
124 if (ret == MM_ERROR_NONE) {
125 ret = mm_sound_client_add_device_connected_callback(flags, func, user_data, id);
127 debug_error("Could not add device connected callback, ret = %x", ret);
134 int mm_sound_remove_device_connected_callback(unsigned int id)
136 int ret = MM_ERROR_NONE;
138 ret = mm_sound_client_remove_device_connected_callback(id);
140 debug_error("Could not remove device connected callback, ret = %x", ret);
146 int mm_sound_add_device_information_changed_callback(int flags, mm_sound_device_info_changed_cb func, void *user_data, unsigned int *id)
148 int ret = MM_ERROR_NONE;
150 if (func == NULL || id == NULL) {
151 debug_error("argument is not valid");
152 return MM_ERROR_INVALID_ARGUMENT;
154 ret = _check_for_valid_mask(flags);
155 if (ret == MM_ERROR_NONE) {
156 ret = mm_sound_client_add_device_info_changed_callback(flags, func, user_data, id);
158 debug_error("Could not add device information changed callback, ret = %x", ret);
165 int mm_sound_remove_device_information_changed_callback(unsigned int id)
167 int ret = MM_ERROR_NONE;
169 ret = mm_sound_client_remove_device_info_changed_callback(id);
171 debug_error("Could not remove device information changed callback, ret = %x", ret);
177 int mm_sound_add_device_state_changed_callback(int flags, mm_sound_device_state_changed_cb func, void *user_data, unsigned int *id)
179 int ret = MM_ERROR_NONE;
181 if (func == NULL || id == NULL) {
182 debug_error("argument is not valid");
183 return MM_ERROR_INVALID_ARGUMENT;
185 ret = _check_for_valid_mask(flags);
186 if (ret == MM_ERROR_NONE) {
187 ret = mm_sound_client_add_device_state_changed_callback(flags, func, user_data, id);
189 debug_error("Could not add device state changed callback, ret = %x", ret);
196 int mm_sound_remove_device_state_changed_callback(unsigned int id)
198 int ret = MM_ERROR_NONE;
200 ret = mm_sound_client_remove_device_state_changed_callback(id);
202 debug_error("Could not remove device state changed callback, ret = %x", ret);
208 int mm_sound_add_device_running_changed_callback(int flags, mm_sound_device_running_changed_cb func, void *user_data, unsigned int *id)
210 int ret = MM_ERROR_NONE;
212 if (func == NULL || id == NULL) {
213 debug_error("argument is not valid");
214 return MM_ERROR_INVALID_ARGUMENT;
216 ret = _check_for_valid_mask(flags);
217 if (ret == MM_ERROR_NONE) {
218 ret = mm_sound_client_add_device_running_changed_callback(flags, func, user_data, id);
220 debug_error("Could not add device running changed callback, ret = %x", ret);
228 int mm_sound_remove_device_running_changed_callback(unsigned int id)
230 int ret = MM_ERROR_NONE;
232 ret = mm_sound_client_remove_device_running_changed_callback(id);
234 debug_error("Could not remove device running changed callback, ret = %x", ret);
241 int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDeviceList_t *device_list)
243 int ret = MM_ERROR_NONE;
246 return MM_ERROR_INVALID_ARGUMENT;
248 ret = _check_for_valid_mask(flags);
249 if (ret != MM_ERROR_NONE) {
250 debug_error("mask[0x%x] is invalid, ret=0x%x", flags, ret);
254 pthread_mutex_lock(&g_thread_mutex);
256 if (g_device_list.list != NULL) {
257 g_list_free_full(g_device_list.list, g_free);
258 g_device_list.list = NULL;
261 g_device_list.is_new_device_list = true;
263 ret = mm_sound_client_get_current_connected_device_list(flags, &g_device_list);
265 debug_error("Could not get current connected device list, ret = %x", ret);
266 g_device_list.list = NULL;
268 *device_list = &g_device_list;
271 pthread_mutex_unlock(&g_thread_mutex);
277 int mm_sound_get_device_list(int flags, MMSoundDeviceList_t *device_list)
279 int ret = MM_ERROR_NONE;
280 mm_sound_device_list_t *_device_list;
283 return MM_ERROR_INVALID_ARGUMENT;
285 ret = _check_for_valid_mask(flags);
286 if (ret != MM_ERROR_NONE) {
287 debug_error("mask[0x%x] is invalid, ret=0x%x", flags, ret);
291 if (!(_device_list = g_malloc0(sizeof(mm_sound_device_list_t)))) {
292 debug_error("Allocate device list failed");
293 return MM_ERROR_SOUND_INTERNAL;
296 _device_list->is_new_device_list = true;
298 ret = mm_sound_client_get_current_connected_device_list(flags, _device_list);
300 debug_error("Could not get current connected device list, ret = %x", ret);
301 g_free(_device_list);
303 *device_list = _device_list;
310 int mm_sound_free_device_list(MMSoundDeviceList_t device_list)
312 return __free_device_list((mm_sound_device_list_t*) device_list);
316 int mm_sound_free_device(MMSoundDevice_t device_h)
318 if (device_h == NULL)
319 return MM_ERROR_INVALID_ARGUMENT;
323 return MM_ERROR_NONE;
327 int mm_sound_get_device_by_id(int device_id, MMSoundDevice_t *device_h)
329 int ret = MM_ERROR_NONE;
330 mm_sound_device_t *device = NULL;
332 if (device_id < 1 || device_h == NULL)
333 return MM_ERROR_INVALID_ARGUMENT;
335 ret = mm_sound_client_get_device_by_id(device_id, &device);
337 debug_error("Could not get device by id, ret = %x", ret);
345 int mm_sound_get_next_device(MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
347 int ret = MM_ERROR_NONE;
348 mm_sound_device_list_t *device_list_t = NULL;
351 if (!device_list || !device)
352 return MM_ERROR_INVALID_ARGUMENT;
354 device_list_t = (mm_sound_device_list_t*) device_list;
355 if (device_list_t->is_new_device_list)
356 node = g_list_first(device_list_t->list);
358 node = g_list_next(device_list_t->list);
361 ret = MM_ERROR_SOUND_NO_DATA;
363 if (device_list_t->is_new_device_list)
364 device_list_t->is_new_device_list = false;
366 device_list_t->list = node;
368 *device = (mm_sound_device_t*)node->data;
369 debug_log("next device[%p]", *device);
375 int mm_sound_get_prev_device(MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
377 int ret = MM_ERROR_NONE;
378 mm_sound_device_list_t *device_list_t = NULL;
381 if (!device_list || !device)
382 return MM_ERROR_INVALID_ARGUMENT;
384 device_list_t = (mm_sound_device_list_t*) device_list;
385 node = g_list_previous(device_list_t->list);
387 ret = MM_ERROR_SOUND_NO_DATA;
388 debug_error("Could not get previous device, ret = %x", ret);
390 device_list_t->list = node;
391 *device = (mm_sound_device_t*)node->data;
392 debug_log("previous device[%p]", *device);
398 int mm_sound_get_device_type(MMSoundDevice_t device_h, mm_sound_device_type_e *type)
400 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
401 if (!device || !type) {
402 debug_error("invalid argument");
403 return MM_ERROR_INVALID_ARGUMENT;
405 __convert_device_type_to_enum(device->type, type);
406 debug_log("device_handle:%p, type:%d", device, *type);
408 return MM_ERROR_NONE;
412 int mm_sound_get_device_io_direction(MMSoundDevice_t device_h, mm_sound_device_io_direction_e *io_direction)
414 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
416 debug_error("invalid handle");
417 return MM_ERROR_INVALID_ARGUMENT;
419 *io_direction = device->io_direction;
420 debug_log("device_handle:%p, io_direction:%d (1:IN,2:OUT,3:INOUT)", device, *io_direction);
422 return MM_ERROR_NONE;
426 int mm_sound_get_device_id(MMSoundDevice_t device_h, int *id)
428 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
430 debug_error("invalid handle");
431 return MM_ERROR_INVALID_ARGUMENT;
434 debug_log("device_handle:%p, id:%d", device, *id);
436 return MM_ERROR_NONE;
440 int mm_sound_get_device_state(MMSoundDevice_t device_h, mm_sound_device_state_e *state)
442 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
444 debug_error("invalid handle");
445 return MM_ERROR_INVALID_ARGUMENT;
447 *state = device->state;
448 debug_log("device_handle:%p, state:%d (0:INACTIVATED,1:ACTIVATED)", device, *state);
450 return MM_ERROR_NONE;
454 int mm_sound_get_device_name(MMSoundDevice_t device_h, char **name)
456 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
458 debug_error("invalid handle");
459 return MM_ERROR_INVALID_ARGUMENT;
461 *name = device->name;
462 debug_log("device_handle:%p, name:%s", device, *name);
464 return MM_ERROR_NONE;
468 int mm_sound_get_device_vendor_id(MMSoundDevice_t device_h, int *vendor_id)
470 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
472 debug_error("invalid handle");
473 return MM_ERROR_INVALID_ARGUMENT;
475 *vendor_id = device->vendor_id;
476 debug_log("device_handle:%p, vendor id:%04x", device, *vendor_id);
478 return MM_ERROR_NONE;
482 int mm_sound_get_device_product_id(MMSoundDevice_t device_h, int *product_id)
484 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
486 debug_error("invalid handle");
487 return MM_ERROR_INVALID_ARGUMENT;
489 *product_id = device->product_id;
490 debug_log("device_handle:%p, product id:%04x", device, *product_id);
492 return MM_ERROR_NONE;
496 int mm_sound_is_stream_on_device(int stream_id, MMSoundDevice_t device_h, bool *is_on)
498 int ret = MM_ERROR_NONE;
500 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
503 if (!device || !is_on) {
504 debug_error("invalid argument");
505 return MM_ERROR_INVALID_ARGUMENT;
508 if (device->stream_num >= 0) {
509 debug_log("device_handle has stream id");
510 for (i = 0; i < device->stream_num; i++) {
511 if (device->stream_id[i] == stream_id) {
517 debug_log("device_handle dosn't have stream id");
518 /* No information about stream in client-side, should ask to server-side */
519 if ((ret = mm_sound_client_is_stream_on_device(stream_id, device->id, &_is_on)) < 0) {
520 debug_error("Failed to query is stream on");
521 return MM_ERROR_SOUND_INTERNAL;
525 debug_log("device(%d) %s stream(%d)", device->id, _is_on ? "has" : "doesn't have", stream_id);
532 int mm_sound_is_device_running(MMSoundDevice_t device_h, bool *is_running)
534 mm_sound_device_t *device = (mm_sound_device_t*)device_h;
535 if (!device || !is_running) {
536 debug_error("invalid argument");
537 return MM_ERROR_INVALID_ARGUMENT;
539 *is_running = device->is_running;
540 debug_log("device_handle:0x%x, running:%d", device, *is_running);
542 return MM_ERROR_NONE;