mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-15 09:11:42 +02:00
Added metadata support for Shairplay-sync/Airplay interface
This commit is contained in:
parent
ce17b0010a
commit
c820f01ca7
6 changed files with 312 additions and 7 deletions
|
@ -22,16 +22,37 @@
|
|||
#include "common/utils.h"
|
||||
#include "aixlog.hpp"
|
||||
|
||||
#ifdef HAS_EXPAT
|
||||
#include "base64.h"
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
static string hex2str(string input)
|
||||
{
|
||||
typedef unsigned char byte;
|
||||
unsigned long x = strtoul(input.c_str(), 0, 16);
|
||||
byte a[] = {byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x), 0};
|
||||
return string((char *)a);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Expat is used in metadata parsing from Shairport-sync.
|
||||
* Without HAS_EXPAT defined no parsing will occur.
|
||||
*
|
||||
* This is currently defined in airplayStream.h, prolly should
|
||||
* move to Makefile?
|
||||
*/
|
||||
|
||||
AirplayStream::AirplayStream(PcmListener* pcmListener, const StreamUri& uri) : ProcessStream(pcmListener, uri), port_(5000)
|
||||
{
|
||||
logStderr_ = true;
|
||||
|
||||
pipePath_ = "/tmp/shairmeta." + to_string(getpid());
|
||||
cout << "Pipe [" << pipePath_ << "]\n";
|
||||
|
||||
// XXX: Check if pipe exists, delete or throw error
|
||||
|
||||
sampleFormat_ = SampleFormat("44100:16:2");
|
||||
uri_.query["sampleformat"] = sampleFormat_.getFormat();
|
||||
|
||||
|
@ -39,16 +60,85 @@ AirplayStream::AirplayStream(PcmListener* pcmListener, const StreamUri& uri) : P
|
|||
|
||||
string devicename = uri_.getQuery("devicename", "Snapcast");
|
||||
params_wo_port_ = "--name=\"" + devicename + "\" --output=stdout";
|
||||
params_wo_port_ += " --metadata-pipename " + pipePath_;
|
||||
params_ = params_wo_port_ + " --port=" + cpt::to_string(port_);
|
||||
|
||||
pipeReaderThread_ = thread(&AirplayStream::pipeReader, this);
|
||||
pipeReaderThread_.detach();
|
||||
}
|
||||
|
||||
|
||||
AirplayStream::~AirplayStream()
|
||||
{
|
||||
parse(string("</metatags>"));
|
||||
XML_ParserFree(parser_);
|
||||
}
|
||||
|
||||
int AirplayStream::parse(string line)
|
||||
{
|
||||
enum XML_Status result;
|
||||
|
||||
if((result = XML_Parse(parser_, line.c_str(), line.length(), false)) == XML_STATUS_ERROR)
|
||||
{
|
||||
XML_ParserFree(parser_);
|
||||
createParser();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void AirplayStream::createParser()
|
||||
{
|
||||
parser_ = XML_ParserCreate("UTF-8");
|
||||
XML_SetElementHandler(parser_, element_start, element_end);
|
||||
XML_SetCharacterDataHandler(parser_, data);
|
||||
XML_SetUserData(parser_, this);
|
||||
|
||||
// Make an outer element to keep parsing going
|
||||
parse(string("<metatags>"));
|
||||
}
|
||||
|
||||
void AirplayStream::push()
|
||||
{
|
||||
string data = entry_->data;
|
||||
if(entry_->isBase64 && entry_->length > 0)
|
||||
data = base64_decode(data);
|
||||
|
||||
if(entry_->type == "ssnc" && entry_->code == "mdst")
|
||||
jtag_ = json();
|
||||
|
||||
if(entry_->code == "asal") jtag_["ALBUM"] = data;
|
||||
if(entry_->code == "asar") jtag_["ARTIST"] = data;
|
||||
if(entry_->code == "minm") jtag_["TITLE"] = data;
|
||||
|
||||
if(entry_->type == "ssnc" && entry_->code == "mden"){
|
||||
//LOG(INFO) << "metadata=" << jtag_.dump(4) << "\n";
|
||||
setMeta(jtag_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AirplayStream::initExeAndPath(const std::string& filename)
|
||||
void AirplayStream::pipeReader()
|
||||
{
|
||||
createParser();
|
||||
|
||||
while(true)
|
||||
{
|
||||
ifstream pipe(pipePath_);
|
||||
|
||||
if(pipe){
|
||||
string line;
|
||||
|
||||
while(getline(pipe, line)){
|
||||
parse(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Wait a little until we try to open it again
|
||||
this_thread::sleep_for(chrono::milliseconds(500));
|
||||
}
|
||||
}
|
||||
|
||||
void AirplayStream::initExeAndPath(const string& filename)
|
||||
{
|
||||
path_ = "";
|
||||
exe_ = findExe(filename);
|
||||
|
@ -86,3 +176,52 @@ void AirplayStream::onStderrMsg(const char* buffer, size_t n)
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef HAS_EXPAT
|
||||
void XMLCALL AirplayStream::element_start(void *userdata, const char *element_name, const char **attr)
|
||||
{
|
||||
AirplayStream *self = (AirplayStream *)userdata;
|
||||
string name(element_name);
|
||||
|
||||
self->buf_.assign("");
|
||||
if(name == "item") self->entry_.reset(new TageEntry);
|
||||
|
||||
for(int i = 0; attr[i]; i += 2){
|
||||
string name(attr[i]);
|
||||
string value(attr[i+1]);
|
||||
if(name == "encoding")
|
||||
self->entry_->isBase64 = (value == "base64"); // Quick & dirty..
|
||||
}
|
||||
}
|
||||
|
||||
void XMLCALL AirplayStream::element_end(void *userdata, const char *element_name)
|
||||
{
|
||||
AirplayStream *self = (AirplayStream *)userdata;
|
||||
string name(element_name);
|
||||
|
||||
if(name == "code")
|
||||
self->entry_->code.assign(hex2str(self->buf_));
|
||||
|
||||
else if(name == "type")
|
||||
self->entry_->type.assign(hex2str(self->buf_));
|
||||
|
||||
else if(name == "length")
|
||||
self->entry_->length = strtoul(self->buf_.c_str(), 0, 10);
|
||||
|
||||
else if(name == "data")
|
||||
self->entry_->data = self->buf_;
|
||||
|
||||
else if(name == "item")
|
||||
self->push();
|
||||
|
||||
else if(name == "metatags") ;
|
||||
else cout << "Unknown tag <" << name << ">\n";
|
||||
}
|
||||
|
||||
void XMLCALL AirplayStream::data(void *userdata, const char *content, int length)
|
||||
{
|
||||
AirplayStream *self = (AirplayStream *)userdata;
|
||||
string value(content, (size_t)length);
|
||||
self->buf_.append(value);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue