Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gdk-pixbuf/gdk-pixbuf.h>
- #include <gst/gst.h>
- #include <gst/app/gstappsrc.h>
- #include <gst/video/video.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct {
- GstPipeline *pipeline;
- GstElement *appsrc;
- GstElement *src;
- GstElement *encoder;
- GstElement *streamer;
- GMainLoop *loop;
- GTimer* timer;
- guint sourceid;
- } gst_app_t;
- static gst_app_t gst_app;
- static gboolean read_data(gst_app_t* app) {
- GstFlowReturn ret;
- gdouble ms;
- static gboolean white = FALSE;
- ms = g_timer_elapsed(app->timer, NULL);
- //GST_INFO("Entering read_data(app=%p), elapsed seconds=%f.", app, ms);
- if (ms > 1.0/15.0) {
- GstBuffer* buffer;
- gboolean ok = TRUE;
- buffer = gst_buffer_new();
- GST_INFO("Pushing new data into pipe, elapsed seconds=%f.", ms);
- // this makes the image black/white
- memset(GST_BUFFER_DATA(buffer), white ? 0xff : 0x0, GST_BUFFER_SIZE(buffer));
- GST_BUFFER_SIZE(buffer) = 320 * 240 * 3 * sizeof(guchar);
- white = !white;
- g_signal_emit_by_name(app->appsrc, "push-buffer", buffer, &ret);
- gst_buffer_unref(buffer);
- if (GST_FLOW_OK != ret) {
- // some error, stop sending data
- GST_INFO("some error: %d", ret);
- ok = FALSE;
- }
- g_timer_start(app->timer);
- }
- //GST_INFO("Exiting read_data(app=%p).", app);
- return TRUE;
- }
- static void start_feed(GstElement* pipeline, guint size, gst_app_t* app) {
- GST_INFO("Entering start_feed(pipeline=%p, size=%d, app=%p).", pipeline, size, app);
- if (0 == app->sourceid) {
- GST_INFO("Start feeding");
- app->sourceid = g_idle_add((GSourceFunc)read_data, app);
- }
- }
- static void stop_feed(GstElement* pipeline, gst_app_t* app) {
- GST_INFO("Entering stop_feed(pipeline=%p, app=%p).", pipeline, app);
- if (0 != app->sourceid) {
- GST_INFO("Stop feeding");
- g_source_remove(app->sourceid);
- app->sourceid = 0;
- }
- }
- static gboolean bus_message(GstBus* bus, GstMessage* message, gst_app_t* app) {
- GST_INFO("Entering bus_message(bus=%p, message=%s, app=%p).", bus, gst_message_type_get_name(GST_MESSAGE_TYPE(message)), app);
- switch(GST_MESSAGE_TYPE(message)) {
- case GST_MESSAGE_ERROR: {
- GError* err = NULL;
- gchar* dbg_info = NULL;
- gst_message_parse_error(message, &err, &dbg_info);
- g_printerr("ERROR from element %s: %s\n",
- GST_OBJECT_NAME(message->src), err->message);
- g_printerr("Debugging info: %s\n",(dbg_info) ? dbg_info : "none");
- g_error_free(err);
- g_free(dbg_info);
- g_main_loop_quit(app->loop);
- break;
- }
- case GST_MESSAGE_EOS:
- GST_INFO("End-of-stream message received, quitting main loop.");
- g_main_loop_quit(app->loop);
- break;
- default:
- break;
- }
- return TRUE;
- }
- int main(int argc, char* argv[]) {
- gst_app_t* app = &gst_app;
- GstBus* bus;
- GstCaps* caps;
- int USE_TESTSRC = 0;
- int i = 0;
- for (i = 0; i < argc; i++) {
- if (!strcmp(argv[i], "--testsrc")) {
- USE_TESTSRC = 1;
- }
- }
- gst_init(&argc, &argv);
- app->loop = g_main_loop_new(NULL, TRUE);
- app->timer = g_timer_new();
- if (USE_TESTSRC) {
- app->pipeline = (GstPipeline*)gst_parse_launch("videotestsrc ! videorate ! ffmpegcolorspace ! videoscale method=1 ! video/x-raw-yuv,width=320,height=240,format=(fourcc)UYVY,framerate=\(fraction\)15/1 ! TIVidenc1 codecName=h264enc engineName=codecServer ! rtph264pay pt=96 ! udpsink host=192.168.1.112 port=5000", NULL);
- } else {
- app->pipeline = (GstPipeline*)gst_parse_launch("appsrc name=mysrc ! videorate ! ffmpegcolorspace ! videoscale method=1 ! video/x-raw-yuv,width=320,height=240,format=(fourcc)UYVY,framerate=\(fraction\)15/1 ! TIVidenc1 codecName=h264enc engineName=codecServer ! rtph264pay pt=96 ! udpsink host=192.168.1.112 port=5000", NULL);
- }
- g_assert (app->pipeline);
- bus = gst_pipeline_get_bus(GST_PIPELINE(app->pipeline));
- g_assert(bus);
- gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
- if (!USE_TESTSRC) {
- app->appsrc = gst_bin_get_by_name (GST_BIN(app->pipeline), "mysrc");
- g_assert(app->appsrc);
- g_assert(GST_IS_APP_SRC(app->appsrc));
- g_signal_connect (app->appsrc, "need-data", G_CALLBACK (start_feed), app);
- g_signal_connect (app->appsrc, "enough-data", G_CALLBACK (stop_feed), app);
- caps = gst_video_format_new_caps(GST_VIDEO_FORMAT_UYVY, 320, 240, 0, 1, 4, 3);
- gst_app_src_set_caps(GST_APP_SRC(app->appsrc), caps);
- //gst_app_src_set_max_bytes(GST_APP_SRC(app->appsrc), 320 * 240 * 3 * 100);
- }
- GST_INFO("Setting pipeline %p to playing.", app->pipeline);
- gst_element_set_state ((GstElement*)app->pipeline, GST_STATE_PLAYING);
- g_main_loop_run (app->loop);
- GST_INFO("Stopping pipeline %p.", app->pipeline);
- gst_element_set_state ((GstElement*)app->pipeline, GST_STATE_NULL);
- gst_object_unref (bus);
- g_main_loop_unref (app->loop);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement