Merge branch 'tizen_3.0' into tizen
[platform/core/multimedia/libmm-sound.git] / mm_sound_focus.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2015 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_client.h"
29
30 #define RETURN_ERROR_IF_FOCUS_CB_THREAD(x_thread) \
31 { \
32         int ret = MM_ERROR_NONE; \
33         bool result = false; \
34         ret = mm_sound_client_is_focus_cb_thread(x_thread, &result); \
35         if (ret) \
36                 return ret; \
37         if (result) { \
38                 debug_error("it might be called in the thread of focus callback, it is not allowed\n"); \
39                 return MM_ERROR_SOUND_INVALID_OPERATION; \
40         } \
41 } \
42
43 EXPORT_API
44 int mm_sound_focus_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void *user_data)
45 {
46         int ret = MM_ERROR_NONE;
47         debug_fenter();
48
49         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
50
51         if (!callback)
52                 return MM_ERROR_INVALID_ARGUMENT;
53
54         ret = mm_sound_client_set_session_interrupt_callback (callback, user_data);
55
56         debug_fleave();
57
58         return ret;
59 }
60
61 EXPORT_API
62 int mm_sound_focus_unset_session_interrupt_callback(void)
63 {
64         int ret = MM_ERROR_NONE;
65         debug_fenter();
66
67         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
68
69         ret = mm_sound_client_unset_session_interrupt_callback ();
70         if (ret) {
71                 debug_error("Failed to mm_sound_client_unset_session_interrupt_callback(), ret[0x%x]\n", ret);
72         }
73
74         debug_fleave();
75
76         return ret;
77 }
78
79 EXPORT_API
80 int mm_sound_focus_get_id(int *id)
81 {
82         int ret = MM_ERROR_NONE;
83
84         debug_fenter();
85
86         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
87
88         ret = mm_sound_client_get_unique_id(id);
89         if (ret) {
90                 debug_error("Failed to mm_sound_client_get_unique_id(), ret[0x%x]\n", ret);
91         }
92
93         debug_fleave();
94
95         return ret;
96 }
97
98 EXPORT_API
99 int mm_sound_focus_is_cb_thread(bool *result)
100 {
101         int ret = MM_ERROR_NONE;
102
103         debug_fenter();
104
105         ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), result);
106         if (!ret) {
107                 if (*result)
108                         debug_msg("it might be called in the thread of focus callback\n");
109         }
110
111         debug_fleave();
112
113         return ret;
114 }
115
116 EXPORT_API
117 int mm_sound_register_focus(int id, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
118 {
119         int ret = MM_ERROR_NONE;
120
121         debug_fenter();
122
123         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
124
125         if (id < 0 || callback == NULL) {
126                 debug_error("argument is not valid\n");
127                 return MM_ERROR_INVALID_ARGUMENT;
128         }
129
130         ret = mm_sound_client_register_focus(id, getpid(), stream_type, callback, false, user_data);
131         if (ret) {
132                 debug_error("Could not register focus, ret[0x%x]\n", ret);
133         }
134
135         debug_fleave();
136
137         return ret;
138 }
139
140 EXPORT_API
141 int mm_sound_register_focus_for_session(int id, int pid, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
142 {
143         int ret = MM_ERROR_NONE;
144
145         debug_fenter();
146
147         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
148
149         if (id < 0 || callback == NULL) {
150                 debug_error("argument is not valid\n");
151                 return MM_ERROR_INVALID_ARGUMENT;
152         }
153
154         ret = mm_sound_client_register_focus(id, pid, stream_type, callback, true, user_data);
155         if (ret) {
156                 debug_error("Could not register focus for session, ret[0x%x]\n", ret);
157         }
158
159         debug_fleave();
160
161         return ret;
162 }
163
164 EXPORT_API
165 int mm_sound_unregister_focus(int id)
166 {
167         int ret = MM_ERROR_NONE;
168         bool result = false;
169
170         debug_fenter();
171
172         if (id < 0) {
173                 debug_error("argument is not valid\n");
174                 return MM_ERROR_INVALID_ARGUMENT;
175         }
176
177         mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
178         if (!result) {
179                 if ((ret = mm_sound_client_unregister_focus(id)))
180                         debug_error("Could not unregister focus, ret = %x\n", ret);
181         } else {
182                 ret = mm_sound_client_execute_focus_func_in_main_context(IDLE_EVENT_TYPE_UNREGISTER_FOCUS, id);
183         }
184
185         debug_fleave();
186
187         return ret;
188 }
189
190 EXPORT_API
191 int mm_sound_set_focus_reacquisition(int id, bool reacquisition)
192 {
193         int ret = MM_ERROR_NONE;
194
195         debug_fenter();
196
197         if (id < 0) {
198                 debug_error("argument is not valid\n");
199                 return MM_ERROR_INVALID_ARGUMENT;
200         }
201
202         ret = mm_sound_client_set_focus_reacquisition(id, reacquisition);
203         if (ret) {
204                 debug_error("Could not set focus reacquisition, ret[0x%x]\n", ret);
205         }
206
207         debug_fleave();
208
209         return ret;
210 }
211
212 EXPORT_API
213 int mm_sound_get_focus_reacquisition(int id, bool *reacquisition)
214 {
215         int ret = MM_ERROR_NONE;
216
217         debug_fenter();
218
219         if (id < 0 || !reacquisition) {
220                 debug_error("argument is not valid\n");
221                 return MM_ERROR_INVALID_ARGUMENT;
222         }
223
224         ret = mm_sound_client_get_focus_reacquisition(id, reacquisition);
225         if (ret) {
226                 debug_error("Could not get focus reacquisition, ret[0x%x]\n", ret);
227         }
228
229         debug_fleave();
230
231         return ret;
232 }
233
234 EXPORT_API
235 int mm_sound_get_stream_type_of_acquired_focus(int focus_type, char **stream_type, int *option, char **ext_info)
236 {
237         int ret = MM_ERROR_NONE;
238
239         debug_fenter();
240
241         if (stream_type == NULL) {
242                 debug_error("argument is not valid\n");
243                 return MM_ERROR_INVALID_ARGUMENT;
244         }
245
246         ret = mm_sound_client_get_acquired_focus_stream_type(focus_type, stream_type, option, ext_info);
247         if (ret) {
248                 debug_error("Could not get acquired focus stream type, ret[0x%x]\n", ret);
249         }
250
251         debug_fleave();
252
253         return ret;
254 }
255
256 EXPORT_API
257 int mm_sound_acquire_focus(int id, mm_sound_focus_type_e focus_type, const char *ext_info)
258 {
259         int ret = MM_ERROR_NONE;
260
261         debug_fenter();
262
263         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
264
265         if (id < 0) {
266                 debug_error("argument is not valid\n");
267                 return MM_ERROR_INVALID_ARGUMENT;
268         }
269         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
270                 debug_error("argument is not valid\n");
271                 return MM_ERROR_INVALID_ARGUMENT;
272         }
273
274         ret = mm_sound_client_acquire_focus(id, focus_type, 0, ext_info);
275         if (ret) {
276                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
277         }
278
279         debug_fleave();
280
281         return ret;
282 }
283
284 EXPORT_API
285 int mm_sound_release_focus(int id, mm_sound_focus_type_e focus_type, const char *ext_info)
286 {
287         int ret = MM_ERROR_NONE;
288
289         debug_fenter();
290
291         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
292
293         if (id < 0) {
294                 debug_error("argument is not valid\n");
295                 return MM_ERROR_INVALID_ARGUMENT;
296         }
297         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
298                 debug_error("argument is not valid\n");
299                 return MM_ERROR_INVALID_ARGUMENT;
300         }
301
302         ret = mm_sound_client_release_focus(id, focus_type, 0, ext_info);
303         if (ret) {
304                 debug_error("Could not release focus, ret[0x%x]\n", ret);
305         }
306
307         debug_fleave();
308
309         return ret;
310 }
311
312 EXPORT_API
313 int mm_sound_acquire_focus_with_option(int id, mm_sound_focus_type_e focus_type, int option, const char *ext_info)
314 {
315         int ret = MM_ERROR_NONE;
316
317         debug_fenter();
318
319         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
320
321         if (id < 0) {
322                 debug_error("id is not valid\n");
323                 return MM_ERROR_INVALID_ARGUMENT;
324         }
325
326         if (option < 0) {
327                 debug_error("option is not valid\n");
328                 return MM_ERROR_INVALID_ARGUMENT;
329         }
330
331         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
332                 debug_error("focus type is not valid\n");
333                 return MM_ERROR_INVALID_ARGUMENT;
334         }
335
336         ret = mm_sound_client_acquire_focus(id, focus_type, option, ext_info);
337         if (ret) {
338                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
339         }
340
341         debug_fleave();
342
343         return ret;
344 }
345
346 EXPORT_API
347 int mm_sound_release_focus_with_option(int id, mm_sound_focus_type_e focus_type, int option, const char *ext_info)
348 {
349         int ret = MM_ERROR_NONE;
350
351         debug_fenter();
352
353         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
354
355         if (id < 0) {
356                 debug_error("id is not valid\n");
357                 return MM_ERROR_INVALID_ARGUMENT;
358         }
359
360         if (option < 0) {
361                 debug_error("option is not valid\n");
362                 return MM_ERROR_INVALID_ARGUMENT;
363         }
364
365         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
366                 debug_error("focus type is not valid\n");
367                 return MM_ERROR_INVALID_ARGUMENT;
368         }
369
370         ret = mm_sound_client_release_focus(id, focus_type, option, ext_info);
371         if (ret) {
372                 debug_error("Could not release focus, ret[0x%x]\n", ret);
373         }
374
375         debug_fleave();
376
377         return ret;
378 }
379
380 EXPORT_API
381 int mm_sound_set_focus_watch_callback(mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, void *user_data, int *id)
382 {
383         int ret = MM_ERROR_NONE;
384
385         debug_fenter();
386
387         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
388
389         if (callback == NULL || id == NULL) {
390                 debug_error("argument is not valid\n");
391                 return MM_ERROR_INVALID_ARGUMENT;
392         }
393         ret = mm_sound_client_set_focus_watch_callback(getpid(), focus_type, callback, false, user_data, id);
394         if (ret) {
395                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
396         }
397
398         debug_fleave();
399
400         return ret;
401 }
402
403 EXPORT_API
404 int mm_sound_set_focus_watch_callback_for_session(int pid, mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, void *user_data, int *id)
405 {
406         int ret = MM_ERROR_NONE;
407
408         debug_fenter();
409
410         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
411
412         if (callback == NULL || id == NULL) {
413                 debug_error("argument is not valid\n");
414                 return MM_ERROR_INVALID_ARGUMENT;
415         }
416         ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, callback, true, user_data, id);
417         if (ret) {
418                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
419         }
420
421         debug_fleave();
422
423         return ret;
424 }
425
426 EXPORT_API
427 int mm_sound_unset_focus_watch_callback(int id)
428 {
429         int ret = MM_ERROR_NONE;
430         bool result = false;
431
432         debug_fenter();
433
434         if (id < 0) {
435                 debug_error("argument is not valid\n");
436                 return MM_ERROR_INVALID_ARGUMENT;
437         }
438
439         mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
440         if (!result) {
441                 if ((ret = mm_sound_client_unset_focus_watch_callback(id)))
442                         debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", id, ret);
443         } else {
444                 ret = mm_sound_client_execute_focus_func_in_main_context(IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB, id);
445         }
446
447         debug_fleave();
448
449         return ret;
450 }