Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* filesrc_to_rtmpsink.cpp
- * Created on: 24-Aug-2012
- * Author: dev*/
- //#include <gst/gst.h>
- /////////////////////////////////////////////////// 1
- #include <gst/gst.h>
- #include <gst/app/gstappsink.h>
- #include <gst/app/gstappsrc.h>
- ///////////////////////////////////////////////////
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @2
- #include <cv.h>
- #include <highgui.h>
- #include <iostream>
- using namespace cv;
- using namespace std;
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'
- #include <stdio.h>
- /////////////////////////////////////////////////// 2
- static GMainLoop *loop = NULL;
- static GstElement *pipeline1 = NULL;
- static GstElement *pipeline2 = NULL;
- ///////////////////////////////////////////////////
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @1
- static int IMG_width = 480;
- static int IMG_height = 360;
- static IplImage *img;
- static uchar *IMG_data;
- //double scale = 4;
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- /////////////////////////////////////////////////// 3
- static const char app_src_name[] = "app-src_01";
- static const char app_sink_name[] = "app-sink_01";
- ///////////////////////////////////////////////////
- /////////////////////////////////////////////////// 4
- static GstFlowReturn new_buffer (GstAppSink *app_sink, gpointer user_data){
- GstBuffer *buffer = gst_app_sink_pull_buffer( (GstAppSink*) gst_bin_get_by_name( GST_BIN(pipeline1), app_sink_name));
- // //=======================================================================
- //
- g_debug("appsink: buffer timestamp(%llu)\t duration(%llu)\t offset(%llu)\t size(%d)\n", GST_BUFFER_TIMESTAMP(buffer), GST_BUFFER_DURATION(buffer), GST_BUFFER_OFFSET(buffer), GST_BUFFER_SIZE(buffer) );
- printf("timestamp = %lu \t duration = %lu \t offset = %lu \t size = %d \n",GST_BUFFER_TIMESTAMP(buffer), GST_BUFFER_DURATION(buffer), GST_BUFFER_OFFSET(buffer), GST_BUFFER_SIZE(buffer));
- //debugging
- if (!gst_bin_get_by_name( GST_BIN(pipeline1), app_sink_name))
- {
- g_print("app-sink não está disponível!\n");
- }
- if (!gst_bin_get_by_name( GST_BIN(pipeline2), app_src_name))
- {
- g_print("app-src não está disponível!\n");
- }
- //=======================================================================
- IMG_data = (uchar*) img->imageData;
- memcpy(IMG_data, GST_BUFFER_DATA(buffer), GST_BUFFER_SIZE(buffer));
- cvConvertImage(img,img,CV_CVTIMG_SWAP_RB);
- ////////////detectAndDraw(img);
- //========>PROCESS THE FRAME HERE<========
- // cvNamedWindow("FRAME BEIGN PROCESED",CV_WINDOW_KEEPRATIO);
- cvCircle(img,Point(350,350),50,Scalar(0,0,255),2,CV_AA,1);
- // cvShowImage("FRAME BEIGN PROCESED",img);
- cvConvertImage(img,img,CV_CVTIMG_SWAP_RB);
- memcpy(GST_BUFFER_DATA(buffer),IMG_data, GST_BUFFER_SIZE(buffer));
- gst_app_src_push_buffer( GST_APP_SRC( gst_bin_get_by_name(GST_BIN(pipeline2),app_src_name)) , buffer);
- return GST_FLOW_OK;
- }
- ///////////////////////////////////////////////////
- /////////////////////////////////////////////////// 5
- static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data)
- {
- gchar *userdata = (gchar *) data;
- switch(GST_MESSAGE_TYPE(msg))
- {
- case GST_MESSAGE_EOS:
- {
- if ( g_ascii_strcasecmp(userdata, gst_element_get_name(pipeline1)) == 0)
- gst_app_src_end_of_stream( GST_APP_SRC( gst_bin_get_by_name( GST_BIN(pipeline2), app_src_name ) ) );
- if ( g_ascii_strcasecmp(userdata, gst_element_get_name(pipeline2)) == 0)
- g_main_loop_quit(loop);
- break;
- }
- case GST_MESSAGE_ERROR:
- {
- gchar *debug;
- GError *error;
- gst_message_parse_error(msg, &error, &debug);
- g_free(debug);
- g_error_free(error);
- g_main_loop_quit(loop);
- break;
- }
- case GST_MESSAGE_STATE_CHANGED :
- {
- GstState oldstate;
- GstState newstate;
- GstState pending;
- gst_message_parse_state_changed (msg,&oldstate,&newstate,&pending);
- g_debug("pipeline:%s old:%s new:%s pending:%s", userdata,gst_element_state_get_name(oldstate),gst_element_state_get_name(newstate),gst_element_state_get_name(pending));
- break;
- }
- case GST_MESSAGE_WARNING:
- {
- gchar *debug;
- GError *error;
- gst_message_parse_warning (msg,&error,&debug);
- g_warning("pipeline:%s",userdata);
- g_warning("debug: %s", debug);
- g_warning("error: %s", error->message);
- g_free (debug);
- g_error_free (error);
- break;
- }
- default:
- break;
- }
- return TRUE;
- }
- ///////////////////////////////////////////////////
- int main(int argc, char **argv)
- {
- /////////////////////////////////////////////////// 6
- GError *error = NULL;
- GstBus *bus = NULL;
- GstAppSinkCallbacks callbacks;
- gchar pipeline1_str[556];
- gchar pipeline2_str[556];
- ///////////////////////////////////////////////////
- //#################################################
- char input_filename[556];
- //#################################################
- // strcpy(input_filename,argv[1]);
- strcpy(input_filename,"mynewstream");
- // mynewstream.flv
- //cvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcv
- img = cvCreateImage( cvSize(IMG_width,IMG_height), IPL_DEPTH_8U, 3);
- //cvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcvcv
- /////////////////////////////////////////////////// 7
- gst_init(&argc, &argv);
- loop = g_main_loop_new(NULL,FALSE);
- ///////////////////////////////////////////////////
- int res = 0;
- //need this for pipeline creation
- res = sprintf(pipeline1_str, "rtmpsrc location=\'rtmp://127.0.0.1:1935/flvplayback/%s live=1\' num-buffers=2147483647 ! queue ! decodebin2 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-rgb, width=%d, height=%d ! appsink name=%s ", input_filename,IMG_width, IMG_height, app_sink_name);
- //or need this for pipeline creation
- // res = sprintf(pipeline1_str, "filesrc location=\"/opt/lampp/htdocs/flvplayback/20051210-w50s.flv\" ! queue ! decodebin ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-rgb, width=%d, height=%d ! appsink name=\"%s\"",IMG_width, IMG_height, app_sink_name);
- // res = sprintf(pipeline1_str, "filesrc location=/home/dev/o.flv ! queue ! decodebin ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-rgb, width=%d, height=%d ! appsink name=%s",IMG_width, IMG_height, app_sink_name);
- if (res < 0)
- return -1;
- //for giving to rtmp i.e. live stream
- //res = sprintf(pipeline2_str, "appsrc name=\"%s\" is-live=1 num-buffers=2147483647 ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux ! rtmpsink location=\"rtmp://127.0.0.1/flvplayback/rtmp-%s\"", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height);
- // for storing in file i.e. filesink
- // res = sprintf(pipeline2_str,"appsrc name=\"%s\" ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! filesink location=\"/home/dev/rtmp-outputop.flv\"", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height);
- // res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! filesink location=/opt/lampp/htdocs/flvplayback/new%s.flv", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height,input_filename);
- // res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! rtmpsink location=\'rtmp://127.0.0.1/flvplayback/new%s live=1\'", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height,input_filename);
- res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! rtmpsink location=\'rtmp://127.0.0.1/flvplayback/rtmp-mymovie live=1\' ", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height);
- // rtmp-mymovie
- // printf(pipeline2_str);
- //aboce line make this program work but trying below now 24 aug 5 pm
- // res = sprintf(pipeline2_str,"appsrc name=%s ! queue ! videoparse format=14 width=%d height=%d framerate=25/1 ! videorate ! videoscale ! ffmpegcolorspace ! video/x-raw-yuv, width=%d, height=%d ! queue ! ffenc_flv ! flvmux streamable=true ! rtmpsink location=rtmp://127.0.0.1/flvplayback/new%s", app_src_name, IMG_width, IMG_height, IMG_width, IMG_height,input_filename);
- //above line doest work well right now
- if (res < 0)
- return -1;
- /////////////////////////////////////////////////// 8
- pipeline1 = gst_parse_launch(pipeline1_str, &error);
- if (error)
- return -1;
- pipeline2 = gst_parse_launch(pipeline2_str, &error);
- if (error)
- return -1;
- if (!gst_bin_get_by_name( GST_BIN(pipeline1), app_sink_name))
- return -1;
- if (!gst_bin_get_by_name( GST_BIN(pipeline2), app_src_name))
- return -1;
- bus = gst_pipeline_get_bus( GST_PIPELINE(pipeline1) );
- gst_bus_add_watch(bus, bus_call, gst_element_get_name(pipeline1));
- gst_object_unref(bus);
- bus = gst_pipeline_get_bus( GST_PIPELINE(pipeline2) );
- gst_bus_add_watch(bus, bus_call, gst_element_get_name(pipeline2));
- gst_object_unref(bus);
- ////putttonig cvNamedWindow here
- // cvNamedWindow("FRAME BEIGN PROCESED",CV_WINDOW_KEEPRATIO);
- callbacks.eos = NULL;
- callbacks.new_preroll = NULL;
- callbacks.new_buffer = new_buffer;
- gst_app_sink_set_callbacks( (GstAppSink*) gst_bin_get_by_name(GST_BIN(pipeline1), app_sink_name), &callbacks, NULL, NULL);
- gst_element_set_state(pipeline1, GST_STATE_PLAYING);
- gst_element_set_state(pipeline2, GST_STATE_PLAYING);
- g_main_loop_run(loop);
- gst_element_set_state(pipeline1, GST_STATE_NULL);
- gst_element_set_state(pipeline2, GST_STATE_NULL);
- gst_object_unref( GST_OBJECT(pipeline1) );
- gst_object_unref( GST_OBJECT(pipeline2) );
- ///////////////////////////////////////////////////
- cvReleaseImage(&img);
- /////////////////////////////////////////////////// 9
- g_main_loop_unref(loop);
- ///////////////////////////////////////////////////
- }
Add Comment
Please, Sign In to add comment