Skip to content

Commit

Permalink
Improve: Internal Software PID Filtering #199
Browse files Browse the repository at this point in the history
  • Loading branch information
Barracuda09 committed Nov 6, 2023
1 parent dda93e4 commit b5a2a54
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ INCLUDES +=

LDFLAGS += $(CPU_FLAGS)
CFLAGS += $(CPU_FLAGS)
CFLAGS_NEW += $(CPU_FLAGS)

# Libraries needed for linking
LDFLAGS += -pthread -lrt
Expand All @@ -35,25 +36,32 @@ endif
# Set Compiler Flags
CFLAGS += -I src -std=c++17 -Werror=vla -Wall -Wextra -Winit-self -Wshadow -pthread $(INCLUDES)

CFLAGS_NEW += -I src -std=c++17 -Werror=vla -Wall -Wextra -Winit-self -Wshadow -pthread $(INCLUDES)

# Build "debug", "release" or "simu"
ifeq "$(BUILD)" "debug"
# "Debug" build - no optimization, with debugging symbols
CFLAGS += -O0 -g3 -gdwarf-2 -DDEBUG -fstack-protector-all -Wswitch-default
CFLAGS += -O0 -g3 -gdwarf-2 -DDEBUG -DDEBUG_LOG -fstack-protector-all -Wswitch-default
CFLAGS_NEW += -O3 -s -DNDEBUG -DDEBUG_LOG
LDFLAGS += -rdynamic
else ifeq "$(BUILD)" "debug1"
# "Debug" build - with optimization, with debugging symbols
CFLAGS += -O2 -g3 -DDEBUG -fstack-protector-all -Wswitch-default
CFLAGS_NEW += -O3 -s -DNDEBUG -DDEBUG_LOG
LDFLAGS += -rdynamic
else ifeq "$(BUILD)" "speed"
# "Debug" build - with optimization, with debugging symbols
# "Debug" build - with optimization, without debugging symbols
CFLAGS += -Os -s -DNDEBUG
CFLAGS_NEW += -Os -s -DNDEBUG
else ifeq "$(BUILD)" "simu"
# "Debug Simu" build - no optimization, with debugging symbols
CFLAGS += -O0 -g3 -DDEBUG -DSIMU -fstack-protector-all
CFLAGS += -O0 -g3 -DDEBUG -DDEBUG_LOG -DSIMU -fstack-protector-all
CFLAGS_NEW += -O3 -s -DNDEBUG -DDEBUG_LOG
LDFLAGS += -rdynamic
else
# "Release" build - optimization, and no debug symbols
CFLAGS += -O2 -s -DNDEBUG
CFLAGS_NEW += -O3 -s -DNDEBUG
endif

# List of source to be compiled
Expand Down Expand Up @@ -211,10 +219,15 @@ $(EXECUTABLE): $(OBJECTS) $(HEADERS)
# A pattern rule is used to build objects from 'cpp' files
# $< represents the first item in the dependencies list
# $@ represents the targetname
$(OBJ_DIR)/mpegts/%.o: $(SRC_DIR)/mpegts/%.cpp $(HEADERS)
@mkdir -p $(@D)
$(CXX) -c $(CFLAGS_NEW) $< -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(HEADERS)
@mkdir -p $(@D)
$(CXX) -c $(CFLAGS) $< -o $@


