RJ / playdar-core

Playdar - a content resolver for music

This URL has Read+Write access

RJ (author)
Sun Nov 08 14:19:00 -0800 2009
commit  4a5c9634f046d83901864f64f922e201d88edc0b
tree    0fd7fb2e4b442ccfdf1b5afa21bd9cd6c199e456
parent  ad68b8bdad09482d0a2245ac1b19e3dcc150b00e
name age message
file .gitignore Thu Nov 05 05:02:06 -0800 2009 add VS2008 express solution for building the ta... [RJ]
file LICENSE.txt Sun Oct 04 10:55:22 -0700 2009 add license file, using MIT [RJ]
file Makefile Sat Oct 24 08:59:31 -0700 2009 scanner on windows fixes from tobbez, change ma... [unknown]
file README Sat Oct 24 09:01:54 -0700 2009 update readme re: building scanner [RJ]
directory contrib/ Loading commit data...
directory deps/ Thu Oct 08 05:14:27 -0700 2009 Neaten main directory structure Move mochiweb-... [Max Howell]
directory etc/ Sun Nov 08 14:19:00 -0800 2009 add db path location examples to conf file [RJ]
directory include/ Thu Oct 29 11:56:44 -0700 2009 * Simplify pipeline mechanics * Resolver module... [RJ]
directory playdar_modules/
file playdarctl Wed Oct 21 10:25:16 -0700 2009 fixes issue #5 [RJ]
directory priv/
directory src/ Tue Nov 03 12:36:24 -0800 2009 add gc fun to delete expired queries [RJ]
file start-dev.sh Thu Oct 15 06:56:48 -0700 2009 implement playdar_ctl and control script [RJ]
file start.sh Thu Oct 15 06:56:48 -0700 2009 implement playdar_ctl and control script [RJ]
README
Playdar
=======
Playdar is a music content resolver, written in Erlang, MIT licensed.
It's modular and can be extended using resolver scripts in any language.
It has an HTTP API to answer the question "can i play this song?".
If yes, you get a localhost URL that will play the song, from whichever souce
playdar resolved it to (plugins do the resolving).

For more information see: http://www.playdar.org/

Building
--------
To build all the erlang code:

    $ make

To build the collection scanner (needs taglib, g++):

    $ make scanner

Running
-------

    $ ./playdarctl start-debug

In another terminal you can now do:

    $ ./playdarctl numfiles
    $ ./playdarctl scan "/path/to/music"
    $ ./playdarctl numfiles

Then check out http://localhost:60210/ and http://www.playdar.org/demos/

Understanding Playdar
=====================

The core playdar daemon is a generic content-resolver engine.
It contains a simple http server (Mochiweb), a resolver infrastructure, external
module loader (plugins) and configuration management. 

Playdar core allows you to dispatch queries and check query status, but by 
itself it can't resolve content. This is all handled by plugins.

Clients talk to Playar using the HTTP API. JSON is used for everything.

Resolver Queries
----------------
All queries passed to Playdar are simply JSON objects of any shape or size. 
Playdar checks for a "qid" propery - a GUID for the query, in each object,
but nothing else - it's up to the plugins to decode the query objects.

If a plugin doesn't recognise a query it simply ignores it.

The following examples are similar in syntax to how Playdar is built, but may
not necessarily be exactly correct. They serve to illustrate the query
mechanics:

Music Query Example
-------------------
The query object:
{
    "qid" : "XXX123",
    "artist" : "Big Bad Sun",
    "track"  : "Sweet Melissa"
}

This is a simple query for a specific song. In this case, the default plugins
that ship with Playdar know how to handle it - because we're using Playdar as
a music content resolver. The library plugin will search your disk and respond
with a result if you have a copy of the song being searched for:
A result object:
{
    "sid" : "YYY456",
    "qid" : "XXX123",
    "result" : {
        "url" : "/path/to/sweet_melissa.mp3",
        "bitrate" : 128,
        "duration" : 283,
        "artist" : "Big Bad Sun",
        "track" : "Sweet Melissa (live)",
        "score" : 0.89
    }
}
In the the above example, the library plugin reports a result for the query 
with a score of 0.89 - it found a live version - but close enough.
The "sid" propery is a GUID for the result.

Resolvers can report multiple result objects for any query.

Non-music Example
-----------------
Depending on the plugins loaded, Playdar could be used to resolve anything.
Here's a fictional example query for an academic essay:
{
    "qid"    : "XXX789",
    "author" : "Jonathan swift",
    "year"   : 1729,
    "title"  : "a modest propsal"
}
... and if you have a resolver plugin that could search for essays and academic
papers, it might respond like this:
{
    "sid"    : "YYY123",
    "qid"    : "XXX789",
    "result" : {
        "author" : "Jonathan Swift",
        "year"   : 1729,
        "title"  : "A Modest Proposal",
        "score"  : 1.00,
        "category"   : "humour",
        "url"    : "http://art-bin.com/art/omodest.html"
    }
}

In this example, the resolver matched the query to an essay posted on the web
instead of the local filesystem. 

Custom Protocols
----------------
Playdar ships with support for reading content from http:// and file:// URLs.
That is to say support for those protocols is implemented by plugins that ship
by default.

A resolver could provide an url like this: "custom://abc123-modest-blah" and
register itself as a handler for custom:// urls. This allows resolver plugins
to fetch and stream content in any way they like. 
You could add support for ftp:// or a custom p2p:// protocol if you wanted.

How Clients Fetch Results
-------------------------
The actual URL to the resolved content, be it a local filesystem or http URL,
is never exposed to clients using the Playdar HTTP API. The "url" propery is
stripped out of result objects before they are returned to clients.
The "sid" GUID in every result object is used to request the content, like so:
 http://localhost:60210/sid/<sid>
Playdar will then fetch the content using the appropriate protocol handler
internally (file, http, p2p, custom, etc), and respond to the client with a 
well formed HTTP response. This means that clients only need to understand
HTTP. The details of how content is fetched by Playdar are hidden from clients.
Clients don't need to know where or how you access your music. 
The Playdar API lets them ask "Can you play this song". The details of where
and how you access the song are kept private.
To reiterate: clients can't tell whether you are playing a song you have on
your local disk, or are streaming over the internet from some other service.

How webpages interact with Playdar
----------------------------------
Desktop apps can obviously just use the HTTP API on localhost without any 
special considerations. Webapps and anything running in the browser is 
subject to the browser security model, specifically the "same-origin policy".
This means that you can't access content in a document loaded from a domain
different to the domain your javascript executes in.

To solve this problem, playdar uses JSONP callbacks. Instead of requesting
http://localhost:60210/api/?method=stat
and getting json in response, you inject a script tag into the dom like this:
<script language="javascript" 
        src="http://localhost:60210/api/?method=stat&jsonp=my_callback"/>
Playdar responds with the json object wrapped in your padding:
 my_callback(...normal json response...);
which then fires the my_callback function - voilia, cross domain RPC.