Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create FIFO for current track playback progress #380

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
aac7261
Requesting progress and metadata in MDNS announcement.
Nov 1, 2013
f37e121
Switching to sub handlers in handle_set_parameter by content-type.
Nov 1, 2013
fc3ebdd
Moved actual parameter handler.
Nov 1, 2013
87344f7
Added empty handlers for cover art.
Nov 1, 2013
cbee548
Added preliminary parsing code for DMAP tags.
Nov 1, 2013
0058c53
Added metadata structures and methods.
Nov 2, 2013
ab63de4
Added command line option to set the cover art output directory.
Nov 2, 2013
016e62e
Added player methods for printing metadata and setting the artwork im…
Nov 2, 2013
16f49c6
Added metadata.c to makefile.
Nov 2, 2013
cb5cc59
Reading the progress: parameter to prevent warnings.
Nov 2, 2013
2562e2b
Settings response status in main handler.
Nov 2, 2013
5c69dcb
Writing the cover image to a file.
Nov 2, 2013
f16f397
Calls to the player for setting and clearing the album cover.
Nov 2, 2013
04dea7f
Removed comments. Naming...
Nov 2, 2013
646a5a2
Setting the metadata from parsed data and passing it to the player.
Nov 2, 2013
a4f4f11
Cleaned logging for SET_PARAMETER requests.
Nov 2, 2013
0c23b3a
Allowing png as content-type.
Nov 2, 2013
ceb8879
Fixed C99 declaration error.
Nov 2, 2013
95eacd8
Added metadata support from allesblinkt.
wnielson Mar 23, 2014
785fa09
Added functionality to write metadata to disk.
wnielson Mar 23, 2014
f31be9d
Improved metadata output.
wnielson Mar 23, 2014
c4992b5
Added command line option to set the cover art output directory.
Nov 2, 2013
4281cb5
Added metadata.c to makefile.
Nov 2, 2013
192d68e
Merged with master
wnielson Oct 16, 2014
632f461
Added FIFO for playback position.
wnielson Oct 16, 2014
8451ea8
Fixed logic error.
wnielson Oct 16, 2014
56643d4
Updated README
wnielson Oct 17, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ An example::
comment=


Additionally, a fifo named `position` will also be created and will be updated
roughly once a second with the position and the total duration of the
currently playing track, space-separated.


