Skip to content

Commit

Permalink
Add C++ streaming transcoder
Browse files Browse the repository at this point in the history
  • Loading branch information
squishyhuman committed Oct 5, 2023
1 parent 49bb388 commit 36022f7
Show file tree
Hide file tree
Showing 10 changed files with 1,621 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# Generated libraries
/frontend/public/motion_tracker
/frontend/public/stream_decoder.*
/frontend/public/stream_encoder.*

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
141 changes: 141 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ set(CMAKE_CXX_STANDARD 17)

find_package(OpenCV REQUIRED)

#
# FFmpeg
#

find_path(AVCODEC_INCLUDE_DIR libavcodec/avcodec.h PATHS "${CMAKE_PREFIX_PATH}/include")
find_path(AVFORMAT_INCLUDE_DIR libavformat/avformat.h PATHS "${CMAKE_PREFIX_PATH}/include")
find_path(AVUTIL_INCLUDE_DIR libavutil/avutil.h PATHS "${CMAKE_PREFIX_PATH}/include")

find_library(AVCODEC_LIBRARY avcodec PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(AVFILTER_LIBRARY avfilter PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(AVFORMAT_LIBRARY avformat PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(AVUTIL_LIBRARY avutil PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(LAME_LIBRARY mp3lame PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(OPENH264_LIBRARY openh264 PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(OPUS_LIBRARY opus PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(SWRESAMPLE_LIBRARY swresample PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(SWSCALE_LIBRARY swscale PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(VPX_LIBRARY vpx PATHS "${CMAKE_PREFIX_PATH}/lib")
find_library(X264_LIBRARY x264 PATHS "${CMAKE_PREFIX_PATH}/lib")

################################################################################
#
# Define sources
Expand All @@ -65,6 +85,27 @@ set(MOTION_TRACKER_SOURCES
utils/math_utils.cpp
)

#
# Stream decoder
#

set(STREAM_DECODER_SOURCES
stream_decoder/stream_decoder.cpp
stream_decoder/stream_decoder_embinder.cpp
utils/emscripten_utils.cpp
)

#
# Stream encoder
#

set(STREAM_ENCODER_SOURCES
stream_encoder/stream_encoder.cpp
stream_encoder/stream_encoder_embinder.cpp
utils/emscripten_utils.cpp
video/video_frame.cpp
)

################################################################################
#
# Build libraries
Expand All @@ -91,17 +132,21 @@ string(APPEND EMSCRIPTEN_LINK_FLAGS
# " --pre-js pre-module.j "
# " --post-js post-module.j "
"-s ALLOW_MEMORY_GROWTH=1 "
"-s ALLOW_TABLE_GROWTH=1 "
"-s ASSERTIONS=1 "
# " -s DEMANGLE_SUPPORT=1 "
# " -s DISABLE_EXCEPTION_CATCHING=0 "
"-s ERROR_ON_UNDEFINED_SYMBOLS=0 "
"-s EXPORTED_RUNTIME_METHODS='[\"addFunction\"]' "
"-s EXTRA_EXPORTED_RUNTIME_METHODS='[\"addFunction\"]' "
# " -s FULL_ES3=1 "
# " -s GL_ASSERTIONS=1 "
# " -s GL_UNSAFE_OPTS=0 "
# " -s INVOKE_RUN=0 "
# " -s LEGACY_GL_EMULATION=0 "
#"-s LLD_REPORT_UNDEFINED "
# " -s OFFSCREENCANVAS_SUPPORT=1 "
"-s RESERVED_FUNCTION_POINTERS=10 "
# " -s SAFE_HEAP=1 "
#"-s TOTAL_MEMORY=67108864 "
# " -s USE_FREETYPE=1 "
Expand Down Expand Up @@ -147,6 +192,78 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
)
endif ()

#
# Stream decoder
#

add_executable(stream_decoder
${STREAM_DECODER_SOURCES}
)

target_include_directories(stream_decoder PRIVATE
${AVCODEC_INCLUDE_DIR}
${AVFORMAT_INCLUDE_DIR}
${AVUTIL_INCLUDE_DIR}
)

target_link_libraries(stream_decoder PRIVATE
${AVCODEC_LIBRARY}
${AVFORMAT_LIBRARY}
${AVUTIL_LIBRARY}
${LAME_LIBRARY}
${OPENH264_LIBRARY}
${OPUS_LIBRARY}
${SWRESAMPLE_LIBRARY}
${SWSCALE_LIBRARY}
${VPX_LIBRARY}
${X264_LIBRARY}
)

if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
set_target_properties(stream_decoder PROPERTIES
COMPILE_FLAGS " \
-Wno-deprecated \
-s ASSERTIONS=1 \
"
LINK_FLAGS ${EMSCRIPTEN_LINK_FLAGS})
endif ()

#
# Stream encoder
#

add_executable(stream_encoder
${STREAM_ENCODER_SOURCES}
)

target_include_directories(stream_encoder PRIVATE
${AVCODEC_INCLUDE_DIR}
${AVFORMAT_INCLUDE_DIR}
${AVUTIL_INCLUDE_DIR}
)

target_link_libraries(stream_encoder PRIVATE
${AVCODEC_LIBRARY}
${AVFORMAT_LIBRARY}
${AVUTIL_LIBRARY}
${LAME_LIBRARY}
${OPENH264_LIBRARY}
${OPUS_LIBRARY}
${SWRESAMPLE_LIBRARY}
${SWSCALE_LIBRARY}
${VPX_LIBRARY}
${X264_LIBRARY}
)

if (${CMAKE_SYSTEM_NAME} MATCHES "Emscripten")
set_target_properties(stream_encoder PROPERTIES
COMPILE_FLAGS " \
-Wno-deprecated \
-s ASSERTIONS=1 \
"
LINK_FLAGS ${EMSCRIPTEN_LINK_FLAGS})
endif ()

################################################################################
#
# Install libraries
Expand All @@ -165,3 +282,27 @@ INSTALL(
DESTINATION
motion_tracker
)

#
# Stream decoder
#

INSTALL(
FILES
"${CMAKE_BINARY_DIR}/stream_decoder.js"
"${CMAKE_BINARY_DIR}/stream_decoder.wasm"
DESTINATION
"${CMAKE_INSTALL_PREFIX}"
)

#
# Stream encoder
#

INSTALL(
FILES
"${CMAKE_BINARY_DIR}/stream_encoder.js"
"${CMAKE_BINARY_DIR}/stream_encoder.wasm"
DESTINATION
"${CMAKE_INSTALL_PREFIX}"
)
Loading

0 comments on commit 36022f7

Please sign in to comment.