From 46412eb1485a8f13e64909a39ddc4990cb106296 Mon Sep 17 00:00:00 2001 From: Mke Brady Date: Thu, 26 Feb 2015 10:56:49 +0000 Subject: [PATCH] Reinistate after bugfix: Change bonjour advertisement if metadata sought, add sender name metadata, use own base64 encoder --- common.h | 1 + mdns.h | 6 ++- mdns_avahi.c | 40 ++++++++++----- mdns_dns_sd.c | 10 +++- mdns_external.c | 29 +++++++++-- mdns_tinysvcmdns.c | 15 +++++- metadata.c | 121 ++++++++++++++++++++++++++++++++++++++++----- metadata.h | 1 + rtsp.c | 17 +++++-- shairport.c | 1 + 10 files changed, 204 insertions(+), 37 deletions(-) diff --git a/common.h b/common.h index 9871465de..d1b5b049d 100644 --- a/common.h +++ b/common.h @@ -94,6 +94,7 @@ double vol2attn(double vol, long max_db, long min_db); uint64_t get_absolute_time_in_fp(void); shairport_cfg config; +char sender_name[1024]; void command_start(void); void command_stop(void); diff --git a/mdns.h b/mdns.h index 68c451804..0ebc8fd38 100644 --- a/mdns.h +++ b/mdns.h @@ -13,7 +13,11 @@ typedef struct { void (*mdns_unregister)(void); } mdns_backend; -#define MDNS_RECORD "tp=UDP", "sm=false", "ek=1", "et=0,1", "cn=0,1", "ch=2", "md=0,1", \ +#define MDNS_RECORD_WITH_METADATA "tp=UDP", "sm=false", "ek=1", "et=0,1", "cn=0,1", "ch=2", "md=0,1", \ + "ss=16", "sr=44100", "vn=3", "txtvers=1", \ + config.password ? "pw=true" : "pw=false" + +#define MDNS_RECORD_WITHOUT_METADATA "tp=UDP", "sm=false", "ek=1", "et=0,1", "cn=0,1", "ch=2", \ "ss=16", "sr=44100", "vn=3", "txtvers=1", \ config.password ? "pw=true" : "pw=false" diff --git a/mdns_avahi.c b/mdns_avahi.c index d22886488..f1db7c68e 100644 --- a/mdns_avahi.c +++ b/mdns_avahi.c @@ -62,17 +62,35 @@ static void register_service(AvahiClient *c) { return; int ret; - ret = avahi_entry_group_add_service(group, - AVAHI_IF_UNSPEC, - AVAHI_PROTO_UNSPEC, - 0, - name, - "_raop._tcp", - NULL, - NULL, - port, - MDNS_RECORD, - NULL); + if (config.meta_dir) { + debug(1,"Avahi with metadata"); + ret = avahi_entry_group_add_service(group, + AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, + 0, + name, + "_raop._tcp", + NULL, + NULL, + port, + MDNS_RECORD_WITH_METADATA, + NULL); + } + else { + debug(1,"Avahi without metadata"); + ret = avahi_entry_group_add_service(group, + AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, + 0, + name, + "_raop._tcp", + NULL, + NULL, + port, + MDNS_RECORD_WITHOUT_METADATA, + NULL); + } + if (ret < 0) die("avahi_entry_group_add_service failed"); diff --git a/mdns_dns_sd.c b/mdns_dns_sd.c index 4581b3ad5..1a458ea3c 100644 --- a/mdns_dns_sd.c +++ b/mdns_dns_sd.c @@ -32,7 +32,15 @@ static DNSServiceRef service; static int mdns_dns_sd_register(char *apname, int port) { - const char *record[] = { MDNS_RECORD, NULL }; + const char *recordwithoutmetadata[] = { MDNS_RECORD_WITHOUT_METADATA, NULL }; + const char *recordwithmetadata[] = { MDNS_RECORD_WITH_METADATA, NULL }; + + char **record; + if (config.meta_dir) + record = recordwithmetadata; + else + record = recordwithoutmetadata; + uint16_t length = 0; const char **field; diff --git a/mdns_external.c b/mdns_external.c index 00c950c65..912dfec1c 100644 --- a/mdns_external.c +++ b/mdns_external.c @@ -89,10 +89,20 @@ static int mdns_external_avahi_register(char *apname, int port) { char mdns_port[6]; sprintf(mdns_port, "%d", config.port); - char *argv[] = { - NULL, apname, "_raop._tcp", mdns_port, MDNS_RECORD, NULL + char *argvwithoutmetadata[] = { + NULL, apname, "_raop._tcp", mdns_port, MDNS_RECORD_WITHOUT_METADATA, NULL }; + char *argvwithmetadata[] = { + NULL, apname, "_raop._tcp", mdns_port, MDNS_RECORD_WITH_METADATA, NULL + }; + + char **argv; + if (config.meta_dir) + argv=argvwithmetadata; + else + argv=argvwithoutmetadata; + argv[0] = "avahi-publish-service"; int pid = fork_execvp(argv[0], argv); if (pid >= 0) @@ -121,8 +131,19 @@ static int mdns_external_dns_sd_register(char *apname, int port) { char mdns_port[6]; sprintf(mdns_port, "%d", config.port); - char *argv[] = {"dns-sd", "-R", apname, "_raop._tcp", ".", - mdns_port, MDNS_RECORD, NULL}; + char *argvwithoutmetadata[] = { + NULL, apname, "_raop._tcp", mdns_port, MDNS_RECORD_WITHOUT_METADATA, NULL + }; + + char *argvwithmetadata[] = { + NULL, apname, "_raop._tcp", mdns_port, MDNS_RECORD_WITH_METADATA, NULL + }; + + char **argv; + if (config.meta_dir) + argv=argvwithmetadata; + else + argv=argvwithoutmetadata; int pid = fork_execvp(argv[0], argv); if (pid >= 0) diff --git a/mdns_tinysvcmdns.c b/mdns_tinysvcmdns.c index 19bdce4ac..fa258e544 100644 --- a/mdns_tinysvcmdns.c +++ b/mdns_tinysvcmdns.c @@ -122,13 +122,24 @@ static int mdns_tinysvcmdns_register(char *apname, int port) { freeifaddrs(ifa); - const char *txt[] = { MDNS_RECORD, NULL }; + char *txtwithoutmetadata[] = { MDNS_RECORD_WITHOUT_METADATA, NULL }; + char *txtwithmetadata[] = { MDNS_RECORD_WITH_METADATA, NULL }; + + char **txt; + + if (config.meta_dir) + txt = txtwithmetadata; + else + txt = txtwithoutmetadata; + + + struct mdns_service *svc = mdnsd_register_svc(svr, apname, "_raop._tcp.local", port, NULL, - txt); // TTL should be 75 minutes, i.e. 4500 seconds + (const char **)txt); // TTL should be 75 minutes, i.e. 4500 seconds mdns_service_destroy(svc); diff --git a/metadata.c b/metadata.c index 92a4df031..09990a3a3 100644 --- a/metadata.c +++ b/metadata.c @@ -47,6 +47,65 @@ #include "common.h" #include "metadata.h" + +// including a simple base64 encoder to minimise malloc/free activity + +// From Stack Overflow, with thanks: +// http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c +// minor mods to make independent of C99. +// more significant changes make it not malloc memory +// needs to initialise the docoding table first + +// add _so to end of name to avoid confusion with polarssl's implementation + +static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/'}; + +static int mod_table[] = {0, 2, 1}; + +// pass in a pointer to the data, its length, a pointer to the output buffer and a pointer to an int containing its maximum length +// the actual length will be returned. + +char *base64_encode_so(const unsigned char *data, + size_t input_length, + char *encoded_data, + size_t *output_length) { + + size_t calculated_output_length = 4 * ((input_length + 2) / 3); + if (calculated_output_length> *output_length) + return(NULL); + *output_length = calculated_output_length; + + int i,j; + for (i = 0, j = 0; i < input_length;) { + + uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; + uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; + uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0; + + uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; + + encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; + encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; + encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; + encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; + } + + for (i = 0; i < mod_table[input_length % 3]; i++) + encoded_data[*output_length - 1 - i] = '='; + + return encoded_data; +} + +// with thanks! +// + metadata player_meta; static int fd = -1; static int dirty = 0; @@ -61,7 +120,7 @@ void metadata_set(char** field, const char* value) { dirty = 1; } -void metadata_open(void) { +void metadata_create(void) { if (!config.meta_dir) return; @@ -74,6 +133,19 @@ void metadata_open(void) { if (mkfifo(path, 0644) && errno != EEXIST) die("Could not create metadata FIFO %s", path); + free(path); +} + +void metadata_open(void) { + if (!config.meta_dir) + return; + + const char fn[] = "shairport_sync_metadata_pipe"; + 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); + fd = open(path, O_WRONLY | O_NONBLOCK); if (fd < 0) debug(1, "Could not open metadata FIFO %s. Will try again later.", path); @@ -86,6 +158,12 @@ static void metadata_close(void) { fd = -1; } +void metadata_init(void) { + if (!config.meta_dir) + return; + metadata_create(); +} + static void print_one(const char *name, const char *value) { int ignore; ignore = write(fd, name, strlen(name)); @@ -217,23 +295,40 @@ void metadata_process(uint32_t type,uint32_t code,char *data,uint32_t length) { char thestring[1024]; snprintf(thestring,1024,"%x%x%u\n",type,code,length); ret = write(fd, thestring, strlen(thestring)); - if (ret < 1) // no reader - metadata_close(); + if (ret < 1) // possibly the pipe is running out of memory becasue the reader is too slow + debug(1,"Error writing to pipe"); if (length>0) { snprintf(thestring,1024,"\n"); ret = write(fd, thestring, strlen(thestring)); if (ret < 1) // no reader - metadata_close(); - - char *b64 = base64_enc(data,length); - ret = write(fd,b64,strlen(b64)); - free(b64); - - if (ret < 1) // no reader - metadata_close(); - snprintf(thestring,1024,"\n\n"); + debug(1,"Error writing to pipe"); + // here, we write the data in base64 form using the crappy base64 encoder provided + // but, we break it into lines of 76 output characters, except for the last one. + // thus, we send groups of (76/4)*3 = 57 bytes to the encoder at a time + size_t remaining_count = length; + char *remaining_data = data; + size_t towrite_count; + char outbuf[76]; + while ((remaining_count) && (ret>=0)) { + size_t towrite_count = remaining_count; + if (towrite_count>57) + towrite_count = 57; + size_t outbuf_size = 76; // size of output buffer on entry, length of result on exit + if (base64_encode_so(remaining_data, towrite_count, outbuf, &outbuf_size)==NULL) + debug(1,"Error encoding base64 data."); + //debug(1,"Remaining count: %d ret: %d, outbuf_size: %d.",remaining_count,ret,outbuf_size); + ret = write(fd,outbuf,outbuf_size); + if (ret<0) + debug(1,"Error writing base64 data to pipe: \"%s\".",strerror(errno)); + remaining_data+=towrite_count; + remaining_count-=towrite_count; + //ret = write(fd,"\r\n",2); + //if (ret<0) + // debug(1,"Error writing base64 cr/lf to pipe."); + } + snprintf(thestring,1024,"\n"); ret = write(fd, thestring, strlen(thestring)); if (ret < 1) // no reader - metadata_close(); + debug(1,"Error writing to pipe"); } } diff --git a/metadata.h b/metadata.h index 9a769b5d9..a57f356a9 100644 --- a/metadata.h +++ b/metadata.h @@ -14,6 +14,7 @@ typedef struct { void metadata_set(char** field, const char* value); void metadata_open(void); +void metadata_init(void); void metadata_write(void); void metadata_cover_image(const char *buf, int len, const char *ext); diff --git a/rtsp.c b/rtsp.c index 357a34dc5..e5df7c438 100644 --- a/rtsp.c +++ b/rtsp.c @@ -631,6 +631,9 @@ static void handle_set_parameter_metadata(rtsp_conn_info *conn, // inform the listener that a set of metadata is ending metadata_process('ssnc','stop',NULL,0); + // send the user some shairport-originated metadata + // send the name of the player, e.g. "Joe's iPhone" or "iTunes" + metadata_process('ssnc','sndr',sender_name,strlen(sender_name)); } static void handle_set_parameter(rtsp_conn_info *conn, @@ -647,7 +650,7 @@ static void handle_set_parameter(rtsp_conn_info *conn, debug(2, "received metadata tags in SET_PARAMETER request\n"); handle_set_parameter_metadata(conn, req, resp); } else if (!strncmp(ct, "image", 5)) { - debug(2, "received image in SET_PARAMETER request\n"); + debug(1, "received image in SET_PARAMETER request\n"); // note: the image/type tag isn't reliable, so it's not being sent // -- best look at the first few bytes of the image metadata_process('ssnc','PICT',req->content,req->contentlength); @@ -721,13 +724,17 @@ static void handle_announce(rtsp_conn_info *conn, conn->stream.fmtp[i] = atoi(strsep(&pfmtp, " \t")); char *hdr = msg_get_header(req, "X-Apple-Client-Name"); - if (hdr) + if (hdr) { + strncpy(sender_name,hdr,1024); debug(1,"Play connection from \"%s\".",hdr); - else { + } else { hdr = msg_get_header(req, "User-Agent"); - if (hdr) + if (hdr) { debug(1,"Play connection from \"%s\".",hdr); - } + strncpy(sender_name,hdr,1024); + } else + sender_name[0]=0; + } resp->respcode = 200; } else { resp->respcode = 453; diff --git a/shairport.c b/shairport.c index 30b7b89d7..8aaa22b6b 100644 --- a/shairport.c +++ b/shairport.c @@ -532,6 +532,7 @@ int main(int argc, char **argv) { md5_finish(&tctx, ap_md5); #endif memcpy(config.hw_addr, ap_md5, sizeof(config.hw_addr)); + metadata_init() ; // create the metadata pipe if necessary rtsp_listen_loop();