struct media_player *media_player_controller_create(const char *path, uint16_t id) { struct media_player *mp; mp = g_new0(struct media_player, 1); mp->device = g_strdup(path); mp->path = g_strdup_printf("%s/player%u", path, id); mp->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); mp->track = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); mp->progress = g_timer_new(); if (!g_dbus_register_interface(btd_get_dbus_connection(), mp->path, MEDIA_PLAYER_INTERFACE, media_player_methods, media_player_signals, media_player_properties, mp, NULL)) { error("D-Bus failed to register %s path", mp->path); media_player_destroy(mp); return NULL; } DBG("%s", mp->path); return mp; }
static DBusMessage *register_player(DBusConnection *conn, DBusMessage *msg, void *data) { struct media_adapter *adapter = data; struct media_player *mp; DBusMessageIter args; const char *sender, *path; int err; sender = dbus_message_get_sender(msg); dbus_message_iter_init(msg, &args); dbus_message_iter_get_basic(&args, &path); dbus_message_iter_next(&args); if (media_adapter_find_player(adapter, sender, path) != NULL) return btd_error_already_exists(msg); mp = media_player_create(adapter, sender, path, &err); if (mp == NULL) { if (err == -EPROTONOSUPPORT) return btd_error_not_supported(msg); else return btd_error_invalid_args(msg); } if (parse_player_properties(mp, &args) == FALSE) { media_player_destroy(mp); return btd_error_invalid_args(msg); } return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); }
static void path_free(void *data) { struct media_adapter *adapter = data; while (adapter->endpoints) release_endpoint(adapter->endpoints->data); while (adapter->players) media_player_destroy(adapter->players->data); adapters = g_slist_remove(adapters, adapter); btd_adapter_unref(adapter->btd_adapter); g_free(adapter); }
static struct media_player *media_player_create(struct media_adapter *adapter, const char *sender, const char *path, int *err) { DBusConnection *conn = btd_get_dbus_connection(); struct media_player *mp; mp = g_new0(struct media_player, 1); mp->adapter = adapter; mp->sender = g_strdup(sender); mp->path = g_strdup(path); mp->timer = g_timer_new(); mp->watch = g_dbus_add_disconnect_watch(conn, sender, media_player_exit, mp, NULL); mp->properties_watch = g_dbus_add_properties_watch(conn, sender, path, MEDIA_PLAYER_INTERFACE, properties_changed, mp, NULL); mp->seek_watch = g_dbus_add_signal_watch(conn, sender, path, MEDIA_PLAYER_INTERFACE, "Seeked", position_changed, mp, NULL); mp->player = avrcp_register_player(adapter->btd_adapter, &player_cb, mp, media_player_free); if (!mp->player) { if (err) *err = -EPROTONOSUPPORT; media_player_destroy(mp); return NULL; } mp->settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); adapter->players = g_slist_append(adapter->players, mp); info("Player registered: sender=%s path=%s", sender, path); if (err) *err = 0; return mp; }
static void media_player_remove(struct media_player *mp) { info("Player unregistered: sender=%s path=%s", mp->sender, mp->path); media_player_destroy(mp); }