4 * Copyright (c) 2000 - 2015 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_client.h"
30 #define RETURN_ERROR_IF_FOCUS_CB_THREAD(x_thread) \
32 int ret = MM_ERROR_NONE; \
33 bool result = false; \
34 ret = mm_sound_client_is_focus_cb_thread(x_thread, &result); \
38 debug_error("it might be called in the thread of focus callback, it is not allowed"); \
39 return MM_ERROR_SOUND_INVALID_OPERATION; \
44 int mm_sound_focus_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void *user_data)
46 int ret = MM_ERROR_NONE;
49 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
52 return MM_ERROR_INVALID_ARGUMENT;
54 ret = mm_sound_client_set_session_interrupt_callback(callback, user_data);
62 int mm_sound_focus_unset_session_interrupt_callback(void)
64 int ret = MM_ERROR_NONE;
67 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
69 ret = mm_sound_client_unset_session_interrupt_callback();
71 debug_error("Failed to mm_sound_client_unset_session_interrupt_callback(), ret[0x%x]", ret);
79 int mm_sound_focus_get_id(int *id)
81 int ret = MM_ERROR_NONE;
85 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
87 ret = mm_sound_client_get_unique_id(id);
89 debug_error("Failed to mm_sound_client_get_unique_id(), ret[0x%x]", ret);
97 int mm_sound_focus_is_cb_thread(bool *result)
99 int ret = MM_ERROR_NONE;
103 ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), result);
106 debug_msg("it might be called in the thread of focus callback");
115 int mm_sound_register_focus(int id, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
117 int ret = MM_ERROR_NONE;
121 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
123 if (id < 0 || callback == NULL) {
124 debug_error("argument is not valid");
125 return MM_ERROR_INVALID_ARGUMENT;
128 ret = mm_sound_client_register_focus(id, getpid(), stream_type, false, callback, user_data);
130 debug_error("Could not register focus, ret[0x%x]", ret);
138 int mm_sound_register_focus_for_session(int id, int pid, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
140 int ret = MM_ERROR_NONE;
144 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
146 if (id < 0 || callback == NULL) {
147 debug_error("argument is not valid");
148 return MM_ERROR_INVALID_ARGUMENT;
151 ret = mm_sound_client_register_focus(id, pid, stream_type, true, callback, user_data);
153 debug_error("Could not register focus for session, ret[0x%x]", ret);
161 int mm_sound_unregister_focus(int id)
163 int ret = MM_ERROR_NONE;
169 debug_error("argument is not valid");
170 return MM_ERROR_INVALID_ARGUMENT;
173 mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
175 if ((ret = mm_sound_client_unregister_focus(id)))
176 debug_error("Could not unregister focus, ret = %x", ret);
178 ret = mm_sound_client_execute_focus_func_in_main_context(IDLE_EVENT_TYPE_UNREGISTER_FOCUS, id);
187 int mm_sound_set_focus_reacquisition(int id, bool reacquisition)
189 int ret = MM_ERROR_NONE;
194 debug_error("argument is not valid");
195 return MM_ERROR_INVALID_ARGUMENT;
198 if ((ret = mm_sound_client_set_focus_reacquisition(id, reacquisition, false)))
199 debug_error("Could not set focus reacquisition, ret[0x%x]", ret);
207 int mm_sound_set_focus_reacquisition_for_session(int id, bool reacquisition)
209 int ret = MM_ERROR_NONE;
214 debug_error("argument is not valid");
215 return MM_ERROR_INVALID_ARGUMENT;
218 if ((ret = mm_sound_client_set_focus_reacquisition(id, reacquisition, true)))
219 debug_error("Could not set focus reacquisition, ret[0x%x]", ret);
227 int mm_sound_get_focus_reacquisition(int id, bool *reacquisition)
229 int ret = MM_ERROR_NONE;
233 if (id < 0 || !reacquisition) {
234 debug_error("argument is not valid");
235 return MM_ERROR_INVALID_ARGUMENT;
238 ret = mm_sound_client_get_focus_reacquisition(id, reacquisition);
240 debug_error("Could not get focus reacquisition, ret[0x%x]", ret);
248 int mm_sound_get_stream_type_of_acquired_focus(int focus_type, char **stream_type, int *option, char **ext_info)
250 int ret = MM_ERROR_NONE;
254 if (stream_type == NULL) {
255 debug_error("argument is not valid");
256 return MM_ERROR_INVALID_ARGUMENT;
259 ret = mm_sound_client_get_acquired_focus_stream_type(focus_type, stream_type, option, ext_info);
261 debug_error("Could not get acquired focus stream type, ret[0x%x]", ret);
269 int mm_sound_acquire_focus(int id, mm_sound_focus_type_e focus_type, const char *ext_info)
271 int ret = MM_ERROR_NONE;
275 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
278 debug_error("argument is not valid");
279 return MM_ERROR_INVALID_ARGUMENT;
281 if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
282 debug_error("argument is not valid");
283 return MM_ERROR_INVALID_ARGUMENT;
286 ret = mm_sound_client_acquire_focus(id, focus_type, 0, ext_info);
288 debug_error("Could not acquire focus, ret[0x%x]", ret);
296 int mm_sound_release_focus(int id, mm_sound_focus_type_e focus_type, const char *ext_info)
298 int ret = MM_ERROR_NONE;
302 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
305 debug_error("argument is not valid");
306 return MM_ERROR_INVALID_ARGUMENT;
308 if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
309 debug_error("argument is not valid");
310 return MM_ERROR_INVALID_ARGUMENT;
313 ret = mm_sound_client_release_focus(id, focus_type, 0, ext_info);
315 debug_error("Could not release focus, ret[0x%x]", ret);
323 int mm_sound_acquire_focus_with_option(int id, mm_sound_focus_type_e focus_type, int option, const char *ext_info)
325 int ret = MM_ERROR_NONE;
330 debug_error("id is not valid");
331 return MM_ERROR_INVALID_ARGUMENT;
335 debug_error("option is not valid");
336 return MM_ERROR_INVALID_ARGUMENT;
339 if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
340 debug_error("focus type is not valid");
341 return MM_ERROR_INVALID_ARGUMENT;
344 ret = mm_sound_client_acquire_focus(id, focus_type, option, ext_info);
346 debug_error("Could not acquire focus, ret[0x%x]", ret);
354 int mm_sound_release_focus_with_option(int id, mm_sound_focus_type_e focus_type, int option, const char *ext_info)
356 int ret = MM_ERROR_NONE;
361 debug_error("id is not valid");
362 return MM_ERROR_INVALID_ARGUMENT;
366 debug_error("option is not valid");
367 return MM_ERROR_INVALID_ARGUMENT;
370 if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
371 debug_error("focus type is not valid");
372 return MM_ERROR_INVALID_ARGUMENT;
375 ret = mm_sound_client_release_focus(id, focus_type, option, ext_info);
377 debug_error("Could not release focus, ret[0x%x]", ret);
385 int mm_sound_update_focus_status(int id, unsigned int status)
387 int ret = MM_ERROR_NONE;
389 if ((ret = mm_sound_client_update_stream_focus_status(id, status)))
390 debug_error("failed to mm_sound_client_update_stream_focus_status(), id(%d), status(%d, ret[0x%x]",
397 int mm_sound_deliver_focus(int src_id, int dst_id, mm_sound_focus_type_e focus_type)
399 int ret = MM_ERROR_NONE;
401 if ((ret = mm_sound_client_deliver_focus(getpid(), src_id, dst_id, focus_type)))
402 debug_error("failed to mm_sound_client_deliver_focus(), ret[0x%x]", ret);
408 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)
410 int ret = MM_ERROR_NONE;
414 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
416 if (callback == NULL || id == NULL) {
417 debug_error("argument is not valid");
418 return MM_ERROR_INVALID_ARGUMENT;
420 ret = mm_sound_client_set_focus_watch_callback(getpid(), focus_type, false, false, callback, user_data, id);
422 debug_error("Could not set focus watch callback, ret[0x%x]", ret);
430 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)
432 int ret = MM_ERROR_NONE;
436 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
438 if (callback == NULL || id == NULL) {
439 debug_error("argument is not valid");
440 return MM_ERROR_INVALID_ARGUMENT;
442 ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, true, false, callback, user_data, id);
444 debug_error("Could not set focus watch callback, ret[0x%x]", ret);
452 int mm_sound_set_focus_watch_callback_for_session_monitor(int pid, mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, void *user_data, int *id)
454 int ret = MM_ERROR_NONE;
458 RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
460 if (callback == NULL || id == NULL) {
461 debug_error("argument is not valid");
462 return MM_ERROR_INVALID_ARGUMENT;
464 ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, true, true, callback, user_data, id);
466 debug_error("Could not set focus watch callback, ret[0x%x]", ret);
474 int mm_sound_unset_focus_watch_callback(int id)
476 int ret = MM_ERROR_NONE;
482 debug_error("argument is not valid");
483 return MM_ERROR_INVALID_ARGUMENT;
486 if ((ret = mm_sound_client_request_unset_focus_watch_callback(id))) {
487 debug_error("failed to mm_sound_client_request_unset_focus_watch_callback, ret[0x%x]", ret);
491 mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
493 if ((ret = mm_sound_client_unset_focus_watch_callback(id)))
494 debug_error("Could not unset focus watch callback, id(%d), ret[0x%x]", id, ret);
496 ret = mm_sound_client_execute_focus_func_in_main_context(IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB, id);
497 debug_msg("mm_sound_client_execute_focus_func_in_main_context() is called, id(%d), ret[0x%x]", id, ret);