Project

General

Profile

scrobble-http-3.diff

Michael Jacobs, June 19, 2012 23:57

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)
70
    {
74 71
        tuple_unref (submit_tuple);
75
    submit_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
76
    if (! submit_tuple)
77
        return;
72
        submit_tuple = NULL;
73
    }
74

  
75
    {
76
        Tuple *temp_tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
77
        if (! temp_tuple)
78
            return;
79
        submit_tuple = tuple_copy (temp_tuple);
80
        tuple_unref (temp_tuple);
81
    }
78 82

  
79
    sc_addentry(m_scrobbler, submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000);
83
    if (is_http_source)
84
    {
85
        if (tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) <= 0)
86
            tuple_set_int(submit_tuple, FIELD_LENGTH, NULL, 240000);
87
    }
88

  
89
    sc_addentry(m_scrobbler, submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000, is_http_source);
80 90

  
81 91
    if (!track_timeout)
82
        track_timeout = g_timeout_add_seconds(1, sc_timeout, NULL);
92
        track_timeout = g_timeout_add_seconds(1, sc_timeout, (gpointer)m_scrobbler);
83 93
}
84 94

  
85 95
static void aud_hook_playback_end(gpointer aud_hook_data, gpointer user_data)
86 96
{
97
    sc_playback_end(m_scrobbler);
98

  
87 99
    sc_idle(m_scrobbler);
88 100

  
89 101
    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
        { 
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, np_item->is_http_source);
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
            if (tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) <= 0)
331
            {
332
                tuple_set_int(submit_tuple, FIELD_LENGTH, NULL, 240000);
333
                current_item->len = 240;
334
            }
335

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

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

  
342
            sc_idle((GMutex*)data);
343

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

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

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

  
288 363
    return TRUE;
289 364
}
290 365

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

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

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

  
291 380
/* Error functions */
292 381

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

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

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

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

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