diff options
Diffstat (limited to 'src/db/plugins/upnp')
-rw-r--r-- | src/db/plugins/upnp/ContentDirectoryService.cxx | 204 | ||||
-rw-r--r-- | src/db/plugins/upnp/Directory.cxx | 262 | ||||
-rw-r--r-- | src/db/plugins/upnp/Directory.hxx | 66 | ||||
-rw-r--r-- | src/db/plugins/upnp/Object.cxx | 25 | ||||
-rw-r--r-- | src/db/plugins/upnp/Object.hxx | 85 | ||||
-rw-r--r-- | src/db/plugins/upnp/Tags.cxx | 33 | ||||
-rw-r--r-- | src/db/plugins/upnp/Tags.hxx | 28 |
7 files changed, 703 insertions, 0 deletions
diff --git a/src/db/plugins/upnp/ContentDirectoryService.cxx b/src/db/plugins/upnp/ContentDirectoryService.cxx new file mode 100644 index 000000000..c097f7644 --- /dev/null +++ b/src/db/plugins/upnp/ContentDirectoryService.cxx @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "lib/upnp/ContentDirectoryService.hxx" +#include "lib/upnp/Domain.hxx" +#include "lib/upnp/ixmlwrap.hxx" +#include "lib/upnp/Action.hxx" +#include "Directory.hxx" +#include "util/NumberParser.hxx" +#include "util/Error.hxx" + +#include <stdio.h> + +static bool +ReadResultTag(UPnPDirContent &dirbuf, IXML_Document *response, Error &error) +{ + const char *p = ixmlwrap::getFirstElementValue(response, "Result"); + if (p == nullptr) + p = ""; + + return dirbuf.parse(p, error); +} + +inline bool +ContentDirectoryService::readDirSlice(UpnpClient_Handle hdl, + const char *objectId, unsigned offset, + unsigned count, UPnPDirContent &dirbuf, + unsigned &didreadp, unsigned &totalp, + Error &error) const +{ + // Create request + char ofbuf[100], cntbuf[100]; + sprintf(ofbuf, "%u", offset); + sprintf(cntbuf, "%u", count); + // Some devices require an empty SortCriteria, else bad params + IXML_Document *request = + MakeActionHelper("Browse", m_serviceType.c_str(), + "ObjectID", objectId, + "BrowseFlag", "BrowseDirectChildren", + "Filter", "*", + "SortCriteria", "", + "StartingIndex", ofbuf, + "RequestedCount", cntbuf); + if (request == nullptr) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + int code = UpnpSendAction(hdl, m_actionURL.c_str(), m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + const char *value = ixmlwrap::getFirstElementValue(response, "NumberReturned"); + didreadp = value != nullptr + ? ParseUnsigned(value) + : 0; + + value = ixmlwrap::getFirstElementValue(response, "TotalMatches"); + if (value != nullptr) + totalp = ParseUnsigned(value); + + bool success = ReadResultTag(dirbuf, response, error); + ixmlDocument_free(response); + return success; +} + +bool +ContentDirectoryService::readDir(UpnpClient_Handle handle, + const char *objectId, + UPnPDirContent &dirbuf, + Error &error) const +{ + unsigned offset = 0, total = -1, count; + + do { + if (!readDirSlice(handle, objectId, offset, m_rdreqcnt, dirbuf, + count, total, error)) + return false; + + offset += count; + } while (count > 0 && offset < total); + + return true; +} + +bool +ContentDirectoryService::search(UpnpClient_Handle hdl, + const char *objectId, + const char *ss, + UPnPDirContent &dirbuf, + Error &error) const +{ + unsigned offset = 0, total = -1, count; + + do { + char ofbuf[100]; + sprintf(ofbuf, "%d", offset); + + IXML_Document *request = + MakeActionHelper("Search", m_serviceType.c_str(), + "ContainerID", objectId, + "SearchCriteria", ss, + "Filter", "*", + "SortCriteria", "", + "StartingIndex", ofbuf, + "RequestedCount", "0"); // Setting a value here gets twonky into fits + if (request == 0) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + auto code = UpnpSendAction(hdl, m_actionURL.c_str(), + m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + const char *value = + ixmlwrap::getFirstElementValue(response, "NumberReturned"); + count = value != nullptr + ? ParseUnsigned(value) + : 0; + + offset += count; + + value = ixmlwrap::getFirstElementValue(response, "TotalMatches"); + if (value != nullptr) + total = ParseUnsigned(value); + + bool success = ReadResultTag(dirbuf, response, error); + ixmlDocument_free(response); + if (!success) + return false; + } while (count > 0 && offset < total); + + return true; +} + +bool +ContentDirectoryService::getMetadata(UpnpClient_Handle hdl, + const char *objectId, + UPnPDirContent &dirbuf, + Error &error) const +{ + // Create request + IXML_Document *request = + MakeActionHelper("Browse", m_serviceType.c_str(), + "ObjectID", objectId, + "BrowseFlag", "BrowseMetadata", + "Filter", "*", + "SortCriteria", "", + "StartingIndex", "0", + "RequestedCount", "1"); + if (request == nullptr) { + error.Set(upnp_domain, "UpnpMakeAction() failed"); + return false; + } + + IXML_Document *response; + auto code = UpnpSendAction(hdl, m_actionURL.c_str(), + m_serviceType.c_str(), + 0 /*devUDN*/, request, &response); + ixmlDocument_free(request); + if (code != UPNP_E_SUCCESS) { + error.Format(upnp_domain, code, + "UpnpSendAction() failed: %s", + UpnpGetErrorMessage(code)); + return false; + } + + bool success = ReadResultTag(dirbuf, response, error); + ixmlDocument_free(response); + return success; +} diff --git a/src/db/plugins/upnp/Directory.cxx b/src/db/plugins/upnp/Directory.cxx new file mode 100644 index 000000000..e43cd48a6 --- /dev/null +++ b/src/db/plugins/upnp/Directory.cxx @@ -0,0 +1,262 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Directory.hxx" +#include "lib/upnp/Util.hxx" +#include "lib/expat/ExpatParser.hxx" +#include "Tags.hxx" +#include "tag/TagBuilder.hxx" +#include "tag/TagTable.hxx" +#include "util/NumberParser.hxx" + +#include <algorithm> +#include <string> + +#include <string.h> + +UPnPDirContent::~UPnPDirContent() +{ + /* this destructor exists here just so it won't get inlined */ +} + +gcc_pure gcc_nonnull_all +static bool +CompareStringLiteral(const char *literal, const char *value, size_t length) +{ + return length == strlen(literal) && + memcmp(literal, value, length) == 0; +} + +gcc_pure +static UPnPDirObject::ItemClass +ParseItemClass(const char *name, size_t length) +{ + if (CompareStringLiteral("object.item.audioItem.musicTrack", + name, length)) + return UPnPDirObject::ItemClass::MUSIC; + else if (CompareStringLiteral("object.item.playlistItem", + name, length)) + return UPnPDirObject::ItemClass::PLAYLIST; + else + return UPnPDirObject::ItemClass::UNKNOWN; +} + +gcc_pure +static int +ParseDuration(const char *duration) +{ + char *endptr; + + unsigned result = ParseUnsigned(duration, &endptr); + if (endptr == duration || *endptr != ':') + return 0; + + result *= 60; + duration = endptr + 1; + result += ParseUnsigned(duration, &endptr); + if (endptr == duration || *endptr != ':') + return 0; + + result *= 60; + duration = endptr + 1; + result += ParseUnsigned(duration, &endptr); + if (endptr == duration || *endptr != 0) + return 0; + + return result; +} + +/** + * Transform titles to turn '/' into '_' to make them acceptable path + * elements. There is a very slight risk of collision in doing + * this. Twonky returns directory names (titles) like 'Artist/Album'. + */ +gcc_pure +static std::string +titleToPathElt(std::string &&s) +{ + std::replace(s.begin(), s.end(), '/', '_'); + return s; +} + +/** + * An XML parser which builds directory contents from DIDL lite input. + */ +class UPnPDirParser final : public CommonExpatParser { + UPnPDirContent &m_dir; + + enum { + NONE, + RES, + CLASS, + } state; + + /** + * If not equal to #TAG_NUM_OF_ITEM_TYPES, then we're + * currently reading an element containing a tag value. The + * value is being constructed in #value. + */ + TagType tag_type; + + /** + * The text inside the current element. + */ + std::string value; + + UPnPDirObject m_tobj; + TagBuilder tag; + +public: + UPnPDirParser(UPnPDirContent& dir) + :m_dir(dir), + state(NONE), + tag_type(TAG_NUM_OF_ITEM_TYPES) + { + } + +protected: + virtual void StartElement(const XML_Char *name, const XML_Char **attrs) + { + if (m_tobj.type != UPnPDirObject::Type::UNKNOWN && + tag_type == TAG_NUM_OF_ITEM_TYPES) { + tag_type = tag_table_lookup(upnp_tags, name); + if (tag_type != TAG_NUM_OF_ITEM_TYPES) + return; + } else { + assert(tag_type == TAG_NUM_OF_ITEM_TYPES); + } + + switch (name[0]) { + case 'c': + if (!strcmp(name, "container")) { + m_tobj.clear(); + m_tobj.type = UPnPDirObject::Type::CONTAINER; + + const char *id = GetAttribute(attrs, "id"); + if (id != nullptr) + m_tobj.m_id = id; + + const char *pid = GetAttribute(attrs, "parentID"); + if (pid != nullptr) + m_tobj.m_pid = pid; + } + break; + + case 'i': + if (!strcmp(name, "item")) { + m_tobj.clear(); + m_tobj.type = UPnPDirObject::Type::ITEM; + + const char *id = GetAttribute(attrs, "id"); + if (id != nullptr) + m_tobj.m_id = id; + + const char *pid = GetAttribute(attrs, "parentID"); + if (pid != nullptr) + m_tobj.m_pid = pid; + } + break; + + case 'r': + if (!strcmp(name, "res")) { + // <res protocolInfo="http-get:*:audio/mpeg:*" size="5171496" + // bitrate="24576" duration="00:03:35" sampleFrequency="44100" + // nrAudioChannels="2"> + + const char *duration = + GetAttribute(attrs, "duration"); + if (duration != nullptr) + tag.SetTime(ParseDuration(duration)); + + state = RES; + } + + break; + + case 'u': + if (strcmp(name, "upnp:class") == 0) + state = CLASS; + } + } + + bool checkobjok() { + if (m_tobj.m_id.empty() || m_tobj.m_pid.empty() || + m_tobj.name.empty() || + (m_tobj.type == UPnPDirObject::Type::ITEM && + m_tobj.item_class == UPnPDirObject::ItemClass::UNKNOWN)) + return false; + + return true; + } + + virtual void EndElement(const XML_Char *name) + { + if (tag_type != TAG_NUM_OF_ITEM_TYPES) { + assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN); + + tag.AddItem(tag_type, value.c_str()); + + if (tag_type == TAG_TITLE) + m_tobj.name = titleToPathElt(std::move(value)); + + value.clear(); + tag_type = TAG_NUM_OF_ITEM_TYPES; + return; + } + + if ((!strcmp(name, "container") || !strcmp(name, "item")) && + checkobjok()) { + tag.Commit(m_tobj.tag); + m_dir.objects.emplace_back(std::move(m_tobj)); + } + + state = NONE; + } + + virtual void CharacterData(const XML_Char *s, int len) + { + if (tag_type != TAG_NUM_OF_ITEM_TYPES) { + assert(m_tobj.type != UPnPDirObject::Type::UNKNOWN); + + value.append(s, len); + return; + } + + switch (state) { + case NONE: + break; + + case RES: + m_tobj.url.assign(s, len); + break; + + case CLASS: + m_tobj.item_class = ParseItemClass(s, len); + break; + } + } +}; + +bool +UPnPDirContent::parse(const char *input, Error &error) +{ + UPnPDirParser parser(*this); + return parser.Parse(input, strlen(input), true, error); +} diff --git a/src/db/plugins/upnp/Directory.hxx b/src/db/plugins/upnp/Directory.hxx new file mode 100644 index 000000000..433979900 --- /dev/null +++ b/src/db/plugins/upnp/Directory.hxx @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_DIRECTORY_HXX +#define MPD_UPNP_DIRECTORY_HXX + +#include "Object.hxx" +#include "Compiler.h" + +#include <string> +#include <vector> + +class Error; + +/** + * Image of a MediaServer Directory Service container (directory), + * possibly containing items and subordinate containers. + */ +class UPnPDirContent { +public: + std::vector<UPnPDirObject> objects; + + ~UPnPDirContent(); + + gcc_pure + UPnPDirObject *FindObject(const char *name) { + for (auto &o : objects) + if (o.name == name) + return &o; + + return nullptr; + } + + /** + * Parse from DIDL-Lite XML data. + * + * Normally only used by ContentDirectoryService::readDir() + * This is cumulative: in general, the XML data is obtained in + * several documents corresponding to (offset,count) slices of the + * directory (container). parse() can be called repeatedly with + * the successive XML documents and will accumulate entries in the item + * and container vectors. This makes more sense if the different + * chunks are from the same container, but given that UPnP Ids are + * actually global, nothing really bad will happen if you mix + * up... + */ + bool parse(const char *didltext, Error &error); +}; + +#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Object.cxx b/src/db/plugins/upnp/Object.cxx new file mode 100644 index 000000000..703fb0be4 --- /dev/null +++ b/src/db/plugins/upnp/Object.cxx @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Object.hxx" + +UPnPDirObject::~UPnPDirObject() +{ + /* this destructor exists here just so it won't get inlined */ +} diff --git a/src/db/plugins/upnp/Object.hxx b/src/db/plugins/upnp/Object.hxx new file mode 100644 index 000000000..16a66c774 --- /dev/null +++ b/src/db/plugins/upnp/Object.hxx @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_OBJECT_HXX +#define MPD_UPNP_OBJECT_HXX + +#include "tag/Tag.hxx" + +#include <string> + +/** + * UpnP Media Server directory entry, converted from XML data. + * + * This is a dumb data holder class, a struct with helpers. + */ +class UPnPDirObject { +public: + enum class Type { + UNKNOWN, + ITEM, + CONTAINER, + }; + + // There are actually several kinds of containers: + // object.container.storageFolder, object.container.person, + // object.container.playlistContainer etc., but they all seem to + // behave the same as far as we're concerned. Otoh, musicTrack + // items are special to us, and so should playlists, but I've not + // seen one of the latter yet (servers seem to use containers for + // playlists). + enum class ItemClass { + UNKNOWN, + MUSIC, + PLAYLIST, + }; + + std::string m_id; // ObjectId + std::string m_pid; // Parent ObjectId + std::string url; + + /** + * A copy of "dc:title" sanitized as a file name. + */ + std::string name; + + Type type; + ItemClass item_class; + + Tag tag; + + UPnPDirObject() = default; + UPnPDirObject(UPnPDirObject &&) = default; + + ~UPnPDirObject(); + + UPnPDirObject &operator=(UPnPDirObject &&) = default; + + void clear() + { + m_id.clear(); + m_pid.clear(); + url.clear(); + type = Type::UNKNOWN; + item_class = ItemClass::UNKNOWN; + tag.Clear(); + } +}; + +#endif /* _UPNPDIRCONTENT_H_X_INCLUDED_ */ diff --git a/src/db/plugins/upnp/Tags.cxx b/src/db/plugins/upnp/Tags.cxx new file mode 100644 index 000000000..fd65df4d0 --- /dev/null +++ b/src/db/plugins/upnp/Tags.cxx @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#include "Tags.hxx" +#include "tag/TagTable.hxx" + +const struct tag_table upnp_tags[] = { + { "upnp:artist", TAG_ARTIST }, + { "upnp:album", TAG_ALBUM }, + { "upnp:originalTrackNumber", TAG_TRACK }, + { "upnp:genre", TAG_GENRE }, + { "dc:title", TAG_TITLE }, + + /* sentinel */ + { nullptr, TAG_NUM_OF_ITEM_TYPES } +}; diff --git a/src/db/plugins/upnp/Tags.hxx b/src/db/plugins/upnp/Tags.hxx new file mode 100644 index 000000000..ec6d18478 --- /dev/null +++ b/src/db/plugins/upnp/Tags.hxx @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2003-2014 The Music Player Daemon Project + * http://www.musicpd.org + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_UPNP_TAGS_HXX +#define MPD_UPNP_TAGS_HXX + +/** + * Map UPnP property names to MPD tags. + */ +extern const struct tag_table upnp_tags[]; + +#endif |