added optional parameter "logStderr" to ProcessStream

This commit is contained in:
badaix 2016-11-02 09:03:40 +01:00
parent ce8ed1e3f3
commit 7af0bdde65
3 changed files with 24 additions and 20 deletions

View file

@ -31,10 +31,10 @@ using namespace std;
ProcessStream::ProcessStream(PcmListener* pcmListener, const StreamUri& uri) : PcmStream(pcmListener, uri), path(""), process_(nullptr)
ProcessStream::ProcessStream(PcmListener* pcmListener, const StreamUri& uri) : PcmStream(pcmListener, uri), path_(""), process_(nullptr)
{
logO << "ProcessStream: " << uri_.getQuery("params") << "\n";
params = uri_.getQuery("params");
params_ = uri_.getQuery("params");
logStderr_ = (uri_.getQuery("logStderr", "false") == "true");
}
@ -88,11 +88,11 @@ std::string ProcessStream::findExe(const std::string& filename)
void ProcessStream::initExeAndPath(const std::string& filename)
{
exe = findExe(filename);
if (exe.find("/") != string::npos)
path = exe.substr(0, exe.find_last_of("/"));
exe_ = findExe(filename);
if (exe_.find("/") != string::npos)
path_ = exe_.substr(0, exe_.find_last_of("/"));
if (!fileExists(exe))
if (!fileExists(exe_))
throw SnapException("file not found: \"" + filename + "\"");
}
@ -115,7 +115,10 @@ void ProcessStream::stop()
void ProcessStream::onStderrMsg(const char* buffer, size_t n)
{
/// do nothing
if (logStderr_)
{
logO << string(buffer, n);
}
}
@ -139,7 +142,7 @@ void ProcessStream::worker()
while (active_)
{
process_.reset(new Process(exe + " " + params, path));
process_.reset(new Process(exe_ + " " + params_, path_));
int flags = fcntl(process_->getStdout(), F_GETFL, 0);
fcntl(process_->getStdout(), F_SETFL, flags | O_NONBLOCK);

View file

@ -43,11 +43,12 @@ public:
virtual void stop();
protected:
std::string exe;
std::string path;
std::string params;
std::string exe_;
std::string path_;
std::string params_;
std::unique_ptr<Process> process_;
std::thread stderrReaderThread_;
bool logStderr_;
virtual void worker();
virtual void stderrReader();

View file

@ -39,8 +39,8 @@ SpotifyStream::SpotifyStream(PcmListener* pcmListener, const StreamUri& uri) : P
if (password.empty())
throw SnapException("missing parameter \"password\"");
params = "--name \"" + devicename + "\" --username \"" + username + "\" --password \"" + password + "\" --bitrate " + bitrate + " --backend stdout";
logO << "params: " << params << "\n";
params_ = "--name \"" + devicename + "\" --username \"" + username + "\" --password \"" + password + "\" --bitrate " + bitrate + " --backend stdout";
// logO << "params: " << params << "\n";
}
@ -51,16 +51,16 @@ SpotifyStream::~SpotifyStream()
void SpotifyStream::initExeAndPath(const std::string& filename)
{
exe = findExe(filename);
if (!fileExists(exe) || (exe == "/"))
exe_ = findExe(filename);
if (!fileExists(exe_) || (exe_ == "/"))
{
exe = findExe("librespot");
if (!fileExists(exe))
exe_ = findExe("librespot");
if (!fileExists(exe_))
throw SnapException("librespot not found");
}
if (exe.find("/") != string::npos)
path = exe.substr(0, exe.find_last_of("/"));
if (exe_.find("/") != string::npos)
path_ = exe_.substr(0, exe_.find_last_of("/"));
}