#include #include #include #include #include #include #include #include GST_DEBUG_CATEGORY_STATIC(TEST); #define GST_CAT_DEFAULT TEST std::sig_atomic_t got_signal = 0; static void signal_handler(int) { got_signal = 1; } GstElement *create_pipeline(const std::string& file) { GError *error = NULL; std::string description = "filesrc location="; description.append(file); description.append(" ! qtdemux name=d d.video_0 ! h264parse ! mp4mux fragment-duration=1000 ! appsink name=sink drop=false max-buffers=10"); return gst_parse_launch(description.c_str(), &error); } void set_state_blocking(GstElement* e, GstState state) { gst_element_set_state(e, state); auto new_state = GST_STATE_NULL; while (new_state != state) { gst_element_get_state(e, &new_state, nullptr, GST_CLOCK_TIME_NONE); } } int main(int argc, char *argv[]) { std::signal(SIGTERM, signal_handler); std::signal(SIGINT, signal_handler); if (!gst_init_check(&argc, &argv, NULL)) { fprintf(stderr, "FATAL: Failed to initialize gstreamer\n"); return -1; } GST_DEBUG_CATEGORY_INIT(TEST, "TEST", 0, NULL); GstElement* pipeline = create_pipeline(argv[1]); set_state_blocking(GST_ELEMENT(pipeline), GST_STATE_PLAYING); std::thread app_thread{[pipeline]() -> void { GstElement* appsink = gst_bin_get_by_name(GST_BIN(pipeline), "sink"); GstSample* video_sample = nullptr; // PULL some data first for (int i =0 ; i < 100; ++i) { video_sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink)); if (video_sample) gst_sample_unref(video_sample); } GST_ERROR("SEEK"); if (!gst_element_seek( pipeline, 1.0, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT), GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_NONE, 0)) { GST_ERROR("SEEK FAILED"); } GST_ERROR("PULL"); video_sample = gst_app_sink_pull_sample(GST_APP_SINK(appsink)); if (video_sample) gst_sample_unref(video_sample); GST_ERROR("PULL DONE"); gst_object_unref(appsink); }}; while (got_signal == 0) { g_main_context_iteration(nullptr, true); } set_state_blocking(pipeline, GST_STATE_NULL); if (pipeline) gst_object_unref(pipeline); app_thread.join(); return 0; }