From 5bf60df0fe9b62460fec04b23966a835e80e0b7a Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Tue, 3 Mar 2020 18:48:14 +0000 Subject: [PATCH] New void* wrapper class. Remove one use of Thread::postStopCleanup --- buffer.cc | 44 ++++++++++++++++++++++++++++++++++++++++ buffer.h | 44 ++++++++++++++++++++++++++++++++++++++++ objects.mk | 2 +- playervideorec.cc | 51 +++++++++++++++++++---------------------------- playervideorec.h | 2 -- 5 files changed, 109 insertions(+), 34 deletions(-) create mode 100644 buffer.cc create mode 100644 buffer.h diff --git a/buffer.cc b/buffer.cc new file mode 100644 index 0000000..dbb33f7 --- /dev/null +++ b/buffer.cc @@ -0,0 +1,44 @@ +/* + Copyright 2020 Chris Tallon + + This file is part of VOMP. + + VOMP 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 2 of the License, or + (at your option) any later version. + + VOMP 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 VOMP. If not, see . +*/ + +#include + +#include "buffer.h" + +Buffer::~Buffer() +{ + if (memory) free(memory); +} + +void Buffer::set(void* newptr) +{ + if (memory) abort(); + memory = newptr; +} + +void Buffer::release() +{ + if (memory) free(memory); + memory = nullptr; +} + +unsigned char* Buffer::ucharp() +{ + return reinterpret_cast(memory); +} diff --git a/buffer.h b/buffer.h new file mode 100644 index 0000000..e5aee0b --- /dev/null +++ b/buffer.h @@ -0,0 +1,44 @@ +/* + Copyright 2020 Chris Tallon + + This file is part of VOMP. + + VOMP 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 2 of the License, or + (at your option) any later version. + + VOMP 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 VOMP. If not, see . +*/ + +#ifndef BUFFER_H +#define BUFFER_H + +class Buffer +{ + public: + Buffer() {}; + Buffer(const Buffer&) = delete; // copy + Buffer& operator=(const Buffer&) = delete; // assign + Buffer(Buffer&&) = delete; // move + Buffer& operator=(Buffer&&) = delete; // move-assign + + ~Buffer(); + + void set(void* mem); + bool isNull() { return (memory == nullptr); } + void release(); + + unsigned char* ucharp(); + + private: + void* memory{}; +}; + +#endif diff --git a/objects.mk b/objects.mk index 62d9089..1e81873 100644 --- a/objects.mk +++ b/objects.mk @@ -18,7 +18,7 @@ OBJ_COMMON = command.o tcp.o dsock.o thread.o timers.o i18n.o vdp6.o wprogressbar.o bitmap.o dvbsubtitles.o tfeed.o vteletextview.o \ teletextdecodervbiebu.o teletxt/txtfont.o movieinfo.o seriesinfo.o \ wmovieview.o wseriesview.o tvmedia.o wtvmedia.o wpictureview.o \ - osdvector.o surfacevector.o + osdvector.o surfacevector.o buffer.o OBJ_RASPBERRY = main.o threadp.o osdopenvg.o \ ledraspberry.o videoomx.o audioomx.o imageomx.o \ diff --git a/playervideorec.cc b/playervideorec.cc index 2f68247..b5ffa04 100644 --- a/playervideorec.cc +++ b/playervideorec.cc @@ -28,6 +28,7 @@ #include "message.h" #include "dvbsubtitles.h" #include "osdreceiver.h" +#include "buffer.h" #include "playervideorec.h" @@ -1068,6 +1069,7 @@ void PlayerVideoRec::threadFeedPlay() if (!vdr->isConnected()) { doConnectionLost(); return; } logger->log("Player", Log::DEBUG, "startFeedPlay: wantedframe %i goto %llu", currentFrameNumber, feedPosition); + Buffer threadBuffer; while(1) { @@ -1104,7 +1106,7 @@ void PlayerVideoRec::threadFeedPlay() } //logger->log("Player", Log::DEBUG, "Get Block in"); - threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead); + threadBuffer.set(vdr->getBlock(feedPosition, askFor, &thisRead)); //logger->log("Player", Log::DEBUG, "Get Block out"); feedPosition += thisRead; @@ -1115,11 +1117,11 @@ void PlayerVideoRec::threadFeedPlay() return; } - if (!threadBuffer) break; + if (threadBuffer.isNull()) break; if (startup) { - int a_stream = demuxer->scan(threadBuffer, thisRead); + int a_stream = demuxer->scan(threadBuffer.ucharp(), thisRead); demuxer->setAudioStream(a_stream); logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream); startup = false; @@ -1130,7 +1132,7 @@ void PlayerVideoRec::threadFeedPlay() while(writeLength < thisRead) { //logger->log("Player", Log::DEBUG, "Put in"); - thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength); + thisWrite = demuxer->put(threadBuffer.ucharp() + writeLength, thisRead - writeLength); //logger->log("Player", Log::DEBUG, "Put out"); writeLength += thisWrite; @@ -1146,9 +1148,7 @@ void PlayerVideoRec::threadFeedPlay() threadCheckExit(); } - free(threadBuffer); - threadBuffer = NULL; - + threadBuffer.release(); } // end of recording @@ -1193,6 +1193,8 @@ void PlayerVideoRec::threadPTSFeedScan() int frameTimeOffset = 0; // Time in msec between frames + Buffer threadBuffer; + if (state == S_FFWD) { direction = 1; // and 0 for backward @@ -1217,23 +1219,20 @@ void PlayerVideoRec::threadPTSFeedScan() logger->log("Player", Log::DEBUG, "XXX Got frame"); - threadBuffer = vdr->getBlock(filePos, iframeLength, &amountReceived); + threadBuffer.set(vdr->getBlock(filePos, iframeLength, &amountReceived)); if (!vdr->isConnected()) { - if (threadBuffer) free(threadBuffer); doConnectionLost(); break; } - threadCheckExit(); - - videoLength = demuxer->stripAudio(threadBuffer, amountReceived); - demuxer->changeTimes(threadBuffer,videoLength,playtime); + videoLength = demuxer->stripAudio(threadBuffer.ucharp(), amountReceived); + demuxer->changeTimes(threadBuffer.ucharp(), videoLength, playtime); int count=0; - while (!video->displayIFrame(threadBuffer, videoLength)) // the device might block + while (!video->displayIFrame(threadBuffer.ucharp(), videoLength)) // the device might block { MILLISLEEP(20); threadCheckExit(); @@ -1259,8 +1258,7 @@ void PlayerVideoRec::threadPTSFeedScan() } } - free(threadBuffer); - threadBuffer = NULL; + threadBuffer.release(); } } @@ -1295,6 +1293,8 @@ void PlayerVideoRec::threadFeedScan() if (state == S_FFWD) direction = 1; // and 0 for backward + Buffer threadBuffer; + while(1) { // Fetch I-frames until we get one that can be displayed in good time @@ -1330,11 +1330,10 @@ void PlayerVideoRec::threadFeedScan() #endif logger->log("Player", Log::DEBUG, "XXX Got frame"); - threadBuffer = vdr->getBlock(filePos, iframeLength, &amountReceived); + threadBuffer.set(vdr->getBlock(filePos, iframeLength, &amountReceived)); if (!vdr->isConnected() || !amountReceived) { - if (threadBuffer) free(threadBuffer); doConnectionLost(); break; } @@ -1355,11 +1354,10 @@ void PlayerVideoRec::threadFeedScan() MILLISLEEP(sleepTime); logger->log("Player", Log::DEBUG, "XXX Slept for %d", sleepTime); - videoLength = demuxer->stripAudio(threadBuffer, amountReceived); - video->displayIFrame(threadBuffer, videoLength); + videoLength = demuxer->stripAudio(threadBuffer.ucharp(), amountReceived); + video->displayIFrame(threadBuffer.ucharp(), videoLength); currentFrameNumber = iframeNumber; - free(threadBuffer); - threadBuffer = NULL; + threadBuffer.release(); #ifndef WIN32 gettimeofday(&clock2, NULL); @@ -1378,15 +1376,6 @@ void PlayerVideoRec::threadFeedScan() } } -void PlayerVideoRec::threadPostStopCleanup() -{ - if (threadBuffer) - { - free(threadBuffer); - threadBuffer = NULL; - } -} - // ----------------------------------- Dev #ifdef DEV diff --git a/playervideorec.h b/playervideorec.h index 8d44911..0ea99c0 100644 --- a/playervideorec.h +++ b/playervideorec.h @@ -122,7 +122,6 @@ class PlayerVideoRec : public Thread_TYPE, public Callback protected: void threadMethod(); - void threadPostStopCleanup(); private: void switchState(UCHAR newState, ULONG jumpFrame=0); @@ -165,7 +164,6 @@ class PlayerVideoRec : public Thread_TYPE, public Callback ULONG currentFrameNumber{}; UINT blockSize{100000}; UINT startupBlockSize{250000}; - UCHAR* threadBuffer{}; UCHAR state{S_STOP}; UCHAR ifactor{4}; // 4, 8, 16, 32 }; -- 2.39.5