Merge pull request #26373 from ksooo/app-fix-multi-resolve-playback
[xbmc.git] / xbmc / music / MusicInfoLoader.cpp
blob6a5aecd55334da9189ea612d76858f056de06283
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "MusicInfoLoader.h"
11 #include "Album.h"
12 #include "Artist.h"
13 #include "FileItem.h"
14 #include "FileItemList.h"
15 #include "MusicDatabase.h"
16 #include "MusicThumbLoader.h"
17 #include "ServiceBroker.h"
18 #include "filesystem/File.h"
19 #include "filesystem/MusicDatabaseDirectory/DirectoryNode.h"
20 #include "filesystem/MusicDatabaseDirectory/QueryParams.h"
21 #include "music/MusicFileItemClassify.h"
22 #include "music/tags/MusicInfoTag.h"
23 #include "music/tags/MusicInfoTagLoaderFactory.h"
24 #include "network/NetworkFileItemClassify.h"
25 #include "playlists/PlayListFileItemClassify.h"
26 #include "settings/Settings.h"
27 #include "settings/SettingsComponent.h"
28 #include "utils/Archive.h"
29 #include "utils/StringUtils.h"
30 #include "utils/URIUtils.h"
31 #include "utils/log.h"
33 using namespace KODI;
34 using namespace MUSIC_INFO;
35 using namespace XFILE;
37 // HACK until we make this threadable - specify 1 thread only for now
38 CMusicInfoLoader::CMusicInfoLoader() : CBackgroundInfoLoader()
40 m_mapFileItems = new CFileItemList;
42 m_thumbLoader = new CMusicThumbLoader();
45 CMusicInfoLoader::~CMusicInfoLoader()
47 StopThread();
48 delete m_mapFileItems;
49 delete m_thumbLoader;
52 void CMusicInfoLoader::OnLoaderStart()
54 // Load previously cached items from HD
55 if (!m_strCacheFileName.empty())
56 LoadCache(m_strCacheFileName, *m_mapFileItems);
57 else
59 m_mapFileItems->SetPath(m_pVecItems->GetPath());
60 m_mapFileItems->Load();
61 m_mapFileItems->SetFastLookup(true);
64 m_strPrevPath.clear();
66 m_databaseHits = m_tagReads = 0;
68 if (m_pProgressCallback)
69 m_pProgressCallback->SetProgressMax(m_pVecItems->GetFileCount());
71 m_musicDatabase.Open();
73 if (m_thumbLoader)
74 m_thumbLoader->OnLoaderStart();
77 bool CMusicInfoLoader::LoadAdditionalTagInfo(CFileItem* pItem)
79 if (!pItem || (pItem->m_bIsFolder && !MUSIC::IsAudio(*pItem)) || PLAYLIST::IsPlayList(*pItem) ||
80 pItem->IsNFO() || NETWORK::IsInternetStream(*pItem))
81 return false;
83 if (pItem->GetProperty("hasfullmusictag") == "true")
84 return false; // already have the information
86 std::string path(pItem->GetPath());
87 // For songs in library set the (primary) song artist and album properties
88 // Use song Id (not path) as called for items from either library or file view,
89 // but could also be listitem with tag loaded by a script
90 if (pItem->HasMusicInfoTag() &&
91 pItem->GetMusicInfoTag()->GetType() == MediaTypeSong &&
92 pItem->GetMusicInfoTag()->GetDatabaseId() > 0)
94 CMusicDatabase database;
95 database.Open();
96 // May already have song artist ids as item property set when data read from
97 // db, but check property is valid array (scripts could set item properties
98 // incorrectly), otherwise fetch artist using song id.
99 CArtist artist;
100 bool artistfound = false;
101 if (pItem->HasProperty("artistid") && pItem->GetProperty("artistid").isArray())
103 CVariant::const_iterator_array varid = pItem->GetProperty("artistid").begin_array();
104 int idArtist = static_cast<int>(varid->asInteger());
105 artistfound = database.GetArtist(idArtist, artist, false);
107 else
108 artistfound = database.GetArtistFromSong(pItem->GetMusicInfoTag()->GetDatabaseId(), artist);
109 if (artistfound)
110 CMusicDatabase::SetPropertiesFromArtist(*pItem, artist);
112 // May already have album id, otherwise fetch album from song id
113 CAlbum album;
114 bool albumfound = false;
115 int idAlbum = pItem->GetMusicInfoTag()->GetAlbumId();
116 if (idAlbum > 0)
117 albumfound = database.GetAlbum(idAlbum, album, false);
118 else
119 albumfound = database.GetAlbumFromSong(pItem->GetMusicInfoTag()->GetDatabaseId(), album);
120 if (albumfound)
121 CMusicDatabase::SetPropertiesFromAlbum(*pItem, album);
123 path = pItem->GetMusicInfoTag()->GetURL();
126 CLog::Log(LOGDEBUG, "Loading additional tag info for file {}", path);
128 // we load up the actual tag for this file in order to
129 // fetch the lyrics and add it to the current music info tag
130 CFileItem tempItem(path, false);
131 std::unique_ptr<IMusicInfoTagLoader> pLoader (CMusicInfoTagLoaderFactory::CreateLoader(tempItem));
132 if (nullptr != pLoader)
134 CMusicInfoTag tag;
135 pLoader->Load(path, tag);
136 pItem->GetMusicInfoTag()->SetLyrics(tag.GetLyrics());
137 pItem->SetProperty("hasfullmusictag", "true");
138 return true;
140 return false;
143 bool CMusicInfoLoader::LoadItem(CFileItem* pItem)
145 bool result = LoadItemCached(pItem);
146 result |= LoadItemLookup(pItem);
148 return result;
151 bool CMusicInfoLoader::LoadItemCached(CFileItem* pItem)
153 if ((pItem->m_bIsFolder && !MUSIC::IsAudio(*pItem)) || PLAYLIST::IsPlayList(*pItem) ||
154 PLAYLIST::IsSmartPlayList(*pItem) ||
155 StringUtils::StartsWithNoCase(pItem->GetPath(), "newplaylist://") ||
156 StringUtils::StartsWithNoCase(pItem->GetPath(), "newsmartplaylist://") || pItem->IsNFO() ||
157 (NETWORK::IsInternetStream(*pItem) && !MUSIC::IsMusicDb(*pItem)))
158 return false;
160 // Get thumb for item
161 m_thumbLoader->LoadItem(pItem);
163 return true;
166 bool CMusicInfoLoader::LoadItemLookup(CFileItem* pItem)
168 if (m_pProgressCallback && !pItem->m_bIsFolder)
169 m_pProgressCallback->SetProgressAdvance();
171 if ((pItem->m_bIsFolder && !MUSIC::IsAudio(*pItem)) || //
172 PLAYLIST::IsPlayList(*pItem) || PLAYLIST::IsSmartPlayList(*pItem) || //
173 StringUtils::StartsWithNoCase(pItem->GetPath(), "newplaylist://") || //
174 StringUtils::StartsWithNoCase(pItem->GetPath(), "newsmartplaylist://") || //
175 pItem->IsNFO() || (NETWORK::IsInternetStream(*pItem) && !MUSIC::IsMusicDb(*pItem)))
176 return false;
178 if ((!pItem->HasMusicInfoTag() || !pItem->GetMusicInfoTag()->Loaded()) && MUSIC::IsAudio(*pItem))
180 // first check the cached item
181 CFileItemPtr mapItem = (*m_mapFileItems)[pItem->GetPath()];
182 if (mapItem && mapItem->m_dateTime==pItem->m_dateTime && mapItem->HasMusicInfoTag() && mapItem->GetMusicInfoTag()->Loaded())
183 { // Query map if we previously cached the file on HD
184 *pItem->GetMusicInfoTag() = *mapItem->GetMusicInfoTag();
185 if (mapItem->HasArt("thumb"))
186 pItem->SetArt("thumb", mapItem->GetArt("thumb"));
188 else
190 std::string strPath = URIUtils::GetDirectory(pItem->GetPath());
191 URIUtils::AddSlashAtEnd(strPath);
192 if (strPath!=m_strPrevPath)
194 // The item is from another directory as the last one,
195 // query the database for the new directory...
196 m_musicDatabase.GetSongsByPath(strPath, m_songsMap);
197 m_databaseHits++;
201 This only loads the item with the song from the database when it maps to a single song,
202 it can not load song data for items with cuesheets that expand to multiple songs.
203 For songs from embedded or separate cuesheets strFileName is not unique, so the song map for
204 the path will have the list of songs from that file. But items with cuesheets are expanded
205 (replacing each item with items for every track) elsewhere. When the item we are looking up
206 has a cuesheet document or is a music file with a cuesheet embedded in the tags, and it maps
207 to more than one song then we can not fill the tag data and thumb from the database.
209 MAPSONGS::iterator it = m_songsMap.find(pItem->GetPath()); // Find file in song map
210 if (it != m_songsMap.end() && it->second.size() == 1)
212 // Have we loaded this item from database before,
213 // and even if it has a cuesheet it has only one song
214 pItem->GetMusicInfoTag()->SetSong(it->second[0]);
215 if (!it->second[0].strThumb.empty())
216 pItem->SetArt("thumb", it->second[0].strThumb);
218 else if (MUSIC::IsMusicDb(*pItem))
219 { // a music db item that doesn't have tag loaded - grab details from the database
220 XFILE::MUSICDATABASEDIRECTORY::CQueryParams param;
221 XFILE::MUSICDATABASEDIRECTORY::CDirectoryNode::GetDatabaseInfo(pItem->GetPath(),param);
222 CSong song;
223 if (m_musicDatabase.GetSong(param.GetSongId(), song))
225 pItem->GetMusicInfoTag()->SetSong(song);
226 if (!song.strThumb.empty())
227 pItem->SetArt("thumb", song.strThumb);
230 else if (CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(
231 CSettings::SETTING_MUSICFILES_USETAGS) ||
232 MUSIC::IsCDDA(*pItem))
233 { // Nothing found, load tag from file,
234 // always try to load cddb info
235 // get correct tag parser
236 std::unique_ptr<IMusicInfoTagLoader> pLoader (CMusicInfoTagLoaderFactory::CreateLoader(*pItem));
237 if (nullptr != pLoader)
238 // get tag
239 pLoader->Load(pItem->GetPath(), *pItem->GetMusicInfoTag());
240 m_tagReads++;
243 m_strPrevPath = strPath;
247 return true;
250 void CMusicInfoLoader::OnLoaderFinish()
252 // cleanup last loaded songs from database
253 m_songsMap.clear();
255 // cleanup cache loaded from HD
256 m_mapFileItems->Clear();
258 // Save loaded items to HD
259 if (!m_strCacheFileName.empty())
260 SaveCache(m_strCacheFileName, *m_pVecItems);
261 else if (!m_bStop && (m_databaseHits > 1 || m_tagReads > 0))
262 m_pVecItems->Save();
264 m_musicDatabase.Close();
266 if (m_thumbLoader)
267 m_thumbLoader->OnLoaderFinish();
270 void CMusicInfoLoader::UseCacheOnHD(const std::string& strFileName)
272 m_strCacheFileName = strFileName;
275 void CMusicInfoLoader::LoadCache(const std::string& strFileName, CFileItemList& items)
277 CFile file;
279 if (file.Open(strFileName))
281 CArchive ar(&file, CArchive::load);
282 int iSize = 0;
283 ar >> iSize;
284 for (int i = 0; i < iSize; i++)
286 CFileItemPtr pItem(new CFileItem());
287 ar >> *pItem;
288 items.Add(pItem);
290 ar.Close();
291 file.Close();
292 items.SetFastLookup(true);
296 void CMusicInfoLoader::SaveCache(const std::string& strFileName, CFileItemList& items)
298 int iSize = items.Size();
300 if (iSize <= 0)
301 return ;
303 CFile file;
305 if (file.OpenForWrite(strFileName))
307 CArchive ar(&file, CArchive::store);
308 ar << items.Size();
309 for (int i = 0; i < iSize; i++)
311 CFileItemPtr pItem = items[i];
312 ar << *pItem;
314 ar.Close();
315 file.Close();