~audio-recorder/audio-recorder/trunk

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
/*
 * Copyright (c) Linux community.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <string.h>
#include <glib.h>
#include <gdk/gdk.h>
#include <gio/gio.h>
#include <unistd.h>

#include "log.h"
#include "utility.h"
#include "support.h"
#include "dbus-player.h"

#include "audio-sources.h"

// MPRIS2 compliant players.
#include "dbus-mpris2.h"

// Handcrafted DBus-interface for Skype.
#include "dbus-skype.h"

// Send commands to rec-manager.c
#include "rec-manager-struct.h"

// List of players
static GHashTable *g_player_list = NULL;

static void dbus_player_disconnect_signals();
static void dbus_player_clear_list();

void add_player_manually(gchar *app_name, gchar *exec_name, gchar *service_name);
void add_skype();

void dbus_player_init() {
    LOG_DEBUG("Init dbus-player.c.\n");

    g_player_list = NULL;

    skype_module_init();

    mpris2_module_init();
}

void dbus_player_exit() {
    LOG_DEBUG("Clean up dbus-player.c.\n");

    // Disconnect DBus signals for all Media Players
    dbus_player_disconnect_signals();

    // Clear the player list
    dbus_player_clear_list();

    mpris2_module_exit();

    skype_module_exit();
}

static RecorderCommand *convert_data(MediaPlayerRec *pl) {
    // Convert MediaPlayerRec to RecorderCommand

    if (!pl) return NULL;
    TrackInfo *tr = &pl->track;

    RecorderCommand *cmd = g_malloc0(sizeof(RecorderCommand));
    cmd->track = g_strndup(tr->track, MPRIS_STRLEN);
    cmd->artist = g_strndup(tr->artist, MPRIS_STRLEN);
    cmd->album = g_strndup(tr->album, MPRIS_STRLEN);
    cmd->track_pos = tr->current_secs;
    cmd->track_len = tr->total_secs;
    cmd->flags = tr->flags;

    // MediaPlayer changed status.
    // Check its status.
    if (tr->status == PLAYER_STATUS_PAUSED) {
        cmd->type = RECORDING_PAUSE;

    } else if (tr->status == PLAYER_STATUS_PLAYING) {
        cmd->type = RECORDING_START;

    } else if (tr->status == PLAYER_STATUS_NOTIFY_MSG) {
        cmd->type = RECORDING_NOTIFY_MSG;

    } else {
        // tr.status == PLAYER_STATUS_CLOSED ||
        // tr.status == PLAYER_STATUS_STOPPED
        cmd->type = RECORDING_STOP;
    }

    return cmd;
}

void dbus_player_process_data(gpointer player) {
    // Send message to the rec-manager.c
    if (!player) return;

    // Convert MediaPlayerRec to RecorderCommand
    RecorderCommand *cmd = convert_data(player);

    // Send the command. Rec-manager will free the cmd structure after processing.
    rec_manager_send_command(cmd);
}

void dbus_player_player_changed(gchar *service_name) {
    // Re-connect DBus signals/methods for the given service_name (Media Player or Skype, etc)

    // Disconnect all signals/object methods
    dbus_player_disconnect_signals();

    // Get MediaPlayerRec for this service_name
    MediaPlayerRec *player = dbus_player_lookup_service_name(service_name);

    if (player && player->func_set_signals) {

        LOG_PLAYER("Connect DBus signals for %s (%s).\n", player->app_name, player->service_name);

        // Start application (Media Player, Skype, etc)
        if (player->func_start_app) {
            player->func_start_app(player);
        }

        // Connect signals so we receive track-changed/start/stop messages from this app (over DBus)
        player->func_set_signals(player, TRUE); // TRUE=connect/register, FALSE=disconnect/unregister

    }
}

static void dbus_player_disconnect_signals() {
    // Disconnect all signal-functions from the DBus
    GHashTableIter iter;
    gpointer key, value;

    g_hash_table_iter_init(&iter, g_player_list);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        MediaPlayerRec *player = (MediaPlayerRec*)value;
        if (player && player->func_set_signals) {
            // Disconnect signals
            player->func_set_signals(player, FALSE); // FALSE=disconnect/unregister
        }
    }
}

GHashTable *dbus_player_get_list_ref() {
    if (!g_player_list)
        g_player_list = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, dbus_player_delete_item);

    return g_player_list;
}

static gboolean dbus_palyer_remove_node(gpointer key, gpointer value, gpointer user_data) {
    // Return TRUE so this (key, value) pair gets removed and deleted.
    return TRUE;
}

static void dbus_player_clear_list() {
    // Delete the entire g_player_list
    if (g_player_list) {
        g_hash_table_foreach_remove(g_player_list, dbus_palyer_remove_node, NULL);
        g_hash_table_unref(g_player_list);
    }
    g_player_list = NULL;
}

void dbus_player_delete_item(gpointer data) {
    MediaPlayerRec *player = (MediaPlayerRec*)data;
    if (!player) return;

    LOG_PLAYER("dbus_player_delete_item: %s (%s).\n", player->app_name, player->service_name);

    if (player->func_set_signals) {
        // Disconnect signals
        player->func_set_signals(player, FALSE); // TRUE=connect, FALSE=disconnect
    }

    if (G_IS_DBUS_PROXY(player->proxy)) {
        g_object_unref(player->proxy);
    }
    player->proxy = NULL;

    if (G_IS_DBUS_PROXY(player->prop_proxy)) {
        g_object_unref(player->prop_proxy);
    }
    player->prop_proxy = NULL;

    g_free(player->service_name);
    g_free(player->exec_name);
    g_free(player->app_name);
    g_free(player->icon_name);
    g_free(player);
}

MediaPlayerRec *dbus_player_lookup_app_name(gchar *app_name) {
    // Lookup player by its application name (p->app_name).
    // Typical app_names are "Amarok 2.3.2", "RhythmBox 2.3" and "Skype 2.1.0".
    GHashTableIter iter;
    gpointer key, value;

    if (str_length(app_name, 1024) < 1) return NULL;

    g_hash_table_iter_init(&iter, g_player_list);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        MediaPlayerRec *p = (MediaPlayerRec*)value;
        if (!g_strcmp0(p->app_name, app_name)) {
            return p;
        }
    }
    return NULL;
}

MediaPlayerRec *dbus_player_lookup_service_name(gchar *service_name) {
    // Lookup player by its service name (p->service_name).
    // "org.mpris.MediaPlayer2.Player.banshee" is a typical service_name.
    GHashTableIter iter;
    gpointer key, value;

    if (str_length(service_name, 1024) < 1) return NULL;

    g_hash_table_iter_init(&iter, g_player_list);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        MediaPlayerRec *p = (MediaPlayerRec*)value;
        if (!g_strcmp0(p->service_name, service_name)) {
            return p;
        }
    }
    return NULL;
}

void dbus_player_debug_print(MediaPlayerRec *p) {
    LOG_PLAYER("------------------------------\n");

    LOG_PLAYER("Player app name:%s\n", p->app_name);
    LOG_PLAYER("Player exec name:%s\n", p->exec_name);
    LOG_PLAYER("Service name:%s\n", p->service_name);

    TrackInfo *tr = &p->track;

    switch (tr->status) {
    case PLAYER_STATUS_CLOSED:
        LOG_PLAYER("Status:%d  PLAYER_STATUS_CLOSED (not running)\n", tr->status);
        break;

    case PLAYER_STATUS_STOPPED:
        LOG_PLAYER("Status:%d  PLAYER_STATUS_STOPPED\n", tr->status);
        break;

    case PLAYER_STATUS_PAUSED:
        LOG_PLAYER("Status:%d  PLAYER_STATUS_PAUSED\n", tr->status);
        break;

    case PLAYER_STATUS_PLAYING:
        LOG_PLAYER("Status:%d  PLAYER_STATUS_PLAYING\n", tr->status);
        break;

    case PLAYER_STATUS_NOTIFY_MSG:
        // Simply a msg to the GUI.
        LOG_PLAYER("Status:%d  PLAYER_STATUS_NOTIFY_MSG\n", tr->status);
        break;

    default:
        LOG_PLAYER("Unknown status:%d\n", tr->status);
    }

    if (tr->status != PLAYER_STATUS_NOTIFY_MSG) {
        LOG_PLAYER("Track:%s\n", tr->track);
        LOG_PLAYER("Artist:%s\n", tr->artist);
        LOG_PLAYER("Album:%s\n", tr->album);
        LOG_PLAYER("Current position in seconds:%d\n", tr->current_secs);
        LOG_PLAYER("Total length in seconds:%d\n", tr->total_secs);
        LOG_PLAYER("Flags:%d\n", tr->flags);
    } else {
        // Simply a msg to the GUI.
        LOG_PLAYER("Message:%s\n", tr->track);
    }

    LOG_PLAYER("------------------------------\n");
}

GHashTable *dbus_player_get_player_list() {
    // Clear the old list
    dbus_player_clear_list();

    // Populate the list.
    // Detect players that follow the org.mpris.MediaPlayer2.* standard.
    mpris2_detect_players();

    // Make sure the most popular players are in the list. 

    // Add Rhythmbox manually (check if installed)
    // TODO: Should we translate the app-names?
    add_player_manually("Rhythmbox", "rhythmbox", "org.mpris.MediaPlayer2.rhythmbox");

    // Add Banshee manually (check if installed)
    add_player_manually("Banshee", "banshee", "org.mpris.MediaPlayer2.banshee");

    // Add Skype manually (check if installed)
    add_skype();

    return g_player_list;
}

void add_player_manually(gchar *app_name, gchar *exec_name, gchar *service_name) {
    // Add player manually to the list. Check if it's installed.

    // Already in the list?
    if (dbus_player_lookup_service_name(service_name)) return;

    // Find executable /usr/bin/exec_name
    gchar *path = find_command_path(exec_name);
    if (!path) {
        // Not installed.
        return;
    }
    g_free(path);

    // New MediaPlayer record
    MediaPlayerRec *player = mpris2_player_new(service_name);

    // Set executable
    player->exec_name = g_strdup(exec_name);

    // Set application name. This is shown in the listbox
    player->app_name = g_strdup(app_name);

    // Function to connect/disconnect event signals for this player
    player->func_set_signals = mpris2_set_signals;

    // Function to get track-info (album, track/song name/title, genre, etc.)
    player->func_get_info = mpris2_get_metadata;

    // Function to start/run the media player
    player->func_start_app = mpris2_start_app;

    // Function to check if this player is running
    player->func_check_is_running = mpris2_service_is_running;

    // Set icon name
    player->icon_name = g_strdup(exec_name);

    if (!dbus_player_lookup_app_name(player->app_name)) {
        // Add to list
        GHashTable *player_list = dbus_player_get_list_ref();
        g_hash_table_insert(player_list, g_strdup(player->service_name), player);
    } else {
        // Duplicate or bad record. Free it.
        dbus_player_delete_item(player);
    }
}

void add_skype() {
    // Add Skype

    gchar *service_name = "com.Skype.API";

    // Already in the list?
    if (dbus_player_lookup_service_name(service_name)) return;

    // Find /usr/bin/skype
    gchar *path = find_command_path("skype");
    if (!path) {
        // Not installed.
        return;
    }
    g_free(path);

    // New MediaPlayer record (yep, we store Skype data in a MediaPlayer record)
    MediaPlayerRec *player = mpris2_player_new(service_name);

    player->type = COMM_PROGRAM;

    // Set application name
    player->app_name = skype_get_app_name();

    // Function to register/unregister notification methods
    player->func_set_signals = skype_setup;

    // Function to get track-info (strictly, Skype does not have "track" data, though it has target filename)
    player->func_get_info = skype_get_info;

    // Function to start/run Skype
    player->func_start_app = skype_start_app;

    // Set icon
    player->icon_name = g_strdup("skype");

    if (!dbus_player_lookup_app_name(player->app_name)) {
        // Add to list
        GHashTable *player_list = dbus_player_get_list_ref();
        g_hash_table_insert(player_list, g_strdup(player->service_name), player);
    } else {
        // Duplicate or bad record. Free it.
        dbus_player_delete_item(player);
    }
}

void dbus_player_send_notification(gchar *msg) {
    RecorderCommand *cmd = g_malloc0(sizeof(RecorderCommand));
    cmd->type = RECORDING_NOTIFY_MSG;
    cmd->track = g_strndup(msg, MPRIS_STRLEN);

    // Send command to rec-manager.c. 
    // It will free the cmd structure after processing.
    rec_manager_send_command(cmd);
}