demo version

This commit is contained in:
Josip Milovac 2023-07-13 11:32:02 +10:00
parent fbb282a801
commit 672d6daa8e
125 changed files with 17918 additions and 1481 deletions

View file

@ -0,0 +1,22 @@
cmake_minimum_required (VERSION 3.8)
find_package(OpenCV REQUIRED)
find_package(PahoMqttCpp CONFIG REQUIRED)
include(FindFFmpeg.cmake)
add_executable(demo_show_video
"src/demo_show_video.cpp")
target_include_directories(demo_show_video PUBLIC
${OpenCV_INCLUDE_DIRS}
${FFMPEG_INCLUDE_DIRS})
target_link_libraries(demo_show_video PRIVATE
${FFMPEG_LIBRARIES}
${OpenCV_LIBRARIES}
PahoMqttCpp::paho-mqttpp3)
target_compile_features(demo_show_video PUBLIC
cxx_std_17)

View file

@ -0,0 +1,39 @@
# rules for finding the FFmpeg libraries
if(WIN32)
find_package(FFMPEG)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_FFMPEG REQUIRED libavformat libavcodec libavutil libswscale)
find_path(AVFORMAT_INCLUDE_DIR libavformat/avformat.h HINTS ${PC_FFMPEG_LIBAVFORMAT_INCLUDEDIR} ${PC_FFMPEG_INCLUDE_DIRS})
find_library(AVFORMAT_LIBRARY NAMES libavformat avformat HINTS ${PC_FFMPEG_LIBAVFORMAT_LIBDIR} ${PC_FFMPEG_LIBRARY_DIRS})
find_path(AVCODEC_INCLUDE_DIR libavcodec/avcodec.h HINTS ${PC_FFMPEG_LIBAVCODEC_INCLUDEDIR} ${PC_FFMPEG_INCLUDE_DIRS})
find_library(AVCODEC_LIBRARY NAMES libavcodec avcodec HINTS ${PC_FFMPEG_LIBAVCODEC_LIBDIR} ${PC_FFMPEG_LIBRARY_DIRS})
find_path(AVUTIL_INCLUDE_DIR libavutil/avutil.h HINTS ${PC_FFMPEG_LIBAVUTIL_INCLUDEDIR} ${PC_FFMPEG_INCLUDE_DIRS})
find_library(AVUTIL_LIBRARY NAMES libavutil avutil HINTS ${PC_FFMPEG_LIBAVUTIL_LIBDIR} ${PC_FFMPEG_LIBRARY_DIRS})
find_path(SWSCALE_INCLUDE_DIR libswscale/swscale.h HINTS ${PC_FFMPEG_LIBSWSCALE_INCLUDEDIR} ${PC_FFMPEG_INCLUDE_DIRS})
find_library(SWSCALE_LIBRARY NAMES libawscale swscale HINTS ${PC_FFMPEG_LIBSWSCALE_LIBDIR} ${PC_FFMPEG_LIBRARY_DIRS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(AVFormat DEFAULT_MSG AVFORMAT_LIBRARY AVFORMAT_INCLUDE_DIR)
find_package_handle_standard_args(AVCodec DEFAULT_MSG AVCODEC_LIBRARY AVCODEC_INCLUDE_DIR)
find_package_handle_standard_args(AVUtil DEFAULT_MSG AVUTIL_LIBRARY AVUTIL_INCLUDE_DIR)
find_package_handle_standard_args(SWScale DEFAULT_MSG SWSCALE_LIBRARY SWSCALE_INCLUDE_DIR)
mark_as_advanced(AVFORMAT_INCLUDE_DIR AVFORMAT_LIBRARY)
mark_as_advanced(AVCODEC_INCLUDE_DIR AVCODEC_LIBRARY)
mark_as_advanced(AVUTIL_INCLUDE_DIR AVUTIL_LIBRARY)
mark_as_advanced(SWSCALE_INCLUDE_DIR SWSCALE_LIBRARY)
set(FFMPEG_INCLUDE_DIRS ${AVFORMAT_INCLUDE_DIR} ${AVCODEC_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR} ${SWSCALE_INCLUDE_DIR})
set(FFMPEG_LIBRARIES ${AVFORMAT_LIBRARY} ${AVCODEC_LIBRARY} ${AVUTIL_LIBRARY} ${SWSCALE_LIBRARY})
if(${AVFORMAT_FOUND} AND ${AVCODEC_FOUND} AND ${AVUTIL_FOUND} AND ${SWSCALE_FOUND})
set(FFMPEG_FOUND TRUE)
else()
set(FFMPEG_FOUND FALSE)
endif()
endif()

View file

@ -0,0 +1,78 @@
#include <cstdio>
#include <vector>
#include <deque>
#include <fstream>
#include <thread>
#include <opencv2/opencv.hpp>
#include "mqtt/client.h"
int main(int argc, const char** argv) {
if (argc <= 3) {
fprintf(stderr, "Expected %s <header location> <broker location> <channel name>\n", argv[0]);
return -1;
}
const char* const header_location = argv[1]; //"C:\\users\\dekibeki\\work\\video_header";//
const char* const broker_location = argv[2]; //"tcp://136.186.108.94:5004";//
const char* const channel_name = argv[3]; //"out/873304a31447291aa9a701bfdfb7076f35f070a7a1473521610bfa6a77858569/0";//
int in = 0;
std::vector<char> header;
std::ifstream file{ header_location, std::ios_base::binary };
while ((in = file.get()) != std::ifstream::traits_type::eof()) {
header.push_back(static_cast<std::uint8_t>(in));
}
if (header.empty()) {
fprintf(stderr, "Empty/non-existent header at `%s`", header_location);
return -1;
}
mqtt::client mqtt_client(broker_location, "demo show video");
auto connOpts = mqtt::connect_options_builder()
.keep_alive_interval(std::chrono::seconds(30))
.automatic_reconnect(std::chrono::seconds(2), std::chrono::seconds(30))
.clean_session(false)
.finalize();
for (;;) {
mqtt::connect_response rsp = mqtt_client.connect(connOpts);
if (!rsp.is_session_present()) {
mqtt_client.subscribe(channel_name);
}
for (std::size_t i = 0;;++i) {
auto msg = mqtt_client.consume_message();
if (msg) {
fprintf(stderr, "New segment: %zd\n", i);
const mqtt::string data = msg->to_string();
FILE* temp_file = fopen("./test.webm", "wb");
fwrite(header.data(), 1, header.size(), temp_file);
fwrite(data.data(), 1, data.size(), temp_file);
fclose(temp_file);
cv::VideoCapture reader("./test.webm");
cv::Mat frame;
while (reader.read(frame)) {
cv::imshow(channel_name, frame);
cv::waitKey(1000 / 5);
}
} else if (!mqtt_client.is_connected()) {
fprintf(stderr, "No connection, sleeping\n");
while (!mqtt_client.is_connected()) {
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
fprintf(stderr, "Reconnected\n");
}
}
}
}