sad

simple audio daemon
git clone git://git.2f30.org/sad
Log | Files | Refs | LICENSE

commit 54c85525a22890837bc64080918ecc2aa2a79dc3
parent 48652b6c0cb314c72beab9029bd59598c74188b0
Author: sin <sin@2f30.org>
Date:   Fri, 26 Dec 2014 14:10:56 +0000

Refactor decoder again

Diffstat:
Mcmd.c | 17+++++++++--------
Mdecoder.c | 19+++++++------------
Mplaylist.c | 10++++++++--
Msad.c | 6+++---
Msad.h | 30+++++++++++++++---------------
5 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/cmd.c b/cmd.c @@ -50,11 +50,10 @@ cmdnext(int fd, int argc, char **argv) return; } - decoder->close(); + s->decoder->close(); s->state = NONE; next = getnextsong(); next->state = PREPARE; - setdecoder(next->path); putcursong(next); dprintf(fd, "OK\n"); } @@ -117,12 +116,11 @@ cmdplay(int fd, int argc, char **argv) cur = getcursong(); if (cur) { - decoder->close(); + cur->decoder->close(); cur->state = NONE; } s->state = PREPARE; - setdecoder(s->path); putcursong(s); printf("Song %s with %d playing\n", s->path, s->id); @@ -144,11 +142,10 @@ cmdprev(int fd, int argc, char **argv) return; } - decoder->close(); + s->decoder->close(); s->state = NONE; prev = getprevsong(); prev->state = PREPARE; - setdecoder(prev->path); putcursong(prev); dprintf(fd, "OK\n"); } @@ -168,7 +165,7 @@ cmdstop(int fd, int argc, char **argv) dprintf(fd, "ERR \"no song is active\"\n"); return; } - decoder->close(); + s->decoder->close(); s->state = NONE; dprintf(fd, "OK\n"); } @@ -187,6 +184,10 @@ cmdadd(int fd, int argc, char **argv) return; } s = addplaylist(argv[1]); + if (!s) { + dprintf(fd, "ERR \"cannot add %s\"\n", argv[1]); + return; + } printf("Added song with path %s and id %d\n", s->path, s->id); dprintf(fd, "OK\n"); @@ -204,7 +205,7 @@ cmdclear(int fd, int argc, char **argv) s = getcursong(); if (s) { - decoder->close(); + s->decoder->close(); s->state = NONE; } clearplaylist(); diff --git a/decoder.c b/decoder.c @@ -8,8 +8,6 @@ #include "sad.h" -Decoder *decoder; - static struct { char *ext; Decoder *decoder; @@ -29,20 +27,17 @@ initdecoders(void) return 0; } -int -setdecoder(const char *name) +Decoder * +matchdecoder(const char *name) { char *ext; int i; ext = strrchr(name, '.'); if (!ext) - return -1; - for (i = 0; i < LEN(Decodermap); i++) { - if (!strcasecmp(Decodermap[i].ext, ext)) { - decoder = Decodermap[i].decoder; - break; - } - } - return 0; + return NULL; + for (i = 0; i < LEN(Decodermap); i++) + if (!strcasecmp(Decodermap[i].ext, ext)) + return Decodermap[i].decoder; + return NULL; } diff --git a/playlist.c b/playlist.c @@ -20,8 +20,13 @@ initplaylist(void) Song * addplaylist(const char *path) { - Song *s; - Song **p; + Decoder *d; + Song *s; + Song **p; + + d = matchdecoder(path); + if (!d) + return NULL; if (!playlist.nsongs || playlist.nsongs + 1 > playlist.maxsongs) { playlist.maxsongs += 4096; @@ -37,6 +42,7 @@ addplaylist(const char *path) s->path[sizeof(s->path) - 1] = '\0'; s->id = rollingid++; s->state = 0; + s->decoder = d; playlist.nsongs++; return s; } diff --git a/sad.c b/sad.c @@ -74,15 +74,15 @@ doaudio(void) switch (s->state) { case PREPARE: - if (decoder->open(s->path) < 0) { + if (s->decoder->open(s->path) < 0) { s->state = NONE; return; } s->state = PLAYING; break; case PLAYING: - if ((nbytes = decoder->decode(buf, sizeof(buf))) <= 0) { - decoder->close(); + if ((nbytes = s->decoder->decode(buf, sizeof(buf))) <= 0) { + s->decoder->close(); s->state = NONE; } else { output->play(buf, nbytes); diff --git a/sad.h b/sad.h @@ -14,19 +14,6 @@ enum { }; typedef struct { - char path[PATH_MAX]; - int id; - int state; -} Song; - -typedef struct { - Song **songs; - Song *cursong; - size_t nsongs; - size_t maxsongs; -} Playlist; - -typedef struct { int (*init)(void); int (*open)(const char *); int (*decode)(void *, int); @@ -41,6 +28,20 @@ typedef struct { int (*close)(void); } Output; +typedef struct { + char path[PATH_MAX]; + int id; + int state; + Decoder *decoder; +} Song; + +typedef struct { + Song **songs; + Song *cursong; + size_t nsongs; + size_t maxsongs; +} Playlist; + /* sad.c */ extern fd_set master; extern fd_set rfds; @@ -76,6 +77,5 @@ int gettokens(char *, char **, int, char *); int tokenize(char *, char **, int); /* decoder.c */ -extern Decoder *decoder; int initdecoders(void); -int setdecoder(const char *); +Decoder *matchdecoder(const char *);