input/despotify: convert to class

This commit is contained in:
Max Kellermann 2013-12-14 13:49:56 +01:00
parent 8297563978
commit 26c731a382

View File

@ -36,7 +36,7 @@ extern "C" {
#include <stdio.h> #include <stdio.h>
struct DespotifyInputStream { class DespotifyInputStream {
InputStream base; InputStream base;
struct despotify_session *session; struct despotify_session *session;
@ -62,30 +62,52 @@ struct DespotifyInputStream {
base.ready = true; base.ready = true;
} }
public:
~DespotifyInputStream() { ~DespotifyInputStream() {
delete tag; delete tag;
despotify_free_track(track); despotify_free_track(track);
} }
static InputStream *Open(const char *url, Mutex &mutex, Cond &cond,
Error &error);
bool IsEOF() const {
return eof;
}
size_t Read(void *ptr, size_t size, Error &error);
Tag *ReadTag() {
Tag *result = tag;
tag = nullptr;
return result;
}
void Callback(int sig);
private:
void FillBuffer();
}; };
static void inline void
refill_buffer(DespotifyInputStream *ctx) DespotifyInputStream::FillBuffer()
{ {
/* Wait until there is data */ /* Wait until there is data */
while (1) { while (1) {
int rc = despotify_get_pcm(ctx->session, &ctx->pcm); int rc = despotify_get_pcm(session, &pcm);
if (rc == 0 && ctx->pcm.len) { if (rc == 0 && pcm.len) {
ctx->len_available = ctx->pcm.len; len_available = pcm.len;
break; break;
} }
if (ctx->eof == true)
if (eof == true)
break; break;
if (rc < 0) { if (rc < 0) {
LogDebug(despotify_domain, "despotify_get_pcm error"); LogDebug(despotify_domain, "despotify_get_pcm error");
ctx->eof = true; eof = true;
break; break;
} }
@ -94,11 +116,9 @@ refill_buffer(DespotifyInputStream *ctx)
} }
} }
static void callback(gcc_unused struct despotify_session* ds, inline void
int sig, gcc_unused void* data, void* callback_data) DespotifyInputStream::Callback(int sig)
{ {
DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data;
switch (sig) { switch (sig) {
case DESPOTIFY_NEW_TRACK: case DESPOTIFY_NEW_TRACK:
break; break;
@ -108,35 +128,38 @@ static void callback(gcc_unused struct despotify_session* ds,
case DESPOTIFY_TRACK_PLAY_ERROR: case DESPOTIFY_TRACK_PLAY_ERROR:
LogWarning(despotify_domain, "Track play error"); LogWarning(despotify_domain, "Track play error");
ctx->eof = true; eof = true;
ctx->len_available = 0; len_available = 0;
break; break;
case DESPOTIFY_END_OF_PLAYLIST: case DESPOTIFY_END_OF_PLAYLIST:
ctx->eof = true; eof = true;
FormatDebug(despotify_domain, "End of playlist: %d", ctx->eof); FormatDebug(despotify_domain, "End of playlist: %d", eof);
break; break;
} }
} }
static void callback(gcc_unused struct despotify_session* ds,
static InputStream * int sig, gcc_unused void* data, void* callback_data)
input_despotify_open(const char *url,
Mutex &mutex, Cond &cond,
gcc_unused Error &error)
{ {
struct despotify_session *session; DespotifyInputStream *ctx = (DespotifyInputStream *)callback_data;
struct ds_link *ds_link;
struct ds_track *track;
ctx->Callback(sig);
}
inline InputStream *
DespotifyInputStream::Open(const char *url,
Mutex &mutex, Cond &cond,
gcc_unused Error &error)
{
if (!StringStartsWith(url, "spt://")) if (!StringStartsWith(url, "spt://"))
return nullptr; return nullptr;
session = mpd_despotify_get_session(); despotify_session *session = mpd_despotify_get_session();
if (!session) if (session == nullptr)
return nullptr; return nullptr;
ds_link = despotify_link_from_uri(url + 6); ds_link *ds_link = despotify_link_from_uri(url + 6);
if (!ds_link) { if (!ds_link) {
FormatDebug(despotify_domain, "Can't find %s", url); FormatDebug(despotify_domain, "Can't find %s", url);
return nullptr; return nullptr;
@ -146,7 +169,7 @@ input_despotify_open(const char *url,
return nullptr; return nullptr;
} }
track = despotify_link_get_track(session, ds_link); ds_track *track = despotify_link_get_track(session, ds_link);
despotify_free_link(ds_link); despotify_free_link(ds_link);
if (!track) if (!track)
return nullptr; return nullptr;
@ -169,26 +192,34 @@ input_despotify_open(const char *url,
return &ctx->base; return &ctx->base;
} }
static size_t static InputStream *
input_despotify_read(InputStream *is, void *ptr, size_t size, input_despotify_open(const char *url, Mutex &mutex, Cond &cond, Error &error)
gcc_unused Error &error)
{ {
DespotifyInputStream *ctx = (DespotifyInputStream *)is; return DespotifyInputStream::Open(url, mutex, cond, error);
size_t to_cpy = size; }
if (ctx->len_available == 0) inline size_t
refill_buffer(ctx); DespotifyInputStream::Read(void *ptr, size_t size, gcc_unused Error &error)
{
if (len_available == 0)
FillBuffer();
if (ctx->len_available < size) size_t to_cpy = std::min(size, len_available);
to_cpy = ctx->len_available; memcpy(ptr, pcm.buf, to_cpy);
memcpy(ptr, ctx->pcm.buf, to_cpy); len_available -= to_cpy;
ctx->len_available -= to_cpy;
is->offset += to_cpy; base.offset += to_cpy;
return to_cpy; return to_cpy;
} }
static size_t
input_despotify_read(InputStream *is, void *ptr, size_t size, Error &error)
{
DespotifyInputStream *ctx = (DespotifyInputStream *)is;
return ctx->Read(ptr, size, error);
}
static void static void
input_despotify_close(InputStream *is) input_despotify_close(InputStream *is)
{ {
@ -203,18 +234,15 @@ input_despotify_eof(InputStream *is)
{ {
DespotifyInputStream *ctx = (DespotifyInputStream *)is; DespotifyInputStream *ctx = (DespotifyInputStream *)is;
return ctx->eof; return ctx->IsEOF();
} }
static Tag * static Tag *
input_despotify_tag(InputStream *is) input_despotify_tag(InputStream *is)
{ {
DespotifyInputStream *ctx = (DespotifyInputStream *)is; DespotifyInputStream *ctx = (DespotifyInputStream *)is;
Tag *tag = ctx->tag;
ctx->tag = nullptr; return ctx->ReadTag();
return tag;
} }
const InputPlugin input_plugin_despotify = { const InputPlugin input_plugin_despotify = {