access buf

This commit is contained in:
2022-03-14 08:46:51 +01:00
parent 682f843b38
commit 1622fc2b0f
2 changed files with 164 additions and 5 deletions

View File

@@ -10,6 +10,7 @@ add_definitions(-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
# Find packages go here. # Find packages go here.
find_package(PkgConfig) find_package(PkgConfig)
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
pkg_search_module(GLIB REQUIRED glib-2.0) pkg_search_module(GLIB REQUIRED glib-2.0)
pkg_check_modules(GSTREAMER REQUIRED gstreamer-1.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_APP_LIBRARIES})
target_link_libraries(gst-peek ${GSTREAMER_VIDEO_LIBRARIES} ) target_link_libraries(gst-peek ${GSTREAMER_VIDEO_LIBRARIES} )
target_link_libraries(gst-peek Threads::Threads) target_link_libraries(gst-peek Threads::Threads)
target_link_libraries(gst-peek OpenSSL::Crypto)

View File

@@ -1,23 +1,179 @@
#include "gst/gstdebugutils.h"
#include <cstdint>
#include <iostream> #include <iostream>
#include <thread>
#include <memory> #include <memory>
#include <thread>
#include <vector>
#include <iomanip>
extern "C" { extern "C" {
#include <glib.h> #include <glib.h>
#include <gst/gst.h> #include <gst/gst.h>
#include <unistd.h> #include <unistd.h>
#include <openssl/evp.h>
} }
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<uint8_t> digest;
};
GstElement *pipeline = nullptr; void digest(EVP_MD_CTX* ctx, std::vector<uint8_t> data, std::vector<uint8_t>& digest) {
GstBus *bus = nullptr;
}
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<size_t>(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<GstElement*>(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; GstMessage *msg = nullptr;
struct appsink_data appsink_data;
struct elements pipe;
appsink_data.mdctx = EVP_MD_CTX_new();
appsink_data.digest = std::vector<uint8_t>(32);
if(1 != EVP_DigestInit_ex(appsink_data.mdctx, EVP_sha256(), NULL)) {
std::cerr << "Error intializing digest" << std::endl;
}
// gstreamer initialization // gstreamer initialization
gst_init(nullptr, nullptr); 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<GstElement *>(gst_object_ref(pipe.src)));
gst_bin_add(GST_BIN(pipeline),
static_cast<GstElement *>(gst_object_ref(pipe.decode)));
gst_bin_add(GST_BIN(pipeline),
static_cast<GstElement *>(gst_object_ref(pipe.tee)));
gst_bin_add(GST_BIN(pipeline),
static_cast<GstElement *>(gst_object_ref(pipe.appqueue)));
gst_bin_add(GST_BIN(pipeline),
static_cast<GstElement *>(gst_object_ref(pipe.videoqueue)));
gst_bin_add(GST_BIN(pipeline),
static_cast<GstElement *>(gst_object_ref(pipe.appsink)));
gst_bin_add(GST_BIN(pipeline),
static_cast<GstElement *>(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; return;
} }
@@ -26,6 +182,7 @@ int main(void) {
std::cout << "Hello GStreamer-Peek" << std::endl; std::cout << "Hello GStreamer-Peek" << std::endl;
newPipeline();
return 0; return 0;
} }