Skip to content

Commit

Permalink
Merge pull request #2 from SatwikSShanbhag/ss/sample_example
Browse files Browse the repository at this point in the history
sample example for valve module as well as seek and flush queue features
  • Loading branch information
AdityaKBhadragond14 committed Jul 4, 2024
2 parents d920279 + 7478f05 commit 7f79097
Show file tree
Hide file tree
Showing 13 changed files with 506 additions and 12 deletions.
29 changes: 22 additions & 7 deletions base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -624,22 +624,27 @@ SET(UT_FILES
)

SET(SAMPLE_UT_FILES
test/utmain.cpp
test/test_utils.cpp
test/test_utils.h
.././samples/timelapse-sample/timelapse_summary_test.cpp
test/utmain.cpp
test/test_utils.cpp
test/test_utils.h
../samples/create_thumbnail_from_mp4_video/test_generate_thumbnail_from_mp4_video.cpp
../samples/play_mp4_from_beginning/test_play_mp4_video_from_beginning.cpp
.././samples/timelapse-sample/timelapse_summary_test.cpp
)

SET(SAMPLE_CORE_FILES
../samples/timelapse-sample/timelapse_summary.h
../samples/create_thumbnail_from_mp4_video/GenerateThumbnailsPipeline.h
../samples/create_thumbnail_from_mp4_video/GenerateThumbnailsPipeline.cpp
../samples/play_mp4_from_beginning/PlayMp4VideoFromBeginning.h
../samples/play_mp4_from_beginning/PlayMp4VideoFromBeginning.cpp
../samples/timelapse-sample/timelapse_summary.h
../samples/timelapse-sample/timelapse_summary.cpp
)

SET(SAMPLE_SOURCE
${SAMPLE_CORE_FILES}
${SAMPLE_UT_FILES}
)

IF(ENABLE_LINUX)
list(APPEND UT_FILES
test/gtkglrenderer_tests.cpp
Expand All @@ -653,7 +658,6 @@ IF(ENABLE_LINUX)
ENDIF(ENABLE_LINUX)

add_executable(aprapipesut ${UT_FILES})

add_executable(aprapipessampleut ${SAMPLE_SOURCE})

IF(ENABLE_ARM64)
Expand All @@ -679,6 +683,17 @@ ${NVCODEC_INCLUDE_DIR}
test
)

IF(ENABLE_ARM64)
target_include_directories ( aprapipesut PRIVATE ${JETSON_MULTIMEDIA_LIB_INCLUDE} ${FFMPEG_ROOT} ${JPEG_INCLUDE_DIR})
ENDIF(ENABLE_ARM64)

IF (ENABLE_CUDA)
target_include_directories ( aprapipesut PRIVATE ${NVCODEC_INCLUDE_DIR})
ENDIF (ENABLE_CUDA)


find_library(OPENH264_LIB NAMES openh264.lib libopenh264.a REQUIRED)
find_library(LIBMP4_LIB NAMES mp4lib.lib libmp4lib.a REQUIRED)

