Pushed latest libmedia-service for tizen beta
[framework/multimedia/libmedia-service.git] / src / common / media-info-util.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@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 <unistd.h>
23 #include <asm/unistd.h>
24 #include <pthread.h>
25 #include <string.h>
26
27 #include "media-info-util.h"
28 #include "media-info-error.h"
29 #include "media-info-debug.h"
30 #include "media-svc.h"
31 #include "audio-svc.h"
32 #include "audio-svc-error.h"
33 #include "uuid.h"
34
35 static GHashTable *g_handle_table = NULL;
36 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
37
38 int _media_info_get_thread_id()
39 {
40         return syscall(__NR_gettid);
41 }
42
43 int _media_info_init_handle_tbl()
44 {
45         pthread_mutex_lock(&mutex);
46
47         if (g_handle_table == NULL)
48                 g_handle_table = g_hash_table_new(g_int_hash, g_int_equal);
49
50         pthread_mutex_unlock(&mutex);
51
52         if (g_handle_table == NULL)
53                 return -1;
54         else
55                 return 0;
56 }
57
58 int _media_info_finalize_handle_tbl()
59 {
60         pthread_mutex_lock(&mutex);
61
62         if (g_handle_table != NULL) {
63                 int size = g_hash_table_size(g_handle_table);
64                 if (size == 0) {
65                         g_hash_table_destroy(g_handle_table);
66                         g_handle_table = NULL;
67                 } else
68                         mediainfo_dbg("handle table is not empty");
69         }
70
71         pthread_mutex_unlock(&mutex);
72
73         return 0;
74 }
75
76 int
77 _media_info_insert_handle(int **tid_key, int tid, HandleTable ** handle_table)
78 {
79         *tid_key = g_malloc(sizeof **tid_key);
80         **tid_key = tid;
81         *handle_table = g_malloc(sizeof **handle_table);
82
83         pthread_mutex_lock(&mutex);
84
85         g_hash_table_insert(g_handle_table, (gpointer) * tid_key,
86                             (gpointer) * handle_table);
87
88         (*handle_table)->ref_cnt = 1;
89
90         pthread_mutex_unlock(&mutex);
91
92         return 0;
93 }
94
95 int _media_info_remove_handle(int tid)
96 {
97         int *key = NULL;
98         HandleTable *val = NULL;
99
100         pthread_mutex_lock(&mutex);
101
102         if (!g_hash_table_lookup_extended
103             (g_handle_table, &tid, (gpointer) & key, (gpointer) & val)) {
104                 pthread_mutex_unlock(&mutex);
105                 return -1;
106         } else {
107                 if (g_hash_table_remove(g_handle_table, (gpointer) & tid))
108                         mediainfo_dbg("g_hash_table_remove done");
109                 else
110                         mediainfo_dbg("g_hash_table_remove fails");
111
112                 if (key)
113                         g_free(key);
114                 if (val)
115                         g_free(val);
116
117                 pthread_mutex_unlock(&mutex);
118                 return 0;
119         }
120 }
121
122 HandleTable *_media_info_search_handle(int tid)
123 {
124         pthread_mutex_lock(&mutex);
125
126         HandleTable *value =
127             (HandleTable *) g_hash_table_lookup(g_handle_table,
128                                                 (gpointer) & tid);
129
130         pthread_mutex_unlock(&mutex);
131
132         return value;
133 }
134
135 sqlite3 *_media_info_get_proper_handle()
136 {
137         int tid = _media_info_get_thread_id();
138         HandleTable *value =
139             (HandleTable *) g_hash_table_lookup(g_handle_table,
140                                                 (gpointer) & tid);
141
142         if (value == NULL) {
143                 return NULL;
144         } else {
145                 return value->handle;
146         }
147 }
148 void _media_info_atomic_add_counting(HandleTable *handle_table)
149 {
150         pthread_mutex_lock(&mutex);
151         handle_table->ref_cnt++;
152         pthread_mutex_unlock(&mutex);
153 }
154
155
156 void _media_info_atomic_sub_counting(HandleTable *handle_table)
157 {
158         pthread_mutex_lock(&mutex);
159         handle_table->ref_cnt--;
160         pthread_mutex_unlock(&mutex);
161 }
162
163 char *_media_info_generate_uuid()
164 {
165         uuid_t uuid_value;
166         static char uuid_unparsed[50];  
167
168         uuid_generate(uuid_value);
169         uuid_unparse(uuid_value, uuid_unparsed);
170
171         //mediainfo_dbg("UUID : %s", uuid_unparsed);
172         return uuid_unparsed;
173 }
174