Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kvs/sink_gstreamer_sample): Use videotestsrc and add additional debug logging #1047

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions samples/kvs_gstreamer_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,10 @@ static bool format_supported_by_source(GstCaps *src_caps, GstCaps *query_caps, i
static bool resolution_supported(GstCaps *src_caps, GstCaps *query_caps_raw, GstCaps *query_caps_h264,
CustomData &data, int width, int height, int framerate) {
if (query_caps_h264 && format_supported_by_source(src_caps, query_caps_h264, width, height, framerate)) {
LOG_DEBUG("src supports h264")
data.h264_stream_supported = true;
} else if (query_caps_raw && format_supported_by_source(src_caps, query_caps_raw, width, height, framerate)) {
LOG_DEBUG("src supports raw")
data.h264_stream_supported = false;
} else {
return false;
Expand Down Expand Up @@ -687,28 +689,66 @@ int gstreamer_live_source_init(int argc, char* argv[], CustomData *data, GstElem
gst-launch-1.0 v4l2src device=/dev/video0 ! video/x-raw,format=I420,width=1280,height=720,framerate=15/1 ! x264enc pass=quant bframes=0 ! video/x-h264,profile=baseline,format=I420,width=1280,height=720,framerate=15/1 ! matroskamux ! filesink location=test.mkv
*/
source_filter = gst_element_factory_make("capsfilter", "source_filter");
if (!source_filter) {
LOG_ERROR("Failed to create capsfilter (1)");
return 1;
}
filter = gst_element_factory_make("capsfilter", "encoder_filter");
if (!filter) {
LOG_ERROR("Failed to create capsfilter (2)");
return 1;
}
appsink = gst_element_factory_make("appsink", "appsink");
if (!appsink) {
LOG_ERROR("Failed to create appsink");
return 1;
}
h264parse = gst_element_factory_make("h264parse", "h264parse"); // needed to enforce avc stream format
if (!h264parse) {
LOG_ERROR("Failed to create h264parse");
return 1;
}

// Attempt to create vtenc encoder
encoder = gst_element_factory_make("vtenc_h264_hw", "encoder");
if (encoder) {
source = gst_element_factory_make("autovideosrc", "source");
vtenc = true;
source = gst_element_factory_make("videotestsrc", "source");
if (source) {
LOG_DEBUG("Using videotestsrc");
} else {
LOG_ERROR("Failed to create videotestsrc");
return 1;
}
} else {
// Failed creating vtenc - check pi hardware encoder
encoder = gst_element_factory_make("omxh264enc", "encoder");
if (encoder) {
LOG_DEBUG("Using omxh264enc");
isOnRpi = true;
} else {
// - attempt x264enc
encoder = gst_element_factory_make("x264enc", "encoder");
isOnRpi = false;
encoder = gst_element_factory_make("x264enc", "encoder");
if (encoder) {
LOG_DEBUG("Using x264enc");
} else {
LOG_ERROR("Failed to create x264enc");
return 1;
}
}
source = gst_element_factory_make("v4l2src", "source");
if (!source) {
if (source) {
LOG_DEBUG("Using v4l2src");
} else {
LOG_DEBUG("Failed to create v4l2src, trying ksvideosrc")
source = gst_element_factory_make("ksvideosrc", "source");
sirknightj marked this conversation as resolved.
Show resolved Hide resolved
sirknightj marked this conversation as resolved.
Show resolved Hide resolved
if (source) {
LOG_DEBUG("Using ksvideosrc");
} else {
LOG_ERROR("Failed to create ksvideosrc");
return 1;
}
}
vtenc = false;
}
Expand All @@ -719,7 +759,9 @@ int gstreamer_live_source_init(int argc, char* argv[], CustomData *data, GstElem
}

/* configure source */
if (!vtenc) {
if (vtenc) {
g_object_set(G_OBJECT (source), "is-live", TRUE, NULL);
} else {
g_object_set(G_OBJECT (source), "do-timestamp", TRUE, "device", "/dev/video0", NULL);
}

Expand Down Expand Up @@ -833,6 +875,7 @@ int gstreamer_live_source_init(int argc, char* argv[], CustomData *data, GstElem

/* build the pipeline */
if (!data->h264_stream_supported) {
LOG_DEBUG("Constructing pipeline with encoding element")
gst_bin_add_many(GST_BIN (pipeline), source, video_convert, source_filter, encoder, h264parse, filter,
appsink, NULL);
if (!gst_element_link_many(source, video_convert, source_filter, encoder, h264parse, filter, appsink, NULL)) {
Expand All @@ -841,6 +884,7 @@ int gstreamer_live_source_init(int argc, char* argv[], CustomData *data, GstElem
return 1;
}
} else {
LOG_DEBUG("Constructing pipeline without encoding element")
gst_bin_add_many(GST_BIN (pipeline), source, source_filter, h264parse, filter, appsink, NULL);
if (!gst_element_link_many(source, source_filter, h264parse, filter, appsink, NULL)) {
g_printerr("Elements could not be linked.\n");
Expand Down Expand Up @@ -1114,7 +1158,10 @@ int main(int argc, char* argv[]) {
LOG_DEBUG("Attempt to upload file: " << data.file_list[i].path);

// control will return after gstreamer_init after file eos or any GST_ERROR was put on the bus.
gstreamer_init(argc, argv, &data);
if (gstreamer_init(argc, argv, &data) != 0) {
LOG_ERROR("Failed to initialize gstreamer");
return 1;
}

// check if any stream error occurred.
stream_status = data.stream_status.load();
Expand Down Expand Up @@ -1164,7 +1211,10 @@ int main(int argc, char* argv[]) {

} else {
// non file uploading scenario
gstreamer_init(argc, argv, &data);
if (gstreamer_init(argc, argv, &data) != 0) {
LOG_ERROR("Failed to initialize gstreamer");
return 1;
}
if (STATUS_SUCCEEDED(stream_status)) {
// if stream_status is success after eos, send out remaining frames.
data.kinesis_video_stream->stopSync();
Expand Down
60 changes: 54 additions & 6 deletions samples/kvssink_gstreamer_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ static bool format_supported_by_source(GstCaps *src_caps, GstCaps *query_caps, i
static bool resolution_supported(GstCaps *src_caps, GstCaps *query_caps_raw, GstCaps *query_caps_h264,
CustomData &data, int width, int height, int framerate) {
if (query_caps_h264 && format_supported_by_source(src_caps, query_caps_h264, width, height, framerate)) {
LOG_DEBUG("src supports h264")
data.h264_stream_supported = true;
} else if (query_caps_raw && format_supported_by_source(src_caps, query_caps_raw, width, height, framerate)) {
LOG_DEBUG("src supports raw")
data.h264_stream_supported = false;
} else {
return false;
Expand Down Expand Up @@ -333,28 +335,66 @@ int gstreamer_live_source_init(int argc, char *argv[], CustomData *data, GstElem

/* create the elemnents */
source_filter = gst_element_factory_make("capsfilter", "source_filter");
if (!source_filter) {
LOG_ERROR("Failed to create capsfilter (1)");
return 1;
}
filter = gst_element_factory_make("capsfilter", "encoder_filter");
if (!filter) {
LOG_ERROR("Failed to create capsfilter (2)");
return 1;
}
kvssink = gst_element_factory_make("kvssink", "kvssink");
if (!kvssink) {
LOG_ERROR("Failed to create kvssink");
return 1;
}
h264parse = gst_element_factory_make("h264parse", "h264parse"); // needed to enforce avc stream format
if (!h264parse) {
LOG_ERROR("Failed to create h264parse");
return 1;
}

// Attempt to create vtenc encoder
encoder = gst_element_factory_make("vtenc_h264_hw", "encoder");
if (encoder) {
source = gst_element_factory_make("autovideosrc", "source");
vtenc = true;
source = gst_element_factory_make("videotestsrc", "source");
if (source) {
LOG_DEBUG("Using videotestsrc");
} else {
LOG_ERROR("Failed to create videotestsrc");
return 1;
}
} else {
// Failed creating vtenc - check pi hardware encoder
encoder = gst_element_factory_make("omxh264enc", "encoder");
if (encoder) {
LOG_DEBUG("Using omxh264enc")
isOnRpi = true;
} else {
// - attempt x264enc
encoder = gst_element_factory_make("x264enc", "encoder");
isOnRpi = false;
encoder = gst_element_factory_make("x264enc", "encoder");
if (encoder) {
LOG_DEBUG("Using x264enc");
} else {
LOG_ERROR("Failed to create x264enc");
return 1;
}
}
source = gst_element_factory_make("v4l2src", "source");
if (!source) {
if (source) {
LOG_DEBUG("Using v4l2src");
} else {
LOG_DEBUG("Failed to create v4l2src, trying ksvideosrc")
source = gst_element_factory_make("ksvideosrc", "source");
if (source) {
LOG_DEBUG("Using ksvideosrc");
} else {
LOG_ERROR("Failed to create ksvideosrc");
return 1;
}
}
vtenc = false;
}
Expand All @@ -365,7 +405,9 @@ int gstreamer_live_source_init(int argc, char *argv[], CustomData *data, GstElem
}

/* configure source */
if (!vtenc) {
if (vtenc) {
g_object_set(G_OBJECT(source), "is-live", TRUE, NULL);
} else {
g_object_set(G_OBJECT(source), "do-timestamp", TRUE, "device", "/dev/video0", NULL);
}

Expand Down Expand Up @@ -482,12 +524,14 @@ int gstreamer_live_source_init(int argc, char *argv[], CustomData *data, GstElem
if (!data->h264_stream_supported) {
gst_bin_add_many(GST_BIN(pipeline), source, video_convert, source_filter, encoder, h264parse, filter,
kvssink, NULL);
LOG_DEBUG("Constructing pipeline with encoding element")
if (!gst_element_link_many(source, video_convert, source_filter, encoder, h264parse, filter, kvssink, NULL)) {
g_printerr("Elements could not be linked.\n");
gst_object_unref(pipeline);
return 1;
}
} else {
LOG_DEBUG("Constructing pipeline without encoding element")
gst_bin_add_many(GST_BIN(pipeline), source, source_filter, h264parse, filter, kvssink, NULL);
if (!gst_element_link_many(source, source_filter, h264parse, filter, kvssink, NULL)) {
g_printerr("Elements could not be linked.\n");
Expand Down Expand Up @@ -772,7 +816,9 @@ int main(int argc, char *argv[]) {
LOG_DEBUG("Attempt to upload file: " << data_global.file_list[i].path);

// control will return after gstreamer_init after file eos or any GST_ERROR was put on the bus.
gstreamer_init(argc, argv, &data_global);
if (gstreamer_init(argc, argv, &data_global) != 0) {
return 1;
}

// check if any stream error occurred.
stream_status = data_global.stream_status.load();
Expand Down Expand Up @@ -816,7 +862,9 @@ int main(int argc, char *argv[]) {

} else {
// non file uploading scenario
gstreamer_init(argc, argv, &data_global);
if (gstreamer_init(argc, argv, &data_global) != 0) {
return 1;
}
stream_status = data_global.stream_status.load();
if (STATUS_SUCCEEDED(stream_status)) {
LOG_INFO("Stream succeeded");
Expand Down