#include <gst/gst.h>
#include <glib.h>
#include <iostream>
#include <fstream>
#include <vector>
GstFlowReturn new_sample (GstElement *sink, std::vector<char>* data) {
GstSample *sample;
g_signal_emit_by_name (sink, "pull-sample", &sample);
if (sample) {
GstBuffer *buffer = gst_sample_get_buffer(sample);
gst_buffer_fill(buffer, 0, data->data(), data->size());
gst_sample_unref(sample);
return GST_FLOW_OK;
}
return GST_FLOW_ERROR;
}
int main(int argc, char *argv[]) {
GstElement *pipeline, *appsrc;
/* Initialize GStreamer */
gst_init (&argc, &argv);
/* Build the pipeline */
std::string pipeline_str = "appsrc name=source ! video/quicktime ! decodebin ! videoconvert ! autovideosink";
pipeline = gst_parse_launch(pipeline_str.c_str(), NULL);
/* Setup appsrc */
appsrc = gst_bin_get_by_name(GST_BIN(pipeline), "source");
if(!appsrc) {
std::cout << "Unable to get appsrc" << std::endl;
return -1;
}
/* Read data into buffer */
std::ifstream file("file.mp4", std::ios::binary);
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
/* Setup necessary signals */
g_signal_connect(appsrc, "need-data", (GCallback)new_sample, &buffer);
/* Start playing */
gst_app_src_set_size((GstAppSrc*) appsrc, buffer.size());
gst_element_set_state(pipeline, GST_STATE_PLAYING);
/* Wait until error or EOS */
GstBus *bus;
GstMessage *msg;
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
/* Free resources */
if (msg != NULL)
gst_message_unref(msg);
gst_object_unref(bus);
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
return 0;
}