mirror of
https://github.com/badaix/snapcast.git
synced 2025-08-04 00:59:32 +02:00
added PCM reader base class
This commit is contained in:
parent
6d1cc2d081
commit
2036477446
8 changed files with 166 additions and 80 deletions
84
server/pcmreader/pcmReader.cpp
Normal file
84
server/pcmreader/pcmReader.cpp
Normal file
|
@ -0,0 +1,84 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2015 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <memory>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "pcmReader.h"
|
||||
#include "../encoder/encoderFactory.h"
|
||||
#include "common/log.h"
|
||||
#include "common/snapException.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
|
||||
PcmReader::PcmReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs) : pcmListener_(pcmListener), sampleFormat_(sampleFormat), pcmReadMs_(pcmReadMs)
|
||||
{
|
||||
EncoderFactory encoderFactory;
|
||||
encoder_.reset(encoderFactory.createEncoder(codec));
|
||||
}
|
||||
|
||||
|
||||
PcmReader::~PcmReader()
|
||||
{
|
||||
stop();
|
||||
close(fd_);
|
||||
}
|
||||
|
||||
|
||||
msg::Header* PcmReader::getHeader()
|
||||
{
|
||||
return encoder_->getHeader();
|
||||
}
|
||||
|
||||
|
||||
void PcmReader::start()
|
||||
{
|
||||
encoder_->init(this, sampleFormat_);
|
||||
active_ = true;
|
||||
readerThread_ = thread(&PcmReader::worker, this);
|
||||
}
|
||||
|
||||
|
||||
void PcmReader::stop()
|
||||
{
|
||||
if (active_)
|
||||
{
|
||||
active_ = false;
|
||||
readerThread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void PcmReader::onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration)
|
||||
{
|
||||
// logO << "onChunkEncoded: " << duration << " us\n";
|
||||
if (duration <= 0)
|
||||
return;
|
||||
|
||||
chunk->timestamp.sec = tvEncodedChunk_.tv_sec;
|
||||
chunk->timestamp.usec = tvEncodedChunk_.tv_usec;
|
||||
chronos::addUs(tvEncodedChunk_, duration * 1000);
|
||||
pcmListener_->onChunkRead(this, chunk, duration);
|
||||
}
|
||||
|
79
server/pcmreader/pcmReader.h
Normal file
79
server/pcmreader/pcmReader.h
Normal file
|
@ -0,0 +1,79 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2015 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#ifndef PCM_READER_H
|
||||
#define PCM_READER_H
|
||||
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include "../encoder/encoder.h"
|
||||
#include "message/sampleFormat.h"
|
||||
#include "message/header.h"
|
||||
|
||||
|
||||
class PcmReader;
|
||||
|
||||
|
||||
/// Callback interface for users of PcmReader
|
||||
/**
|
||||
* Users of PcmReader should implement this to get the data
|
||||
*/
|
||||
class PcmListener
|
||||
{
|
||||
public:
|
||||
virtual void onChunkRead(const PcmReader* pcmReader, const msg::PcmChunk* chunk, double duration) = 0;
|
||||
virtual void onResync(const PcmReader* pcmReader, double ms) = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
/// Reads and decodes PCM data
|
||||
/**
|
||||
* Reads PCM and passes the data to an encoder.
|
||||
* Implements EncoderListener to get the encoded data.
|
||||
* Data is passed to the PcmListener
|
||||
*/
|
||||
class PcmReader : public EncoderListener
|
||||
{
|
||||
public:
|
||||
/// ctor. Encoded PCM data is passed to the PcmListener
|
||||
PcmReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs = 20);
|
||||
virtual ~PcmReader();
|
||||
|
||||
virtual void start();
|
||||
virtual void stop();
|
||||
|
||||
/// Implementation of EncoderListener::onChunkEncoded
|
||||
virtual void onChunkEncoded(const Encoder* encoder, msg::PcmChunk* chunk, double duration);
|
||||
virtual msg::Header* getHeader();
|
||||
|
||||
protected:
|
||||
virtual void worker() = 0;
|
||||
int fd_;
|
||||
timeval tvEncodedChunk_;
|
||||
std::atomic<bool> active_;
|
||||
std::thread readerThread_;
|
||||
PcmListener* pcmListener_;
|
||||
msg::SampleFormat sampleFormat_;
|
||||
size_t pcmReadMs_;
|
||||
std::unique_ptr<Encoder> encoder_;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
103
server/pcmreader/pipeReader.cpp
Normal file
103
server/pcmreader/pipeReader.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2015 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#include <memory>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "pipeReader.h"
|
||||
#include "../encoder/encoderFactory.h"
|
||||
#include "common/log.h"
|
||||
#include "common/snapException.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
|
||||
PipeReader::PipeReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs) : PcmReader(pcmListener, sampleFormat, codec, fifoName, pcmReadMs)
|
||||
{
|
||||
umask(0);
|
||||
mkfifo(fifoName.c_str(), 0666);
|
||||
fd_ = open(fifoName.c_str(), O_RDONLY | O_NONBLOCK);
|
||||
if (fd_ == -1)
|
||||
throw SnapException("failed to open fifo: \"" + fifoName + "\"");
|
||||
}
|
||||
|
||||
|
||||
PipeReader::~PipeReader()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void PipeReader::worker()
|
||||
{
|
||||
timeval tvChunk;
|
||||
std::unique_ptr<msg::PcmChunk> chunk(new msg::PcmChunk(sampleFormat_, pcmReadMs_));
|
||||
|
||||
while (active_)
|
||||
{
|
||||
gettimeofday(&tvChunk, NULL);
|
||||
tvEncodedChunk_ = tvChunk;
|
||||
long nextTick = chronos::getTickCount();
|
||||
try
|
||||
{
|
||||
while (active_)
|
||||
{
|
||||
chunk->timestamp.sec = tvChunk.tv_sec;
|
||||
chunk->timestamp.usec = tvChunk.tv_usec;
|
||||
int toRead = chunk->payloadSize;
|
||||
int len = 0;
|
||||
do
|
||||
{
|
||||
int count = read(fd_, chunk->payload + len, toRead - len);
|
||||
|
||||
if (count <= 0)
|
||||
usleep(100*1000);
|
||||
else
|
||||
len += count;
|
||||
}
|
||||
while ((len < toRead) && active_);
|
||||
|
||||
encoder_->encode(chunk.get());
|
||||
nextTick += pcmReadMs_;
|
||||
chronos::addUs(tvChunk, pcmReadMs_ * 1000);
|
||||
long currentTick = chronos::getTickCount();
|
||||
|
||||
if (nextTick >= currentTick)
|
||||
{
|
||||
// logO << "sleep: " << nextTick - currentTick << "\n";
|
||||
usleep((nextTick - currentTick) * 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
gettimeofday(&tvChunk, NULL);
|
||||
tvEncodedChunk_ = tvChunk;
|
||||
pcmListener_->onResync(this, currentTick - nextTick);
|
||||
nextTick = currentTick;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(const std::exception& e)
|
||||
{
|
||||
logE << "Exception: " << e.what() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
44
server/pcmreader/pipeReader.h
Normal file
44
server/pcmreader/pipeReader.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2015 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
***/
|
||||
|
||||
#ifndef PIPE_READER_H
|
||||
#define PIPE_READER_H
|
||||
|
||||
#include "pcmReader.h"
|
||||
|
||||
|
||||
|
||||
/// Reads and decodes PCM data from a named pipe
|
||||
/**
|
||||
* Reads PCM from a named pipe and passes the data to an encoder.
|
||||
* Implements EncoderListener to get the encoded data.
|
||||
* Data is passed to the PcmListener
|
||||
*/
|
||||
class PipeReader : public PcmReader
|
||||
{
|
||||
public:
|
||||
/// ctor. Encoded PCM data is passed to the PipeListener
|
||||
PipeReader(PcmListener* pcmListener, const msg::SampleFormat& sampleFormat, const std::string& codec, const std::string& fifoName, size_t pcmReadMs = 20);
|
||||
virtual ~PipeReader();
|
||||
|
||||
protected:
|
||||
void worker();
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue