Project

General

Profile

scrobble-http.diff

John Lindgren, June 16, 2012 23:30

View differences:

src/scrobbler/plugin.c
53 53
    gint playlist = aud_playlist_get_playing();
54 54
    gint pos = aud_playlist_get_position(playlist);
55 55

  
56
    if (aud_playlist_entry_get_length (playlist, pos, FALSE) < 30)
57
    {
58
        AUDDBG(" *** not submitting due to entry->length < 30");
59
        return;
60
    }
61

  
62 56
    gchar * filename = aud_playlist_entry_get_filename (playlist, pos);
63
    if (ishttp (filename))
57
    gboolean is_http_source = ishttp (filename);
58
    str_unref (filename);
59

  
60
    if (aud_playlist_entry_get_length (playlist, pos, FALSE) < 30
61
        && !is_http_source)
64 62
    {
65
        AUDDBG(" *** not submitting due to HTTP source");
66
        str_unref (filename);
63
        AUDDBG(" *** not submitting due to entry->length < 30");
67 64
        return;
68 65
    }
69
    str_unref (filename);
70 66

  
71 67
    sc_idle(m_scrobbler);
72 68

  
73 69
    if (submit_tuple)
74 70
        tuple_unref (submit_tuple);
75
    submit_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
76
    if (! submit_tuple)
77
        return;
71
    {
72
        Tuple *temp_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
73
        if (! temp_tuple)
74
            return;
75
        submit_tuple = tuple_copy (temp_tuple);
76
        tuple_unref (temp_tuple);
77
    }
78 78

  
79
    sc_addentry(m_scrobbler, submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000);
79
    if (is_http_source)
80
    {
81
        if (tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) <= 0)
82
            tuple_set_int(submit_tuple, FIELD_LENGTH, NULL, 240000);
83
    }
84

  
85
    sc_addentry(m_scrobbler, submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000, is_http_source);
80 86

  
81 87
    if (!track_timeout)
82
        track_timeout = g_timeout_add_seconds(1, sc_timeout, NULL);
88
        track_timeout = g_timeout_add_seconds(1, sc_timeout, (gpointer)m_scrobbler);
83 89
}
84 90

  
85 91
static void aud_hook_playback_end(gpointer aud_hook_data, gpointer user_data)
86 92
{
93
    sc_playback_end(m_scrobbler);
94

  
87 95
    sc_idle(m_scrobbler);
88 96

  
89 97
    if (track_timeout)
src/scrobbler/scrobbler.c
14 14
#include <audacious/debug.h>
15 15
#include <audacious/misc.h>
16 16
#include <audacious/plugin.h>
17
#include <audacious/playlist.h>
17 18

  
18 19
#define SCROBBLER_CLI_ID "aud"
19 20
#define SCROBBLER_HS_WAIT 1800
......
63 64
    int utctime, track, len;
64 65
    int timeplayed;
65 66
    int numtries;
67
    gboolean is_http_source;
66 68
    void *next;
67 69
} item_t;
68 70

  
......
172 174
    else
173 175
        item->album = fmt_escape("");
174 176

  
177
    item->is_http_source = FALSE;
178

  
175 179
    item->next = NULL;
176 180

  
177 181
    return item;
......
190 194
    return q_additem(item);
191 195
}
192 196

  
193
static item_t *set_np(Tuple *tuple, int len)
197
static item_t *set_np(Tuple *tuple, int len, gboolean is_http_source)
194 198
{
195 199
    if (np_item != NULL)
196 200
        q_item_free (np_item);
197 201

  
198 202
    if ((np_item = create_item (tuple, len)) != NULL)
203
    {
199 204
        AUDDBG ("Tracking now-playing track: %s - %s\n", np_item->artist,
200 205
         np_item->title);
206
        np_item->is_http_source = is_http_source;
207
    }
201 208

  
202 209
    return np_item;
203 210
}
......
262 269
    while (q_get());
263 270
}
264 271

  
272
static int sc_submit_np(Tuple * tuple);
265 273

  
266 274
/* isn't there better way for that? --desowin */
267 275
gboolean sc_timeout(gpointer data)
......
271 279
        if (aud_drct_get_playing() && !aud_drct_get_paused())
272 280
            np_item->timeplayed+=1;
