focus_server: copy pid of node that will be used for session backward compatibility
[platform/core/multimedia/libmm-sound.git] / mm_sound_device.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sangchul Lee <sc11.lee@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 <stdlib.h>
23 #include <unistd.h>
24
25 #include <mm_debug.h>
26
27 #include "include/mm_sound.h"
28 #include "include/mm_sound_device.h"
29 #include "include/mm_sound_client.h"
30
31 #define VOLUME_TYPE_LEN 64
32
33 mm_sound_device_list_t g_device_list;
34 pthread_mutex_t g_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
35
36 static int _check_for_valid_mask(int flags)
37 {
38         int ret = MM_ERROR_NONE;
39         bool at_least_cond = false;
40
41         if (flags > 0 && flags <= MM_SOUND_DEVICE_ALL_FLAG) {
42                 if (flags & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG)
43                         at_least_cond = true;
44                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG))
45                         at_least_cond = true;
46                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG))
47                         at_least_cond = true;
48                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG))
49                         at_least_cond = true;
50                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG))
51                         at_least_cond = true;
52                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG))
53                         at_least_cond = true;
54                 if (!at_least_cond && (flags & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG))
55                         at_least_cond = true;
56         } else {
57                 ret = MM_ERROR_INVALID_ARGUMENT;
58         }
59
60         if (!at_least_cond)
61                 ret = MM_ERROR_INVALID_ARGUMENT;
62
63         if (ret)
64                 debug_error("flags[0x%x] is not valid", flags);
65
66         return ret;
67 }
68
69 static int __convert_device_type_to_enum(char *device_type, mm_sound_device_type_e *device_type_enum)
70 {
71         int ret = MM_ERROR_NONE;
72
73         if (!device_type || !device_type_enum)
74                 return MM_ERROR_INVALID_ARGUMENT;
75
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;
94         } else {
95                 ret = MM_ERROR_INVALID_ARGUMENT;
96                 debug_error("not supported device_type(%s), err(0x%08x)", device_type, ret);
97         }
98
99         return ret;
100 }
101
102 static int __free_device_list(mm_sound_device_list_t *device_list_t)
103 {
104         if (!device_list_t)
105                 return MM_ERROR_INVALID_ARGUMENT;
106
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);
110
111         return MM_ERROR_NONE;
112 }
113
114 EXPORT_API
115 int mm_sound_add_device_connected_callback(int flags, mm_sound_device_connected_cb func, void *user_data, unsigned int *id)
116 {
117         int ret = MM_ERROR_NONE;
118
119         if (func == NULL || id == NULL) {
120                 debug_error("argument is not valid");
121                 return MM_ERROR_INVALID_ARGUMENT;
122         }
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);
126                 if (ret < 0)
127                         debug_error("Could not add device connected callback, ret = %x", ret);
128         }
129
130         return ret;
131 }
132
133 EXPORT_API
134 int mm_sound_remove_device_connected_callback(unsigned int id)
135 {
136         int ret = MM_ERROR_NONE;
137
138         ret = mm_sound_client_remove_device_connected_callback(id);
139         if (ret < 0)
140                 debug_error("Could not remove device connected callback, ret = %x", ret);
141
142         return ret;
143 }
144
145 EXPORT_API
146 int mm_sound_add_device_information_changed_callback(int flags, mm_sound_device_info_changed_cb func, void *user_data, unsigned int *id)
147 {
148         int ret = MM_ERROR_NONE;
149
150         if (func == NULL || id == NULL) {
151                 debug_error("argument is not valid");
152                 return MM_ERROR_INVALID_ARGUMENT;
153         }
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);
157                 if (ret < 0)
158                         debug_error("Could not add device information changed callback, ret = %x", ret);
159         }
160
161         return ret;
162 }
163
164 EXPORT_API
165 int mm_sound_remove_device_information_changed_callback(unsigned int id)
166 {
167         int ret = MM_ERROR_NONE;
168
169         ret = mm_sound_client_remove_device_info_changed_callback(id);
170         if (ret < 0)
171                 debug_error("Could not remove device information changed callback, ret = %x", ret);
172
173         return ret;
174 }
175
176 EXPORT_API
177 int mm_sound_add_device_state_changed_callback(int flags, mm_sound_device_state_changed_cb func, void *user_data, unsigned int *id)
178 {
179         int ret = MM_ERROR_NONE;
180
181         if (func == NULL || id == NULL) {
182                 debug_error("argument is not valid");
183                 return MM_ERROR_INVALID_ARGUMENT;
184         }
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);
188                 if (ret < 0)
189                         debug_error("Could not add device state changed callback, ret = %x", ret);
190         }
191
192         return ret;
193 }
194
195 EXPORT_API
196 int mm_sound_remove_device_state_changed_callback(unsigned int id)
197 {
198         int ret = MM_ERROR_NONE;
199
200         ret = mm_sound_client_remove_device_state_changed_callback(id);
201         if (ret < 0)
202                 debug_error("Could not remove device state changed callback, ret = %x", ret);
203
204         return ret;
205 }
206
207
208 EXPORT_API
209 int mm_sound_get_current_device_list(mm_sound_device_flags_e flags, MMSoundDeviceList_t *device_list)
210 {
211         int ret = MM_ERROR_NONE;
212
213         if (!device_list)
214                 return MM_ERROR_INVALID_ARGUMENT;
215
216         ret = _check_for_valid_mask(flags);
217         if (ret != MM_ERROR_NONE) {
218                 debug_error("mask[0x%x] is invalid, ret=0x%x", flags, ret);
219                 return ret;
220         }
221
222         pthread_mutex_lock(&g_thread_mutex);
223
224         if (g_device_list.list != NULL) {
225                 g_list_free_full(g_device_list.list, g_free);
226                 g_device_list.list = NULL;
227         }
228
229         g_device_list.is_new_device_list = true;
230
231         ret = mm_sound_client_get_current_connected_device_list(flags, &g_device_list);
232         if (ret < 0) {
233                 debug_error("Could not get current connected device list, ret = %x", ret);
234                 g_device_list.list = NULL;
235         } else {
236                 *device_list = &g_device_list;
237         }
238
239         pthread_mutex_unlock(&g_thread_mutex);
240
241         return ret;
242 }
243
244 EXPORT_API
245 int mm_sound_get_device_list(int flags, MMSoundDeviceList_t *device_list)
246 {
247         int ret = MM_ERROR_NONE;
248         mm_sound_device_list_t *_device_list;
249
250         if (!device_list)
251                 return MM_ERROR_INVALID_ARGUMENT;
252
253         ret = _check_for_valid_mask(flags);
254         if (ret != MM_ERROR_NONE) {
255                 debug_error("mask[0x%x] is invalid, ret=0x%x", flags, ret);
256                 return ret;
257         }
258
259         if (!(_device_list = g_malloc0(sizeof(mm_sound_device_list_t)))) {
260                 debug_error("Allocate device list failed");
261                 return MM_ERROR_SOUND_INTERNAL;
262         }
263
264         _device_list->is_new_device_list = true;
265
266         ret = mm_sound_client_get_current_connected_device_list(flags, _device_list);
267         if (ret < 0) {
268                 debug_error("Could not get current connected device list, ret = %x", ret);
269                 g_free(_device_list);
270         } else {
271                 *device_list = _device_list;
272         }
273
274         return ret;
275 }
276
277 EXPORT_API
278 int mm_sound_free_device_list(MMSoundDeviceList_t device_list)
279 {
280         return __free_device_list((mm_sound_device_list_t*) device_list);
281 }
282
283 EXPORT_API
284 int mm_sound_free_device(MMSoundDevice_t device_h)
285 {
286         if (device_h == NULL)
287                 return MM_ERROR_INVALID_ARGUMENT;
288
289         g_free(device_h);
290
291         return MM_ERROR_NONE;
292 }
293
294 EXPORT_API
295 int mm_sound_get_device_by_id(int device_id, MMSoundDevice_t *device_h)
296 {
297         int ret = MM_ERROR_NONE;
298         mm_sound_device_t *device = NULL;
299
300         if (device_id < 1 || device_h == NULL)
301                 return MM_ERROR_INVALID_ARGUMENT;
302
303         ret = mm_sound_client_get_device_by_id(device_id, &device);
304         if (ret < 0)
305                 debug_error("Could not get device by id, ret = %x", ret);
306         else
307                 *device_h = device;
308
309         return ret;
310 }
311
312 EXPORT_API
313 int mm_sound_get_next_device(MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
314 {
315         int ret = MM_ERROR_NONE;
316         mm_sound_device_list_t *device_list_t = NULL;
317         GList *node = NULL;
318
319         if (!device_list || !device)
320                 return MM_ERROR_INVALID_ARGUMENT;
321
322         device_list_t = (mm_sound_device_list_t*) device_list;
323         if (device_list_t->is_new_device_list)
324                 node = g_list_first(device_list_t->list);
325         else
326                 node = g_list_next(device_list_t->list);
327
328         if (!node) {
329                 ret = MM_ERROR_SOUND_NO_DATA;
330         } else {
331                 if (device_list_t->is_new_device_list)
332                         device_list_t->is_new_device_list = false;
333                 else
334                         device_list_t->list = node;
335
336                 *device = (mm_sound_device_t*)node->data;
337                 debug_log("next device[0x%x]", *device);
338         }
339         return ret;
340 }
341
342 EXPORT_API
343 int mm_sound_get_prev_device(MMSoundDeviceList_t device_list, MMSoundDevice_t *device)
344 {
345         int ret = MM_ERROR_NONE;
346         mm_sound_device_list_t *device_list_t = NULL;
347         GList *node = NULL;
348
349         if (!device_list || !device)
350                 return MM_ERROR_INVALID_ARGUMENT;
351
352         device_list_t = (mm_sound_device_list_t*) device_list;
353         node = g_list_previous(device_list_t->list);
354         if (!node) {
355                 ret = MM_ERROR_SOUND_NO_DATA;
356                 debug_error("Could not get previous device, ret = %x", ret);
357         } else {
358                 device_list_t->list = node;
359                 *device = (mm_sound_device_t*)node->data;
360                 debug_log("previous device[0x%x]", *device);
361         }
362         return ret;
363 }
364
365 EXPORT_API
366 int mm_sound_get_device_type(MMSoundDevice_t device_h, mm_sound_device_type_e *type)
367 {
368         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
369         if (!device || !type) {
370                 debug_error("invalid argument");
371                 return MM_ERROR_INVALID_ARGUMENT;
372         }
373         __convert_device_type_to_enum(device->type, type);
374         debug_log("device_handle:0x%x, type:%d", device, *type);
375
376         return MM_ERROR_NONE;
377 }
378
379 EXPORT_API
380 int mm_sound_get_device_io_direction(MMSoundDevice_t device_h, mm_sound_device_io_direction_e *io_direction)
381 {
382         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
383         if (!device) {
384                 debug_error("invalid handle");
385                 return MM_ERROR_INVALID_ARGUMENT;
386         }
387         *io_direction = device->io_direction;
388         debug_log("device_handle:0x%x, io_direction:%d (1:IN,2:OUT,3:INOUT)", device, *io_direction);
389
390         return MM_ERROR_NONE;
391 }
392
393 EXPORT_API
394 int mm_sound_get_device_id(MMSoundDevice_t device_h, int *id)
395 {
396         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
397         if (!device) {
398                 debug_error("invalid handle");
399                 return MM_ERROR_INVALID_ARGUMENT;
400         }
401         *id = device->id;
402         debug_log("device_handle:0x%x, id:%d", device, *id);
403
404         return MM_ERROR_NONE;
405 }
406
407 EXPORT_API
408 int mm_sound_get_device_state(MMSoundDevice_t device_h, mm_sound_device_state_e *state)
409 {
410         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
411         if (!device) {
412                 debug_error("invalid handle");
413                 return MM_ERROR_INVALID_ARGUMENT;
414         }
415         *state = device->state;
416         debug_log("device_handle:0x%x, state:%d (0:INACTIVATED,1:ACTIVATED)", device, *state);
417
418         return MM_ERROR_NONE;
419 }
420
421 EXPORT_API
422 int mm_sound_get_device_name(MMSoundDevice_t device_h, char **name)
423 {
424         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
425         if (!device) {
426                 debug_error("invalid handle");
427                 return MM_ERROR_INVALID_ARGUMENT;
428         }
429         *name = device->name;
430         debug_log("device_handle:0x%x, name:%s", device, *name);
431
432         return MM_ERROR_NONE;
433 }
434
435 EXPORT_API
436 int mm_sound_get_device_vendor_id(MMSoundDevice_t device_h, int *vendor_id)
437 {
438         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
439         if (!device) {
440                 debug_error("invalid handle");
441                 return MM_ERROR_INVALID_ARGUMENT;
442         }
443         *vendor_id = device->vendor_id;
444         debug_log("device_handle:0x%x, vendor id:%04x", device, *vendor_id);
445
446         return MM_ERROR_NONE;
447 }
448
449 EXPORT_API
450 int mm_sound_get_device_product_id(MMSoundDevice_t device_h, int *product_id)
451 {
452         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
453         if (!device) {
454                 debug_error("invalid handle");
455                 return MM_ERROR_INVALID_ARGUMENT;
456         }
457         *product_id = device->product_id;
458         debug_log("device_handle:0x%x, product id:%04x", device, *product_id);
459
460         return MM_ERROR_NONE;
461 }
462
463 EXPORT_API
464 int mm_sound_is_stream_on_device(int stream_id, MMSoundDevice_t device_h, bool *is_on)
465 {
466         int ret = MM_ERROR_NONE;
467         int i;
468         mm_sound_device_t *device = (mm_sound_device_t*)device_h;
469         bool _is_on = false;
470
471         if (!device || !is_on) {
472                 debug_error("invalid argument");
473                 return MM_ERROR_INVALID_ARGUMENT;
474         }
475
476         if (device->stream_num >= 0) {
477                 debug_log("device_handle has stream id");
478                 for (i = 0; i < device->stream_num; i++) {
479                         if (device->stream_id[i] == stream_id) {
480                                 _is_on = true;
481                                 break;
482                         }
483                 }
484         } else {
485                 debug_log("device_handle dosn't have stream id");
486                 /* No information about stream in client-side, should ask to server-side */
487                 if ((ret = mm_sound_client_is_stream_on_device(stream_id, device->id, &_is_on)) < 0) {
488                         debug_error("Failed to query is stream on");
489                         return MM_ERROR_SOUND_INTERNAL;
490                 }
491         }
492
493         debug_log("device(%d) %s stream(%d)", device->id, _is_on ? "has" : "doesn't have", stream_id);
494         *is_on = _is_on;
495
496         return ret;
497 }