2 * Copyright 2017 Konsulko Group
4 * Based on bluetooth-manager.c
5 * Copyright 2016 ALPS ELECTRIC CO., LTD.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/types.h>
30 #include <glib-object.h>
33 #include "mediaplayer-manager.h"
35 static Binding_RegisterCallback_t g_RegisterCallback = { 0 };
36 static stMediaPlayerManage MediaPlayerManage = { 0 };
38 /* ------ LOCAL FUNCTIONS --------- */
40 g_mutex_lock(&(MediaPlayerManage.m));
44 g_mutex_unlock(&(MediaPlayerManage.m));
47 void DebugTraceSendMsg(int level, gchar* message)
49 #ifdef LOCAL_PRINT_DEBUG
56 case DT_LEVEL_WARNING:
77 g_print("%s",message);
86 GList* media_lightmediascanner_scan(void)
95 list = MediaPlayerManage.list;
97 // Returned cached result
101 db_path = scanner1_get_data_base_path(MediaPlayerManage.lms_proxy);
103 ret = sqlite3_open(db_path, &conn);
105 LOGE("Cannot open SQLITE database: '%s'\n", db_path);
109 ret = sqlite3_prepare_v2(conn, SQL_QUERY, strlen(SQL_QUERY) + 1, &res, &tail);
111 LOGE("Cannot execute query '%s'\n", SQL_QUERY);
115 while (sqlite3_step(res) == SQLITE_ROW) {
117 const char *path = (const char *) sqlite3_column_text(res, 0);
119 ret = stat(path, &buf);
123 list = g_list_append(list, g_strdup_printf("file://%s", path));
126 MediaPlayerManage.list = list;
133 on_interface_proxy_properties_changed (GDBusProxy *proxy,
134 GVariant *changed_properties,
135 const gchar* const *invalidated_properties)
140 const gchar *pInterface;
143 pInterface = g_dbus_proxy_get_interface_name (proxy);
145 if (0 != g_strcmp0(pInterface, LIGHTMEDIASCANNER_INTERFACE))
148 g_variant_iter_init (&iter, changed_properties);
149 while (g_variant_iter_next (&iter, "{&sv}", &key, &subValue))
152 if (0 == g_strcmp0(key,"IsScanning")) {
153 g_variant_get(subValue, "b", &val);
156 } else if (0 == g_strcmp0(key, "WriteLocked")) {
157 g_variant_get(subValue, "b", &val);
165 list = media_lightmediascanner_scan();
167 if (list != NULL && g_RegisterCallback.binding_device_added)
168 g_RegisterCallback.binding_device_added(list);
174 on_device_removed (GDBusProxy *proxy, gpointer user_data)
178 g_list_free(MediaPlayerManage.list);
179 MediaPlayerManage.list = NULL;
181 if (g_RegisterCallback.binding_device_removed)
182 g_RegisterCallback.binding_device_removed((const char *) user_data);
187 static int MediaPlayerDBusInit(void)
189 GError *error = NULL;
191 MediaPlayerManage.lms_proxy = scanner1_proxy_new_for_bus_sync(
192 G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, LIGHTMEDIASCANNER_SERVICE,
193 LIGHTMEDIASCANNER_PATH, NULL, &error);
195 if (MediaPlayerManage.lms_proxy == NULL) {
196 LOGE("Create LightMediaScanner Proxy failed\n");
200 MediaPlayerManage.udisks_proxy = org_freedesktop_udisks_proxy_new_for_bus_sync(
201 G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, UDISKS_SERVICE,
202 UDISKS_PATH, NULL, &error);
204 if (MediaPlayerManage.udisks_proxy == NULL) {
205 LOGE("Create UDisks Proxy failed\n");
209 g_signal_connect (MediaPlayerManage.lms_proxy,
210 "g-properties-changed",
211 G_CALLBACK (on_interface_proxy_properties_changed),
214 g_signal_connect (MediaPlayerManage.udisks_proxy,
216 G_CALLBACK (on_device_removed),
222 static void *media_event_loop_thread(void *unused)
224 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
227 ret = MediaPlayerDBusInit();
229 LOGD("g_main_loop_run\n");
230 g_main_loop_run(loop);
233 g_main_loop_unref(loop);
239 * Create MediaPlayer Manager Thread
240 * Note: mediaplayer-api should do MediaPlayerManagerInit() before any other
242 * Returns: 0 - success or error conditions
244 int MediaPlayerManagerInit() {
247 g_mutex_init(&(MediaPlayerManage.m));
249 pthread_create(&thread_id, NULL, media_event_loop_thread, NULL);
255 * Register MediaPlayer Manager Callback functions
257 void BindingAPIRegister(const Binding_RegisterCallback_t* pstRegisterCallback)
259 if (NULL != pstRegisterCallback)
261 if (NULL != pstRegisterCallback->binding_device_added)
263 g_RegisterCallback.binding_device_added =
264 pstRegisterCallback->binding_device_added;
267 if (NULL != pstRegisterCallback->binding_device_removed)
269 g_RegisterCallback.binding_device_removed =
270 pstRegisterCallback->binding_device_removed;