Add to check if a focus API is called in the same thread with focus callback's
[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 #include <stdio.h>
25 #include <vconf.h>
26
27 #include <mm_debug.h>
28
29 #include "include/mm_sound.h"
30 #include "include/mm_sound_client.h"
31 #include "include/mm_sound_focus.h"
32 #include "focus_server/include/mm_sound_mgr_focus.h"
33
34 #define RETURN_ERROR_IF_FOCUS_CB_THREAD(x_thread) \
35 { \
36         int ret = MM_ERROR_NONE; \
37         bool result = false; \
38         ret = mm_sound_client_is_focus_cb_thread(x_thread, &result); \
39         if (ret) \
40                 return ret; \
41         else if (result) { \
42                 debug_error("it might be called in the thread of focus callback, it is not allowed\n"); \
43                 return MM_ERROR_SOUND_INVALID_OPERATION; \
44         } \
45 } \
46
47 EXPORT_API
48 int mm_sound_focus_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void *user_data)
49 {
50         int ret = MM_ERROR_NONE;
51         debug_fenter();
52
53         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
54
55         if (!callback)
56                 return MM_ERROR_INVALID_ARGUMENT;
57
58         ret = mm_sound_client_set_session_interrupt_callback (callback, user_data);
59
60         debug_fleave();
61
62         return ret;
63 }
64
65 EXPORT_API
66 int mm_sound_focus_unset_session_interrupt_callback(void)
67 {
68         int ret = MM_ERROR_NONE;
69         debug_fenter();
70
71         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
72
73         ret = mm_sound_client_unset_session_interrupt_callback ();
74         if (ret) {
75                 debug_error("Failed to mm_sound_client_unset_session_interrupt_callback(), ret[0x%x]\n", ret);
76         }
77
78         debug_fleave();
79
80         return ret;
81 }
82
83 EXPORT_API
84 int mm_sound_focus_get_id(int *id)
85 {
86         int ret = MM_ERROR_NONE;
87
88         debug_fenter();
89
90         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
91
92         ret = mm_sound_client_get_unique_id(id);
93         if (ret) {
94                 debug_error("Failed to mm_sound_client_get_unique_id(), ret[0x%x]\n", ret);
95         }
96
97         debug_fleave();
98
99         return ret;
100 }
101
102 EXPORT_API
103 int mm_sound_focus_is_cb_thread(bool *result)
104 {
105         int ret = MM_ERROR_NONE;
106
107         debug_fenter();
108
109         ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), result);
110         if (!ret) {
111                 if (*result)
112                         debug_error("it might be called in the thread of focus callback, it is not allowed\n");
113         }
114
115         debug_fleave();
116
117         return ret;
118 }
119
120 EXPORT_API
121 int mm_sound_register_focus(int id, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
122 {
123         int ret = MM_ERROR_NONE;
124
125         debug_fenter();
126
127         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
128
129         if (id < 0 || callback == NULL) {
130                 debug_error("argument is not valid\n");
131                 return MM_ERROR_INVALID_ARGUMENT;
132         }
133
134         ret = mm_sound_client_register_focus(id, getpid(), stream_type, callback, false, user_data);
135         if (ret) {
136                 debug_error("Could not register focus, ret[0x%x]\n", ret);
137         }
138
139         debug_fleave();
140
141         return ret;
142 }
143
144 EXPORT_API
145 int mm_sound_register_focus_for_session(int id, int pid, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
146 {
147         int ret = MM_ERROR_NONE;
148
149         debug_fenter();
150
151         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
152
153         if (id < 0 || callback == NULL) {
154                 debug_error("argument is not valid\n");
155                 return MM_ERROR_INVALID_ARGUMENT;
156         }
157
158         ret = mm_sound_client_register_focus(id, pid, stream_type, callback, true, user_data);
159         if (ret) {
160                 debug_error("Could not register focus for session, ret[0x%x]\n", ret);
161         }
162
163         debug_fleave();
164
165         return ret;
166 }
167
168 EXPORT_API
169 int mm_sound_unregister_focus(int id)
170 {
171         int ret = MM_ERROR_NONE;
172
173         debug_fenter();
174
175         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
176
177         if (id < 0) {
178                 debug_error("argument is not valid\n");
179                 return MM_ERROR_INVALID_ARGUMENT;
180         }
181
182         ret = mm_sound_client_unregister_focus(id);
183         if (ret) {
184                 debug_error("Could not unregister focus, ret = %x\n", ret);
185         }
186
187         debug_fleave();
188
189         return ret;
190 }
191
192 EXPORT_API
193 int mm_sound_acquire_focus(int id, mm_sound_focus_type_e focus_type, const char *additional_info)
194 {
195         int ret = MM_ERROR_NONE;
196
197         debug_fenter();
198
199         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
200
201         if (id < 0) {
202                 debug_error("argument is not valid\n");
203                 return MM_ERROR_INVALID_ARGUMENT;
204         }
205         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
206                 debug_error("argument is not valid\n");
207                 return MM_ERROR_INVALID_ARGUMENT;
208         }
209
210         ret = mm_sound_client_acquire_focus(id, focus_type, additional_info);
211         if (ret) {
212                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
213         }
214
215         debug_fleave();
216
217         return ret;
218 }
219
220 EXPORT_API
221 int mm_sound_release_focus(int id, mm_sound_focus_type_e focus_type, const char *additional_info)
222 {
223         int ret = MM_ERROR_NONE;
224
225         debug_fenter();
226
227         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
228
229         if (id < 0) {
230                 debug_error("argument is not valid\n");
231                 return MM_ERROR_INVALID_ARGUMENT;
232         }
233         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
234                 debug_error("argument is not valid\n");
235                 return MM_ERROR_INVALID_ARGUMENT;
236         }
237
238         ret = mm_sound_client_release_focus(id, focus_type, additional_info);
239         if (ret) {
240                 debug_error("Could not release focus, ret[0x%x]\n", ret);
241         }
242
243         debug_fleave();
244
245         return ret;
246 }
247
248 EXPORT_API
249 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)
250 {
251         int ret = MM_ERROR_NONE;
252
253         debug_fenter();
254
255         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
256
257         if (callback == NULL || id == NULL) {
258                 debug_error("argument is not valid\n");
259                 return MM_ERROR_INVALID_ARGUMENT;
260         }
261         ret = mm_sound_client_set_focus_watch_callback(getpid(), focus_type, callback, false, user_data, id);
262         if (ret) {
263                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
264         }
265
266         debug_fleave();
267
268         return ret;
269 }
270
271 EXPORT_API
272 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)
273 {
274         int ret = MM_ERROR_NONE;
275
276         debug_fenter();
277
278         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
279
280         if (callback == NULL || id == NULL) {
281                 debug_error("argument is not valid\n");
282                 return MM_ERROR_INVALID_ARGUMENT;
283         }
284         ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, callback, true, user_data, id);
285         if (ret) {
286                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
287         }
288
289         debug_fleave();
290
291         return ret;
292 }
293
294 EXPORT_API
295 int mm_sound_unset_focus_watch_callback(int id)
296 {
297         int ret = MM_ERROR_NONE;
298
299         debug_fenter();
300
301         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
302
303         ret = mm_sound_client_unset_focus_watch_callback(id);
304         if (ret) {
305                 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", id, ret);
306         }
307
308         debug_fleave();
309
310         return ret;
311 }