Remove unused include statement
[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_error("it might be called in the thread of focus callback, it is not allowed\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
202         if (ret) {
203                 debug_error("Could not set focus reacquisition, ret[0x%x]\n", ret);
204         }
205
206         debug_fleave();
207
208         return ret;
209 }
210
211 EXPORT_API
212 int mm_sound_get_focus_reacquisition(int id, bool *reacquisition)
213 {
214         int ret = MM_ERROR_NONE;
215
216         debug_fenter();
217
218         if (id < 0 || !reacquisition) {
219                 debug_error("argument is not valid\n");
220                 return MM_ERROR_INVALID_ARGUMENT;
221         }
222
223         ret = mm_sound_client_get_focus_reacquisition(id, reacquisition);
224
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, char **additional_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, additional_info);
247
248         if (ret) {
249                 debug_error("Could not get acquired focus stream type, ret[0x%x]\n", ret);
250         }
251
252         debug_fleave();
253
254         return ret;
255 }
256
257 EXPORT_API
258 int mm_sound_acquire_focus(int id, mm_sound_focus_type_e focus_type, const char *additional_info)
259 {
260         int ret = MM_ERROR_NONE;
261
262         debug_fenter();
263
264         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
265
266         if (id < 0) {
267                 debug_error("argument is not valid\n");
268                 return MM_ERROR_INVALID_ARGUMENT;
269         }
270         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
271                 debug_error("argument is not valid\n");
272                 return MM_ERROR_INVALID_ARGUMENT;
273         }
274
275         ret = mm_sound_client_acquire_focus(id, focus_type, additional_info);
276         if (ret) {
277                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
278         }
279
280         debug_fleave();
281
282         return ret;
283 }
284
285 EXPORT_API
286 int mm_sound_release_focus(int id, mm_sound_focus_type_e focus_type, const char *additional_info)
287 {
288         int ret = MM_ERROR_NONE;
289
290         debug_fenter();
291
292         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
293
294         if (id < 0) {
295                 debug_error("argument is not valid\n");
296                 return MM_ERROR_INVALID_ARGUMENT;
297         }
298         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
299                 debug_error("argument is not valid\n");
300                 return MM_ERROR_INVALID_ARGUMENT;
301         }
302
303         ret = mm_sound_client_release_focus(id, focus_type, additional_info);
304         if (ret) {
305                 debug_error("Could not release focus, ret[0x%x]\n", ret);
306         }
307
308         debug_fleave();
309
310         return ret;
311 }
312
313 EXPORT_API
314 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)
315 {
316         int ret = MM_ERROR_NONE;
317
318         debug_fenter();
319
320         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
321
322         if (callback == NULL || id == NULL) {
323                 debug_error("argument is not valid\n");
324                 return MM_ERROR_INVALID_ARGUMENT;
325         }
326         ret = mm_sound_client_set_focus_watch_callback(getpid(), focus_type, callback, false, user_data, id);
327         if (ret) {
328                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
329         }
330
331         debug_fleave();
332
333         return ret;
334 }
335
336 EXPORT_API
337 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)
338 {
339         int ret = MM_ERROR_NONE;
340
341         debug_fenter();
342
343         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
344
345         if (callback == NULL || id == NULL) {
346                 debug_error("argument is not valid\n");
347                 return MM_ERROR_INVALID_ARGUMENT;
348         }
349         ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, callback, true, user_data, id);
350         if (ret) {
351                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
352         }
353
354         debug_fleave();
355
356         return ret;
357 }
358
359 EXPORT_API
360 int mm_sound_unset_focus_watch_callback(int id)
361 {
362         int ret = MM_ERROR_NONE;
363
364         debug_fenter();
365
366         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
367
368         ret = mm_sound_client_unset_focus_watch_callback(id);
369         if (ret) {
370                 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", id, ret);
371         }
372
373         debug_fleave();
374
375         return ret;
376 }