From 5fa419c6585f2e16b31429f4dba71cb45932eee2 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 6 Aug 2017 23:00:26 +0100 Subject: Tidy demux code to use file handles --- p2pvr/devices/tuner.cpp | 43 ++++++++++++++++++++++--------------------- p2pvr/devices/tuner.h | 9 ++++++--- p2pvr/devices/tunerSendSi.cpp | 4 ++-- p2pvr/devices/tunerSendSi.h | 2 +- p2pvr/devices/tunerSendTs.cpp | 4 ++-- p2pvr/devices/tunerSendTs.h | 2 +- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/p2pvr/devices/tuner.cpp b/p2pvr/devices/tuner.cpp index bb27a44..6055379 100644 --- a/p2pvr/devices/tuner.cpp +++ b/p2pvr/devices/tuner.cpp @@ -33,12 +33,8 @@ TunerI::TunerI(const boost::filesystem::path & df) : TunerI::~TunerI() { - { - std::lock_guard g(lock); - while (!backgroundClients.empty()) { - close(backgroundClients.begin()->first); - backgroundClients.erase(backgroundClients.begin()); - } + ScopeLock(lock) { + backgroundClients.clear(); } if (backgroundThread) { backgroundThread->join(); @@ -64,14 +60,10 @@ TunerI::GetDevice() return deviceRoot.string(); } -int +AdHoc::FileUtils::FileHandle TunerI::OpenDemux() const { - int demux = open((deviceRoot / "demux0").string().c_str(), O_RDWR | O_NONBLOCK); - if (demux < 0) { - throw DeviceError(deviceRoot.string(), strerror(errno), errno); - } - return demux; + return AdHoc::FileUtils::FileHandle(deviceRoot / "demux0", O_RDWR | O_NONBLOCK); } void @@ -137,7 +129,7 @@ TunerI::SendPID(int pid, const RawDataClientPrx & client) const AdHoc::FileUtils::FileHandle demux(OpenDemux()); RequestPID(pid, demux); - return ReadDemuxAndSend(demux, client); + return ReadDemuxAndSend(std::move(demux), client); } void @@ -155,14 +147,14 @@ TunerI::RequestPID(int pid, int demux) const } uint64_t -TunerI::ReadDemuxAndSend(int demux, const RawDataClientPrx & _client) const +TunerI::ReadDemuxAndSend(AdHoc::FileUtils::FileHandle && demux, const RawDataClientPrx & _client) const { logger->messagebf(LOG::DEBUG, "%s: begin", __PRETTY_FUNCTION__); struct pollfd ufd; memset(&ufd, 0, sizeof(pollfd)); ufd.fd = demux; ufd.events = POLLIN | POLLPRI; - BackgroundClient client = BackgroundClient(new SendSi(_client)); + BackgroundClient client = BackgroundClient(new SendSi(std::move(demux), _client)); do { // Wait for data to appear switch (poll(&ufd, 1, options->DemuxReadTimeout)) { @@ -201,9 +193,10 @@ TunerI::StartSendingSection(int pid, const RawDataClientPrx & client) logger->message(LOG::DEBUG, __PRETTY_FUNCTION__); Lock(lock); - int demux = backgroundClients.insert(BackgroundClients::value_type(OpenDemux(), - BackgroundClient(new SendSi(client)))).first->first; - RequestPID(pid, demux); + BackgroundClient bgc(new SendSi(OpenDemux(), client)); + RequestPID(pid, bgc->fileHandle()); + int demux = backgroundClients.insert({ + bgc->fileHandle(), bgc }).first->first; startSenderThread(); return demux; } @@ -217,8 +210,8 @@ TunerI::StartSendingTS(const PacketIds & pids, const RawDataClientPrx & client) } Lock(lock); - int demux = backgroundClients.insert(BackgroundClients::value_type(OpenDemux(), - BackgroundClient(new SendTs(client)))).first->first; + BackgroundClient bgc(new SendTs(OpenDemux(), client)); + int demux = bgc->fileHandle(); struct dmx_pes_filter_params pesFilterParams; memset(&pesFilterParams, 0, sizeof(struct dmx_pes_filter_params)); @@ -252,6 +245,7 @@ TunerI::StartSendingTS(const PacketIds & pids, const RawDataClientPrx & client) throw DeviceError("demux", strerror(errno), errno); } + backgroundClients.insert({ demux, bgc }); startSenderThread(); return demux; } @@ -348,8 +342,9 @@ TunerI::senderThread() lock.unlock(); } -TunerI::IDataSender::IDataSender(const RawDataClientPrx & c) : +TunerI::IDataSender::IDataSender(AdHoc::FileUtils::FileHandle && h, const RawDataClientPrx & c) : _packetsSent(0), + fh(std::move(h)), client(c) { } @@ -364,6 +359,12 @@ TunerI::IDataSender::PacketsSent() const return _packetsSent; } +int +TunerI::IDataSender::fileHandle() const +{ + return fh; +} + TunerI::Options::Options() : IceTray::Options("P2PVR Tuner Options") { diff --git a/p2pvr/devices/tuner.h b/p2pvr/devices/tuner.h index 695b322..7db09e4 100644 --- a/p2pvr/devices/tuner.h +++ b/p2pvr/devices/tuner.h @@ -12,6 +12,7 @@ #include #include #include +#include #include namespace P2PVR { @@ -24,15 +25,17 @@ class TunerI : public Tuner { public: class IDataSender { public: - IDataSender(const RawDataClientPrx &); + IDataSender(AdHoc::FileUtils::FileHandle &&, const RawDataClientPrx &); virtual ~IDataSender() = 0; virtual void NewData(const Data &) = 0; virtual bool IsFinished() = 0; uint64_t PacketsSent() const; + int fileHandle() const; protected: uint64_t _packetsSent; + AdHoc::FileUtils::FileHandle fh; const RawDataClientPrx client; }; typedef boost::shared_ptr BackgroundClient; @@ -58,10 +61,10 @@ class TunerI : public Tuner { void StopSending(int handle) override; private: - int OpenDemux() const; + AdHoc::FileUtils::FileHandle OpenDemux() const; uint64_t SendPID(int pid, const RawDataClientPrx & client) const; void RequestPID(int pid, int fd) const; - uint64_t ReadDemuxAndSend(int fd, const RawDataClientPrx & client) const; + uint64_t ReadDemuxAndSend(AdHoc::FileUtils::FileHandle && fd, const RawDataClientPrx & client) const; void startSenderThread(); void senderThread(); static void setBufferSize(int fd, unsigned long bytes); diff --git a/p2pvr/devices/tunerSendSi.cpp b/p2pvr/devices/tunerSendSi.cpp index 3e09aaf..bc901f7 100644 --- a/p2pvr/devices/tunerSendSi.cpp +++ b/p2pvr/devices/tunerSendSi.cpp @@ -7,8 +7,8 @@ namespace P2PVR { namespace DVB { IceTray::Logging::LoggerPtr SendSi::logger(LOGMANAGER()->getLogger()); -SendSi::SendSi(const RawDataClientPrx & c) : - TunerI::IDataSender(c->ice_collocationOptimized(false)) +SendSi::SendSi(AdHoc::FileUtils::FileHandle && fh, const RawDataClientPrx & c) : + TunerI::IDataSender(std::move(fh), c->ice_collocationOptimized(false)) { } diff --git a/p2pvr/devices/tunerSendSi.h b/p2pvr/devices/tunerSendSi.h index 688fb2a..e590e29 100644 --- a/p2pvr/devices/tunerSendSi.h +++ b/p2pvr/devices/tunerSendSi.h @@ -8,7 +8,7 @@ namespace P2PVR { namespace DVB { class SendSi : public TunerI::IDataSender { public: - SendSi(const P2PVR::RawDataClientPrx &); + SendSi(AdHoc::FileUtils::FileHandle &&, const P2PVR::RawDataClientPrx &); ~SendSi(); void NewData(const P2PVR::Data &); diff --git a/p2pvr/devices/tunerSendTs.cpp b/p2pvr/devices/tunerSendTs.cpp index fef418f..3f8864c 100644 --- a/p2pvr/devices/tunerSendTs.cpp +++ b/p2pvr/devices/tunerSendTs.cpp @@ -10,8 +10,8 @@ namespace DVB { IceTray::Logging::LoggerPtr SendTs::logger(LOGMANAGER()->getLogger()); -SendTs::SendTs(const RawDataClientPrx & c) : - TunerI::IDataSender(c->ice_collocationOptimized(false)) +SendTs::SendTs(AdHoc::FileUtils::FileHandle && fh, const RawDataClientPrx & c) : + TunerI::IDataSender(std::move(fh), c->ice_collocationOptimized(false)) { buffer.reserve(TARGET_BUFFER_SIZE); } diff --git a/p2pvr/devices/tunerSendTs.h b/p2pvr/devices/tunerSendTs.h index bbec0a9..abdd4b5 100644 --- a/p2pvr/devices/tunerSendTs.h +++ b/p2pvr/devices/tunerSendTs.h @@ -8,7 +8,7 @@ namespace P2PVR { namespace DVB { class SendTs : public TunerI::IDataSender { public: - SendTs(const P2PVR::RawDataClientPrx &); + SendTs(AdHoc::FileUtils::FileHandle &&, const P2PVR::RawDataClientPrx &); ~SendTs(); void NewData(const P2PVR::Data &); -- cgit v1.2.3