273 281

  
282
        if (!np_item->is_http_source)
283
    {
274 284
        /*
275 285
         * Check our now-playing track to see if it should go into the queue
276 286
         */
......
284 294
            dump_queue();
285 295
        }
286 296
    }
297
    else
298
    {
299
        /*
300
         * It is possibly a stream. So check if the track info has changed.
301
         * This is for sure not the most efficient way to do this.
302
         * But it is straight forward for now...
303
         */
304

  
305
        gint playlist = aud_playlist_get_playing();
306
        gint pos = aud_playlist_get_position(playlist);
307

  
308
        Tuple *submit_tuple = NULL;
309
        {
310
            Tuple *temp_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
311
            if (! temp_tuple)
312
                return TRUE;
313
            submit_tuple = tuple_copy (temp_tuple);
314
            tuple_unref (temp_tuple);
315
        }
316

  
317
        item_t* current_item = create_item(submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000);
318
        if (! current_item)
319
            return TRUE;
320

  
321
        AUDDBG("np: %s\ncurrent: %s\n", np_item->title, current_item->title);
322

  
323
        if ( strcmp(np_item->artist, current_item->artist)
324
            || strcmp(np_item->title, current_item->title)
325
            || strcmp(np_item->album, current_item->album)
326
            || np_item->track != current_item->track )
327
        {
328
            AUDDBG("submitting!!!\n");
329

  
330
            current_item->is_http_source = TRUE;
331

  
332
            if (tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) <= 0)
333
            {
334
                tuple_set_int(submit_tuple, FIELD_LENGTH, NULL, 240000);
335
                current_item->len = 240;
336
            }
337

  
338
            np_item->len = np_item->timeplayed;
339

  
340
            q_additem(np_item);
341
            np_item = current_item;
342
            dump_queue();
343

  
344
            sc_idle((GMutex*)data);
345

  
346
            sc_submit_np(submit_tuple);
347
        }
348
        else
349
        {
350
            q_item_free(current_item);
351

  
352
            if (np_item->timeplayed > np_item->len)
353
            {
354
                /* Make sure that the track stays on now playing. */
355
                tuple_set_int(submit_tuple, FIELD_LENGTH, NULL, np_item->len * 1000);
356
                sc_submit_np(submit_tuple);
357
                np_item->len *= 2;
358
            }
359
        }
360

  
361
        tuple_unref (submit_tuple);
362
    }
363
    }
287 364

  
288 365
    return TRUE;
289 366
}
290 367

  
368
void sc_playback_end(GMutex *mutex)
369
{
370
    if (np_item && np_item->is_http_source)
371
    {
372
        AUDDBG("submitting!!!\n");
373

  
374
        np_item->len = np_item->timeplayed;
375

  
376
                q_additem(np_item);
377
                np_item = NULL;
378
                dump_queue();
379
    }
380
}
381

  
291 382
/* Error functions */
292 383

  
293 384
static void sc_throw_error(char *errortxt)
......
1126 1217
    AUDDBG("scrobbler starting up\n");
1127 1218
}
1128 1219

  
1129
void sc_addentry(GMutex *mutex, Tuple *tuple, int len)
1220
void sc_addentry(GMutex *mutex, Tuple *tuple, int len, gboolean is_http_source)
1130 1221
{
1131 1222
    g_mutex_lock(mutex);
1132 1223

  
1224
    AUDDBG("length: %d, %d\n", tuple_get_int(tuple, FIELD_LENGTH, NULL), len);
1225

  
1133 1226
    sc_submit_np(tuple);
1134
    set_np(tuple, len);
1227
    set_np(tuple, len, is_http_source);
1135 1228

  
1136 1229
    /*
1137 1230
     * This will help make sure the queue will be saved on a nasty
src/scrobbler/scrobbler.h
14 14

  
15 15
int sc_idle(GMutex *);
16 16
void sc_init(char *, char *, char *);
17
void sc_addentry(GMutex *, Tuple *, int);
17
void sc_addentry(GMutex *, Tuple *, int, gboolean);
18 18
void sc_cleaner(void);
19 19
int sc_catch_error(void);
20 20
char *sc_fetch_error(void);
21 21
void sc_clear_error(void);
22
void sc_playback_end(GMutex *);
22 23
#endif