I have ran this code many times trying to figure out why it doesn't work. I am getting the feed from a IP camera. The program goes through each but then it fails to play. The original start out was gst-launch-1.0 udpsrc address=[ip] port=[port] ! application/x-rtp,clock-rate=10,media=video ! rtpmp4vdepay ! decodebin ! d3dvideosink
I used gst_parse_launch() with it and works but for some reason won't play. I have been trying figure out what I am doing wrong.
convertString.h just throws the Char *port to a method to convert string to int.I don't want to use the gst_parse_launch() because I am wanting to use it to overlay into application.
Anybody have any idea what I am missing?
#include <gst/gst.h>
#include <gst/video/videooverlay.h>
#include <stdio.h>
#include <string.h>
#include "convertString.h"
#include <Windows.h>
#include <direct.h>
void nullvalues(GstElement*,GstBus*,GstMessage*);
int main(int argc, char *argv[])
{
GstElement *pipeline,*sink,*source, *rtppay,*filter1,*decodebin;
GstCaps *filtercaps;
GstBus *bus;
GstMessage *msg;
gboolean link_ok = FALSE;
GstStateChangeReturn ret;
char *ip_address = [ip];
char *port = [port];
char *window_handle = "";
char *title = "";
int wid;
char *sourcestring = "";
#define url_size 28
char url[url_size];
strcpy_s(url,url_size,"");
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'i':
ip_address = &argv[1][2];
break;
case 'p':
port = &argv[1][2];
break;
case 'w':
window_handle = &argv[1][2];
break;
case 't':
title = &argv[1][2];
break;
}
++argv;
--argc;
}
strcat_s(url,url_size,"udp://");
strcat_s(url,url_size,ip_address);
strcat_s(url,url_size,":");
strcat_s(url,url_size,port);
/* Initialize GStreamer */
gst_init(&argc,&argv);
/* Build Pipeline */
if(title != "")
pipeline = gst_pipeline_new (title);
else
pipeline = gst_pipeline_new("My pipeline");
source = gst_element_factory_make ("udpsrc","source");
filter1 = gst_element_factory_make("capsfilter","filter");
rtppay = gst_element_factory_make( "rtpmp4vdepay", "depayl");
decodebin = gst_element_factory_make ("decodebin","decode");
sink = gst_element_factory_make ("d3dvideosink", "sink");
gst_bin_add_many (GST_BIN (pipeline),source,filter1,rtppay,decodebin,sink, NULL);
filtercaps = gst_caps_new_simple("application/x-rtp","clock-rate",G_TYPE_INT,10,"media",G_TYPE_STRING,"video",NULL);
g_object_set(GST_OBJECT(source),"address",ip_address,NULL);
g_object_set(GST_OBJECT(source),"port",StringToInt(port),NULL);
g_object_set (G_OBJECT (filter1), "caps",filtercaps,NULL);
gst_caps_unref(filtercaps);
link_ok = gst_element_link_many(source,filter1,rtppay,decodebin,sink);
if(!link_ok)
printf("%s\nNOPE\n");
/* Start playing */
ret = gst_element_set_state (pipeline, GST_STATE_READY);
if(ret == GST_STATE_CHANGE_FAILURE)
printf("\nFailed\n");
else if(ret == GST_STATE_CHANGE_SUCCESS)
printf("\nSuccess\n");
/* Wait until error or EOS */
bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered(bus,GST_CLOCK_TIME_NONE,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);
printf("\nDone\n");
return 0;
}
If gst_parse_launch() is working for you I suggest to use it. You can still overlay the d3dvideosink in your application.
Just use a unique name for the d3dvideosink element in the pipeline definition (e.g. "d3dvideosink name=myvideosink").
Get a handle to the d3dvideosink from the pipeline via gst_bin_get_by_name(). This handle should then also support the GstVideoOverlay interface and you can set the overlay via gst_video_overlay_set_window_handle().
using this will solve the problem
Gstreamer Decodebin
Related
My pipeline is like this
gst-launch-1.0 v4l2src ! videoconvert ! xvimagesink
and my code is like this
#include <gst/gst.h>
// easier to pass them as callbacks
typedef struct _CustomData{
GstElement *pipeline;
GstElement *source;
GstElement *convert;
GstElement *sink;
}CustomData;
// callback function
// here src is the v4l2src, newpad is gstpad that has just been added to src element. This is usually the pad to which we want to lnk
// data is the pointer we provided when attaching to the signal.
static void pad_added_handler(GstElement *src, GstPad *new_pad,CustomData *data)
{
GstPad *sink_pad = gst_element_get_static_pad(data->convert, "sink");
GstPadLinkReturn ret;
GstCaps *new_pad_caps = NULL;
GstStructure *new_pad_struct = NULL;
const gchar *new_pad_type = NULL;
if(gst_pad_is_linked(sink_pad))
{
g_print("we are linked. igonring\n");
}
// check the new pad types
// we have previously created a piece of pipeline which deals with videoconvert linked with xvimagesink and we will nto be able to link it to a pad producing video.
//gst-pad_get_current_caps()- retrieves current capabilities of pad
new_pad_caps = gst_pad_get_current_caps(new_pad);
new_pad_struct = gst_caps_get_structure(new_pad_caps, 0);
new_pad_type = gst_structure_get_name(new_pad_struct);
if(!g_str_has_prefix(new_pad_type, "video/x-raw"))
{
g_print("It has new pad type");
}
// gst_pad_link tries to link two pads . the link must be specified from source to sink and both pads must be owned by elements residing in same pipeline
ret = gst_pad_link(new_pad, sink_pad);
if(GST_PAD_LINK_FAILED(ret))
{
g_print("type is new_pad_type");
}
if(new_pad_caps !=NULL)
{
gst_caps_unref(new_pad_caps);
}
gst_object_unref(sink_pad);
}
int main(int argc, char *argv[])
{
GMainLoop *loop;
CustomData data;
GstBus *bus;
GstMessage *msg;
gboolean terminate = FALSE;
gst_init(&argc, &argv);
// loop = g_main_loop_new(NULL, FALSE);
// create the elements
data.source = gst_element_factory_make("v4l2src", "source");
data.convert = gst_element_factory_make("videoconvert", "convert");
data.sink = gst_element_factory_make("xvimagesink", "sink");
data.pipeline = gst_pipeline_new("new-pipeline");
if(!data.pipeline || !data.source || !data.convert || !data.sink)
{
g_printerr("Not all elements could be created\n");
return -1;
}
//we did not link source at this point of time, we will do it later
gst_bin_add_many(GST_BIN(data.pipeline), data.source, data.convert, data.sink, NULL);
// we link convert element to sink, do not link them with source. we dont have source pads here. so we just have videoconvert->sink unlinked
// gst_element_link(data.source, data.convert);
if(!gst_element_link(data.convert,data.sink))
{
g_printerr("elements could not be linked\n");
gst_object_unref(data.pipeline);
return -1;
}
// we set the device source
//g_object_set(source, "device", "/dev/video0", NULL);
//connect to pad added signal.
// we want to attach pad added signal to source element. to do so, we are using g_signal_connect and provide callback function and datapointer.
// when source element has enough information to start producing data, it will create source pads and trigger the pad added signal. at this point, our callback is called
g_signal_connect(G_OBJECT(data.source), "pad-added", G_CALLBACK(pad_added_handler), &data );
//g_signal_connect(G_OBJECT(data.source), "pad-added", G_CALLBACK(handler), &data);
GstStateChangeReturn ret;
ret =gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
if (ret == GST_STATE_CHANGE_FAILURE) {
g_printerr ("Unable to set the pipeline to the playing state.\n");
gst_object_unref (data.pipeline);
return -1;
}
// g_main_loop_run(loop);
/* Listen to the bus */
bus = gst_element_get_bus (data.pipeline);
do {
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
/* Parse message */
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg, &err, &debug_info);
g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
terminate = TRUE;
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
terminate = TRUE;
break;
case GST_MESSAGE_STATE_CHANGED:
/* We are only interested in state-changed messages from the pipeline */
if (GST_MESSAGE_SRC (msg) == GST_OBJECT (data.pipeline)) {
GstState old_state, new_state, pending_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, &pending_state);
g_print ("Pipeline state changed from %s to %s:\n",
gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));
}
break;
default:
/* We should not reach here */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
} while (!terminate);
/* Free resources */
gst_object_unref (bus);
gst_element_set_state(data.pipeline, GST_STATE_NULL);
gst_object_unref(data.pipeline);
return 0;
}
and I am getting error like this
Pipeline state changed from NULL to READY:
Pipeline state changed from READY to PAUSED:
Error received from element source: Internal data stream error.
Debugging information: gstbasesrc.c(3055): gst_base_src_loop (): /GstPipeline:new-pipeline/GstV4l2Src:source:
streaming stopped, reason not-linked (-1)
Please let me know what changes should I make to make my pipeline work. Thanks! the above code is based on dynamic pipeline example from gstreamer tutorials. I dont understand where I am going wrong.
The following works though
#include <gst/gst.h>
int main(int argc, char *argv[])
{
GstElement *pipeline, *source,*filter, *convert, *sink;
GstBus *bus;
GstMessage *msg;
GstCaps *caps;
gst_init(&argc, &argv);
source = gst_element_factory_make("v4l2src", "source");
filter = gst_element_factory_make("capsfilter","filter");
convert = gst_element_factory_make("videoconvert", "convert");
sink = gst_element_factory_make("xvimagesink", "sink");\
pipeline = gst_pipeline_new("pipe");
gst_bin_add_many(GST_BIN(pipeline), source, convert,sink, NULL);
gst_element_link_many(source,convert,sink,NULL);
caps = gst_caps_new_simple("video/x-raw", "format", G_TYPE_STRING, "YUY2", NULL);
g_object_set(G_OBJECT(filter), "caps", caps, NULL);
gst_element_set_state(pipeline,GST_STATE_PLAYING);
bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
/* Parse message */
if (msg != NULL) {
GError *err;
gchar *debug_info;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ERROR:
gst_message_parse_error (msg, &err, &debug_info);
g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
g_clear_error (&err);
g_free (debug_info);
break;
case GST_MESSAGE_EOS:
g_print ("End-Of-Stream reached.\n");
break;
default:
/* We should not reach here because we only asked for ERRORs and EOS */
g_printerr ("Unexpected message received.\n");
break;
}
gst_message_unref (msg);
}
/* Free resources */
gst_object_unref(bus);
gst_element_set_state(pipeline,GST_STATE_NULL);
gst_object_unref(pipeline);
}
Any ideas why if I add pads, it is not working well??
How can I capture 802.11 beacon frames on my linux machine using a C program.Also how can I send a frame response using a C program?
Try using libpcap. See this answer on SO for a basic example.
below program only work :-
1. if your device support link layer type as DLT_IEEE802_11_RADIO
2. if you ahev libpacp installed in your machine.
3. compile it with libcap library( ex:- cc prog.c -lpcap
4. during run provide interface on command line ( ex:- a.out wlan0)
#include <pcap.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void packet_view(unsigned char *args,const struct pcap_pkthdr *h,const unsigned char *p);
#define SNAP_LEN 3000
void packet_view(
unsigned char *args,
const struct pcap_pkthdr *h,
const unsigned char *p
){
int len;
len = 0;
printf("PACKET\n");
while(len < h->len) {
printf("%02x ", *(p++));
if(!(++len % 16))
printf("\n");
}
printf("\n");
return ;
}
int main(int argc, char **argv)
{
char *dev = NULL; /* capture device name */
char errbuf[PCAP_ERRBUF_SIZE]; /* error buffer */
pcap_t *handle; /* packet capture handle */
char filter_exp[] = "wlan type mgt subtype beacon"; /* filter expression [3] */
// char filter_exp[] = "ip"; /* filter expression [3] */
struct bpf_program fp; /* compiled filter program (expression) */
bpf_u_int32 mask; /* subnet mask */
bpf_u_int32 net; /* ip */
int num_packets = 10; /* number of packets to capture */
/* check for capture device name on command-line */
if (argc == 2) {
dev = argv[1];
}
else if (argc > 2) {
fprintf(stderr, "error: unrecognized command-line options\n\n");
exit(EXIT_FAILURE);
}
else {
/* find a capture device if not specified on command-line */
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n",
errbuf);
exit(EXIT_FAILURE);
}
}
/* get network number and mask associated with capture device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n",
dev, errbuf);
net = 0;
mask = 0;
}
/* print capture info */
printf("Device: %s\n", dev);
printf("Number of packets: %d\n", num_packets);
printf("Filter expression: %s\n", filter_exp);
/* open capture device */
handle = pcap_open_live(dev, SNAP_LEN, 1, -1, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
exit(EXIT_FAILURE);
}
/* for Ethernet device change the type to DLT_EN10MB */
/* your wlan device need to supprot this link layer type otherwise failure */
if (pcap_datalink(handle) != DLT_IEEE802_11_RADIO) {
fprintf(stderr, "%s is not an Wlan packet\n", dev);
exit(EXIT_FAILURE);
}
/* compile the filter expression */
if (pcap_compile(handle, &fp, filter_exp, 1,PCAP_NETMASK_UNKNOWN) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n",
filter_exp, pcap_geterr(handle));
exit(EXIT_FAILURE);
}
/* apply the compiled filter */
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n %s: %s\n",
filter_exp, pcap_geterr(handle));
exit(EXIT_FAILURE);
}
/* now we can set our callback function */
pcap_loop(handle, num_packets, packet_view, NULL);
/* cleanup */
pcap_freecode(&fp);
pcap_close(handle);
printf("\nCapture complete.\n");
return 0;
}
I'm trying to run one of the examples using FLTK in Mathgl. While basic programs will compile without error, I seem to be getting a linker error when compiling FLTK_MathGL examples.
This is the error portion I get from my build log.
C:\mathgl-2.3\src\libmgl.dll.a C:\mathgl-2.3\widgets\libmgl-fltk.a -mwindows
obj\Release\main.o:main.cpp:(.text$_ZN7mglFLTK3RunEv[__ZN7mglFLTK3RunEv]+0x1): undefined reference to "_imp__mgl_fltk_run'
obj\Release\main.o:main.cpp:(.text$_ZN7mglFLTKC1EPFiP8mglGraphEPKc[__ZN7mglFLTKC1EPFiP8mglGraphEPKc]+0x39): undefined reference to "_imp___ZTV7mglFLTK'
c:/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: obj\Release\main.o: bad reloc address 0x39 in section ".text$_ZN7mglFLTKC1EPFiP8mglGraphEPKc[__ZN7mglFLTKC1EPFiP8mglGraphEPKc]'
c:/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 3 second(s))
2 error(s), 0 warning(s) (0 minute(s), 3 second(s))
I'm not sure what else I can do. I have pasted the program below.
#define MGL_HAVE_FLTK
#include "mgl2/fltk.h"
//-----------------------------------------------------------------------------
#if defined(WIN32) || defined(_MSC_VER) || defined(__BORLANDC__)
#include <windows.h>
#else
#include <unistd.h>
#endif
void long_calculations() // just delay which correspond to simulate calculations
{
#if defined(WIN32) || defined(_MSC_VER) || defined(__BORLANDC__)
Sleep(1000);
#else
sleep(1); // which can be very long
#endif
}
//-----------------------------------------------------------------------------
#if defined(PTHREAD_SAMPLE1) // first variant of multi-threading usage of mglFLTK window
mglFLTK *gr=NULL;
void *calc(void *)
{
mglPoint pnt;
for(int i=0;i<10;i++) // do calculation
{
long_calculations(); // which can be very long
pnt = mglPoint(2*mgl_rnd()-1,2*mgl_rnd()-1);
if(gr)
{
gr->Clf(); // make new drawing
gr->Line(mglPoint(),pnt,"Ar2");
char str[10] = "i=0"; str[2] = '0'+i;
gr->Puts(mglPoint(),str);
gr->Update(); // update window
}
}
exit(0);
}
int main(int argc,char **argv)
{
static pthread_t thr;
pthread_create(&thr,0,calc,0);
pthread_detach(thr);
gr = new mglFLTK;
gr->Run(); return 0;
}
#elif defined(PTHREAD_SAMPLE2) // another variant of multi-threading usage of mglFLTK window. Work only if pthread was enabled for MathGL
mglPoint pnt; // some global variable for changeable data
int main(int argc,char **argv)
{
mglFLTK gr("test");
gr.RunThr(); // <-- need MathGL version which use pthread
for(int i=0;i<10;i++) // do calculation
{
long_calculations();// which can be very long
pnt = mglPoint(2*mgl_rnd()-1,2*mgl_rnd()-1);
gr.Clf(); // make new drawing
gr.Line(mglPoint(),pnt,"Ar2");
char str[10] = "i=0"; str[3] = '0'+i;
gr.Update(); // update window
}
return 0; // finish calculations and close the window
}
#else // just default samples
int test_wnd(mglGraph *gr);
int sample(mglGraph *gr);
int sample_1(mglGraph *gr);
int sample_2(mglGraph *gr);
int sample_3(mglGraph *gr);
int sample_d(mglGraph *gr);
//-----------------------------------------------------------------------------
int main(int argc,char **argv)
{
mglFLTK *gr;
char key = 0;
if(argc>1) key = argv[1][0]!='-' ? argv[1][0]:argv[1][1];
else printf("You may specify argument '1', '2', '3' or 'd' for viewing examples of 1d, 2d, 3d or dual plotting\n");
switch(key)
{
case '0': gr = new mglFLTK((mglDraw *)NULL,"1D plots"); break;
case '1': gr = new mglFLTK(sample_1,"1D plots"); break;
case '2': gr = new mglFLTK(sample_2,"2D plots"); break;
case '3': gr = new mglFLTK(sample_3,"3D plots"); break;
case 'd': gr = new mglFLTK(sample_d,"Dual plots");break;
case 't': gr = new mglFLTK(test_wnd,"Testing"); break;
default: gr = new mglFLTK(sample,"Drop and waves"); break;
}
if(key=='0')
{ gr->Rotate(40,60); gr->Box(); gr->Light(true); gr->FSurf("sin(4*pi*x*y)"); gr->Update(); }
gr->Run(); return 0;
}
#endif
//-----------------------------------------------------------------------------
I want to use execvp to run commands through my program. The user is prompted for a command (exits on eof).
Once the program has a command it forks a child process to process the command while the parent waits for the child to finish.
I'm tokenizing the input to store it in a char* array which is kept track of by variable 'i'.
Except 'i' keeps changing its value with each iteration of the while loop.
sample input: /bin/ls -l
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#define BUFFER 1024
int main(){
pid_t p;
char* paramList[] = {};
char input[BUFFER];
int i = 0;
char* segments;
printf(">");
while(fgets(input, BUFFER, stdin) != NULL){
if((p = fork()) == 0){
printf("Executing: %s\n", input);
i = 0;
segments = strtok(input, " ");
paramList[i] = segments;
printf("%s%d\n", paramList[i], i);
i++;
while(segments != NULL){
segments = strtok(NULL, " ");
paramList[i] = segments;
printf("%s%d\n", segments, i);
i++;
}
paramList[i] = NULL;
execvp(paramList[0], paramList);
}else{
printf(">");
waitpid(p, NULL, 0);
}
}
return 0;
}
You're not declaring a size for paramList, but you're giving it an empty initializer list; thus paramList has zero elements. And then you're writing more than zero elements into it, overflowing onto other local variables (like i).
i am still new on opencv, i make simple program based on sample to access webcam but always fails. i change variable id to 0,1,2...100 but i got same result. this is my program:
#include "cv.h"
#include "highgui.h"
#include "stdio.h"
#include "iostream"
// A Simple Camera Capture Framework
int main()
{
IplImage* img = NULL;
CvCapture* cap = NULL;
int id=0;
cap = cvCaptureFromCAM(id);
cvNamedWindow("Images",CV_WINDOW_AUTOSIZE);
if ( !cap )
printf("ERROR\n\n");
else
for(;;)
{
img = cvQueryFrame(cap);
cvShowImage("Imagenes", img);
cvWaitKey(10);
}
cvReleaseImage(&img);
cvReleaseCapture(&cap);
return 0;
}
thank you for your help
Do yourself a favor and check the return of the functions. Maybe some of them are failing and you'll never know why.
Another tip: try with id = -1.
#include <iostream>
#include <sstream>
#include <string>
#include <cv.h>
#include <highgui.h>
int main()
{
CvCapture* capture = NULL;
if ((capture = cvCaptureFromCAM(-1)) == NULL)
{
fprintf(stderr, "ERROR: capture is NULL \n");
return -1;
}
cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE);
cvQueryFrame(capture); // Sometimes needed to get correct data
IplImage* frame = NULL;
while (1)
{
if ((frame = cvQueryFrame(capture)) == NULL)
{
fprintf( stderr, "ERROR: cvQueryFrame failed\n");
break;
}
if (frame == NULL)
{
usleep(100000);
continue;
}
cvShowImage("mywindow", frame); // Do not release the frame!
int key = cvWaitKey(10);
if (key == 27) // ESC was pressed
break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("mywindow");
return 0;
}