Fix bug regarding roll-back scenario of focus reacquisition setting, revise some...
[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         else 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
169         debug_fenter();
170
171         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
172
173         if (id < 0) {
174                 debug_error("argument is not valid\n");
175                 return MM_ERROR_INVALID_ARGUMENT;
176         }
177
178         ret = mm_sound_client_unregister_focus(id);
179         if (ret) {
180                 debug_error("Could not unregister focus, ret = %x\n", ret);
181         }
182
183         debug_fleave();
184
185         return ret;
186 }
187
188 EXPORT_API
189 int mm_sound_set_focus_reacquisition(int id, bool reacquisition)
190 {
191         int ret = MM_ERROR_NONE;
192
193         debug_fenter();
194
195         if (id < 0) {
196                 debug_error("argument is not valid\n");
197                 return MM_ERROR_INVALID_ARGUMENT;
198         }
199
200         ret = mm_sound_client_set_focus_reacquisition(id, reacquisition);
201         if (ret) {
202                 debug_error("Could not set focus reacquisition, ret[0x%x]\n", ret);
203         }
204
205         debug_fleave();
206
207         return ret;
208 }
209
210 EXPORT_API
211 int mm_sound_get_focus_reacquisition(int id, bool *reacquisition)
212 {
213         int ret = MM_ERROR_NONE;
214
215         debug_fenter();
216
217         if (id < 0 || !reacquisition) {
218                 debug_error("argument is not valid\n");
219                 return MM_ERROR_INVALID_ARGUMENT;
220         }
221
222         ret = mm_sound_client_get_focus_reacquisition(id, reacquisition);
223         if (ret) {
224                 debug_error("Could not get focus reacquisition, ret[0x%x]\n", ret);
225         }
226
227         debug_fleave();
228
229         return ret;
230 }
231
232 EXPORT_API
233 int mm_sound_get_stream_type_of_acquired_focus(int focus_type, char **stream_type, int *option, char **ext_info)
234 {
235         int ret = MM_ERROR_NONE;
236
237         debug_fenter();
238
239         if (stream_type == NULL) {
240                 debug_error("argument is not valid\n");
241                 return MM_ERROR_INVALID_ARGUMENT;
242         }
243
244         ret = mm_sound_client_get_acquired_focus_stream_type(focus_type, stream_type, option, ext_info);
245         if (ret) {
246                 debug_error("Could not get acquired focus stream type, ret[0x%x]\n", ret);
247         }
248
249         debug_fleave();
250
251         return ret;
252 }
253
254 EXPORT_API
255 int mm_sound_acquire_focus(int id, mm_sound_focus_type_e focus_type, const char *ext_info)
256 {
257         int ret = MM_ERROR_NONE;
258
259         debug_fenter();
260
261         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
262
263         if (id < 0) {
264                 debug_error("argument is not valid\n");
265                 return MM_ERROR_INVALID_ARGUMENT;
266         }
267         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
268                 debug_error("argument is not valid\n");
269                 return MM_ERROR_INVALID_ARGUMENT;
270         }
271
272         ret = mm_sound_client_acquire_focus(id, focus_type, 0, ext_info);
273         if (ret) {
274                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
275         }
276
277         debug_fleave();
278
279         return ret;
280 }
281
282 EXPORT_API
283 int mm_sound_release_focus(int id, mm_sound_focus_type_e focus_type, const char *ext_info)
284 {
285         int ret = MM_ERROR_NONE;
286
287         debug_fenter();
288
289         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
290
291         if (id < 0) {
292                 debug_error("argument is not valid\n");
293                 return MM_ERROR_INVALID_ARGUMENT;
294         }
295         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
296                 debug_error("argument is not valid\n");
297                 return MM_ERROR_INVALID_ARGUMENT;
298         }
299
300         ret = mm_sound_client_release_focus(id, focus_type, 0, ext_info);
301         if (ret) {
302                 debug_error("Could not release focus, ret[0x%x]\n", ret);
303         }
304
305         debug_fleave();
306
307         return ret;
308 }
309
310 EXPORT_API
311 int mm_sound_acquire_focus_with_option(int id, mm_sound_focus_type_e focus_type, int option, const char *ext_info)
312 {
313         int ret = MM_ERROR_NONE;
314
315         debug_fenter();
316
317         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
318
319         if (id < 0) {
320                 debug_error("id is not valid\n");
321                 return MM_ERROR_INVALID_ARGUMENT;
322         }
323
324         if (option < 0) {
325                 debug_error("option is not valid\n");
326                 return MM_ERROR_INVALID_ARGUMENT;
327         }
328
329         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
330                 debug_error("focus type is not valid\n");
331                 return MM_ERROR_INVALID_ARGUMENT;
332         }
333
334         ret = mm_sound_client_acquire_focus(id, focus_type, option, ext_info);
335         if (ret) {
336                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
337         }
338
339         debug_fleave();
340
341         return ret;
342 }
343
344 EXPORT_API
345 int mm_sound_release_focus_with_option(int id, mm_sound_focus_type_e focus_type, int option, const char *ext_info)
346 {
347         int ret = MM_ERROR_NONE;
348
349         debug_fenter();
350
351         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
352
353         if (id < 0) {
354                 debug_error("id is not valid\n");
355                 return MM_ERROR_INVALID_ARGUMENT;
356         }
357
358         if (option < 0) {
359                 debug_error("option is not valid\n");
360                 return MM_ERROR_INVALID_ARGUMENT;
361         }
362
363         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
364                 debug_error("focus type is not valid\n");
365                 return MM_ERROR_INVALID_ARGUMENT;
366         }
367
368         ret = mm_sound_client_release_focus(id, focus_type, option, ext_info);
369         if (ret) {
370                 debug_error("Could not release focus, ret[0x%x]\n", ret);
371         }
372
373         debug_fleave();
374
375         return ret;
376 }
377
378 EXPORT_API
379 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)
380 {
381         int ret = MM_ERROR_NONE;
382
383         debug_fenter();
384
385         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
386
387         if (callback == NULL || id == NULL) {
388                 debug_error("argument is not valid\n");
389                 return MM_ERROR_INVALID_ARGUMENT;
390         }
391         ret = mm_sound_client_set_focus_watch_callback(getpid(), focus_type, callback, false, user_data, id);
392         if (ret) {
393                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
394         }
395
396         debug_fleave();
397
398         return ret;
399 }
400
401 EXPORT_API
402 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)
403 {
404         int ret = MM_ERROR_NONE;
405
406         debug_fenter();
407
408         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
409
410         if (callback == NULL || id == NULL) {
411                 debug_error("argument is not valid\n");
412                 return MM_ERROR_INVALID_ARGUMENT;
413         }
414         ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, callback, true, user_data, id);
415         if (ret) {
416                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
417         }
418
419         debug_fleave();
420
421         return ret;
422 }
423
424 EXPORT_API
425 int mm_sound_unset_focus_watch_callback(int id)
426 {
427         int ret = MM_ERROR_NONE;
428
429         debug_fenter();
430
431         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
432
433         ret = mm_sound_client_unset_focus_watch_callback(id);
434         if (ret) {
435                 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", id, ret);
436         }
437
438         debug_fleave();
439
440         return ret;
441 }