Project

General

Profile

scrobble-http-2.diff

Michael Jacobs, June 17, 2012 18:31

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)
71

  
72
    Tuple *temp_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
73
    if (! temp_tuple)
77 74
        return;
75
    submit_tuple = tuple_copy (temp_tuple);
76
    tuple_unref (temp_tuple);
77
    temp_tuple = NULL;
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

  
......
137 139
    return newitem;
138 140
}
139 141

  
140
static item_t *create_item(Tuple *tuple, int len)
142
static item_t *create_item(Tuple *tuple, int len, gboolean is_http_source)
141 143
{
142 144
    item_t *item;
143 145
    gchar *album, *artist, *title;
......
172 174
    else
173 175
        item->album = fmt_escape("");
174 176

  
177
    item->is_http_source = is_http_source;
178

  
175 179
    item->next = NULL;
176 180

  
177 181
    return item;
......
181 185
{
182 186
    item_t *item;
183 187

  
184
    if ((item = create_item (tuple, len)) == NULL)
188
    if ((item = create_item (tuple, len, FALSE)) == NULL)
185 189
        return NULL;
186 190

  
187 191
    item->timeplayed = len;
......
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
    if ((np_item = create_item (tuple, len)) != NULL)
202
    if ((np_item = create_item (tuple, len, is_http_source)) != NULL)
203
    {
199 204
        AUDDBG ("Tracking now-playing track: %s - %s\n", np_item->artist,
200 205
         np_item->title);
206
    }
201 207

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

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

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

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

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

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

  
316
        item_t* current_item = create_item(submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000, np_item->is_http_source);
317
        if (! current_item)
318
            return TRUE;
319

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

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

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

  
335
            np_item->len = np_item->timeplayed;
336

  
337
            q_additem(np_item);
338
            np_item = current_item;
339
            dump_queue();
340

  
341
            sc_idle((GMutex*)data);
342

  
343
            sc_submit_np(submit_tuple);
344
        }
345
        else
346
        {
347
            q_item_free(current_item);
348

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

  
358
        tuple_unref (submit_tuple);
359
    }
360
    }
287 361

  
288 362
    return TRUE;
289 363
}
290 364

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

  
371
        np_item->len = np_item->timeplayed;
372

  
373
                q_additem(np_item);
374
                np_item = NULL;
375
                dump_queue();
376
    }
377
}
378

  
291 379
/* Error functions */
292 380

  
293 381
static void sc_throw_error(char *errortxt)
......
1126 1214
    AUDDBG("scrobbler starting up\n");
1127 1215
}
1128 1216

  
1129
void sc_addentry(GMutex *mutex, Tuple *tuple, int len)
1217
void sc_addentry(GMutex *mutex, Tuple *tuple, int len, gboolean is_http_source)
1130 1218
{
1131 1219
    g_mutex_lock(mutex);
1132 1220

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

  
1133 1223
    sc_submit_np(tuple);
1134
    set_np(tuple, len);
1224
    set_np(tuple, len, is_http_source);
1135 1225

  
1136 1226
    /*
1137 1227
     * 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