1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
#include <mtp/metadata/Library.h>
#include <mtp/ptp/Session.h>
#include <mtp/ptp/ObjectPropertyListParser.h>
#include <mtp/log.h>
#include <algorithm>
#include <unordered_map>
namespace mtp
{
namespace
{
const std::string UknownArtist ("UknownArtist");
const std::string UknownAlbum ("UknownAlbum");
const std::string VariousArtists ("VariousArtists");
}
Library::NameToObjectIdMap Library::ListAssociations(ObjectId parentId)
{
NameToObjectIdMap list;
ByteArray data = _session->GetObjectPropertyList(parentId, ObjectFormat::Association, ObjectProperty::ObjectFilename, 0, 1);
ObjectPropertyListParser<std::string> parser;
parser.Parse(data, [&](ObjectId id, ObjectProperty property, const std::string &name) {
list.insert(std::make_pair(name, id));
});
return list;
}
ObjectId Library::GetOrCreate(ObjectId parentId, const std::string &name)
{
auto objects = _session->GetObjectHandles(_storage, mtp::ObjectFormat::Association, parentId);
for (auto id : objects.ObjectHandles)
{
auto oname = _session->GetObjectStringProperty(id, ObjectProperty::ObjectFilename);
if (name == oname)
return id;
}
return _session->CreateDirectory(name, parentId, _storage).ObjectId;
}
Library::Library(const mtp::SessionPtr & session, ProgressReporter && reporter): _session(session)
{
auto storages = _session->GetStorageIDs();
if (storages.StorageIDs.empty())
throw std::runtime_error("no storages found");
u64 progress = 0, total = 0;
if (reporter)
reporter(State::Initialising, progress, total);
_artistSupported = _session->GetDeviceInfo().Supports(ObjectFormat::Artist);
debug("device supports ObjectFormat::Artist: ", _artistSupported? "yes": "no");
{
auto propsSupported = _session->GetObjectPropertiesSupported(ObjectFormat::AbstractAudioAlbum);
_albumDateAuthoredSupported = propsSupported.Supports(ObjectProperty::DateAuthored);
_albumCoverSupported = propsSupported.Supports(ObjectProperty::RepresentativeSampleData);
mtp::debug("abstract album supports date authored: ", _albumDateAuthoredSupported, ", cover: ", _albumCoverSupported);
}
_storage = storages.StorageIDs[0]; //picking up first storage.
//zune fails to create artist/album without storage id
{
ByteArray data = _session->GetObjectPropertyList(Session::Root, ObjectFormat::Association, ObjectProperty::ObjectFilename, 0, 1);
ObjectStringPropertyListParser::Parse(data, [&](ObjectId id, ObjectProperty property, const std::string &name)
{
if (name == "Artists")
_artistsFolder = id;
else if (name == "Albums")
_albumsFolder = id;
else if (name == "Music")
_musicFolder = id;
});
}
if (_artistSupported && _artistsFolder == ObjectId())
_artistsFolder = _session->CreateDirectory("Artists", Session::Root, _storage).ObjectId;
if (_albumsFolder == ObjectId())
_albumsFolder = _session->CreateDirectory("Albums", Session::Root, _storage).ObjectId;
if (_musicFolder == ObjectId())
_musicFolder = _session->CreateDirectory("Music", Session::Root, _storage).ObjectId;
debug("artists folder: ", _artistsFolder != ObjectId()? _artistsFolder.Id: 0);
debug("albums folder: ", _albumsFolder.Id);
debug("music folder: ", _musicFolder.Id);
auto musicFolders = ListAssociations(_musicFolder);
using namespace mtp;
ByteArray artists, albums;
if (_artistSupported)
{
debug("getting artists...");
if (reporter)
reporter(State::QueryingArtists, progress, total);
artists = _session->GetObjectPropertyList(Session::Root, mtp::ObjectFormat::Artist, mtp::ObjectProperty::Name, 0, 1);
HexDump("artists", artists);
total += ObjectStringPropertyListParser::GetSize(artists);
}
{
debug("getting albums...");
if (reporter)
reporter(State::QueryingAlbums, progress, total);
albums = _session->GetObjectPropertyList(Session::Root, mtp::ObjectFormat::AbstractAudioAlbum, mtp::ObjectProperty::Name, 0, 1);
HexDump("albums", artists);
total += ObjectStringPropertyListParser::GetSize(albums);
}
if (_artistSupported)
{
if (reporter)
reporter(State::LoadingArtists, progress, total);
ObjectStringPropertyListParser::Parse(artists, [&](ObjectId id, ObjectProperty property, const std::string &name)
{
debug("artist: ", name, "\t", id.Id);
auto artist = std::make_shared<Artist>();
artist->Id = id;
artist->Name = name;
auto it = musicFolders.find(name);
if (it != musicFolders.end())
artist->MusicFolderId = it->second;
else
artist->MusicFolderId = _session->CreateDirectory(name, _musicFolder, _storage).ObjectId;
_artists.insert(std::make_pair(name, artist));
if (reporter)
reporter(State::LoadingArtists, ++progress, total);
});
}
if (reporter)
reporter(State::LoadingAlbums, progress, total);
std::unordered_map<ArtistPtr, NameToObjectIdMap> albumFolders;
ObjectStringPropertyListParser::Parse(albums, [&](ObjectId id, ObjectProperty property, const std::string &name)
{
auto artistName = _session->GetObjectStringProperty(id, ObjectProperty::Artist);
std::string albumDate;
if (_albumDateAuthoredSupported)
albumDate = _session->GetObjectStringProperty(id, ObjectProperty::DateAuthored);
auto artist = GetArtist(artistName);
if (!artist)
artist = CreateArtist(artistName);
debug("album: ", artistName, " -- ", name, "\t", id.Id, "\t", albumDate);
auto album = std::make_shared<Album>();
album->Name = name;
album->Artist = artist;
album->Id = id;
album->Year = !albumDate.empty()? ConvertDateTime(albumDate): 0;
if (albumFolders.find(artist) == albumFolders.end()) {
albumFolders[artist] = ListAssociations(artist->MusicFolderId);
}
auto it = albumFolders.find(artist);
if (it == albumFolders.end())
throw std::runtime_error("no iterator after insert, internal error");
const auto & albums = it->second;
auto alit = albums.find(name);
if (alit != albums.end())
album->MusicFolderId = alit->second;
else
album->MusicFolderId = _session->CreateDirectory(name, artist->MusicFolderId, _storage).ObjectId;
_albums.insert(std::make_pair(std::make_pair(artist, name), album));
if (reporter)
reporter(State::LoadingAlbums, ++progress, total);
});
if (reporter)
reporter(State::Loaded, progress, total);
}
Library::~Library()
{ }
Library::ArtistPtr Library::GetArtist(std::string name)
{
if (name.empty())
name = UknownArtist;
auto it = _artists.find(name);
return it != _artists.end()? it->second: ArtistPtr();
}
Library::ArtistPtr Library::CreateArtist(std::string name)
{
if (name.empty())
name = UknownArtist;
auto artist = std::make_shared<Artist>();
artist->Name = name;
artist->MusicFolderId = GetOrCreate(_musicFolder, name);
if (_artistSupported)
{
ByteArray propList;
OutputStream os(propList);
os.Write32(2); //number of props
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Name));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(name);
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::ObjectFilename));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(name + ".art");
auto response = _session->SendObjectPropList(_storage, _artistsFolder, ObjectFormat::Artist, 0, propList);
artist->Id = response.ObjectId;
}
_artists.insert(std::make_pair(name, artist));
return artist;
}
Library::AlbumPtr Library::GetAlbum(const ArtistPtr & artist, std::string name)
{
if (name.empty())
name = UknownAlbum;
auto it = _albums.find(std::make_pair(artist, name));
return it != _albums.end()? it->second: AlbumPtr();
}
Library::AlbumPtr Library::CreateAlbum(const ArtistPtr & artist, std::string name, int year)
{
if (!artist)
throw std::runtime_error("artists is required");
if (name.empty())
name = UknownAlbum;
ByteArray propList;
OutputStream os(propList);
bool sendYear = year != 0 && _albumDateAuthoredSupported;
os.Write32(3 + (sendYear? 1: 0)); //number of props
if (_artistSupported)
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::ArtistId));
os.Write16(static_cast<u16>(DataTypeCode::Uint32));
os.Write32(artist->Id.Id);
}
else
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Artist));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(artist->Name);
}
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Name));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(name);
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::ObjectFilename));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(artist->Name + "--" + name + ".alb");
if (sendYear)
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::DateAuthored));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(ConvertYear(year));
}
auto album = std::make_shared<Album>();
album->Artist = artist;
album->Name = name;
album->Year = year;
album->MusicFolderId = GetOrCreate(artist->MusicFolderId, name);
auto response = _session->SendObjectPropList(_storage, _albumsFolder, ObjectFormat::AbstractAudioAlbum, 0, propList);
album->Id = response.ObjectId;
_albums.insert(std::make_pair(std::make_pair(artist, name), album));
return album;
}
bool Library::HasTrack(const AlbumPtr & album, const std::string &name, int trackIndex)
{
if (!album)
return false;
LoadRefs(album);
auto & tracks = album->Tracks;
auto range = tracks.equal_range(name);
for(auto i = range.first; i != range.second; ++i)
{
if (i->second == trackIndex)
return true;
}
return false;
}
Library::NewTrackInfo Library::CreateTrack(const ArtistPtr & artist,
const AlbumPtr & album,
ObjectFormat type,
std::string name, const std::string & genre, int trackIndex,
const std::string &filename, size_t size)
{
ByteArray propList;
OutputStream os(propList);
os.Write32(3 + (!genre.empty()? 1: 0) + (trackIndex? 1: 0)); //number of props
if (_artistSupported)
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::ArtistId));
os.Write16(static_cast<u16>(DataTypeCode::Uint32));
os.Write32(artist->Id.Id);
}
else
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Artist));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(artist->Name);
}
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Name));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(name);
if (trackIndex)
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Track));
os.Write16(static_cast<u16>(DataTypeCode::Uint16));
os.Write16(trackIndex);
}
if (!genre.empty())
{
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::Genre));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(genre);
}
os.Write32(0); //object handle
os.Write16(static_cast<u16>(ObjectProperty::ObjectFilename));
os.Write16(static_cast<u16>(DataTypeCode::String));
os.WriteString(filename);
auto response = _session->SendObjectPropList(_storage, album->MusicFolderId, type, size, propList);
NewTrackInfo ti;
ti.Id = response.ObjectId;
ti.Name = name;
ti.Index = trackIndex;
return ti;
}
void Library::LoadRefs(AlbumPtr album)
{
if (!album || album->RefsLoaded)
return;
auto refs = _session->GetObjectReferences(album->Id).ObjectHandles;
std::copy(refs.begin(), refs.end(), std::inserter(album->Refs, album->Refs.begin()));
for(auto trackId : refs)
{
auto name = _session->GetObjectStringProperty(trackId, ObjectProperty::Name);
auto index = _session->GetObjectIntegerProperty(trackId, ObjectProperty::Track);
debug("[", index, "]: ", name);
album->Tracks.insert(std::make_pair(name, index));
}
album->RefsLoaded = true;
}
void Library::AddTrack(AlbumPtr album, const NewTrackInfo & ti)
{
if (!album)
return;
LoadRefs(album);
auto & refs = album->Refs;
auto & tracks = album->Tracks;
msg::ObjectHandles handles;
std::copy(refs.begin(), refs.end(), std::back_inserter(handles.ObjectHandles));
handles.ObjectHandles.push_back(ti.Id);
_session->SetObjectReferences(album->Id, handles);
refs.insert(ti.Id);
tracks.insert(std::make_pair(ti.Name, ti.Index));
}
void Library::AddCover(AlbumPtr album, const mtp::ByteArray &data)
{
if (!album || !_albumCoverSupported)
return;
mtp::debug("sending ", data.size(), " bytes of album cover...");
_session->SetObjectPropertyAsArray(album->Id, mtp::ObjectProperty::RepresentativeSampleData, data);
}
bool Library::Supported(const mtp::SessionPtr & session)
{
auto & gdi = session->GetDeviceInfo();
return
gdi.Supports(OperationCode::GetObjectPropList) &&
gdi.Supports(OperationCode::SendObjectPropList) &&
gdi.Supports(OperationCode::SetObjectReferences) &&
gdi.Supports(ObjectFormat::AbstractAudioAlbum);
;
}
}
|