Commit 42706240 authored by Sam Thursfield's avatar Sam Thursfield Committed by Martyn Russell

tracker-extract: Rework albumart API to allow multiple media types

* tracker-extract-gstreamer, tracker-extract-mp3: Update to use new API
* tracker-extract-mp3: Pass URI instead of filename for consistency
parent 5a28408e
......@@ -49,6 +49,12 @@
#define ALBUMARTER_PATH "/com/nokia/albumart/Requester"
#define ALBUMARTER_INTERFACE "com.nokia.albumart.Requester"
static const gchar *media_art_type_name[TRACKER_MEDIA_ART_TYPE_COUNT] = {
"invalid",
"album",
"video"
};
typedef struct {
TrackerStorage *storage;
gchar *art_path;
......@@ -553,26 +559,29 @@ static gboolean
albumart_set (const unsigned char *buffer,
size_t len,
const gchar *mime,
TrackerMediaArtType type,
const gchar *artist,
const gchar *album,
const gchar *title,
const gchar *uri)
{
gchar *local_path;
gboolean retval = FALSE;
if (!artist && !album) {
g_warning ("Could not save embedded album art, no artist or album supplied");
g_return_val_if_fail (type > TRACKER_MEDIA_ART_NONE && type < TRACKER_MEDIA_ART_TYPE_COUNT, FALSE);
if (!artist && !title) {
g_warning ("Could not save embedded album art, not enough metadata supplied");
return FALSE;
}
tracker_albumart_get_path (artist, album, "album", NULL, &local_path, NULL);
tracker_albumart_get_path (artist, title, media_art_type_name[type], NULL, &local_path, NULL);
if (artist == NULL || g_strcmp0 (artist, " ") == 0) {
if (type != TRACKER_MEDIA_ART_ALBUM || (artist == NULL || g_strcmp0 (artist, " ") == 0)) {
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, local_path);
} else {
gchar *album_path;
tracker_albumart_get_path (NULL, album, "album", NULL, &album_path, NULL);
tracker_albumart_get_path (NULL, title, media_art_type_name[type], NULL, &album_path, NULL);
if (!g_file_test (album_path, G_FILE_TEST_EXISTS)) {
retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, album_path);
......@@ -659,11 +668,12 @@ albumart_set (const unsigned char *buffer,
}
static void
albumart_request_download (TrackerStorage *storage,
const gchar *album,
const gchar *artist,
const gchar *local_uri,
const gchar *art_path)
albumart_request_download (TrackerStorage *storage,
TrackerMediaArtType type,
const gchar *album,
const gchar *artist,
const gchar *local_uri,
const gchar *art_path)
{
if (connection) {
GetFileInfo *info;
......@@ -672,6 +682,10 @@ albumart_request_download (TrackerStorage *storage,
return;
}
if (type != TRACKER_MEDIA_ART_ALBUM) {
return;
}
info = g_slice_new (GetFileInfo);
info->storage = storage ? g_object_ref (storage) : NULL;
......@@ -880,43 +894,41 @@ gboolean
tracker_albumart_process (const unsigned char *buffer,
size_t len,
const gchar *mime,
TrackerMediaArtType type,
const gchar *artist,
const gchar *album,
const gchar *filename)
const gchar *title,
const gchar *uri)
{
gchar *art_path;
gchar *local_art_uri = NULL;
gboolean processed = TRUE, a_exists, created = FALSE;
gchar *local_uri = NULL;
gchar *filename_uri;
guint64 mtime, a_mtime = 0;
g_debug ("Processing album art, buffer is %ld bytes, artist:'%s', album:'%s', filename:'%s', mime:'%s'",
(long int) len,
g_return_val_if_fail (type > TRACKER_MEDIA_ART_NONE && type < TRACKER_MEDIA_ART_TYPE_COUNT, FALSE);
g_debug ("Processing media art: artist:'%s', title:'%s', type:'%s', uri:'%s'. Buffer is %ld bytes, mime:'%s'",
artist ? artist : "",
album ? album : "",
filename,
title ? title : "",
media_art_type_name[type],
uri,
(long int) len,
mime);
/* TODO: We can definitely work with GFiles better here */
filename_uri = (strstr (filename, "://") ?
g_strdup (filename) :
g_filename_to_uri (filename, NULL, NULL));
mtime = tracker_file_get_mtime_uri (filename_uri);
mtime = tracker_file_get_mtime_uri (uri);
tracker_albumart_get_path (artist,
album,
"album",
filename_uri,
title,
media_art_type_name[type],
uri,
&art_path,
&local_uri);
&local_art_uri);
if (!art_path) {
g_debug ("Album art path could not be obtained, not processing any further");
g_free (filename_uri);
g_free (local_uri);
g_free (local_art_uri);
return FALSE;
}
......@@ -928,12 +940,7 @@ tracker_albumart_process (const unsigned char *buffer,
}
if ((buffer && len > 0) && ((!a_exists) || (a_exists && mtime > a_mtime))) {
processed = albumart_set (buffer,
len,
mime,
artist,
album,
filename_uri);
processed = albumart_set (buffer, len, mime, type, artist, title, uri);
set_mtime (art_path, mtime);
created = TRUE;
}
......@@ -944,7 +951,7 @@ tracker_albumart_process (const unsigned char *buffer,
gchar *dirname = NULL;
GFile *file, *dirf;
file = g_file_new_for_uri (filename_uri);
file = g_file_new_for_uri (uri);
dirf = g_file_get_parent (file);
if (dirf) {
dirname = g_file_get_path (dirf);
......@@ -952,18 +959,19 @@ tracker_albumart_process (const unsigned char *buffer,
}
g_object_unref (file);
key = g_strdup_printf ("%s-%s-%s",
key = g_strdup_printf ("%i-%s-%s-%s",
type,
artist ? artist : "",
album ? album : "",
title ? title : "",
dirname ? dirname : "");
g_free (dirname);
if (!g_hash_table_lookup (albumart_cache, key)) {
if (!albumart_heuristic (artist,
album,
filename_uri,
local_uri,
title,
uri,
local_art_uri,
NULL)) {
/* If the heuristic failed, we
* request the download the
......@@ -971,9 +979,10 @@ tracker_albumart_process (const unsigned char *buffer,
* downloaders
*/
albumart_request_download (albumart_storage,
type,
artist,
album,
local_uri,
title,
local_art_uri,
art_path);
}
......@@ -988,25 +997,24 @@ tracker_albumart_process (const unsigned char *buffer,
} else {
if (!created) {
g_debug ("Album art already exists for uri:'%s' as '%s'",
filename_uri,
uri,
art_path);
}
}
if (local_uri && !g_file_test (local_uri, G_FILE_TEST_EXISTS)) {
if (local_art_uri && !g_file_test (local_art_uri, G_FILE_TEST_EXISTS)) {
/* We can't reuse art_exists here because the
* situation might have changed
*/
if (g_file_test (art_path, G_FILE_TEST_EXISTS)) {
albumart_copy_to_local (albumart_storage,
art_path,
local_uri);
local_art_uri);
}
}
g_free (art_path);
g_free (filename_uri);
g_free (local_uri);
g_free (local_art_uri);
return processed;
}
......@@ -24,14 +24,23 @@
G_BEGIN_DECLS
typedef enum {
TRACKER_MEDIA_ART_NONE,
TRACKER_MEDIA_ART_ALBUM,
TRACKER_MEDIA_ART_VIDEO,
TRACKER_MEDIA_ART_TYPE_COUNT
} TrackerMediaArtType;
gboolean tracker_albumart_init (void);
void tracker_albumart_shutdown (void);
gboolean tracker_albumart_process (const unsigned char *buffer,
size_t len,
const gchar *mime,
const gchar *mime_type,
TrackerMediaArtType type,
const gchar *artist,
const gchar *album,
const gchar *filename);
const gchar *title,
const gchar *uri);
G_END_DECLS
......
......@@ -598,7 +598,8 @@ extractor_apply_general_metadata (MetadataExtractor *extractor,
TrackerSparqlBuilder *metadata,
const gchar *graph,
gchar **p_performer_uri,
gchar **p_composer_uri)
gchar **p_composer_uri,
gchar **p_media_title)
{
const gchar *performer = NULL;
gchar *performer_temp = NULL;
......@@ -647,7 +648,12 @@ extractor_apply_general_metadata (MetadataExtractor *extractor,
g_free (artist_temp);
g_free (composer);
g_free (genre);
g_free (title);
if (p_media_title != NULL) {
*p_media_title = title;
} else {
g_free (title);
}
}
static void
......@@ -929,7 +935,8 @@ extract_track_metadata (MetadataExtractor *extractor,
postupdate,
graph,
&track_performer_uri,
&track_composer_uri);
&track_composer_uri,
NULL);
extractor_apply_audio_metadata (extractor,
toc_entry->tag_list,
......@@ -999,8 +1006,9 @@ extract_metadata (MetadataExtractor *extractor,
TrackerSparqlBuilder *preupdate,
TrackerSparqlBuilder *postupdate,
TrackerSparqlBuilder *metadata,
gchar **album_artist,
gchar **album_title,
TrackerMediaArtType *media_type,
gchar **media_artist,
gchar **media_title,
const gchar *graph)
{
g_return_if_fail (extractor != NULL);
......@@ -1008,6 +1016,8 @@ extract_metadata (MetadataExtractor *extractor,
g_return_if_fail (postupdate != NULL);
g_return_if_fail (metadata != NULL);
*media_type = TRACKER_MEDIA_ART_NONE;
if (extractor->toc) {
gst_tag_list_insert (extractor->tagcache,
extractor->toc->tag_list,
......@@ -1050,8 +1060,12 @@ extract_metadata (MetadataExtractor *extractor,
if (extractor->toc == NULL || extractor->toc->entry_list == NULL)
tracker_sparql_builder_object (metadata, "nmm:MusicPiece");
*media_type = TRACKER_MEDIA_ART_ALBUM;
} else if (extractor->mime == EXTRACT_MIME_VIDEO) {
tracker_sparql_builder_object (metadata, "nmm:Video");
*media_type = TRACKER_MEDIA_ART_VIDEO;
} else {
tracker_sparql_builder_object (metadata, "nfo:Image");
......@@ -1078,7 +1092,8 @@ extract_metadata (MetadataExtractor *extractor,
metadata,
graph,
&performer_uri,
&composer_uri);
&composer_uri,
media_title);
extractor_apply_device_metadata (extractor,
extractor->tagcache,
......@@ -1092,14 +1107,21 @@ extract_metadata (MetadataExtractor *extractor,
metadata,
graph);
if (extractor->mime == EXTRACT_MIME_VIDEO)
if (extractor->mime == EXTRACT_MIME_VIDEO) {
extractor_apply_video_metadata (extractor,
extractor->tagcache,
metadata,
performer_uri,
composer_uri);
}
if (extractor->mime == EXTRACT_MIME_AUDIO) {
/* FIXME: we need to use album title instead of file title - we can
* avoid this waste by storing the media art extract info in the
* MetadataExtractor object
*/
g_free (*media_title);
extractor_apply_album_metadata (extractor,
extractor->tagcache,
preupdate,
......@@ -1107,8 +1129,8 @@ extract_metadata (MetadataExtractor *extractor,
&album_artist_uri,
&album_uri,
&album_disc_uri,
album_artist,
album_title);
media_artist,
media_title);
extractor_apply_audio_metadata (extractor,
extractor->tagcache,
......@@ -1946,7 +1968,8 @@ tracker_extract_gstreamer (const gchar *uri,
{
MetadataExtractor *extractor;
gchar *cue_sheet;
gchar *album_artist, *album_title;
TrackerMediaArtType media_type;
gchar *media_artist, *media_title;
gboolean success;
g_return_if_fail (uri);
......@@ -1994,20 +2017,31 @@ tracker_extract_gstreamer (const gchar *uri,
extractor->toc = tracker_cue_sheet_parse_uri (uri);
}
album_artist = NULL;
album_title = NULL;
media_artist = NULL;
media_title = NULL;
extract_metadata (extractor, uri, preupdate, postupdate, metadata, &album_artist, &album_title, graph);
extract_metadata (extractor,
uri,
preupdate,
postupdate,
metadata,
&media_type,
&media_artist,
&media_title,
graph);
tracker_albumart_process (extractor->album_art_data,
extractor->album_art_size,
extractor->album_art_mime,
album_artist,
album_title,
uri);
if (media_type != TRACKER_MEDIA_ART_NONE) {
tracker_albumart_process (extractor->album_art_data,
extractor->album_art_size,
extractor->album_art_mime,
media_type,
media_artist,
media_title,
uri);
}
g_free (album_artist);
g_free (album_title);
g_free (media_artist);
g_free (media_title);
gst_tag_list_free (extractor->tagcache);
......
......@@ -2506,9 +2506,10 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
tracker_albumart_process (md.albumart_data,
md.albumart_size,
md.albumart_mime,
TRACKER_MEDIA_ART_ALBUM,
md.performer,
md.album,
filename);
uri);
g_free (md.albumart_data);
g_free (md.albumart_mime);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment