summaryrefslogtreecommitdiff
path: root/mm_sound_focus_private.c
blob: b087839344aed4c26b3f03612d43c324ce389996 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
 * libmm-sound
 *
 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Sangchul Lee <sc11.lee@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <glib.h>
#include <poll.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

#include <mm_debug.h>
#include <mm_error.h>
#include "include/mm_sound_focus_private.h"

focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];

static void unlink_if_symbolic_link(const char *path)
{
	int ret = 0;
	char *resolved_path = NULL;

	if (path == NULL)
		return;

	/* return if it does not exist */
	if ((ret = access(path, F_OK)))
		return;

	if ((resolved_path = realpath(path, NULL))) {
		/* assume that the path paramether is an absolute path */
		if (strcmp(path, resolved_path)) {
			debug_warning("unexpected symbolic link!, unlink the symbolic link(%s) to the resolved path(%s)", path, resolved_path);
			unlink(path);
		}
		free(resolved_path);
	} else {
		char str_error[256];
		strerror_r(errno, str_error, sizeof(str_error));
		debug_warning("failed to realpath() for path:%s, err:%s", path, str_error);
	}
}

static gpointer _focus_thread_func(gpointer data)
{
	unsigned int thread_id = (unsigned int)pthread_self();
	GMainLoop *focus_loop = (GMainLoop*)data;

	debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
	if (focus_loop)
		g_main_loop_run(focus_loop);
	debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);

	return NULL;
}

static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
{
#ifdef __DEBUG__
	debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout ? *timeout : -1);
#endif
	return FALSE;
}

static gboolean _focus_fd_check(GSource * source)
{
	FocusSource* fsource = (FocusSource *)source;

	if (!fsource) {
		debug_error("GSource is null");
		return FALSE;
	}
#ifdef __DEBUG__
	debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
#endif
	if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
		return TRUE;
	else
		return FALSE;
}

static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
{
	debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
	return callback(user_data);
}

static void _focus_fd_finalize(GSource *source)
{
	debug_warning("### FINALIZE : %p", source);
}

