Add sound manager initial source code
[staging/soundmanager.git] / sample / mediaplayer / binding / mediaplayer-manager.c
1 /*
2  *  Copyright 2017 Konsulko Group
3  *
4  *  Based on bluetooth-manager.c
5  *   Copyright 2016 ALPS ELECTRIC CO., LTD.
6  *
7  *   Licensed under the Apache License, Version 2.0 (the "License");
8  *   you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *   Unless required by applicable law or agreed to in writing, software
14  *   distributed under the License is distributed on an "AS IS" BASIS,
15  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *   See the License for the specific language governing permissions and
17  *   limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26
27 #include <pthread.h>
28 #include <glib.h>
29 #include <gio/gio.h>
30 #include <glib-object.h>
31 #include <sqlite3.h>
32
33 #include "mediaplayer-manager.h"
34
35 static Binding_RegisterCallback_t g_RegisterCallback = { 0 };
36 static stMediaPlayerManage MediaPlayerManage = { 0 };
37
38 /* ------ LOCAL  FUNCTIONS --------- */
39 void ListLock() {
40     g_mutex_lock(&(MediaPlayerManage.m));
41 }
42
43 void ListUnlock() {
44     g_mutex_unlock(&(MediaPlayerManage.m));
45 }
46
47 void DebugTraceSendMsg(int level, gchar* message)
48 {
49 #ifdef LOCAL_PRINT_DEBUG
50     switch (level)
51     {
52             case DT_LEVEL_ERROR:
53                 g_print("[E]");
54                 break;
55
56             case DT_LEVEL_WARNING:
57                 g_print("[W]");
58                 break;
59
60             case DT_LEVEL_NOTICE:
61                 g_print("[N]");
62                 break;
63
64             case DT_LEVEL_INFO:
65                 g_print("[I]");
66                 break;
67
68             case DT_LEVEL_DEBUG:
69                 g_print("[D]");
70                 break;
71
72             default:
73                 g_print("[-]");
74                 break;
75     }
76
77     g_print("%s",message);
78 #endif
79
80     if (message) {
81         g_free(message);
82     }
83
84 }
85
86 GList* media_local_scan(GList *list)
87 {
88     gchar *path = g_strconcat(g_get_home_dir(), "/", "Music", NULL);
89     gchar *tmp = NULL;
90     GDir *dir;
91
92     dir = g_dir_open(path, 0, NULL);
93     if (dir == NULL)
94     {
95         LOGE("Cannot open media path %s\n", path);
96         return list;
97     }
98
99     while ((tmp = (gchar *) g_dir_read_name(dir)) != NULL)
100     {
101         list = g_list_append(list, g_strdup_printf("file://%s/%s", path, tmp));
102     }
103
104     g_free(path);
105     g_dir_close(dir);
106
107     return list;
108 }
109
110 GList* media_lightmediascanner_scan(void)
111 {
112     sqlite3 *conn;
113     sqlite3_stmt *res;
114     GList *list = NULL;
115     const char *tail;
116     const gchar *db_path;
117     int ret = 0;
118
119     db_path = scanner1_get_data_base_path(MediaPlayerManage.lms_proxy);
120
121     ret = sqlite3_open(db_path, &conn);
122     if (ret) {
123         LOGE("Cannot open SQLITE database: '%s'\n", db_path);
124         return NULL;
125     }
126
127     ret = sqlite3_prepare_v2(conn, SQL_QUERY, strlen(SQL_QUERY) + 1, &res, &tail);
128     if (ret) {
129         LOGE("Cannot execute query '%s'\n", SQL_QUERY);
130         return NULL;
131     }
132
133     while (sqlite3_step(res) == SQLITE_ROW) {
134         struct stat buf;
135         const char *path = (const char *) sqlite3_column_text(res, 0);
136
137         ret = stat(path, &buf);
138         if (ret)
139             continue;
140
141         list = g_list_append(list, g_strdup_printf("file://%s", path));
142     }
143
144     return list;
145 }
146
147
148 static void
149 on_interface_proxy_properties_changed (GDBusProxy *proxy,
150                                     GVariant *changed_properties,
151                                     const gchar* const  *invalidated_properties)
152 {
153     GVariantIter iter;
154     const gchar *key;
155     GVariant *subValue;
156     const gchar *pInterface;
157     GList *list;
158
159     pInterface = g_dbus_proxy_get_interface_name (proxy);
160
161     if (0 != g_strcmp0(pInterface, LIGHTMEDIASCANNER_INTERFACE))
162         return;
163
164     g_variant_iter_init (&iter, changed_properties);
165     while (g_variant_iter_next (&iter, "{&sv}", &key, &subValue))
166     {
167         gboolean val;
168         if (0 == g_strcmp0(key,"IsScanning")) {
169             g_variant_get(subValue, "b", &val);
170             if (val == TRUE)
171                 return;
172         } else if (0 == g_strcmp0(key, "WriteLocked")) {
173             g_variant_get(subValue, "b", &val);
174             if (val == TRUE)
175                 return;
176         }
177     }
178
179     ListLock();
180
181     list = media_lightmediascanner_scan();
182
183     if (list != NULL && g_RegisterCallback.binding_device_added)
184         g_RegisterCallback.binding_device_added(list);
185
186     g_list_free_full(list, g_free);
187
188     ListUnlock();
189 }
190
191 static int MediaPlayerDBusInit(void)
192 {
193     GError *error = NULL;
194
195     MediaPlayerManage.lms_proxy = scanner1_proxy_new_for_bus_sync(
196         G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, LIGHTMEDIASCANNER_SERVICE,
197         LIGHTMEDIASCANNER_PATH, NULL, &error);
198
199     if (MediaPlayerManage.lms_proxy == NULL) {
200         LOGE("Create LightMediaScanner Proxy failed\n");
201         return -1;
202     }
203
204     g_signal_connect (MediaPlayerManage.lms_proxy,
205                       "g-properties-changed",
206                       G_CALLBACK (on_interface_proxy_properties_changed),
207                       NULL);
208
209     return 0;
210 }
211
212 static void *media_event_loop_thread(void *unused)
213 {
214     GMainLoop *loop = g_main_loop_new(NULL, FALSE);
215     int ret;
216
217     ret = MediaPlayerDBusInit();
218     if (ret == 0) {
219         LOGD("g_main_loop_run\n");
220         g_main_loop_run(loop);
221     }
222
223     g_main_loop_unref(loop);
224
225     return NULL;
226 }
227
228 void
229 unmount_cb (GFileMonitor      *mon,
230             GFile             *file,
231             GFile             *other_file,
232             GFileMonitorEvent  event,
233             gpointer           udata)
234 {
235     gchar *path = g_file_get_path(file);
236     gchar *uri = g_strconcat("file://", path, NULL);
237     g_free(path);
238
239     ListLock();
240
241     if (g_RegisterCallback.binding_device_removed &&
242         event == G_FILE_MONITOR_EVENT_DELETED) {
243             g_RegisterCallback.binding_device_removed(uri);
244     }
245
246     ListUnlock();
247     g_free(uri);
248 }
249
250 /*
251  * Create MediaPlayer Manager Thread
252  * Note: mediaplayer-api should do MediaPlayerManagerInit() before any other 
253  *       API calls
254  * Returns: 0 - success or error conditions
255  */
256 int MediaPlayerManagerInit() {
257     pthread_t thread_id;
258     GFile *file;
259     GFileMonitor *mon;
260
261     g_mutex_init(&(MediaPlayerManage.m));
262
263     file = g_file_new_for_path("/media");
264     g_assert(file != NULL);
265
266     mon = g_file_monitor (file, G_FILE_MONITOR_NONE, NULL, NULL);
267     g_assert(mon != NULL);
268     g_signal_connect (mon, "changed", G_CALLBACK(unmount_cb), NULL);
269
270     pthread_create(&thread_id, NULL, media_event_loop_thread, NULL);
271
272     return 0;
273 }
274
275 /*
276  * Register MediaPlayer Manager Callback functions
277  */
278 void BindingAPIRegister(const Binding_RegisterCallback_t* pstRegisterCallback)
279 {
280     if (NULL != pstRegisterCallback)
281     {
282         if (NULL != pstRegisterCallback->binding_device_added)
283         {
284             g_RegisterCallback.binding_device_added =
285                 pstRegisterCallback->binding_device_added;
286         }
287
288         if (NULL != pstRegisterCallback->binding_device_removed)
289         {
290             g_RegisterCallback.binding_device_removed =
291                 pstRegisterCallback->binding_device_removed;
292         }
293     }
294 }