target_link_libraries(aprapipessampleut
aprapipes
Expand Down
8 changes: 4 additions & 4 deletions base/src/Mp4ReaderSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,20 +629,20 @@ class Mp4ReaderDetailAbs
{
int seekedToFrame = -1;
uint64_t skipMsecsInFile = 0;

if (!mState.startTimeStampFromFile)
if (!mState.resolvedStartingTS)
{
LOG_ERROR << "Start timestamp is not saved in the file. Can't support seeking with timestamps.";
return false;
}
if (skipTS < mState.startTimeStampFromFile)
if (skipTS < mState.resolvedStartingTS)
{
LOG_INFO << "seek time outside range. Seeking to start of video.";
skipMsecsInFile = 0;
}
else
{
skipMsecsInFile = skipTS - mState.startTimeStampFromFile;
skipMsecsInFile = skipTS - mState.resolvedStartingTS;
}

LOG_INFO << "Attempting seek <" << mState.mVideoPath << "> @skipMsecsInFile <" << skipMsecsInFile << ">";
Expand Down
4 changes: 3 additions & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
)

add_subdirectory(timelapse-sample)
add_subdirectory(create_thumbnail_from_mp4_video)
add_subdirectory(play_mp4_from_beginning)
add_subdirectory(timelapse-sample)
34 changes: 34 additions & 0 deletions samples/create_thumbnail_from_mp4_video/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.22)
set(TARGET generateThumbnailFromMp4Video)
SET(SAMPLE_FILES
GenerateThumbnailsPipeline.cpp
pipelineMain.cpp
)
SET(SAMPLE_FILES_H
GenerateThumbnailsPipeline.h
)
SET(SOURCE
${SAMPLE_FILES}
${SAMPLE_FILES_H}
)
add_executable(${TARGET} ${SOURCE})
target_include_directories ( ${TARGET} PRIVATE
${JETSON_MULTIMEDIA_LIB_INCLUDE}
${OpenCV_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${LIBMP4_INC_DIR}
${NVCODEC_INCLUDE_DIR}
)
target_link_libraries(
${TARGET}
aprapipes
${JPEG_LIBRARIES}
${LIBMP4_LIB}
${OPENH264_LIB}
${Boost_LIBRARIES}
${FFMPEG_LIBRARIES}
${OpenCV_LIBRARIES}
${NVCUDAToolkit_LIBS}
${NVCODEC_LIB}
${NVJPEGLIB_L4T}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

#include "GenerateThumbnailsPipeline.h"
#include "CudaMemCopy.h"
#include "ExternalSinkModule.h"
#include "FileReaderModule.h"
#include "FileWriterModule.h"
#include "FrameMetadata.h"
#include "H264Decoder.h"
#include "H264Metadata.h"
#include "JPEGEncoderNVJPEG.h"
#include "Logger.h"
#include "Mp4ReaderSource.h"
#include "Mp4VideoMetadata.h"
#include "ValveModule.h"
#include <boost/test/unit_test.hpp>

GenerateThumbnailsPipeline::GenerateThumbnailsPipeline()
: pipeLine("thumnailSamplePipeline") {}

bool GenerateThumbnailsPipeline::setUpPipeLine(
const std::string &videoPath, const std::string &outFolderPath) {
// Implementation

bool parseFS = false;
auto h264ImageMetadata = framemetadata_sp(new H264Metadata(0, 0));
auto frameType = FrameMetadata::FrameType::H264_DATA;
auto mp4ReaderProps =
Mp4ReaderSourceProps(videoPath, parseFS, 0, true, false, false);
//initializing source Mp4 reader to read Mp4 video
mMp4Reader =
boost::shared_ptr<Mp4ReaderSource>(new Mp4ReaderSource(mp4ReaderProps));
mMp4Reader->addOutPutPin(h264ImageMetadata);

auto mp4Metadata = framemetadata_sp(new Mp4VideoMetadata("v_1"));
mMp4Reader->addOutPutPin(mp4Metadata);

std::vector<std::string> mImagePin;
mImagePin = mMp4Reader->getAllOutputPinsByType(frameType);

//initializing H264 decoder to decode frame in H264 format
mDecoder =
boost::shared_ptr<H264Decoder>(new H264Decoder(H264DecoderProps()));
//Selecting an image pin of H264 data frame type is necessary because the decoder processes H264 frames for decoding.
mMp4Reader->setNext(mDecoder, mImagePin);

//Initializing the valve to send only one frame. It is currently set to 0, meaning no frames are captured.
mValve = boost::shared_ptr<ValveModule>(new ValveModule(ValveModuleProps(0)));
mDecoder->setNext(mValve);

//initialize cuda memory
auto stream = cudastream_sp(new ApraCudaStream);
mCudaCopy = boost::shared_ptr<CudaMemCopy>(
new CudaMemCopy(CudaMemCopyProps(cudaMemcpyHostToDevice, stream)));
mValve->setNext(mCudaCopy);

//initializing Jpeg encoder to encode the frame in jpeg format
mJpegEncoder = boost::shared_ptr<JPEGEncoderNVJPEG>(
new JPEGEncoderNVJPEG(JPEGEncoderNVJPEGProps(stream)));
mCudaCopy->setNext(mJpegEncoder);

//initilizing file writer as sink to write frame at given path
mFileWriter = boost::shared_ptr<FileWriterModule>(
new FileWriterModule(FileWriterModuleProps(outFolderPath)));
mJpegEncoder->setNext(mFileWriter);

return true;
}

bool GenerateThumbnailsPipeline::startPipeLine() {
pipeLine.appendModule(mMp4Reader);
if (!pipeLine.init()) {
throw AIPException(
AIP_FATAL,
"Engine Pipeline init failed. Check IPEngine Logs for more details.");
return false;
}
pipeLine.run_all_threaded();
//allowing only one frame to get captured.
mValve->allowFrames(1);

return true;
}

bool GenerateThumbnailsPipeline::stopPipeLine() {
pipeLine.stop();
pipeLine.term();
pipeLine.wait_for_all();
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <PipeLine.h>
#include <ValveModule.h>
#include "Mp4ReaderSource.h"
#include "H264Decoder.h"
#include "ColorConversionXForm.h"
#include "CudaMemCopy.h"
#include "FileWriterModule.h"
#include "JPEGEncoderNVJPEG.h"



class GenerateThumbnailsPipeline
{
public:
GenerateThumbnailsPipeline();
bool setUpPipeLine(const std::string &videoPath,const std::string &outFolderPath);
bool startPipeLine();
bool stopPipeLine();

private:
PipeLine pipeLine;
boost::shared_ptr<ValveModule> mValve;
boost::shared_ptr<Mp4ReaderSource> mMp4Reader;
boost::shared_ptr<H264Decoder> mDecoder;
boost::shared_ptr<ColorConversion> mColorchange;
boost::shared_ptr<CudaMemCopy> mCudaCopy;
boost::shared_ptr<JPEGEncoderNVJPEG> mJpegEncoder;
boost::shared_ptr<FileWriterModule> mFileWriter;
};

32 changes: 32 additions & 0 deletions samples/create_thumbnail_from_mp4_video/pipelineMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "GenerateThumbnailsPipeline.h"
#include <chrono>
#include <iostream>

void main(int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <videoPath> <outFolderPath>"
<< std::endl;
}

std::string videoPath = argv[argc - 2];
std::string outFolderPath = argv[argc - 1];

GenerateThumbnailsPipeline thumbnailPipeline;
if (!thumbnailPipeline.setUpPipeLine(videoPath, outFolderPath)) {
std::cerr << "Failed to setup pipeline." << std::endl;
}

if (!thumbnailPipeline.startPipeLine()) {
std::cerr << "Failed to start pipeline." << std::endl;
}

// Wait for the pipeline to run for 10 seconds
boost::this_thread::sleep_for(boost::chrono::seconds(5));

// Stop the pipeline
if (!thumbnailPipeline.stopPipeLine()) {
std::cerr << "Failed to stop pipeline." << std::endl;
} else {
std::cerr << "Saved Generated Thumbnail in <" << outFolderPath << ">";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "GenerateThumbnailsPipeline.h"
#include "test_utils.h"
#include <boost/test/unit_test.hpp>
#include <chrono>

BOOST_AUTO_TEST_SUITE(generateThumbnails)

BOOST_AUTO_TEST_CASE(generateThumbnails_from_mp4) {
auto generateThumbnailPipeline =
boost::shared_ptr<GenerateThumbnailsPipeline>(
new GenerateThumbnailsPipeline());
std::string videoPath = "../../data/Mp4_videos/h264_video_metadata/20230514/0011/1686723796848.mp4";
std::string outFolderPath = "data/generated_thumbnail/thumbnail_????.jpg";
BOOST_CHECK_NO_THROW(generateThumbnailPipeline->setUpPipeLine(videoPath, outFolderPath));
BOOST_CHECK_NO_THROW(generateThumbnailPipeline->startPipeLine());

boost::this_thread::sleep_for(boost::chrono::seconds(5));

const uint8_t *pReadDataTest = nullptr;
unsigned int readDataSizeTest = 0U;

BOOST_TEST(Test_Utils::readFile("data/test_thumbnail/sample_thumbnail.jpg",
pReadDataTest, readDataSizeTest));
Test_Utils::saveOrCompare(
"data/generated_thumbnail/thumbnail_0000.jpg", pReadDataTest,
readDataSizeTest, 0);

generateThumbnailPipeline->stopPipeLine();
}

BOOST_AUTO_TEST_SUITE_END()
33 changes: 33 additions & 0 deletions samples/play_mp4_from_beginning/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.22)
set(TARGET play_mp4_from_beginning)
SET(SAMPLE_FILES
PlayMp4VideoFromBeginning.cpp
pipelineMain.cpp
)
SET(SAMPLE_FILES_H
PlayMp4VideoFromBeginning.h
)
SET(SOURCE
${SAMPLE_FILES}
${SAMPLE_FILES_H}
)
add_executable(${TARGET} ${SOURCE})
target_include_directories ( ${TARGET} PRIVATE
${OpenCV_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${LIBMP4_INC_DIR}
)
target_link_libraries(
${TARGET}
aprapipes
${JPEG_LIBRARIES}
${LIBMP4_LIB}
${OPENH264_LIB}
${Boost_LIBRARIES}
${OpenCV_LIBRARIES}
${NVCUDAToolkit_LIBS}
${NVJPEGLIB_L4T}
${NVCODEC_LIB}
${FFMPEG_LIBRARIES}
)

Loading

0 comments on commit 7f79097

Please sign in to comment.