static gboolean _focus_callback_handler(gpointer user_data)
{
	focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
	GPollFD *poll_fd;
	int count;
	int tid = 0;
	focus_cb_data_lib cb_data;

	debug_log(">>> thread id(%u)", (unsigned int)pthread_self());

	if (!focus_handle) {
		debug_error("focus_handle is null");
		return G_SOURCE_CONTINUE;
	}
	poll_fd = &focus_handle->fsrc->poll_fd;
	debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);

	memset(&cb_data, 0, sizeof(focus_cb_data_lib));

	if (poll_fd->revents & (POLLIN | POLLPRI)) {
		int changed_state = -1;

		count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
		if (count < 0) {
			char str_error[256];
			strerror_r(errno, str_error, sizeof(str_error));
			debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
			return G_SOURCE_CONTINUE;
		}
		changed_state = cb_data.state;

		g_mutex_lock(&focus_handle->focus_lock);

		tid = focus_handle->focus_pid;

		if (changed_state != -1) {
			debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
					tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
			if (focus_handle->focus_callback == NULL) {
					debug_error("focus callback is null..");
					g_mutex_unlock(&focus_handle->focus_lock);
					return G_SOURCE_CONTINUE;
			}
			debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
			(focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
										cb_data.option, cb_data.ext_info, focus_handle->user_data);
			debug_msg("[CALLBACK END]");
		}
		{
			int rett = 0;
			int tmpfd = -1;
			unsigned int buf = 0;
			char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_pid, cb_data.handle);

			unlink_if_symbolic_link(filename2);
			tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
			if (tmpfd < 0) {
				char str_error[256];
				strerror_r(errno, str_error, sizeof(str_error));
				debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
							tid, tmpfd, filename2, errno, str_error);
				g_free(filename2);
				g_mutex_unlock(&focus_handle->focus_lock);
				return G_SOURCE_CONTINUE;
			}
			/* buf contains data as below,
			 * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
			buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
			rett = write(tmpfd, &buf, sizeof(buf));
			close(tmpfd);
			g_free(filename2);
			debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
		}
	}

	g_mutex_unlock(&focus_handle->focus_lock);

	debug_fleave();

	return G_SOURCE_CONTINUE;
}

static gboolean _focus_watch_callback_handler(gpointer user_data)
{
	focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
	GPollFD *poll_fd;
	int count;
	int tid = 0;
	focus_cb_data_lib cb_data;

	debug_log(">>> thread id(%u)", (unsigned int)pthread_self());

	if (!focus_handle) {
		debug_error("focus_handle is null");
		return G_SOURCE_CONTINUE;
	}
	poll_fd = &focus_handle->fsrc->poll_fd;
	debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);

	memset(&cb_data, 0, sizeof(focus_cb_data_lib));

	if (poll_fd->revents & (POLLIN | POLLPRI)) {
		count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
		if (count < 0) {
			char str_error[256];
			strerror_r(errno, str_error, sizeof(str_error));
			debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
			return G_SOURCE_CONTINUE;
		}

		if (!focus_handle->is_used) {
			debug_warning("unsetting watch calllback has been already requested");
			goto SKIP_CB_AND_RET;
		}

		g_mutex_lock(&focus_handle->focus_lock);

		tid = focus_handle->focus_pid;

		debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
				tid, cb_data.handle,  cb_data.type, cb_data.state, cb_data.stream_type);

		if (focus_handle->watch_callback == NULL) {
			debug_warning("watch callback is null..");
			goto SKIP_CB_AND_RET;
		}
		if (focus_handle->unset_watch_callback_requested == true) {
			debug_warning("unset_watch_callback_requested..");
			goto SKIP_CB_AND_RET;
		}

		debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
		(focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
									cb_data.ext_info, focus_handle->user_data);
		debug_msg("[CALLBACK END]");

SKIP_CB_AND_RET:
		{
			int rett = 0;
			int tmpfd = -1;
			int buf = -1;
			char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_pid, cb_data.handle);

			unlink_if_symbolic_link(filename2);
			tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
			if (tmpfd < 0) {
				char str_error[256];
				strerror_r(errno, str_error, sizeof(str_error));
				debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
							tid, tmpfd, filename2, errno, str_error);
				g_free(filename2);
				g_mutex_unlock(&focus_handle->focus_lock);
				return G_SOURCE_CONTINUE;
			}
			buf = cb_data.handle;
			rett = write(tmpfd, &buf, sizeof(buf));
			close(tmpfd);
			g_free(filename2);
			debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
		}
	}

	if (focus_handle->is_used) {
		debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
		g_mutex_unlock(&focus_handle->focus_lock);
	}

	debug_fleave();

	return G_SOURCE_CONTINUE;
}

#define INTERVAL_MS 20
static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
{
	int reduced_time_ms = timeout_ms;
	if (!focus_loop || timeout_ms < 0) {
		debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
		return MM_ERROR_INVALID_ARGUMENT;
	}

	do {
		if (g_main_loop_is_running(focus_loop))
			return MM_ERROR_NONE;

		usleep(INTERVAL_MS * 1000);
		if (reduced_time_ms < timeout_ms)
			debug_warning("reduced_time_ms(%d)", reduced_time_ms);
	} while ((reduced_time_ms -= INTERVAL_MS) >= 0);

	debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);

	return MM_ERROR_SOUND_INTERNAL;
}

static void _focus_open_callback(int index, bool is_for_watching)
{
	mode_t pre_mask;
	char *filename;

	debug_fenter();

	if (index < 0 || index >= FOCUS_HANDLE_MAX) {
		debug_error("Invalid focus handle index [%d]", index);
		return;
	}

	if (is_for_watching) {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
								g_focus_sound_handle[index].focus_pid,
								g_focus_sound_handle[index].handle);
	} else {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
								g_focus_sound_handle[index].focus_pid,
								g_focus_sound_handle[index].handle);
	}
	unlink_if_symbolic_link(filename);
	pre_mask = umask(0);
	if (mknod(filename, S_IFIFO|0666, 0))
		debug_error("mknod() failure, errno(%d)", errno);
	umask(pre_mask);
	g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
	if (g_focus_sound_handle[index].focus_fd == -1) {
		debug_error("Open fail : index(%d), file open error(%d)", index, errno);
	} else {
		debug_log("Open success : index(%d), filename(%s), fd(%d)",
				index, filename, g_focus_sound_handle[index].focus_fd);
	}
	g_free(filename);
	filename = NULL;

	if (is_for_watching) {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
									g_focus_sound_handle[index].focus_pid,
									g_focus_sound_handle[index].handle);
	} else {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%dr",
									g_focus_sound_handle[index].focus_pid,
									g_focus_sound_handle[index].handle);
	}
	pre_mask = umask(0);
	if (mknod(filename, S_IFIFO | 0666, 0))
		debug_error("mknod() failure, errno(%d)", errno);
	umask(pre_mask);
	g_free(filename);
	filename = NULL;

	debug_fleave();
}

void _focus_close_callback(int index, bool is_for_watching)
{
	char *filename = NULL;

	debug_fenter();

	if (index < 0 || index >= FOCUS_HANDLE_MAX) {
		debug_error("Invalid focus handle index [%d]", index);
		return;
	}

	if (g_focus_sound_handle[index].focus_fd < 0) {
		debug_error("Close fail : index(%d)", index);
	} else {
		close(g_focus_sound_handle[index].focus_fd);
		debug_log("Close Success : index(%d)", index);
	}

	if (is_for_watching) {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
								g_focus_sound_handle[index].focus_pid,
								g_focus_sound_handle[index].handle);
	} else {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
								g_focus_sound_handle[index].focus_pid,
								g_focus_sound_handle[index].handle);
	}
	if (remove(filename)) {
		debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
					filename, errno);
	}
	g_free(filename);
	filename = NULL;

	if (is_for_watching) {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
									g_focus_sound_handle[index].focus_pid,
									g_focus_sound_handle[index].handle);
	} else {
		filename = g_strdup_printf("/tmp/FOCUS.%d.%dr",
									g_focus_sound_handle[index].focus_pid,
									g_focus_sound_handle[index].handle);
	}
	if (remove(filename)) {
		debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
					filename, errno);
	}
	g_free(filename);
	filename = NULL;

	debug_fleave();
}

static GSourceFuncs event_funcs = {
	.prepare = _focus_fd_prepare,
	.check = _focus_fd_check,
	.dispatch = _focus_fd_dispatch,
	.finalize = _focus_fd_finalize,
};

static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
{
	FocusSource *fsrc = NULL;
	GSource *src = NULL;
	guint fsrc_id = 0;

	debug_fenter();

	g_mutex_init(&g_focus_sound_handle[index].focus_lock);

	src = g_source_new(&event_funcs, sizeof(FocusSource));
	if (!src) {
		debug_error("failed to g_source_new for focus source");
		goto ERROR;
	}

	fsrc = (FocusSource*) src;

	fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
	fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
	g_source_add_poll(src, &fsrc->poll_fd);

	g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);

	debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
				fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);

	fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
	if (!fsrc_id) {
		debug_error("failed to attach the source to context");
		goto ERROR;
	}
	g_source_unref(src);

	g_focus_sound_handle[index].fsrc = fsrc;

	debug_fleave();
	return true;

ERROR:
	if (src)
		g_source_unref(src);

	return false;
}

static bool _focus_remove_sound_callback(int index)
{
	focus_sound_info_t *h = NULL;

	debug_fenter();

	if (index < 0 || index >= FOCUS_HANDLE_MAX) {
		debug_error("Invalid focus handle index [%d]", index);
		return false;
	}

	h = &g_focus_sound_handle[index];
	if (h->fsrc) {
		g_source_destroy((GSource *)h->fsrc);
		h->fsrc = NULL;
	}

	h->focus_callback = NULL;
	h->watch_callback = NULL;

	g_mutex_clear(&h->focus_lock);

	debug_fleave();

	return true;
}

static void _focus_add_callback(int index, bool is_for_watching)
{
	debug_fenter();

	if (index < 0 || index >= FOCUS_HANDLE_MAX) {
		debug_error("Invalid focus handle index [%d]", index);
		return;
	}

	if (!is_for_watching) {
		if (!_focus_add_sound_callback(index, _focus_callback_handler))
			debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
	} else {
		if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
			debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
	}
	debug_fleave();
}

static void _focus_remove_callback(int index)
{
	debug_fenter();
	if (!_focus_remove_sound_callback(index))
		debug_error("failed to __focus_remove_sound_callback()");
	debug_fleave();
}

int focus_find_empty_index(int *index)
{
	int i = 0;

	if (!index)
		return -1;

	for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
		if (g_focus_sound_handle[i].is_used == false) {
			g_focus_sound_handle[i].is_used = true;
			debug_log("use index[%d]", i);
			break;
		}
	}
	if (i == FOCUS_HANDLE_MAX) {
		debug_error("could not find empty slot, it is full");
		return -1;
	}

	*index = i;

	return 0;
}

int focus_find_index_by_handle(int handle)
{
	int i = 0;
	for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
		if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
			/* debug_msg("found index(%d) for handle(%d)", i, handle);*/
			return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
		}
	}
	return -1;
}

