mirror of
https://github.com/owntone/owntone-server.git
synced 2024-12-26 23:25:56 -05:00
Merge branch 'master' into fixthreads
This commit is contained in:
commit
b128271656
24
.travis.yml
Normal file
24
.travis.yml
Normal file
@ -0,0 +1,24 @@
|
||||
language: c
|
||||
sudo: required
|
||||
dist: trusty
|
||||
env:
|
||||
matrix:
|
||||
- CFG=""
|
||||
- CFG="--enable-lastfm"
|
||||
- CFG="--enable-itunes"
|
||||
- CFG="--enable-spotify"
|
||||
|
||||
script:
|
||||
- autoreconf -fi
|
||||
- ./configure $CFG
|
||||
- scan-build --status-bugs -disable-checker deadcode.DeadStores make
|
||||
|
||||
before_install:
|
||||
- wget -q -O - https://apt.mopidy.com/mopidy.gpg | sudo apt-key add -
|
||||
- sudo wget -q -O /etc/apt/sources.list.d/mopidy.list https://apt.mopidy.com/jessie.list
|
||||
- sudo apt-get -qq update
|
||||
- sudo apt-get install -y build-essential clang git autotools-dev autoconf libtool gettext gawk gperf antlr3 libantlr3c-dev libconfuse-dev libunistring-dev libsqlite3-dev libavcodec-dev libavformat-dev libavfilter-dev libswscale-dev libavutil-dev libasound2-dev libmxml-dev libgcrypt11-dev libavahi-client-dev zlib1g-dev libevent-dev libplist-dev libcurl4-openssl-dev libjson-c-dev libspotify-dev
|
||||
|
||||
# Disable email notification
|
||||
notifications:
|
||||
email: false
|
@ -21,7 +21,7 @@ Debug domains; available domains are: \fIconfig\fP, \fIdaap\fP,
|
||||
\fIrsp\fP, \fIscan\fP, \fIxcode\fP, \fIevent\fP, \fIhttp\fP, \fIremote\fP,
|
||||
\fIdacp\fP, \fIffmpeg\fP, \fIartwork\fP, \fIplayer\fP, \fIraop\fP,
|
||||
\fIlaudio\fP, \fIdmap\fP, \fIfdbperf\fP, \fIspotify\fP, \fIlastfm\fP,
|
||||
\fIcache\fP, \fImpd\fP, \fIstream\fP.
|
||||
\fIcache\fP, \fImpd\fP, \fIstream\fP, \fIcast\fP, \fIfifo\fP, \fIlib\fP.
|
||||
.TP
|
||||
\fB\-c\fR \fIfile\fP
|
||||
Use \fIfile\fP as the configuration file.
|
||||
|
@ -2,11 +2,11 @@
|
||||
sbin_PROGRAMS = forked-daapd
|
||||
|
||||
if COND_ITUNES
|
||||
ITUNES_SRC=filescanner_itunes.c
|
||||
ITUNES_SRC=library/filescanner_itunes.c
|
||||
endif
|
||||
|
||||
if COND_SPOTIFY
|
||||
SPOTIFY_SRC=spotify.c spotify.h
|
||||
SPOTIFY_SRC=spotify.c spotify.h spotify_webapi.c spotify_webapi.h
|
||||
endif
|
||||
|
||||
if COND_LASTFM
|
||||
@ -79,9 +79,10 @@ forked_daapd_SOURCES = main.c \
|
||||
logger.c logger.h \
|
||||
conffile.c conffile.h \
|
||||
cache.c cache.h \
|
||||
filescanner.c filescanner.h \
|
||||
filescanner_ffmpeg.c filescanner_playlist.c \
|
||||
filescanner_smartpl.c $(ITUNES_SRC) \
|
||||
library/filescanner.c library/filescanner.h \
|
||||
library/filescanner_ffmpeg.c library/filescanner_playlist.c \
|
||||
library/filescanner_smartpl.c $(ITUNES_SRC) \
|
||||
library.c library.h \
|
||||
mdns_avahi.c mdns.h \
|
||||
remote_pairing.c remote_pairing.h \
|
||||
avio_evbuffer.c avio_evbuffer.h \
|
||||
|
@ -1415,7 +1415,7 @@ cache_daap_threshold(void)
|
||||
* @return 0 if successful, -1 if an error occurred
|
||||
*/
|
||||
void
|
||||
cache_artwork_ping(char *path, time_t mtime, int del)
|
||||
cache_artwork_ping(const char *path, time_t mtime, int del)
|
||||
{
|
||||
struct cache_arg *cmdarg;
|
||||
|
||||
|
@ -31,7 +31,7 @@ cache_daap_threshold(void);
|
||||
#define CACHE_ARTWORK_INDIVIDUAL 1
|
||||
|
||||
void
|
||||
cache_artwork_ping(char *path, time_t mtime, int del);
|
||||
cache_artwork_ping(const char *path, time_t mtime, int del);
|
||||
|
||||
int
|
||||
cache_artwork_delete_by_path(char *path);
|
||||
|
49
src/db.c
49
src/db.c
@ -779,7 +779,8 @@ db_purge_cruft(time_t ref)
|
||||
void
|
||||
db_purge_all(void)
|
||||
{
|
||||
#define Q_TMPL "DELETE FROM playlists WHERE type <> %d;"
|
||||
#define Q_TMPL_PL "DELETE FROM playlists WHERE type <> %d;"
|
||||
#define Q_TMPL_DIR "DELETE FROM directories WHERE id >= %d;"
|
||||
char *queries[4] =
|
||||
{
|
||||
"DELETE FROM inotify;",
|
||||
@ -807,7 +808,8 @@ db_purge_all(void)
|
||||
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
|
||||
}
|
||||
|
||||
query = sqlite3_mprintf(Q_TMPL, PL_SPECIAL);
|
||||
// Purge playlists
|
||||
query = sqlite3_mprintf(Q_TMPL_PL, PL_SPECIAL);
|
||||
if (!query)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
||||
@ -827,7 +829,31 @@ db_purge_all(void)
|
||||
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
|
||||
|
||||
sqlite3_free(query);
|
||||
#undef Q_TMPL
|
||||
|
||||
// Purge directories
|
||||
query = sqlite3_mprintf(Q_TMPL_DIR, DIR_MAX);
|
||||
if (!query)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Out of memory for query string\n");
|
||||
return;
|
||||
}
|
||||
|
||||
DPRINTF(E_DBG, L_DB, "Running purge query '%s'\n", query);
|
||||
|
||||
ret = db_exec(query, &errmsg);
|
||||
if (ret != SQLITE_OK)
|
||||
{
|
||||
DPRINTF(E_LOG, L_DB, "Purge query '%s' error: %s\n", query, errmsg);
|
||||
|
||||
sqlite3_free(errmsg);
|
||||
}
|
||||
else
|
||||
DPRINTF(E_DBG, L_DB, "Purged %d rows\n", sqlite3_changes(hdl));
|
||||
|
||||
sqlite3_free(query);
|
||||
|
||||
#undef Q_TMPL_PL
|
||||
#undef Q_TMPL_DIR
|
||||
}
|
||||
|
||||
static int
|
||||
@ -2082,7 +2108,7 @@ db_file_id_by_virtualpath_match(char *path)
|
||||
}
|
||||
|
||||
void
|
||||
db_file_stamp_bypath(char *path, time_t *stamp, int *id)
|
||||
db_file_stamp_bypath(const char *path, time_t *stamp, int *id)
|
||||
{
|
||||
#define Q_TMPL "SELECT f.id, f.db_timestamp FROM files f WHERE f.path = '%q';"
|
||||
char *query;
|
||||
@ -2845,7 +2871,7 @@ db_pl_fetch_byquery(char *query)
|
||||
}
|
||||
|
||||
struct playlist_info *
|
||||
db_pl_fetch_bypath(char *path)
|
||||
db_pl_fetch_bypath(const char *path)
|
||||
{
|
||||
#define Q_TMPL "SELECT p.* FROM playlists p WHERE p.path = '%q';"
|
||||
struct playlist_info *pli;
|
||||
@ -3009,7 +3035,7 @@ db_pl_add(struct playlist_info *pli, int *id)
|
||||
}
|
||||
|
||||
int
|
||||
db_pl_add_item_bypath(int plid, char *path)
|
||||
db_pl_add_item_bypath(int plid, const char *path)
|
||||
{
|
||||
#define Q_TMPL "INSERT INTO playlistitems (playlistid, filepath) VALUES (%d, '%q');"
|
||||
char *query;
|
||||
@ -3526,12 +3552,12 @@ db_directory_addorupdate(char *virtual_path, int disabled, int parent_id)
|
||||
}
|
||||
|
||||
void
|
||||
db_directory_ping_bymatch(char *path)
|
||||
db_directory_ping_bymatch(char *virtual_path)
|
||||
{
|
||||
#define Q_TMPL_DIR "UPDATE directories SET db_timestamp = %" PRIi64 " WHERE virtual_path = '/file:%q' OR virtual_path LIKE '/file:%q/%%';"
|
||||
#define Q_TMPL_DIR "UPDATE directories SET db_timestamp = %" PRIi64 " WHERE virtual_path = '%q' OR virtual_path LIKE '%q/%%';"
|
||||
char *query;
|
||||
|
||||
query = sqlite3_mprintf(Q_TMPL_DIR, (int64_t)time(NULL), path, path);
|
||||
query = sqlite3_mprintf(Q_TMPL_DIR, (int64_t)time(NULL), virtual_path, virtual_path);
|
||||
|
||||
db_query_run(query, 1, 0);
|
||||
#undef Q_TMPL_DIR
|
||||
@ -4663,7 +4689,10 @@ queue_fetch_byposrelativetoitem(int pos, uint32_t item_id, char shuffle, struct
|
||||
|
||||
ret = queue_fetch_bypos(pos_absolute, shuffle, queue_item, with_metadata);
|
||||
|
||||
DPRINTF(E_DBG, L_DB, "Fetch by pos: fetched item (id=%d, pos=%d, file-id=%d)\n", queue_item->id, queue_item->pos, queue_item->file_id);
|
||||
if (ret < 0)
|
||||
DPRINTF(E_LOG, L_DB, "Error fetching item by pos: pos (%d) relative to item with id (%d)\n", pos, item_id);
|
||||
else
|
||||
DPRINTF(E_DBG, L_DB, "Fetch by pos: fetched item (id=%d, pos=%d, file-id=%d)\n", queue_item->id, queue_item->pos, queue_item->file_id);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
8
src/db.h
8
src/db.h
@ -527,7 +527,7 @@ int
|
||||
db_file_id_by_virtualpath_match(char *path);
|
||||
|
||||
void
|
||||
db_file_stamp_bypath(char *path, time_t *stamp, int *id);
|
||||
db_file_stamp_bypath(const char *path, time_t *stamp, int *id);
|
||||
|
||||
struct media_file_info *
|
||||
db_file_fetch_byid(int id);
|
||||
@ -570,7 +570,7 @@ void
|
||||
db_pl_ping_bymatch(char *path, int isdir);
|
||||
|
||||
struct playlist_info *
|
||||
db_pl_fetch_bypath(char *path);
|
||||
db_pl_fetch_bypath(const char *path);
|
||||
|
||||
struct playlist_info *
|
||||
db_pl_fetch_byvirtualpath(char *virtual_path);
|
||||
@ -582,7 +582,7 @@ int
|
||||
db_pl_add(struct playlist_info *pli, int *id);
|
||||
|
||||
int
|
||||
db_pl_add_item_bypath(int plid, char *path);
|
||||
db_pl_add_item_bypath(int plid, const char *path);
|
||||
|
||||
int
|
||||
db_pl_add_item_byid(int plid, int fileid);
|
||||
@ -633,7 +633,7 @@ int
|
||||
db_directory_addorupdate(char *virtual_path, int disabled, int parent_id);
|
||||
|
||||
void
|
||||
db_directory_ping_bymatch(char *path);
|
||||
db_directory_ping_bymatch(char *virtual_path);
|
||||
|
||||
void
|
||||
db_directory_disable_bymatch(char *path, char *strip, uint32_t cookie);
|
||||
|
@ -28,7 +28,7 @@
|
||||
#define T_ADMIN \
|
||||
"CREATE TABLE IF NOT EXISTS admin(" \
|
||||
" key VARCHAR(32) PRIMARY KEY NOT NULL," \
|
||||
" value VARCHAR(32) NOT NULL" \
|
||||
" value VARCHAR(255) NOT NULL" \
|
||||
");"
|
||||
|
||||
#define T_FILES \
|
||||
|
@ -1017,6 +1017,8 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u
|
||||
}
|
||||
}
|
||||
|
||||
player_get_status(&status);
|
||||
|
||||
cuequery = evhttp_find_header(query, "query");
|
||||
if (cuequery)
|
||||
{
|
||||
@ -1031,12 +1033,9 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (status.status != PLAY_STOPPED)
|
||||
{
|
||||
player_get_status(&status);
|
||||
|
||||
if (status.status != PLAY_STOPPED)
|
||||
player_playback_stop();
|
||||
player_playback_stop();
|
||||
}
|
||||
|
||||
param = evhttp_find_header(query, "dacp.shufflestate");
|
||||
@ -1074,7 +1073,7 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u
|
||||
|
||||
dmap_send_error(req, "cacr", "Playback failed to start");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -1097,9 +1096,9 @@ dacp_reply_cue_play(struct evhttp_request *req, struct evbuffer *evbuf, char **u
|
||||
|
||||
dmap_send_error(req, "cacr", "Playback failed to start");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ret = player_playback_start_byitem(queue_item);
|
||||
free_queue_item(queue_item, 0);
|
||||
@ -1597,6 +1596,8 @@ dacp_reply_playqueuecontents(struct evhttp_request *req, struct evbuffer *evbuf,
|
||||
return;
|
||||
}
|
||||
|
||||
player_get_status(&status);
|
||||
|
||||
/*
|
||||
* If the span parameter is negativ make song list for Previously Played,
|
||||
* otherwise make song list for Up Next and begin with first song after playlist position.
|
||||
@ -1626,8 +1627,6 @@ dacp_reply_playqueuecontents(struct evhttp_request *req, struct evbuffer *evbuf,
|
||||
}
|
||||
else
|
||||
{
|
||||
player_get_status(&status);
|
||||
|
||||
memset(&query_params, 0, sizeof(struct query_params));
|
||||
if (status.shuffle)
|
||||
query_params.sort = S_SHUFFLE_POS;
|
||||
|
856
src/library.c
Normal file
856
src/library.c
Normal file
@ -0,0 +1,856 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Christian Meffert <christian.meffert@googlemail.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
#include "library.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <event2/event.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <pthread.h>
|
||||
#ifdef HAVE_PTHREAD_NP_H
|
||||
# include <pthread_np.h>
|
||||
#endif
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unictype.h>
|
||||
#include <uninorm.h>
|
||||
#include <unistr.h>
|
||||
|
||||
#include "cache.h"
|
||||
#include "commands.h"
|
||||
#include "conffile.h"
|
||||
#include "db.h"
|
||||
#include "library/filescanner.h"
|
||||
#include "logger.h"
|
||||
#include "misc.h"
|
||||
#include "player.h"
|
||||
|
||||
|
||||
static struct commands_base *cmdbase;
|
||||
static pthread_t tid_library;
|
||||
|
||||
struct event_base *evbase_lib;
|
||||
|
||||
/* Flag for aborting scan on exit */
|
||||
static bool scan_exit;
|
||||
|
||||
/* Flag for scan in progress */
|
||||
static bool scanning;
|
||||
|
||||
extern struct library_source filescanner;
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
extern struct library_source spotifyscanner;
|
||||
#endif
|
||||
|
||||
static struct library_source *sources[] = {
|
||||
&filescanner,
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
&spotifyscanner,
|
||||
#endif
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
static void
|
||||
sort_tag_create(char **sort_tag, char *src_tag)
|
||||
{
|
||||
const uint8_t *i_ptr;
|
||||
const uint8_t *n_ptr;
|
||||
const uint8_t *number;
|
||||
uint8_t out[1024];
|
||||
uint8_t *o_ptr;
|
||||
int append_number;
|
||||
ucs4_t puc;
|
||||
int numlen;
|
||||
size_t len;
|
||||
int charlen;
|
||||
|
||||
/* Note: include terminating NUL in string length for u8_normalize */
|
||||
|
||||
if (*sort_tag)
|
||||
{
|
||||
DPRINTF(E_DBG, L_LIB, "Existing sort tag will be normalized: %s\n", *sort_tag);
|
||||
o_ptr = u8_normalize(UNINORM_NFD, (uint8_t *)*sort_tag, strlen(*sort_tag) + 1, NULL, &len);
|
||||
free(*sort_tag);
|
||||
*sort_tag = (char *)o_ptr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!src_tag || ((len = strlen(src_tag)) == 0))
|
||||
{
|
||||
*sort_tag = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set input pointer past article if present
|
||||
if ((strncasecmp(src_tag, "a ", 2) == 0) && (len > 2))
|
||||
i_ptr = (uint8_t *)(src_tag + 2);
|
||||
else if ((strncasecmp(src_tag, "an ", 3) == 0) && (len > 3))
|
||||
i_ptr = (uint8_t *)(src_tag + 3);
|
||||
else if ((strncasecmp(src_tag, "the ", 4) == 0) && (len > 4))
|
||||
i_ptr = (uint8_t *)(src_tag + 4);
|
||||
else
|
||||
i_ptr = (uint8_t *)src_tag;
|
||||
|
||||
// Poor man's natural sort. Makes sure we sort like this: a1, a2, a10, a11, a21, a111
|
||||
// We do this by padding zeroes to (short) numbers. As an alternative we could have
|
||||
// made a proper natural sort algorithm in sqlext.c, but we don't, since we don't
|
||||
// want any risk of hurting response times
|
||||
memset(&out, 0, sizeof(out));
|
||||
o_ptr = (uint8_t *)&out;
|
||||
number = NULL;
|
||||
append_number = 0;
|
||||
|
||||
do
|
||||
{
|
||||
n_ptr = u8_next(&puc, i_ptr);
|
||||
|
||||
if (uc_is_digit(puc))
|
||||
{
|
||||
if (!number) // We have encountered the beginning of a number
|
||||
number = i_ptr;
|
||||
append_number = (n_ptr == NULL); // If last char in string append number now
|
||||
}
|
||||
else
|
||||
{
|
||||
if (number)
|
||||
append_number = 1; // A number has ended so time to append it
|
||||
else
|
||||
{
|
||||
charlen = u8_strmblen(i_ptr);
|
||||
if (charlen >= 0)
|
||||
o_ptr = u8_stpncpy(o_ptr, i_ptr, charlen); // No numbers in sight, just append char
|
||||
}
|
||||
}
|
||||
|
||||
// Break if less than 100 bytes remain (prevent buffer overflow)
|
||||
if (sizeof(out) - u8_strlen(out) < 100)
|
||||
break;
|
||||
|
||||
// Break if number is very large (prevent buffer overflow)
|
||||
if (number && (i_ptr - number > 50))
|
||||
break;
|
||||
|
||||
if (append_number)
|
||||
{
|
||||
numlen = i_ptr - number;
|
||||
if (numlen < 5) // Max pad width
|
||||
{
|
||||
u8_strcpy(o_ptr, (uint8_t *)"00000");
|
||||
o_ptr += (5 - numlen);
|
||||
}
|
||||
o_ptr = u8_stpncpy(o_ptr, number, numlen + u8_strmblen(i_ptr));
|
||||
|
||||
number = NULL;
|
||||
append_number = 0;
|
||||
}
|
||||
|
||||
i_ptr = n_ptr;
|
||||
}
|
||||
while (n_ptr);
|
||||
|
||||
*sort_tag = (char *)u8_normalize(UNINORM_NFD, (uint8_t *)&out, u8_strlen(out) + 1, NULL, &len);
|
||||
}
|
||||
|
||||
static void
|
||||
fixup_tags(struct media_file_info *mfi)
|
||||
{
|
||||
cfg_t *lib;
|
||||
size_t len;
|
||||
char *tag;
|
||||
char *sep = " - ";
|
||||
char *ca;
|
||||
|
||||
if (mfi->genre && (strlen(mfi->genre) == 0))
|
||||
{
|
||||
free(mfi->genre);
|
||||
mfi->genre = NULL;
|
||||
}
|
||||
|
||||
if (mfi->artist && (strlen(mfi->artist) == 0))
|
||||
{
|
||||
free(mfi->artist);
|
||||
mfi->artist = NULL;
|
||||
}
|
||||
|
||||
if (mfi->title && (strlen(mfi->title) == 0))
|
||||
{
|
||||
free(mfi->title);
|
||||
mfi->title = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Default to mpeg4 video/audio for unknown file types
|
||||
* in an attempt to allow streaming of DRM-afflicted files
|
||||
*/
|
||||
if (mfi->codectype && strcmp(mfi->codectype, "unkn") == 0)
|
||||
{
|
||||
if (mfi->has_video)
|
||||
{
|
||||
strcpy(mfi->codectype, "mp4v");
|
||||
strcpy(mfi->type, "m4v");
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(mfi->codectype, "mp4a");
|
||||
strcpy(mfi->type, "m4a");
|
||||
}
|
||||
}
|
||||
|
||||
if (!mfi->artist)
|
||||
{
|
||||
if (mfi->orchestra && mfi->conductor)
|
||||
{
|
||||
len = strlen(mfi->orchestra) + strlen(sep) + strlen(mfi->conductor);
|
||||
tag = (char *)malloc(len + 1);
|
||||
if (tag)
|
||||
{
|
||||
sprintf(tag,"%s%s%s", mfi->orchestra, sep, mfi->conductor);
|
||||
mfi->artist = tag;
|
||||
}
|
||||
}
|
||||
else if (mfi->orchestra)
|
||||
{
|
||||
mfi->artist = strdup(mfi->orchestra);
|
||||
}
|
||||
else if (mfi->conductor)
|
||||
{
|
||||
mfi->artist = strdup(mfi->conductor);
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle TV shows, try to present prettier metadata */
|
||||
if (mfi->tv_series_name && strlen(mfi->tv_series_name) != 0)
|
||||
{
|
||||
mfi->media_kind = MEDIA_KIND_TVSHOW; /* tv show */
|
||||
|
||||
/* Default to artist = series_name */
|
||||
if (mfi->artist && strlen(mfi->artist) == 0)
|
||||
{
|
||||
free(mfi->artist);
|
||||
mfi->artist = NULL;
|
||||
}
|
||||
|
||||
if (!mfi->artist)
|
||||
mfi->artist = strdup(mfi->tv_series_name);
|
||||
|
||||
/* Default to album = "<series_name>, Season <season_num>" */
|
||||
if (mfi->album && strlen(mfi->album) == 0)
|
||||
{
|
||||
free(mfi->album);
|
||||
mfi->album = NULL;
|
||||
}
|
||||
|
||||
if (!mfi->album)
|
||||
{
|
||||
len = snprintf(NULL, 0, "%s, Season %d", mfi->tv_series_name, mfi->tv_season_num);
|
||||
|
||||
mfi->album = (char *)malloc(len + 1);
|
||||
if (mfi->album)
|
||||
sprintf(mfi->album, "%s, Season %d", mfi->tv_series_name, mfi->tv_season_num);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check the 4 top-tags are filled */
|
||||
if (!mfi->artist)
|
||||
mfi->artist = strdup("Unknown artist");
|
||||
if (!mfi->album)
|
||||
mfi->album = strdup("Unknown album");
|
||||
if (!mfi->genre)
|
||||
mfi->genre = strdup("Unknown genre");
|
||||
if (!mfi->title)
|
||||
{
|
||||
/* fname is left untouched by unicode_fixup_mfi() for
|
||||
* obvious reasons, so ensure it is proper UTF-8
|
||||
*/
|
||||
mfi->title = unicode_fixup_string(mfi->fname, "ascii");
|
||||
if (mfi->title == mfi->fname)
|
||||
mfi->title = strdup(mfi->fname);
|
||||
}
|
||||
|
||||
/* Ensure sort tags are filled, manipulated and normalized */
|
||||
sort_tag_create(&mfi->artist_sort, mfi->artist);
|
||||
sort_tag_create(&mfi->album_sort, mfi->album);
|
||||
sort_tag_create(&mfi->title_sort, mfi->title);
|
||||
|
||||
/* We need to set album_artist according to media type and config */
|
||||
if (mfi->compilation) /* Compilation */
|
||||
{
|
||||
lib = cfg_getsec(cfg, "library");
|
||||
ca = cfg_getstr(lib, "compilation_artist");
|
||||
if (ca && mfi->album_artist)
|
||||
{
|
||||
free(mfi->album_artist);
|
||||
mfi->album_artist = strdup(ca);
|
||||
}
|
||||
else if (ca && !mfi->album_artist)
|
||||
{
|
||||
mfi->album_artist = strdup(ca);
|
||||
}
|
||||
else if (!ca && !mfi->album_artist)
|
||||
{
|
||||
mfi->album_artist = strdup("");
|
||||
mfi->album_artist_sort = strdup("");
|
||||
}
|
||||
}
|
||||
else if (mfi->media_kind == MEDIA_KIND_PODCAST) /* Podcast */
|
||||
{
|
||||
if (mfi->album_artist)
|
||||
free(mfi->album_artist);
|
||||
mfi->album_artist = strdup("");
|
||||
mfi->album_artist_sort = strdup("");
|
||||
}
|
||||
else if (!mfi->album_artist) /* Regular media without album_artist */
|
||||
{
|
||||
mfi->album_artist = strdup(mfi->artist);
|
||||
}
|
||||
|
||||
if (!mfi->album_artist_sort && (strcmp(mfi->album_artist, mfi->artist) == 0))
|
||||
mfi->album_artist_sort = strdup(mfi->artist_sort);
|
||||
else
|
||||
sort_tag_create(&mfi->album_artist_sort, mfi->album_artist);
|
||||
|
||||
/* Composer is not one of our mandatory tags, so take extra care */
|
||||
if (mfi->composer_sort || mfi->composer)
|
||||
sort_tag_create(&mfi->composer_sort, mfi->composer);
|
||||
}
|
||||
|
||||
void
|
||||
library_process_media(const char *path, time_t mtime, off_t size, enum data_kind data_kind, enum media_kind force_media_kind, bool force_compilation, struct media_file_info *external_mfi, int dir_id)
|
||||
{
|
||||
struct media_file_info *mfi;
|
||||
const char *filename;
|
||||
time_t stamp;
|
||||
int id;
|
||||
char virtual_path[PATH_MAX];
|
||||
int ret;
|
||||
|
||||
filename = strrchr(path, '/');
|
||||
if ((!filename) || (strlen(filename) == 1))
|
||||
filename = path;
|
||||
else
|
||||
filename++;
|
||||
|
||||
db_file_stamp_bypath(path, &stamp, &id);
|
||||
|
||||
if (stamp && (stamp >= mtime))
|
||||
{
|
||||
db_file_ping(id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!external_mfi)
|
||||
{
|
||||
mfi = (struct media_file_info*)malloc(sizeof(struct media_file_info));
|
||||
if (!mfi)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Out of memory for mfi\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(mfi, 0, sizeof(struct media_file_info));
|
||||
}
|
||||
else
|
||||
mfi = external_mfi;
|
||||
|
||||
if (stamp)
|
||||
mfi->id = id;
|
||||
|
||||
mfi->fname = strdup(filename);
|
||||
if (!mfi->fname)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Out of memory for fname\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
mfi->path = strdup(path);
|
||||
if (!mfi->path)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Out of memory for path\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
mfi->time_modified = mtime;
|
||||
mfi->file_size = size;
|
||||
|
||||
if (force_compilation)
|
||||
mfi->compilation = 1;
|
||||
if (force_media_kind)
|
||||
mfi->media_kind = force_media_kind;
|
||||
|
||||
if (data_kind == DATA_KIND_FILE)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_FILE;
|
||||
ret = scan_metadata_ffmpeg(path, mfi);
|
||||
}
|
||||
else if (data_kind == DATA_KIND_HTTP)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_HTTP;
|
||||
ret = scan_metadata_ffmpeg(path, mfi);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Playlist URL '%s' is unavailable for probe/metadata, assuming MP3 encoding\n", path);
|
||||
mfi->type = strdup("mp3");
|
||||
mfi->codectype = strdup("mpeg");
|
||||
mfi->description = strdup("MPEG audio file");
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
else if (data_kind == DATA_KIND_SPOTIFY)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_SPOTIFY;
|
||||
ret = mfi->artist && mfi->album && mfi->title;
|
||||
}
|
||||
else if (data_kind == DATA_KIND_PIPE)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_PIPE;
|
||||
mfi->type = strdup("wav");
|
||||
mfi->codectype = strdup("wav");
|
||||
mfi->description = strdup("PCM16 pipe");
|
||||
ret = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Unknown scan type for '%s', this error should not occur\n", path);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Could not extract metadata for '%s'\n", path);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!mfi->item_kind)
|
||||
mfi->item_kind = 2; /* music */
|
||||
if (!mfi->media_kind)
|
||||
mfi->media_kind = MEDIA_KIND_MUSIC; /* music */
|
||||
|
||||
unicode_fixup_mfi(mfi);
|
||||
|
||||
fixup_tags(mfi);
|
||||
|
||||
if (data_kind == DATA_KIND_HTTP)
|
||||
{
|
||||
snprintf(virtual_path, PATH_MAX, "/http:/%s", mfi->title);
|
||||
mfi->virtual_path = strdup(virtual_path);
|
||||
}
|
||||
else if (data_kind == DATA_KIND_SPOTIFY)
|
||||
{
|
||||
snprintf(virtual_path, PATH_MAX, "/spotify:/%s/%s/%s", mfi->album_artist, mfi->album, mfi->title);
|
||||
mfi->virtual_path = strdup(virtual_path);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(virtual_path, PATH_MAX, "/file:%s", mfi->path);
|
||||
mfi->virtual_path = strdup(virtual_path);
|
||||
}
|
||||
|
||||
mfi->directory_id = dir_id;
|
||||
|
||||
if (mfi->id == 0)
|
||||
db_file_add(mfi);
|
||||
else
|
||||
db_file_update(mfi);
|
||||
|
||||
out:
|
||||
if (!external_mfi)
|
||||
free_mfi(mfi, 0);
|
||||
}
|
||||
|
||||
int
|
||||
library_add_playlist_info(const char *path, const char *title, const char *virtual_path, enum pl_type type, int parent_pl_id, int dir_id)
|
||||
{
|
||||
struct playlist_info *pli;
|
||||
int plid;
|
||||
int ret;
|
||||
|
||||
pli = db_pl_fetch_bypath(path);
|
||||
if (pli)
|
||||
{
|
||||
DPRINTF(E_DBG, L_LIB, "Playlist found ('%s', link %s), updating\n", title, path);
|
||||
|
||||
plid = pli->id;
|
||||
|
||||
pli->type = type;
|
||||
free(pli->title);
|
||||
pli->title = strdup(title);
|
||||
if (pli->virtual_path)
|
||||
free(pli->virtual_path);
|
||||
pli->virtual_path = safe_strdup(virtual_path);
|
||||
pli->directory_id = dir_id;
|
||||
|
||||
ret = db_pl_update(pli);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Error updating playlist ('%s', link %s)\n", title, path);
|
||||
|
||||
free_pli(pli, 0);
|
||||
return -1;
|
||||
}
|
||||
|
||||
db_pl_clear_items(plid);
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_DBG, L_LIB, "Adding playlist ('%s', link %s)\n", title, path);
|
||||
|
||||
pli = (struct playlist_info *)malloc(sizeof(struct playlist_info));
|
||||
if (!pli)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Out of memory\n");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(pli, 0, sizeof(struct playlist_info));
|
||||
|
||||
pli->type = type;
|
||||
pli->title = strdup(title);
|
||||
pli->path = strdup(path);
|
||||
pli->virtual_path = safe_strdup(virtual_path);
|
||||
pli->parent_id = parent_pl_id;
|
||||
pli->directory_id = dir_id;
|
||||
|
||||
ret = db_pl_add(pli, &plid);
|
||||
if ((ret < 0) || (plid < 1))
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Error adding playlist ('%s', link %s, ret %d, plid %d)\n", title, path, ret, plid);
|
||||
|
||||
free_pli(pli, 0);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
free_pli(pli, 0);
|
||||
return plid;
|
||||
}
|
||||
|
||||
static void
|
||||
purge_cruft(time_t start)
|
||||
{
|
||||
DPRINTF(E_DBG, L_LIB, "Purging old library content\n");
|
||||
db_purge_cruft(start);
|
||||
db_groups_cleanup();
|
||||
db_queue_cleanup();
|
||||
|
||||
DPRINTF(E_DBG, L_LIB, "Purging old artwork content\n");
|
||||
cache_artwork_purge_cruft(start);
|
||||
}
|
||||
|
||||
static enum command_state
|
||||
rescan(void *arg, int *ret)
|
||||
{
|
||||
time_t starttime;
|
||||
time_t endtime;
|
||||
int i;
|
||||
|
||||
DPRINTF(E_LOG, L_LIB, "Library rescan triggered\n");
|
||||
|
||||
starttime = time(NULL);
|
||||
|
||||
for (i = 0; sources[i]; i++)
|
||||
{
|
||||
if (!sources[i]->disabled && sources[i]->rescan)
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Rescan library source '%s'\n", sources[i]->name);
|
||||
sources[i]->rescan();
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Library source '%s' is disabled\n", sources[i]->name);
|
||||
}
|
||||
}
|
||||
|
||||
purge_cruft(starttime);
|
||||
|
||||
endtime = time(NULL);
|
||||
DPRINTF(E_LOG, L_LIB, "Library rescan completed in %.f sec\n", difftime(endtime, starttime));
|
||||
|
||||
scanning = false;
|
||||
*ret = 0;
|
||||
return COMMAND_END;
|
||||
}
|
||||
|
||||
static enum command_state
|
||||
fullrescan(void *arg, int *ret)
|
||||
{
|
||||
time_t starttime;
|
||||
time_t endtime;
|
||||
int i;
|
||||
|
||||
DPRINTF(E_LOG, L_LIB, "Library full-rescan triggered\n");
|
||||
|
||||
starttime = time(NULL);
|
||||
|
||||
player_playback_stop();
|
||||
db_queue_clear();
|
||||
db_purge_all(); // Clears files, playlists, playlistitems, inotify and groups
|
||||
|
||||
for (i = 0; sources[i]; i++)
|
||||
{
|
||||
if (!sources[i]->disabled && sources[i]->fullrescan)
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Full-rescan library source '%s'\n", sources[i]->name);
|
||||
sources[i]->fullrescan();
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Library source '%s' is disabled\n", sources[i]->name);
|
||||
}
|
||||
}
|
||||
|
||||
endtime = time(NULL);
|
||||
DPRINTF(E_LOG, L_LIB, "Library full-rescan completed in %.f sec\n", difftime(endtime, starttime));
|
||||
|
||||
scanning = false;
|
||||
*ret = 0;
|
||||
return COMMAND_END;
|
||||
}
|
||||
|
||||
void
|
||||
library_rescan()
|
||||
{
|
||||
if (scanning)
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Scan already running, ignoring request to trigger a new init scan\n");
|
||||
return;
|
||||
}
|
||||
|
||||
scanning = true; // TODO Guard "scanning" with a mutex
|
||||
commands_exec_async(cmdbase, rescan, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
library_fullrescan()
|
||||
{
|
||||
if (scanning)
|
||||
{
|
||||
DPRINTF(E_INFO, L_LIB, "Scan already running, ignoring request to trigger a new full rescan\n");
|
||||
return;
|
||||
}
|
||||
|
||||
scanning = true; // TODO Guard "scanning" with a mutex
|
||||
commands_exec_async(cmdbase, fullrescan, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
initscan()
|
||||
{
|
||||
time_t starttime;
|
||||
time_t endtime;
|
||||
bool clear_queue_disabled;
|
||||
int i;
|
||||
|
||||
scanning = true;
|
||||
starttime = time(NULL);
|
||||
|
||||
// Only clear the queue if enabled (default) in config
|
||||
clear_queue_disabled = cfg_getbool(cfg_getsec(cfg, "mpd"), "clear_queue_on_stop_disable");
|
||||
if (!clear_queue_disabled)
|
||||
{
|
||||
db_queue_clear();
|
||||
}
|
||||
|
||||
for (i = 0; sources[i]; i++)
|
||||
{
|
||||
if (!sources[i]->disabled && sources[i]->initscan)
|
||||
sources[i]->initscan();
|
||||
}
|
||||
|
||||
if (! (cfg_getbool(cfg_getsec(cfg, "library"), "filescan_disable")))
|
||||
{
|
||||
purge_cruft(starttime);
|
||||
|
||||
DPRINTF(E_DBG, L_LIB, "Running post library scan jobs\n");
|
||||
db_hook_post_scan();
|
||||
}
|
||||
|
||||
endtime = time(NULL);
|
||||
DPRINTF(E_LOG, L_LIB, "Library init scan completed in %.f sec\n", difftime(endtime, starttime));
|
||||
|
||||
scanning = false;
|
||||
}
|
||||
|
||||
/*
|
||||
* @return true if scan is running, otherwise false
|
||||
*/
|
||||
bool
|
||||
library_is_scanning()
|
||||
{
|
||||
return scanning;
|
||||
}
|
||||
|
||||
/*
|
||||
* @param is_scanning true if scan is running, otherwise false
|
||||
*/
|
||||
void
|
||||
library_set_scanning(bool is_scanning)
|
||||
{
|
||||
scanning = is_scanning;
|
||||
}
|
||||
|
||||
/*
|
||||
* @return true if a running scan should be aborted due to imminent shutdown, otherwise false
|
||||
*/
|
||||
bool
|
||||
library_is_exiting()
|
||||
{
|
||||
return scan_exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute the function 'func' with the given argument 'arg' in the library thread.
|
||||
*
|
||||
* The pointer passed as argument is freed in the library thread after func returned.
|
||||
*
|
||||
* @param func The function to be executed
|
||||
* @param arg Argument passed to func
|
||||
* @return 0 if triggering the function execution succeeded, -1 on failure.
|
||||
*/
|
||||
int
|
||||
library_exec_async(command_function func, void *arg)
|
||||
{
|
||||
return commands_exec_async(cmdbase, func, arg);
|
||||
}
|
||||
|
||||
static void *
|
||||
library(void *arg)
|
||||
{
|
||||
int ret;
|
||||
|
||||
#ifdef __linux__
|
||||
struct sched_param param;
|
||||
|
||||
/* Lower the priority of the thread so forked-daapd may still respond
|
||||
* during library scan on low power devices. Param must be 0 for the SCHED_BATCH
|
||||
* policy.
|
||||
*/
|
||||
memset(¶m, 0, sizeof(struct sched_param));
|
||||
ret = pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Warning: Could not set thread priority to SCHED_BATCH\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = db_perthread_init();
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_LIB, "Error: DB init failed\n");
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
initscan();
|
||||
|
||||
event_base_dispatch(evbase_lib);
|
||||
|
||||
if (!scan_exit)
|
||||
DPRINTF(E_FATAL, L_LIB, "Scan event loop terminated ahead of time!\n");
|
||||
|
||||
db_perthread_deinit();
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
/* Thread: main */
|
||||
int
|
||||
library_init(void)
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
scan_exit = false;
|
||||
scanning = false;
|
||||
|
||||
evbase_lib = event_base_new();
|
||||
if (!evbase_lib)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_LIB, "Could not create an event base\n");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; sources[i]; i++)
|
||||
{
|
||||
if (!sources[i]->init)
|
||||
continue;
|
||||
|
||||
ret = sources[i]->init();
|
||||
if (ret < 0)
|
||||
sources[i]->disabled = 1;
|
||||
}
|
||||
|
||||
cmdbase = commands_base_new(evbase_lib, NULL);
|
||||
|
||||
ret = pthread_create(&tid_library, NULL, library, NULL);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_LIB, "Could not spawn library thread: %s\n", strerror(errno));
|
||||
|
||||
goto thread_fail;
|
||||
}
|
||||
|
||||
#if defined(HAVE_PTHREAD_SETNAME_NP)
|
||||
pthread_setname_np(tid_library, "library");
|
||||
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
|
||||
pthread_set_name_np(tid_library, "library");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
thread_fail:
|
||||
event_base_free(evbase_lib);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Thread: main */
|
||||
void
|
||||
library_deinit()
|
||||
{
|
||||
int i;
|
||||
int ret;
|
||||
|
||||
scan_exit = true;
|
||||
commands_base_destroy(cmdbase);
|
||||
|
||||
ret = pthread_join(tid_library, NULL);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_LIB, "Could not join library thread: %s\n", strerror(errno));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; sources[i]; i++)
|
||||
{
|
||||
if (sources[i]->deinit && !sources[i]->disabled)
|
||||
sources[i]->deinit();
|
||||
}
|
||||
|
||||
event_base_free(evbase_lib);
|
||||
}
|
99
src/library.h
Normal file
99
src/library.h
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Christian Meffert <christian.meffert@googlemail.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef SRC_LIBRARY_H_
|
||||
#define SRC_LIBRARY_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "commands.h"
|
||||
#include "db.h"
|
||||
|
||||
/*
|
||||
* Definition of a library source
|
||||
*
|
||||
* A library source is responsible for scanning items into the library db.
|
||||
*/
|
||||
struct library_source
|
||||
{
|
||||
char *name;
|
||||
int disabled;
|
||||
|
||||
/*
|
||||
* Initialize library source (called from the main thread)
|
||||
*/
|
||||
int (*init)(void);
|
||||
|
||||
/*
|
||||
* Shutdown library source (called from the main thread after
|
||||
* terminating the library thread)
|
||||
*/
|
||||
void (*deinit)(void);
|
||||
|
||||
/*
|
||||
* Run initial scan after startup (called from the library thread)
|
||||
*/
|
||||
int (*initscan)(void);
|
||||
|
||||
/*
|
||||
* Run rescan (called from the library thread)
|
||||
*/
|
||||
int (*rescan)(void);
|
||||
|
||||
/*
|
||||
* Run a full rescan (purge library entries and rescan) (called from the library thread)
|
||||
*/
|
||||
int (*fullrescan)(void);
|
||||
|
||||
};
|
||||
|
||||
|
||||
void
|
||||
library_process_media(const char *path, time_t mtime, off_t size, enum data_kind data_kind, enum media_kind force_media_kind, bool force_compilation, struct media_file_info *external_mfi, int dir_id);
|
||||
|
||||
int
|
||||
library_add_playlist_info(const char *path, const char *title, const char *virtual_path, enum pl_type type, int parent_pl_id, int dir_id);
|
||||
|
||||
void
|
||||
library_rescan();
|
||||
|
||||
void
|
||||
library_fullrescan();
|
||||
|
||||
bool
|
||||
library_is_scanning();
|
||||
|
||||
void
|
||||
library_set_scanning(bool is_scanning);
|
||||
|
||||
bool
|
||||
library_is_exiting();
|
||||
|
||||
int
|
||||
library_exec_async(command_function func, void *arg);
|
||||
|
||||
int
|
||||
library_init();
|
||||
|
||||
void
|
||||
library_deinit();
|
||||
|
||||
|
||||
#endif /* SRC_LIBRARY_H_ */
|
@ -54,7 +54,7 @@
|
||||
|
||||
#include "logger.h"
|
||||
#include "db.h"
|
||||
#include "filescanner.h"
|
||||
#include "library/filescanner.h"
|
||||
#include "conffile.h"
|
||||
#include "misc.h"
|
||||
#include "remote_pairing.h"
|
||||
@ -62,6 +62,7 @@
|
||||
#include "cache.h"
|
||||
#include "artwork.h"
|
||||
#include "commands.h"
|
||||
#include "library.h"
|
||||
|
||||
#ifdef LASTFM
|
||||
# include "lastfm.h"
|
||||
@ -104,14 +105,14 @@ struct stacked_dir {
|
||||
struct stacked_dir *next;
|
||||
};
|
||||
|
||||
static int scan_exit;
|
||||
static int inofd;
|
||||
static struct event_base *evbase_scan;
|
||||
static struct event *inoev;
|
||||
static pthread_t tid_scan;
|
||||
static struct deferred_pl *playlists;
|
||||
static struct stacked_dir *dirstack;
|
||||
static struct commands_base *cmdbase;
|
||||
|
||||
/* From library.c */
|
||||
extern struct event_base *evbase_lib;
|
||||
|
||||
|
||||
#ifndef __linux__
|
||||
struct deferred_file
|
||||
@ -131,9 +132,6 @@ static struct event *deferred_inoev;
|
||||
/* Count of files scanned during a bulk scan */
|
||||
static int counter;
|
||||
|
||||
/* Flag for scan in progress */
|
||||
static int scanning;
|
||||
|
||||
/* When copying into the lib (eg. if a file is moved to the lib by copying into
|
||||
* a Samba network share) inotify might give us IN_CREATE -> n x IN_ATTRIB ->
|
||||
* IN_CLOSE_WRITE, but we don't want to do any scanning before the
|
||||
@ -151,10 +149,12 @@ static int
|
||||
inofd_event_set(void);
|
||||
static void
|
||||
inofd_event_unset(void);
|
||||
static enum command_state
|
||||
filescanner_initscan(void *arg, int *retval);
|
||||
static enum command_state
|
||||
filescanner_fullrescan(void *arg, int *retval);
|
||||
static int
|
||||
filescanner_initscan();
|
||||
static int
|
||||
filescanner_rescan();
|
||||
static int
|
||||
filescanner_fullrescan();
|
||||
|
||||
|
||||
static int
|
||||
@ -323,416 +323,6 @@ file_type_get(const char *path) {
|
||||
return FILE_REGULAR;
|
||||
}
|
||||
|
||||
static void
|
||||
sort_tag_create(char **sort_tag, char *src_tag)
|
||||
{
|
||||
const uint8_t *i_ptr;
|
||||
const uint8_t *n_ptr;
|
||||
const uint8_t *number;
|
||||
uint8_t out[1024];
|
||||
uint8_t *o_ptr;
|
||||
int append_number;
|
||||
ucs4_t puc;
|
||||
int numlen;
|
||||
size_t len;
|
||||
int charlen;
|
||||
|
||||
/* Note: include terminating NUL in string length for u8_normalize */
|
||||
|
||||
if (*sort_tag)
|
||||
{
|
||||
DPRINTF(E_DBG, L_SCAN, "Existing sort tag will be normalized: %s\n", *sort_tag);
|
||||
o_ptr = u8_normalize(UNINORM_NFD, (uint8_t *)*sort_tag, strlen(*sort_tag) + 1, NULL, &len);
|
||||
free(*sort_tag);
|
||||
*sort_tag = (char *)o_ptr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!src_tag || ((len = strlen(src_tag)) == 0))
|
||||
{
|
||||
*sort_tag = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set input pointer past article if present
|
||||
if ((strncasecmp(src_tag, "a ", 2) == 0) && (len > 2))
|
||||
i_ptr = (uint8_t *)(src_tag + 2);
|
||||
else if ((strncasecmp(src_tag, "an ", 3) == 0) && (len > 3))
|
||||
i_ptr = (uint8_t *)(src_tag + 3);
|
||||
else if ((strncasecmp(src_tag, "the ", 4) == 0) && (len > 4))
|
||||
i_ptr = (uint8_t *)(src_tag + 4);
|
||||
else
|
||||
i_ptr = (uint8_t *)src_tag;
|
||||
|
||||
// Poor man's natural sort. Makes sure we sort like this: a1, a2, a10, a11, a21, a111
|
||||
// We do this by padding zeroes to (short) numbers. As an alternative we could have
|
||||
// made a proper natural sort algorithm in sqlext.c, but we don't, since we don't
|
||||
// want any risk of hurting response times
|
||||
memset(&out, 0, sizeof(out));
|
||||
o_ptr = (uint8_t *)&out;
|
||||
number = NULL;
|
||||
append_number = 0;
|
||||
|
||||
do
|
||||
{
|
||||
n_ptr = u8_next(&puc, i_ptr);
|
||||
|
||||
if (uc_is_digit(puc))
|
||||
{
|
||||
if (!number) // We have encountered the beginning of a number
|
||||
number = i_ptr;
|
||||
append_number = (n_ptr == NULL); // If last char in string append number now
|
||||
}
|
||||
else
|
||||
{
|
||||
if (number)
|
||||
append_number = 1; // A number has ended so time to append it
|
||||
else
|
||||
{
|
||||
charlen = u8_strmblen(i_ptr);
|
||||
if (charlen >= 0)
|
||||
o_ptr = u8_stpncpy(o_ptr, i_ptr, charlen); // No numbers in sight, just append char
|
||||
}
|
||||
}
|
||||
|
||||
// Break if less than 100 bytes remain (prevent buffer overflow)
|
||||
if (sizeof(out) - u8_strlen(out) < 100)
|
||||
break;
|
||||
|
||||
// Break if number is very large (prevent buffer overflow)
|
||||
if (number && (i_ptr - number > 50))
|
||||
break;
|
||||
|
||||
if (append_number)
|
||||
{
|
||||
numlen = i_ptr - number;
|
||||
if (numlen < 5) // Max pad width
|
||||
{
|
||||
u8_strcpy(o_ptr, (uint8_t *)"00000");
|
||||
o_ptr += (5 - numlen);
|
||||
}
|
||||
o_ptr = u8_stpncpy(o_ptr, number, numlen + u8_strmblen(i_ptr));
|
||||
|
||||
number = NULL;
|
||||
append_number = 0;
|
||||
}
|
||||
|
||||
i_ptr = n_ptr;
|
||||
}
|
||||
while (n_ptr);
|
||||
|
||||
*sort_tag = (char *)u8_normalize(UNINORM_NFD, (uint8_t *)&out, u8_strlen(out) + 1, NULL, &len);
|
||||
}
|
||||
|
||||
static void
|
||||
fixup_tags(struct media_file_info *mfi)
|
||||
{
|
||||
cfg_t *lib;
|
||||
size_t len;
|
||||
char *tag;
|
||||
char *sep = " - ";
|
||||
char *ca;
|
||||
|
||||
if (mfi->genre && (strlen(mfi->genre) == 0))
|
||||
{
|
||||
free(mfi->genre);
|
||||
mfi->genre = NULL;
|
||||
}
|
||||
|
||||
if (mfi->artist && (strlen(mfi->artist) == 0))
|
||||
{
|
||||
free(mfi->artist);
|
||||
mfi->artist = NULL;
|
||||
}
|
||||
|
||||
if (mfi->title && (strlen(mfi->title) == 0))
|
||||
{
|
||||
free(mfi->title);
|
||||
mfi->title = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Default to mpeg4 video/audio for unknown file types
|
||||
* in an attempt to allow streaming of DRM-afflicted files
|
||||
*/
|
||||
if (mfi->codectype && strcmp(mfi->codectype, "unkn") == 0)
|
||||
{
|
||||
if (mfi->has_video)
|
||||
{
|
||||
strcpy(mfi->codectype, "mp4v");
|
||||
strcpy(mfi->type, "m4v");
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(mfi->codectype, "mp4a");
|
||||
strcpy(mfi->type, "m4a");
|
||||
}
|
||||
}
|
||||
|
||||
if (!mfi->artist)
|
||||
{
|
||||
if (mfi->orchestra && mfi->conductor)
|
||||
{
|
||||
len = strlen(mfi->orchestra) + strlen(sep) + strlen(mfi->conductor);
|
||||
tag = (char *)malloc(len + 1);
|
||||
if (tag)
|
||||
{
|
||||
sprintf(tag,"%s%s%s", mfi->orchestra, sep, mfi->conductor);
|
||||
mfi->artist = tag;
|
||||
}
|
||||
}
|
||||
else if (mfi->orchestra)
|
||||
{
|
||||
mfi->artist = strdup(mfi->orchestra);
|
||||
}
|
||||
else if (mfi->conductor)
|
||||
{
|
||||
mfi->artist = strdup(mfi->conductor);
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle TV shows, try to present prettier metadata */
|
||||
if (mfi->tv_series_name && strlen(mfi->tv_series_name) != 0)
|
||||
{
|
||||
mfi->media_kind = MEDIA_KIND_TVSHOW; /* tv show */
|
||||
|
||||
/* Default to artist = series_name */
|
||||
if (mfi->artist && strlen(mfi->artist) == 0)
|
||||
{
|
||||
free(mfi->artist);
|
||||
mfi->artist = NULL;
|
||||
}
|
||||
|
||||
if (!mfi->artist)
|
||||
mfi->artist = strdup(mfi->tv_series_name);
|
||||
|
||||
/* Default to album = "<series_name>, Season <season_num>" */
|
||||
if (mfi->album && strlen(mfi->album) == 0)
|
||||
{
|
||||
free(mfi->album);
|
||||
mfi->album = NULL;
|
||||
}
|
||||
|
||||
if (!mfi->album)
|
||||
{
|
||||
len = snprintf(NULL, 0, "%s, Season %d", mfi->tv_series_name, mfi->tv_season_num);
|
||||
|
||||
mfi->album = (char *)malloc(len + 1);
|
||||
if (mfi->album)
|
||||
sprintf(mfi->album, "%s, Season %d", mfi->tv_series_name, mfi->tv_season_num);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check the 4 top-tags are filled */
|
||||
if (!mfi->artist)
|
||||
mfi->artist = strdup("Unknown artist");
|
||||
if (!mfi->album)
|
||||
mfi->album = strdup("Unknown album");
|
||||
if (!mfi->genre)
|
||||
mfi->genre = strdup("Unknown genre");
|
||||
if (!mfi->title)
|
||||
{
|
||||
/* fname is left untouched by unicode_fixup_mfi() for
|
||||
* obvious reasons, so ensure it is proper UTF-8
|
||||
*/
|
||||
mfi->title = unicode_fixup_string(mfi->fname, "ascii");
|
||||
if (mfi->title == mfi->fname)
|
||||
mfi->title = strdup(mfi->fname);
|
||||
}
|
||||
|
||||
/* Ensure sort tags are filled, manipulated and normalized */
|
||||
sort_tag_create(&mfi->artist_sort, mfi->artist);
|
||||
sort_tag_create(&mfi->album_sort, mfi->album);
|
||||
sort_tag_create(&mfi->title_sort, mfi->title);
|
||||
|
||||
/* We need to set album_artist according to media type and config */
|
||||
if (mfi->compilation) /* Compilation */
|
||||
{
|
||||
lib = cfg_getsec(cfg, "library");
|
||||
ca = cfg_getstr(lib, "compilation_artist");
|
||||
if (ca && mfi->album_artist)
|
||||
{
|
||||
free(mfi->album_artist);
|
||||
mfi->album_artist = strdup(ca);
|
||||
}
|
||||
else if (ca && !mfi->album_artist)
|
||||
{
|
||||
mfi->album_artist = strdup(ca);
|
||||
}
|
||||
else if (!ca && !mfi->album_artist)
|
||||
{
|
||||
mfi->album_artist = strdup("");
|
||||
mfi->album_artist_sort = strdup("");
|
||||
}
|
||||
}
|
||||
else if (mfi->media_kind == MEDIA_KIND_PODCAST) /* Podcast */
|
||||
{
|
||||
if (mfi->album_artist)
|
||||
free(mfi->album_artist);
|
||||
mfi->album_artist = strdup("");
|
||||
mfi->album_artist_sort = strdup("");
|
||||
}
|
||||
else if (!mfi->album_artist) /* Regular media without album_artist */
|
||||
{
|
||||
mfi->album_artist = strdup(mfi->artist);
|
||||
}
|
||||
|
||||
if (!mfi->album_artist_sort && (strcmp(mfi->album_artist, mfi->artist) == 0))
|
||||
mfi->album_artist_sort = strdup(mfi->artist_sort);
|
||||
else
|
||||
sort_tag_create(&mfi->album_artist_sort, mfi->album_artist);
|
||||
|
||||
/* Composer is not one of our mandatory tags, so take extra care */
|
||||
if (mfi->composer_sort || mfi->composer)
|
||||
sort_tag_create(&mfi->composer_sort, mfi->composer);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
filescanner_process_media(char *path, time_t mtime, off_t size, int type, struct media_file_info *external_mfi, int dir_id)
|
||||
{
|
||||
struct media_file_info *mfi;
|
||||
char *filename;
|
||||
time_t stamp;
|
||||
int id;
|
||||
char virtual_path[PATH_MAX];
|
||||
int ret;
|
||||
|
||||
filename = strrchr(path, '/');
|
||||
if ((!filename) || (strlen(filename) == 1))
|
||||
filename = path;
|
||||
else
|
||||
filename++;
|
||||
|
||||
db_file_stamp_bypath(path, &stamp, &id);
|
||||
|
||||
if (stamp && (stamp >= mtime))
|
||||
{
|
||||
db_file_ping(id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!external_mfi)
|
||||
{
|
||||
mfi = (struct media_file_info*)malloc(sizeof(struct media_file_info));
|
||||
if (!mfi)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Out of memory for mfi\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(mfi, 0, sizeof(struct media_file_info));
|
||||
}
|
||||
else
|
||||
mfi = external_mfi;
|
||||
|
||||
if (stamp)
|
||||
mfi->id = id;
|
||||
|
||||
mfi->fname = strdup(filename);
|
||||
if (!mfi->fname)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Out of memory for fname\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
mfi->path = strdup(path);
|
||||
if (!mfi->path)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Out of memory for path\n");
|
||||
goto out;
|
||||
}
|
||||
|
||||
mfi->time_modified = mtime;
|
||||
mfi->file_size = size;
|
||||
|
||||
if (type & F_SCAN_TYPE_COMPILATION)
|
||||
mfi->compilation = 1;
|
||||
if (type & F_SCAN_TYPE_PODCAST)
|
||||
mfi->media_kind = MEDIA_KIND_PODCAST; /* podcast */
|
||||
if (type & F_SCAN_TYPE_AUDIOBOOK)
|
||||
mfi->media_kind = MEDIA_KIND_AUDIOBOOK; /* audiobook */
|
||||
|
||||
if (type & F_SCAN_TYPE_FILE)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_FILE;
|
||||
ret = scan_metadata_ffmpeg(path, mfi);
|
||||
}
|
||||
else if (type & F_SCAN_TYPE_URL)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_HTTP;
|
||||
ret = scan_metadata_ffmpeg(path, mfi);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Playlist URL '%s' is unavailable for probe/metadata, assuming MP3 encoding\n", path);
|
||||
mfi->type = strdup("mp3");
|
||||
mfi->codectype = strdup("mpeg");
|
||||
mfi->description = strdup("MPEG audio file");
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
else if (type & F_SCAN_TYPE_SPOTIFY)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_SPOTIFY;
|
||||
ret = mfi->artist && mfi->album && mfi->title;
|
||||
}
|
||||
else if (type & F_SCAN_TYPE_PIPE)
|
||||
{
|
||||
mfi->data_kind = DATA_KIND_PIPE;
|
||||
mfi->type = strdup("wav");
|
||||
mfi->codectype = strdup("wav");
|
||||
mfi->description = strdup("PCM16 pipe");
|
||||
ret = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Unknown scan type for '%s', this error should not occur\n", path);
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_INFO, L_SCAN, "Could not extract metadata for '%s'\n", path);
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!mfi->item_kind)
|
||||
mfi->item_kind = 2; /* music */
|
||||
if (!mfi->media_kind)
|
||||
mfi->media_kind = MEDIA_KIND_MUSIC; /* music */
|
||||
|
||||
unicode_fixup_mfi(mfi);
|
||||
|
||||
fixup_tags(mfi);
|
||||
|
||||
if (type & F_SCAN_TYPE_URL)
|
||||
{
|
||||
snprintf(virtual_path, PATH_MAX, "/http:/%s", mfi->title);
|
||||
mfi->virtual_path = strdup(virtual_path);
|
||||
}
|
||||
else if (type & F_SCAN_TYPE_SPOTIFY)
|
||||
{
|
||||
snprintf(virtual_path, PATH_MAX, "/spotify:/%s/%s/%s", mfi->album_artist, mfi->album, mfi->title);
|
||||
mfi->virtual_path = strdup(virtual_path);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(virtual_path, PATH_MAX, "/file:%s", mfi->path);
|
||||
mfi->virtual_path = strdup(virtual_path);
|
||||
}
|
||||
|
||||
mfi->directory_id = dir_id;
|
||||
|
||||
if (mfi->id == 0)
|
||||
db_file_add(mfi);
|
||||
else
|
||||
db_file_update(mfi);
|
||||
|
||||
out:
|
||||
if (!external_mfi)
|
||||
free_mfi(mfi, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
process_playlist(char *file, time_t mtime, int dir_id)
|
||||
{
|
||||
@ -795,7 +385,7 @@ process_deferred_playlists(void)
|
||||
free(pl->path);
|
||||
free(pl);
|
||||
|
||||
if (scan_exit)
|
||||
if (library_is_exiting())
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -804,15 +394,30 @@ process_deferred_playlists(void)
|
||||
static void
|
||||
process_file(char *file, time_t mtime, off_t size, int type, int flags, int dir_id)
|
||||
{
|
||||
int is_bulkscan;
|
||||
int ret;
|
||||
bool is_bulkscan;
|
||||
bool is_type_compilation;
|
||||
enum media_kind media_kind;
|
||||
enum data_kind data_kind;
|
||||
|
||||
is_bulkscan = (flags & F_SCAN_BULK);
|
||||
|
||||
switch (file_type_get(file))
|
||||
{
|
||||
case FILE_REGULAR:
|
||||
filescanner_process_media(file, mtime, size, type, NULL, dir_id);
|
||||
is_type_compilation = type & F_SCAN_TYPE_COMPILATION;
|
||||
if (type & F_SCAN_TYPE_AUDIOBOOK)
|
||||
media_kind = MEDIA_KIND_AUDIOBOOK;
|
||||
else if (type & F_SCAN_TYPE_PODCAST)
|
||||
media_kind = MEDIA_KIND_PODCAST;
|
||||
else
|
||||
media_kind = 0;
|
||||
|
||||
if (F_SCAN_TYPE_PIPE & type)
|
||||
data_kind = DATA_KIND_PIPE;
|
||||
else
|
||||
data_kind = DATA_KIND_FILE;
|
||||
|
||||
library_process_media(file, mtime, size, data_kind, media_kind, is_type_compilation, NULL, dir_id);
|
||||
|
||||
cache_artwork_ping(file, mtime, !is_bulkscan);
|
||||
// TODO [artworkcache] If entry in artwork cache exists for no artwork available, delete the entry if media file has embedded artwork
|
||||
@ -884,7 +489,7 @@ process_file(char *file, time_t mtime, off_t size, int type, int flags, int dir_
|
||||
|
||||
DPRINTF(E_LOG, L_SCAN, "Startup rescan triggered, found init-rescan file: %s\n", file);
|
||||
|
||||
filescanner_initscan(NULL, &ret);
|
||||
library_rescan();
|
||||
break;
|
||||
|
||||
case FILE_CTRL_FULLSCAN:
|
||||
@ -893,7 +498,7 @@ process_file(char *file, time_t mtime, off_t size, int type, int flags, int dir_
|
||||
|
||||
DPRINTF(E_LOG, L_SCAN, "Full rescan triggered, found full-rescan file: %s\n", file);
|
||||
|
||||
filescanner_fullrescan(NULL, &ret);
|
||||
library_fullrescan();
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -982,7 +587,7 @@ process_directory(char *path, int parent_id, int flags)
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (scan_exit)
|
||||
if (library_is_exiting())
|
||||
break;
|
||||
|
||||
errno = 0;
|
||||
@ -1139,7 +744,7 @@ process_directories(char *root, int parent_id, int flags)
|
||||
|
||||
process_directory(root, parent_id, flags);
|
||||
|
||||
if (scan_exit)
|
||||
if (library_is_exiting())
|
||||
return;
|
||||
|
||||
while ((dir = pop_dir(&dirstack)))
|
||||
@ -1149,7 +754,7 @@ process_directories(char *root, int parent_id, int flags)
|
||||
free(dir->path);
|
||||
free(dir);
|
||||
|
||||
if (scan_exit)
|
||||
if (library_is_exiting())
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -1167,9 +772,8 @@ bulk_scan(int flags)
|
||||
time_t end;
|
||||
int parent_id;
|
||||
int i;
|
||||
|
||||
// Set global flag to avoid queued scan requests
|
||||
scanning = 1;
|
||||
char virtual_path[PATH_MAX];
|
||||
int ret;
|
||||
|
||||
start = time(NULL);
|
||||
|
||||
@ -1197,7 +801,11 @@ bulk_scan(int flags)
|
||||
|
||||
db_file_ping_bymatch(path, 1);
|
||||
db_pl_ping_bymatch(path, 1);
|
||||
db_directory_ping_bymatch(path);
|
||||
ret = snprintf(virtual_path, sizeof(virtual_path), "/file:%s", path);
|
||||
if ((ret < 0) || (ret >= sizeof(virtual_path)))
|
||||
DPRINTF(E_LOG, L_SCAN, "Virtual path exceeds PATH_MAX (/file:%s)\n", path);
|
||||
else
|
||||
db_directory_ping_bymatch(virtual_path);
|
||||
|
||||
continue;
|
||||
}
|
||||
@ -1210,14 +818,14 @@ bulk_scan(int flags)
|
||||
|
||||
free(deref);
|
||||
|
||||
if (scan_exit)
|
||||
if (library_is_exiting())
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(flags & F_SCAN_FAST) && playlists)
|
||||
process_deferred_playlists();
|
||||
|
||||
if (scan_exit)
|
||||
if (library_is_exiting())
|
||||
return;
|
||||
|
||||
if (dirstack)
|
||||
@ -1231,103 +839,8 @@ bulk_scan(int flags)
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Protect spotify from the imminent purge if rescanning */
|
||||
db_transaction_begin();
|
||||
db_file_ping_bymatch("spotify:", 0);
|
||||
db_pl_ping_bymatch("spotify:", 0);
|
||||
db_transaction_end();
|
||||
|
||||
DPRINTF(E_DBG, L_SCAN, "Purging old database content\n");
|
||||
db_purge_cruft(start);
|
||||
db_groups_cleanup();
|
||||
db_queue_cleanup();
|
||||
|
||||
cache_artwork_purge_cruft(start);
|
||||
|
||||
DPRINTF(E_LOG, L_SCAN, "Bulk library scan completed in %.f sec\n", difftime(end, start));
|
||||
|
||||
DPRINTF(E_DBG, L_SCAN, "Running post library scan jobs\n");
|
||||
db_hook_post_scan();
|
||||
}
|
||||
|
||||
// Set scan in progress flag to FALSE
|
||||
scanning = 0;
|
||||
}
|
||||
|
||||
|
||||
/* Thread: scan */
|
||||
static void *
|
||||
filescanner(void *arg)
|
||||
{
|
||||
int clear_queue_on_stop_disabled;
|
||||
int ret;
|
||||
#ifdef __linux__
|
||||
struct sched_param param;
|
||||
|
||||
/* Lower the priority of the thread so forked-daapd may still respond
|
||||
* during file scan on low power devices. Param must be 0 for the SCHED_BATCH
|
||||
* policy.
|
||||
*/
|
||||
memset(¶m, 0, sizeof(struct sched_param));
|
||||
ret = pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Warning: Could not set thread priority to SCHED_BATCH\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = db_perthread_init();
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Error: DB init failed\n");
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
ret = db_watch_clear();
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Error: could not clear old watches from DB\n");
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
// Only clear the queue if enabled (default) in config
|
||||
clear_queue_on_stop_disabled = cfg_getbool(cfg_getsec(cfg, "mpd"), "clear_queue_on_stop_disable");
|
||||
if (!clear_queue_on_stop_disabled)
|
||||
{
|
||||
ret = db_queue_clear();
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Error: could not clear queue from DB\n");
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
if (cfg_getbool(cfg_getsec(cfg, "library"), "filescan_disable"))
|
||||
bulk_scan(F_SCAN_BULK | F_SCAN_FAST);
|
||||
else
|
||||
bulk_scan(F_SCAN_BULK);
|
||||
|
||||
if (!scan_exit)
|
||||
{
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
spotify_login(NULL);
|
||||
#endif
|
||||
|
||||
/* Enable inotify */
|
||||
event_add(inoev, NULL);
|
||||
|
||||
event_base_dispatch(evbase_scan);
|
||||
}
|
||||
|
||||
if (!scan_exit)
|
||||
DPRINTF(E_FATAL, L_SCAN, "Scan event loop terminated ahead of time!\n");
|
||||
|
||||
db_perthread_deinit();
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
static int
|
||||
@ -1904,10 +1417,10 @@ inofd_event_set(void)
|
||||
return -1;
|
||||
}
|
||||
|
||||
inoev = event_new(evbase_scan, inofd, EV_READ, inotify_cb, NULL);
|
||||
inoev = event_new(evbase_lib, inofd, EV_READ, inotify_cb, NULL);
|
||||
|
||||
#ifndef __linux__
|
||||
deferred_inoev = evtimer_new(evbase_scan, inotify_deferred_cb, NULL);
|
||||
deferred_inoev = evtimer_new(evbase_lib, inotify_deferred_cb, NULL);
|
||||
if (!deferred_inoev)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Could not create deferred inotify event\n");
|
||||
@ -1931,141 +1444,82 @@ inofd_event_unset(void)
|
||||
}
|
||||
|
||||
/* Thread: scan */
|
||||
static int
|
||||
filescanner_initscan()
|
||||
{
|
||||
int ret;
|
||||
|
||||
static enum command_state
|
||||
filescanner_initscan(void *arg, int *retval)
|
||||
ret = db_watch_clear();
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Error: could not clear old watches from DB\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (cfg_getbool(cfg_getsec(cfg, "library"), "filescan_disable"))
|
||||
bulk_scan(F_SCAN_BULK | F_SCAN_FAST);
|
||||
else
|
||||
bulk_scan(F_SCAN_BULK);
|
||||
|
||||
if (!library_is_exiting())
|
||||
{
|
||||
/* Enable inotify */
|
||||
event_add(inoev, NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
filescanner_rescan()
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Startup rescan triggered\n");
|
||||
|
||||
inofd_event_unset(); // Clears all inotify watches
|
||||
db_watch_clear();
|
||||
|
||||
inofd_event_set();
|
||||
bulk_scan(F_SCAN_BULK | F_SCAN_RESCAN);
|
||||
|
||||
*retval = 0;
|
||||
return COMMAND_END;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static enum command_state
|
||||
filescanner_fullrescan(void *arg, int *retval)
|
||||
static int
|
||||
filescanner_fullrescan()
|
||||
{
|
||||
DPRINTF(E_LOG, L_SCAN, "Full rescan triggered\n");
|
||||
|
||||
player_playback_stop();
|
||||
db_queue_clear();
|
||||
inofd_event_unset(); // Clears all inotify watches
|
||||
db_purge_all(); // Clears files, playlists, playlistitems, inotify and groups
|
||||
|
||||
inofd_event_set();
|
||||
bulk_scan(F_SCAN_BULK);
|
||||
|
||||
*retval = 0;
|
||||
return COMMAND_END;
|
||||
}
|
||||
|
||||
void
|
||||
filescanner_trigger_initscan(void)
|
||||
{
|
||||
if (scanning)
|
||||
{
|
||||
DPRINTF(E_INFO, L_SCAN, "Scan already running, ignoring request to trigger a new init scan\n");
|
||||
return;
|
||||
}
|
||||
|
||||
commands_exec_async(cmdbase, filescanner_initscan, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
filescanner_trigger_fullrescan(void)
|
||||
{
|
||||
if (scanning)
|
||||
{
|
||||
DPRINTF(E_INFO, L_SCAN, "Scan already running, ignoring request to trigger a new init scan\n");
|
||||
return;
|
||||
}
|
||||
|
||||
commands_exec_async(cmdbase, filescanner_fullrescan, NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
* Query the status of the filescanner
|
||||
* @return 1 if scan is running, otherwise 0
|
||||
*/
|
||||
int
|
||||
filescanner_scanning(void)
|
||||
{
|
||||
return scanning;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Thread: main */
|
||||
int
|
||||
static int
|
||||
filescanner_init(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
scan_exit = 0;
|
||||
scanning = 0;
|
||||
|
||||
evbase_scan = event_base_new();
|
||||
if (!evbase_scan)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_SCAN, "Could not create an event base\n");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = inofd_event_set();
|
||||
if (ret < 0)
|
||||
{
|
||||
goto ino_fail;
|
||||
}
|
||||
|
||||
cmdbase = commands_base_new(evbase_scan, NULL);
|
||||
|
||||
ret = pthread_create(&tid_scan, NULL, filescanner, NULL);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_SCAN, "Could not spawn filescanner thread: %s\n", strerror(errno));
|
||||
|
||||
goto thread_fail;
|
||||
}
|
||||
|
||||
#if defined(HAVE_PTHREAD_SETNAME_NP)
|
||||
pthread_setname_np(tid_scan, "filescanner");
|
||||
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
|
||||
pthread_set_name_np(tid_scan, "filescanner");
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
thread_fail:
|
||||
commands_base_free(cmdbase);
|
||||
close(inofd);
|
||||
ino_fail:
|
||||
event_base_free(evbase_scan);
|
||||
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Thread: main */
|
||||
void
|
||||
static void
|
||||
filescanner_deinit(void)
|
||||
{
|
||||
int ret;
|
||||
|
||||
scan_exit = 1;
|
||||
commands_base_destroy(cmdbase);
|
||||
|
||||
ret = pthread_join(tid_scan, NULL);
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_SCAN, "Could not join filescanner thread: %s\n", strerror(errno));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
inofd_event_unset();
|
||||
|
||||
event_base_free(evbase_scan);
|
||||
}
|
||||
|
||||
|
||||
struct library_source filescanner =
|
||||
{
|
||||
.name = "filescanner",
|
||||
.disabled = 0,
|
||||
.init = filescanner_init,
|
||||
.deinit = filescanner_deinit,
|
||||
.initscan = filescanner_initscan,
|
||||
.rescan = filescanner_rescan,
|
||||
.fullrescan = filescanner_fullrescan,
|
||||
};
|
@ -12,18 +12,10 @@
|
||||
#define F_SCAN_TYPE_SPOTIFY (1 << 5)
|
||||
#define F_SCAN_TYPE_PIPE (1 << 6)
|
||||
|
||||
int
|
||||
filescanner_init(void);
|
||||
|
||||
void
|
||||
filescanner_deinit(void);
|
||||
|
||||
void
|
||||
filescanner_process_media(char *path, time_t mtime, off_t size, int type, struct media_file_info *external_mfi, int dir_id);
|
||||
|
||||
/* Actual scanners */
|
||||
int
|
||||
scan_metadata_ffmpeg(char *file, struct media_file_info *mfi);
|
||||
scan_metadata_ffmpeg(const char *file, struct media_file_info *mfi);
|
||||
|
||||
int
|
||||
scan_metadata_icy(char *url, struct media_file_info *mfi);
|
||||
@ -39,13 +31,4 @@ void
|
||||
scan_itunes_itml(char *file);
|
||||
#endif
|
||||
|
||||
void
|
||||
filescanner_trigger_initscan(void);
|
||||
|
||||
void
|
||||
filescanner_trigger_fullrescan(void);
|
||||
|
||||
int
|
||||
filescanner_scanning(void);
|
||||
|
||||
#endif /* !__FILESCANNER_H__ */
|
@ -31,8 +31,8 @@
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/opt.h>
|
||||
|
||||
#include "db.h"
|
||||
#include "logger.h"
|
||||
#include "filescanner.h"
|
||||
#include "misc.h"
|
||||
#include "http.h"
|
||||
|
||||
@ -351,7 +351,7 @@ extract_metadata(struct media_file_info *mfi, AVFormatContext *ctx, AVStream *au
|
||||
}
|
||||
|
||||
int
|
||||
scan_metadata_ffmpeg(char *file, struct media_file_info *mfi)
|
||||
scan_metadata_ffmpeg(const char *file, struct media_file_info *mfi)
|
||||
{
|
||||
AVFormatContext *ctx;
|
||||
AVDictionary *options;
|
@ -41,7 +41,6 @@
|
||||
|
||||
#include "logger.h"
|
||||
#include "db.h"
|
||||
#include "filescanner.h"
|
||||
#include "conffile.h"
|
||||
#include "misc.h"
|
||||
|
@ -36,8 +36,9 @@
|
||||
|
||||
#include "logger.h"
|
||||
#include "db.h"
|
||||
#include "filescanner.h"
|
||||
#include "library/filescanner.h"
|
||||
#include "misc.h"
|
||||
#include "library.h"
|
||||
|
||||
/* Formats we can read so far */
|
||||
#define PLAYLIST_PLS 1
|
||||
@ -238,7 +239,7 @@ scan_playlist(char *file, time_t mtime, int dir_id)
|
||||
if (extinf)
|
||||
DPRINTF(E_INFO, L_SCAN, "Playlist has EXTINF metadata, artist is '%s', title is '%s'\n", mfi.artist, mfi.title);
|
||||
|
||||
filescanner_process_media(filename, mtime, 0, F_SCAN_TYPE_URL, &mfi, DIR_HTTP);
|
||||
library_process_media(filename, mtime, 0, DATA_KIND_HTTP, 0, false, &mfi, DIR_HTTP);
|
||||
}
|
||||
/* Regular file, should already be in library */
|
||||
else
|
@ -33,7 +33,6 @@
|
||||
|
||||
#include "logger.h"
|
||||
#include "db.h"
|
||||
#include "filescanner.h"
|
||||
#include "misc.h"
|
||||
|
||||
#include "SMARTPLLexer.h"
|
@ -44,7 +44,7 @@ static int threshold;
|
||||
static int console = 1;
|
||||
static char *logfilename;
|
||||
static FILE *logfile;
|
||||
static char *labels[] = { "config", "daap", "db", "httpd", "http", "main", "mdns", "misc", "rsp", "scan", "xcode", "event", "remote", "dacp", "ffmpeg", "artwork", "player", "raop", "laudio", "dmap", "dbperf", "spotify", "lastfm", "cache", "mpd", "stream", "cast", "fifo" };
|
||||
static char *labels[] = { "config", "daap", "db", "httpd", "http", "main", "mdns", "misc", "rsp", "scan", "xcode", "event", "remote", "dacp", "ffmpeg", "artwork", "player", "raop", "laudio", "dmap", "dbperf", "spotify", "lastfm", "cache", "mpd", "stream", "cast", "fifo", "lib" };
|
||||
static char *severities[] = { "FATAL", "LOG", "WARN", "INFO", "DEBUG", "SPAM" };
|
||||
|
||||
/* We need our own check to avoid nested locking or recursive calls */
|
||||
|
@ -34,8 +34,9 @@
|
||||
#define L_STREAMING 25
|
||||
#define L_CAST 26
|
||||
#define L_FIFO 27
|
||||
#define L_LIB 28
|
||||
|
||||
#define N_LOGDOMAINS 28
|
||||
#define N_LOGDOMAINS 29
|
||||
|
||||
/* Severities */
|
||||
#define E_FATAL 0
|
||||
|
32
src/main.c
32
src/main.c
@ -58,20 +58,17 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL;
|
||||
#include "logger.h"
|
||||
#include "misc.h"
|
||||
#include "cache.h"
|
||||
#include "filescanner.h"
|
||||
#include "httpd.h"
|
||||
#include "mpd.h"
|
||||
#include "mdns.h"
|
||||
#include "remote_pairing.h"
|
||||
#include "player.h"
|
||||
#include "worker.h"
|
||||
#include "library.h"
|
||||
|
||||
#ifdef HAVE_LIBCURL
|
||||
# include <curl/curl.h>
|
||||
#endif
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
# include "spotify.h"
|
||||
#endif
|
||||
|
||||
#define PIDFILE STATEDIR "/run/" PACKAGE ".pid"
|
||||
|
||||
@ -738,25 +735,16 @@ main(int argc, char **argv)
|
||||
goto cache_fail;
|
||||
}
|
||||
|
||||
/* Spawn file scanner thread */
|
||||
ret = filescanner_init();
|
||||
/* Spawn library scan thread */
|
||||
ret = library_init();
|
||||
if (ret != 0)
|
||||
{
|
||||
DPRINTF(E_FATAL, L_MAIN, "File scanner thread failed to start\n");
|
||||
DPRINTF(E_FATAL, L_MAIN, "Library thread failed to start\n");
|
||||
|
||||
ret = EXIT_FAILURE;
|
||||
goto filescanner_fail;
|
||||
goto library_fail;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
/* Spawn Spotify thread */
|
||||
ret = spotify_init();
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_INFO, L_MAIN, "Spotify thread not started\n");;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Spawn player thread */
|
||||
ret = player_init();
|
||||
if (ret != 0)
|
||||
@ -895,14 +883,10 @@ main(int argc, char **argv)
|
||||
player_deinit();
|
||||
|
||||
player_fail:
|
||||
#ifdef HAVE_SPOTIFY_H
|
||||
DPRINTF(E_LOG, L_MAIN, "Spotify deinit\n");
|
||||
spotify_deinit();
|
||||
#endif
|
||||
DPRINTF(E_LOG, L_MAIN, "File scanner deinit\n");
|
||||
filescanner_deinit();
|
||||
DPRINTF(E_LOG, L_MAIN, "Library scaner deinit\n");
|
||||
library_deinit();
|
||||
|
||||
filescanner_fail:
|
||||
library_fail:
|
||||
DPRINTF(E_LOG, L_MAIN, "Cache deinit\n");
|
||||
cache_deinit();
|
||||
|
||||
|
@ -262,6 +262,15 @@ safe_hextou64(const char *str, uint64_t *val)
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
safe_strdup(const char *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
return NULL;
|
||||
|
||||
return strdup(str);
|
||||
}
|
||||
|
||||
|
||||
/* Key/value functions */
|
||||
struct keyval *
|
||||
|
@ -42,6 +42,8 @@ safe_atou64(const char *str, uint64_t *val);
|
||||
int
|
||||
safe_hextou64(const char *str, uint64_t *val);
|
||||
|
||||
char *
|
||||
safe_strdup(const char *str);
|
||||
|
||||
/* Key/value functions */
|
||||
struct keyval *
|
||||
|
@ -56,8 +56,8 @@
|
||||
#include "artwork.h"
|
||||
|
||||
#include "player.h"
|
||||
#include "filescanner.h"
|
||||
#include "commands.h"
|
||||
#include "library.h"
|
||||
|
||||
|
||||
static pthread_t tid_mpd;
|
||||
@ -748,7 +748,7 @@ mpd_command_status(struct evbuffer *evbuf, int argc, char **argv, char **errmsg)
|
||||
(status.pos_ms / 1000.0));
|
||||
}
|
||||
|
||||
if (filescanner_scanning())
|
||||
if (library_is_scanning())
|
||||
{
|
||||
evbuffer_add(evbuf, "updating_db: 1\n", 15);
|
||||
}
|
||||
@ -3218,7 +3218,7 @@ mpd_command_update(struct evbuffer *evbuf, int argc, char **argv, char **errmsg)
|
||||
return ACK_ERROR_ARG;
|
||||
}
|
||||
|
||||
filescanner_trigger_initscan();
|
||||
library_rescan();
|
||||
|
||||
evbuffer_add(evbuf, "updating_db: 1\n", 15);
|
||||
|
||||
|
@ -1388,7 +1388,10 @@ source_read(uint8_t *buf, int len, uint64_t rtptime)
|
||||
{
|
||||
ret = source_open(ps, cur_streaming->end + 1, 0);
|
||||
if (ret < 0)
|
||||
return -1;
|
||||
{
|
||||
source_free(ps);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = source_play();
|
||||
if (ret < 0)
|
||||
@ -2485,6 +2488,7 @@ playback_prev_bh(void *arg, int *retval)
|
||||
ret = source_open(ps, last_rtptime + AIRTUNES_V2_PACKET_SAMPLES, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
source_free(ps);
|
||||
playback_abort();
|
||||
|
||||
*retval = -1;
|
||||
@ -2556,6 +2560,7 @@ playback_next_bh(void *arg, int *retval)
|
||||
ret = source_open(ps, last_rtptime + AIRTUNES_V2_PACKET_SAMPLES, 0);
|
||||
if (ret < 0)
|
||||
{
|
||||
source_free(ps);
|
||||
playback_abort();
|
||||
*retval = -1;
|
||||
return COMMAND_END;
|
||||
|
1170
src/spotify.c
1170
src/spotify.c
File diff suppressed because it is too large
Load Diff
757
src/spotify_webapi.c
Normal file
757
src/spotify_webapi.c
Normal file
@ -0,0 +1,757 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Espen Jürgensen <espenjurgensen@gmail.com>
|
||||
* Copyright (C) 2016 Christian Meffert <christian.meffert@googlemail.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "spotify_webapi.h"
|
||||
|
||||
#include <event2/event.h>
|
||||
#include <json.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "db.h"
|
||||
#include "http.h"
|
||||
#include "library.h"
|
||||
#include "logger.h"
|
||||
|
||||
|
||||
|
||||
// Credentials for the web api
|
||||
static char *spotify_access_token;
|
||||
static char *spotify_refresh_token;
|
||||
|
||||
static int32_t expires_in = 3600;
|
||||
static time_t token_requested = 0;
|
||||
|
||||
// Endpoints and credentials for the web api
|
||||
static const char *spotify_client_id = "0e684a5422384114a8ae7ac020f01789";
|
||||
static const char *spotify_client_secret = "232af95f39014c9ba218285a5c11a239";
|
||||
static const char *spotify_auth_uri = "https://accounts.spotify.com/authorize";
|
||||
static const char *spotify_token_uri = "https://accounts.spotify.com/api/token";
|
||||
static const char *spotify_playlist_uri = "https://api.spotify.com/v1/users/%s/playlists/%s";
|
||||
|
||||
|
||||
/*--------------------- HELPERS FOR SPOTIFY WEB API -------------------------*/
|
||||
/* All the below is in the httpd thread */
|
||||
|
||||
|
||||
static void
|
||||
jparse_free(json_object* haystack)
|
||||
{
|
||||
if (haystack)
|
||||
{
|
||||
#ifdef HAVE_JSON_C_OLD
|
||||
json_object_put(haystack);
|
||||
#else
|
||||
if (json_object_put(haystack) != 1)
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Memleak: JSON parser did not free object\n");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
jparse_array_from_obj(json_object *haystack, const char *key, json_object **needle)
|
||||
{
|
||||
if (! (json_object_object_get_ex(haystack, key, needle) && json_object_get_type(*needle) == json_type_array) )
|
||||
return -1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const char *
|
||||
jparse_str_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_string)
|
||||
return json_object_get_string(needle);
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
jparse_int_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
if (json_object_object_get_ex(haystack, key, &needle) && json_object_get_type(needle) == json_type_int)
|
||||
return json_object_get_int(needle);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static time_t
|
||||
jparse_time_from_obj(json_object *haystack, const char *key)
|
||||
{
|
||||
const char *tmp;
|
||||
struct tm tp;
|
||||
time_t parsed_time;
|
||||
|
||||
memset(&tp, 0, sizeof(struct tm));
|
||||
|
||||
tmp = jparse_str_from_obj(haystack, key);
|
||||
if (!tmp)
|
||||
return 0;
|
||||
|
||||
strptime(tmp, "%Y-%m-%dT%H:%M:%SZ", &tp);
|
||||
parsed_time = mktime(&tp);
|
||||
if (parsed_time < 0)
|
||||
return 0;
|
||||
|
||||
return parsed_time;
|
||||
}
|
||||
|
||||
static const char *
|
||||
jparse_str_from_array(json_object *array, int index, const char *key)
|
||||
{
|
||||
json_object *item;
|
||||
int count;
|
||||
|
||||
if (json_object_get_type(array) != json_type_array)
|
||||
return NULL;
|
||||
|
||||
count = json_object_array_length(array);
|
||||
if (count <= 0 || count <= index)
|
||||
return NULL;
|
||||
|
||||
item = json_object_array_get_idx(array, index);
|
||||
return jparse_str_from_obj(item, key);
|
||||
}
|
||||
|
||||
static void
|
||||
free_http_client_ctx(struct http_client_ctx *ctx)
|
||||
{
|
||||
if (!ctx)
|
||||
return;
|
||||
|
||||
if (ctx->input_body)
|
||||
evbuffer_free(ctx->input_body);
|
||||
if (ctx->output_headers)
|
||||
{
|
||||
keyval_clear(ctx->output_headers);
|
||||
free(ctx->output_headers);
|
||||
}
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
char *
|
||||
spotifywebapi_oauth_uri_get(const char *redirect_uri)
|
||||
{
|
||||
struct keyval kv;
|
||||
char *param;
|
||||
char *uri;
|
||||
int uri_len;
|
||||
int ret;
|
||||
|
||||
uri = NULL;
|
||||
memset(&kv, 0, sizeof(struct keyval));
|
||||
ret = ( (keyval_add(&kv, "client_id", spotify_client_id) == 0) &&
|
||||
(keyval_add(&kv, "response_type", "code") == 0) &&
|
||||
(keyval_add(&kv, "redirect_uri", redirect_uri) == 0) &&
|
||||
(keyval_add(&kv, "scope", "playlist-read-private user-library-read") == 0) &&
|
||||
(keyval_add(&kv, "show_dialog", "false") == 0) );
|
||||
if (!ret)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Cannot display Spotify oath interface (error adding parameters to keyval)\n");
|
||||
goto out_clear_kv;
|
||||
}
|
||||
|
||||
param = http_form_urlencode(&kv);
|
||||
if (param)
|
||||
{
|
||||
uri_len = strlen(spotify_auth_uri) + strlen(param) + 3;
|
||||
uri = calloc(uri_len, sizeof(char));
|
||||
snprintf(uri, uri_len, "%s/?%s", spotify_auth_uri, param);
|
||||
|
||||
free(param);
|
||||
}
|
||||
|
||||
out_clear_kv:
|
||||
keyval_clear(&kv);
|
||||
|
||||
return uri;
|
||||
}
|
||||
|
||||
static int
|
||||
tokens_get(struct keyval *kv, const char **err)
|
||||
{
|
||||
struct http_client_ctx ctx;
|
||||
char *param;
|
||||
char *body;
|
||||
json_object *haystack;
|
||||
const char *tmp;
|
||||
int ret;
|
||||
|
||||
param = http_form_urlencode(kv);
|
||||
if (!param)
|
||||
{
|
||||
*err = "http_form_uriencode() failed";
|
||||
ret = -1;
|
||||
goto out_clear_kv;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(struct http_client_ctx));
|
||||
ctx.url = (char *)spotify_token_uri;
|
||||
ctx.output_body = param;
|
||||
ctx.input_body = evbuffer_new();
|
||||
|
||||
ret = http_client_request(&ctx);
|
||||
if (ret < 0)
|
||||
{
|
||||
*err = "Did not get a reply from Spotify";
|
||||
goto out_free_input_body;
|
||||
}
|
||||
|
||||
// 0-terminate for safety
|
||||
evbuffer_add(ctx.input_body, "", 1);
|
||||
|
||||
body = (char *)evbuffer_pullup(ctx.input_body, -1);
|
||||
if (!body || (strlen(body) == 0))
|
||||
{
|
||||
*err = "The reply from Spotify is empty or invalid";
|
||||
ret = -1;
|
||||
goto out_free_input_body;
|
||||
}
|
||||
|
||||
DPRINTF(E_DBG, L_SPOTIFY, "Token reply: %s\n", body);
|
||||
|
||||
haystack = json_tokener_parse(body);
|
||||
if (!haystack)
|
||||
{
|
||||
*err = "JSON parser returned an error";
|
||||
ret = -1;
|
||||
goto out_free_input_body;
|
||||
}
|
||||
|
||||
free(spotify_access_token);
|
||||
spotify_access_token = NULL;
|
||||
|
||||
tmp = jparse_str_from_obj(haystack, "access_token");
|
||||
if (tmp)
|
||||
spotify_access_token = strdup(tmp);
|
||||
|
||||
tmp = jparse_str_from_obj(haystack, "refresh_token");
|
||||
if (tmp)
|
||||
{
|
||||
free(spotify_refresh_token);
|
||||
spotify_refresh_token = strdup(tmp);
|
||||
}
|
||||
|
||||
expires_in = jparse_int_from_obj(haystack, "expires_in");
|
||||
if (expires_in == 0)
|
||||
expires_in = 3600;
|
||||
|
||||
jparse_free(haystack);
|
||||
|
||||
if (!spotify_access_token)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Could not find access token in reply: %s\n", body);
|
||||
|
||||
*err = "Could not find access token in Spotify reply (see log)";
|
||||
ret = -1;
|
||||
goto out_free_input_body;
|
||||
}
|
||||
|
||||
token_requested = time(NULL);
|
||||
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "token: '%s'\n", spotify_access_token);
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "refresh-token: '%s'\n", spotify_refresh_token);
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "expires in: %d\n", expires_in);
|
||||
|
||||
if (spotify_refresh_token)
|
||||
db_admin_set("spotify_refresh_token", spotify_refresh_token);
|
||||
|
||||
ret = 0;
|
||||
|
||||
out_free_input_body:
|
||||
evbuffer_free(ctx.input_body);
|
||||
free(param);
|
||||
out_clear_kv:
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_token_get(const char *code, const char *redirect_uri, const char **err)
|
||||
{
|
||||
struct keyval kv;
|
||||
int ret;
|
||||
|
||||
memset(&kv, 0, sizeof(struct keyval));
|
||||
ret = ( (keyval_add(&kv, "grant_type", "authorization_code") == 0) &&
|
||||
(keyval_add(&kv, "code", code) == 0) &&
|
||||
(keyval_add(&kv, "client_id", spotify_client_id) == 0) &&
|
||||
(keyval_add(&kv, "client_secret", spotify_client_secret) == 0) &&
|
||||
(keyval_add(&kv, "redirect_uri", redirect_uri) == 0) );
|
||||
|
||||
if (!ret)
|
||||
{
|
||||
*err = "Add parameters to keyval failed";
|
||||
ret = -1;
|
||||
}
|
||||
else
|
||||
ret = tokens_get(&kv, err);
|
||||
|
||||
keyval_clear(&kv);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_token_refresh()
|
||||
{
|
||||
struct keyval kv;
|
||||
char *refresh_token;
|
||||
const char *err;
|
||||
int ret;
|
||||
|
||||
if (token_requested && difftime(time(NULL), token_requested) < expires_in)
|
||||
{
|
||||
DPRINTF(E_DBG, L_SPOTIFY, "Spotify token still valid\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
refresh_token = db_admin_get("spotify_refresh_token");
|
||||
if (!refresh_token)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "No spotify refresh token found\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
DPRINTF(E_DBG, L_SPOTIFY, "Spotify refresh-token: '%s'\n", refresh_token);
|
||||
|
||||
memset(&kv, 0, sizeof(struct keyval));
|
||||
ret = ( (keyval_add(&kv, "grant_type", "refresh_token") == 0) &&
|
||||
(keyval_add(&kv, "client_id", spotify_client_id) == 0) &&
|
||||
(keyval_add(&kv, "client_secret", spotify_client_secret) == 0) &&
|
||||
(keyval_add(&kv, "refresh_token", refresh_token) == 0) );
|
||||
if (!ret)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Add parameters to keyval failed");
|
||||
ret = -1;
|
||||
}
|
||||
else
|
||||
ret = tokens_get(&kv, &err);
|
||||
|
||||
free(refresh_token);
|
||||
keyval_clear(&kv);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
request_uri(struct spotify_request *request, const char *uri)
|
||||
{
|
||||
char bearer_token[1024];
|
||||
int ret;
|
||||
|
||||
memset(request, 0, sizeof(struct spotify_request));
|
||||
|
||||
if (0 > spotifywebapi_token_refresh())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
request->ctx = calloc(1, sizeof(struct http_client_ctx));
|
||||
request->ctx->output_headers = calloc(1, sizeof(struct keyval));
|
||||
request->ctx->input_body = evbuffer_new();
|
||||
request->ctx->url = uri;
|
||||
|
||||
snprintf(bearer_token, sizeof(bearer_token), "Bearer %s", spotify_access_token);
|
||||
if (keyval_add(request->ctx->output_headers, "Authorization", bearer_token) < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Add bearer_token to keyval failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = http_client_request(request->ctx);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Request for saved tracks/albums failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 0-terminate for safety
|
||||
evbuffer_add(request->ctx->input_body, "", 1);
|
||||
|
||||
request->response_body = (char *) evbuffer_pullup(request->ctx->input_body, -1);
|
||||
if (!request->response_body || (strlen(request->response_body) == 0))
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Request for saved tracks/albums failed, response was empty\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// DPRINTF(E_DBG, L_SPOTIFY, "Wep api response for '%s'\n%s\n", uri, request->response_body);
|
||||
|
||||
request->haystack = json_tokener_parse(request->response_body);
|
||||
if (!request->haystack)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "JSON parser returned an error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
DPRINTF(E_DBG, L_SPOTIFY, "Got response for '%s'\n", uri);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
spotifywebapi_request_end(struct spotify_request *request)
|
||||
{
|
||||
free_http_client_ctx(request->ctx);
|
||||
jparse_free(request->haystack);
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_request_next(struct spotify_request *request, const char *uri)
|
||||
{
|
||||
char *next_uri;
|
||||
int ret;
|
||||
|
||||
if (request->ctx && !request->next_uri)
|
||||
{
|
||||
// Reached end of paging requests, terminate loop
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!request->ctx)
|
||||
{
|
||||
// First paging request
|
||||
next_uri = strdup (uri);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Next paging request
|
||||
next_uri = strdup(request->next_uri);
|
||||
spotifywebapi_request_end(request);
|
||||
}
|
||||
|
||||
ret = request_uri(request, next_uri);
|
||||
free(next_uri);
|
||||
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
request->total = jparse_int_from_obj(request->haystack, "total");
|
||||
request->next_uri = jparse_str_from_obj(request->haystack, "next");
|
||||
|
||||
if (jparse_array_from_obj(request->haystack, "items", &request->items) < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "No items in reply from Spotify. See:\n%s\n", request->response_body);
|
||||
return -1;
|
||||
}
|
||||
|
||||
request->count = json_object_array_length(request->items);
|
||||
|
||||
DPRINTF(E_DBG, L_SPOTIFY, "Got %d items\n", request->count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
parse_metadata_track(json_object* jsontrack, struct spotify_track* track)
|
||||
{
|
||||
json_object* jsonalbum;
|
||||
json_object* jsonartists;
|
||||
|
||||
if (json_object_object_get_ex(jsontrack, "album", &jsonalbum))
|
||||
{
|
||||
track->album = jparse_str_from_obj(jsonalbum, "name");
|
||||
if (json_object_object_get_ex(jsonalbum, "artists", &jsonartists))
|
||||
{
|
||||
track->album_artist = jparse_str_from_array(jsonartists, 0, "name");
|
||||
}
|
||||
}
|
||||
if (json_object_object_get_ex(jsontrack, "artists", &jsonartists))
|
||||
{
|
||||
track->artist = jparse_str_from_array(jsonartists, 0, "name");
|
||||
}
|
||||
track->disc_number = jparse_int_from_obj(jsontrack, "disc_number");
|
||||
track->album_type = jparse_str_from_obj(jsonalbum, "album_type");
|
||||
track->is_compilation = (track->album_type && 0 == strcmp(track->album_type, "compilation"));
|
||||
track->duration_ms = jparse_int_from_obj(jsontrack, "duration_ms");
|
||||
track->name = jparse_str_from_obj(jsontrack, "name");
|
||||
track->track_number = jparse_int_from_obj(jsontrack, "track_number");
|
||||
track->uri = jparse_str_from_obj(jsontrack, "uri");
|
||||
track->id = jparse_str_from_obj(jsontrack, "id");
|
||||
}
|
||||
|
||||
static int
|
||||
get_year_from_date(const char *date)
|
||||
{
|
||||
char tmp[5];
|
||||
uint32_t year = 0;
|
||||
|
||||
if (date && strlen(date) >= 4)
|
||||
{
|
||||
strncpy(tmp, date, sizeof(tmp));
|
||||
tmp[4] = '\0';
|
||||
safe_atou32(tmp, &year);
|
||||
}
|
||||
|
||||
return year;
|
||||
}
|
||||
|
||||
static void
|
||||
parse_metadata_album(json_object *jsonalbum, struct spotify_album *album)
|
||||
{
|
||||
json_object* jsonartists;
|
||||
|
||||
if (json_object_object_get_ex(jsonalbum, "artists", &jsonartists))
|
||||
{
|
||||
album->artist = jparse_str_from_array(jsonartists, 0, "name");
|
||||
}
|
||||
album->name = jparse_str_from_obj(jsonalbum, "name");
|
||||
album->uri = jparse_str_from_obj(jsonalbum, "uri");
|
||||
album->id = jparse_str_from_obj(jsonalbum, "id");
|
||||
|
||||
album->album_type = jparse_str_from_obj(jsonalbum, "album_type");
|
||||
album->is_compilation = (album->album_type && 0 == strcmp(album->album_type, "compilation"));
|
||||
|
||||
album->label = jparse_str_from_obj(jsonalbum, "label");
|
||||
|
||||
album->release_date = jparse_str_from_obj(jsonalbum, "release_date");
|
||||
album->release_date_precision = jparse_str_from_obj(jsonalbum, "release_date_precision");
|
||||
album->release_year = get_year_from_date(album->release_date);
|
||||
|
||||
// TODO Genre is an array of strings ('genres'), but it is always empty (https://github.com/spotify/web-api/issues/157)
|
||||
//album->genre = jparse_str_from_obj(jsonalbum, "genre");
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_saved_albums_fetch(struct spotify_request *request, json_object **jsontracks, int *track_count, struct spotify_album *album)
|
||||
{
|
||||
json_object *jsonalbum;
|
||||
json_object *item;
|
||||
json_object *needle;
|
||||
|
||||
memset(album, 0, sizeof(struct spotify_album));
|
||||
*track_count = 0;
|
||||
|
||||
if (request->index >= request->count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
item = json_object_array_get_idx(request->items, request->index);
|
||||
if (!(item && json_object_object_get_ex(item, "album", &jsonalbum)))
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Unexpected JSON: Item %d did not have 'album'->'uri'\n", request->index);
|
||||
request->index++;
|
||||
return -1;
|
||||
}
|
||||
|
||||
parse_metadata_album(jsonalbum, album);
|
||||
|
||||
album->added_at = jparse_str_from_obj(item, "added_at");
|
||||
album->mtime = jparse_time_from_obj(item, "added_at");
|
||||
|
||||
if (json_object_object_get_ex(jsonalbum, "tracks", &needle))
|
||||
{
|
||||
if (jparse_array_from_obj(needle, "items", jsontracks) == 0)
|
||||
{
|
||||
*track_count = json_object_array_length(*jsontracks);
|
||||
}
|
||||
}
|
||||
|
||||
request->index++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_album_track_fetch(json_object *jsontracks, int index, struct spotify_track *track)
|
||||
{
|
||||
json_object *jsontrack;
|
||||
|
||||
memset(track, 0, sizeof(struct spotify_track));
|
||||
|
||||
jsontrack = json_object_array_get_idx(jsontracks, index);
|
||||
|
||||
if (!jsontrack)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
parse_metadata_track(jsontrack, track);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
parse_metadata_playlist(json_object *jsonplaylist, struct spotify_playlist *playlist)
|
||||
{
|
||||
json_object *needle;
|
||||
|
||||
playlist->name = jparse_str_from_obj(jsonplaylist, "name");
|
||||
playlist->uri = jparse_str_from_obj(jsonplaylist, "uri");
|
||||
playlist->id = jparse_str_from_obj(jsonplaylist, "id");
|
||||
playlist->href = jparse_str_from_obj(jsonplaylist, "href");
|
||||
|
||||
if (json_object_object_get_ex(jsonplaylist, "owner", &needle))
|
||||
{
|
||||
playlist->owner = jparse_str_from_obj(needle, "id");
|
||||
}
|
||||
|
||||
if (json_object_object_get_ex(jsonplaylist, "tracks", &needle))
|
||||
{
|
||||
playlist->tracks_href = jparse_str_from_obj(needle, "href");
|
||||
playlist->tracks_count = jparse_int_from_obj(needle, "total");
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_playlists_fetch(struct spotify_request *request, struct spotify_playlist *playlist)
|
||||
{
|
||||
json_object *jsonplaylist;
|
||||
|
||||
memset(playlist, 0, sizeof(struct spotify_playlist));
|
||||
|
||||
if (request->index >= request->count)
|
||||
{
|
||||
DPRINTF(E_DBG, L_SPOTIFY, "All playlists processed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
jsonplaylist = json_object_array_get_idx(request->items, request->index);
|
||||
if (!jsonplaylist)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Error fetching playlist at index '%d'\n", request->index);
|
||||
return -1;
|
||||
}
|
||||
|
||||
parse_metadata_playlist(jsonplaylist, playlist);
|
||||
request->index++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extracts the owner and the id from a spotify playlist uri
|
||||
*
|
||||
* Playlist-uri has the following format: spotify:user:[owner]:playlist:[id]
|
||||
* Owner and plid must be freed by the caller.
|
||||
*/
|
||||
static int
|
||||
get_owner_plid_from_uri(const char *uri, char **owner, char **plid)
|
||||
{
|
||||
char *ptr1;
|
||||
char *ptr2;
|
||||
char *tmp;
|
||||
size_t len;
|
||||
|
||||
ptr1 = strchr(uri, ':');
|
||||
if (!ptr1)
|
||||
return -1;
|
||||
ptr1++;
|
||||
ptr1 = strchr(ptr1, ':');
|
||||
if (!ptr1)
|
||||
return -1;
|
||||
ptr1++;
|
||||
ptr2 = strchr(ptr1, ':');
|
||||
|
||||
len = ptr2 - ptr1;
|
||||
|
||||
tmp = malloc(sizeof(char) * (len + 1));
|
||||
strncpy(tmp, ptr1, len);
|
||||
tmp[len] = '\0';
|
||||
*owner = tmp;
|
||||
|
||||
ptr2++;
|
||||
ptr1 = strchr(ptr2, ':');
|
||||
if (!ptr1)
|
||||
{
|
||||
free(tmp);
|
||||
return -1;
|
||||
}
|
||||
ptr1++;
|
||||
*plid = strdup(ptr1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_playlisttracks_fetch(struct spotify_request *request, struct spotify_track *track)
|
||||
{
|
||||
json_object *item;
|
||||
json_object *jsontrack;
|
||||
|
||||
memset(track, 0, sizeof(struct spotify_track));
|
||||
|
||||
if (request->index >= request->count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
item = json_object_array_get_idx(request->items, request->index);
|
||||
if (!(item && json_object_object_get_ex(item, "track", &jsontrack)))
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Unexpected JSON: Item %d did not have 'track'->'uri'\n", request->index);
|
||||
request->index++;
|
||||
return -1;
|
||||
}
|
||||
|
||||
parse_metadata_track(jsontrack, track);
|
||||
track->added_at = jparse_str_from_obj(item, "added_at");
|
||||
track->mtime = jparse_time_from_obj(item, "added_at");
|
||||
|
||||
request->index++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
spotifywebapi_playlist_start(struct spotify_request *request, const char *path, struct spotify_playlist *playlist)
|
||||
{
|
||||
char uri[1024];
|
||||
char *owner;
|
||||
char *id;
|
||||
int ret;
|
||||
|
||||
ret = get_owner_plid_from_uri(path, &owner, &id);
|
||||
if (ret < 0)
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Error extracting owner and id from playlist uri '%s'\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = snprintf(uri, sizeof(uri), spotify_playlist_uri, owner, id);
|
||||
if (ret < 0 || ret >= sizeof(uri))
|
||||
{
|
||||
DPRINTF(E_LOG, L_SPOTIFY, "Error creating playlist endpoint uri for playlist '%s'\n", path);
|
||||
free(owner);
|
||||
free(id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = request_uri(request, uri);
|
||||
if (ret < 0)
|
||||
{
|
||||
free(owner);
|
||||
free(id);
|
||||
return -1;
|
||||
}
|
||||
|
||||
request->haystack = json_tokener_parse(request->response_body);
|
||||
parse_metadata_playlist(request->haystack, playlist);
|
||||
|
||||
free(owner);
|
||||
free(id);
|
||||
return 0;
|
||||
}
|
||||
|
121
src/spotify_webapi.h
Normal file
121
src/spotify_webapi.h
Normal file
@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Espen Jürgensen <espenjurgensen@gmail.com>
|
||||
* Copyright (C) 2016 Christian Meffert <christian.meffert@googlemail.com>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef SRC_SPOTIFY_WEBAPI_H_
|
||||
#define SRC_SPOTIFY_WEBAPI_H_
|
||||
|
||||
|
||||
#include <event2/event.h>
|
||||
#ifdef HAVE_JSON_C_OLD
|
||||
# include <json/json.h>
|
||||
#else
|
||||
# include <json-c/json.h>
|
||||
#endif
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "http.h"
|
||||
|
||||
#define SPOTIFY_WEBAPI_SAVED_ALBUMS "https://api.spotify.com/v1/me/albums?limit=50"
|
||||
#define SPOTIFY_WEBAPI_SAVED_PLAYLISTS "https://api.spotify.com/v1/me/playlists?limit=50"
|
||||
|
||||
struct spotify_album
|
||||
{
|
||||
const char *added_at;
|
||||
time_t mtime;
|
||||
|
||||
const char *album_type;
|
||||
bool is_compilation;
|
||||
const char *artist;
|
||||
const char *genre;
|
||||
const char *id;
|
||||
const char *label;
|
||||
const char *name;
|
||||
const char *release_date;
|
||||
const char *release_date_precision;
|
||||
int release_year;
|
||||
const char *uri;
|
||||
};
|
||||
|
||||
struct spotify_track
|
||||
{
|
||||
const char *added_at;
|
||||
time_t mtime;
|
||||
|
||||
const char *album;
|
||||
const char *album_artist;
|
||||
const char *artist;
|
||||
int disc_number;
|
||||
const char *album_type;
|
||||
bool is_compilation;
|
||||
int duration_ms;
|
||||
const char *id;
|
||||
const char *name;
|
||||
int track_number;
|
||||
const char *uri;
|
||||
};
|
||||
|
||||
struct spotify_playlist
|
||||
{
|
||||
const char *id;
|
||||
const char *name;
|
||||
const char *owner;
|
||||
const char *uri;
|
||||
|
||||
const char *href;
|
||||
|
||||
const char *tracks_href;
|
||||
int tracks_count;
|
||||
};
|
||||
|
||||
struct spotify_request
|
||||
{
|
||||
struct http_client_ctx *ctx;
|
||||
char *response_body;
|
||||
json_object *haystack;
|
||||
json_object *items;
|
||||
int count;
|
||||
int total;
|
||||
const char *next_uri;
|
||||
|
||||
int index;
|
||||
};
|
||||
|
||||
char *
|
||||
spotifywebapi_oauth_uri_get(const char *redirect_uri);
|
||||
int
|
||||
spotifywebapi_token_get(const char *code, const char *redirect_uri, const char **err);
|
||||
int
|
||||
spotifywebapi_token_refresh();
|
||||
|
||||
void
|
||||
spotifywebapi_request_end(struct spotify_request *request);
|
||||
int
|
||||
spotifywebapi_request_next(struct spotify_request *request, const char *uri);
|
||||
int
|
||||
spotifywebapi_saved_albums_fetch(struct spotify_request *request, json_object **jsontracks, int *track_count, struct spotify_album *album);
|
||||
int
|
||||
spotifywebapi_album_track_fetch(json_object *jsontracks, int index, struct spotify_track *track);
|
||||
int
|
||||
spotifywebapi_playlists_fetch(struct spotify_request *request, struct spotify_playlist* playlist);
|
||||
int
|
||||
spotifywebapi_playlisttracks_fetch(struct spotify_request *request, struct spotify_track *track);
|
||||
int
|
||||
spotifywebapi_playlist_start(struct spotify_request *request, const char *path, struct spotify_playlist *playlist);
|
||||
|
||||
#endif /* SRC_SPOTIFY_WEBAPI_H_ */
|
Loading…
Reference in New Issue
Block a user