From 1622fc2b0f130178337517e7b26c47c14d87739e Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Mon, 14 Mar 2022 08:46:51 +0100 Subject: [PATCH] access buf --- CMakeLists.txt | 2 + src/main.cpp | 167 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 164 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34e69f3..f752dcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON) # Find packages go here. find_package(PkgConfig) find_package(Threads REQUIRED) +find_package(OpenSSL REQUIRED) pkg_search_module(GLIB REQUIRED glib-2.0) pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.0) @@ -29,3 +30,4 @@ target_link_libraries(gst-peek ${GSTREAMER_LIBRARIES}) target_link_libraries(gst-peek ${GSTREAMER_APP_LIBRARIES}) target_link_libraries(gst-peek ${GSTREAMER_VIDEO_LIBRARIES} ) target_link_libraries(gst-peek Threads::Threads) +target_link_libraries(gst-peek OpenSSL::Crypto) diff --git a/src/main.cpp b/src/main.cpp index fd58714..4ef8a7e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,24 +1,180 @@ +#include "gst/gstdebugutils.h" +#include #include -#include #include +#include +#include +#include extern "C" { #include #include #include +#include } + struct elements { + GstElement *src; + GstElement *decode; + GstElement *tee; + GstElement *appqueue; + GstElement *videoqueue; + GstElement *appsink; + GstElement *videosink; + }; -void newPipeline(const char * addr) { + struct appsink_data { + EVP_MD_CTX* mdctx; + std::vector digest; + }; - GstElement *pipeline = nullptr; - GstBus *bus = nullptr; +void digest(EVP_MD_CTX* ctx, std::vector data, std::vector& digest) { + +} + +static GstFlowReturn new_sample(GstElement *sink, appsink_data* appsink_data) { + GstSample *sample; + + g_signal_emit_by_name(sink, "pull-sample", &sample); + if (sample) { + g_print("*"); + GstBuffer* buf = gst_sample_get_buffer(sample); + GstMemory* mem = gst_buffer_get_all_memory(buf); + EVP_DigestUpdate(appsink_data->mdctx, &(mem->offset), static_cast(mem->size)); + gst_sample_unref(sample); + return GST_FLOW_OK; + } + + return GST_FLOW_ERROR; +} + +static void cb_new_pad (GstElement *element, GstPad *pad, gpointer data) +{ + gchar *name; + GstElement *other = static_cast(data); + + name = gst_pad_get_name (pad); + g_print ("A new pad %s was created for %s\n", name, gst_element_get_name(element)); + g_free (name); + + g_print ("element %s will be linked to %s\n", + gst_element_get_name(element), + gst_element_get_name(other)); + gst_element_link(element, other); +} + +void newPipeline() { + + GstElement *pipeline; + GstBus *bus; GstMessage *msg = nullptr; + + + struct appsink_data appsink_data; + struct elements pipe; + + appsink_data.mdctx = EVP_MD_CTX_new(); + appsink_data.digest = std::vector(32); + + if(1 != EVP_DigestInit_ex(appsink_data.mdctx, EVP_sha256(), NULL)) { + std::cerr << "Error intializing digest" << std::endl; + } + // gstreamer initialization gst_init(nullptr, nullptr); + pipeline = gst_pipeline_new("peekpipeline"); + + // creating elements + pipe.src = gst_element_factory_make("filesrc", "src"); + pipe.decode = gst_element_factory_make("decodebin", "decode"); + pipe.tee = gst_element_factory_make("tee", "tee"); + pipe.appqueue = gst_element_factory_make("queue", "appqueue"); + pipe.videoqueue = gst_element_factory_make("queue", "videoqueue"); + pipe.appsink = gst_element_factory_make("appsink", "appsink"); + pipe.videosink = gst_element_factory_make("autovideosink", "videosink"); + + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.src))); + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.decode))); + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.tee))); + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.appqueue))); + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.videoqueue))); + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.appsink))); + gst_bin_add(GST_BIN(pipeline), + static_cast(gst_object_ref(pipe.videosink))); + + // linking elements + gst_element_link(pipe.src, pipe.decode); + g_signal_connect (pipe.decode, "pad-added", G_CALLBACK (cb_new_pad), pipe.tee); + + + auto teeSrcTpl = gst_element_class_get_pad_template( + GST_ELEMENT_GET_CLASS(pipe.tee), "src_%u"); + auto apppad = gst_element_request_pad(pipe.tee, teeSrcTpl, nullptr, nullptr); + auto videopad = + gst_element_request_pad(pipe.tee, teeSrcTpl, nullptr, nullptr); + if (gst_pad_link(apppad, gst_element_get_static_pad(pipe.appqueue, "sink")) != + GST_PAD_LINK_OK) { + std::cerr << "Error linking appqueue" << std::endl; + } + + if (gst_pad_link(videopad, gst_element_get_static_pad( + pipe.videoqueue, "sink")) != GST_PAD_LINK_OK) { + std::cerr << "Error linking videoqueue" << std::endl; + } + + gst_element_link(pipe.appqueue, pipe.appsink); + gst_element_link(pipe.videoqueue, pipe.videosink); + + // configuring filesrc + g_object_set(pipe.src, "location", "/home/moritz/Downloads/Big_Buck_Bunny_1080_10s_10MB.mp4", nullptr); + + // configuring appsink + GstCaps *caps = gst_caps_new_simple( + "video/x-raw", NULL); + g_object_set(pipe.appsink, "emit-signals", TRUE, "caps", caps, nullptr); + g_signal_connect (pipe.appsink, "new-sample", G_CALLBACK (new_sample), &appsink_data); + + gst_element_set_state(pipeline, GST_STATE_PLAYING); + + bus = gst_element_get_bus(pipeline); + bool shouldRun = true; + while (shouldRun) { + msg = gst_bus_timed_pop(bus, 10000); + if (msg) { + + switch (GST_MESSAGE_TYPE(msg)) { + case GST_MESSAGE_ERROR: + std::cerr << "Error occured" << std::endl; + shouldRun = false; + break; + case GST_MESSAGE_EOS: + std::cerr << "EOS occured" << std::endl; + shouldRun = false; + break; + default: + break; + } + } + } + + unsigned char hash[EVP_MAX_MD_SIZE]; + unsigned int md_len = EVP_MAX_MD_SIZE; + + EVP_DigestFinal_ex(appsink_data.mdctx, hash, &md_len); + + for (auto& el : hash) + std::cout << std::setfill('0') << std::setw(2) << std::hex << (0xff & (unsigned int)el); + std::cout << '\n'; + return; } @@ -26,6 +182,7 @@ int main(void) { std::cout << "Hello GStreamer-Peek" << std::endl; - + newPipeline(); + return 0; }