2006-10-19 09:42:58 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-10-07 18:39:44 +00:00
|
|
|
* Copyright (C) 2006-2008 Robert Keevil
|
2006-10-19 09:42:58 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-10-19 09:42:58 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
/*
|
|
|
|
Audioscrobbler spec at:
|
|
|
|
http://www.audioscrobbler.net/wiki/Portable_Player_Logging
|
|
|
|
*/
|
|
|
|
|
2010-05-06 21:04:40 +00:00
|
|
|
#include <stdio.h>
|
2010-06-21 16:53:00 +00:00
|
|
|
#include <config.h>
|
2006-10-19 09:42:58 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "logf.h"
|
2008-10-15 06:38:51 +00:00
|
|
|
#include "metadata.h"
|
2006-10-19 09:42:58 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "audio.h"
|
2011-08-30 14:01:33 +00:00
|
|
|
#include "core_alloc.h"
|
2006-10-19 09:42:58 +00:00
|
|
|
#include "settings.h"
|
2007-02-11 05:34:14 +00:00
|
|
|
#include "ata_idle_notify.h"
|
2010-08-01 16:15:27 +00:00
|
|
|
#include "filefuncs.h"
|
2008-10-16 10:38:03 +00:00
|
|
|
#include "appevents.h"
|
2006-10-19 09:42:58 +00:00
|
|
|
|
2007-03-16 23:47:03 +00:00
|
|
|
#if CONFIG_RTC
|
2006-10-19 09:42:58 +00:00
|
|
|
#include "time.h"
|
|
|
|
#include "timefuncs.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "scrobbler.h"
|
|
|
|
|
2008-10-07 18:39:44 +00:00
|
|
|
#define SCROBBLER_VERSION "1.1"
|
2006-10-19 09:42:58 +00:00
|
|
|
|
|
|
|
/* increment this on any code change that effects output */
|
2006-10-19 10:25:10 +00:00
|
|
|
#define SCROBBLER_REVISION " $Revision$"
|
2006-10-19 09:42:58 +00:00
|
|
|
|
|
|
|
#define SCROBBLER_MAX_CACHE 32
|
|
|
|
/* longest entry I've had is 323, add a safety margin */
|
|
|
|
#define SCROBBLER_CACHE_LEN 512
|
|
|
|
|
|
|
|
static bool scrobbler_initialised = false;
|
2013-07-12 16:06:38 +00:00
|
|
|
static int scrobbler_cache = 0;
|
|
|
|
static int cache_pos = 0;
|
|
|
|
static bool pending = false;
|
2007-03-16 23:47:03 +00:00
|
|
|
#if CONFIG_RTC
|
2006-10-19 09:42:58 +00:00
|
|
|
static time_t timestamp;
|
2013-07-12 16:06:38 +00:00
|
|
|
#define BASE_FILENAME ".scrobbler.log"
|
|
|
|
#define HDR_STR_TIMELESS
|
|
|
|
#define get_timestamp() ((long)timestamp)
|
|
|
|
#define record_timestamp() ((void)(timestamp = mktime(get_time())))
|
|
|
|
#else /* !CONFIG_RTC */
|
|
|
|
#define HDR_STR_TIMELESS " Timeless"
|
|
|
|
#define BASE_FILENAME ".scrobbler-timeless.log"
|
|
|
|
#define get_timestamp() (0l)
|
|
|
|
#define record_timestamp() ({})
|
|
|
|
#endif /* CONFIG_RTC */
|
2006-10-19 09:42:58 +00:00
|
|
|
|
2011-02-15 21:13:03 +00:00
|
|
|
static void get_scrobbler_filename(char *path, size_t size)
|
|
|
|
{
|
|
|
|
int used;
|
|
|
|
/* Get location of USB mass storage area */
|
2011-02-15 21:32:53 +00:00
|
|
|
#ifdef APPLICATION
|
|
|
|
#if (CONFIG_PLATFORM & PLATFORM_MAEMO)
|
2013-07-12 16:06:38 +00:00
|
|
|
used = snprintf(path, size, "/home/user/MyDocs/%s", BASE_FILENAME);
|
2011-02-15 21:32:53 +00:00
|
|
|
#elif (CONFIG_PLATFORM & PLATFORM_ANDROID)
|
2013-07-12 16:06:38 +00:00
|
|
|
used = snprintf(path, size, "/sdcard/%s", BASE_FILENAME);
|
2012-09-03 19:48:18 +00:00
|
|
|
#elif defined (SAMSUNG_YPR0)
|
2013-07-12 16:06:38 +00:00
|
|
|
used = snprintf(path, size, "%s/%s", HOME_DIR, BASE_FILENAME);
|
2011-02-15 21:32:53 +00:00
|
|
|
#else /* SDL/unknown RaaA build */
|
2013-07-12 16:06:38 +00:00
|
|
|
used = snprintf(path, size, "%s/%s", ROCKBOX_DIR, BASE_FILENAME);
|
2011-02-15 21:32:53 +00:00
|
|
|
#endif /* (CONFIG_PLATFORM & PLATFORM_MAEMO) */
|
|
|
|
|
2011-02-15 21:13:03 +00:00
|
|
|
#else
|
2013-07-12 16:06:38 +00:00
|
|
|
used = snprintf(path, size, "/%s", BASE_FILENAME);
|
2011-02-15 21:13:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (used >= (int)size)
|
|
|
|
{
|
|
|
|
logf("SCROBBLER: not enough buffer space for log file");
|
|
|
|
memset(path, 0, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
static void write_cache(void)
|
|
|
|
{
|
|
|
|
int i;
|
2008-04-15 18:10:22 +00:00
|
|
|
int fd;
|
2006-10-19 09:42:58 +00:00
|
|
|
|
2011-02-15 21:13:03 +00:00
|
|
|
char scrobbler_file[MAX_PATH];
|
|
|
|
get_scrobbler_filename(scrobbler_file, MAX_PATH);
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
/* If the file doesn't exist, create it.
|
|
|
|
Check at each write since file may be deleted at any time */
|
2011-02-15 21:13:03 +00:00
|
|
|
if(!file_exists(scrobbler_file))
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
2011-02-15 21:13:03 +00:00
|
|
|
fd = open(scrobbler_file, O_RDWR | O_CREAT, 0666);
|
2008-04-15 18:10:22 +00:00
|
|
|
if(fd >= 0)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
2008-04-15 18:10:22 +00:00
|
|
|
fdprintf(fd, "#AUDIOSCROBBLER/" SCROBBLER_VERSION "\n"
|
2013-07-12 16:06:38 +00:00
|
|
|
"#TZ/UNKNOWN\n" "#CLIENT/Rockbox "
|
|
|
|
TARGET_NAME SCROBBLER_REVISION
|
|
|
|
HDR_STR_TIMELESS "\n");
|
2008-04-15 18:10:22 +00:00
|
|
|
|
|
|
|
close(fd);
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf("SCROBBLER: cannot create log file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write the cache entries */
|
2011-02-15 21:13:03 +00:00
|
|
|
fd = open(scrobbler_file, O_WRONLY | O_APPEND);
|
2008-04-15 18:10:22 +00:00
|
|
|
if(fd >= 0)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
2013-07-12 16:03:20 +00:00
|
|
|
logf("SCROBBLER: writing %d entries", cache_pos);
|
2011-08-30 14:01:33 +00:00
|
|
|
/* copy data to temporary storage in case data moves during I/O */
|
|
|
|
char temp_buf[SCROBBLER_CACHE_LEN];
|
2006-10-19 09:42:58 +00:00
|
|
|
for ( i=0; i < cache_pos; i++ )
|
|
|
|
{
|
|
|
|
logf("SCROBBLER: write %d", i);
|
2011-08-30 14:01:33 +00:00
|
|
|
char* scrobbler_buf = core_get_data(scrobbler_cache);
|
|
|
|
ssize_t len = strlcpy(temp_buf, scrobbler_buf+(SCROBBLER_CACHE_LEN*i),
|
|
|
|
sizeof(temp_buf));
|
|
|
|
if (write(fd, temp_buf, len) != len)
|
|
|
|
break;
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
2008-04-15 18:10:22 +00:00
|
|
|
close(fd);
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf("SCROBBLER: error writing file");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear even if unsuccessful - don't want to overflow the buffer */
|
|
|
|
cache_pos = 0;
|
|
|
|
}
|
|
|
|
|
2009-10-20 21:54:44 +00:00
|
|
|
static void scrobbler_flush_callback(void *data)
|
2007-02-11 05:34:14 +00:00
|
|
|
{
|
2009-10-20 21:54:44 +00:00
|
|
|
(void)data;
|
2007-02-11 05:34:14 +00:00
|
|
|
if (scrobbler_initialised && cache_pos)
|
|
|
|
write_cache();
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
static void add_to_cache(const struct mp3entry *id)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
|
|
|
if ( cache_pos >= SCROBBLER_MAX_CACHE )
|
|
|
|
write_cache();
|
|
|
|
|
|
|
|
char rating = 'S'; /* Skipped */
|
2011-08-30 14:01:33 +00:00
|
|
|
char* scrobbler_buf = core_get_data(scrobbler_cache);
|
2006-10-19 09:42:58 +00:00
|
|
|
|
|
|
|
logf("SCROBBLER: add_to_cache[%d]", cache_pos);
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
if (id->elapsed > id->length / 2)
|
2006-10-19 09:42:58 +00:00
|
|
|
rating = 'L'; /* Listened */
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
char tracknum[11] = { "" };
|
|
|
|
|
|
|
|
if (id->tracknum > 0)
|
|
|
|
snprintf(tracknum, sizeof (tracknum), "%d", id->tracknum);
|
|
|
|
|
|
|
|
int ret = snprintf(scrobbler_buf+(SCROBBLER_CACHE_LEN*cache_pos),
|
|
|
|
SCROBBLER_CACHE_LEN,
|
|
|
|
"%s\t%s\t%s\t%s\t%d\t%c\t%ld\t%s\n",
|
|
|
|
id->artist,
|
|
|
|
id->album ?: "",
|
|
|
|
id->title,
|
|
|
|
tracknum,
|
|
|
|
(int)(id->length / 1000),
|
|
|
|
rating,
|
|
|
|
get_timestamp(),
|
|
|
|
id->mb_track_id ?: "");
|
2006-10-19 09:42:58 +00:00
|
|
|
|
|
|
|
if ( ret >= SCROBBLER_CACHE_LEN )
|
|
|
|
{
|
|
|
|
logf("SCROBBLER: entry too long:");
|
2013-07-12 16:06:38 +00:00
|
|
|
logf("SCROBBLER: %s", id->path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-19 09:42:58 +00:00
|
|
|
cache_pos++;
|
2008-11-01 16:14:28 +00:00
|
|
|
register_storage_idle_func(scrobbler_flush_callback);
|
2007-02-11 05:34:14 +00:00
|
|
|
}
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 21:54:44 +00:00
|
|
|
static void scrobbler_change_event(void *data)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
2013-07-12 16:06:38 +00:00
|
|
|
struct mp3entry *id = ((struct track_event *)data)->id3;
|
2006-10-19 09:42:58 +00:00
|
|
|
|
|
|
|
/* check if track was resumed > %50 played
|
|
|
|
check for blank artist or track name */
|
2013-07-12 16:06:38 +00:00
|
|
|
if (id->elapsed > id->length / 2 || !id->artist || !id->title)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
|
|
|
pending = false;
|
|
|
|
logf("SCROBBLER: skipping file %s", id->path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf("SCROBBLER: add pending");
|
2013-07-12 16:06:38 +00:00
|
|
|
record_timestamp();
|
2006-10-19 09:42:58 +00:00
|
|
|
pending = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
static void scrobbler_finish_event(void *data)
|
|
|
|
{
|
|
|
|
struct track_event *te = (struct track_event *)data;
|
|
|
|
|
|
|
|
/* add entry using the currently ending track */
|
|
|
|
if (pending && (te->flags & TEF_CURRENT)
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
|
|
|
&& !(te->flags & TEF_REWIND)
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
|
|
|
pending = false;
|
|
|
|
add_to_cache(te->id3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
int scrobbler_init(void)
|
|
|
|
{
|
2013-07-12 16:06:38 +00:00
|
|
|
if (scrobbler_initialised)
|
|
|
|
return 1;
|
2007-06-24 18:46:04 +00:00
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
scrobbler_cache = core_alloc("scrobbler",
|
|
|
|
SCROBBLER_MAX_CACHE*SCROBBLER_CACHE_LEN);
|
2006-10-19 09:42:58 +00:00
|
|
|
|
2012-06-10 15:15:47 +00:00
|
|
|
if (scrobbler_cache <= 0)
|
2011-08-30 14:01:45 +00:00
|
|
|
{
|
|
|
|
logf("SCROOBLER: OOM");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-19 09:42:58 +00:00
|
|
|
|
|
|
|
cache_pos = 0;
|
|
|
|
pending = false;
|
2013-07-12 16:06:38 +00:00
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
scrobbler_initialised = true;
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
add_event(PLAYBACK_EVENT_TRACK_CHANGE, false, scrobbler_change_event);
|
|
|
|
add_event(PLAYBACK_EVENT_TRACK_FINISH, false, scrobbler_finish_event);
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-08-01 19:43:13 +00:00
|
|
|
static void scrobbler_flush_cache(void)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
|
|
|
/* Add any pending entries to the cache */
|
2013-07-12 16:06:38 +00:00
|
|
|
if (pending)
|
|
|
|
{
|
2006-10-19 09:42:58 +00:00
|
|
|
pending = false;
|
2013-07-12 16:06:38 +00:00
|
|
|
if (audio_status())
|
|
|
|
add_to_cache(audio_current_track());
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
2013-07-12 16:06:38 +00:00
|
|
|
|
|
|
|
/* Write the cache to disk if needed */
|
|
|
|
if (cache_pos)
|
|
|
|
write_cache();
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
void scrobbler_shutdown(bool poweroff)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
2013-07-12 16:06:38 +00:00
|
|
|
if (!scrobbler_initialised)
|
|
|
|
return;
|
|
|
|
|
|
|
|
remove_event(PLAYBACK_EVENT_TRACK_CHANGE, scrobbler_change_event);
|
|
|
|
remove_event(PLAYBACK_EVENT_TRACK_FINISH, scrobbler_finish_event);
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
scrobbler_flush_cache();
|
2007-06-24 18:46:04 +00:00
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
if (!poweroff)
|
2006-10-19 09:42:58 +00:00
|
|
|
{
|
2011-08-30 14:01:45 +00:00
|
|
|
/* get rid of the buffer */
|
|
|
|
core_free(scrobbler_cache);
|
|
|
|
scrobbler_cache = 0;
|
2006-10-19 09:42:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-12 16:06:38 +00:00
|
|
|
scrobbler_initialised = false;
|
2007-06-24 18:46:04 +00:00
|
|
|
}
|
|
|
|
|
2006-10-19 09:42:58 +00:00
|
|
|
bool scrobbler_is_enabled(void)
|
|
|
|
{
|
|
|
|
return scrobbler_initialised;
|
|
|
|
}
|