int focus_watch_find_index_by_handle(int handle)
{
	int i = 0;
	for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
		if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
			/* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
			return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
		}
	}
	return -1;
}

#define LOOP_RUNNING_WAIT_TIME_MS 2500
int focus_init_context(int index)
{
	int ret = MM_ERROR_NONE;
	GMainContext *focus_context;

	debug_fenter();

	if (index < 0 || index >= FOCUS_HANDLE_MAX) {
		debug_error("index(%d) is not valid", index);
		return MM_ERROR_INVALID_ARGUMENT;
	}

	focus_context = g_main_context_new();
	g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
	g_main_context_unref(focus_context); /* FYI, this context resource will be released when calling g_main_loop_unref() */
	if (g_focus_sound_handle[index].focus_loop == NULL) {
		debug_error("could not create mainloop..");
		goto ERROR;
	}

	g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
									_focus_thread_func,
									g_focus_sound_handle[index].focus_loop);
	if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
		debug_error("could not create thread..");
		goto ERROR;
	}

	debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
				g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);

	if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
		debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
		goto ERROR;
	}

	debug_fleave();

	return MM_ERROR_NONE;

ERROR:
	if (g_focus_sound_handle[index].focus_loop) {
		if (g_focus_sound_handle[index].focus_cb_thread) {
			g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
			g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
			debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
					g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
			g_focus_sound_handle[index].focus_cb_thread = NULL;
		}
		g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
		g_focus_sound_handle[index].focus_loop = NULL;
	}
	return MM_ERROR_SOUND_INTERNAL;
}

void focus_deinit_context(int index)
{
	debug_fenter();

	if (index < 0 || index >= FOCUS_HANDLE_MAX) {
		debug_error("index(%d) is not valid", index);
		return;
	}

	if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
		debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
				g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
		return;
	}

	g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
	g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
	debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
			g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
	g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
	g_focus_sound_handle[index].focus_loop = NULL;
	g_focus_sound_handle[index].focus_cb_thread = NULL;

	debug_fleave();
}

void focus_init_callback(int index, bool is_for_watching)
{
	debug_fenter();
	_focus_open_callback(index, is_for_watching);
	_focus_add_callback(index, is_for_watching);
	debug_fleave();
}

void focus_deinit_callback(int index, bool is_for_watching)
{
	debug_fenter();
	_focus_remove_callback(index);
	_focus_close_callback(index, is_for_watching);
	debug_fleave();
}