Skip to content

Commit

Permalink
Merge pull request #299 from alienatedsec/roleo_testing
Browse files Browse the repository at this point in the history
Merging tested commits
  • Loading branch information
alienatedsec committed Sep 30, 2023
2 parents db856f3 + b305a15 commit e171cc3
Show file tree
Hide file tree
Showing 24 changed files with 478 additions and 116 deletions.
18 changes: 17 additions & 1 deletion src/h264grabber/h264grabber/h264grabber.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 roleo.
* Copyright (c) 2023 roleo.
*
* 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
Expand Down Expand Up @@ -140,6 +140,10 @@
#define FIFO_NAME_HIGH "/tmp/h264_high_fifo"
#define FIFO_NAME_AAC "/tmp/aac_audio_fifo"

#define F_LINUX_SPECIFIC_BASE 1024
#define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
#define F_GETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 8)

// Unused vars
unsigned char IDR[] = {0x65, 0xB8};
unsigned char NAL_START[] = {0x00, 0x00, 0x00, 0x01};
Expand Down Expand Up @@ -530,6 +534,10 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error opening fifo %s\n", FIFO_NAME_LOW);
return -1;
}
if (fcntl(fileno(fOut), F_SETPIPE_SZ, 65536) != 65536) {
fprintf(stderr, "Cannot set size of fifo\n");
return -1;
};
} else if (resolution == RESOLUTION_HIGH) {
unlink(FIFO_NAME_HIGH);
if (mkfifo(FIFO_NAME_HIGH, mode) < 0) {
Expand All @@ -546,6 +554,10 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error opening fifo %s\n", FIFO_NAME_HIGH);
return -1;
}
if (fcntl(fileno(fOut), F_SETPIPE_SZ, 262144) != 262144) {
fprintf(stderr, "Cannot set size of fifo\n");
return -1;
};
} else if (resolution == RESOLUTION_AUDIO) {
unlink(FIFO_NAME_AAC);
if (mkfifo(FIFO_NAME_AAC, mode) < 0) {
Expand All @@ -562,6 +574,10 @@ int main(int argc, char **argv) {
fprintf(stderr, "Error opening fifo %s\n", FIFO_NAME_AAC);
return -1;
}
if (fcntl(fileno(fOut), F_SETPIPE_SZ, 8192) != 8192) {
fprintf(stderr, "Cannot set size of fifo\n");
return -1;
};
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/rRTSPServer/Makefile.rRTSPServer
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##### Change the following for your environment:
COMPILE_OPTS = $(INCLUDES) -I. -I./include -O2 -ffunction-sections -fdata-sections -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 -DNO_OPENSSL=1 -DRTP_PAYLOAD_MAX_SIZE=1352 -DNEWLOCALE_NOT_USED
COMPILE_OPTS = $(INCLUDES) -I. -I./include -O1 -ffunction-sections -fdata-sections -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 -DNO_OPENSSL=1 -DRTP_PAYLOAD_MAX_SIZE=1352 -DNEWLOCALE_NOT_USED
C = c
C_COMPILER = $(CC)
C_FLAGS = $(COMPILE_OPTS) $(CPPFLAGS) $(CFLAGS)
Expand Down Expand Up @@ -73,7 +73,7 @@ livemedia:

rRTSPServer_OBJS = src/rRTSPServer.$(OBJ) \
src/ADTSAudioFifoServerMediaSubsession.$(OBJ) src/ADTSAudioFifoSource.$(OBJ) \
src/DummySink.$(OBJ) src/ByteStreamFifoSource.$(OBJ) src/H264VideoFifoServerMediaSubsession.$(OBJ) \
src/ByteStreamFifoSource.$(OBJ) src/H264VideoFifoServerMediaSubsession.$(OBJ) \

rRTSPServer$(EXE): $(rRTSPServer_OBJS) $(LOCAL_LIBS)
$(LINK)$@ $(CONSOLE_LINK_OPTS) $(rRTSPServer_OBJS) $(LIBS) -lpthread
Expand Down
Empty file modified src/rRTSPServer/include/ADTSAudioFifoServerMediaSubsession.hh
100755 → 100644
Empty file.
Empty file modified src/rRTSPServer/include/ADTSAudioFifoSource.hh
100755 → 100644
Empty file.
Empty file modified src/rRTSPServer/include/ByteStreamFifoSource.hh
100755 → 100644
Empty file.
Empty file modified src/rRTSPServer/include/H264VideoFifoServerMediaSubsession.hh
100755 → 100644
Empty file.
Empty file modified src/rRTSPServer/include/presentationTime.hh
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions src/rRTSPServer/src/ADTSAudioFifoServerMediaSubsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this library; if not, write to the Free Software Foundation, Inc.,
// Implementation

#include "ADTSAudioFifoServerMediaSubsession.hh"
#include "ADTSAudioStreamDiscreteFramer.hh"
#include "ADTSAudioFifoSource.hh"
#include "MPEG4GenericRTPSink.hh"
#include "FramedFilter.hh"
Expand Down
122 changes: 63 additions & 59 deletions src/rRTSPServer/src/ADTSAudioFifoSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,90 +212,94 @@ void ADTSAudioFifoSource::doReadFromFile() {
#else
if (read(fileno(fFid), headers, sizeof headers) < (signed) sizeof headers) {
#endif
// The input source has ended:
handleClosure();
return;
}
fFrameSize = 0;
fNumTruncatedBytes = 0;
// // The input source has ended:
// handleClosure();
// return;
} else {

// Extract important fields from the headers:
Boolean protection_absent = headers[1]&0x01;
u_int16_t frame_length
= ((headers[3]&0x03)<<11) | (headers[4]<<3) | ((headers[5]&0xE0)>>5);
u_int16_t syncword = (headers[0]<<4) | (headers[1]>>4);
if (debug & 4) fprintf(stderr, "Read frame: syncword 0x%x, protection_absent %d, frame_length %d\n", syncword, protection_absent, frame_length);
if (syncword != 0xFFF) {
fprintf(stderr, "WARNING: Bad syncword! Try to recover sync.\n");
// Resync
while (1) {
// Extract important fields from the headers:
Boolean protection_absent = headers[1]&0x01;
u_int16_t frame_length
= ((headers[3]&0x03)<<11) | (headers[4]<<3) | ((headers[5]&0xE0)>>5);
u_int16_t syncword = (headers[0]<<4) | (headers[1]>>4);
if (debug & 4) fprintf(stderr, "Read frame: syncword 0x%x, protection_absent %d, frame_length %d\n", syncword, protection_absent, frame_length);
if (syncword != 0xFFF) {
fprintf(stderr, "WARNING: Bad syncword! Try to recover sync.\n");
// Resync
while (1) {
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
fread(headers, 1, 1, fFid);
fread(headers, 1, 1, fFid);
#else
read(fileno(fFid), headers, 1);
read(fileno(fFid), headers, 1);
#endif
if (headers[0] == 0xFF) {
if (headers[0] == 0xFF) {
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
fread(&headers[1], 1, 1, fFid);
fread(&headers[1], 1, 1, fFid);
#else
read(fileno(fFid), &headers[1], 1);
read(fileno(fFid), &headers[1], 1);
#endif
// Check the 'syncword':
if ((headers[1]&0xF0) == 0xF0) {
// Check the 'syncword':
if ((headers[1]&0xF0) == 0xF0) {
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
fread(&headers[2], 1, sizeof(headers) - 2, fFid);
fread(&headers[2], 1, sizeof(headers) - 2, fFid);
#else
read(fileno(fFid), &headers[2], sizeof(headers) - 2);
read(fileno(fFid), &headers[2], sizeof(headers) - 2);
#endif
break;
break;
}
}
usleep(1000);
}
usleep(10000);
}
}
unsigned numBytesToRead
= frame_length > sizeof headers ? frame_length - sizeof headers : 0;
unsigned numBytesToRead
= frame_length > sizeof headers ? frame_length - sizeof headers : 0;

// If there's a 'crc_check' field, skip it:
if (!protection_absent) {
unsigned char null[2];
// If there's a 'crc_check' field, skip it:
if (!protection_absent) {
unsigned char null[2];
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
fread(null, 1, 2, fFid);
fread(null, 1, 2, fFid);
#else
read(fileno(fFid), null, 2);
read(fileno(fFid), null, 2);
#endif
numBytesToRead = numBytesToRead > 2 ? numBytesToRead - 2 : 0;
}
numBytesToRead = numBytesToRead > 2 ? numBytesToRead - 2 : 0;
}

// Next, read the raw frame data into the buffer provided:
if (numBytesToRead > fMaxSize) {
fNumTruncatedBytes = numBytesToRead - fMaxSize;
numBytesToRead = fMaxSize;
}
// Next, read the raw frame data into the buffer provided:
if (numBytesToRead > fMaxSize) {
fNumTruncatedBytes = numBytesToRead - fMaxSize;
numBytesToRead = fMaxSize;
}
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
int numBytesRead = fread(fTo, 1, numBytesToRead, fFid);
int numBytesRead = fread(fTo, 1, numBytesToRead, fFid);
#else
int numBytesRead = read(fileno(fFid), fTo, numBytesToRead);
int numBytesRead = read(fileno(fFid), fTo, numBytesToRead);
#endif
if (numBytesRead < 0) numBytesRead = 0;
fFrameSize = numBytesRead;
fNumTruncatedBytes += numBytesToRead - numBytesRead;

// Set the 'presentation time':
if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
// This is the first frame, so use the current time:
gettimeofday(&fPresentationTime, NULL);
} else {
if (numBytesRead < 0) numBytesRead = 0;
fFrameSize = numBytesRead;
fNumTruncatedBytes += numBytesToRead - numBytesRead;

// Set the 'presentation time':
if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
// This is the first frame, so use the current time:
gettimeofday(&fPresentationTime, NULL);
} else {
#ifndef PRES_TIME_CLOCK
// Increment by the play time of the previous frame:
unsigned uSeconds = fPresentationTime.tv_usec + fuSecsPerFrame;
fPresentationTime.tv_sec += uSeconds/1000000;
fPresentationTime.tv_usec = uSeconds%1000000;
// Increment by the play time of the previous frame:
unsigned uSeconds = fPresentationTime.tv_usec + fuSecsPerFrame;
fPresentationTime.tv_sec += uSeconds/1000000;
fPresentationTime.tv_usec = uSeconds%1000000;
#else
// Use system clock to set presentation time
gettimeofday(&fPresentationTime, NULL);
// Use system clock to set presentation time
gettimeofday(&fPresentationTime, NULL);
#endif
}
}
if (debug & 4) fprintf(stderr, "AAC frame - fPresentationTime, sec = %ld, usec = %ld\n", fPresentationTime.tv_sec, fPresentationTime.tv_usec);

fDurationInMicroseconds = fuSecsPerFrame;
fDurationInMicroseconds = fuSecsPerFrame;
}

// Switch to another task, and inform the reader that he has data:
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
Expand Down
4 changes: 4 additions & 0 deletions src/rRTSPServer/src/ByteStreamFifoSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ along with this library; if not, write to the Free Software Foundation, Inc.,

////////// ByteStreamFifoSource //////////

extern int debug;

ByteStreamFifoSource*
ByteStreamFifoSource::createNew(UsageEnvironment& env, char const* fileName,
unsigned preferredFrameSize,
Expand Down Expand Up @@ -170,7 +172,9 @@ void ByteStreamFifoSource::doReadFromFile() {
// We don't know a specific play time duration for this data,
// so just record the current time as being the 'presentation time':
gettimeofday(&fPresentationTime, NULL);
fDurationInMicroseconds = fPlayTimePerFrame;
}
if (debug & 4) fprintf(stderr, "h264 frame - fPresentationTime, sec = %ld, usec = %ld\n", fPresentationTime.tv_sec, fPresentationTime.tv_usec);

// Inform the reader that he has data:
#ifdef READ_FROM_FILES_SYNCHRONOUSLY
Expand Down
2 changes: 1 addition & 1 deletion src/rRTSPServer/src/H264VideoFifoServerMediaSubsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ FramedSource* H264VideoFifoServerMediaSubsession::createNewStreamSource(unsigned
estBitrate = 500; // kbps, estimate

// Create the video source:
ByteStreamFifoSource* fileSource = ByteStreamFifoSource::createNew(envir(), fFileName);
ByteStreamFifoSource* fileSource = ByteStreamFifoSource::createNew(envir(), fFileName, 0, 50000);
if (fileSource == NULL) return NULL;
fFileSize = fileSource->fileSize();

Expand Down
8 changes: 4 additions & 4 deletions src/rRTSPServer/src/rRTSPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ StreamReplicator* startReplicatorStream(const char* inputAudioFileName) {
FramedSource* source = replicator->createStreamReplica();

// Then create a 'dummy sink' object to receive the replica stream:
MediaSink* sink = DummySink::createNew(*env, "dummy");
// MediaSink* sink = DummySink::createNew(*env, "dummy");

// Now, start playing, feeding the sink object from the source:
sink->startPlaying(*source, NULL, NULL);
// sink->startPlaying(*source, NULL, NULL);

return replicator;
}
Expand Down Expand Up @@ -362,7 +362,7 @@ int main(int argc, char** argv)

announceStream(rtspServer, sms_low, streamName, inputFileName, audio);
}
/*

// An ADTS audio elementary stream:
if (audio != 0)
{
Expand All @@ -379,7 +379,7 @@ int main(int argc, char** argv)

announceStream(rtspServer, sms_audio, streamName, inputAudioFileName, audio);
}
*/

// Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling.
// Try first with the default HTTP port (80), and then with the alternative HTTP
// port numbers (8000 and 8080).
Expand Down
7 changes: 5 additions & 2 deletions src/snapshot/snapshot/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
OBJECTS = imggrabber.o convert2jpg.o add_water.o water_mark.o
OBJECTS = imggrabber.o convert2jpg.o add_water.o water_mark.o cap_pic.o
FFMPEG = ffmpeg-6.0
FFMPEG_DIR = ./$(FFMPEG)
INC_FF = -I$(FFMPEG_DIR)
LIB_FF = $(FFMPEG_DIR)/libavcodec/libavcodec.a $(FFMPEG_DIR)/libavutil/libavutil.a -lpthread -lm
LIB_FF = $(FFMPEG_DIR)/libavcodec/libavcodec.a $(FFMPEG_DIR)/libavutil/libavutil.a -lpthread -lm -lrt
JPEGLIB = jpeg-9e
JPEGLIB_DIR = ./$(JPEGLIB)
JPEGSRC = jpegsrc.v9e
Expand All @@ -28,6 +28,9 @@ add_water.o: add_water.c $(HEADERS)
water_mark.o: water_mark.c $(HEADERS)
$(CC) -c $< $(INC_J) -fPIC -O2 -o $@

cap_pic.o: cap_pic.c $(HEADERS)
$(CC) -c $< $(INC_J) -fPIC -O2 -o $@

imggrabber: $(OBJECTS)
$(CC) $(OBJECTS) $(LIB_J) $(LIB_FF) -fPIC -O2 -o $@
$(STRIP) $@
Expand Down

0 comments on commit e171cc3

Please sign in to comment.