[vlc-devel] Start a VLC from a C-Code without any URL

Anna Richter anna.richter1 at gmx.net
Wed Feb 18 13:09:56 CET 2009


Hello everybody!

I habe the following problem: i need to implement a multimedia-streaming solution with the feature, that the server streams a video to a client and plays this video at the same time. VLC player is used to display the data. 
How I do this: 

Client-Side
on my client-pc is a program (vlcsocket) which creates a UDP socket, binds it to an IP-address and then let it join a multicast group. Then this program starts a new VLC-Client (using this source code: http://wiki.videolan.org/LibVLC_Tutorial ) and waits for the data from the server. The Videodata-URL is given to the program as a paratemeter, means the call to this programm looks like this: 
./vlcsocket rtsp://192.168.67.12:8556/newTest.mpg.
That works so far and the client socket receives and plays the videostream.

Server-Side

The server is waiting for a client connection. When a client request is received, the server starts streaming the videodata to the multicast IP-address. At the same time the video data has to be displayed on this server PC. 
For this task I have the following idea: parallel to the server another vlcsocket-program is running. This program is similar to that one on the client side, it also creates a new socket, binds it to an IP-address and let it join the multicast group. Then it should create a new VLC-Player and just wait for videodata from the server. And here my problem begins: first of all: I start this program independently from the server. So the program should just start and wait for the data. Second: the program doesn´t receive any data-url as a parameter. This is because this program should always play the same data, that is sent to the requesting client. That is a problem, because of the function m = libvlc_media_new (inst, movieurl, &ex); When I try to start the program without any url I receive the message: segmentation fault and the program exits. The other problem is that I don´t know how to make the program creating a new vlc-player-instance and then just wait for incoming data. It should do just as the official VLC player when you type "vlc" on the terminal: just be ready. I tried it with the recvfrom()-function, but the program just blocks and nothing happens. Below you can view the program code. It would be great, if you knew to help me.

Best wishes

Anna

P.S. Here comes the source code of the vlcsocket-program on the server-side:

#include <stdio.h>
#include <stdlib.h>
#include <vlc/vlc.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

#define RECEIVESIZE 10000

void DieWithError(char*errorMessage);

static void raise(libvlc_exception_t * ex)
{
    if (libvlc_exception_raised (ex))
    {
         fprintf (stderr, "error: %s\n", libvlc_exception_get_message(ex));
         exit (-1);
    }
}
int main(int argc, char* argv[])
{
	int sock; 				/*Socket descriptor*/
	struct sockaddr_in receiverAddr;	/*socket IP and port*/
	unsigned short receiverPort = 9000;	
	struct ip_mreq multicastRequest;	/*multicast group
	char *movieurl;
	struct sockaddr_in serverAddr;
	char receiveBuffer[RECEIVESIZE];
	int receiveBytes;	
	int serverSize;

	/*Create a socket*/
	if((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
	{
		DieWithError("Failed to create a socket");
	}

	memset(&receiverAddr, 0, sizeof(receiverAddr));
	receiverAddr.sin_family = AF_INET;
	receiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
	receiverAddr.sin_port = htons(receiverPort);

	/*Bind this socket to a IP-Adress and Port number*/
	if(bind(sock, (struct sockaddr*) &receiverAddr, sizeof(receiverAddr)) < 0)
	{
		DieWithError("bind() failed");
	}

	/*Specify the multicast group*/
	multicastRequest.imr_multiaddr.s_addr = inet_addr("224.1.2.3"); 
	multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);

	/*Join the multicast-group*/
	if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *) &multicastRequest, sizeof(multicastRequest)) < 0)
	{
		DieWithError("setsockopt() failed");
	}	
	serverSize = sizeof(serverAddr);
	receiveBytes = recvfrom(sock, receiveBuffer, RECEIVESIZE, 0, (struct sockaddr *) &serverAddr, &serverSize);
   const char * const vlc_args[] = {
              "-I", "dummy", /* Don't use any interface */
              "--ignore-config", /* Don't use VLC's config */
              "--plugin-path=/set/your/path/to/libvlc/module/if/you/are/on/windows/or/macosx" };
    if(argc == 2)
    {
	    movieurl=argv[1];
    }
    else
    {
	movieurl = NULL;
    }
	
    libvlc_exception_t ex;
    libvlc_instance_t * inst;
    libvlc_media_player_t *mp;
    libvlc_media_t *m;
    
    libvlc_exception_init (&ex);
    /* init vlc modules, should be done only once */
    inst = libvlc_new (sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args, &ex);
    raise (&ex);
 
    /* Create a new item */
    m = libvlc_media_new (inst, movieurl, &ex);
    raise (&ex);
   
    /* XXX: demo art and meta information fetching */
   
    /* Create a media player playing environement */
    mp = libvlc_media_player_new_from_media (m, &ex);
    raise (&ex);
    
    
    /* No need to keep the media now */
    libvlc_media_release (m);

#if 0
    /* This is a non working code that show how to hooks into a window,
     * if we have a window around */
     libvlc_drawable_t drawable = xdrawable;
    /* or on windows */
     libvlc_drawable_t drawable = hwnd;

     libvlc_media_player_set_drawable (mp, drawable, &ex);
     raise (&ex);
#endif

    /* play the media_player */
    libvlc_media_player_play (mp, &ex);
    raise (&ex);
   
    sleep (50); /*Let it play a bit */
   
    /* Stop playing */
    libvlc_media_player_stop (mp, &ex);

    /* Free the media_player */
    libvlc_media_player_release (mp);

    libvlc_release (inst);
    raise (&ex);

    return 0;
}

void DieWithError(char*errorMessage)
{
	perror(errorMessage);
	exit(1);
}

-- 
Jetzt 1 Monat kostenlos! GMX FreeDSL - Telefonanschluss + DSL 
für nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vlcsocket_new.c
Type: text/x-csrc
Size: 3641 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/vlc-devel/attachments/20090218/1e9600d5/attachment.c>


More information about the vlc-devel mailing list