# Create debug versions
debug:
$(MAKE) "BUILD=debug"
Expand Down
20 changes: 10 additions & 10 deletions src/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,7 @@ class Log {

#define MPEGTS_TABLES 0x100

#ifdef NDEBUG
#define SI_LOG_INFO(format, ...) Log::applog(LOG_INFO, format, ##__VA_ARGS__)
#define SB_LOG_INFO(subsys, format, ...) Log::applog(LOG_INFO | subsys, format, ##__VA_ARGS__)
#define SI_LOG_ERROR(format, ...) Log::applog(LOG_ERR, format, ##__VA_ARGS__)
#define SI_LOG_DEBUG(format, ...) Log::applog(LOG_DEBUG, format, ##__VA_ARGS__)
#define SI_LOG_PERROR(format, ...) Log::applog(LOG_ERR, "@#1: @#2 (code @#3)", StringConverter::stringFormat(format, ##__VA_ARGS__), strerror(errno), errno)
#define SI_LOG_GIA_PERROR(format, err, ...) Log::applog(LOG_ERR, "@#1: @#2 (code @#3)", StringConverter::stringFormat(format, ##__VA_ARGS__), gai_strerror(err), err)
#define SI_LOG_COND_DEBUG(cond, format, ...) if (cond) { SI_LOG_DEBUG(format, ##__VA_ARGS__); }
#define SI_LOG_BIN_DEBUG(p, length, fmt, ...) Log::binlog(LOG_DEBUG, p, length, fmt, ##__VA_ARGS__)
#else
#ifdef DEBUG_LOG
#define SI_LOG_INFO(format, ...) Log::applog(LOG_INFO, "[@#1:@#2] @#3", STR(__FILE__, 45), DIGIT(__LINE__, 3), StringConverter::stringFormat(format, ##__VA_ARGS__))
#define SB_LOG_INFO(subsys, format, ...) Log::applog(LOG_INFO | subsys, "[@#1:@#2] @#3", STR(__FILE__, 45), DIGIT(__LINE__, 3), StringConverter::stringFormat(format, ##__VA_ARGS__))
#define SI_LOG_ERROR(format, ...) Log::applog(LOG_ERR, "[@#1:@#2] @#3", STR(__FILE__, 45), DIGIT(__LINE__, 3), StringConverter::stringFormat(format, ##__VA_ARGS__))
Expand All @@ -114,6 +105,15 @@ class Log {
#define SI_LOG_GIA_PERROR(format, err, ...) Log::applog(LOG_ERR, "[@#1:@#2] @#3: @#4 (code @#5)", STR(__FILE__, 45), DIGIT(__LINE__, 3), StringConverter::stringFormat(format, ##__VA_ARGS__), gai_strerror(err), err)
#define SI_LOG_COND_DEBUG(cond, format, ...) if (cond) { SI_LOG_DEBUG(format, ##__VA_ARGS__); }
#define SI_LOG_BIN_DEBUG(p, length, fmt, ...) Log::binlog(LOG_DEBUG, p, length, "[@#1:@#2] @#3", STR(__FILE__, 45), DIGIT(__LINE__, 3), StringConverter::stringFormat(fmt, ##__VA_ARGS__))
#else
#define SI_LOG_INFO(format, ...) Log::applog(LOG_INFO, format, ##__VA_ARGS__)
#define SB_LOG_INFO(subsys, format, ...) Log::applog(LOG_INFO | subsys, format, ##__VA_ARGS__)
#define SI_LOG_ERROR(format, ...) Log::applog(LOG_ERR, format, ##__VA_ARGS__)
#define SI_LOG_DEBUG(format, ...) Log::applog(LOG_DEBUG, format, ##__VA_ARGS__)
#define SI_LOG_PERROR(format, ...) Log::applog(LOG_ERR, "@#1: @#2 (code @#3)", StringConverter::stringFormat(format, ##__VA_ARGS__), strerror(errno), errno)
#define SI_LOG_GIA_PERROR(format, err, ...) Log::applog(LOG_ERR, "@#1: @#2 (code @#3)", StringConverter::stringFormat(format, ##__VA_ARGS__), gai_strerror(err), err)
#define SI_LOG_COND_DEBUG(cond, format, ...) if (cond) { SI_LOG_DEBUG(format, ##__VA_ARGS__); }
#define SI_LOG_BIN_DEBUG(p, length, fmt, ...) Log::binlog(LOG_DEBUG, p, length, fmt, ##__VA_ARGS__)
#endif

#endif // LOG_H_INCLUDE
7 changes: 3 additions & 4 deletions src/input/childpipe/TSReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool TSReader::isDataAvailable() {
}

bool TSReader::readTSPackets(mpegts::PacketBuffer& buffer) {
/* //////////////////////
/*
if (_deviceData.generatePSI()) {
const mpegts::TSData data = _deviceData.getPSIGenerator().generatePSIFrom(
_feID, _transform.getTransformationMap());
Expand All @@ -131,12 +131,12 @@ bool TSReader::readTSPackets(mpegts::PacketBuffer& buffer) {
return buffer.full();
}
////////////////////// */
*/
if (!_exec.isOpen()) {
return false;
}
const bool filter = _deviceData.isInternalPidFilteringEnabled();
for (int i = 0; i < 7; ++i) {
for (int i = 0; i < 21; ++i) {
const int readSize = _exec.read(buffer.getWriteBufferPtr(), buffer.getAmountOfBytesToWrite());
if (readSize > 0) {
buffer.addAmountOfBytesWritten(readSize);
Expand All @@ -146,7 +146,6 @@ bool TSReader::readTSPackets(mpegts::PacketBuffer& buffer) {
return true;
}
}
std::this_thread::sleep_for(std::chrono::microseconds(2));
}
// Check again if buffer is full
return buffer.full();
Expand Down
44 changes: 26 additions & 18 deletions src/mpegts/PacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,37 @@ void PacketBuffer::purge() {
if (bufSize == 0) {
return;
}
const int begin = (bufSize / TS_PACKET_SIZE) - 1;
const int end = (_processedIndex > RTP_HEADER_LEN) ? (_processedIndex / TS_PACKET_SIZE) : 0;
std::size_t skipData = 0;
for (int i = begin; i >= end; --i) {
unsigned char *cData = getTSPacketPtr(i);
if (cData[1] == 0xFF) {
// Next packet needs to be purged as well, then continue to next
if (i > 0 && (cData - TS_PACKET_SIZE)[1] == 0xFF) {
skipData += TS_PACKET_SIZE;
const unsigned char* tsBegin = getTSReadBufferPtr();
const unsigned char* tsCheckBegin = getTSReadBufferPtr() + TS_PACKET_SIZE;
unsigned char* cDataPtr = _buffer + _writeIndex - TS_PACKET_SIZE;;
const unsigned char* nextData = cDataPtr;
const unsigned char* endData = _buffer + _writeIndex;
// Purge trailing packets by moving pointer
for (; cDataPtr > tsBegin; cDataPtr -= TS_PACKET_SIZE) {
if (*(cDataPtr + 1) == 0xFF) {
endData -= TS_PACKET_SIZE;
nextData -= TS_PACKET_SIZE;
if (cDataPtr > tsCheckBegin && *((cDataPtr - TS_PACKET_SIZE) + 1) == 0xFF) {
continue;
}
skipData += TS_PACKET_SIZE;
// Remove current (plus other) packet from the buffer
unsigned char *endData = getWriteBufferPtr();
unsigned char *nextData = cData + skipData;
if (nextData < endData) {
std::memcpy(cData, nextData, endData - nextData);
}
cDataPtr -= TS_PACKET_SIZE;
break;
}
// Copy/Move requested packets to begin of buffer (Purge packets)
for (; cDataPtr >= tsBegin; cDataPtr -= TS_PACKET_SIZE) {
if (*(cDataPtr + 1) == 0xFF) {
if (cDataPtr > tsCheckBegin && *((cDataPtr - TS_PACKET_SIZE) + 1) == 0xFF) {
continue;
}
_writeIndex -= skipData;
_processedIndex = _writeIndex;
skipData = 0;
const auto size = endData - nextData;
std::memcpy(cDataPtr, nextData, size);
endData = cDataPtr + size;
}
nextData = cDataPtr;
}
_writeIndex = endData - _buffer;
_processedIndex = _writeIndex;
}

void PacketBuffer::tagRTPHeaderWith(
Expand Down
11 changes: 4 additions & 7 deletions src/mpegts/PacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,9 @@ class PacketBuffer {
/// be processed.
std::size_t getBeginOfUnFilteredPackets() const {
if (_processedIndex == RTP_HEADER_LEN) {
_processedIndex = _writeIndex;
return 0;
}
const std::size_t index = _processedIndex;
_processedIndex = _writeIndex;
return (index - RTP_HEADER_LEN) / TS_PACKET_SIZE;
return (_processedIndex - RTP_HEADER_LEN) / TS_PACKET_SIZE;
}

/// get the amount of data that is in this TS buffer
Expand All @@ -123,7 +120,7 @@ class PacketBuffer {
}

/// Get the write pointer for this TS buffer
unsigned char *getWriteBufferPtr() {
unsigned char* getWriteBufferPtr() {
return &_buffer[_writeIndex];
}

Expand All @@ -134,13 +131,13 @@ class PacketBuffer {

/// This function will return the begin of the first TS packet in this TS buffer
/// so without RTP header
unsigned char *getTSReadBufferPtr() {
unsigned char* getTSReadBufferPtr() {
return &_buffer[RTP_HEADER_LEN];
}

/// Get the TS packet pointer for packets 0 up until NUMBER_OF_TS_PACKETS
/// @param packetNumber a value from 0 up until NUMBER_OF_TS_PACKETS
unsigned char *getTSPacketPtr(std::size_t packetNumber) {
unsigned char* getTSPacketPtr(std::size_t packetNumber) {
const std::size_t index = (packetNumber * TS_PACKET_SIZE) + RTP_HEADER_LEN;
return &_buffer[index];
}
Expand Down

0 comments on commit b5a2a54

Please sign in to comment.