Thanks
------
Big thanks to David Hammerton for releasing an ALAC decoder, which is reproduced here in full.
Expand All @@ -100,7 +105,7 @@ Contributors to version 1.x
* [Peter Körner](http://mazdermind.de)
* [Muffinman](http://github.com/therealmuffin)
* [Skaman](http://github.com/skaman)
* [Weston](http://github.com/wnielson)
* [Weston Nielson](http://github.com/wnielson)
* [allesblinkt](http://github.com/allesblinkt)

Contributors to version 0.x
Expand Down Expand Up @@ -128,4 +133,4 @@ Known Ports and Tools
* [shairport4w](http://sf.net/projects/shairport4w)
* OS X:
* [ShairportMenu](https://github.com/rcarlsen/ShairPortMenu), a GUI wrapper as a menu widget
* [MacShairport](https://github.com/joshaber/MacShairport)
* [MacShairport](https://github.com/joshaber/MacShairport)
63 changes: 63 additions & 0 deletions metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
metadata player_meta;
static int fd = -1;
static int dirty = 0;
static int fdp = -1;

void metadata_set(char** field, const char* value) {
if (*field) {
Expand Down Expand Up @@ -77,6 +78,31 @@ static void metadata_close(void) {
fd = -1;
}

void metadata_position_open(void) {
if (!config.meta_dir)
return;

const char fn[] = "position";
size_t pl = strlen(config.meta_dir) + 1 + strlen(fn);

char* path = malloc(pl+1);
snprintf(path, pl+1, "%s/%s", config.meta_dir, fn);

if (mkfifo(path, 0644) && errno != EEXIST)
die("Could not create metadata FIFO %s", path);

fdp = open(path, O_WRONLY | O_NONBLOCK);
if (fdp < 0)
debug(1, "Could not open metadata position FIFO %s. Will try again later.", path);

free(path);
}

static void metadata_position_close(void) {
close(fdp);
fdp = -1;
}

static void print_one(const char *name, const char *value) {
write_unchecked(fd, name, strlen(name));
write_unchecked(fd, "=", 1);
Expand Down Expand Up @@ -114,6 +140,43 @@ void metadata_write(void) {
metadata_close();
}

void metadata_position_write(void) {
int ret;

if (player_meta.paused)
return;

// readers may go away and come back
if (fdp < 0)
metadata_position_open();
if (fdp < 0)
return;

char number[10];
int chars;

//
// XXX: The sample rate is always 44100 Hertz, right?
//
unsigned int position = (player_meta.position-player_meta.start)/44100;
unsigned int length = (player_meta.end-player_meta.start)/44100;

if (length == 0 || position > length)
return;

chars = sprintf(number, "%d", position);
write_unchecked(fdp, number, chars);

write_unchecked(fdp, " ", 1);

chars = sprintf(number, "%d", length);
write_unchecked(fdp, number, chars);

ret = write(fdp, "\n", 1);
if (ret < 1) // no reader
metadata_position_close();
}

void metadata_cover_image(const char *buf, int len, const char *ext) {
if (!config.meta_dir)
return;
Expand Down
7 changes: 7 additions & 0 deletions metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ typedef struct {
char *artwork;
char *comment;
char *genre;

unsigned int paused;
unsigned int position;
unsigned int start;
unsigned int curr;
unsigned int end;
} metadata;

void metadata_set(char** field, const char* value);
void metadata_open(void);
void metadata_write(void);
void metadata_cover_image(const char *buf, int len, const char *ext);
void metadata_position_write(void);

extern metadata player_meta;

Expand Down
2 changes: 2 additions & 0 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ static void *player_thread_func(void *arg) {
#endif
play_samples = stuff_buffer(bf_playback_rate, inbuf, outbuf);

player_meta.paused = 0;
config.output->play(outbuf, play_samples);
}

Expand All @@ -496,6 +497,7 @@ void player_volume(double f) {
}
}
void player_flush(void) {
player_meta.paused = 1;
pthread_mutex_lock(&ab_mutex);
ab_resync();
pthread_mutex_unlock(&ab_mutex);
Expand Down
9 changes: 8 additions & 1 deletion rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <netdb.h>
#include "common.h"
#include "player.h"
#include "metadata.h"

// only one RTP session can be active at a time.
static int running = 0;
Expand All @@ -57,8 +58,14 @@ static void *rtp_receiver(void *arg) {

ssize_t plen = nread;
uint8_t type = packet[1] & ~0x80;
if (type == 0x54) // sync
if (type == 0x54) { // sync
player_meta.position = (packet[4] << 24) |
(packet[5] << 16) |
(packet[6] << 8) |
(packet[7]);
metadata_position_write();
continue;
}
if (type == 0x60 || type == 0x56) { // audio data / resend
pktp = packet;
if (type==0x56) {
Expand Down
8 changes: 7 additions & 1 deletion rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ static void handle_setup(rtsp_conn_info *conn,
int sport = rtp_setup(&conn->remote, cport, tport);
if (!sport)
return;

player_play(&conn->stream);

char resphdr[100];
Expand Down Expand Up @@ -445,6 +445,12 @@ static void handle_set_parameter_parameter(rtsp_conn_info *conn,
} else if(!strncmp(cp, "progress: ", 10)) {
char *progress = cp + 10;
debug(1, "progress: %s\n", progress);

if (sscanf(progress, "%u/%u/%u", &(player_meta.start), &(player_meta.curr), &(player_meta.end)) != 3)
{
player_meta.position = 0;
}

} else {
debug(1, "unrecognised parameter: >>%s<< (%d)\n", cp, strlen(cp));
}
Expand Down