C/C++ code emulate/call command line functions

This forum is about all development around libVLC.
erotavlas_turbo
Blank Cone
Blank Cone
Posts: 75
Joined: 26 Nov 2009 10:52

C/C++ code emulate/call command line functions

Postby erotavlas_turbo » 26 Nov 2009 17:50

Hi,

I'm a new user and i don't know if this is the right section of the forum. I would like to write a C/C++ code (i'm not a good programmer) to emulate/call the command line. I would like to call vlc with the following parameters:

Code: Select all

vlc -vv video.avi --rtsp-caching=1200 --sout '#transcode{vcodec=H263,vb=800,width=352,height=288,acodec=mp3,ab=128,channels=2,samplerate=44100}:duplicate{dst=display,dst=rtp{rtsp://10.36.1.12:8080/test.sdp}}'
If the code/example don't exist, can you direct me to documention/examples?

Thank you very much

Rémi Denis-Courmont
Developer
Developer
Posts: 15280
Joined: 07 Jun 2004 16:01
VLC version: master
Operating System: Linux
Contact:

Re: C/C++ code emulate/call command line functions

Postby Rémi Denis-Courmont » 26 Nov 2009 21:10

It's not very difficult is it?

Code: Select all

#include <spawn.h> extern char **environ; /* ... */ int status; pid_t pid; char *args[6]; args[0] = "vlc"; args[1] = "-vv"; args[2] = "video.avi"; args[3] = "--rtsp-caching=1200" args[4] = "--sout=#transcode{vcodec=H263,vb=800,width=352,height=288," "acodec=mp3,ab=128,channels=2,samplerate=44100}" ":duplicate{dst=display,dst=rtp{rtsp://10.36.1.12:8080/test.sdp}}"; args[5] = NULL; posix_spawnp(&pid, "vlc", NULL, NULL, args, environ); /* ... */ while (waitpid(pid, &status, 0) == -1);
Rémi Denis-Courmont
https://www.remlab.net/
Private messages soliciting support will be systematically discarded

erotavlas_turbo
Blank Cone
Blank Cone
Posts: 75
Joined: 26 Nov 2009 10:52

Re: C/C++ code emulate/call command line functions

Postby erotavlas_turbo » 03 Dec 2009 18:23

Hi,

thank your for your post. It work but only local, if i try to play the stream it doesn't work. Why? With the same command line i can see the video.

Code: Select all

vlc rtsp://ip:port/test.sdp
Where can i find documentation about call VLC?or other programm?i'm a "young" programmer.

Thank

erotavlas_turbo
Blank Cone
Blank Cone
Posts: 75
Joined: 26 Nov 2009 10:52

Re: C/C++ code emulate/call command line functions

Postby erotavlas_turbo » 04 Dec 2009 15:28

Hi,

If i try to call VLC with a system() the streaming is ok and i can see it from the other PC over the network.

Code: Select all

system("vlc -vv campione2.jpg --sout '#transcode{vcodec=H263p,vb=800,width=352,height=288}:duplicate{dst=display,dst=rtp{sdp=rtsp://10.36.1.23:8080/test.sdp},dst=rtp{dst=192.168.0.4,port=1234,mux=ts}}'");
If i try to call VLC with your part of code the streaming is ok, but i can't see it from the other PC over the network.


What is the problem?If possibile i prefer your solution because i can change the streaming file on the fly.

Thank

erotavlas_turbo
Blank Cone
Blank Cone
Posts: 75
Joined: 26 Nov 2009 10:52

Re: C/C++ code emulate/call command line functions

Postby erotavlas_turbo » 02 Apr 2010 10:55

Hi,

I have reopened this thread for a new problem :).

I have learned how to call VLC program with many parameters from C code with a system() function. I can say that it works perfectly.

Now I'm trying to use fork() + execv() instead of system() function.

The code is the following:

Code: Select all

#define _POSIX_SOURCE /* for kill() */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <net/if.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <signal.h> #include <unistd.h> /* for fork, exec, kill */ #include <sys/types.h> /* for pid_t */ #include <sys/wait.h> /* for waitpid */ /* * fork a child process, execute vlc, and return it's pid. * returns -1 if fork failed. */ pid_t callVLC(char* outputImage, char* IP, char ports[2][10]) { char* args[10]; args[0] = " -I rc "; args[1] = outputImage; args[2] = " --fake-duration 7200000 --sout '#transcode{vcodec=H263p,width=352,height=288,acodec=none}:duplicate{dst=rtp{dst="; args[3] = IP; args[4] = ",port-audio="; args[5] = ports[0]; args[6] = ",port-video="; args[7] = ports[1]; //args[8] = ",sdp=file:///home/tore/Scrivania/appconference_modificato/temp/file.sdp}}' vlc://quit &"; args[8] = "}}' "; args[9] = NULL; pid_t processID = fork(); if (processID == -1) { perror("fork"); return -1; } if (processID == 0) { //execvp("vlc", args); execv("/usr/bin/vlc", args); perror("vlc"); abort(); } else { /* parent, return the child's PID back to main. */ printf("execv vlc: PID %d\n", processID); //ast_log(LOG_NOTICE, "execv vlc: PID %d\n", processID); return processID; } } int main() { char * outputImage = "image.jpg"; char* IP = "192.168.0.4"; char ports[2][10]; pid_t processID = callVLC(outputImage, IP, ports); if (processID == -1) { fprintf(stderr, "failed to fork child process\n"); return 0; } printf("spawned vlc with pid %d\n", processID); sleep(30); /* kill will send the specified signal to the specified process. * in this case, we send a TERM signal to VLC, requesting that it * terminate. If that doesn't work, we send a KILL signal. * If that doesn't work, we give up. */ if (kill(processID, SIGTERM) < 0) { perror("kill with SIGTERM"); if (kill(processID, SIGKILL) < 0) { perror("kill with SIGKILL"); } } /* this shows how we can get the exit status of our child process. * it will wait for the the VLC process to exit, then grab it's return * value. */ int status = 0; waitpid(processID, &status, 0); printf("VLC exited with status %d\n", WEXITSTATUS(status)); return 0; }

With the function execv("usr/bin/vlc", args) the code works well while with execv("usr/bin/vlc-wrapper", args) doesn't work.

I need to use the code as root user. How can I solve the problem?

Thank you


Return to “Development around libVLC”

Who is online

Users browsing this forum: No registered users and 46 guests